proc.c 9.6 KB

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