proc_syscalls.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 = 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. (void)status;
  74. if (options != 0)
  75. {
  76. return(EINVAL);
  77. }
  78. if (pid < PID_MIN || pid > PID_MAX) return ESRCH;
  79. struct proc * target = getChild(curproc, pid);
  80. if (!(target)) return ECHILD;
  81. if (target->exitcode >= 0)
  82. {
  83. *retval = target->exitcode;
  84. return 0;
  85. }
  86. lock_acquire(target->waitlock);
  87. cv_wait(target->waiting, target->waitlock);
  88. lock_release(target->waitlock);
  89. *retval = target->exitcode;
  90. proc_destroy(target);
  91. return 0;
  92. }
  93. int sys_fork(struct trapframe * tf, int * retval)
  94. {
  95. // create new process' memory space
  96. struct proc * child = proc_create_runprogram("childproc");
  97. if ((!child)) return ENOMEM;
  98. struct trapframe * new_tf = kmalloc(sizeof(*tf));
  99. struct addrspace * new_as;
  100. as_copy(curproc_getas(), &new_as);
  101. if (!(new_as) || !(new_tf))
  102. {
  103. proc_destroy(child);
  104. kfree(new_as);
  105. kfree(new_tf);
  106. return ENOMEM;
  107. }
  108. // set PIDs, etc. copy data in to the new space
  109. child->p_addrspace = new_as;
  110. child->parent = curproc;
  111. add_child(curproc, child->pid);
  112. *new_tf = *tf;
  113. // start new thread
  114. thread_fork("childproc", child, enter_forked_process, new_tf, 0);
  115. // return correct values
  116. if (curproc->pid == child->pid) *retval = 0;
  117. else *retval = child->pid;
  118. return 0;
  119. }
  120. // this function takes an argc, argv, and puts argv into the userptr argvaddr, on the stack stackptr
  121. int copyArgs(int argc, char ** argv, userptr_t * argvAddr, vaddr_t * stackptr)
  122. {
  123. vaddr_t stack = *stackptr; // for ease of referencing
  124. char ** newArgv = kmalloc(sizeof(char *) * (argc + 1)); // need argc + 1 space since we want a final address to be null
  125. size_t wasteOfSpace;
  126. int errcode;
  127. for(int i = 0; i < argc; ++i)
  128. {
  129. int arglen = strlen(*(argv + i)) + 1; // length of char array for this arg
  130. stack -= ROUNDUP(arglen, 8); // make the stack bigger by this length, but rounded to 8 cause bytes
  131. 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
  132. if(errcode)
  133. {
  134. kfree(newArgv);
  135. return errcode; // if an error, fuck life
  136. }
  137. *(newArgv + i) = (char *)stack; // our argv kernel array is going to contain the user space address
  138. }
  139. *(newArgv + argc) = NULL; // set final address to NULL
  140. 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
  141. {
  142. stack -= sizeof(char *); // move the stack pointer back one pointer worth of space
  143. 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.
  144. if(errcode)
  145. {
  146. kfree(newArgv);
  147. return errcode; // cry. just cry. then return your error.
  148. }
  149. }
  150. *argvAddr = (userptr_t)stack; // set the argv array in userland to start at where we put it (current stackptr location)
  151. 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)
  152. else stack -= 4;
  153. *stackptr = stack; // set the real stack pointer to the one we've been fucking with
  154. kfree(newArgv); // all data copied over, can free our temp array now
  155. return 0; // return no error
  156. }
  157. static void freeArgv(char ** argv, int len)
  158. {
  159. for (int i = 0; i <= len; ++i)
  160. {
  161. kfree(argv[i]);
  162. }
  163. }
  164. // args is an array of null terminated strings, with the last element being a null pointer so we don't overflow if iterating
  165. // since this is a userptr type, we need to use copyin and copyout to get data properly
  166. // hopefully the path in program is actually a full filepath
  167. int sys_execv(const char * program, userptr_t args)
  168. {
  169. userptr_t temp = args;
  170. int argcount = 0;
  171. int errcode; // why don't we have errno!!! my beautful one-liners!!!
  172. // see how long args is
  173. while (1)
  174. {
  175. void * temp2 = kmalloc(sizeof(temp));
  176. errcode = copyin(temp, temp2, sizeof(temp2));
  177. if (errcode) return errcode;
  178. if (!(*(char **)temp2)) break;
  179. temp += sizeof(temp);
  180. kfree(temp2);
  181. ++argcount;
  182. }
  183. // allocate space for argv
  184. char ** argv = kmalloc(argcount * sizeof(char *));
  185. if (!argv) return ENOMEM;
  186. // get kernel copy of args in argv
  187. temp = args;
  188. for (int i = 0; i < argcount; ++i)
  189. {
  190. char * useraddr = kmalloc(sizeof(temp));
  191. // useraddr is now the address of the string in userland
  192. errcode = copyin(temp, useraddr, sizeof(temp));
  193. if (errcode)
  194. {
  195. kfree(argv);
  196. return errcode;
  197. }
  198. // change argv[i] to be a string of length enough
  199. int strLen = strlen(*(char **)useraddr) + 1;
  200. size_t wasteOfSpace;
  201. argv[i] = kmalloc(strLen * sizeof(char));
  202. if (!(argv[i]))
  203. {
  204. freeArgv(argv, i);
  205. kfree(argv);
  206. return ENOMEM;
  207. }
  208. errcode = copyinstr((userptr_t)*(char **)temp, argv[i], strLen, &wasteOfSpace);
  209. if (errcode)
  210. {
  211. freeArgv(argv, i);
  212. kfree(argv);
  213. return errcode;
  214. }
  215. temp += sizeof(temp);
  216. }
  217. // allocate space for filepath
  218. char * filepath = kmalloc(sizeof(program));
  219. if (!(filepath))
  220. {
  221. freeArgv(argv, argcount - 1);
  222. kfree(argv);
  223. return ENOMEM;
  224. }
  225. // get kernel copy of filepath
  226. size_t useless;
  227. errcode = copyinstr((const_userptr_t)program, filepath, strlen(program) + 1, &useless);
  228. if (errcode)
  229. {
  230. freeArgv(argv, argcount - 1);
  231. kfree(argv);
  232. kfree(filepath);
  233. return errcode;
  234. }
  235. // open program file
  236. struct vnode * v;
  237. errcode = vfs_open(filepath, O_RDONLY, 0, &v);
  238. if (errcode)
  239. {
  240. freeArgv(argv, argcount - 1);
  241. kfree(argv);
  242. kfree(filepath);
  243. return errcode;
  244. }
  245. // create new address space
  246. struct addrspace * as = as_create();
  247. if (!(as))
  248. {
  249. vfs_close(v);
  250. freeArgv(argv, argcount - 1);
  251. kfree(argv);
  252. kfree(filepath);
  253. return ENOMEM;
  254. }
  255. // Switch to the new as and activate. keep old in case we fail later
  256. struct addrspace * oldas = curproc_getas();
  257. curproc_setas(as);
  258. as_activate();
  259. vaddr_t entrypoint;
  260. // Load santa's favourite slave
  261. errcode = load_elf(v, &entrypoint);
  262. if (errcode)
  263. {
  264. vfs_close(v);
  265. freeArgv(argv, argcount - 1);
  266. kfree(argv);
  267. kfree(filepath);
  268. curproc_setas(oldas);
  269. as_destroy(as);
  270. return errcode;
  271. }
  272. // close the file now that we're done
  273. vfs_close(v);
  274. // create the new stack
  275. vaddr_t newstack;
  276. errcode = as_define_stack(as, &newstack);
  277. if (errcode)
  278. {
  279. freeArgv(argv, argcount - 1);
  280. kfree(argv);
  281. kfree(filepath);
  282. curproc_setas(oldas);
  283. as_destroy(as);
  284. return errcode;
  285. }
  286. // put args into the stack
  287. userptr_t userV;
  288. errcode = copyArgs(argcount, argv, &userV, &newstack);
  289. if (errcode)
  290. {
  291. freeArgv(argv, argcount - 1);
  292. kfree(argv);
  293. kfree(filepath);
  294. curproc_setas(oldas);
  295. as_destroy(as);
  296. return errcode;
  297. }
  298. // delete old addrspace, enter new process
  299. as_destroy(oldas);
  300. kfree(filepath);
  301. freeArgv(argv, argcount - 1);
  302. kfree(argv);
  303. enter_new_process(argcount, userV, newstack, entrypoint);
  304. panic("Enter new process returned!\n");
  305. return EINVAL;
  306. }