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

Similar documents
CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 18

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

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

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

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

THE PROCESS ABSTRACTION. CS124 Operating Systems Winter , Lecture 7

Unix System Programming - Chapter 8

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

CSCI 4061: Signals and Signal Handlers

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

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

Operating Systems 9/6/ SIGINT Terminate Interrupt from keyboard (ctl-c) 9 SIGKILL Terminate Kill program (cannot override or ignore)

Signals. Joseph Cordina

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

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 19

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 20

Signals and Session Management. Signals. Mechanism to notify processes of system events

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

Signals: Management and Implementation. Sanjiv K. Bhatia Univ. of Missouri St. Louis

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

CS 33. Signals Part 1. CS33 Intro to Computer Systems XXII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Processes (Intro) Yannis Smaragdakis, U. Athens

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

an infinite loop Processes and Exceptions doing nothing on a busy system timing nothing

Signal types (some of them) Networks and Operating Systems ( ) Chapter 5: Memory Management. Where do signals come from?

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

Process Control. Philipp Koehn. 23 April 2018

SMD149 - Operating Systems

Signals. Goals of this Lecture. Help you learn about: Sending signals Handling signals

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

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

KING FAHD UNIVERSITY OF PETROLEUM AND MINERALS Information and Computer Science Department ICS 431 Operating Systems Lab # 6

Processes & Signals. System Runs Many Processes Concurrently. State consists of memory image + register values + program counter

Princeton University. Computer Science 217: Introduction to Programming Systems. Signals

COSC243 Part 2: Operating Systems

System Programming. Signals I

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 25

Signals. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CSE 421: Introduction to Operating Systems

Signal Example 1. Signal Example 2

ECE 650 Systems Programming & Engineering. Spring 2018

Processes. Dr. Yingwu Zhu

PROCESS CONTROL BLOCK TWO-STATE MODEL (CONT D)

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

Processes. Processes (cont d)

UNIX System Programming. Overview. 2. Signal Types (31 in POSIX) Signal Sources. Signals. 1. Definition

n Unconditionally kill process 8371 n Interactive interrupt key n See /usr/include/bits/signum.h (e.g., SIGKILL 9) 2 -Ken Wong, Sep 2008

Implementing Lightweight Threads

Computer Science & Engineering Department I. I. T. Kharagpur. Operating System: CS rd Year CSE: 5th Semester (Autumn ) Lecture VII

Multitasking. Programmer s model of multitasking. fork() spawns new process. exit() terminates own process

3. Process Management in xv6

Outline. 1 Details of paging. 2 The user-level perspective. 3 Case study: 4.4 BSD 1 / 19

Signals. CSC209: Software Tools and Systems Programming. Furkan Alaca & Paul Vrbik

Operating Systems. Threads and Signals. Amir Ghavam Winter Winter Amir Ghavam

Lecture 4: Mechanism of process execution. Mythili Vutukuru IIT Bombay

CS 167 Final Exam Solutions

CS 537 Lecture 2 - Processes

F28HS Hardware-Software Interface: Systems Programming

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2015 Lecture 11

W4118: interrupt and system call. Junfeng Yang

ECF Exists at All Levels of a System Exceptional Control Flow Part II Oct. 22, 2002 Topics! Process Hierarchy! Shells! Signals!

Linux Signals and Daemons

Process. Signal #8. Signals are software interrupts from unexpected events. a power failure. an alarm clock. the death of a child process

Processes. Johan Montelius KTH

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

CS 326: Operating Systems. Process Execution. Lecture 5

A process. the stack

CS 378 (Spring 2003)

Last Class. System Calls, Kernel Mode, and Process Implementation. Processes. Today. Operating Systems 9/11/2018 CSC 256/456 1

Exceptional Control Flow Exists at All Levels of a System. Systemprogrammering 2007 Föreläsning 3 Exceptional Control Flow Part II

CSE 153 Design of Operating Systems Fall 18

Interrupts, Fork, I/O Basics

Section 4: Threads and Context Switching

CSC 2400: Computer Systems. Using the Stack for Function Calls

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

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

Exceptional Control Flow Part II

Programming Assignments will be.. All the PAs are continuous 3 major factors that you should consider

What is an Operating System? Signals, Processes, & Threads. Resource Sharing. Resource Abstraction ... Operating Systems 10/20/2010

ECEN 449 Microprocessor System Design. Hardware-Software Communication. Texas A&M University

CS 5460/6460 Operating Systems

Signals and Inter-Process Communica.on

CSE 506: Opera.ng Systems Signals and Inter-Process Communica.on

System Calls & Signals. CS449 Spring 2016

Chapter 5: Memory Management

The UtePC/Yalnix Memory System

Digital Forensics Lecture 3 - Reverse Engineering

CSC 8400: Computer Systems. Using the Stack for Function Calls

Operating Systems 2010/2011

Protection and System Calls. Otto J. Anshus

CSC369 Lecture 2. Larry Zhang

CSC369 Lecture 2. Larry Zhang, September 21, 2015

The Kernel. wants to be your friend

CMPS 105 Systems Programming. Prof. Darrell Long E2.371

Goals of this Lecture

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

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

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

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Giving credit where credit is due

Transcription:

IMPLEMENTATION OF SIGNAL HANDLING CS124 Operating Systems Fall 2017-2018, Lecture 15

2 Signal Handling UNIX operating systems allow es to register for and handle signals Provides exceptional control flow mechanism for es User program registers a signal handler via a system call Example: typedef void (*sig_t)(int); sig_t is a function-pointer to a function that takes an int argument and returns void sig_t signal(int sig, sig_t func); This system call sets the signal handler for the specified signal type, and returns the previous signal handler Declarations are in C standard header signal.h

3 Signal Handling: Example /* Print a message, then request another SIGALRM. */ void handle_sigalrm(int sig) { printf("hello!\n"); alarm(1); /* Request another SIGALRM in 1 second. */ } /* User typed Ctrl-C. Taunt them. */ void handle_sigint(int sig) { printf("ha ha, can't kill me!\n"); } int main() { signal(sigint, handle_sigint); signal(sigalrm, handle_sigalrm); alarm(1); /* Request a SIGALRM in 1 second. */ while (1) pause(); /* Wait for signals in a loop. */ } return 0;

4 Advanced Signal Handling Support UNIX also provides more advanced signal handling: int sigaction(int sig, const struct sigaction *act, struct sigaction *oact) sigaction struct specifies various details, including the kind of handler function: Either the simple void handler(int sig) as before Or, a more advanced handler function: void sigact(int sig, siginfo *info, void *ctxt) The siginfo struct includes many details about signals e.g. sending process ID, memory address that caused fault, etc. ctxt points to a ucontext_t structure A platform/architecture-dependent machine context, containing the CPU state of the, when it was interrupted by signal Facilitates e.g. user-space threading libraries

5 Pending and Blocked Signals The kernel maintains two bit-vectors for every process Every type of signal has a specific bit in this bit-vector pending bit-vector records what signals have yet to be delivered to the process Note: if multiple instances of a given signal occur before a process receives the signal, it will see only one instance of the signal Signals indicate one or more events of given type have occurred blocked bit-vector records what signals are currently not allowed to be delivered to the process Can have a signal that is both blocked and pending When the signal is unblocked, it will be delivered to the process When a signal is delivered to a process, that type of signal is automatically blocked for the process Prevents a given signal handler from interrupting itself One kind of signal can interrupt another kind of signal

6 The Kernel and Signal Handling How does the kernel provide signal handling to user processes? When is a signal delivered to the receiving process? What if process is running on the CPU? What if ready or blocked? How does signal handling affect process scheduling? If a process has multiple signals pending, how does the kernel dispatch all these signals to the process? Note: we are ignoring many issues caused by using signals in multithreaded programs Individual threads can block signals so that only one thread handles signals, etc.

7 Generating and Delivering Signals A process isn t always running when a signal is sent to it e.g. kill() syscall is invoked by another process e.g. a child process dies, causing SIGCHLD to be sent to parent, but a higher priority process currently preempts the parent Kernels make a distinction between generating a signal and delivering the signal Signal generation: kernel updates the data structures of the receiving process to record that the signal was sent Signal delivery: kernel forces the receiving process to respond to the signal (e.g. by invoking a signal handler) Time may pass between generating and delivering signal

8 Process Signal Data Structures Already mentioned pending / blocked signal bit-vectors At a coarse-grain level of detail, records which signals need to be delivered, and which signals are currently blocked from delivery Each process also has a linked list of pending signals siginfo_t struct records relevant details of the pending signal Each process also has an array of signal action structs Specifies how to handle each kind of signal e.g. default action, ignore, or a user-space handler (Flags also record other options for handling signals) Process: pending blocked signals sighand siginfo flags sigaction sigaction sigaction siginfo flags

9 Generating a Signal When a signal is sent to a process: The kernel invokes a specific function to update the process signal structures, perform scheduling tasks, etc. e.g. Linux 2.6 has specific_send_sig_info() kernel function If the process already has a pending signal of that type, the new signal is ignored For real-time signals, this test is skipped; every occurrence of a real-time signal is delivered If the process is ignoring the signal, nothing is done No structures are updated No scheduling tasks occur Process: pending blocked signals sighand siginfo flags sigaction sigaction sigaction siginfo flags

10 Generating a Signal (2) Otherwise, new signal is appended to the signal queue The pending bit-vector is also updated If the process is currently blocked or suspended, it is moved to the ready state Note: a few signal types are not added to signal queue e.g. SIGKILL, SIGSTOP These signals are enforced immediately by the kernel the next time the process runs Affects the process execution state in the scheduler Process: pending blocked signals sighand siginfo flags sigaction sigaction sigaction siginfo flags

11 Delivering Signals Signals are only delivered to the currently running process Obvious; the process must hold the CPU to run the signal handler The kernel checks for pending signals when it is about to return back to the Kernel checks the process signal state If there are pending signals to deliver, they are delivered at this point Two ways a signal can be handled: Signal is ignored, or default action is to be performed These signals are handled by the kernel Signal has a user-mode handler Kernel must invoke the user-mode handler no Kernel Schedule a Pending signal(s)? yes Handle signal(s) Return to User Process Process context

12 Delivering Signals (2) Two ways a signal can be handled: Signal is ignored, or the default action is to be performed Signal has a user-mode handler For kernel-handled signals, it can handle as many as are pending For user-process-handled signals, only one signal is handled Other pending signals will be delivered the next time the scheduler is invoked In Linux 2.6 kernel, signal delivery handled by do_signal() function This code has been greatly restructured in subsequent Linux kernel releases Kernel Schedule a Pending signal(s)? no yes Kernel-handled signal? no yes Handle signal Return to User Process User process signal handler Process context

13 Delivering Signals (3) Several big signal delivery challenges Signal handler must return back to the kernel so that the previous user process context can be restored Signals can interrupt system calls Particularly on preemptible kernels Handlers can make system calls When returning from syscall handler, must return to the signal handler, not the interrupted context Signals can siglongjmp() from the signal handler back to another part of the Kernel Schedule a Pending signal(s)? no yes Kernel-handled signal? no yes Handle signal Return to User Process User process signal handler Process context

14 Delivering Signals (4) Linux 2.6 do_signal() uses a loop: Try to find a pending signal to service If there are multiple pending signals, kernel may choose to service them in a different order from queue order (Some signals also cancel each other, like SIGSTOP and SIGCONT) If no more pending signals, the kernel returns to the If a pending signal was found, it is removed from pending queue User process state is also updated to mark the signal as no longer pending Kernel Schedule a Pending signal(s)? no yes Kernel-handled signal? no yes Handle signal Return to User Process User process signal handler Process context

15 Delivering Signals (5) Linux 2.6 do_signal(), cont. If signal is currently being ignored, do_signal continues to next signal A signal may be ignored if the process has set its handler to SIG_IGN Or, if signal s handler is SIG_DFL and default action is to ignore the signal, the signal is ignored e.g. SIGCHLD is ignored by default Kernel Schedule a Pending signal(s)? no yes Kernel-handled signal? no yes Handle signal Return to User Process User process signal handler Process context

16 Delivering Signals (6) Linux 2.6 do_signal(), cont. Next, do_signal checks if the default action should be used Signal s handler is set to SIG_DFL (and default action is not to ignore) If so, do_signal carries out the action Default actions: Terminate kill the process Dump kill process, create a core dump Ignore ignore signal (handled earlier) Stop move process to suspended state Continue move process to ready state Kernel Schedule a Pending signal(s)? no yes Kernel-handled signal? no yes Handle signal Return to User Process User process signal handler Process context

17 Delivering Signals (7) If none of the previous cases hold, do_signal must invoke the signal handler Recall the state of our stacks within do_signal call: It s easy to cause the to run the signal handler Just set the process EIP to the address of the signal handler Problem: can t just overwrite the previous CPU state of the process When signal handler completes, must return to whatever was interrupted in the Must set up a new CPU context for the signal handler to use User Process Stack stack contents (before signal) Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of interrupted process other kernel stack data

18 Delivering Signals (8) Another problem: the CPU context of the interrupted execution is on the kernel stack but kernel stack will be emptied when kernel returns to user mode! Solution: do_signal copies some critical details to user stack CPU context of the before it was interrupted Other details necessary for properly completing signal handler invocation e.g. a bit-vector of blocked signals, other saved registers not in CPU context, etc. These details are used when the signal handler returns: The kernel uses them to go back to the previous point in the interrupted process Then, the kernel can change the CPU context on the kernel stack to invoke the signal handler User Process Stack stack contents (before signal) of interrupted process other signal handler details 2 1 set regs->eip to handler address Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of interrupted process other kernel stack data

19 Delivering Signals (9) Finally, do_signal must set up the stack frame for signal handler function to use If calling a 1-arg signal handler, just push signal # onto stack If calling a 3-arg signal handler, also push signal details onto stack Since the process interrupted CPU context is already on the stack, the machine context pointer is easy And, it allows signal handlers to modify the process CPU state directly, e.g. to implement user-mode threading libraries User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of signal handler other kernel stack data

20 Delivering Signals (10) One last component needed for the stack frame: The return address for when the signal handler returns Need the signal handler to return to the kernel: Allow kernel to complete final signalhandling tasks, and restore the interrupted process original context The kernel inserts address of code to invoke the sigreturn syscall i.e. a wrapper to code that executes mov NR_sysreturn, %eax; int $0x80 sigreturn has a single purpose: Perform the final task of restoring the interrupted process CPU context from its location on the stack User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of signal handler other kernel stack data

21 Delivering Signals (11) Since user stack was modified, must update caller s esp Finally, do_signal() is done do_signal returns to its caller inside the kernel The kernel returns to user mode Register state of the is restored from the kernel stack User process begins executing the signal handler! Signal handler can make system calls with no problems Original CPU context of the interrupted process is safely stored on user stack User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of signal handler other kernel stack data

22 Delivering Signals (12) When the signal handler returns, the sysreturn system call is invoked Note: again the kernel thread stack contains user-process details The sysreturn syscall copies the original CPU context of interrupted process back into the kernel stack (along with any changes to the CPU context made by the signal handler) Then, sysreturn returns back to the Resumes process execution at the point where the signal interrupted the process User stack pointer is restored as well, eliminating now-unneeded stack data User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of signal handler

23 Signals and System Calls Process may be blocked on a syscall when signal occurs The system call is interrupted by the signal Generally, some action must be taken by the kernel in this case e.g. the system call might return EINTR as its error code Some system calls can be automatically restarted when the signal occurs e.g. read() or write() may be automatically restarted if they hadn t performed any work by the time the signal occurs Some system calls must report that they were interrupted e.g. if nanosleep() is interrupted by a signal, it returns EINTR, and reports the amount of time remaining in the sleep interval

24 Signals and System Calls (2) If a process is in a system call when it receives a signal: It was previously in kernel code when signal is delivered It entered the kernel by performing an int $0x80 trap operation (or a fast system-call, e.g. using sysenter instruction) Or, the process may be in some blocked queue The signal delivery mechanism will see the interrupt # in process state Original value of eax is also stored, indicates which syscall was interrupted User Process Stack stack contents (before signal) Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of interrupted process other kernel stack data

25 Signals and System Calls (3) Depending on the process configuration, and on system call that was interrupted, kernel takes one of two actions: Option 1: the kernel modifies the interrupted process CPU context to show the system call as interrupted Sets the process eax register to -EINTR The process will see the system call return this error code Option 2: the kernel modifies process CPU context to rerun the system call Sets the process eax register to the original system call number Subtracts 2 from process eip register to force int $0x80 (or sysenter) to run again User Process Stack stack contents (before signal) Kernel Thread Stack caller s ss caller s esp caller s eflags caller s cs caller s eip error code interrupt no. of interrupted process other kernel stack data

26 Signal Handlers and siglongjmp Signal handlers can use siglongjmp() to jump to another part of the user program Effectively terminates the signal handler and resumes execution of the user program at a different point Described as a non-local goto In fact, could use longjmp() or siglongjmp() sigsetjmp / siglongjmp are strongly preferred because they can optionally save and restore blocked-signals mask In practice, that is the only difference between setjmp and sigsetjmp User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr signal handler stack data Kernel Thread Stack (empty)

27 Signal Handlers and siglongjmp (2) Does performing a siglongjmp from a signal handler break the kernel s signal-delivery machinery at all? Answer should be obvious from the stack state, and from User Process Stack previous discussion Recall: When a pending signal is dequeued by do_signal(), process state is updated to show the signal as no longer pending Kernel doesn t require the signal handler to return in order to complete processing As far as kernel is concerned, signal is done! Only task is to restore blocked signal mask (which siglongjmp can also take care of) stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr signal handler stack data Kernel Thread Stack (empty)

28 Signal Handlers and siglongjmp (3) Also: kernel will always check for pending signals to deliver, when returning to the Any pending signals will likely be delivered the next time the scheduler starts running the process again Finally, the kernel s signal-delivery mechanism leaves no state on the kernel stack All state for both the signal handler and the interrupted point in the process is contained within the user stack If a long-jumps out of signal handler, it will have no effect on delivery of any future signals User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr signal handler stack data Kernel Thread Stack (empty)

29 Signal Handlers and siglongjmp (4) Why would a process long-jump out of signal handler? Needs to have a jump-buffer that records the CPU state at some earlier point in stack A process that long-jumps out of a signal handler doesn t want to restore the old execution state! The process doesn t want to resume executing the code that was interrupted when the signal occurred Long-jump will discard all intervening execution state, including CPU state saved by the kernel at signal delivery No need to invoke sysreturn; the longjump will restore the desired CPU state And, siglongjmp will restore the blockedsignal mask to an appropriate state sigjmp_buf User Process Stack stack contents (before signal) of interrupted process other signal handler details void *uctxt siginfo *info int signal sigreturn addr signal handler stack data Kernel Thread Stack (empty)

30 Next Time Kernels maintain complex structures on behalf of es Kernel has a very limited amount of memory for dynamic allocation Kernel has a fixed memory area for data that pertains to all processes And allocation needs to be fast 0xc0000000 %esp Process-specific data structures Kernel stack Mapping to physical memory Kernel code and global data User stack Kernel Space Next time: kernel allocators 0x40000000 brk Memory mapped region for shared libraries Run-time heap (via malloc) User Space Uninitialized data (.bss) Initialized data (.data) 0x08048000 0 Program text (.text) Forbidden