《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
15 KiB

  1. #include <proc.h>
  2. #include <kmalloc.h>
  3. #include <string.h>
  4. #include <sync.h>
  5. #include <pmm.h>
  6. #include <error.h>
  7. #include <sched.h>
  8. #include <elf.h>
  9. //#include <vmm.h>
  10. #include <trap.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. /* ------------- process/thread mechanism design&implementation -------------
  15. (an simplified Linux process/thread mechanism )
  16. introduction:
  17. ucore implements a simple process/thread mechanism. process contains the independent memory sapce, at least one threads
  18. for execution, the kernel data(for management), processor state (for context switch), files(in lab6), etc. ucore needs to
  19. manage all these details efficiently. In ucore, a thread is just a special kind of process(share process's memory).
  20. ------------------------------
  21. process state : meaning -- reason
  22. PROC_UNINIT : uninitialized -- alloc_proc
  23. PROC_SLEEPING : sleeping -- try_free_pages, do_wait, do_sleep
  24. PROC_RUNNABLE : runnable(maybe running) -- proc_init, wakeup_proc,
  25. PROC_ZOMBIE : almost dead -- do_exit
  26. -----------------------------
  27. process state changing:
  28. alloc_proc RUNNING
  29. + +--<----<--+
  30. + + proc_run +
  31. V +-->---->--+
  32. PROC_UNINIT -- proc_init/wakeup_proc --> PROC_RUNNABLE -- try_free_pages/do_wait/do_sleep --> PROC_SLEEPING --
  33. A + +
  34. | +--- do_exit --> PROC_ZOMBIE +
  35. + +
  36. -----------------------wakeup_proc----------------------------------
  37. -----------------------------
  38. process relations
  39. parent: proc->parent (proc is children)
  40. children: proc->cptr (proc is parent)
  41. older sibling: proc->optr (proc is younger sibling)
  42. younger sibling: proc->yptr (proc is older sibling)
  43. -----------------------------
  44. related syscall for process:
  45. SYS_exit : process exit, -->do_exit
  46. SYS_fork : create child process, dup mm -->do_fork-->wakeup_proc
  47. SYS_wait : wait process -->do_wait
  48. SYS_exec : after fork, process execute a program -->load a program and refresh the mm
  49. SYS_clone : create child thread -->do_fork-->wakeup_proc
  50. SYS_yield : process flag itself need resecheduling, -- proc->need_sched=1, then scheduler will rescheule this process
  51. SYS_sleep : process sleep -->do_sleep
  52. SYS_kill : kill process -->do_kill-->proc->flags |= PF_EXITING
  53. -->wakeup_proc-->do_wait-->do_exit
  54. SYS_getpid : get the process's pid
  55. */
  56. // the process set's list
  57. list_entry_t proc_list;
  58. // idle proc
  59. struct proc_struct *idleproc = NULL;
  60. // init procs
  61. struct proc_struct *initproc1 = NULL;
  62. struct proc_struct *initproc2 = NULL;
  63. // current proc
  64. struct proc_struct *current = NULL;
  65. static int nr_process = 0;
  66. void kernel_thread_entry(void);
  67. void forkrets(struct trapframe *tf);
  68. void switch_to(struct context *from, struct context *to);
  69. // alloc_proc - alloc a proc_struct and init all fields of proc_struct
  70. static struct proc_struct *
  71. alloc_proc(void) {
  72. struct proc_struct *proc = kmalloc(sizeof(struct proc_struct));
  73. if (proc != NULL) {
  74. //LAB4:EXERCISE1 2012011346
  75. /*
  76. * below fields in proc_struct need to be initialized
  77. * enum proc_state state; // Process state
  78. * int pid; // Process ID
  79. * int runs; // the running times of Proces
  80. * uintptr_t kstack; // Process kernel stack
  81. * volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  82. * struct proc_struct *parent; // the parent process
  83. * struct mm_struct *mm; // Process's memory management field
  84. * struct context context; // Switch here to run process
  85. * struct trapframe *tf; // Trap frame for current interrupt
  86. * uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  87. * uint32_t flags; // Process flag
  88. * char name[PROC_NAME_LEN + 1]; // Process name
  89. */
  90. memset(proc, 0, sizeof(struct proc_struct));
  91. proc->state = PROC_UNINIT;
  92. proc->pid = -1;
  93. proc->cr3 = boot_cr3;
  94. }
  95. return proc;
  96. }
  97. // set_proc_name - set the name of proc
  98. char *
  99. set_proc_name(struct proc_struct *proc, const char *name) {
  100. memset(proc->name, 0, sizeof(proc->name));
  101. return memcpy(proc->name, name, PROC_NAME_LEN);
  102. }
  103. // get_proc_name - get the name of proc
  104. char *
  105. get_proc_name(struct proc_struct *proc) {
  106. static char name[PROC_NAME_LEN + 1];
  107. memset(name, 0, sizeof(name));
  108. return memcpy(name, proc->name, PROC_NAME_LEN);
  109. }
  110. // get_pid - alloc a unique pid for process
  111. static int
  112. get_pid(void) {
  113. static_assert(MAX_PID > MAX_PROCESS);
  114. struct proc_struct *proc;
  115. list_entry_t *list = &proc_list, *le;
  116. static int next_safe = MAX_PID, last_pid = MAX_PID;
  117. if (++ last_pid >= MAX_PID) {
  118. last_pid = 1;
  119. goto inside;
  120. }
  121. if (last_pid >= next_safe) {
  122. inside:
  123. next_safe = MAX_PID;
  124. repeat:
  125. le = list;
  126. while ((le = list_next(le)) != list) {
  127. proc = le2proc(le, list_link);
  128. if (proc->pid == last_pid) {
  129. if (++ last_pid >= next_safe) {
  130. if (last_pid >= MAX_PID) {
  131. last_pid = 1;
  132. }
  133. next_safe = MAX_PID;
  134. goto repeat;
  135. }
  136. }
  137. else if (proc->pid > last_pid && next_safe > proc->pid) {
  138. next_safe = proc->pid;
  139. }
  140. }
  141. }
  142. return last_pid;
  143. }
  144. // proc_run - make process "proc" running on cpu
  145. // NOTE: before call switch_to, should load base addr of "proc"'s new PDT
  146. void
  147. proc_run(struct proc_struct *proc) {
  148. if (proc != current) {
  149. bool intr_flag;
  150. struct proc_struct *prev = current, *next = proc;
  151. local_intr_save(intr_flag);
  152. {
  153. current = proc;
  154. load_esp0(next->kstack + KSTACKSIZE);
  155. lcr3(next->cr3);
  156. switch_to(&(prev->context), &(next->context));
  157. }
  158. local_intr_restore(intr_flag);
  159. }
  160. }
  161. // forkret -- the first kernel entry point of a new thread/process
  162. // NOTE: the addr of forkret is setted in copy_thread function
  163. // after switch_to, the current proc will execute here.
  164. static void
  165. forkret(void) {
  166. forkrets(current->tf);
  167. }
  168. // find_proc - find proc frome proc hash_list according to pid
  169. struct proc_struct *
  170. find_proc(int pid) {
  171. if (0 < pid && pid < MAX_PID) {
  172. list_entry_t *list = &proc_list, *le = list;
  173. while ((le = list_next(le)) != list) {
  174. struct proc_struct *proc = le2proc(le, list_link);
  175. if (proc->pid == pid) {
  176. return proc;
  177. }
  178. }
  179. }
  180. return NULL;
  181. }
  182. // kernel_thread - create a kernel thread using "fn" function
  183. // NOTE: the contents of temp trapframe tf will be copied to
  184. // proc->tf in do_fork-->copy_thread function
  185. int
  186. kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags) {
  187. struct trapframe tf;
  188. memset(&tf, 0, sizeof(struct trapframe));
  189. tf.tf_cs = KERNEL_CS;
  190. tf.tf_ds = tf.tf_es = tf.tf_ss = KERNEL_DS;
  191. tf.tf_regs.reg_ebx = (uint32_t)fn;
  192. tf.tf_regs.reg_edx = (uint32_t)arg;
  193. tf.tf_eip = (uint32_t)kernel_thread_entry;
  194. return do_fork(clone_flags | CLONE_VM, 0, &tf);
  195. }
  196. // setup_kstack - alloc pages with size KSTACKPAGE as process kernel stack
  197. static int
  198. setup_kstack(struct proc_struct *proc) {
  199. struct Page *page = alloc_pages(KSTACKPAGE);
  200. if (page != NULL) {
  201. proc->kstack = (uintptr_t)page2kva(page);
  202. return 0;
  203. }
  204. return -E_NO_MEM;
  205. }
  206. // put_kstack - free the memory space of process kernel stack
  207. static void
  208. put_kstack(struct proc_struct *proc) {
  209. free_pages(kva2page((void *)(proc->kstack)), KSTACKPAGE);
  210. }
  211. // copy_thread - setup the trapframe on the process's kernel stack top and
  212. // - setup the kernel entry point and stack of process
  213. static void
  214. copy_thread(struct proc_struct *proc, uintptr_t esp, struct trapframe *tf) {
  215. proc->tf = (struct trapframe *)(proc->kstack + KSTACKSIZE) - 1;
  216. *(proc->tf) = *tf;
  217. proc->tf->tf_regs.reg_eax = 0;
  218. proc->tf->tf_esp = esp;
  219. proc->tf->tf_eflags |= FL_IF;
  220. proc->context.eip = (uintptr_t)forkret;
  221. proc->context.esp = (uintptr_t)(proc->tf);
  222. }
  223. /* do_fork - parent process for a new child process
  224. * @clone_flags: used to guide how to clone the child process
  225. * @stack: the parent's user stack pointer. if stack==0, It means to fork a kernel thread.
  226. * @tf: the trapframe info, which will be copied to child process's proc->tf
  227. */
  228. int
  229. do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
  230. int ret = -E_NO_FREE_PROC;
  231. struct proc_struct *proc;
  232. if (nr_process >= MAX_PROCESS) {
  233. goto fork_out;
  234. }
  235. ret = -E_NO_MEM;
  236. //LAB4:EXERCISE2 2012011346
  237. /*
  238. * Some Useful MACROs, Functions and DEFINEs, you can use them in below implementation.
  239. * MACROs or Functions:
  240. * alloc_proc: create a proc struct and init fields (lab4:exercise1)
  241. * setup_kstack: alloc pages with size KSTACKPAGE as process kernel stack
  242. * copy_thread: setup the trapframe on the process's kernel stack top and
  243. * setup the kernel entry point and stack of process
  244. * hash_proc: add proc into proc hash_list
  245. * get_pid: alloc a unique pid for process
  246. * wakeup_proc: set proc->state = PROC_RUNNABLE
  247. * VARIABLES:
  248. * proc_list: the process set's list
  249. * nr_process: the number of process set
  250. */
  251. // 1. call alloc_proc to allocate a proc_struct
  252. proc = alloc_proc();
  253. proc->pid = get_pid();
  254. // 2. call setup_kstack to allocate a kernel stack for child process
  255. setup_kstack(proc);
  256. // 3. call copy_thread to setup tf & context in proc_struct
  257. copy_thread(proc, stack, tf);
  258. // 4. insert proc_struct into proc_list
  259. list_add_before(&proc_list, &proc->list_link);
  260. // 5. call wakeup_proc to make the new child process RUNNABLE
  261. wakeup_proc(proc);
  262. // 7. set ret vaule using child proc's pid
  263. nr_process++;
  264. ret = proc->pid;
  265. // 8. set parent
  266. proc->parent=current;
  267. fork_out:
  268. return ret;
  269. bad_fork_cleanup_kstack:
  270. put_kstack(proc);
  271. bad_fork_cleanup_proc:
  272. kfree(proc);
  273. goto fork_out;
  274. }
  275. // remove_links - clean the relation links of process
  276. static void
  277. remove_links(struct proc_struct *proc) {
  278. list_del(&(proc->list_link));
  279. nr_process --;
  280. }
  281. // do_wait - wait one OR any children with PROC_ZOMBIE state, and free memory space of kernel stack
  282. // - proc struct of this child.
  283. // NOTE: only after do_wait function, all resources of the child proces are free.
  284. int
  285. do_wait(int pid, int *code_store) {
  286. struct proc_struct *proc;
  287. bool intr_flag, haskid;
  288. repeat:
  289. cprintf("do_wait: begin\n");
  290. haskid = 0;
  291. list_entry_t *list = &proc_list, *le = list;
  292. while ((le = list_next(le)) != list) {
  293. proc = le2proc(le, list_link);
  294. if (proc != NULL) {
  295. haskid = 1;
  296. if (proc->state == PROC_ZOMBIE) {
  297. goto found;
  298. }
  299. }
  300. }
  301. if (haskid) {
  302. cprintf("do_wait: has kid begin\n");
  303. current->state = PROC_SLEEPING;
  304. current->wait_state = WT_CHILD;
  305. schedule();
  306. goto repeat;
  307. }
  308. return -E_BAD_PROC;
  309. found:
  310. cprintf("do_wait: has kid find child pid%d\n",proc->pid);
  311. if (proc == idleproc ) {
  312. panic("wait idleproc \n");
  313. }
  314. local_intr_save(intr_flag);
  315. {
  316. remove_links(proc);
  317. }
  318. local_intr_restore(intr_flag);
  319. put_kstack(proc);
  320. kfree(proc);
  321. return 0;
  322. }
  323. // do_exit - called by sys_exit
  324. // 1. set process' state as PROC_ZOMBIE, then call wakeup_proc(parent) to ask parent reclaim itself.
  325. // 2. call scheduler to switch to other process
  326. int
  327. do_exit(int error_code) {
  328. if (current == idleproc) {
  329. panic("idleproc exit.\n");
  330. }
  331. cprintf(" do_exit: proc pid %d will exit\n", current->pid);
  332. cprintf(" do_exit: proc parent %x\n", current->parent);
  333. current->state = PROC_ZOMBIE;
  334. bool intr_flag;
  335. struct proc_struct *proc;
  336. local_intr_save(intr_flag);
  337. {
  338. proc = current->parent;
  339. if (proc->wait_state == WT_CHILD) {
  340. wakeup_proc(proc);
  341. }
  342. }
  343. local_intr_restore(intr_flag);
  344. schedule();
  345. panic("do_exit will not return!! %d.\n", current->pid);
  346. }
  347. // init_main - the second kernel thread used to create user_main kernel threads
  348. static int
  349. init_main(void *arg) {
  350. cprintf(" kernel_thread, pid = %d, name = %s\n", current->pid, get_proc_name(current));
  351. schedule();
  352. cprintf(" kernel_thread, pid = %d, name = %s , arg %s \n", current->pid, get_proc_name(current), (const char *)arg);
  353. schedule();
  354. cprintf(" kernel_thread, pid = %d, name = %s , en.., Bye, Bye. :)\n",current->pid, get_proc_name(current));
  355. return 0;
  356. }
  357. // proc_init - set up the first kernel thread idleproc "idle" by itself and
  358. // - create the second kernel thread init_main
  359. void
  360. proc_init(void) {
  361. int i;
  362. list_init(&proc_list);
  363. if ((idleproc = alloc_proc()) == NULL) {
  364. panic("cannot alloc idleproc.\n");
  365. }
  366. idleproc->pid = 0;
  367. idleproc->state = PROC_RUNNABLE;
  368. idleproc->kstack = (uintptr_t)bootstack;
  369. idleproc->need_resched = 1;
  370. set_proc_name(idleproc, "idle");
  371. nr_process ++;
  372. current = idleproc;
  373. int pid1= kernel_thread(init_main, "init main1: Hello world!!", 0);
  374. int pid2= kernel_thread(init_main, "init main2: Hello world!!", 0);
  375. if (pid1 <= 0 || pid2<=0) {
  376. panic("create kernel thread init_main1 or 2 failed.\n");
  377. }
  378. initproc1 = find_proc(pid1);
  379. initproc2 = find_proc(pid2);
  380. set_proc_name(initproc1, "init1");
  381. set_proc_name(initproc2, "init2");
  382. cprintf("proc_init:: Created kernel thread init_main--> pid: %d, name: %s\n",initproc1->pid, initproc1->name);
  383. cprintf("proc_init:: Created kernel thread init_main--> pid: %d, name: %s\n",initproc2->pid, initproc2->name);
  384. assert(idleproc != NULL && idleproc->pid == 0);
  385. }
  386. // cpu_idle - at the end of kern_init, the first kernel thread idleproc will do below works
  387. void
  388. cpu_idle(void) {
  389. while (1) {
  390. if (current->need_resched) {
  391. schedule();
  392. }
  393. }
  394. }