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

116 lines
2.5 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 (*syscalls[])(uint32_t arg[]) = {
  70. [SYS_exit] sys_exit,
  71. [SYS_fork] sys_fork,
  72. [SYS_wait] sys_wait,
  73. [SYS_exec] sys_exec,
  74. [SYS_yield] sys_yield,
  75. [SYS_kill] sys_kill,
  76. [SYS_getpid] sys_getpid,
  77. [SYS_putc] sys_putc,
  78. [SYS_pgdir] sys_pgdir,
  79. [SYS_gettime] sys_gettime,
  80. [SYS_lab6_set_priority] sys_lab6_set_priority,
  81. };
  82. #define NUM_SYSCALLS ((sizeof(syscalls)) / (sizeof(syscalls[0])))
  83. void
  84. syscall(void) {
  85. struct trapframe *tf = current->tf;
  86. uint32_t arg[5];
  87. int num = tf->tf_regs.reg_eax;
  88. if (num >= 0 && num < NUM_SYSCALLS) {
  89. if (syscalls[num] != NULL) {
  90. arg[0] = tf->tf_regs.reg_edx;
  91. arg[1] = tf->tf_regs.reg_ecx;
  92. arg[2] = tf->tf_regs.reg_ebx;
  93. arg[3] = tf->tf_regs.reg_edi;
  94. arg[4] = tf->tf_regs.reg_esi;
  95. tf->tf_regs.reg_eax = syscalls[num](arg);
  96. return ;
  97. }
  98. }
  99. print_trapframe(tf);
  100. panic("undefined syscall %d, pid = %d, name = %s.\n",
  101. num, current->pid, current->name);
  102. }