Processes and the Kernel Processes and the Kernel The kernel sets processes up process in private data data execution Processes, Protection and the Kernel: Processes, Protection and the Kernel: virtual contexts to address “virtualize” the Mode, Space, and Context Mode, Space, and Context spaces machine. ...and upcalls (e.g., system call traps signals) Threads or shared kernel processes code and data enter the in shared kernel for address space services. CPU and devices force entry to the kernel to handle exceptional events. Objectives Objectives The Kernel The Kernel • Today, all “real” operating systems have protected kernels. • The nature of the classical kernel, its protection mechanisms, The kernel resides in a well-known file: the “machine” and architectural support for protected kernels. automatically loads it into memory ( boots ) on power-on/reset. Mode, space, and context. Our “kernel” is called the executive in some systems (e.g., MS). • Control transfer from user code into the kernel. • The kernel is (mostly) a library of service procedures shared System calls (traps) and user program events (faults). by all user programs, but the kernel is protected : Access control: handles, IDs, and Access Control Lists. User code cannot access internal kernel data structures directly, and it can invoke the the kernel only at well-defined entry • Control transfer from the kernel to user code. points ( system calls ). Signals, APCs, syscallreturn. • Kernel code is like user code, but the kernel is privileged : • Kernel synchronization. The kernel has direct access to all hardware functions, and • Process structure and process birth/death, process states. defines the entry points of handlers for interrupts and exceptions (traps and faults) . Fork/exec/exit/join/wait and process trees. Kernel Mode Kernel Mode Thread/Process States and Transitions Thread/Process States and Transitions 0 running CPU OS code CPU mode (a field (user) in some status OS data register) indicates interrupt, exception, whether the CPU is Program A return running in a user physical program or in the data Data interrupt, mode address exception protected kernel . Yield R0 space running x Program B Some instructions or (kernel) Sleep Run Rn data accesses are Data only legal when the x PC CPU is executing in blocked ready registers Wakeup kernel mode. code library 2 n main memory 1
CPU Events: Interrupts and Exceptions CPU Events: Interrupts and Exceptions Protecting Entry to the Kernel Protecting Entry to the Kernel An interrupt is caused by an external event. Protected events and kernel mode are the architectural foundations of kernel-based OS (Unix, NT+, etc). device requests attention, timer expires, etc. • The machine defines a small set of exceptional event types. An exception is caused by an executing instruction. • The machine defines what conditions raise each event. CPU requires software intervention to handle a fault or trap . • The kernel installs handlers for each event at boot time. control flow unplanned deliberate exception.cc e.g., a table in kernel memory read by the machine fault syscall trap sync interrupt async AST The machine transitions to kernel mode user AST: A synchronous S ystem T rap only on an exceptional event. Also called a software interrupt or an event handler (e.g., Asynchronous or Deferred Procedure Call The kernel defines the event handlers. event/return event/return ISR : I nterrupt S ervice (APC or DPC) R outine) Therefore the kernel chooses what code Note : different “cultures” may use some of these terms (e.g., will execute in kernel mode, and when. kernel trap, fault, exception, event, interrupt) slightly differently . Handling Events, Part I: The Big Picture Handling Events, Part I: The Big Picture The Role of Events The Role of Events 1. To deliver the event, the machine saves relevant state in Once the system is booted, every entry to the kernel occurs as a temporary storage, then transfers control to the kernel. result of an event. Set kernel mode and set PC := handler . • In some sense, the whole kernel is a big event handler. 2. Kernel handler examines registers and saved machine state. • Event handlers are kernel-defined and execute in kernel mode. What happened? What was the machine doing when it happened? How should the kernel respond? • Events do not change the identity of the executing thread/process. 3. Kernel responds to the condition. Context: thread/process context, or interrupt context. Execute kernel service, device control code, fault handlers, etc ., modify machine state as needed. Loosely, whose stack are you running on. 4. Kernel restores saved context (registers) and resumes activity. For purposes of this discussion, suppose one thread per process. 5. Specific events and mechanisms for saving, examining, or • Events do not change the current space! restoring context are machine-dependent . The Virtual Address Space The Virtual Address Space Example: Process and Kernel Address Spaces Example: Process and Kernel Address Spaces 0 0x0 text A typical process VAS space includes: data data 0 0x0 • user regions in the lower half BSS sbrk () n-bit virtual 32-bit virtual V->P mappings specific to each process data data jsr address user stack address accessible to user or kernel code space args/env space 2 n-1 • kernel regions in upper half 0x7FFFFFFF 2 n - 1 -1 shared by all processes kernel text accessible only to kernel code and kernel data • Nachos: process virtual address space 2 n - 1 0x80000000 includes only user portions. 2 n -1 0xffffffff 2 n -1 0xFFFFFFFF A VAS for a private address space system (e.g., Unix) executing on a typical 32-bit architecture. 2
Recommend
More recommend