Technical Women

037
Figure 1. Barbara Liskov

Today

  • Finish hardware interrupts.

  • Software interrupts and exceptions.

$ cat announce.txt

  • I have good news and bad news…​

Good News: test161!

  • test161 has been released. Please follow the installation instructions on Discourse.

  • …​And it’s awesome. Thanks to Scott and Yihong!

  • test161 submit should be done shortly. If needed, we’ll push the ASST1 due date. (But don’t plan on it.)

Bad News

  • It was brought to my attention that there was inappropriate material presented in yesterday’s recitation.

  • As a result, if you would like partial credit on the assignment for the parts that were revealed please contact the course staff.

  • (Note that the "solutions" that were revealed were not entirely correct, so please beware. We are preparing an update to the test cases to catch these errors.)

  • Note also that you need working locks and CVs for ASST2 and ASST3, so receiving credit will not solve that problem.

  • Note that the video for yesterday’s recitation will not be posted.

  • Any questions?

Questions: Privileges, Interrupts, Exceptions?

Review: Operating System Privilege

Why does the operating system need special privileges?
  • To divide resources between multiple application and enforce these divisions.

True or false: implementing common operating system abstractions requires special privileges?
  • False. Exokernels provide a design point indicating that implementing most of operating system abstractions in unprivileged libraries is possible.

Review: Operating Modes

How does the processor provide the operating system with special privileges?
  • Through privileged or kernel mode which allows the kernel to use special instructions.

Review: Interrupt Handling

When an interrupt happens the CPU:
  1. Enters privileged mode,

  2. records state necessary to allow the operating system to process the interrupt

  3. jumps to a fixed memory location and begins executing instructions.

Review: Hardware Interrupts

Hardware interrupts occur when a device needs attention.

What are examples of hardware interrupts?
  1. A disk read completed.

  2. A network card received a packet.

  3. A timer fired.

Questions: Privileges, Interrupts, Exceptions?

Masking Interrupts

  • Hardware interrupts can be either asynchronous or synchronous.

  • Asynchronous interrupts can be ignored or masked.

    • The processor provides an interrupt mask allowing the operating system to choose which interrupts will trigger the ISR.

    • If an interrupt is masked, it will not trigger the ISR.

    • If an interrupt is still asserted when it is unmasked, it will trigger the ISR at that point.

  • Some interrupts are synchronous or not maskable: these typically indicate very serious conditions which must be handled immediately.

    • Example: processor reset.

Why Mask Interrupts?

  • Choosing to ignore interrupts allows the system to prioritize certain interrupts over others.

    • "The DVD write buffer is empty" is more important than "There is a network packet waiting to be processed."

  • In some cases handling certain interrupts generates other interrupts which would prevent the system from handling the original interrupt!

Protecting Interrupt Handlers

  • What would happen if applications were allowed to modify the interrupt service routines?

    • Applications could take control of devices by preventing the kernel from communicating with them. Disaster!

  • Interrupt handlers allow the operating system to control access to hardware devices and protect them from direct control by untrusted applications.

  • The memory that contains interrupt handlers is protected from access by user applications.

  • One of the first things that the kernel does on boot is install its interrupt handlers!

Software Interrupts

Given that the operating system prevents unprivileged code from directly accessing system resources, how do applications gain access to these protected resources?

  • "Hey, over here, I need to read some data from disk!"

  • "I could really use some more memory. Please?"

syscall

  • CPUs provide a special instruction (syscall on the MIPS) that generates a software (or synthetic) interrupt.

  • Software interrupts provide a mechanism for user code to indicate that it needs help from the kernel.

  • Rest of the interrupt handling path is unchanged. The CPU:

    1. enters privileged mode,

    2. records state necessary to process the interrupt,

    3. jumps to a pre-determined memory location and begins executing instructions.

Making System Calls

To access the kernel system call interface an application:

  1. arranges arguments to the system call in an agreed-on place where the kernel can find them, typically in registers or on its stack,

  2. loads a number identifying the system call it wants the kernel to perform into a pre-determined register, and

  3. executes the syscall instruction.

libc provides the wrappers around the syscall instruction that programmers are familiar with.

You can find this code in your OS/161 source tree. Look around!

Software Exceptions

An software exception indicates that code running on the CPU has created a situation that the processor needs help to address.

Can you think of examples of software exceptions?
  • Divide by zero—​probably kills the process.

  • Attempt to use a privileged instruction—​also probably kills the process.

  • Attempt to use a virtual address that the CPU does not know how to translate—​a common exception handled transparently as part of virtual memory management.

Software Exceptions v. Software Interrupts

Interrupts are voluntary.
  • Think of the CPU as saying to the kernel: "The /bin/true process would like your assistance."

Exceptions are non-voluntary.
  • Think of the CPU as saying to the kernel: "I need some help with this /bin/false process. It just tried to divide by zero and I think it needs to be terminated."

Questions?

Next Time

  • Multiplexing: a historical perspective.

  • Preemption and context switching.