home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / nextstep / expanded / LocalApps / Executor.app / ardimods / ardimods.s < prev    next >
Encoding:
Text File  |  1994-06-15  |  8.5 KB  |  292 lines

  1. /*
  2.  * Although written by Abacus R&D, the source code in this file is not
  3.  * copyrighted by Abacus R&D, but is in the public domain.
  4.  */
  5.     
  6. /*
  7.  * Revised ardimods:
  8.  *
  9.  * V1.1.1:
  10.  *   In some instances either the copyin or copyout was corrupting the
  11.  *   user stack pointer.  My guess is that when copyin or copyout took
  12.  *   a page fault, the fault code made assumptions about the amount of
  13.  *   state that had already been saved.  The assumptions were not correct,
  14.  *   because the ARDImods short-circuits the saving of much state (that's
  15.  *   what makes them fast).  The new strategy is to spl7(), set a flag
  16.  *   that says we're special and do the copyin and copyout ourselves,
  17.  *   noting whether or not we get a bus error.  If we get the error we'll
  18.  *   just do things the old-fashioned, slow way, using UNIX style signal
  19.  *   handlers.  This will rarely be the case, so we will still see tremendous
  20.  *   speedups using the ARDImods.
  21.  *
  22.  * V1.2:
  23.  *   Running "Globes" with "Sync to VBL" chosen can lock up machines.  The
  24.  *   symptoms suggest that somehow an important interrupt is being missed
  25.  *   and from that point on the machine continues running without ever
  26.  *   switching tasks.  In light of this, the new strategy is to not use
  27.  *   spl7, but to look at the return pc to determine whether or not we're
  28.  *   in this code.
  29.  *
  30.  * V1.3:
  31.  *   The bottom line is that without access to the NeXT kernel source, there's
  32.  *   an incredibly slim chance that the fastaline code will cause a machine
  33.  *   to hang.  For that reason, we've disabled that section of the code.
  34.  *   However, if you're interested, you can uncomment out the lines in
  35.  *
  36.  * V1.4:
  37.  *   Replacing rte with bra ardifpsp_done fixes the potential hang problem.
  38.  *   Also moved all of the illegal instruction simulation into user space
  39.  *   (with the exception of "movc d0, cacr", which is now totally in assembly).
  40.  *   FASTALINETRAPS is now de rigueur since there's not even a one instruction
  41.  *   window of vulnerability.  NOTE: a bug in NeXTSTEP2.x makes hangs still
  42.  *   possible.  We haven't seen any hangs, nor do I believe they're possible
  43.  *   in NS3.0.
  44.  *
  45.  * V1.5:
  46.  *   NEXTSTEP 3.1 has totally changed fpsp_done.  We now use theirs and I
  47.  *   believe they've fixed their bugs.  If you have a pre 3.1 version of
  48.  *   NEXTSTEP, then you might want to use "ardifpsp_done" instead of
  49.  *   NeXT's fpsp_done.
  50.  *
  51.  * V1.6:
  52.  *   Some people are going to want to use these mods on 3.0 systems, so
  53.  *   instead of making the user patch a line, we'll try to detect 3.0 systems
  54.  *   ourselves.
  55.  *
  56.  * V1.7:
  57.  *   Got rid of the stuff that temporarily disables the cache.  There's no
  58.  *   need for it.  Macs have had '040's in them for a while now.
  59.  *
  60.  * V1.8:
  61.  *   newsrstuff sets up location 0x54 with a copy of dma_chip so we can
  62.  *   test for the turbo monitors for accelerated graphics.  This is not
  63.  *   a pretty hack, but it's painless and gets the job done, while we're
  64.  *   waiting for a better solution from NeXT.
  65.  *
  66.  */
  67.  
  68.     .data
  69.     .align 2
  70. oldalinevec:    .long    0    /* for dispatching A-Line requests */
  71. oldprivvec:    .long    0    /* for handling moves to/from SR */
  72. oldbusvec:    .long    0    /* for detecting page faults */
  73.  
  74. troublevec:    .long    0    /* used when we may want to back out
  75.                    of a bus error on an 040 */
  76.  
  77. busbadness:    .byte    0    /* used when we detect a bus error on an 030 */
  78.  
  79.     .text
  80.     .even
  81.  
  82. .globl _replace_vectors
  83. _replace_vectors:
  84.     movc    vbr,            a0
  85.     movel    a0@(0x08),        oldbusvec
  86.     movel    #dobuscheck,        a0@(0x08)
  87.     movel    a0@(0x28),        oldalinevec
  88.     movel    #fastalinecode,        a0@(0x28)
  89.     movel    a0@(0x20),        oldprivvec
  90.     movel    #newsrstuff,        a0@(0x20)
  91.     rts
  92.  
  93. .globl _restore_vectors
  94. _restore_vectors:
  95.     movc    vbr,            a0
  96.     movel    oldprivvec,        a0@(0x20)
  97.     movel    oldalinevec,        a0@(0x28)
  98.     movel    oldbusvec,        a0@(0x08)
  99.     rts
  100.  
  101. fastalinecode:
  102.     clrb    busbadness
  103.     moveml    d0/d1/a0/a1,    sp@-        /* C saving precautions */
  104.     movew    sr,        sp@-        /* so we can SPL7 later */
  105.     link    a6,        #0        /* so we can back out easily */
  106.     movel    oldalinevec,    troublevec    /* so we'll back out to the */
  107.     moveq    #1,        d1        /* D1: Constant: #1 */
  108.     movec    d1,        sfc        /* source: user space */
  109.  
  110.     lea    0x5C,        a0
  111.     movesl    a0@,        d0
  112.     cmpl    #0x41524449,    d0    /* check for 'ARDI' in 0x5C */
  113.     bne    newnogood
  114.  
  115.     movesl    @(0x28),    a1
  116.  
  117.  
  118.     movec    d1,        dfc
  119.     oriw    #0x700,        sr
  120.     movc    usp,        a0    /* A0: user stack pointer */
  121.     movel    a6@(24),    d1    /* D1: OLD PC */
  122.     movesl    d1,        a0@-
  123.     movew    a6@(22),    d1    /* D1: OLD status */
  124.     movesw    d1,        a0@-
  125.     tstb    busbadness
  126.     bne    newnogood
  127.     movc    a0,        usp
  128.     movel    a1,        a6@(24)
  129.     bra    amf
  130.     
  131. dobuscheck:
  132.     cmpl    #fastalinecode,    sp@(2)
  133.     ble    notours
  134.     cmpl    #amf,        sp@(2)
  135.     bgt    notours
  136.  
  137. /*
  138.  * Check to make sure that we were in Supervisor mode.
  139.  */
  140.  
  141.     btst    #5,    sp@
  142.     beq    notours
  143.  
  144.     tstb    _cpu_type
  145.     beq    dobus_30
  146.  
  147. dobus_40:
  148.     movew    d0,        sp@-
  149.     movew    sp@(0xe),    d0    /* SSW */
  150.     andiw    #0xDA3F,    d0    /* ignore CT, RW, X, SIZE1 */
  151.     cmpiw    #0x0001,    d0    /* TM = 1 */
  152.     bne    not_ours2
  153.     movew    sp@+,        d0
  154.     lea    newnogood,    a0    /* this will get our code to end */
  155.     movel    a0,        sp@(2)    /* properly */
  156.     rte
  157.  
  158. dobus_30:
  159.     bclr    #0,    sp@(0xA)    /* Axe DF bit */
  160.     st    busbadness
  161.     rte                /* too bad */
  162.  
  163. not_ours2:
  164.     movew    sp@+,        d0
  165. notours:
  166.     movel    oldbusvec,    sp@-    /* It's not ours; set up oldbusvec */
  167.     rts                /* and jump */
  168.  
  169.  
  170. /*
  171.  * In order to verify that we are indeed and Executor
  172.  * process, we check out location 0x5C and look for
  173.  * 'ARDI', but page zero can get swapped out, and even
  174.  * if we find what we want, we could take a page fault
  175.  * when we try to put stuff on the stack.
  176.  *
  177.  * So, here's the trick: 
  178.  *
  179.  * If we don't get what we want, we just let
  180.  * the normal UNIX trap handler get it's hands on things,
  181.  * but instead of actually responding to the ILL or ALINE,
  182.  * the UNIX signal handler just touches the relevent
  183.  * pages and restarts the instruction which should work
  184.  * the second time around.
  185.  *
  186.  * Because we're paranoid, we SPL7 before we start putting stuff
  187.  * on the user's stack, since even though it's unlikely, there
  188.  * could be some interrupt driven routine that also munges stuff
  189.  * on the users stack pointer.
  190.  */
  191.  
  192. /*
  193.  * this location is used when we weren't able to process an ALINE or
  194.  * an ILL and we need to go through the UNIX mechanism
  195.  */
  196.  
  197. newnogood:
  198.     unlk    a6
  199.     movew    sp@+,        sr
  200.     moveml    sp@+,        d0/d1/a0/a1
  201.     movel    troublevec,    sp@-
  202.     rts
  203.  
  204. newsrstuff:
  205.     clrb    busbadness
  206.     moveml    d0/d1/a0/a1,    sp@-        /* C saving precautions */
  207.     movew    sr,        sp@-        /* so we can SPL7 later */
  208.     link    a6,        #0        /* so we can back out easily */
  209.     movel    oldprivvec,    troublevec    /* so we'll back out to the */
  210.                         /* right place */
  211.     moveq    #1,        d1        /* D1: Constant: #1 */
  212.     movec    d1,        sfc        /* source: user space */
  213.     movec    d1,        dfc        /* dest: user space */
  214.  
  215.     movesl    @(0x5C),    d1
  216.  
  217.     cmpl    #0x41524449,    d1    /* check for 'ARDI' in 0x5C */
  218.     bne    newnogood
  219.  
  220.     movel    _dma_chip,    d1        /* For turbo detection */
  221.     movesl    d1,        @(0x54)        /* to be picked up later... */
  222.  
  223.     movel    a6@(24),    a0        /* pick up faulting longword */
  224.     movesl    a0@,        d1
  225.  
  226.     tstb    busbadness
  227.     bne    newnogood
  228.  
  229. /* don't know what it is, hand it off */
  230. /* leaving the old pc buried under the old sr-as-a-longword */
  231.  
  232.     oriw    #0x700,        sr
  233.     movc    usp,        a0
  234.     movew    a6@(22),    d0
  235.     movesl    d0,        a0@-
  236.     movel    a6@(24),    d0
  237.     movesl    d0,        a0@-
  238.     lea    0x20,        a1
  239.     movesl    a1@,        d0
  240.     tstb    busbadness
  241.     bne    newnogood
  242.     movel    d0,        a6@(24)
  243.     movc    a0,        usp
  244.     bra    amf
  245.  
  246. /* This is our implementation of movc    d0, cacr */
  247.  
  248. amf:
  249.     unlk    a6
  250.     movew    sp@+,        sr
  251.     moveml    sp@+,        d0/d1/a0/a1
  252.     cmpl    #0x040a3058,    fpsp_done+6    /* old value of
  253.                            "active_threads".  NOTE:
  254.                            This test may not work
  255.                            for 2.x Systems.  If you
  256.                            have a 2.x System, you may
  257.                            want to change the following
  258.                            "beq" to a "bra" */
  259.     beq    ardifpsp_done    /* == 3.0 system, use our patched fpsp_done */
  260.     bra    fpsp_done    /* != 3.1 system, use "real" fpsp_done */
  261.  
  262. /*
  263.  * The code below is based on the functionality seen in fpsp_done, with
  264.  * a few efficiencies tossed in, some bogus code removed and a fix for
  265.  * what I suspect to be a NeXT bug.
  266.  */
  267.  
  268. ardifpsp_done:
  269.     movel    a0,sp@-
  270.     lea    _active_threads,a0
  271.     movel    a0@,a0            // only one CPU
  272.     movel    a0@(0x28:w),a0
  273.     movew    #0x2700,sr      // No interrupts
  274.     btst    #2,a0@(0x1a2:w)      // Is TRACE_AST set?
  275.     bne    needtrace        // YES: need trace (NOTE: This is
  276.                   //      fixs an annoying bug)
  277.     bclr    #3,a0@(0x1a2:w)      // Is AST_SCHEDULE set?
  278.     beq     out            // NO: don't need trace
  279.     bset    #2,a0@(0x1a2:w)   // Set TRACE_AST
  280. needtrace:
  281.     btst    #6,sp@(0x4:w)      // Is T0 already set?
  282.     bne    1f          // YES: Don't set T1
  283.     bset    #7,sp@(0x4:w)      // Set the T1 bit so we'll be thrown back
  284.                   // Into supervisor as soon as we RTE
  285. 1:
  286.     bclr    #0,a0@(0x1a2:w)      // Don't wait until the end of the
  287.                   // System call; we're not in a system
  288.                   // call and aren't guaranteed to be soon
  289. out:
  290.     movel    sp@+,a0
  291.     rte
  292.