home *** CD-ROM | disk | FTP | other *** search
/ ftp.ee.pdx.edu / 2014.02.ftp.ee.pdx.edu.tar / ftp.ee.pdx.edu / pub / users / Harry / Blitz / version-1-0 / OSProject / p6 / Switch.s < prev    next >
Text File  |  2006-05-09  |  7KB  |  228 lines

  1. ! BLITZ OS - Switch.s
  2. !
  3. ! Harry Porter  -  12/07/03
  4. !
  5. ! The following functions are implemented in this file:
  6. !
  7.     .export    Switch
  8.     .export    ThreadStartUp
  9.     .export    GetOldUserPCFromSystemStack
  10.     .export    LoadPageTableRegs
  11.     .export SaveUserRegs
  12.     .export RestoreUserRegs
  13.     .export BecomeUserThread
  14. !
  15. ! The following functions are imported:
  16. !
  17.     .import    _P_Kernel_ThreadStart
  18.     .import    _P_Kernel_ThreadFinish
  19. !
  20.     .text
  21.  
  22.  
  23. ! ===============  ThreadStartUp  ===============
  24. ! external ThreadStartUp ()
  25. !
  26. ! Upon entry, r2 points to the main function for the new thread and r3
  27. ! contains an integer argument.  This routine will call ThreadStart.
  28. ! When that returns, it will call the main function, passing the argument.
  29. ! When and if that returns, this routine will call ThreadFinish.
  30. ! ThreadFinish will not return.
  31. ThreadStartUp:
  32.         mov    r0,r14            ! Clear the FP register
  33.         mov    r0,r12            ! Clear the Catch Stack pointer
  34.         push    r3            ! Push argument to "fun"
  35.         push    r2            ! Push ptr to "fun"
  36.         call    _P_Kernel_ThreadStart    ! Call ThreadStart
  37.         pop    r1            ! Pop ptr to "fun"
  38.         call    r1            ! .  and call it
  39.         pop    r1            ! Pop argument
  40.         call    _P_Kernel_ThreadFinish    ! Call ThreadFinish
  41. ThreadHang:    debug                ! Should never reach this point
  42.         jmp    ThreadHang        ! .
  43.  
  44.  
  45.  
  46. ! ===============  Switch  ===============
  47. ! external Switch (prevThread, nextThread: ptr to Thread)
  48. !
  49. ! This routine is passed ptrs to 2 Thread objects.  Each Thread object
  50. ! contains a place to store the state of the machine.  This includes:
  51. !    r2-r14
  52. !    stackTop (r15)
  53. ! This routine switches the state from one thread to the next and returns.
  54. ! Of course it will be returning to a different invocation, since the thread
  55. ! will have been switched.
  56. !
  57. ! This routine trashes r1.
  58. Switch:
  59.         load    [r15+4],r1        ! Move the prevThread into r1
  60.         add    r1,16,r1        ! Make r1 point to r1.regs
  61.         store    r2,[r1+0]        ! Save r2..r14 in r1.regs
  62.         store    r3,[r1+4]        ! .
  63.         store    r4,[r1+8]        ! .
  64.         store    r5,[r1+12]        ! .
  65.         store    r6,[r1+16]        ! .
  66.         store    r7,[r1+20]        ! .
  67.         store    r8,[r1+24]        ! .
  68.         store    r9,[r1+28]        ! .
  69.         store    r10,[r1+32]        ! .
  70.         store    r11,[r1+36]        ! .
  71.         store    r12,[r1+40]        ! .
  72.         store    r13,[r1+44]        ! .
  73.         store    r14,[r1+48]        ! .
  74.         store    r15,[r1+52]        ! Save r15 in r1.stackTop
  75.  
  76.         load    [r15+8],r1        ! Move the nextThread into r1
  77.         add    r1,16,r1        ! Make r1 point to r1.regs
  78.         load    [r1+0],r2        ! Restore r2..r14 from r1.regs
  79.         load    [r1+4],r3        ! .
  80.         load    [r1+8],r4        ! .
  81.         load    [r1+12],r5        ! .
  82.         load    [r1+16],r6        ! .
  83.         load    [r1+20],r7        ! .
  84.         load    [r1+24],r8        ! .
  85.         load    [r1+28],r9        ! .
  86.         load    [r1+32],r10        ! .
  87.         load    [r1+36],r11        ! .
  88.         load    [r1+40],r12        ! .
  89.         load    [r1+44],r13        ! .
  90.         load    [r1+48],r14        ! .
  91.         load    [r1+52],r15        ! Restore r15 from r1.stackTop
  92.         ret
  93.  
  94.  
  95.  
  96. ! ===============  GetOldUserPCFromSystemStack  ===============
  97. ! external GetOldUserPCFromSystemStack () returns int
  98. !
  99. ! This routine is called by the kernel after a syscall has occurred.  It expects
  100. ! the assembly SyscallTrapHandler to have called the high-level SyscallTrapHandler,
  101. ! which then called Handle_Sys_Fork.  It expects to be called from Handle_Sys_Fork,
  102. ! and will not work properly otherwise.
  103. !
  104. ! This routine looks down into stuff buried in the system stack and finds the
  105. ! exception block that was pushed onto the stack at the time of the syscall.
  106. ! From that, it retrieves the user-mode PC, which points to the instruction
  107. ! the kernel must return to after the syscall.
  108. GetOldUserPCFromSystemStack:
  109.         load    [r14],r1        ! r1 = ptr to frame of SyscallTrapHandler
  110.         load    [r1+28],r1        ! r1 = pc from interrupt block
  111.         store    r1,[r15+4]
  112.         ret
  113.  
  114.  
  115.  
  116. ! =====================  LoadPageTableRegs  =====================
  117. ! external LoadPageTableRegs (ptbr, ptlr: int)
  118. !
  119. ! This routine will load the Page Table Base Register (PTBR) and the
  120. ! Page Table Length Register (PTLR).
  121. LoadPageTableRegs:
  122.         load    [r15+4],r1    ! Get arg1
  123.         ldptbr    r1        ! Move it to PTBR
  124.         load    [r15+8],r1    ! Get arg2
  125.         ldptlr    r1        ! Move it to PTLR
  126.         ret            ! return
  127.  
  128.  
  129.  
  130. ! =====================  SaveUserRegs  =====================
  131. ! external SaveUserRegs (ptr to int)
  132. !
  133. ! This routine is passed a pointer to a block of memory.  It is called
  134. ! while in System Mode.  It copies the user registers to the block of memory.
  135. !
  136. SaveUserRegs:
  137.         load    [r15+4],r1        ! Move ptr to memory into r1
  138.         readu    r1,[r1+0]        ! Copy user registers to memory
  139.         readu    r2,[r1+4]        ! .
  140.         readu    r3,[r1+8]        ! .
  141.         readu    r4,[r1+12]        ! .
  142.         readu    r5,[r1+16]        ! .
  143.         readu    r6,[r1+20]        ! .
  144.         readu    r7,[r1+24]        ! .
  145.         readu    r8,[r1+28]        ! .
  146.         readu    r9,[r1+32]        ! .
  147.         readu    r10,[r1+36]        ! .
  148.         readu    r11,[r1+40]        ! .
  149.         readu    r12,[r1+44]        ! .
  150.         readu    r13,[r1+48]        ! .
  151.         readu    r14,[r1+52]        ! .
  152.         readu    r15,[r1+56]        ! .
  153.         ret                ! Return
  154.  
  155.  
  156.  
  157. ! =====================  RestoreUserRegs  =====================
  158. ! external RestoreUserRegs (ptr to int)
  159. !
  160. ! This routine is passed a pointer to a block of memory.  It is called
  161. ! while in System Mode.  It copies data from memory into the user registers.
  162. !
  163. RestoreUserRegs:
  164.         load    [r15+4],r1        ! Move ptr to memory into r1
  165.         writeu    [r1+0],r1        ! Copy data to user registers
  166.         writeu    [r1+4],r2        ! .
  167.         writeu    [r1+8],r3        ! .
  168.         writeu    [r1+12],r4        ! .
  169.         writeu    [r1+16],r5        ! .
  170.         writeu    [r1+20],r6        ! .
  171.         writeu    [r1+24],r7        ! .
  172.         writeu    [r1+28],r8        ! .
  173.         writeu    [r1+32],r9        ! .
  174.         writeu    [r1+36],r10        ! .
  175.         writeu    [r1+40],r11        ! .
  176.         writeu    [r1+44],r12        ! .
  177.         writeu    [r1+48],r13        ! .
  178.         writeu    [r1+52],r14        ! .
  179.         writeu    [r1+56],r15        ! .
  180.         ret                ! Return
  181.  
  182.  
  183.  
  184. ! =====================  BecomeUserThread  =====================
  185. ! external BecomeUserThread (initStack, initPC, initSystemStack: int)
  186. !
  187. ! This routine changes the mode from system-mode to user-mode.  It
  188. ! also enables interrupts and paging.  It is passed the initial value
  189. ! for the user stack and the initial address to branch to.  The user
  190. ! thread will never "return" (it must execute something like
  191. ! Syscall_Exit).  However, the user thread will execute syscalls from
  192. ! time to time.  Thus, the system stack as it exists now will be pushed
  193. ! onto when ever this thread does a syscall.
  194. !
  195. ! Note that 0x00000020 is a StatusRegister (interrupts, user mode, paging off).
  196. ! Note that 0x00000028 is a StatusRegister (interrupts, user mode, paging on).
  197. !
  198. BecomeUserThread:
  199.         load    [r15+4],r3        ! r3 := initStack
  200.         writeu    r3,r15            ! Store stack-ptr in user reg r15
  201.         load    [r15+8],r2        ! Move initPC into r2
  202.         load    [r15+12],r15        ! Move initSystemStack into r15
  203.         push    r0            ! Push a fake interrupt block
  204.         mov    0x00000028,r4        ! .  onto the system stack
  205.         push    r4            ! .
  206.         push    r2            ! .
  207.         clr    r1            ! Set user reg r1 to zero
  208.         writeu    r1,r1            ! .
  209.         reti                ! "return" from interrupt
  210.