proc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright (c) 2013
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Process support.
  31. *
  32. * There is (intentionally) not much here; you will need to add stuff
  33. * and maybe change around what's already present.
  34. *
  35. * p_lock is intended to be held when manipulating the pointers in the
  36. * proc structure, not while doing any significant work with the
  37. * things they point to. Rearrange this (and/or change it to be a
  38. * regular lock) as needed.
  39. *
  40. * Unless you're implementing multithreaded user processes, the only
  41. * process that will have more than one thread is the kernel process.
  42. */
  43. #include <types.h>
  44. #include <proc.h>
  45. #include <current.h>
  46. #include <addrspace.h>
  47. #include <vnode.h>
  48. #include <vfs.h>
  49. #include <synch.h>
  50. #include <kern/fcntl.h>
  51. #include <list.h>
  52. #include <limits.h>
  53. #include <kern/errno.h>
  54. typedef struct list list;
  55. typedef struct procs procs;
  56. //The process for the kernel; this holds all the kernel-only threads.
  57. struct proc * kproc;
  58. // The process array
  59. procs * processes;
  60. // The lock for the above array
  61. struct lock * proclock;
  62. /*
  63. * Mechanism for making the kernel menu thread sleep while processes are running
  64. */
  65. #ifdef UW
  66. /* count of the number of processes, excluding kproc */
  67. static volatile unsigned int proc_count;
  68. /* provides mutual exclusion for proc_count */
  69. /* it would be better to use a lock here, but we use a semaphore because locks are not implemented in the base kernel */
  70. static struct semaphore * proc_count_mutex;
  71. /* used to signal the kernel menu thread when there are no processes */
  72. struct semaphore * no_proc_sem;
  73. #endif // UW
  74. /*
  75. * Create a proc structure.
  76. */
  77. static struct proc * proc_create(const char * name)
  78. {
  79. struct proc * proc;
  80. proc = kmalloc(sizeof(*proc));
  81. if (proc == NULL)
  82. {
  83. return NULL;
  84. }
  85. proc->p_name = kstrdup(name);
  86. if (proc->p_name == NULL)
  87. {
  88. kfree(proc);
  89. return NULL;
  90. }
  91. proc->kids = newlist();
  92. if (!(proc->kids))
  93. {
  94. kfree(proc);
  95. return NULL;
  96. }
  97. threadarray_init(&proc->p_threads);
  98. spinlock_init(&proc->p_lock);
  99. proc->waitlock = lock_create("threadlock");
  100. proc->waiting = cv_create("threadcv");
  101. /* VM fields */
  102. proc->p_addrspace = NULL;
  103. /* VFS fields */
  104. proc->p_cwd = NULL;
  105. #ifdef UW
  106. proc->console = NULL;
  107. #endif // UW
  108. // My additions
  109. proc->pid =0;
  110. proc->parent = NULL;
  111. proc->exitcode = -1;
  112. return proc;
  113. }
  114. static procs * create_procs(void)
  115. {
  116. procs * temp = kmalloc(sizeof(procs));
  117. if (!(temp)) panic("Could not create process list!\n");
  118. temp->lastpid = PID_MIN - 1;
  119. return temp;
  120. }
  121. struct proc * getChild(struct proc * p, int pid)
  122. {
  123. int result = listearch(p->kids, pid);
  124. if (!(result)) return NULL;
  125. return processes->pids[pid];
  126. }
  127. int assignpid(struct proc * proc)
  128. {
  129. lock_acquire(proclock);
  130. for (int i = processes->lastpid + 1; i <= PID_MAX; i++)
  131. {
  132. if (processes->pids[i] == 0)
  133. {
  134. proc->pid = i;
  135. processes->lastpid = i;
  136. processes->pids[i] = proc;
  137. lock_release(proclock);
  138. return i;
  139. }
  140. if (i == PID_MAX)
  141. {
  142. i = PID_MIN - 1;
  143. continue;
  144. }
  145. if (i == processes->lastpid)
  146. {
  147. lock_release(proclock);
  148. return 0;
  149. }
  150. }
  151. lock_release(proclock);
  152. return 0;
  153. }
  154. void add_child(struct proc * parent, int pid)
  155. {
  156. listsert(parent->kids, pid);
  157. }
  158. /*
  159. static void delete_procs()
  160. {
  161. kfree(processes->pids);
  162. lock_destroy(proclock);
  163. }*/
  164. /*
  165. * Destroy a proc structure.
  166. */
  167. void proc_destroy(struct proc * proc)
  168. {
  169. /*
  170. * note: some parts of the process structure, such as the address space,
  171. * are destroyed in sys_exit, before we get here
  172. *
  173. * note: depending on where this function is called from, curproc may not
  174. * be defined because the calling thread may have already detached itself
  175. * from the process.
  176. */
  177. KASSERT(proc != NULL);
  178. KASSERT(proc != kproc);
  179. /*
  180. * We don't take p_lock in here because we must have the only
  181. * reference to this structure. (Otherwise it would be
  182. * incorrect to destroy it.)
  183. */
  184. kprintf("TRYING TO DESTROY CWD\n");
  185. /* VFS fields */
  186. if (proc->p_cwd)
  187. {
  188. VOP_DECREF(proc->p_cwd);
  189. proc->p_cwd = NULL;
  190. }
  191. kprintf("DESTROYED CWD\n");
  192. #ifndef UW // in the UW version, space destruction occurs in sys_exit, not here
  193. if (proc->p_addrspace)
  194. {
  195. /*
  196. * In case p is the currently running process (which
  197. * it might be in some circumstances, or if this code
  198. * gets moved into exit as suggested above), clear
  199. * p_addrspace before calling as_destroy. Otherwise if
  200. * as_destroy sleeps (which is quite possible) when we
  201. * come back we'll be calling as_activate on a
  202. * half-destroyed address space. This tends to be
  203. * messily fatal.
  204. */
  205. struct addrspace * as;
  206. as_deactivate();
  207. as = curproc_setas(NULL);
  208. as_destroy(as);
  209. }
  210. #endif // UW
  211. kprintf("DESTROYED AS AGAIN\n");
  212. /*
  213. #ifdef UW
  214. if (proc->console)
  215. {
  216. vfs_close(proc->console);
  217. }
  218. #endif // UW*/
  219. kprintf("DESTROYED VFS\n");
  220. //threadarray_cleanup(&proc->p_threads);
  221. //spinlock_cleanup(&proc->p_lock);
  222. kprintf("CLEANUP DONE\n");
  223. lock_acquire(proclock);
  224. processes->pids[proc->pid] = NULL; // update available PIDs
  225. lock_release(proclock);
  226. kprintf("PID RELEASED\n");
  227. //listelete(proc->kids);
  228. lock_destroy(proc->waitlock);
  229. cv_destroy(proc->waiting);
  230. kprintf("CV DESTROYED\n");
  231. kfree(proc->p_name);
  232. kfree(proc);
  233. kprintf("KFREES DONE\n");
  234. #ifdef UW
  235. /* decrement the process count */
  236. /* note: kproc is not included as_copy in the process count, but proc_destroy
  237. is never called on kproc (see KASSERT above), so we're OK to decrement
  238. the proc_count unconditionally here */
  239. P(proc_count_mutex);
  240. KASSERT(proc_count > 0);
  241. proc_count--;
  242. /* signal the kernel menu thread if the process count has reached zero */
  243. if (proc_count == 0)
  244. {
  245. V(no_proc_sem);
  246. }
  247. V(proc_count_mutex);
  248. #endif // UW
  249. kprintf("DESTROYS DONE\n");
  250. }
  251. /*
  252. * Create the process structure for the kernel.
  253. */
  254. void proc_bootstrap(void)
  255. {
  256. kproc = proc_create("[kernel]");
  257. if (kproc == NULL)
  258. {
  259. panic("proc_create for kproc failed\n");
  260. }
  261. #ifdef UW
  262. proc_count = 0;
  263. proc_count_mutex = sem_create("proc_count_mutex",1);
  264. if (proc_count_mutex == NULL)
  265. {
  266. panic("could not create proc_count_mutex semaphore\n");
  267. }
  268. no_proc_sem = sem_create("no_proc_sem",0);
  269. if (no_proc_sem == NULL)
  270. {
  271. panic("could not create no_proc_sem semaphore\n");
  272. }
  273. #endif // UW
  274. processes = create_procs();
  275. int processesLen = PID_MAX;
  276. for (int i = 0; i < processesLen; i++)
  277. {
  278. processes->pids[i] = NULL;
  279. }
  280. proclock = lock_create("proclock");
  281. if (!(proclock)) panic("Process manager lock could not be created!\n");
  282. }
  283. /*
  284. * Create a fresh proc for use by runprogram.
  285. *
  286. * It will have no address space and will inherit the current
  287. * process's (that is, the kernel menu's) current directory.
  288. */
  289. struct proc * proc_create_runprogram(const char * name)
  290. {
  291. struct proc * proc;
  292. char * console_path;
  293. proc = proc_create(name);
  294. if (proc == NULL)
  295. {
  296. return NULL;
  297. }
  298. #ifdef UW
  299. /* open the console - this should always succeed */
  300. console_path = kstrdup("con:");
  301. if (console_path == NULL)
  302. {
  303. panic("unable to copy console path name during process creation\n");
  304. }
  305. if (vfs_open(console_path,O_WRONLY,0,&(proc->console)))
  306. {
  307. panic("unable to open the console during process creation\n");
  308. }
  309. kfree(console_path);
  310. #endif // UW
  311. /* VM fields */
  312. proc->p_addrspace = NULL;
  313. /* VFS fields */
  314. #ifdef UW
  315. /* we do not need to acquire the p_lock here, the running thread should
  316. have the only reference to this process */
  317. /* also, acquiring the p_lock is problematic because VOP_INCREF may block */
  318. if (curproc->p_cwd != NULL)
  319. {
  320. VOP_INCREF(curproc->p_cwd);
  321. proc->p_cwd = curproc->p_cwd;
  322. }
  323. #else // UW
  324. spinlock_acquire(&curproc->p_lock);
  325. if (curproc->p_cwd != NULL)
  326. {
  327. VOP_INCREF(curproc->p_cwd);
  328. proc->p_cwd = curproc->p_cwd;
  329. }
  330. spinlock_release(&curproc->p_lock);
  331. #endif // UW
  332. #ifdef UW
  333. /* increment the count of processes */
  334. /* we are assuming that all procs, including those created by fork(),
  335. are created using a call to proc_create_runprogram */
  336. P(proc_count_mutex);
  337. proc_count++;
  338. if (!(assignpid(proc)))
  339. {
  340. kfree(proc);
  341. return NULL;
  342. }
  343. V(proc_count_mutex);
  344. #endif // UW
  345. return proc;
  346. }
  347. /*
  348. * Add a thread to a process. Either the thread or the process might
  349. * or might not be current.
  350. */
  351. int proc_addthread(struct proc * proc, struct thread * t)
  352. {
  353. int result;
  354. KASSERT(t->t_proc == NULL);
  355. spinlock_acquire(&proc->p_lock);
  356. result = threadarray_add(&proc->p_threads, t, NULL);
  357. spinlock_release(&proc->p_lock);
  358. if (result)
  359. {
  360. return result;
  361. }
  362. t->t_proc = proc;
  363. return 0;
  364. }
  365. /*
  366. * Remove a thread from its process. Either the thread or the process
  367. * might or might not be current.
  368. */
  369. void proc_remthread(struct thread * t)
  370. {
  371. struct proc *proc;
  372. unsigned i, num;
  373. proc = t->t_proc;
  374. KASSERT(proc != NULL);
  375. spinlock_acquire(&proc->p_lock);
  376. /* ugh: find the thread in the array */
  377. num = threadarray_num(&proc->p_threads);
  378. for (i=0; i<num; i++)
  379. {
  380. if (threadarray_get(&proc->p_threads, i) == t)
  381. {
  382. threadarray_remove(&proc->p_threads, i);
  383. spinlock_release(&proc->p_lock);
  384. t->t_proc = NULL;
  385. return;
  386. }
  387. }
  388. /* Did not find it. */
  389. spinlock_release(&proc->p_lock);
  390. panic("Thread (%p) has escaped from its process (%p)\n", t, proc);
  391. }
  392. /*
  393. * Fetch the address space of the current process. Caution: it isn't
  394. * refcounted. If you implement multithreaded processes, make sure to
  395. * set up a refcount scheme or some other method to make this safe.
  396. */
  397. struct addrspace * curproc_getas(void)
  398. {
  399. struct addrspace *as;
  400. #ifdef UW
  401. /* Until user processes are created, threads used in testing
  402. * (i.e., kernel threads) have no process or address space.
  403. */
  404. if (curproc == NULL)
  405. {
  406. return NULL;
  407. }
  408. #endif
  409. spinlock_acquire(&curproc->p_lock);
  410. as = curproc->p_addrspace;
  411. spinlock_release(&curproc->p_lock);
  412. return as;
  413. }
  414. /*
  415. * Change the address space of the current process, and return the old
  416. * one.
  417. */
  418. struct addrspace * curproc_setas(struct addrspace * newas)
  419. {
  420. struct addrspace * oldas;
  421. struct proc * proc = curproc;
  422. spinlock_acquire(&proc->p_lock);
  423. oldas = proc->p_addrspace;
  424. proc->p_addrspace = newas;
  425. spinlock_release(&proc->p_lock);
  426. return oldas;
  427. }