xref: /DragonOS/kernel/src/syscall/syscall.c (revision aa0367d69e15989684109c5b454e85da9ecb1975)
1 #include "syscall.h"
2 #include <common/errno.h>
3 #include <common/fcntl.h>
4 #include <common/kthread.h>
5 #include <common/string.h>
6 #include <driver/disk/ahci/ahci.h>
7 #include <exception/gate.h>
8 #include <exception/irq.h>
9 #include <filesystem/vfs/VFS.h>
10 #include <mm/slab.h>
11 #include <process/process.h>
12 #include <time/sleep.h>
13 // 导出系统调用入口函数,定义在entry.S
14 extern void syscall_int(void);
15 
16 /**
17  * @brief 重新定义为:把系统调用函数加入系统调用表
18  * @param syscall_num 系统调用号
19  * @param symbol 系统调用处理函数
20  */
21 #define SYSCALL_COMMON(syscall_num, symbol) [syscall_num] = symbol,
22 
23 /**
24  * @brief 通过中断进入系统调用
25  *
26  * @param syscall_id
27  * @param arg0
28  * @param arg1
29  * @param arg2
30  * @param arg3
31  * @param arg4
32  * @param arg5
33  * @param arg6
34  * @param arg7
35  * @return long
36  */
37 
38 long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
39 {
40     long err_code;
41     __asm__ __volatile__("movq %2, %%r8 \n\t"
42                          "movq %3, %%r9 \n\t"
43                          "movq %4, %%r10 \n\t"
44                          "movq %5, %%r11 \n\t"
45                          "movq %6, %%r12 \n\t"
46                          "movq %7, %%r13 \n\t"
47                          "movq %8, %%r14 \n\t"
48                          "movq %9, %%r15 \n\t"
49                          "int $0x80   \n\t"
50                          : "=a"(err_code)
51                          : "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5), "m"(arg6),
52                            "m"(arg7)
53                          : "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rcx", "rdx");
54 
55     return err_code;
56 }
57 
58 /**
59  * @brief 打印字符串的系统调用
60  *
61  * 当arg1和arg2均为0时,打印黑底白字,否则按照指定的前景色和背景色来打印
62  *
63  * @param regs 寄存器
64  * @param arg0 要打印的字符串
65  * @param arg1 前景色
66  * @param arg2 背景色
67  * @return ul 返回值
68  */
69 ul do_put_string(char *s, uint32_t front_color, uint32_t background_color)
70 {
71 
72     printk_color(front_color, background_color, s);
73     return 0;
74 }
75 
76 /**
77  * @brief 将堆内存调整为arg0
78  *
79  * @param arg0 新的堆区域的结束地址
80  * @return uint64_t 错误码
81  *
82  */
83 uint64_t sys_do_brk(uint64_t newaddr)
84 {
85     uint64_t new_brk = PAGE_2M_ALIGN(newaddr);
86     // kdebug("sys_brk input= %#010lx ,  new_brk= %#010lx bytes current_pcb->mm->brk_start=%#018lx
87     // current->end_brk=%#018lx", regs->r8, new_brk, current_pcb->mm->brk_start, current_pcb->mm->brk_end);
88     struct mm_struct *mm = current_pcb->mm;
89     if (new_brk < mm->brk_start || new_brk > new_brk >= current_pcb->addr_limit)
90         return mm->brk_end;
91 
92     if (mm->brk_end == new_brk)
93         return new_brk;
94 
95     int64_t offset;
96     if (new_brk >= current_pcb->mm->brk_end)
97         offset = (int64_t)(new_brk - current_pcb->mm->brk_end);
98     else
99         offset = -(int64_t)(current_pcb->mm->brk_end - new_brk);
100 
101     new_brk = mm_do_brk(current_pcb->mm->brk_end, offset); // 扩展堆内存空间
102 
103     current_pcb->mm->brk_end = new_brk;
104     return mm->brk_end;
105 }
106 
107 /**
108  * @brief 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程)
109  *
110  * @param incr offset偏移量
111  * @return uint64_t the previous program break
112  */
113 uint64_t sys_do_sbrk(int64_t incr)
114 {
115     uint64_t retval = current_pcb->mm->brk_end;
116     if ((int64_t)incr > 0)
117     {
118 
119         uint64_t new_brk = PAGE_2M_ALIGN(retval + incr);
120         if (new_brk > current_pcb->addr_limit) // 堆地址空间超过限制
121         {
122             kdebug("exceed mem limit, new_brk = %#018lx", new_brk);
123             return -ENOMEM;
124         }
125     }
126     else
127     {
128         if ((__int128_t)current_pcb->mm->brk_end + (__int128_t)incr < current_pcb->mm->brk_start)
129             return retval;
130     }
131     // kdebug("do brk");
132     uint64_t new_brk = mm_do_brk(current_pcb->mm->brk_end, (int64_t)incr); // 调整堆内存空间
133     // kdebug("do brk done, new_brk = %#018lx", new_brk);
134     current_pcb->mm->brk_end = new_brk;
135     return retval;
136 }
137 
138 /**
139  * @brief 执行新的程序
140  *
141  * @param user_path(r8寄存器) 文件路径
142  * @param argv(r9寄存器) 参数列表
143  * @return uint64_t
144  */
145 uint64_t c_sys_execve(char *user_path, char **argv, char **envp, struct pt_regs *regs)
146 {
147 
148     int path_len = strnlen_user(user_path, PAGE_4K_SIZE);
149 
150     if (path_len >= PAGE_4K_SIZE)
151         return -ENAMETOOLONG;
152     else if (path_len <= 0)
153         return -EFAULT;
154 
155     char *path = (char *)kmalloc(path_len + 1, 0);
156     if (path == NULL)
157         return -ENOMEM;
158 
159     memset(path, 0, path_len + 1);
160 
161     // 拷贝文件路径
162     strncpy_from_user(path, user_path, path_len);
163     path[path_len] = '\0';
164 
165     // 执行新的程序
166     uint64_t retval = do_execve(regs, path, argv, NULL);
167 
168     kfree(path);
169     return retval;
170 }
171 
172 /**
173  * @brief 等待进程退出
174  *
175  * @param pid 目标进程id
176  * @param status 返回的状态信息
177  * @param options 等待选项
178  * @param rusage
179  * @return uint64_t
180  */
181 uint64_t c_sys_wait4(pid_t pid, int *status, int options, void *rusage)
182 {
183 
184     struct process_control_block *proc = NULL;
185     struct process_control_block *child_proc = NULL;
186 
187     // 查找pid为指定值的进程
188     // ps: 这里判断子进程的方法没有按照posix 2008来写。
189     // todo: 根据进程树判断是否为当前进程的子进程
190     // todo: 当进程管理模块拥有pcblist_lock之后,调用之前,应当对其加锁
191     child_proc = process_find_pcb_by_pid(pid);
192 
193     if (child_proc == NULL)
194         return -ECHILD;
195 
196     // 暂时不支持options选项,该值目前必须为0
197     if (options != 0)
198         return -EINVAL;
199 
200     // 如果子进程没有退出,则等待其退出
201     // BUG: 这里存在问题,由于未对进程管理模块加锁,因此可能会出现子进程退出后,父进程还在等待的情况
202     // (子进程退出后,process_exit_notify消息丢失)
203     while (child_proc->state != PROC_ZOMBIE)
204         wait_queue_sleep_on_interriptible(&current_pcb->wait_child_proc_exit);
205 
206     // 拷贝子进程的返回码
207     if (likely(status != NULL))
208         *status = child_proc->exit_code;
209     // copy_to_user(status, (void*)child_proc->exit_code, sizeof(int));
210 
211     process_release_pcb(child_proc);
212     return 0;
213 }
214