Kernel: Minimal set of functions necessary to manage system resources safely and efficiently.
The OS Hierarchy:
Remember: Applications and System Software can use abstractions like library functions or just invoke kernel functions directly via supervisor calls.
System Programs: Provide convenient environment for program development and execution.
The following are some different operating system structures.
Written to provide the most functionality in the least space.
Problems with MS-DOS — Application programs have direct access to device drivers.
- Poor security due to unprotected layers.
- Results in errors and crashes that can’t be caught by the OS.
Two separable parts, created by limited hardware.
Problems with UNIX — In a monolithic kernel, one breakage could break everything.
- Also, maintenance and debugging cost increases.
Operating system divided into layers.
Examples — Windows NT, XP, Vista
Problems with Layered — How do you order layers?
- Does memory management layer go above or below security? What about I/O? Why?
- Functions can invoke multiple services (e.g., reading a file invokes I/O layer and then memory layer; but writing a file would invoke memory layer and then I/O layer; making optimal layer ordering challenging)
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
- Flipping mode bit over-and-over introduces efficiency issues.
Modules: Many operating systems implement loadable kernel modules.
Examples — Linux, Solaris
We divide the CPU instruction set into two sets:
The CPU can operate in two modes (mode bit):
Why? — Accessing I/O devices, control registers, etc. is risky for security, so we only let the kernel do it.
User Process:
trap mode bit = 0
Kernel:
return mode bit = 1
User Process:
Remember — User space and kernel space are separate and protected, but a single user process can operate in both.
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:
- Privileged: The call switches the CPU to kernel mode via the mode bit, and
- Well-Defined Location: The function isn’t specified by an address, but indirectly via an index into a branch vector.
- This prevents a call from branching to arbitrary locations within the kernel.
Remember: Changing the mode bit of the CPU from user \to kernel mode can only be done by a privileged instruction.
- Going the other way is not restricted.
System calls can be:
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:
Branch Vector: A data structure that contains a list of function addresses pointing to specific kernel routines.
sys_read(), index 1 to sys_write()Analogy: Restaurant Menu
- The system call can only order item #1, #2, #2, etc. off the menu, and cannot reach into the kitchen and give dangerous orders like “give me whatever is in pot #7”.
Three methods to pass parameters to system calls:
Why? — Storing in memory (block or stack) allows us to avoid limits like having limited registers.
Six major categories of system calls:
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.
Two Most-Common Uses of Interrupts:
Examples of Traps:
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:
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: Kernel function invoked whenever an interrupt occurs.