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

  1.                     SECTION   CODE
  2.  
  3.                     INCLUDE   "exec/memory.i"
  4.                     INCLUDE   "exec/execbase.i"
  5.                     INCLUDE   "exec/semaphores.i"
  6.                     INCLUDE   "exec/ables.i"
  7.                     INCLUDE   "dos/dosextens.i"
  8.                     INCLUDE   "exec/funcdef.i"
  9.                     INCLUDE   "exec/ports.i"
  10.                     INCLUDE   "exec/exec_lib.i"
  11.                     INCLUDE   "dos/dos_lib.i"
  12.                     INCLUDE   "shared_defs.i"
  13.                     INCLUDE   "macros.i"
  14.  
  15.  
  16.                     XDEF      _LoadSegPatch
  17.                     XDEF      _NewLoadSegPatch
  18.                     IFD       DEBUG
  19.                     XDEF      _CrashHandler
  20.                     XDEF      _FindHunk
  21.                     XDEF      _OrigFuncs
  22.                     XDEF      _AlertPatch
  23.                     XREF      _OrigAlert
  24.                     INT_ABLES
  25.                     ENDC
  26.  
  27.                     XREF      _OrigLoadSeg
  28.                     XREF      _CodePagingAllowed
  29.                     XREF      _AllocVM
  30.                     XREF      _OrigAllocMem
  31.                     XREF      _OrigFreeMem
  32.                     XREF      _ChangeOwner
  33.                     XREF      _MemTracking
  34.                     XREF      _LoadingTasksList
  35.  
  36.                     IFD       _PHXASS_
  37.                     MACHINE   MC68030
  38.                     ENDC
  39.  
  40. * There's an undocumented feature of LoadSeg:
  41. * If it's passed a NULL value in A1, it should do some kind of
  42. * overlay loading. I don't know exactly how this works, so
  43. * in this case the original LoadSeg routine is called
  44.  
  45. _LVOInternalLoadSeg EQU       -$2f4
  46. _LVOFilePart        EQU       -$366
  47. _LVONameFromLock    EQU       -$192
  48.  
  49. UseOrigLoadSeg      move.l    _OrigLoadSeg,a0
  50.                     PRINT_DEB "LoadSeg into PUBLIC"
  51.                     jmp       (a0)
  52.  
  53. _LoadSegPatch       * d1 : name (char*)
  54. _NewLoadSegPatch    * a6 : DOSBase
  55.                     *
  56.                     * returns:
  57.                     * d0 : SegList
  58.                     * d1 : SegList
  59.  
  60.                     PRINT_DEB "LoadSegPatch called"
  61.                     tst.l     d1
  62.                     beq       UseOrigLoadSeg
  63.                     move.l    d1,a0
  64.                     tst.b     (a0)
  65.                     beq       GetNameFromCurrentDir
  66. CheckCodePaging
  67.                     move.l    d1,-(sp)
  68.                     jsr       _CodePagingAllowed
  69.                     move.l    (sp)+,d1
  70.                     tst.w     d0
  71.                     beq       UseOrigLoadSeg
  72.  
  73.                     movem.l   d2-d5/a2-a4,-(sp)
  74.  
  75.                     * Register assignments for the following routine
  76.                     * d3: pointer to input filename
  77.                     * d4: filehandle received from Open ()
  78.                     * d5: SegList received from InternalLoadSeg ()
  79.                     * a3: LoadingTaskStruct
  80.                     * a4: DOSBase
  81.  
  82.                     move.l    d1,d3                    ; store filename and DOSBase
  83.                     move.l    a6,a4                    ; they are needed multiple times
  84.                     suba.l    a3,a3                    ; default pointer to LoadingTaskStruct
  85.                     tst.w     _MemTracking
  86.                     beq       DontTrack
  87.  
  88.                     move.l    4,a6
  89.                     moveq     #LT_SIZE,d0
  90.                     moveq     #MEMF_PUBLIC,d1
  91.                     move.l    _OrigAllocMem,a0         ; no need to go through patch
  92.                     jsr       (a0)
  93.                     move.l    ThisTask(a6),a0
  94.                     tst.l     d0
  95.                     move.l    d0,a3                    ; save pointer to LoadingTaskStruct
  96.                     beq       DontTrack
  97.                     move.l    d3,LoadfileName(a3)
  98.                     move.l    a0,LoadingTask(a3)
  99.                     lea       _LoadingTasksList,a0
  100.                     move.l    a3,a1
  101.                     FORBID
  102.                     ADDHEAD                            ; d1 is not touched by this
  103.                     PERMIT
  104.  
  105. DontTrack           moveq     #0,d5                    ; default result
  106.                     move.l    a4,a6                    ; restore DOSBase
  107.                     move.l    #MODE_OLDFILE,d2
  108.                     move.l    d3,d1
  109.                     jsr       _LVOOpen(a6)
  110.                     tst.l     d0
  111.                     beq       Finish
  112.                     PRINT_DEB "Open successful"
  113.                     move.l    d0,d4                    ; save fh
  114.  
  115.                     lea       FuncVMTable(pc),a1
  116.                     PRINT_DEB "LoadSeg into VM"
  117.                     suba.l    a0,a0          ; overlay table = NULL
  118.                     lea       DummyStackSize(pc),a2
  119.                     jsr       _LVOInternalLoadSeg(a6)
  120.                     move.l    d0,d5
  121.                     move.l    d4,d1
  122.                     PRINT_DEB "LoadSeg: Closing file"
  123.                     jsr       _LVOClose(a6)
  124.                     PRINT_DEB "LoadSeg: File closed"
  125.  
  126. Finish              tst.l     a3                       ; check if LoadingTaskStruct has to be freed
  127.                     beq       DontFree
  128.  
  129.                     move.l    4,a6
  130.                     move.l    a3,a1
  131.                     FORBID
  132.                     REMOVE                             ; from LoadingTasksList
  133.                     PERMIT
  134.                     move.l    a3,a1
  135.                     move.l    #LT_SIZE,d0
  136.                     move.l    _OrigFreeMem,a0
  137.                     jsr       (a0)
  138.                     move.l    a4,a6                    ; restore DOSBase
  139.  
  140. DontFree            move.l    d5,d0
  141.                     move.l    d5,d1
  142.                     movem.l   (sp)+,d2-d5/a2-a4
  143.                     rts
  144.  
  145. GetNameFromCurrentDir:
  146.                     PRINT_DEB "LoadSegPatch: Reading loadfile from current dir"
  147.                     movem.l   d2-d3/a4,-(sp)
  148.                     move.l    a6,a4
  149.                     sub.w     #128,sp
  150.                     move.l    4,a6
  151.                     move.l    ThisTask(a6),a0
  152.                     move.l    a4,a6
  153.                     move.l    pr_CurrentDir(a0),d1
  154.                     move.l    sp,d2
  155.                     moveq     #127,d3
  156.                     jsr       _LVONameFromLock(a6)
  157.                     move.l    sp,d1
  158.                     tst.w     d0
  159.                     bne       GotName
  160.                     PRINT_DEB "LoadSegPatch: Couldn't get name from lock"
  161.                     clr.b     (sp)
  162. GotName:            bsr       CheckCodePaging
  163.                     add.w     #128,sp
  164.                     movem.l   (sp)+,d2-d3/a4
  165.                     rts
  166.  
  167. *********************************************************************
  168.  
  169. ReadFunc:           jmp       _LVORead(a6)
  170. FreeFunc:           jmp       _LVOFreeMem(a6)
  171.  
  172. AllocFunc           jsr       _AllocVM
  173.                     tst.w     _MemTracking
  174.                     beq       AllocReady
  175.  
  176.                     move.l    d0,-(sp)            ; pass buffer to ChangeOwner
  177.                     jsr       _ChangeOwner
  178.                     move.l    (sp)+,d0
  179. AllocReady          rts
  180.  
  181. *********************************************************************
  182.  
  183.                     IFD       DEBUG
  184.  
  185. _CrashHandler:      sub.w     #4,sp               ; reserve space for orig handler
  186.                     movem.l   d0-d1/a0-a1,-(sp)
  187.                     move.l    sp,a0
  188.                     PRINT_RANGE
  189.                     moveq     #0,d0
  190.                     move.w    5*4+6(sp),d0       ; Vector offset and frame id
  191.                     and.w     #$0fff,d0
  192.                     lea       _OrigFuncs,a0
  193.                     move.l    (a0,d0.w),4*4(sp)
  194.                     asr.w     #2,d0
  195.                     PRINT_DEB "Trap %lx occurred.",d0
  196.                     move.l    5*4+2(sp),d0
  197.                     PRINT_DEB "Stacked PC = %lx",d0
  198.                     move.l    d0,-(sp)
  199.                     bsr       _FindHunk
  200.                     add.w     #4,sp
  201.                     movem.l   (sp)+,d0-d1/a0-a1
  202.                     rts                           ; call orig func
  203.                     
  204.  
  205. *********************************************************************
  206.  
  207. _FindHunk:          * called with an address on the stack
  208.                     * prints the information received from SegTracker
  209.                     * to the debug file
  210.  
  211.                     movem.l   a2-a3/a6,-(sp)
  212.                     move.l    4,a6
  213.                     DISABLE
  214.                     lea       SegTrackerName(pc),a1
  215.                     lea       SemaphoreList(a6),a0
  216.                     jsr       _LVOFindName(a6)
  217.                     tst.l     d0
  218.                     bne       SegTrackerRunning
  219.                     PRINT_DEB "SegTracker is not running"
  220.                     bra       EndFindSegment
  221.  
  222. SegTrackerRunning   move.l    d0,a0
  223.                     move.l    SS_SIZE(a0),a3      ; function ptr
  224.                     move.l    4*4(sp),a0          ; address
  225.                     PRINT_DEB "SegTracker for address: %lx",a0
  226.                     sub.w     #8,sp
  227.                     lea       (sp),a1             ; ptr to SegNum
  228.                     lea       4(sp),a2            ; ptr to offset
  229.                     jsr       (a3)                ; returns name in d0
  230.                     tst.l     d0
  231.                     beq       NoHunk
  232.                     pea       0
  233.                     move.l    d0,-(sp)
  234.                     jsr       _PrintDebugMsg      ; print seg name
  235.                     add.w     #8,sp
  236.                     move.l    (sp),d0
  237.                     PRINT_DEB "Hunk: %ld",d0
  238.                     move.l    4(sp),d0
  239.                     PRINT_DEB "Offset: %lx",d0
  240. NoHunk              add.w     #8,sp
  241.  
  242. EndFindSegment      ENABLE
  243.                     movem.l   (sp)+,a2-a3/a6
  244.                     rts
  245.  
  246. *********************************************************************
  247.  
  248. _AlertPatch:        PRINT_DEB "Alert %lx called",d7
  249.  
  250.                     move.l    _OrigAlert,-(sp)
  251.                     rts
  252.  
  253. *********************************************************************
  254.  
  255. SegTrackerName:     dc.b      "SegTracker",0
  256.                     ds.w      0
  257.  
  258. _OrigFuncs          ds.l      60
  259.  
  260.                     ENDC      ; DEBUG
  261.  
  262. *********************************************************************
  263.  
  264. DummyStackSize      dc.l      0
  265.  
  266. FuncVMTable         dc.l      ReadFunc
  267.                     dc.l      AllocFunc
  268.                     dc.l      FreeFunc
  269.  
  270.                     end
  271.