Project 2: User Programs

Similar documents
Project 2: User Programs

User Programs. Computer Systems Laboratory Sungkyunkwan University

CS 140 Project 2: User Programs. Fall 2015

Project 2-1 User Programs

Project 2--User Programs

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

Laboratory work in TDDI04 Pintos Assignments 2, 3

Pintos Project 2 User Programs

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

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

Pintos Project 2 User Programs. September 28, 2016

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

TDDB68 Lesson 3. Simon Ståhlberg

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

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

TDDB68. Lesson 1. Simon Ståhlberg

Operating System Structure

Project 3: Virtual Memory

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

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

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

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

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

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

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Project 4: Virtual Memory

Midterm Exam #1 September 28, 2016 CS162 Operating Systems

Section 7: Wait/Exit, Address Translation

Interrupts, Fork, I/O Basics

Computer Systems Assignment 2: Fork and Threads Package

Project #1 Exceptions and Simple System Calls

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

CSC 1600 Unix Processes. Goals of This Lecture

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

Processes (Intro) Yannis Smaragdakis, U. Athens

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

518 Lecture Notes Week 3

Operating Systems and Networks Assignment 2

Processes. Johan Montelius KTH

A process. the stack

CS 537 Lecture 2 - Processes

3. Process Management in xv6

Section 2: Processes

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

The Kernel Abstraction. Chapter 2 OSPP Part I

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

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

Process Management! Goals of this Lecture!

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

CSC369 Lecture 2. Larry Zhang, September 21, 2015

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

CS 5460/6460 Operating Systems

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

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.

Altering the Control Flow

CS 322 Operating Systems Practice Midterm Questions

CSC369 Lecture 2. Larry Zhang

Project 5: File Systems

Exceptions and Processes

TCSS 422: OPERATING SYSTEMS

Section 4: Threads and Context Switching

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

Assignment 1. Teaching Assistant: Michalis Pachilakis (

CS240: Programming in C

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

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

Preview. Process Control. What is process? Process identifier The fork() System Call File Sharing Race Condition. COSC350 System Software, Fall

Process Management 1

CS240: Programming in C

Recall: Four fundamental OS concepts. Recall: give the illusion of multiple processors? Process Control Block

Process Control. Philipp Koehn. 23 April 2018

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

CS140 Project 3: Virtual Memory. TA: Diveesh Singh

Pebbles Kernel Specification September 26, 2004

Altering the Control Flow

CMSC 412 Project #3 Threads & Synchronization Due March 17 th, 2017, at 5:00pm

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

OS lpr. www. nfsd gcc emacs ls 9/18/11. Process Management. CS 537 Lecture 4: Processes. The Process. Why Processes? Simplicity + Speed

Process Management! Goals of this Lecture!

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

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

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

Lecture 4: Memory Management & The Programming Interface

CSE 153 Design of Operating Systems Fall 2018

Project 2: Shell with History1

ENGR 3950U / CSCI 3020U Midterm Exam SOLUTIONS, Fall 2012 SOLUTIONS

CS 550 Operating Systems Spring Process III

System Calls & Signals. CS449 Spring 2016

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

This is an open book, open notes exam. But no online or in-class chatting.

SE350: Operating Systems

A Unix Process. Joseph Cordina

This is an open book, open notes exam. But no online or in-class chatting.

Operating Systems. II. Processes

CS61 Scribe Notes Lecture 18 11/6/14 Fork, Advanced Virtual Memory

Threads (light weight processes) Chester Rebeiro IIT Madras

Giving credit where credit is due

Introduction to the Process David E. Culler CS162 Operating Systems and Systems Programming Lecture 2 Sept 3, 2014

Transcription:

Project 2: User Programs Presented by Jaishankar Sundararaman 21 February 2008

Till now All code part of Pintos Kernel Code compiled directly with the kernel This required that the tests call some functions whose interface should remain unmodified From now on, run user programs on top of kernel Freedom to modify the kernel to make the user programs work

Why Project 2 is not Project 1? Project 2 Tests User Programs lib/user/syscall.c int 0x30 Exceptions Project 1 Tests syscall layer filesystem Kernel exc handling Timer Interrupts Other IRQs

A Comparison Sample Project 1 Test Code (alarm-zero.c) Void test_alarm_zero (void) { timer_sleep (0); pass (); } Kernel Function (Operates from kernel space) Sample Project 2 Test Code (close-normal.c) void test_main (void) { int handle; CHECK ((handle = open ("sample.txt")) > 1, "open \"sample.txt\""); msg ("close \"sample.txt\""); close (handle); } System call (Operates from user space)

Using the File system May need to interact with file system Do not modify the file system! Certain limitations (till Project 4) No internal synchronization Fixed file size No subdirectories File names limited to 14 chars System crash might corrupt the file system Files to take a look at: filesys.h & file.h

Some commands Creating a simulated disk pintos-mkdisk fs.dsk 2 Formatting the disk pintos -f q This will only work after your kernel is built! Copying the program into the disk pintos -p../../examples/echo -a echo -- -q Running the program pintos -q run echo x Single command: pintos --fs-disk=2 -p../../examples/echo -a echo -- -f -q run echo x $ make check Builds the disk automatically Copy&paste the commands make check does!

Various directories Few user programs: src/examples Relevant files: userprog/ Other files: threads/

Requirements Process Termination Messages Argument Passing System calls Deny writes to executables

Process Termination Process Terminates printf ("%s: exit(%d)\n",...); for eg: args-single: exit(0) Return Code Program name Do not print any other message!

Argument Passing Pintos currently lacks argument passing. You Implement it! Change *esp = PHYS_BASE to *esp = PHYS_BASE 12 in setup_stack() to get started Change process_execute() in process.c to process multiple arguments Could limit the arguments to fit in a page(4 kb) String Parsing: strtok_r() in lib/string.h pgm.c main(int argc, char *argv[]) { } $ pintos run pgm alpha beta argc = 3 argv[0] = pgm argv[1] = alpha argv[2] = beta Example taken from Abdelmounaam Rezgui s presentation

Memory layout 4GB Kernel Virtual Memory PHYS_BASE = 3GB PHYS_ BASE User stack Invalid Pointer Area (for User Programs) Grows downward Grows upward Uninitialized data segment ( Block Starting Symbol, BSS) Initialized data segment User Virtual Memory 0x 08048000 Code segment 0 Figure taken from Abdelmounaam Rezgui s presentation

Setting up the Stack How to setup the stack for the program - /bin/ls l foo bar

Setting up the Stack Contd bffffffc0 00 00 00 00... bffffffd0 04 00 00 00 d8 ff ff bf-ed ff ff bf f5 ff ff bf... bffffffe0 f8 ff ff bf fc ff ff bf-00 00 00 00 00 2f 62 69.../bi bfffffff0 6e 2f 6c 73 00 2d 6c 00-66 6f 6f 00 62 61 72 00 n/ls.-l.foo.bar.

System Calls Pintos lacks support for system calls currently! Implement the system call handler in userprog/syscall.c System call numbers defined in lib/syscall-nr.h Process Control: exit, exec, wait File system: create, remove, open, filesize, read, write, seek, tell, close Others: halt Syscall handler currently static void syscall_handler (struct intr_frame *f UNUSED) { printf ("system call!\n"); thread_exit (); }

System Call Details Types of Interrupts External and Internal System calls Internal Interrupts or Software Exceptions 80x86 int instruction to invoke system calls Pintos int $0x30 to invoke system call

Continued A system call has: System call number (possibly) arguments When syscall_handler() gets control: syscall_handler (struct intr_frame *f) { f->esp. f->eax = ; }... Arg #2 Arg #1 Sys. Call # System calls that return a value () must modify f->eax Caller s User Stack Figure taken from Abdelmounaam Rezgui s presentation

System calls File system Decide on how to implement the file descriptors O(n) data structures perfectly fine! Access granularity is the entire file system Have 1 global lock! write() fd 1 writes to console use putbuf() to write entire buffer to console read() fd 0 reads from console use input_getc() to get input from keyboard Implement the rest of the system calls

System calls Process Control wait(pid) Waits for process pid to die and returns the status pid returned from exit Returns -1 if pid was terminated by the kernel pid does not refer to child of the calling thread wait() has already been called for the given pid exec(cmd) runs the executable whose name is given in command line returns -1 if the program cannot be loaded exit(status) terminates the current program, returns status status of 0 indicates success, non zero otherwise Parent process executes Parent: wait() Parent: exec() Child process executes Child process exits OS notifies Figure taken and modified from Dr. Back s lecture CS3204 - Fall 2006

Process Control continued Implement process_wait() in process.c Then, implement wait() using process_wait() Cond variables and/or semaphores will help Think about what semaphores may be used for and how they must be initialized Some Conditions to take care! Parent may or may not wait for its child Parent may call wait() after child terminates! int process_wait (tid_t child_tid UNUSED) { return -1; } main() { int i; pid_t p; p = exec( pgm a b ); // i = wait (p); }

Memory Access Invalid pointers must be rejected. Why? Kernel has access to all of physical memory including that of other processes Kernel like user process would fault when it tries to access unmapped addresses User process cannot access kernel virtual memory User Process after it has entered the kernel can access kernel virtual memory and user virtual memory How to handle invalid memory access?

Memory Access contd Two methods to handle invalid memory access Verify the validity of user provided pointer and then dereference it Look at functions in userprog/pagedir.c, threads/vaddr.h Strongly recommended! Check if user pointer is below PHYS_BASE and dereference it Could cause page fault Handle the page fault by modifying the page_fault() code in userprog/exception.c Make sure that resources are not leaked

Some Issues to look at Check the validity of the system call parameters Every single location should be checked for validity before accessing it. For e.g. not only f->esp, but also f->esp +1, f->esp+2 and `f->esp+3 should be checked Read system call parameters into kernel memory (except for long buffers) copy_in function recommended!

Denying writes to Executables Use file_deny_write() to prevent writes to an open file Use file_allow_write() to re enable write Closing a file will automatically re enable writes

Suggested Order of Implementation Change *esp = PHYS_BASE to *esp = PHYS_BASE 12 to get started Implement the system call infrastructure Change process_wait() to a infinite loop to prevent pintos getting powered off before the process gets executed Implement exit system call Implement write system call Start making other changes

Misc Deadline: Mar 12, 11:59 pm Do not forget the design document Must be done individually Good Luck!