The Linux Kernel: Signals & Interrupts CS591 (Spring

The Linux Kernel: Signals & Interrupts CS591 (Spring 2001) Signals Introduced in UNIX systems to simplify IPC. Used by the kernel to notify processes of system events. A signal is a short message sent to a process, or group of processes, containing the number identifying the signal. No data is delivered with traditional signals. POSIX.4 defines i/f for queueing & ordering RT signals w/ arguments.

CS591 (Spring 2001) Example Signals Linux supports 31 non-real-time signals. POSIX standard defines a range of values for RT signals: SIGRTMIN 32 SIGRTMAX (_NSIG-1) in # 1 2 9 10 11

Signal Name SIGHUP SIGINT SIGKILL SIGUSR1 SIGSEGV CS591 (Spring 2001) Default Action Abort Abort Abort Abort Dump Comment Hangup terminal or process

Keyboard interrupt (usually Ctrl-C) Forced process termination Process specific Invalid memory reference Signal Transmission Signal sending: Kernel updates descriptor of destination process. Signal receiving: Kernel forces target process to handle signal. Pending signals are sent but not yet received. Up to one pending signal per type for each process, except for POSIX.4 signals.

Subsequent signals are discarded. Signals can be blocked, i.e., prevented from being received. CS591 (Spring 2001) Signal-Related Data Structures sigset_t stores array of signals sent to a process. The process descriptor (struct task_struct in ) has several fields for tracking sent, blocked and pending signals. struct sigaction { void (*sa_handler)();/* handler address, or SIG_IGN, or SIG_DFL */ sigset_t sa_mask; int sa_flags; }

CS591 (Spring 2001) /* blocked signal list */ /* options e.g., SA_RESTART */ Sending Signals A signal is sent due to occurrence of corresponding event (see kernel/signal.c). e.g., send_sig_info(int sig, struct siginfo *info, struct task_struct *t); sig is signal number. info is either:

address of RT signal structure. 0, if user mode process is signal sender. 1, if kernel is signal sender. e.g., kill_proc_info(int sig, struct siginfo *info, pid_t pid); CS591 (Spring 2001) Receiving Signals Before process p resumes execution in user mode, kernel checks for pending non-blocked signals for p. Done in entry.S by call to ret_from_intr(), which is invoked after handling an interrupt or exception. do_signal() repeatedly invokes dequeue_signal() until no more non-blocked pending signals are left. If the signal is not ignored, or the default action is

not performed, the signal must be caught. CS591 (Spring 2001) Catching Signals handle_signal() is invoked by do_signal() to execute the processs registered signal handler. Signal handlers reside (& run) in user mode code segments. handle_signal() runs in kernel mode. Process first executes signal handler in user mode before resuming normal execution. Note: Signal handlers can issue system calls. Makes signal mechanism complicated. Where do we stack state info while crossing

kernel-user boundary? CS591 (Spring 2001) Re-execution of System Calls Slow syscalls e.g. blocking read/write, put processes into waiting state: TASK_(UN)INTERRUPTIBLE. A task in state TASK_INTERRUPTIBLE will be changed to the TASK_RUNNING state by a signal. TASK_RUNNING means a process can be scheduled. If executed, its signal handler will be run before completion of slow syscall. The syscall does not complete by default. If SA_RESTART flag set, syscall is restarted after signal handler finishes. CS591 (Spring 2001)

Real-Time Signals Real-Time signals are queued as a list of signal_queue elements: struct signal_queue { struct signal_queue *next; siginfo_t info; /* See asm-*/siginfo.h */ } A processs descriptor has a sigqueue field that points to the first member of the RT signal queue. send_sig_info() enqueues RT signals in a signal_queue.

dequeue_signal() removes the RT signal. CS591 (Spring 2001) RT Signal Parameters siginfo_t contains a member for RT signals. The argument to RT signals is a sigval_t type: typedef union sigval { int sigval_int; void *sival_ptr; } sigval_t; Extensions? Explicit scheduling of signals and corresponding

processes. CS591 (Spring 2001) Signal Handling System Calls int sigaction(int sig, const struct sigaction *act, struct sigaction *oact); Replaces the old signal() function. Used to bind a handler to a signal. For RT signals, the handlers prototype is of form: void (*sa_sigaction)(int, siginfo_t *, void *); See Stevens Advanced Programming in the UNIX Environment for more CS591 (Spring 2001)

Interrupts Interrupts are events that alter sequence of instructions executed by a processor. Maskable interrupts: Sent to INTR pin of x86 processor. Disabled by clearing IF flag of eflags register. Non-maskable interrupts: Sent to NMI pin of x86 processor. Not disabled by clearing IF flag. Exceptions:

Can be caused by faults, traps, programmed exceptions (e.g., syscalls) & hardware failures. CS591 (Spring 2001) Interrupt & Exception Vectors 256 8-bit vectors on x86 (0..255): Identify each interrupt or exception. Vectors: 0..31 for exceptions & non-maskable interrupts. 32..47 for interrupts caused by IRQs. 48..255 for software interrupts. Linux uses vector 128 (0x80) for system calls. CS591 (Spring 2001)

IRQs & Interrupts Hardware device controllers that issue interrupt requests, do so on an IRQ (Interrupt ReQuest) line. IRQ lines connect to input pins of interrupt controller (e.g., 8259A PIC). Interrupt controller repeatedly: Monitors IRQ lines for raised signals. Converts signal to vector & stores it in an I/O port for CPU to access via data bus. Sends signal to INTR pin of CPU. Clears INTR line upon receipt of ack from CPU on designated I/O port.

CS591 (Spring 2001) Example Exceptions # Exception 0 Divide Error 1 Debug Exception Handler Signal divide_error() SIGFPE debug() SIGTRAP 6 Invalid Opcode invalip_op()

14 Page Fault CS591 (Spring 2001) page_fault() SIGILL SIGSEGV Interrupt Descriptor Table A system Interrupt Descriptor Table (IDT) maps each vector to an interrupt or exception handler. IDT has up to 256 8-byte descriptor entries.

idtr register on x86 holds base address of IDT. Linux uses two types of descriptors: Interrupt gates & trap gates. Gate descriptors identify address of interrupt / exception handlers Interrupt gates clear IF flag, trap gates dont. CS591 (Spring 2001) Interrupt Handling CPU checks for interrupts after executing each instruction. If interrupt occurred, control unit: Determines vector i, corresponding to interrupt. Reads ith entry of IDT referenced by idtr. IDT entry contains a segment selector,

identifying a segment descriptor in the global descriptor table (GDT), that identifies a memory segment holding handler fn. Checks interrupt was issued by authorized source. CS591 (Spring 2001) Interrupt Handling continued Control Unit then: Checks for a change in privilege level. If necessary, switches to new stack by: Loading ss & esp regs with values found in the task state segment (TSS) of current process. Saving old ss & esp values. Saves state on stack including eflags, cs & eip.

Loads cs & eip w/ segment selector & offset fields of gate descriptor in ith entry of IDT. Interrupt handler is then executed! CS591 (Spring 2001) Protection Issues A general protection exception occurs if: Interrupt handler has lower privilege level than a program causing interrupt. Applications attempt to access interrupt or trap gates. What would it take to vector interrupts to user level? Programs execute with a current privilege level

(CPL). e.g., If gate descriptor privilege level (DPL) is lower than CPL, a general protection fault occurs. CS591 (Spring 2001) Gates, Gates but NOT Bill Gates! Linux uses the following gate descriptors: Interrupt gate: DPL=0, so cannot be accessed by user mode progs. System gate: DPL=3, so can be accessed by user mode progs. e.g., vector 128 accessed via syscall triggered by int 0x80. Trap gate: DPL=0. Trap gates are used for activating exception handlers.

CS591 (Spring 2001) Initializing IDT Linux uses the following functions: set_intr_gate(n, addr); set_trap_gate(n,addr); set_system_gate(n,addr); Insert gate descriptor into nth entry of IDT. addr identifies offset in kernels code segment, which is base address of interrupt handler. DPL value depends on which fn (above) is called. e.g., set_system_gate(0x80,&system_call); CS591 (Spring 2001)

Recently Viewed Presentations

  • Medical Terminology  Module 2 - Prefixes PREFIXES Now

    Medical Terminology Module 2 - Prefixes PREFIXES Now

    Slide 7 of 10 A/An - a negative condition Arthr/Arthro - joint A term you learned earlier in this chapter was esthesia, which refers to sensation, feeling, or perception. Adding the prefix an to this word gives us the word...
  • Ascending & Descending nerve tracts Sanjaya Adikari Department

    Ascending & Descending nerve tracts Sanjaya Adikari Department

    Lemniscus. This term is used for some ascending (sensory) nerve tracts in the upper part of the medulla, pons and midbrain. Spinal lemniscus. combination of spinothalamic, spinotectal tracts. Medial lemniscus. crossed posterior column fibers. Lateral lemniscus. 3rd neuron of auditory...
  • 8g彩票代理


    8g彩票代理 版权所有 友情链接:东方彩票 天天彩票网站 京彩彩票 网上买彩票的正规网站 疾速赛车 免责声明 本站资料及图片来源互联网文章 . 本网不承担任何由内容信息所引起的争议和法律责任。 所有作品版权归原创作者所有,与本站立场无...
  • Seasonal Forecasting: Lecture 4 Analysis of real-time predictions

    Seasonal Forecasting: Lecture 4 Analysis of real-time predictions

    December 2015. ERAI. Maidens et al. 2019. NAO+. Also trough over N Atlantic. South-westerly flow. Here's our positive NAO in early winter. The notable feature is the troughing over the Atlantic and SW flow.
  • Tissue vitality and injury effect

    Tissue vitality and injury effect

    Recall techniques of internal fixation that avoid soft-tissue damage while preserving blood supply to the bone. Describe the role of the fibula in the operative treatment of distal tibial fractures. Teaching points: Indirect reduction for preliminary stabilization and correction of...
  • Biot-Savart Law - Georgetown High School

    Biot-Savart Law - Georgetown High School

    Seb Oliver Lecture 14: Biot-Savart Law Summary: Lecture 13 Practical uses of moving charge in magnetic field Lorentz Force Force on Wire Biot-Savart Law Introduction We have discussed how an existing magnetic field influences moving charges (and thus currents) We...
  • CTM for 1C - The Citadel

    CTM for 1C - The Citadel

    CTM for 1C and the Senior Mentor Program. CTM 1-1. ... During the drill period, selected sword bearers circulate and make individual corrections. ... "Sandwich" the conversation by beginning and ending on a positive note. Restore and maintain the relationship....
  • O caldo de pedra - Servidor profismael

    O caldo de pedra - Servidor profismael

    Esta sopa irá consolar, até um anjo que a venha provar! Depois de meter tudo na panela, deixou ferver bem e era um regalo o cheiro gostoso que saía dela. Pegou na colher e pôs-se a prová-lo! Pôs-se a comer...