proc_syscalls.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. #include <types.h>
  2. #include <kern/errno.h>
  3. #include <kern/unistd.h>
  4. #include <kern/wait.h>
  5. #include <mips/trapframe.h>
  6. #include <lib.h>
  7. #include <vfs.h>
  8. #include <vnode.h>
  9. #include <syscall.h>
  10. #include <current.h>
  11. #include <proc.h>
  12. #include <thread.h>
  13. #include <addrspace.h>
  14. #include <copyinout.h>
  15. #include <synch.h>
  16. #include <kern/fcntl.h>
  17. void sys__exit(int exitcode)
  18. {
  19. struct addrspace * as;
  20. struct proc * p = curproc;
  21. p->exitcode = _MKWAIT_EXIT(exitcode);
  22. DEBUG(DB_SYSCALL,"Syscall: _exit(%d)\n",exitcode);
  23. KASSERT(curproc->p_addrspace != NULL);
  24. lock_acquire(proclock);
  25. int x = listpop(p->kids);
  26. while (x)
  27. {
  28. if(processes->pids[x])
  29. {
  30. processes->pids[x]->parent = NULL;
  31. if (processes->pids[x]->exitcode >= 0)
  32. {
  33. lock_release(proclock);
  34. proc_destroy(processes->pids[x]);
  35. lock_acquire(proclock);
  36. }
  37. }
  38. x = listpop(p->kids);
  39. }
  40. lock_release(proclock);
  41. listelete(p->kids);
  42. // VFS fields
  43. if (p->p_cwd)
  44. {
  45. VOP_DECREF(p->p_cwd);
  46. p->p_cwd = NULL;
  47. }
  48. as_deactivate();
  49. as = curproc_setas(NULL);
  50. as_destroy(as);
  51. /* detach this thread from its process */
  52. /* note: curproc cannot be used after this call */
  53. proc_remthread(curthread);
  54. threadarray_cleanup(&p->p_threads);
  55. spinlock_cleanup(&p->p_lock);
  56. lock_acquire(p->waitlock);
  57. cv_broadcast(p->waiting, p->waitlock);
  58. lock_release(p->waitlock);
  59. if (!(p->parent) || p->parent->exitcode >= 0 || p->parent == kproc) proc_destroy(p);
  60. thread_exit();
  61. /* thread_exit() does not return, so we should never get here */
  62. panic("return from thread_exit in sys_exit\n");
  63. }
  64. // basically, return an error code, and put the actual result in retval
  65. int sys_getpid(pid_t * retval)
  66. {
  67. if (!(curproc)) return 1;
  68. *retval = curproc->pid;
  69. return(0);
  70. }
  71. int sys_waitpid(pid_t pid, userptr_t status, int options, pid_t * retval)
  72. {
  73. if (options != 0 || !(void *)(status))
  74. {
  75. return(EINVAL);
  76. }
  77. if (pid < PID_MIN || pid > PID_MAX) return ESRCH;
  78. struct proc * target = getChild(curproc, pid);
  79. if (!(target)) return ECHILD;
  80. if (!(target->exitcode >= 0))
  81. {
  82. lock_acquire(target->waitlock);
  83. cv_wait(target->waiting, target->waitlock);
  84. lock_release(target->waitlock);
  85. }
  86. int exitstatus = target->exitcode;
  87. *retval = target->exitcode;
  88. int result = copyout((void *)&exitstatus, status, sizeof(int));
  89. if (result)
  90. {
  91. return result;
  92. }
  93. proc_destroy(target);
  94. return 0;
  95. }
  96. int sys_fork(struct trapframe * tf, int * retval)
  97. {
  98. // create new process' memory space
  99. struct proc * child = proc_create_runprogram("childproc");
  100. if ((!child)) return ENOMEM;
  101. struct trapframe * new_tf = kmalloc(sizeof(*tf));
  102. struct addrspace * new_as;
  103. as_copy(curproc_getas(), &new_as);
  104. if (!(new_as) || !(new_tf))
  105. {
  106. proc_destroy(child);
  107. kfree(new_as);
  108. kfree(new_tf);
  109. return ENOMEM;
  110. }
  111. // set PIDs, etc. copy data in to the new space
  112. child->p_addrspace = new_as;
  113. child->parent = curproc;
  114. add_child(curproc, child->pid);
  115. *new_tf = *tf;
  116. // start new thread
  117. thread_fork("childproc", child, enter_forked_process, new_tf, 0);
  118. // return correct values
  119. if (curproc->pid == child->pid) *retval = 0;
  120. else *retval = child->pid;
  121. return 0;
  122. }
  123. // this function takes an argc, argv, and puts argv into the userptr argvaddr, on the stack stackptr
  124. int copyArgs(int argc, char ** argv, userptr_t * argvAddr, vaddr_t * stackptr)
  125. {
  126. vaddr_t stack = *stackptr; // for ease of referencing
  127. char ** newArgv = kmalloc(sizeof(char *) * (argc + 1)); // need argc + 1 space since we want a final address to be null
  128. size_t wasteOfSpace;
  129. int errcode;
  130. for(int i = 0; i < argc; ++i)
  131. {
  132. int arglen = strlen(*(argv + i)) + 1; // length of char array for this arg
  133. stack -= ROUNDUP(arglen, 8); // make the stack bigger by this length, but rounded to 8 cause bytes
  134. errcode = copyoutstr(*(argv + i), (userptr_t)stack, arglen, &wasteOfSpace); // take the string in argv at i, put it at the stack position we calculated just before as the start for a string of our length
  135. if(errcode)
  136. {
  137. kfree(newArgv);
  138. return errcode; // if an error, fuck life
  139. }
  140. *(newArgv + i) = (char *)stack; // our argv kernel array is going to contain the user space address
  141. }
  142. *(newArgv + argc) = NULL; // set final address to NULL
  143. for(int i = 0; i <= argc; ++i) // after we store the array contents, we want to store the array of pointers to those contents. which is "before" in the stack becuase fuck you, that's why
  144. {
  145. stack -= sizeof(char *); // move the stack pointer back one pointer worth of space
  146. errcode = copyout(newArgv + (argc - i), (userptr_t)stack, sizeof(char *)); // copy the pointers in reverse order into the stack, since we are going backwards in the stack.
  147. if(errcode)
  148. {
  149. kfree(newArgv);
  150. return errcode; // cry. just cry. then return your error.
  151. }
  152. }
  153. *argvAddr = (userptr_t)stack; // set the argv array in userland to start at where we put it (current stackptr location)
  154. if (stack % 8 == 0) stack -= 8; // move the stack pointer back the minimum amount (if we are at an 8 byte address already, a full 8 bytes. otherwise we must be at a 4 byte address, so just half that)
  155. else stack -= 4;
  156. *stackptr = stack; // set the real stack pointer to the one we've been fucking with
  157. kfree(newArgv); // all data copied over, can free our temp array now
  158. return 0; // return no error
  159. }
  160. static void freeArgv(char ** argv, int len)
  161. {
  162. for (int i = 0; i <= len; ++i)
  163. {
  164. kfree(argv[i]);
  165. }
  166. }
  167. // args is an array of null terminated strings, with the last element being a null pointer so we don't overflow if iterating
  168. // since this is a userptr type, we need to use copyin and copyout to get data properly
  169. // hopefully the path in program is actually a full filepath
  170. int sys_execv(const char * program, userptr_t args)
  171. {
  172. userptr_t temp = args;
  173. int argcount = 0;
  174. int errcode; // why don't we have errno!!! my beautful one-liners!!!
  175. // see how long args is
  176. while (1)
  177. {
  178. void * temp2 = kmalloc(sizeof(temp));
  179. errcode = copyin(temp, temp2, sizeof(temp2));
  180. if (errcode) return errcode;
  181. if (!(*(char **)temp2)) break;
  182. temp += sizeof(temp);
  183. kfree(temp2);
  184. ++argcount;
  185. }
  186. // allocate space for argv
  187. char ** argv = kmalloc(argcount * sizeof(char *));
  188. if (!argv) return ENOMEM;
  189. // get kernel copy of args in argv
  190. temp = args;
  191. for (int i = 0; i < argcount; ++i)
  192. {
  193. char * useraddr = kmalloc(sizeof(temp));
  194. if (!(useraddr))
  195. {
  196. kfree(argv);
  197. return ENOMEM;
  198. }
  199. // useraddr is now the address of the string in userland
  200. errcode = copyin(temp, useraddr, sizeof(temp));
  201. if (errcode)
  202. {
  203. kfree(argv);
  204. return errcode;
  205. }
  206. // change argv[i] to be a string of length enough
  207. int strLen = strlen(*(char **)useraddr) + 1;
  208. kfree(useraddr);
  209. size_t wasteOfSpace;
  210. argv[i] = kmalloc(strLen * sizeof(char));
  211. if (!(argv[i]))
  212. {
  213. freeArgv(argv, i);
  214. kfree(argv);
  215. return ENOMEM;
  216. }
  217. errcode = copyinstr((userptr_t)*(char **)temp, argv[i], strLen, &wasteOfSpace);
  218. if (errcode)
  219. {
  220. freeArgv(argv, i);
  221. kfree(argv);
  222. return errcode;
  223. }
  224. temp += sizeof(temp);
  225. }
  226. // allocate space for filepath
  227. char * filepath = kmalloc((strlen(program) + 1) * sizeof(char));
  228. if (!(filepath))
  229. {
  230. freeArgv(argv, argcount - 1);
  231. kfree(argv);
  232. return ENOMEM;
  233. }
  234. // get kernel copy of filepath
  235. size_t useless;
  236. errcode = copyinstr((const_userptr_t)program, filepath, strlen(program) + 1, &useless);
  237. if (errcode)
  238. {
  239. freeArgv(argv, argcount - 1);
  240. kfree(argv);
  241. kfree(filepath);
  242. return errcode;
  243. }
  244. // open program file
  245. struct vnode * v;
  246. errcode = vfs_open(filepath, O_RDONLY, 0, &v);
  247. if (errcode)
  248. {
  249. freeArgv(argv, argcount - 1);
  250. kfree(argv);
  251. kfree(filepath);
  252. return errcode;
  253. }
  254. // create new address space
  255. struct addrspace * as = as_create();
  256. if (!(as))
  257. {
  258. vfs_close(v);
  259. freeArgv(argv, argcount - 1);
  260. kfree(argv);
  261. kfree(filepath);
  262. return ENOMEM;
  263. }
  264. // Switch to the new as and activate. keep old in case we fail later
  265. struct addrspace * oldas = curproc_getas();
  266. curproc_setas(as);
  267. as_activate();
  268. vaddr_t entrypoint;
  269. // Load santa's favourite slave
  270. errcode = load_elf(v, &entrypoint);
  271. if (errcode)
  272. {
  273. vfs_close(v);
  274. freeArgv(argv, argcount - 1);
  275. kfree(argv);
  276. kfree(filepath);
  277. curproc_setas(oldas);
  278. as_destroy(as);
  279. return errcode;
  280. }
  281. // close the file now that we're done
  282. vfs_close(v);
  283. // create the new stack
  284. vaddr_t newstack;
  285. errcode = as_define_stack(as, &newstack);
  286. if (errcode)
  287. {
  288. freeArgv(argv, argcount - 1);
  289. kfree(argv);
  290. kfree(filepath);
  291. curproc_setas(oldas);
  292. as_destroy(as);
  293. return errcode;
  294. }
  295. // put args into the stack
  296. userptr_t userV;
  297. errcode = copyArgs(argcount, argv, &userV, &newstack);
  298. if (errcode)
  299. {
  300. freeArgv(argv, argcount - 1);
  301. kfree(argv);
  302. kfree(filepath);
  303. curproc_setas(oldas);
  304. as_destroy(as);
  305. return errcode;
  306. }
  307. // delete old addrspace, enter new process
  308. as_destroy(oldas);
  309. kfree(filepath);
  310. freeArgv(argv, argcount - 1);
  311. kfree(argv);
  312. enter_new_process(argcount, userV, newstack, entrypoint);
  313. panic("Enter new process returned!\n");
  314. return EINVAL;
  315. }