X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=solutions%2Fp2.patch;h=00711a2a484aead2743ddcb81a6cd0bd7e86167d;hb=58ff179ed68e25b33037ca479b0f51f8fdbec7fe;hp=fc5389190d08d8ddb4a460e6bac5a2428e9f8567;hpb=15aa248a41556196803c75cb4f56ddad05f5d64e;p=pintos-anon diff --git a/solutions/p2.patch b/solutions/p2.patch index fc53891..00711a2 100644 --- a/solutions/p2.patch +++ b/solutions/p2.patch @@ -1,6 +1,7 @@ -diff -urp -X pat src/threads/thread.c~ src/threads/thread.c ---- src/threads/thread.c~ 2005-03-30 10:26:13.000000000 -0800 -+++ src/threads/thread.c 2005-03-30 16:19:26.000000000 -0800 +Index: src/threads/thread.c +diff -u src/threads/thread.c~ src/threads/thread.c +--- src/threads/thread.c~ ++++ src/threads/thread.c @@ -13,6 +13,7 @@ #include "threads/synch.h" #ifdef USERPROG @@ -9,7 +10,7 @@ diff -urp -X pat src/threads/thread.c~ src/threads/thread.c #endif /* Random value for struct thread's `magic' member. -@@ -243,16 +244,19 @@ thread_tid (void) +@@ -251,16 +252,19 @@ thread_tid (void) void thread_exit (void) { @@ -31,21 +32,21 @@ diff -urp -X pat src/threads/thread.c~ src/threads/thread.c schedule (); NOT_REACHED (); } -@@ -353,6 +357,11 @@ init_thread (struct thread *t, const cha +@@ -400,6 +404,10 @@ init_thread (struct thread *t, const cha strlcpy (t->name, name, sizeof t->name); t->stack = (uint8_t *) t + PGSIZE; t->priority = priority; + list_init (&t->children); -+ t->exit_code = -1; + t->wait_status = NULL; + list_init (&t->fds); + t->next_handle = 2; t->magic = THREAD_MAGIC; } -diff -urp -X pat src/threads/thread.h~ src/threads/thread.h ---- src/threads/thread.h~ 2005-03-30 10:26:13.000000000 -0800 -+++ src/threads/thread.h 2005-03-30 16:17:45.000000000 -0800 +Index: src/threads/thread.h +diff -u src/threads/thread.h~ src/threads/thread.h +--- src/threads/thread.h~ ++++ src/threads/thread.h @@ -4,6 +4,7 @@ #include #include @@ -54,26 +55,27 @@ diff -urp -X pat src/threads/thread.h~ src/threads/thread.h /* States in a thread's life cycle. */ enum thread_status -@@ -89,6 +90,11 @@ struct thread +@@ -89,6 +90,10 @@ struct thread uint8_t *stack; /* Saved stack pointer. */ int priority; /* Priority. */ + /* Owned by process.c. */ -+ int exit_code; /* Exit code. */ + struct wait_status *wait_status; /* This process's completion status. */ + struct list children; /* Completion status of children. */ + /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ -@@ -97,10 +103,29 @@ struct thread +@@ -96,11 +102,31 @@ struct thread + /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ #endif - ++ struct file *bin_file; /* Executable. */ ++ + /* Owned by syscall.c. */ + struct list fds; /* List of file descriptors. */ + int next_handle; /* Next handle value. */ -+ + /* Owned by thread.c. */ unsigned magic; /* Detects stack overflow. */ }; @@ -93,12 +95,13 @@ diff -urp -X pat src/threads/thread.h~ src/threads/thread.h + struct semaphore dead; /* 1=child alive, 0=child dead. */ + }; + - void thread_init (void); - void thread_start (void); - void thread_tick (void); -diff -urp -X pat src/userprog/exception.c~ src/userprog/exception.c ---- src/userprog/exception.c~ 2005-01-01 18:09:59.000000000 -0800 -+++ src/userprog/exception.c 2005-03-30 13:26:14.000000000 -0800 + /* If false (default), use round-robin scheduler. + If true, use multi-level feedback queue scheduler. + Controlled by kernel command-line options "-o mlfqs". +Index: src/userprog/exception.c +diff -u src/userprog/exception.c~ src/userprog/exception.c +--- src/userprog/exception.c~ ++++ src/userprog/exception.c @@ -150,6 +150,14 @@ page_fault (struct intr_frame *f) write = (f->error_code & PF_W) != 0; user = (f->error_code & PF_U) != 0; @@ -114,57 +117,62 @@ diff -urp -X pat src/userprog/exception.c~ src/userprog/exception.c /* To implement virtual memory, delete the rest of the function body, and replace it with code that brings in the page to which fault_addr refers. */ -diff -urp -X pat src/userprog/process.c~ src/userprog/process.c ---- src/userprog/process.c~ 2005-03-30 10:40:10.000000000 -0800 -+++ src/userprog/process.c 2005-03-30 16:19:32.000000000 -0800 +Index: src/userprog/process.c +diff -u src/userprog/process.c~ src/userprog/process.c +--- src/userprog/process.c~ ++++ src/userprog/process.c @@ -14,11 +14,23 @@ #include "threads/init.h" #include "threads/interrupt.h" - #include "threads/mmu.h" +#include "threads/malloc.h" #include "threads/palloc.h" #include "threads/thread.h" + #include "threads/vaddr.h" - static thread_func execute_thread NO_RETURN; + static thread_func start_process NO_RETURN; -static bool load (const char *cmdline, void (**eip) (void), void **esp); +static bool load (const char *cmd_line, void (**eip) (void), void **esp); + +/* Data structure shared between process_execute() in the -+ invoking thread and execute_thread() in the newly invoked ++ invoking thread and start_process() in the newly invoked + thread. */ +struct exec_info + { -+ const char *filename; /* Program to load. */ ++ const char *file_name; /* Program to load. */ + struct semaphore load_done; /* "Up"ed when loading complete. */ + struct wait_status *wait_status; /* Child process. */ + bool success; /* Program successfully loaded? */ + }; /* Starts a new thread running a user program loaded from - FILENAME. The new thread may be scheduled before -@@ -26,29 +38,33 @@ static bool load (const char *cmdline, v + FILE_NAME. The new thread may be scheduled (and may even exit) +@@ -27,29 +39,37 @@ static bool load (const char *cmdline, v tid_t - process_execute (const char *filename) + process_execute (const char *file_name) { - char *fn_copy; + struct exec_info exec; ++ char thread_name[16]; ++ char *save_ptr; tid_t tid; -- /* Make a copy of FILENAME. +- /* Make a copy of FILE_NAME. - Otherwise there's a race between the caller and load(). */ - fn_copy = palloc_get_page (0); - if (fn_copy == NULL) - return TID_ERROR; -- strlcpy (fn_copy, filename, PGSIZE); +- strlcpy (fn_copy, file_name, PGSIZE); + /* Initialize exec_info. */ -+ exec.filename = filename; -+ sema_init (&exec.load_done, 0, "load done"); ++ exec.file_name = file_name; ++ sema_init (&exec.load_done, 0); - /* Create a new thread to execute FILENAME. */ -- tid = thread_create (filename, PRI_DEFAULT, execute_thread, fn_copy); + /* Create a new thread to execute FILE_NAME. */ +- tid = thread_create (file_name, PRI_DEFAULT, start_process, fn_copy); - if (tid == TID_ERROR) - palloc_free_page (fn_copy); -+ tid = thread_create (filename, PRI_DEFAULT, execute_thread, &exec); ++ strlcpy (thread_name, file_name, sizeof thread_name); ++ strtok_r (thread_name, " ", &save_ptr); ++ tid = thread_create (thread_name, PRI_DEFAULT, start_process, &exec); + if (tid != TID_ERROR) + { + sema_down (&exec.load_done); @@ -180,20 +188,20 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c /* A thread function that loads a user process and starts it running. */ static void --execute_thread (void *filename_) -+execute_thread (void *exec_) +-start_process (void *file_name_) ++start_process (void *exec_) { -- char *filename = filename_; +- char *file_name = file_name_; + struct exec_info *exec = exec_; struct intr_frame if_; bool success; -@@ -57,10 +73,28 @@ execute_thread (void *filename_) +@@ -58,10 +78,29 @@ start_process (void *file_name_) if_.gs = if_.fs = if_.es = if_.ds = if_.ss = SEL_UDSEG; if_.cs = SEL_UCSEG; if_.eflags = FLAG_IF | FLAG_MBS; -- success = load (filename, &if_.eip, &if_.esp); -+ success = load (exec->filename, &if_.eip, &if_.esp); +- success = load (file_name, &if_.eip, &if_.esp); ++ success = load (exec->file_name, &if_.eip, &if_.esp); + + /* Allocate wait_status. */ + if (success) @@ -204,14 +212,15 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c + } - /* If load failed, quit. */ -- palloc_free_page (filename); +- palloc_free_page (file_name); + /* Initialize wait_status. */ + if (success) + { -+ lock_init (&exec->wait_status->lock, "child process"); ++ lock_init (&exec->wait_status->lock); + exec->wait_status->ref_cnt = 2; + exec->wait_status->tid = thread_current ()->tid; -+ sema_init (&exec->wait_status->dead, 0, "dead child"); ++ exec->wait_status->exit_code = -1; ++ sema_init (&exec->wait_status->dead, 0); + } + + /* Notify parent thread and clean up. */ @@ -220,7 +229,7 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c if (!success) thread_exit (); -@@ -74,19 +108,48 @@ execute_thread (void *filename_) +@@ -75,18 +113,47 @@ start_process (void *file_name_) NOT_REACHED (); } @@ -273,21 +282,21 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c return -1; } - /* Free the current process's resources. */ -@@ -93,8 +157,29 @@ void +@@ -95,8 +162,30 @@ void process_exit (void) { struct thread *cur = thread_current (); + struct list_elem *e, *next; uint32_t *pd; -+ printf ("%s: exit(%d)\n", cur->name, cur->exit_code); ++ /* Close executable (and allow writes). */ ++ file_close (cur->bin_file); + + /* Notify parent that we're dead. */ + if (cur->wait_status != NULL) + { + struct wait_status *cs = cur->wait_status; -+ cs->exit_code = cur->exit_code; ++ printf ("%s: exit(%d)\n", cur->name, cs->exit_code); + sema_up (&cs->dead); + release_child (cs); + } @@ -304,24 +313,24 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c /* Destroy the current process's page directory and switch back to the kernel-only page directory. */ pd = cur->pagedir; -@@ -192,7 +277,7 @@ struct Elf32_Phdr +@@ -193,7 +284,7 @@ struct Elf32_Phdr + #define PF_W 2 /* Writable. */ #define PF_R 4 /* Readable. */ - static bool load_segment (struct file *, const struct Elf32_Phdr *); -static bool setup_stack (void **esp); +static bool setup_stack (const char *cmd_line, void **esp); - - /* Aborts loading an executable, with an error message. */ - #define LOAD_ERROR(MSG) \ -@@ -208,13 +293,15 @@ static bool setup_stack (void **esp); + static bool validate_segment (const struct Elf32_Phdr *, struct file *); + static bool load_segment (struct file *file, off_t ofs, uint8_t *upage, + bool writable); +@@ -209,13 +300,15 @@ static bool setup_stack (void **esp); and its initial stack pointer into *ESP. Returns true if successful, false otherwise. */ bool --load (const char *filename, void (**eip) (void), void **esp) +-load (const char *file_name, void (**eip) (void), void **esp) +load (const char *cmd_line, void (**eip) (void), void **esp) { struct thread *t = thread_current (); -+ char filename[NAME_MAX + 2]; ++ char file_name[NAME_MAX + 2]; struct Elf32_Ehdr ehdr; struct file *file = NULL; off_t file_ofs; @@ -330,22 +339,31 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c int i; /* Allocate and activate page directory. */ -@@ -223,6 +310,14 @@ load (const char *filename, void (**eip) - LOAD_ERROR (("page directory allocation failed")); +@@ -224,13 +317,22 @@ load (const char *file_name, void (**eip) + goto done; process_activate (); -+ /* Extract filename from command line. */ ++ /* Extract file_name from command line. */ + while (*cmd_line == ' ') + cmd_line++; -+ strlcpy (filename, cmd_line, sizeof filename); -+ cp = strchr (filename, ' '); ++ strlcpy (file_name, cmd_line, sizeof file_name); ++ cp = strchr (file_name, ' '); + if (cp != NULL) + *cp = '\0'; + /* Open executable file. */ - file = filesys_open (filename); - if (file == NULL) -@@ -283,7 +378,7 @@ load (const char *filename, void (**eip) +- file = filesys_open (file_name); ++ t->bin_file = file = filesys_open (file_name); + if (file == NULL) + { + printf ("load: %s: open failed\n", file_name); + goto done; + } ++ file_deny_write (file); + + /* Read and verify executable header. */ + if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr +@@ -284,7 +386,7 @@ load (const char *file_name, void (**eip) } /* Set up stack. */ @@ -354,7 +372,15 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c goto done; /* Start address. */ -@@ -392,10 +487,92 @@ load_segment (struct file *file, const s +@@ -294,7 +396,6 @@ load (const char *file_name, void (**eip) + + done: + /* We arrive here whether the load is successful or not. */ +- file_close (file); + return success; + } + +@@ -393,10 +494,92 @@ load_segment (struct file *file, const s return true; } @@ -420,7 +446,7 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c + for (karg = strtok_r (cmd_line_copy, " ", &saveptr); karg != NULL; + karg = strtok_r (NULL, " ", &saveptr)) + { -+ char *uarg = upage + (karg - (char *) kpage); ++ void *uarg = upage + (karg - (char *) kpage); + if (push (kpage, &ofs, &uarg, sizeof uarg) == NULL) + return false; + argc++; @@ -450,38 +476,39 @@ diff -urp -X pat src/userprog/process.c~ src/userprog/process.c { uint8_t *kpage; bool success = false; -@@ -403,9 +580,9 @@ setup_stack (void **esp) +@@ -404,9 +587,9 @@ setup_stack (void **esp) kpage = palloc_get_page (PAL_USER | PAL_ZERO); if (kpage != NULL) { -- success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage); +- success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true); - if (success) - *esp = PHYS_BASE; + uint8_t *upage = ((uint8_t *) PHYS_BASE) - PGSIZE; -+ if (install_page (upage, kpage)) ++ if (install_page (upage, kpage, true)) + success = init_cmd_line (kpage, upage, cmd_line, esp); else palloc_free_page (kpage); } -diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c ---- src/userprog/syscall.c~ 2004-09-26 14:15:17.000000000 -0700 -+++ src/userprog/syscall.c 2005-03-30 15:11:37.000000000 -0800 -@@ -1,20 +1,478 @@ +Index: src/userprog/syscall.c +diff -u src/userprog/syscall.c~ src/userprog/syscall.c +--- src/userprog/syscall.c~ ++++ src/userprog/syscall.c +@@ -1,20 +1,486 @@ #include "userprog/syscall.h" #include +#include #include +#include "userprog/process.h" +#include "userprog/pagedir.h" -+#include "devices/kbd.h" ++#include "devices/input.h" +#include "filesys/filesys.h" +#include "filesys/file.h" +#include "threads/init.h" #include "threads/interrupt.h" +#include "threads/malloc.h" -+#include "threads/mmu.h" +#include "threads/palloc.h" #include "threads/thread.h" ++#include "threads/vaddr.h" - + + @@ -503,13 +530,14 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c - +static void copy_in (void *, const void *, size_t); + ++/* Serializes file system operations. */ +static struct lock fs_lock; + void syscall_init (void) { - intr_register (0x30, 3, INTR_ON, syscall_handler, "syscall"); -+ lock_init (&fs_lock, "fs"); + intr_register_int (0x30, 3, INTR_ON, syscall_handler, "syscall"); ++ lock_init (&fs_lock); } + +/* System call handler. */ @@ -568,7 +596,8 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c +static bool +verify_user (const void *uaddr) +{ -+ return pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL; ++ return (uaddr < PHYS_BASE ++ && pagedir_get_page (thread_current ()->pagedir, uaddr) != NULL); +} + +/* Copies a byte from user address USRC to kernel address DST. @@ -578,7 +607,7 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c +get_user (uint8_t *dst, const uint8_t *usrc) +{ + int eax; -+ asm ("mov %%eax, offset 1f; mov %%al, %2; mov %0, %%al; 1:" ++ asm ("movl $1f, %%eax; movb %2, %%al; movb %%al, %0; 1:" + : "=m" (*dst), "=&a" (eax) : "m" (*usrc)); + return eax != 0; +} @@ -590,7 +619,7 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c +put_user (uint8_t *udst, uint8_t byte) +{ + int eax; -+ asm ("mov %%eax, offset 1f; mov %0, %b2; 1:" ++ asm ("movl $1f, %%eax; movb %b2, %0; 1:" + : "=m" (*udst), "=&a" (eax) : "r" (byte)); + return eax != 0; +} @@ -628,7 +657,10 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c + for (length = 0; length < PGSIZE; length++) + { + if (us >= (char *) PHYS_BASE || !get_user (ks + length, us++)) -+ thread_exit (); ++ { ++ palloc_free_page (ks); ++ thread_exit (); ++ } + + if (ks[length] == '\0') + return ks; @@ -648,7 +680,7 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c +static int +sys_exit (int exit_code) +{ -+ thread_current ()->exit_code = exit_code; ++ thread_current ()->wait_status->exit_code = exit_code; + thread_exit (); + NOT_REACHED (); +} @@ -792,7 +824,7 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c + if (handle == STDIN_FILENO) + { + for (bytes_read = 0; (size_t) bytes_read < size; bytes_read++) -+ if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, kbd_getc ())) ++ if (udst >= (uint8_t *) PHYS_BASE || !put_user (udst++, input_getc ())) + thread_exit (); + return bytes_read; + } @@ -900,7 +932,8 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c + struct file_descriptor *fd = lookup_fd (handle); + + lock_acquire (&fs_lock); -+ file_seek (fd->file, position); ++ if ((off_t) position >= 0) ++ file_seek (fd->file, position); + lock_release (&fs_lock); + + return 0; @@ -945,13 +978,16 @@ diff -urp -X pat src/userprog/syscall.c~ src/userprog/syscall.c + struct file_descriptor *fd; + fd = list_entry (e, struct file_descriptor, elem); + next = list_next (e); ++ lock_acquire (&fs_lock); + file_close (fd->file); ++ lock_release (&fs_lock); + free (fd); + } +} -diff -urp -X pat src/userprog/syscall.h~ src/userprog/syscall.h ---- src/userprog/syscall.h~ 2004-09-05 22:38:45.000000000 -0700 -+++ src/userprog/syscall.h 2005-03-30 13:26:14.000000000 -0800 +Index: src/userprog/syscall.h +diff -u src/userprog/syscall.h~ src/userprog/syscall.h +--- src/userprog/syscall.h~ ++++ src/userprog/syscall.h @@ -2,5 +2,6 @@ #define USERPROG_SYSCALL_H