home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / switch_patch.asm < prev    next >
Encoding:
Assembly Source File  |  1995-12-16  |  9.5 KB  |  264 lines

  1.                     INCLUDE   "exec/funcdef.i"
  2.                     INCLUDE   "exec/exec_lib.i"
  3.                     INCLUDE   "exec/nodes.i"
  4.                     INCLUDE   "exec/lists.i"
  5.                     INCLUDE   "exec/ables.i"
  6.                     INCLUDE   "exec/tasks.i"
  7.                     INCLUDE   "exec/ports.i"
  8.                     INCLUDE   "exec/memory.i"
  9.                     INCLUDE   "shared_defs.i"
  10.                     INCLUDE   "macros.i"
  11.  
  12. * $Id: switch_patch.asm,v 3.5 95/12/16 18:36:35 Martin_Apel Exp $
  13.  
  14.                     XDEF      _SwitchPatch
  15.                     XDEF      _AddTaskPatch
  16.                     XDEF      _WaitPatch
  17.                     IFD       DEBUG
  18.                     XDEF      _RemTaskPatch
  19.                     XREF      _OrigRemTask
  20.                     XDEF      _StackSwapPatch
  21.                     XREF      _OrigStackSwap
  22.                     ENDC
  23.  
  24.                     XREF      _OrigSwitch
  25.                     XREF      _OrigAddTask
  26.                     XREF      _OrigWait
  27.                     XREF      _Free
  28.                     XREF      _VirtAddrStart
  29.                     XREF      _VirtAddrEnd
  30.                     XREF      _ReportError
  31.  
  32.                     IFD       _PHXASS_
  33.                     MACHINE   MC68030
  34.                     ENDC
  35.  
  36.                     SECTION   CODE
  37.  
  38. * This routine is called in Supervisor mode just before the
  39. * registers of a task are saved onto the user stack by the
  40. * EXEC routine Switch.
  41. * It checks if the stackpointer lies within virtual ram.
  42. * Only then is the USP replaced by a pointer
  43. * to non-virtual ram. This is undone, when the task is next launched
  44.  
  45.  
  46. _SwitchPatch        move.l    a0,-(sp)
  47.                     move.l    usp,a0
  48.                     IN_VM     a0,NoVirtMemSwitch
  49.  
  50. *                    PRINT_DEB "Switch: SP = %lx",a0
  51.  
  52.                     movem.l   d0/a1/a6,-(sp)
  53.                     lea       _Free,a0
  54.                     move.l    4,a6
  55.                     REMHEAD
  56.                     tst.l     d0
  57.                     beq       SVAlarm
  58.  
  59.                     move.l    ThisTask(a6),a1
  60.                     move.l    d0,a0
  61.                     move.l    a1,TS_FaultTask(a0)
  62.                     lea       TS_TmpStack+TMP_STACKSIZE(a0),a1
  63.                     move.l    a0,-(a1)            ; Start address of trap struct
  64.                     move.l    usp,a0
  65.                     move.l    a0,-(a1)            ; orig USP
  66.                     move.w    4*4(sp),-(a1)       ; SR
  67.                     move.l    4*4+2(sp),-(a1)     ; PC of task
  68.                     move.l    a1,usp
  69.                     lea       LaunchPatch(pc),a0
  70.                     move.l    a0,4*4+2(sp)
  71.                     movem.l   (sp)+,d0/a1/a6
  72.                     move.l    (sp)+,a0
  73.  
  74.                     move.l    _OrigSwitch,-(sp)
  75.  
  76.                     rts                           ; call orig switch routine
  77.  
  78.                     ALIGN_LONG
  79. NoVirtMemSwitch     move.l    (sp),a0
  80.                     move.l    _OrigSwitch,(sp)
  81.                     rts
  82.  
  83. SVAlarm:            
  84.                     PRINT_DEB "Switch: SV Alarm"
  85.                     move.l    4,a6
  86.                     move.l    #NoTrapStructsAlertNum,d7
  87.                     jmp       _LVOAlert(a6)
  88.  
  89.                     ALIGN_LONG
  90. LaunchPatch:        * The trap struct stack now contains the following:
  91.                     * high: | Start address of trap struct
  92.                     *       | orig USP
  93.                     *       | CCR.W 
  94.                     * low : | PC of task
  95.  
  96. *                    PRINT_DEB "Launch"
  97.  
  98.                     move.l    a2,([6,sp],-10)     ; put a2 onto user stack
  99.                     move.l    6(sp),a2            ; orig USP
  100.                     sub.w     #10,a2
  101.                     move.l    (sp),6(a2)          ; orig PC
  102.                     move.w    4(sp),4(a2)         ; CCR
  103.  
  104.                     movem.l   d0/a0-a1/a6,-(a2)
  105.                     move.l    10(sp),a1
  106.                     lea       _Free,a0
  107.                     move.l    4,a6
  108.                     FORBID
  109.                     ADDTAIL
  110.                     PERMIT
  111.                     move.l    a2,sp
  112.                     movem.l   (sp)+,d0/a0-a1/a6
  113.                     move.l    (sp)+,a2
  114.  
  115.                     rtr
  116.  
  117. *********************************************************************
  118.  
  119.                     ALIGN_LONG
  120. _AddTaskPatch:      * Called with the following parameters:
  121.                     * a1 : Pointer to Task
  122.                     * a2 : Initial PC
  123.                     * a3 : Final PC
  124.                     * a6 : SysBase
  125.  
  126.                     PRINT_DEB "AddTask: PC = %lx. NewTaskName:",a2
  127.  
  128.                     IFD       DEBUG
  129.                     move.l    a1,-(sp)
  130.                     clr.l     -(sp)
  131.                     move.l    LN_NAME(a1),-(sp)
  132.                     jsr       _PrintDebugMsg
  133.                     add.w     #8,sp
  134.                     move.l    (sp)+,a1
  135.                     ENDC
  136.  
  137.                     move.l    TC_SPREG(a1),d0
  138.                     IN_VM     d0,NoVirtMemAdd
  139.  
  140.                     PRINT_DEB "AddTask: Stack in VM. PC = %lx",a2
  141.  
  142.                     movem.l   a2/a3,-(sp)
  143.                     move.l    a1,-(sp)
  144.                     lea       _Free,a0
  145.                     FORBID
  146.                     REMHEAD                       ; Get a trap struct
  147.                     PERMIT
  148.                     move.l    (sp)+,a1            ; Task
  149.                     tst.l     d0
  150.                     beq       AddTaskAlarm
  151.                     move.l    TC_SPREG(a1),a0
  152.                     move.l    a3,-(a0)            ; FinalPC
  153.                     bne       FinalPCValid
  154.                     move.l    TaskExitCode(a6),(a0)
  155. FinalPCValid        move.l    d0,a3               ; TrapStruct
  156.  
  157.                     move.l    a5,-(sp)
  158.                     lea       TS_TmpStack+TMP_STACKSIZE(a3),a5
  159.                     move.l    a3,-(a5)            ; TrapStruct
  160.                     move.l    a0,-(a5)            ; orig USP
  161.                     clr.w     -(a5)               ; CCR
  162.                     move.l    a5,TC_SPREG(a1)
  163.                     move.l    (sp)+,a5
  164.  
  165.                     move.l    a2,a3
  166.                     lea       LaunchPatch(pc),a2
  167.                     move.l    _OrigAddTask,a0
  168.                     jsr       (a0)
  169.                     movem.l   (sp)+,a2-a3
  170.                     rts
  171.  
  172.                     ALIGN_LONG
  173. NoVirtMemAdd:       move.l    _OrigAddTask,a0
  174.                     jmp       (a0)
  175.  
  176. AddTaskAlarm:       PRINT_DEB "AddTaskAlarm"
  177.                     move.w    #ERR_CONTINUE,-(sp)
  178.                     pea       AddTaskAlarmText(pc)
  179.                     jsr       _ReportError
  180.                     moveq     #0,d0
  181.                     jmp       _LVOWait(a6)        ; forever
  182.  
  183. AddTaskAlarmText    dc.b      "No more trapstructs in AddTask."
  184.                     dc.b      10             ; LF
  185.                     dc.b      "Adding task stopped",0
  186.                     ds.l      0
  187.  
  188. *********************************************************************
  189.  
  190.                     ALIGN_LONG
  191. _WaitPatch:         * Called with the following parameters:
  192.                     * d0 : Awaited Signals 
  193.                     * a6 : SysBase
  194.                     *
  195.                     * Upon return:
  196.                     * d0 : Received Signals
  197.  
  198.                     IN_VM     sp,StackNotInVM
  199.  
  200.                     move.l    sp,a0
  201. *                    PRINT_DEB "WaitPatch: Stack in VM at %lx",a0
  202.                     lea       _Free,a0
  203.                     movem.l   d2/a2,-(sp)
  204.                     move.l    d0,d2               ; save awaited signals,
  205.                                                   ; so they can be accessed 
  206.                                                   ; after StackSwap
  207.                     FORBID
  208.                     REMHEAD
  209.                     PERMIT
  210.                     tst.l     d0
  211.                     beq       NoTrapStruct
  212.                     move.l    d0,a2
  213.                     lea       TS_TmpStackSwap(a2),a0
  214.                     jsr       _LVOStackSwap(a6)
  215.                     move.l    d2,d0               ; restore awaited signals
  216.                     move.l    _OrigWait,a0
  217. *                    PRINT_DEB "WaitPatch: Before Wait"
  218.                     jsr       (a0)
  219. *                    PRINT_DEB "WaitPatch: Returned from Wait. Signals %08lx",d0
  220.                     move.l    d0,d2               ; save received signals
  221.                     lea       TS_TmpStackSwap(a2),a0
  222.                     jsr       _LVOStackSwap(a6)
  223.                     lea       _Free,a0
  224.                     move.l    a2,a1
  225.                     FORBID
  226.                     ADDTAIL
  227.                     PERMIT
  228.                     move.l    d2,d0               ; restore received signals
  229.                     movem.l   (sp)+,d2/a2
  230.                     rts
  231.  
  232.  
  233. NoTrapStruct:       move.l    d2,d0
  234.                     movem.l   (sp)+,d2/a2
  235.                     PRINT_DEB "WaitPatch: No TrapStruct"
  236.                     bra       StackNotInVM
  237.  
  238.                     ALIGN_LONG
  239. StackNotInVM:       move.l    _OrigWait,a0
  240.                     jmp       (a0)
  241.  
  242. *********************************************************************
  243.  
  244.                     IFD       DEBUG
  245.  
  246. _RemTaskPatch       PRINT_DEB "Calling RemTask"
  247.                     move.l    _OrigRemTask,a0
  248.                     jsr       (a0)
  249.                     PRINT_DEB "RemTask returned"
  250.                     rts
  251.  
  252. *********************************************************************
  253.  
  254. _StackSwapPatch     move.l    sp,a1
  255.                     PRINT_DEB "Calling StackSwap. CurrentStack: %lx",a1
  256.                     move.l    stk_Pointer(a0),d0
  257.                     PRINT_DEB "New Stack at %lx",d0
  258.                     move.l    _OrigStackSwap,a1
  259.                     jmp       (a1)
  260.  
  261.                     ENDC
  262.  
  263.                     end
  264.