Google
 

Trailing-Edge - PDP-10 Archives - de-10-omona-v-mc9 - segcon.mac
There are 12 other files named segcon.mac in the archive. Click here to see a list.
TITLE	SEGCON - HIGH SEGMENT CONTROL FOR REENTRANT USER PROGRAMMING V6746
SUBTTL	T. HASTINGS/TW/TNM/TH/AF/CHW/TW/PFC/DAL  28 NOV 78
	SEARCH	F,S
	$RELOC
	$HIGH



;THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY ONLY BE USED
;  OR COPIED IN ACCORDANCE WITH THE TERMS OF SUCH LICENSE.
;
;COPYRIGHT (C) 1973,1974,1975,1976,1977,1978 BY DIGITAL EQUIPMENT CORP., MAYNARD, MASS.
XP VSEGCN,6746
		;PUT VERSION NUMBER IN GLOB LISTING AND LOADER MAP

;ALL OF THE CODE DEALING WITH THE SECOND RELOCATION REGISTER HAS BEEN ISOLATED
;IN THIS ONE ROUTINE NAMED SEGCON

;SIX OF THE MONITOR MODULES CALL SUBROUTINES IN SEGCON WITH PUSHJ'S.
;THIS HAS BEEN DONE FOR TWO REASONS:
;	1.TO MAKE IT EASY TO BUILD SYSTEMS WITH OR WITHOUT 2 REG. CAPABILITY
;	SEGCON CAN BE REPLACED BY A SHORT DUMMY ROUTINE(NULSEG), SO THERE
;	IS NO CONDITIONAL ASSEMBLY REQUIRED TO ELIMINATE MOST OF THE CODE.
;	THOSE CUSTOMERS WHO WOULD LIKE TO ELIMINATE ALL OF THE CODE MAY DO SO BY
;	REASSEMBLING THE MONITOR WITH FEATURE SWITCH FT2REL=0.
;	THIS WILL ELIMINATE ALL PUSHJ CALLS TO SEGCON

;	2. TO MAKE IT CLEAR EXACTLY WHERE TWO SEGMENTS ARE DEALT WITH


;ALL OF THE CALLS TO SEGCON ARE OF THE FORM:
;IFN FT2REL,<
;	EXTERN	XXXXXX
;	PUSHJ	P,XXXXXX
;	ERROR RETURN OR NOTHING TO DO
;	OK RETURN
;>

;OR:
;IFN FT2REL,<
;	EXTERN	XXXXXX
;	PUSHJ	P,XXXXXX
;	OK RETURN
;>

;SEGCON IS GOOD FOR SWAPPING AND NON-SWAPPING SYSTEMS
;AND FOR SYSTEMS WITH AND WITHOUT DISK



;TO MAKE IT CLEAR WHERE EACH SUBROUTINE IS CALLED FROM
;AND WHAT FUNCTION IT IS PERFORMING, THE MODULES OF THE MONITOR HAVE BEEN REPLICATED
;IN SEGCON USING SUBTTL.  THE MODULES ARE:
;	1. CLOCK
;	2. COMCSS
;	3. CORE1
;	4. SAVGET
;	5. SWAP
;	6. UUOCON
     
;ADDITIONAL MONITOR DATA TO HANDLE TWO SEGMENT USER PROGRAMS:

;A NUMBER OF THE ALREADY EXISTING JOB TABLES HAVE BEEN LENGTHENED
;BY AN AMOUNT SEGN(THE NUMBER OF POSSIBLE HIGH SEGMENTS).  THE LOWER
;PART OT THESE TABLES HAVE REMAINED AS THEY WERE(O THRU JOBMAX=JOBN-1)
;WITH ONE WORD PER JOB( OR LOW SEGMENT).  THE NEW HALF(GREATER THAN JOBMAX)
;HAVE ONE WORD FOR EACH HIGH SEGMENT.  A HIGH SEGMENT NUMBER IS REALLY
;A LARGE JOB NUMBER AND IS AN INDEX INTO THE JOB TABLES.
;THE HIGHEST INDEX IN THESE LENGTHENED JOB TABLES IS JBTMAX.
;THERE IS AT LEAST ENOUGH TABLE SPACE SO THAT EACH JOB CAN HAVE A
;DIFFERENT HIGH SEGMENT.  THE FOLLOWING TABLES HAVE BEEN LEGNTHENED:

;JBTSTS- JOB(SEG) STATUS
;	LH=STATUS BITS:
;		SWP=1 IF HIGH SEG SWAPPED OUT(WHETHER SPACE ON DISK OR NONE)
;			OR ON WAY IN OR OUT.  IE SWP=1 MEANS NOT RUNABLE COR IN CORE.
;			SWP=0 WHEN HIGH SEG HAS RUNABLE COR IN CORE
;			EVEN THOUGH ALSO HAS SPACE ONDISK.

;		SNA=1 IF HIGH SEG NO. APPEARS IN AT LEAST ONE USER'S LOGICAL ADR. SPACE
;			SNA IS SIGN BIT OF JBTSTS.
;		SNA=0 IF HIGH SEG IS COMPLETELY FREE OR IS DORMANT,
;		WHERE DORMANT MEANS THAT THE SEGMENT DOES NOT APPEAR IN ANY USER'S
;		  LOGINCAL ADDRESSING SPACE, BUT IS STILL AROUND PHYSICALLY ON DISK
;		  OR IN CORE
;		SHRSEG=1 IF SEG IS SHARABLE(WHETHER IT STILL HAS A NAME OR
;			HAS BEEN SUPERCEDED).
;	BITS 9-17 ARE ACCESS PRIBILEGES - SAME FORMAT AS DISK FILES
;		HSAPOS=RIGHT MOST BIT POSITION
;		HSAMSK=MASK FOR BITS(ALL ONES)
;		HSASIZ=SIZE OF FIELD
;	RH=IN-CORE COUNT(RATHER THAN RUN TIME LEFT) IE NUMBER OF JOBS
;		IN CORE USING HIGH SEG IN CORE
     
;JBTADR- HIGH JOB(SEG) CORE ASSIGNMENT
;	LH=LENGTH-1 OF HIGH SEG(NOT PROTECTION LIKE LOW SEG)
;		FOR LOW SEGS PROTECTION(HIGHEST LEGAL USER ADDRESS) AND LENGTH-1
;		ARE THE SAME. HOWEVER THEY ARE DIFFERENT FOR HIGH SEGS, SO LENGTH-1
;		WAS CHOSEN INSTEAD OF THE PROTECTION AS THE QUANTITY FOR THE LH.
;	RH=ABSOLUTE ADR. OF BEG OF HIGH SEG(NOT RELOCATION LIKE LOW SEG)
;		FOR LOW SEGS THE RELOACTION AND THE ABSOLUTE ORIGIN ARE THE SAME.
;		HOWEVER THEY ARE DIFFERENT FOR HIGH SEGS, SO THE ABSOLUTE ORIGIN WAS
;		CHOSEN INSTEAD OF THE RELOCATION AS THE QUANTITY FOR THE RH.
;	0 MEANS NO CORE IN MEMORY AT ALL
;	CORE IS ASSIGNED DURING SWAP IN AND OUT TOO
;JBTSWP- JOB SWAP INFO
;	LH=DISK ADR. ALWAYS(NEVER CORE PROTECT TIME LIKE LOW SEG WHEN IN CORE)
;	RH=IMGIN,IMGOUT - NO. OF 1K BLOCKS WHEN BROUGHT IN, NO. OF 1K BLOCKS ON DISK
;	IMGOUT=0 WHEN NO COPY ON DISK(ALWAYS TRUE IF UWP IS OFF
;		FOR HIGH SEG SO USER CAN MODIFY HIGH SEG)
;JBTCHK- CHECKSUM FOR HIGH SEG WHEN SWAPPED OUT
     
;THE FOLLOWING JOB TABLES HAVE BEEN ADDED:
;JBTSGN(0...JOBMAX) - HIGH SEG NUMBER WHICH JOB IS USING IN CORE OR ON DISK
;	0 MEANS JOB DOES NOT HAVE A HIGH SEGMENT.
;		LH BITS:
;	SPYSEG=1(SIGN BIT) HIGH SEG IS PART OF PHYSICAL CORE(SPY UUO),
;	 RH IS HIGHEST PHYSICAL ADR FOR THIS USER TO LOOK AT
;	SERSEG=1 MEANS THAT THE HIGH SEG WHICH THIS USER IS USING IS SHARABLE
;	 THIS BIT ALSO APPEARS IN THE JBTSTS WORD IN THE SAME POSITION
;	 IN THE WORD, SINCE SHARABLE IS A PROPERTY OF A SEGMENT, NOT OF
;	 THE JOB USING THE SEGMENT.  IT IS DUPLICATED HERE ONLY AS
;	 A CONVENIENCE SO IT WILL APPEAR IN THE LH OF AC ITEM.
;	UWPOFF=1 USER WRITE PROTECT IS OFF FOR THIS USER(NORMALLY 
;	  UWP OS ON, SO UWPOFF=0)
;	 MEDDLE=1 IF USER HAS MEDDLED WITH PROGRAM SO HE SHOULD NOT BE ALLOWED
;		TO TURN OFF UWP OR CHANGE CORE ASSIGNMENT
;		MEDDLE IS SET ONLY IF HIGH SEG IS SHARABLE OR A SPY SEG
;		MEDDLING IS DEFINED AS ANYTHING WHICH CAUSES THE PROGRAM NOT
;		NOT TO BE IN CONTROL OF ITSELF:
;		1. START COMMAND WITH ARG
;		2. DEPOSIT COMMAND(D) IN LOW SEG(HIGH SEG ALWAYS ILLEGAL)
;		3. RUN UUO WITH BIGGER START ADR. INCREMENT THAN 0 OR 1
;		4. GETSEG UUO
;		5. HIGH SEG IS PHYSICAL CORE(SPY UUO)
;CORCNT=1 IF HIGH SEG IN CORE COUNT HAS BEEN INCREMENTED FOR THIS JOB
;		 (JOB LOW SEG MAY OR MAY NOT BE IN CORE)
;		 SET BY INCCNT ROUTINE, CLEARED BY DECCNT ROUTINE
;JBTDEV(JOBN...JBTMAX) - HIGH SEG DEVICE NAME OF FILE STRUCTURE NAME
;			(EG DSKA,DSKB,DTAN,MTAN)
;JBTPPN(JOBN...JBTMAX) - HIGH SEG DIRECTORY NAME(EXTENSION OF JBTPPN TABLE)
;	IS PROJECT-PROGRAMMER IF DISK, 0 IF DTA OR MTA
;JBTNAM(JOBN...JBTMAX) - HIGH SEG NAME(EXTENSION ASSUMED TO BE SHR)
;	(EXTENSION OF JBTPRG TABLE)
;	THE NAME(AND DISK COPY) IS RETAINED EVEN AFTER NO JOBAS ARE USING
;		HIGH SEG IN CORE OR ON DISK, THUS SAVING TIME FOR GET'S.
;		HOWEVER, IF SWAPPING SPACE BECOMES FULL, THESE UNUSED HIGH SEGS
;		ARE DELETED ONE AT A TIME, UNTIL THERE IS ROOM. IF STILL NO ROOM,
;		COPIES OF HIGH SEGS WHICH ARE ALSO IN CORE ARE DELETED.
;		ANY HIGH SEG WHICH HAS A NAME IS SHARABLE AND HAS SHRSEG SET IN JBTSTS.
;		A HIGH SEG WITH SEGSHR SET AND JBTNAM=0 IS A HIGH SEG WHICH HAS BEEN
;		SUPERCEDED BY A LATER VERSION OF A HIGH FILE.
     
;THE FOLLOWING LOCATION HAS BEEN ADDED TO THE JOB DATA AREA:

;JOBHRL - RH IS ANALOGOUS TO JOBREL, HIGHEST RELATIVE(USER) ADR. IN HIGH SEG
;	IE HIGHEST LEGAL USER ADDRESS IN HIGH SEG
;	0 MEANS JOB DOES NOT HAVE A HIGH SEG
;	SET BY MONITOR EVERY TIME JOB RUNS
;	LH IS ANALOGOUS TO LH OF JOBSA, FIRST FREE(RELATIVE) LOCATION IN HIGH SEG
;	SET BY LINKING LOADER, USED ON SAVE FOR AMOUNT TO WRITE
;	THE FIRST FREE LOCATION CONTAINS THE STARTING ADDRESS OF JOB
;	WHICH IS SET BY SAVE FROM JOBSA WHICH WAS SET BY LINKING LOADER
;	BOTH HALVES ARE 0 IF NO HIGH SEG

;JOBCOR - LH IS HIGHEST LOC LOADED WITH DATA IN LOW SEG
;	SET BY LINKING LOADER
;	USED BY SAVE FOR 2 SEG PROGRAM ONLY, WRITE LOW SEG IF LH JOBCOR IS
;	GREATER THAN 137(HIGHEST LOC IN JOB DATA AREA

;THE HIGH SEGMENT HAS A VERY SMALL JOB DATA AREA(10 LOCATIONS)
;WHICH IS USED FOR INITIALIZING THE REGULAR JOB DATA AREA ON A GET
;IN CASE THE JOB DOES NOT HAVE A LOW SEGMENT FILE(HIGHLY DESIRABLE)
;SEE JOBDAT FOR DEFINITIONS.

;THE CONTENTS OF AC J MAY BE A JOB NUMBER OR A HIGH SEGMENT NUMBER
;AC J IS LOADED FROM JBTSGN INDEXED BY JOB NUMBER
;SO THAT AC J CONTAINS LH BITS FOR EASY TESTING WHEN THE RH
;HAS A HIGH EGMENT NUMBER IN IT.  WHEN AC J HAS A JOB NUMBER
;IN IT, THE LH IS ALWAYS 0
;IN SWAP, AC J IS DEFINED TO BE AC J
;HOWEVER, A COMPARE WITH JOBMAX WILL STILL GIVE HIGH SEG OR LOW SEG
;NOTE, THAT SPYSEG IS SIGN BIT SO 'SKIPG' TEST FOR REAL HIGH SEG


;TERMINOLOGY IN THE COMMENTS OF SEGCON:
;'SEG' IS AN ABREVIATION FOR 'SEGMENT'
;'JOB' OR 'JOB NUMBER' ALWAYS REFERS TO NUBERS LESS-THAN OR EQUAL-TO JOBMAX
;THE COMMENTS IN THE REST OF THE MONITOR HAVE NOT BEEN CHANGED, SO THAT THERE ARE
;MANY ROUTINES WHICH ARE NOW DEALING WITH BOTH JOB NUMBERS AND HIG SEG NUMBERS
;(MAINLY IN CORE AND SWAP ROUTINES) WITHOUT KNOWING IT.
;'SEGMENT' REFERS TO BOTH LOW AND HIGH SGMENTS, RATHER THAN JUST HIGH SEGMENTS
;AND WILL ALWAYS BE PRECEDED BY 'LOW' OR 'HIGH' IF BOTH IS NOT INTENDED.
;'JOB NUMBER' IS USED INTERCHANGEABLE WITH 'LOW SEG NUMBER'.

;'ADDRESS','ADR','LOCATION','LOC', AND 'ORIGIN' ALL
;REFER TO USER(IE BEFORE HARDWARE OR SOFTWARE RELOCATION) QUANTITIES.
;MOST OF THE TIME THESE WORDS ARE MODIFIED WITH 'USER' OR 'RELATIVE'.
;HOWEVER, THIS IS NOT COMPLETELY CONSISTENT.
;OCCASIONALLY ABSOLUTE ADDRESSES ARE COMPUTED BY THE MONITOR. IN ALL THESE
;CASES, 'ABSOLUTE' OR 'ABS' PRECEEDS THESE WORDS.
;THE REENTRANT CAPABILITY IMPROVES SYSTEM THRUPUT BY REDUCING THE DEMANDS ON:
;	1. CORE MEMORY - SHARING
;	2. SWAPPING STORAGE - SHARING
;	3. SWAPPING CHANNEL READS
;	4. SWAPPING CHANNEL WRITES
;	5. STORAGE CHANNEL READS - GET
;HOWEVER, 2 COMPETES WITH 4 AND 5, IN THAT TO REDUCE THE DEMANDS 4 AND 5,
;COPIES OF UNUSED (DORMANT) HIGH SEGMENTS ARE KEPT ON THE SWAPPING DEVICE, THEREBY
;INCREASING THE DEMAND FOR SWAPPING STORAGE(2).  THE QUESTION NATURALLY ARISES,
;HOW DOES THE SYSTEM DETERMINE THIS SPACE-TIME TRADEOFF?
;THE MONITOR ACHIEVES THE BALANCE DYNAMICALLY, AFTER THE OPERATOR
;ESTABLISHED THE SIZE OF THE SWAPPING SPACE WHEN THE SYSTEM IS STARTED
;(ONCE ONLY DIALOG). THE MONITOR ASSUMES THAT THERE IS NO SHORTAGE
;OF SWAPPING SPACE, AND SO KEEPS A SINGLE COPY OF AS MANY HIGH SHARABLE
;SEGMENTS IN THE SWAPPING SPACE AS THERE ARE HIGH SEG NUMBERS.
;(THE MAX NUMBER OF HIGH SEG NUMBERS IS ESTABLISHED AT BUILD TIME
;AND IS EQUAL TO OR GREATER THAN THE NUMBER OF JOBS(COUNTING THE NULL JOB).
;THUS DEMANDS 4 AND 5 ARE MINIMIZED.
;HOWEVER IF SWAPING SPACE IS EXHAUSTED, THE MONITOR WILL TRY DELETING
;A HIGH SHARABLE SEG THAT IS NOT IN USE BY ANY JOB.(A SO-CALLED DORMANT SEG)
;WHEN DELETED SEG IS NEXT NEEDED(A GET OCCURRED), IT WILL
;BE GOTTEN FROM STORAGE DEVICE, INCREASING DEMAND 5.  IF NO UNUSED HIGH SEGS EXIST
;THE MONITOR WILL TRY DELETING A HIGH(SHARABLE OR NON-SHARABLE) SEG THAT
;IS IN USE AND HAS A COPY, BOTH ON THE DISK AND IN CORE.  THUS WHEN THIS HIGH SEG IS
;SWAPPED OUT, IT WILL REQUIRE A WRITE, INCREASING DEMAND 4.

;TO RESTATE: THE MONITOR USES ALL THE SWAPPING SPACE AVAILABLE. IF IT RUNS OUT,
;IT INCREASES STORAGE CHANNEL READS(GET), THEN SWAPPING CHANNEL WRITES IN ORDER
;TO OVERCOME THE SPACE SHORTAGE.

;THE SAME LOOK-A-SIDE TECHNIQUE IS USED FOR CORE IN BOTH SWAPPING AND NON-SWAPPING
; SYSTEMS IN THAT THE MONITOR KEEPS DORMANT SEGMENTS IN CORE
;UNTIL THEY ARE NEEDED OR CORE DEMAND REQUIRES THAT THEY BE RETURNED
;THERE IS A THIRD CLASS OF SEGMENTS, CALLED 'IDLE'.
;AN IDLE SEGMENT IS A SHARABLE SEG WHICH IS IN CORE, BUT NO JOB IN CORE
;IS USING IT, BUT SOMEONE ON DISK IS.  IT WILL BE DELETED IF CORE IS NEEDED
;THE 3 CLASSES OF SEGMENTS ARE ACTIVE,IDLE,DORMANT.
;LOCATION CORTAL CONTAINS THE SUM OF FREE BLOCKS+DORMANT BLOCKS
;HOWEVER CORTAB CONTAINS 0 BITS ONLY FOR FREE BLOCKS
;TO FREE STORAGE
;THE CORE COMMAND WITH NO ARG STILL PRINTS CORE AS IF NO DORMANT SEGS IN CORE

;VIRTAL CONTAINS THH AMOUNT OF FREE VIRTUAL CORE LEFT IN SYSTEM
;INCLUDING HIGH SEGMENTS.  SINCE THIS QUANTITY IS USED TO PREVENT
;THE CORE ROUTINES FROM OVER COMMITTING SWAP SPACE. SHARED SEG ARE ONLY
;COUNTED ONCE, AND DORMANT SEGMENTS ARE NOT INCLUDED.  THUS VIRTAL IS
;DECREMENTED WHEN THE FIRST USER SHARES A SEGMENT CHANGING IT FROM DORMANT
;TO ACTIVE
     
	ENTRY	SEGCON

SEGCON::	;ENTRY POINT SYMBOL TO CAUSE LOADING OF SEGCON
		;(AS OPPOSED TO NULSEG OR NEITHER)


SUBTTL	ONCE - ONCE ONLY STARTUP CODE

;LOCATION 'TWOREG' IS A FLAG WHICH IS TESTED AT POINTS WHERE A HIGH
;SEGMENT CAN BE CREATED, TO SEE IF BOTH HARDWARE AND SOFTWARE HAVE TWO
;REGISTER CAPABILITY

;INITIALIZE LOC TWOREG TO 0 IF SOFTWARE OR HARDWARE DOES NOT HAVE 2 SEGMENT
;CAPABILTIY.  SET TWOREG TO -1 IF BOTH DO HAVE 2 SEGMENT CAPABILITY.
;SYMBOL AND2RG IS ANDDED TO 'TWOREG' FROM ONCE ONLY CODE AFTER THE HARDWARE IS
;EXAMINED AND TWOREG HAS BEEN SET TO 0 OR -1 ACCORDINGLY.


AND2RG=:-1	;AND WITH -1, IE LEAVE 'TWOREG' SET TO 0 OR -1 ACCORDING TO THE
			;CAPABILITIES OF THE HARDWARE.
     
SUBTTL	COMCSS - COMMAND DECODER SUBROUTINES

;ROUTINE TO STOP NEW USERS FROM SHARAING EXISTING SEGMENTS ON DECTAPE
;AND MAGTAPE WHICH HAVE BEEN JUST ASSIGNED, SINCE THE NEW TAPE MAY HAVE
;A LATER VERSION OF THE FILE WITH THE SAME NAME AS THE SEGMENT.
;REMSEG - DELETE ALL SEGS OF DISK FILE STRUCTURE BEING REMOVED 
;CALLED FROM ASSIGN,DEASG(DEASSIGN,FINISH,KJOB) CONSOLE COMMAND. REMSTRR UUO
;CALL:	MOVE F,DEVICE DATA BLOCK ADDRESS OR STR DATA BLOCK ADR.
;	MOVE T2,DEVMOD(F)	;DEVICE CHARACTERISTICS
;	PUSHJ P,ASGHGH
;	ALWAYS RETURN, C(T2) DESTROYED


ASGHGH::TLNN	T2,DVDTA!DVMTA	;IS THIS DEVICE JUST ASSIGNED A DTA OR MTA?
	POPJ	P,		;NO, NO CONFLICT POSSIBLE
REMSEG::PUSH	P,J		;YES, SAVE JOB NUMBER
ASG1:	MOVE	J,SEGPTR##	;SCAN ALL HIGH SEGMENTS
ASG2:	MOVE	T1,DEVNAM(F)	;GET DEVICE NAME(PHYSICAL)
	CAME	T1,JBTDEV##(J)	;DOES IT MATCH DEVICE NAME OF SEG?
ASG3:	AOBJN	J,ASG2		;NO, KEEP LOOKING, FINISHED?
	JUMPG	J,IPOPJ##	;RETURN IF FINISHED AND RESTORE JOB NUMBER
	TLZ	J,-1		;CLEAR OUT LH BITS OF HIGH SEG NO.
	PUSHJ	P,CLRNAM	;NOT FINISHED, CLEAR SEG NAME
				;AND RETURN DISK SPACE IF NOT IN USE
	JRST	ASG1		;CONTINUE SCAN, RESTORE T1 TO DEVICE NAME


;ROUTINE TO PRINT SIZE OF HIGH SEG LOGICAL CORE
;CALL:	MOVE J,JOB NO.
;	PUSHJ P,PRTHGH
;	ALWAYS RETURN WITH J RESTORED
;PRINTS +N(UNLESS NOT TWO REG HARDWARE, SO USERS CAN TELL IF 2 REG. HARD.)


PRTHGH::SKIPN	TWOREG##		;2 REG. MACHINE?
	POPJ	P,		;NO, DO NOT PRINT EVEN +0
	PUSHJ	P,PRTPLS##	;YES, PRINT +
	PUSH	P,J		;SAVE JOB NUMBER
	SKIPG	J,JBTSGN##(J)	;DOES JOB HAVE A REAL HIGH SEG?
	MOVEI	J,0		;NO, MAKE SURE NO HIGH SEG(IN CASE SPY SEG)
	PUSHJ	P,PRTSEG##	;PRINT SIZE OF LOGICAL ADR. HIGH SEG
	JRST	IPOPJ##		;RESTORE JOB NUMBER AND RETURN
     
;ROUTINE TO CHECK TO SEE IF USER IS MEDDLING WITH A SHARABE PROGRAM
; IF YES, TURN UWPOFF OFF SO UWP(USER MODE WRITE PROTECT) WILL BE ON
; AND SET MEDDLE, SO HE CANNOT TURN UWP BACK OFF AND CANNOT CHANGE CORE
; ASSIGNMENT OF HIGH SEG(EXCEPT TO REMOVE IT COMPLETELY)
;CALL:	MOVE J,JOB NO.
;	PUSHJ P,CHKMED
;	ALWAYS RETURN, J RESTORED AND T2 PRESERVED(T1 USED)
;CALLED FROM DEPOSIT, START N, RUN UUO, GETSEG UUO
;T2 PRESERVED


CHKMED::SKIPLE	T1,JBTSGN##(J)	;DOES JOB HAVE A REAL HIGH SEG?
	TLNN	T1,SHRSEG	;YES, IS IT SHARABLE?
	POPJ	P,		;NO(NONE OR SPY SEG(MEDDLE ALREADY ON))
	TLO	T1,MEDDLE	;YES, FLAG TIS USER AS MEDDLING
	TLZ	T1,UWPOFF	;AND FORCE UWP ON IN HARDWARE SO WRITES TRAP
	MOVEM	T1,JBTSGN##(J)	;NEXT TIME USER RUNS
	POPJ	P,
     

;ROUTINE TO COMPUTE NO. OF K USED BY A JOB
;BOTH LOW AND HIGH SEGMENTS
;CALL:	MOVE J, JOB NO.
;	PUSHJ P,JOBSIZ
;	RETURN WITH T2=SIZE INK, J PRESERVED


JOBSIZ::PUSHJ	P,SEGSIZ##	;SIZE OF LOW SEG
	MOVE	T1,T2		;SAVE IN T1
	PUSH	P,J		;SAVE LOW SEG NUMBER
	SKIPG	J,JBTSGN##(J)	;DOES JOB HAVE REAL HIGH SEG?
	JRST	JOBSZ1		;NO, RESTORE JOB NUMBER
	PUSHJ	P,SEGSIZ##	;YES, GET SIZE OF HIGH SEG
	ADD	T2,T1		;ADD LOW SIZE TO HIGH SIZE
JOBSZ1:
IFN FTKI10!FTKL10,<
	ADDI	T2,UPMPSZ##	;ROUND UP TO NEXT ONE K BOUNDARY
	LSH	T2,P2KLSH##	;CONVERT PAGES TO K
>
	JRST	IPOPJ##		;RESTORE JOB NO. AND RETURN
     
SUBTTL CORE1 - LOGICAL AND PHYSICAL CORE ALLOCATION

;ROUTINE TO CHECK FOR SAVE IN PROGRESS FROM A HIGH SEG
;ABOUT TO BE SHUFFLED OR SWAPPED, OR CORE REASSIGNED
;CALL:	MOVE J,LOW OR HIGH SEG TO BE SHUFFLED OR SWAPPED
;	PUSHJ P,ANYSAV
;	RET1 - THIS IS A HIGH SEG WHICH SOME JOB IS DOING A SAVE OF
;	RET2 - THIS IS A HIGH SEG WHICH CAN BE SHUFFLED OR SWAPPED
;CALLED FROM ANYACT IN CORE MODULE,T2 PRESERVED FOR SWAPPER


ANYSAV::CAILE	J,JOBMAX##	;IS THIS A LOW SEG?
	SKIPN	JBTADR##(J)	;NO, IS THIS HIGH SEG IN CORE?
	JRST	CPOPJ1##	;YES, GIVE SINGLE SKIP RETURN
	PUSH	P,T1		;SAVE T1 FOR SHUFFLER
	PUSH	P,J		;SAVE HIGH SEG NUMBER
	MOVE	J,HIGHJB##	;HIGHEST JOB NUMBER ASSIGNED
ANYSV0:	SKIPN	R,JBTADR##(J)	;IS JOB IN CORE (R=R)?
	JRST	ANYSV1		;NO
IFN FTKI10!FTKL10,<
	PUSHJ	P,MAPHC##	;MAKE THE JOBHCU ADDRESSABLE
>
	SKIPL	JOBHCU##(R)	;YES, IS A SSVE OR GET FOR EITHER SEG IN PROGRESS?
ANYSV1:	SOJG	J,ANYSV0	;NO, KEEP LOOKING, FINISHED?
	JUMPLE	J,ANYSV2	;YES, FIND A SAVE OR GET IN PROGRESS
	MOVSI	T1,SWP		;JOB SWAPPED OUT OR ON WAY IN OR OUT BIT
	TDNE	T1,JBTSTS##(J)	;IS THIS JOB SWAPPED OUT OR ON WAY IN OR OUT
	JRST	ANYSV1		;YES, DO NOT LOOK AT JOB DATA AREA
				; SINCE IT MAY NOT HAVE BEEN WRITTEN INTO
				; IF THIS IS A SWAP IN
	HRRZ	T1,JBTSGN##(J)	;NO, GET ITS HIGH NUMBER, IF ANY
	SKIPLE	JBTSGN##(J)	;A REAL HIGH SEGMENT?
	CAIE	T1,@(P)		;IS IT SAME AS HIGH SEG IN QUESTION?
				; (INDIRECT AND INDEX BITS ALWAYS 0)
	JRST	ANYSV1		;NO, KEEP LOOKING
IFN FTKI10!FTKL10,<
	MOVE	R,JBTADR##(J)	;RESTORE R (IT WAS CHANGED BY MAPHC)
>
IFN FTKA10,<
	PUSHJ	P,ANYDEV##	;ANY ACTIVE DEVICES?
	  CAIA			;YES
	JRST	ANYSV1		;NO
>
	SOS	-2(P)		;YES, GIVE SAVE ACTIVE RETURN
ANYSV2:	POP	P,J		;RESTORE HIGH SEG NUMBER
	MOVE	R,JBTADR##(J)	;RESTORE ITS RELOC.
	JRST	TPOPJ1##	;GIVE ERROR OR OK RETURN
     
;ROUTINE TO DEPOSIT USER COMMAND ARG IN HIGH SEG IF USER HAS PRIVILEGES
; TO WRITE OR SEG IS NON-SHARABLE
;CALL:	MOVE J,JOB NO.
;	MOVE R,JOB RELOC.
;	MOVE T2,REL. USER ADR. NOT IN LOW SEG
;	MOVE S,WORD TO DEPOSIT
;	PUSHJ P,HGHDEP
;	ERROR RETURN
;	OK RETURN - JOBEXM SET TO USER ADR IN HIGH SEG, S STORED IN HIGH SEG


HGHDEP::HRR	M,T2		;MOVE REL. ADR. TO M FOR HGHWRD ROUT.
	PUSHJ	P,HGHWRD	;SET T1 TO HIGH WORD, T2 TO ADR.
	  POPJ	P,		;NOT IN BOUNDS, ERROR RETURN OR NO HIGH SEG
	MOVE	P2,J		;SETUP JOB NUMBER FOR CHKHSW ROUT.
	PUSH	P,J		;SAVE JOB NUMBER
IFN FTKI10!FTKL10,<
	PUSHJ	P,MAPWRD##	;T2 = EVA OF WORD
>
	PUSH	P,T2		;SAVE ABS. ADR. OF LOC. IN HIGH SEG
	SKIPLE	J,JBTSGN##(J)	;GET HIGH SEG NUMBER
	PUSHJ	P,CHKHSW	;CAN JOB(P2) WRITE IN HIGH SEG(J)?
	  JRST	HGHDP1		;NO, ERROR RETURN
	MOVEM	S,@(P)		;YES,STORE USER ARG
IFN FTEXAMINE,<
	HRRZM	M,JOBEXM##(R)	;STORE LOC OF DEPOSIT FOR D,E WITH NO ARG
>
				; LH=O MEANING D WAS LAST DONE RATHER THAN E
IFN FTSWAP,<
	PUSHJ	P,ZERSWP##	;DELETE SWAPPING SPACE,IF ANY
>
	AOS	-2(P)		;SET FOR SKIP RETURN
HGHDP1:	POP	P,T2		;RESTORE ADR
	JRST	IPOPJ##		;RESTORE JOB NUMBER AND ERROR OR OK RETURN
     

;ROUTINE TO REMOVE HIGH SEGMENT FROM USER'S LOGICAL ADDRESSING SPACE
;THIS IS CONTRANSTED WITH REMOVING A HIGH SEGMENT FROM PHYSICAL CORE
;WHICH THE SWAPPER DOES EVERY TIME A JOB WITH A HIGH SEG IS SWAPPED OUT (KCORE1)
;THE SEGMENT NUMBER ASSOCIATED WITH THE JOB IS ERASED (JBTSGN)

;IF NO OTHER JOB IS USING HIGH SEG IN CORE(IN CORE COUNT GOES TO 0)
; HIGH SEG IS NOT DELETED FROM CORE(UNLESS NOT SHARABLE) BUT RATHER BECOMES 'IDLE'.
; WHERE IDLE HAS SNA SET.
;IF NO OTHER JOB IS USING HIGH SEG IN LOGICAL ADR. SPACE, HIGH SEG IS
; NOT DELETED FROM CORE OR DISK(UNLESS NOT SHARABLE) BUT RATHER BECOMES 'DORMANT'.
; WHERE DORMANT HAS SNA TURNED OFF, BUT HAS CORE AND/OR DISK SPACE.
;RATHER IT BECOMES DORMANT(UNLESS IT NO LONGER HAS A NAME OR WAS NEVER SHARABLE)
;A DORMANT SEGMENT IS LEFT IN SYSTEM ACCORDING TO:
;	A. SWAP SYS - LEFT ON BOTH SWAPPING DEVICE AND CORE
;	B. NON-SWAP SYS - LEFT IN CORE
;DORMANT SEGMENTS HAVE JNA BIT OFF, HOWEVER THEY HAVE DISK SPACE AND/OR CORE
;DORMANT SEGMENTS ARE FLUSHED ONLY WHEN DISK OR CORE IS NEEDED
; OR HIGH SEGMENT NUMBERS ARE NEEDED

;KILHGH IS CALLED AT CLOCK AND UUO LEVELS BY:
;GET,R,RUN,LOGIN COMMANDS
;CORE 0 COMMAND
;CORE UUO(0 LESS-THAN LH LESS-THAN 400000)
;KJOB COMMAND AT UUO LEVEL
;REMAP UUO
;CALLER MUST INSURE THAT NEITHER LOW OR HIGH SEG ARE BEING SWAPPED

;CALL:	MOVE J,JOB NUMBER
;	MOVE R,LOW SEG RELOC. AND PROT.
;	PUSHJ P,KILHGH
;	OK RETURN - J,R RESTORED TO JOB (LOW SEG) RELOCATION


;ROUTINE TO REMOVE SEG FROM LOGICAL ADR. SPACE AND RESET HARDWARE RELOC
; KILHSH IS CALLED ONLY WHEN CALLER KNOWS THAT NO NEW HIGH SEG WILL
; TAKE THE PLACE OF THE ONE BEING REMOVED (SO LESS OVERHEAD).
;CALL: SAME AS KILHGH
     
;SUB TO REMOVE HIGH SEG - FROM LOG. ADR. SPACE AND CHANGE HARDWARE RELOC., THEN SKIP
KILHG1:	AOS	(P)		;HERE FROM CORE UUO (UCORHI) TO REMOVE HIGH SEG
				; ALWAYS SKIP RETURN

IFN FTWATCH&FTVERS,<
	PUSHJ	P,VERWAT##	;SEE IF WATCHING
	  SKIPA			;NO..PROCEED, DO NOT PRINT "]"
	PUSHJ	P,PRRBKC##	;YES - AND RIGHT BRACKET <CRLF>
>

;SUB TO REMOVE HIGH SEG FROM LOG. ADR. SPACE AND CHANGE HARDWARE RELOC
KILHSH::PUSHJ	P,KILHGC	;REMOVE HIGH SEG FROM LOGICAL ADR. SPACE OF CURRENT JOB
	PJRST	SETMAP##	;THEN RESET HARDWARE RELOC. SO USER CANNOT REFERENCE

;SUB. TO REMOVE HIGH SEG FROM LOGICAL ADR. SPACE OF CURRENT JOB
KILHGC:	MOVE	J,JOB##		;KILL HIGH SEG FOR CURRENT JOB(GETSEG,REMAP,CORE UUO)
KILHGH::PUSHJ	P,SAVE1##	;SAVE P1
	PUSH	P,J		;SAVE JOB NUMBER
	SKIPG	J,JBTSGN##(J)	;DOES JOB HAVE A REAL HIGH SEG?
	JRST	KILFIN		;NO, ZERO HIGH SEG NUMBER FOR THIS JOB IN CASE SPY SEG
IFN FTSWAP,<
	PUSHJ	P,SEGSIZ##	;T2=SIZE OF HIGH SEG
	PUSH	P,T2		;SAVE SIZE IN CASE NEED TO INCREASE AMOUNT
				;OF FREE VERTICAL CORE LEFT.
>
	SKIPN	R,JBTADR##(J)	;YES, IS HIGH SEG ALSO IN CORE?
	JRST	SCNLOG		;NO, SCAN TO SEE IF OTHER USERS USING THIS HIGH SEG
				; IN THEIR LOGICAL ADDRESSING SPACE

IFE FTSWAP,<
	MOVE	T1,(P)		;JOB NUMBER
>
IFN FTSWAP,<
	MOVE	T1,-1(P)	;JOB NUMBER
>
	PUSHJ	P,DECCNT	;YES, DECREMENT HIGH SEG IN CORE COUNT FOR THIS JOB
				; HIGH SEG IN-CORE COUNT TRANSITION TO 0?
	  JRST	KILFN0		;NO, JUST REMOVE HIGH SEG FROM THIS
				; USER'S LOG. ADR SPACE
     
SCNLOG:
IFN FTSWAP,<	MOVE	T1,HIGHJB##	;SCAN ALL USERS LOGICAL ADR. SPACE TO SEE IF ANY
				; OTHER USER HAVE THIS HIGH SEG IN THEIR LOG.
				; ADR. SPACE
	HRRZ	P1,-1(P)	;REMEMBER THIS JOB'S NUMBER
KILLOP:	SKIPLE	T2,JBTSGN##(T1)	;A REAL HIGH SEGMENT
	HRRZ	T2,JBTSGN##(T1)	;GET HIGH SEG NUMBER FOR NEXT USER(IF ANY)
	CAIN	T2,(J)		;SAME AS HIGH SEG BEING KILED?
	CAIN	P1,(T1)		;YES, DIFFERENT USER FROM ONE WE ARE REMOVING HI SEG?
	SOJG	T1,KILLOP	;NO, KEEP LOOKING, FINISHED?
	JUMPG	T1,KILFN0	;YES, FIND SOME OTHER USER WITH SAME HIGH SEG
				; IN HIS LOG. ADR. SPACE TOO?
IFN FTLOCK,<
	PUSHJ	P,UNLOCK##	;NO. UNLOCK IT
>
	SKIPE	JBTNAM##(J)	;NO, COULD NEW USER'S SHARE THIS HIGH SEG
				; ON A GET IF WE LEFT IT ON SWAPPING SPACE?
	JRST	KILLP1		;YES, DON'T DELETE IT
IFE FTVM,<
	PUSHJ	P,KCOREH	;RETURN CORE
>
IFN FTVM,<
	PUSHJ	P,KCOREV##	;RETURN REAL PLUS VIRTUAL CORE
>
	PUSHJ	P,ZERSWP##	;DELETE DISK SPACE(IF ANY)
KILLP1:
>
	MOVSI	T1,SNA		;FLAG HIGH SEG AS UNASSIGNED(OR DORMANT
				; IF STILL IN
				; CORE OR DISK).
	ANDCAM	T1,JBTSTS##(J)
IFN FTSWAP,<
	MOVE	T1,(P)		;SIZE IN K OF HIGH SEG WHICH IS BECOMING DORMANT
	ADDM	T1,VIRTAL##	; OR UNASSIGNED, INCREASE AMOUNT OF VIRTUAL CORE LEFT
				; IN SYSTEM,(EVEN THOUGH CORE AND DISK SPACE
				; MAYBE STILL USED)
>
IFN FTLOCK,<
	JRST	KILFN1		;SKIP CALL TO UNLOKH
>
KILFN0:
IFN FTLOCK,<
	PUSHJ	P,UNLOKH##	;IF THE HIGH SEGMENT IS LOCKED, UNLOCK IT IF
				;  NO OTHER LOCKED LOW SEGMENT IS SHARING IT
>
KILFN1:	MOVE	J,IFN FTSWAP,<-1>(P)	;GET JOB NUMBER
	MOVE	J,JBTSGN##(J)	;GET SEGMENT NUMBER
	MOVE	T2,[NSWP,,ICCMSK]
	TDNN	T2,JBTSTS##(J)	;SEG IN USE OR STILL LOCKED?
	SKIPN	R,JBTADR##(J)	;NO, DOES IT STILL HAVE PHYSICAL CORE?
	JRST	KILFN2		;DON'T ADJUST CORTAL IF LOCKED, SHARED,OR 0 CORE
	LDB	T1,PCORSZ##	;SIZE OF SEGMENT IN K
	ADDI	T1,1
	ADDM	T1,CORTAL##	;INCREASE TOTAL FREE+DORMANT+IDLE CORE
KILFN2:
IFN FTHALT,<
	PUSHJ	P,CHKTAL	;CHECK CORTAL AGAINST CORTAB, HALT IF BAD
>
IFN FTSWAP,<
	POP	P,T1		;REMOVE SIZE OF SEG IN K
>
     

KILFIN:	POP	P,J		;RESTORE JOB NUMBER
IFN FTKI10!FTKL10,<
	HRRZ	T4,JBTUPM##(J)	;NO HI SEG TO REMOVE IF NO MAP
	SKIPE	T4
	PUSHJ	P,REMHGH##	;REMOVE THE HIGH SEGMENT FROM THE PAGE MAP
>	;END IFN <FTKI10!FTKL10>
	SETZM	JBTSGN##(J)	;REMOVE HIGH SEG FROM THIS USER'S LOGICAL ADR. SPACE
	SKIPN	R,JBTADR##(J)	;RESTORE LOW SEG RELOC AND PRTECTION
	POPJ	P,		;IF NONE, RETURN
	HLRZ	P1,R		;GET LOW-SEG END
	HRRZ	T1,JOBSA##(R)	;GET START ADDRESS
	CAMLE	T1,P1		;SEE IF LOW SEG
	HLLZS	JOBSA##(R)	;NO - CLEAR IT
	HRRZ	T1,JOBREN##(R)	;GET REENTER ADDRESS
	CAMLE	T1,P1		;SEE IF LOW SEG
	HLLZS	JOBREN##(R)	;NO - CLEAR IT
	HRRZS	JOBHRL##(R)	;CLEAR OUT SIZE OF HIGH SEG AS SET BY LOADER
				; SO USER'S CAN TEST FOR HIGH SEG BY SKIPN JOBHRL
IFN FTVM,<
	CAME	J,.C0JOB##	;CURRENT JOB?
	PUSHJ	P,SVEUB##	;NO, MAKE SURE ITS ADDRESSABLE
	SETZB	T1,.UPMP+.UPHSE	;NO HIGH SEGMENT NOW
	DPB	T1,[POINT 9,JBTVRT##(J),26]  ;HIGH SEGMENT SIZE IS NOW 0
>
	POPJ	P,		;RETURN
     
;ROUTINE TO CLEAR A HIGH SEGMENT NAME SO NO NEW USERS CAN SHARE
;AND RETURN BOTH DISK AND CORE SPACE (IF ANY) IF HIGH SEG WAS DORMANT(SNA=0)
;IE NO LONGER IN ANY USER'S VIRTUAL ADR. SPACE
;CALL:	MOVE J, HIGH SEG NUMBER
;	PUSHJ P, CLRNAM
;	RETURN J AND R PRESERVED
;CALLED FROM FRESWP,FRECOR,FNDSGN (UREMAP,UCORHI), ASGHGH,RELSEG

IFN FTDISK,<>

CLRNAM:	SETZM	JBTDEV##(J)	;CLEAR DEVICE NAME OR STR NAME
IFN FTDISK,<SETZM JBTPPN##(J)	; CLEAR DIRECTORY NAME >
CLRNM1:	SETZM	JBTNAM##(J)	;AND SEG NAME SO NEW USER'S CAN USE
				; CLRNM2 CALLED PQOM ASGHGH AND RELSEG
				; WHEN FILE SUPERCEDED, BUT LEAVE DIR
				; NAME FOR SYSTAT TO PRINT (OBS)
	SKIPGE	JBTSTS##(J)	;IS SEG DORMANT(IE NOT IN ANY USER'S VIRTUAL
				; ADR. SPACE)?
	POPJ	P,		;NO,(SNA=1) ALLOW USER'S TO CONTINUE TO SHARE
				; UNTIL FINISHED, HOWEVER NO NEW USERS CAN
				; SHARE SINCE NO LONGER A NAME
IFN FTSWAP,<
	PUSHJ	P,ZERSWP##	;YES, RETURN DISK SPACE, IF ANY
>
				;FALL INTO ROUTINE TO KILL DORMANT CORE

;ROUTINE TO RETURN DORMANT OR IDLE CORE (IF ANY)
;CALL:	MOVE J, HIGH SEG NUMBER
;	PUSHJ P,KDORCR
;	RETURN J,R PRESERVED
;CALLED FROM CLRNAM AND FRECOR


KDORCR:	PUSH	P,R		;SAVE R(EITHER HIGH OR LOW RELOC)
	PUSH	P,CORTAL##	;SAVE FREE CORE COUNT
	SKIPE	R,JBTADR##(J)	;DOES DORMANT SEG HAVE CORE?
	PUSHJ	P,KCOREH	;YES, RETURN PHYSICAL COREAND FLAG HIGH SEG
				; AS ON DISK IF IT HAS DISK SPACE(SET SWP=1)

	POP	P,CORTAL##	;RESTORE FREE CORE COUNT, SINCE DORMANT
				; SEGS ALREADY INCLUDED IN CORTAL
IFN FTHALT,<
	PUSHJ	P,CHKTAL	;CHECK CORTAL WITH CORTAB, HALT IF BAD
>
	POP	P,R		;RESTORE R
	POPJ	P,		;RETURN

     
;ROUTINE TO RETURN HIGH SEGMENT CORE(MUST BE SOME) AND FLAG SEG AS ON DISK
; IF HIGH SEG HAS DISK SPACE(IE SET SWP=1 IF IMGOUT NON-ZERO)
;CALL:	MOVE J,HIGH SEG NUMBER
;	MOVE R,HIGH SEG RELOC AND PROTECTION
;	PUSHJ P,KCOREH
;CALLED FROM KDORCR(FRECOR,CLRNAM,KILHGH) AND FORHGH

IFN FTSWAP,<

KCOREH::LDB	T1,IMGOUT##	;DOES HIGH SEG HAVE DISK SPACE?
	JUMPE	T1,KCORE1##	;NO, RETURN CORE
	MOVSI	T1,SWP		;YES, FLAG HIGH SEG AS SWAPPED OUT
	IORM	T1,JBTSTS##(J)	;SET SWP=1
	JRST	KCORE1##	;THEN RETURN CORE
>
IFE FTSWAP,<
	KCOREH=KCORE1##
>
     
;CORE UUO ROUTINE TO ASSIGN OR REASSIGN CORE FOR HIGH SEG
;CALLED AFTER LOW SEG SEG IS REASSIGNED IF USER HAS NON-ZERO RH IN AC
;CALL:	HRRZ T1,DESCRED HIGHEST USER ADR. IN HIGH SEG
; 	MOVE J, JOB NUMBER
;	PUSHJ P,UCORHI
;	ERROR RETURN - SUM OF SEGMENTS EXCEED MAX. PHYSICAL CORE OR
;		EXCEED TOTAL SYSTEM VIRTUAL CORE CAPACITY
;		SHARABLE PROGRAM MEDDLED WITH(AND NO WRITE PRIV.)
;		 OR HIGH SEG IS SPY SEG

; 	OK RETURN - CORE ASSIGNED(MAYBE NOT IN MEMORY) OR USER ASKED NO CHANGE(LH=0)
;	J AND R RESTORED TO LOW SEG NO AND RELOC

;IF LH IS NON-ZERO BUT LESS THAN 400000 OR END OF LOW SEG(IF GREATER THAN 400000)
;THE HIGH SEGMENT WILL BE REMOVED FROM USERS LOGICAL ADDRESSING SPACE


UCORHI::JUMPE	T1,CPOPJ1##	;ASKING FOR ANY CHANGE?
	MOVE	T3,T1		;SAVE THE USER'S ARGUMENT
	PUSHJ	P,HSVAD		;GET ORIGIN AND LENGTH OF THE HIGH SEGMENT
IFE FTVM,<
	SUB	T3,T2		;LENGTH-1 OF HIGH SEGMENT
	JUMPL	T3,KILHG1	;KILL HIGH SEG IF ITS HIGHEST USER ADR WOULD
				; BE LESS THAN USER ORIGIN OF HIGH SEG
				; ALWAYS SKIP RETURN - AND CHANGE HARDWARE RELOC TOO
				; (OK TO REMOVE EVEN IF LOCKED)
	IORI	T3,PG.BDY##	;USER WANTS MORE OR LESS HIGH SEG CORE, 
				; MAKE IT EVEN 1K-1
	PUSH	P,T3		;SAVE CORE ARG(NEW SIZE-1)
IFN FTKI10!FTKL10,<
	LSH	T2,W2PLSH##	;PAGE NUMBER WHERE HIGH SEGMENT ORIGINS
	PUSH	P,T2		;SAVE THAT
>>
IFN FTVM,<
	CAMGE	T3,T2		;IF NEW HIGHEST ADDRESS .LT.ORIGIN,
	JRST	KILHG1		; REMOVE THE HIGH SEGMENT
	SKIPN	T1		;CREATING A HIGH SEGMENT?
	MOVE	T1,T2		;YES, HIGHEST ADDRESS EQUALS ORIGIN
	EXCH	T1,T3		;SETUP ARGS FOR CKNZW
	EXCH	T2,T3		; ..
	IORI	T1,PG.BDY##	;ROUND UP TO A PAGE BOUNDRY
	PUSHJ	P,CKNZW##	;WILL HI SEG OVERLAP LOW SEG?
	  POPJ	P,		;YES, LOSE
	SUB	T1,T3		;NEW LENGTH OF THE HIGH SEG
	PUSH	P,T1		;SAVE THAT
	LSH	T3,W2PLSH##	;HIGH SEG ORIGIN PAGE
	PUSH	P,T3		;SAVE THAT
>
IFN FTLOCK,<			;LOCK UUO FEATURE?
	PUSHJ	P,LOKCHK##	;IS JOB LOCKED?
	  JRST	UCORHE		;YES, ERROR RETURN-CANNOT CHANGE SIZE
>
	SKIPL	J,JBTSGN##(J)	;GET HIGH SEG NUMBER(IF ANY),SPY SEG?
	SKIPN	TWOREG##	;NO, DOES MACHINE HAVE JUST 1 RELOC REG?
	JRST	UCORHE		;YES, GIVE ERROR RETURN TO USER
	PUSHJ	P,CHKHWC	;NO, SEE IF CURRENT JOB HAS NOT BEEN MEDDLING OR
				; HAS UPDATE PRIVILEGES IF HAS MEDDLED WITH HIGH SEG
	  JRST	UCORHE		;NO, ERROR RETURN TO USER
UCORH1:	SKIPN	J		;YES, DOES JOB HAVE A HIGH SEG YET?
				; OR IS THIS FIRST ASSIGNMENT OF CORE TO HIGH SEG
	PUSHJ	P,FNDSGN	;NO, FIND FREE SEG NUMBER BEFORE ASSIGNING CORE
     
IFN FTKI10!FTKL10,<
	POP	P,T1		;HIGH SEGMENT ORIGIN
	DPB	T1,JBYHSO##	;STORE IT FOR POSTERITY
>
IFN FTSWAP,<
	PUSHJ	P,ZERSWP##	;RETURN SWAPPING SPACE, IF ANY (NULL JOB NEVER HAS
				; SINCE NEW SIZE MAY BE DIFFERENT FROM OLD SIZE
				; SO IT SHOULD BE WRITTEN NEXT SWAPOUT
				; EVEN IF WRITE PROTECTED.
>
	MOVE	T1,(P)		;CORE ARGUMENT
;HERE TO MAKE SURE THAT ALLOWING THIS HIGH SEGMENT EXPANSION WILL NOT KEEP
; OTHER JOBS SHARING IT FROM BEING ABLE TO BE RUN (SWAPPED IN)
	HLRZ	T2,JBTADR##(J)	;CURRENT SIZE OF THE HIGH SEGMENT
	CAIG	T2,(T1)		;ALWAYS OK TO GET SMALLER
	TLNN	J,SHRSEG	;ALWAYS OK TO MAKE A NON-SHARABLE SEGMENT BIGGER
	JRST	UCORH4		;OK TO DO THE CORE UUO
IFN FTVM,<
	PUSH	P,P1
	MOVSI	P1,PHONLY	;CHECK PHYSICAL LIMITS
>
	PUSH	P,J		;SAVE THE HIGH SEGMENT NUMBER
	MOVE	J,HIGHJB##	;HIGHEST JOB NUMBER ASSIGNED
UCORH2:	HRRZ	T2,JBTSGN##(J)	;THIS JOB'S HIGH SEGMENT NUMBER
	SKIPLE	JBTSGN##(J)	;A REAL HIGH SEGMENT?
	CAIE	T2,@(P)		;EQUAL TO THE SEGMENT NUMBER OF THE EXPANDING SEG?
	JRST	UCORH3		;NO
	PUSHJ	P,CORBND##	;AMOUNT OF CORE THIS JOB IS ALLOWED
	PUSHJ	P,SEGSIZ##	;CURRENT SIZE OF THE LOW SEGMENT
	LSH	T2,P2WLSH##
	ADD	T2,-1-IFN FTVM,<1>(P)	;SIZE JOB WILL BE IF HI SEG EXPANDS
IFN FTKI10!FTKL10,<
	SUBI	T2,UPMSZW##	;CORBND DOESN'T COUNT UPMP SIZE
>
	CAMLE	T2,T1		;WILL THIS JOB STILL FIT?
	JRST	[POP P,J	;NO, LOSE
		 IFN FTVM,<POP P,P1>
		 JRST UCRHE0]
UCORH3:	SOJG	J,UCORH2	;LOOK AT ALL JOBS SHARING THIS SEGMENT
	POP	P,J		;RESTORE THE HIGH SEGMENT NUMBER
IFN FTVM,<
	POP	P,P1		;RESTORE USERS P.O. BITS
>
UCORH4:	MOVE	R,JBTADR##(J)	;R TO PREVIOUS HIGH SEG CORE ASSIGNMENT(IF ANY)
	POP	P,T1		;RESTORE AMOUNT OF CORE REQUEST
	CPLOCK	(SCD)
				;DON'T ALLOW CPU1 TO RUN THIS SEGMENT
				;WHILE CORE SIZE IS CHANGING
	PUSHJ	P,CORE0##	;TRY TO ASSIGN CORE TO HIGH SEG(ON DISK OR MEMORY)
	  JRST	[CPUNLK	(SCD)
		 JRST	UCRHE1]
				;ERROR, CORE REQUEST TOO BIG, IF NO ONE IS USING
				; (INCLUDING THIS USER) LEAVE SEG NON-EXISTENT,
				; OTHERWISE DORMANT OR STILL IN USE BY THIS AND/OR
				; OTHER USERS
	CPUNLK	(SCD)
	MOVSI	T1,SWP		;IF THIS IS A HIGH SEGMENT WHICH WAS CREATED
	SKIPN	JBTADR(J)	; BY THIS CORE UUO, CORE MAY BE ASSIGNED ON DISK
	IORM	T1,JBTSTS(J)	;IT WAS - MARK THE HIGH SEGMENT AS SWAPPED
				; SHARE1 WILL MARK THE LOW SEGMENT
	MOVE	R,JOBADR##	;RESTORE LOW SEGMENT ADDRESS, FOR USE BY WSCHED
	SJSP	T3,SHARE1	;OK RETURN, MARK USER AS HAVING THIS HIGH SEG IN LOG.
				; ADR. SPACE(IN CASE IT WAS NOT BEFORE)
				; SET RELOC AND SKIP RETURN
				; AND INCREMENT HIGH SEG IN CORE CONT FOR THIS JOB
				; IF NOT ALREADY DONE SO.
				; FLAG (T3) NOT TO CHECK FOR CONNECTING TO IDLE
				; HI-SEG SINCE IT CANNOT BE (BUT LOOKS IDLE)
UCORHE:
IFN FTKI10!FTKL10,<
	POP	P,T1		;POP OFF JUNK
>
UCRHE0:	POP	P,T1		;RESTORE PDL
UCRHE1:
	JRST	SETMAP##	;AND SETUP HARDWARE, SET R TO LOW SEG
				; RELOC, J TO CURRENT JOB, AND ERROR RETURN
     
;UUO TO REMAP TOP PART OF LOW SEGMENT INTO HIGH SEGMENT
;PREVIOUS HIGH SEGMENT(IF IT EXISTED) IS REMOVED FROM LOGICAL ADDRESSING SPACE
;CALL:	MOVE T1,HIGHEST DESIRED REL. ADR. IN NEW LOW SEG.
;				(EXEC ORES IN 1777 TO MAKE EVEN 1K-1)
;	MOVE R,XWD PROTECTION,RELOCATION FOR LOW SEGMENT
;	PUSHJ P,UREMAP
;	ERROR RETURN - DESIRED HIGHEST REL. ADR PAST END OF LOW SEG
;			;OR 1 REGISTER MACHINE
;	OK RETURN - HARDWARE AND SOFTWARE RELOCATION INFO RESET
;	J RESET TO CURRENT JOB NUMBER, AND R TO RELOC
;MUST BE CALLED AT UUO LEVEL ONLY


UREMAP::
IFN FTVM,<
	SETZM	SGAEXT##(R)	;SO NEW HGH CAN TELL SHR FROM HGH
	MOVSI	T2,(UP.GET)
	ANDCAM	T2,.UPMP+.UPBTS	;GET NO LONGER IN PROGRESS
>
UREMA0:	MOVSI	T2,NSWP!NSHF
	TDNE	T2,JBTSTS##(J)	;LOCKED?
	POPJ	P,		;YES, ERROR RETURN
IFN FTKA10,<
	JUMPL	T1,CPOPJ##	;IS USER ARG NEGATIVE? GIVE ERROR RET IF YES
>
	IORI	T1,PG.BDY##	;MAKE AN EVEN MULTIPLE OF 1K-1
	PUSH	P,T1		;YES, SAVE DESIRED HIGHEST ADR OF NEW LOW SEG
IFN FTKI10!FTKL10,<
	HRRZS	T1		;CLEAR POSSIBLE HIGH SEGMENT ORIGIN
>
	SKIPE	TWOREG##	;DOES MACHINE HAVE TWO RELOCATION REG?
	CAMLE	T1,USRREL##	;YES, IS NEW TOP OF LOW SEG LESS THAN OR EQUAL TO OLD?
	JRST	TPOPJ		;NO, ERROR RETURN - 1 REG MACHINE OR ARG TOO BIG
	PUSH	P,F		;SAVE F (IOWAIT CHANGED IT)
	PUSHJ	P,IOWAIT##	;WAIT FOR IO TO STOP IN LOW SEG
	POP	P,F		;RESTORE F
	PUSHJ	P,KILHGC	;KILL PREVIOUS HIGH SEG IF IT EXISTED FOR CUR. JOB
	HRRZ	T2,(P)		;RESTORE DESIRED HIGHEST ADR IN NEW LOW SEG
IFN FTVM,<
	SKIPE	T1,.UPMP+.UPREL	;IF NON-ZERO, PREVIOUS HIGHEST ADR IN LOW SEG
	JRST	UREMA2		;DON'T CHANGE JBTADR
UREMA1:>
	HLRZ	T1,JBTADR##(J)	;GET PREVIOUS HIGHEST ADR OF LOW SEG
	HRLM	T2,JBTADR##(J)	;STORE NEW(LESS THAN OR EQUAL) HIGHEST ADR OF LOW SEG
UREMA2:	SUBI	T1,1(T2)	;COMPUTE LENGTH-1 OF NEW HIGH SEG
	JUMPL	T1,UREMA3	;-1 MEANS NEW LOW SEG=OLD LOW SEG
				; RESET HARDWARE BECAUSE NO HIGH SEG ANY MORE
				; FOR SURE, SET RELOC. HARD. AND SKIP RETURN
	ADD	T2,JBTADR##(J)	;ADD ABS. ADR. OF LOW SEG TO LENGTH-1 OF LOW SEG
				;TO FORM LENGTH-1 OF NEW HIGH SEG
	HRLI	T1,1(T2)	;MOVE ABS ADR. OF NEW HIGH SEG TO LH
IFE FTVM,<
	PUSHJ	P,FNDSGN	;FIND FREE SEG NUMBER(T1 PRESERVED)
				; AND SET IN CORE COUNT TO 0
>
	POP	P,T2		;RESTORE USER'S ARGUMENT (NEWHGH WILL
				; PUT THE HIGH SEGMENT AT THE LEFT HALF
				; OF T2 IF NON-ZERO
IFN FTKI10!FTKL10,<
	PUSHJ	P,NEWHGH##	;STORE ABSOLUTE ADDRESS OF FIRST PAGE OF NEW HIGH
				; SEGMENT AND RETURN VIRTUAL ADDRESS OF
				; HIGH SEGMENT IN T1.
	  JRST	UREMA4		;HIGH SEGMENT WOULD OVERLAP THE LOW SEGMENT
>
	MOVSM	T1,JBTADR##(J)	;STORE LENGTH-1 AND ABS. ADR. OF NEW HIGH SEG
	SJSP	T3,SHARE1	;SET IN CORE COUNT TO 1, FLAG THIS SEG IN CURRENT
				; JOB'S LOG. ADR. SPACE, AND FLAG HIGH SEG NO.
				; AS ASSIGNED(SNA=1)
				; RESET HARD. AND SOFT. RELOC, AND SKIP RETURN
				; (SET FLAG(T3) SO NOT CHECK FOR CONNECTING TO
				; IDLE HI-SEG SINCE IT CANNOT BE IDLE, BUT
				; IT WILL LOOK LIKE IT IS.)
IFN FTKI10!FTKL10,<
UREMA4:	SOSA	(P)		;SETRL1 DOES AOS (P), SO FORCE ERROR RETURN
>
UREMA3:	POP	P,T1		;POP OFF JUNK & GIVE GOOD RETURN
				;FOR UNCHANGED LOW SEG (DISCARD HISEG)
	JRST	SETRL1		;REDO PROTECTION-RELOCATION AND GIVE ERROR RETURN
     
;ROUTINE TO FIND A FREE SEG NUMBER FOR A NEW HIGH SEGMENT FOR CURRENT JOB
;CALLED BY CORE UUO AND REMAP UUO(MUST BE CALLED AT UUO LEVEL ONLY)
;TRIES TO FIND A FREE SEGMENT WITHOUT DORMANT COPY ON DISK OR CORE
;IF NONE, IT FINDS A SEG WHICH IS DORMANT ON DISK AND/OR CORE
; DELETES UNUSED DISK AND CORE AND REUSES THAT HIGH SEG NUMBER
;(A SEGMENT CANNOT BE DORMANT ON DISK AND USED IN CORE OR VICE VERSA)
;NEW HIGH SEG IS FLAGGED AS NON-SHARABLE(SHRSEG=0 IN JBTSTS FOR HIGH SEG)
;IN CORE COUNT IS SET TO 0
;CALL:	PUSHJ P,FNDSGN
;	RETURN WITH J SET TO FREE SEG NUMBER
;HOWEVER CURENT JOB IS NOT MARKED AS HAVING THIS SEG IN LOGICAL ADR SPACE
; SINCE THER MAY NOT BE ENOUGH CORE FOR SEGMENT. THE CALLER MUST FLAG USER
; AS HAVEING THIS SEG IN ADDRESSING SPACE IF EVERYTHING OK
;THE LH OF J IS SET TO 0(SHRSEG=MEDDLE=UWPOFF=SPYSEG=0)
;THE CALLER MUST ALSO FLAG HIGH SEG AS ASSIGNED(SNA=1) IF EVERYTHING OK
;T1 IS PRESERVED, T2 IS USED

IFN FTDISK,<>

FNDSGN::PUSH	P,T1		;SAVE T1(HIGHEST REL. ADR. DESIRED
				;IN CALLS TO CORE AND REMAP)
FND0:	MOVE	J,SEGPTR##	;SCAN JUST HIGH SEGMENTS(NOT JOBS)
IFN FTSWAP,<
	MOVEI	T2,OUTMSK##	;MASK FOR DISK IMAGE BYTE POINTER
>
FND1:	SKIPL	JBTSTS##(J)	;SEG NUMBER ASSIGNED(SNA=1)?
	SKIPE	JBTADR##(J)	;NO, DOES HIGH SEG STILL HAVE CORE ANYWAY?
IFN FTSWAP,<
	JRST	.+2		;YES, KEEP LOOKING
	TDNE	T2,JBTSWP##(J)	;NO, DOES IT HAVE DISK SPACE?
>
	AOBJN	J,FND1		;YES, KEEP LOOKING, MORE TO GO?
	JUMPL	J,FNDOK		;NO, WAS A FREE SEG NUMBER FOUND WITH NO CORE ASSIGNED?
	MOVEI	T2,SEGN##	;NO. OF HIGH SEGMENTS IN SYSTEM
	AOS	J,SEGPT1##	;START AT SEGMENT AFTER LAST DORMANT SEG
FND2:	CAILE	J,JBTMAX##	;STILL LESS THAN OR EQUAL TO MAX SEG NO.?
	HRRZ	J,SEGPTR##	;NO, START OVER AT FIRST HIGH SEG
	SKIPL	T1,JBTSTS##(J)	;IS THIS HIGH SEG NO. ASSIGNED (SNA=1)?
	JRST	FNDOK1		;NO, FOUND A DORMANT SEG?
FND3:	ADDI	J,1		;NO, LOOK AT NEXT ONE
	SOJG	T2,FND2		;LOOKED AT ALL SEG?
	MOVEI	T1,1		;THE ONLY DORMANT SEGMENTS FOUND WERE
				; IN THE PROCESS OF BEING SWAPPED OUT.
	PUSHJ	P,SLEEP##	;SO WAIT UNTIL SWAPOUT COMPLETES
	JRST	FND0		;TRY AGAIN
FNDOK1:	TLNE	T1,NSHF		;SWAP OUT OF THIS SEGMENT IN PROGRESS?
	JRST	FND3		;YES, SKIP IT FOR NOW
	MOVEM	J,SEGPT1##	;REMEMBER LAST DORMANT SEGMENT FOUND FOR NEXT TIME
				; SO WILL THROW AWAY OLDEST DORMANT SEG
FNDOK:	TLZ	J,777777	;TURN OFF MEDDLE,SHRSEG,UWPOFF,SPYSEG,CORCNT)
	PUSHJ	P,CLRNAM	;CLEAR HIGH SEG NAME SO NO LONGER
				; SHARABLE TO NEW USERS, DELETE DISK AND CORE(IF ANY)
	SETZM	JBTSTS##(J)	;CLEAR ALL BITS AND IN CORE COUNTS JUST FOR GOOD
				; MEASURE(SHRSEG!SERR COULD BE STILL ON FOR HIGH SEG)
	JRST	TPOPJ##		;RESTORE T1 AND RETURN
IFN FTKI10!FTKL10,< 
;SUBROUTINE TO FREE UP CORE IN CORE BY DELETING DORMANT OR IDLE
; SEGMENTS WHICH HAVE DISK COPIES
;ENTER WITH T3= NUMBER OF PAGES REQUIRED
;EXIT CPOPJ IF NOT ENOUGH PAGES CAN BE MADE AVAILABLE,
; EXIT CPOPJ1 IF ENOUGH CORE HAS BEEN FREED
;PRESERVES ALL ACS

FRDCR::	CAMLE	T3,CORTAL##	;CAN ENOUGH CORE BE FREED?
	POPJ	P,		;NO
	PUSHJ	P,SAVT##	;SAVE T1-T4
	PUSH	P,J		;SAVE J
	MOVE	J,SEGPTR##	;AOBJN POINTER TO HIGH SEGMENT TABLES
FRDCR1:	MOVEI	T1,ICCMSK	;IN CORE COUNT MASK
	HRLI	T1,NSHF!NSWP	;LOOK FOR DORMANT OR IDLE UNLOCKED SEGMENTS
	MOVE	T3,-4(P)	;AMOUNT OF CORE REQUIRED
	CAMG	T3,BIGHOL##	;IS THAT MUCH AVAILABLE
	JRST	JPOPJ1##	;YES, RESTORE J AND GIVE CORE AVAILABLE RETURN
FRDCR2:	SKIPE	JBTADR##(J)	;SEGEMENT IN CORE?
	TDNE	T1,JBTSTS##(J)	;UNLOCKED AND DORMANT OR IDLE?
	JRST	FRDCR3		;NO
	LDB	T2,IMGOUT##	;DOES IT HAVE A DISK COPY?
	JUMPE	T2,FRDCR3	;NO, CONTINUE LOOKING
	PUSH	P,J		;SAVE THE AOBJN POINTER
	ANDI	J,-1		;CLEAR THE LEFT HALF
	PUSHJ	P,KDORCR	;DELETE THE DORMANT OR IDLE SEGMENT FROM CORE
	POP	P,J		;RESTORE THE AOBJN POINTER
	JRST	FRDCR1		;SEE IF ENOUGH CORE IS AVAILABLE NOW
FRDCR3:	AOBJN	J,FRDCR2	;LOOK AT THE NEXT SEGMENT
	JRST	JPOPJ##		;SOME OF THE DORMANT SEGMENTS DON'T HAVE A DISK COPY
				; RESTORE S AND GIVE CORE NOT AVAILABLE RETURN
>
;ROUTINE TO INCREMENT HIGH SEG IN CORE COUNT (OCNCE)
; FOR A JOB WHICH HAS A REAL HIGH SEG WHICH MUST BE IN CORE
;CALL:	MOVE J,XWD JOB HIGH SEG BITS, HIGH SEG NUMBER
;	MOVE T1,JOB NUMBER
;	PUSHJ P,INCCNT
;	ALWAYS RETURN WITH C(J)=JBTSGN(T1)
;CORCNT IS SET TO 1 TO FLAG THIS JOB AS HAVING INCREMENTED IN CORE COUNT
;CALLED FROM SHARES(GET,REMAP,CORE) AND FININ


INCCNT:	TLON	J,CORCNT	;HAS HIGH SEG IN CORE COUNT ALREADY BEEN
	AOS	JBTSTS##(J)	; INCREMENTED FOR THIS JOB?
				;NO, INCREMENT HIGH SEG IN CORE COUNT
IFN FTHALT,<
	PUSHJ	P,COMIT		;COMPARE J WITH JBTSGN(T1)
>
	MOVEM	J,JBTSGN##(T1)	;STORE CORCNT FLAG FOR THIS JOB?
	POPJ	P,


;ROUTINE TO DECREMENT HIGH SEG IN CORE COUNT (ONCE)
; FOR A JOB WHICH HAS A REAL HIGH SEG WHICH MUST BE IN CORE
;CALL:	MOVE J XWD JOB HIGH SEG BITS, HIGH SEG NUMBER
;	MOVE T1, JOB NUMBER
;	PUSHJ P,DECCNT
;	RET1 IN CORE COUNT NOT YET 0 OR IN-CORE COUNT NOT UP FOR THIS JOB
;	RET2 IN CORE COUNT WENT TO 0
;CALLED FROM KILHGH,FORHGH,FINOT


DECCNT::
IFN FTHALT,<
	PUSHJ	P,COMIT		;COMPARE J WITH JBTSGN(T1)
>
	TLZN	J,CORCNT	;HAS HIGH SEG IN CORE COUNT ALREADY BEEN
				;DECREMENTED FOR THIS JOB?
	POPJ	P,		;YES, RETURN
	SOS	JBTSTS##(J)	;NO, DECREMENT HIGH SEG IN CORE COUNT
	MOVEM	J,JBTSGN##(T1)	;CLEAR CORCNT FLAG IN MEMORY FOR THIS JOB
DECERR:	LDB	T1,JBYICC##	;GET HIGH SEG IN CORE COUNT
	TRCN	T1,ICCMSK	;IN CORE COUNT EQUAL ZERO?
	AOSA	(P)		;YES, SKIP RETURN
	SKIPE	T1		;NO, NEGATIVE?
	POPJ	P,		;NO
	AOS	JBTSTS##(J)	;NO, ERROR ADD BACK TO 0
	STOPCD	DECERR,DEBUG,ICN,	;++INCORE COUNT NEGATIVE
     
;ROUTINE TO COMPARE RH OF J WITH JBTSGN(T1)
;BEFORE STORES INTO JBTSGN(T1)
;WHERE T1 CONTAINS JOB NUMBER AND J IS HIGH SEG NUMBER
;USED FOR DEBUGGING.  MONITOR CANNOT RUN VERY LONG IF THIS ERROR GOES UNDETECTED

IFN FTHALT,<

COMIT:	PUSH	P,T1		;SAVE JOB NUMBER
	PUSHJ	P,LGLPRC##	;MAKE SURE T1 IS A JOB NUMBER
	STOPCD	.,STOP,POR,	;++PROCESS OUT OF RANGE
	HRRZ	T1,JBTSGN##(T1)	;GET JOBS HIGH SEG NUMBER
	JUMPE	T1,TPOPJ##	;IF NONE, OK TO RESTORE J
	CAIE	T1,(J)		;MAKE SURE RH OF J SAME AS RH OF JBTSGN(T1)
	STOPCD	.,STOP,BSN,	;++BAD SEGMENT NO.
	JRST	TPOPJ##		;RESTORE T1 AND RETURN
>
     
;ROUTINE TO CHANGE RELOC INFO IF SEG JUST ASSIGNED IS A HIGH SEG
;WHICH THE CURRENT USER MAY BE USING
;CALL:	MOVE J,LOW OR HIGH SEG FOR WHICH CORE WAS JUST ASSIGNED(OR RETURNED)
;	PUSHJ P,CURHGH
;	ALSWAYS RETURN WITH J PRESERVED
;	R RESET TO RELOC FOR HIGH SEG IF J IS HIGH SEG, OTHERWISE R AND
;		J UNCHANGED


CURHGH::MOVE	T1,JOB##		;CURRENT JOB NUMBER
	HRRZ	T1,JBTSGN##(T1)	;GET HIGH SEG NUMBER OF CURRENT JOB OR 0
	CAIE	T1,(J)		;IS HIGH SEG JUST ASSIGNED SAME AS CURRENT JOB'S
	POPJ	P,		;NO
	PUSH	P,J		;YES, SAVE HIGH SEG NUMBER
	PUSHJ	P,SETMAP##	;SET HARD. AND SOFT. RELOC INFO FOR CURRENT JOB
				; (EVEN THOUGH THIS CALL TO CURHGH MAY BE FOR SOME
				; OTHER JOB) SONCE HIGH SEG MAY BE IN NEW PLACE IN CORE
	POP	P,J		;RESTORE HIGH SEG NUMBER
	MOVE	R,JBTADR##(J)	;RESTORE HIGH RELOCATION
	POPJ	P,		;RETURN
     
;ROUTINE TO SEE IF SUM OF BOTH SEGMENTS WILL FIT INTO MAXIMUM SIZE
;OF PHYSICAL CORE TOGETHER
;CALL:	MOVE T1,REQUESTED LENGTH-1 OF SEG BEING ASSIGNED(LOW OR HIGH)
;		T1 IS ASSUMED TO HAVE 1777 ORED IN BEFORE CALL
;	MOVE J,JOB OR HIGH SEG NUMBER
;	PUSHJ P,SUMSEG
;	ERROR RETURN, CAN NEVER FIT
;	OK RETURN
;PREVIOUS CONTENTS OF T1 AND J PRESERVED ON BOTH RETURNS
;IF HIGH SEG, MUST BY CALLED AT UUO LEVEL, IF T1 NON-ZERO
;OK TO CALL FOR HIGH SEG AT CLOCK LEVEL, PROVIDED ASKING FOR 0 ONLY.
;SHUFFLER MODIFIED  SO IT NEVER CALLS SUMSEG(SINCE IT MIGHT SHUFFLE 
;HIGH SEGMENT AT CLOCK LEVEL WHICH IS NOT THE CURRENT USERS)
;THIS WILL BE TRUE AS LONG AS A CORE COMMAND IS NOT IMPLEMENTED WHICH
;ALLOWS USER TO ADJUST HIGH SEG TO ANYTHING BUT 0.
;CALLED FROM CORE UUO(CORE0 AND CORE1 ROUTINES)


SUMSEG::JUMPE	T1,CPOPJ1##	;ASKING FOR 0 CORE(IF YES, COULD BE
				;HIGH SEG AT CLOCK LEVEL)? HAVE NO IDEA WHAT LOW
				; SEG NO. IS HOWEVER, 0 ALWAYS WILL FIT
	JUMPL	J,CPOPJ1##	;PDB'S ALWAYS FIT
	PUSH	P,T1		;SAVE USER ARG FOR NO. WORDS -1 FOR A SEG
	PUSH	P,J		;SAVE LOW OR HIGH SEGMENT NUMBER
	CAILE	J,JOBMAX##	;IS REQUEST FOR A LOW SEG?
	MOVE	J,JOB##		;NO, ASSUME AT UUO LEVEL - GET CURRENT JOB#
	PUSHJ	P,CORBND##	;T1 = UPPER BOUND # WORD FOR THIS JOB
	MOVE	J,(P)		;RESTORE LOW OR HIGH SEG # FOR CORE REQUEST
	CAILE	J,JOBMAX##	;IS REQUEST FOR A LOW SEG?
	SKIPA	J,JOB##		;NO, ASSUME AT UUO LEVEL - GET CURRENT JOB #
	MOVE	J,JBTSGN##(J)	;YES, GET HIGH SEG NUMBER (IF ANY)
	MOVEI	T2,0		;ASSUME NO HIGH SEG
	JUMPLE	J,SUM1		;IF NEG OR 0 NONE
	PUSHJ	P,SEGSIZ##	;T2=SIZE OF SEG NOT BEING REQUESTED
IFN FTKI10!FTKL10,<
	CAIG	J,JOBMAX##	;HIGH SEGMENT REQUEST?
	SUBI	T2,UPMPSZ##	;YES, UPMP SIZE ALREADY ACCOUNTED FOR
>
	ASH	T2,P2WLSH##	;CONVERT FROM PAGES TO WORDS
SUM1:	ADD	T2,-1(P)	;DESIRED SIZE=OTHER SEG SIZE (WORDS)+
				; REQUEST (ORED 1777) SIZE-1 (OR 0)
	POP	P,J		;RESTORE REQUESTING SEG NO
	CAMLE	T2,T1		;IS DESIRED (LENGTH-1)+OTHER LENGTH .LE. JOB UPPER BOUND?
	JRST	TPOPJ##		;NO, ERROR
	JRST	TPOPJ1##	;YES, OK RETURN

     
;ROUTINE TO COMPARE CORTAL(FREE+DORMANT+IDLE CORE) WITH CORTAB(USE BIT TABLE)
;AND HALT IF INCONSISTENT.
;CALL:	PUSHJ P,CHKTAL	-AFTER CORTAL ADJUSTED
;	RETURN ONLY IF OK-ALL ACS SAVED

IFN FTHALT,<

CHKTAL::SOSLE	CHKCNT##	;CHECK ON THIS CALL?
				; MONGEN DEFAULT MD.CHK==^D32
	POPJ	P,		;NO, AVOID OVERHEAD (5 M:S.) MOST OF TIME
	PUSH	P,T1		;YES, SAVE ALL ACS USED
	MOVEI	T1,M.CMCT##	;CHECK MONITOR CORE MEMORY EVERY M.CMCT CALL
	MOVEM	T1,CHKCNT##	;SO REDUCE MONITOR CPU OVERHEAD
	PUSH	P,J
	PUSH	P,U
	MOVEI	U,0		;SET FREE+DORMANT+IDLE COUNT TO 0
	MOVE	J,CORE2P##	;POINTER TO CORTAB, 1 BIT BYTES
CHKT1:	ILDB	T1,J		;GET NEXT 1K BIT
	XCT	SKPPIU##	;IS THAT PAGE IN USE?
	ADDI	U,1		;NO, ADD 1 TO FREE COUNT
	CAME	J,CORLST##	;FINISHED CORTAB?
	JRST	CHKT1		;NO,
	MOVE	J,SEGPTR##	;YES, SCAN ALL HIGH SEGMENTS
CHKT2:	PUSHJ	P,CHKLKH	;CHECK FOR DORMANT OR IDLE (UNLOCKED)
	  JRST	CHKT3		;NO
IFE FTVM,<
	LDB	T1,PCORSZ##	;YES, THEREFORE DORMANT OR IDLE
	ADDI	U,1(T1)		;ADD SIZE IN K TO FREE
>
IFN FTVM,<
	LDB	T1,IMGIN##	;SEGMENT SIZE
	ADD	U,T1		;ADD IT TO TOTAL FREE
>
CHKT3:	AOBJN	J,CHKT2		;SCANNED ALL HIGH SEGS?
	CAME	U,CORTAL##	;YES, IS FREE+DORMANT+IDLE = BIT TABLE
	STOPCD	.,STOP,CMU,	;++CORE MESSED UP
	POP	P,U		;YES, RETORE ACS AND RETURN
	POP	P,J
	JRST	TPOPJ##		;RETORE T1 AND RETURN
>

;SUBROUTINE TO CHECK IF A JOB IS LOCKED (LOW OR HIGH SEG)
; OR IF HIGH SEG HAS NO DISK COPY - SET ERROR BIT TO STOP JOB
;ALWAYS SWAP JOB (LOW & HIGH SEG) OUT (UNLESS LOCKED)
; TO TRY TO FIX ERROR IF HIGH SEG HAS DISK COPY
;CALL:	MOVE J,JOB NO.
;	PUSHJ P,HGHPR1
;	ALWAYS RETURN

HGHPR1:
IFN FTLOCK,<			;LOCK UUO FEATURE?
	PUSHJ	P,LOKCHK##	;IS THIS JOB'S LOW OR HI SEG LOCKED?
	  PJRST	PARJB1##	;YES, STOP JOB GIVE ERROR MESSAGE
>
	PJRST	XPANDH##	;NO, SWAP HIM OUT AND ALL OTHER JOBS
				; IF THIS IS A HIGH SEG.
     
SUBTTL ERRCON - MONITOR DETECTED ERROR ROUTINES

;ILLEGAL MEMORY(ILM) DETERMINE IF WILD TRANSFER OR NOT
;CALL:	HRRZ T1,USER PC
;	PUSHJ P,SEGILM
;	LEGAL PC RETURN(MUST BE ILLEGAL REF)
;	ILLEGAL PC RETURN(MUST BE WILD TRANSFER)


SEGILM::MOVE	T3,T1		;SAVE THE PC
	PUSHJ	P,HSVAD		;GET STARTING AND HIGHEST LEGAL
				; HIGH SEGMENT ADDRESSES
	CAIL	T3,(T2)		;ABOVE THE START OF THE HIGH SEG?
	CAILE	T3,(T1)		;AND BELOW THE TOP OF HIGH SEG?
	AOS	(P)		;NO, ILLEGAL PC RETURN
	POPJ	P,		;YES, PC IN HIGH SEG(MUST BE ILLEGAL REF)

;SUBROUTINE TO PRINT "SWAP OUT CHN MEM PAR ERR" ON ALL JOBS USING HIGH SEG
; CLEAR NAME SO NO NEW USERS WILL GET
;CALL:	MOVE J,LOW OR HIGH SEG NO WHICH HAD MEM PAR ERR ON SWAP OUT
;	PUSHJ P,HGHSWE
;	  HERE IF HIGH SEG
;	SKIP RET IF LOW SEG

IFN FTSWAP,<			;SWAPPING (NOT UNDER FTMEMPAR)
HGHSWE::CAIG	J,JOBMAX##	;IS THIS A HIGH SEG?
	JRST	CPOPJ1##	;NO, SKIP RETURN
	PUSHJ	P,CLRNAM	;CLEAR HIGH SEG NAME SO NO NEW USERS WILL GET
	MOVEI	T1,SWOMES##	;ADR OF SUB IN ERRCON WHICH PRINTS
				; SWAP OUT MEM PAR ERR
	PJRST	HGHAPP		;GO APPLY IT TO ALL JOBS USING HIGH SEG
>

;SUBROUTINE TO HANDLE SPURIOUS PARITY ERROR (MAYBE READ PAUSE WRITE)
;BLAME ALL JOBS SHARING HIGH SEG OF CURRENT JOB IF NO DISK COPY
;ELSE JUST SWAY OTHERS OUT
;CALL:	MOVE	J,CURRENT JOB NO.
;	PUSHJ	P,HGHSPE
;	ALWAYS RETURN - J - PRESERVED

IFN FTMEMPAR,<			;MEMORY PARITY FEATURE?
HGHSPE::PUSH	P,J		;SAVE CURRENT JOB NO.
	SKIPLE	J,JBTSGN##(J)	;DOES CURRENT JOB HAVE A HIGH SEG?
	PUSHJ	P,HGHPAR	;YES, EITHER FLAG JOBS FOR ERROR OR SWAP OUT
	JRST	JPOPJ##		;RESTORE CURRENT JOB NO AND RETURN
     

;SUBROUTINE TO HANDLE MEM PARITY IN A HIGH SEG
;CAUSE ALL JOBS TO BE SWAPPED OUT (SO GOOD DISK COPY GET USED IF ONE)
;IF HIGH SEG DOES NOT HAVE COPY SWAPPED OUT, ALL JOBS GET ERROR MESSAGE
;CALL:	MOVE	J, HIGH SEG NUMBER
;	PUSHJ	P,HGHPAR
;	ALWAYS RETURN

HGHPAR::MOVEI	T1,HGHPR1	;APPLY SUB TO SWAP OUT ALL JOBS USING HIGH SEG
				; UNLESS LOCKED OR NO DISK COPY
	LDB	T2,IMGOUT##	;ALLOCATION DISK
	PJUMPN	T2,HGHAPP	;IF A COPY THERE, JUST SWAP OUT ALL JOBS USING IT
				; GIVE ERROR MESSAGES TO LOCKED JOBS
	PUSHJ	P,CLRNAM	;CLEAR SEGNAME SO NO ONE NEW WILL USE
				; (RETURN DISK AND CORE SPACE IF DORMANT)
	MOVEI	T1,PARJB1##	;SUB TO FLAG JOB TO GET MESSAGE AND STOP
				;FALL INTO SUB TO APPLY PARJOB TO ALL JOBS

>				;END FTMEMPAR
;ROUTINE TO APPLY A SUB TO EACH JOB WITH THIS HIGH SEG
;CALL:	MOVEI T1,ADR OF SUB. TO BE APPLIED
;	MOVE J,HIGH SEG NUMBER
;	PUSHJ P,HGHAPP
;	ALWAYS RETURN -J CLOBBERRED

HGHAPP::PUSHJ	P,SAVE1##	;SAVE P1
	PUSH	P,T1		;PUT SUB ADR ON STACK
	HRRZ	P1,J		;REMEMBER BAD HIGH SEG
	MOVE	J,HIGHJB##	;SCAN ALL JOBS FROM HIGHEST DOWN
;LOOP TO SCAN FOR ALL JOBS USING BAD HIGH SEG
HGHPLP:	SKIPG	T1,JBTSGN##(J)	;THIS JOB HAVE A HIGH SEG?
HGHPL1:	SOJG	J,HGHPLP	;NO, RUN OUT OF JOBS?
	JUMPLE	J,TPOPJ##	;YES, FINISHED?
	CAIN	P1,(T1)		;NO, IS THIS HIGH SEG THE BAD ONE?
	PUSHJ	P,@(P)		;YES, CALL SUB AS SPECIFIED BY CALLER
	JRST	HGHPL1		;GO BACK FOR MORE JOBS
     
SUBTTL	SAVGET - SAVE, GET, R, AND RUN COMMANDS

;THE LOWER CORE LOCATION 0-17 IN USER AREA ARE USED FOR TEMP STORAGE FOR SAVE-GET
;THEY ARE DEFINED IN BEGINNING OF SAVGET ROUTINE


;ROUTINE TO SET EXTENSION OF HIGH SEG
;SAME CALL AS SETEXT EXCEPT S NOT SETUP
;CALLED FROM GETARG(RUN, GET SEG UUO)


SETEX1::TLZ	S,NSRBIT	;TRY SHARABLE SEG FIRST(.SHR) THEN .HGH
				;FALL INTO SETEXT

;ROUTINE TO SET EXTENSION OF HIGH SEGMENT TO SHR(SSAVE,GET,R) OR HGH(SAVE)
;CALL:	HRLI S,0(GET,SSAVE,RUN) OR NSRBIT(SAVE)
;	MOVE R,XWD PROTECT,RELOC FOR LOW SEG
;	MOVEI T2, LOW FILE EXTENSION OR 0 IF NONE TYPED IN
;	PUSHJ P,SETEXT
;	RETURN T1 PRESERVED(TTY INPUT BYTE POINTER)
;	CALLED FROM COMMAND DECODER ROUTINE SGSET, T1 PRESERVED (INPUT BYTE POINTER)


SETEXT::
	HRLZM	T2,SGALOW##(R)	;SAVE EXTENSION USER TYPED FOR LOW SEG
	MOVE	T2,[SIXBIT /SHRHGH/]	;ASSUME SSAVE COMMAND(OR  GET)
	TLNE	S,NSRBIT	;WAS IT?
	MOVSS	T2		;NO, EXCHANGE ROLES OF EXTENSIONS, MUST
				; BY SAVE

	MOVEM	T2,SGAHGH##(R)	;SAVE FOR ENTER AND RENAME OF HIGH SEGMENT

	POPJ	P,

;SUBROUTINE TO SETUP JOBHRL
;CALL:	MOVE	J,HI SEG NUMBER
;	MOVE	R,LOW SEG PROT,,RELOC
;	PUSHJ	P,SETHRL
SETHRL::HLRZ	T2,JBTADR##(J)	;LENGTH-1 OF HIGH SEG
	ADDI	T2,1		;LENGTH
	HLLZ	T1,JOBHRL##(R)	;RELATIVE FIRST FREE LOC IN HIGH SEG(SET BY LOADER)
	TLNE	T1,777777	;HAS LOADER SET IT (OR IS THIS SEG CREATED
				; BY CORE, GETSEG, OR REMAP BY USER PROG)?
	SKIPE	USRDDT##	;YES, IS USER USING DDT?
	HRLM	T2,JOBHRL##(R)	;YES, SET FIRST FREE TO BE FIRST WORD BEYOND END(REL)
	POPJ	P,		;RETURN
     
;ROUTINE TO SAVE HIGH SEG IF IT EXISTS (AS.SHR(SSAVE) OR .HGH(SAVE))
;AND DETERMINE IF LOW SEG HAS DATA TO BE SAVED, OR NOT(IF YES IT WILL BE WRITTEN
;AS LOW UNLESS USER SPECIFIED DIFFERENTLY RATHER THAN.SAV)
;ALWAYS SAVE LOW SEG IF MAGTAPE OR IF NO HIGH SEG
;CALL:	MOVE J, JOB NUMBER (R,R,F SET UP)
;	PUSHJ P,SAVHGH
;	RETURN1 NO DATA IN LOW SEGMENT
;	RETURN2 SAVE LOW SEGMENT
;	J DESTROYED,SGAEXT RESTORED TO SAV OR USERS TYPED EXTENSION
;	SGALLEN RESTORED TO IOWD FOR LOW SEG IF DTA,PROJ R IF DSK
;	DEVICE INITED ON EITHER RETURN

SAVHGH::SKIPG	J,JBTSGN##(J)	;DOES CURRENT JOB HAVE A REAL HIGH SEGMENT?
	JRST	SAVLOW		;NO, RESTORE SGAEXT,SGALEN AND SKIP
				;RETURN SO LOW SEG WILL BE SAVED
				; USER HAS NO CHOICE(MAY BE HAD SPY SEG
	PUSHJ	P,SGH		;SET EXTENSION IN E+1 OF ENTER BLOCK
	PUSHJ	P,SETHRL	;SETUP JOBHRL
	HLRZ	T1,JOBHRL##(R)	;GET CORRECT FIRST REL FREE LOC IN HIGH SEG
	MOVNS	T1		;SET RH=-NO. OF WORDS TO WRITE, LH=-1
	PUSHJ	P,SG4##		;SETUP E+3
	PUSH	P,JOBVER##(R)	;SAVE LOWSEG VERSION
	MOVE	T1,JBTADR##(J)	;LOCATION OF HISEG
	SKIPE	T1,JOBHVR##(T1)	;HISEG VERSION NUMBER
	MOVEM	T1,JOBVER##(R)	;STORE FOR FILSER
	MOVNI	T1,4		;INDICATE HIGH SEGMENT SAVE IN PROGRESS
	HRLM	T1,USRHCU##	;SET LH OF HIGEST CHANNEL USED AS A FLAGE THAT
				; A SAVE IS IN PROGRESS FOR HIGH SEG
	HRLM	T1,JOBHCU##(R)	;ALSO SET IN JOB DATA AREA FOR ANYSAVE ROUTINE
				; TO SEE(CALLED FROM ANYACT)
	ENTER	0,SGANAM##	;ENTER NAME IN DIRECTORY(SHR OR HGH)
	JRST	[POP P,JOBVER##(R)  ;RESTORE LOWSEG VERSION
		JRST SAVERR##]	;  PRINT ERROR MESSAGE
	POP	P,JOBVER##(R)	;RESTORE VERSION
	PUSHJ	P,COPVJD	;COPY LOW SEGMENT JOB DATA AREA NUMBERS TO
				; VESTIGUAL JOBDAT
SAVSHR:
	PUSHJ	P,OUTHGH
	PUSHJ	P,SGIOCK##	;CHECK FOR TRANSMISSION ERRORS(RETURN IF OK)
	CLOSE	0,		;CLOSE OUTPUT(BEFORE LOOKUP FOR DELETE)
				; SO SHARABLE SEG WILL BE SUPERCEDE BY NEW FILE
				; IF SAME NAME
	HRLZ	T1,SGAHGH##(R)	;DELETE HGH IF SHR WRITTEN OR VICE VERSA
	PUSHJ	P,DELET		;DEPENDING IF THIS WAS A SSAVE OR SAVE COM.
	MOVSI	T1,'SAV'	;DELETE OLD .SAV
	PUSHJ	P,DELET		; FILE, IF ANY.
IFN FTEXE,<SETZ	T3,		;SET FLAG THAT WE CAME FROM "SAVE">
	PUSHJ	P,LOWCHK	;CHANGE EXTENSION FOR LOW FILE TO .LOW
				; UNLESS USER SUPPLIED HIS OWN ARG
	JRST	.+2		;DO NOT NEED LOW FILE WRITTEN
				; SO DELETE POSSIBLE PREVIOUS VERSION
				; SO USER WILL NOT BE CONFUSED
	JRST	CPOPJ1##	;LOW FILE NEEDED, DO NOT BOTHER TO DELETE
				; GIVE SKIP RETURN TO CALLER
	MOVE	T1,DEVMOD(F)
	TLNE	T1,DVMTA	;DEVICE A MAGTAPE?
	PUSHJ	P,SAVNUL	;YES, WRITE A NULL LOW SEGMENT.
	SKIPN	T1,SGALOW##(R)	;OBTAIN LOW SEGMENT NAME
	MOVSI	T1,(SIXBIT /LOW/)	;FILL IN DEFAULT IF NULL
	JRST	DELET		;DELETE FILE AND GIVE NO SKIP RETURN TO CALLER

SAVNUL:	HRROS	USRHCU##	;SET SAVE IN PROGRESS FLAG
	MOVNI	T1,JOBHRN##+1	;NEGATIVE SIZE OF RECORD TO OUTPUT
	HRLI	T1,JOBNSV##-1	;ADDRESS IN JOBDAT TO WRITE OUT NULL SAVE FILE
	MOVSM	T1,SGALEN##(R)	;SET UP OUTPUT IOWD
	MOVEI	T1,JOBHRN##	;ADDRESS IN FILE TO ZERO
	ADDI	T1,JOBNSV##(R)	;START OF FILE
	SETZM	(T1)		;CLEAR WORD TO INDICATE NULL FILE.
	OUTPUT	0,SGALEN##	;WRITE THE NULL SEGMENT
	HRRZS	USRHCU##	;CLEAR SAVE IN PROGRESS FLAG
	PUSHJ	P,SGIOCK##	;CHECK FOR ERRORS (RETURN IF OK)
	CLOSE	0,		;WRITE EOF
	POPJ	P,
     
;ROUTINE TO DIDDLE RELOCATION DURING DUMP MODE SAVE OPERATION OF HIGH SEGMENT
; WHEN IO OPERATION IS DONE IN MORE THAN ONE STEP
; IE WHEN DEVICE GOES INACTIVE IN MIDDLE(SAY BECAUSE CONTIGUOUS DISK SPACE
; EXHAUSTED) AND HIGH AND/OR LOW SEG MAY HAVE BEEN SHUFFLED OR SWAPPED
;CALLED ONLY AT UUO LEVEL
;CALL:	MOVE T1,[IOWD N,ADR (BEFORE RELOCATION)]
;	MOVE R,LOW SEG RELOC
;	PUSHJ P,SAVDDL
;	ALWAYS RETRUN - RH OF AC T1 ADJUSTED FOR NEW POSITION OF HIGH SEG
;	WITH RESPECT TO LOW SEG.  NO OTHER ACS CHANGED, LH AC T1 SAME
;CALLED FOR DISK SERVICE


SAVDDL::PUSH	P,J		;SAVE CONTENTS OF J
	MOVE	J,JOB##		;SETUP CURRENT JOB NUMBER
IFN FTEXE,<
	PUSHJ	P,CHKEXE##	;EXE FILE SAVE?
	  JRST	JPOPJ##		;YES, RESTORE J AND EXIT
>;END IFN FTEXE
	SKIPGE	USRHCU##	;IS A SAVE OR GET IN PROGRESS FOR CURRENT USER?
	SKIPG	J,JBTSGN##(J)	;YES, DOES CURRENT JOB HAVE A HIGH SEG?
	JRST	IPOPJ##		;NO, NO DIDDLING REQUIRED
	PUSH	P,T1		;SAVE T1
	HRRZ	T1,SGALEN##(R)	;ORIGIN-1 OF HIGH SEG WITH RESPECT TO LOW SEG
				; (STORED BY SAVGET AS COMMAND LIST)
	CAMGE	T1,USRREL##	;IS COMMAND LIST ABOVE LOW SEG?
	JRST	SAVDLW		;NO, MUST BE LOW SEG SAVE IN PROGRESS
	HRRZ	T1,JBTADR##(J)	;YES, ABSOLUTE ADDRESS OF HIGH SEG
	SUBI	T1,1(R)		; MINUS ABSOLUTE ADDRESS OF LOW SEG MINUS 1
				; EQUALS NEW HIGH SEG ORIGIN-1 W.R.T. LOW SEG
	SUB	T1,SGALEN##(R)	;MINUS OLD HIGH SEG ORIGIN-1 WITH RESPECT
				; TO OLD LOW SEG YIELDS CORRECTION FACTOR
	ADD	T1,(P)		;ADD CORRECTION FACTOR TO SAVDDL ARG
	HRRM	T1,(P)		;STORE ONLY RH (AVOID RH TO LH CARRY)
SAVDLW:	POP	P,T1		;RESTORE MODIFIED SAVDDL ARGUMENT TO T1
	JRST	IPOPJ##		;THEN RESTORE J AND RETURN
;SUBROUTINE TO SETUP THE VESTIGUAL JOB DATA AREA FROM THE JOB DATA
; AREA ON A SAVE IF NECESSARY
;CALLING SEQUENCE:
;	PUSHJ	P,SETVJD
;ALWAYS RETURNS CPOPJ

COPVJD::MOVE	J,.C0JOB##	;RESTORE JOB NUMBER
	SKIPLE	J,JBTSGN##(J)	;GET SEGMENT NUMBER FROM JOB NUMBER
	TLNE	J,SHRSEG	;IS HIGH SEG SHARABLE?
	POPJ	P,		;YES, DO NOT INITIALIZE VESTIGIAL JOB DATA AREA
				; SINCE IT MUST HAVE BEEN DONE WHEN HIGH FILE CREATED
				;  AND USER CANNOT MODIFY NOW
IFN FTVM,<
	LDB	T1,JBYHSO##	;PAGE WHERE HI SEG ORIGINS
	PUSHJ	P,YANKIT##	;MAKE SURE THE PAGE IS IN CORE
	  JRST	[PUSHJ P,CRPAGE##  ;ABZ, CREATE THE PAGE
		   JRST NEED1P	;CAN'T
		 JRST .+1]	;CONTINUE
	MOVE	J,.C0JOB##	;RESTORE JOB NUMBER
	MOVE	J,JBTSGN##(J)	;AND HI SEG NUMBER
>
	MOVE	T1,JBTADR##(J)	;NO, INITIALIZE VESTIGIAL JOB DATA AREA IN HIGH SEG
	SUBI	T1,1		;FROM LOW SEG JOB DATA AREA
	PUSH	T1,JOBSA##(R)	;SAVE JOB STARTING ADDRESS (RH)
				; AND INITIAL FIRST FREE LOC (LH)
	PUSH	T1,JOB41##(R)	;SAVE USER LOC 41(USER UUO HANDLER JSR)
	PUSH	T1,JOBCOR##(R)	;SAVE USER TYPED THIRD (CORE) ARG (RH) AND
				; HIGHEST DATA LOC LOADED IN LOW SEG (LH
	HRR	T2,JOBREN##(R)	;SAVE REENTER COMMAND STARTING ADDRESS
	HLL	T2,JOBHRL##(R)	;AND FIRST (REL) FREE LOCATION SET BY LOADER
	PUSH	T1,T2
	PUSH	T1,JOBVER##(R)	;SAVE BOTH HALVES OF PROGRAM VERSION NUMBER
				; LH IS USED FOR PROGRAMMER NUMBER LAST MODIFYING
	PUSH	T1,SGANAM##(R)	;SAVE NAME OF SEGMENT FOR DDT
	AOBJN	T1,.+1		;SKIP DDT SYMBOL TABLE POINTER
	PUSH	T1,[EXP 0]	;CLEAR 10 (OCTAL) FOR FUTURE
IFN FTKI10!FTKL10,<
	LDB	T2,JBYHSO##	;PAGE NUMBER WHERE HIGH SEGMENT ORIGINS
	DPB	T2,[POINT 9,(T1),17]
>
	POPJ	P,		;RETURN
;SUBROUTINE TO OUTPUT THE HIGH SEGMENT
;CALL:
;	PUSHJ	P,OUTHGH
;	RETURN HERE (I/O ERRORS NOT CHECKED FOR)
;
OUTHGH::HLLZ	T1,JOBHRL##(R)	;GET CORRECT FIRST REL FREE LOC IN HIGH SEG
	MOVNM	T1,SGALEN##(R)	;SET LH=-NO. WORDS TO WRITE, RH=0
				; SET AGAIN SINCE ENTER CHANGED E+3
	PUSHJ	P,FIXR		;FIX RELOCATION FOR HIGH SEG SAVE
	OUTPUT	0,SGALEN##	;WRITE ALL OF ORIGINAL HIGH SEG FROM EXTENDED LOW SEG
				; JBTADR FOR LOW SEG REMAINS SAME AS DOES
				; JOBADR,JOBDAT.  THE UUO HANDLER ASSUMES THAT
				; R AND P ARE ALREADY SETUP PROPERLY FOR UUOS
				; FROM MONITOR, AND SO DOES NOT SETUP R AND P
				; FROM JOBADR, CLOCK ROUTINE SAVES ACS IN JOB DATA
				; AREA USING JOBDAT, SHUFFLING AND SWAPPING COULD
				; EVEN TAKE PLACE BECAUSE
				; R AND P WOULD BE DIDLES PROPERLY
				; FOR NEW POSITION, IO SERVICE ROUTINES
				; WHICH CAN DO SAVES(DISK,MTA,U) MUST BE SAVE R
				; AT UUO LEVEL IN DEVICE DATA BLOCK, FOR
				; USE AT INTERRUPT LEVEL, INSTEAD OF SETTING
				; R UP FROM JBTADR WHEN INTERRUPT OCCURS.
				; IN CASE DUMP IO CANNOT GO TO COMPLETION
				; IN ONE OPERATION(IE JOB MUST BE RESCHEDULED)
				; SERVICE ROUTINE MUST CALL SAVDDL EACH TIME
				; TO DIDDLE RELOCATION IN CASE JOB SHUFFLED
				; OR SWAPPED AFTER PARTIAL IO COMPLETED
	MOVE	J,JOB##		;RESTORE JOB NUMBER OF CURRENT JOB
	MOVE	R,JBTADR##(J)	;RESTORE XWD,PROT,RELOC FOR LOW SEG
	HLRZM	R,USRREL##	;RESET USRREL
	HRRZS	USRHCU##	;NOW CLEAR SAVE IN PROGRESS FLAG IN
	HRRZS	JOBHCU##(R)	;PROTECTED PART OF MONITOR AND
	POPJ	P,		;RETURN
;SUBROUTINE TO ADJUST USER RELOCATION TO ENABLE A HIGH SEGMENT SAVE
; TO BYPASS THE MONITOR ADDRESS CHECKING
;
;CALL:
;	PUSHJ	P,FIXR
;	RETURN HERE ALWAYS
;
FIXR::
IFN FTKI10!FTKL10,<
	MOVEI	T1,.VJDT	;KI HIGH SEGMENTS ALWAYS HERE
>
IFN FTKA10,<
	HRRZ	T1,JBTADR##(J)	;ABSOLUTE ADDRESS OF HIGH SEG
>
	SUBI	T1,1(R)		;-ABSOLUTE ADDRESS OF LOW SEG-1
	HRRM	T1,SGALEN##(R)	;=REL ADR. OF HIGH SEG-1 AS SEEN FROM EXPANDED LOW SEG
	HLRZ	T2,JBTADR##(J)	;LENGTH-1 OF HIGH SEG
	ADDI	T1,1(T2)	;+LENGTH OF HIGH SEG=REL ADR OF LAST LOC IN HIGH SEG
				; AS SEEN FROM LOW SEGMENT
	HRL	R,T1		;INCREASE PROTECTION TO INCLUDE ALL OF HIGH SEGMENT
				; THIS MAY REACH OVER MONITOR AND OTHER USERS
				; IF HIGH SEG IS BELOW LOW SEG
	HLRZM	R,USRREL##	;ALSO CHANGE PROTECTION USED FOR ADDRESS CHECKING
	POPJ	P,		;RETURN
;UUO TO GET JUST HIGH SEGMENT, USER CHANNELS 1 THRU 17 UNTOUCHED
;CALL:	MOVEI AC,D	;WHERE D IS LOCATION OF 5 ARG(SEE RUN UUO)
;			;THE 6TH(CORE ARG IS IGNORED) BUT MUST BE IN LEGAL MEMORY
;	CALL AC,[SIXBIT /GETSEG/] OR CALLI AC,40
;	ERROR RETURN UNLESS LH=HALT
;	OK RETURN


UGETHI::SKIPN	TWOREG##	;IS HARDWARE 2 RELOC REG
	JRST	UGTERR##	;NO, ERROR RETURN TO USER UNLESS HALT IN LH
	PUSHJ	P,SAVE1##	;SAVE P1

	PUSHJ	P,GETARG##	;YES,GET 6 ARGS FROM USER
IFN FTKI10!FTKL10,<
	MOVEI	T1,JS.XOR	;CLEAR EXECUTE STATUS
	ANDCAM	T1,JBTSTS##(J)
	MOVSI	T1,(JS.IGS)	;INDICATE IN GETSEG
	IORM	T1,JBTST2##(J)
>
IFN FTEXE,<
	MOVSI	T1,GTHGH##	;JUST GET THE HIGH SEG
>;END IFN FTEXE
	PUSHJ	P,GETSEG	;TRY TO SHARE HIGH SEG OR READ IN HIGH FILE
	  JRST	NOFILE##	;NO FILE FOUND, PRINT MESSAGE OR ERROR RETURN
IFN FTEXE,<
	  JFCL			;HIGH FILE IN, EVERYTHING OK
>;END IFN FTEXE
	PUSHJ	P,CHKMED	;CHECK TO SEE IF THIS IS SHARABLE SEG(EXT=SHR)
				; IF YES, SET MEDDLE BIT FOR THIS USER
				; SO HE CANNOT TURN OFF UWP OR CHANGE CORE ASSIGNMENT
IFN FTKI10!FTKL10,<
	MOVSI	T1,(JS.IGS)
	ANDCAM	T1,JBTST2##(J)	;CLEAR "IN GETSEG"
>
IFN FTVM,<
	MOVSI	T1,(UP.GET)
	ANDCAM	T1,.UPMP+.UPBTS	;GET NO LONGER IN PROGRESS
>
IFN FTWATCH&FTVERS,<
	PUSHJ	P,VERWAT##	;SEE IF WATCHING
	  JRST	GETHI1		;NO--RETURN
	PUSHJ	P,VERHGH##	;YES - PRINT HIGH VERSION
	PUSHJ	P,PGMFIL##	;PRINT FILE SPEC
	PUSHJ	P,PRRBKC##	;PRINT RIGHT BRACKET <CRLF>
>
GETHI1:	PUSHJ	P,SGREL##	;RELEASE JUST CHANNEL 0 IF WAS INIT
	JRST	TPOPJ1##	;OK(SKIP RETURN TO USER AFTER REMOVING USER ARG+AC
				;FROM PD LIST WHICH GETARG PUT ON
;ROUTINE TO ADD CURRENT USER TO A SHARED SEGMENT IF IT EXISTS OR
;TRY TO INITIALIZE A HIGH SEGMENT FROM A FILE AND MAKE IT SHARABLE(-SHR) OR NOT(.HGH)
;DEPENDING ON THE EXTENSION OF THE FILE
;CALL:	MOVE J, CURRENT JOB NUMBER
;	ACS F,R,R,P,SETUP
;IFN FTDISK,<MOVE T2,DEVMOD(F)>
;	PUSHJ P,GETHGH
;	RETURN1-HIGH SEG NOW IN ADDRESSING SPACE AND NO LOW FILE NEED BE GOTTEN
;	RETURN2 - HIGH SEG NOW IN ADDRESSING SPACE, AND LOW FILE NEEDED
;		OR NO HIGH FILES FOUND, TRY LOW FILE
;IFN FTEXE, <RETURNS - BOTH SEGS IN AND READY TO GO>
;	J RESTORED TO CURRENT JOB NUMBER
;	USER MODE FLAG CLEARED IN UUO PD LIST IR RUN UUO FROM HIGH SEG
;	WHICH IS NO LONGER SAME(SO NO ATTEMPT TO ERROR RETURN IF ERROR)

IFN FTDISK,<>

GETHGH::
IFN FTEXE,<
	MOVSI	T1,(JS.EXE)	;CLEAR EXE BIT
	ANDCAM	T1,JBTST2(J)
	MOVSI	T1,GTBTH##	;GET BOTH SEGMENTS
	SKIPN	TWOREG##	;2 RELOC CAPABILITY?
	TLZ	T1,GTHGH##	;NO, GET ONLY THE LOW SEG
>;END OF INF FTEXE
IFE FTEXE, <SKIPE	TWOREG##>  ;IS THIS A 2 RELOC REG. MACHINE?
	PUSHJ	P,GETSEG	;YES, TRY TO ATTACH TO HIGH SEG OR READ
				; IN HIGH FILE IF IT EXISTS, HIGH SEG OK?
	  JRST	NOHIGH		;NO, TRY TO READ IN LOW FILE OLD HIGH
				; SEG STILL IN ADR SPACE(AF ANY)
IFN FTEXE,<
	  SKIPA			;HIGH FILE OK, GO READ LOW FILE
	JRST	CPOPJ2##	;BOTH FILES IN, RETURN TO CALL+3
>;END OF IFN FTEXE
	MOVSI	T1,JOBSV##(R)	;CLEAR JOB DATA AREA
	HRRI	T1,JOBSV##+1(R)	;WHERE IO WOULD BE DONE INTO
	SETZM	JOBSV##(R)	;IF A LOW FILE EXISTS
	BLT	T1,JOBDA##-1(R)	;DO THIS IN CASE THERE IS NO LOW FILE
	SETZM	JOBDDT##(R)	;ALSO CLEAR DDT STARTING ADDRESS
	SETZM	USRDDT##	;IN MONITOR PROTECTED AREA AS WELL AS JOBDAT
				; IN CASE RETURN TO USER WITHOUT RESCHEDULING
	PUSHJ	P,ADJCOR	;COMPUTE AMOUNT OF CORE FOR BOTH LO & HI SEGS
				; FROM USER'S CORE ARG.(IF ANY)
				; RETURNS WITH HI SEG # IN T1
	HRRO	T1,JBTADR##(T1)	;ABSOLUTE ADDRESS OF HIGH SEG(LH=-1 FOR
				; POPS SO NO PDL UNDERFLOW)
	ADDI	T1,JOBPOP##	;ADD POP OFFSET
	POP	T1,JOBVER##(R)	;RESTORE BOTH HALVES OF PROG. VERSION NO.
	POP	T1,T2		;RESTORE
	HLLM	T2,JOBHRL##(R)	;RELATIVE FIRST FREE LOC IN HIGH SEG
	HRRZM	T2,JOBREN##(R)	;PROGRAM REENTER ADR, LH=0
				; SAVED FOR FUTURE USE
	POP	T1,JOBCOR##(R)	;HIGHEST DATA LOC IN LOW SEG(LH)
				; AMOUNT OF CORE IN RH TO BE ASSIGNED(#J-1)
	POP	T1,JOB41##(R)	;USER UUO HANDLER JSR
	POP	T1,JOBSA##(R)	;FIRST FREE LOC IN LOW SEG(LH), START COMMAND
				; STARTING ADDRESS(RH)
				;HERE FROM SAVE TOO
IFN FTEXE,<
	SETO	T3,		;FLAG THAT THIS IS "GET"
>
LOWCHK:	HRRZ	T1,JOBPD1##(R)	;IS CALL FROM HIGH SEGMENT?
	CAMLE	T1,USRREL##	;COMPARE PC WITH LOW SEG PROTECTION
	HRRZS	JOBPD1##(R)	;YES,(OR EXEC) SO CLEAR USRMOD PC FLAG IN LH
				; SO ERROR MESSAGES WILL NOT ATTEMPT TO ERROR
				; RETURN TO HIGH SEG WHICH IS NO LONGER
				; THE ORIGINAL ONE
	HLRZ	T1,JOBCOR##(R)	;DOES THIS JOB NEED A LOW SEG INPUT OR OUTPUT
	CAIGE	T1,JOBDA##	;I.E. IT THERE ANY DATA LOADED ABOVE JOB DATA AREA
	JRST	SG3##		;NO, RETURN AND DO NOT READ IN LOW FILE
IFN FTEXE,<
	JUMPE	T3,LOWCK2	;JUMP IF CAME FROM "SAVE"
	MOVSI	T1,(JS.EXE)	;WAS LOOKUP DONE?
	TDZE	T1,JBTST2##(J)
	JRST	LOWCK2		;YES
	MOVSI	T2,'EXE'	;SET UP TO CHECK FOR .EXE FILE
	MOVEM	T2,SGAEXT(R)	;STORE IN LOOKUP BLOCK
	SKIPL	F		;DEVICE INITED?
	PUSHJ	P,SGOPEN	;NO, DO IT
	PUSHJ	P,LUKFIL##	;LOOKUP .EXE FILE
	  JRST	LOWCK2		;NO FOUND--CHECK FOR .LOW
	MOVSI	P1,GTLOW##	;GET ONLY THE LOW SEGMENT
	PUSHJ	P,GETNEW	;READ IN THE .EXE FILE
	  JRST	GETH2B		;LOW SEG LOCKED
	JRST	CPOPJ2##
>;END OF IFN FTEXE
LOWCK2:	MOVSI	T2,(SIXBIT /LOW/)	;YES, READ OR WRITE LOW FILE WITH EXTENSION .LOW
	JRST	SETLOW

NOHIGH:	HRRZ	T1,JOBPD1##(R)	;(HERE ON GET COM. OR RUN UUO - NO NEW HIGH SEG
				; BUT NOT ON GETSEG UUO)
	CAMG	T1,USRREL##	;COMPARE PC WITH LOW SEG PROT.
	JRST	NOHIG1		;NO
	HRRZS	JOBPD1##(R)	;YES - CLEAR USRMOD BIT SO ERRORS WON'T
				;RETURN TO HI SEG WHICH IS NO LONGER
				;THE ORIGINAL ONE
NOHIG1:	PUSHJ	P,KILHSH	;REMOVE PREVIOUS HI SEG (IF ANY)
				; BOTH SOFTWARE AND HARDWARE-WISE
TRYLOW:	MOVE	T2,SGALOW##(R)	;(HERE ON SAVE)
				; LOW FILE EXTENSION USER TYPED OR 0 IF HE DIDN'T
SETLOW:
IFN FTEXE,<
	MOVSI	T1,(JS.EXE)	;CLEAR EXE BIT
	ANDCAM	T1,JBTST2##(J)
>
	MOVEM	T2,SGAEXT##(R)	;SET IN E+1 FOR LOOKUP
	AOS	(P)		;SET FOR NEED LOW READ OR WRITE RETURN
				; (HERE FROM SAVE IF NO HIGH SEG)
	JUMPL	F,SG3##		;HAS DEVICE ALREADY BEEN INITED?
				; INITB=1(BIT 0) IF YES
SGOPEN::OPEN	0,SGAMOD##	;NO, INIT DEVICE IN SAVMOD
	  JRST	SGERRA##		;ERROR, NOT AVAILABLE
	JRST	SG3##		;OK RESTORE SGALEN TO IOWD IF DTA,PP IF DSK AND RET

SAVLOW:	MOVE	T2,DEVMOD(F)
	TLNE	T2,DVMTA	;DEVICE MAGTAPE?
	PUSHJ	P,SAVNUL	;YES, WRITE A NULL HIGH SEGMENT
	JRST	TRYLOW		;RESTORE SGAEXT ETC,
     
;ROUTINE TO CREATE A NEW HIGH SEG AND READ IN HIGH FILE OR
;PLACE THE DESIRED SHARABLE HIGH SEGMENT IN CURRENT USER'S LOGICAL ADDRESSING SPACE
;CALL:	MOVE R,LOW SEG RELOC
;	MOVE F,ADR. OF DEVICE DATA BLOCK
;	MOVE J,JOB NUMBER
;IFN FTEXE,<MOVSI T1,FLAGS>
;	PUSHJ P,GETSEG
;	RETURN 1 - HIGH SEG OR FILE NOT FOUND
;	RETURN 2 - HIGH SEG FOUND AND IN LOG ADR. SPACE(IN FACT ALSO IN CORE)
;IFN FTEXE,<RETURN 3 - BOTH SEGS IN AND READY TO GO>


GETSEG::
IFN FTEXE,<
	MOVEM	T1,SGAEND##(R)
>
	PUSHJ	P,SGH		;SET E+3 TO 0 IF DTA,PPN IF DSK
	PUSH	P,F		;SAVE POINTER TO DDB
	SKIPE	USRJDA##+0	;CHANNEL 0 OPEN?
	RELEAS	0,		;YES, RELEASE IT
	POP	P,F		;RESTORE IT
	HLRZ	T2,SGAEXT##(R)	;GET USER PROVIDED EXTENSION
	CAIE	T2,'SHR'	;USER SPECIFIED SHARABLE SEGMENT?
	JUMPN	T2,GETH0	;NO, IF SPECIFIED, DO OPEN,LOOKUP
	TLNN	F,SYSDEV	;FROM SYS?
	JRST	GETH5		;NO
	MOVSI	T1,SYSSEG##	;SEGMENT FROM SYS BIT
	JUMPE	T1,GETH5	;AVOID SEARCH IF DEFINED EQUAL TO ZERO
	MOVS	T2,SGADEV##(R)	;USER SUPPLIED DEVICE NAME
	CAIE	T2,'SYS'	;UNQUALIFIED?
	JRST	GETH5		;NO
	PUSHJ	P,SYSNM##	;GET THE PPN OF USER'S SYS
	MOVE	J,SEGPTR##	;YES, SEARCH FOR A KNOWN SEGMENT
	SKIPN	T2,SGANAM##(R)	;NAME MATCH?
	POPJ	P,		;0 NAME - NO MATCH
GETH3:	CAME	T2,JBTNAM##(J)	;NAME MATCH?
GETH4:	AOBJN	J,GETH3		;NO, TRY THE NEXT ONE
	JUMPGE	J,GETH5		;IF NO MORE, QUIT
	CAMN	T3,JBTPPN##(J)	;FROM THIS USER'S SYS?
	TDNN	T1,JBTSTS##(J)	;NAME MATCHES, FROM SYS?
	JRST	GETH4		;NO, LOOK FOR ANOTHER MATCH
	HRLI	J,SHRSEG	;YES, FLAG AS SHARABLE
	JRST	GETH0A		;SKIP OPEN, LOOKUP, AND CLOSE UUOS
GETH5:	MOVE	T2,DEVMOD(F)	;EXTENSION 'SHR' OR ZERO, SEARCH KNOWN SEGS,
	TLNE	T2,DVDSK	;SKIP IF NOT A DISK
	SKIPA	T1,SGAPPN##(R)	;USE PROVIDED PPN
	MOVEI	T1,0		;IF NON-DISK, PPN ARG=0
	MOVEI	T2,DSKDDB##
	CAIN	T2,(F)		;LOOKING AT PROTOTYPE DDB?
	SKIPA	U,SGADEV##(R)	;YES, USE USER PROVIDED DEVICE
	SKIPE	U,DEVNAM(F)	;NO, USE PHYSICAL NAME FROM DDB
	SKIPE	T2,SGANAM##(R)	;SEG NAME
	PUSHJ	P,SRCSEG	;SEARCH KNOWN SEGMENT TABLE
	  JRST GETH0		;NOT FOUND, GO DO OPEN AND LOOKUP IN CASE ON TAPE
GETH0A:
IFN FTFDAE,<
	LDB	T1,[POINT HSASIZ,JBTSTS##(J),HSAPOS]
	TRNN	T1,400		;SIGN BIT OF OWNER PROTECTION FIELD ON?
				;YES, DO THE LOOKUP SO FILE DAEMON WILL GET
				; CALLED IF IT SHOULD BE
>
	JRST	GETH1		;FOUND, CHECK ACCESS PRIVILEGES

GETH0:	OPEN	0,SGAMOD##	;NO, TRY TO ASSIGN DEVICE
	  JRST SGERRA##		;CAN'T, GO PRINT ERROR OR RETURN TO USER.
IFN FTEXE,<
	MOVE	T1,DEVMOD(F)	;GET DEVICE BITS
	TLNE	T1,DVDTA	;DTA?
	JRST	GETH5A		;YES, SKIP LOOKUP
	MOVSI	T1,'EXE'	;GET EXTENSION OF EXE
	EXCH	T1,SGAEXT(R)	;PUT IT IN LOOKUP BLOCK AND SAVE OLD EXT
	PUSH	P,T1		;SAVE OLD EXTENSION
	HLRZ	T1,SGALOW##(R)	;GET USER EXTEN.
	SKIPE	T1		;IS THERE ONE?
	CAIN	T1,'EXE'	;OR IS IT "EXE"?
	LOOKUP	SGANAM##	;IS THERE AN .EXE FILE?
	  SKIPA	T1,SGAEXT##(R)	;NO
	JRST	[POP	P,(P)	;YES, GO READ IT IN
		 JRST	GETH6]
	HRRZS	T1		;ERROR CODE
	JUMPN	T1,NOFILE##	;REPORT ERROR IF NOT FILE NOT FOUND
	POP	P,SGAEXT(R)	;RESTORE EXTENSION
GETH5A:	SETZM	SGAEND##(R)	;CLEAR FLAGS, WE'RE THRU WITH THEM
	MOVSI	T1,(JS.EXE)	;SET EXE BIT
	IORM	T1,JBTST2##(J)	;IN JOB STATUS TO INDICATE LOOKUP FAILED
>;END OF IFN FTEXE
	MOVE	T1,DEVMOD(F)	;IS THIS MAG TAPE? (MAG TAPE CAN NEVER BE SHARABLE)
	TLNN	T1,DVMTA	;(MAG TAPE LOOKUPS ALWAYS SUCCEED)
	LOOKUP	0,SGANAM##	;NO, LOOKUP ON HIGH FILE WITH EXT .SHR
	  JRST TRYHGH		;NOT FOUND, TRY EXTENSION OF .HGH
GETH6:	PUSHJ	P,FNDSEG	;DOES SEGMENT ALREADY EXIST?
IFE FTEXE,<
	  JRST GETH2		;NO, TRY TO READ IN HIGH FILE
>
IFN FTEXE,<
	  JRST	[HLRZ	T1,SGAEXT##(R)	;GET LOOKUP'D EXTENSION
		 CAIN	T1,'EXE'	;EXE?
		 JRST	EXEFND		;YES
		 JRST	GETH2]		;GO READ IN HIGH FILE
>
	JRST	GETH1A		;PROTECTION WAS CHECKED BY THE LOOKUP
GETH1:	MOVEI	P1,PR.RED##	;YES, CHECK EXECUTE ONLY PROTECTION
	MOVE	P2,JOB##	;FOR CURRENT JOB
	PUSHJ	P,CHKHSA	;WITH FILE WHICH INITIALIZED HIGH SEG
	POPJ	P,		;CAN'T READ-DTA,MTA NOT AVAIL OR DSK ACCESS RIGHTS
				; NOT PROPER, GIVE HIGH SEG RETURN TO GETSEG
GETH1A:	PUSH	P,J		;SAVE HIGH SEG NUMBER
	SKIPN	USRJDA##+0	;HAS CHANNEL 0 BEEN INITED? (IF NOT-ITS
				; A DTA, DO NOT DO A CLOSE WHICH WOULD CLEAR F)
	JRST	SHARE		;GO SHARE HIGH SEGMENT
	CLOSE	0,CLSNMB	;DO A CLOSE WHICH WILL RETAIN CORE BLOCKS
				; EVEN THOUGH NO IO.
	PUSHJ	P,FNDSEG	;AFTER CLOSE CAN STILL FIND HIGH SEG?
	  JRST	GETH2A		;NO, (RARE) CAN HAPPEN ONLY IF FILE GETS
				; SUPERSEDED DURING CLOSE BY ANOTHER JOB
				; OR DORMANT SEG DELETED TO CLEAN UP SWAP SPACE
	JRST	SHARE		;CAN READ, GO SHARE HIGH SEGMENT

;COME HERE IF THERE IS AN .EXE FILE TO BE READ IN
;
IFN FTEXE,<
EXEFND:	MOVE	P1,SGAEND##(R)	;GET SAVED FLAGS
	PUSHJ	P,GETNEW	;READ IN NEW SAVE FILE
	  JRST	GETH2B		;ERROR, FLUSH THIS USER
	TLNE	P1,GTLOW##	;WAS IT A GETSEG?
	JRST	SETVJD		;NO, SET UP VEST JOB DAT
	TLNE	P1,GT%GTH	;WAS IT SUCCESSFUL?
	JRST	NOVJD		;YES, DON'T SET UP VEST JOB DATA AREA
	HLLZS	SGAEXT##(R)	;NO HI SEG IN THE FILE, SO FNFERR
	POPJ	P,		;EXIT
SETVJD:	TLNN	P1,GT%GTH	;IS THERE A HI SEG?
	JRST	NOVJD		;NO, DONT SET IT UP
	PUSHJ	P,ADJCOR	;SUBTRACT HIGH SEG SIZE FROM USER'S
				; CORE ARGUMENT
IFN FTVM,<
	MOVE	J,JBTSGN##(J)	;HISEG START
	LDB	T1,JBYHSO##	;GET HISEG START
	MOVE	J,JOB##		;RESTORE JOB #
	TLNN	P1,SV%SHR	;SHARABLE
	PUSHJ	P,YANKIT##	;BRING PAGE IN, IF OUT
	  JFCL			;SHOULDN'T BE ABLE
>	;END OF FTVM
	MOVE	T2,JBTSGN##(J)	;GET HIGH SEGMENT NUMBER
	MOVE	T2,JBTADR##(T2)	;GET ITS STARTING ADDRESS
	MOVE	T1,JOBSA##(R)	;SET UP VESTIGIAL JOB DATA AREA
	MOVEM	T1,JOBHSA##(T2)	;SET UP JOBSA
	MOVE	T1,JOB41##(R)	; AND JOB 41
	MOVEM	T1,JOBH41##(T2)
	MOVE	T1,JOBCOR##(R)	; AND JOBCOR
	MOVEM	T1,JOBHCR##(T2)
	MOVE	T1,JOBHRL##(R)	; AND JOBHRL
	HRR	T1,JOBREN##(R)
	MOVEM	T1,JOBHRN##(T2)
	MOVE	T1,JOBVER##(R)	; AND JOBVER
	MOVEM	T1,JOBHVR##(T2)
NOVJD:	AOS	(P)		;BUMP RETURN ADDRESS TO CALL+1
	TLNN	P1,SV%SHR	;DID WE FIND A SHARABLE HIGH SEG?
	JRST	CPOPJ1##	;NO, THEN, EXIT TO CALL+3
	PUSHJ	P,FNDSEG	;IS THIS AN ACTIVE SEGMENT?
	  JRST	STONAM		;NO, GO STORE NAME OF SHARABLE HIGH SEG
				; AND EXIT TO CALL+3
	SOS	(P)		;YES, WE MUST UNDO THE AOS SO THAT WE
				; WILL RETURN TO CALL+2
	JRST	GETH2C		;GO SET UP ITS PARAMETERS
>;END OF IFN FTEXE
;HERE ON RACE CONDITION - FILE SUPERSEDED DURING CLOSE (VERY RARE)
GETH2A:	POP	P,J		;REMOVE HIGH SEG NO.
	PUSHJ	P,SGH2		;RESET E+3(PPN) FOR LOOKUP
	LOOKUP	0,SGANAM##	;LOOKUP FILE AGAIN
	  JRST TRYHGH		;CAN'T FIND IT THIS TIME, GO TRY HGH
GETH2:	PUSHJ	P,INPSEG	;FOUND, GO READ IN HIGH FILE
GETH2B:
IFN FTLOCK!<FTKI10!FTKL10>,< 
	  JRST	NOCORE		;LOW SEGMENT IS LOCKED
>
IFE FTLOCK!<FTKI10!FTKL10>,< 
	  STOPCD	.+1,DEBUG,SCR,	;++SEGMENT COULDN'T BE READ
>
	PUSHJ	P,FNDSEG	;SEE IF FILE NAME SNUCK INTO SEGMENT TABLE
				; FOR OTHER USER WHILE IO GOING ON(UNLIKELY)
	  JRST STONAM		;USUALLY NOT FOUND
GETH2C:	PUSH	P,J		;FOUND, SAVE SHARABLE+NAMED HIGH SEG NO. TO BE
				; SHARED BY THIS USER
;HERE TO CONNECT JOB TO KNOWN HIGH (SHARABLE) SEG (SEG # ON PD LIST)
SHARE:	PUSHJ	P,KILHGC	;REMOVE PRESENT HIGH SEG FROM LOGICAL ADR.
				; SPACE(DELETE CORE SPACE IF JUST ASSIGNED BY
				; INPSEG SINCE NOT NAMED YET)
;HERE TO SEE IF THE HIGH SEGMENT WILL OVERLAP THE LOW SEGMENT
IFN FTKI10!FTKL10,<
	MOVE	J,(P)		;NEW HIGH SEGMENT NUMBER
	LDB	T2,JBYHSO##	;PAGE NUMBER OF ORIGIN OF HIGH SEGMENT
IFN FTVM,<
	LDB	T1,IMGIN##	;CURRENT SIZE OF THE HIGH SEGMENT
	ADD	T1,T2		;TOP PAGE OF THE HIGH SEGMENT
	PUSHJ	P,CKNZP##	;WILL HIGH AND LOW SEGMENTS OVERLAP?
	  JRST	SHROVL		;YES, LOSE
>>
IFN FTSWAP,<
IFE FTVM,<IFN <FTKI10!FTKL10>,<
	MOVE	J,.C0JOB##	;CURRENT LOW SEGMENT NUMBER
	LDB	T1,PCORSZ##	;SIZE OF THE LOW SEGMENT
	CAIL	T1,(T2)		;WILL THE HIGH SEGMENT OVERLAP THE LOW?
	JRST	SHROVL		;YES, LOSE
>
	MOVE	T1,0(P)		;PICK UP HIGH SEG NUMBER
	MOVEM	T1,JBTSGN##(J)	;SET FOR CORBND
	PUSHJ	P,CORBND##	;GET JOB CORE LIMIT
	MOVE	T3,T1		;SAVE FOR LATER
	SETZM	JBTSGN##(J)	;CLEAR JBTSGN IN CASE OF ERROR
	PUSHJ	P,SEGSIZ##	;CALCULATE SIZE OF LOW SEGMENT IN K
	MOVE	T1,T2		;SAVE LOW SEGMENT SIZE
IFN FTKI10!FTKL10,<
	SUBI	T1,UPMPSZ##	;CORBND DOESN'T COUNT UPMP SIZE
>
>
	POP	P,J		;RESTORE NEW HIGH SEG. NUMBER
	PUSHJ	P,SEGSIZ##	;YES, T2=SIZE IN K OF HIGH SEGMENT
IFE FTVM,<
	ADDI	T1,(T2)		;SIZE OF JOB (LOW + HIGH SEGS) IN K
	LSH	T1,P2WLSH##	;SIZE IN WORDS
	SKIPL	JBTSTS##(J)	;IS THIS HIGH SEG DORMANT (SNA=0)?
	CAMG	T2,VIRTAL##	;YES, WILL ALLOWING THIS SEGMENT TO BECOME ACTIVE
				; EXCEED VIRTUAL CORE ?
	CAMLE	T1,T3		;NO, WILL ALLOWING THIS SEGMENT IN ADDRESSING SPACE
				;EXCEED LARGEST LEGAL JOB SIZE?
	SOJA	T1,SHRNRM	;YES, DO NOT LET HIM SHARE DORMANT COPY, EVEN
				; THOUGH IT MAY BE ON DISK, BECAUSE THIS
				; WOULD OVER COMMIT THE SYSTEM AND MEAN
				; THAT NOT ABLE TO SWAP OUT BIGGEST JOB
>
IFN FTVM,<
	SKIPL	JBTSTS##(J)	;DORMANT SEGMENT
	CAMG	T2,VIRTAL##	;YES, WILL ALLOWING IT TO BECOME ACTIVE MAKE VIRTAL
				; NEGATIVE
	PUSHJ	P,GSZSG##	;NO, SEE IF LOW + NEW HIGH WILL STILL FIT
	  JRST	SHRNRM		;TOO BIG
IFN FTEXE,<PUSHJ P,[PUSH P,T2
		    PUSH P,J
		    PUSHJ P,PAGLOW##
		    POP P,J
		    JRST T2POPJ##]>  ;PAGE OUT LOW SEG
IFE FTEXE,<PJRST GETGTS##>	;LET "GET" DO IT
>
	MOVNS	T2		;NO,-NO. OF K IN DORMANT SEG
	SKIPL	JBTSTS##(J)	;IS THIS HIGH SEG ALREADY ACTIVE (SNA-1)?
	ADDM	T2,VIRTAL##	;NO, DECREASE COUNT OF VIRTUAL CORE AVAILABLE
>
IFN FTEXE,<
	MOVEI	T1,JS.SFE	;SEGMENT IS FROM AN EXE FILE BIT
	MOVSI	T2,'EXE'	;ASSUME IT WAS
	TDNE	T1,JBTSTS##(J)	;WAS IT?
	MOVEM	T2,SGAEXT##(R)	;YES, STORE EXTENSION FOR THE PROGRAM
>
	MOVEI	T3,0		;FLAG CALL IS GETSEG OR RUN UUO
				; (SO THAT CONNECTING TO AN IDLE SEG
				; WILL BE CHECKED).
     

;HERE FROM CORE AND REMAP UUOS - WITH T3 NON-ZERO AS A FLAG
; NOT TO CHECK FOR CONNECTING TO AN IDLE HI-SEG (SINCE IT LOOKS LIKE
; ONE BUT CANNOT BE)
SHARE1::MOVE	T1,.C0JOB##	;CURRENT JOB NUMBER
IFN FTHALT,<
	PUSHJ	P,COMIT		;COMPARE J TO JBTSGN(T1), HALT IF DIFF. SEGS
>
	MOVEM	J,JBTSGN##(T1)	;MAKE SURE THIS HIGH SEG IS MARKED AS BEING
				; IN CURRENT USER'S LOGICAL ADR. SPACE
	MOVSI	T2,SNA		;MAKE SURE THIS HIGH SEG IS FLAGGED AS IN USE
				; IE SEG IN SOME USER'S LOG. ADR. SPACE
	IORB	T2,JBTSTS##(J)	;MAY HAVE BEEN JUST ASSIGNED OR DORMANT
IFN FTSWAP,<
	MOVE	U,JBTSTS##(T1)	;JOB STATUS WORD
	TLNN	T2,JXPN!SWP	;IS HIGH SEG IN CORE AND NOT EXPANDING?
	JRST	SHARE3		;YES, GO INCREMENT IN CORE COUNT FOR JOB
	TLNN	U,JXPN		;JOB WAITING TO EXPAND LOW OR HIGH SEG?
	TLNE	T2,JXPN		;NO, IS HIGH SEG WAITING TO EXPAND?
	JRST	SETRL0		;YES, WAIT TILL HIGH SEG IN CORE TOO
	MOVSI	U,SWP		;NO, SET SWP BIT SO JOB CANNOT BE RUN
	IORM	U,JBTSTS##(T1)	;UNTIL HIGH SEG IS SWAPPED IN
	MOVSI	U,(JS.NNQ)	;ICPT IGNORED WITH SWP SET. DON'T ASSIGN
	IORM	U,JBTST2##(T1)	;NEW QUANTA IF JUST HIGH SEG COMES IN.
				;BECAUSE LOW SEG HASN'T GONE OUT
	JRST	SETRL2		;GO CALL SCHEDULER
>

;HERE IF HIGH SEG ALREADY IN-CORE (CORE, REMAP, RUN, GET SEG UUOS)
SHARE3:	SKIPN	T3		;CORE OR REMAP (HI-SEG LOOKS IDLE BUT ISN'T)?
	PUSHJ	P,CHKIDL	;NO, CHECK TO SEE IF HI-SEG IS IDLE IN CORE
				; IE IN-CORE COUNT 0 BUT IN SOME USER'S
				; ADR SPACE WHO IS SWAPPED OUT OR DORMANT IN CORE
				; (IE IN NO ONE'S LOGICAL ADR. SPACE)
				;IF YES, DECREASE COUNT OF FREE CORE (CORTAL)
	MOVE	T1,JOB##	;CURENT JOB NO. (FOR INCCNT)
	PUSHJ	P,INCCNT	;NO, INCREMENT HIGH SEG IN-CORE COUNT FOR THIS
				; JOB(T1) IF NOT ALREADY INCREMENTED, RETURN
				; WITH J = HIGH SEG NO.
IFN FTHALT,<
	PUSHJ	P,CHKTAL	;CHECK CORTAL WITH CORTAB, HALT IF DIFF.
>
	TLNN	U,JXPN		;IS JOB (LOW SEG) EXPANDING?
	JRST	SETRL1		;NO, SET HARD AND SOFT RELOC AND RETURN
	JRST	SETRL0		;YES, SWAP JOB OUT
				; (CAN HAPPEN IF CORE UUO WHICH
				; ASSIGNS HIGH SEG AND EXPANDS LOW SEG)

;HERE WHEN NOT ENOUGH ROOM IN SYSTEM TO ALLOW USER TO USE SEG
;T1=NO. WORDS-1=SUM OF BOTH SEGS-1
IFN FTKI10!FTKL10,<
SHROVL:	SKIPA	T3,[EXP LOHERR]	;HIGH AND LOW SEGMENTS OVERLAP
>
SHRNRM:	MOVEI	T3,NECERR	;NOT ENOUGH CORE
	HRRZ	T2,JOBPD1##(R)	;GET RETURN PC IN CASE THIS IS RUN UUO
	CAMLE	T2,USRREL##	;IS IT IN THE HIGH SEG WHICH IS NOW GONE ?
	HRRZS	JOBPD1##(R)	;YES, TURN OFF USER MODE FLAG, SO ERROR
				; WILL NOT LOOK FOR HALT AFTER UUO
	PUSH	P,T1		;SAVE NO. OF WORDS-1 NEEDED FOR MESSAGE IN NROOM
	PUSH	P,T3		;SAVE ERROR CODE
	PUSHJ	P,SETMAP##	;GO SET HARDWARE RELOC. SO JOB
				; CANNOT REFERENCE HIGH SEG
	POP	P,T1		;T1 = ERROR CODE
	JRST	NROOM##		;GO PRINT MONITOR MESSAGE AND STOP JOB
				; OR ERROR RETURN TO LOW SEGMENT - 0(P)
				; HAS #WORDS-1
				; (UNLESS UUO HAS A HALT FOLLOWING IT)
     
STONAM:	MOVE	T3,JOB##	;CURRENT JOB NUMBER
	MOVSI	J,SHRSEG	;FLAG THIS USER AS USING A SHARABLE HIGH SEG
	IORB	J,JBTSGN##(T3)	;AND SETUP HIGH SEG NUMBER
IFN FTDISK,<MOVEM T1,JBTPPN##(J)>	;STORE DIRECTORY
	MOVEM	T2,JBTNAM##(J)	;STORE HIGH SEG FILENAME
	MOVEM	U,JBTDEV##(J)	;STORE DEVICE NAME(STR) FOR HIGH SEG
	MOVSI	T1,SHRSEG	;ALSO FLAG HIGH SEG ASSIGNED
	IORM	T1,JBTSTS##(J)	;BY INPSEG AS SHARABLE FOR OTHER USERS
IFN FTDISK,<
	MOVE	T1,SGADAT##(R)	;GET E+2
	LDB	T1,[POINT 9,T1,8]	;GET ACCESS RIGHTS FROM LOOKUP E+2
	DPB	T1,[POINT 9,JBTSTS##(J),HSAPOS]	;AND STORE IN MONITOR WITH HIGH
				; SEG STATUS
>
IFN FTEXE,<
	HLRZ	T1,SGAEXT##(R)	;EXTENSION
	MOVEI	T2,JS.SFE	;SEGMENT FROM AN EXE FILE BIT
	CAIN	T1,'EXE'	;DID THE SEGMENT COME FROM AN EXE FILE?
	IORM	T2,JBTSTS##(J)	;YES, REMEMBER THAT
>
	MOVS	T1,SGADEV##(R)	;GET ORIGIONAL DEVICE NAME
	TLNE	F,SYSDEV	;FROM SYS
	CAIE	T1,'SYS'		;AND AN UNQUALIFIED 'SYS'?
	JRST	STON1		;WRONG
	MOVSI	T1,SYSSEG##	;YES, FLAG AS GOTTEN FROM SYS
	IORM	T1,JBTSTS##(J)
STON1:
IFN FTSWAP,<
SETRL0:	MOVE	J,JOB##		;CURRENT JOB NUMBER TO BE EXPANDED
				; (NOT HIGH SEG)
	PUSHJ	P,XPANDH##	;FLAG THIS USER AS TRYING TO EXPAND(SWP=1)
				; SO THAT THIS NEW HIGH SEG WILL BE SWAPPED IN
SETRL2:	PUSHJ	P,WSCHED##	;WAIT TILL IT IS SWAPPED IN(CALL SCHEDULER)
>
SETRL1:
IFN FTMS&FTKL10,<
	PUSHJ	P,CHKSWS##	;SEE IF ATTACHING TO A SHARABLE
				; WRITEABLE IH SEG. IF SO, MAKE
				; SURE ITS UNCACHED IN THIS JOBS MAP
>
IFN FTKI10!FTKL10,<
	MOVE	J,.C0JOB##	;CURRENT JOB
	MOVEI	T1,JS.XOR
	MOVSI	T2,(JS.IGS)	;YES, GETSEG IN PROGRESS BIT
	TDNE	T1,JBTSTS##(J)	;EXECUTE ONLY FILE?
	TDNN	T2,JBTST2##(J)	;GETSEG IN PROGRESS ?
	JRST	SETRL3		;NO
	MOVSI	T1,GTSSEG	;SEGMENT WAS GETSEGED BUT
	IORM	T1,JBTSGN##(J)	;YES, LIGHT GETSEGED BIT
SETRL3:	PUSHJ	P,EXONLY##	;IF EXECUTE ONLY PROTECT THE CORE IMAGE
>
	AOS	(P)		;SKIP RETURN (GETSEG,UCORHI,UREMAP)
IFN FTVM,<
	PUSHJ	P,SXOPFH##	;MAKE PFH CONCEALED IF GETSEG OF XO HI-SEG
	PUSHJ	P,STVSIZ##	;STORE CURRENT VIRTUAL SIZE OF BOTH SEGMENTS
	MOVE	J,.C0JOB##	;
	PUSHJ	P,HSVAD		;HI SEG LIMITS
	DMOVEM	T1,.UPMP+.UPHSE	;SAVE FOR ADDRESS CHECKING
	SKIPLE	T1,JBTSGN##(J)	;IS THERE A REAL HIGH SEGMENT?
	TLNE	T1,SHRSEG	;YES-IS IT NON-SHARABLE?
	JRST	SETRLH##	;NO, REDO THE MAP
	MOVE	R,JBTADR##(J)	;YES, CLEAR PAGE MEMORY
	PJRST	MAPHGH##
>
IFE FTVM,<
	PJRST	SETRLH##	;SET HARDWARE AND SOFTWARE RELOC INFO
>
				; FOR CURENT JOB AND RESTORE R AND J

;ROUTINE TO ADJUST USER'S CORE ARG. IN GET,R,RUN OR SAVE COMMANDS SO THAT
; THE ARG. SUPPLIED IS THE TOTAL OF BOTH THE LO & HI SEGS. IF THERE IS NO
; HI SEG THEN ARG. IS THE LO SEG SIZE AS IN NON-REENTRANT SYSTEMS.
; IF THERE ARE BOTH LO & HI SEGS AND THE ARG. IS LESS THAN THE HI SEG + 1K
; MANDATORY LO SEG SIZE THEN THE CORE ALLOCATION IS UNCHANGED.

;CALL	MOVE R,ADR. OF JOB DATA AREA
;	MOVE J,JOB NO.
;	PUSHJ P,ADJCOR
;	ALWAYS RETURN WITH T1=HI SEG #,T2 & U DESTROYED


ADJCOR::SKIPG	T1,JBTSGN##(J)	;IS THERE A HI SEG ?
	POPJ	P,		;EVIDENTLY NOT
	HLRZ	T2,JBTADR##(T1)	;GET ITS SIZE
	SKIPE	U,SGANEW##(R)	;WAS AN ARG. TYPED OF 0 OR NOT AT ALL ?
	SUBI	U,1(T2)		;NO - COMPUTE LO SEG SIZE
	SKIPG	U		;MUST BE 1K OR GREATER!
	POPJ	P,
	MOVEM	U,SGANEW##(R)	;STORE IT
	POPJ	P,		;RETURN
     
				; TO LOW SEG RELOC AND JOB NUMBER
;TRY TO READ IN NON-SHARABLE FILE WITH EXTENSION .HGH

TRYHGH:	MOVE	T1,DEVMOD(F)	;LOOKUP FAILURE, SEE IF OTHER THAN NOT FOUND
	TLNN	T1,DVDSK	;WAS LOOKUP ON DISK?
	JRST	TRYHG2		;NO, MUST BE A NOT FOUND
	HRRZ	T1,SGAEXT##(R)	;PICK UP POSSIBLE ERROR CODE
	CAILE	T1,1		;FILE NOT FOUND OR NO UFD?
	CAIN	T1,23		;NO--NON-EXISTANT SFD?
	JRST	TRYHG2		;YES--TRY .HGH
	JRST	NOFILE##	;REPORT ERROR

TRYHG1:	MOVE	J,JOB##		;RESTORE J WHICH MAY HAVE BEEN CLOBBERED BY FILSER
				; AND IS NEEDED BY COMCON FOR ERRORS
	MOVE	T1,DEVMOD(F)	;FAILURE FOR EXTENSTION .HGH
	TLNN	T1,DVDSK	;SAME AS BEFORE?
	POPJ	P,		;JUST NOT FOUND!
	HRRZ	T1,SGAEXT##(R)	;ERROR CODE
	CAILE	T1,1		;NO FILE OR NO UFD?
	CAIN	T1,23		;NO--NON-EXISTANT SFD?
	POPJ	P,		;FILE NOT FOUND!
	JRST	NOFILE##	;NO--GIVE ERROR
TRYHG2:	MOVSS	T1,SGAHGH##(R)	;TRY LOOKUP WITH EXTENSION .HGH
	PUSHJ	P,SGH1		;SET EXT AND E+3 OF LOOKUP BLOCK
				; FOR DTA OR DSK
	LOOKUP	0,SGANAM##	;LOOKUP WITH EXT .HGH
	  JRST	TRYHG1		;NOT FOUND, NO HIGH SEG RETURN FROM GETSEG
IFN FTLOCK!<FTKI10!FTKL10>,< 
	PUSHJ	P,INPSEG	;TRY TO READ IN THE HIGH SEGMENT
	  JRST	NOCORE		;ERROR, LOW SEGMENT IS LOCKED SO CAN'T
				; ASSIGN CORE FOR THE HIGH SEGMENT
	JRST	CPOPJ1##	;HIGH SEGMENT READ IN, GIVE OK RETURN
NOCORE:	HRRZ	T2,JOBCOR##(R)	;AMOUNT OF CORE
	PUSH	P,T2		;ERROR REPORTER EXPECTS ARG. ON STACK
	JRST	NROOM##		;REPORT THE ERROR
>	;END IFN FTLOCK CONDITIONAL
     
;ROUTINE TO READ IN A HIGH FILE INTO TOP OF LOW SEG AND REMAP
;LOOKUP MUST HAVE BEEN ALREADY BEEN DONE
;CALL:	MOVE R,XWD PROTECTION, RELOCATION FOR LOW SET
;	PUSHJ P,INPSEG
;	RETURN1 - NO HIGH FILE,
;	RETURN2 - HIGH FILE READ IN
;	C(J)=JOB NUMBER
;CALLED FROM TRYHGH AND GETH1


INPSEG:	MOVE	J,JOB##		;GET JOB NUMBER (IT MAY HAVE BEEN CLOBBERED BY FILSER)
IFN FTLOCK!<FTKI10!FTKL10>,< 
	MOVSI	T2,NSHF!NSWP	;CANNOT ASSIGN CORE IF THE JOB IS LOCKED
	TDNE	T2,JBTSTS##(J)	;IS IT LOCKED?
	JRST	[MOVEI	T1,SNSERR	;SEGMENT NOT ON SWAPPING SPACE ERROR
		POPJ	P,	;ERROR RETURN
]
>	;END IFN FTLOCK CONDITIONAL
IFN FTVM,<
	SKIPN	T1,.UPMP+.UPREL	;HIGHEST RELATIVE ADDRESS IN THE LOW SEGMENT
>
	MOVE	T1,USRREL##	;HIGHEST REL(USER) ADR IN LOW SEG FOR CURRENT USER
	TRO	T1,PG.BDY##	;MAKE EVEN 1K-1(SHOULD BE 140) SO CAN USE REMAP UUO
				; OR USER'S LOW SEG IF RUN OR GETSEG UUO
	HRRM	T1,SGALEN##(R)	;STORE ADR-1 OF IOWD FOR READING IN
				; HIGH FILE JUST ABOVE END OF CURRENT LOW SEG
	MOVE	T3,DEVMOD(F)	;DEVICE CHARACTERISTICS
	TLNE	T3,DVMTA	;MTA?
	JRST	INPMAG		;YES, GO DO SOME SPECIAL STUFF
	HLRE	T2,SGALEN##(R)	;-FILE LENGTH FROM DIRECTORY LOOKUP
IFN FTKA10,<
	JUMPGE	T2,GETERR##	;MUST BE NEGATIVE(128K MAX SIZE FOR HIGH FILE)
>
IFN FTKI10!FTKL10,<
	JUMPL	T2,INPSE2	;GO IF NEGATIVE WORD COUNT
	JUMPE	T2,GETERR##	;ERROR IF ZERO LENGTH
	LSH	T2,BLKLSH##	;CONVERT BLOCKS TO WORDS
	MOVNS	T2		;NEGATIVE WORD COUNT
	HRLM	T2,SGALEN##(R)	;STORE WORD COUNT IN IOWD
>
INPSE2:	SUB	T1,T2		;FIRST ADR-1 PLUS(-MINUS) LENGTH YIELDS:
				; LAST USER LOC TO BE READ INTO
	PUSHJ	P,GETCOR##	;ASSIGN ENOUGH CORE FOR LAST WORD OF HIGH FILE
				; RETURN ONLY IF OK
	INPUT	0,SGALEN##	;READIN HIGH FILE
	PUSHJ	P,SGIOCK##	;ANY ERRORS?
	MOVE	T3,DEVMOD(F)	;DEVICE CHARACTERISTICS
	TLNE	T3,DVMTA	;MTA?
	JRST	INPMG2		;YES, DO STUFF WHICH IS DONE ON LOOKUP FOR 
				;DIRECTORY DEVICES
;INSPE3 IS CALLED AS A SUBROUTINE
;FROM GETEXE IN SEGCON IF EXE FILES
;ARE SUPPORTED
INPSE3:	HRRZ	T1,SGALEN##(R)	;GET HIGHEST USER ADR IN ORIGINAL LOW SEG
IFN FTKI10!FTKL10,<
	EXCTUX	<LDB	T2,[POINT 9,10(T1),17]>
	LSH	T2,P2WLSH##
	HRL	T1,T2
>
	MOVE	J,.C0JOB##	;INPUT CLOBBERS J
	PUSHJ	P,UREMA0	;DO REMAP UUO
IFN FTKA10,<
	  STOPCD .,JOB,HCR,	;++HISEG CAN NOT BE REMAPED
>
IFN FTKI10!FTKL10,<
	  JRST	[MOVEI	T1,LOHERR	;LOW SEGMENT OVERLAPS HIGH SEGMENT
		POPJ	P,	;ERROR RETURN
]
>
	JRST	CPOPJ1##	;RETURN, CALLI UUO SETS JOB NO IN J

INPMAG:	MOVNI	T2,MTSIZ##	;LENGTH OF A MAGTAPE RECORD
	HRLM	T2,SGALEN##(R)	;SET TO READ 1ST RECORD OF FILE
	JRST	INPSE2		;GO READ IT

INPMG2:	TRNE	S,IODEND	;EOF?
IFE FTLOCK!<FTKI10!FTKL10>,<
	POPJ	P,
>
IFN FTLOCK!<FTKI10!FTKL10>,<
	JRST	TPOPJ##		;YES, THERE IS NO HIGH SEGMENT
>
	MOVE	T1,SGALEN##(R)	;NO, THERE IS A HIGH SEGMENT
	PUSH	P,T1		;SAVE ORIGINAL IOWD
	MOVEI	T1,JOBHRN##+1(T1)  ;LENGTH OF HIGH SEG
IFN FTKA10,<
	TLO	T1,R
>
	EXCTUX	<HLRZ T1,@T1>
	JUMPE	T1,INPMG4	;JUMP IF NULL HIGH SEGMENT
	CAIG	T1,MTSIZ##
	JRST	INPMG3		;WHOLE FILE WAS 1 RECORD
	MOVNM	T1,T2		;-WORD COUNT
	HRL	T2,(P)		;ADDRESS OF START OF 1ST RECORD
	ADD	T2,[XWD MTSIZ##,MTSIZ]	;INCREMENT OVER 1ST RECORD
	MOVSM	T2,SGALEN##(R)	;NEW IOWD
	HRRZ	T2,0(P)		;GET ADDRESS OF FIRST RECORD OF HIGH SEG
	ADD	T1,T2		;HIGHEST ADDRESS NEEDED
	PUSHJ	P,FNDPDS##	;TAPSER WIPES W
	PUSHJ	P,GETCOR##	;GET THE CORE
	INPUT	SGALEN##	;READ THE REST OF THE FILE
	PUSHJ	P,SGIOCK##	;ANY ERRORS?
INPMG3:	STATO	IOTEND+IODEND	;MAGTAPE AT END OF TAPE OR END OF FILE?
	MTAPE	16		;NO, SKIP OVER THE EOF
	SETSTS	DR		;RESET THE EOF BIT
	POP	P,SGALEN##(R)	;RESTORE THE ORIGINAL IOWD
	JRST	INPSE3		;AND CONTINUE

INPMG4:	MTAPE	16		;SKIP TO EOF
	POP	P,T1
IFE FTLOCK!<FTKI10!FTKL10>,<
	POPJ	P,
>
IFN FTLOCK!<FTKI10!FTKL10>,<
	PJRST	TPOPJ##
>
     
;ROUTINE TO CHECK IF HIGH SEG ABOUT TO BE SHARED WAS IDLE OR DORMANT
;IF YES, THE COUNT OF FREE+DORMANT+IDLE CORE IS DECREASED
;CALL:	MOVE J,HIGH SEG NUMBER
;	PUSHJ P,CHKIDL
;CALLED FROM GETSEG AND FININ
CHKIDL:	PUSHJ	P,CHKLKH	;CHECK FOR DORMANT OR IDLE (UNLOCKED)
	  POPJ	P,		;NO, CORTAL ALREADY ADJUSTED
IFE FTVM,<
	LDB	T1,PCORSZ##	;SIZE IN CORE(J-1)
	MOVNI	T1,1(T1)	;- NO. OF K IN CORE
>
IFN FTVM,<
	LDB	T1,IMGIN##	;SIZE IN PAGES
	MOVNS	T1		;-NO. OF PAGES IN CORE
>
	ADDM	T1,CORTAL##	;YES, THEN DECREASE COUNT OF FREE+DORMANT+IDLE
				; CORE SINCE THIS HIGH SEG WILL NOT BE IDLE 
	POPJ	P,		; ANY MORE

;SUBROUTINE TO CHECK FOR CORE ASSIGNED TO A DORMANT OR UNLOCKED
; IDLE SEGMENT SO THAT CORTAL MAY BE ADJUSTED PROPERLY

CHKLKH:	MOVE	T1,JBTSTS##(J)	;GET HIGH SEGMENT STATUS
	TRNN	T1,ICCMSK	;DORMANT OR IDLE?
	SKIPN	JBTADR##(J)	;AND IN CORE?
	POPJ	P,		;NO
	TLNN	T1,NSWP		;NO, IDLE - LOCKED?
	AOS	(P)		;YES, SKIP RETURN
	POPJ	P,		;NO

;ROUTINE TO SETUP LOWER CORE FOR LOOKUP OR ENTER FOR HIGH FILE
;CALL:	LH OF SGAHGH(R) SET TO FILE EXT FOR HIGH SEG
;	PUSHJ P,SGH
;	SETUP SGALEN TO -1 IF DTA OR MTA, PROJ R IF DSK


SGH:	MOVE	T1,SGAHGH##(R)	;SETUP EXTENSION(FOR HIGH FILE SHR OR HGH)
SGH1:	HLLZM	T1,SGAEXT##(R)	;STORE EXTENSION FOR LOOKUP ORENTER
SGH2:	MOVEI	T1,-1		;SET SGALEN TO -1 IF DEVICE NOT DISK
				; (FIRST REL LOC-1 DUMPED FROR HIGH SEG)
	JRST	SG4##		;OTHERWISE SET TO PROJ,R NO.

;ROUTINE TO DELETE A FILE, SO SUBSEQUENT GET WILL NOT BE CONFUSED
;USED BY SAVE AND SSAVE (NOT BY GET)
;CALL:	HRLI T1, EXTENSION (.LOW,.SHR,.HGH, OR USER TYPED ARG)
;	PUSHJ P,DELET
;	ALWAYS RETURN

DELET:	PUSHJ	P,SGH1		;SETUP EXTENSION, AND E+3 IN DIRECTORY BLOCK
	PUSH	P,DEVPPN(F)
	LOOKUP	0,SGANAM##	;LOOKUP FILE
	JRST	TPOPJ##		;NOT FOUND
	CLOSE	0,		;CLOSE SO RENAME WILL WORK
	POP	P,T1
	CAMN	T1,DEVPPN(F)	;DON'T DELETE IF NOT SAME PPN
	RENAME	0,SGAREN##	;DELETE FILE
	JFCL			;IGNORE IF CAN'T, WE TRIED
	POPJ	P,		;RETURN
     
;ROUTINE CALLED AFTER A HIGH OR LOW SEG SWAP IN READ ERROR
;CLEAR HIGH SEG NAME, SO NO NEW USERS WILL SHARE
;CALL:	MOVE J,HGIH OR LOW SEG NO.
;	PUSHJ P,SEGERR
;	ALWAYS RETURN WITH J=JOB NUMBER

IFN	FTSWAP & FTSWPERR,<

SEGERR::CAIG	J,JOBMAX##	;LOW SEG?
	POPJ	P,		;YES
	MOVE	T1,JBTSTS##(J)	;NO, HAS THE ERROR BEEN DETECTED FOR THIS HIGH SEG
	TLNN	T1,SERR		; ON A PREVIOUS SWAPPING?
	PUSHJ	P,SWPREC##	;NO, RECORD ERROR AND DECREASE OF VIRTUAL CORE
	MOVSI	T1,SERR		;SET HIGH SEG ERROR BIT
	IORM	T1,JBTSTS##(J)	;SO SPACE WILL NEVER BE RETURNED BY SERSWP
	SETZM	JBTNAM##(J)	;CLEAR HI SEG NAME SO NO NEW USER WILL SHARE
	MOVE	J,SWPIN##	;RETURN JOB NUMBER ASSOCIATED WITH HIGH SEG
	POPJ	P,
>	;END CONDITIONAL ON FTSWAP & FTSWPERR

;SUBROUTINE TO CALCULATE THE STARTING VIRTUAL
; ADDRESS IN A HIGH SEGMENT AND THE HIGHEST
; LEGAL ADDRESS IN THE SEGMENT
;CALLING SEQUENCE:
;	MOVE	J,JOB NUMBER
;	PUSHJ	P,HSVAD
;RETURNS HERE, T1=HIGHEST LEGAL ADDRESS, T2=
; STARTING VIRTUAL ADDRESS

HSVAD::	PUSHJ	P,SAVE2##	;SAVE P1-P2
	PUSH	P,J		;SAVE JOB NUMBER
IFN	FTVM,<
	HRRZ	P1,.UPMP+.UPREL
	SKIPN	P1		;HIGHEST ADR. IN LOW SEG
>
	HLRZ	P1,JBTADR##(J)	;LOW SEG PROTECTION
	MOVE	J,JBTSGN##(J)	;HIGH SEGMENT #
	MOVEI	P2,377777	;ASSUME AT 400000
	TRNE	P1,400000	;IS IT?
	MOVE	P2,P1		;NO, ABOVE LOW SEG
	AOS	T2,P2		;T2 = STARTING V.A.
IFN FTKI10!FTKL10,<
	JUMPLE	J,HSVAD1	;SPY SEG. (ALWAYS STARTS AT 400000)
	LDB	T2,JBYHSO##	;PAGE NUMBER WHERE HIGH SEGMENT ORIGINS
	LSH	T2,P2WLSH##	;CONVERT TO STARTING ADDRESS
HSVAD1:>
	SKIPL	T1,J		;SPY SEG?
	HLRZ	T1,JBTADR##(J)	;NO, SIZE OF HI. SEG.
	HRRZS	T1		;CLEAR POSSIBLE SPYSEG BIT
	SKIPE	T1		;SKIP IF NO HI. SEG.
	ADDI	T1,(T2)		;T1 = HIGHEST ADR.
	JRST	JPOPJ##		;RETURN
;ROUTINE TO GET A NEW-STYLE SAVE FILE:
;
;
;AC USAGE:
;
;	AC		CONTENTS
;
;	P1		LH = FLAGS
;			RH= POINTER TO CURRENT DIRECTORY
;
;	P2		LH = CURRENT FILE PAGE # FOR INPUT FILE
;			RH = DIRECTORY LENGTH COUNTER
;
;	P3		1ST WORD OF CURRENT DIRECTORY ENTRY
;
;	P4		2ND WORD OF CURRENT DIRECTORY ENTRY
;
;
;IT IS POSSIBLE (THOUGH UNLIKELY) THAT THERE ARE MORE THAN ONE 
; DIRECTORY PAGE WHICH HAS TO BE READ. IN THAT CASE, ALL OF THEM
; (THERE MUST BE NO MORE THAN THREE) ARE READ IN INITIALLY BEFORE
; ANY PROCESSING OF THE SAVE FILE BEGINS. THE POINTER TO
; THE 2ND DIRECTORY PAGE IS KEPT IN SGANAM, THE POINTER TO THE 3RD
; PAGE IS IN SGAEXT. WHEN THE PROCESSING OF THE 1ST DIRECTORY PAGE
; IS COMPLETED, THE PAGE IN P1 IS DESTROYED, THE ONE IN
; SGANAM IS MOVED TO P1, AND SGAEXT IS MOVED TO SGANAM.
;
;
;IT ALSO MAY OCCUR THAT A PAGE WHICH IS ABOUT TO BE READ
; IN FROM THE SAVE FILE IS GOING TO OVERLAP ONE OF THE
; DIRECTORY PAGES. IN THAT CASE, THE DIRECTORY PAGE IS MOVED
; TO A FREE PAGE, AND THE NEW PAGE IS READ IN WHERE THE DIRECTORY
; PAGE USED TO BE.
;
;
;THE BASIC ALGORITHM IS AS FOLLOWS:
;
; 1 )	READ IN DIRECTORY PAGE(S) AND STORE POINTER(S) TO IT
; 2 )	COMPUTE # OF PAGES IN CORE IMAGE, AND RANGE OF HIGH SEG
; 3 )	(NON-VM) ALLOCATE CORE FOR ENTIRE FILE AND MOVE DIRECTORY TO TOP OF CORE
; 4 )	GET A DIRECTORY ENTRY
; 5 )	(VM) MOVE DIRECTORY PAGE IF NEW ENTRY WILL OVERLAP IT.
; 6 )	(VM) ALLOCATE CORE FOR PAGES.
; 7 )	INPUT PAGES FROM FILE
; 8 )	(VM) IF FILE GREATER THEN PHYSICAL LIMIT, PAGE OUT PAGES
; 9 )	IF MORE ENTRIES IN DIRECTORY, GO TO STEP 3.
;10 )	(NON-VM) GET RID OF EXTRA CORE, IF NECESSARY, AND CLEAR DIRECTORY (KA ONLY).
;11 )	PERFORM REMAP IF HIGH SEG WAS READ IN
REPEAT 0,<


	***** EXPLANATION OF "GET" FOR EXE FILES *****



The algorithm to get an EXE file is straightforward  in  the
simple  case, and quite complicated in some rare cases.  The
primary problem is obviously the placement and  manipulation
of the directory.

For VM systems, the directory can be longer than  one  page;
for  non-VM  systems,  we have restricted this length to one
page (however, this restriction can be changed in the future
if customer reaction demands it).

For non-VM systems, the directory  is  initially  read  into
page 1 on a GET.  On a GETSEG, it is read into the next page
above the existing low seg (on a KA,  this  means  that  two
extra pages must be allocated).

For VM-systems, the entire directory is read  into  as  many
pages  as  is necessary (pointers to the directory pages are
kept in P1, and the first two words  of  the  user's  shadow
AC's).   As  the  file pages are read into core later, these
directory pages are shuffled around if necessary,  in  order
to  avoid  being  destroyed  by the incoming pages.  As each
directory page is scanned, it is removed from the  page  map
to make room for the file pages.  The only exception to this
rule is when the file fits precisely into available core, in
which  case  the  last  file  page  is read in over the last
directory page.

For non-VM systems, the core allocation algorithm  is  quite
different from that on VM-systems.  The technique chosen was
to allocate all required core before any part of the file is
read  in,  and  then to BLT the directory to the top of that
core.  There were two other algorithms which were considered
but rejected for the following reasons:


          1 )  Keep directory in user AC's

               -eliminates possibility  of  zero-compression
          because directory must be kept very short

               -eliminates   full   file    transportability
          between systems


          2 )  Allocate core on a per-directory-entry basis,
          get  one  extra  page  of core each time, and move
          directory to free core above each core block

               -requires   excessive    memory    management
          overhead

               -requires (possibly) many useless BLT's

               -eliminates possibility of files as large  as
          CORMAX


The  core  allocation  algorithm  is  complicated   by   the
difference between units of allocation of the KA/KI, and the
possibility that the operation  being  performed  may  be  a
GETSEG.   Also, for very small files (e.g.  those which only
contain page zero), extra pages must be allocated  for  page
zero (because input cannot be done directory into page zero)
and for the directory.  The core allocation  algorithm  then
becomes:


          1 )  On a GETSEG, allocate # of pages in high  seg
          minus  one (minus two on KA).  However, on KA's if
          the high seg is less than  three  pages  long,  no
          extra core must be allocated.

          2 )  On a GET, allocate total # of pages  in  file
          minus two (for directory and page zero).  However,
          allocate at least one page (for a grand  total  of
          three  pages) to allow for moving of page zero and
          the  directory.   These  extra  pages  are   later
          flushed.


The basic processing of the file pages is  almost  identical
for non-VM and VM systems with the following exceptions:

          1 )   page 0 is read into page 1  and  BLTed  down
          for  non-VM,  and  into a random free page for VM.
          Only those locations above JOBDDT are  moved  down
          into physical page zero.

          2 )  VM must be able to read a page from the  file
          immediately  after creating it in order to page it
          out if a user's physical limit is to  be  exceeded
          by  the  file.   In  most  cases, though, all core
          pages for a given directory descriptor are created
          before  any  of  the  corresponding file pages are
          read in.

          3 )  VM systems read the high  seg  directly  into
          the  process  page  number  specified  in the file
          directory.   Non-VM  systems  maintain  a   moving
          pointer  (in the left half of SGAHGH) which points
          to the next low seg page into which the  high  seg
          is  to be read.  This pointer is contually updated
          as each block of the high seg is read in (the high


          seg  may  have  more than one directory descriptor
          because of allocated-but-zero pages).   Therefore,
          the process page number is essentially ignored.

          4 )  VM systems must delete  the  directory  after
          processing  unless  the last page was read in over
          it.   On  non-VM  KI  systems,  the  directory  is
          guaranteed to be overwritten by the last page read
          in.  On non-VM KA systems, this also occurs if the
          number  of  pages in the file was even.  If it was
          odd, the directory  remains  intact  and  must  be
          cleared to zeroes.



          Please notE that EXE files  are  not  supported  on
          DECtape  at  all.   This  is  due  to the inherent
          difficulties in doing so and  the  fact  that  EXE
          files   are   not  transportable  between  certain
          devices anyway.  Also,  EXE  files  which  reflect
          "virtual"   core   images  are  not  supported  on
          Magtape.


>;END OF REPEAT 0
IFN FTEXE,<

;ROUTINE TO READ IN PART OR ALL OF AN "EXE" SAVE FILE
;
;CALL:
;	MOVSI	P1,FLAGS
;	PUSHJ	P,GETEXE
;	HERE
;
;
;FLAGS: (THESE ARE DEFINED IN COMCON)
;GTLOW		GET THE LOW SEGMENT
;GTHGH		;GET THE HIGH SEGMENT
;GTBTH		;GET BOTH SEGMENTS


;FIRST, WE WILL READ IN ALL THE DIRECTORY PAGES FOR THE SAVE FILE.
;WHEN THIS PAGE OF CODE IS THRU, THE DIRECTORIES WILL BE IN CORE
; AND THE POINTERS TO THEM (OR "IT") WILL BE IN P1,P2,P3.
GETEXE::
	SETZM	SGAEND##(R)	;CLEAR IOWD
IFN FTVM,<
	SETZB	P2,P3		;CLEAR AC'S WHERE DIRECTORY POINTERS WILL GO
	MOVEI	P4,P1		;P4 IS MOVING DIRECTORY POINTER FOR NOW
MORDIR:	PUSH	P,T3		;SAVE COUNTER OF # OF DIRECTORY PAGES
	PUSHJ	P,GTFREP##	;GET A FREE PAGE FOR DIRECTORY
	 JRST	NEED1P		;CAN'T--REPORT ERROR
	PUSHJ	P,CRPAGE##	;CREATE THIS PAGE
	 JRST	NEED1P		;COULDN'T --ABORT USER
	LSH	T1,P2WLSH##	;MAKE INTO AN ADDRESS
	HRRM	T1,(P4)		;SAVE POINTER TO THIS DIRECTORY PAGE
>;END IFN FTVM
IFE FTVM,<
	HRRZ	T1,USRREL##	;GET TOP OF LOW SEG
IFN FTKA10,<
	TLNE	P1,GTLOW	;GETSEG?
	JRST	DIRADR		;NO, USE PAGE 1 FOR DIRECTORY
>
	ADDI	T1,EXESIZ	;RESERVE ROOM FOR DIRECTORY
	PUSHJ	P,GETCOR	;GET AN EXTRA PAGE
DIRADR:	SUBI	T1,EXESIZ-1	;BUMP T1 BACK TO START OF DIRECTORY PAGE
	HRR	P1,T1		;SAVE ADDRESS
>;END IFE FTVM
	PUSHJ	P,STIOWD##	;SET UP AN IOWD FOR THIS PAGE
	PUSHJ	P,RDSTUF	;READ 1ST PGE FROM FILE INTO THIS PAGE
IFN FTVM,<
	POP	P,T3		;RESTORE DIRECTORY PAGE COUNTER
	CAIE	P4,P1		;IS THIS THE 1ST DIRECTORY PAGE?
	JRST	NOT1ST		;NO, DON'T CHECK HEADER AND LENGTH
>
	HRR	M,P1		;GET ADDRESS OF DIRECTORY
	PUSHJ	P,GETWDU##	;GET WORD FROM DIRECTORY
	HLRZ	T2,T1		;GET CODE
	CAIE	T2,SV.DIR	;IS THIS THE DIRECTORY?
	JRST	NOTEXE		;NO, REPORT ERROR
	HRRZ	T3,T1		;GET # OF WORDS IN DIRECTORY
	LSH	T3,WXPLSH	;=# OF DIRECTORY PAGES -1
	TRNN	T1,1		;LENGTH MUST BE ODD
	JRST	BADDIR		;EVEN LENGTH
IFE FTVM,<CAILE	T3,0>		;NON-VM DIRECTORIES CAN ONLY BE 1 PAGE
IFN FTVM,<CAILE	T3,2>		;VM DIRECTORY MUST BE .LTE. 3 PAGES
	JRST	EXEBIG		;IT ISN'T, FLUSH USER
IFN FTVM,<
	MOVN	T1,T3		;SAVE 3-(NO OF DIRECTORY PAGES)
	ADDI	T1,3		; AS THE NUMBER OF TIMES TO ROTATE ENTRIES
	PUSH	P,T1
>
NOT1ST:
IFN FTVM,<
	AOS	P4		;BUMP POINTER TO DIR STORAGE
	SOJGE	T3,MORDIR	;JUMP IF DIRECTORY IS MORE
				; THAN 1 PAGE LONG (FAIRLY UNLIKELY)
	MOVEM	P2,SGANAM##(R)	;STORE 2ND AND 3RD DIR POINTERS
	MOVEM	P3,SGAEXT##(R)	; IN SGANAM AND SGAEXT IN USER SHADOW AREA.
>
	PUSHJ	P,GTDIRW	;GET 1ST WORD FROM DIRECTORY
	MOVEI	P2,-1(T1)	;SAVE LENGTH FOR LATER
	JUMPE	P2,BADDIR	;DON'T ALLOW NULL DIRECTORIES

;FALL THRU TO NEXT PAGE...

;STILL IN IFN FTEXE CONDITIONAL...
;WE MUST NOW COMPUTE HOW MUCH CORE WE ARE GOING TO NEED FOR THESE
; SEGMENTS, AND THE UPPER AND LOWER LIMITS OF THE HIGH SEG.
;WE WILL DO THIS BY CYCLING THRU THE DIRECTORY AND ADDING UP
; THE NUMBER OF PAGES THAT WE WILL NEED TO ALLOCATE FOR
; THIS FILE.
;
;P1 HAS A POINTER TO THE 1ST DIRECTORY PAGE IN ITS RH
;SGANAM HAS POINTER TO 2ND PAGE (IF ANY)
;SGAEXT HAS POINTER TO 3RD PAGE (IF ANY)
;
;
;THIS ROUTINE ASSUMES THAT ALL ENTRIES FOR THE HIGH SEG
; WILL APPEAR SEQUENTIALLY IN THE DIRECTORY IN INCREASING PAGE
; ADDRESSES. THAT IS, IF WE FIND A HIGH SEG ENTRY, THEN IF WE FIND
; ANOTHER HIGH SEG ENTRY LATER ON, THE SECOND SET OF HIGH SEG PAGES
; MUST BE CONTIGUOUS TO THE FIRST SET OF HIGH SEG PAGES.
;
;WE WILL USE T3 TO HOLD THE TOTAL # OF PAGES NEEDED
; AND T4 = START OF HISEG,,END OF HISEG
	SETZB	T3,T4		;CLEAR OUT THESE AC'S
IFN FTVM,<TLO	P1,SV%DWO##>	;SET THE "DON'T WRITE OUT" BIT.
				;THIS WILL TELL "GTDIRE" THAT THE DIRECTORY
				; PAGES SHOULD NOT BE DELETED WHEN WE ARE
				; THRU SCANNING ONE OF THEM, BUT MERELY
				; ROTATED.
CKSIZ0:	PUSHJ	P,GTDIRE	;GET THE NEXT DIR ENTRY IN P3,P4
	 JRST	CKSIZ3		;NO MORE LEFT, WE ARE THRU
	PUSHJ	P,WANTIT	;BUT DO WE WANT THIS SEGMENT?
	  JRST	CKSIZ0		;NO, IGNORE IT
	LDB	T2,[POINT 9,P4,8]	;GET REPEAT COUNT
	ADDI	T3,1(T2)	;BUMP TOTAL PAGE COUNT
IFE FTVM,<
	ADD	P2,[2,,0]	;COUNT NO OF DIRECTORY ENTRIES WE WANT
>
	JUMPGE	P3,CKSIZ0	;DON'T CHECK LIMITS IF NOT HIGH SEG
	ADDI	T2,(P4)		;COMPUTE LAST PAGE # IN THIS ENTRY
	JUMPE	T4,[HRL	T4,P4	;IF THIS IS THE 1ST HISEG ENTRY,
		    JRST CKSIZ1]	;SAVE START ADDR AND GO ON
	MOVEI	T1,1(T4)	;# OF NEXT CONTIGUOUS PAGE IN HIGH SEG
	CAIE	T1,(P4)		;IS THIS ENTRY CONTIGOUS WITH PREVIOUS
				; HIGH SEG ENTRY? 
	JRST	BADDIR		;NO, BAD FORMAT FOR DIRECTORY
CKSIZ1:	HRR	T4,T2		;RESET HIGH SEG END PAGE #
	JRST	CKSIZ0		;IT'S OK
;COME HERE WHEN WE HAVE FINISHED SCANNING
; THE DIRECTORIES. WE MUST NOW ROTATE THE DIRECTORY
; POINTERS UNTIL THEY ARE BACK IN THEIR ORIGINAL POSITION.
CKSIZ3:	TRZ	P1,EXESIZ-1	;POINT CURRENT DIR POINTER TO START OF PAGE
IFN FTVM,<
	TLZ	P1,GT%DW1##	;CLEAR BIT SO THAT WHEN WE START
				; SCANNING THE DIRECTORY PAGES AGAIN,
				; THEY WILL BE FLUSHED AFTER THEY ARE
				; THRU.
	SOSGE	(P)		;LAST DIRECTORY PAGE?
	JRST	CKSIZ4		;YES, THIS MUST BE HOW WE START OUT
	EXCH	P1,SGAEXT##(R)	;NO, ROTATE ALL POINTERS ONE PLACE
	EXCH	P1,SGANAM##(R)	;...
	JRST	CKSIZ3		;AND TRY AGAIN
>
;FOR NON-VM SYSTEMS, WE MUST NOW ALLOCATE ENOUGH CORE FOR
; THE ENTIRE EXE FILE, AND MOVE THE DIRECTORY TO THE TOP OF THAT CORE.
; THE DIRECTORY WILL THEN BE OVER-WRITTEN BY THE LAST PAGE
; WHICH IS READ IN FROM THE FILE. HOWEVER, IF A LOW SEGMENT
; MUST BE GOTTEN, AND THE TOTAL # OF PAGES IN THE FILE IS LESS 
; THAN 3, WE MUST ALLOCATE SOME EXTRA CORE BECAUSE PAGE ZERO MUST
; BE READ INTO PAGE 1 AND BLT'ED DOWN WITHOUT CLOBBERING THE
; DIRECTORY.
;
;FOR VM-SYSTEMS, WE MUST NOW DETERMINE IF THIS FILE WILL EXCEED
; THE USER'S PHYSICAL LIMIT AND SET SOME FLAGS APPROPRIATELY.
; ALSO, WE MAY HAVE TO PAGE OUT HIS LOW SEG ON A "VIRTUAL" GETSEG.
;
;WE WILL FIRST COMPUTE SOME VALUES WHICH WE WILL SAVE UNTIL THE
; REMAP WHEN THEY WILL BE NEEDED. THESE ARE AS FOLLOWS:
;	SGAHGH = 1ST PAGE OF HIGH SEG,,LENGTH (IN PAGES) OF HIGH SEG
;	SGALOW = # OF PAGES REQUIRED FOR ENTIRE FILE
;
;FOR VM SYSTEMS, THE STARTING ADDRESS OF THE HIGH SEG WILL
; BE THE ACTUAL USER VIRTUAL PAGE NUMBER FOR THE START
; OF THE HIGH SEG. FOR NON-VM SYSTEMS, THIS PAGE NUMBER WILL BE
; THE LOW SEG PAGE INTO WHICH THE HIGH SEG WILL BE READ,
; AND THEN REMAPPED TO ITS ACTUAL ADDRESS.
;
;
CKSIZ4:
IFN FTVM,<
	POP	P,(P)		;REMOVE DIRECTORY-COUNT FROM LIST
>
	HLRZ	T1,T4		;GET 1ST PAGE OF HIGH SEG
	SKIPE	T1		;IF NO HIGH SEG, DON'T SUBTRACT
	SUBI	T4,-1(T1)	;LAST-FIRST-1 YIELDS # OF PAGES IN HIGH SEG
	MOVEM	T4,SGAHGH##(R)	;SAVE START,,LENGTH OF HIGH SEG
	MOVEM	T3,SGALOW##(R)	; AND TOTAL PAGES IN CORE IMAGE
IFE FTVM,<
	SUBI	T3,(T4)		;TOTAL-HIGH PAGES=LOW SEG PAGES
IFN FTKA10,<
	TRNE	T3,1		;IF ODD # OF LOW PAGES
	AOS	T3		;MAKE IT EVEN
	TRNE	T4,1		;SAME FOR HIGH SEG
	AOS	T4
>
	HRREI	T1,-1(T3)	;# LOW PAGE -1 (FOR DIRECTORY)
	ADDI	T1,(T4)		;= TOTAL PAGES NEEDED -1
	JUMPE	T4,CKSIZ5	;IF NO HIGH SEG, JUMP AROUND THIS
	MOVE	T2,.C0REL##
	LSH	T2,WXPLSH	;=LAST PAGE # OF USER CORE (EXCEPT
				; IN THE CASE OF A GETSEG, THIS WILL
				; USUALLY BE PAGE 1)
IFN FTKI10,<
	SKIPE	T3
>
	ADDI	T2,-1(T3)	;ADD NUMBER OF LOW SEG PAGES TO BE READ
				;MINUS 1 (BECAUSE PAGE 0)
				; ALWAYS EXISTS FOR EACH USER)
	HRLM	T2,SGAHGH##(R)	;STORE THIS PAGE # AS THE START
				; OF THE HIGH SEG, BECAUSE WE MUST
				; READ IN THE HIGH SEG INTO THE TOP
				; OF THE LOW SEG, AND THEN REMAP IT
CKSIZ5:	JUMPE	T3,ACQCOR	;IF THERE ARE NO LOW SEG PAGES, WE
				; DON'T NEED ANY EXTRA CORE
	SOSG	T1		;ACCOUNT FOR PAGE 0
	MOVEI	T1,1		;WE MUST GET AT LEAST 1 PAGE
ACQCOR:
IFN FTKA10,<
	SKIPN	T3		;IF KA AND GETSEG
	SOJLE	T1,GOTCOR	;ACCOUNT FOR EXTRA PAGE
>
	LSH	T1,PXWLSH	;CONVERT CORE TO AN ADDRESS
	ADD	T1,.C0REL##
	TLZE	T1,-1
	MOVEI	T1,-1
	PUSHJ	P,GETCOR	;GET CORE FOR ENTIRE FILE
	SKIPN	SGAHGH##(R)	;IF GETSEG OF A NULL LOW SEG,
	SKIPE	SGALOW##(R)
	CAIA
	POPJ	P,		;WE'RE DONE - RETURN
GOTCOR:
>;END IFE FTVM
	PUSHJ	P,GTDIRW	;GET HEADER WORD FROM DIRECTORY
IFE FTVM,<
	HLRZS	P2		;NO OF DIRECTORY ENTRIES WE WANT
	TLNE	P1,GTHGH##	;JUST LOW SEG? (IF SO, LAST DATA
				; PAGE WILL OVERWRITE DIRECTORY)
>
	MOVEI	P2,-1(T1)	;PUT LENGTH OF DIRECTORY ENTRIES IN P2
	TLZ	T1,-1		;CLEAR HEADER CODE
	LSH	T1,WXPLSH	;#PAGES-1
	HRLI	P2,1(T1)	;=1ST PAGE AFTER DIRECTORY
IFE FTVM,<
	MOVEI	T2,-1(P1)	;SOURCE POINTER FOR BLT
IFN FTKI10,<
	HRRZ	T3,.C0REL##
	TRZ	T3,EXESIZ-1
	CAMN	T2,T3
	JRST	CKSIZ6
>
IFN FTKA10,<
	ADDI	T2,(R)		;RELOCATE
>;END IFN FTKA10
	MOVEI	T3,1(T2)
	MOVSS	T2		;SOURCE,,0
	HRR	T2,USRREL##	;SOURCE,,JOBREL
	TRZ	T2,EXESIZ-1	;WE WILL MOVE THE DIRECTORY PAGE UP
				; TO THE HIGHEST PAGE IN CORE
	HRRI	P1,1(T2)	;RESET 1ST POINTER TO NEW DIRECTORY PAGE
	HRRZ	T1,T2		;GET START OF LAST PAGE
	ADDI	T1,1(P2)	;ADD DIRECTORY LENGTH
IFN FTKA10,<
	ADDI	T1,(R)		;RELOCATE
	ADDI	T2,(R)
>;END IFN FTKA10
	PXCTUU	<BLT	T2,(T1)>,<PX.SRC!PX.MEM>  ;MOVE DIRECTORY TO TOP OF USER CORE
	EXCTUU	<SETZM	-1(T3)>	;CLEAR WHERE DIRECTORY WAS
	HRLI	T3,-1(T3)	;GET POINTER
	HRRZ	T2,T3
	ADD	T2,P2
	PXCTUU	<BLT	T3,(T2)>,<PX.SRC!PX.MEM>  ;COMPUTE LAST ADR
>;END OF IFE FTVM
CKSIZ6:	MOVE	J,JOB##		;SET UP JOB NUMBER
IFN FTVM,<
	PUSHJ	P,FNDPDS##	;GET THIS GUY'S PDB
	MOVE	T3,SGALOW##(R)	;CHECK # PAGES
	PUSHJ	P,GSIZI##	;IS THERE ROOM?
	  JFCL			;MAYBE
	  TLO	P1,GT%PLE##	;SET BIT TO INDICATE THAT THE
				; PHYSICAL LIMIT IS EXCEEDED, SO WE
				; WILL PAGE OUT ALL LOW SEG PAGES
				; AS WE READ THEM IN LATER.
	HLRZ	T1,.PDCVL##(W)	;GET CURRENT VIRTUAL LIMIT
	CAMGE	T1,SGALOW##(R)	;IS THAT LESS THAN SIZE OF FILE?
	JUMPN	T1,NROOM	;NO, CAN'T POSSIBLY FIT FILE IN

	SKIPN	.UPMP+.UPVRT	;IS THIS A VIRTUAL GUY?
	JRST	GTLOOP		;NO
	MOVE	T1,.UPMP+.UPUAC+4	;YES, CHECK WHAT THIS IS
	CAMN	T1,['GETSEG']	;IS THIS A GETSEG?
	PUSHJ	P,PAGLOW##	;YES, PAGE OUT THE LOW SEGMENT

>;END IFN FTVM
;THIS IS THE MAIN LOOP FOR DIRECTORY PROCESSING
;THIS LOOP FETCHES ONE DIRECTORY ENTRY AND PROCESSES ALL PAGES
; CONTAINED IN THAT ENTRY.
GTLOOP:	PUSHJ	P,GTDIRE	;GET NEXT DIRECTORY ENTRY
	 JRST	GETFIN		;NO MORE, WE ARE THRU
	PUSHJ	P,WANTIT	;DO WE WANT THIS ENTRY?
	  JRST	GTLOOP		;NO, IGNORE THIS DIRECTORY ENTRY
IFN FTVM,<
	HRRZ	T1,P1		;GET CURRENT DIRECTORY POINTER
	PUSHJ	P,CKRADR	;WILL IT BE OVERWRITTEN BY THESE PAGES?
	  PUSHJ	P,MOVDIR	;YES, WE MUST MOVE IT
	HRRZ	T1,SGANAM##(R)	;GET 2ND DIR PAGE
	JUMPE	T1,HAVDR5	;TO SAVE TIME, DON'T CHECK IF ONLY 1 DIR PAGE
	PUSHJ	P,CKRADR	;WILL IT BE OVER-WRITTEN?
	  PUSHJ	P,MOVDIR	;YES, WE MUST MOVE IT
	HRRZ	T1,SGAEXT##(R)	;GET 3RD PAGE POINTER
	JUMPE	T1,HAVDR5	;LEAVE IF NO DIREC.
	PUSHJ	P,CKRADR	;IS IT IN THE WAY?
	  PUSHJ	P,MOVDIR	;YES, MOVE IT
;	JRST	HAVDR5		;NO, KEEP GOING
>;END IFN FTVM

;FALL THRU TO NEXT PAGE...

;STILL IN IFN FTEXE  CONDITIONAL...
;COME HERE IF NO DIRECTORY PAGES ARE ABOUT TO BE OVER-WRITTEN
;WE CAN NOW READ IN THE SAVE FILE.
;IF THIS ENTRY CONTAINS PAGE ZERO, WE MUST DO SOME SPECIAL
; PROCESSING WHICH IS ON THIS PAGE. IF PAGE ZERO IS NOT
; WITHIN THIS ENTRY, WE SKIP TO THE MAIN LOOP AT "NOTP0".
;
;
HAVDR5:
	TRNE	P4,-1		;IS THIS PAGE 0?
	JRST	NOTP0		;NO
IFN FTVM,<
	PUSHJ	P,GTFREP##	;YES, WE CAN'T READ DIRECTLY
				; INTO PAGE 0, SO WE MUST CREATE A
				; DUMMY PAGE INTO WHICH WE WILL READ
				; THIS DATA PAGE. THEN WE WILL MOVE
				; THIS PAGE TO PAGE 0 AND EVERYTHING
				; WILL BE OK.
	 JRST	NEED1P		;COULDN'T GET FREE PAGE
	PUSHJ	P,CRPAGE##	;CREATE IT
	 JRST	NEED1P		;COULDN'T
	LSH	T1,P2WLSH##	;PAGE ADDRESS
	PUSH	P,T1		;SAVE PAGE ADDRESS FOR A SEC...
	SOS	T1		;=START-1
	HRRM	T1,SGALEN##(R)	;STORE IN IOWD
	PUSHJ	P,SKPBLK	;MAKE SURE WE'RE AT THE RIGHT PLACE
	PUSHJ	P,RDSTUF	;READ PAGE FROM SAVE FILE
	HRLZ	T1,0(P)		;PAGE ADDRESS IN LEFT HALF, 0 IN RH
>;END IFN FTVM
IFE FTVM,<
	MOVEI	T1,EXESIZ-1	;READ THE FIRST PAGE (0-777) INTO PAGE 1
	HRRM	T1,SGALEN##(R)	;STORE IN IOWD
	PUSHJ	P,SKPBLK	;MAKE SURE WE'RE AT THE RIGHT FILE PAGE
	PUSHJ	P,RDSTUF	;READ PAGE 0 INTO PAGE 1
IFN FTKI10!FTKL10,<
	MOVSI	T1,EXESIZ	;SOURCE ADDR FOR BLT (PAGE 1)
>>;END IFN FTKI10 AND IFE FTVM
IFN FTKI10!FTKL10,<
	ADD	T1,[JOBDDT##,,JOBDDT##]	;SET UP BLT AC
	EXCTUU	<BLT	T1,EXESIZ-1>
>
IFN FTKA10,<
	MOVSI	T1,EXESIZ(R)
	HRRI	T1,(R)
	ADD	T1,[JOBDDT##,,JOBDDT##]	;DO BLT FOR KA'S
	BLT	T1,EXESIZ-1(R)	
>;END IFN FTKA10
	MOVE	T1,JOBDDT##(R)	;GET DDT'S ADDRESS
	MOVEM	T1,USRDDT##	;SAVE IT OVER CONTEXT SWITCH
IFE FTVM,<MOVEI	T1,EXESIZ>	;ADDR OF PAGE 1
IFN FTKA10,<
	ADDI	T1,(R)		;...RELOCATE REFERENCE TO PAGE 1
>
IFN FTVM,<POP	P,T1>		;GET TEMPORARY PAGE ADDRESS BACK
	EXCTUU	<MOVE T2,JOB41##(T1)>	;RESTORE JOB41
	MOVEM	T2,JOB41##(R)	;
IFE FTVM,<
	EXCTUU	<SETZM	(T1)>	;WE MUST NOW CLEAR PAGE 1 FOR LATER USE
	HRLS	T1		;PUT ADDR OF PAGE 1 IN BOTH HALVES
	MOVEI	T2,EXESIZ-1(T1)	;FORM ENDING BLT ADDRESS
	AOS	T1		;START OF PAGE 1,,START + 1
	PXCTUU	<BLT	T1,(T2)>,<PX.SRC!PX.MEM>  ;CLEAR PAGE 1
>;END IFE FTVM
IFN FTVM,<
	PUSHJ	P,ZAPPAG	;DESTROY IT
	 JRST	GTABRT##	;COULDN'T DO IT FOR SOME REASON
>;END IFN FTVM
	AOS	P3		;BUMP FILE PAGE #
	ADD	P2,[1,,0]	;..AND CURRENT LOCATION ON DEVICE
	ADD	P4,[777000,,1]	;INCR. PROCESS PAGE NO, DECR COUNT
	JUMPL	P4,GTLOOP	;IF ONLY PAGE 0, GO BACK FOR MORE

;IF MORE IN THIS DIRECTORY ENTRY, FALL THRU TO NEXT PAGE...
     
;COME HERE IF THE GROUP OF PAGES THAT WE ARE ABOUT TO FETCH
; FROM THE SAVE FILE DO NOT INCLUDE ANY OF THE DIRECTORY PAGES,
; OR PAGE ZERO.

NOTP0:	PUSHJ	P,SKPBLK	;POSITION OURSELVES ON THE DEVICE
IFN FTVM,<
	LDB	T3,[POINT 9,P4,8]	;GET REAPEAT COUNT
	HRRZ	T1,P4		;..AND PROCESS PAGE #
NOTP01:	PUSH	P,T3		;SAVE REPEAT COUNT
	PUSH	P,T1		;AND PAGE #
	TRNN	P3,-1		;ALLOCATED-BUT-ZERO?
	TLNE	P3,SV%SHR	;YES, SHARABLE?
	JRST	.+3		;NO, DON'T CREATE THIS PAGE ON DISK
	TLNE	P1,GT%PLE##	;PHYS. LIMIT EXCEEDED?
	TLO	T1,(1B1)	;YES, CREATE THIS PAGE ON DISK
	PUSHJ	P,CRPAGE	;CREATE THIS PAGE
	  JRST	[CAIE	T2,PAGCE%	;IS THIS AN OVERLAP ERROR?
		 JRST	NEED1P		;NO,SOME OTHER TYPE OF ERROR
		 TLNN	P1,GT%DAF	;YES, OVERLAP WITH THE DIRECTORY PAGE?
		 JRST	OVRERR		;NO, REPORT THE ERROR
		 JRST	.+1]		;YES, READ THE DATA OVER THE DIRECTORY PAGE
	TRNE	P3,-1		;SKIP IF ALLOCATED-BUT-ZERO PAGE
	TLNN	P1,GT%PLE##	;ARE WE READING 1 PAGE AT A TIME?
	JRST	NOTP02		;NO, GO ON
	LSH	T1,P2WLSH##	;RETURN WITH T1=PAGE #, FIND ADDRESS
	SOS	T1		;=START OF PAGE-1
	HRRM	T1,SGALEN##(R)	;STORE IN IOWD
	PUSHJ	P,RDSTUF	;NO, MUST READ PAGES IN
				; AS WE CREATE THEM
	MOVE	T1,(P)		;GET SAVED PAGE #
	MOVE	J,JOB##		;MAKE SURE WE HAVE THE RIGHT JOB
	TLNE	P1,GT%PLE##	;PHYS. LIMIT EXCEEDED?
	TLNE	P3,SV%SHR	;AND NON-SHARABLE HIGH SEG?
	SKIPA			;NO
	PUSHJ	P,PAGOUT##	;YES, PAGE IT OUT
	  JFCL			;SHARABLE OR COULDN'T PAGE OUT
NOTP02:	POP	P,T1		;RESTORE PAGE # JUST CREATED
	POP	P,T3		;AND REPEAT COUNTER
	AOS	T1		;BUMP CURRENT PAGE #
	SOJGE	T3,NOTP01	;MORE PAGES TO CREATE?
	TLNE	P1,GT%PLE##	;ONE PAGE AT A TIME?
	JRST	NTRYLP		;YES
>	;END IFN FTVM
	LDB	T2,[POINT 9,P4,8]	;NO, GET REPEAT COUNT AGAIN
	AOS	T2		;=# OF PAGES IN GROUP
IFE FTVM,<
	TLNN	P3,SV%HIS	;IS THIS THE HIGH SEG?
	JRST	NOTP02		;NO
	HLR	P4,SGAHGH##(R)	;YES, RESET START OF HIGH SEG
	HRLZ	T3,T2		;# OF PAGES IN HIGH SEG,,0
	ADDM	T3,SGAHGH##(R)	;RESET START OF HIGH SEG
NOTP02:
>
	HRRZ	T1,P4		;GET 1ST PAGE #
	LSH	T1,PXWLSH	;=START ADDRESS OF GROUP
	SOS	T1		;=START-1
	HRRM	T1,SGALEN##(R)	;STORE IN IOWD
	LSH	T2,PXWLSH	;=LENGTH OF SEGMENT
IFE FTVM,<
	ADDI	T1,(T2)		;=LAST LOC TO BE READ
	CAMLE	T1,USRREL##	;TOO HIGH?
	JRST	BADDIR		;YES
>
	MOVNS	T2		;-LENGTH OF SEGMENT
	HRLM	T2,SGALEN##(R)	;STORE IN IOWD
	TRNE	P3,-1		;UNLESS PAGES ARE ALLOCATED-BUT-ZERO...
	PUSHJ	P,RDSTUF	;READ IN ENTIRE GROUP OF PAGES
;COME HERE AFTER PROCESSING OF EACH DIRECTORY ENTRY
;
NTRYLP:	LDB	T1,[POINT 9,P4,8]	;GET REPEAT COUNT AGAIN
	MOVSI	T1,1(T1)	;# OF PAGES,,0
	TRNE	P3,-1		;IF THESE PAGES WERE ABZ, DON'T...
	ADD	P2,T1		;BUMP CURRENT LOCATION POINTER
	TLNE	P3,SV%SHR	;DID WE SEE A SHARABLE HIGH SEG?
	TLO	P1,SV%SHR	;YES, REMEMBER THAT FACT
	JRST	GTLOOP		;GO BACK FOR MORE
;COME HERE WHEN THE ENTIRE SAVE FILE HAS BEEN READ IN...
;
GETFIN:
IFN FTVM,<
	HRRZ	T1,P1		;GET CURRENT DIRECTORY POINTER
	TLNN	P1,GT%DAF##	;DID WE READ IN THE LAST FILE PAGE
				; OVER THE DIRECTORY PAGE?
	PUSHJ	P,ZAPPAG##	;NO, FLUSH THE DIRECTORY PAGE
	 JFCL
	HLRZ	T1,SGAHGH##(R)	;GET 1ST PAGE OF HIGH SEG
	JUMPE	T1,CPOPJ##	;IF NO HIGH SEG, EXIT NOW
	MOVE	T2,T1		;IT IS ALSO THE NEW STARTING VIRT PAGE #
	HRRZ	T3,SGAHGH##(R)	;GET # OF PAGES IN HIGH SEG
	MOVE	T4,P1		;SET T4 TO BE NON-ZERO
	TLNE	T4,SV%SHR	;DID WE SEE A SHARABLE HIGH SEG?
	SETZ	T4,		;YES, T4=0
>;END IFN FTVM
IFE FTVM,<
IFN FTKI10,<
	HRRZ	T1,SGAHGH##(R)	;GET # OF PAGES IN HIGH SEG
	SUB	T1,SGALOW##(R)	;=-# OF PAGES IN LOW SEG
	JUMPE	T1,GTFIN2	;IF NO LOW SEG, WE DIDN'T GET EXTRA CORE
	MOVE	T1,SGALOW##(R)	;FETCH TOTAL PAGES IN CORE IMAGE
	CAILE	T1,2
	JRST	GTFIN2
	LSH	T1,PXWLSH	;FIND LAST LOC IN LOW SEG+1
	SOS	T1		;=LAST VALID LOW SEG LOC
	PUSHJ	P,GETCOR	;REDUCE CORE AND GET RID OF DIRECTORY
GTFIN2:
>
IFN FTKA10,<
	HRRZ	T1,SGAHGH##(R)	;# HIGH PAGES
	SKIPN	T1		;IF NONE,
	HRRZ	T1,SGALOW##(R)	;=#PAGES IN LOW SEG
	HRRZ	T2,P1		;GET ADDR OF DIRECTORY
	TRZ	T2,EXESIZ-1	;POINT TO START
	TRNE	P3,-1		;IF LAST PAGE ABZ
	TRNE	T1,1		;OR ODD NUMBER PAGES, SO LAST UNUSED
	CAMLE	T2,USRREL##	;AND DIRECTORY PAGE STILL AROUND
	JRST	GTFIN3		;NOT TRUE, DIRECTORY ALREADY GONE
	ADDI	T2,(R)		;RELOCATE
	HRRZ	T1,(T2)		;GET LENGTH
	EXCTUU	<SETZM(T2)>	;CLEAR FIRST WORD OF DIRECTORY
	HRLS	T2		;PUT ADDR IN BOTH HALVES
	ADDI	T1,LN.END-1(T2)	;LAST LOC IN DIRECTORY
	AOS	T2		;START OF PAGE, START+1
	BLT	 T2,(T1)	;CLEAR DIRECTORY
GTFIN3:
>
	SKIPN	T2,SGAHGH##(R)	;IF THERE IS NO HIGH SEG...
	POPJ	P,		; WE CAN EXIT WITHOUT REMAP'ING
IFN FTKA10,<
	TRNE	T2,1		;IF ODD#
	AOS	T2		;MAKE IT EVEN
>
	LSH	T2,PXWLSH	;=LENGTH OF HIGH SEG
	MOVE	T1,USRREL##	;GET CURRENT TOP OF USER CORE
	SUBI	T1,(T2)		;=LAST LOC IN LOW SEG
	HRRM	T1,SGALEN##(R)	;STORE THIS ADDRESS IN IOWD FOR
				; "INPSE3" TO SEE. THIS IS NOT REALLY
				; NECESSARY EXCEPT IN THE CASE OF
				; A HIGH SEG WITH MULTIPLE DIRECTORY ENTRIES
>;END IFE FTVM
;COME HERE TO REMAP HIGH SEG
	PUSH	P,JOBSA##(R)	;REMAP CLEARS THESE JOB DATA LOCS
	PUSH	P,JOBREN##(R)	;...
	PUSH	P,JOBHRL##(R)	;...
IFN FTVM,<
	PUSHJ	P,NREMAP##	;DO THE REMAP
	 STOPCD	.,STOP,RDS,	;++REMAP DIDN'T SKIP
>;END IFN FTVM
IFE FTVM,<
	PUSHJ	P,INPSE3	;DO REMAP
	 JRST	GETH2B		;COULDN'T
>;END IFE FTVM
	POP	P,JOBHRL##(R)	;RESTORE JOB DATA LOCS
	POP	P,JOBREN##(R)	;...
	POP	P,JOBSA##(R)	;...
IFN FTVM,<
	SOS	(P)		;SINCE SHARE1 WILL EXIT TO CPOPJ1,
				; WE MUST BACK UP THE RETURN ADDR SO RETURN
				; TO CPOPJ
	JSP	T3,SHARE1	;GO SET UP JBTSGN, ETC...
>;END IFN FTVM
IFE FTVM,<
	PJRST	SETMAP##	;JUST RETURN
>;END IFE FTVM


;SUBROUTINE TO CHECK TO SEE IF THE CURRENT ENTRY IS WHAT
; WE WANT
;
;CALL:
;	DMOVE	P3,DIRECTORY-ENTRY
;	PUSHJ	P,WANTIT
;	HERE IF NO
;	HERE IF YES
;
WANTIT:	MOVSI	T2,GTHGH##	;ASSUME HIGH SEG
	TLNN	P3,SV%HIS	;IS THIS THE HISEG?
	MOVSI	T2,GTLOW##	;NO, SET UP LOW SEG BIT
	TDNN	P1,T2		;YES, DID WE WANT THIS SEGMENT?
	POPJ	P,		;NO
	HRRZ	T1,P4		;GET PROCESS PAGE #
	CAIL	T1,EXESIZ	;CHECK UPPER LIMIT
	JRST	BADDIR		;TOO HIGH
	JUMPL	P3,WANTI1	;GO IF A HI SEG
	TLNE	P3,SV%WRT	;LOW SEG MUST BE WRITABLE
	TLNE	P3,SV%CON+SV%SHR ;AND NOT CONC OR SHARABLE
	JRST	BADDIR		;ERROR IF SO
	JRST	CPOPJ1##	;YES


WANTI1:	TLNE	P3,SV%SHR	;THIS PAGE SHARABLE?
	TLON	P1,GT%S1S##	;YES, SAW 1 SHARABLE PAGE
	TLNN	P1,GT%S1S##	;1ST SHARABLE PAGE, OR NOT SHARABLE
	TLOA	P1,GT%GTH##	;THIS PAGE IS OK, SAW A HI-SEG PAGE
	TLON	P1,GT%GTH##	;MODE CHANGE (HI TO SHR OR VICE VERSA). 1ST HI PAGE?
	TRNN	P4,-1		;PAGE OK, PAGE 0 CANT BE IN HI-SEG
	JRST	BADDIR		;PAGE 0 OR MIXTURE OF SHR AND NON-SHR PAGES
	JRST	CPOPJ1##	;PAGE IS OK, TAKE GOOD RETURN

;SUBROUTINE TO READ IN A PAGE FROM THE SAVE FILE
; AND PERFORM SOME MISCELLANEOUS HOUSE-KEEPING
;
;CALL:
;	PUSHJ	P,RDSTUF
;	RETURN HERE IF OK
;
RDSTUF:	MOVE	T1,DEVIOS(F)	;GET STATUS
	TRNE	T1,IODEND	;EOF?
	JRST	BADDIR		;YES, FILE TOO SHORT
	TLNN	P1,GTLOW##	;DON'T SET USRHCU ON A GETSEG BECAUSE
				; THE BLT TO RESTORE USER CHANNELS WILL FAIL
	JRST	RDSTF2
	HRROS	USRHCU##	;SET SAVE/GET FLAG
	HRROS	JOBHCU##(R)	; AND IN USER AREA
RDSTF2:	PUSH	P,P1		;AN INPUT KILLS THESE AC'S
	PUSH	P,P4		;...
	INPUT	SGALEN##	;READ A PAGE
	PUSHJ	P,SGIOCK##	;CHECK FOR ERRORS
	HRRZS	USRHCU##	;CLEAR FLAG
	HRRZS	JOBHCU##(R)
PP4P1:	POP	P,P4		;RESTORE AC'S
	POP	P,P1		;...
	POPJ	P,		;RETURN
IFN FTVM,<

;SUBROUTINE TO MOVE A DIRECTORY PAGE
;
;CALL:
;	MOVE	T1,PAGE # TO MOVE
;	PUSHJ	P,MOVDIR
;	RETURN WITH PAGE MOVED AND DIRECTORY POINTERS UPDATED
;
;COME HERE IF ONE OF THE DIRECTORY PAGES IS ABOUT TO BE
; OVER-WRITTEN BY THE CURRENT DIRECTORY ENTRY. THE DIRECTORY PAGE
; IS MOVED TO ANOTHER FREE PAGE.

MOVDIR:	PUSH	P,T1		;SAVE DIR PAGE # WHICH WE MUST MOVE
	MOVEI	T1,PAGSIZ##-1	;START LOOKING FOR A FREE PAGE AT PAGE 777
MOVDR2:	PUSHJ	P,GTFRE2##	;TRY TO FIND A FREE PAGE
	 JRST	MOVDR3		;COULDN'T, WE'VE GOT PROBLEMS
	PUSHJ	P,CKRANG	;IS THIS ONE IN THE WAY TOO?
	 SOJA	T1,MOVDR2	;YES, AND LOOK FROM WHERE WE LEFT OFF
	POP	P,T2		;RESTORE PAGE # OF DIRECTORY TO MOVE
	MOVSS	T2		;PUT SOURCE PAGE IN LH
	HRR	T2,T1		;DESTINATION PAGE IN RH
	MOVSI	T1,.PAGEM	;SET UP FUNCTION CODE FOR PAGE. UUO
	PUSHJ	P,PAGUUO##	;MOVE THE DIRECTORY PAGE TO FREE PAGE
	 JRST	NEED1P		;COULDN'T
	HRRZ	T4,T1		;GET PAGE # WHICH WAS RETURNED BY PAGUUO
	HLRZ	T1,T1		;GET OLD DIRECTORY PAGE 
	PUSHJ	P,CKDIRP##	;FIND WHICH DIR IT WAS
	DPB	T4,[POINT 9,(T3),26]	;THIS STORES THE NEW
				; DIRECTORY PAGE NUMBER IN THE OLD
				; DIRECTORY POINTER. IF THE OLD DIRECTORY
				; PAGE IS KEPT IN SGANAM OR SGAEXT, THEN
				; THIS IS NOT THE QUICKEST WAY TO RESET
				; THE POINTERS. BUT IF THE OLD DIR. PAGE
				; IS KEPT IN P1 (AS IS 99% CERTAIN),
				; THEN THIS TECHNIQUE MEANS THAT WE
				; DON'T HAVE TO WORRY ABOUT ANY OFFSET
				; WITHIN THE PAGE. BY JUST CHANGING THE
				; PAGE #, WE PRESERVE THE CURRENT RELATIVE
				; STATE OF THE DIRECTORY PAGE POINTER
	POPJ	P,		;RETURN

MOVDR3:	TLO	P1,GT%DAF##	;SET "DIRECTORY ALREADY FLUSHED" BIT
				; WHICH MEANS THAT THE CORE IMAGE EXACTLY
				; FITS INTO THE AVAILABLE CORE SPACE.
				;THE LAST FILE PAGE WILL OVERWRITE THE
				; LAST DIRECTORY PAGE SO THAT THIS PAGE
				;DOESN'T HAVE TO BE FLUSHED.
	JRST	TPOPJ
>;END OF IFN FTVM
;STILL IN IFN FTEXE CONDITIONAL...
;ERROR ROUTINES FOR NEW SAVE FILES
;
;HERE IF 1 MORE PAGE IS NEEDED FOR VARIOUS REASONS
NEED1P::PUSH	P,[0]		;WILL BE INCREMENTED TO 1P
	JRST	NROOM		;PRINT ERROR MESSAGE

;OVERLAY ERROR 
OVRERR:	MOVEI	T1,LOHERR	;OVERLAP ERROR
	PUSHJ	P,SGRELE##	;RESET DEVICE
	JSP	T1,PHOLD##	;START TTY AND STOP JOB
	ASCIZ	/PAGE OVERLAP ERROR/

;DIRECTORY FORMAT ERROR
BADDIR:	MOVEI	T1,BEDERR	;BAD EXE DIRECTORY ERROR
	PUSHJ	P,SGRELE##	;RESET DEVICE
	JSP	T1,PHOLD##
	ASCIZ	/BAD FORMAT FOR EXE FILE DIRECTORY/


;NOT AN .EXE FILE (1ST WORD DID NOT HAVE PROPER DIRECTORY CODE IN IT)
NOTEXE:	MOVEI	T1,BEEERR	;ERROR CODE IN CASE THIS IS A RUN UUO
	PUSHJ	P,SGRELE##	;RELEASE DEVICE AND ERROR RETURN TO
				; USER IF RUN UUO OR FIND TTY AND
				; PRINT ?CRLF
	JSP	T1,PHOLD##	;START TTY AND STOP JOB
	ASCIZ	/NOT AN EXE FILE/

EXESAV::MOVEI	T1,BEEERR	;ERROR CODE FOR RUN/GETSEG UUO
	PUSHJ	P,SGRELE##	;AN EXE FILE DID NOT HAVE EXE
	JSP	T1,PHOLD##	; EXTENSION
	ASCIZ	/MUST HAVE "EXE" EXTENSION/

EXEBIG:	MOVEI	T1,DTBERR	;ERROR CODE FOR RUN/GETSEG UUO
	PUSHJ	P,SGRELE##	;EXE DIRECTORY WAS TOO BIG
	JSP	T1,PHOLD##
	ASCIZ	/FILE DIRECTORY IS TOO LARGE/
;STILL IN IFN FTEXE CONDITIONAL...
;SUBROUTINE TO CHECK CURRENT LOCATION ON THE DEVICE
;THIS ROUTINE IS USED ONLY IN THE CASE WHERE THE
; DIRECTORY DATA PAGES ARE NOT CONTIGUOUS.
;
;CALL:
;	MOVSI	P2,CURRENT-PAGE-IN-SAVE-FILE
;	HRR	P3,FILE-PAGE-#-OF -THIS-ENTRY
;	PUSHJ	P,SKPBLK
;	RETURN HERE WITH THE DEVICE PROPERLY POSITIONED
;
SKPBLK:	TRNN	P3,-1		;IF PAGES ARE ABZ, WE DON'T NEED
	POPJ	P,		; TO CHECK LOCATION ON DEVICE
	PUSH	P,P1		;USETI KILLS THESE AC'S
	PUSH	P,P4		;SO, WE SAVE THEM FIRST
SKPBK2:	HLRZ	T3,P2		;GET CURRENT FILE PAGE #
	SUBI	T3,(P3)		;FIND DIFFERENCE IN WHERE WE ARE ON
				; THE DEVICE, AND WHERE WE WANT TO GO
	JUMPE	T3,PP4P1	;IF WE ARE THERE, RESTORE P4,P1 AND EXIT
	JUMPG	T3,BADDIR	;IF WE ARE PAST WHERE WE WANT TO BE,
				; IT CAN ONLY BE A BAD FORMAT FILE
	MOVE	T4,DEVMOD(F)	;WE ARE NOT THERE YET (CAN ONLY HAPPEN IF THERE
				; ARE "HOLES" IN THE 
				; SAVE FILE, IMPOSSIBLE UNLESS
				; THE DEFINED STRUCTURE OF THE
				; SAVE FILE CHANGES IN THE
				; FUTURE, OR IF WE ARE SKIPPING 1 SEGMENT.
	TLNE	T4,DVDSK	;IS IT A DISK?
	JRST	[HRL	P2,P3	;YES, RESET COUNTER OF CURRENT FILE PAGE #
		 HRRZ	T3,P3	;FETCH FILE PAGE # FOR THIS ENTRY
		 LSH	T3,2	 ;FIND BLOCK NUMBER
		 USETI	1(T3)	;MOVE TO CORRECT FILE PAGE #
		 JRST	PP4P1]	; AND CONTINUE
	PUSHJ	P,RDSTUF	;NO, READ PAGE OF DATA FROM DEVICE
	ADD	P2,[1,,0]	;BUMP CURRENT FILE PAGE #
	JRST	SKPBK2		;AND CHECK AGAIN


;ROUTINE TO GET THE NEXT DIRECTORY ENTRY FROM THE SAVE FILE
;
;CALL:
;	MOVE	P1,ADDRESS-OF-ENTRY
;	MOVE	P2,CURRENT-LENGTH-OF-DIRECTORY
;	PUSHJ	P,GTDIRE
;	 HERE IF DIRECTORY IS FINISHED
;	HERE IF MORE IN DIRECTORY (ENTRY IS IN P3,P4)
;
GTDIRE:	TRNN	P2,-1		;IS THERE ANY MORE?
	POPJ	P,		;NO
	SUBI	P2,NTRYSZ	;DECREMENT LENGTH OF ENTRY
	PUSHJ	P,GTDIRW	;GET 1ST WORD
	MOVE	P3,T1		;STORE IT IN P3
	PUSHJ	P,GTDIRW	;GET NEXT WORD
	MOVE	P4,T1		;STORE IT IN P4
	JRST	CPOPJ1##	;SKIP RETURN




;ROUTINE TO GET ONE WORD FROM THE DIRECTORY
;
;CALL:
;	MOVE	P1,ADDRESS-OF-WORD
;	PUSHJ	P,GTDIRW
;
;ON EXIT, T1 WILL CONTAIN THE WORD FETCHED FROM THE DIRECTORY
;
;IF THE PAGE IS FINISHED, THE PAGE POINTED TO BY P1 IS
; REMOVED FROM THE LOGICAL ADDRESSING SPACE AND A NEW
; DIRECTORY PAGE IS FETCHED FROM SGANAM.

GTDIRW:	HRR	M,P1		;GET POINTER TO DIRECTORY
	PUSHJ	P,GETWDU##	;GET WORD FROM USER SPACE
	AOS	P1		;BUMP POINTER
	TRNE	P1,EXESIZ-1	;OVERLAP TO NEW PAGE?
	POPJ	P,		;NO
IFN FTVM,<
	SUBI	P1,EXESIZ	;YES, MOVE POINTER BACK TO LAST PAGE
	PUSH	P,T1		;SAVE CURRENT DIRECTORY ENTRY
	HRRZ	T1,P1		;VIRTUAL ADDRESS OF PAGE TO BE ZAPPED
	PUSH	P,T3		;THESE ARE THE CURRENT DIRECTORY ENTRIES,
	PUSH	P,T4		; SO, WE MUST SAVE THEM
	TLNN	P1,SV%DWO##	;IS THIS A TRIAL RUN? (ARE WE MERELY
				; SCANNING THE DIRECTORY TO COMPUTE
				; THE AMOUNT OF CORE REQUIRED)
	PUSHJ	P,ZAPPAG##	;NO, KILL THIS PAGE
	 JFCL			;?
	POP	P,T4		;RESTORE AC'S
	POP	P,T3
	HLLZ	T1,P1		;SAVE FLAGS
	EXCH	P1,SGAEXT##(R)	;ROTATE DIRECTORY POINTERS
	EXCH	P1,SGANAM##(R)
	TLNE	T1,SV%DWO##	;SCANNING DIRECTORY?
	TLOA	P1,SV%DWO##	;YES, MAKE SURE PAGES AREN'T FLUSHED
	HLL	P1,T1		;NO, RESTORE FLAGS
	JRST	TPOPJ##		;RESTORE DIRECTORY ENTRY AND RETURN
>;END IFN FTVM

IFE FTVM,<
	STOPCD	CPOPJ,DEBUG,DPO,  ;++DIRECTORY PAGE OVERLAP
>


;STILL IN IFN FTEXE CONDITIONAL...
;ROUTINE TO SET UP FOR A NEW STYLE SAVE FILE GET
;
;CALL:
;	MOVSI	P1,FLAGS
;	PUSHJ	P,GETNEW
;	 HERE IF ERROR
;	HERE OTHERWISE
;

GETNEW:	MOVE	J,JOB##		;RESTORE JOB NUMBER
IFN FTLOCK!FTKI10,<
	MOVSI	T2,NSHF!NSWP	;CAN'T ASSIGN CORE IF JOB IS LOCKED
	TDNE	T2,JBTSTS##(J)	;LOCKED?
	JRST	[MOVEI	T1,SNSERR	;SEGMENT NOT ON SWAPPING SPACE
		 POPJ	P,]
>;END OF FTLOCK!FTKI10

	TLNE	P1,GTHGH##	;DOES HE WANT THE HIGH SEG?
	PUSHJ	P,KILHGC	;YES, REMOVE PRESENT HISEG FROM LOG.
				; ADR. SPACE

GETLOW:	TLNN	P1,GTLOW##	;DOES HE WANT THE LOW SEGS?
	JRST	GTHILO		;NO
	MOVEI	T1,EXESIZ-1	;SET UP FOR A CORE 0
	PUSH	P,P1		;SAVE FLAGS
IFE FTVM,<
	PUSHJ	P,GETCOR##	;YES, DO THE CORE. UUO, RETURN IF OK
	HRRZS	JOBPD1##(R)	;CAN'T RETURN TO THE USER NOW
>
IFN FTVM,<
	PUSHJ	P,GET1PG##	;GET COR MIGHT CALL GETSEG
>
	POP	P,P1		;RESTORE FLAGS

GTHILO:	PUSH	P,SGANAM##(R)	;SAVE THIS STUFF
	PUSH	P,SGAEXT##(R)	;...
	PUSH	P,SGALOW##(R)	;SAVE EXTENSION
	PUSH	P,SGANEW##(R)	;SAVE USER CORE ARG
	PUSHJ	P,GETEXE	;READ THE EXE FILE
	POP	P,SGANEW##(R)	;RESTORE USER CORE ARG
	POP	P,SGALOW##(R)	;RESTORE EXTENSION
	POP	P,SGAEXT##(R)	;RESTORE GOOD STUFF
	POP	P,SGANAM##(R)
	MOVE	J,JOB##		;MAKE SURE J IS SET UP WHEN WE LEAVE
	JRST	CPOPJ1##	;RETURN TO CALL+2
IFN FTVM,<

;ROUTINE TO CHECK TO SEE IF A GIVEN PAGE IS WITHIN THE RANGE
; OF THE CURRENT DIRECTORY ENTRY. THIS ROUTINE IS NECESSARY
; TO MAKE SURE THAT A DIRECTORY PAGE IS NOT OVERWRITTEN,
; AND TO DO SOME OPTIMIZATION ON THE CHOICE OF A NEW PAGE
; IF A DIRECTORY PAGE MUST BE MOVED.
;
;CALL:
;	MOVE	T1,PAGE ADDRESS/PAGE #
;	DMOVE	P3,DIRECTORY-ENTRY
;	PUSHJ	P,CKRADR/CKRANG
;	 HERE IF THERE IS AN OVERLAP
;	HERE IF OK
;
CKRADR:	LSH	T1,W2PLSH##	;MAKE INTO A PAGE NUMBER
CKRANG:	LDB	T2,[POINT 9,P4,8]	;GET REPEAT COUNT
	ADDI	T2,(P4)		;FIND LAST PAGE OF THIS ENTRY
	CAIL	T1,(P4)		;CHECK IF NEW PAGE IS LESS THAN 1ST PAGE
	CAILE	T1,(T2)		;OR GREATER THAN LAST PAGE
	AOS	(P)		;YES, NO CONFLICT
	POPJ	P,		;OVERLAP, MUST FIND NEW PAGE


>;END OF IFN FTVM
>;END OF IFN FTEXE
SUBTTL	SWAP - USER PROGRAM SWAPPING ROUTINES

;ROUTINE CALLED AFTER A HIGH OR LOW SEG JUST SWAPPED IN
;IT SEES IF A HIGH SEG IS STILL TO BE SWAPPED IN
;DOES POST PROCESSING AFTER HIGH SEG SWAPPEN IN
;CALL:	MOVE J,JOB NUMBER
;	PUSHJ P,FININ
;	RETURN0 (VM SYSTEMS ONLY) IF THE HIGH SEG CURRENTLY
;		HAS SWAPPING I/O IN PROGRESS
;	RETURN1 IF HIGH SEG TO BE SWAPPEN IN(HIGH SEG NO. IN AC J)
;	RETURN2 IF NO HIGH SEG, OR HIGH SEG ALREADY IN, OR THIS WAS HIGH SEG
;		AC J RETURNED WITH JOB NUMBER(IE LOW SEG NO.)

;J CONTAINS JOB OR HIGH SEG NUMBER(SAME AC AS SWAP USES)

IFN FTSWAP,<

FININ::	CAILE	J,JOBMAX##	;WAS A LOW SEG JUST SWAPPED IN?
	JRST	FINHGH		;NO, HIGH SEG JUST SWAPPED IN, GO FINISH
	MOVEM	J,SWPIN##	;YES, SAVE JOB NUMBER(FOR FINHGH AND RETURN)
IFN FTLOCK,<
	MOVE	T1,J		;IF LOCKING THE LOW SEGMENT,
				;  FORGET ABOUT SWAPPING IN THE HIGH SEGMENT.
				;  IF THE HIGH SEGMENT IS SWAPPED WHEN LOCKING
				;  COMPLETES, THE LOCK UUO WILL SET THE SWP
				;  BIT FOR THE LOW SEGMENT. THE LOK BIT
				;  WILL KEEP THE JOB FROM RUNNING.
>
	SKIPG	J,JBTSGN##(J)	;DOES JOB HAVE A REAL HIGH SEG?
	JRST	BOTHIN		;NO, GIVE NOTHING TO DO RETURN(MAYBE SPYSEG)
	SKIPE	JBTADR##(J)	;YES, IS HIGH SEG ALREADY IN CORE?
				;(NOT POSSIBLE TO BE IN MIDDLE OF SWAPPING)
	JRST	FIN0
IFN FTLOCK,<
	CAMN	T1,LOCK##
	JRST	BOTHIN
>
IFE FTVM,<
	POPJ	P,		;NO, GIVE "SWAP-IN-HIGH-SEG" RETURN,
				; AC J SET TO HIGH SEG NO.
>
IFN FTVM,<
	JRST	CPOPJ1##
>
FIN0:
IFN FTVM,<
	MOVSI	T1,SWP		;SWP ON FOR THE HI SEG?
	TDNE	T1,JBTSTS##(J)	; ONLY IF SWAPPING I/O IS IN PROGRESS
	POPJ	P,		;RETURN DOING NOTHING UNTIL THE HI-SEG I/O IS DONE
>
	PUSHJ	P,CHKIDL	;YES, CHECK IF THIS HIGH SEG IS IDLE(IF YES,
				; DECREASE CORTAL=FREE+DORMANT+IDLE CORE
				; BY SIZE IN K OF HIGH SEG SINCE IT WILL
				; NO LONGER BE IDLE(IN CORE COUNT NON-ZERO)

	MOVE	T1,SWPIN##	;RESTORE JOB NUMBER
	PUSHJ	P,INCCNT	;INCREMENT HIGH SEG IN CORE COUNT
				; FOR THIS JOB
IFN FTHALT,<
	PUSHJ	P,CHKTAL	;CHECK CORTAL WITH CORTAB, HALT IF DIFFER
>
IFN FTVM,<
	TLO	J,1
	JRST	FINHG1
>
FINHGH:
IFN FTVM,<
	MOVE	T1,SWPIN##	;JOB WHICH HISEG WAS SWAPPED IN FOR
	HLL	J,JBTSGN##(T1)	;GET THE STATE OF UWP
>
FINHG1:	MOVSI	T1,SWP		;CLEAR SWAPPED OUT OR ON WAY FLAG FOR HIGH SEG
	ANDCAM	T1,JBTSTS##(J)	;AND CLEAR SUPPRESS IN-CORE COUNT INCREMENT FLAG

	TLNE	J,UWPOFF	;IS USER MODE WRITE PROTECT(UWP) OFF
				; FOR THIS USER?
	PUSHJ	P,ZERSWP##	;YES, CHANGE SWAPPING SPACE TO 0 FOR HIGH SEG
				;0 TO IMGOUT,IMGIN, AND SWP IN JBTSTS(HIGH SEG)
				;NO, KEEP COPY OF SEG ON DISK(USUAL CASE)
				; WHETHER SHARABLE OR NOT
IFN FTVM,<
	TLZN	J,1
	PUSHJ	P,DLTSLE##	;DELETE ENTRY FROM SWPLST
>
BOTHIN:	MOVE	J,SWPIN##	;RESTORE JOB NUMBER(LOW SEG NUMBER)
IFE FTVM,<
	JRST	CPOPJ1##	;SKIP RETURN
>
IFN FTVM,<
	JRST	CPOPJ2##	;SWAPPING I/O DONE RETURN
>
>
     

;ROUTINE TO CHECK FOR EXPANDING HIGH SEGMENT ON SWAP IN
;IF A JOB IS USING A SHARABLE HIGH SEGMENT WHICH HAS BEEN
;EXPANDED BY ANOTHER JOB, NONE OF THE SHARERS CAN BE
;SWAPPED IN UNTIL ALL HAVE BEEN SWAPPED OUT (JXPN=0)
;CALL:	MOVE J,JOB NUMBER (OR HI SEG) TO BE SWAPPED IN
;	PUSHJ P,CKXPN
;	  RETURN1 IF HIGH SEG IS EXPANDING (J=SAME)
;	RETURN2 ON ALL OTHER CONDITIONS (J=SAME)
;NOTE:	T1 AND T2 ARE PRESERVED (HENCE SHORT NAME FOR ROUTINE)

IFN FTSWAP,<

CKXPN::	CAIG	J,JOBMAX##	;IS THIS A LOW SEG (JOB #)?
	SKIPG	T3,JBTSGN##(J)	;YES, DOES JOB HAVE A HIGH SEG?
	JRST	CPOPJ1##	;NO, RETURN IMMEDIATELY AND TRY TO FIT
	MOVE	T3,JBTSTS##(T3)	;YES, GET HIGH SEG STATUS WORD
	TLNN	T3,JXPN		;IS THIS HIGH SEG EXPANDING?
	AOS	(P)		;NO,
	POPJ	P,		;YES, DO NOT TRY TO SWAP THIS JOB IN
>
     
;ROUTINE CALLED JUST BEFORE HIGH OR LOW SEG TO BE SWAPPED IN
; AND AFTER CORE HAS BEEN ASSIGNED
;FITHGH SEES IF HIGH SEG AND INCREASES IN-CORE COUNT(USUALLY TO 1)
; FOR JOB BEING SWAPPED IN(SWPIN ALREADY SET TO JOB # BY PREVIOUS CALL
; TO FININ FOR LOW SEGMENT OF THIS JOB

IFN FTSWAP,<

FITHGH::CAIG	J,JOBMAX##	;IS THIS A HIGH SEGMENT
	POPJ	P,		;NO,
	MOVE	T1,SWPIN##	;YES, GET JOB NUMBER(SET BY FININ) THAT 
				; GOES WITH THIS HIGH SEG
	JRST	INCCNT		;AND INCREMENT INCORE COUNT
>


;ROUTINE TO CHECK IF THIS WAS A HIGH SEG JUST SWAPPED OUT
;IF YES, RETURN JOB NUMBER SO LOW SEG CAN BE SWAPPED OUT(IF NOT ACTIVE IO)
;CALL:	MOVE J,HIGH OR LOW SEG NUMBER JUST FINISHED SWAPPING OUT
;	PUSHJ P,FINOT
;	RETURN1 - LOW SEG TO BE WRITTEN, C(J) SET TO JOB(LOW SEG) NUMBER
;	RETURN2 - NOTHIGN MORE TO SWAP OUT FOR THIS JOB

IFN FTSWAP,<

FINOT::	CAIG	J,JOBMAX##	;IS SEG JUST WRITTEN A HIGH SEG?
	JRST	CPOPJ1##	;NO, IT WAS A LOW SEG-NOTHING MORE TO DO
	MOVSI	T1,JXPN		;YES, CLEAR EXPAND BIT FOR THIS HIGH SEG
	ANDCAM	T1,JBTSTS##(J)	;IN CASE IT WAS ON.
	SKIPN	T1,SWPOUT##	;IF LOW SEG NO. IS 0,THIS SWAPOUT WAS
				;A HIGH IDLE SEG(IF NO ASSOCIATED LOW SEG)
	JRST 	FINOT1		;YES,ADJUST CORTAL AND GIVE NOTHING MORE TO
				;SWAPOUT RETURN(ALWAYS SKIP RETURN)
	HLL	J,JBTSGN##(T1)	;DECREMENT HIGH SEG IN CORE COUNT
	PUSHJ	P,DECCNT	;WITH CORRECT CORCNT BIT FROM TABLE
				;FOR JOB FOR WHICH IT WAS JUST SWAPPED OUT
	  JFCL			;ALREADY DECREMENTED. IGNORE.
IFN FTVM,<
	MOVE	T1,SWPOUT##	;JOB NUMBER THAT SWAPPING IS IN PROGRESS FOR
	MOVE	T1,JBTSTS##(T1)	;JOB STATUS
	TLNE	T1,SHF		;SWAPPING I/O IN PROGRESS FOR THIS LOW SEGMENT?
	JRST	CPOPJ1##	;YES, NOTHING TO DO
>
	JRST	 RETJOB		;YES, RETURN JOB NUMBER(LOW SEG NUMBER)
				; TO TRY TO BE SWAPPED OUT
FINOT1:	LDB	T1,IMGOUT##	;GET SIZE OF SHARABLE HIGH DATA SEG
	MOVNS	T1		;DECREMENT CORTAL SINCE SWAPPING IDLE CORE
	ADDM	T1,CORTAL##	;DOESN'T REALLY INCREASE FREE+IDLE+DORMANT COUNT
	MOVSI	T1,NSHF		;CLEAR NSHF WHICH WAS TURNED
	ANDCAM	T1,JBTSTS##(J)	; ON TO KEEP THE SEGMENT FROM APPEARING
				; DORMANT DURING THE SWAP OUT
	JRST	CPOPJ1##	;SKIP RETURN,NO LOW SEG TO SWAP OUT
>
     
;ROUTINE TO COMPUTE AMOUNT OF CORE NEEDED TO GET THIS USER INTO CORE
;CALL:	MOVE J,JOB NUMBER OR HIGH SEG NUMBER IF LOW SEG ALREADY IN
;	LDB P1,IMGIN		;SIZE OF THIS SEG WHEN SWAPPED IN(OR O IF ALREADY IN)
;	PUSHJ P,FITSIZ
;	RET1 - CANNOT FIT THIS USER IN(P1=SUM OF SEGS NEEDED)
;	RET2 - BOTH WILL FIT(P1 RESTORED TO SIZE OF THIS SEG)
;	J RESTORED ON BOTH RETURNS

IFN FTSWAP,<

FITSIZ::JUMPL	J,CPOPJ1##	;PDB'S ALWAYS FIT
	PUSH	P,P1		;SAVE SIZE OF THIS SEG FOR RETURN
	PUSH	P,J		;SAVE LOW OR HIGH SEG NUMBER
	CAILE	J,JOBMAX##	;IS THIS A LOW SEG(IF NO, LOW SEG ALREADY IN)?
	JRST	FITCHK		;YES--NO PDB FOR HISEGS
IFN FTPDBS,< ;ONLY COUNT PDB'S IF THE SWAP
	ADDI	P1,PDBPGS##	;LOWSEG ACCOUNT FOR PDB
> ;END FTPDBS
	SKIPG	J,JBTSGN##(J)	;YES, DOES THIS JOB HAVE A REAL HIGH SEG?
	JRST	FITCHK		;NO, GO SEE IF JUST THIS HIGH OR LOW SEG WILL FIT
	LDB	P2,IMGIN##	;YES, INCLUDE SIZE OF HIGH SEG WHEN SWAPPED IN
	SKIPN	JBTADR##(J)	;IS IT IN CORE SO NO SWAP IN NEEDED?
	ADD	P1,P2		;NO, ADD LENGTH OF HIGH SEG IF NEEDED
FITCHK:	POP	P,J		;RESTORE LOW OR HIGH SEG NUMBER
	CAMLE	P1,CORTAL##	;IS AMOUNT OF CORE REQUIRED, AVAILABLE?
				; EVEN IF ALL CORE DORMANT SEGS ARE DELETED
	JRST	TPOPJ##		;NO, POP OFF SIZE OF SEG AND RETURN
				; AND SCAN FOR JOB TO SWAP OUT
	POP	P,P1		;RESTORE SIZE OF THIS SEG FOR RETURN
	JRST	CPOPJ1##	;RETURN AND TRY SHUFFLING TO GET A HOLE BIG ENOUGH
				; IF THAT NOT GOOD ENOUGH, DELETE SOME DORMANT
				; SEGS FROM CORE
>

;ROUTINE TO RETURN HIGH PRIORITY QUEUE NO FOR A JOB
; IF ITS HIGH SEG IS BEING FIT INTO CORE
;CALL:	MOVE	J,FIT(LOW OR HI SEG)
;	PUSHJ	P,FITHPQ
;	RETURN WITH J=JOB NO BEING FITTED

FITHPQ::CAILE	J,JOBMAX##	;IS THIS A HIGH SEG BEING FIT?
	MOVE	J,SWPIN##	;YES, RETURN JOB NO.
	POPJ	P,		;RETURN

     
;ROUTINE TO COMPUTE SIZE OF CORE FREED UP IF A JOB WERE TO BE FORCED OUT
;CALL:	MOVE J,JOBNUMBER
;	MOVE F,LOW SEG SIZE IN 1K BLOCKS
;	PUSHJ P,FORSIZ
;	RETURN - AC F INCREMENTED APPROPRIATELY(C(J)=JOB NUMBER)
;	ACS T1 AND T2 ARE PRESERVED BECAUSE THEY ARE IN USE BY
;		QUEUE SCANNING ROUTINES

IFN FTSWAP,<

FORSIZ::PUSHJ	P,SAVE3##	;SAVE P1 - P3
	MOVSI	P3,SWP		;SWP BIT SET WHEN HIGH SEG SWAPPED
	TDNN	P3,JBTSTS##(J)	;HAS HIGH SEG ALREADY BEEN SWAPPED OUT FOR
				;THIS JOB(OR IN-CORE COUNT DECREMENTED)?
	SKIPG	P3,JBTSGN##(J)	;NO, DOES JOB HAVE A REAL HIGH SEG?
	POPJ	P,		;NO, RETURN J STILL SETUP TO JOB NUMBER
				; (MAYBE SPYSEG)
	MOVE	P2,JBTSTS##(P3)	;IS HIGH SEGMENT SWAPPABLE?
	TLNE	P2,SNA		;IDLE?
	TLNN	P2,NSWP		;YES, AND LOCKED?
	JRST	FORSZ1		;NO, PROCEED
	POPJ	P,		;YES, CAN'T BE SWAPPED OUT
FORSZ1:
IFN FTVM,<
	LDB	P2,IMGIP3##
>
IFE FTVM,<
	HLRZ	P2,JBTADR##(P3)	;YES - GET HISEG SIZE
>
	JUMPE	P2,CPOPJ##	;NOT IN CORE - TETURN
IFE FTVM,<
	LSH	P2,W2PLSH##	;COMPUTE SIZE IN 1K BLOCKS
	ADDI	P2,1
>
	MOVE	P3,JBTSTS##(P3)	;GET NO. OF IN CORE USERS
	ANDI	P3,ICCMSK	;..
	IDIV	P2,P3		;COMPUTE HISEG SIZE/NO. OR USERS
	ADDI	F,1(P2)		;ADD THIS TO TOTAL JOB SIZE AND ADD 1 FOR LUCK?
	POPJ	P,		;RETURN
>
     

;ROUTINE TO FREE UP CORE FOR NON-SWAPPING SYSTEMS
;BY DELETING 1 DORMANT SEGMENT
;CALL:	MOVE T1,LENGTH-1 DESIRED(HIGH OR LOW SEG)
;	MOVE J,JOB OR SEG NUMBER
;	PUSHJ P,FRECOR
;	RETURN1 - NOT ENOUGH CORE, EVEN IF ALL DORMANT SEG DELETED
;	RETURN2 - 1 DORMANT SEG DELETED, TRY TO ASSIGN CORE AGAIN
;	T1, AND J PRESERVED
;CALLED FROM CORE ROUTINE (NON-SWAPPING SYSTEMS ONLY) WHEN CORE NEEDED

IFE FTSWAP,<

FRECOR::MOVE	T2,CORTAL##	;NO. OF FREE & DORMANT 1K BLOCKS OF CORE
	LSH	T2,P2WLSH##	;CONVERY TO NUMBER OF WORDS
	CAML	T1,T2		;WILL REQUEST FIT, EVEN IF ALL DORMANT SEGS DELETED?
	POPJ	P,		;NO, ERROR (DON'T DELETE DORMANT SEGS FOR NOTHING)
				; YES, FALL INTO FRECR1
>

     
;ROUTINE TO FREE UP CORE FOR SWAPPING SYSTEMS
;BY DELETING ONE DORMANT OR IDLE SEGMENT IN CORE (DISK COPY LEFT IF ANY)
;CALL:	PUSHJ P,FRECR1
;ERROR RETURN-WITH J=HIGH IDLE SEG WITH NO COPY ON DISK (IT MUST BE 
;SWAPPED OUT BEFORE CORE IS FREED SINCE IDLE SEG IS IN SOME SWAPPED OUT USERS
;VIRTUAL ADDRESSING SPACE)
;	 SKIP RETURN - J PRESERVED

IFN FTDISK,<>

FRECR1::PUSH	P,T1		;SAVE CORE ARG(ON CALLS TO FRECOR)
	PUSH	P,J		; SAVE JOB OR SEG NUMBER
	MOVE	J,SEGPTR##	;SCAN JUST HIGH SEGMENTS
	MOVEI	T1,ICCMSK	;MASK FOR CHECKING NON-ZERO IN-CORE COUNT
	HRLI	T1,SNA		;LOOK FOR DORMANT SEGMENTS FIRST
				; SNA 20 AND IN CORE COUNT = 0
FREC0:	SKIPE	JBTADR##(J)	;DOES THIS HIGH SEG HAVE CORE?
	TDNE	T1,JBTSTS##(J)	;YES, IS IN-CORE COUNT EQUAL TO 0?
	AOBJN	J,FREC0		;NO, KEEP LOOKING, FINISHED?
	JUMPL	J,FREC4		;YES, DID WE FIND A DORMANT SEG WITH CORE?
	MOVE	J,SEGPTR##	;NO
	MOVEI	T3,ICCMSK	;MASK FOR CHECKING NON-ZERO IN-CORE COUNT
	HRLI	T3,NSHF!NSWP	;NOW LOOK FOR UNLOCKED IDLE SEGMENTS
FREC1:	SKIPE	JBTADR##(J)	;DOES THIS SEGMENT HAVE CORE?
	TDNE	T3,JBTSTS##(J)	;IS IT IDLE AND NOT LOCKED?
FREC2:	AOBJN	J,FREC1		;NO, KEEP LOOKING
	JUMPL	J,FREC3		;WAS AN UNLOCKED IDLE SEGMENT WITH CORE FOUND


IFE FTSWAP,<
	POP	P,J		;NO, RESTORE JOB OR SEG. #
	JRST	TPOPJ##		;RESTORE CORE REQUEST AND RETURN
>


IFN FTSWAP,<
	POP	P,J		;NO, MEMORY IS FRAGMENTED
	AOS	-1(P)		;GIVE DOUBLE SKIP RETURN SO
	JRST	TPOPJ1##	; SCHED WILL SHUFFLE OR SCAN FOR OUTPUT
>
FREC3:
IFN FTVM,<
	MOVSI	T1,SHRSEG	;SHARABLE SEGMENT BIT
	TDNN	T1,JBTSTS##(J)	;IS THIS A NON-SHARABLE HIGH SEGMENT?
	JRST	FREC2		;NO, IT LOOKS IDLE BUT CANNOT BE
>
	MOVE	T1,FIT##	;SEGMENT BEING SWAPPED IN
	CAIG	T1,JOBMAX##	;IS IT A HIGH SEGMENT?
	SKIPG	T1,JBTSGN##(T1)	;NO, DOES IT HAVE A HIGH SEGMENT?
	JRST	FREC4		;NO, ITS NOT AN IDLE SEGMENT BELONGING TO THE
	TLZ	T1,-1		; JOB BEING SWAPPED IN SO DELETE IT
	CAIN	T1,(J)		;IS JOB BEING SWAPPED IN SHARING THIS IDLE SEG?
	JRST	FREC2		; IDLE SEGMENT
;HERE FROM THE LOCK UUO TO DELETE A SPECIFIC DORMANT
; OR IDLE SEGMENT
FREC4::
IFN FTLOCK,<
	PUSH	P,J		;REMEMBER WHERE CALLED FROM
>
	TLZ	J,-1		;CLEAR OUT LH OF SEG NUMBER
IFN FTSWAP,<
	LDB	T1,IMGOUT##	;DOES HIGH SEG ALSO HAVE COPY ON DISK?
	JUMPN	T1,FREC5	;IF NON-ZERO, DO NOT CLEAR NAME SINCE NEW USERS
				; CAN USE DISK COPY
;HERE FROM FRECR1 ROUTINE IF FOUND AN IDLE OR DORMANT SEGMENT IN CORE 
;WITH NO DISK COPY SO CANNOT FREE UP CORE WITHOUT SWAPPING IDLE HIGH SEG FIRST
; NOT NECESSARY TO SWAP DORMANT SEG BUT USEFUL FOR MODIFIED HIGH
;SEGMENTS LIKE LOGIN TO STAY AROUND LONGER
	SETZM	SWPOUT##	;SET ASSOCIATED JOB NUMBER TO ZERO
				;AS A FLAG THAT THERE IS NO LOW SEGMENT
	MOVSI	T1,NSHF		;TURN ON NSHF
	IORM	T1,JBTSTS##(J)	; SO THAT THE SEGMENT WON'T APPEAR TO BE
				; DORMANT DURING THE SWAP OUT
IFN FTLOCK,<
	POP	P,T1		;IF CALLED FROM LOCK
	JUMPG	T1,CPOPJ##	;DON'T RESTORE ACS
>
	POP	P,T1		;EXIT AS ERROR RETURN FROM FREC1
	JRST	TPOPJ##		;WITH J=HIGH SEG TO BE SWAPPED OUT BECAUSE
				;CORE NEEDED AND THERE IS NO COPY
				;ON DISK (WRITE LOCK OFF)

;HERE IF HIGH SEG ALREADY HAS COPY ON DISK
FREC5:	PUSHJ	P,KDORCR	; JUST RETURN DORMANT OR IDLE CORE
				; BUT LEAVE DORMANT DISK COPY
				; STILL DORMANT ON DISK FOR NEW USERS
>
IFN FTLOCK,<
	POP	P,J		;IF CALLED FROM LOCK
	JUMPG	J,CPOPJ1##	;DON'T RESTORE ACS
>
	POP	P,J		;RESTORE JOB OR SEG NUMBER
	JRST	TPOPJ1##	;RESTORE CORE REQUEST AND SKIP RETURN
     
;ROUTINE TO CHECK IF JOB HAS A HIGH SEG WHICH NEEDS TO BE SWAPPED OUT
;IF YES, IT IS WRITTEN FIRST BEFORE LOW SEG
;IN THIS WAY THE IO BUFFERS MAY STAY ACTIVE LONGER
;IN FACT, THE HIGH SEG CAN BE BROUGHT BACK IN WITHOUT LOW SEG
;EVER LEAVING CORE.
;SHF BIT IS SET IN JOB STATUS WORD IF JOB ONLY HAS A LOW SEG, SO IO WILL STOP
;ON NEXT BUFFERFUL.  THUS ONLY 2 SEG JOBS HAVE ACTIVE IO LONGER.
;BUFFER ADVANCE ROUTINES LOOK AT SHF BUT NOT SWP FOR JOB
;CALL:	MOVEM FIT SWAP IN JOB NO.,FIT
;	MOVE J,LOW OR HIGH SEG NUMBER
;	PUSHJ P,FORHGH
;	ALWAYS RETURN WITH HIGH OR LOW SEG TO BE WRITTEN IN J
;
IFN FTSWAP,<

FORHGH::MOVSI	T1,SWP		;SET WHEN HIGH SEG SWAPPED OUT OR IN-CORE
				; COUNT REDUCED
	CAIG	J,JOBMAX##	;IS THIS A HIGH SEG(ONLY IF WAITING FOR HIGH SEG IO)
	TDNE	T1,JBTSTS##(J)	;NO, HAS HIGH SEG ALREADY BEEN SWAPPED FOR THIS USER?
	POPJ	P,		;YES, TRY HIGH SEG AGAIN OR WRITE LOW SEG
	IORM	T1,JBTSTS##(J)	;NO,SET SWAP BIT IN JOB STATUS WORD TO INDICATE
				; 1. HIGH SEG SWAPPED IF ANY OR LOW SEG SWAPPED IF NONE
	SKIPLE	JBTSGN##(J)	;DOES JOB HAVE A REAL HIGH SEG IN CORE OR DISK?
	JRST	FORH1		;YES
	MOVSI	T1,SHF		;NO, SET WAITING TO SHUFFLE BIT IN JOB STATUS WORD
				; SO IO WILL STOP AFTER NEXT BUFFERFUL(MAYBE SPYSEG)
	IORM	T1,JBTSTS##(J)	;CHECK EVEY BUFFERFUL IN ADVBFF, ADVBFE ROUTINES
	POPJ	P,		;RETURN AND TRY TO SWAP OUT LOW SEG
     
;ROUTINE TO CHECK FOR DANGLING HIGH SEGMENT
; WHEN A JOB CANNOT BE FIT INTO CORE
;DANGLING CAN ONLY HAPPEN IF IN-CORE COUNT COUNTED UP
; FOR A HI-SEG FOR JOB BEING SWAPPED IN BECAUSE JOB BEING SWAPPED OUT
; HAD SAME HI-SEG (USUALLY IN-CORE COUNT COUNTED UP AFTER BOTH SEGS ARE
; SWAPPED IN)
;CALL:	MOVE J,FIT		;JOB (OR HIGH SEG NO) WHICH CANNOT FIT
;	SETZM FIT		;PRETEND WE WEREN'T TRYING TO FIT THE JOB
				; IN WITH THE DANGUNG HIGH SEGMENT
;	PUSHJ P,NOFITH
;	  SWAP OUT THE FITJOB, J=JOB NO. IF HI-SEG ALREADY ON DISK, =HI SEG NO IF NEEDS SWAPOUT
;	NOT A JOB, OR NO HIGH SEG, OR HI-SEG IN CORE COUNT NOT UP FOR
;	THIS JOB (2ND RETURN IS USUAL CASE)


NOFITH::MOVSI	T1,CORCNT	;HI-SEG IN CORE COUNT UP FOR THIS JOB
	CAIG	J,JOBMAX##	;IS THIS A LOW SEG BEING FITTED?
	TDNN	T1,JBTSGN##(J)	;YES, IS IN CORE COUNT UP FOR THIS JOB
	JRST	CPOPJ1##	;NO, GIVE USUAL NO ACTION RETURN
				;YES, FALL INTO FORH1
IFN FTLOCK,<
	MOVSI	T1,NSWP		;IF THE HIGH SEGMENT IS LOCKED,
	MOVE	T2,JBTSGN##(J)	;BY DEFINITION ITS NOT DANGLING.
	TDNE	T1,JBTSTS##(T2)	;IS IT LOCKED?
	JRST	CPOPJ1##	;YES, GIVE NO ACTION RETURN
>
FORH1:	MOVEM	J,SWPOUT##	;REMEMBER THIS JOB NUMBER DURING HIGH SEG SWAPOUT
	MOVE	T1,J		;SET UP JOB NUMBER FOR DECCNT ROUTINE
	MOVE	J,JBTSGN##(J)	;GET HIGH SEG NUMBER FOR THIS JOB
	SKIPE	R,JBTADR##(J)	;IS HIGH SEG STILL IN CORE?
				; MAY NOT BE IF JOB JUST CONNECTED TO HIGH SEG
				; BY GET(XPANDH) OR HIGH SEG SWAPPED OUT PREVIOUSLY
				;SETUP R IN CASE KCOREH CALLED
	PUSHJ	P,DECCNT	;YES, DECREMENT IN CORE USE COUNT
	  JRST	RETJOB		;STILL GREATER THAN 0(OR NOT IN CORE), SWAP OUT LOW SEG
     

IFN FTLOCK,<
	PUSHJ	P,LOKHGH##	;SEE IF THE HIGH SEGMENT IS LOCKED OR IS
				;  BEING LOCKED
	  JRST	FORH1A		;YES, DON'T DELETE HIGH SEGMENT SINCE ITS LOCKED OR
				;THE LOW SEGMENT WILL BE SWAPPED IN AS SOON AS
				;THE HIGH SEGMENT IS IN PLACE.  INSTEAD JUST
				; COUNT IN CORE COUNT BACK UP FOR
				; THIS JOB AND SWAP OUT
				; ONLY THE LOW SEG.
>

	MOVE	T1,FIT##	;IS JOB BEING FITTED IN(0 IF NOT FITTING IN)
	HRRZ	T2,JBTSGN##(T1)	;ABOUT TO USE THIS SAME HIGH SEG?
	CAIG	T1,JOBMAX##	;IS SEGMENT BEING FITTED A LOW SEG(JOB #)
				; (USUALLY SINCE SWAPPER DOES NOT SWAP IN UNTIL
				; ROOM FOR BOTH HIGH AND LOW SEGS
	CAIE	T2,(J)		;YES, FIT JOB HIGH SEG SAME?
	JRST	FORH2		;NO, EITHER FIT HIGH SEG(RARE) OR NOT SAME
	HLL	J,JBTSGN##(T1)	;YES, SETUP PROPER LH FOR JOB BEING FITTED IN.
				; IN PARTICULAR PRESERVE UWPOFF IN CASE JOB
				;BEING SWAPPED OUT HAS WRITE LOCK OFF
				;AND JOB BEING SWAPPED IN HAS IT ON.
FORH1A:	PUSHJ	P,INCCNT	;YES,INCREMENT HIGH SET IN CORE COUNT FOR JOB
				; ABOUT TO BE FITTED IN CORE EVEN THOUGH ITS LOW
				; SEG HAS YET TO BE SWAPPED IN. (COUNT WILL ONLY
				; BE INCREMENTED ONCE SINCE CORCNT BIT WILL BE SET)
				; HIGH SEG WILL NOT APPEAR IDLE IN CORE
				; DURING OTHER SWAPOUTS AND FIT SWAP IN
				; LEAST FRECOR GRAB IT
	JRST	RETJOB		;RETURN AND SWAP JUST LOW SEG OUT (LEAVING
				; HIGH SEG IN CORE FOR FIT USER)

;HERE TO SWAP OUT HIGH SEG UNLESS ALREADY ON DISK
FORH2:
IFN FTVM,<
	LDB	T1,IMGIN##	;INPUT SIZE
	JUMPE	T1,RETJOB	;RETURN JOB IF HI-SEG CORE IS INCLUDED
				; IN THE LOW SEG.
>
	MOVE	T1,SWPOUT##	;JOB NUMBER OF JOB WHICH HAS HIGH SEG
				; WHICH MAY BE WRITTEN
IFN FTVM,<
	MOVSI	T2,JXPN		;SEGMENT EXPANDING BIT
	TDNE	T2,JBTSTS##(J)	;IS THE SEGMENT EXPANDING?
	PJRST	INCCNT		;YES, INCREMENT IN-CORE COUNT AND
				; SWAP OUT THE HIGH SEGMENT SINCE
				; IT HAS NO DISK COPY (UCORHI
				; CALLED ZERSWP)
>
	LDB	T2,IMGOUT##	;IS A COPY OF SEG ALREADY ON DISK?
	PJUMPE	T2,INCCNT	;IF NOT(0), RETURN AND WRITE HIGH SEG
				; AFTER INCREMENTING IN-CORE COUNT TO 1
				; SO IT WILL NOT APPEAR IDLE DURING SWAP OUT
	PUSHJ	P,KCOREH	;ALREADY ON DISK, JUST RETURN CORE
				; AND FLAG HIGH SEG AS ON DISK(SET SWP=1)
RETJOB:
IFN FTHALT,<
	PUSHJ	P,CHKTAL	;CHECK CORTAL WITH CORTAB, HALT IF DIFFER
>
	MOVE	J,SWPOUT##	;RETURN JOB NUMBER(LOW SEG NUMBER)
	POPJ	P,		;AND TRY TO SWAP IT OUT
>
     
;SUBROUTINE TO EXPAND A HIGH (SHARABLE) SEGMENT AT UUO LEVEL
;CALL:	MOVE J, JOB OR HIGH SEG NO. EXPANDING
;	PUSHJ P,XPNHGH
;	ALWAYS RETURN WITH J=JOB NUMBER
;CALLED FROM XPANDH IN SCHED1.
IFN FTSWAP,<


XPNHGH::CAIG	J,JOBMAX##	;IS THIS A LOW SEG EXPANDING?
	POPJ	P,		;YES, RETURN IMMEDIATELY (MAYBE FROM
				; RECURSIVE CALL BELOW)
	TLNN	J,SHRSEG	;NO, IS THIS HIGH SEG NON-SHARABLE?
	JRST	XPNRET		;YES, JUST RETURN CURRENT JOB NO.
	PUSHJ	P,SAVE1##	;NO, SAVE P1 ON STACK
	MOVSI	T1,JXPN		;FLAG THIS SHARABLE HIGH SEG AS EXPANDING
	IORM	T1,JBTSTS##(J)	;SO IF OTHER JOBS GETSEG, THEY WILL BE SWAPPED OUT
	HRRZ	P1,J		;SAVE EXPANDING SHARABLE HIGH SEG NO.

;LOOP TO SCAN ALL JOBS FOR ONES USING THIS SHARABLE HIGH SEG AND FLAG JOBS AS EXPANDING IF IN CORE
	MOVE	J,HIGHJB##	;SCAN ALL JOBS INITIALIZED
XPNLP1:	SKIPLE	JBTSGN##(J)	;A REAL HIGH SEGMENT?
	CAIE	P1,@JBTSGN##(J)	;DOES THIS JOB HAVE THE SAME HIGH SEG?
	SOJG	J,XPNLP1	;NO, FINISHED (NULL JOB=0 NEVER HAS HIGH SEG)
	JUMPLE	J,XPNRET	;YES, FINISHED?
	MOVSI	T1,SWP		;IS THIS JOB SWAPPED OR LOCKED IN CORE?
	TDNN	T1,JBTSTS##(J)	;
	PUSHJ	P,XPANDH##	;NO, EXPAND THIS JOB BY SWAPPING OUT (UNLESS LOCKED)
	SOJG	J,XPNLP1	;FINISHED?
XPNRET:	MOVE	J,JOB##		;YES, ALWAYS RETURN CURRENT JOB NO.
				; AS LOW SEG NO. ASSOCIATED WITH EXPANDING
				; HIGH SEG NO.
	POPJ	P,		;RETURN (RESTORE P1)

;ROUTINE TO CHECK FOR A HIGH SEG WITH SWAP READ ERROR
;CALLED FROM ZERSWP WHENEVER PHYSICAL DISK SPACE IS BEING RECLAIMED
;CALL:	MOVE J,LOW OR HIGH SEG NO.
;	PUSHJ P,ZERSWH
;	  LOW SEG OR NO ERROR IN HIGH SEG
;	ERROR IN HIGH SEG (ERROR BIT IS THEN CLEARED)


ZERSWH::MOVE	T2,JBTSTS##(J)	;SEGMENT STATUS WORD
	CAILE	J,JOBMAX##	;IS THIS A HIGH SEG?
	TLNN	T2,SERR		;YES, DOES IT HAVE A SWAP READ ERROR?
	POPJ	P,		;NO, RETURN AND RECLAIM PHYSICAL DISK SPACE
	MOVSI	T2,SERR		;YES, SWAP READ ERROR FLAG
	ANDCAM	T2,JBTSTS##(J)	;CLEAR IT IN HIGH SEG STATUS WORD
				; SO BE READY TO COUNT A NEW ERROR
				; IF IT OCCURS IN A NEW PLACE
	JRST	CPOPJ1##	;SKIP RETURN AND DO NOT RECLAIM DISK SPACE
				; SO NO ONE WILL BE ABLE TO USE IT
>
     

;SUBROUTINE TO DELETE ENOUGH DORMANT SEGS ON THIS UNIT TO MAKE ROOM
;ARGS	P2=NUMBER 1K BLOCKS NEEDED
;	U=ADDR OF UNIT DATA BLOCK
;PRESERVES P2,U,J
;CLOBBERS P1,P3,P4 ***NOTE: VIOLATION OF SUB CALL CONVENTIONS (KCORE1)
;SKIP RETURN IF OK, NON-SKIP IF NOT

IFN	FTSWAP,<


DODELE::PUSH	P,J
	PUSH	P,P2
	MOVE	J,SEGPTR##
DODEL1:	LDB	T1,IMGOUT##	;GET SPACE ON DISK OF THIS SEG
	SKIPL	T2,JBTSTS##(J)	;SEG UNUSED? (SNA=0)
	CAIN	T1,0		;YES, SPACE ON DISK?
	JRST	DODEL3		;NO, KEEP LOOKING
IFN FTVM,<
	TLNE	T2,NSHF		;IS THIS SEGMENT CURRENTLY BEING SWAPPED OUT
	JRST	DODEL3		;YES, KEEP LOOKING
>
IFN FT2SWP,<
	SKIPL	JBTSWP##(J)	;SEE IF THIS SEG HAS SPACE ON OUR UNIT
	JRST	DODELA		;NOT FRAGMENTED
	HLRZ	T1,JBTSWP##(J)	;GET ADDR OF LIST OF PIECES
	TRZ	T1,FRGSEG	;CLEAR FRAGMENTED BIT
DODELB:	HLRE	T2,(T1)		;GET K OF THIS PIECE
	JUMPLE	T2,DODELC	;NOT CORE, POINTER TO NEW LIST
	LDB	T2,[POINT 3,(T1),22]	;GET INDEX OF UNIT IN SWPTAB
	CAMN	U,SWPTAB##(T2)	;SEE IF ITS US
	JRST	DODEL2		;YES, WE DO HAVE SPACE HERE!
	AOJA	T1,DODELB	;NO, TRY NEXT PIECE
DODELC:	SKIPE	T1,(T1)		;SEE IF END OF LIST
	JRST	DODELB		;NO, KEEP GOING
	JRST	DODEL3		;YES, WE CANT HELP
DODELA:	LDB	T1,JBYSUN##	;NOT FRAGMENTED, GET UNIT SEG IS ON
	CAME	U,SWPTAB##(T1)	;SKIP IF ON THIS UNIT
	JRST	DODEL3		;NO
>	;END CONDITIONAL ON FT2SWP
DODEL2:	HLLM	J,-2(P)		;SAVE LEFT HALF OF J ON P=PART OF AOBJN PTR
	TLZ	J,-1		;CLEAR LH OF J SO J=SEGMENT NUMBER
	PUSHJ	P,CLRNAM	;DELETE THE DORMANT SEGMENT
	MOVE	P2,(P)		;P2=NUMBER OF 1K BLOCKS NEEDED
	CAMG	P2,UNIFKS##(U)	;SKIP IF STILL NOT ENOUGH SPACE
	JRST	DODELX		;DONE, EXIT
	HLL	J,-2(P)		;RESTORE LEFT HALF OF AOBJN PTR
DODEL3:	AOBJN	J,DODEL1	;JUMP IF MORE SEGS
	SOS	-2(P)		;OFFSET SKIP RETURN, GIVE ERROR RETURN, NOT ENOUGH ROOM
DODELX:	POP	P,P2		;POP OFF NUMBER 1K BLOCKS NEEDED
	JRST	IPOPJ1##
>
     
;SUBROUTINE TO SEE IF BY DELETING DORMANT SEGS WILL BE ENOUGH ROOM
; ON THIS UNIT
;ARGS	P2=NUMBER 1K BLOCKS NEEDED
;	U=ADDR OF UNIT DATA BLOCK
;CALL	MOVEI	P2,1K BLOCKS NEEDED
;	MOVEI	U,ADDR
;	PUSHJ	P,IFDELE
;	ERROR RETURN - CANT CREATE ENOUGH SPACE ON THIS UNIT
;	OK RETURN - CAN CREATE ENOUGH SPACE BY DELETING SEGS
;PRESERVES P1,P2,P3,P4,U,J

IFN	FTSWAP,<
IFN FT2SWP,<
IFDELE::PUSH	P,J
	MOVE	T2,UNIFKS##(U)	;T2 IS COUNTER OF AVAILABLE SPACE
	MOVE	J,SEGPTR##
IFDEL1:	LDB	T1,IMGOUT##	;GET DISK SPACE OF THIS SEG
	SKIPL	JBTSTS##(J)	;SEG UNUSED? (SNA=0)
	CAIN	T1,0		;YES, SPACE ON DISK?
	JRST	IFDEL3		;NO, KEEP LOOKING
	SKIPL	JBTSWP##(J)	;FRAGMENTED?
	JRST	IFDEL2		;NO
	PUSH	P,P1		;YES, SAVE P1
	HLRZ	T1,JBTSWP##(J)
	TRZ	T1,FRGSEG
IFDFK:	HRRZ	P1,T1		;P1=ADDR OF LIST
IFDFK1:	HRRZ	T3,(P1)		;DISK ADDR
	HLRE	T1,(P1)		;NUMBER OF J
	JUMPLE	T1,IFDFK2	;JUMP IF NOT DISK SPACE
	LDB	T3,[POINT 3,T3,22]	;GET UNIT INDEX
	CAMN	U,SWPTAB##(T3)	;SEE IF ITS THE RIGHT UNIT
	ADD	T2,T1		;YES
	AOJA	P1,IFDFK1
IFDFK2:	SKIPE	T1,(P1)		;SKIP IF END OF LIST
	JRST	IFDFK
	POP	P,P1		;RESTORE P1
	JRST	IFDEL4
IFDEL2:	LDB	T3,JBYSUN##	;GET SWAPPING UNIT NUMBER
	CAMN	U,SWPTAB##(T3)
	ADD	T2,T1		;IF RIGHT UNIT, COUNT ITS SPACE
IFDEL4:	CAML	T2,P2		;HAVE ENOUGH YET?
	JRST	IPOPJ1##	;YEAH, MAN
IFDEL3:	AOBJN	J,IFDEL1	;NO, KEEP ON
	JRST	IPOPJ##		;CANT DO IT
>		;END CONDITONAL ON FT2SWP
>		;END CONDITIONAL ON FTSWAP
     
SUBTTL	UUOCON - UUO HANDLING ROUTINES

;ROUTINE TO CHECK TO SEE IF A KNOWN SEGMENT IS BEING SUPERCEDED BY A FILE
;OF THE SAME NAME WITH EXTENSION OF .SHR IN THE SAME DIRECTORY.
;IF YES, THE SHARABLE SEGMENT IF FLAGGED AS NO LONGER SHARABLE FOR
;ADDITIONAL USERS(ITS NAME IS SET TO 0 IN JBTNAM).  THE USERS
;WHO ARE USING IT WILL CONTINUE TO SHARE IT UNTIL ALL OF THEM ARE THROUGH.
;IF NO USERS ARE USING SEGMENT ON DISK OR CORE, ITS DISK SPACE WILL BE RETURNED.
;CALLED FROM CLOSE INPUT AND OUTPUT DUMP AND BUFFERED(RELSEG) AND SUCCESSFUL RENAME(RELSG1)
;CALL:	MOVE F,ADR. OF DEVICE DATA BLOCK
;	PUSHJ P,RELSEG
;	ALWAYS RETURN (RELSEG)
;	ALWAYS SKIP RETURN (RELSG1)


RELSEG::TLNE	F,ENTRB		;HAS AN ENTER BEEN DONE(HERE ON CLOSE)?
				; IS THIS OUTPUT CLOSE?
				; I.E. WAS A FILE CREATED OR RECREATED?
 	PUSHJ	P,FNDSEG	;YES, DOES A KNOWN SEG ALREADY HAVE THIS NAME?
				; SEARCH USING OLD (RATHER THAN NEW) IF RENAME
	  POPJ	P,		;NO, NO CONFLICT
	JRST	RNMSG1		;YES, CLEAR NAME AND DELETE CORE AND DISK IF
				; BUT LEAVE DIRECTORY NAME FOR YSTAT
				; SO IT CAN PRINT (OBS) P,P
				; HIGH SEGMENT IS ALREADY DORMANT


;ROUTINE TO RESET HIGH SEG
;CALLED ON RESET UUO
;SETS USER:MODE WRITE PROTECT BIT ON IN HARDWARE AND SOFTWARE FOR THIS
;JOB ONLY(CURRENT JOB). OTHER JOBS MAY HAVE UWP ON OR OFF FOR
;SAME HIGH SEGMENT(EXCEPT THAT INITIALLY SETUWP UUO DOES NOT ALLOW
;TURNING UWP OFF IF HIGH SEG SHARABLE)
;CALL:	MOVE J,CURRENT JOB NUMBER
;	PUSHJ P,HRESET
;	RETURN WITH APR RESET AND SOFTWARE RESET


HRESET::MOVSI	T1,UWPOFF	;FLAG THIS USER AS HAVING UWP ON FOR HIGH SEG
	ANDCAM	T1,JBTSGN##(J)	;SO HE WILL NOT BE ABLE TO WRITE HIGH SEG
	JRST	SETMAP##		;GO DO DATAO APR AND SET UWP ON
     
;UUO ROUTINE TO SET OR CLEAR USER-MODE WRITE PROTECT(UWP)
;ON HIGH SEGMENT FOR CURRENT JOB
;CALL:	MOVEI AC,0 OR 1(CLEAR OR SET UWP)
;	MOVE J,CURRENT JOB NUMBER
;	PUSHJ P,USTUWP
;	ERROR RETURN - MACHINE OR MONITOR NOT HAVE 2 REG. CAPABILITY OR
;		TRYING TO TURN OFF UWP AFTER R HAS BEEN MEDDLED WITH
;		  AND USER DOES NOT HAVE WRITE PRIV. TO FILE WHICH INIT HIGH SEG
;		OR HIGH SEG IS A SPYSEG(IE PHYSICAL CORE)
;	OK RETURN - WHETHER JOB HAS HIGH SEG OR NOT
;		PREVIOUS SETTING OF UWP RETURNED IN AC(BOTH RETURNS)


USTUWP::SKIPN	TWOREG##	;IS THIS A TWO REG MACHINE?
	JRST	RTZER##		;NO, ERROR RETURN TO USER, WITH 0 IN HIS AC
	PUSHJ	P,SAVE2##	;SAVE ACS P1-P2
	AOS	(P)		;ASSUME OK RETURN
	SKIPN	J,JBTSGN##(J)	;DOES JOB HAVE A REAL OR SPY HIGH SEG?
	JRST	USTU0		;NO, GIVE OK RETURN, BUT RETURN 0 AS PREVIOUS
				; UWP SETTING
	JUMPN	T1,UWPON	;YES, IS USER TRYING TO TURN UWP ON?
IFN FTPEEKSPY,<			;PEEK AND SPY
	JUMPL	J,USTUE		;NO, IS THIS A SPY SEG? (IF YES, GIVE ERROR RETURN)
>
	PUSHJ	P,CHKHWC	;NO, HAS CURRENT JOB AVOIDED MEDDLING OR
				;DOES CURRENT JOB HAVE WRITE PRIVILEGES TO FILE
				; EVEN THOUGH MEDDLING WITH PROGRAM?
				; MAYBE SHARED DATA SEGMENT WITH GETSEG UUO
USTUE:	  SOSA	(P)		;NO, THEN DO NOT ALLOW PROGRAM TO TURN OFF UWP
USTU1:	TLOA	J,UWPOFF	;YES, SO OK TO TURN OFF UWP, EVEN IF THIS
				; IS SHARABLE HIGH SEG
UWPON:	TLZ	J,UWPOFF	;TURN OFF UWPOFF SO UWP WILL BE ON
IFN FTSWAP,<
	TLNE	J,UWPOFF	;WILL HIGH SEG BE WRITEABLE?
	PUSHJ	P,ZERSWP##	;YES, RETURN SWAPPING SPACE SO HIGH SEG
				; WILL HAVE TO BE WRITTEN WHEN JOB
				; IS SWAPPED OUT
>
	MOVE	T1,JOB##	;CURRENT JOB NUMBER
IFN FTHALT,<
	PUSHJ	P,COMIT		;COMPARE J WITH JBTSGN(T1), HALT IF DIFF.
>
	EXCH	J,JBTSGN##(T1)	;MARK NEW UWP SETTING FOR THIS USER AND GET OLD ONE
	MOVEI	T1,1		;ASSUME UWP WAS ON FOR THIS USER
	TLNE	J,UWPOFF	;WAS UWP OFF?
USTU0:	MOVEI	T1,0		;YES, RETURN 0
	PUSHJ	P,STOTAC##	;TO USER'S AC ARG
IFN FTKL10&FTMS,<
	PUSHJ	P,CLRCSH##	;CAUSE CACHE BITS TO BE CLEARED IN THE MAPS
				; OF ALL JOBS SHARING THIS HIGH SEGMENT SO
				; WRITTEN DATA WILL BE SEEN BY ALL
>
	JRST	SETMAP##	;GO RESET HARDWARE AND SOFTWARE RELOC.
				; AND RETURN TO USER
     
;ROUTINE TO CHECK ACCESS PRIVILEGES FOR SHARED SEGMENTS
;CALL:	MOVE J,HIGH SEG NUMBER
;	MOVEI P1,FUNCTION (FNCEXC,FNCRED,FNCUPD)
;	MOVE P2,JOB NUMBER
;	PUSHJ P,CHKHSA
;	CAN'T READ OR WRITE
;	USER HAS PRIV IF SHARABLE, OR HIGH SEG NOT SHARABLE
;CHKHSW IS WRITE ENTRY POINT, CHKHWC IS WRITE FOR CURRENT JOB
;DTA READ OK IF DEVICE UNASSIGNED,SYSTEM, OR ASSIGNED TO THIS JOB
;DTA WRITE OK IF DEVICE UNASSIGNED OR ASSIGNED TO THAT JOB, EXCEPT CAN'T
;DEPOSIT SYSTEM TAPE
;CALLED FROM SAVGET TO SEE IF USER HAS READ PRIVILEGES TO SHARE
;CALLED FROM DEPOSIT COMMAND IF IN HIGH SEG TO SEE FI WRITE PROV
;CALLED FROM SETUWP IF TRY TO SET UWP TO 0 AND MEDDLE BIT IS ON TO
; SEE IF WRITE PRIV. OK

IFN FTDISK,<>

;ROUTINE TO CHECK WRITE PRIV FOR CURRENT JOB IF MEDDLING
				;GIVE OK RETURN IF CURRENT JOB NOT MEDDLING
CHKHWC::TLNN	J,MEDDLE	;HAS CURRENT USER BEEN MEDDLING WITH ITS HIGH SEG?
				; OR IS THIS A SPY SEG?
	PJRST	CPOPJ1##	;NO, GIVE OK TO UPDATE THIS HIGH SEG RETURN
	MOVE	P2,JOB##	;YES, CHECK UPDATE PRIVILEGES FOR CURRENT JOB
;ROUTINE TO CHECK WRITE PRIV
CHKHSW:	PUSHJ	P,SAVE1##	;SAVE P1
	MOVEI	P1,PR.WRT##	;CHECK FOR WRITE PROTECTION
CHKHSA:	TLNN	J,SHRSEG	;IS THIS HIGH SEG SHARABLE?
	JRST	CPOPJ1##	;NO, ALWAYS OK TO READ OR WRITE
IFN FTDISK,<
	SKIPN	T2,JBTPPN##(J)	;YES, IS HIGH SEG FROM DTA OR MTA
				; DIRECTORY NAME IS 0 ON DTA,MTA
	JRST	CHKHS3		;YES, GO SEE IF DEVICE IS AVAILABLE
	MOVE	T3,JBTPPN##(P2)	;SET REQUESTORS DIRECTORY PPN
	MOVE	T1,P1		;SETUP HIGH SEG ACCESS PRIV
	LDB	T4,JBYSPR##	;HIGH SEG ACCESS PRIV
	PUSH	P,J		;SAVE HIGH SEG NUMBER
	MOVE	J,P2		;SETUP JOB NUMBER
	PUSHJ	P,CHKACC##	;GO CHECK ACCESS PRIV (IN DISK SERVICE)
	  PJRST	JPOPJ##		;ERROR - CANNOT ACCESS,RESTORE HIGH SEG NO
	PJRST	JPOPJ1##	;OK TO ACCESS, RESTORE HIGH SEG NO.
>

CHKHS3:	MOVE	T1,JBTDEV##(J)	;DEVICE NAME OF DTA OR MTA
	PUSHJ	P,DEVPHY##	;YES, SEARCH FOR PHYSICAL DEVICE
	  POPJ	P,		;NO, NOT FOUND
IFE FTDISK,<	CAME	T1,SYSTAP##
>
	TLNE	F,SYSDEV	;SYSTEM TAPE?
	JRST	CHKHS2		;NO
	LDB	P3,PJOBN##	;JOB DEVICE ASSIGNED TO, IF ANY
	JUMPE	P3,CPOPJ1##	;IS NON-SYSTEM DEVICE ASSIGNED?
				; ALWAYS READ OR WRITE IF UNASSIGNED
	CAMN	P3,P2		;YES, IT IT ASSIGNED TO THIS JOB?
	AOS	(P)		;YES, CAN READ OR WRITE
	POPJ	P,		;NO, CANNOT READ OR WRITE

CHKHS2:	CAIGE	P1,PR.WRT##	;YES, TRYING TO WRITE(UPDATE)CLEAR UWP OR DEPOSIT)
	AOS	(P)		;NO, OK TO READ SYSTEM TAPE
	POPJ	P,		;YES, CAN'T WRITE SYSTEM TAPE

     
;UUO TO ALLOW PRIVILEGED USER PROGRAM TO LOOK AT ALL OF CORE
; BY HAVING PHYSICAL CORE APPEAR AS HIGH SEG
; ANY PROGRAMS WRITTEN TO USE THIS UO SHOULD BE PROGRAMMED TO USE THE
; PEEK UUO TOO, IN CASE THIS MACHINE OR MONITOR DOES NOT HAVE 2 RELOC. REG.
; OR INSTALLATION DECIDES TO MAKE THIS UUO RESTRICTED
; THIS UUO IS PROVIDED ONLY FOR EFFICIENT, SINCE IT NEED ONLY BE DONE
; ONCE, WHILE PEEK UUO MUST BE DONE FOR EVERY WORD EXAMINED
;CALL:	HRRI AC,HIGHEST PYHSICAL ADR. TOAPPEAR IN HIGH SEG
;	CALL AC,[SIXBIT /SPY/] OR CALLI AC,42
;	ERROR RETURN, ADR. TOO IGH OR THIS USER CAN'T USE
;	 THIS UUO, OR NOT 2 RELOC. REG. MACHINE
;	OK RETURN, HIGH SEG SETUP IN ADDRESSING SPACE
;NOTE: A SPYSEG APPEARS TO THE SYSTEM AS IF TIS USER DOES NOT HAVE A HIGH SEG
; IN ALL CASES EXCEPT WHEN THE RELOC. HARDWARE IS SET. THIS IS BECAUSE
; THE SIGN BIT OF JBTSGN(JOBNUMBER) IS SET TO INDICATE SPY SEG
; AND THE MONITOR CHECK FOR REAL HIGH SEGS SITH SKIPG INSTR.
; THE RH OF JBTSGN(JOBNUMBER) HAS HIGHEST LEGAL PHY. ADR.
; FOR THIS USER TO LOOK AT(CARE IS TAKEN SINCE THIS IS MUCH BIGGER THAN A
; HIGH SEG NUMBER WHICH IS USUALLY IN RH)

IFN FTPEEKSPY,<
USPY::	SKIPN	TWOREG##		;DOES MACHINE HAVE 2 REG. HARDWARE?
	POPJ	P,		;NO, ERROR RETURN TO USER
IFN FTPRV,<	MOVSI	T1,PVSPYM!PVSPYA
	PUSHJ	P,PRVBIT##	;IS USER PRIVILEGED TO SPY?
	  SKIPA			;YES
	POPJ	P,0		;NO, ERROR RETURN
>
	PUSHJ	P,KILHGC	;YES, REMOVE PREVIOUS HIGH SEG FROM LOGICAL
				;ADR. SPACE, IF ANY INCLUDING PREVIOUS SPY SEG
	PUSHJ	P,GETWDU##	;GET USER'S COR ARG ON UUO
	JUMPE	T1,SETRL1	;DOES HE WANT TO SPY AT ALL?
				; IF 0, SET RELOC HARDWARE AND SKIP RETURN
	TLZN	T1,-1		;YES, ANY LH BITS (CLEAR FOR COMPARE)?
	CAML	T1,MEMSIZ##	;NO, IS HIGHEST ADR. USER WANTS ABOVE MONITOR?
	PJRST	SETMAP##	;YES, ERROR RETURN TO USER (AFTER SETTING HARDWARE)
IFN FTPRV, <
	PUSH	P,T1		;SAVE USER'S ARGUMENT
	CAMGE	T1,SYSSIZ##	;TRYING TO SPY BEYOND END OF MONITOR ?
	JRST	USPY1		;NO, O.K.
	MOVSI	T1,PVSPYA	;YES, IS HE PRIVILEGED TO DO SO ?
	PUSHJ	P,PRVBIT##	;
	  JRST	USPY1		;YES, O.K.
	POP	P,T1		;NO, ERROR RETURN
	PJRST	SETMAP##

USPY1:
	POP	P,T1		;RESTORE ARGUMENT
>
	MOVE	T3,T1
	TRO	T3,PG.BDY##	;INSURE AT LEAST ONE PAGE
				; (ALSO MAKE SURE RH CAN NOT LOOK LIKE A HIGH SEG NO.)
	PUSHJ	P,HSVAD		;CALCULATE ORIGIN OF SPY SEG IN USER'S ADR SPACE
	MOVE	T1,T2		;T1 = STARTING VIRTUAL ADDRESS
	ADD	T1,T3		;T1 = TOP VIRTUAL ADDRESS
	TLNE	T1,-1		;TOP VIRTUAL ADDRESS 256K
	PJRST	SETMAP##	;YES, GIVE ERROR RETURN
IFN FTVM,<
	PUSHJ	P,CKNZW##	;WILL THE SPY SEG OVERLAP THE LOW SEGMENT?
	  PJRST	SETMAP##	;YES, GIVE ERROR RETURN
>

	HRLI	T3,SPYSEG!MEDDLE;FLAG THIS USER AS SPYING
	MOVEM	T3,JBTSGN##(J)	;STORE HIGHEST PHYSICAL ADR. USER WANTS
	JRST	SETRL1		;OK RETURN TO USER AFTER SETTING HARDWARE
>				;END CONDITIONAL ON FTPEEKSPY
IFE FTPEEKSPY,<
USPY==:CPOPJ##
>
;ROUTINE TO SCAN KNOWN SEG TABLE(JBTPPN/JBTNAM) TO SEE IF A SEG IS ALREADY
;IN IT AFTER FILE HAS BEEN WRITTEN OR RENAMED
;CALL:	MOVE F,DEVICE DATA BLOCK ADDRESS OF DEVICE WITH FILE
;	PUSHJ P,FNDSEG
;	NOT FOUND RETURN, OR FILE EXT WAS NOT SHR, OR DEVICE HAS SHORT DISP TABLE
			;C(J)=GARBAGE
;	FOUND RETURN, C(J)=HIGH SEG NUMBER(LH=0)
;		C(T1)=DIRECTORY NAME(PROJPROG NO IF DISK, 0 IF MTA OR DTA
;		C(T2)=FILE NAME
;		C(U)=DEVICE OR FILE STRUCTURE NAME
;CALLED FROMRELSEG(CLOSE OUTPUT AND SUCCESSFUL RENAME)
;AND GET AFTER INPUT
;MUST BE CALLED AT UUO LEVEL

IFN FTDISK,<>

FNDSEG:
IFN FTDISK,<	PUSHJ	P,NAMSTR##	;FIND FILE STRUCTURE NAME(T1)
	  SKIPA U,DEVNAM(F);DEVICE NOT A DISK, USP PHYSICAL NAME
	MOVE	U,T1		;SETUP FILE STRUCTURE NAME
>
IFE FTDISK,<
	MOVE	U,DEVNAM(F)	;SETUP PHYSICAL DEVICE NAME(DTA,MTA)
>
	MOVE	T2,DEVMOD(F)	;DEVICE CHARACTERISTICS
	HLRZ	T1,DEVEXT(F)	;FILE EXTENSION
	TLNE	T2,DVLNG	;IS DEVICE HAVE FILES(MTA,DTA,DSK)?
	CAIE	T1,(SIXBIT /SHR/)	;YES, IS EXTENSION SHR?
IFN FTEXE,<
	CAIN	T1,(SIXBIT /EXE/)	;ALLOW EXE FILES AS SHARABLE
	SKIPA			;SKIP OVER POPJ IF EXE
>;END OF IFN FTEXE
	POPJ	P,		;NO, DO NOT LOOK FOR NAME IN KNOWN TABLE
IFN FTDISK,<
	TLNE	T2,DVDSK	;IS THIS DEVICE A DISK?
	SKIPA	T1,DEVPPN(F)	;YES, GET PROJECT-PROGRAMMER NUMBER
>
	MOVEI	T1,0		;NO, SET DIR TO 0, MUST BE DTA OR MTA
	MOVE	T2,DEVFIL(F)	;GET FILE NAME FROM DEVICE DATA BLOCK
				; OLD (RATHER THAN NEW) IF RENAME
				;FALL INTO SECSEG
     


;ROUTINE SAME AS FNDSEG, EXCEPT DEVICE NOT LOOKED UP(CALLED FROM GET WHEN DEVICE IS DTA)
;USED TO AVOID DOING OPEN AND LOOKUP WHEN DEVICE IS DTA
;SINCE OPEN MAY QUEUE FOR SYS AND LOOKUP SPINS TAPE
;CALL:	MOVE T1,PROJ,R NO IF DISK, ZERO IF DTA OR MTA
;	MOVE T2,FILE NAME
;	MOVE U,DEVICE NAME OR FILE STRUCTURE NAME
;	PUSHJ P,SRCSEG
;	NOT FOUND RETURN
;	FOUND RETURN - J SET UP TO HIGH SEG NUMBER
;		LH OF J SET AS SHARABLE(SHRSEG=1)
;		(MEDDLE=UWPOFF=SPYSEG=0)

SRCSEG:	MOVE	J,SEGPTR##	;POINTER TO JUST HIGH SEGMENTS IN JOB TABLES
FNDLOP:
IFN FTDISK,<CAMN T1,JBTPPN##(J)	;YES, DO DIRECTORY NAMES MATCH?>
	CAME	T2,JBTNAM##(J)	;DO FILE NAMES MATCH?
FNDLP1:	AOBJN	J,FNDLOP	;NO, KEEP LOOKING,FINISHED?
	JUMPGE	J,CPOPJ##	;YES, FINISHED?(IF YES, GIVE NOT FOUND RETURN)
	CAME	U,JBTDEV##(J)	;NO, DO DEVICE(STR) NAMES MATCH?
	JRST	FNDLP1		;NO, GO BACK AND LOOK SOME MORE
	HRLI	J,SHRSEG	;YES, THIS MUST BE A MATCH(FLAG SEG AS SHARABLE)
	JRST	CPOPJ1##	;FOUND RETURN

;ROUTINE TO GET WORD FROM HIGH SEGMENT IN CORE
;CALL:	MOVE J,JOB NO.
;	HRR M,RELATIVE(USER) ADDRESS IN HIGH SEGMENT
;	PUSHJ P,HGHWRD
;	ERROR RETURN, ADR NOT IN HIGH SEG OR NO HIGH SEG
;	OK RETURN, CONTENTS OF LOC RETURNED IN T1
;	ABS. OR USER VIRTUAL ADR. OF LOC RETURNED IN AC T2(USED BY DEPOSIT)

;CAN BE CALLED AT CLOCK OR UUO LEVELS
;CALLED FROM E COMMAND AND CALL,INIT UUOS,SAVE AND GET JOBS


HGHWRD::SKIPG	T2,JBTSGN##(J)	;YES, DOES JOB HAVE A HIGH SEG?
	POPJ	P,		;NO, ERROR RETURN - LESS THAN 400000 OR NO HIGH SEG
				; OR SPYSEG
	PUSHJ	P,HSVAD
	CAIG	T2,(M)
	CAIGE	T1,(M)
	POPJ	P,		;NO, ERROR RETURN
IFN FTKA10,<
	MOVE	T1,JBTSGN##(J)	;YES, GET SEG NUMBER AGAIN
	HRRZ	T1,JBTADR##(T1)	;ABSOLUTE ADR. OF FIRST WORD OF HIGH SEG
				;(NOT RELOCATION)
	SUBM	T1,T2		;FORM RELOCATION, SUBTRACT 400000(USUAL) OR
				;LENGTH OF LOW SEG IF GREATER
	ADDI	T2,(M)		;ADD USER ADDRESS TO RELOCATION OF HIGH SEG
	MOVE	T1,(T2)		;GET CONTENTS OF THAT WORD
	JRST	CPOPJ1##	;OK RETURN
>
IFN FTKI10!FTKL10,<
HGHWR1:	HRRZ	T2,M		;SETUP T2 FOR HGHDEP
	EXCTUX	<MOVE T1,(M)>	;GET CONTENTS OF THAT WORD
	JRST	CPOPJ1##	;OK RETURN
>
     
;ROUTINE TO FLAG SHARABLE SEGMENT AS NO LONGER SHARABLE AND RETURN
;DISK OR CORE IF POSSIBLE, THEN DISPATCH TO SERVICE ROUTINE TO DO RENAME
;CALL:	MOVE	F,ADDRESS OF DEVICE DATA BLOCK
;	PUSHJ	P,RNMSEG
;CALLED FROM RENAME


RNMSEG::PUSHJ	P,FNDSEG	;DOES A KNOWN SEGMENT HAVE OLD NAME?
	JRST	DRN(T4)		;NO SO TRY RENAME
	PUSH	P,J		;YES,SAVE HIGH SEGMENT#
	PUSHJ	P,DRN(T4)	;ATTEMPT A RENAME
	JRST	IPOPJ##		;FAILURE
	POP	P,J
	AOS	(P)		;GIVE SUCCESS RETURN
RNMSG1:	PUSHJ	P,SAVE4##	;SAVE P1-P4
	JRST	CLRNM1		;CLEAR NAME AND DELETE DISK AND CORE IF ANY
				;AND RETURN

SEGEND:	END