xref: /DragonOS/kernel/src/syscall/syscall.c (revision 8d94ea66a3eb3e02039730c8d08e9bead8c344b8)
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 等待进程退出
78  *
79  * @param pid 目标进程id
80  * @param status 返回的状态信息
81  * @param options 等待选项
82  * @param rusage
83  * @return uint64_t
84  */
85 uint64_t c_sys_wait4(pid_t pid, int *status, int options, void *rusage)
86 {
87 
88     struct process_control_block *proc = NULL;
89     struct process_control_block *child_proc = NULL;
90 
91     // 查找pid为指定值的进程
92     // ps: 这里判断子进程的方法没有按照posix 2008来写。
93     // todo: 根据进程树判断是否为当前进程的子进程
94     // todo: 当进程管理模块拥有pcblist_lock之后,调用之前,应当对其加锁
95     child_proc = process_find_pcb_by_pid(pid);
96 
97     if (child_proc == NULL)
98         return -ECHILD;
99 
100     // 暂时不支持options选项,该值目前必须为0
101     if (options != 0)
102         return -EINVAL;
103 
104     // 如果子进程没有退出,则等待其退出
105     // BUG: 这里存在问题,由于未对进程管理模块加锁,因此可能会出现子进程退出后,父进程还在等待的情况
106     // (子进程退出后,process_exit_notify消息丢失)
107     while (child_proc->state != PROC_ZOMBIE)
108         wait_queue_sleep_on_interriptible(&current_pcb->wait_child_proc_exit);
109 
110     // 拷贝子进程的返回码
111     if (likely(status != NULL))
112         *status = child_proc->exit_code;
113     // copy_to_user(status, (void*)child_proc->exit_code, sizeof(int));
114 
115     process_release_pcb(child_proc);
116     return 0;
117 }
118