proc_syscalls.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <types.h>
  2. #include <kern/errno.h>
  3. #include <kern/unistd.h>
  4. #include <kern/wait.h>
  5. #include <lib.h>
  6. #include <syscall.h>
  7. #include <current.h>
  8. #include <proc.h>
  9. #include <thread.h>
  10. #include <addrspace.h>
  11. #include <copyinout.h>
  12. /* this implementation of sys__exit does not do anything with the exit code */
  13. /* this needs to be fixed to get exit() and waitpid() working properly */
  14. void sys__exit(int exitcode)
  15. {
  16. struct addrspace * as;
  17. struct proc * p = curproc;
  18. /* for now, just include this to keep the compiler from complaining about
  19. an unused variable */
  20. (void)exitcode;
  21. DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
  22. KASSERT(curproc->p_addrspace != NULL);
  23. as_deactivate();
  24. /*
  25. * clear p_addrspace before calling as_destroy. Otherwise if
  26. * as_destroy sleeps (which is quite possible) when we
  27. * come back we'll be calling as_activate on a
  28. * half-destroyed address space. This tends to be
  29. * messily fatal.
  30. */
  31. as = curproc_setas(NULL);
  32. as_destroy(as);
  33. /* detach this thread from its process */
  34. /* note: curproc cannot be used after this call */
  35. proc_remthread(curthread);
  36. /* if this is the last user process in the system, proc_destroy()
  37. will wake up the kernel menu thread */
  38. proc_destroy(p);
  39. thread_exit();
  40. /* thread_exit() does not return, so we should never get here */
  41. panic("return from thread_exit in sys_exit\n");
  42. }
  43. // basically, return an error code, and put the actual result in retval
  44. int sys_getpid(pid_t * retval)
  45. {
  46. if (!(curproc)) return 1;
  47. *retval = curproc->pid;
  48. return(0);
  49. }
  50. /* stub handler for waitpid() system call */
  51. int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval)
  52. {
  53. int exitstatus;
  54. int result;
  55. /* this is just a stub implementation that always reports an
  56. exit status of 0, regardless of the actual exit status of
  57. the specified process.
  58. In fact, this will return 0 even if the specified process
  59. is still running, and even if it never existed in the first place.
  60. Fix this!
  61. */
  62. if (options != 0)
  63. {
  64. return(EINVAL);
  65. }
  66. /* for now, just pretend the exitstatus is 0 */
  67. exitstatus = 0;
  68. result = copyout((void *)&exitstatus,status,sizeof(int));
  69. if (result)
  70. {
  71. return(result);
  72. }
  73. *retval = pid;
  74. return(0);
  75. }
  76. int sys_fork(struct trapframe * tf, int * retval)
  77. {
  78. // create new process' memory space
  79. struct proc * child = proc_create_runprogram("childproc");
  80. struct addrspace * new_as;
  81. struct trapframe * new_tf = kmalloc(sizeof(*tf));
  82. if ((!child) return ENOMEM;
  83. as_copy(curproc_getas(), &childAddrspace);
  84. if (!(new_as) || !(new_tf))
  85. {
  86. proc_destroy(child);
  87. kfree(new_as);
  88. kfree(new_tf);
  89. return ENOMEM;
  90. }
  91. // might want to handle inserting PID into overall struct here. perhaps a setpid function or something in proc_create_runprogram...
  92. // set PIDs, etc. copy data in to the new space
  93. child->p_addrspace = new_as;
  94. child->parent = curproc;
  95. *new_tf = *tf;
  96. // start new thread
  97. thread_fork("childproc", child, enter_forked_process, new_tf, 0);
  98. // return correct values
  99. if (curproc->pid == child->pid) *retval = 0;
  100. else *retval = child->pid;
  101. return 0;
  102. }