《操作系统》的实验代码。
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

372 lignes
13 KiB

il y a 12 ans
  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. #define HASH_SHIFT 10
  59. #define HASH_LIST_SIZE (1 << HASH_SHIFT)
  60. #define pid_hashfn(x) (hash32(x, HASH_SHIFT))
  61. // has list for process set based on pid
  62. static list_entry_t hash_list[HASH_LIST_SIZE];
  63. // idle proc
  64. struct proc_struct *idleproc = NULL;
  65. // init proc
  66. struct proc_struct *initproc = NULL;
  67. // current proc
  68. struct proc_struct *current = NULL;
  69. static int nr_process = 0;
  70. void kernel_thread_entry(void);
  71. void forkrets(struct trapframe *tf);
  72. void switch_to(struct context *from, struct context *to);
  73. // alloc_proc - alloc a proc_struct and init all fields of proc_struct
  74. static struct proc_struct *
  75. alloc_proc(void) {
  76. struct proc_struct *proc = kmalloc(sizeof(struct proc_struct));
  77. if (proc != NULL) {
  78. //LAB4:EXERCISE1 YOUR CODE
  79. /*
  80. * below fields in proc_struct need to be initialized
  81. * enum proc_state state; // Process state
  82. * int pid; // Process ID
  83. * int runs; // the running times of Proces
  84. * uintptr_t kstack; // Process kernel stack
  85. * volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  86. * struct proc_struct *parent; // the parent process
  87. * struct mm_struct *mm; // Process's memory management field
  88. * struct context context; // Switch here to run process
  89. * struct trapframe *tf; // Trap frame for current interrupt
  90. * uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  91. * uint32_t flags; // Process flag
  92. * char name[PROC_NAME_LEN + 1]; // Process name
  93. */
  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. // hash_proc - add proc into proc hash_list
  169. static void
  170. hash_proc(struct proc_struct *proc) {
  171. list_add(hash_list + pid_hashfn(proc->pid), &(proc->hash_link));
  172. }
  173. // find_proc - find proc frome proc hash_list according to pid
  174. struct proc_struct *
  175. find_proc(int pid) {
  176. if (0 < pid && pid < MAX_PID) {
  177. list_entry_t *list = hash_list + pid_hashfn(pid), *le = list;
  178. while ((le = list_next(le)) != list) {
  179. struct proc_struct *proc = le2proc(le, hash_link);
  180. if (proc->pid == pid) {
  181. return proc;
  182. }
  183. }
  184. }
  185. return NULL;
  186. }
  187. // kernel_thread - create a kernel thread using "fn" function
  188. // NOTE: the contents of temp trapframe tf will be copied to
  189. // proc->tf in do_fork-->copy_thread function
  190. int
  191. kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags) {
  192. struct trapframe tf;
  193. memset(&tf, 0, sizeof(struct trapframe));
  194. tf.tf_cs = KERNEL_CS;
  195. tf.tf_ds = tf.tf_es = tf.tf_ss = KERNEL_DS;
  196. tf.tf_regs.reg_ebx = (uint32_t)fn;
  197. tf.tf_regs.reg_edx = (uint32_t)arg;
  198. tf.tf_eip = (uint32_t)kernel_thread_entry;
  199. return do_fork(clone_flags | CLONE_VM, 0, &tf);
  200. }
  201. // setup_kstack - alloc pages with size KSTACKPAGE as process kernel stack
  202. static int
  203. setup_kstack(struct proc_struct *proc) {
  204. struct Page *page = alloc_pages(KSTACKPAGE);
  205. if (page != NULL) {
  206. proc->kstack = (uintptr_t)page2kva(page);
  207. return 0;
  208. }
  209. return -E_NO_MEM;
  210. }
  211. // put_kstack - free the memory space of process kernel stack
  212. static void
  213. put_kstack(struct proc_struct *proc) {
  214. free_pages(kva2page((void *)(proc->kstack)), KSTACKPAGE);
  215. }
  216. // copy_mm - process "proc" duplicate OR share process "current"'s mm according clone_flags
  217. // - if clone_flags & CLONE_VM, then "share" ; else "duplicate"
  218. static int
  219. copy_mm(uint32_t clone_flags, struct proc_struct *proc) {
  220. assert(current->mm == NULL);
  221. /* do nothing in this project */
  222. return 0;
  223. }
  224. // copy_thread - setup the trapframe on the process's kernel stack top and
  225. // - setup the kernel entry point and stack of process
  226. static void
  227. copy_thread(struct proc_struct *proc, uintptr_t esp, struct trapframe *tf) {
  228. proc->tf = (struct trapframe *)(proc->kstack + KSTACKSIZE) - 1;
  229. *(proc->tf) = *tf;
  230. proc->tf->tf_regs.reg_eax = 0;
  231. proc->tf->tf_esp = esp;
  232. proc->tf->tf_eflags |= FL_IF;
  233. proc->context.eip = (uintptr_t)forkret;
  234. proc->context.esp = (uintptr_t)(proc->tf);
  235. }
  236. /* do_fork - parent process for a new child process
  237. * @clone_flags: used to guide how to clone the child process
  238. * @stack: the parent's user stack pointer. if stack==0, It means to fork a kernel thread.
  239. * @tf: the trapframe info, which will be copied to child process's proc->tf
  240. */
  241. int
  242. do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
  243. int ret = -E_NO_FREE_PROC;
  244. struct proc_struct *proc;
  245. if (nr_process >= MAX_PROCESS) {
  246. goto fork_out;
  247. }
  248. ret = -E_NO_MEM;
  249. //LAB4:EXERCISE2 YOUR CODE
  250. /*
  251. * Some Useful MACROs, Functions and DEFINEs, you can use them in below implementation.
  252. * MACROs or Functions:
  253. * alloc_proc: create a proc struct and init fields (lab4:exercise1)
  254. * setup_kstack: alloc pages with size KSTACKPAGE as process kernel stack
  255. * copy_mm: process "proc" duplicate OR share process "current"'s mm according clone_flags
  256. * if clone_flags & CLONE_VM, then "share" ; else "duplicate"
  257. * copy_thread: setup the trapframe on the process's kernel stack top and
  258. * setup the kernel entry point and stack of process
  259. * hash_proc: add proc into proc hash_list
  260. * get_pid: alloc a unique pid for process
  261. * wakup_proc: set proc->state = PROC_RUNNABLE
  262. * VARIABLES:
  263. * proc_list: the process set's list
  264. * nr_process: the number of process set
  265. */
  266. // 1. call alloc_proc to allocate a proc_struct
  267. // 2. call setup_kstack to allocate a kernel stack for child process
  268. // 3. call copy_mm to dup OR share mm according clone_flag
  269. // 4. call copy_thread to setup tf & context in proc_struct
  270. // 5. insert proc_struct into hash_list && proc_list
  271. // 6. call wakup_proc to make the new child process RUNNABLE
  272. // 7. set ret vaule using child proc's pid
  273. fork_out:
  274. return ret;
  275. bad_fork_cleanup_kstack:
  276. put_kstack(proc);
  277. bad_fork_cleanup_proc:
  278. kfree(proc);
  279. goto fork_out;
  280. }
  281. // do_exit - called by sys_exit
  282. // 1. call exit_mmap & put_pgdir & mm_destroy to free the almost all memory space of process
  283. // 2. set process' state as PROC_ZOMBIE, then call wakeup_proc(parent) to ask parent reclaim itself.
  284. // 3. call scheduler to switch to other process
  285. int
  286. do_exit(int error_code) {
  287. panic("process exit!!.\n");
  288. }
  289. // init_main - the second kernel thread used to create user_main kernel threads
  290. static int
  291. init_main(void *arg) {
  292. cprintf("this initproc, pid = %d, name = \"%s\"\n", current->pid, get_proc_name(current));
  293. cprintf("To U: \"%s\".\n", (const char *)arg);
  294. cprintf("To U: \"en.., Bye, Bye. :)\"\n");
  295. return 0;
  296. }
  297. // proc_init - set up the first kernel thread idleproc "idle" by itself and
  298. // - create the second kernel thread init_main
  299. void
  300. proc_init(void) {
  301. int i;
  302. list_init(&proc_list);
  303. for (i = 0; i < HASH_LIST_SIZE; i ++) {
  304. list_init(hash_list + i);
  305. }
  306. if ((idleproc = alloc_proc()) == NULL) {
  307. panic("cannot alloc idleproc.\n");
  308. }
  309. idleproc->pid = 0;
  310. idleproc->state = PROC_RUNNABLE;
  311. idleproc->kstack = (uintptr_t)bootstack;
  312. idleproc->need_resched = 1;
  313. set_proc_name(idleproc, "idle");
  314. nr_process ++;
  315. current = idleproc;
  316. int pid = kernel_thread(init_main, "Hello world!!", 0);
  317. if (pid <= 0) {
  318. panic("create init_main failed.\n");
  319. }
  320. initproc = find_proc(pid);
  321. set_proc_name(initproc, "init");
  322. assert(idleproc != NULL && idleproc->pid == 0);
  323. assert(initproc != NULL && initproc->pid == 1);
  324. }
  325. // cpu_idle - at the end of kern_init, the first kernel thread idleproc will do below works
  326. void
  327. cpu_idle(void) {
  328. while (1) {
  329. if (current->need_resched) {
  330. schedule();
  331. }
  332. }
  333. }