7. Exceptions and System Calls

Document Sample
7. Exceptions and System Calls Powered By Docstoc
					                     7. Exceptions and
                     System Calls

                                                    Mark Handley

CPU modes
Recall the mode bit in the CPU PSW register:
        user-mode: only a subset of instructions and features are
        kernel-mode: all instructions and features are accessible.

How does a user process ask the OS to perform a function for it?
   It is running in user-mode, so cannot directly access kernel
    memory or the hardware.
   If it could just directly switch to kernel-mode, there would
    be no security.


   What happens when a user process does something
      Divide-by-zero.
      Runs an illegal instruction.

   How does the Java Virtual Machine manage to throw
    an floating exception, rather than simply crash?

Example: Illegal Instruction

   The “halt” instruction is illegal in user-mode.
     (in kernel mode, it halts the system).

   If a user-mode process calls halt, this generates a
    trap in the CPU, which suspends the user process,
    switches into kernel-mode, and calls a corresponding
    exception handler.
      What happens next depends on the OS.

Process Address Spaces


                            Shared Kernel Memory

 Memory            Process 1       Process 2     Process 3
Addresses          User Area       User Area     User Area


Kernel Stack and Kernel Memory

              User Mode                            Kernel Mode

                   SP                   SP                    USP
    kernel stack          user stack           kernel stack         user stack

    Kernel memory is only accessible in kernel mode.
    User memory is mapped by the MMU, but kernel memory is
     generally shared (unmapped) by all processes.
    There is a separate kernel stack

Kernel Exception Handler

   When the OS starts, it fills in the Exception Vector
      This contains pointers to code to run when a
       particular exception occurs.
      Interrupts are handled through the same table.

CPU Exception Handling
                       Exception Vectors   Exception Handlers
       Save PC
       and PSW                             code to handle
                                             exception A

 Load PC from
Exception Vector
                                           code to handle
                                             exception B
     CPU switches
    to kernel-mode

                                           code to handle
    Use PC to fetch                          exception C
    next instruction

   Handling a Trap
      Trap occurs. CPU switches to kernel mode.
      CPU saves information about the current running program
       (PSW, PC, etc) on the kernel stack.
      CPU calls OS exception handler for this exception from the
       exception vector table.
      OS handles exception.
         Unix: look up exception handler in signal table.
         Win32: call a dispatcher with relevant interrupt object.
         Call relevant signal handler code.
            In some cases, terminates the process.
            In some cases, returns control to user process.
      If not fatal, calls “return from exception”
         CPU restores PSW and PC. Switches back to user-mode.

   User Exception Handling
Execution by    User Program                        OS Exception Handler
user process

Exception                          handler called

handler can                     Control passed to
pass control                    user program’s
back to where                   exception handler
the exception

System Calls
   A system call is the mechanism by which a user process calls a
    kernel procedure.
      Used to do I/O, read/write files, etc.

   Basic mechanism is very similar to exception handling:
      Generate a trap.
      Kernel exception handler called.
      OS identifies which system call is required, and calls
       relevant procedure.
      Returns execution to user process on completion.

System Calls:
Generating a Trap
   TRAP instruction (68000)
      4-bit opcode: TRAP #0 to TRAP #16

   INT (interrupt) instruction (Pentium, etc)
     8-bit opcode allows 256 different exceptions to be
     Linux: INT 0x80 is a system call.
     Windows: INT 0x2E is a system call.

Processing a System Call

      User program                           Kernel
                                        Validate Parameters
       libc API call                       Find and call
                                         correct procedure
                                           Return result

      libc API library                   System call table
    Set up parameters
       System call                          System call
      Return result

Example System Call

Call from C program:
   count = read(fd, &buffer, length);
    read up to length bytes of data from from file
      descriptor fd into buffer, and returns the number of
      bytes actually read, or -1 if an error occurred.

C library call for read() calls the read system call.

Steps in Making a System Call
read (fd, &buffer, nbytes)

         User program                       Kernel
         Push nbytes;                       Save state
         Push &buffer;
              Push fd                  Validate syscall code
        Call read function           Call call_table_base+code
         Increment SP
                                          Return result

        libc API library                 System call table
        Put code for read
        syscall in register;         Validate read parameters
                                          from user stack
     Trap to kernel (int 0x80)
        Return to caller                 Do read function

System Call sanity checking

   Kernel does not trust user processes.
   Needs to validate any parameters from system call
    very carefully.
      Eg: buffers for I/O must be in user memory.
      Pentium has special instuctions:
        VERR - verify read instruction
        VERW - verify write instruction

System Calls For Process Management

System Calls For File Management

System Calls For Directory Management

System Calls For Miscellaneous Tasks

Unix vs Win32 System calls


   System calls are how a user process gains access to
    functions provided by the OS.
   Implemented via trap to kernel.
   Much in common with exception/interrupt handling.
   Different systems have different system calls.


Shared By: