The OS Structure, User & Kernel Mode, Interrupts

Operating System Structure

OS Hierarchy

Kernel: Minimal set of functions necessary to manage system resources safely and efficiently.

The OS Hierarchy:

  1. Applications and System Software
  2. General Libraries
  3. System Call Libraries
  4. OS Kernel

Remember: Applications and System Software can use abstractions like library functions or just invoke kernel functions directly via supervisor calls.

System Programs

System Programs: Provide convenient environment for program development and execution.

Common Operating System Structures

The following are some different operating system structures.

MS-DOS (Simple)

Written to provide the most functionality in the least space.

Problems with MS-DOS — Application programs have direct access to device drivers.

UNIX (Monolithic)

Two separable parts, created by limited hardware.

  1. System Programs
  2. The Kernel (everything below system-call interface and above physical hardware)

Problems with UNIX — In a monolithic kernel, one breakage could break everything.

Layered Approach

Operating system divided into layers.

Examples — Windows NT, XP, Vista

Problems with Layered — How do you order layers?

Microkernel (Small)

Implements only the essential functions (e.g., communication, memory management, CPU scheduling) rather than monolithically.

Examples — macOS mach

Problems with Microkernel — Efficiency issues from constantly dipping into the kernel for essential functions

Modules

Modules: Many operating systems implement loadable kernel modules.

Examples — Linux, Solaris

CPU Modes and Protection

User v.s. Kernel Mode

We divide the CPU instruction set into two sets:

  1. Privileged
  2. Non-Privileged

The CPU can operate in two modes (mode bit):

  1. Kernel Mode: Privileged and non-privilege instructions allowed.
  2. User Mode: Only non-privileged instructions allowed.

Why? — Accessing I/O devices, control registers, etc. is risky for security, so we only let the kernel do it.

Example: Transitioning from user mode to kernel mode

User Process:

  1. User Process Executing
  2. Call System Call

trap mode bit = 0

Kernel:

  1. Execute System Call

return mode bit = 1

User Process:

  1. Return from system call

Remember — User space and kernel space are separate and protected, but a single user process can operate in both.

Mode Transition: System Calls

System Calls and Supervisor Calls

System Call: A higher-level library function.

Supervisor Call: Lower-level privileged instruction that automatically transfers execution control to a well-defined location in the kernel.

Definitions:

Remember: Changing the mode bit of the CPU from user \to kernel mode can only be done by a privileged instruction.

System Call Implementation

System calls can be:

  1. Defined in the main body of the kernel, or
  2. Loaded in main memory

Typically a system call number is associated with each system call.

Why a System Call Number? — This enforces the “well-defined location” rule. The user process provides the index and can’t jump to arbitrary addresses in the kernel.

System-Call Interface:

More on Branch Vector:

Branch Vector: A data structure that contains a list of function addresses pointing to specific kernel routines.

Analogy: Restaurant Menu

Passing Parameters to the Kernel

Three methods to pass parameters to system calls:

  1. Registers: Put data right into registers.
  2. Block/Table in Memory: Put address of table/array/list in register.
  3. Stack in Memory: Push parameters to stack, let kernel pop them off.

Why? — Storing in memory (block or stack) allows us to avoid limits like having limited registers.

Types of System Calls

Six major categories of system calls:

  1. Process Control
  2. File Manipulation
  3. Device Manipulation
  4. Information Maintenance
  5. Communication
  6. Protection

Interrupts and Traps

Interrupt: Event that diverts current execution of a program to respond to an event.

Trap: An interrupt triggered by the currently-executing instruction.

Remember — Interrupts (internal and external) stop the execution of the current process, save the state of computation, and transfer control to the kernel.

Example: Interrupts and Traps Overview

Two Most-Common Uses of Interrupts:

  1. An I/O device signals to the OS the completion of an I/O operation.
  2. Implementation of time-sharing CPU among multiple concurrent computations (interrupt generated by countdown timer).

Examples of Traps:

Example: System Call with Multiprogramming without Timesharing

Suppose two applications, Application 1 and Application 2

The first trap occurs when switching the CPU from user mode to kernel mode.

Why? — Application 1 wants to call S() \to Supervisor Call \to TRAP

Now in the kernel, the kernel will:

  1. Start slow I/O, and
  2. Block app

This is so that the CPU can move onto application 2 while the I/O device works.

An interrupt is generated by the I/O device once it is finished.

Interrupt Handler

Interrupt Handler: Kernel function invoked whenever an interrupt occurs.