         PCC      0
         TITLE    'SWAP SCHEDULER REFS AND DEFS'
UFLAGS   SET      1
         SYSTEM   UTS
***********************************************************************
*M*               SWPSCHED - SWAP SCHEDULER
***********************************************************************
         SPACE    2
SWPSCED:          EQU               %
***********************************************************************
*P*      NAME:    SWPSCHED
*P*      PURPOSE: TO SCHEDULE INSWAPS OF THE HIGHEST PRIORITY
*P*               EXECUTABLE USERS NOT COMPLETELY INCORE.
*P*      DESCRIPTION: THE HIGHEST PRIORITY OUT OF CORE USER
*P*               IS SELECTED BY RULES IDENTICAL TO THOSE FOR
*P*               SCHEDULING EXECUTION.  THE PAGES REQUIRED TO
*P*               INSWAP THIS USER WILL BE OBTAINED FROM THE FREE PAGE
*P*               POOL, UNUSED SHARED PROCEDURES INCORE AND OTHER
*P*               LOWER PRIORITY OR NON-EXECUTABLE USERS.
*P*               THE OUTPUT OF THE SWAP SCHEDULER IS AN INSWAP
*P*               USER NUMBER, A PAGE CHAIN AND A LIST OF OUTSWAP
*P*               USERS WHICH IS PASSED TO THE SWAPPER TO ACTUALLY
*P*               PERFORM THE SWAP.
*P*      ENVIRONMENT: SWPSCHED EXECUTES MASTER MODE UNMAPPED
************************************************************************
         SPACE    2
*        ORIGINAL IMPLEMENTATION FOR UTS BY:
*                                             G. A. PERRY
*                                             H. L. SCANTLIN
*
*        SPEED-UP ENHANCEMENTS BY: T. W. MARTIN
*
*        MULTI-PRIORITY SCHEDULING FOR REAL TIME BY:
*                                             R. I. HUSTVEDT
*
         PAGE
***********************************************************************
*                 EXTERNAL DEFINITIONS
***********************************************************************
         DEF      GIVEUP            GIVE UP ON SWAP ATTEMPT
         DEF      PGCHKM            CHECK FREE PAGE CHAIN
         DEF      SWPSCED:          MODULE NAME FOR PATCHING
         DEF      T:PGCHK           PAGE CHAIN CHECK ROUTINE
         DEF      T:SS              SWAP SCHEDULER
         DEF      TSS1              SWAP SCHED RE-ENTRY POINT
         DEF      TSS2              SWAP SCHED RE-ENTRY POINT
         PAGE
***********************************************************************
*                 EXTERNAL REFERENCES
***********************************************************************
         REF      ALLOOUT           OUTSWAP FLAG FOR ALLOCAT
         REF      C:NOPROC          CNT OF TIME PROC NOT IN-CORE
         REF      C:NSP             COUNT OF SWAP SCHEDULE FAILURES
         REF      C:PROCREQ         CNT OF TIMES PROC REQUIRED
         REF      DASP              DISASSOCIATE ASP
         REF      DDB               DISASSOCIATE DEBUGGER
         REF      DID%IO            MARKED IF SWAPPER DOES I/O
         REF      DOUBLEZERO        CONSTANT 0,0 (DBLWD)
         REF      DOV               DISASSOCIATE  MONITOR OVERLAY
         REF      DTEL              DISASSOCIATE COMMAND PROCESSOR
         REF      DTORP             DISSSSOCIATE ACP OR APR
         REF      E:KO              KICKOUT EVENT NUMBER
         REF      IOV               INCREMENT MON OVERLAY PB:UC
         REF      IPROCS            INC PB:UC FOR PROCESSORS
         REF      ITEL              INC PB:UC FOR COMMAND PROCESSOR
         REF      ITORP             INC PB:UC FOR ACP OR PROCS
         REF,1    JB:PNR            PARTITION NUMBER FOR BATCH
         REF      JBPPC             USER PAGE CHAIN COUNT
         REF,1    JIT               BASE ADDRESS OF JIT
         REF      M:FPPC            FREE PAGE CHAIN COUNT
         REF      M:FPPH            FREE PAGE CHAIN HEAD
         REF      M:FPPT            FREE PAGE CHAIN TAIL
         REF      MX:PPUT           MONITOR PHYSICAL PAGE TABLES
         REF      M6                CONSTANT X'0000003F'
         REF      M8                CONSTANT X'000000FF'
         REF      NB31TO0           TABLE OF INVERTED BITS
         REF      NPMC              NO-PAGE MAP CONSTANT
         REF      PB:PSZ            PROCEDURE SIZE
         REF      PB:UC             USER COUNT
         REF      PH:FRQ            CALL FREQUENCY
         REF      PLH:FLG           BATCH PARTITION FLAGS
         REF      PPROCS            NO OF PROCS
         REF      PX:HPP            HEAD OF PHYSICAL PAGES
         REF      PX:TPP            TAIL        "      "
         REF      RCVPSD            PSD TO ENTER RECOVERY
         REF      S:ACORE           COUNT OF CURRENT FLUID PAGES
         REF      S:EVF             SCHEDULER RE-ENTRANCY COUNTER
         REF      S:FPL             FREE PROCESSOR LIST
         REF      S:FPPC            SWAPPER PAGE CHAIN COUNT
         REF      S:FPPH            SWAPPER PAGE CHAIN HEAD
         REF      S:FPPT            SWAPPER PAGE CHAIN TAIL
         REF      S:FSEVF           SEVF AT SWAP SCHED FAIL
         REF      S:ISUN            INSWAP USER NUMBER
         REF      S:ISUNF           ISUN AT SWAP SCHED FAIL
         REF      S:OPC             OVERLAY PROTECTION COUNTER
         REF      S:OSS             NO. OF PAGES FROM OUTSWAP LIST
         REF      S:PCORE           MAX PHYS USER SIZ
         REF      S:PCT             PAGES REQUIRED FOR SWAP
         REF      S:PRPC            PAGES FROM UNUSED SHARED PROCS
         REF      S:RTCORE          TOTAL OF REAL TIME HOLD PAGES
         REF      S:SEVF            SWAP SET CHANGE COUNTER
         REF      S:SIP             SWAP IN PROGRESS FLAG
         REF      S:STL#            NUMBER OF CUR STOLEN PAGES
         REF      S:STLC            STEALABLE PAGES
         REF      SB:FPL            LIST OF PROCS FREED FOR OSUL
         REF      SB:FPN            NO OF PROCS FREED FOR OSUL
         REF      SB:HQ             STATE QUEUE HEADS
         REF      SB:NP             NO OF INSWAP PROCS
         REF      SB:OSN            NO OF USERS IN OSUL
         REF      SB:OSUL           OUT SWAP USER LIST (OSUL)
         REF      SB:PNL            INSWAP PROCESSOR NUMBER LIST
         REF      SB:SWP            LIST OF SWAPPABLE STATES
         REF      SB:TQ             STATE QUEUE TAIL
         REF      SEXU              HIGHEST EXEC STATE NUMBER
         REF      SL:RSVP           COUNT OF PAGES RESERVED FOR STEALER
         REF      SMAXOUT           MAX SIZE FOR OSUL
         REF      SRT               REAL TIME STATE
         REF      SWAPIN            SWAP A USER IN
         REF      SWAPOUT           OUTSWAP A LIST THEN SWAPIN
         REF      T:RUE             REPORT USER EVENT
         REF      T:SEXIT           SWAPPER EXIT
         REF      T:TOTSZ           COMPUTE SWAP SIZE
         REF      TEMP              GENERAL TEMP CELL
         REF      UB:ACP            ASSOCIATED COMMAND PROCESSOR NO
         REF      UB:APO            ASSOC PROC OVERLAY NUMBER
         REF      UB:APR            ASSOCIATED PROCESSOR
         REF      UB:ASP            ASSOC SPECIAL PROCESSOR
         REF      UB:BL             STATE CHAIN BLINK
         REF      UB:DB             DEBUGGER
         REF      UB:FL             STATE CHAIN FLINK
         REF      UB:MF             MASTER FUNCTION COUNT
         REF      UB:OV             MONITOR OVERLAY NUMBER
         REF      UB:PCT            USER PAGE COUNT
         REF      UB:US             CURRENT STATE
         REF      UH:FLG            USER FLAGS
         REF      UH:FLG2           SECOND USER FLAGS
         REF      UX:JIT            JIT PHYS PAGE NUMBER
         REF      XN2               CONSTANT X'FFFFFFFE'
         REF      X8000             CONSTANT X'00008000'
         REF      Y7D               EXPONENT FOR FLOATING SHIFT
         REF      Y8                CONSTANT X'80000000'
         PAGE
XFDFF    EQU      NB31TO0+14
XFFEF    EQU      NB31TO0+5
XFFF7    EQU      NB31TO0+4
OPNCLSUSR EQU     8                 USER IS OPNCLS USER
SPECFILE EQU      4
OPNBOOST EQU      X'04'             PRIORITY BOOST FOR OPNCLSUS
SQUAN    EQU      8                 UH:FLG2 NOT HAD SWAP QUAN BIT
SWAPD    EQU      16                UH:FLG2 JUST SWAPPED IN
RTHOLD   EQU      X'800'            UH:FLG2 REAL TIME OLD
CALINT   EQU      X'200'            UH:FLG2 INTERRUPTED IN A CAL
RMAHOLD  EQU      X'2000'           UH:FLG2 RMA HOLD IN CORE
HANGUP   EQU      X'20'             UH:FLG2 COC LINE HANG UP
BYPASS   EQU      X'8000'           UH:FLG SWAP BYPASS FOR PAGE STEALING
INHIBIT  EQU      2                 UH:FLG INTENTRY INHIBIT
NREADY   EQU      XN2               UH:FLG MASK TO RESET RTR
         PAGE
*
R0       EQU      0
R1       EQU      1
R2       EQU      2
R3       EQU      3
R4       EQU      4
R5       EQU      5
R6       EQU      6
R7       EQU      7
R8       EQU      8
R9       EQU      9
R10      EQU      10
R11      EQU      11
R12      EQU      12
R13      EQU      13
R14      EQU      14
R15      EQU      15
         TITLE    'S W A P     S C H E D U L E R'
         SPACE    2
*************************************
*        SWAP SCHEDULER             *
*************************************
         SPACE    2
*D*      NAME:    T:SS
*D*      PURPOSE: T:SS WILL SUPPLY THE EXECUTION SCHEDULER WITH IN-CORE
*D*               EXECUTABLE USERS.
*D*      DESCRIPTION: T:SS WILL CONVERT THE HIGHEST PRIORITY USER
*D*               WITH THE READY-TO-RUN BIT(RTR) OFF INTO READY-TO-RUN
*D*               USERS BY PROVIDING MEMORY PAGES, SHARED PROCESSORS,
*D*               ETC. AS REQUIRED.  IF NECESSARY OTHER, LOWER PRIORITY
*D*               USERS WILL BE OUTSWAPPED.
*D*      ENVIRONMENT: T:SS EXECUTES MASTER MODE UNMAPPED. IT SHOULD BE
*D*               CALLED WITH INTERRUPTS ENABLED AND WILL ENABLE THEM
*D*               UPON EXIT.  THE FLAG S:SIP IS USED TO PREVENT
*D*               RE-ENTRANCE WHILE A SWAP IS IN PROGRESS.
*D*      REGISTERS: R11 - LINK
*D*                 ALL OTHER REGISTER ARE VOLATILE
*D*      INTERFACE: T:RUE- REPORT KICKOUT EVENT
*D*                 T:SEXIT - EXIT
*D*                 SWAPIN - INSWAP USER
*D*                 SWAPOUT - OUTSWAP USER LIST
*D*********************************************************************
T:SS     EQU      %
         LI,0     1
         XW,0     S:SIP             SWAPPER BUSY?
         BNE      *R11              RETURN IF SWAP SCHED IS BUSY
         STW,0    DID%IO
TSS1     EQU      %
         PUSH     11
TSS2     EQU      %
         LD,14    DOUBLEZERO        GET MORE ZEROES
         LCI      3                 SET TO STORE 3 WORDS
         STM,14   S:PCT             CLEAR S:PCT,S:FPPH,S:FPPT
         STM,14   S:FPPC            CLEAR S:FPPC,S:AJP,SB:OSN
         STM,R14  S:OSS             ZAP COUNT OF PROCESSORS
*
* PICK A USER TO SWAP IN.
*
         LI,15    RTR
         LD,2     SB:HQ             GET PRESENCE INDICATORS FOR FIRST 7
         BEZ      PIKF1             NONE, SKIP FIRST SEVEN
         OR,2     Y7D               SET EXPONENT FOR FLOATING SHIFT
         SFL,2    16                DO NORMALIZE TO FIND FIRST FULL STATE
         SCS,2    7                 COUNT=COUNT/2
         AND,2    M6                SCRUB
         EOR,2    M6                INVERT TO FORM INDEX
         B        %+2
PIKF1    LI,2     8                 START WITH EIGHTH STATE
PIKUS1   LB,4     SB:HQ,2           PICK UP QUEUE HEAD
         BEZ      PIKUS3
PIKUS2   CH,15    UH:FLG,4
         BAZ      SSIN              GOT ONE WHO NEEDS TO COME IN
PIKUS25  RES      0
         LB,4     UB:FL,4           MOVE TO NEXT IN Q
         BNEZ     PIKUS2
PIKUS3   EQU      %
         AI,2     1                 NEXT STATE
         CI,2     SEXU              MUST BE EXECUTABLE
         BLE      PIKUS1            IT IS, GET HEAD AND LOOK
NO1IN    LI,14    0
         STW,14   S:ISUN
         B        USERSOUT
*
PIKUS5   LI,0     0                 ZERO ZERO
         LI,R15   RTR               RESTORE READY TO RUN BIT
         LB,R2    UB:US,R4          RESTORE USER STATE NUMBER
*                                   BEFORE CONTINUING SCAN
         B        PIKUS25           TRY ANOTHER
         PAGE
*  AT THIS POINT WE KNOW WHO WE WANT TO BRING IN.
*  REGISTERS ARE:
*     0 = 0
*     4 = USER #
*     3 = Q HE IS IN.
*
*  OTHER RESISTERS USED CONSISTANTLY ARE:
*     1 = UB:APR         ASSOCIATED PROCESSOR ROOT
*     2 = UB:APR         ASSOCIATED PROCESSOR OVRLAY SEG.
*   15 = PAGES NEEDED
*    14 = UH:FLG,4
*
*
SSIN     CW,R4    S:ISUNF           DID WE FAIL ON THIS GUY?
         BNE      SSIN12             NO
         LW,R7    S:SEVF            HAS SWAP SET CHANGE
         CW,R7    S:FSEVF           OCCURRED SINCE WE FAILED?
         BNE      SSIN12            YES GO ON
         LW,R7    ALLOOUT           DOES ALLYCAT NEED TO GO OUT?
         BEZ      PROUT3            NO
         B        NO1IN             YES
SSIN12   RES      0
         MTH,0    UH:FLG,4          CHECK FOR BYPASS FLAG
         BGE      SSIN1             NO
         BAL,R6   GIVEUP7           TRIGGER PAGE STEALER RETURN
         B        PIKUS5            STILL WONT FIT
         LH,R7    UH:FLG,R4         GET FLAGS
         AI,R7    -BYPASS           RESET BYPASS BIT
         STH,R7   UH:FLG,R4         PUT AWAY UPDATED FLAGS
         LI,0     0                 ZERO ZERO
SSIN1    RES      0
         STW,4    S:ISUN            SAVE USERS #
         LB,14    UB:PCT,4          PICK UP # OF PAGES USER NEEDS
         STB,0    SB:NP
         LB,6     UB:OV,4
         BAL,1    PRCAV+1
         LH,15    UH:FLG,4
         CI,15    JIC               SEE IF THE GUYS PARTIALLY IN CORE
         BAZ      CHKDELTEL            NOPE'
         LOAD,6   UX:JIT,4          SUBTRACT THE PAGES HE HAS
         SLS,6    11                 FROM OUR REQUIREMENT
         AI,6     JBPPC
         LB,7     0,6
         SW,14    7
         CI,15    TIC               IS TEL IN CONTROL
         BANZ     CHKDT4             YES GET TEL
         B        PROCAVL           NO GET OTHER PROCS
         PAGE
*
*  CHECK TO SEE IF DEL OR TEL NEED TO BE
*  ASSOCIATED OR DE-ASSOCIATED
*
CHKDELTEL   EQU   %
         CI,15    TIC               IS TEL IN CONTROL
         BAZ      CHKDT3
         BAL,2    ITEL              INC TEL
         BAL,2    IOV
CHKDT4   EQU      %
         LB,6     UB:ACP,4          GET COMMAND PROC #
         BAL,R1   PRCAV             PUT IN INSWAP LIST
         CI,R15   BAT               IF TIC SET AND BATCH
         BAZ      SWIPEPGS
         LB,R6    UB:ASP,R4         PUT ASP IN INSWAP LIST TOO
         B        PRCAVM1           ITS TEL TO DO SETS
CHKDT3   BAL,2    IPROCS            COUNT UP PROCS
         BAL,2    IOV
         PAGE
*
*  NOW MAKE SURE THE NECESSARY PROCESSORS ARE AVAILABLE
*
PROCAVL  EQU      %
         CI,15    DIC
         BANZ     %+3
         LB,6     UB:ASP,4
         BNEZ     %+2
         LB,6     UB:DB,4
         BAL,1    PRCAV+1
         LB,6     UB:APR,4          MAKE PROC ROOT AVAIL
         BAL,1    PRCAV+1
         LB,6     UB:APO,4          MAKE PROC OVERLAY AVAIL
PRCAVM1  LI,1     SWIPEPGS
PRCAV    EQU      %
         AI,6     0
         BEZ      PRCAV1
*
         MTW,1    C:PROCREQ         COUNT #TIMES PROCESSOR REQUIRED
         MTH,+1   PH:FRQ,R6         BUMP REQUEST COUNT
*
         LOAD,8   PX:HPP,6
         BNEZ     PRCAV1
*
         MTW,1    C:NOPROC          COUNT UP # TIMES NOT AVAIL
*                                                                       01745000
         MTB,1    SB:NP
         LB,7     PB:PSZ,6
         AW,14    7
         LB,7     SB:NP             PUT PROC  INTO INSWAP LIST
         STB,6    SB:PNL,7
PRCAV1   RES      0
         B        0,1
         PAGE
SWIPEPGS EQU      %
*
*  NOW WE KNOW EXACTLY HOW MANY PAGES WE NEED.
*  FIRST WE'LL GET THE FREE ONES, THEN TRY TO SWIPE
*  SOME FROM UNUSED PROCESSORS
*
         STW,14   S:PCT
         AI,14    0
         BE       GOTEXAC
         DISABLE
         LCI      3                 PICK UP ALL FREE PAGES
         LM,1     DOUBLEZERO           (ASSUMES M AND S:FPPH,T,C IN
         LM,5     M:FPPH                SEQUENCE)
         STM,0    M:FPPH
         STM,5    S:FPPH
         ENABLE
         LI,3     PPROCS+1
         SW,R14   S:FPPC            CHECK FOR FIT WITH FREE PAGES
         BLZ      GOTNUF            YES
         BEZ      SWAPIN            EXACTLY EVEN
*        B        PFA               FALL THROUGH TO PFA
         PAGE
*
*        ACCUMULATE A LIST OF ALL FREE, IN-CORE SHARED PROCESSORS
*
*        THE SHARED PROCESSOR TABLE, PX:HPP, WILL BE SCANNED A
*        DOUBLEWORD AT A TIME TO QUICKLY SKIP OVER THE MAJORITY
*        CASE OF PROCESSEORS WHICH ARE NOT IN CORE.  A NON-ZERO
*        DOUBLEWORD LOOK WILL TRIGGER A DETAIL SCAN OF THAT AREA.
*        THOSE IN-CORE PROCESSORS WITH ZERO USE COUNT(PB:UC) WILL
*        BE ADDED TO THE LIST OF ACQUIRABLE PROCESSORS(S:FPL) BY
*        GETPRCPG AND THE TOTAL PAGES WHICH MAY BE ACQUIRED FROM
*        UNUSED SHARED PROCESSORS ACCUMULATED IN S:PRPC.
*        NO PROCESSORS WILL ACTUALLY BE KICKED OUT UNTIL IT
*        HAS BEEN DETERMINED THAT A SUCCESSFUL SWAP MAY BE
*        SCHEDULED.
*
PFA      RES      0
         CI,R3    7-:BIG-:BIG-:BIG-:BIG   :BIG=1 FOR SIGMA9; 0=SIGMA6
         BANZ     PF3A              STAY IN SLO LOOP
         SLS,R3   :BIG-3            ALIGN  FOR FAST LOOP SCAN
PF2A     AI,3     -1                NEXT GROUP
         BGEZ     PF1               THERE IS 1
         B        PF3B              THAT'S ALL, SEE IF SUFFICIENT
PF1      LD,6     PX:HPP,3          4 OR 8 ENTITIES
         BEZ      PF2A              NONE ARE IN
         SLS,R3   3-:BIG            ALIGN FOR SLO LOOP
         AI,R3    7-:BIG-:BIG-:BIG-:BIG
PF3      LOAD,8   PX:HPP,3          IS IT IN CORE
         BEZ      PFA               NOT THIS ONE
         LB,2     PB:UC,3           HOW MANY USERS?
         BNEZ     PFA               AT LEAST 1
         BAL,6    GETPRCPG          LINK IT'S PAGES TO S:FPPH
PF3A     BDR,R3   PF3               NEXT PROCESSOR
PF3B     SW,R14   S:PRPC            SUBTRACT PROCESSOR TOTAL PAGES
         BGZ      USERSOUT          NOT ENOUGH, TRY FOR USERS TOO
GOTNUF   RES      0
         MTW,+1   S:SEVF            BUMP SWAP SET CHANGE COUNTER
         LW,R14   S:ISUN            ANY BODY COMING IN?
         BEZ      SWAPOUT           NO, THEN WE HAVE NO PAGES
         LW,R14   S:PCT             GET PAGE COUNT REQUIRED
         SW,R14   S:OSS             SUBTRACT USER PAGES
         SW,R14   S:FPPC            AND FREE PAGES
         BLZ      RETXCS            RETURN EXCESS IF ANY
         BEZ      DOSWAP            EXACTLY, DOSWAP
         LW,R3    S:FPL             GET COUNT OF PROCSIN LIST
         CI,R3    1                 SKIP SORT IF ONLY ONE
         BLE      KOPROC            YES
SORTL    LI,R8    0                 CLEAR SWITCH FLAG
         LW,R3    S:FPL             GET COUNT
         AI,R3    -1                CORRECT INDEX
SL1      LW,R9    S:FPL,R3          GET AN ELEMENT
         CW,R9    S:FPL+1,R3        CHECK ORDER
         BGE      SL2               OK SKIP SWITCH
         XW,R9    S:FPL+1,R3        XCHANGE
         STW,R9   S:FPL,R3           THEM
         LI,R8    4                 AND SET FLAG TO SAY WE DID
SL2      BDR,R3   SL1               CONTINUE SCAN
         BDR,R8   SORTL             ANOTHER PASS IF ANY SWITCHES
KOPROC   RES      0
*        KICKOUT NECESSARY FREE PROCESSORS
*
         LW,R1    S:FPL             GET COUNT IN LIST
KOPRL    LI,R3    X'FF'             MASK FOR PROCESSOR NUMBER
         AND,R3   S:FPL,R1          GET LOWEST FREQ PROCESSOR
         LOAD,R8  PX:HPP,R3         GET HEAD OF ITS PAGE CHAIN
         LOAD,R7  PX:TPP,R3         GET TAIL OF PROCS CHAIN
         XW,R7    S:FPPT            SWAP WITH SWAPPER CHAIN TAIL
         BNEZ     %+3               GOT SOME ALREADY
         STW,R8   S:FPPH            ELSE ESTABLISH HEAD
         B        %+2
         STORE,R8 MX:PPUT,R7        OTHERWISE CHAIN TO TAIL OF SWAPPER CHAIN
         LB,R7    PB:PSZ,R3         ADD SIZE
         STORE,R0 PX:HPP,R3           (ZAP CHAIN HEAD)
         AWM,R7   S:FPPC            TO PAGE COUNT
         AI,R1    -1                NEXT PROCESSOR
         SW,R14   R7                AND SUBTRACT FROM REQUIREMENT
         BGZ      KOPRL             STILL NEED MORE
         BEZ      DOSWAP            EXACTLY
RETXCS   LW,R5    S:FPPH            GET HEAD OF CHAIN
         DISABLE
         LCW,R6   R14               GET POSITIVE NUMBER TO RETURN
         CW,R6    S:FPPC            AND COMPARE WITH CURRENT FREE POOL
         BLE      RETOK             ALL OK, NOT MORE THAN ARE THERE
         LCW,R14  S:FPPC            TOO MANY, DEFAULT TO ALL
         BEZ      RETXCS1           NONE TO RETURN
         LW,R6    S:FPPC            POSITIVE RETURN COUNT
RETOK    AWM,R14  S:FPPC            DECREMENT SWAPPER PAGE COUNT
         BNE      %+2               SOME LEFT
         STW,R0   S:FPPT            RETURNING ALL, ZAP TAIL
         AWM,R6   M:FPPC            ADD COUNT TO FREE PAGE POOL
         LW,6     M:FPPT
         BNEZ     %+3               DOES FREE PAGE CHAIN EXIST?
         STW,5    M:FPPH            NO, ESTABLISH HEAD
         B        %+4
         STORE,5  MX:PPUT,6         YES, LINK TO TAIL
         B        %+2
         LOAD,5   MX:PPUT,5         FIRST BIR,14 FOR CASE OF 1 PAGE
         BIR,14   %-1
         STW,5    M:FPPT            UPDATE TAIL
         LOAD,6   MX:PPUT,5
         STORE,0  MX:PPUT,5         ZERO TAIL OF CHAIN
         STW,6    S:FPPH            UPDATE SWAPPER HEAD
RETXCS1  RES      0
         ENABLE
DOSWAP   LI,R1    SWAPIN            ASSUME BEST CASE
         LB,R14   SB:OSN            ANY TO OUTSWAP?
         BEZ      %+2               NO
         LI,R1    SWAPOUT           YES
PGCHKM   RES      0
         LI,7     M:FPPH
T:PGCHK  RD,0     0
         BCR,1    0,1
         DISABLE
         STW,R2   TEMP              SAVE R2
         LI,0     -1024       MAX POSSIBLE CHAIN LENGTH
         LW,2     0,7
         BEZ      PPR2
PPR3     BIR,0    %+2
         B        PGSCR             LOOP
         LOAD,11  MX:PPUT,2
         BEZ      PPR2
         LOAD,2   MX:PPUT,2
         AI,11    -NPMC
         BNE      PPR3
PGSCR    SCREECH  X'01'
         PAGE
*S*********************************************************************
*S*                                                                   *
*S*      SCREECH CODE: 01                                             *
*S*      REPORTED BY:  SWPSCHED                                         *
*S*      MESSAGE: PAGE CHAIN INCONSISTENCY                            *
*S*      SIGNIFICANT REGISTERS:                                       *
*S*               R0 - NEGATIVE COUNT OF MAX CHAIN LENGTH REMAINING   *
*S*               R1 - RETURN ADDRESS FOR T:PGCHK                     *
*S*               R2 - CURRENT PAGE NUMBER IN CHAIN                   *
*S*               R7 - ADDRESS OF CHAIN BEING CHECKED(S:FPPH,M:FPPH)  *
*S*              R11 - TEMP FOR CURRENT PAGE BEING CHECKED            *
*S*      REMARKS: THIS CHECK IS ONLY PERFORMED IS SENSE SWITCH        *
*S*               FOUR IS SET AS IT COSTS MUCH CPU TIME.  SINCE       *
*S*               THERE ARE MANY REASONS FOR DECLARING A PAGE         *
*S*               CHAIN INCONSISTENT OR DEFECTIVE, AN INSPECTION      *
*S*               OF THE APPROPRIATE CHAIN WILL BE NECESSARY TO       *
*S*               ASCERTAIN THE DEFECT PRESENT.                       *
*S*               T:PGCHK WILL REJECT A CHAIN IF:                     *
*S*               A.) LINKING FROM HEAD FOR COUNT PAGES DOES NOT      *
*S*                   YIELD TAIL.                                     *
*S*               B.) ANY PAGE IS LESS THAN NPMC (NO PAGE MAP CONST)  *
*S*               C.) THE CHAIN LENGTH EXCEEDS MAXIMUM INDICATING     *
*S*                   A CIRCULAR CHAIN.                               *
*S*********************************************************************
PPR2     CW,2     1,7
         BNE      PGSCR
         AI,0     1024
         CW,0     2,7
         BNE      PGSCR
         LW,R2    TEMP              RESTORE R2
         ENABLE
         B        0,1
GOTEXAC  EQU      SWAPIN
         PAGE
USEROR   BAL,R5   GIVEUP3           GIVEUP AND RETRY
USERSOUT EQU      %
*
*  WE HAVE TO CHOOSE SOME ONE TO GO OUT.
*  WE'LL TRY FOR JUST 1 USER, BUT ALSO FORM A LIST OF
*  A SET TO SWAP OUT IF NECESSARY
*
*
         LW,R7    Y8                SET USEROUT IN PROG FLAG
         STS,R7   S:ISUNF           BIT 0 OF S:SIP
         LW,R13   S:EVF             GET EVENT FLAG COUNTER
         LI,11    0
         LI,7     0
         STB,7    SB:FPN            # FREED
         STB,7    SB:OSN
         STW,7    S:OSS             PGS ACQRD FRM USERS
         LI,9     0                 TOTAL PAGES ACCUMULATED
         LI,15    JIC+RTR
USOUT4   AI,7     1
         LB,3     SB:SWP,7
         CI,3     SEXU              ARE WE AT QUEUES WORTH KEEPING
         BG       USOUT2            NOT YET
         LW,11    S:ISUN            MEAKE 11 NON-ZERO
         BEZ      PROUT3            NO ISUN HAVE ALL OUTSWAP PEOPLE
         AI,3     0                 TEST FOR END
         BE       GIVEUP
*
USOUT2   LB,4     SB:TQ,3           SEARCH Q'S BACKWARD
         BEZ      USOUT4
         CW,R4    S:ISUN            IS HE THE GUY WE ARE BRINGING IN
         BE       GIVEUP
         CH,15    UH:FLG,4
         BANZ     USOUT5
USOUT7   LB,4     UB:BL,4
         B        USOUT2+1
USOUT5   EQU      %                 DON'T SWAP THE ONLY USER WILL
*                                   WILL SCHEDULE
         LH,6     UH:FLG2,4
         CI,6     RTHOLD+CALINT     REAL TIME LOCK IN CORE OR CAL INTERRUPT
         LB,5     UB:MF,4           DOES HE HAVE I/O GOING
         BCS,7    USOUT7            YES, LOCKED OR I/O GOING
         LB,2     UB:US,4
         CI,2     SEXU              IS HE EXECUTABLE
         BG       USOUT9            NO
         LH,5     UH:FLG,4
         CI,5     RTR
         BAZ      USOUT9            NOPE
         CI,6     RMAHOLD           RMA LOCK
         BANZ     USOUT7            YES.. PASS HIM BY.
         CI,6     SQUAN             NOT HAD SWAP QUANTUM
         BAZ      USOUT6            NO.. GIVE HIM A CHANCE
         LW,6     S:ISUNF           YEP. BUT IF WE FAILED ON RT INSWAP
         LB,6     UB:US,6           USER DON'T KEEP HIM IN.
         CI,6     SRT
         BNE      USOUT7
         MTW,0    S:OPC             FAIL SL:OPC TIMES.
         BGEZ     USOUT7
USOUT6   CI,5     BAT               IS HE BATCH.
         BAZ      USOUT9            NO, OK TO SWAP HIM
         LOAD,5   UX:JIT,4          GET HIS JIT
         SLS,5    11                MAKE IT A BYTE ADDRESS
         AI,5     BA(JB:PNR)-BA(JIT)
         LB,5     0,5               GET PARTITION NUMBER
         LH,5     PLH:FLG,5         CHECK FOR HOLD
         BLZ      USOUT7            HE IS HELD. DONT SWAP HIM
USOUT9   EQU      %
         LB,6     SB:OSUL           DONT EXCEED TABLE
         CI,6     SMAXOUT-1         SIZE
         BGE      USOUT7
         LOAD,6   UX:JIT,4          PHYS JIT ADR TO FIND
         SLS,6    11                 HOW MANY PAGES HE HAS
         AI,6     JBPPC
         LB,8     0,6               USER PAGE COUNT
         AW,9     8                 ADD TO TOTAL PAGES ACCUM.
         AWM,R8   S:OSS             ACCUMULATE TOTAL OF USER PAGES
         MTB,1    SB:OSN
         LB,2     SB:OSN
         STB,4    SB:OSUL,2         ADD HIM TO THE OUS SWAP LIST
         LI,2     ADDPRC            NOW LETS GET ANY PROCESSORS
         BAL,3    DOV               THAT COME FREE BECOUSE
         LI,3     USOUT10           THIS GUY IS GETTING SWAPPED
         LH,15    UH:FLG,4
         CI,15    TIC
         BANZ     DTEL
         LI,3     %+4
         CI,15    DIC
         BANZ     DDB
         B        DASP
         LB,1     UB:APR,4
         BAL,3    CHKPRC
         LB,1     UB:APO,4
         BAL,3    CHKPRC
USOUT10  EQU      %
         LI,15    JIC+RTR
         LW,6     S:ISUN
         BEZ      USOUT7
         CW,9     14
         BL       USOUT7            NOT ENOUGH PAGES YET
         B        PROUT2
CHKPRC   EQU      %
         MTB,-1   PB:UC,1           COUNT IT DOWN
ADDPRC   BNEZ     0,3               NOT 0 YET
         CW,9     14
         BGE      0,3
         LOAD,6   PX:HPP,1
         BEZ      0,3               NOT IN CORE
         LB,6     PB:PSZ,1          # PAGE
         BEZ      0,3               NONE
         AW,9     6                 TOTAL PAGES ACCUM.
         MTB,1    SB:FPN            ADD TO FREE PROC LIST
         LB,6     SB:FPN
         STB,1    SB:FPN,6
         B        0,3
         PAGE
*
*  DECREMENT ANY ASSOCIATED PROCESSORS FOR USER LIST
*  IF WE HAVE ENOUGH PAGE SWAP THE LIST.
*  IF NOT, SEE IF THE USERS WE CHOSE MADE ANY PROCESSORS FREE
*  IF SO, TAKE THEIR PAGES
*  IF NOT GIVE UP. WE CANT FIND A SWAP SET
*
PROUT2   EQU      %
         CW,R13   S:EVF             TEST FOR ANY EVENTS
         BNE      USEROR            YES
         LB,5     SB:FPN
         BEZ      PROUT3
         LB,3     SB:FPL,5
         BAL,6    GETPRCPG          GET THEIR PAGES
         BDR,5    %-2
PROUT3   EQU      %
         LB,7     SB:OSN
         BEZ      SSEXIT
         LI,11    PROUT5
PROUT4   RES      0
         LI,6     E:KO
         LB,5     SB:OSUL,7         KICK THE USERS OUT
         B        T:RUE             REPORT KICKOUT EVENT ON THIS USR
PROUT5   BDR,7    PROUT4
         STB,R7   S:ISUNF           CLEAR USEROUT FLAG
*                                   (R7 CONTAINS ZERO FROM BDR)
         B        GOTNUF            GO TO RETURN EXCESS AND PERFORM SWAP
GIVEUP   EQU      %
         MTW,-1   S:OPC             DECREMENT CTR OF CONSECUTIVE
*                                   SWAP SCHEDULE FAILURES
         MTW,1    C:NSP             COUNT NO SWAP BY ANY MEANS
         LCI      3                 CANT SWAP =
         LM,5     S:FPPH
         AI,6     0
         BEZ      GIVEUP6
         DISABLE
         LW,4     M:FPPT            ANY IN CHAIN?
         BEZ      GIVEUP5           NO
         STORE,5  MX:PPUT,4         HEAD->TAIL
         STW,6    M:FPPT            TAIL -> TAIL
         AWM,7    M:FPPC            COUNT -> COUNT
         B        GIVEUP6
GIVEUP5  EQU      %
         LCI      3
         STM,5    M:FPPH            ALL BACK AT ONCE
GIVEUP6  EQU      %
         ENABLE
         LI,5     GIVEUP4
GIVEUP3  EQU      %
         LB,7     SB:OSN
         BEZ      0,5
         LB,4     SB:OSUL,7         INCREMENT THEIR PROCESSORS SINCE
         BAL,2    ITORP
         BAL,2    IOV
         BDR,7    %-3
         B        0,5
GIVEUP4  EQU      %
         LW,4     S:ISUN
         STW,R4   S:ISUNF           SAVE FAILING USER
         LW,R15   S:SEVF            AND SWAP SET COUNTER
         STW,R15  S:FSEVF           TO DEFINE FAILURE
         LH,15    UH:FLG,4
         CI,15    JIC
         BANZ     GIVEUP1
         BAL,2    DOV
         BAL,R2   DTORP             DECREMENT TEL OR PROCESSORS
GIVEUP1  EQU      %
         LI,R6    GIVEUPA           SET RETURN
GIVEUP7  BAL,R7   T:TOTSZ           COMPUTE USER SWAP SIZE
         AI,R2    -6                ACCOUNT FOR MONITOR OVERLAY
         CW,R2    S:PCORE           CHECK FOR FIT EVER
         BLE      GIVEUP8           YES
         SCREECH  X'62'             NO- IMPOSSIBLE SWAP
         PAGE
*S*********************************************************************
*S*                                                                   *
*S*      SCREECH CODE: 62                                             *
*S*      REPORTED BY: SWPSCHED                                         *
*S*      MESSAGE: IMPOSSIBLE SWAP CONDITION                           *
*S*      SIGNIFICANT REGISTERS:                                       *
*S*               R0 = USER SIZE IN PAGES LESS LOCKED PROCESSORS      *
*S*               R2 = GROSS USER SIZE IN PAGES                       *
*S*               R4 = USER NUMBER                                    *
*S*               R6 = RETURN ADDRESS FOR GIVEUP7                     *
*S*              R15 = USER FLAGS (UH:FLG)                            *
*S*      REMARKS: THE USER CHOSEN TO INSWAP WILL NEVER FIT INTO       *
*S*               AVAILABLE PHYSICAL MEMORY.  THIS MAY BE DUE TO      *
*S*               EITHER AN OPERATING SYSTEM SOFTWARE PROBLEM OR     *
*S*               ABORTING WITHOUT FIRST RELEASING LOCK.             *
*S*********************************************************************
GIVEUP8  RES      0
         DISABLE                               *** INHIBIT ***
         LW,R7    S:ACORE           GET AVAIL FLUID PAGES
         SW,R7    S:RTCORE          SUBTRACT RTHOLD PAGES
         LW,R5    S:STL#            NUMBER OF STOLEN PAGES
         SW,R5    SL:RSVP           RESERVED PAGES
         BGEZ     %+2               ALL ARE RESERVED
         LI,R5    0                 SET RESERVED CONTRIBUTION TO ZERO
         LH,R3    UH:FLG2,R4        GET SECOND FLAGS FOR USER
         CI,R3    RTHOLD            CHECK FOR REAL TIME LOCK IN CORE
         BAZ      %+2               NO
         LW,R0    S:PCT             YES, USE SWAPPER REQUIRMENT
         SW,R0    R7                FIND REQUIREMENT DEFICIT
         BLEZ     1,R6              HE FITS, RETURN SKIPPING
         CW,R0    R5                CAN IT BE SATISFIED BY STOLEN
         BG       GIVEUP9           NO, JUST SET BYPASS FLAG
         LCW,0    0                 INVERT
         CW,0     S:STLC            REMEMBER MOST NEGATIVE
         BGE      %+2
         STW,0    S:STLC
GIVEUP9  RES      0
         ENABLE
         B        0,R6              NORMAL RETURN
GIVEUPB  RES      0
         LH,7     UH:FLG,4          GET FLAGS
         OR,7     X8000             SET BYPASS BIT
         STH,7    UH:FLG,4          PUT AWAY
         B        SSEXIT            EXIT SWAP SCHEDULER
GIVEUPA  B        GIVEUPB
SSEXIT   LI,0     0
         STB,R0   S:ISUNF           CLEAR USEROUT FLAG
         STW,0    S:SIP             RESET THE SWAP-IN-PROGRESS FLAG
         ENABLE
         B        T:SEXIT
         PAGE
GETPRCPG EQU      %
*
*        ADD PROCESSOR PAGES TO CURRENT TOTAL AND PLACE PROCESSOR
*        IN LIST OF PROCESSORS TO BE FREED.
*
*        INPUT :  R3 - PROCESSOR NUMBER
*        OUTPUT:  S:FPL = S:FPL+1
*                 S:FPL(S:FPL(0)) = PH:FRQ(R3),0,R3
*                 S:PRPC = S:PRPC+PB:PSZ(R3)
*        SCRATCH: R7,R8,BITS 0-23 OF R3
*        CALL:    BAL,R6 GETPRCPG
*                 NORMAL RETURN ONLY
*
*
         MTW,+1   S:FPL             BUMP COUNT OF PROCS IN LIST
         LW,R7    S:FPL             GET COUNT
         LH,R8    PH:FRQ,R3         GET PROCESSOR FREQ
         STH,R8   R3                MERGE WITH PROC NUMBER
         STW,R3   S:FPL,R7          ADD TO LIST
         AND,R3   M8                SCRUB BACK TO PROCESSOR NUMBER
         LB,R7    PB:PSZ,R3         GET SIZE OF PROCESSOR
         AWM,R7   S:PRPC            ADD TO PAGE TOTAL
         B        0,R6              RETURN
         END

