CS 140 Project 2: User Programs. Fall 2015

Similar documents
Project 2--User Programs

Project 2: User Programs

Project 2: User Programs

User Programs. Computer Systems Laboratory Sungkyunkwan University

Pintos Project 2 User Programs

Project 2-1 User Programs

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

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

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

TDDB68. Lesson 1. Simon Ståhlberg

Project 4: Virtual Memory

TDDB68 Lesson 3. Simon Ståhlberg

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Operating System Structure

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Project 5: File Systems

Pintos Project 2 User Programs. September 28, 2016

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

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

CSC369 Lecture 2. Larry Zhang

Project #1 Exceptions and Simple System Calls

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

Project 3: Virtual Memory

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

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

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

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

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

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.

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

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

Section 4: Threads and Context Switching

Processes. Processes (cont d)

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

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

Exceptions and Processes

Altering the Control Flow

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

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

Altering the Control Flow

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

EECS 482 Introduction to Operating Systems

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

Processes: Introduction. CS 241 February 13, 2012

Today. Exceptional Control Flow Processes. Exceptions and Processes. Control Flow. Altering the Control Flow

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

The Kernel Abstraction. Chapter 2 OSPP Part I

Processes (Intro) Yannis Smaragdakis, U. Athens

Concurrency. Stefan D. Bruda. Winter 2018

CS240: Programming in C

CS 201. Processes. Gerson Robboy Portland State University

CS510 Operating System Foundations. Jonathan Walpole

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

Computer Systems Assignment 2: Fork and Threads Package

Recitation 8: Tshlab + VM

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

CPSC 341 OS & Networks. Processes. Dr. Yingwu Zhu

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

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

Multithreaded Programming

CS 5460/6460 Operating Systems

Programming Assignment #1: A Simple Shell

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

Nachos Tutorial. Outline. Directory & File Structure. Courtesy: University of Waterloo

System Calls & Signals. CS449 Spring 2016

Exceptional Control Flow: Exceptions and Processes

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

3. Process Management in xv6

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

Interrupts, Fork, I/O Basics

Mon Sep 17, 2007 Lecture 3: Process Management

Processes. Johan Montelius KTH

Pebbles Kernel Specification September 26, 2004

Today. Introduction to Computer Systems /18 243, Fall th Lecture. Control Flow. Altering the Control Flow.

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

Midterm Exam #1 September 28, 2016 CS162 Operating Systems

A process. the stack

CPS 310 first midterm exam, 2/26/2014

Fall 2015 COMP Operating Systems. Lab #3

Lecture 7: file I/O, more Unix

(In columns, of course.)

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Processes. q Process concept q Process model and implementation q Multiprocessing once again q Next Time: Scheduling

CSE506: Operating Systems CSE 506: Operating Systems

CSCE 313: Intro to Computer Systems

W4118: OS Overview. Junfeng Yang

Final Precept: Ish. Slides Originally Prepared by: Wonho Kim

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

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Processes. CS3026 Operating Systems Lecture 05

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

Process Control. Philipp Koehn. 23 April 2018

CSC 1600 Unix Processes. Goals of This Lecture

Assignment 1. Teaching Assistant: Michalis Pachilakis (

OS Interaction and Processes

Operating Systems. II. Processes

System Calls and Signals: Communication with the OS. System Call. strace./hello. Kernel. Context Switch

Transcription:

CS 140 Project 2: User Programs Fall 2015

Important Dates Project 1: DUE TODAY (Friday, October 9 th ) at 12pm Project 2 will be due Friday, October 23 rd at 12pm 2 weeks Start early J 2

Overview Project 2: User programs Build support for running user processes Requires good understanding of: Steps for running a user program Distinctions between user and kernel virtual memory System call interface and handling Kernel file system interface 3

User Programs Suppose a user types in the command % cp r fromdir todir What happens? 4

% cp r fromdir todir Step 1: shell parses input argc = 4; argv = { cp, -r, fromdir, todir } Step 2: shell calls fork() and execve(argv[0], argv, env) Step 3: cp uses system call interface to read/ write files. Step 4: cp may print messages to stdout Step 5: cp exits 5

User Programs in Pintos threads/init.c run_actions() à run_task(argv) 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) User programs have NO heap/malloc starts executing user process 6

User vs Kernel Virtual Memory 7

Starting User Processes lib/user/entry.c void _start (int argc, char *argv[]) { exit(main(argc, argv)); } Pass process start arguments 8

Project 2 Requirements You will be implementing: Argument passing Safe memory access System calls Process exit messages Deny write to in- use executable files 9

Argument Passing In preparation of starting a user process, the kernel must push the command line arguments onto the stack Break command line input into individual tokens input: cp r fromdir todir output: { cp, -r, fromdir, todir } strtok_r( ) in lib/string.c will be helpful 10

Argument Passing (stack) 11

Safe Memory Access 12

Safe Memory Access (cont.) The kernel will often access memory through user- provided pointers THIS IS DANGEROUS null pointers pointers to unmapped virtual addresses pointers to kernel addresses Kill the process (free its resources, e.g. locks, memory) Be careful with buffers, strings, any other pointers 13

Safe Memory Access (cont.) There are 2 approaches to solving this problem: Simpler Approach: Verify every user pointer before dereferencing Ensure that it is in user address space (i.e. below PHYS_BASE) Ensure it is mapped (look at userprog/ pagedir.c:pagedir_get_page()) For buffer, ensure for the entire buffer Modify fault handler in userprog/exception.c Ensure pointer or buffer is below PHYS_BASE Invalid pointers should trigger page faults See 3.1.5 Accessing User Memory for more details 14

System Calls: how do they work?! Works like normal function calls (args in stack) Execute internal interrupt (int instruction) syscall_handler(struct intr_frame *f) Stack pointer (f->esp) at syscall number Calling thread data available to pass args to handler to return value to user process Return value just like functions (f- >eax) 15

System Call: Implementation userprog/syscall.c:syscall_handler() Read syscall number at stack pointer Dispatch a particular function to handle syscall Read (validate!) arguments (above the stack pointer) above the stack pointer validate pointer and buffers! syscall numbers defined in lib/syscall-nr.h See 3.3.4 System Calls for Project 2 s required calls 16

System Call: File System Many syscalls involve file system functionality Simple filesys impl is provided: filesys.h, file.h No need to modify it, but familiarize yourself File system is not thread- safe! (Project 4) Use a coarse lock to protect it Syscalls take file descriptors as args Pintos represents file with struct file * You must design the mapping Special cases: reading from keyboard and writing to console write(stdout_fileno, ) use putbuf or putchar read(stdin_fileno, ) use input_getc 17

System Call: Processes Generally these syscalls require the most design and implementation time pid_t exec(const char *cmd_line) like UNIX fork() + execve() Creates a child process Must not return until a new process has been created (or creation failed) 18

System Call: Processes int wait (pid_t pid) Parent must block until 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 before void exit (int status) Exit with status and free resources Process termination message Parent must be able to retrieve status via wait 19

System Calls: Security How does a system recover from null pointer segfault in user program? How does a system recover from null pointer segfault in kernel? 20

System Calls: Security How does a system recover from null pointer segfault in user program? kill user process, life goes on How does a system recover from null pointer segfault in kernel? it (basically) doesn t Verify all user- passed memory references (pointers, buffers, strings) Kill user program if passed illegal addresses 21

Denying writes to executables Executables are files like any other Pintos should not allow code that is currently running to be modified use file_deny_write() to prevent writes to an open file closing a file will re- enable writes keep executable open as long as the process is running 22

Utilities: Making disks User executables must be on virtual hard disk cd pintos/src/userprog make pintos-mkdisk fs.dsk --filesys-size=2 /* Create 2MB disk */ pintos --disk=fs.dsk -- -f -q /* Format disk */ pintos --disk=fs.dsk -p../examples/echo -a echo -- -q /* copy to disk */ pintos --disk=fs.dsk -- -q run echo x /* run program */ User code examples in src/examples You can write your own code to test But you don t have to 23

Utilities: GDB You can use GDB to debug user code Start GDB as usual, then do: (gdb) loadusersymbols <userprog.o> You can set breakpoints and inspect data as usual User symbols will not override kernel symbols Work around duplicate symbols by inverting order Run gdb with pintos-gdb <userprog.o> Then load kernel symbols with (gdb) loadusersymbols kernel.o 24

Getting Started Make a disk and add simple programs run make in src/examples Temporarily setup stack to avoid page faulting In userprog/process.c:setup_stack() Change *esp = PHYS_BASE to *esp = PHYS_BASE - 12 This will allow running programs with no arguments Implement safe user memory access 25

Getting Started (cont.) Setup syscall dispatch Implement exit Implement write to STDOUT_FILENO No tests will pass until you can write to the console Change process_wait( ) to an infinite loop Stub implementation exits immediately Pintos will power off before any processes can run Start early! Good luck! 26

Start early! threads/thread.c 13 threads/thread.h 26 + userprog/exception.c 8 userprog/process.c 247 ++++++++++++++-- userprog/syscall.c 468 ++++++++++++++++++++++++++++++userprog/syscall.h 1 6 files changed, 725 insertions(+), 38 deletions(-) 27