Docstoc

proc_mgmt

Document Sample
proc_mgmt Powered By Docstoc
					     CS454 Lecture Notes



      Process Management In Xinu




    Process Management (Ch 4)
q   Refer to notes for the x86 register definitions.
q   Context switch: Method of saving the state (context)
    of the machine during execution of one process
    (registers), and restoring the machine state of
    another process and continue execution.
q   Context switches can occur in the following
    situations:
    – Current process time quantum expires
    – Waiting for I/O request
    – Semaphore wait
      Context Switching During
       Subroutine Execution
q   Caller:
     – Push parameters on stack. (1st parameter is on TOP of the stack)
     – Call subroutine (JSR)
q   Callee:
     – Create a current frame ptr (push BP, which points to the previous
       record).
     – Reserve space for locals & save registers used for locals.
     – Execute: Parameters and locals are accessed from stack using BP
       as the base pointer.
     – Release space for locals & restore registers used for locals.
     – Restore the previous frame pointer (pop BP).
     – Return from subroutine (RTS)
q   Caller:
     – Remove the parameters from the stack.




     General C Stack Diagram
q   C Stack:
                                        SP
      Local V ariables
      Fram pointer to previous record
           e                            BP
      Return address
      Call Param  eters
      Local V ariables
           e
      Fram pointer to previous record
      Return address
      Call Param  eters
       C Conventions: Function
             Invocation
q   1. Type conversion takes place when arguments are pass to functions
     – char, short --> int (long) (4 bytes)
     – float --> double (8 bytes)
q   2. Return value
     – char, short, int, float: returned through d0 as 4 bytes
     – double: returned through d0, d1 as 8 bytes
q   3. Before any procedure (function) call:
     – Push parameters in the reverse order on the stack (caller’s
        responsibility)
q   4. Jump to subroutine instruction
     – return address is pushed on the stack
     – control goes to the subroutine




       C Conventions: Function
          Invocation cont.
q   5. When any procedure (function) is compiled:
     – the first instruction is always “link, a6, #-d”
     – d is the number of bytes for local variables
     – link is a powerful instruction, its effect is as follows:
          q push a6

          q sp -> a6

          q sp - d -> sp

q   6. The register a6 is used as the Frame Pointer (FP) for the activation
    stack record (frame) for the procedure call. The local variables and
    parameters are accessed relative to a6. The activation stack record is
    “released” when the procedure returns.
           C Conventions: Function Invocation cont.
q   7. In the compiled code for a procedure, the instruction before the
    return instruction (rts or rte) is always “unlk a6”
     – The effect of the “unlk” instruction is as follows:
           q a6 -> sp (release local variable area)

           q pop a6 (recover old FP: pop the contents and store in a6)

     – As a result of pop: sp points to the location where the return
         address is saved. The register a6 points to the activation stack
         record before the call.
q   8. Return instruction
     – To return from a call, the called routine executes an rts (return from
         subroutine) or an rte (return from exception).
     – rts expects the top of the stack to contain the return address:
           q rts:

                – load PC from top of stack and increment SP (pop stack)
     – rte expects the top of the stack to contain the new value of SR
         (process status word) and below that in the stack there should be a
         return address
           q rte:

                – load SR from top of stack and then pop stack
                – load PC from top of stack and pop stack




        C Conventions: Function
           Invocation cont.
q   9. The jump to subroutine instruction is always followed by an
    instruction “addq #e, sp”. The purpose of the instruction is to release
    the parameter area. Note here the convention is that the caller removes
    the parameters from the stack once the call is returned.
q   10. As a result of the conventions for a procedure call the first argument
    (for the procedure) is always supposed to be located at +8 relative to
    FP (also known as a6).
          Diagrams of Subroutine
             Context Switches
q   Caller before JSR
              rg
           A (0)
           ...
              rg
           A (n-1)
     a6
            ocal ariables
           L V




                    Diagram cont.
q   Caller: JSR
           Return Address
           Arg (0)
           ...
           Arg (n-1)
     a6    Local Variables
                 Diagram cont.
                            sp
                                      Local variables
q   Callee:                 a6
                                      Old Frame Address
                                      Return Address
    –   Link a6, #-d                  Arg (0)
    –   Push a6                       ...
                                      Arg (n-1)
    –   SP -> a6                      Local Variables
                                      Old Frame Address
    –   SP - d -> SP




                 Diagram cont.
                                 Released local variables
                                 Released a6
q   Callee:            sp
                                 Return Address
    – Unlk                          rg
                                 A (0)
                                 ...
    – a6 -> SP                      rg
                                 A (n-1)
                                 Local Variables
    – pop a6           a6        Old Frame Address
                 Diagram cont.
q   Callee:
                   sp
    – RTS               Arg (0)
                        ...
    – pop (PC)          Arg (n-1)
                        Local Variables
                   a6   Old Frame Address




                 Diagram cont.
q   Caller:
    – Releases the arguments
                                      Released arguments

                             sp
                                      Local Variables
                             a6
                                               e ddress
                                      Old Fram A

				
DOCUMENT INFO