Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.29 KB | None | 0 0
  1. #include "userprog/process.h"
  2. #include <debug.h>
  3. #include <inttypes.h>
  4. #include <round.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "userprog/gdt.h"
  9. #include "userprog/pagedir.h"
  10. #include "userprog/tss.h"
  11. #include "filesys/directory.h"
  12. #include "filesys/file.h"
  13. #include "filesys/filesys.h"
  14. #include "threads/flags.h"
  15. #include "threads/init.h"
  16. #include "threads/interrupt.h"
  17. #include "threads/palloc.h"
  18. #include "threads/thread.h"
  19. #include "threads/vaddr.h"
  20.  
  21. static thread_func start_process NO_RETURN;
  22. static bool load(const char *cmdline, void (**eip)(void), void **esp,char*save_ptr);
  23.  
  24. /* Starts a new thread running a user program loaded from
  25. FILENAME. The new thread may be scheduled (and may even exit)
  26. before process_execute() returns. Returns the new process's
  27. thread id, or TID_ERROR if the thread cannot be created. */
  28. tid_t process_execute(const char *file_name) {
  29. char *fn_copy;
  30. tid_t tid;
  31.  
  32. /* Make a copy of FILE_NAME.
  33. Otherwise there's a race between the caller and load(). */
  34. fn_copy = palloc_get_page(0);
  35. if (fn_copy == NULL)
  36. return TID_ERROR;
  37.  
  38. strlcpy(fn_copy, file_name, PGSIZE);
  39.  
  40. /* Create a new thread to execute FILE_NAME. */
  41.  
  42. // ^__^
  43. char *save_ptr;
  44. file_name = strtok_r(file_name, " ", &save_ptr);
  45.  
  46. tid = thread_create(file_name, PRI_DEFAULT, start_process, fn_copy);
  47. if (tid == TID_ERROR)
  48. palloc_free_page(fn_copy);
  49. return tid;
  50. }
  51.  
  52. /* A thread function that loads a user process and starts it
  53. running. */
  54. static void start_process(void *file_name_) {
  55. char *file_name = file_name_;
  56. struct intr_frame if_;
  57. bool success;
  58. char *save_ptr;
  59. //printf("heehehehehehehe [%s]\n", file_name);
  60. file_name = strtok_r(file_name, " ", &save_ptr);
  61. // printf("heehehehehehehe2 [%s]\n", file_name);
  62. /* Initialize interrupt frame and load executable. */
  63. memset(&if_, 0, sizeof if_);
  64. if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG;
  65. if_.cs = SEL_UCSEG;
  66. if_.eflags = FLAG_IF | FLAG_MBS;
  67. success = load(file_name, &if_.eip, &if_.esp,save_ptr);
  68.  
  69. /* If load failed, quit. */
  70. palloc_free_page(file_name);
  71. if (!success)
  72. thread_exit();
  73.  
  74. /* Start the user process by simulating a return from an
  75. interrupt, implemented by intr_exit (in
  76. threads/intr-stubs.S). Because intr_exit takes all of its
  77. arguments on the stack in the form of a `struct intr_frame',
  78. we just point the stack pointer (%esp) to our stack frame
  79. and jump to it. */
  80. asm volatile ("movl %0, %%esp; jmp intr_exit" : : "g" (&if_) : "memory");
  81. NOT_REACHED ()
  82. ;
  83. }
  84.  
  85. struct child * check_tid(struct thread * new_thread,tid_t i){
  86. struct list_elem *e;
  87. for (e = list_begin (&new_thread->child_list); e != list_end (&new_thread->child_list);
  88. e = list_next (e))
  89. {
  90. struct child *chil = list_entry (e, struct child, ellem);
  91. if(chil->tidd==i){
  92. return chil;
  93. }
  94. }
  95. return NULL;
  96. }
  97. // ^_^ ma haza ?!
  98. /* Waits for thread TID to die and returns its exit status. If
  99. it was terminated by the kernel (i.e. killed due to an
  100. exception), returns -1. If TID is invalid or if it was not a
  101. child of the calling process, or if process_wait() has already
  102. been successfully called for the given TID, returns -1
  103. immediately, without waiting.
  104.  
  105. This function will be implemented in problem 2-2. For now, it
  106. does nothing. */
  107. int process_wait(tid_t child_tid ) {
  108. //int i;
  109. //for(i = 0;i<3999999999;i++);
  110. //return 0;
  111.  
  112. /////////////////////////////////////////
  113.  
  114. struct child * new_child =check_tid(thread_current(),child_tid);
  115.  
  116. if(new_child !=NULL){
  117. //struct thread *t=get_by_tid(child_tid);
  118. //lock_acquire(&t->process_lock);
  119. if(new_child->exit==1){
  120. //lock_release(&t->process_lock);
  121. return new_child->exit_status;
  122. }
  123. else{
  124. //cond_wait(&t->cond,&t->process_lock);
  125. //lock_release(&t->process_lock);
  126. while(new_child->exit == 0)
  127. barrier();
  128. return new_child->exit_status;
  129. }
  130. }
  131. return -1;
  132.  
  133.  
  134. }
  135.  
  136. /* Free the current process's resources. */
  137. void process_exit(void) {
  138. struct thread *cur = thread_current();
  139. uint32_t *pd;
  140.  
  141. /* Destroy the current process's page directory and switch back
  142. to the kernel-only page directory. */
  143. pd = cur->pagedir;
  144. if (pd != NULL) {
  145. /* Correct ordering here is crucial. We must set
  146. cur->pagedir to NULL before switching page directories,
  147. so that a timer interrupt can't switch back to the
  148. process page directory. We must activate the base page
  149. directory before destroying the process's page
  150. directory, or our active page directory will be one
  151. that's been freed (and cleared). */
  152. cur->pagedir = NULL;
  153. pagedir_activate(NULL);
  154. pagedir_destroy(pd);
  155. }
  156. }
  157.  
  158. /* Sets up the CPU for running user code in the current
  159. thread.
  160. This function is called on every context switch. */
  161. void process_activate(void) {
  162. struct thread *t = thread_current();
  163.  
  164. /* Activate thread's page tables. */
  165. pagedir_activate(t->pagedir);
  166.  
  167. /* Set thread's kernel stack for use in processing
  168. interrupts. */
  169. tss_update();
  170. }
  171. /* We load ELF binaries. The following definitions are taken
  172. from the ELF specification, [ELF1], more-or-less verbatim. */
  173.  
  174. /* ELF types. See [ELF1] 1-2. */
  175. typedef uint32_t Elf32_Word, Elf32_Addr, Elf32_Off;
  176. typedef uint16_t Elf32_Half;
  177.  
  178. /* For use with ELF types in printf(). */
  179. #define PE32Wx PRIx32 /* Print Elf32_Word in hexadecimal. */
  180. #define PE32Ax PRIx32 /* Print Elf32_Addr in hexadecimal. */
  181. #define PE32Ox PRIx32 /* Print Elf32_Off in hexadecimal. */
  182. #define PE32Hx PRIx16 /* Print Elf32_Half in hexadecimal. */
  183.  
  184. /* Executable header. See [ELF1] 1-4 to 1-8.
  185. This appears at the very beginning of an ELF binary. */
  186. struct Elf32_Ehdr {
  187. unsigned char e_ident[16];
  188. Elf32_Half e_type;
  189. Elf32_Half e_machine;
  190. Elf32_Word e_version;
  191. Elf32_Addr e_entry;
  192. Elf32_Off e_phoff;
  193. Elf32_Off e_shoff;
  194. Elf32_Word e_flags;
  195. Elf32_Half e_ehsize;
  196. Elf32_Half e_phentsize;
  197. Elf32_Half e_phnum;
  198. Elf32_Half e_shentsize;
  199. Elf32_Half e_shnum;
  200. Elf32_Half e_shstrndx;
  201. };
  202.  
  203. /* Program header. See [ELF1] 2-2 to 2-4.
  204. There are e_phnum of these, starting at file offset e_phoff
  205. (see [ELF1] 1-6). */
  206. struct Elf32_Phdr {
  207. Elf32_Word p_type;
  208. Elf32_Off p_offset;
  209. Elf32_Addr p_vaddr;
  210. Elf32_Addr p_paddr;
  211. Elf32_Word p_filesz;
  212. Elf32_Word p_memsz;
  213. Elf32_Word p_flags;
  214. Elf32_Word p_align;
  215. };
  216.  
  217. /* Values for p_type. See [ELF1] 2-3. */
  218. #define PT_NULL 0 /* Ignore. */
  219. #define PT_LOAD 1 /* Loadable segment. */
  220. #define PT_DYNAMIC 2 /* Dynamic linking info. */
  221. #define PT_INTERP 3 /* Name of dynamic loader. */
  222. #define PT_NOTE 4 /* Auxiliary info. */
  223. #define PT_SHLIB 5 /* Reserved. */
  224. #define PT_PHDR 6 /* Program header table. */
  225. #define PT_STACK 0x6474e551 /* Stack segment. */
  226.  
  227. /* Flags for p_flags. See [ELF3] 2-3 and 2-4. */
  228. #define PF_X 1 /* Executable. */
  229. #define PF_W 2 /* Writable. */
  230. #define PF_R 4 /* Readable. */
  231. #define DEFAULT_ARGV 2
  232. #define WORD_SIZE 4
  233.  
  234. static bool setup_stack(void **esp, const char *file_name,char* save_ptr);
  235. static bool validate_segment(const struct Elf32_Phdr *, struct file *);
  236. static bool load_segment(struct file *file, off_t ofs, uint8_t *upage,
  237. uint32_t read_bytes, uint32_t zero_bytes,
  238. bool writable);
  239. static void fill_stack(void **esp, const char *file_name,char* save_ptr);
  240. /* Loads an ELF executable from FILE_NAME into the current thread.
  241. Stores the executable's entry point into *EIP
  242. and its initial stack pointer into *ESP.
  243. Returns true if successful, false otherwise. */
  244. bool load(const char *file_name, void (**eip)(void), void **esp,char * save_ptr) {
  245. struct thread *t = thread_current();
  246. struct Elf32_Ehdr ehdr;
  247. struct file *file = NULL;
  248. off_t file_ofs;
  249. bool success = false;
  250. int i;
  251.  
  252. /* Allocate and activate page directory. */
  253. t->pagedir = pagedir_create();
  254. if (t->pagedir == NULL)
  255. goto done;
  256. process_activate();
  257.  
  258. /* Open executable file. */
  259. file = filesys_open(file_name);
  260. if (file == NULL) {
  261. printf("load: %s: open failed\n", file_name);
  262. goto done;
  263. }
  264.  
  265. /* Read and verify executable header. */
  266. if (file_read(file, &ehdr, sizeof ehdr) != sizeof ehdr
  267. || memcmp(ehdr.e_ident, "\177ELF\1\1\1", 7) || ehdr.e_type != 2
  268. || ehdr.e_machine != 3 || ehdr.e_version != 1
  269. || ehdr.e_phentsize != sizeof(struct Elf32_Phdr)
  270. || ehdr.e_phnum > 1024) {
  271. printf("load: %s: error loading executable\n", file_name);
  272. goto done;
  273. }
  274.  
  275. /* Read program headers. */
  276. file_ofs = ehdr.e_phoff;
  277. for (i = 0; i < ehdr.e_phnum; i++) {
  278. struct Elf32_Phdr phdr;
  279.  
  280. if (file_ofs < 0 || file_ofs > file_length(file))
  281. goto done;
  282. file_seek(file, file_ofs);
  283.  
  284. if (file_read(file, &phdr, sizeof phdr) != sizeof phdr)
  285. goto done;
  286. file_ofs += sizeof phdr;
  287. switch (phdr.p_type) {
  288. case PT_NULL:
  289. case PT_NOTE:
  290. case PT_PHDR:
  291. case PT_STACK:
  292. default:
  293. /* Ignore this segment. */
  294. break;
  295. case PT_DYNAMIC:
  296. case PT_INTERP:
  297. case PT_SHLIB:
  298. goto done;
  299. case PT_LOAD:
  300. if (validate_segment(&phdr, file)) {
  301. bool writable = (phdr.p_flags & PF_W) != 0;
  302. uint32_t file_page = phdr.p_offset & ~PGMASK;
  303. uint32_t mem_page = phdr.p_vaddr & ~PGMASK;
  304. uint32_t page_offset = phdr.p_vaddr & PGMASK;
  305. uint32_t read_bytes, zero_bytes;
  306. if (phdr.p_filesz > 0) {
  307. /* Normal segment.
  308. Read initial part from disk and zero the rest. */
  309. read_bytes = page_offset + phdr.p_filesz;
  310. zero_bytes = (ROUND_UP(page_offset + phdr.p_memsz, PGSIZE)
  311. - read_bytes);
  312. } else {
  313. /* Entirely zero.
  314. Don't read anything from disk. */
  315. read_bytes = 0;
  316. zero_bytes = ROUND_UP(page_offset + phdr.p_memsz, PGSIZE);
  317. }
  318. if (!load_segment(file, file_page, (void *) mem_page,
  319. read_bytes, zero_bytes, writable))
  320. goto done;
  321. } else
  322. goto done;
  323. break;
  324. }
  325. }
  326.  
  327. /* Set up stack. */
  328. if (!setup_stack(esp, file_name,save_ptr))
  329. goto done;
  330.  
  331. /* Start address. */
  332. *eip = (void (*)(void)) ehdr.e_entry;
  333.  
  334. success = true;
  335.  
  336. done:
  337. /* We arrive here whether the load is successful or not. */
  338. file_close(file);
  339. return success;
  340. }
  341. /* load() helpers. */
  342.  
  343. static bool install_page(void *upage, void *kpage, bool writable);
  344.  
  345. /* Checks whether PHDR describes a valid, loadable segment in
  346. FILE and returns true if so, false otherwise. */
  347. static bool validate_segment(const struct Elf32_Phdr *phdr, struct file *file) {
  348. /* p_offset and p_vaddr must have the same page offset. */
  349. if ((phdr->p_offset & PGMASK) != (phdr->p_vaddr & PGMASK))
  350. return false;
  351.  
  352. /* p_offset must point within FILE. */
  353. if (phdr->p_offset > (Elf32_Off) file_length(file))
  354. return false;
  355.  
  356. /* p_memsz must be at least as big as p_filesz. */
  357. if (phdr->p_memsz < phdr->p_filesz)
  358. return false;
  359.  
  360. /* The segment must not be empty. */
  361. if (phdr->p_memsz == 0)
  362. return false;
  363.  
  364. /* The virtual memory region must both start and end within the
  365. user address space range. */
  366. if (!is_user_vaddr((void *) phdr->p_vaddr))
  367. return false;
  368. if (!is_user_vaddr((void *) (phdr->p_vaddr + phdr->p_memsz)))
  369. return false;
  370.  
  371. /* The region cannot "wrap around" across the kernel virtual
  372. address space. */
  373. if (phdr->p_vaddr + phdr->p_memsz < phdr->p_vaddr)
  374. return false;
  375.  
  376. /* Disallow mapping page 0.
  377. Not only is it a bad idea to map page 0, but if we allowed
  378. it then user code that passed a null pointer to system calls
  379. could quite likely panic the kernel by way of null pointer
  380. assertions in memcpy(), etc. */
  381. if (phdr->p_vaddr < PGSIZE)
  382. return false;
  383.  
  384. /* It's okay. */
  385. return true;
  386. }
  387.  
  388. /* Loads a segment starting at offset OFS in FILE at address
  389. UPAGE. In total, READ_BYTES + ZERO_BYTES bytes of virtual
  390. memory are initialized, as follows:
  391.  
  392. - READ_BYTES bytes at UPAGE must be read from FILE
  393. starting at offset OFS.
  394.  
  395. - ZERO_BYTES bytes at UPAGE + READ_BYTES must be zeroed.
  396.  
  397. The pages initialized by this function must be writable by the
  398. user process if WRITABLE is true, read-only otherwise.
  399.  
  400. Return true if successful, false if a memory allocation error
  401. or disk read error occurs. */
  402. static bool load_segment(struct file *file, off_t ofs, uint8_t *upage,
  403. uint32_t read_bytes, uint32_t zero_bytes, bool writable) {
  404. ASSERT((read_bytes + zero_bytes) % PGSIZE == 0);
  405. ASSERT(pg_ofs(upage) == 0);
  406. ASSERT(ofs % PGSIZE == 0);
  407.  
  408. file_seek(file, ofs);
  409. while (read_bytes > 0 || zero_bytes > 0) {
  410. /* Calculate how to fill this page.
  411. We will read PAGE_READ_BYTES bytes from FILE
  412. and zero the final PAGE_ZERO_BYTES bytes. */
  413. size_t page_read_bytes = read_bytes < PGSIZE ? read_bytes : PGSIZE;
  414. size_t page_zero_bytes = PGSIZE - page_read_bytes;
  415.  
  416. /* Get a page of memory. */
  417. uint8_t *kpage = palloc_get_page(PAL_USER);
  418. if (kpage == NULL)
  419. return false;
  420.  
  421. /* Load this page. */
  422. if (file_read(file, kpage, page_read_bytes) != (int) page_read_bytes) {
  423. palloc_free_page(kpage);
  424. return false;
  425. }
  426. memset(kpage + page_read_bytes, 0, page_zero_bytes);
  427.  
  428. /* Add the page to the process's address space. */
  429. if (!install_page(upage, kpage, writable)) {
  430. palloc_free_page(kpage);
  431. return false;
  432. }
  433.  
  434. /* Advance. */
  435. read_bytes -= page_read_bytes;
  436. zero_bytes -= page_zero_bytes;
  437. upage += PGSIZE;
  438. }
  439. return true;
  440. }
  441.  
  442. /* Create a minimal stack by mapping a zeroed page at the top of
  443. user virtual memory. */
  444. static bool setup_stack(void **esp, const char *file_name,char* save_ptr) {
  445. // uint8_t --> unsigned char
  446. uint8_t *kpage;
  447. bool success = false;
  448.  
  449. kpage = palloc_get_page(PAL_USER | PAL_ZERO);
  450. if (kpage != NULL) {
  451. success = install_page(((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
  452. if (success) {
  453. *esp = PHYS_BASE;
  454. fill_stack(esp, file_name,save_ptr);
  455. } else
  456. palloc_free_page(kpage);
  457. }
  458. return success;
  459. }
  460. static void
  461. fill_stack(void **esp, const char *file_name,char *save_ptr) {
  462. int pointerArraySize=2;
  463. char *current;
  464. char **pointerArray=malloc(2*sizeof(char *));
  465. int numOfArgs = 0;
  466. int length = 0; // num of chars used for allignement
  467. //printf("heehehehehehehe3 [%s]\n", file_name);
  468. // put args in reverse order , ex: /bin x we will push /bin then x
  469. for (current = (char *)file_name; current != NULL;current = strtok_r(NULL, " ", &save_ptr)) {
  470. //printf("current =[%s]\n",current );
  471. *esp -= strlen(current) + 1; // strlen msh bt7sb el null char
  472. // save pointer to the current arg
  473. pointerArray[numOfArgs] = *esp;
  474. numOfArgs++;
  475.  
  476. if (numOfArgs>= 65) { // Let's allow a maximum of 65 arguments.
  477. palloc_free_page ((void*)file_name);
  478. thread_exit();
  479. }
  480.  
  481. // put current arg in stack
  482. length += strlen(current) + 1;
  483. if (numOfArgs >= pointerArraySize)
  484. {
  485. pointerArraySize *= 2;
  486. pointerArray = realloc(pointerArray, pointerArraySize*sizeof(char *));
  487. }
  488. memcpy(*esp, current, strlen(current) + 1);
  489. }
  490. pointerArray[numOfArgs] = 0; // due stack specs , see page 16
  491.  
  492. // Alignement for fast access , #Micro #LiLi
  493.  
  494. *esp -= (4 - length % 4) % 4;
  495.  
  496. // Push all addresses
  497. int i = 0;
  498. for (i = numOfArgs; i >= 0; i--) {
  499. *esp -= sizeof(char *);
  500. memcpy(*esp, &pointerArray[i], sizeof(char *));
  501. }
  502. /////////// add char **arrayOfPointers//////////
  503. int shift = sizeof(*esp);
  504. current = *esp;
  505. *esp -= shift;
  506. memcpy(*esp, &current, shift);
  507.  
  508. ////// add num of args ////////
  509. *esp -= sizeof(int);
  510. memcpy(*esp, &numOfArgs, sizeof(int));
  511.  
  512. //// add 7mada bl gnzbel ^_^////
  513. *esp -= sizeof(void *);
  514. memcpy(*esp, &pointerArray[numOfArgs], sizeof(void *));
  515. free(pointerArray);
  516.  
  517. }
  518.  
  519. /* Adds a mapping from user virtual address UPAGE to kernel
  520. virtual address KPAGE to the page table.
  521. If WRITABLE is true, the user process may modify the page;
  522. otherwise, it is read-only.
  523. UPAGE must not already be mapped.
  524. KPAGE should probably be a page obtained from the user pool
  525. with palloc_get_page().
  526. Returns true on success, false if UPAGE is already mapped or
  527. if memory allocation fails. */
  528. static bool install_page(void *upage, void *kpage, bool writable) {
  529. struct thread *t = thread_current();
  530.  
  531. /* Verify that there's not already a page at that virtual
  532. address, then map our page there. */
  533. return (pagedir_get_page(t->pagedir, upage) == NULL
  534. && pagedir_set_page(t->pagedir, upage, kpage, writable));
  535. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement