syscall.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #include "syscall.h"
  2. #include "../process/process.h"
  3. #include <exception/gate.h>
  4. #include <exception/irq.h>
  5. #include <driver/disk/ahci/ahci.h>
  6. #include <mm/slab.h>
  7. #include <common/errno.h>
  8. #include <common/fcntl.h>
  9. #include <filesystem/fat32/fat32.h>
  10. #include <filesystem/VFS/VFS.h>
  11. #include <driver/keyboard/ps2_keyboard.h>
  12. #include <process/process.h>
  13. // 导出系统调用入口函数,定义在entry.S中
  14. extern void system_call(void);
  15. extern void syscall_int(void);
  16. /**
  17. * @brief 导出系统调用处理函数的符号
  18. *
  19. */
  20. #define SYSCALL_COMMON(syscall_num, symbol) extern unsigned long symbol(struct pt_regs *regs);
  21. SYSCALL_COMMON(0, system_call_not_exists); // 导出system_call_not_exists函数
  22. #undef SYSCALL_COMMON // 取消前述宏定义
  23. /**
  24. * @brief 重新定义为:把系统调用函数加入系统调用表
  25. * @param syscall_num 系统调用号
  26. * @param symbol 系统调用处理函数
  27. */
  28. #define SYSCALL_COMMON(syscall_num, symbol) [syscall_num] = symbol,
  29. /**
  30. * @brief sysenter的系统调用函数,从entry.S中跳转到这里
  31. *
  32. * @param regs 3特权级下的寄存器值,rax存储系统调用号
  33. * @return ul 对应的系统调用函数的地址
  34. */
  35. ul system_call_function(struct pt_regs *regs)
  36. {
  37. return system_call_table[regs->rax](regs);
  38. }
  39. /**
  40. * @brief 初始化系统调用模块
  41. *
  42. */
  43. void syscall_init()
  44. {
  45. kinfo("Initializing syscall...");
  46. set_system_trap_gate(0x80, 0, syscall_int); // 系统调用门
  47. }
  48. /**
  49. * @brief 通过中断进入系统调用
  50. *
  51. * @param syscall_id
  52. * @param arg0
  53. * @param arg1
  54. * @param arg2
  55. * @param arg3
  56. * @param arg4
  57. * @param arg5
  58. * @param arg6
  59. * @param arg7
  60. * @return long
  61. */
  62. long enter_syscall_int(ul syscall_id, ul arg0, ul arg1, ul arg2, ul arg3, ul arg4, ul arg5, ul arg6, ul arg7)
  63. {
  64. long err_code;
  65. __asm__ __volatile__(
  66. "movq %2, %%r8 \n\t"
  67. "movq %3, %%r9 \n\t"
  68. "movq %4, %%r10 \n\t"
  69. "movq %5, %%r11 \n\t"
  70. "movq %6, %%r12 \n\t"
  71. "movq %7, %%r13 \n\t"
  72. "movq %8, %%r14 \n\t"
  73. "movq %9, %%r15 \n\t"
  74. "int $0x80 \n\t"
  75. : "=a"(err_code)
  76. : "a"(syscall_id), "m"(arg0), "m"(arg1), "m"(arg2), "m"(arg3), "m"(arg4), "m"(arg5), "m"(arg6), "m"(arg7)
  77. : "memory", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rcx", "rdx");
  78. return err_code;
  79. }
  80. /**
  81. * @brief 打印字符串的系统调用
  82. *
  83. * 当arg1和arg2均为0时,打印黑底白字,否则按照指定的前景色和背景色来打印
  84. *
  85. * @param regs 寄存器
  86. * @param arg0 要打印的字符串
  87. * @param arg1 前景色
  88. * @param arg2 背景色
  89. * @return ul 返回值
  90. */
  91. ul sys_put_string(struct pt_regs *regs)
  92. {
  93. printk_color(regs->r9, regs->r10, (char *)regs->r8);
  94. // printk_color(BLACK, WHITE, (char *)regs->r8);
  95. return 0;
  96. }
  97. uint64_t sys_open(struct pt_regs *regs)
  98. {
  99. char *filename = (char *)(regs->r8);
  100. int flags = (int)(regs->r9);
  101. // kdebug("filename=%s", filename);
  102. long path_len = strnlen_user(filename, PAGE_4K_SIZE) + 1;
  103. if (path_len <= 0) // 地址空间错误
  104. {
  105. return -EFAULT;
  106. }
  107. else if (path_len >= PAGE_4K_SIZE) // 名称过长
  108. {
  109. return -ENAMETOOLONG;
  110. }
  111. // 为待拷贝文件路径字符串分配内存空间
  112. char *path = (char *)kmalloc(path_len, 0);
  113. if (path == NULL)
  114. return -ENOMEM;
  115. memset(path, 0, path_len);
  116. strncpy_from_user(path, filename, path_len);
  117. // 寻找文件
  118. struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
  119. // if (dentry != NULL)
  120. // printk_color(ORANGE, BLACK, "Found %s\nDIR_FstClus:%#018lx\tDIR_FileSize:%#018lx\n", path, ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus, dentry->dir_inode->file_size);
  121. // else
  122. // printk_color(ORANGE, BLACK, "Can`t find file\n");
  123. kfree(path);
  124. if (dentry == NULL)
  125. return -ENOENT;
  126. // 要求打开文件夹而目标不是文件夹
  127. if ((flags & O_DIRECTORY) && (dentry->dir_inode->attribute != VFS_ATTR_DIR))
  128. return -ENOTDIR;
  129. // 要找的目标是文件夹
  130. if ((flags & O_DIRECTORY) && dentry->dir_inode->attribute == VFS_ATTR_DIR)
  131. return -EISDIR;
  132. // todo: 引入devfs后删除这段代码
  133. // 暂时遇到设备文件的话,就将其first clus设置为特定值
  134. if (path_len >= 5 && filename[0] == '/' && filename[1] == 'd' && filename[2] == 'e' && filename[3] == 'v' && filename[4] == '/')
  135. {
  136. if (dentry->dir_inode->attribute & VFS_ATTR_FILE)
  137. {
  138. // 对于fat32文件系统上面的设备文件,设置其起始扇区
  139. ((struct fat32_inode_info_t *)(dentry->dir_inode->private_inode_info))->first_clus |= 0xf0000000;
  140. dentry->dir_inode->sb->sb_ops->write_inode(dentry->dir_inode);
  141. dentry->dir_inode->attribute |= VFS_ATTR_DEVICE;
  142. }
  143. }
  144. // 创建文件描述符
  145. struct vfs_file_t *file_ptr = (struct vfs_file_t *)kmalloc(sizeof(struct vfs_file_t), 0);
  146. memset(file_ptr, 0, sizeof(struct vfs_file_t));
  147. int errcode = -1;
  148. file_ptr->dEntry = dentry;
  149. file_ptr->mode = flags;
  150. // todo: 接入devfs
  151. // 特判一下是否为键盘文件
  152. if (dentry->dir_inode->attribute & VFS_ATTR_DEVICE)
  153. {
  154. file_ptr->file_ops = &ps2_keyboard_fops; // 如果是设备文件,暂时认为它是键盘文件
  155. }
  156. else
  157. file_ptr->file_ops = dentry->dir_inode->file_ops;
  158. // 如果文件系统实现了打开文件的函数
  159. if (file_ptr->file_ops && file_ptr->file_ops->open)
  160. errcode = file_ptr->file_ops->open(dentry->dir_inode, file_ptr);
  161. if (errcode != VFS_SUCCESS)
  162. {
  163. kfree(file_ptr);
  164. return -EFAULT;
  165. }
  166. if (file_ptr->mode & O_TRUNC) // 清空文件
  167. file_ptr->dEntry->dir_inode->file_size = 0;
  168. if (file_ptr->mode & O_APPEND)
  169. file_ptr->position = file_ptr->dEntry->dir_inode->file_size;
  170. else
  171. file_ptr->position = 0;
  172. struct vfs_file_t **f = current_pcb->fds;
  173. int fd_num = -1;
  174. // 在指针数组中寻找空位
  175. // todo: 当pcb中的指针数组改为动态指针数组之后,需要更改这里(目前还是静态指针数组)
  176. for (int i = 0; i < PROC_MAX_FD_NUM; ++i)
  177. {
  178. if (f[i] == NULL) // 找到指针数组中的空位
  179. {
  180. fd_num = i;
  181. break;
  182. }
  183. }
  184. // 指针数组没有空位了
  185. if (fd_num == -1)
  186. {
  187. kfree(file_ptr);
  188. return -EMFILE;
  189. }
  190. // 保存文件描述符
  191. f[fd_num] = file_ptr;
  192. return fd_num;
  193. }
  194. /**
  195. * @brief 关闭文件系统调用
  196. *
  197. * @param fd_num 文件描述符号
  198. *
  199. * @param regs
  200. * @return uint64_t
  201. */
  202. uint64_t sys_close(struct pt_regs *regs)
  203. {
  204. int fd_num = (int)regs->r8;
  205. // kdebug("sys close: fd=%d", fd_num);
  206. // 校验文件描述符范围
  207. if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
  208. return -EBADF;
  209. // 文件描述符不存在
  210. if (current_pcb->fds[fd_num] == NULL)
  211. return -EBADF;
  212. struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
  213. uint64_t ret;
  214. // If there is a valid close function
  215. if (file_ptr->file_ops && file_ptr->file_ops->close)
  216. ret = file_ptr->file_ops->close(file_ptr->dEntry->dir_inode, file_ptr);
  217. kfree(file_ptr);
  218. current_pcb->fds[fd_num] = NULL;
  219. return 0;
  220. }
  221. /**
  222. * @brief 从文件中读取数据
  223. *
  224. * @param fd_num regs->r8 文件描述符号
  225. * @param buf regs->r9 输出缓冲区
  226. * @param count regs->r10 要读取的字节数
  227. *
  228. * @return uint64_t
  229. */
  230. uint64_t sys_read(struct pt_regs *regs)
  231. {
  232. int fd_num = (int)regs->r8;
  233. void *buf = (void *)regs->r9;
  234. int64_t count = (int64_t)regs->r10;
  235. // kdebug("sys read: fd=%d", fd_num);
  236. // 校验文件描述符范围
  237. if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
  238. return -EBADF;
  239. // 文件描述符不存在
  240. if (current_pcb->fds[fd_num] == NULL)
  241. return -EBADF;
  242. if (count < 0)
  243. return -EINVAL;
  244. struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
  245. uint64_t ret;
  246. if (file_ptr->file_ops && file_ptr->file_ops->read)
  247. ret = file_ptr->file_ops->read(file_ptr, (char *)buf, count, &(file_ptr->position));
  248. return ret;
  249. }
  250. /**
  251. * @brief 向文件写入数据
  252. *
  253. * @param fd_num regs->r8 文件描述符号
  254. * @param buf regs->r9 输入缓冲区
  255. * @param count regs->r10 要写入的字节数
  256. *
  257. * @return uint64_t
  258. */
  259. uint64_t sys_write(struct pt_regs *regs)
  260. {
  261. int fd_num = (int)regs->r8;
  262. void *buf = (void *)regs->r9;
  263. int64_t count = (int64_t)regs->r10;
  264. kdebug("sys write: fd=%d", fd_num);
  265. // 校验文件描述符范围
  266. if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
  267. return -EBADF;
  268. // 文件描述符不存在
  269. if (current_pcb->fds[fd_num] == NULL)
  270. return -EBADF;
  271. if (count < 0)
  272. return -EINVAL;
  273. struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
  274. uint64_t ret;
  275. if (file_ptr->file_ops && file_ptr->file_ops->write)
  276. ret = file_ptr->file_ops->write(file_ptr, (char *)buf, count, &(file_ptr->position));
  277. return ret;
  278. }
  279. /**
  280. * @brief 调整文件的访问位置
  281. *
  282. * @param fd_num 文件描述符号
  283. * @param offset 偏移量
  284. * @param whence 调整模式
  285. * @return uint64_t 调整结束后的文件访问位置
  286. */
  287. uint64_t sys_lseek(struct pt_regs *regs)
  288. {
  289. int fd_num = (int)regs->r8;
  290. long offset = (long)regs->r9;
  291. int whence = (int)regs->r10;
  292. // kdebug("sys_lseek: fd=%d", fd_num);
  293. uint64_t retval = 0;
  294. // 校验文件描述符范围
  295. if (fd_num < 0 || fd_num > PROC_MAX_FD_NUM)
  296. return -EBADF;
  297. // 文件描述符不存在
  298. if (current_pcb->fds[fd_num] == NULL)
  299. return -EBADF;
  300. struct vfs_file_t *file_ptr = current_pcb->fds[fd_num];
  301. if (file_ptr->file_ops && file_ptr->file_ops->lseek)
  302. retval = file_ptr->file_ops->lseek(file_ptr, offset, whence);
  303. return retval;
  304. }
  305. uint64_t sys_fork(struct pt_regs *regs)
  306. {
  307. // kdebug("sys_fork");
  308. return do_fork(regs, 0, regs->rsp, 0);
  309. }
  310. uint64_t sys_vfork(struct pt_regs *regs)
  311. {
  312. kdebug("sys vfork");
  313. return do_fork(regs, CLONE_VM | CLONE_FS | CLONE_SIGNAL, regs->rsp, 0);
  314. }
  315. /**
  316. * @brief 将堆内存调整为arg0
  317. *
  318. * @param arg0 新的堆区域的结束地址
  319. * arg0=-1 ===> 返回堆区域的起始地址
  320. * arg0=-2 ===> 返回堆区域的结束地址
  321. * @return uint64_t 错误码
  322. *
  323. */
  324. uint64_t sys_brk(struct pt_regs *regs)
  325. {
  326. uint64_t new_brk = PAGE_2M_ALIGN(regs->r8);
  327. // kdebug("sys_brk input= %#010lx , new_brk= %#010lx bytes current_pcb->mm->brk_start=%#018lx current->end_brk=%#018lx", regs->r8, new_brk, current_pcb->mm->brk_start, current_pcb->mm->brk_end);
  328. if ((int64_t)regs->r8 == -1)
  329. {
  330. // kdebug("get brk_start=%#018lx", current_pcb->mm->brk_start);
  331. return current_pcb->mm->brk_start;
  332. }
  333. if ((int64_t)regs->r8 == -2)
  334. {
  335. // kdebug("get brk_end=%#018lx", current_pcb->mm->brk_end);
  336. return current_pcb->mm->brk_end;
  337. }
  338. if (new_brk > current_pcb->addr_limit) // 堆地址空间超过限制
  339. return -ENOMEM;
  340. int64_t offset;
  341. if (new_brk >= current_pcb->mm->brk_end)
  342. offset = (int64_t)(new_brk - current_pcb->mm->brk_end);
  343. else
  344. offset = -(int64_t)(current_pcb->mm->brk_end - new_brk);
  345. /*
  346. if (offset < 0)
  347. {
  348. kdebug("decrease brk, offset = %#010lx", (uint64_t)(-offset));
  349. }
  350. */
  351. new_brk = mm_do_brk(current_pcb->mm->brk_end, offset); // 扩展堆内存空间
  352. current_pcb->mm->brk_end = new_brk;
  353. return 0;
  354. }
  355. /**
  356. * @brief 将堆内存空间加上offset(注意,该系统调用只应在普通进程中调用,而不能是内核线程)
  357. *
  358. * @param arg0 offset偏移量
  359. * @return uint64_t the previous program break
  360. */
  361. uint64_t sys_sbrk(struct pt_regs *regs)
  362. {
  363. uint64_t retval = current_pcb->mm->brk_end;
  364. if ((int64_t)regs->r8 > 0)
  365. {
  366. uint64_t new_brk = PAGE_2M_ALIGN(retval + regs->r8);
  367. if (new_brk > current_pcb->addr_limit) // 堆地址空间超过限制
  368. {
  369. kdebug("exceed mem limit, new_brk = %#018lx", new_brk);
  370. return -ENOMEM;
  371. }
  372. }
  373. else
  374. {
  375. if ((__int128_t)current_pcb->mm->brk_end + (__int128_t)regs->r8 < current_pcb->mm->brk_start)
  376. return retval;
  377. }
  378. // kdebug("do brk");
  379. uint64_t new_brk = mm_do_brk(current_pcb->mm->brk_end, (int64_t)regs->r8); // 调整堆内存空间
  380. // kdebug("do brk done, new_brk = %#018lx", new_brk);
  381. current_pcb->mm->brk_end = new_brk;
  382. return retval;
  383. }
  384. /**
  385. * @brief 重启计算机
  386. *
  387. * @return
  388. */
  389. uint64_t sys_reboot(struct pt_regs *regs)
  390. {
  391. // 重启计算机
  392. io_out8(0x64, 0xfe);
  393. return 0;
  394. }
  395. /**
  396. * @brief 切换工作目录
  397. *
  398. * @param dest_path 目标路径
  399. * @return
  400. +--------------+------------------------+
  401. | 返回码 | 描述 |
  402. +--------------+------------------------+
  403. | 0 | 成功 |
  404. | EACCESS | 权限不足 |
  405. | ELOOP | 解析path时遇到路径循环 |
  406. | ENAMETOOLONG | 路径名过长 |
  407. | ENOENT | 目标文件或目录不存在 |
  408. | ENODIR | 检索期间发现非目录项 |
  409. | ENOMEM | 系统内存不足 |
  410. | EFAULT | 错误的地址 |
  411. | ENAMETOOLONG | 路径过长 |
  412. +--------------+------------------------+
  413. */
  414. uint64_t sys_chdir(struct pt_regs *regs)
  415. {
  416. char *dest_path = (char *)regs->r8;
  417. kdebug("dest_path=%s", dest_path);
  418. // 检查目标路径是否为NULL
  419. if (dest_path == NULL)
  420. return -EFAULT;
  421. // 计算输入的路径长度
  422. int dest_path_len;
  423. if (regs->cs & USER_CS)
  424. {
  425. dest_path_len = strnlen_user(dest_path, PAGE_4K_SIZE);
  426. }
  427. else
  428. dest_path_len = strnlen(dest_path, PAGE_4K_SIZE);
  429. // 长度小于等于0
  430. if (dest_path_len <= 0)
  431. return -EFAULT;
  432. else if (dest_path_len >= PAGE_4K_SIZE)
  433. return -ENAMETOOLONG;
  434. // 为路径字符串申请空间
  435. char *path = kmalloc(dest_path_len + 1, 0);
  436. // 系统内存不足
  437. if (path == NULL)
  438. return -ENOMEM;
  439. memset(path, 0, dest_path_len + 1);
  440. if (regs->cs & USER_CS)
  441. {
  442. // 将字符串从用户空间拷贝进来, +1是为了拷贝结尾的\0
  443. strncpy_from_user(path, dest_path, dest_path_len + 1);
  444. }
  445. else
  446. strncpy(path, dest_path, dest_path_len + 1);
  447. kdebug("chdir: path = %s", path);
  448. struct vfs_dir_entry_t *dentry = vfs_path_walk(path, 0);
  449. kfree(path);
  450. if (dentry == NULL)
  451. return -ENOENT;
  452. kdebug("dentry->name=%s, namelen=%d", dentry->name, dentry->name_length);
  453. // 目标不是目录
  454. if (dentry->dir_inode->attribute != VFS_ATTR_DIR)
  455. return -ENOTDIR;
  456. return 0;
  457. }
  458. /**
  459. * @brief 获取目录中的数据
  460. *
  461. * @param fd 文件描述符号
  462. * @return uint64_t
  463. */
  464. uint64_t sys_getdents(struct pt_regs *regs)
  465. {
  466. int fd = (int)regs->r8;
  467. void *dirent = (void *)regs->r9;
  468. long count = (long)regs->r10;
  469. if (fd < 0 || fd > PROC_MAX_FD_NUM)
  470. return -EBADF;
  471. if (count < 0)
  472. return -EINVAL;
  473. struct vfs_file_t *filp = current_pcb->fds[fd];
  474. if (filp == NULL)
  475. return -EBADF;
  476. uint64_t retval = 0;
  477. if (filp->file_ops && filp->file_ops->readdir)
  478. retval = filp->file_ops->readdir(filp, dirent, &vfs_fill_dentry);
  479. return retval;
  480. }
  481. /**
  482. * @brief 执行新的程序
  483. *
  484. * @param user_path(r8寄存器) 文件路径
  485. * @param argv(r9寄存器) 参数列表
  486. * @return uint64_t
  487. */
  488. uint64_t sys_execve(struct pt_regs *regs)
  489. {
  490. // kdebug("sys_execve");
  491. char *user_path = (char *)regs->r8;
  492. char **argv = (char **)regs->r9;
  493. int path_len = strnlen_user(user_path, PAGE_4K_SIZE);
  494. // kdebug("path_len=%d", path_len);
  495. if (path_len >= PAGE_4K_SIZE)
  496. return -ENAMETOOLONG;
  497. else if (path_len <= 0)
  498. return -EFAULT;
  499. char *path = (char *)kmalloc(path_len + 1, 0);
  500. if (path == NULL)
  501. return -ENOMEM;
  502. memset(path, 0, path_len + 1);
  503. // kdebug("before copy file path from user");
  504. // 拷贝文件路径
  505. strncpy_from_user(path, user_path, path_len);
  506. path[path_len] = '\0';
  507. // kdebug("before do_execve, path = %s", path);
  508. // 执行新的程序
  509. uint64_t retval = do_execve(regs, path, argv, NULL);
  510. kfree(path);
  511. return retval;
  512. }
  513. /**
  514. * @brief 等待进程退出
  515. *
  516. * @param pid 目标进程id
  517. * @param status 返回的状态信息
  518. * @param options 等待选项
  519. * @param rusage
  520. * @return uint64_t
  521. */
  522. uint64_t sys_wait4(struct pt_regs *regs)
  523. {
  524. uint64_t pid = regs->r8;
  525. int *status = (int *)regs->r9;
  526. int options = regs->r10;
  527. void *rusage = (void *)regs->r11;
  528. struct process_control_block *proc = NULL;
  529. struct process_control_block *child_proc = NULL;
  530. // 查找pid为指定值的进程
  531. // ps: 这里判断子进程的方法没有按照posix 2008来写。
  532. // todo: 根据进程树判断是否为当前进程的子进程
  533. for (proc = &initial_proc_union.pcb; proc->next_pcb != &initial_proc_union.pcb; proc = proc->next_pcb)
  534. {
  535. if (proc->next_pcb->pid == pid)
  536. {
  537. child_proc = proc->next_pcb;
  538. break;
  539. }
  540. }
  541. if (child_proc == NULL)
  542. return -ECHILD;
  543. // 暂时不支持options选项,该值目前必须为0
  544. if (options != 0)
  545. return -EINVAL;
  546. // 如果子进程没有退出,则等待其退出
  547. while (child_proc->state != PROC_ZOMBIE)
  548. wait_queue_sleep_on_interriptible(&current_pcb->wait_child_proc_exit);
  549. // 拷贝子进程的返回码
  550. *status = child_proc->exit_code;
  551. // copy_to_user(status, (void*)child_proc->exit_code, sizeof(int));
  552. proc->next_pcb = child_proc->next_pcb;
  553. // 释放子进程的页表
  554. process_exit_mm(child_proc);
  555. // 释放子进程的pcb
  556. kfree(child_proc);
  557. return 0;
  558. }
  559. /**
  560. * @brief 进程退出
  561. *
  562. * @param exit_code 退出返回码
  563. * @return uint64_t
  564. */
  565. uint64_t sys_exit(struct pt_regs *regs)
  566. {
  567. return process_do_exit(regs->r8);
  568. }
  569. ul sys_ahci_end_req(struct pt_regs *regs)
  570. {
  571. ahci_end_request();
  572. return 0;
  573. }
  574. // 系统调用的内核入口程序
  575. void do_syscall_int(struct pt_regs *regs, unsigned long error_code)
  576. {
  577. ul ret = system_call_table[regs->rax](regs);
  578. regs->rax = ret; // 返回码
  579. }
  580. system_call_t system_call_table[MAX_SYSTEM_CALL_NUM] =
  581. {
  582. [0] = system_call_not_exists,
  583. [1] = sys_put_string,
  584. [2] = sys_open,
  585. [3] = sys_close,
  586. [4] = sys_read,
  587. [5] = sys_write,
  588. [6] = sys_lseek,
  589. [7] = sys_fork,
  590. [8] = sys_vfork,
  591. [9] = sys_brk,
  592. [10] = sys_sbrk,
  593. [11] = sys_reboot,
  594. [12] = sys_chdir,
  595. [13] = sys_getdents,
  596. [14] = sys_execve,
  597. [15] = sys_wait4,
  598. [16] = sys_exit,
  599. [17] = sys_mkdir,
  600. [18 ... 254] = system_call_not_exists,
  601. [255] = sys_ahci_end_req};