《操作系统》的实验代码。
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.

896 lines
29 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  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. #include <unistd.h>
  15. /* ------------- process/thread mechanism design&implementation -------------
  16. (an simplified Linux process/thread mechanism )
  17. introduction:
  18. ucore implements a simple process/thread mechanism. process contains the independent memory sapce, at least one threads
  19. for execution, the kernel data(for management), processor state (for context switch), files(in lab6), etc. ucore needs to
  20. manage all these details efficiently. In ucore, a thread is just a special kind of process(share process's memory).
  21. ------------------------------
  22. process state : meaning -- reason
  23. PROC_UNINIT : uninitialized -- alloc_proc
  24. PROC_SLEEPING : sleeping -- try_free_pages, do_wait, do_sleep
  25. PROC_RUNNABLE : runnable(maybe running) -- proc_init, wakeup_proc,
  26. PROC_ZOMBIE : almost dead -- do_exit
  27. -----------------------------
  28. process state changing:
  29. alloc_proc RUNNING
  30. + +--<----<--+
  31. + + proc_run +
  32. V +-->---->--+
  33. PROC_UNINIT -- proc_init/wakeup_proc --> PROC_RUNNABLE -- try_free_pages/do_wait/do_sleep --> PROC_SLEEPING --
  34. A + +
  35. | +--- do_exit --> PROC_ZOMBIE +
  36. + +
  37. -----------------------wakeup_proc----------------------------------
  38. -----------------------------
  39. process relations
  40. parent: proc->parent (proc is children)
  41. children: proc->cptr (proc is parent)
  42. older sibling: proc->optr (proc is younger sibling)
  43. younger sibling: proc->yptr (proc is older sibling)
  44. -----------------------------
  45. related syscall for process:
  46. SYS_exit : process exit, -->do_exit
  47. SYS_fork : create child process, dup mm -->do_fork-->wakeup_proc
  48. SYS_wait : wait process -->do_wait
  49. SYS_exec : after fork, process execute a program -->load a program and refresh the mm
  50. SYS_clone : create child thread -->do_fork-->wakeup_proc
  51. SYS_yield : process flag itself need resecheduling, -- proc->need_sched=1, then scheduler will rescheule this process
  52. SYS_sleep : process sleep -->do_sleep
  53. SYS_kill : kill process -->do_kill-->proc->flags |= PF_EXITING
  54. -->wakeup_proc-->do_wait-->do_exit
  55. SYS_getpid : get the process's pid
  56. */
  57. // the process set's list
  58. list_entry_t proc_list;
  59. #define HASH_SHIFT 10
  60. #define HASH_LIST_SIZE (1 << HASH_SHIFT)
  61. #define pid_hashfn(x) (hash32(x, HASH_SHIFT))
  62. // has list for process set based on pid
  63. static list_entry_t hash_list[HASH_LIST_SIZE];
  64. // idle proc
  65. struct proc_struct *idleproc = NULL;
  66. // init proc
  67. struct proc_struct *initproc = NULL;
  68. // current proc
  69. struct proc_struct *current = NULL;
  70. static int nr_process = 0;
  71. void kernel_thread_entry(void);
  72. void forkrets(struct trapframe *tf);
  73. void switch_to(struct context *from, struct context *to);
  74. // alloc_proc - alloc a proc_struct and init all fields of proc_struct
  75. static struct proc_struct *
  76. alloc_proc(void) {
  77. struct proc_struct *proc = kmalloc(sizeof(struct proc_struct));
  78. if (proc != NULL) {
  79. //LAB4:EXERCISE1 YOUR CODE
  80. /*
  81. * below fields in proc_struct need to be initialized
  82. * enum proc_state state; // Process state
  83. * int pid; // Process ID
  84. * int runs; // the running times of Proces
  85. * uintptr_t kstack; // Process kernel stack
  86. * volatile bool need_resched; // bool value: need to be rescheduled to release CPU?
  87. * struct proc_struct *parent; // the parent process
  88. * struct mm_struct *mm; // Process's memory management field
  89. * struct context context; // Switch here to run process
  90. * struct trapframe *tf; // Trap frame for current interrupt
  91. * uintptr_t cr3; // CR3 register: the base addr of Page Directroy Table(PDT)
  92. * uint32_t flags; // Process flag
  93. * char name[PROC_NAME_LEN + 1]; // Process name
  94. */
  95. //LAB5 YOUR CODE : (update LAB4 steps)
  96. /*
  97. * below fields(add in LAB5) in proc_struct need to be initialized
  98. * uint32_t wait_state; // waiting state
  99. * struct proc_struct *cptr, *yptr, *optr; // relations between processes
  100. */
  101. //LAB6 YOUR CODE : (update LAB5 steps)
  102. /*
  103. * below fields(add in LAB6) in proc_struct need to be initialized
  104. * struct run_queue *rq; // running queue contains Process
  105. * list_entry_t run_link; // the entry linked in run queue
  106. * int time_slice; // time slice for occupying the CPU
  107. * skew_heap_entry_t lab6_run_pool; // FOR LAB6 ONLY: the entry in the run pool
  108. * uint32_t lab6_stride; // FOR LAB6 ONLY: the current stride of the process
  109. * uint32_t lab6_priority; // FOR LAB6 ONLY: the priority of process, set by lab6_set_priority(uint32_t)
  110. */
  111. }
  112. return proc;
  113. }
  114. // set_proc_name - set the name of proc
  115. char *
  116. set_proc_name(struct proc_struct *proc, const char *name) {
  117. memset(proc->name, 0, sizeof(proc->name));
  118. return memcpy(proc->name, name, PROC_NAME_LEN);
  119. }
  120. // get_proc_name - get the name of proc
  121. char *
  122. get_proc_name(struct proc_struct *proc) {
  123. static char name[PROC_NAME_LEN + 1];
  124. memset(name, 0, sizeof(name));
  125. return memcpy(name, proc->name, PROC_NAME_LEN);
  126. }
  127. // set_links - set the relation links of process
  128. static void
  129. set_links(struct proc_struct *proc) {
  130. list_add(&proc_list, &(proc->list_link));
  131. proc->yptr = NULL;
  132. if ((proc->optr = proc->parent->cptr) != NULL) {
  133. proc->optr->yptr = proc;
  134. }
  135. proc->parent->cptr = proc;
  136. nr_process ++;
  137. }
  138. // remove_links - clean the relation links of process
  139. static void
  140. remove_links(struct proc_struct *proc) {
  141. list_del(&(proc->list_link));
  142. if (proc->optr != NULL) {
  143. proc->optr->yptr = proc->yptr;
  144. }
  145. if (proc->yptr != NULL) {
  146. proc->yptr->optr = proc->optr;
  147. }
  148. else {
  149. proc->parent->cptr = proc->optr;
  150. }
  151. nr_process --;
  152. }
  153. // get_pid - alloc a unique pid for process
  154. static int
  155. get_pid(void) {
  156. static_assert(MAX_PID > MAX_PROCESS);
  157. struct proc_struct *proc;
  158. list_entry_t *list = &proc_list, *le;
  159. static int next_safe = MAX_PID, last_pid = MAX_PID;
  160. if (++ last_pid >= MAX_PID) {
  161. last_pid = 1;
  162. goto inside;
  163. }
  164. if (last_pid >= next_safe) {
  165. inside:
  166. next_safe = MAX_PID;
  167. repeat:
  168. le = list;
  169. while ((le = list_next(le)) != list) {
  170. proc = le2proc(le, list_link);
  171. if (proc->pid == last_pid) {
  172. if (++ last_pid >= next_safe) {
  173. if (last_pid >= MAX_PID) {
  174. last_pid = 1;
  175. }
  176. next_safe = MAX_PID;
  177. goto repeat;
  178. }
  179. }
  180. else if (proc->pid > last_pid && next_safe > proc->pid) {
  181. next_safe = proc->pid;
  182. }
  183. }
  184. }
  185. return last_pid;
  186. }
  187. // proc_run - make process "proc" running on cpu
  188. // NOTE: before call switch_to, should load base addr of "proc"'s new PDT
  189. void
  190. proc_run(struct proc_struct *proc) {
  191. if (proc != current) {
  192. bool intr_flag;
  193. struct proc_struct *prev = current, *next = proc;
  194. local_intr_save(intr_flag);
  195. {
  196. current = proc;
  197. load_esp0(next->kstack + KSTACKSIZE);
  198. lcr3(next->cr3);
  199. switch_to(&(prev->context), &(next->context));
  200. }
  201. local_intr_restore(intr_flag);
  202. }
  203. }
  204. // forkret -- the first kernel entry point of a new thread/process
  205. // NOTE: the addr of forkret is setted in copy_thread function
  206. // after switch_to, the current proc will execute here.
  207. static void
  208. forkret(void) {
  209. forkrets(current->tf);
  210. }
  211. // hash_proc - add proc into proc hash_list
  212. static void
  213. hash_proc(struct proc_struct *proc) {
  214. list_add(hash_list + pid_hashfn(proc->pid), &(proc->hash_link));
  215. }
  216. // unhash_proc - delete proc from proc hash_list
  217. static void
  218. unhash_proc(struct proc_struct *proc) {
  219. list_del(&(proc->hash_link));
  220. }
  221. // find_proc - find proc frome proc hash_list according to pid
  222. struct proc_struct *
  223. find_proc(int pid) {
  224. if (0 < pid && pid < MAX_PID) {
  225. list_entry_t *list = hash_list + pid_hashfn(pid), *le = list;
  226. while ((le = list_next(le)) != list) {
  227. struct proc_struct *proc = le2proc(le, hash_link);
  228. if (proc->pid == pid) {
  229. return proc;
  230. }
  231. }
  232. }
  233. return NULL;
  234. }
  235. // kernel_thread - create a kernel thread using "fn" function
  236. // NOTE: the contents of temp trapframe tf will be copied to
  237. // proc->tf in do_fork-->copy_thread function
  238. int
  239. kernel_thread(int (*fn)(void *), void *arg, uint32_t clone_flags) {
  240. struct trapframe tf;
  241. memset(&tf, 0, sizeof(struct trapframe));
  242. tf.tf_cs = KERNEL_CS;
  243. tf.tf_ds = tf.tf_es = tf.tf_ss = KERNEL_DS;
  244. tf.tf_regs.reg_ebx = (uint32_t)fn;
  245. tf.tf_regs.reg_edx = (uint32_t)arg;
  246. tf.tf_eip = (uint32_t)kernel_thread_entry;
  247. return do_fork(clone_flags | CLONE_VM, 0, &tf);
  248. }
  249. // setup_kstack - alloc pages with size KSTACKPAGE as process kernel stack
  250. static int
  251. setup_kstack(struct proc_struct *proc) {
  252. struct Page *page = alloc_pages(KSTACKPAGE);
  253. if (page != NULL) {
  254. proc->kstack = (uintptr_t)page2kva(page);
  255. return 0;
  256. }
  257. return -E_NO_MEM;
  258. }
  259. // put_kstack - free the memory space of process kernel stack
  260. static void
  261. put_kstack(struct proc_struct *proc) {
  262. free_pages(kva2page((void *)(proc->kstack)), KSTACKPAGE);
  263. }
  264. // setup_pgdir - alloc one page as PDT
  265. static int
  266. setup_pgdir(struct mm_struct *mm) {
  267. struct Page *page;
  268. if ((page = alloc_page()) == NULL) {
  269. return -E_NO_MEM;
  270. }
  271. pde_t *pgdir = page2kva(page);
  272. memcpy(pgdir, boot_pgdir, PGSIZE);
  273. pgdir[PDX(VPT)] = PADDR(pgdir) | PTE_P | PTE_W;
  274. mm->pgdir = pgdir;
  275. return 0;
  276. }
  277. // put_pgdir - free the memory space of PDT
  278. static void
  279. put_pgdir(struct mm_struct *mm) {
  280. free_page(kva2page(mm->pgdir));
  281. }
  282. // copy_mm - process "proc" duplicate OR share process "current"'s mm according clone_flags
  283. // - if clone_flags & CLONE_VM, then "share" ; else "duplicate"
  284. static int
  285. copy_mm(uint32_t clone_flags, struct proc_struct *proc) {
  286. struct mm_struct *mm, *oldmm = current->mm;
  287. /* current is a kernel thread */
  288. if (oldmm == NULL) {
  289. return 0;
  290. }
  291. if (clone_flags & CLONE_VM) {
  292. mm = oldmm;
  293. goto good_mm;
  294. }
  295. int ret = -E_NO_MEM;
  296. if ((mm = mm_create()) == NULL) {
  297. goto bad_mm;
  298. }
  299. if (setup_pgdir(mm) != 0) {
  300. goto bad_pgdir_cleanup_mm;
  301. }
  302. lock_mm(oldmm);
  303. {
  304. ret = dup_mmap(mm, oldmm);
  305. }
  306. unlock_mm(oldmm);
  307. if (ret != 0) {
  308. goto bad_dup_cleanup_mmap;
  309. }
  310. good_mm:
  311. mm_count_inc(mm);
  312. proc->mm = mm;
  313. proc->cr3 = PADDR(mm->pgdir);
  314. return 0;
  315. bad_dup_cleanup_mmap:
  316. exit_mmap(mm);
  317. put_pgdir(mm);
  318. bad_pgdir_cleanup_mm:
  319. mm_destroy(mm);
  320. bad_mm:
  321. return ret;
  322. }
  323. // copy_thread - setup the trapframe on the process's kernel stack top and
  324. // - setup the kernel entry point and stack of process
  325. static void
  326. copy_thread(struct proc_struct *proc, uintptr_t esp, struct trapframe *tf) {
  327. proc->tf = (struct trapframe *)(proc->kstack + KSTACKSIZE) - 1;
  328. *(proc->tf) = *tf;
  329. proc->tf->tf_regs.reg_eax = 0;
  330. proc->tf->tf_esp = esp;
  331. proc->tf->tf_eflags |= FL_IF;
  332. proc->context.eip = (uintptr_t)forkret;
  333. proc->context.esp = (uintptr_t)(proc->tf);
  334. }
  335. /* do_fork - parent process for a new child process
  336. * @clone_flags: used to guide how to clone the child process
  337. * @stack: the parent's user stack pointer. if stack==0, It means to fork a kernel thread.
  338. * @tf: the trapframe info, which will be copied to child process's proc->tf
  339. */
  340. int
  341. do_fork(uint32_t clone_flags, uintptr_t stack, struct trapframe *tf) {
  342. int ret = -E_NO_FREE_PROC;
  343. struct proc_struct *proc;
  344. if (nr_process >= MAX_PROCESS) {
  345. goto fork_out;
  346. }
  347. ret = -E_NO_MEM;
  348. //LAB4:EXERCISE2 YOUR CODE
  349. /*
  350. * Some Useful MACROs, Functions and DEFINEs, you can use them in below implementation.
  351. * MACROs or Functions:
  352. * alloc_proc: create a proc struct and init fields (lab4:exercise1)
  353. * setup_kstack: alloc pages with size KSTACKPAGE as process kernel stack
  354. * copy_mm: process "proc" duplicate OR share process "current"'s mm according clone_flags
  355. * if clone_flags & CLONE_VM, then "share" ; else "duplicate"
  356. * copy_thread: setup the trapframe on the process's kernel stack top and
  357. * setup the kernel entry point and stack of process
  358. * hash_proc: add proc into proc hash_list
  359. * get_pid: alloc a unique pid for process
  360. * wakup_proc: set proc->state = PROC_RUNNABLE
  361. * VARIABLES:
  362. * proc_list: the process set's list
  363. * nr_process: the number of process set
  364. */
  365. // 1. call alloc_proc to allocate a proc_struct
  366. // 2. call setup_kstack to allocate a kernel stack for child process
  367. // 3. call copy_mm to dup OR share mm according clone_flag
  368. // 4. call copy_thread to setup tf & context in proc_struct
  369. // 5. insert proc_struct into hash_list && proc_list
  370. // 6. call wakup_proc to make the new child process RUNNABLE
  371. // 7. set ret vaule using child proc's pid
  372. //LAB5 YOUR CODE : (update LAB4 steps)
  373. /* Some Functions
  374. * set_links: set the relation links of process. ALSO SEE: remove_links: lean the relation links of process
  375. * -------------------
  376. * update step 1: set child proc's parent to current process, make sure current process's wait_state is 0
  377. * update step 5: insert proc_struct into hash_list && proc_list, set the relation links of process
  378. */
  379. fork_out:
  380. return ret;
  381. bad_fork_cleanup_kstack:
  382. put_kstack(proc);
  383. bad_fork_cleanup_proc:
  384. kfree(proc);
  385. goto fork_out;
  386. }
  387. // do_exit - called by sys_exit
  388. // 1. call exit_mmap & put_pgdir & mm_destroy to free the almost all memory space of process
  389. // 2. set process' state as PROC_ZOMBIE, then call wakeup_proc(parent) to ask parent reclaim itself.
  390. // 3. call scheduler to switch to other process
  391. int
  392. do_exit(int error_code) {
  393. if (current == idleproc) {
  394. panic("idleproc exit.\n");
  395. }
  396. if (current == initproc) {
  397. panic("initproc exit.\n");
  398. }
  399. struct mm_struct *mm = current->mm;
  400. if (mm != NULL) {
  401. lcr3(boot_cr3);
  402. if (mm_count_dec(mm) == 0) {
  403. exit_mmap(mm);
  404. put_pgdir(mm);
  405. mm_destroy(mm);
  406. }
  407. current->mm = NULL;
  408. }
  409. current->state = PROC_ZOMBIE;
  410. current->exit_code = error_code;
  411. bool intr_flag;
  412. struct proc_struct *proc;
  413. local_intr_save(intr_flag);
  414. {
  415. proc = current->parent;
  416. if (proc->wait_state == WT_CHILD) {
  417. wakeup_proc(proc);
  418. }
  419. while (current->cptr != NULL) {
  420. proc = current->cptr;
  421. current->cptr = proc->optr;
  422. proc->yptr = NULL;
  423. if ((proc->optr = initproc->cptr) != NULL) {
  424. initproc->cptr->yptr = proc;
  425. }
  426. proc->parent = initproc;
  427. initproc->cptr = proc;
  428. if (proc->state == PROC_ZOMBIE) {
  429. if (initproc->wait_state == WT_CHILD) {
  430. wakeup_proc(initproc);
  431. }
  432. }
  433. }
  434. }
  435. local_intr_restore(intr_flag);
  436. schedule();
  437. panic("do_exit will not return!! %d.\n", current->pid);
  438. }
  439. /* load_icode - load the content of binary program(ELF format) as the new content of current process
  440. * @binary: the memory addr of the content of binary program
  441. * @size: the size of the content of binary program
  442. */
  443. static int
  444. load_icode(unsigned char *binary, size_t size) {
  445. if (current->mm != NULL) {
  446. panic("load_icode: current->mm must be empty.\n");
  447. }
  448. int ret = -E_NO_MEM;
  449. struct mm_struct *mm;
  450. //(1) create a new mm for current process
  451. if ((mm = mm_create()) == NULL) {
  452. goto bad_mm;
  453. }
  454. //(2) create a new PDT, and mm->pgdir= kernel virtual addr of PDT
  455. if (setup_pgdir(mm) != 0) {
  456. goto bad_pgdir_cleanup_mm;
  457. }
  458. //(3) copy TEXT/DATA section, build BSS parts in binary to memory space of process
  459. struct Page *page;
  460. //(3.1) get the file header of the bianry program (ELF format)
  461. struct elfhdr *elf = (struct elfhdr *)binary;
  462. //(3.2) get the entry of the program section headers of the bianry program (ELF format)
  463. struct proghdr *ph = (struct proghdr *)(binary + elf->e_phoff);
  464. //(3.3) This program is valid?
  465. if (elf->e_magic != ELF_MAGIC) {
  466. ret = -E_INVAL_ELF;
  467. goto bad_elf_cleanup_pgdir;
  468. }
  469. uint32_t vm_flags, perm;
  470. struct proghdr *ph_end = ph + elf->e_phnum;
  471. for (; ph < ph_end; ph ++) {
  472. //(3.4) find every program section headers
  473. if (ph->p_type != ELF_PT_LOAD) {
  474. continue ;
  475. }
  476. if (ph->p_filesz > ph->p_memsz) {
  477. ret = -E_INVAL_ELF;
  478. goto bad_cleanup_mmap;
  479. }
  480. if (ph->p_filesz == 0) {
  481. continue ;
  482. }
  483. //(3.5) call mm_map fun to setup the new vma ( ph->p_va, ph->p_memsz)
  484. vm_flags = 0, perm = PTE_U;
  485. if (ph->p_flags & ELF_PF_X) vm_flags |= VM_EXEC;
  486. if (ph->p_flags & ELF_PF_W) vm_flags |= VM_WRITE;
  487. if (ph->p_flags & ELF_PF_R) vm_flags |= VM_READ;
  488. if (vm_flags & VM_WRITE) perm |= PTE_W;
  489. if ((ret = mm_map(mm, ph->p_va, ph->p_memsz, vm_flags, NULL)) != 0) {
  490. goto bad_cleanup_mmap;
  491. }
  492. unsigned char *from = binary + ph->p_offset;
  493. size_t off, size;
  494. uintptr_t start = ph->p_va, end, la = ROUNDDOWN(start, PGSIZE);
  495. ret = -E_NO_MEM;
  496. //(3.6) alloc memory, and copy the contents of every program section (from, from+end) to process's memory (la, la+end)
  497. end = ph->p_va + ph->p_filesz;
  498. //(3.6.1) copy TEXT/DATA section of bianry program
  499. while (start < end) {
  500. if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) {
  501. goto bad_cleanup_mmap;
  502. }
  503. off = start - la, size = PGSIZE - off, la += PGSIZE;
  504. if (end < la) {
  505. size -= la - end;
  506. }
  507. memcpy(page2kva(page) + off, from, size);
  508. start += size, from += size;
  509. }
  510. //(3.6.2) build BSS section of binary program
  511. end = ph->p_va + ph->p_memsz;
  512. if (start < la) {
  513. /* ph->p_memsz == ph->p_filesz */
  514. if (start == end) {
  515. continue ;
  516. }
  517. off = start + PGSIZE - la, size = PGSIZE - off;
  518. if (end < la) {
  519. size -= la - end;
  520. }
  521. memset(page2kva(page) + off, 0, size);
  522. start += size;
  523. assert((end < la && start == end) || (end >= la && start == la));
  524. }
  525. while (start < end) {
  526. if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) {
  527. goto bad_cleanup_mmap;
  528. }
  529. off = start - la, size = PGSIZE - off, la += PGSIZE;
  530. if (end < la) {
  531. size -= la - end;
  532. }
  533. memset(page2kva(page) + off, 0, size);
  534. start += size;
  535. }
  536. }
  537. //(4) build user stack memory
  538. vm_flags = VM_READ | VM_WRITE | VM_STACK;
  539. if ((ret = mm_map(mm, USTACKTOP - USTACKSIZE, USTACKSIZE, vm_flags, NULL)) != 0) {
  540. goto bad_cleanup_mmap;
  541. }
  542. assert(pgdir_alloc_page(mm->pgdir, USTACKTOP-PGSIZE , PTE_USER) != NULL);
  543. assert(pgdir_alloc_page(mm->pgdir, USTACKTOP-2*PGSIZE , PTE_USER) != NULL);
  544. assert(pgdir_alloc_page(mm->pgdir, USTACKTOP-3*PGSIZE , PTE_USER) != NULL);
  545. assert(pgdir_alloc_page(mm->pgdir, USTACKTOP-4*PGSIZE , PTE_USER) != NULL);
  546. //(5) set current process's mm, sr3, and set CR3 reg = physical addr of Page Directory
  547. mm_count_inc(mm);
  548. current->mm = mm;
  549. current->cr3 = PADDR(mm->pgdir);
  550. lcr3(PADDR(mm->pgdir));
  551. //(6) setup trapframe for user environment
  552. struct trapframe *tf = current->tf;
  553. memset(tf, 0, sizeof(struct trapframe));
  554. /* LAB5:EXERCISE1 YOUR CODE
  555. * should set tf_cs,tf_ds,tf_es,tf_ss,tf_esp,tf_eip,tf_eflags
  556. * NOTICE: If we set trapframe correctly, then the user level process can return to USER MODE from kernel. So
  557. * tf_cs should be USER_CS segment (see memlayout.h)
  558. * tf_ds=tf_es=tf_ss should be USER_DS segment
  559. * tf_esp should be the top addr of user stack (USTACKTOP)
  560. * tf_eip should be the entry point of this binary program (elf->e_entry)
  561. * tf_eflags should be set to enable computer to produce Interrupt
  562. */
  563. ret = 0;
  564. out:
  565. return ret;
  566. bad_cleanup_mmap:
  567. exit_mmap(mm);
  568. bad_elf_cleanup_pgdir:
  569. put_pgdir(mm);
  570. bad_pgdir_cleanup_mm:
  571. mm_destroy(mm);
  572. bad_mm:
  573. goto out;
  574. }
  575. // do_execve - call exit_mmap(mm)&pug_pgdir(mm) to reclaim memory space of current process
  576. // - call load_icode to setup new memory space accroding binary prog.
  577. int
  578. do_execve(const char *name, size_t len, unsigned char *binary, size_t size) {
  579. struct mm_struct *mm = current->mm;
  580. if (!user_mem_check(mm, (uintptr_t)name, len, 0)) {
  581. return -E_INVAL;
  582. }
  583. if (len > PROC_NAME_LEN) {
  584. len = PROC_NAME_LEN;
  585. }
  586. char local_name[PROC_NAME_LEN + 1];
  587. memset(local_name, 0, sizeof(local_name));
  588. memcpy(local_name, name, len);
  589. if (mm != NULL) {
  590. lcr3(boot_cr3);
  591. if (mm_count_dec(mm) == 0) {
  592. exit_mmap(mm);
  593. put_pgdir(mm);
  594. mm_destroy(mm);
  595. }
  596. current->mm = NULL;
  597. }
  598. int ret;
  599. if ((ret = load_icode(binary, size)) != 0) {
  600. goto execve_exit;
  601. }
  602. set_proc_name(current, local_name);
  603. return 0;
  604. execve_exit:
  605. do_exit(ret);
  606. panic("already exit: %e.\n", ret);
  607. }
  608. // do_yield - ask the scheduler to reschedule
  609. int
  610. do_yield(void) {
  611. current->need_resched = 1;
  612. return 0;
  613. }
  614. // do_wait - wait one OR any children with PROC_ZOMBIE state, and free memory space of kernel stack
  615. // - proc struct of this child.
  616. // NOTE: only after do_wait function, all resources of the child proces are free.
  617. int
  618. do_wait(int pid, int *code_store) {
  619. struct mm_struct *mm = current->mm;
  620. if (code_store != NULL) {
  621. if (!user_mem_check(mm, (uintptr_t)code_store, sizeof(int), 1)) {
  622. return -E_INVAL;
  623. }
  624. }
  625. struct proc_struct *proc;
  626. bool intr_flag, haskid;
  627. repeat:
  628. haskid = 0;
  629. if (pid != 0) {
  630. proc = find_proc(pid);
  631. if (proc != NULL && proc->parent == current) {
  632. haskid = 1;
  633. if (proc->state == PROC_ZOMBIE) {
  634. goto found;
  635. }
  636. }
  637. }
  638. else {
  639. proc = current->cptr;
  640. for (; proc != NULL; proc = proc->optr) {
  641. haskid = 1;
  642. if (proc->state == PROC_ZOMBIE) {
  643. goto found;
  644. }
  645. }
  646. }
  647. if (haskid) {
  648. current->state = PROC_SLEEPING;
  649. current->wait_state = WT_CHILD;
  650. schedule();
  651. if (current->flags & PF_EXITING) {
  652. do_exit(-E_KILLED);
  653. }
  654. goto repeat;
  655. }
  656. return -E_BAD_PROC;
  657. found:
  658. if (proc == idleproc || proc == initproc) {
  659. panic("wait idleproc or initproc.\n");
  660. }
  661. if (code_store != NULL) {
  662. *code_store = proc->exit_code;
  663. }
  664. local_intr_save(intr_flag);
  665. {
  666. unhash_proc(proc);
  667. remove_links(proc);
  668. }
  669. local_intr_restore(intr_flag);
  670. put_kstack(proc);
  671. kfree(proc);
  672. return 0;
  673. }
  674. // do_kill - kill process with pid by set this process's flags with PF_EXITING
  675. int
  676. do_kill(int pid) {
  677. struct proc_struct *proc;
  678. if ((proc = find_proc(pid)) != NULL) {
  679. if (!(proc->flags & PF_EXITING)) {
  680. proc->flags |= PF_EXITING;
  681. if (proc->wait_state & WT_INTERRUPTED) {
  682. wakeup_proc(proc);
  683. }
  684. return 0;
  685. }
  686. return -E_KILLED;
  687. }
  688. return -E_INVAL;
  689. }
  690. // kernel_execve - do SYS_exec syscall to exec a user program called by user_main kernel_thread
  691. static int
  692. kernel_execve(const char *name, unsigned char *binary, size_t size) {
  693. int ret, len = strlen(name);
  694. asm volatile (
  695. "int %1;"
  696. : "=a" (ret)
  697. : "i" (T_SYSCALL), "0" (SYS_exec), "d" (name), "c" (len), "b" (binary), "D" (size)
  698. : "memory");
  699. return ret;
  700. }
  701. #define __KERNEL_EXECVE(name, binary, size) ({ \
  702. cprintf("kernel_execve: pid = %d, name = \"%s\".\n", \
  703. current->pid, name); \
  704. kernel_execve(name, binary, (size_t)(size)); \
  705. })
  706. #define KERNEL_EXECVE(x) ({ \
  707. extern unsigned char _binary_obj___user_##x##_out_start[], \
  708. _binary_obj___user_##x##_out_size[]; \
  709. __KERNEL_EXECVE(#x, _binary_obj___user_##x##_out_start, \
  710. _binary_obj___user_##x##_out_size); \
  711. })
  712. #define __KERNEL_EXECVE2(x, xstart, xsize) ({ \
  713. extern unsigned char xstart[], xsize[]; \
  714. __KERNEL_EXECVE(#x, xstart, (size_t)xsize); \
  715. })
  716. #define KERNEL_EXECVE2(x, xstart, xsize) __KERNEL_EXECVE2(x, xstart, xsize)
  717. // user_main - kernel thread used to exec a user program
  718. static int
  719. user_main(void *arg) {
  720. #ifdef TEST
  721. KERNEL_EXECVE2(TEST, TESTSTART, TESTSIZE);
  722. #else
  723. KERNEL_EXECVE(exit);
  724. #endif
  725. panic("user_main execve failed.\n");
  726. }
  727. // init_main - the second kernel thread used to create user_main kernel threads
  728. static int
  729. init_main(void *arg) {
  730. size_t nr_free_pages_store = nr_free_pages();
  731. size_t kernel_allocated_store = kallocated();
  732. int pid = kernel_thread(user_main, NULL, 0);
  733. if (pid <= 0) {
  734. panic("create user_main failed.\n");
  735. }
  736. extern void check_sync(void);
  737. check_sync(); // check philosopher sync problem
  738. while (do_wait(0, NULL) == 0) {
  739. schedule();
  740. }
  741. cprintf("all user-mode processes have quit.\n");
  742. assert(initproc->cptr == NULL && initproc->yptr == NULL && initproc->optr == NULL);
  743. assert(nr_process == 2);
  744. assert(list_next(&proc_list) == &(initproc->list_link));
  745. assert(list_prev(&proc_list) == &(initproc->list_link));
  746. cprintf("init check memory pass.\n");
  747. return 0;
  748. }
  749. // proc_init - set up the first kernel thread idleproc "idle" by itself and
  750. // - create the second kernel thread init_main
  751. void
  752. proc_init(void) {
  753. int i;
  754. list_init(&proc_list);
  755. for (i = 0; i < HASH_LIST_SIZE; i ++) {
  756. list_init(hash_list + i);
  757. }
  758. if ((idleproc = alloc_proc()) == NULL) {
  759. panic("cannot alloc idleproc.\n");
  760. }
  761. idleproc->pid = 0;
  762. idleproc->state = PROC_RUNNABLE;
  763. idleproc->kstack = (uintptr_t)bootstack;
  764. idleproc->need_resched = 1;
  765. set_proc_name(idleproc, "idle");
  766. nr_process ++;
  767. current = idleproc;
  768. int pid = kernel_thread(init_main, NULL, 0);
  769. if (pid <= 0) {
  770. panic("create init_main failed.\n");
  771. }
  772. initproc = find_proc(pid);
  773. set_proc_name(initproc, "init");
  774. assert(idleproc != NULL && idleproc->pid == 0);
  775. assert(initproc != NULL && initproc->pid == 1);
  776. }
  777. // cpu_idle - at the end of kern_init, the first kernel thread idleproc will do below works
  778. void
  779. cpu_idle(void) {
  780. while (1) {
  781. if (current->need_resched) {
  782. schedule();
  783. }
  784. }
  785. }
  786. //FOR LAB6, set the process's priority (bigger value will get more CPU time)
  787. void
  788. lab6_set_priority(uint32_t priority)
  789. {
  790. if (priority == 0)
  791. current->lab6_priority = 1;
  792. else current->lab6_priority = priority;
  793. }
  794. // do_sleep - set current process state to sleep and add timer with "time"
  795. // - then call scheduler. if process run again, delete timer first.
  796. int
  797. do_sleep(unsigned int time) {
  798. if (time == 0) {
  799. return 0;
  800. }
  801. bool intr_flag;
  802. local_intr_save(intr_flag);
  803. timer_t __timer, *timer = timer_init(&__timer, current, time);
  804. current->state = PROC_SLEEPING;
  805. current->wait_state = WT_TIMER;
  806. add_timer(timer);
  807. local_intr_restore(intr_flag);
  808. schedule();
  809. del_timer(timer);
  810. return 0;
  811. }