《操作系统》的实验代码。
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.

123 lines
2.7 KiB

12 years ago
12 years ago
12 years ago
  1. #include <unistd.h>
  2. #include <proc.h>
  3. #include <syscall.h>
  4. #include <trap.h>
  5. #include <stdio.h>
  6. #include <pmm.h>
  7. #include <assert.h>
  8. #include <clock.h>
  9. static int
  10. sys_exit(uint32_t arg[]) {
  11. int error_code = (int)arg[0];
  12. return do_exit(error_code);
  13. }
  14. static int
  15. sys_fork(uint32_t arg[]) {
  16. struct trapframe *tf = current->tf;
  17. uintptr_t stack = tf->tf_esp;
  18. return do_fork(0, stack, tf);
  19. }
  20. static int
  21. sys_wait(uint32_t arg[]) {
  22. int pid = (int)arg[0];
  23. int *store = (int *)arg[1];
  24. return do_wait(pid, store);
  25. }
  26. static int
  27. sys_exec(uint32_t arg[]) {
  28. const char *name = (const char *)arg[0];
  29. size_t len = (size_t)arg[1];
  30. unsigned char *binary = (unsigned char *)arg[2];
  31. size_t size = (size_t)arg[3];
  32. return do_execve(name, len, binary, size);
  33. }
  34. static int
  35. sys_yield(uint32_t arg[]) {
  36. return do_yield();
  37. }
  38. static int
  39. sys_kill(uint32_t arg[]) {
  40. int pid = (int)arg[0];
  41. return do_kill(pid);
  42. }
  43. static int
  44. sys_getpid(uint32_t arg[]) {
  45. return current->pid;
  46. }
  47. static int
  48. sys_putc(uint32_t arg[]) {
  49. int c = (int)arg[0];
  50. cputchar(c);
  51. return 0;
  52. }
  53. static int
  54. sys_pgdir(uint32_t arg[]) {
  55. print_pgdir();
  56. return 0;
  57. }
  58. static int
  59. sys_gettime(uint32_t arg[]) {
  60. return (int)ticks;
  61. }
  62. static int
  63. sys_lab6_set_priority(uint32_t arg[])
  64. {
  65. uint32_t priority = (uint32_t)arg[0];
  66. lab6_set_priority(priority);
  67. return 0;
  68. }
  69. static int
  70. sys_sleep(uint32_t arg[]) {
  71. unsigned int time = (unsigned int)arg[0];
  72. return do_sleep(time);
  73. }
  74. static int (*syscalls[])(uint32_t arg[]) = {
  75. [SYS_exit] sys_exit,
  76. [SYS_fork] sys_fork,
  77. [SYS_wait] sys_wait,
  78. [SYS_exec] sys_exec,
  79. [SYS_yield] sys_yield,
  80. [SYS_kill] sys_kill,
  81. [SYS_getpid] sys_getpid,
  82. [SYS_putc] sys_putc,
  83. [SYS_pgdir] sys_pgdir,
  84. [SYS_gettime] sys_gettime,
  85. [SYS_lab6_set_priority] sys_lab6_set_priority,
  86. [SYS_sleep] sys_sleep,
  87. };
  88. #define NUM_SYSCALLS ((sizeof(syscalls)) / (sizeof(syscalls[0])))
  89. void
  90. syscall(void) {
  91. struct trapframe *tf = current->tf;
  92. uint32_t arg[5];
  93. int num = tf->tf_regs.reg_eax;
  94. if (num >= 0 && num < NUM_SYSCALLS) {
  95. if (syscalls[num] != NULL) {
  96. arg[0] = tf->tf_regs.reg_edx;
  97. arg[1] = tf->tf_regs.reg_ecx;
  98. arg[2] = tf->tf_regs.reg_ebx;
  99. arg[3] = tf->tf_regs.reg_edi;
  100. arg[4] = tf->tf_regs.reg_esi;
  101. tf->tf_regs.reg_eax = syscalls[num](arg);
  102. return ;
  103. }
  104. }
  105. print_trapframe(tf);
  106. panic("undefined syscall %d, pid = %d, name = %s.\n",
  107. num, current->pid, current->name);
  108. }