Project 2--User ProgramsJason BauCS140 Winter ‗09Slides Acknowledgements to previous CS140 TAsUser Program/Process What happens in Unix shell when?myth13:~/> cp –r pintos .1. Shell handles user input 2. fork() and execve(“cp”, “-r pintos .”)3. cp accesses file system to perform copy4. cp prints messages (if any) to stdout5. cp exitsQ: What is shell doing in the mean time? Q: Which lines require system calls?1/23/20092 CS140 Fall ‟09 -- Project #2Kernel/User Differentiation1/23/20093 CS140 Fall ‟09 -- Project #2Pintos – without shell Tests for proj2 in userprog are user processesHow do they get run? On disk—details later threads/init.c run_actions() -> run_task() process_wait(process_execute (task)); userprog/process.c process_execute() creates thread running start_process() thread loads executable file sets up user virtual memory (stack, data, code) starts executing user process @ _start (…)1/23/20094 CS140 Fall ‟09 -- Project #2User vs. Kernel Virtual MemoryUser stackUninit dataUser codeInit data• User code cannot address above PHYS_BASE• User can only access mapped addresses• User access to unmapped address page fault.• Kernel can page fault if it accesses unmapped user addressUservirtual memory4GBPHYS_BASEKernel virtual memory01/23/20095 CS140 Fall ‟09 -- Project #2Starting User Process lib/user/entry.cvoid _start (int argc, char *argv[]) { exit (main (argc, argv)); } Pass process start arguments on user stackReturn ValueArg 0Arg 1Stack Pointer (esp)1/23/20096 CS140 Fall ‟09 -- Project #2Starting User Process lib/user/entry.cvoid _start (int argc, char *argv[]) { exit (main (argc, argv)); } Pass process start arguments on user stackReturn ValueargcargvStack Pointer (esp)What are types of argc and argv?(Especially argv)1/23/20097 CS140 Fall ‟09 -- Project #2Setting up Starting Arguments cp –r pintos .argc = 4argv[0]= ―cp‖argv[1]= ―-r‖argv[2]= ―pintos‖argv[3]= ―.‖Pictured without all alignment elementsReturn Value4argv[0]Stack Pointerargv[1]argv[2]argv[3]“cp\0”“-r\0”“pint”“os\0”“.\0”argvPHYS_BASE1/23/20098 CS140 Fall ‟09 -- Project #2Project 2 Assignment Argument passing Already covered System calls List in assignment page We‟ll discuss shortly Process exit messages Denying writes to in-use executable files1/23/20099 CS140 Fall ‟09 -- Project #2System Calls Push args same as normal function calls Stack pointer at syscall number Execute internal interrupt int instruction Calling thread data available syscall_handler(struct intr_frame *f) Use to pass args to handler, AND ??? To return value to user process1/23/200910 CS140 Fall ‟09 -- Project #2System Calls—File System You are writing syscall interface for this project You do NOT need to change Pintos file system code for this project Users deal with file descriptors (ints) Pintos file system uses struct file * You design mapping File system is not thread-safe (proj 4). Use coarse synchronization to protect it1/23/200911 CS140 Fall ‟09 -- Project #2System Calls—File System Reading from the keyboard and writing to the console are special cases fd STDOUT_FILENO Can use putbuf(…) or putchar(…) In src/lib/kernel/console.c fd STDIN_FILENO Can use input_getc(…) In src/devices/input.h1/23/200912 CS140 Fall ‟09 -- Project #2System Calls—Processes int wait (pid_t pid) Parent must block until the child process pid exits Returns exit status of the child Must work if child has ALREADY exited Must fail if it has already been called on child void exit (int status) Exit with status and free resources Process termination message Communicate with wait so parent can retrieve your exit status1/23/200913 CS140 Fall ‟09 -- Project #2System Calls—Processes pid_t exec(const char *cmd_line) Like unix fork() + execve() Creates a child process This must not return until new process has been successfully created (or has failed)Generally, these three syscalls require most design + implementation time. Do them well.1/23/200914 CS140 Fall ‟09 -- Project #2System Calls—Security How does system recover from null-pointer segfault in user program? Kill user process, schedule others, and life goes on How does system recover from null-pointer segfault in kernel? It (basically) doesn‟t!1/23/200915 CS140 Fall ‟09 -- Project #2Protecting the Kernel from Users Verify user-passed mem reference before use Buffers Strings Pointers Check mem reference (two available techniques) Is passed address in user memory? Is it mapped? pagedir_get_page() in userprog/pagedir.c Modify page fault handler in userprog/exception.c Size of reference a consideration? Kill the user program it passed illegal address Remember to release any resources held1/23/200916 CS140 Fall ‟09 -- Project #2Utilities—Making Disks User code must be on virtual hard diskcd pintos/src/userprogmakepintos-mkdisk fs.dsk 2 /* Create 2MB disk*/pintos -f -q /* Format the disk */pintos -p ../examples/echo -a echo -- -q /* put a prog on the disk */pintos -q run „echo x‟ /* run the program */1/23/200917 CS140 Fall ‟09 -- Project #2Utilities—Making Disks Recommend making a backup disk w/programs in case yours gets trashed User code examples in src/examples You can write your own user code for test, but don‟t NEED to.1/23/200918 CS140 Fall ‟09 -- Project #2Getting Started Make a disk and add some simple programs Run make in src/examples Maybe some of the first tests (args-*) Temporarily setup stack to avoid page faulting esp = esp - 12; Basic syscall handler Which syscall to dispatch Reading from user memory address Skeleton exit system call body Handle write() syscall to STDOUT_FILENO Change process_wait() to infinite loop to instead of exit1/23/200919 CS140 Fall ‟09 -- Project #2Utilities—debugging user code Start pintos-gdb as usual add-symbol-file program.o Set breakpoints, etc, in user code Kernel names take precedence over user code To change: pintos-gdb userprog Then add-symbol-file kernel.o1/23/200920 CS140 Fall ‟09 -- Project
View Full Document