From de6fa5350d2ddf2d36bab80236cf09d8127cde6b Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Mon, 20 Sep 2004 22:29:18 +0000 Subject: [PATCH] Rename addrspace to process. --- doc/userprog.texi | 48 +++++++++--------- doc/vm.texi | 28 +++++++++-- src/Makefile.build | 2 +- src/threads/init.c | 4 +- src/threads/thread.c | 6 +-- src/threads/thread.h | 2 +- src/userprog/addrspace.h | 10 ---- src/userprog/{addrspace.c => process.c} | 66 ++++++++++++------------- src/userprog/process.h | 10 ++++ 9 files changed, 101 insertions(+), 75 deletions(-) delete mode 100644 src/userprog/addrspace.h rename src/userprog/{addrspace.c => process.c} (98%) create mode 100644 src/userprog/process.h diff --git a/doc/userprog.texi b/doc/userprog.texi index 8ac03da..14fa118 100644 --- a/doc/userprog.texi +++ b/doc/userprog.texi @@ -56,15 +56,9 @@ doing is to simply go over each part you'll be working with. In where the bulk of your work will be: @table @file -@item addrspace.c -@itemx addrspace.h -An address space keeps track of all the data necessary to execute a -user program. Address space data is stored in @code{struct thread}, -but manipulated only by @file{addrspace.c}. Address spaces need to -keep track of things like paging information for the process (so that -it knows which memory the process is using). Address spaces also -handle loading the program into memory and starting up the process's -execution. +@item process.c +@itemx process.h +Loads ELF binaries and starts processes. @item pagedir.c @itemx pagedir.h @@ -172,16 +166,16 @@ we require you to support.) The only other limitation is that Pintos can't run programs using floating point operations, since it doesn't include the necessary kernel functionality to save and restore the processor's floating-point unit when switching threads. You can look -in @file{test} directory for some examples. +in @file{tests/userprog} directory for some examples. Pintos loads ELF executables, where ELF is an executable format used by Linux, Solaris, and many other Unix and Unix-like systems. Therefore, you can use any compiler and linker that produce 80@var{x}86 ELF executables to produce programs for Pintos. We -recommend using the tools we provide in the @file{tests} directory. By -default, the @file{Makefile} in this directory will compile the test -programs we provide. You can edit the @file{Makefile} to compile your -own test programs as well. +recommend using the tools we provide in the @file{tests/userprog} +directory. By default, the @file{Makefile} in this directory will +compile the test programs we provide. You can edit the +@file{Makefile} to compile your own test programs as well. One thing you should realize immediately is that, until you use the above operation to copy a test program to the emulated disk, Pintos @@ -222,9 +216,9 @@ access kernel virtual memory will cause a page fault, handled by @code{page_fault()} in @file{userprog/exception.c}, and the process will be terminated. Kernel threads can access both kernel virtual memory and, if a user process is running, the user virtual memory of -the running process. However, an attempt to access memory at a user -virtual address that doesn't have a page mapped into it will also -cause a page fault. +the running process. However, even in the kernel, an attempt to +access memory at a user virtual address that doesn't have a page +mapped into it will cause a page fault. @node Global Requirements @section Global Requirements @@ -246,13 +240,13 @@ first process. @node Problem 2-1 Argument Passing @section Problem 2-1: Argument Passing -Currently, @code{thread_execute()} does not support passing arguments +Currently, @code{process_execute()} does not support passing arguments to new processes. UNIX and other operating systems do allow passing command line arguments to a program, which accesses them via the argc, argv arguments to main. You must implement this functionality by -extending @code{thread_execute()} so that instead of simply taking a +extending @code{process_execute()} so that instead of simply taking a program file name, it can take a program name with arguments as a -single string. That is, @code{thread_execute("grep foo *.c")} should +single string. That is, @code{process_execute("grep foo *.c")} should be a legal call. @xref{80x86 Calling Convention}, for information on exactly how this works. @@ -374,7 +368,7 @@ is not safe to call into the filesystem code provided in the recommend adding a single lock that controls access to the filesystem code. You should acquire this lock before calling any functions in the @file{filesys} directory, and release it afterward. Don't forget -that @file{addrspace_load()} also accesses files. @strong{For now, we +that @file{process_execute()} also accesses files. @strong{For now, we recommend against modifying code in the @file{filesys} directory.} We have provided you a function for each system call in @@ -433,7 +427,7 @@ You need to modify @file{tests/Makefile}. @b{What's the difference between @code{tid_t} and @code{pid_t}?} A @code{tid_t} identifies a kernel thread, which may have a user -process running in it (if created with @code{thread_execute()}) or not +process running in it (if created with @code{process_execute()}) or not (if created with @code{thread_create()}). It is a data type used only in the kernel. @@ -464,6 +458,10 @@ dereference user pointers directly and handle page faults by terminating the process. In either case, you'll need to reject kernel pointers as a special case. +If you choose to translate user addresses into kernel addresses, +you'll want to look at @file{threads/mmu.h}, which has all kinds of +useful functions for manipulating virtual addresses. + @item @b{I'm also confused about reading from and writing to the stack. Can you help?} @@ -485,6 +483,12 @@ the location. @item Each character is 1 byte. @end itemize + +@item +@b{Why doesn't keyboard input work with @option{-nv}?} + +Serial input isn't implemented. Don't use @option{-nv} if you want to +use the shell or otherwise type at the keyboard. @end enumerate @item Argument Passing FAQs diff --git a/doc/vm.texi b/doc/vm.texi index d008067..d9d5504 100644 --- a/doc/vm.texi +++ b/doc/vm.texi @@ -16,7 +16,7 @@ directory contains only the @file{Makefile}s. The only change from write will either be newly generated files (e.g.@: if you choose to implement your paging code in their own source files), or will be modifications to pre-existing code (e.g.@: you will change the -behavior of @file{addrspace.c} significantly). +behavior of @file{process.c} significantly). You will be building this assignment on the last one. It will benefit you to get your project 2 in good working order before this assignment @@ -141,6 +141,28 @@ address. / / @end example +Header @file{threads/mmu.h} has useful functions for various +operations on virtual addresses. You should look over the header +yourself, but its most important functions include these: + +@table @code +@item pd_no(@var{va}) +Returns the page directory index in virtual address @var{va}. + +@item pt_no(@var{va}) +Returns the page table index in virtual address @var{va}. + +@item pg_ofs(@var{va}) +Returns the page offset in virtual address @var{va}. + +@item pg_round_down(@var{va}) +Returns @var{va} rounded down to the nearest page boundary, that is, +@var{va} but with its page offset set to 0. + +@item pg_round_up(@var{va}) +Returns @var{va} rounded up to the nearest page boundary. +@end table + @node Disk as Backing Store @section Disk as Backing Store @@ -265,7 +287,7 @@ Follow the PDE to the page table. Point the PTE for the faulting virtual address to the physical page found in step 2. @end enumerate -You'll need to modify the ELF loader in @file{userprog/addrspace.c} to +You'll need to modify the ELF loader in @file{userprog/process.c} to do page table management according to your new design. As supplied, it reads all the process's pages from disk and initializes the page tables for them at the same time. For testing purposes, you'll @@ -328,7 +350,7 @@ file itself as backing store for read-only segments, since these segments won't change. There are a few special cases. Look at the loop in -@code{load_segment()} in @file{userprog/addrspace.c}. Each time +@code{load_segment()} in @file{userprog/process.c}. Each time around the loop, @code{read_bytes} represents the number of bytes to read from the executable file and @code{zero_bytes} represents the number of bytes to initialize to zero following the bytes read. The two diff --git a/src/Makefile.build b/src/Makefile.build index 1c92b77..bd619f4 100644 --- a/src/Makefile.build +++ b/src/Makefile.build @@ -51,7 +51,7 @@ filesys_SRC += filesys/inode.c # File headers. filesys_SRC += filesys/fsutil.c # Utilities. # User process code. -userprog_SRC = userprog/addrspace.c # Address spaces. +userprog_SRC = userprog/process.c # Process loading. userprog_SRC += userprog/pagedir.c # Page directories. userprog_SRC += userprog/exception.c # User exception handler. userprog_SRC += userprog/syscall.c # System call handler. diff --git a/src/threads/init.c b/src/threads/init.c index 1025172..0d9c699 100644 --- a/src/threads/init.c +++ b/src/threads/init.c @@ -21,7 +21,7 @@ #include "threads/test.h" #include "threads/thread.h" #ifdef USERPROG -#include "userprog/addrspace.h" +#include "userprog/process.h" #include "userprog/exception.h" #include "userprog/gdt.h" #include "userprog/syscall.h" @@ -113,7 +113,7 @@ main (void) if (initial_program != NULL) { printf ("\nExecuting '%s':\n", initial_program); - addrspace_execute (initial_program); + process_execute (initial_program); } #else test (); diff --git a/src/threads/thread.c b/src/threads/thread.c index fc358de..0861e29 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -12,7 +12,7 @@ #include "threads/switch.h" #include "threads/synch.h" #ifdef USERPROG -#include "userprog/addrspace.h" +#include "userprog/process.h" #include "userprog/gdt.h" #endif @@ -357,7 +357,7 @@ destroy_thread (struct thread *t) ASSERT (t != thread_current ()); #ifdef USERPROG - addrspace_destroy (t); + process_destroy (t); #endif if (t != initial_thread) palloc_free (t); @@ -387,7 +387,7 @@ schedule_tail (struct thread *prev) #ifdef USERPROG /* Activate the new address space. */ - addrspace_activate (); + process_activate (); #endif /* If the thread we switched from is dying, destroy it. diff --git a/src/threads/thread.h b/src/threads/thread.h index 26e785f..9b55700 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -93,7 +93,7 @@ struct thread list_elem elem; /* List element. */ #ifdef USERPROG - /* Owned by userprog/addrspace.c. */ + /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ #endif diff --git a/src/userprog/addrspace.h b/src/userprog/addrspace.h deleted file mode 100644 index 6e3b584..0000000 --- a/src/userprog/addrspace.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef USERPROG_ADDRSPACE_H -#define USERPROG_ADDRSPACE_H - -#include "threads/thread.h" - -tid_t addrspace_execute (const char *filename); -void addrspace_destroy (struct thread *); -void addrspace_activate (void); - -#endif /* userprog/addrspace.h */ diff --git a/src/userprog/addrspace.c b/src/userprog/process.c similarity index 98% rename from src/userprog/addrspace.c rename to src/userprog/process.c index 10d5616..08e78c6 100644 --- a/src/userprog/addrspace.c +++ b/src/userprog/process.c @@ -1,4 +1,4 @@ -#include "userprog/addrspace.h" +#include "userprog/process.h" #include #include #include @@ -22,9 +22,9 @@ static bool load (const char *cmdline, void (**eip) (void), void **esp); /* Starts a new thread running a user program loaded from FILENAME. The new thread may be scheduled before - addrspace_execute() returns.*/ + process_execute() returns.*/ tid_t -addrspace_execute (const char *filename) +process_execute (const char *filename) { char *fn_copy; tid_t tid; @@ -67,7 +67,7 @@ execute_thread (void *filename_) thread_exit (); /* Switch page tables. */ - addrspace_activate (); + process_activate (); /* Start the user process by simulating a return from an interrupt, implemented by intr_exit (in @@ -81,6 +81,34 @@ execute_thread (void *filename_) : "g" (&if_)); NOT_REACHED (); } + +/* Destroys the user address space in T and frees all of its + resources. */ +void +process_destroy (struct thread *t) +{ + ASSERT (t != thread_current ()); + + if (t->pagedir != NULL) + { + pagedir_destroy (t->pagedir); + t->pagedir = NULL; + } +} + +/* Sets up the CPU for running user code in the current + thread. */ +void +process_activate (void) +{ + struct thread *t = thread_current (); + + /* Activate T's page tables. */ + pagedir_activate (t->pagedir); + + /* Set T's kernel stack for use in processing interrupts. */ + tss_set_esp0 ((uint8_t *) t + PGSIZE); +} /* We load ELF binaries. The following definitions are taken from the ELF specification, [ELF1], more-or-less verbatim. */ @@ -246,36 +274,8 @@ load (const char *filename, void (**eip) (void), void **esp) file_close (file); return success; } - -/* Destroys the user address space in T and frees all of its - resources. */ -void -addrspace_destroy (struct thread *t) -{ - ASSERT (t != thread_current ()); - - if (t->pagedir != NULL) - { - pagedir_destroy (t->pagedir); - t->pagedir = NULL; - } -} - -/* Sets up the CPU for running user code in the current - thread. */ -void -addrspace_activate (void) -{ - struct thread *t = thread_current (); - - /* Activate T's page tables. */ - pagedir_activate (t->pagedir); - - /* Set T's kernel stack for use in processing interrupts. */ - tss_set_esp0 ((uint8_t *) t + PGSIZE); -} -/* addrspace_load() helpers. */ +/* load() helpers. */ static bool install_page (void *upage, void *kpage); diff --git a/src/userprog/process.h b/src/userprog/process.h new file mode 100644 index 0000000..cd9463d --- /dev/null +++ b/src/userprog/process.h @@ -0,0 +1,10 @@ +#ifndef USERPROG_PROCESS_H +#define USERPROG_PROCESS_H + +#include "threads/thread.h" + +tid_t process_execute (const char *filename); +void process_destroy (struct thread *); +void process_activate (void); + +#endif /* userprog/process.h */ -- 2.30.2