Exceptions and faults
Video 032 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 4:45, with English and Hindi audio.
A program runs normally, then suddenly tries something impossible. It reads memory it does not own, or divides a number by zero. The processor notices, and raises an exception. This article explains what an exception is, the three kinds of exception and what each one allows, and how the kernel answers. It ends with a small, safe experiment on a lab machine that shows a segmentation fault and its exit status.
What you will learn
- An exception is an event that the processor detects while it runs an instruction.
- A fault is reported before the instruction finishes, so it can often be fixed and retried.
- A trap is reported after the instruction, and an abort is severe enough that the program cannot resume.
- The kernel either fixes the cause or ends the program with a signal.
What an exception is and who handles it
An exception is an event that the processor detects while it runs an instruction. The cause comes from the instruction itself, not from a device. The processor then hands control to a handler in the kernel, much as it does for an interrupt. The kernel decides what happens next.
Many exceptions are routine. The kernel fixes the cause, and the program carries on as if nothing happened.
Faults, traps and aborts: three kinds, three meanings
- Fault: reported before the instruction completes. Once the cause is fixed, the instruction can run again. A typical example is a page fault, where a block of memory is not in place yet.
- Trap: reported after the instruction completes. Breakpoints work this way: the program stops just past the marked spot, so a debugger can look around.
- Abort: a severe error. The processor cannot restore the program's state, so the program cannot continue. A double fault, where the handler itself fails, is one example.
How the kernel answers an exception
The processor stops the instruction and looks up the handler. Its table of handlers was set up by the kernel in advance. The processor switches into kernel mode and jumps to that handler, so the kernel is now in charge.
The kernel asks whether it can fix the cause. If it can, the instruction runs again, as if nothing had happened. If it cannot, the kernel ends the program by sending it a signal. Linux names the signal by its meaning. A memory violation becomes the segmentation fault signal, which most people simply call a segfault.
Causing a segmentation fault safely on a lab machine
Note: Try this only on your lab machine, never on production. The Python line below reads memory at address zero, which no program is allowed to use.
python3 -c 'import ctypes; ctypes.string_at(0)'The processor raises a fault, because that address is not mapped to real memory. The kernel finds no valid mapping and sends the signal. The shell may also print a segmentation fault message.
echo $?The shell reports 139, which is 128 plus 11. The number 11 is the number of the segmentation fault signal.
kill -l 11The short name of signal 11 is SEGV, the name of the segmentation fault.
Where exceptions go wrong, and how to notice
A bad pointer is a common cause of faults in programs. A pointer is a variable that holds a memory address. Pointing at the wrong place crashes that program alone, and the rest of the machine carries on.
A fault in kernel mode is more serious. Kernel code is trusted with everything, so a fault there can bring down the whole machine. You would notice a program fault in its own logs. A kernel fault shows up as an oops or a panic, which halts the machine, on the console.
Make a segmentation fault and read its status
Use only a lab machine, never production. You need a terminal with python3. Run the three commands in order, so you can see the crash and then read its exit status and signal name.
Read memory at address zero. No program may use that address, so the program dies.
python3 -c 'import ctypes; ctypes.string_at(0)'What you should see: The program stops. The shell may print a segmentation fault message.
Show the exit status of the program you just ran. The status tells you how it ended.
echo $?What you should see: 139, which is 128 plus 11, the number of the segmentation fault signal.
Ask the shell for the name of signal 11.
kill -l 11What you should see: SEGV, the short name for the segmentation fault.
Terms
- Exception
- An event that the processor detects while it runs an instruction, which hands control to the kernel.
- Page fault
- A fault raised when a program uses a memory address that is not yet mapped to real memory.
- Segmentation fault
- The error that happens when a program touches memory it may not use. Linux stops the program with the SEGV signal.
- Pointer
- A variable that holds a memory address.
Check yourself
What is the difference between a fault and an abort?
A fault is reported before the instruction finishes, so it can often be fixed and retried. An abort is severe, and the program cannot be resumed.
What does an exit status of 139 tell you?
The program ended with a segmentation fault, because 139 is 128 plus 11, and 11 is the segmentation fault signal.
Why is a fault in kernel mode more serious than one in a program?
The kernel is trusted with everything, so a fault there can bring down the whole machine, not just one program.
Previous: Interrupts: how devices get the CPU's attention. Next: Context switches and what they cost.