proc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. typedef struct list list;
  54. typedef struct procs procs;
  55. //The process for the kernel; this holds all the kernel-only threads.
  56. struct proc * kproc;
  57. // The process array
  58. procs * procs;
  59. // The lock for the above array
  60. struct lock * proclock;
  61. /*
  62. * Mechanism for making the kernel menu thread sleep while processes are running
  63. */
  64. #ifdef UW
  65. /* count of the number of processes, excluding kproc */
  66. static volatile unsigned int proc_count;
  67. /* provides mutual exclusion for proc_count */
  68. /* it would be better to use a lock here, but we use a semaphore because locks are not implemented in the base kernel */
  69. static struct semaphore * proc_count_mutex;
  70. /* used to signal the kernel menu thread when there are no processes */
  71. struct semaphore * no_proc_sem;
  72. #endif // UW
  73. /*
  74. * Create a proc structure.
  75. */
  76. static struct proc * proc_create(const char * name)
  77. {
  78. struct proc * proc;
  79. proc = kmalloc(sizeof(*proc));
  80. if (proc == NULL)
  81. {
  82. return NULL;
  83. }
  84. proc->p_name = kstrdup(name);
  85. if (proc->p_name == NULL)
  86. {
  87. kfree(proc);
  88. return NULL;
  89. }
  90. proc->kids = newlist();
  91. if (!(proc->kids))
  92. {
  93. kfree(proc);
  94. return NULL;
  95. }
  96. threadarray_init(&proc->p_threads);
  97. spinlock_init(&proc->p_lock);
  98. /* VM fields */
  99. proc->p_addrspace = NULL;
  100. /* VFS fields */
  101. proc->p_cwd = NULL;
  102. #ifdef UW
  103. proc->console = NULL;
  104. #endif // UW
  105. // My additions
  106. proc->pid =0;
  107. proc->parent = NULL;
  108. proc->kids = NULL;
  109. return proc;
  110. }
  111. static procs * create_procs(void)
  112. {
  113. procs * temp = kmalloc(sizeof procs);
  114. if (!(temp)) panic("Could not create process list!\n");
  115. temp->lastpid = PID_MIN - 1;
  116. return temp;
  117. }
  118. proc * getChild(struct proc * p, int pid)
  119. {
  120. int result = listearch(p->kids, pid);
  121. if (!(result)) return NULL;
  122. return procs->pids[pid];
  123. }
  124. void delete_procs(procs * procs)
  125. {
  126. kfree(procs->pids);
  127. lock_destroy(proclock);
  128. }
  129. /*
  130. * Destroy a proc structure.
  131. */
  132. void proc_destroy(struct proc * proc)
  133. {
  134. /*
  135. * note: some parts of the process structure, such as the address space,
  136. * are destroyed in sys_exit, before we get here
  137. *
  138. * note: depending on where this function is called from, curproc may not
  139. * be defined because the calling thread may have already detached itself
  140. * from the process.
  141. */
  142. KASSERT(proc != NULL);
  143. KASSERT(proc != kproc);
  144. /*
  145. * We don't take p_lock in here because we must have the only
  146. * reference to this structure. (Otherwise it would be
  147. * incorrect to destroy it.)
  148. */
  149. /* VFS fields */
  150. if (proc->p_cwd)
  151. {
  152. VOP_DECREF(proc->p_cwd);
  153. proc->p_cwd = NULL;
  154. }
  155. #ifndef UW // in the UW version, space destruction occurs in sys_exit, not here
  156. if (proc->p_addrspace)
  157. {
  158. /*
  159. * In case p is the currently running process (which
  160. * it might be in some circumstances, or if this code
  161. * gets moved into exit as suggested above), clear
  162. * p_addrspace before calling as_destroy. Otherwise if
  163. * as_destroy sleeps (which is quite possible) when we
  164. * come back we'll be calling as_activate on a
  165. * half-destroyed address space. This tends to be
  166. * messily fatal.
  167. */
  168. struct addrspace * as;
  169. as_deactivate();
  170. as = curproc_setas(NULL);
  171. as_destroy(as);
  172. }
  173. #endif // UW
  174. #ifdef UW
  175. if (proc->console)
  176. {
  177. vfs_close(proc->console);
  178. }
  179. #endif // UW
  180. threadarray_cleanup(&proc->p_threads);
  181. spinlock_cleanup(&proc->p_lock);
  182. kfree(proc->p_name);
  183. kfree(proc);
  184. #ifdef UW
  185. /* decrement the process count */
  186. /* note: kproc is not included in the process count, but proc_destroy
  187. is never called on kproc (see KASSERT above), so we're OK to decrement
  188. the proc_count unconditionally here */
  189. P(proc_count_mutex);
  190. KASSERT(proc_count > 0);
  191. proc_count--;
  192. /* signal the kernel menu thread if the process count has reached zero */
  193. if (proc_count == 0)
  194. {
  195. V(no_proc_sem);
  196. }
  197. V(proc_count_mutex);
  198. #endif // UW
  199. }
  200. /*
  201. * Create the process structure for the kernel.
  202. */
  203. void proc_bootstrap(void)
  204. {
  205. kproc = proc_create("[kernel]");
  206. if (kproc == NULL)
  207. {
  208. panic("proc_create for kproc failed\n");
  209. }
  210. #ifdef UW
  211. proc_count = 0;
  212. proc_count_mutex = sem_create("proc_count_mutex",1);
  213. if (proc_count_mutex == NULL)
  214. {
  215. panic("could not create proc_count_mutex semaphore\n");
  216. }
  217. no_proc_sem = sem_create("no_proc_sem",0);
  218. if (no_proc_sem == NULL)
  219. {
  220. panic("could not create no_proc_sem semaphore\n");
  221. }
  222. #endif // UW
  223. }
  224. /*
  225. * Create a fresh proc for use by runprogram.
  226. *
  227. * It will have no address space and will inherit the current
  228. * process's (that is, the kernel menu's) current directory.
  229. */
  230. struct proc * proc_create_runprogram(const char * name)
  231. {
  232. struct proc * proc;
  233. char * console_path;
  234. proc = proc_create(name);
  235. if (proc == NULL)
  236. {
  237. return NULL;
  238. }
  239. #ifdef UW
  240. /* open the console - this should always succeed */
  241. console_path = kstrdup("con:");
  242. if (console_path == NULL)
  243. {
  244. panic("unable to copy console path name during process creation\n");
  245. }
  246. if (vfs_open(console_path,O_WRONLY,0,&(proc->console)))
  247. {
  248. panic("unable to open the console during process creation\n");
  249. }
  250. kfree(console_path);
  251. #endif // UW
  252. /* VM fields */
  253. proc->p_addrspace = NULL;
  254. /* VFS fields */
  255. #ifdef UW
  256. /* we do not need to acquire the p_lock here, the running thread should
  257. have the only reference to this process */
  258. /* also, acquiring the p_lock is problematic because VOP_INCREF may block */
  259. if (curproc->p_cwd != NULL)
  260. {
  261. VOP_INCREF(curproc->p_cwd);
  262. proc->p_cwd = curproc->p_cwd;
  263. }
  264. #else // UW
  265. spinlock_acquire(&curproc->p_lock);
  266. if (curproc->p_cwd != NULL)
  267. {
  268. VOP_INCREF(curproc->p_cwd);
  269. proc->p_cwd = curproc->p_cwd;
  270. }
  271. spinlock_release(&curproc->p_lock);
  272. #endif // UW
  273. #ifdef UW
  274. /* increment the count of processes */
  275. /* we are assuming that all procs, including those created by fork(),
  276. are created using a call to proc_create_runprogram */
  277. P(proc_count_mutex);
  278. proc_count++;
  279. V(proc_count_mutex);
  280. #endif // UW
  281. return proc;
  282. }
  283. /*
  284. * Add a thread to a process. Either the thread or the process might
  285. * or might not be current.
  286. */
  287. int proc_addthread(struct proc * proc, struct thread * t)
  288. {
  289. int result;
  290. KASSERT(t->t_proc == NULL);
  291. spinlock_acquire(&proc->p_lock);
  292. result = threadarray_add(&proc->p_threads, t, NULL);
  293. spinlock_release(&proc->p_lock);
  294. if (result)
  295. {
  296. return result;
  297. }
  298. t->t_proc = proc;
  299. return 0;
  300. }
  301. /*
  302. * Remove a thread from its process. Either the thread or the process
  303. * might or might not be current.
  304. */
  305. void proc_remthread(struct thread * t)
  306. {
  307. struct proc *proc;
  308. unsigned i, num;
  309. proc = t->t_proc;
  310. KASSERT(proc != NULL);
  311. spinlock_acquire(&proc->p_lock);
  312. /* ugh: find the thread in the array */
  313. num = threadarray_num(&proc->p_threads);
  314. for (i=0; i<num; i++)
  315. {
  316. if (threadarray_get(&proc->p_threads, i) == t)
  317. {
  318. threadarray_remove(&proc->p_threads, i);
  319. spinlock_release(&proc->p_lock);
  320. t->t_proc = NULL;
  321. return;
  322. }
  323. }
  324. /* Did not find it. */
  325. spinlock_release(&proc->p_lock);
  326. panic("Thread (%p) has escaped from its process (%p)\n", t, proc);
  327. }
  328. /*
  329. * Fetch the address space of the current process. Caution: it isn't
  330. * refcounted. If you implement multithreaded processes, make sure to
  331. * set up a refcount scheme or some other method to make this safe.
  332. */
  333. struct addrspace * curproc_getas(void)
  334. {
  335. struct addrspace *as;
  336. #ifdef UW
  337. /* Until user processes are created, threads used in testing
  338. * (i.e., kernel threads) have no process or address space.
  339. */
  340. if (curproc == NULL)
  341. {
  342. return NULL;
  343. }
  344. #endif
  345. spinlock_acquire(&curproc->p_lock);
  346. as = curproc->p_addrspace;
  347. spinlock_release(&curproc->p_lock);
  348. return as;
  349. }
  350. /*
  351. * Change the address space of the current process, and return the old
  352. * one.
  353. */
  354. struct addrspace * curproc_setas(struct addrspace * newas)
  355. {
  356. struct addrspace * oldas;
  357. struct proc * proc = curproc;
  358. spinlock_acquire(&proc->p_lock);
  359. oldas = proc->p_addrspace;
  360. proc->p_addrspace = newas;
  361. spinlock_release(&proc->p_lock);
  362. return oldas;
  363. }