Advertisement
Guest User

Untitled

a guest
May 18th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include "types.h"
  2. #include "param.h"
  3. #include "memlayout.h"
  4. #include "riscv.h"
  5. #include "spinlock.h"
  6. #include "proc.h"
  7. #include "syscall.h"
  8. #include "defs.h"
  9.  
  10. // Fetch the uint64 at addr from the current process.
  11. int
  12. fetchaddr(uint64 addr, uint64 *ip)
  13. {
  14. struct proc *p = myproc();
  15. if(addr >= p->sz || addr+sizeof(uint64) > p->sz)
  16. return -1;
  17. if(copyin(p->pagetable, (char *)ip, addr, sizeof(*ip)) != 0)
  18. return -1;
  19. return 0;
  20. }
  21.  
  22. // Fetch the nul-terminated string at addr from the current process.
  23. // Returns length of string, not including nul, or -1 for error.
  24. int
  25. fetchstr(uint64 addr, char *buf, int max)
  26. {
  27. struct proc *p = myproc();
  28. int err = copyinstr(p->pagetable, buf, addr, max);
  29. if(err < 0)
  30. return err;
  31. return strlen(buf);
  32. }
  33.  
  34. static uint64
  35. argraw(int n)
  36. {
  37. struct proc *p = myproc();
  38. switch (n) {
  39. case 0:
  40. return p->trapframe->a0;
  41. case 1:
  42. return p->trapframe->a1;
  43. case 2:
  44. return p->trapframe->a2;
  45. case 3:
  46. return p->trapframe->a3;
  47. case 4:
  48. return p->trapframe->a4;
  49. case 5:
  50. return p->trapframe->a5;
  51. }
  52. panic("argraw");
  53. return -1;
  54. }
  55.  
  56. // Fetch the nth 32-bit system call argument.
  57. int
  58. argint(int n, int *ip)
  59. {
  60. *ip = argraw(n);
  61. return 0;
  62. }
  63.  
  64. // Retrieve an argument as a pointer.
  65. // Doesn't check for legality, since
  66. // copyin/copyout will do that.
  67. int
  68. argaddr(int n, uint64 *ip)
  69. {
  70. *ip = argraw(n);
  71. return 0;
  72. }
  73.  
  74. // Fetch the nth word-sized system call argument as a null-terminated string.
  75. // Copies into buf, at most max.
  76. // Returns string length if OK (including nul), -1 if error.
  77. int
  78. argstr(int n, char *buf, int max)
  79. {
  80. uint64 addr;
  81. if(argaddr(n, &addr) < 0)
  82. return -1;
  83. return fetchstr(addr, buf, max);
  84. }
  85.  
  86. extern uint64 sys_chdir(void);
  87. extern uint64 sys_close(void);
  88. extern uint64 sys_dup(void);
  89. extern uint64 sys_exec(void);
  90. extern uint64 sys_exit(void);
  91. extern uint64 sys_fork(void);
  92. extern uint64 sys_fstat(void);
  93. extern uint64 sys_getpid(void);
  94. extern uint64 sys_kill(void);
  95. extern uint64 sys_link(void);
  96. extern uint64 sys_mkdir(void);
  97. extern uint64 sys_mknod(void);
  98. extern uint64 sys_open(void);
  99. extern uint64 sys_pipe(void);
  100. extern uint64 sys_read(void);
  101. extern uint64 sys_sbrk(void);
  102. extern uint64 sys_sleep(void);
  103. extern uint64 sys_unlink(void);
  104. extern uint64 sys_wait(void);
  105. extern uint64 sys_write(void);
  106. extern uint64 sys_uptime(void);
  107. extern uint64 sys_trace(void);
  108.  
  109. static uint64 (*syscalls[])(void) = {
  110. [SYS_fork] sys_fork,
  111. [SYS_exit] sys_exit,
  112. [SYS_wait] sys_wait,
  113. [SYS_pipe] sys_pipe,
  114. [SYS_read] sys_read,
  115. [SYS_kill] sys_kill,
  116. [SYS_exec] sys_exec,
  117. [SYS_fstat] sys_fstat,
  118. [SYS_chdir] sys_chdir,
  119. [SYS_dup] sys_dup,
  120. [SYS_getpid] sys_getpid,
  121. [SYS_sbrk] sys_sbrk,
  122. [SYS_sleep] sys_sleep,
  123. [SYS_uptime] sys_uptime,
  124. [SYS_open] sys_open,
  125. [SYS_write] sys_write,
  126. [SYS_mknod] sys_mknod,
  127. [SYS_unlink] sys_unlink,
  128. [SYS_link] sys_link,
  129. [SYS_mkdir] sys_mkdir,
  130. [SYS_close] sys_close,
  131. [SYS_trace] sys_trace,
  132. };
  133.  
  134. void
  135. syscall(void)
  136. {
  137. int num;
  138. struct proc *p = myproc();
  139.  
  140. num = p->trapframe->a7;
  141. if(num > 0 && num < NELEM(syscalls) && syscalls[num]) {
  142. p->trapframe->a0 = syscalls[num]();
  143. } else {
  144. printf("%d %s: unknown sys call %d\n",
  145. p->pid, p->name, num);
  146. p->trapframe->a0 = -1;
  147. }
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement