CS372: Solutions for Homework 1


 

Problem 1:

Explain the steps that an operating system goes through when the CPU receives an interrupt.

First, the hardware switches the machine to supervisor mode and causes control to transfer to a routine at a pre-specified
address. The operating system has already loaded that address with a pointer to a function that handles the interrupt. The
function starts by saving all registers, possibly setting the allowed interrupts to a level that guarantees correct execution of the
operating system, and then executes the code to serve the interrupt. When the operating system is done, it restores the
registers again and calls the "reti" to return to the user program that was running when the interrupt occurred. If there is no
runnable program, the operating system jumps to the idle loop and waits for an interrupt. Finally, if the interrupt occurred
while the operating system was running, then the OS simply restores the registers and jumps to the instruction following the
one at which the interrupt occurred.

Problem 2:

Define three styles of switching from user mode to supervisor mode.
  1. Interrupts, when a device sends an interrupt to the CPU.
  2. When a program executes a system call, which is usually implemented by a trap instruction.
  3. When a program performs an operation that causes a hardware exception, such as divide by zero, illegal memory access or execution of an illegal opcode.

Problem 3

A typical hardware architecture provides an instruction called "return from interrupt", and abbreviated by something like "reti". This instruction switches the mode of operation from supervisor mode to user mode. This instruction is usually only available while the machine is running in supervisor mode.
  1. Explain where in the operating system this instruction would be used.
  2. What happens if an application program executes this instruction?
  1. The operating system calls "reti" whenever it wants to give control to a user program. This happens at the end of a contextswitch, or when an interrupt service routine is finished.
  2. Since the "reti" instruction is usually a privileged instruction, an application program trying to execute it will cause a hardware exception, because it is illegal for user programs to execute privileged opcodes.