DOC PREVIEW
Stanford CS 140 - Homework

This preview shows page 1-2-24-25 out of 25 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 25 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

10/8/2007 CS140 HW2CS140 Help SessionHomework 2UserProgMegan Wachs10/8/2007 CS140 HW2drivernetworkdriverconsoledriverdiskls gcc emacsKERNELUSERCPUschedulerSyscall Handler10/8/2007 CS140 HW2drivernetworkdriverconsoledriverdiskls gcc emacsKERNELUSERCPUschedulerSyscall Handler10/8/2007 CS140 HW2User vs. Kernel Code• Tests in src/userprog/ are now user code• More user programs to play with are in src/examples• You can write and compile your own, with some limitations• You don’t NEED to write any user code10/8/2007 CS140 HW2User vs. Kernel Code• OLD: run_test ran code directly in the main thread• NEW: process_execute("cp -r pintos .")calls create_thread(...)calls execute_thread(...)Runs “cp” in user space.10/8/2007 CS140 HW2User Virtual MemoryUser vs. Kernel MemoryKernel Virtual MemoryPHYS_BASE04 GBUser code can not access addresses above PHYS_BASE.The user can only access mapped addresses10/8/2007 CS140 HW2User Virtual MemoryPHYS_BASE0If the user tries to access an unmapped address, it will page faultEven in kernel mode you can page fault if you try to access an unmapped user addressunusedcodedataStack10/8/2007 CS140 HW2Enabling User Code1. Start it running• Set up the stack10/8/2007 CS140 HW2Enabling User Code1. Start it running• Set up the stack 2. Allow it to do things• Interact with the file system• Communicate with other processes10/8/2007 CS140 HW2Enabling User Code1. Start it running• Set up the stack 2. Allow it to do things• Interact with the file system• Interact with the user• Communicate with other processes3. Don’t let it crash the kernel!• Check all user pointers10/8/2007 CS140 HW2Setting Up the Stackvoid_start (int argc, char *argv[]) {exit (main (argc, argv));}Your code must set up the “main” function call by hand.Argument 0Return valueArgument 1Stack Pointer10/8/2007 CS140 HW2Setting Up the Stackvoid_start (int argc, char *argv[]) {exit (main (argc, argv));}Your code must set up the “main” function call by hand.argcReturn valueargvStack Pointer10/8/2007 CS140 HW2Setting Up the Stackcp -r pintos .argc = 4argv[0] = “cp”argv[1] = “-r”argv[2] = “pintos”argv[3] = “.”argv[4] = 0argcReturn valueargvStack Pointerargv[0]cp-rpintos.argv[1]argv[2]argv[3]argv[4]PHYS_BASE10/8/2007 CS140 HW2System Calls• File System Related• Process Related• more to come in HW 310/8/2007 CS140 HW2System Callssrc/lib/user/syscall.hsrc/userprog/syscall.csrc/filesys/filesys.hint fd = fopen ("foo.c");syscall_handler(struct intr_frame *f);File * file = filesys_open ("foo.c");Intr_frame{ …void *esp…}eaxUSERKERNEL10/8/2007 CS140 HW2System Callssrc/lib/user/syscall.hsrc/userprog/syscall.csrc/filesys/filesys.hint fd = fopen ("foo.c");syscall_handler(struct intr_frame *f);File * file = filesys_open ("foo.c");Intr_frame{ …void *esp…}eaxUSERKERNELYour code10/8/2007 CS140 HW2System Calls - File System• You do NOT need to change the file system for this project• Users deal with file descriptors (ints). The file system uses struct file *. You need to enforce a mapping.• The file system is not thread-safe (yet!) so use coarse synchronization to protect it.10/8/2007 CS140 HW2System 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.h10/8/2007 CS140 HW2System Calls - Processes• int wait (pid_t pid)– Parent must block until the child process pid exits– Must return the status value of the child– Must work if child has already exited– Must fail if it has already been called on the child.• void exit (int status)– Exit w/ the specified status & free resources– Required: print out the exit status– Need synchronization with wait so the parent can retrieve your status if desired.10/8/2007 CS140 HW2System Calls - Processes• pid_t exec (const char *cmd_line)– Create a new child process– This MUST not return until the new process has successfully been created (or has failed)Design these three well! They are the most time-consuming.10/8/2007 CS140 HW2System Calls - Paranoia• In a system call, the user will pass you all kinds of addresses (buffers, strings, stack pointers, etc).• Trust no one. Check anything the user passes to you. – Is the passed-in address in user memory?– You can use pagedir_get_page(…) to check if the address is mapped for the user program.• Kill the child if it passes an illegal address.– Free any locks and resources.10/8/2007 CS140 HW2Utilities –Making Disks• User code must be on a virtual hard drivecd pintos/src/userprogmakepintos-mkdisk fs.dsk 2 /*create a 2MB disk*/pintos -f -q /*format the disk*/pintos -p ../examples/echo -a echo -- -q /*put a program on the disk and rename it*/pintos -q run 'echo x' /* run the program*/10/8/2007 CS140 HW2Utilities –Making Disks• Recommend making a backup disk w/ programs loaded in case your disk gets trashed10/8/2007 CS140 HW2Getting Started• Make a disk and add some simple programs– run make in src/examples • Temporarily set up the stack to avoid page faulting immediately. esp = esp - 12;• Enable reading from user memory addresses• Handle write( ) syscall for STDOUT_FILENO• Change process_wait to an infinite loop so that pintos doesn’t power off10/8/2007 CS140 HW2Utilities – Debugging User Code• Start pintos-gdb as usual• add-symbol-file program• Set breakpoints, etc, in user code• Kernel names will take precedence over user code• Change this by doing pintos-gdb userprog.o, then add-symbol-file


View Full Document

Stanford CS 140 - Homework

Documents in this Course
Notes

Notes

8 pages

Load more
Download Homework
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Homework and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Homework 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?