Duke CPS 210 - Processes and the Kernel

Unformatted text preview:

Processes, Protection and the Kernel:Processes, Protection and the Kernel:Mode, Space, and ContextMode, Space, and ContextProcesses and the KernelProcesses and the Kerneldata dataprocesses in private virtual address spacessystem call traps...and upcalls (e.g., signals)shared kernel code and data in shared address spaceThreads or processes enter the kernel for services.The kernel sets up process execution contexts to “virtualize” the machine.CPU and devices force entry to the kernel to handle exceptional events.ObjectivesObjectives• The nature of the classical kernel, its protection mechanisms, and architectural support for protected kernels.Mode, space, and context.• Control transfer from user code into the kernel.System calls (traps) and user program events (faults).Access control: handles, IDs, and Access Control Lists.• Control transfer from the kernel to user code.Signals, APCs, syscall return.• Kernel synchronization.• Process structure and process birth/death, process states.Fork/exec/exit/join/wait and process trees.The KernelThe Kernel• Today, all “real” operating systems have protected kernels.The kernel resides in a well-known file: the “machine” automatically loads it into memory (boots) on power-on/reset. Our “kernel” is called the executive in some systems (e.g., MS). • The kernel is (mostly) a library of service procedures shared by all user programs, but the kernel is protected:User code cannot access internal kernel data structures directly, and it can invoke the the kernel only at well-defined entry points (system calls).• Kernel code is like user code, but the kernel is privileged:The kernel has direct access to all hardware functions, and defines the entry points of handlers for interrupts and exceptions (traps and faults).Kernel ModeKernel Mode02ncode libraryOS dataOS codeProgram AdataDataProgram BDataregistersCPUR0RnPCmain memoryxxmodeCPU mode (a field in some status register) indicates whether the CPU is running in a userprogram or in the protected kernel.Some instructions or data accesses are only legal when the CPU is executing in kernel mode.physical address spaceThread/Process States and TransitionsThread/Process States and Transitionsrunning(user)running(kernel)readyblockedRunWakeupinterrupt,exceptionSleepYieldinterrupt, exception,returnCPU Events: Interrupts and ExceptionsCPU Events: Interrupts and ExceptionsAn interrupt is caused by an external event.device requests attention, timer expires, etc.An exception is caused by an executing instruction.CPU requires software intervention to handle a fault or trap.unplanned deliberatesync fault syscall trapasync interrupt ASTcontrol flowevent handler (e.g., ISR: Interrupt Service Routine)exception.ccAST: Asynchronous System TrapAlso called a software interrupt or an Asynchronous or Deferred Procedure Call (APC or DPC)Note: different “cultures” may use some of these terms (e.g., trap, fault, exception, event, interrupt) slightly differently.Protecting Entry to the KernelProtecting Entry to the KernelProtected events and kernel mode are the architectural foundations of kernel-based OS (Unix, NT+, etc).• The machine defines a small set of exceptional event types.• The machine defines what conditions raise each event.• The kernel installs handlers for each event at boot time.e.g., a table in kernel memory read by the machineThe machine transitions to kernel mode only on an exceptional event.The kernel defines the event handlers.Therefore the kernel chooses what code will execute in kernel mode, and when.userkernelevent/returnevent/returnHandling Events, Part I: The Big PictureHandling Events, Part I: The Big Picture1. To deliver the event, the machine saves relevant state in temporary storage, then transfers control to the kernel.Set kernel mode and set PC := handler.2. Kernel handler examines registers and saved machine state.What happened? What was the machine doing when it happened? How should the kernel respond?3. Kernel responds to the condition.Execute kernel service, device control code, fault handlers, etc., modify machine state as needed.4. Kernel restores saved context (registers) and resumes activity.5. Specific events and mechanisms for saving, examining, or restoring context are machine-dependent.The Role of EventsThe Role of EventsOnce the system is booted, every entry to the kernel occurs as a result of an event.• In some sense, the whole kernel is a big event handler.• Event handlers are kernel-defined and execute in kernel mode.• Events do not change the identity of the executing thread/process.Context: thread/process context, or interrupt context.Loosely, whose stack are you running on.For purposes of this discussion, suppose one thread per process.• Events do not change the current space!The Virtual Address SpaceThe Virtual Address SpaceA typical process VAS space includes:• user regions in the lower halfV->P mappings specific to each processaccessible to user or kernel code• kernel regions in upper halfshared by all processesaccessible only to kernel code• Nachos: process virtual address space includes only user portions.textdataBSSuser stackargs/env0datakernel textandkernel data2n-12n-10x00xffffffffA VAS for a private address space system (e.g., Unix) executing on a typical 32-bit architecture.sbrk()jsrExample: Process and Kernel Address SpacesExample: Process and Kernel Address Spacesdata02n-1-12n-12n-1data0x7FFFFFFF0x800000000xFFFFFFFF0x0n-bit virtual address space32-bit virtual address spaceIntroduction to Virtual AddressingIntroduction to Virtual AddressingtextdataBSSuser stackargs/envkerneldatavirtualmemory(big)physicalmemory(small)virtual-to-physical translationsUser processes address memory through virtual addresses.The kernel and the machine collude to translate virtual addresses to physical addresses.The kernel controls the virtual-physical translations in effect for each space.The machine does not allow a user process to access memory unless the kernel “says it’s OK”.The specific mechanisms for implementing virtual address translation are machine-dependent: we will cover them later.System Call TrapsSystem Call TrapsUser code invokes kernel services by initiating system call traps.• Programs in C, C++, etc. invoke system calls by linking to a standard library of procedures written in assembly language.The library defines a stub or wrapper routine for each syscall.Stub executes a special trap instruction (e.g., chmk


View Full Document

Duke CPS 210 - Processes and the Kernel

Download Processes and the Kernel
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Processes and the Kernel and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Processes and the Kernel 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?