proc_syscalls.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. char * temp2;
  176. errcode = copyin(temp, temp2, sizeof(char *));
  177. if (errcode) return errcode;
  178. if (!(temp2)) break;
  179. temp += sizeof(char *);
  180. ++argcount;
  181. }
  182. // allocate space for argv
  183. char ** argv = kmalloc(argcount * sizeof(char *));
  184. if (!argv) return ENOMEM;
  185. // get kernel copy of args in argv
  186. temp = args;
  187. for (int i = 0; i < argcount; ++i)
  188. {
  189. char * useraddr;
  190. // useraddr is now the address of the string in userland
  191. errcode = copyin(temp, useraddr, sizeof(char *));
  192. if (errcode)
  193. {
  194. kfree(argv);
  195. return errcode;
  196. }
  197. // change argv[i] to be a string of length enough
  198. int strLen = strlen(useraddr) + 1;
  199. size_t wasteOfSpace;
  200. argv[i] = kmalloc(strLen * sizeof(char));
  201. if (!(argv[i]))
  202. {
  203. freeArgv(argv, i);
  204. kfree(argv);
  205. return ENOMEM;
  206. }
  207. errcode = copyinstr(temp, argv[i], strLen, &wasteOfSpace);
  208. if (errcode)
  209. {
  210. freeArgv(argv, i);
  211. kfree(argv);
  212. return errcode;
  213. }
  214. temp += sizeof(char *);
  215. }
  216. // allocate space for filepath
  217. char * filepath = kmalloc(sizeof(program));
  218. if (!(filepath))
  219. {
  220. freeArgv(argv, argcount - 1);
  221. kfree(argv);
  222. return ENOMEM;
  223. }
  224. // get kernel copy of filepath
  225. size_t useless;
  226. errcode = copyinstr((const_userptr_t)program, filepath, strlen(program) + 1, &useless);
  227. if (errcode)
  228. {
  229. freeArgv(argv, argcount - 1);
  230. kfree(argv);
  231. kfree(filepath);
  232. return errcode;
  233. }
  234. // open program file
  235. struct vnode * v;
  236. errcode = vfs_open(filepath, O_RDONLY, 0, &v);
  237. if (errcode)
  238. {
  239. freeArgv(argv, argcount - 1);
  240. kfree(argv);
  241. kfree(filepath);
  242. return errcode;
  243. }
  244. // create new address space
  245. struct addrspace * as = as_create();
  246. if (!(as))
  247. {
  248. vfs_close(v);
  249. freeArgv(argv, argcount - 1);
  250. kfree(argv);
  251. kfree(filepath);
  252. return ENOMEM;
  253. }
  254. // Switch to the new as and activate. keep old in case we fail later
  255. struct addrspace * oldas = curproc_getas();
  256. curproc_setas(as);
  257. as_activate();
  258. vaddr_t entrypoint;
  259. // Load santa's favourite slave
  260. errcode = load_elf(v, &entrypoint);
  261. if (errcode)
  262. {
  263. vfs_close(v);
  264. freeArgv(argv, argcount - 1);
  265. kfree(argv);
  266. kfree(filepath);
  267. curproc_setas(oldas);
  268. as_destroy(as);
  269. return errcode;
  270. }
  271. // close the file now that we're done
  272. vfs_close(v);
  273. // create the new stack
  274. vaddr_t newstack;
  275. errcode = as_define_stack(as, &newstack);
  276. if (errcode)
  277. {
  278. freeArgv(argv, argcount - 1);
  279. kfree(argv);
  280. kfree(filepath);
  281. curproc_setas(oldas);
  282. as_destroy(as);
  283. return errcode;
  284. }
  285. userptr_t userV;
  286. errcode = copyArgs(argcount, argv, &userV, &newstack);
  287. if (errcode)
  288. {
  289. freeArgv(argv, argcount - 1);
  290. kfree(argv);
  291. kfree(filepath);
  292. curproc_setas(oldas);
  293. as_destroy(as);
  294. return errcode;
  295. }
  296. // need to copy data over into the new address space, as rn it is all in kernel heap
  297. // delete old addrspace, enter new process
  298. as_destroy(oldas);
  299. kfree(filepath);
  300. freeArgv(argv, argcount - 1);
  301. kfree(argv);
  302. //kprintf("agrc: %d, userv: %p, stack: %p, entry: %p\n", argcount, userV, (void *)newstack, (void *)entrypoint);
  303. enter_new_process(argcount, userV, newstack, entrypoint);
  304. panic("Enter new process returned!\n");
  305. return EINVAL;
  306. }