《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.5 KiB

10 years ago
  1. #include <defs.h>
  2. #include <unistd.h>
  3. #include <proc.h>
  4. #include <syscall.h>
  5. #include <trap.h>
  6. #include <stdio.h>
  7. #include <pmm.h>
  8. #include <assert.h>
  9. #include <clock.h>
  10. #include <stat.h>
  11. #include <dirent.h>
  12. #include <sysfile.h>
  13. static int
  14. sys_exit(uint32_t arg[]) {
  15. int error_code = (int)arg[0];
  16. return do_exit(error_code);
  17. }
  18. static int
  19. sys_fork(uint32_t arg[]) {
  20. struct trapframe *tf = current->tf;
  21. uintptr_t stack = tf->tf_esp;
  22. return do_fork(0, stack, tf);
  23. }
  24. static int
  25. sys_wait(uint32_t arg[]) {
  26. int pid = (int)arg[0];
  27. int *store = (int *)arg[1];
  28. return do_wait(pid, store);
  29. }
  30. static int
  31. sys_exec(uint32_t arg[]) {
  32. const char *name = (const char *)arg[0];
  33. int argc = (int)arg[1];
  34. const char **argv = (const char **)arg[2];
  35. return do_execve(name, argc, argv);
  36. }
  37. static int
  38. sys_yield(uint32_t arg[]) {
  39. return do_yield();
  40. }
  41. static int
  42. sys_kill(uint32_t arg[]) {
  43. int pid = (int)arg[0];
  44. return do_kill(pid);
  45. }
  46. static int
  47. sys_getpid(uint32_t arg[]) {
  48. return current->pid;
  49. }
  50. static int
  51. sys_putc(uint32_t arg[]) {
  52. int c = (int)arg[0];
  53. cputchar(c);
  54. return 0;
  55. }
  56. static int
  57. sys_pgdir(uint32_t arg[]) {
  58. print_pgdir();
  59. return 0;
  60. }
  61. static uint32_t
  62. sys_gettime(uint32_t arg[]) {
  63. return (int)ticks;
  64. }
  65. static uint32_t
  66. sys_lab6_set_priority(uint32_t arg[])
  67. {
  68. uint32_t priority = (uint32_t)arg[0];
  69. lab6_set_priority(priority);
  70. return 0;
  71. }
  72. static int
  73. sys_sleep(uint32_t arg[]) {
  74. unsigned int time = (unsigned int)arg[0];
  75. return do_sleep(time);
  76. }
  77. static int
  78. sys_open(uint32_t arg[]) {
  79. const char *path = (const char *)arg[0];
  80. uint32_t open_flags = (uint32_t)arg[1];
  81. return sysfile_open(path, open_flags);
  82. }
  83. static int
  84. sys_close(uint32_t arg[]) {
  85. int fd = (int)arg[0];
  86. return sysfile_close(fd);
  87. }
  88. static int
  89. sys_read(uint32_t arg[]) {
  90. int fd = (int)arg[0];
  91. void *base = (void *)arg[1];
  92. size_t len = (size_t)arg[2];
  93. return sysfile_read(fd, base, len);
  94. }
  95. static int
  96. sys_write(uint32_t arg[]) {
  97. int fd = (int)arg[0];
  98. void *base = (void *)arg[1];
  99. size_t len = (size_t)arg[2];
  100. return sysfile_write(fd, base, len);
  101. }
  102. static int
  103. sys_seek(uint32_t arg[]) {
  104. int fd = (int)arg[0];
  105. off_t pos = (off_t)arg[1];
  106. int whence = (int)arg[2];
  107. return sysfile_seek(fd, pos, whence);
  108. }
  109. static int
  110. sys_fstat(uint32_t arg[]) {
  111. int fd = (int)arg[0];
  112. struct stat *stat = (struct stat *)arg[1];
  113. return sysfile_fstat(fd, stat);
  114. }
  115. static int
  116. sys_fsync(uint32_t arg[]) {
  117. int fd = (int)arg[0];
  118. return sysfile_fsync(fd);
  119. }
  120. static int
  121. sys_getcwd(uint32_t arg[]) {
  122. char *buf = (char *)arg[0];
  123. size_t len = (size_t)arg[1];
  124. return sysfile_getcwd(buf, len);
  125. }
  126. static int
  127. sys_getdirentry(uint32_t arg[]) {
  128. int fd = (int)arg[0];
  129. struct dirent *direntp = (struct dirent *)arg[1];
  130. return sysfile_getdirentry(fd, direntp);
  131. }
  132. static int
  133. sys_dup(uint32_t arg[]) {
  134. int fd1 = (int)arg[0];
  135. int fd2 = (int)arg[1];
  136. return sysfile_dup(fd1, fd2);
  137. }
  138. static int (*syscalls[])(uint32_t arg[]) = {
  139. [SYS_exit] sys_exit,
  140. [SYS_fork] sys_fork,
  141. [SYS_wait] sys_wait,
  142. [SYS_exec] sys_exec,
  143. [SYS_yield] sys_yield,
  144. [SYS_kill] sys_kill,
  145. [SYS_getpid] sys_getpid,
  146. [SYS_putc] sys_putc,
  147. [SYS_pgdir] sys_pgdir,
  148. [SYS_gettime] sys_gettime,
  149. [SYS_lab6_set_priority] sys_lab6_set_priority,
  150. [SYS_sleep] sys_sleep,
  151. [SYS_open] sys_open,
  152. [SYS_close] sys_close,
  153. [SYS_read] sys_read,
  154. [SYS_write] sys_write,
  155. [SYS_seek] sys_seek,
  156. [SYS_fstat] sys_fstat,
  157. [SYS_fsync] sys_fsync,
  158. [SYS_getcwd] sys_getcwd,
  159. [SYS_getdirentry] sys_getdirentry,
  160. [SYS_dup] sys_dup,
  161. };
  162. #define NUM_SYSCALLS ((sizeof(syscalls)) / (sizeof(syscalls[0])))
  163. void
  164. syscall(void) {
  165. struct trapframe *tf = current->tf;
  166. uint32_t arg[5];
  167. int num = tf->tf_regs.reg_eax;
  168. if (num >= 0 && num < NUM_SYSCALLS) {
  169. if (syscalls[num] != NULL) {
  170. arg[0] = tf->tf_regs.reg_edx;
  171. arg[1] = tf->tf_regs.reg_ecx;
  172. arg[2] = tf->tf_regs.reg_ebx;
  173. arg[3] = tf->tf_regs.reg_edi;
  174. arg[4] = tf->tf_regs.reg_esi;
  175. tf->tf_regs.reg_eax = syscalls[num](arg);
  176. return ;
  177. }
  178. }
  179. print_trapframe(tf);
  180. panic("undefined syscall %d, pid = %d, name = %s.\n",
  181. num, current->pid, current->name);
  182. }