User Programs. Computer Systems Laboratory Sungkyunkwan University

Similar documents
Project 2-1 User Programs

Project 2: User Programs

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

Project 2: User Programs

CS 140 Project 2: User Programs. Fall 2015

Project 2--User Programs

Pintos Project 2 User Programs. September 28, 2016

Laboratory work in TDDI04 Pintos Assignments 2, 3

Project 3: Virtual Memory

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)

Pintos Project 2 User Programs

TDDB68 Lesson 3. Simon Ståhlberg

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

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

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

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

Project 4: Virtual Memory

TDDB68. Lesson 1. Simon Ståhlberg

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Project 5: File Systems

File System Case Studies. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

Operating System Structure

Project #1 Exceptions and Simple System Calls

Processes. Johan Montelius KTH

A process. the stack

File System Case Studies. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

Contents. IPC (Inter-Process Communication) Representation of open files in kernel I/O redirection Anonymous Pipe Named Pipe (FIFO)

Buffer Overflow. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File System Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

CS 550 Operating Systems Spring Process II

Processes. Process Management Chapter 3. When does a process gets created? When does a process gets terminated?

CSC 1600 Unix Processes. Goals of This Lecture

(In columns, of course.)

Interrupts, Fork, I/O Basics

Exceptions and Processes

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

A Unix Process. Joseph Cordina

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

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

File System Case Studies. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

Pintos Project 3 Virtual Memory. October 26, 2016

Linux Operating System

Announcement (1) Due date for PA3 is changed (~ next week) PA4 will also be started in the next class. Not submitted. Not scored

3. Process Management in xv6

Section 4: Threads and Context Switching

CSCI 237 Sample Final Exam

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

fork System-Level Function

Midterm Exam #2 April 20, 2016 CS162 Operating Systems

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

Universidad Carlos III de Madrid Computer Science and Engineering Department Operating Systems Course

Contents. PA1 review and introduction to PA2. IPC (Inter-Process Communication) Exercise. I/O redirection Pipes FIFOs

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

Processes. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Section 2: Processes

Computer Systems Laboratory Sungkyunkwan University

Altering the Control Flow

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

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

Mon Sep 17, 2007 Lecture 3: Process Management

Applications of. Virtual Memory in. OS Design

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

Quiz I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Fall Department of Electrical Engineering and Computer Science

CS 537 Lecture 2 - Processes

CSCE 313 Introduction to Computer Systems. Instructor: Dezhen Song

Threads Implementation. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

File System Internals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSCE 313: Intro to Computer Systems

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

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 2 November 14, 2014

518 Lecture Notes Week 3

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

CSC369 Lecture 2. Larry Zhang, September 21, 2015

OPERATING SYSTEMS ASSIGNMENT 3 MEMORY MANAGEMENT

CS140 Project 3: Virtual Memory. TA: Diveesh Singh

CMSC 412 Project #4 Virtual Memory Due Friday April 11, 2014, at 5:00pm

Processes (Intro) Yannis Smaragdakis, U. Athens

Altering the Control Flow

Pebbles Kernel Specification September 20, 2017

Lecture 24: Filesystems: FAT, FFS, NTFS

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

Announcement (1) sys.skku.edu is now available

Announcement. Exercise #2 will be out today. Due date is next Monday

File System Internals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 4. File Systems. Part 1

Chap 4, 5: Process. Dongkun Shin, SKKU

Princeton University Computer Science 217: Introduction to Programming Systems. Process Management

Computer Systems Laboratory Sungkyunkwan University

What is a Process. Processes. Programs and Processes. System Classification 3/5/2013. Process: An execution stream and its associated state

Computer Science 162, Fall 2014 David Culler University of California, Berkeley Midterm 2 November 14, 2014

Processes COMPSCI 386

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

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

Transcription:

Project 2: User Programs Jin-Soo Kim (jinsookim@skku.edu) Computer Systems Laboratory Sungkyunkwan University http://csl.skku.edu

Supporting User Programs What should be done to run user programs? 1. Provide file system accesses 2. Manage processes 3. Setup an address space for each process 4. Pass arguments 5. Provide system calls 2

File Systems (1) Block device abstraction A set of sectors (sector = 512 bytes) 512B 512B 512B 0 1 N 1 File systems <filename, metadata, data> <a set of sectors> meta1 a.out 1 4 3 2 1 2 3 dog.jpg meta2 3

File Systems (2) Pintos file system No internal synchronization File size is fixed at creation time File data is allocated as a single extent (i.e., in a contiguous range of sectors on disk) No subdirectories File names are limited to 14 characters A system crash may corrupt the disk Unix-like semantics: If a file is open when it is removed, it may still be accessed by any threads that it open, until the last one closes it. 4

File Systems (3) Using the Pintos file system $ pintos mkdisk fs.dsk 2 Creates a 2MB disk named fs.dsk $ pintos f q Formats the disk ( f) and exits as soon as the format is done ( q) $ pintos p../../examples/echo a echo q Put the file../../examples/echo /examples/echo to the Pintos file system under the name echo $ pintos q run echo x Run the executable file echo, passing argument x $ pintos fs disk=2 p../../examples/echo a echo f q run echo x 5

File Systems (4) Note: User programs are loaded from the file system To run a program, it should be moved to the file system. Executable files are in ELF format Data files can be also stored in the file system Refer to the interfaces provided d by the file system In filesys/filesys.h and filesys/file.h You will need them to implement system calls related to file systems» e.g., create(), remove(), open(), filesize(), read(), write(), seek(), tell(), close() You don t need to modify the file system code (under the filesys directory) for this project 6

Processes (1) Processes in Pintos Each process has only one thread The initial program is loaded by the Pintos In run_task() @ threads/init.c New processes are created by the exec() system call fork() + exec() in Unix 7

Processes (2) Managing g processes A process is identified by PID (process id) The exec() system call returns the child s pid Processes form a tree A process can have many child processes parent Each process has a unique parent process exec() The exit() system call terminates the current process, returning status to the kernel The parent process waits for the termination of its child process by calling wait(), and gets the status wait() Blocked arguments child status exit() 8

Processes (3) Implementing processes A process is created, scheduled, and managed using a kernel thread Need to keep track of additional information for processes PID, parent PID, file descriptors (more on later), etc. Basic skeleton codes are available in userprog/process.c 9

Address Spaces (1) Virtual memory Gives an illusion that a process has a large memory Each process has a separate virtual address space Process B Process A Virtual memory Page 3 Page 2 Page 1 Page 0 Page 5 Page 4 Page 3 Page 2 Page 1 Page 0 Page tables Physical memory Frame 11 Frame 10 Frame 9 Frame 8 Frame 7 Frame 6 Frame 5 Frame 4 Frame 3 Frame 2 Frame 1 Frame 0 10

Address Spaces (2) Typical memory layout in Pintos 0x00000000 0x08048000 code segment initialized data segment BSS Only the used memory regions have valid mapping entries in page tables 0xc0000000 = PHYS_BASE user stack fixed in size until Project 3 Kernel address space 11

Address Spaces (3) Page tables in x86 processors Common page size: 4KB 2-level page tables Page directory Page table Page tables allocated when a process is created The address of the page directory is a part of process context Changed on context switch (struct thread *) t >pagedir 12

Address Spaces (4) Page faults A user program can only access its own user virtual memory (0x00000000 to 0xbfffffff) An attempt to access kernel virtual memory causes a page fault (page_fault() @ userprog/exception.c) On page fault, the process will be terminated Kernel threads can access both kernel virtual memory and, if a user process is running, the user virtual memory of the running process. However, even in the kernel, an attempt t to access memory at an unmapped user virtual address will cause a page fault. 13

Program Arguments (1) Program startup _start() is the entry point for user programs Linked with user programs In lib/user/entry.c _start() will make a procedure call to main() main() requires two parameters: argc, argv Arguments are passed via the user stack 0xbffffe7c 3 0xbffffe78 2 0xbffffe74 1 SP 0xbffffe70 return address x86 Calling Convention for f(1, 2, 3) 14

Program Arguments (2) PHYS_BASE = 0xc0000000 padding Kernel space 0xbffffffc b a r 00 0xbffffff8 f o o 00 0xbffffff4 00 l 00 0xbffffff0 n / l s 0xbfffffec 00 / b i command: /bin/ls l foo bar 0xbfffffe8 00 00 00 00 0 argv[4] 0xbfffffe4 fc ff ff bf 0xbffffffc argv[3] 0xbfffffe0 f8 ff ff bf 0xbffffff8 argv[2] 0xbfffffdc f5 ff ff bf 0xbffffff5 argv[1] 0xbfffffd8 ed ff ff bf 0xbfffffed argv[0] 0xbfffffd4 d8 ff ff bf 0xbfffffd8 argv 0xbfffffd0 04 00 00 00 4 argc 0xbfffffcc return address 15

System Calls (1) System calls look just like library functions examples/cat.c 16

System Calls (2) However, they are implemented differently lib/user/syscall.c lib/syscall nr.h 17

System Calls (3) INT $0x30 0x00 Interrupt Descriptor Table (IDT) Divide error userprog/syscall.c 0x0d Page fault exception 0x30 INT $0x30 0xff 18

System Calls (4) Getting system call number & arguments System call numbers are defined in <lib/syscall nr.h> System call number and arguments are put in the caller s user stack The caller s register context is available to the exception handler (struct intr_frame *f) Use the caller s stack pointer to access them (struct intr_frame *) f >esp Save the return value to the caller s EAX register (struct intr_frame *) f >eax Note that you re accessing user address space in the kernel! 19

System Calls (5) Accessing user memory Can you trust the information provided by user? Stack pointer itself! System call arguments, especially pointers» Buffer addresses, string addresses, etc. What s wrong with pointers provided d by user? What if it s a NULL pointer? What if it points to the kernel address (>= PHYS_BASE)? What if it points to the unmapped user address? Requires various sanity checks on system call entry If any pointer is invalid, kill the process with -1 exit code Return error if the system call number is wrong Return error if any argument value is not what you want, 20

System Calls (6) Accessing user memory: Option 1 Verify the validity of a user-provided pointer, then dereference it How to verify? Traverse the user s page tables The valid address should have the corresponding PTE (page table entry) Present flag in the PTE should be set Refer to userprog/pagedir.c, threads/vaddr.h, and threads/pte.h Simple Pessimistic approach 21

System Calls (7) Accessing user memory: Option 2 Check only that a user pointer < PHYS_BASE, then dereference it Use get_user() and put_user() routines to read from or write to user memory (provided in the Pintos documentation Detects and handles invalid user pointer in the page fault handler In page_fault() @ userprog/exception.c For a page fault occurred in the kernel, set EAX to 0xffffffff and copy its former value into EIP Optimistic approach, faster (used in Linux) 22

System Calls (8) System calls related to processes void pid_t int exit (int status); exec (const char *cmd_line); wait (pid_t pid); All of a process s s resources must be freed on exit() The child can exit() before the parent performs wait() A process can perform wait() only for its children Wait() can be called twice for the same process The second wait() should fail Nested waits are possible: A B, B C Pintos should not be terminate until the initial process exits 23

System Calls (9) System calls related to files bool create (const char *file, unsigned initial_size); bool remove (const char *file); int open (const char *file); int filesize (int fd); int read (int fd, void *buffer, unsigned size); int write (int fd, void *buffer, unsigned size); void seek (int fd, unsigned position); unsigned tell (int fd); void close (int fd); create()/remove()/open() work on file names The rest of them work on file descriptors 24

System Calls (10) File descriptor An integer (C type int) An index for an entry in a kernel-resident data structure containing the details of all open files (file descriptor tables) PCB 0 Reserved for standard input Reserved for standard output Reserved for standard error 1 2 3 4 5 6 7. info on open file a info on open file b struct file 25

System Calls (11) Implementing system calls related to files No need to change the code in the filesys directory The existing routines in the filesys directory work on the file structure (struct file *) Maintain a mapping structure from a file descriptor to the corresponding file structure Deny writes to a running process s executable file Ensure only one process at a time is executing the file system code 26

Tips First things to implement Argument passing System call infrastructure Get system call number Get system call arguments write() system call for file descriptor 1 exit() process_wait() & wait() Build project 2 on top of your project 1 or start fresh Work in the userprog directory 27

Submission Due November 10, 11:59PM Fill out the design document (userprog.txt) p g and put it in your source tree (pintos/src/userprog) Tar and gzip your Pintos source codes $ cd pintos $ (cd src/userprog; make clean) $ tar cvzf TeamName.tar.gz./src Send it to the instructor via e-mail (NOT to the GoogleGroups!!) Hand in the printed version of your design document in the following class on November 11. 28