Project 2: Signals. Consult the submit server for deadline date and time

Similar documents
3. Process Management in xv6

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

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

ICS143A: Principles of Operating Systems. Midterm recap, sample questions. Anton Burtsev February, 2017

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

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

Section 4: Threads and Context Switching

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Lecture 7: Signals and Events. CSC 469H1F Fall 2006 Angela Demke Brown

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

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

Processes. CS439: Principles of Computer Systems January 30, 2019

Shell Execution of Programs. Process Groups, Session and Signals 1

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

CSC369 Lecture 2. Larry Zhang

Signals. POSIX defines a variety of signal types, each for a particular

CSC369 Lecture 2. Larry Zhang, September 21, 2015

The Process Abstraction. CMPU 334 Operating Systems Jason Waterman

System Programming. Signals I

Processes (Intro) Yannis Smaragdakis, U. Athens

CS 537 Lecture 2 - Processes

What You Need to Know for Project Three. Dave Eckhardt Steve Muckle

COSC243 Part 2: Operating Systems

Processes. Dr. Yingwu Zhu

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

Unix System Programming - Chapter 8

Processes. CS439: Principles of Computer Systems January 24, 2018

Last class: Today: Thread Background. Thread Systems

Exceptions and Processes

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

CS 31: Intro to Systems Processes. Kevin Webb Swarthmore College March 31, 2016

Low-Level Essentials for Understanding Security Problems Aurélien Francillon

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

CS213. Exceptional Control Flow Part II. Topics Process Hierarchy Signals

Assembly Language: Function Calls

+ Overview. Projects: Developing an OS Kernel for x86. ! Handling Intel Processor Exceptions: the Interrupt Descriptor Table (IDT)

CSci 4061 Introduction to Operating Systems. (Advanced Control Signals)

The Instruction Set. Chapter 5

Low Level Programming Lecture 2. International Faculty of Engineerig, Technical University of Łódź

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Assembly Language: Function Calls" Goals of this Lecture"

Getting to know you. Anatomy of a Process. Processes. Of Programs and Processes

Digital Forensics Lecture 3 - Reverse Engineering

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

Assembly Language: Function Calls" Goals of this Lecture"

CS 318 Principles of Operating Systems

Assembly Language: Function Calls. Goals of this Lecture. Function Call Problems

Linux Signals and Daemons

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

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

CS 318 Principles of Operating Systems

Overview. Administrative. * HW 1 grades. * HW 2 Due. Topics. * 5.1 What is a Signal? * Dealing with Signals - masks, handlers

OS Structure, Processes & Process Management. Don Porter Portions courtesy Emmett Witchel

For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to

Problem Set 2. CS347: Operating Systems

The Microprocessor and its Architecture

4. The Abstraction: The Process

What is a Process? Processes and Process Management Details for running a program

The x86 Architecture

Hardware and Software Architecture. Chapter 2

Objectives. Making TOS preemptive Avoiding race conditions

µ-kernel Construction (12)

CS 31: Intro to Systems Functions and the Stack. Martin Gagne Swarthmore College February 23, 2016

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

x86 architecture et similia

Announcements Processes: Part II. Operating Systems. Autumn CS4023

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

COS 318: Operating Systems. Overview. Prof. Margaret Martonosi Computer Science Department Princeton University

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

Processes and Non-Preemptive Scheduling. Otto J. Anshus

x86 Assembly Tutorial COS 318: Fall 2017

CSE 410: Computer Systems Spring Processes. John Zahorjan Allen Center 534

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission

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

A read or write being atomic means that its effect is as if it happens instantaneously.

6.828: Using Virtual Memory. Adam Belay

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

W4118 Operating Systems. Junfeng Yang

Computer Systems Assignment 2: Fork and Threads Package

Processes. Johan Montelius KTH

A process. the stack

Signals are a kernel-supported mechanism for reporting events to user code and forcing a response to them. There are actually two sorts of such

Final Exam. Spring Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

Each terminal window has a process group associated with it this defines the current foreground process group. Keyboard-generated signals are sent to

Processes and Threads

X86 Stack Calling Function POV

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Most of the work is done in the context of the process rather than handled separately by the kernel

Lesson 3. The func procedure allows a user to choose the action upon receipt of a signal.

Shell and Signals. Computer Organization 3/17/2015. CSC252 - Spring The World of Multiprogramming or Multitasking. Unix Process Hierarchy

Basic Execution Environment

Process. Heechul Yun. Disclaimer: some slides are adopted from the book authors slides with permission 1

IA32 Intel 32-bit Architecture

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Subprograms: Local Variables

Project 2-1 User Programs

1 Programs and Processes

System calls and assembler

MICROPROCESSOR TECHNOLOGY

Transcription:

Project 2: Signals Consult the submit server for deadline date and time You will implement the Signal() and Kill() system calls, preserving basic functions of pipe (to permit SIGPIPE) and fork (to permit SIGCHLD). The primary goal of this assignment is to develop an understanding of the behavior of signal handlers and the interactions between signals and processes. This assignment also reinforces register state manipulation from the fork and exec assigment, adding changes to the user stack. 1 Signals A signal is an inter-process communication mechanism that involves causing one of a small array of functions to be invoked in another process. Each process can use the signal system call to manipulate a table that lists signal handlers (function pointers) to be invoked at the request of another process. To send a signal, a process calls the kill system call with the pid of the target and the signal number to send. The signal number represents the index into the table of signal handlers in the target process. The kernel will then arrange for the signal handler function to be called within the process. This is your main task. The tricky part is to ensure that when the signal handler returns, control is passed back to the kernel and the process can resume from wherever it was. To accomplish this task, we define a return signal system call that will be invoked as the signal handler returns. In implementing signals, you will need to to arrange for the process to have both a context when executing the signal handler and a saved context that the signal handler will return to after the signal handler completes. 2 System Calls and the Application Interface The Signal() system call will register a handler, which will be a function that takes an integer argument representing the signal number. The Signal() call can also take a behavior, to, for example, ignore a signal or return to default behavior. Registered signal handlers are preserved across Fork(), and discarded across Exec() for reasons that should be obvious. The Kill() system call will deliver a signal to a given process. Signal delivery need not take place synchronously, rather, a signal may be queued for later delivery. This is comparable to how an interrupt might arrive while the processor has interrupts disabled: the interrupt will be delivered once interrupts are enabled. In the signals case, the signal may be delivered just as the process is about to regain the processor. Other actions generate signals, including the death of a child that is not being Wait()ed for (SIGCHLD), a write to a pipe that has no readers (SIGPIPE), or (not for this assignment) a countdown timer alarm (SIGALARM). 2.1 Repository cd geekos-project && git pull Using git, occasionally you will want to invoke git pull to grab any changes we may have made, e.g., to a.submit file or the projects.h file. The projects.h file includes flags that cause different TODO() macros in the distribution to fire. 1

3 Getting Started Implement the following system calls. Sys Signal: This system call registers a signal handler for a given signal number. The signal handler is a function that takes the signal number as an argument (it may not be useful to it), processes the signal in some way, then returns nothing (void). If called with SIGKILL, return an error (EINVALID). The handler may be set as the pre-defined SIG DFL or SIG IGN handlers. SIG IGN tells the kernel that the process wants to ignore the signal (it need not be delivered). SIG DFL tells the kernel to revert to its default behavior, which is to terminate the process on KILL, PIPE, USR1, and USR2, and to discard (ignore) SIGCHLD. A process may need to set SIG DFL after setting the handler to something else. Sys RegDeliver: The signal handling infrastructure requires a special trampoline function to be implemented. This trampoline invokes the system call Sys ReturnSignal (see below) at the conclusion of signal handler. The RegDeliver system call is invoked by Sig Init when called by the Entry function in src/libc/entry.c; i.e., this function is invoked prior to running the user program s main(). Sys Kill: In this project, Kill will be used to send a signal to a certain process. So in addition to the PID, Sys Kill will take a signal number as defined above. It should be implemented as setting a flag in the process to which the signal is to be delivered, so that when the given process is about to start executing in user space again, rather than returning to where it left off, it will execute the appropriate signal handler instead. Sys ReturnSignal: This system call is not invoked by user-space programs directly, but rather is executed by some stub code at the completion of a signal handler. That is, Sys Kill/Send Signal sends process P a signal, which causes it to run its signal handler. When this handler completes, we will have set up its stack so that it will return to the trampoline registered by Sys RegDeliver. This trampoline wil invoke Sys ReturnSignal to indicate that signal handling is complete. Sys WaitNoPID: The Sys Wait system call takes as its argument the PID of the child process to wait for, and returns when that process dies. The Sys WaitNoPID call, in contrast, takes a pointer to an integer as its argument, and reaps any zombie child process that happens to have terminated. It places the exit status in the memory location the argument points to and returns the pid of the zombie. If there are no dead child process, then the system call should return ENOZOMBIES. 3.1 The default handler If the default handler is invoked for a signal that will terminate the process (e.g., SIGKILL), Print("Terminated %d.\n", g_currentthread->pid); and invoke Exit. 3.2 Reentrancy and Preemption Sending a signal should appear as if setting a flag in the PCB about the pending signal; the signal handler need not be executed immediately. In particular, if the process is executing a signal handler, do not start executing another signal handler. Further, multiple invocations of kill() to send the same signal to the same process before it begins handling even one will have the same effect as just one invocation of kill(). For example, if two children finish while another handler is executing (and blocked), the SIGCHLD handler will be called only once. However, if one child finishes while the parent s SIGCHLD handler is executing, another SIGCHLD handler should be called. See the sigaction() man page if in doubt about reentrancy. The delivery order of pending signals is not specified. (They need not be delivered in the order received.) 4 Helpers in signal.c To implement signal delivery, you will need to implement (at least) five routines in src/geekos/signal.c: 2

This struct reflects the contents of the stack when a C interrupt handler function is called. It must be kept up to date with the code in lowlevel.asm. struct Interrupt State { The register contents at the time of the exception. We save these explicitly. uint t gs; uint t fs; uint t es; uint t ds; uint t ebp; uint t edi; uint t esi; uint t edx; uint t ecx; uint t ebx; uint t eax; We explicitly push the interrupt number. This makes it easy for the handler function to determine which interrupt occurred. uint t intnum; This may be pushed by the processor; if not, we push a dummy error code, so the stack layout is the same for every type of interrupt. uint t errorcode; }; These are always pushed on the stack by the processor. uint t eip; uint t cs; uint t eflags; An interrupt that occurred in user mode. If Is User Interrupt(state) returns true, then the Interrupt State object may be cast to this kind of struct. struct User Interrupt State { struct Interrupt State state; uint t espuser; uint t ssuser; }; Figure 1: User Interrupt State 3

Send Signal: takes as its arguments a pointer to the kernel thread to which to deliver the signal, and the signal number to deliver. This should set a flag in the given thread to indicate that a signal is pending. This flag is used by Check Pending Signal, described next. Check Pending Signal: is called by code in lowlevel.asm when a kernel thread is about to be contextswitched to. It returns true if the following THREE conditions hold: 1. A signal is pending for that user process. 2. The process is about to start executing in user space. This can be determined by checking the Interrupt State s CS register: if it is not the kernel s CS register (see include/geekos/defs.h), then the process is about to return to user space. 3. The process is not currently handling another signal (recall that signal handling is non-reentrant). Set Handler: use this routine to register a signal handler provided by the Sys Signal system call. Setup Frame: this routine is called when Check Pending Signal returns true, to set up a user process s user stack and kernel stack so that when it starts executing, it will execute the correct signal handler, and when that handler completes, the process will invoke the Sys ReturnSignal system call to go back to what it was doing. IF instead the process is relying on SIG IGN or SIG DFL, handle the signal within the kernel. IF the process has defined a signal handler for this signal, this function will have to do the following: 1. Choose the correct handler to invoke. 2. Acquire the pointer to the top of the user stack. This is below the saved interrupt state stored on the kernel stack as shown in the figure above. 3. Push onto the user stack a snapshot of the interrupt state that is currently stored at the top of the kernel stack. The interrupt state is the topmost portion of the kernel stack, defined in include/geekos/int.h in struct Interrupt State, shown above. 4. Push onto the user stack the number of the signal being delivered. 5. Push onto the user stack the address of the signal trampoline that invokes the Sys ReturnSignal system call, and was registered by the Sys RegDeliver system call, mentioned above. 6. Change the current kernel stack such that (notice that you already saved a copy in the user stack) (a) The user stack pointer is updated to reflect the changes made in step 3-5. (b) The saved program counter (eip) points to the signal handler. Complete Handler: this routine should be called (by your code) when the Sys ReturnSignal call is invoked, to indicate a signal handler has completed. It needs to restore back on the top of the kernel stack the snapshot of the interrupt state currently on the top of the user stack. 5 Hints Remember that the call assembly instruction does two things: it pushes the address of the next instruction on the stack as the return address, and it overwrites the instruction pointer to the top of the called routine. To invoke a function in assembly (using x86 conventions) requires: 1. saving any caller-save registers (not necessary for us), 2. pushing the arguments onto the stack right-to-left, 3. calling the function, 4. popping the arguments off (or, equivalently, incrementing the stack pointer above the arguments), 5. restoring any saved caller-save registers (not needed for us). 4

You ll probably forget to push or pop something, creating an off-by-something error on a stack pointer that will lead to an exception. You should be able to tell which direction you re off by looking for values that are in the wrong place (for example, finding a segment number in the base pointer field). If you would like to blow your mind, read https://cseweb.ucsd.edu/~hovav/dist/rop.pdf or maybe a summary http://en.wikipedia.org/wiki/return-oriented_programming. We use this sort of technique (point the return address to a function) for good, but it could be powerful evil. 5