Project 2: User Programs

Similar documents
CS 140 Project 2: User Programs. Fall 2015

Project 2--User Programs

Project 2: User Programs

Pintos Project 2 User Programs

Project 2-1 User Programs

User Programs. Computer Systems Laboratory Sungkyunkwan University

Laboratory work in TDDB68 Pintos Assignments 2, 3. Viacheslav Izosimov

Laboratory work in TDDI04 Pintos Assignments 2, 3

Project-2 Review. (Extending the functionality of PintOS user program implementation) Summer 2018

TDDB68 Lesson 2 Pintos Assignments 3 & 4. Mattias Eriksson 2010 (or

According to this plan, you should have started with Lab 2 now Around 50% have passed Lab 1. The environment (1)

TDDB Lesson 3 Pintos Assignments (2), 3 & 4. Erik Hansson

TDDB Lesson 3 Pintos Assignments (2), 3 & 4. Erik Hansson

TDDB Lesson 2 Pintos Assignments (2), 3 & 4. Erik Hansson

TDDB68 Lesson 3. Simon Ståhlberg

TDDB68. Lesson 1. Simon Ståhlberg

Operating System Structure

Pintos Project 2 User Programs. September 28, 2016

Project 4: Virtual Memory

Project 3: Virtual Memory

TDDB68 Lesson 1 Introduction to Pintos Assignments 00, 0, 1, 2

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

Processes. Operating System CS 217. Supports virtual machines. Provides services: User Process. User Process. OS Kernel. Hardware

CS 355 Operating Systems. Keeping Track of Processes. When are processes created? Process States 1/26/18. Processes, Unix Processes and System Calls

LAB3: VIRTUAL MEMORY. Operating Systems 2015 Spring by Euiseong Seo

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Introduction to OS Processes in Unix, Linux, and Windows MOS 2.1 Mahmoud El-Gayyar

TDDB Lesson 2 Introduction to Pintos Assignments (1) and 2

Reading Assignment 4. n Chapter 4 Threads, due 2/7. 1/31/13 CSE325 - Processes 1

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Processes. Johan Montelius KTH

CSC369 Lecture 2. Larry Zhang

A process. the stack

Processes. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

CS 33. Architecture and the OS. CS33 Intro to Computer Systems XIX 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Project #1 Exceptions and Simple System Calls

Windows architecture. user. mode. Env. subsystems. Executive. Device drivers Kernel. kernel. mode HAL. Hardware. Process B. Process C.

Fall 2015 COMP Operating Systems. Lab #3

CSC369 Lecture 2. Larry Zhang, September 21, 2015

CSCE Operating Systems Interrupts, Exceptions, and Signals. Qiang Zeng, Ph.D. Fall 2018

Project 2! CSE 421/521 - Operating Systems! Virtual Memory in Pintos

The Kernel Abstraction. Chapter 2 OSPP Part I

Altering the Control Flow

EECS 482 Introduction to Operating Systems

Computer Systems Assignment 2: Fork and Threads Package

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Lecture 4: Process Management

Altering the Control Flow

Processes. Processes (cont d)

CS510 Operating System Foundations. Jonathan Walpole

CSci 4061 Introduction to Operating Systems. Processes in C/Unix

Unix-Linux 2. Unix is supposed to leave room in the process table for a superuser process that could be used to kill errant processes.

CS240: Programming in C

Interrupts, Fork, I/O Basics

Midterm Exam #2 Solutions April 20, 2016 CS162 Operating Systems

Exceptions and Processes

A2 Design Considerations. CS161, Spring

A Unix Process. Joseph Cordina

Multithreaded Programming

Section 4: Threads CS162. September 15, Warmup Hello World Vocabulary 2

Project 5: File Systems

OS COMPONENTS OVERVIEW OF UNIX FILE I/O. CS124 Operating Systems Fall , Lecture 2

Processes & Threads. Today. Next Time. ! Process concept! Process model! Implementing processes! Multiprocessing once again. ! More of the same J

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

SOFTWARE ARCHITECTURE 3. SHELL

Applications of. Virtual Memory in. OS Design

Operating Systems. II. Processes

CSC 1600 Unix Processes. Goals of This Lecture

CS240: Programming in C

Recitation 8: Tshlab + VM

Processes in linux. What s s a process? process? A dynamically executing instance of a program. David Morgan. David Morgan

Processes. What s s a process? process? A dynamically executing instance of a program. David Morgan

by Marina Cholakyan, Hyduke Noshadi, Sepehr Sahba and Young Cha

IMPLEMENTATION OF SIGNAL HANDLING. CS124 Operating Systems Fall , Lecture 15

Processes (Intro) Yannis Smaragdakis, U. Athens

3. Process Management in xv6

CS 550 Operating Systems Spring Process III

CS140 Project 3: Virtual Memory. TA: Diveesh Singh

CSE 451: Operating Systems Winter Module 4 Processes. Mark Zbikowski Allen Center 476

Mon Sep 17, 2007 Lecture 3: Process Management

Prepared by Prof. Hui Jiang Process. Prof. Hui Jiang Dept of Electrical Engineering and Computer Science, York University

Process management. What s in a process? What is a process? The OS s process namespace. A process s address space (idealized)

Layers in a UNIX System. Create a new process. Processes in UNIX. fildescriptors streams pipe(2) labinstructions

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Midterm Exam CPS 210: Operating Systems Spring 2013

Process. Prepared by Prof. Hui Jiang Dept. of EECS, York Univ. 1. Process in Memory (I) PROCESS. Process. How OS manages CPU usage? No.

Section 4: Threads and Context Switching

There are 10 total numbered pages, 5 Questions. You have 60 minutes. Budget your time carefully!

518 Lecture Notes Week 3

CSCE 313: Intro to Computer Systems

Section 2: Processes

Pebbles Kernel Specification September 26, 2004

Last time: introduction. Networks and Operating Systems ( ) Chapter 2: Processes. This time. General OS structure. The kernel is a program!

Processes. CS3026 Operating Systems Lecture 05

Week 2 Intro to the Shell with Fork, Exec, Wait. Sarah Diesburg Operating Systems CS 3430

The Kernel. wants to be your friend

TCSS 422: OPERATING SYSTEMS

CSE506: Operating Systems CSE 506: Operating Systems

CS 5460/6460 Operating Systems

Transcription:

Project 2: User Programs CS140 - Winter 2010 Slides by Andrew He, adapted from previous CS140 offerings

Overview Project 2 is due Thursday, February 4 This project requires an understanding of: How user programs run in general Distinctions between user and kernel virtual memory The system call infrastructure / file system interface

Important notes You will not be able to use the corn machines for this project. Use myth, pod, or cardinal instead. You can choose to build project 2 on top of your project 1, or start from scratch.

A guiding example What happens when a user wants to run the following program in the Unix shell? myth13:~$ cp -r /usr/bin/temp. 1) Shell parses user input 2) Shell calls fork() and execve( cp, argv, env) 3) cp uses file system interface to copy files 4) cp may print messages to stdout 5) cp exits These interactions require system calls

Syscall handler Recall: syscalls provide the interface between a user process and the OS. Popular syscalls: open, read, write, wait, exec, exit

User programs in Pintos threads/init.c main() => run_actions(argv) after booting run_actions => run_task(argv) the task to run is argv[1] run_task => process_wait(process_execute(task)) userprog/process.c process_execute creates a thread that runs start_process(filename ) => load(filename ) load sets up the stack, data, and code, as well as the start address

Virtual memory layout User virtual memory 4 GB PHYS_BASE 128 MB 0 Kernel virtual memory User stack Uninitialized data Initialized data Code segment Virtual memory is divided between user and kernel virtual memory. Each user process has its own mapping of user virtual addresses (the page directory). User processes are not allowed to access kernel memory or unmapped user virtual addresses This causes page faults The kernel can also page fault if it accesses an unmapped user virtual address

Project 2 requirements You will need to implement the following: Passing command-line arguments to programs Safe memory access A set of system calls Long list, in section 3.3.4 of the Pintos docs Process termination messages Denying writes to files in use as executables

Argument passing Before a user program starts executing, the kernel must push the functionʼs arguments onto the stack. This involves breaking the command-line input into individual words. Consider /bin/ls -l foo bar => /bin/ls, -l, foo, bar Implement the string parsing however you like in process_execute() one solution is to use strtok_r() in lib/string.c

Argument passing (cont.) /bin/ls -l foo bar argc = 4 (Note: word alignment not shown) stack pointer bar\0 foo\0 -l\0 /bin/ls\0 0 (sentinel) argv[3] argv[2] argv[1] argv[0] argv 4 (argc) 0 (fake RA) Push the words onto the stack Push a null pointer sentinel Push the address of each word in right-toleft order Push argv and argc Push 0 as a fake return address hex_dump() function in <stdio.h> may be useful for seeing the layout of your stack

Safe memory access The kernel will often access memory through user-provided pointers These pointers can be problematic: null pointers, pointers to unmapped user virtual memory, or pointers to kernel addresses If a user process passes these to the kernel, you must kill the process and free its resources (locks, allocated memory) Be aware of potential problems with buffers, strings, and pointers

Safe memory access (cont.) Two approaches to solving this problem: Verify every user pointer before dereference (simpler) Is it in the userʼs address space, ie below PHYS_BASE? (look at is_user_vaddr in threads/vaddr.h) Is it mapped or unmapped? (look at pagedir_get_page() in userprog/pagedir.c) These checks apply to buffers as well Modify fault handler in userprog/exception.c Only check that a user pointer is below PHYS_BASE Invalid pointers will trigger a page fault See 3.1.5 [Accessing User Memory] for more details

System calls Implement syscall_handler() in syscall.c What does this involve? Read syscall number at stack pointer SP is accessible as esp member of the struct intr_frame *f passed to syscall_handler() Read some number of arguments above the stack pointer, depending on the syscall Dispatch to the desired function Return the value to the user in f->eax

System calls and filesys Syscall numbers are defined in lib/syscall-nr.h You will not be implementing all the calls -- see 3.3.4 [System Calls] for a list of required calls for Project 2 Many of the syscalls involve file system functionality A simple filesys implementation is provided (you do NOT need to modify this, but familiarize yourself with the interfaces in filesys.h and file.h) The file system is not thread-safe Use coarse synchronization to ensure that any file system code is a critical section Syscalls take file descriptors as arguments, but the Pintos file system uses struct file *s You must design a proper mapping scheme

System calls and filesys (cont.) Reading from the keyboard and writing to the console are special cases with special file descriptors write syscall with fd STDOUT_FILENO Use putbuf( ) or putchar( ) in lib/kernel/console.c read syscall with fd STDIN_FILENO Use input_getc( ) in devices/input.h

System calls for processes int wait (pid_t pid) caller blocks until the child process corresponding to pid exits use synchronization primitives rather than thread_block() returns the exit status of the child or -1 in certain cases if wait has already been successfully called on the child if pid does not reference a child of the caller many cases to think about: multiple waits, nested waits, etc. suggestion: implement process_wait(), and then wait() on top of process_wait() involves the most work of all the syscalls void exit (int status) terminate user program, return status to the kernel print a termination message if a child is exiting, communicate exit status back to the parent who called wait

System calls for processes pid_t exec (const char *cmd_line) behaves like Unixʼs fork() + execve( ) creates a child process running the program in cmd_line must not return until the new process has successfully loaded or failed (use synchronization to ensure this)

Denying writes to executables Pintos should not allow code that is currently running to be modified Use file_deny_write() to prevent writes to an open file Note: closing a file will re-enable writes, so an executable file must be kept open as long as the process is running

Creating disks In order to run programs, you will need to create simulated disks containing user code How? From userprog/build folder: pintos-mkdisk filesys.dsk --filesys-size=2 /* creates a 2 MB disk */ pintos -f -q /* formats the disk */ pintos -p../examples/echo -a echo -- -q /* copies../examples/echo to disk and names it echo */ pintos -q run ʻecho xʼ /* runs the program with arg x */ Example programs are in src/examples, test programs are in src/userprog/build/tests Run make in these directories to build the programs You may want to make backup disks, in case the disk gets trashed (there is no filesys recovery tool)

Using GDB You can use GDB to debug user code Start GDB as usual, then do (gdb) loadusersymbols <userprog name> You can set breakpoints and inspect data as usual Note: a name that appears in both the kernel and the user program will refer to the kernel Fix this by typing pintos-gdb <userprog name> Then, loadusersymbols kernel.o

Suggested initial strategy Make a disk and add a few programs (like args-*) Temporarily set up stack to avoid immediate page faults In setup_stack() of process.c, change *esp = PHYS_BASE to *esp = PHYS_BASE - 12 This will allow execution of programs that take no args Implement safe user memory access Set up basic syscall infrastructure Read the syscall number, dispatch to function Implement exit syscall Implement write syscall to STDOUT_FILENO No tests will pass until you can functionally write to the console Change process_wait() to an infinite loop The stub implementation exits immediately, so Pintos will power off before any processes can run