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
          accessible.
        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.




                                                                     1
Exceptions

   What happens when a user process does something
    illegal?
      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.




                                                           2
Process Address Spaces

    0xFFFFFFFF

                            Shared Kernel Memory
 0xC0000000
0xBFFFFFFF


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


    0x00000000




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




                                                                                 3
Kernel Exception Handler

   When the OS starts, it fills in the Exception Vector
    Table.
      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




                                                                4
   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
Exception                          handler called
occurs




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




                                                                           5
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
       identified.
     Linux: INT 0x80 is a system call.
     Windows: INT 0x2E is a system call.




                                                                     6
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
                                          implementation
      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.




                                                              7
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
                                              Return




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




                                                                 8
System Calls For Process Management




System Calls For File Management




                                      9
System Calls For Directory Management




System Calls For Miscellaneous Tasks




                                        10
Unix vs Win32 System calls




Summary

   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.




                                                          11

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:9
posted:2/6/2011
language:English
pages:11