The OS Structure, User & Kernel Mode, Interrupts

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.

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 mode processes and kernel mode processes are separate!

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.

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

More on Branch Vector:

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

Analogy: Restaurant Menu

System Call Implementation

System calls can be:

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

Typically a number is associated with each system call.

System-Call Interface:

System Call Parameter Passing

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 params 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 program, 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.

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.

System Programs

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

Different Operating System Structures

The following are some different operating system structures.

MS-DOS (Simple)

Written to povide most functionality in least sapce.

Problems: Poor security due to unprotected layers.

UNIX (Monolithic)

UNIX: Two separable parts, created by limited hardware.

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

Problems: In a monolithic kernel, one breakage could break everything.

Layered Approach

Layered: Operating system divided into layers.

Examples: Windows NT, XP, Vista

Problems: Ordering layers. (Does memory management layer goes above security? Below I/O? Why?)

Microkernel (Small)

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

Examples: macOS mach

Problems: Communication between user mode and microkernel (e.g., flipping mode bit constantly) introduces efficiency issues.

Modules

Modules: Many operating systems implement loadable kernel modules.

Example: Linux, Solaris