DOC PREVIEW
Duke CPS 110 - Traps and Faults

This preview shows page 1-2-3 out of 9 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 9 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Traps and FaultsTraps and FaultsReview: Mode and SpaceReview: Mode and Spacedata datausermodekernelmodeABC“kernelspace”2Review: the Role of EventsReview: the Role of EventsACPUevent is an “unnatural” change in control flow.Like a procedure call, an event changes the PC.Also changes mode or context (current stack), or both.Events do not change the current space!The kernel defines a handler routine for each event type.Event handlers always execute in kernel mode.The specific types of events are defined by the machine.Once the system is booted, every entry to the kernel occurs as aresult of an event.In some sense, the whole kernel is a big event handler.Categorizing EventsCategorizing EventsAn 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 deliberatesyncfault syscall trapasyncinterruptASTcontrol flowevent handler (e.g.,ISR: Interrupt ServiceRoutine)exception.ccAST: Asynchronous System TrapAlso called a software interrupt or anAsynchronous 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.3System CallSystem CallTrapsTrapsUser code invokes kernel services by initiating system call traps.• Programs in C, C++, etc. invoke system calls by linking to astandard library of procedures written in assembly language.the library defines a stub or wrapper routine for each syscallstub executes a special trap instruction (e.g., chmk or callsys)syscall arguments/results passed in registers or user stackread() in Unix libc.a library (executes in user mode):#define SYSCALL_READ 27 # code for a read system callmove arg0…argn, a0…an # syscall args in registers A0..ANmove SYSCALL_READ, v0 # syscall dispatch code in V0callsys # kernel trapmove r1, _errno # errno = return statusreturnAlpha CPU architecture“Bullet-Proofing” the Kernel“Bullet-Proofing” the KernelSystem calls must be “safe” to protect the kernel from buggyor malicious user programs.1. System calls enter the kernel at a well-known safe point.Enter at the kernel trap handler; control transfers to the “middle”of the kernel are not permitted.2. The kernel validates all system call arguments before use.Kernel may reject a request if it is meaningless or if the userprocess has inadequate privilege for the requested operation.3. All memory used by the system call handler is in kernelspace, so it is protected from interference by user code.What stack does the system call execute on?4Kernel Stacks and System Call HandlingKernel Stacks and System Call HandlingdataProcessesexecute usercode on a userstack in the userportion of theprocess virtualaddress space.Each process has asecond kernel stackin kernel space (thekernel portion of theaddress space).stackstackstackstackSystem callsruninkernelmode on theprocess kernelstack.syscalldispatchtableSystem calls runin the processspace, so copyinand copyout canaccess usermemory.The syscall trap handler makes an indirect call through the systemcall dispatch table to the handler for the specific system call.Example: Mechanics ofExample: Mechanics ofan Alphaan AlphaSyscall TrapSyscall Trap1. Machine saves return address and switches to kernel stack.save user SP, global pointer(GP), PC on kernel stackset kernel mode andtransfertoasyscalltraphandler(entSys)2. Trap handler saves software state, and dispatches.save some/all registers/arguments on process kernel stackvector to syscall routine through sysent[v0: dispatchcode]3. Trap handler returns to user mode.when syscall routine returns, restore user register stateexecute privileged return-from-syscall instruction (retsys)machine restores SP, GP, PC and sets user modeemerges at user instruction following the callsys5Safe Handling of Syscall Args/ResultsSafe Handling of Syscall Args/Results1. Decode and validate by-value arguments.Process (stub) leaves arguments in registers or on the stack.2. Validate by-reference (pointer) IN arguments.Validate user pointers and copy data into kernel memory with aspecial safe copy routine, e.g., copyin().3. Validate by-reference (pointer) OUT arguments.Copy OUT results into user memory with special safe copyroutine, e.g., copyout().4. Set up registers with return value(s); return to user space.Stub may check to see if syscall failed, possibly raising a userprogram exception or storing the result in a variable.Questions About System Call HandlingQuestions About System Call Handling1. Why do we need special copyin and copyout routines?validate user addresses before using them2. What would happen if the kernel did not save all registers?3. Where should per-process kernel global variables reside?syscall arguments (consider size) and error code4. What if the kernel executes a callsys instruction? What ifuser code executes a retsys instruction?5. How to pass references to kernel objects as arguments orresults to/from system calls?pointers? No: use integer object handles or descriptors (alsosometimes called capabilities).6KernelKernelObject HandlesObject HandlesInstances of kernel abstractions may be viewed as “objects”named by protected handles held by processes.• Handles are obtained by create/open calls, subject tosecurity policies that grant specific rights for each handle.• Any process with a handle for an object may operate on theobject using operations (system calls).Specific operations are defined by the object’s type.• The handle is an integer index to a kernel table.portfileetc.objecthandlesuser space kernelMicrosoft NT object handlesUnix file descriptorsNachos FileID and SpaceIDFaultsFaultsFaults are similar to system calls in some respects:• Faults occur as a result of a process executing an instruction.Fault handlers execute on the process kernel stack; the fault handlermay block (sleep) in the kernel.• The completed fault handler may return to the faulted context.But faults are different from syscall traps in other respects:• Syscalls are deliberate, but faults are “accidents”.divide-by-zero, dereference invalid pointer, memory page fault• Not every execution of the faulting instruction results in a fault.may depend on memory state or register contents7Options for Handling a Fault (1)Options for Handling a Fault (1)1. Some faults are handled by “patching things up” and returningto the faulted


View Full Document

Duke CPS 110 - Traps and Faults

Download Traps and Faults
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 Traps and Faults 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 Traps and Faults 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?