Google
 

Trailing-Edge - PDP-10 Archives - bb-bt99s-bb - 10,7/daemon/daemon.mac
There are 17 other files named daemon.mac in the archive. Click here to see a list.
TITLE	DAEMON - PROGRAM TO SERVICE USER REQUESTS V24(1031) - 5-DEC-89
SUBTTL	D BLACK/DAL/RCC/JSL/CER/RLD/SMM/BAH/PMW/WRS/KPY/TJW/ISM/DBD/JLG/GMU/RKB

	SEARCH	JOBDAT,MACTEN,UUOSYM

	SALL		;PRETTY LISTINGS
	.DIRECT	FLBLST	;PRETTIER LISTINGS

VWHO==0			;WHO LAST UPDATED THIS CUSP
VDAEMON==24
VMINOR==0		;MINOR VERSION NUMBER
VEDIT==1031
;********************************************************************
;* The following symbol gives the version of the monitor that this  *
;* version of DAEMON is built for.  It must be changed for each new *
;* version of the monitor                                           *
;********************************************************************

VMON==705		;MONITOR



;            COPYRIGHT (c) DIGITAL EQUIPMENT CORPORATION
;    1971,1978,1979,1980,1981,1982,1985,1986,1987,1988,1989,1990.
;                        ALL RIGHTS RESERVED.
;
;THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND COPIED
;ONLY  IN  ACCORDANCE  WITH  THE  TERMS  OF  SUCH LICENSE AND WITH THE
;INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE OR ANY  OTHER
;COPIES THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
;OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF THE  SOFTWARE  IS  HEREBY
;TRANSFERRED.
;
;THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT  NOTICE
;AND  SHOULD  NOT  BE  CONSTRUED  AS A COMMITMENT BY DIGITAL EQUIPMENT
;CORPORATION.
;
;DIGITAL ASSUMES NO RESPONSIBILITY FOR THE USE OR RELIABILITY  OF  ITS
;SOFTWARE ON EQUIPMENT WHICH IS NOT SUPPLIED BY DIGITAL.
COPYRIGHT (c) DIGITAL EQUIPMENT CORPORATION 1971,1990.
ALL RIGHTS RESERVED.
\; \ ENDS COPYRIGHT-IN-EXE MACRO

;	UPDATE SUMMARY AS OF VERSION 7(52)

;053	PARAMETER NOT SET UP CORRECTLY FOR GETLCH UUO IN ERROR
;	INTERCEPTING ROUTINE AT INTRES+5 (SPR 10-11,097).

;054	NEW ROUTINES INTRODUCED TO PROVIDE ERROR REPORTING FOR
;	MAGTAPES AS PART OF THE 507/601 RELEASE.  NEW ROUTINES ARE
;	COMPLETELY BACKWARD COMPATIBLE AND WILL NOT BE EXERSIZED FOR
;	MONITORS PRIOR TO  50622.

;055	ERROR.SYS UPTIME INCORRECT BY FACTOR OF 4.  ERRINI+14,
;	MAKE ASHC OPERAND DECIMAL 17 RATHER THAN OCTAL.

;056	RANDOM PROTECTION OF FACT FILES.  APPNDF+4, STORE AC A,
;	RATHER THAN 0.

;057	FT1975, SUPPORT FOR NEW FORMAT FACT FILE ENTRIES, IN WHICH
;	THE DATE LASTS PAST 1975.

;060	FIXES TO MAKE EDIT 54 WORK.

;061	FIX TO TAKE CARE OF AOBJN DIFFERENCE FROM KA TO KI. CLOCK
;	REQUEST FOR HIGHEST JOB NUMBER WAS NOT GETTING SERVICED ON KA.

;062	ADD DEFENSIVE CODE TO HANDLE ERRORS ON ERROR.SYS. DAEMON WILL
;	NOW WRITE ERROR.X01 TO ERROR.X77 IF NECESSARY.

;063	GENERATE CORRECT FORMAT DATE/TIME WORD IN FACT ENTRIES

;064	FIX FOR UNDEFINED SYMBOL NXTWAK IF USING CERTAIN NONSTANDARD
;	FEATURE TEST SETTINGS

;065	CLEAR .JBINT ON EXIT

;066	INSURE THAT DUMP DOESN'T GET DONE IF AN ERROR THAT WILL PREVENT
;	IT FROM WORKING IS DETECTED

;067	QPNTR IS STILL SET UP WRONG (ON KA AND KI).RH NEEDS TO BE
;	-(# OF INTERNAL CLOCK REQUESTS -1). (SINCE 0 REPRESENTS
;	A QUEUE IN JBTCLK.
;070	IMPLEMENTS 6.01 SUPPORT

;071	COMPLETES IMPLEMENTATION OF 6.01 SUPPORT AND INCLUDES THE
;	SOURCE FOR SCANER AS PART OF DAEMON.MAC.

;072	MINOR EDIT TO CLEAN UP SOME SMALL DEFICIENCIES LEFT OVER FROM
;	EDIT 71 AND TO PROVIDE A COMMON FATHER FOR SOUPS OF FUTURE
;	DEVELOPMENT AND INTEGRATION OF IPCF STUFF

;073	EDITS PREPARING THE INTEGRATION OF [SYSTEM] INFO
;	ALL [SYSTEM]INFO DEPENDENCIES ARE DETERMINED BY THE
;	FEATURE SWITCH FTIPCF.THIS SWITCH IS DEFINED AS OFF
;	FOR STANDARD RELEASES AND SHOULD NOT BE TURNED ON
;	NOTE: THAT TURNING ON FTIPCF IMPLIES AN IMPURE VERSION.
;	IT MIGHT BE REMARKED THAT A PURE DAEMON IS A RATHER
;	IMPURE IDEA

;074	ADD THE CAPABILITY TO DAEMON TO RESTART ON ILLEGAL UUO'S
;	ILL. MEM.REF ADDRESS CHECK EXTERNAL JOB ERROR,NXM ,PDL
;	OVERFLOW

;075	FIX SOME BUGS AND CHANGE THE FORMAT OF A SAVE FILE SO THAT
;	NON-CONTIGUOUS LOW SEGMENTS CAN BE 'GOTTEN' INTO A CORE IMAGE
;	THAT RESEMBLES THE ORIGINAL.  THIS IS DONE BY OUTPUTTING A NULL
;	IOWD FOR EACH EXISTANT BUT ZERO PAGE.

;076	INSERT A NEW SUBROUTINE 'SCNBRK' TO FORCE TTY SCANNING
;	TO GO TO END OF LINE FOR EACH DAEMON COMMAND.

;077	FIX A BUG INVOLVING USING THE WRONG HALF OF 'SEGSWP' TO STORE
;	A SWAPPING POINTER BEFORE CALL TO REDPEC

;100	A BLIND ATTEMPT TO FIX WHAT MIGHT BE A PROBLEM GETTING UPMP'S
;	OFF THE SWAPPING SPACE.

;101	START DAEMON AT DAEMON +2 WHEN RESTARTING IT,THIS WILL IDENTIFY
;	THAT IT HAS TO DETACH FROM THE CURRENT TERMINAL AND SUPPRESS OTHER OUTPUT
;	NOTE THAT RESTART ALWAYS DEFINED AND ALWAYS IS ZERO WHEN
;	NO IPCF OR RESTART FUNCTIONS ARE WISHED

;102	FIX A SECURITY LEAK IN THE DCORE COMMAND

;103	CHANGE NAME TO DAE AND NO TO DUMMY

;104	INIT DSK:[1,4] INSTEAD OF SYS:[1,4] FOR FACT.SYS UPDATES
;	AS ELSEWISE THE ENABLING OF NEW E.G. WILL LET
;	THE UPDATE MODE ACCESS FAIL

;105	IF ANOTHER DAEMON IS RUNNING THEN JUST EXIT
;	AND LET OPERATOR DECIDE WHAT TO DO

;106	AS 104 ERROR.SYS CANNOT BE UPDATED WITH AMBIGUOUS SYS

;107	A REENTER OF DAEMON FAILED TO EXIT WITH THE TERMINAL ATTACHED
;	THIS WAS DUE TO THE OMISSION OF A JFCL AFTER THE ZAPRUN CALL

;110	EDIT ERROR IN EDIT 102 IN DAEOPN CODE

;110(A)	WHEN A SECOND DAEMON IS RUNNING DAEMON SHOULD CALL
;	DAEREN TO EXIT

;110(B)	GIVE MORE INFORMATION THEN COULD NOT GET AT SWAP POINTER

;EDITS 111-210 RESERVED FOR SPECIAL RELEASES

;VERSION 10(110) RELEASED WITH ALL ABOVE EDITS
;ALL LATER EDITS IN VERSION 11

;211	FIX CODE TO HANDLE NEW SWAPPING POINTERS FOR
;	6.01 CORRECTLY

;212	REMOVE CRLF FROM JOB SAVED MESSAGE TO LOOK LIKE MONITOR

;213	FIX EXAMINE AND DEPOSIT COMMANDS FOR HIGH SEGMENTS

;214	CHECK FOR OUTPUT DEVICE OF NUL AND FINISH WITH NO IO IF TRUE

;215	FIX HIGH SEGMENT SAVE TO ONLY WRITE EXACT
;	NUMBER OF WORD IN LAST BLOCK OF HGH OR SHR FILE

;216	FIX TO EDIT 62 TO MAKE DAEMON WRITE IN LAST ERROR.X?? FILE
;	WHICH EXISTS ON ERRDEV

;217	FIX UNDEFINED GLOBALS ILIST AND OLIST IF FTFACT!FTCHKPNT=0

;220	FIX TO MAKE DAEMON SAVE DO SAME AS MONITOR DEPENDENT ON CONTENTS
;	OF .JBCOR AND .JBHRL.

;221	FIX TO NOT ERROR INTERCEPT ON ALL IO.BKT ERRORS SO WE CAN
;	REPORT THEM ON COMMANDS

;222	FIX 220 AND 215 TO DO THE RIGHT THING TOGETHER

;223	ADD EXTERNAL SYMBOLS SO SYSINF KNOWS START AND END OF DAEMON

;224	FIX TO 220/222 TO NOT SAVE .LOW IF .JBCOR .LT. 140

;EDITS 225-324 RESERVED FOR SPECIAL RELEASES.
;VERSION 11(224) RELEASED WITH ALL ABOVE EDITS. ALL LATER EDITS IN
;VERSION 12.

;325	ADD A FEATURE TEST TO CREATE THE ABILITY TO ASSEMBLE AS TWO
;	SEPARATE PROGRAMS, ONE TO DO ALL COMMANDS AND THE DCORE FUNCTION
;	OF THE UUO, AND ONE TO DO ERROR REPORTING, CHECKPOINTING,
;	ACCOUNTING, OTHER UUO FUNCTIONS, AND SYSTEM INFO.  THE FEATURE
;	TEST IS FTSAVE, EQUAL -1 FOR SAVE ONLY, 0 FOR BOTH, AND +1
;	FOR DAEMON ONLY.  NOTE THAT FTIPCF=-1 OR FTRSTR=-1 WILL NOT
;	WORK WITH FTSAVE=-1.

;326	ADD SUPPORT FOR RH10 DEVICES. THIS IMPLEMENTS THE SPECIAL DRIVE
;	REGISTERS ON MASSBUS DEVICES. THIS EDIT IS ONLY INTENDED FOR
;	RP04'SAND RS04'S ON SYSTEMS WITH THE RP04 LIR. IT WILL HAVE NO
;	EFFECT ON SYSTEMS WITHOUT THE RP04 LIR.


;327	FIX SOME INCONSISTENCIES IN BEHAVIOR ON SAVE

;330	SAVE ADDRESS OF DDB ALONG WITH LENGTH SO USER CAN ASSOCIATE
;	DDB'S WITH CHANNELS (SPR 14451)

;331	FINISH EDIT 325 -- MAKE TYPEOUT SAY THE RIGHT THINGS

;332	FIX BUG IN FINCHK WHICH CAUSED JOB 8 (AC J =8 ) TO BE CHECKED
;	RATHER THAN THE JOB WE WERE INTERESTED IN. THIS BUG CAUSED
;	RANDOM ERROR CODE 3 TO DAEMON UUO'S AND ALSO WRITING IN RANDOM
;	PAGES.

;333	FIX DAEMON TO TAKE ADVANTAGE OF THE GETTABS ADDED IN 507/601
;	FOR ITS BENEFIT.

;334	ADD CODE TO DUMP UNILOG AS LAST ITEM IN CODE 10 ENTRY IN
;	ERROR FILE

;335	MAKE UNKNOWN COMMANDS GIVE ERROR MESSAGE, INSTEAD OF DOING
;	DCORE AS IN PREVIOUS VERSIONS

;336	FIX SWAPPING POINTERS FOR 507. THEY ARE THE SAME ON
;	BOTH 507 AND 601, NOT DIFFERENT AS PREVIOUSLY CODED
;	SEE EDIT 211 FOR SIMILAR CHANGE FOR 601.

;337	FIX BUGS ADDED BY CHANGE TO DAEMON/SAVER(EDIT 325), AND
;	IN USING NEW GETTABS(EDIT 333)
;340	FIX RACE ADDED BY EDIT 325

;341	SPEED UP COMPRS (IGNORE NONEXISTENT PAGES, DON'T LOOK AT EVERY
;	WORD ON THEM)

;EDITS 342-441 RESERVED FOR SPECIAL EDITS

;342	ADD SUPPORT FOR TAPSER MAGTAPE ERROR REPORTING.

;VERSION 12A(342)RELEASED WITH TAPSER LIR.
;VERSION 12(341) RELEASED WITH ALL ABOVE EDITS
;ALL LATER EDITS IN VERSION 13

;442	ADD CODE TO USE ERRPT. UUO INSTEAD OF SCANNING FOR ERRORS

;443	FIX HSVADR TO NOT NEED UPMP IN CORE. ALSO FIX CALLS TO NOT READ
;	UPMP IF NOT NECESSARY FOR OTHER REASONS.

;444	FIX DCORE TO DO CORRECT THING WITH FIRST PAGE OF HIGH SEG

;445	CHANGE ALL SUPER I/O TO USE SUSET.

;446	CHANGE REFERENCES TO SYMBOLS IN UPMP TO USE GETTABS, NOT DEFINED
;	SYMBOLS

;447	FIX SETUP OF PARAMETERS FOR SWAPPING TO USE GETTABS
;	AND WIN WITH UNITS MISSING

;450	FIX CODE FOR HANDLING ERROR FILE TO ADD WORD AT START
;	OF EACH BLOCK POINTING AT FIRST COMPLETE ENTRY
;	IN BLOCK.  ALSO, PAD LAST BLOCK WITH ZEROS.

;451	ADD SUPPORT FOR CODE 50 IN ERROR.SYS, DL10 ERRORS
;	602 AND LATER MONITORS ONLY.

;452	ADD FUNCTION 5 TO DAEMON UUO, APPEND TO ERROR.SYS

;453	ADD SUPPORT FOR OSAVE AND OSSAVE COMMANDS FOR 6.02

;454	ADD CODE TO GET JOB # FROM DDB ON BOTH 602 AND EARLIER MONITORS
;	JOB # WAS MOVED TO NEW WORD IN 602

;455	ADD CODE TO SYNCHRONIZE ERROR REPORTING WITH STOPPED JOBS. NO
;	JOBS WILL NOW BE STARTED AT THE JOB LOOP -- ALL WILL NOW BE
;	STARTED AFTER THE ERROR THAT STOPPED THEM IS REPORTED

;456	ADD CODE TO SUPPORT SOME NEW ERRORS REPORTED IN 6.02
;	NEW ERRORS ARE: .ESCSC (CONFIGURATION STATUS CHANGE) = 15
;			ER.MSE (MONITOR SOFTWARE ERROR)      =  2
;			.ESNXM (NON-EX-MEM, NEVER BEFORE SUPPORTED) = 4

;457	CORRECT IMPLEMENTATION OF ER.MSE

;460	FIX BUG IN IMPLEMENTATION OF DL10 (ER.DLE) ERRORS

;461	FIX TAPSER ERROR REPORTING TO USE TAPOP'S TO GET THE POINTERS
;	TO THE INITIAL AND FINAL ERROR STATUS AREAS, RATHER THAN
;	ABSOLUTE OFFSETS RELATIVE TO TUBRID

;462	FIX UP MASSBUSS ERRORS TO REPORT USER'S PPN AND FILENAME CORRECTLY

;463	ADD USERS ID ETC TO CODE 3 & 10,ADD BAD BLOCKS SLOTS
;	TO CODE 10 & 11, ADD EXPECTED TERM. WD TO CODE 6 (RLD)

;464	CHANGE ERROR FILE HEADER LENGTH TO 4 AND INCLUDE SYS SER. # AND
;	CHAGE HEADER VERSION TO 1, INCREASE MAX ENTRY SIZE TO 777 VICE
;	77 BY MOVING HEADER LENGTH LEFT 3 PLACES (RLD)

;465	ANSWER TO SPR#15908

;466	MODIFICATIONS TO CORRECT FOR VERSION 1 HEADERS IN ERROR FILE

;467	ADD CODE TO REPORT NXM AND SET MEMORY OFFLINE TYPE ERRORS.
;	THIS ALSO CHANGES THE WHY RELOAD ERROR ENTRY SINCE IT
;	KEEPS A COPY OF THE ORIGINAL NXM TABLE IN OUR CORE.

;470	FIX CODE TO USE SUPER USETI./O IF SUSET. FAILS.

;471	(16349)MAKE DCORE, DUMP, AND SAVE ABORT ON DSK ERRORS

;472	FIX BUGS IN CODE 4(NXM), CODE 15(MEMORY ON/OFF LINE), AND IN
;	EDIT 466.

;473	FIX SOME USES OF MONITOR VERSION NUMBER TO BE USES OF "VMMON"
;	INSTEAD

;474	FIX BYTE POINTER TO STORE ENTRY LENGTH IN ERROR FILE HEADER
;	TO STORE 9 BITS, NOT 6. PART OF EDIT 466

;EDITS 475-507 RESERVED FOR SPECIAL RELEASES
;VERSION 13(474) RELEASED WITH ALL ABOVE EDITS
;ALL LATER EDITS IN VERSION 14

;510	FIX BUG IN COMPUTING POINTER TO PARITY SUBTABLE IN CODE 3
;	ERROR FILE ENTRY

;511	DON'T REPORT MEMORY ON-LINE AT RELOAD ON 507/601 AND EARLIER
;	MONITORS

;512	ALLOW ERROR FILE ENTRIES TO SPAN MORE THAN ONE BLOCK, I.E.
;	DO THE RIGHT THING IF AN ENTRY HAS TWO BLOCK BOUNDARIES
;	IN IT

;513	ADD SUPPORT FOR KL10 PROCESSOR ERRORS, CODE 60-AR/ARX PARITY
;	TRAP, CODE 61- PARITY INTERRUPT, CODE 67-ADDRESSING FAILURE
;VERSION 14(513) RELEASED WITH ALL ABOVE EDITS
;ALL LATER EDITS IN VERSION 15

;EDITS 514-517 RESERVED FOR SPECIAL RELEASES

;520	PERFORM PROPER ADDRESS CHECKING FOR ALLOCATED BUT ZERO PAGE

;521	CORRECT ADDRESS CHECK CODE TO PREVENT POSSIBLE
;	OVERWRITING OF JOB DATA AREA

;522	SET UP JBINT TO PREVENT HANG IN TO STATE ON OPR
;	ACTION REQUESTED CONDITIONS

;523	UUOSYMIZE DAEMON

;524	IN CASE OF STRUUO FAILURE WHEN RESETTING DAEMON SEARCH LIST,
;	PURGE INVALID STRUCTURES AND RETRY STRUUO.

;525	FIX ERRORS IN WRTSWP (ROUTINE TO WRITE SWAPPING SPACE)

;526	FIX IMPROPER ADDI AT UADOLD-2 (FROM EDIT 521)

;527	ELLIMINATE MILLIONS OF  HRRI T1,.GTSTS 'S

;530	USE XOR IN PLACE OF ANDCA TO SEE IF A BLOCK CROSSES PAGE BOUNDARIES

;531	FIX POTENTIAL LOOP IN FINCHK BY CHECKING FOR PROPER SEGMENTS

;532	GET RID OF UNNEEDED  MOVSI T1,(J) 'S

;533	MAKE DAEMON FUNCTION TO APPEND TO ERROR.SYS WORK

;534	ZERO TRAILING SPACE ON ERROR.SYS ON OUTPUT

;535	REASSIGN CHANNELS TO OUR JOB ON RELEASE OF CHANNEL.  THUS IF
;	WE ARE ASKED TO LOG AN ERROR FOR THAT FILE, THE DDB WILL STILL
;	EXIST.

;536	RIP OUT SCANER AND REPLACE WITH REASONABLE COMMAND SCANNER.
;	MAKE MINOR NECESSARY CHANGES TO THE DEVIL HIMSELF, INCLUDING
;	REMOVAL OF EDIT 76

;537	FIX UP INTERRUPT ROUTINE SO IT WON'T DESTROY T1 OR SELF-DESTRUCT

;540	WRITE DAEMON FILES ON SYS ONCE AGAIN.  DISENABLE NEW TO AVOID
;	ANY PROBLEMS.  HACK OUT CODE WHICH RESETS DAEMON SEARCH LIST,
;	SINCE THIS ISN'T NECESSARY WHEN WRITING ON ERSATZ DEVICES

;541	FIX IMPROPER INDEX AC TO PREVENT PROBLEMS WHEN READING ARG
;	BLOCK FROM USER WHICH IS SPLIT ACROSS PAGES.

;542	MAKE SURE USER HAS RENAME PRIV'S WHEN DELETING .SAV, .HGH, ETC.
;	ALSO FIX CHCACC FUNCTION IN ACCCHK TO CHECK WRITE PRIV'S INSTEAD
;	OF RENAME PRIV'S WHEN TRYING TO WRITE
;543	PUT PATCHING SPACE AND JUNK UNDER FTDEBG

;544	SET UP INTADR SO WE CAN'T LEAVE USER IN DAEMON ERROR PAUSE
;	IF WE BLOW UP WHILE RECORDING ERROR

;545	ON DISK ERROR, DON'T GIVE EXPECTED CHANNEL TERMINATION WORD
;	UNLESS HAVE A 601 OR GREATER MONITOR, SINCE THIS VALUE IS NOT
;	STORED FOR THE LAST ERROR ON PREVIOUS MONITORS, AND THE BEST
;	WE CAN DO IS PICK UP THE LAST EXPECTED CHANNEL TERMINATION WORD,
;	WHICH MAY BE FOR A MORE RECENT TRANSFER THAN THE LAST ERROR.

;546	ADD SUPPORT FOR KL10 CPU ERROR INFORMATION OBTAINED FROM
;	RSX20 FRONT END -11

;547	CHANGE FORMAT OF KL ERROR ENTRY IN ERROR.SYS TO INCLUDE
;	COUNT OF 8 BIT DATA BYTES
;550	ADD DEFINITION OF %CVPTR&.ESDRE UNTIL UUOSYM GETS
;	UPDATED.  ALSO ADD SUPPORT FOR LP100-CODE 71
;551	ADD FUNCTION TO SUPPORT READING PENDING ENTRIES AND OTER INFO
;	FROM CRASH.EXE FILES.
;552	FIXES TO CORRECTLY RECORD OVER 200 WORD ERROR FILE ENTRIES
;553	FIX PREV.CHANGES & MAKE UNUSED AND ILLEGAL ERROR CODES
;	GO TO JOBFIN VOCE CPOPJ

;554	FIX EDIT 545 SO THAT MONVER GETS SET UP CORRECTLY
;555	MAKE WORK CORRECTLY FOR RPO6'S

;556	FIX KLERR STUFF


;557 PUT CRASH STUFF IN FTSAVE CONDITIONALS
;560 FIX EDIT 557

;561	SAVE PDBPTR AROUND CRASH CHECK STUFF AND FIX TYPO'S

;562	GET UDT FROM %CNDTM NOT %CNDAT

;563	FIX GTUPMP CODE

;564	DON'T SMASH TWO PACK STRUCTURES WITH CRASH STUFF

;565	FIX EDIT 544

;566	FIX MAGTAPE ERROR REPORTING

;567	FIX REDSWP ON VM

;570	MAKE DAEMON EXIT GRACEFULLY ON RE-ENTER

;571	DCORE SPOOLED DDB'S CORRECTLY.

;572	HANDLE AN ARGUMENT BLOCK SPLIT ACROSS FRAGMENTED PAGES
;	ON THE SWAPPING SPACE CORRECTLY ON A VM SYSTEM.
;573	FIX MAGTAPE STATISTICS REPORTING.  THE UNIT NAME WAS BEING
;	OUTPUT AS GARBAGE.
;574	FIX MAGTAPE STATISTICS SO THAT THERE IS SOME USEFUL INFORMATION
;	IN THE ERROR.SYS FILE.
;575	FIX PATH. UUO SO WE DON'T WASTE A PPB
;576	FIX CALL TO GETZ AT CRSIN3+5
;577	HISTORICALLY THE MAXIMUM FACT FILE SIZE WAS 2048 BLOCKS
;	WITH SAID MAXIMUM ACHIEVED WHEN P2 EXCEED 777577 AND
;	RESULTING ARITHMETIC ERRONEOUSLY POINTED TO THE WRONG BLOCK.
;	RECODE TO TEST FOR THIS CONDITION AND EXIT ACCORDINGLY.
;600	FIX BUG WHERE DATA FROM TAPE UNLOAD IS NOT SAVED.
;	CURE: AT GTPST1+1 ADJUST T1 NOT P1
;601	ADD USER'S P,PN AND PROGRAM NAME TO MAGTAPE ERROR REPORTING
;602	ADD IPCF SUPPORT FOR SYSERR
;603	5-10-78	ISM	CHECK FOR 0 LENGTH DAEMON QUEUE TABLE IN
;	CRASH.EXE FILE. THIS FIXES A PROBLEM WITH DAEMON LOOPING
;	WHILE READING CERTAIN CRASH.EXE FILES DURING STARTUP.
;604	5-15-78	ISM	FIX EDIT 544. INTADR IMPROPERLY SET UP
;	SO THAT DAEMON MAY GO INTO TO STATE WHILE DETACHED.
;605	5-19-78	ISM	DCORE FILES GET WRITTEN INTO [1,2] IF SIGN
;	BIT IS SET IN THE PPN. CHECK FOR SIGN BIT ON AND BOMB THE USER.
;606	6-5-78	ISM	DAEMON DOES NOT HANDLE DUMP COMMAND FOLLOWED BY
;	SPACES PROPERLY. THROW AWAY THE SPACES.
;607	RELEASE DISK CHANNEL UPON EXITING CRSCHK SINCE WE ARE DONE WITH IT
;610	AFTER DOING A DCORE, CHECK IF USER JOB IS DETACHED.  IF NOT
;	DO NOT REATTACH JOB.
;611	8-22-78	ISM	FILES WITH PROTECTION GREATER THAN <400>
;	(FILE DAEMON CALLABLE) CAN GET WIPED OUT BY DCORE FUNCTION. USE
;	"IN YOUR BEHALF" FILOP. TO CHECK PROTECTION PROPERLY.
;612	18-SEP-78/BAH DAEMON SENDS A PACKET WITH AN INCORRECT INDEX INTO
;	ERROR.SYS WHEN USING THE /NOW SWITCH IN SYSERR. ALSO SYSERR ALWAYS
;	REPORTS 0 FOR CHAR/H/S FOR READ/WRITE ERRORS IN MAGTAPE STATS
;	BECAUSE DAEMON USES WRONG OFFSET TO GET THE DATA.
;VERSION 17
;700	ADD SUPPORT FOR KS10 SYSTEM ERROR ENTRIES
;701	CHANGE THE REFERENCES TO WORD 36 TO .JBSYM (WORD 116). (USED
;	AS THE FLAG WORD FOR DETERMINING IF A CRASH FILE HAS BEEN PROCESSED
;	BY DAEMON OR NOT)
;702	FIX MAGTAPE STATS ENTRY ROUTINE
;703	(SPR 10-26436) ALLOW CONTROLLER NUMBER TO BE REPORTED TO
;	SYSERR.  AREA AFFECTED: MBDVDN
;704	ADD CODE TO LOG DX20 DEVICE ERRORS.  AT PRESENT THIS MEANS
;	TU7X TAPE ERRORS REPORTED BY TAPSER.
;705	REMOVE ALL CODE SPECIFIC TO PRE-603 MONITORS.  THIS IS THE FIRST
;	STEP IN UPDATING DAEMON FOR 701.
;706	ADD CODE TO DUMP THE CPU STATUS BLOCK VIA ERROR CODE 63 (.ESCSB)
;707	ADD CODE TO DUMP THE DEVICE STATUS BLOCK VIA ERROR CODE 64
;	(.ESDSB)
;710	ADD CODE TO SEND THE DATE/TIME INCREMENTAL CHANGE TO %SIACT
;	IF THE SYSTEM DATE/TIME CHANGES.  INVOKED BY SUBFUNCTION 3
;	OF ERROR CODE 15.
;711	BRING ALL THE CODE UP TO 7.01 LEVEL.  ALSO FIX A LARGE NUMBER
;	OF BUGS IN THE ERROR REPORTING CODE.
;712	ADD CODE TO LOG SOFTWARE EVENTS OF INTEREST VIA CODE 14.
;713	REWRITE MISCELANEOUS PARTS OF THE CODE TO CLEAN IT UP
;	AND FIX A LOT OF BUGS.
;714	TO BE ABLE TO MEASURE SOME SORT OF GENERIC "AVAILABILITY"
;	FOR A SYSTEM,  MAKE DAEMON WAKE UP EVERY 6 MINUTES AND
;	LOOK AROUND TO SEE WHAT THE SYSTEM STATE IS.  LOG THIS
;	INFORMATION TO A NEW FILE, SYS:AVAIL.SYS.
;715	FOR SYSTEMS WITH JOBN+SEGN-1 .GT. 511 (7.01 REMEMBER),
;	THE GETTAB SIMULATION FAILS FOR TABLES INDEXED BY JOB
;	OR SEGMENT NUMBER IF THE INDEX IS LARGER THAN 511.
;	NUMTAB ONLY HAS 9 BITS FOR THE MAXIMUM TABLES SIZE.  THE
;	MONITOR USES JBTMXL EXPLICITLY FOR SUCH TABLES.
;716	FOR SYSTEMS WITH A LARGE NUMBER OF (POSSIBLY DUAL PORTED)
;	DISKS, ERROR CODE 5, INFORMATION EXTRACTED FROM A CRASH
;	MAY OVERFLOW WHILE WRITING THE ENTRIES FOR EACH DISK.
;	REMOVE THE DISK STUFF FROM ERROR CODE 5 AND WRITE A
;	NEW ENTRY (45) WHICH CONTAINS THE INFORMATION.
;717	LOG NODE OFF AND ON-LINE VIA SUB-CODES 6 AND 7 OF THE
;	CONFIGURATION STATUS CHANGE ENTRY.
;720	IF THE ERPTBK TABLE IN A CRASHED MONITOR IS FULL AND EPKIDX
;	IS 0, DAEMON WILL LOOP PROCESSING THE ENTRIES.  REDO THE CODE
;	AT READCR TO AVOID THIS PROBLEM.
;721	DAEMON MAY REATTACH THE WRONG JOB TO A TERMINAL AFTER PROCESSING
;	A REQUEST IF THE USER ATTACHES AND LOGS OUT THE JOB DOING THE
;	REQUEST AND ANOTHER USER LOGS IN AND DETACHES BEFORE THE REQUEST
;	IS COMPLETED.  DON'T REATTACH A JOB TO A TERMINAL IF THE JOB'S
;	LOGIN TIME DOESN'T MATCH THAT SAVED AT THE START OF THE REQUEST
;722	FIX PROBLEM WHERE DAEMON WAS RETURNING A UUO ERROR CODE WHILE
;	PROCESSING HARDWARE ERRORS.
;723	IF AN ERROR OCCURS WHEN DAEMON IS DETACHED, THE ERROR MESSAGE,
;	IF ANY, IS THROWN ON THE FLOOR.  USE TRMOP. FUNCTION .TODSP
;	TO PRINT THE ERROR MESSAGE ON DEVICE OPR.
;724	AVOID PROBLEMS WITH USERS GETTING A [1,2] JOB BY ALWAYS
;	SETTING DSKFUL ERROR WHEN DAEMON STARTS UP.
;725	SINCE THE 700 MONITOR IS CAPABLE OF CHANGING THE ACTIVE
;	SWAPPING LIST ON THE FLY, WE CAN NO LONGER READ SWPTAB
;	WHEN DAEMON STARTS UP.  READ THE INFORMATION FROM SWPTAB
;	ON EACH REQUEST TO READ/WRITE THE SWAPPING SPACE.
;726	TC10C TAPE ERRORS ARE REPORTED BY SYSERR AS DX10 ERRORS.
;	THE DEVICE CODE FOR TC10C'S IS 724 AND THE CODE ASSUMED
;	THAT ANY DEVICE CODE LARGER THAT 540 WAS AN RH20.  A COMPLEMENTING
;	BUG IN SYSERR THEN TURNED THAT INTO A DX10 ERROR REPORT.
;727	FIX BUG IN PEKUSR THAT CAUSED SWAP POINTER INCONSISTENCIES
;	WHILE READING FUNNY SPACE ADDRESSES.
;730	FIX RANDOM ERROR CODE 3 FAILURES IN DAEMON UUO FACT FUNCTION
;	CAUSED BY RELEASING THE JOB WITHOUT PROCESSING IT
;	IF JS.DPM WAS SET.
;731	YET ANOTHER SECURITY FIX TO PREVENT A USER GETTING A [1,2]
;	JOB.
;732	FIX ROUTINE TO DUMP KS10 HALT STATUS BLOCK
;START VERSION 20 HERE WITH 7.01 FIELD TEST
;
;733	FIX KLERR ROUTINE TO CORRECTLY PROCESS ENTRIES ON CPUS OTHER
;	THAN THE BOOT CPU
;734	CHANGE THE FORMAT OF THE ERPTBK TABLE TO USE 4 WORD ENTRIES SO
;	THAT THE CPU NUMBER CAN BE INCLUDED IN THE ERROR DATA RETURNED
;	BY THE MONITOR.
;735	ADD SUPPORT FOR NEW SYSERR ENTRIES FOR 7.01 MONITOR. THESE ARE
;	ERROR TYPES 6,51,52,54,55,56 AND 57.
;736	FIX DATA GATHERING PROBLEM IN ENTRY TYPE 56.
;737	FIX SBDIAG POINTER IN ENTRY TYPE 52
;740	MAKE ANOTHER ATTEMPT AT FIXING THE KLERR REPORT.
;741	FIX ILL MEM REF IN MASSBUS ERROR ROUTINE.
;742	MONBTS NOW WRITES DUMPS THAT ARE NOT A CONTIGUOUS PHYSICAL
;	CORE IMAGE.  CHANGE PEKSPY TO READ AND FOLLOW THE .EXE
;	DIRECTORY WHEN READING WORDS FROM THE DISK.
;743	IF A USER ^C'S DAEMON WHILE IT IS WRITING A DUMP/DCORE/SAVE
;	FILE, DAEMON DOESN'T RELEASE THE CHANNEL WHICH CAUSES THE
;	USER TO GET AN FBM ERROR WHEN HE TRYS TO DELETE THE TMP FILE.
;744	DEVICE OPR0 DOESN'T EXIST IF FTNET IS TURNED OFF OR IF A SITE
;	HACKS THEIR MONITOR.  MAKE DAEMON TRY HARDER TO FIND A TTY TO
;	TRMOP. TO.
;745	AVAIL HAS TROUBLE FIGURING OUT WHEN AVAIL.SYS STARTED AND WHEN
;	IT ENDED.  DEFINE 2 NEW ENTRIES, 46 AND 47 WHICH ARE THE FIRST
;	AND LAST ENTRIES IN AVAIL.SYS.  THESE ENTRIES PROVIDE TIMESTAMPS
;	FOR AVAIL.
;746	MAKE PROCESSING OF ERRORS DURRING INITIALIZATION CLEANER
;747	LIMIT THE NUMBER OF ENTRIES MADE IN THE DISK STATISTICS
;	ENTRY TO AVOID INFINITE ERROR FILES IF THE UDB CHAINS GET
;	SCREWED UP.
;750	CHANGE DAEMON TO USE %CNSUP AS THE UPTIME INSTEAD OF %NSUPT
;	SO THAT IT GETS CLOCK REQUESTS RIGHT IF CPU0 ISN'T RUNNING.
;751	FIX BUG THAT CAUSED ERROR CODE 2 FROM JOBPEK WHEN READING
;	FRAGMENTED CORE.
;752	ADD CODE TO LOG RP20 DISK DEVICE ERRORS
;753	SPR #10-29485 	RKB	2-JUN-80
;	FIX ROUTINE "CHECK" TO FORCE A FACT.SYS UPDATE AT EACH CHECK-
;	POINT.  THIS HELPS PRESERVE MORE ACCOUNTING DATA.
;754	AVAIL.SYS UPDATE ROUTINES DON'T USE ERRDEV AND ERRPPN TO
;	DO THEIR UPDATES.
;755	SPR #10-29218	RKB	11-JUN-80
;	FIX ROUTINE "COMPRS" TO AVOID CREATING IOWDS OVER 128K.
;756	ALLOW A USER HAVING POKE PRIVILEGES TO DO THE APPEND TO
;	ERROR.SYS DAEMON UUO FUNCTION.
;757	LOOK FOR CRASH FILES TO PROCESS ON THE UNION OF THE
;	SYSTEM SEARCH LIST AND THE SYSTEM DUMP LIST.
;760	CLEAN UP THE RP20 ERROR REPORTING LOGIC TO MAKE IT LESS DEPENDENT
;	ON ASSEMBLED-IN PARAMETERS.
;START VERSION 21 HERE WITH 7.02
;
;761	UPDATE EDIT NUMBER FOR 7.02, REMOVE THE FT603 CONDITIONAL AND
;	MAKE SOME COSMETIC CHANGES.
;762	UNCONDITIONALLY LINK THE STATE OF FTCHKPNT TO THAT OF FTFACT.
;	YOU CAN NO LONGER ASSEMBLE DAEMON WITH FTFACT OFF AND FTCHKPNT
;	ON.  THIS WILL ALLOW US TO TURN OFF THE FTFACT FEATURE TEST
;	SWITCH IN PREPARATION FOR THE MOVE TO USAGE ACCOUNTING.
;763	ADD THE FTI701 FEATURE TEST SWITCH THAT WILL SURROUND CODE
;	THAT MUST EXIST FOR PRE-702 MONITORS.  THE DEFAULT VALUE
;	WILL BE ON.  THE FEATURE TEST SWITCH AND THE CODE THAT
;	IT SURROUNDS WILL BE REMOVED ON 703 SHIP.
;764	DO SOME MORE GENERAL CODE CLEANUP.
;765	REMOVE SUPPORT FOR THE SAVE AND SSAVE COMMANDS.  THESE COMMANDS
;	WERE ONLY INVOKED BY .SAV FILE WRITES FOR VIRTUAL PROGRAMS.
;766	SUPPORT KL-PAGING MONITORS; DESUPPORT E, D, VERSION, DCORE, AND DUMP
;	COMMANDS FOR 7.02 MONITORS.
;767	SPR #32036	BCM	26-MAR-82
;	INCREASE THE MAXIMUM NUMBER OF CPUS WE CAN REPORT TO THE
;	CURRENT SUPPORTED OPERATING SYSTEM MAXIMUM.
;770	IT HAS BECOME NEARLY IMPOSSIBLE TO CONTINUE SUPPORTING BOTH
;	THE CURRENT AND PREVIOUS VERSIONS OF THE MONITOR WITH DAEMON.
;	FIX DAEMON TO DETERMINE IF IT IS RUNNING WITH A PREVIOUS
;	VERSION OF THE MONITOR AND RUN THE OLD VERSION OF DAEMON IF
;	IT IS.  THE PREVIOUS VERSION IS ASSUMED TO HAVE A NAME OF
;	THE FORM DAEXXX.EXE, WHERE XXX IS THE MONITOR VERSION, E.G.,
;	DAE701 FOR THE 701 MONITOR.  WE CAN ALSO REMOVE A GREAT
;	DEAL OF CODE THAT WAS NECESSARY TO SUPPORT PRE-702 MONITORS
;	AS A RESULT.  INSTEAD OF REMOVING THIS CODE COMPLETELY FROM
;	THE SOURCE, IT HAS BEEN PLACED UNDER THE FTUNSUPPORTED FEATURE
;	TEST SWITCH.  THIS VERSION OF DAEMON HAS ONLY BEEN TESTED WITH
;	THAT SWITCH TURNED OFF AND WE EXPRESSLY DO NOT SUPPORT DAEMON
;	WITH THE SWITCH TURNED ON.  THE SWITCH IS THERE STRICTLY FOR
;	REFERENCE PURPOSES AND ALL CODE CONTAINED WITHIN THE SCOPE
;	OF THE FEATURE TEST SWITCH WILL BE REMOVED FOR THE NEXT RELEASE
;	OF THE MONITOR.
;771	INCREASE THE SIZE OF THE ERROR.SYS HEADER TO 5 WORDS AND ADD
;	A MONOTONICALLY INCREASING SEQUENCE NUMBER IN THE NEW WORD.
;	DAEMON ALSO WRITES THE SEQUENCE NUMBER INTO THE EOF WORD AT
;	THE END OF THE FILE WHEN IT WRITES AN ENTRY.  EVERYTIME IT
;	STARTS UP, DAEMON FINDS THE EOF WORD IN THE FILE AND USES
;	THAT NUMBER AS THE START FOR THE NEW RUN.  IF IT CAN'T FIND
;	THE EOF MARK IN THE FILE FOR SOME REASON, DAEMON WILL TRY
;	TO FIND THE FIRST SEQUENCE NUMBER IN THE FILE.  IT THEN ADDS
;	THE SIZE OF THE FILE DIVIDED BY THE SIZE OF THE SMALLEST
;	ERROR ENTRY TO THAT NUMBER AND USES THAT AS THE SEQUENCE
;	NUMBER.  AS PART OF THIS CHANGE, DAEMON WILL CONTINUE TO
;	USE ERROR.XNN INSTEAD OF ERROR.SYS IF IT FINDS AN .XNN
;	FILE.  THIS MEANS THAT THE SEQUENCE NUMBERS IN THE FILES
;	SHOULD START IN ERROR.SYS AND INCREASE THRU ERROR.XNN.
;	NOTE THAT THIS MEANS THAT ALL ERROR.* FILES MUST BE
;	PROCESSED AS A UNIT.  SEE THE 7.02 BEWARE FILE FOR
;	MORE INFORMATION.
;772	STU GROSSMAN	13-JAN-83
;	SYMPTOM: THE FIRST ENTRY IN ERROR.XNN IS WRONG SIZE, AND MAY CONTAIN
;	PIECES OF DAEMON CODE.  ALSO, MANY RE-SYNC POINTERS GENERATED.
;	IF YOU GET AN I/O ERROR IN ERRFIN WHILE WRITING AN ENTRY INTO
;	THE FILE, WE CREATE AN ERROR.XNN+1 FILE, AND THEN RESTART ERRFIN.
;	UNFORTUNATELY P4 (WHICH SHOULD POINT TO THE LAST WORD WRITTEN INTO
;	ERABUF), MAY HAVE BEEN CHANGED INTO A WORD COUNT BY ERFILL.  WHEN
;	WE GET TO ERFILL AGAIN, IT TRIES TO CONVERT P4 INTO A WORD COUNT
;	(AGAIN!).  THINGS GO DOWN HILL FROM THERE.
;	FIX: RESTORE OLD VALUE OF P4 BEFORE RESTARTING ERRFIN.
;773	GMU/KBY		7-OCT-83
;	AVAIL.SYS GETS BAD ENTRIES IN IT SWITCHING FROM 7.01(A) TO 7.02.
;	THIS IS BECAUSE WE THINK THE HEADER FOR THE AVAIL.SYS WORKING
;	ENTRY IS THE SAME LENGTH AS THE HEADER FOR THE ERROR.SYS/AVAIL.SYS
;	DATA ENTRIES.  THIS CHANGED WITH 7.02.  USE NEW SYMBOL AWELEN WHERE
;	APPROPRIATE FOR DEALING WITH THE AWE.  ALSO, DON'T STORE EL.HED IN
;	WORKING ENTRY HEADER.
;774	TARL		17-OCT-83
;	FIX PEKDI6, WHERE READING A KI PAGING CRASH WOULD RETRIEVE BAD
;	ADDRESSES FROM CRASH FILE THROUGH PEKDSK.
;775	TARL		17-OCT-83
;	MOVE CLRBFI FROM INITIALIZATION CODE TO RIGHT BEFORE THE HIBER.
;	HELPS AVOID LOOPS WHILE DEBUGGING DAEMON ON A TERMINAL.
;776	TARL		17-OCT-83
;	WHILE WE'RE IN THE CODE, COMBINE THE BINARY AND SOURCE COPYRIGHT
;	STATEMENTS. HAVE THEM BE ON THE FIRST PAGE.
;
;777	DZIEDZIC	2-FEB-84
;	ADD SYSTEM ERROR BLOCK SUPPORT AS PART OF CI DISK PROJECT.
;START VERSION 22 HERE.
;
;1000	TARL		22-NOV-83
;	UP THE MONITOR VERSION TO 703
;
;1001	TARL		15-MAY-84
;	MCO 11194, SIX CHARACTER STOPCODE NAMES. STOPCODE NAME IS NOW IN
;	%SYSCD INSTEAD OF %SYSPC.
;
;1002	TARL		22-MAY-84, SPR 34163
;	BAD RANGE CHECK AT DSPERR, FIXED.
;
;1003	TL		28-Nov-84
;	Do ORION messages when KLINIK events are logged
;
;1004	CJA		1985-03-22 09:25:45
;	In routine ERRFIN
;	make DAEMON do multi-block output to ERROR/AVAIL file
;	so we don't write a partial entry if the system crashes.
;
;1005	CJA		17-APR-85
;	In routine AVLAGE, rename LAST-WEEK's AVAIL file to .Ann
;	and rename AVAIL.SYS to AVAIL.LWK (to make it the new
;	LAST-WEEK file). Complain if we already used AVAIL.A77.
;
;1006	CJA		24-APR-85
;	Since edit 1004, we get some entries in ERROR.SYS which
;	are not reflected in RIBSIZE.  Perhaps the data was written
;	but the system crashed before the RIB was updated.  Try
;	to preserve these entries when looking for EOF.
;
;1007	JAD		2-MAY-85
;	Fix QUEUE. UUO argument block so WTO's aren't so messy.
;
;1010	LEO		9-AUG-85
;	DO COPYRIGHTS.
;
;1011	CJA		30-APR-85
;	First attempt to flag software-detected disk errors positively.
;	Left half of 4 word ERRPT. block will contain a code to indicate
;	which of the several software-detected errors occur.  These include
;	SAT errors, RIB errors, Recovered Transfer hung, etc.  All of
;	these errors are detected in FILIO.  This enlarges the massbus-disk
;	entry (11) by one word.
;
;1012	CJA		30-APR-85
;	Create new entry (code 74) for FILIO-detected CI disk error.
;	Similar to massbuss disk (code 11) entry, but without a lot
;	of useless information about controller status.
;
;1013	DPM	#33947	29-Apr-86
;	Range check the number of kontroller registers.  Don't allow
;	more than MAXREG.
;
;1014	JAD		20-May-86
;	Don't attempt to SPY on more than 128K.
;Start version 23 for use with 7.04 monitor
;
;1015	DPM		 6-Jan-87
;	Use new GETTAB table .GTCHN to fetch channel data block offsets
;	instead of hardwiring needed values.
;
;1016	JAD		 1-Apr-87
;	Ignore code 53 error entries (KS memory errors) for the moment.
;
;1017	DPM		 5-May-87
;	If the oldest (first) entry in the error file is older than
;	7 days, create a new file.
;
;1020	JAD		27-Jul-87
;	Always do an SEBLK. UUO after the ERRPT. UUO to ensure any system
;	error blocks are copied to the error file in a timely fashion.
;
;1021	DPM		13-Aug-87
;	Update FILSER and TAPSER KDB/UDB offsets to agree with the new
;	values defined in DEVPRM.
;
;1022	DPM		 3-Nov-87
;	Fix up references where UNIKON was used to find the KDB.  UDBKDB
;	now points to the KDB.  Delete definition of UNIKON since it is
;	no longer used.
;
;1023	DPM		12-May-88
;	Edit 1022 revisited.  UDB offsets changed again.
;
;1024	?
;
;1025	JAD		31-May-88
;	Handle IPA20 DRAM dump record in ERROR.SYS and write a dump
;	file containing the DRAM data on XPN:.
;
;1026	RCB		05-Apr-89
;	Add EH.AVL to the flags defined in our ERROR.SYS entry header
;	so that we can write system error blocks to AVAIL.SYS.
;	This will be used as we covert the monitor from using old
;	ERRPT.-style blocks to SE blocks for error reporting.
;
;1027	DPM		05-Sep-89
;	Add EH.NER to the flags defined in our ERROR.SYS entry header
;	which signifies that the record is NOT destined for ERROR.SYS.
;	Presumably, the monitor will lite EH.AVL, causing the record
;	to be written elsewhere.
;
;1030	DPM		14-Nov-89
;	Strip out old error logging stuff and code to read crash files.
;	The monitor now prepares fully formatted system error blocks
;	and reads crash files on startup.
;
;1031	DPM		 5-Dec-89
;	Require %CNDAE to return 705.  If not, run another DAEMON.
;
; END OF EDIT HISTORY
SUBTTL SYMBOL DEFINITIONS

;DEBUGGING NOTE:
;IF DAEMON IS LOOPING AND YOU WANT TO STOP IT AND SAVE THE CORE
;IMAGE FOR LATER ANALYSIS, DO THE FOLLOWING:
;	.ATTACH 5[1,2]		;ATTACH TO IT FROM A [1,2] JOB
;	.HALT			;STOP IT
;	.REENTER		;STOP IT GRACEFULLY
;	.SAVE BADDAE		;SAVE FOR LATER
;THIS PROCEDURE WILL CAUSE DAEMON TO SAVE ITS AC'S SO THAT YOU GET
;A DUMP WITH SOME USEFUL INFORMATION.

	LOC	.JBVER
	BYTE	(3)VWHO(9)VDAEMON(6)VMINOR(18)VEDIT
	RELOC



ND	FTDEBG,0	;NON-ZERO FOR DEBUGGING FEATURES

ND	PDLEN,50	;LENGTH OF PUSH DOWN LIST

ND	EVASIZ,^D128	;NUMBER OF WORDS TO READ FROM FUNNY SPACE IN PEKUSR
			;THIS **MUST** BE ONE OF 128, 256, OR 512. ANY
			;OTHER VALUE WILL CAUSE PEKUSR TO RETURN WRONG
			;VALUES
ND	EVDPPB,340000	;BEGINNING OF "PER PROCESS" ADDRESS SPACE
ND	EVDPPE,400000	;END (+1) OF "PER PROCESS" ADDRESS SPACE

ND	LINSIZ,^D200	;MAX CHARACTERS/LINE
ND	CPUN,6		;MAX NO OF CPU'S TO LOG ERRORS ON
ND	ERRDEV,SIXBIT/SYS/ ;SYS IS THE RIGHT PLACE
ND	ERRPPN,<0>	;UFD (DEFAULT SYS:) FOR HARDWARE ERROR LOG FILE
ND	ERRSIZ,777	;LARGEST ERROR-FILE TRANSACTION
ND	MAXINT,^D100000	;NUMBER OF ERROR INTERCEPTS BEFORE STOPING
			;AUTOMATICALLY
ND	AVLTIM,^D360	;SECONDS BETWEEN AVAIL.SYS UPDATES
ND	ERRTIM,^D60	;SECONDS BETWEEN RETRIES ON ERROR.SYS
ND	SLPTIM,^D60	;TIME TO SLEEP IF NO WORK
ND	ICLKRQ,4	;NO OF INTERNAL CLOCK REQUEST TYPES
ND	IOERQ,-2	;TIMER TO RETRY ERRORS ON ERROR.SYS
ND	AVLRQ,-3	;UPDATE AVAIL.SYS REQUEST
;AC'S

T1=1		;TEMP AC'S
T2=2
T3=3
T4=4
P1=5		;PERMANENT AC'S
P2=6
P3=7
P4=10		;PERMANENT AC
J=11		;JOB NUMBER BEING SERVICED
F=13		;FLAGS
SC=16		;16 USED AS CHAR AC IN COMMAND SCANNER
P=17		;PUSH DOWN LIST PTR


;I/O CHANNELS

FCT==10
;MACROS

	ER.STP==1B12	;STOP PROGRAM ON THIS ERROR
	ER.OPR==1B11	;NOTIFY OPR ON THIS ERROR
	ER.NCR==1B10	;NO CRLF AT END OF MESSAGE

DEFINE	ERROR (MSG,FLG,LBL),<
	ERRWRN (<?>,<MSG>,FLG,LBL)
>

DEFINE	WARN  (MSG,FLG,LBL),<
	ERRWRN (<%>,<MSG>,FLG,LBL)
>

DEFINE	TELL  (MSG,FLG,LBL),<
	ERRWRN (< >,<MSG>,FLG,LBL)
>

DEFINE	ERRWRN (PFX,MSG,FLG,LBL),<
	PUSHJ	P,ERRMSG
	XLIST
	..XX==0
	..YY==0
	IRP FLG,<
	IFIDN <FLG>,<STOP>,<..XX==..XX!<ER.STP_-^D23>>
	IFIDN <FLG>,<NOCRLF>,<..XX==..XX!<ER.NCR_-^D23>>
	IFIDN <FLG>,<OPR>,<..XX=..XX!<ER.OPR_-^D23>>
	>
	IFNB <PFX>,<..YY=="'PFX'">
	IFB  <LBL>,<CAI ..XX,[XWD ..YY,[ASCIZ\MSG\]]>
	IFNB <LBL>,<CAIA ..XX,[XWD ..YY,[ASCIZ\MSG\]]
		    JRST LBL>
	LIST
>
;FLAGS IN F

L.ANY==(1B8)		;SET IF ANY JOBS REQUIRING DAEMON SERVICE FOUND DURING SCAN
L.OPR==(1B9)		;SET IF MESSAGE SHOULD GO TO OPR
L.INI==(1B11)		;SET DURING INITIALIZATION TO INHIBIT TYPING
			;ERROR MESSAGES ON OPR
L.QUE==(1B12)		;OPR GOES ONLY TO ORION VIA QUEUE.

R.NER==1B18		;DO NOT WRITE RECORD INTO ERROR.SYS
R.AVL==1B19		;APPEND THIS ENTRY TO AVAIL.SYS IN ADDITION
			;TO ERROR.SYS (USED BY ERRFIN)
R.PAE==1B20		;THIS PASS THROUGH ERRFIN IS APPENDING THE
			;ENTRY TO AVAIL.SYS
R.NEB==1B21		;NO ERROR BLOCK FOR THIS CALL TO ERRINI, USE
			;BOOT CPU SERIAL NUMBER
R.INF==1B23		;THIS PASS THROUGH ERRFIN IS DOING THE
			;SEQUENCE NUMBER INITIALIZATION FOR ERROR.SYS
;GETTAB ITEM FOR VARIOUS TABLES

DACODE=='JD'	;STSTBL - QUEUE CODE FOR DAEMON QUEUE

;BITS IN JBTSTS WORD - LH

JACCT==1	;THE GODLY BIT

;BITS IN JBTSTS WORD - RH

JS.DEP==10000	;JOB WAITING IN DAEMON ERROR PAUSE
JS.DPM==100	;JOB HAS BEEN WAITING FOR DAEMON TO SERVICE IT
		;FOR A MINUTE.  IF THIS BIT IS SET, WE FREE THE
		;JOB SINCE IT'S BETTER TO MISS SOMETHING THAN
		;HANG UP THE SYSTEM.
;OFFSETS IN THE HEADER FOR ERROR.SYS

.EHTYP==0	;TYPE, VERSIONS, LENGTHS
	EH.TYP==777B8	;MASK FOR TYPE FIELD
	EH.NER==1B15	;DO NOT WRITE RECORD TO ERROR.SYS
	EH.CRS==1B16	;ENTRY EXTRACTED FROM CRASH.EXE
	EH.AVL==1B17	;COPY TO AVAIL.SYS AS WELL AS ERROR.SYS (NOT IN FILE)
;			; SPEAR CALLS THIS THE "TOPS-20" BIT
	EH.FFM==7B20	;MASK FOR FORMAT VERSION FIELD
		EL.FVR==0	;FORMAT VERSION OF ERROR.SYS
	EH.HFM==7B23	;MASK FOR HEADER VERSION FIELD
		EL.HVR==2	;HEADER VERSION OF ERROR.SYS
	EH.HLN==7B26	;MASK FOR HEADER LENGTH FIELD
		EL.HED==5	;LENGTH OF STANDARD HEADER
.EHDTM==1	;UNIVERSAL DATE/TIME OF ENTRY
.EHUPT==2	;UPTIME OF SYSTEM WHEN ENTRY CREATED
.EHASN==3	;CPU SERIAL NUMBER OF CPU ON WHICH ERROR WAS DETECTED
.EHSEQ==4	;SEQUENCE NUMBER OF ENTRY

;OFFSETS IN HEADER FOR AVAIL.SYS WORKING ENTRY

AWEHDR==0	;BYTE(9).ESMRV(9)0(6)EL.HVR(3)EL.HED(9)BODY LENGTH
AWEDAT==1	;UNIVERSAL DATE/TIME OF ENTRY
AWECDA==2	;UNIVERSAL DATE/TIME OF FILE CREATION
AWECDM==3	;15 BIT DATE,,TIME OF MAXCNF.SYS CREATION
    AWELEN==4	;LENGTH OF AVAIL.SYS WORKING ENTRY HEADER

;OFFSETS IN AVAIL.SYS MONITOR RUN VALUES (.ESMRV) BODY

MRVPSN==0	;-LEN,,OFFSET FROM MRVPSN TO ASCIZ SYSTEM NAME
MRVVER==1	;MONITOR VERSION NUMBER
MRVUPT==2	;UPTIME IN UNIVERSAL DATE/TIME FORMAT
MRVCTM==3	;MONITOR CRASH TIME (+/- 6 MIN) IN UNIVERSAL DATE/TIME FMT
MRVRTM==4	;MONITOR RELOAD TIME IN UNIVERSAL DATE/TIME FORMAT
MRVWHY==5	;WHY RELOAD CODE IN SIXBIT
MRVIDT==6	;SUM OF ALL INCREMENTAL DATE/TIME CHANGES SINCE RELOAD
MRVSNM==7	;FIRST OF 5 WORDS OF ASCIZ SYSTEM NAME
;MISC

B2WLSH==7	;BITS TO SHIFT TO CONVERT BLOCKS TO WORDS
BLKSIZ==^D128 	;NUMBER OF WORDS PER BLOCK
AVLWRK==<<ERRSIZ+AWELEN+BLKSIZ-1>_-B2WLSH> ;NUMBER OF BLOCKS USED FOR
		;WORKING ENTRY AT THE BEGINNING OF AVAIL.SYS
PG.BDY==777	;MASK FOR OFFSET IN PAGE
PG.SIZ==1000	;SIZE OF PAGE IN WORDS
ABSTAB==410	;ABSOLUTE LOCATION OF ADDRESS OF GETTAB TABLES
MAXUCR==1000	;MAXIMUM BLOCK TO READ FROM USER CORE
MAXUPM==PG.SIZ	;SIZE OF BLOCK TO USE AS UPMP BUFFER
SUBTTL INITIALIZE

DAEMON:	JFCL			;IN CASE OF CCL ENTRY
	RESET			;GET ALL RESET
	MOVE	P,PDL		;GET A STACK POINTER(IS A RUN UUO ACCU!!)
	MOVEI	T1,DAEREN	;REENTER FOR SHUT DOWN
	MOVEM	T1,.JBREN	;STORE IN REENTER ADDRESS
	SETZB	J,ZER		;J=VALUE OF QUEUE CODE FOR DAEMON QUEUE
	MOVE	T1,[ZER,,ZER+1]
	BLT	T1,EZER		;CLEAR STORAGE LOCATIONS
	MOVSI	F,L.INI		;CLEAR FLAGS, NOTE INITIALIZATION
	MOVEI	T1,E.EINT	;REENTER ADDRESS TO START WITH
	MOVEM	T1,INTADR
	MOVE	T1,[XWD 4,INTRES]  ;ROUTINE TO RESTART AFTER ERROR INTERCEPTS
	MOVEM	T1,INTBLK	;STORE IN INTERCEPT BLOCK
	MOVEI	T1,777747	;ENABLE FOR ALL ERRORS
	TLO	T1,400000	;SUPRESS ERROR TYPEOUTS
	SKIPE	.JBDDT		;ARE WE DEBUGGING?
	TXZ	T1,ER.ICC	;YES, PREVENT INSANE PROGRAMMERS
	MOVEM	T1,INTBLK+1	;STORE CONDITIONS IN INTERCEPT BLOCK
	MOVEI	T1,INTBLK
	MOVEM	T1,.JBINT	;SET UP JOB INTERCEPT BLOCK
	HRROI	T1,.GTPPN
	GETTAB	T1,		;GET OUR PPN
	  JRST	E.GTBF
	MOVE	T2,[%LDFFA]
	GETTAB	T2,		;GET FULL FILE ACCESS PPN
	  JRST	E.GTBF
	MOVEM	T2,OPRPPN	;STORE AWAY FOR LATER
	CAME	T1,T2		;SKIP IF HAVE FULL FILE ACCESS
	JRST	[ERROR	<Unprivileged job >,NOCRLF
		 PJOB	T1,	;GET THE JOB NUMBER
		 PUSHJ	P,TDEC	;TYPE IT
		 TELL	< attempted to run DAEMON>,STOP]

	PJOB	T1,		;OUR JOB NUMBER
	MOVEM	T1,THSJOB	;SAVE FOR LATER

	SETOM	THSLIN		;INDICATE OUR LINE
	GETLCH	THSLIN		;GET CONTROLLING LINE NO & BITS
	MOVE	T1,[%CNDAE]	;GETTAB ARG TO CHECK CORRECT VERSION
	GETTAB	T1,		;GET IT
	  MOVE	T1,['701',,701] ;MUST BE 701 OR EARLIER
	HLRZ	P1,T1		;KEEP DAEMON NAME IN RH OF P1
	HRRZS	T1		;KEEP JUST CURRENT DAEMON VERSION
	SUBI	T1,VMON		;COMPARE WITH THIS VERSION OF DAEMON
	JUMPE	T1,DAEMO1	;GO IF THIS IS THE RIGHT VERSION
	WARN	<Wrong version of DAEMON run; Invoking SYS:>,NOCRLF
	MOVSI	T2,'DAE'	;GET LH OF NEW NAME
	HRR	T2,P1		;PLUS REST OF NAME
	PUSHJ	P,TSIX		;TYPE THE NAME
	TELL	<.EXE as system DAEMON>
	MOVEI	T1,RUNBLK	;POINT TO RUN UUO BLOCK
	MOVSI	T2,'SYS'	;DEVICE
	MOVEM	T2,.RNDEV(T1)
	MOVSI	T2,'DAE'	;LH OF OUR NAME
	HRR	T2,P1		;AND RH VERSION
	MOVEM	T2,.RNNAM(T1)
	SETZM	.RNEXT(T1)	;DEFAULT TO .EXE
	SETZM	.RNPPN(T1)	;SYS IMPLIES A PPN
	SETZM	.RNMEM(T1)	;NO CORE ARGUMENTS
	RUN	T1,UU.PHY	;RUN THE NEW DAEMON
	  HALT	.		;NEVER RETURN

DAEMO1:	SETZM	ERABUF		;ZERO ERABUF.  MUST BE DONE HERE RATHER
	MOVE	T1,[ERABUF,,ERABUF+1] ;THAN WITH ZER,,EZER BLT BECAUSE
	BLT	T1,ERAEND	;PRVDAE OVERLAYS ERRBUF
	MOVE	T1,[%CNSJN]
	GETTAB	T1,		;GET LOWEST HIGH SEG NUMBER
	  JRST	E.GTBF		;CAN'T GIVE UP
	SOJ	T1,		;DECR TO HIGHEST JOB NUMBER
	HRRZM	T1,HJOB		;SAVE FOR JOB SCANS
	HLRE	T2,T1		;GET -SEGN
	MOVNS	T2		;MAKE IT POSITIVE
	ADDI	T2,(T1)		;COMPUTE MAX JOB+SEG NUMBER
	MOVEM	T2,JBTMXL	;SAVE FOR GETTAB SIMULATION
;HERE TO BUILD COPY OF GETTAB TABLE FOR GTBSPY
	MOVE 	T1,[%CNPDB]	;FIND WHERE JBTPDB IS
	GETTAB	T1,		; FROM MONITOR
	  JRST	E.GTBF		;CAN'T, ERROR
	MOVEM	T1,PDBPTR	;STORE FOR USE IN GETTAB SIMULATOR
	SETZB	T3,T4		;IN CASE WE CAN'T SPY
	MOVE	T1,[%CNSIZ]	;GET SIZE OF SYSTEM
	GETTAB	T1,
	  JRST	E.GTBF		;CAN'T, ERROR
	SUBI	T1,1		;HIGHEST LOC IN MONITOR
	CAILE	T1,377777	;MORE THAN 128K?
	MOVEI	T1,377777	;YES, REDUCE TO THE MAXIMUM
	SPY	T1,		;SET SPY
	  ERROR	<Can't SPY on the monitor>,STOP

	MOVEI	T1,.GTSLF	;GET TABLE NUMBER FOR NUMTAB
	HRLI	T1,(T1)		;WANT THE NUMTAB ENTRY FOR .GTSLF
	GETTAB	T1,		;GET IT
	  JRST	E.GTBF		;FAILED
	LSH	T1,-^D27	;KEEP JUST MAX ENTRY IN NUMTAB
	MOVEI	T2,1(T1)	;PLUS ONE FOR LENGTH OF NUMTAB
	PUSHJ	P,GETCOR	;GET THAT MUCH CORE
	  ERROR	<Can't get core for NUMTAB>,STOP
	MOVEM	T2,GTBST	;SAVE ADDRESS OF START OF TABLE
NXTGTB:	HRLZ	T1,T3		;NEXT TABLE PTR
	HRRI	T1,.GTSLF
	GETTAB	T1,		;GET NEXT TABLE PTR
	  JRST	ENDGTB		;THAT'S ALL
	TLZE	T1,37		;CLEAR MONITOR XR AND INDIRECT
	TLO	T1,P3		;SET OUR AC IF TABLE EXISTS
				; (IE, IF POINTER WAS INDEXED)
	TRO	T1,400000	;SET SPY BIT
	MOVEM	T1,(T2)		;STORE IN TABLE OF TABLE PTRS
	ADDI	T2,1		;BUMP TABLE POINTER
	AOJA	T3,NXTGTB	;LOOP FOR ALL POSSIBLE GETTABS

ENDGTB:	SUBI	T3,1		;COMPUTE MAX NUMBER OF GETTABS
	MOVEM	T3,MAXGTB	;SAVE MAX GETTAB NUMBER
	MOVE	T2,GTBST	;GET ADDRESS OF START OF POINTERS
	CAIGE	T3,.GTIDX	;DO WE HAVE A RANGE TABLE?
	TDZA	T3,T3		;NO, MAKE IT ZERO
	HRRZ	T3,.GTIDX(T2)	;GET SPYSEG ADDRESS OF RANGE TABLE
	TRZ	T3,400000	;TURN OFF SPYSEG BIT
	MOVEM	T3,RNGTAB	;SAVE FOR GETTAB SIMULATION
;HERE TO FIND QUEUE # FOR DAEMON WAIT

	SETZ	T2,
FNDDAQ:	HRLZ	T1,T2		;LH T1=NEXT WORD IN TABLE
	HRRI	T1,.GTWSN	;RH=QUEUE CODES TABLE
	PUSHJ	P,GTBSPY	;GET NEXT WORD IN QUEUE CODES TABLE
	  JRST	E.GTBF		;MUST FIND IT
	MOVE	T3,[POINT 12,T1]
FNDDA1:	TLNN	T3,770000	;SKIP IF MORE CODES IN THIS WORD
	AOJA	T2,FNDDAQ	;NO MORE, TRY NEXT WORD
	ILDB	T4,T3		;T4=NEXT CODE IN SIXBIT
	CAIE	T4,DACODE	;SKIP IF FOUND DAEMON QUEUE CODE
	AOJA	J,FNDDA1	;NO, TRY NEXT, COUNT CODES SKIPPED

;HERE WHEN FOUND DAEMON QUEUE CODE, NOW FIND CTY LINE NUMBER

	MOVEM	J,DAQNAM	;SAVE DAEMON QUEUE CODE
	MOVE	T1,[%CNPTY]
	PUSHJ	P,GTBSPY	;GET OFFSET FOR PTY'S
	  JRST	E.GTBF		;CAN'T GET IT
	HLRES	T1		;T1=OFFSET FOR PTY=LINE NUM OF CTY+1
	SUBI	T1,1		;LINE NUMBER OF CTY
	MOVEM	T1,CTYLIN	;STORE LINE NUMBER OF CTY
	SKIPGE	T1		;GET SWAPPING PARAMETERS IF OK
	ERROR	<Can't get CTY line number>,STOP

	MOVE T1,[%CNVER]	;GET MONITOR VERSION
	PUSHJ	P,GTBSPY
	  JRST	E.GTBF		;CAN'T GET IT
	MOVEM	T1,MONVER	;SAVE VERSION

;CONTINUED ON NEXT PAGE
;CONTINUED FROM PREVIOUS PAGE

	MOVE	T1,[%VMPPB]	;GETTAB ARG POINTER TO
	PUSHJ	P,GTBSPY	;GET BEGINNING OF PER PROCESS ADDRESS SPACE
	  MOVX	T1,EVDPPB	;DEFAULT
	MOVEM	T1,EVAPPB	;SET IT

	MOVE	T1,[%VMPPE]	;GETTAB ARG POINTER TO
	PUSHJ	P,GTBSPY	;GET END (+1) OF PER PROCESS ADDRESS SPACE
	  MOVX	T1,EVDPPE	;DEFAULT
	MOVEM	T1,EVAPPE	;SET IT

;FALL THROUGH TO GET SWAP STUFF
;GET, SAVE ORIGINAL FILE STRUCTURE SEARCH LIST FOR THIS JOB

	MOVE	T1,[3,,[.PTFSL	;DEFINE ADDITIONAL PATH TO BE SEARCHED
			0	;NO LIB, NO SYS, NO NEW, NO PROBLEMS
			0]]	;DON'T WASTE A PPB
	PATH.	T1,		;SET UP
	  JFCL			;WHAT ISN'T IMPLEMENTED CAN'T HURT US
	MOVE	T1,[.STDFL,,.DFERR] ;ARG TO SET DSKFUL ERROR
	SETUUO	T1,		;DO IT
	  WARN	<SETUUO to set DSKFUL ERROR failed>,OPR
;HERE TO CHECK FOR OTHER DAEMON'S RUNNING AROUND

	MOVE	T1,[.STDAE,,1]	;FUNCTION,,SET
	SETUUO	T1,		;MAKE US THE SYSTEM DAEMON
	  ERROR	<Cannot become system DAEMON>,STOP
	JRST	GETJIF		;PRESS ON
	ERROR	<DAEMON already running in job >,NOCRLF
	MOVE	T1,[%CNDJN]	;SET UP GETTAB
	PUSHJ	P,GTBSPY	;GET REAL DAEMON'S JOB NUMBER
	  SETZ	T1,		;???
	PUSHJ	P,TDEC		;PRINT IT
	TELL	<; cannot become system DAEMON>,STOP
;HERE TO GET SYSTEM CLOCK RATE (JIFFIES/SECOND)

GETJIF:	MOVE	T1,[%CNTIC]
	PUSHJ	P,GTBSPY
	  JRST	E.GTBF
	MOVEM	T1,JIFSEC	;STORE JIFFIES PER SECOND
	IMULI	T1,^D24*^D3600	;JIFFIES PER DAY
	MOVEM	T1,JIFDAY	;HOLD FOR DIVIDE
	MOVEI	T1,^D1000	;MILLISECONDS PER SECOND
	IDIV	T1,JIFSEC	;GET MILLISECONDS PER JIFFY
	MOVEM	T1,MILJIF	;STORE THAT, TOO

;HERE TO SET UP CLOCK FUNCTION TABLE

	MOVEI	T2,ICLKRQ-1	;GET NUMBER OF  QUEUES LESS THAN 0
	MOVNM	T2,QPNTR	;SAVE IN QPNTR AS MOST NEG. QUEUE NO
	ADD	T2,.JBFF	;POINT TO JOB 0
	HRRM	T2,JBTCLK	;STORE THE POINTER
	HRRZ	T1,HJOB		;HIGHEST JOB # IN SYSTEM
	MOVNI	T3,ICLKRQ+1(T1)	;-VE LENGTH OF TABLE
	HRLM	T3,QPNTR	;STORE IN POINTER
	MOVEI	T2,ICLKRQ+1(T1)	;GET NUMBER OF WORDS IN TABLE
	PUSHJ	P,GETCOR	;ALLOCATE THAT MUCH CORE
	  ERROR	<Can't get core for clock table>,STOP
;HERE TO START INTERNAL TIMERS

IFN AVLTIM,<
	HRREI	J,AVLRQ		;INSERT THE AVAIL.SYS UPDATE SCAN
	MOVEI	T1,AVLTIM	;INTERVAL
	PUSHJ	P,CLKREQ	;PUT IN THE REQUEST
>;END IFN AVLTIM
;REBUILD THE AVAIL.SYS WORKING ENTRY IN CORE

	PUSHJ	P,REDAVL	;READ WORKING ENTRY OR BUILD NEW ONE
	PUSHJ	P,ERRSTR	;SET UP EXTENSION/SEQ NUMBER FOR ERROR FILE
	MOVSI	T1,.ESDRE_9	;[550]SET UP RESTART CODE
	PUSHJ	P,ERRINI	;[550]STORE HEADER, SET P4 AND CLR BUFF
	PUSH	P4,.JBVER	;[550]GET JBVER
	PUSHJ	P,ERRFIN	;[550]AND STUFF IT

;HERE TO GET ADDRESS OF EXECT1 IN JOB DATA AREA

	MOVE	T1,[%CNET1]	;GETTAB THE ADDRESS OF EXEC T1
	PUSHJ	P,GTBSPY
	  JRST	E.GTBF
	MOVEM	T1,EXECT1	;AND SAVE FOR LATER
;HERE TO DETACH TTY

	HRLZ	T1,THSLIN	;LH IS NOW LINE NUMBER, RH=0 TO DETACH
	HRRZ	T2,.JBDDT	;GET ADDR OF DDT IF ANY
	JUMPN	T2,NODET	;DONT DETACH IF DDT
	TELL	<Detaching>
	ATTACH	T1,		;DETACH OUR TTY
	  ERROR <DETACH failed>,STOP
NODET:	TLZ	F,L.INI		;NO LONGER IN INITIALIZATION

	MOVEI	T1,DALOOK	;
	MOVEM	T1,INTADR	;
	SETOM	WTUERR		;INIT FLAG TO CATCH RECURSIVE EXIT ATTEMPTS
SUBTTL	CHECK FOR MONITOR-INITIATED DAEMON REQUEST

;HERE WHEN DAEMON AWAKENED
; IF A HARDWARE ERROR HAS BEEN DETECTED BY THE MONITOR, RECORD THE INFO
;COLLECTED INTO ERROR.SYS.  MONITOR COUNTERS GET BUMPED ON EVERY ERROR

DALOOK:	CAME	P,PDL		;VERIFY STACK CORRECTNESS
	MOVEM	P,PDLGUF	;OR SAVE THE BAD GUY
	MOVE	P,PDL		;RESTORE GOODNESS
	PUSHJ	P,NBSDAT	;GET DATE/TIME
	MOVEM	T1,DLKDTM	;SAVE FOR DEBUGGING
	SETZM	EVAADR		;CLEAR POINTER TO FUNNY SPACE BUFFER
	TLZ	F,L.OPR+L.QUE	;CLEAR ASSORTED BITS
	PUSHJ	P,RSEBLK	;PROCESS ANY SYSTEM ERROR BLOCKS
	JRST	DALOKJ		;DONE, GO LOOK AT JOBS
;SUBROUTINE TO RESTART JOB WAITING FOR ERROR TO BE REPORTED
;ARGS	J=JOB NUMBER

JOBFIN:	HRLZI	T1,(J)		;GET JOB NUMBER INTO LH OF T1
	IFN .GTSTS,<HRRI T1,.GTSTS>	;SET UP TO GET JOBS JBTSTS WORD
	PUSHJ	P,GTBDFL	;GET VALUE, DEFAULT TO ZERO IF FAIL
	TRNN	T1,JS.DEP	;IS JOB WAITING?
	POPJ	P,		;NOPE, RETURN
	MOVEI	T1,J		;GET ADDR OF ARGUMENT FOR DAEFIN
	DAEFIN	T1,		;AND SET JOB FREE
	  AOS	FINGUF		;REMEMBER # OF LOSSAGES
	POPJ	P,
SUBTTL LOOK FOR A JOB REQUIRING DAEMON SERVICE

DALOKJ:	HRRZ	J,HJOB		;J=HIGHEST JOB NUMBER IN SYSTEM
DALOO1:	TLZ	F,L.OPR+L.QUE	;CLEAR DUMP FLAG AND SAVE FLAG
	MOVEM	J,CURJOB	;SAVE LAST JOB EXAMINED FOR DEBUG
	PUSHJ	P,NBSDAT	;GET DATE/TIME
	MOVEM	T1,DLJDTM	;SAVE FOR DEBUGGING
	HRROI	T1,.GTSTS
	PUSHJ	P,GTBSPY	;GET JBTSTS WORD FOR JOB
	  WARN	<Failed to get JBTSTS in DALOKJ loop>,OPR,DALOO2
	LDB	T2,QUECOD	;GET CODE FOR DAEMON QUEUE
	CAMN	T2,DAQNAM	;SKIP IF NOT IN DAEMON QUEUE
	JRST	UUSERV		;GO PROCESS THE UUO
	TRNN	T1,JS.DPM	;IF JOB HAS BEEN WAITING FOR A MINUTE
	JRST	DALOO2		; AND WE DID NOT PROCESS IT, RELEASE
	AOS	DPMCNT		; IT SO THAT THE OPR DOESN'T GET
	MOVEM	J,DPMJOB	; OBSCURE MESSAGES
	JRST	DETUSR
DALOO2:	CAME	J,CURJOB	;MAKE DEFENSIVE CHECK
	WARN	<J/COMJOB mismatch in DALOKJ loop>,OPR
	SOJG	J,DALOO1	;LOOP FOR ALL JOBS IN SYSTEM
	TLZE	F,L.ANY		;SKIP IF NO JOBS FOUND
	JRST	DALOOK		;DID SOMETHING, GO THROUGH AGAIN
;HERE TO CHECK FOR EXPIRED CLOCK REQUESTS

	SKIPN	T2,NEXTUP	;GET THE NEXT JOB TO WAKE
	JRST	ZZZ		;THERE WAS NONE
	PUSHJ	P,GETUPT	;GET THE UPTIME IN TICKS
	SUB	T2,T1		;GET THE DIFFERENCE
	JUMPLE	T2,WAKJOB	;WAKE ANY JOBS THAT NEED IT,
				; AND RESCAN FROM DALOOK
	IMUL	T2,MILJIF	;CONVERT TO MILLISECONDS
	TLNE	T2,-1		;IS LEFT HALF EMPTY?
	MOVEI	T2,-1		;NO, SET MAX SLEEP TIME

ZZZ:	MOVEI	T1,SLPTIM	;TIME TO SLEEP
	HRLI	T2,(HB.RWJ!HB.RTL)	;ONLY MONITOR CAN WAKE US
	SKIPE	.JBDDT		;RUNNING WITH DDT (READ ATTACHED)	[775]
	CLRBFI			;YES, CLEAR OUT INPUT BUFFER		[775]
				; THIS NOT-AT-ALL OBVIOUS CONDITION IS
				; BECAUSE OTHERWISE THE HIBER WILL (IN-
				; CORRECTLY) THINK INPUT IS PENDING AND
				; CONSTANTLY WAKE US UP!!!!
	HIBER	T2,		;HIBERNATE
	  SLEEP	T1,		;ON SECOND THOUGHT SLEEP
	PUSHJ	P,NBSDAT	;GET CURRENT DATE/TIME
	MOVEM	T1,ZZZDTM	;STORE FOR DEBUG
	JRST	DALOOK		;NOW LOOK AGAIN
DAEREN:	MOVEM	17,CRSHAC+17	;SAVE AC 17
	MOVEI	17,CRSHAC	;MAKE BLT POINTER
	BLT	17,CRSHAC+16	;SAVE THE REST
	MOVE	T1,.JBOPC	;GET CRASH PC FROM .REENTER COMMAND
	MOVEM	T1,CRSHPC	;AND SAVE IT AWAY
	SETZM	.JBINT		;DO NOT INTERCEPT EXIT (ESTOP2 CODE)
	MOVSI	T1,(SIXBIT /DAE/)	;**[103] MAKE NAME DAE
	SETNAM	T1,		;CHANGE THE NAME TO CHEAT THE SYSTEM
	RESET			;KILL PSI INTERRUPTS IF ANY
	TLNN	F,L.INI		;DON'T ISSUE MESSAGE IF INITIALIZING
	OUTSTR	[ASCIZ/
[DAEMON shut down: please SAVE, then start a new DAEMON]
/]
	EXIT			;FORGET THE THING
;HERE TO PROCESS JOB ISSUING DAEMON UUO

;FOR DAEMON UUO'S:
;P1=USER LOCATION EXECT1=EXEC AC T1, AND CONTAINS FUNCTION REQUESTED
;P2=USER LOCATION EXECT2=EXEC AC T2, AND CONTAINS +LENGTH,ADDR OF ARGUMENT LIST

;DAEMON RETURNS BY STORING AN ERROR CODE (OR THE ORIGINAL CONTENTS OF EXECT2)
;IN EXECT1 (NOTE, T1), WHICH WILL BE PASSED BACK TO THE USER IN HIS
;CALLING REGISTER, AND 0 IN EXECT2 FOR A SUCCESSFUL (SKIP) RETURN,
;OR NON-ZERO IN EXECT2 FOR A NON-SKIP RETURN.

UUSERV:	SETZM	EVAADR		;CLEAR FUNNY SPACE ADDRESS
	MOVEM	J,UUOJOB	;SAVE JOB NUMBER FOR DEBUGGING
	TLO	F,L.ANY		;SOMETHING HAPPENED, A UUO
	MOVEI	T1,DETUSR	;NEW RESTART ADDRESS
	MOVEM	T1,INTADR
	MOVSI	T1,.JBDA	;GET JOB DATA AREA
	PUSHJ	P,GTUCOR	;GET JOB'S JOB DATA AREA
	  JRST	UUERR2		;ADDRESS CHECK
	MOVE	T1,EXECT1	;GET ADDRESS IN JOBDAT OF EXEC T1
	MOVE	P1,USRCOR(T1)	 ;EXEC AC T1=FUNCTION REQUESTED
	MOVEM	P1,UUOFNC	;SAVE FUNCTION FOR DEBUGGING
	JUMPLE	P1,UUERR1	;- OR 0 IS ILLEGAL
	CAILE	P1,MAXFNC	;SKIP IF LEGAL FUNCTION
	JRST	UUERR1		;TOO BIG
	MOVE	P2,USRCOR+1(T1) ;P2=XWD
	MOVEM	P2,SAVXWD	;SAVE XWD
	JRST	@FNCADR-1(P1)	;CALL ROUTINE TO PROCESS REQUEST

FNCADR:
	EXP	DMPUUO		;1  .DCORE  WRITE A DCORE FILE
	EXP	CLKUUO		;2  .CLOCK  WAKE A JOB IN THE FUTURE
	EXP	FCTUUO		;3  .FACT   APPEND TO FACT FILE
	EXP	UUERR1		;4  .DMQUE  RESERVED FOR QUEUE FUNCTION
	EXP	EAPUUO		;5  .DMERR  APPEND TO ERROR.SYS
MAXFNC==.-FNCADR
;HERE FOR .CLOCK FUNCTION
;CALL:
;	MOVE	AC,[XWD ARGLEN,ARGLST]
;	DAEMON	AC,
;	  ERROR RETURN
;	GOOD RETURN
;ARGLST:2	;.CLOCK
;	# SECS BEFORE WAKE ;OVERRIDES ANY PENDING REQUEST,
;			; 0 IMPLIES IMMEDIATE WAKE

CLKUUO:	MOVE	T1,P2		;T1=XWD
	TLNN	T1,-1		;LENGTH GIVEN?
	HRLI	T1,2		;NO, DEFAULT TO 2
	HLRZ	T2,T1		;COPY THE LENGTH
	CAILE	T2,2		;NEED 2 ARGS, 0 IMPLIES 2
	JRST	UUERR3		;WRONG NUMBER OF ARGUMENTS
	PUSHJ	P,GTUCOR	;READ HIS CORE IMAGE
	  JRST	UUERR2		;ADDRESS CHECK
	MOVE	T1,USRCOR+1	;GET THE TIME
	PUSHJ	P,CLKREQ	;PUT IN QUEUE
	JRST	UUOKX		;EXIT
;HERE FOR APPEND TO ERROR.SYS FUNCTION

;CALL:
;	MOVE AC,[XWD ARGLEN,ARGLST]
;	DAEMON	AC,
;	  ERROR RETURN
;	GOOD RETURN
;ARGLST:5	;.DMERR
;	CODE		;CODE FOR ENTRY IN ERROR FILE
;	BODY		;BLOCK OF ARGLEN-2 WORDS TO PUT IN ERROR FILE

EAPUUO:	HRROI	T1,.GTSTS	;GET JOB'S STATUS
	PUSHJ	P,GTBSPY	;GO READ THE MONITOR
	  JRST	UUERR4		;YOU CAN'T WIN THEM ALL
	MOVE	T2,T1		;SAVE THE WORD
	HRROI	T1,.GTPRV	;GET PRIVILEGE WORD
	PUSHJ	P,GTBSPY	;GET IT
	  JRST	UUERR4		;SHOULDN'T HAPPEN
	TLNN	T1,(JP.POK)	;POKE PRIVILEGE?
	TLNE	T2,JACCT	;PRIVLEGED?
	JRST	EAPUU1		;YES--WIN
	HRROI	T1,.GTPPN	;SETUP JBTPPN INDEX
	PUSHJ	P,GTBSPY	;READ JOB'S PPN
	  SETZ	T1,		;CAN'T
	CAME	T1,OPRPPN	;ARE YOU A GOOD GUY?
	JRST	UUERR6		;NO--GET LOST
EAPUU1:	MOVE	T1,P2		;GET XWD INTO T1
	HLRE	T2,T1		;GET LENGTH INTO T2
	JUMPLE	T2,UUERR3	;WRONG NUMBER  OF ARGS
	CAIN	T2,1		;ONLY 1 ARG (NULL ENTRY)?
	  JRST	UUOKX		;YES, GOOD RETURN BUT DO NOTHING
	CAILE	T2,MAXUCR	;ARG BIGGER THAN OUR BUFFER?
	  JRST	UUERR3		;YES, WRONG NUMBER OF ARGS
	PUSHJ	P,GTUCOR	;GO READ HIS CORE
	  JRST	UUERR2		;ADDRESS CHECK
	MOVE	T1,USRCOR+1	;GET CODE FOR ERROR FILE INTO T1
	LSH	T1,^D27		;AND SHIFT IT TO GOOD PLACE
	PUSHJ	P,ERRINB	;GO PUT HEADER IN BUFFER
	HLRZ	T2,P2		;RESTORE LENGTH OF ARG BLOCK
	SUBI	T2,2		;MAKE T2 NUMBER OF DATA WORDS OF ENTRY
	JUMPE	T2,EAPUU2	;SKIP ON IF NO DATA
	HRLS	T2		;PUT IT INTO BOTH HALVES
	HRRZI	T1,1(P4)	;GET FIRST WORD TO TRANSFER TO INTO  T1
	HRLI	T1,USRCOR+2	;AND FROM INTO LH
	ADD	P4,T2		;UPDATE PUSHDOWN PONTER TO ERROR BUFFER
	JUMPG	P4,UUERR3	;CHECK FOR OVERFLOW OF IOWD
	BLT	T1,(P4)		;AND BLT STUFF TO BUFFER
EAPUU2:	PUSHJ	P,ERRFIN	;WRITE ENTRY AND CLOSE FILE
	JRST	UUOKX		;GIVE GOOD RETURN
UUERR6:	MOVEI	T1,DMNPV%	;NO PRIVS TO DO THAT UUO
	JRST	UUERRX		;EXIT

UUERR4:	MOVEI	T1,DMSNH%	;SHOULD NEVER HAPPEN
	JRST	UUERRX		; BUT...

UUERR3:	MOVEI	T1,DMWNA%	;WRONG NUMBER OF ARGS
	JRST	UUERRX

UUERR2:	MOVEI	T1,DMACK%	;ADDRESS CHECK
	JRST	UUERRX		;ERROR EXIT

DMPUUO:!
FCTUUO:!
UUERR1:	MOVEI	T1,DMILF%	;ILLEGAL FUNCTION ERROR CODE
;	JRST	UUERRX		;FALL INTO UUERRX


;HERE FOR DAEMON UUO ERRORS, T1=ERROR CODE

UUERRX:	SETOM	USRCOR+1	;NOTE ERROR IN FUNCTION
	MOVE	P,PDL		;RESTORE P, SINCE ERROR MAY HAVE BEEN
				;AT ANY LEVEL ON THE STACK
	JRST	UUEXIT		;AND EXIT UUO

;HERE WHEN FUNCTION COMPELETED PROPERLY, T1=RETURN CODE

UUOKX:	SETZM	USRCOR+1	;NOTE FUNCTION COMPLETED PROPERLY
	MOVE	T1,SAVXWD	;GET OLD CONTENTS OF USERS REG
UUEXIT:	MOVEM	T1,USRCOR	;STORE CODE TO RETURN TO USER
	MOVEM	T1,UUXCOD	;SAVE FOR DEBUGGING
	MOVEM	J,UUXJOB	;  ALONG WITH THE JOB NUMBER
	MOVSI	T1,2		;WRITE 2 WORDS
	HRR	T1,EXECT1	;AT EXEC T1 IN USER'S JOBDAT
	AOSG	WTUERR		;CHECK FOR RECURSION IN EXIT PROCESS
	PUSHJ	P,WTUCOR	;WRITE USER'S CORE
	SETOM	WTUERR		;RESET FLAG
	JRST	DETUSR		;AND FINISH USER'S REQUEST
;HERE TO UUO USER OUT OF DAEMON REQUEST

DETUSR:	MOVEI	T1,DALOOK	;NEW RESTART ADDR
	MOVEM	T1,INTADR
	MOVEM	J,DFNJOB	;SAVE JOB NUMBER FOR DEBUGGING
	MOVEI	T1,J		;T1=ADDRESS OF ARGUMENT LIST=JOB NUMBER
	DAEFIN	T1,		;ALL DONE, REQUE USER'S JOB
	  AOS	FINGUF		;DEBUG - NOTE AN SNH
	JRST	NULEXT		;FINISH UP
;HERE TO RESET SEARCH LIST TO ORIGINAL STATE

	NULEXT=DALOO2	;IF DON'T CARE ABOUT SEARCH LIST, FORGET THIS
	SUBTTL ERRORS

E.GTBF:	ERROR	<Necessary GETTAB failed>,STOP

FBMTRY:	SOJLE	P1,CPOPJ1	;EXIT IF FINISHED RETRYING
	CAIE	T1,ERFBM%	;SKIP IF FILE BEING MODIFIED
	JRST	CPOPJ1		;NO, SOME OTHER ERROR
	MOVEI	T1,1
	SLEEP	T1,
	POPJ	P,

TPOPJ1:	AOS	-1(P)		;BUMP RETURN
TPOPJ:	POP	P,T1		;YES, RESTORE T1
	POPJ	P,		;RETURN

OCRLF:	TLO	F,L.OPR		;FORCE MESSAGE TO OPR
	PUSHJ	P,PCRLF		;OUTPUT THE CRLF
	TLZ	F,L.OPR		;CLEAR THE BIT
	POPJ	P,		;RETURN

PCRLF:	MOVEI	SC,.CHCRT	;GET A CARRIAGE RETURN
	PUSHJ	P,TYPCHR	;TYPE IT
	MOVEI	SC,.CHLFD	;GET A LINE FEED
	PJRST	TYPCHR		;TYPE IT AND RETURN
;ROUTINE TO PROCESS THE ERROR, WARN, AND TELL MACROS.
;CALL	PUSHJ	P,ERRMSG
;	CAI	BITS,[PREFIX,[MESSAGE]]
;	RETURN HERE UNLESS ER.STP IS SET

ERRMSG:	PUSH	P,T1		;SAVE ALL THE AC'S THAT
	PUSH	P,SC		;WE USE IN THIS ROUTINE
	PUSH	P,P1		;...
	MOVE	P1,@-3(P)	;GET CAI WORD
	TLNE	P1,(ER.STP!ER.OPR) ;WANT THIS MESSAGE ISSUED TO OPR?
	TLO	F,L.OPR		;YES, TELL TYPCHR
	HLRZ	SC,(P1)		;GET MESSAGE PREFIX
	SKIPE	SC		;ANY THERE?
	PUSHJ	P,TYPCHR	;YES, TYPE IT
	HRRZ	T1,(P1)		;GET MESSAGE ADDRESS
	PUSHJ	P,TYPSTR	;TYPE IT
	TLNN	P1,(ER.NCR)	;WANT CRLF AT END OF THIS MESSAGE?
	PUSHJ	P,PCRLF		;YES, ADD ONE
	TLZ	F,L.OPR		;CLEAR OPR MESSAGE FLAG
	TLNE	P1,(ER.STP)	;STOP ON THIS ERROR?
	PJRST	DAEREN		;YES, TAKE IT DOWN
	POP	P,P1		;RESTORE THE AC'S SAVED ON ENTRY
	POP	P,SC		;...
	JRST	TPOPJ1		;AND RETURN
SUBTTL SUBROUTINES TO WRITE USER CORE

;SUBROUTINE TO WRITE IN USER CORE TO STORE VALUES AT THE END OF
;A DAEMON UUO.  MAKES NO ADDRESS CHECKS SINCE JOBPEK WILL DO THAT
;FOR US.
;ARGS	T1=+LENGTH,,ADDR IN JOB
;	J=JOB NUMBER
;	USRCOR CONTAINS CORE TO BE WRITTEN OUT

WTUCOR:	MOVSI	T3,<(JK.WRT)>(J) ;JOB NUMBER AND WRITE FLAG TO LH
	HLR	T3,T1		;NUMBER OF WORDS TO RH
	HRRZ	T4,T1		;DESTINATION ADDRESS IN RH OF T4
	HRLI	T4,USRCOR	;SOURCE IS USRCOR
	MOVEI	T2,T3		;SETUP ADDRESS OF JOBPEK BLOCK
	PUSHJ	P,DOJBPK	;DO THE JOBPEK TO WRITE THE CORE
	  POPJ	P,		;JOBPEK SUCCEEDED
	  JFCL			;ADDRESS CHECK, SHOULDN'T HAPPEN HERE
	ERROR	<Error returning values from DAEMON UUO>,OPR,JPKUER


;HERE IF THE JOBPEK FAILED FOR A READ OR WRITE DONE AS THE RESULT OF
;A DAEMON UUO.  DUMP THE UUO FUNCTION AND XWD TO MAKE IT EASIER TO
;DEBUG

JPKUER:	TELL	<UUOFNC=>,NOCRLF ;INDICATE THAT THIS IS THE FUNCTION
	MOVE	T1,UUOFNC	;GET THE UUO FUNCTION CODE
	PUSHJ	P,THALF		;TYPE IT
	TELL	<, SAVXWD=>,NOCRLF ;NOW THE XWD
	MOVE	T1,SAVXWD	;GET THE UUO XWD
	PUSHJ	P,THALF		;TYPE IT
	PJRST	OCRLF		;END THE LINE AND RETURN
SUBTTL SUBROUTINES TO READ USER CORE

;SUBROUTINE TO GET CORE FROM USER'S JOB - MAKES NO ADDRESS CHECKS SINCE
;JOBPEK WILL DO THAT FOR US.
;ARGS:	T1=+LENGTH,ADDRESS IN USER'S JOB
;	J=JOB NUMBER
;RETURNS CPOPJ IF COULDN'T GET DATA (MESSAGE ISSUED IF NECESSARY)
;	 CPOPJ1 IF DATA IS IN USRCOR

GTUCOR:	SETZM	USRCOR		;ZERO FIRST WORD OF BUFFER
	MOVE	T3,[USRCOR,,USRCOR+1] ;MAKE BLT POINTER
	BLT	T3,USRCOR+MAXUCR-1 ;CLEAR THE BUFFER
	HLRZ	T3,T1		;PUT LENGTH INTO RH OF T3
	HRL	T3,J		;AND JOB NUMBER INTO LH
	HRLZ	T4,T1		;PUT READ ADDRESS INTO LH OF T4
	HRRI	T4,USRCOR	;PLUS ADDRESS OF OUR BUFFER IN RH
	MOVEI	T2,T3		;POINT TO ARG BLOCK
	PUSHJ	P,DOJBPK	;TRANSFER THE DATA
	  JRST	CPOPJ1		;GOOD RETURN
	  POPJ	P,		;ADDRESS CHECK
	ERROR	<Error reading arguments for DAEMON UUO>,OPR,JPKUER
GTBDFL:	PUSHJ	P,GTBSPY
	  SETZ	T1,
	POPJ	P,
;SUBROUTINE TO PERFORM A JOBPEK UUO AND CHECK THE ERROR CODE IF
;THE UUO TAKES THE ERROR RETURN.  FOR READS, THE CALLER MUST HAVE
;ZEROED THE BUFFER BEFORE THE CALL SINCE THIS ROUTINE GIVES A
;SUCCESSFUL RETURN ON JKABZ% (ALLOCATED BUT ZERO) ERRORS.
;CALL:	MOVE	T2,JOBPEK ARG
;	PUSHJ	P,DOJBPK
;RETURNS CPOPJ IF THE JOBPEK SUCCEEDED WITH THE DATA TRANSFERRED
;	 CPOPJ1 IF THE JOBPEK CAUSED AN ILLEGAL ADDRESS ERROR
;		(JKIAD%)
;	 CPOPJ2 IF THE JOBPEK CAUSED ANY OTHER ERROR WITH AN APPROPRIATE
;		ERROR MESSAGE ISSUED
;RESPECTS T1

DOJBPK:	PUSHJ	P,SAVE4		;GET SOME AC'S TO USE
	PUSH	P,0(T2)		;HAVE TO MOVE THE ARGS TO P1 AND P2 VIA
	PUSH	P,1(T2)		; THE STACK SINCE WE DON'T KNOW WHERE
	POP	P,P2		; THE ARGS ARE (THEY MAY BE P1 AND
	POP	P,P1		; P2
	MOVEI	P3,(P1)		;SET P3:=WORDS LEFT TO DO

DOJBP1:	HRRZ	T2,P2		;ASSUME WRITING, GET WRITE ADDRESS
	TXNN	P1,JK.WRT	;ARE WE WRITING USER CORE?
	HLRZ	T2,P2		;NO, GET READ ADDRESS
	MOVEI	P4,(T2)		;COPY THE USER ADDRESS TO P4
	TRO	P4,PG.BDY	;INCREASE TO LAST WORD IN PAGE
	SUBI	P4,-1(T2)	;COMPUTE NUMBER OF WORDS LEFT IN PAGE
	CAIGE	P4,(P3)		;DO WE HAVE MORE THAN WE NEED?
	HRRM	P4,P1		;NO, USE WHAT WE HAVE
	SUBI	P3,(P1)		;ACCOUNT FOR THOSE THAT WE CAN DO

	MOVEI	T2,P1		;GET ADDRESS OF OUR JOBPEK BLOCK
	JOBPEK	T2,		;TRANSFER THE DATA
	  JRST	DOJBP3		;FAILED, ANALYZE THE ERROR
DOJBP2:	JUMPLE	P3,CPOPJ	;RETURN IF ALL WORDS ARE DONE
	MOVEI	P4,(P1)		;GET THE NUMBER OF WORDS WE DID
	HRLS	P4		;DUPLICATE IN BOTH HALVES
	ADD	P2,P4		;STEP THE ADDRESSES FOR THOSE THAT WE DID
	HRRM	P3,P1		;TRY FOR THE REST THIS TIME
	JRST	DOJBP1		;TRY FOR MORE

DOJBP3:	CAIN	T2,JKABZ%	;WAS THE PAGE ALLOCATED BUT ZERO?
	JRST	DOJBP2		;YES, THAT'S OK.
	CAIN	T2,JKIAD%	;ILLEGAL ADDRESS?
	JRST	CPOPJ1		;YES, WE EXPECT THAT ONE
	AOS	0(P)		;SETUP FOR CPOPJ2 RETURN


;CONTINUED ON THE NEXT PAGE
;CONTINUED FROM THE PREVIOUS PAGE


;HERE IF THE JOBPEK FAILED WITH A CODE THAT WE DON'T EXPECT.  PRINT
;EVERYTHING WE KNOW ABOUT THE FAILURE IN THE HOPE THAT SOMEBODY CAN
;DEBUG IT.

	WARN	<JOBPEK failed with unexpected error code>,OPR
	TELL	<Code=>,NOCRLF	;DUMP WHAT WE KNOW ABOUT THE ERROR
	MOVE	T1,T2		;GET THE ERROR CODE
	PUSHJ	P,TOCT		;TYPE IT

	TELL	<, JOBPEK wd1=>,NOCRLF
	MOVE	T1,P1		;GET FIRST WORD OF ARG BLOCK
	PUSHJ	P,THALF		;TYPE IT

	TELL	<, JOBPEK wd2=>,NOCRLF
	MOVE	T1,P2		;GET SECOND WORD OR ARG BLOCK
	PUSHJ	P,THALF		;TYPE IT
	PUSHJ	P,OCRLF		;END THE LINE AND PUT IT OUT

	TELL	<Job=>,NOCRLF
	MOVE	T1,J		;JOB NUMBER
	PUSHJ	P,TDEC		;...

	TELL	<, JBTSTS=>,NOCRLF
	HRROI	T1,.GTSTS	;GETTAB FOR JBTSTS
	PUSHJ	P,GTBDFL	;GET THE VALUE
	PUSHJ	P,THALF		;TYPE IT

	TELL	<, JBTADR=>,NOCRLF
	HRROI	T1,.GTADR	;GETTAB FOR JBTADR
	PUSHJ	P,GTBDFL	;GET THE VALUE
	PUSHJ	P,THALF		;TYPE IT
	PUSHJ	P,OCRLF		;END THE LINE
	JRST	CPOPJ1
;ROUTINE TO RESTART AFTER ERROR INTERCEPT
;ARGS	INTRES CONTAINS ADDRESS TO RESTART AT

INTRES:	SKIPE	.JBBPT		;IS DDT PROBABLY LOADED?
	JSR	@.JBBPT		;YEAH, CALL IT IN THEN
	PUSH	P,INTBLK+2	;SAVE INT PC
	PUSH	P,T1		;SAVE AC FOR INT ROUTINE
	SETZB	T1,INTBLK+2	;AND ENABLE INTERRUPTS AGAIN
	EXCH	T1,INTBLK+3	;GRAB & CLEAR REASONS
	MOVEM	T1,INTFLG	;SAVE FOR DEBUG
	MOVE	T1,-1(P)	;GET PC
	TLZ	F,L.OPR!L.QUE	;MAKE SURE OPR MESSAGE BIT IS CLEAR
	MOVEM	T1,INTPC	;SAVE PC FOR DEBUG
	GETLIN	T1,		;GET LINE NUMBER
	TLNN	F,L.INI		;ERROR DURING INITIALIZATION?
	TLNN	T1,-1		;ATTACHED?
	JRST	REGO		;NO--RESTART
	SETO	T1,		;INDICATE CURRENT LINE "7-53"
	GETLCH	T1		;GET LINE NUMBER
	SKIPE	.JBDDT		;GOT DDT LOADED?
	JRST	REGO1		;YES, DON'T DETATCH
	HRLZI	T1,(T1)		;LH=LINE, RH=DETACH
	ATTACH	T1,		;DO A DET
	  JFCL			;WE TRIED
REGO:
REGO1:	AOS	T1,INTGUF	;COUNT THE INTERCEPTS
	CAIL	T1,MAXINT	;TOO MANY?
	  ERROR	<Error intercept count exceeded>,STOP
	HRRZ	T1,-1(P)	;RETURN WITH PC IN T1
	MOVE	P,PDL		;RESET PUSH DOWN POINTER
	JRST	@INTADR
;COME HERE ON INTERCEPT DURING STARTUP

E.EINT:	PUSHJ	P,TYPIPC	;TYPE MESSAGE AND PC
	PUSHJ	P,OCRLF		;END LINE
	JRST	DAEREN		;QUIT

;HERE TO TYPE MESSAGE AND PC FOR ERROR DURING STARTUP

TYPIPC:	ERROR	<Error intercept @PC >,NOCRLF
	PJRST	TOCT		;PRINT IN OCTAL
;SUBROUTINE TO PERFORM GETTAB VIA SPY IF POSSIBLE
;ARGS	T1=GETTAB ARG
;VALUES	T1=GETTAB VALUE
;THERE IS NO NEED TO SUPPORT -2 ITEM (JOBS HIGH SEG)

GTBSPY:	PUSHJ	P,SAVE4		;SAVE P1-P4
	MOVEI	P1,(T1)		;P1=TABLE NUMBER
	CAMLE	P1,MAXGTB	;SKIP IF LEGAL TABLE
	JRST	GTBGTB		;NO, TRY GETTAB ANYWAY
	ADD	P1,GTBST	;ADDR OF PTR FOR THIS TABLE
	LDB	P3,[POINT 3,0(P1),11] ;GET TYPE OF TABLE
	CAIE	P3,.SLIXI	;RANDOM TABLE?
	CAIN	P3,.SLIXR	; OR RANGED GETTAB?
	JRST	GTBSP2		;YES, LH=-1 IS NOT OUR JOB
	JUMPGE	T1,GTBSP2	;IF LH IS NEGATIVE, MAKE INDEX
	HRLI	T1,(J)		;  JOB IN J
GTBSP2:	CAIN	P3,.SLIXP	;IS DATUM IN THE PDB?
	JRST	GTBPDB		;YES, GO GET IT
	CAIN	P3,.SLIXS	;INDEXED BY JOB OR SEGMENT NUMBER?
	SKIPA	P2,JBTMXL	;YES, MAX IS JOBN+SEGN-1
	LDB	P2,[POINT 9,0(P1),8] ;NO. GET MAX ITEM VALUE
	CAIE	P3,.SLIXR	;IS THIS A RANGED GETTAB?
	JRST	GTBSP1		;NO, P2 CONTAINS 0,,MAX
	SKIPN	P3,RNGTAB	;DO WE HAVE THE ADDRESS OF RNGTAB?
	JRST	GTBGTB		;NO, USE A GETTAB
	ADDI	P2,(P3)		;ADD OFFSET INTO RNGTAB FOR THIS ENTRY
	MOVE	P2,400000(P2)	;GET MIN,,MAX FROM RNGTAB ENTRY
GTBSP1:	HLRE	P3,T1		;GET ITEM NUMBER CALLER WANTS
	HRRE	P4,P2		;GET MAX VALUE OF TABLE
	CAMLE	P3,P4		;.LE. MAX?
	JRST	GTBGTB		;NO, TRY A GETTAB
	HLRE	P4,P2		;KEEP JUST MIN
	CAMGE	P3,P4		;AND .GE. MIN?
	JRST	GTBGTB		;NO, TRY A GETTAB
	MOVE	T1,@(P1)	;GET VALUE
	JRST	CPOPJ1		;GOOD RETURN

GTBPDB:	SKIPN	PDBPTR		;IS JBTPDB BASE AVAILABLE?
	JRST	GTBGTB		;NO. USE A GETTAB
	HLRZS	T1		;JOB NUMBER
	ADD	T1,PDBPTR	;PLUS JBTPDB ADDRESS
	MOVE	T1,400000(T1)	;GET ADDRESS OF WD 0 OF JOBS PDB
	JUMPE	T1,CPOPJ1	;RETURN ZERO IF NO PDB
	ADD	T1,(P1)		;ADD ON SPY OFFSET AND ITEM NUMBER
				; WHICH WERE BOTH IN GTBST TABLE
	MOVE	T1,(T1)		;GET THE WORD FROM THE PDB
	JRST	CPOPJ1		;GIVE GOOD RETURN

GTBGTB:	GETTAB	T1,		;TRY GETTAB
	  POPJ	P,
	AOS	GTBGUF		;DEBUG--COUNT TIMES WE THOUGHT GETTAB
				; WOULD LOSE BUT IT DIDNT
	JRST	CPOPJ1		;WINS
;SUBROUTINE TO GET DATA FROM GETTAB SUB-TABLE
;ARGS	T3= GETTAB ARG FOR SUB-TABLE POINTER
;	T2= SUBTABLE ARG: TABLE# CONTAINING SUBTABLE,,ITEM #
;VALUE	T1= DESIRED ITEM
;SKIP RETURN IF ITEM DEFINED

GSUBTB:	PUSH	P,T3		;SAVE THAT ARG
	CAMN	T3,LSTSTP	;SAME SUBTABLE POINTER AS LAST TIME?
	JRST	[MOVE	T1,LSTSTV ;YES, GET LAST VALUE
		 JRST	GSUBT1]	  ;AND JOIN COMMON CODE
	MOVEM	T3,LSTSTP	;SAVE THIS SUBTABLE POINTER FOR NEXT TIME
	MOVE	T1,T3		;GET SUBTABLE POINTER
	PUSHJ	P,GTBSPY
	  JRST	GSUBTX		;TOO BAD, NO SUBTABLE
	MOVEM	T1,LSTSTV	;SAVE VALUE OF POINTER FOR QUICK CHECK
GSUBT1:	JUMPE	T1,GSUBTX	;NO SUBTABLE EXISTS
	LDB	T3,[POINT 9,T1,8] ;GET UPPER LIMIT ON SUBTABLE
	CAIGE	T3,0(T2)	;COMPARE DESIRED ITEM #
	JRST	GSUBTX		;OOPS, OFF THE END
	HRRZS	T1		;SUBTABLE POINTER IS AN ITEM NUMBER
	ADD	T1,T2		;WITH SUB-ITEM AND TABLE
	MOVSS	T1		;SWAP TO GETTAB FORM
	PUSHJ	P,GTBSPY	;GETTAB THAT
	  JRST	GSUBTX		;LOSE
	AOS	-1(P)		;WIN, TAKE SKIP RETURN
GSUBTX:	POP	P,T3		;RESTORE T3
	POPJ	P,


;SUBROUTINE TO READ MONITOR LOW SEGMENT VIA SPY OR PEEK.  ALSO HANDLES
;PER-PROCESS ADDRESSES FOR THE CURRENT JOB (C(EVAPPB) THRU C(EVAPPE)-1)
;ARGS	T1=ADDR OF LOCATION DESIRED
;VALUES	T1=CONTENTS OF LOCATION

PEKSPY:	TLNN	T1,777776	;IS IT SECTION 0 OR 1?
	TLZ	T1,-1		;YES, MAP SECTION 1 TO 0

	CAML	T1,EVAPPB	;IN FUNNY SPACE FOR THIS
	CAML	T1,EVAPPE	;  JOB?
	CAIA			;NO
	PJRST	PEKUSR		;YES, GO GET IT

	TDNN	T1,[-1,,400000] ;IF NOT IN MONITOR LOW SEG, DO PEEK
	SKIPA	T1,400000(T1)	;OK, GET CONTENTS OF LOC
	PEEK	T1,		;CANT, TRY PEEK
	POPJ	P,
;SUBROUTINE TO READ A WORD FROM THE PER-PROCESS AREA FOR A JOB.
;ARGS	T1=EXEC VIRTUAL ADDRESS MAPPED THROUGH JOB'S UPMP
;	   IN THE RANGE C(EVAPPB) THRU C(EVAPPE)-1
;	J=JOB NUMBER
;VALUES	T1=CONTENTS OF LOCATION OR 0 IF ERROR

PEKUSR:	PUSHJ	P,SAVE2		;SAVE P1-P2
	MOVE	P1,EVAADR	;GET ADDR OF FIRST WORD IN CORE
	CAIL	T1,(P1)		;IF WORD WE WANT IS ALREADY IN
	CAILE	T1,EVASIZ-1(P1) ;  CORE, JUST PICK IT UP
	CAIA			;NO, MUST READ IT
	JRST	PEKUS2		;GET IT FROM CORE
	SETZM	EVAADR		;FORCE READ NEXT TIME
	MOVSI	P1,<(JK.EVA)>(J) ;JOB NUMBER+READ EVA FLAG TO LH
	HRRI	P1,EVASIZ	;NUMBER WORDS TO READ TO RH
	MOVSI	P2,(T1)		;ADDRESS TO READ IN LH
	TLZ	P2,EVASIZ-1	;MOVE BACK TO BUFFER BOUNDARY
	HRRI	P2,EVABUF	;ADDR IN OUR CORE TO RH
	PUSH	P,T2		;SAVE T2 (USE T2 BECAUSE OF DOJBPK)
	MOVEI	T2,P1		;POINT TO BLOCK
	PUSHJ	P,DOJBPK	;READ IT
	  JRST	PEKUS1		;GOT THE DATA, GO GET IT
	  JFCL			;ADDRESS CHECK
	MOVEI	T1,0		;ON ERROR, RETURN 0
	POP	P,T2		;PRUNE THE STACK
	POPJ	P,		;GIVE ERROR RETURN
PEKUS1:	POP	P,T2		;RESTORE T2
	HLRZM	P2,EVAADR	;UPDATE FIRST ADDRESS IN CORE
PEKUS2:	SUB	T1,EVAADR	;COMPUTE OFFSET IN BLOCK
	MOVE	T1,EVABUF(T1)	;GET WORD FROM BUFFER
	POPJ	P,		;RETURN
SUBTTL	CLOCK QUEUE SUBROUTINES

;HERE WHEN A TIMER RUNS OUT
WAKJOB:	HRRE	J,NEXTJB	;GET THE JOB NUMBER
	SETZM	@JBTCLK		;CLEAR OUT ENTRY
	PUSHJ	P,CLEANQ	;CLEAN THE QUEUE
	JUMPLE	J,WAKSLF	;JOBS .LE. 0 ARE INTERNAL
	WAKE	J,		;WAKE THE JOB
	  AOS	WAKGUF		;WE TRIED
	JRST	DALOOK		;RESCAN THE QUEUE

WAKSLF:	PUSHJ	P,@CLKDSP(J)	;INVOKE INTERNAL ROUTINE
	JRST	DALOOK

	EXP	AVLSCN		;(-3) AVAIL.SYS UPDATE SCAN
	EXP	IOECLR		;(-2)CLEAR I/O ERROR ON ERROR.SYS
	EXP	CPOPJ		;(-1) FORCE OUT THE FACT BUFFER
CLKDSP:	EXP	CPOPJ		;(0) CHECKPOINT JOBS

AVLSCN:	PUSHJ	P,AVLAGE	;SEE IF TIME TO START A NEW AVAIL.SYS
	HRREI	J,AVLRQ		;SET FOR NEXT WAKEUP
	MOVEI	T1,AVLTIM	;FOR AVAIL.SYS SCAN
	PJRST	CLKREQ		;ENTER THE REQUEST AND RETURN
;SUBROUTINE TO ENTER A REQUEST INTO THE QUEUE
;ARGS	T1=NUMBER OF SEC TO SLEEP
;	J=JOB NUMBER
;USES T1-T4

CLKREQ:	JUMPE	T1,NULREQ	;NULL REQUEST - CLEAN Q AND WAKE IMMEDIATELY
	MUL	T1,JIFSEC	;CONVERT TO JIFFIES IN T2
	PUSHJ	P,GETUPT	;GET THE UPTIME IN TICKS
	ADD	T2,T1		;TIME TO GET UP
	MOVEM	T2,@JBTCLK	;STORE IN QUEUE
	PJRST	CLEANQ		;CLEAN THE QUEUE

NULREQ:	SETZM	@JBTCLK		;CLEAR THE REQUEST
	JUMPLE	J,CLEANQ	;NO WAKE IF INTERNAL REQUEST
	WAKE	J,		;WAKE THE JOB IMMEDIATELY
	  AOS	WAKGUF		;WE TRIED AND FAILED
;	PJRST	CLEANQ		;FALL THROUGH TO RE-SCAN Q

;HERE TO SORT THE QUEUE

CLEANQ:	SETZB	T2,NEXTUP	;FLAG QUEUE EMPTY
				; T2 GETS NEXT JOB TO WAKE
	PUSH	P,J		;SAVE AC J
	MOVE	J,QPNTR		;GET THE AOBJN POINTER
	HRLOI	T1,377777	;T1_UPTIME OF NEXT WAKE
FNDMIN:	SKIPE	T3,@JBTCLK	;GET FIRST TIME
	CAMG	T1,T3		;YES--IS IT BETTER
	JRST	NXTREQ		;NO--TRY NEXT
	MOVE	T1,T3		;T1_SHORTEST TIME
	HRREI	T2,(J)		;T2_JOB WITH THAT TIME
NXTREQ:	AOBJN	J,FNDMIN	;LOOP OVER ALL JOBS
	MOVEM	T1,NEXTUP	;SAVE T1
	MOVEM	T2,NEXTJB	;SAVE JOB NUMBER
	POP	P,J
	POPJ	P,
IOECLR:	SETZM	IOERR		;CLEAR FLAG SO ERRFIN RETRIES NOW
	POPJ	P,		;RETURN
;ROUTINE TO PROCESS SYSTEM ERROR BLOCKS

;NOTE THAT THE MONITOR ATTEMPTS TO BUILD A "STANDARD" FORMAT
;ERROR.SYS HEADER.  IF THE LENGTH THE MONITOR SUPPLIES DIFFERS
;FROM DAEMON'S IDEA A WARNING MESSAGE IS PRINTED ON THE CTY.

RSEBLK:
RSEBL1:	MOVE	T1,[XWD <ERAEND-ERABUF>,ERABUF] ;POINT AT BUFFER AREA
	SEBLK.	T1,		;ASK MONITOR FOR NEXT SYSTEM ERROR BLOCK
	  POPJ	P,		;NO MORE ERROR BLOCKS
	CAILE	T1,<ERAEND-ERABUF> ;DID THIS ONE GET TRUNCATED?
	WARN	<System error block was truncated>,OPR
	LDB	T1,[POINTR ERABUF+.EHTYP,EH.HLN] ;GET PURPORTED HEADER LENGTH
	CAXE	T1,EL.HED	;DOES MONITOR'S IDEA AND OURS AGREE?
	ERROR	<System error block length inconsistent>,OPR,RSEBL1
	PUSHJ	P,SEBLOG	;LOG THE SYSTEM ERROR BLOCK
	JRST	RSEBL1		;LOOP FOR ANY OTHER BLOCKS

;MINOR SUBROUTINE TO INSERT THE SYSTEM ERROR BLOCK IN ERABUF
;INTO THE ERROR.SYS FILE.

SEBLOG:	TLO	F,L.ANY		;NOTE WE GOT SOMETHING
	MOVE	T1,HEADR0	;GET THE STANDARD FIELD DEFINITIONS
	IOR	T1,ERABUF+.EHTYP ;MAKE THIS LOOK LIKE A GOOD ERROR ENTRY
	TXZE	T1,EH.AVL	;IF MONITOR WANTS IT TO GO TO AVAIL.SYS,
	TRO	F,R.AVL		;TELL ERRFIN TO COPY IT THERE
	TXZE	T1,EH.NER	;SUPPRESS WRITING INTO ERROR.SYS?
	TRO	F,R.NER		;YES
	MOVEM	T1,ERABUF+.EHTYP ;SET FINAL TYPE WORD
	AOS	T1,ERRSEQ	;INCREMENT SEQUENCE NUMBER
	MOVEM	T1,ERABUF+.EHSEQ ;(4) SEQUENCE NUMBER
	LDB	P4,[POINT 9,ERABUF+.EHTYP,35] ;GET MONITOR-SUPPLIED LENGTH
	ADDI	P4,ERABFS-1	;ACCOUNT FOR SUBTRACT AT ERFILL
	TRNN	F,R.NER		;SKIP ERROR.SYS?
	PJRST	ERRFIN		;STUFF THIS ENTRY IN ERROR.SYS AND RETURN
	PJRST	AVLFIN		;GO DO AVAIL THING
;SUBROUTINE TO APPEND TO ERROR.SYS
;ARGS	P4=LAST ADDR STORED IN ERRBUF
;BLOWS T1-3
;ALWAYS RETURNS CPOPJ

CRRFIN:	POP	P,(P)		;PRUNE STACK
ERRFIN:	SKIPE	IOERR		;CAN WE WRITE IN ERROR.SYS
	JRST	ERRFIX		;NO, CLEAR FLAGS AND RETURN
	MOVEM	P4,SAVEP4	;SAVE P4
	PUSHJ	P,FLSPAK	;FLUSH INPUT QUEUE AND CLEAR BLOCK
	TRZA	F,R.PAE		;INSURE THAT R.PAE IS NOT SET
AVLFIN:	TRO	F,R.PAE		;SET PROCESSING AVAIL.SYS FLAG
	TRZ	F,R.INF		;MAKE SURE THIS BIT IS OFF
INIFIN:	MOVX	T1,UU.PHS!.IODMP ;OPEN IN DUMP MODE
	MOVE	T2,CERDEV	;GET DEVICE
	MOVEI	T3,0		;NO BUFFERS
	OPEN	FCT,T1		;OPEN THE CHANNEL
	  JRST	E.IEF		;CAN'T INIT ERROR FILE
	MOVE	T1,CERDEV	;GET DEVICE WHERE ERROR.SYS LIVES
	MOVEM	T1,SSELBK+4	;INTO THE IPCF MSG
	MOVEI	T1,RBSIZE-RBCNTE ;LENGTH OF DESIRED EXTENDED LOOKUP
	MOVEM	T1,RBCNTE
	MOVE	T1,CERPPN	;GET PPN OF ERROR FILE
	MOVEM	T1,RBPPNE	;STORE DIRECTORY
	MOVEM	T1,SSELBK+7	;IN IPCF MSG TOO
	MOVE	T1,[SIXBIT/ERROR/]
	TRNE	F,R.PAE		;DOING AVAIL.SYS?
	MOVE	T1,[SIXBIT/AVAIL/] ;YES, CHANGE NAME
	MOVEM	T1,RBNAME	;STORE FILNAM
	MOVEM	T1,SSELBK+5	;IN IPCF PACKET TOO
	HRLZ	T1,ERREXT	;GET CURRENT ERROR EXTENSION
	TRNE	F,R.PAE		;DOING AVAIL.SYS?
	MOVSI	T1,'SYS'	;YES, EXTENSION IS ALWAYS SYS
	MOVEM	T1,RBEXTE	;STORE EXT
	MOVEM	T1,SSELBK+6	;IN IPCF TOO
	MOVSI	T1,(155B8)	;IF NOT FOUND, MAKE IT READ-ONLY
	MOVEM	T1,RBPRVE
	SETZM	RBSIZE		;IF LOOKUP FAILS, LEN IS ZERO
	MOVEI	T1,ERRBUF-1	;PREPARE IOWD
	HRRM	T1,ILIST	; FOR INPUT
	HRRM	T1,OLIST	; AND FOR OUTPUT
	MOVEI	T1,-200		;PUT WORD COUNT INTO OUTPUT
	HRLM	T1,OLIST	; IOWD FOR FULL BLOCK
	MOVEI	T1,-1		;MASK IN RIGHT HALF FOR ERROR CODE
	TXNN	F,R.INF		;DOING SEQUENCE NUMBER INITIALIZATION?
	JRST	INIFI1		;YES, DO NORMAL STUFF

	LOOKUP	FCT,EBLOCK	;DO EXTENDED LOOKUP
	  JRST	E.INF1		;FAILED
	JRST	INIFI2		;JUMP AROUND OTHER STUFF

INIFI1:	LOOKUP	FCT,EBLOCK	;DO EXTENDED LOOKUP
	  TDZN	T1,RBEXTE	;IF FILE NOT FOUND, MAKE ONE, ELSE BOMB
	ENTER	FCT,EBLOCK	;DO UPDATE
	  JRST	E.EEF		;WHAT?

INIFI2:	MOVE	T1,RBSIZE	;GET SIZE IN WORDS
	TRNE	F,R.PAE		;IF WE ARE DOING AVAIL.SYS AND
	CAIE	T1,BLKSIZ*AVLWRK ;THE SIZE IS THE LENGTH OF THE
	SKIPA			;WORKING ENTRY, JUST APPEND
	JRST	ERFIL5		;TO THE FILE
	SOJL	T1,ERFIL0	;DECR FOR LAST WORD
	ROT	T1,-B2WLSH	;COMPUTE LAST BLOCK
	MOVEI	T2,1(T1)	;START AT BLOCK 1
	MOVEM	T2,SSELBK+1	;PUT LAST BLOCK NUMBER IN IPCF PACKET
	USETI	FCT,(T2)	;READ LAST BLOCK
	ROT	T1,B2WLSH	;RESTORE WORD COUNT
	ANDI	T1,BLKSIZ-1	;RELATIVE LAST WORD IN BLOCK
	INPUT	FCT,ILIST	;READ THE LAST BLOCK
	STATZ	FCT,IO.ERR	;OK?
	  JRST	[TXNN	F,R.INF	;DOING SEQ NUMBER INITIALIZATION?
		 JRST	E.INEF	;OOPS
		 JRST	E.INF]	;YES
;HERE TO CHECK FOR MESS-UPS IN ERROR.SYS

	SKIPN	T3,ERRBUF(T1)	;PICK UP LAST WORD
	JRST	ERBAK		;IF 0, BACK UP TO FIND GOOD ENTRY
	LSH	T3,-^D27	;KEEP JUST TYPE CODE
	CAIN	T3,.ESEOF	;EOF MARKER?
	JRST	ERFEOF		;YES, INSERT NEW ENTRY OVER IT
;HERE, WE FOUND SOMETHING THAT WAS NOT AN EOF MARKER AT THE END
;OF THE FILE.  IT MIGHT BE A LEGITIMATE ENTRY HEADER IF WE
;CRASHED AFTER WRITING A NEW ENTRY, BUT BEFORE RECORDING NEW SIZE IN
;THE RIB.  IF IT "LOOKS LIKE" AN ENTRY HEADER, THEN CALCULATE WHERE
;THE END OF THE ENTRY SHOULD BE.  LOOK THERE FOR AN EOF.  IF THERE IS
;AN EOF MARK THERE, USE IT AS THE PROPER END OF FILE.
;
	CAIN	T3,.ESHIA	;IS IT AN OLD HIATUS MARK?
	JRST	ERPHIA		;YES, OVERWRITE IT WITH NEW ONE
	PUSH	P,T2		;SAVE BLOCK NUMBER
	PUSH	P,T4		;SUPERSTITION
	MOVE	T2,ERRBUF(T1)	;GET POSSIBLE ENTRY HEADER WORD
	LDB	T3,[POINTR T2,EH.HFM] ;GET HDR FMT
	LDB	T4,[POINTR T2,EH.HLN] ;  AND HDR LEN
	CAIL	T3,EL.HVR	;HEADER VERSION OK?
	CAIGE	T4,EL.HED	;LENGTH OK?
	JRST	ERPHI0		;NO, WRITE A HIATUS MARK
;
;IT LOOKS LIKE WE ARE ABOUT TO STEP ON A REAL ENTRY HEADER
;
	ANDI	T2,777		;KEEP BODY LENGTH
	ADD	T2,T4		;ADD HEADER LENGTH
	ADDI	T2,(T1)		;AND POINT TO WHERE NEXT ENTRY SHOULD START
	CAILE	T2,BLKSIZ-1	;IF IT'S NOT IN THE BLOCK WE READ
	JRST	ERPHI0		;THEN FORGET IT
	LDB	T3,[POINTR ERRBUF(T2),EH.TYP]	;GET ENTRY TYPE
	CAIE	T3,.ESEOF	;IF IT'S NOT AN EOF MARK
	JRST	ERPHI0		;THEN WE WERE WRONG, GO WRITE NORMAL HIATUS
	HRR	T1,T2		;WE WERE GOING TO WIPE A REAL ENTRY, SO
	POP	P,T4		;JUST POINT TO NEWLY FOUND EOF
	POP	P,T2
	JRST	ERFEOF		;AND PRETEND NOTHING HAPPENED
ERPHI0:	POP	P,T4
	POP	P,T2		;RESTORE BLOCK NUMBER
ERPHIA:	TXNE	F,R.INF		;DOING SEQUENCE NUBMER INITIALIZATION?
	JRST	E.INF		;YES, ERROR
	MOVSI	T3,.ESHIA_9	;MARK HIATUS
	MOVEM	T3,ERRBUF(T1)	;IN THE BUFFER
	USETO	FCT,(T2)	;WHEN WE MARK HAITUS,
	OUTPUT	FCT,OLIST	;WE USE NEW BLOCK
	STATZ	FCT,IO.ERR	;OK
	  JRST	E.OUEF		;NOPE!
	AOJA	T2,ERFIL3	;AND GO PUT THIS ENTRY INTO NEXT BLOCK

ERBAK:	JUMPE	T1,ERPHIA	;STOP BACKING AT BEGINNING OF BUFFER
	SOJ	T1,		;ELSE BACK UP
	SKIPN	T3,ERRBUF(T1)	;GET NEXT WORD
	JRST	ERBAK		;BACK UP AGAIN IF STILL ZERO
	LSH	T3,-^D27	;KEEP JUST TYPE CODE
	CAIE	T3,.ESEOF	;EOF MARKER?
	AOJA	T1,ERPHIA	;NO, DO NOT OVERWRITE IT
ERFEOF:	MOVEM	T1,SSELBK	;SAVE WC FOR IPCF PACKET
	TXNN	F,R.INF		;DOING SEQUENCE NUMBER INITIALIZATION?
	JRST	ERFILL		;NO, GO STORE OVER CODE
	MOVE	T1,ERRBUF(T1)	;GET THE EOF MARKER BACK
	TXZ	T1,EH.TYP	;KEEP JUST SEQUENCE NUMBER
	JUMPE	T1,E.INF	;MUST HAVE BEEN WRITTEN BY OLD DAEMON
	MOVEM	T1,ERRSEQ	;SETUP INITIAL SEQUENCE NUMBER
	JRST	RELFCT		;RELEASE CHANNEL AND RETURN

ERFIL0:	TRNE	F,R.PAE!R.INF	;THINGS ARE VERY BAD IF AVAIL.SYS DOESN'T
	JRST	ERRFIX		;EXIST, SO GIVE UP NOW
	SKIPA	T2,[1]		;SO USETO WILL WRITE FIRST BLOCK
ERFIL5:	MOVEI	T2,AVLWRK+1	;APPEND TO WORKING ENTRY IN AVAIL.SYS
ERFIL3:	SETZM	ERRBUF		;CLEAR FIRST WORD
	MOVE	T1,[ERRBUF,,ERRBUF+1]	;[534] SET UP TO CLEAR ANY JUNK
	BLT	T1,ERRBFE	;BLITTTT!
	MOVE	T1,[.ESOFF_9,,1];PUT POINTER TO FIRST ENTRY IN BLOCK
	MOVEM	T1,ERRBUF	;INTO FIRST WORD OF BLOCK
;	Here to write out the entry in one swell I/O command so we
;	don't end up writing a partial entry if the monitor crashes.
;	The following will only work on a KL or KS since it assumes
;	that the contents of a BLT pointer is known after the BLT
;	Also BEWARE heavy use of 18 bit address arithmetic which will
;	not work in non-zero section.
;
;	T1 has OFFSET to first word to write in ERRBUF
;	P4 has ADDRESS of last word written in ERABUF
;
ERFILL:	USETO	FCT,(T2)	;SET UP THE BLOCK NUMBER TO OUTPUT
	HRLI	T1,ERABUF	;STUFF SOURCE FIELD INTO BLT POINTER
	ADDI	T1,ERRBUF	;MAKE DESTINATION FIELD
	SUBI	P4,ERABFS-1	;P4 NOW HAS # OF WORDS OF DATA
	DPB	P4,[POINT 9,ERABUF,35] ;SAVE LENGTH IN HEADER
	ADDI	P4,EL.HED	;AND ACCOUNT FOR HEADER WORDS NOW
ERFIL1:	HRREI	T3,-1(P4)	;GET WORDS - 1
	JUMPL	T3,ERFIL2	;DONE IF NEGATIVE
	MOVEI	T2,-ERRBUF(T1)	;GET INDEX INTO ERRBUF
	ADD	T3,T2		;CALCULATE LAST WORD TO WRITE
	IORI	T2,BLKSIZ-1	;AND LAST WORD IN THIS BLOCK
	CAIL	T3,(T2)		;CAN WE WRITE IT ALL?
	  JRST	ESPLIT		;NOPE, SPLIT IT UP
	BLT	T1,ERRBUF(T3)	;WRITE THE REMAINING PART
ERFIL2:	MOVSI	T3,.ESEOF_9	;STUFF CODE
	IOR	T3,ERRSEQ	;INCLUDING SEQ NUMBER
	MOVEM	T3,(T1)		;TO MARK EOF
	MOVNI	T1,-<ERRBUF-1>(T1) ;CALCULATE HOW MANY WORDS WE WROTE
	HRLM	T1,OLIST	;AND STUFF IT INTO IO COMMAND
;-[1004]
	OUTPUT	FCT,OLIST
	STATZ	FCT,IO.ERR	;OK?
	  JRST	E.OUEF		;BAD EGGS
	TRNE	F,R.PAE		;DOING AVAIL.SYS?
	JRST	ERFIL4		;YES, DON'T SEND ANOTHER MESSAGE
	MOVE	T1,ERABUF	;GET HEADER WD 0
	MOVEM	T1,SSELBK+2	;INTO THE IPCF PACKET
	MOVE	T1,ERABUF+1	;GET HEADER WD 1
	MOVEM	T1,SSELBK+3	;INTO IPCF PACKET
	PUSHJ	P,SNDSEL	;SEND PACKET TO SYSTEM ERROR LOGGER
	MOVEI	T1,^D10		;REINITIALIZE NUMBER
	MOVEM	T1,IERCNT	;OF ERROR RETRIES
	MOVEM	T1,NEWEXT	;AND COUNT ON NEW EXTENSIONS IN ROW
ERFIL4:	RELEAS	FCT,		;RELEASE THE CHANNEL
	TRZN	F,R.AVL		;WANT THIS ENTRY APPENDED TO AVAIL.SYS?
	JRST	ERRFIX		;NO, EXIT CLEARING ALL FLAGS
	MOVE	P4,SAVEP4	;RESTORE ORIGINAL VALUE OF P4
	PJRST	AVLFIN		;AND CALL OURSELVES
;+[1004]
ESPLIT:	BLT	T1,ERRBUF(T2)	;COPY AS MUCH AS WILL FIT
	SUBI	T3,-1(T2)	;CALCULATE REMAINING WORDS
	MOVEI	P4,-1(T3)	;PUT IT WHERE IT BELONGS
	CAIL	T3,BLKSIZ	;STILL MORE THAN A BLOCK?
	MOVEI	T3,BLKSIZ	;SAY A BLOCK
	HRLI	T3,.ESOFF_9	;STUFF SYNC CODE
	MOVEM	T3,(T1)		;INTO BUFFER
	AOJA	T1,ERFIL1	;ADVANCE BLT POINTER AND CONTINUE
;-[1004]
;SUBROUTINE TO INITIALIZE FOR WRITING IN ERROR.SYS OR AVAIL.SYS
;ARGS	T1=FIRST (TYPE) WORD OF ENTRY
;VALUES	P4=IOWD ERRSIZ,ERABUF	SUITABLE FOR PUSH-ING
;	HEADER WORD SETUP (EXCEPT LENGTH)
;	DATE & TIME IN WORD 1
;	UPTIME IN WORD 2
;	CPU SERIAL NUMBER IN WORD 3
;	SEQUENCE NUMBER IN WORD 4
;RETURN	CPOPJ, T1-4 BLOWN

ERRINB:	TROA	F,R.NEB		;SET FLAG TO CALL BOOTCP
ERRINI:	TRZ	F,R.NEB		;CLEAR FLAG FOR CALL TO ERRINI
	TLO	F,L.ANY		;NOTE SOMETHING DONE
	SETZM	ERABUF		;CLEAR BUFFER
	MOVE	T2,[XWD ERABUF,ERABUF+1]
	BLT	T2,ERAEND	;CLEAR TO END
AVLINI:	HRR	T1,HEADR0	;FORMAT VERS & HEADER LEN
	MOVEM	T1,ERABUF+.EHTYP ;(0) HEADER TYPE IDENTIFIER
	PUSHJ	P,NBSDAT	;GET DATE AND TIME
	MOVEM	T1,ERABUF+.EHDTM ;(1) DATE (LH) AND TIME (RH)
	PUSHJ	P,UPTIME	;COMPUTE UPTIME
	MOVEM	T1,ERABUF+.EHUPT ;(2) UPTIME IN STD FORMAT
	TRNN	F,R.NEB		;WANT BOOT CPU SERIAL NUMBER?
	PUSHJ	P,CPUASN	;NO, GET CPU SERIAL NUMBER
	TRZE	F,R.NEB		;REVERSE SENSE OF TEST
	PUSHJ	P,BOOTCP	;AND GET BOOT CPU SERIAL NUMBER
	MOVEM	T1,ERABUF+.EHASN ;(3) CPU S/N [464]
	AOS	T1,ERRSEQ	;INCREMENT SEQUENCE NUMBER
	MOVEM	T1,ERABUF+.EHSEQ ;(4) SEQUENCE NUMBER
	MOVE	P4,[IOWD ERRSIZ,ERABUF+EL.HED] ;SETUP FOR PUSHING
	POPJ	P,		;SO FAR SO GOOD
;ROUTINE TO COMPUTE UPTIME IN UNIVERSAL DATE/TIME FORMAT.
;ENTER AT UPTCNV IF HAVE UPTIME IN JIFFIES IN T1.

UPTIME:	PUSHJ	P,GETUPT	;GET THE UPTIME IN TICKS
	SETZ	T2,
	ASHC	T1,-^D17	;JIFFIES*2**18 IN T2[7(55)]
	DIV	T1,JIFDAY	;DAYS IN LH, FRACTION IN RH
	POPJ	P,		;RETURN


;ROUTINE TO RETURN THE UPTIME IN TICKS IN T1.

GETUPT:	MOVE	T1,[%CNSUP]	;GETTAB ARG FOR SYSTEM UPTIME
	PUSHJ	P,GTBSPY	;GET IT
	  SKIPA	T1,[%NSUPT]	;SHOULDN'T FAIL, BUT TRY THIS ONE ANYWAY
	POPJ	P,		;RETURN
	PJRST	GTBDFL		;GET THE CPU0 UPTIME AND RETURN


;ROUTINE TO RETURN THE CPU SERIAL NUMBER OF THE CPU ON WHICH AN ERROR
;WAS DETECTED.

CPUASN:	LDB	T1,[POINT 3,ERPTBK+2,17] ;GET CPU NUMBER FROM ERROR BLOCK
	LSH	T1,1		;TIMES 2
	ADD	T1,[%CCSER]	;SET TO READ CPU SERIAL NUMBER
	SKIPE	ERPTBK+2	;IS THIS AN OLD MONITOR?
	PUSHJ	P,GTBSPY	;NO, GET IT
	  CAIA			;FAILED, DO IT THE OLD WAY
	POPJ	P,		;RETURN WITH NUMBER IN T1
BOOTCP:	MOVE	T1,[%CNSER]	;GET SERIAL NUMBER OF BOOT CPU
	PJRST	GTBDFL		;GET IT AND RETURN
;NEW ROUTINES TO PROCESS I/O ERRORS ON ERROR.SYS

;HERE IF INIT FAILS
E.IEF:	TRNN	F,R.PAE!R.INF	;DOING AVAIL.SYS?
	SETOM	IOERR		;SET FLAG SO WE STOP TRYING
	RELEAS	FCT,		;RELEASE THE CHANNEL
ERRFIX:	TXNE	F,R.INF		;DOING SEQUENCE NUMBER INITIALIZATION
	JRST	E.INF1		;YES, GIVE UP
	TRZ	F,R.AVL!R.PAE	;CLEAR ALL FLAGS
	POPJ	P,		;AND RETURN

;HERE IF LOOKUP/ENTER ERROR
E.EEF:	CAIN	T1,-1		;LOOKUP HAS CODE IN T1
	TDZ	T1,RBEXTE	;ENTER HAD -1 IN T1, THIS GETS CODE
	XORI	T1,-1		;CODE WAS IN ONE'S COMPLEMENT
	CAIN	T1,ERTRN%	;BAD RIB OR UFD?
	JRST	NEREXT		;YES, TRY NEW FILE NOW
	CAIE	T1,ERFBM%	;FILE BEING MODIFIED?
	CAIN	T1,ERNRM%	;OR DISK FULL?
	JRST	ERRCLK		;YES, DO SOME RETRIES
	CAIE	T1,ERNET%	;NO MONITOR FREE CORE?
	CAIN	T1,ERSLE%	;OR EMPTY SEARCH LIST
	JRST ERRCLK		;YES, DO SOME RETRIES
	PJRST	E.IEF		;ALL OTHERS FATAL, TREAT LIKE INIT ERROR

;HERE ON INPUT/OUTPUT ERROR
E.INEF:
E.OUEF:	GETSTS	FCT,T1		;GET STATUS IN T1
	TRNE	T1,IO.BKT	;DISK FULL
	PJRST	ERRCLK		;YES,DO SOME RETRIES

;ROUTINE TO TRY NEW EXTENSIONS.INPUT/OUTPUT ERRORS FALL THROUGH
;EXCEPT IO.BKT
NEREXT:	TRNN	F,R.PAE		;DOING AVAIL.SYS?
	SOSG	NEWEXT		;HERE TOO MANY TIMES IN A ROW?
	PJRST	E.IEF		;YES, GIVE UP
	HRRZ	T1,ERREXT	;TRY NEW FILE. FIRST GET CURRENT EXT
	CAIN	T1,'SYS'	;IS IT SYS?
	MOVEI	T1,'X00'	;YES, SET UP X00
	ADDI	T1,1		;INCREMENT TO GET NEW EXTENSION
	TRNN	T1,7		;SHOULD WE CARRY TO SECOND DIGIT?
	ADDI	T1,100-10	;YES,CAUSE THE SIXBIT TO CARRY
	CAILE	T1,'X77'	;HAS THE EXTENSION OVERFLOWED?
	PJRST	E.IEF		;YES, FATAL
	HRRZM	T1,ERREXT	;GOOD NEW EXT., STORE IT
	RELEAS	FCT,		;RELEASE THE CHANNEL
	MOVE	P4,SAVEP4	;RESTORE P4 IN CASE WE GOT TO ERFILL
	PJRST	ERRFIN		;AND TRY THE NEW FILE
; HERE ON ERRORS DETECTED DURING SEQUENCE NUMBER INITIALIZATION.

E.INF:	USETI	FCT,1		;SET TO READ BLOCK 1 OF FILE
	INPUT	FCT,ILIST	;READ FIRST 200 WORDS
	STATZ	FCT,IO.ERR	;ERRORS?
	  JRST	E.INF1		;YES, GIVE UP
	MOVE	T1,ERRBUF	;GET FIRST WORD OF FILE
	CAME	T1,[.ESOFF_9,,1] ;BETTER BE RESYNC WORD
	JRST	E.INF1		;NO
	LDB	T1,[POINTR ERRBUF+1+.EHTYP,EH.HFM] ;GET HDR FMT
	LDB	T2,[POINTR ERRBUF+1+.EHTYP,EH.HLN] ;  AND HDR LEN
	CAIL	T1,EL.HVR	;HEADER VERSION OK?
	CAIGE	T2,EL.HED	;LENGHT OK?
	JRST	E.INF1		;NO
	MOVE	T1,RBSIZE	;GET SIZE OF ERROR FILE
	IDIVI	T1,EL.HED+1	;COMPUTE MAX ENTRIES IN FILE
	ADD	T1,ERRBUF+1+.EHSEQ ;COMPUTE A SEQUENCE NUMBER
	MOVEM	T1,ERRSEQ	;USE THAT ONE TO START
	WARN	<Unable to resync sequence number from error file>,OPR
	WARN	<Computed initial value-1 is >,NOCRLF
	PUSHJ	P,TDEC		;TYPE THE NUMBER
	PUSHJ	P,OCRLF		;END MESSAGE AND RETURN
	JRST	RELFCT		;RELEASE CHANNEL
E.INF1:	WARN	<Unable to initialize error file sequence number>,OPR
	WARN	<Resyncing sequence number to 1>,OPR
	JRST	RELFCT		;RELEASE CHANNEL
;ROUTINE FOR RETRIES ON SAME FILE. USES INTERNAL CLOCK REQUEST TO
;WAIT BETWEEN RETRIES. AFTER 10 RETRIES WITH NO LUCK,IT TRIES A NEW FILE
ERRCLK:	TRNN	F,R.PAE		;DOING AVAIL.SYS?
	SOSG	IERCNT		;DECREMENT COUNT OF RETRIES LEFT
	PJRST	NEREXT		;TOO MANY RETRIES, TRY NEW FILE
	SETOM	IOERR		;NO IO TO ERROR FILE UNTIL CLOCK EXPIRES
	RELEAS	FCT,		;RELEASE THE CHANNEL
	PUSH	P,J		;SAVE J
	HRREI	J,IOERQ		;SET UP Q # FOR INTERNAL CLOCK REQUEST
	MOVEI	T1,ERRTIM	;SET UP TIME FOR CLOCK REQUEST
	PUSHJ	P,CLKREQ	;DO THE CLOCK REQUEST
	POP	P,J		;RESTORE J
	POPJ	P,		;RETURN

;SUBROUTINE TO INITIALIZE EXTENSION FOR ERROR FILE
;NO ARGS
;CHANGES ERREXT TO THE EXTENSION OF THE LAST ERROR FILE
; FOUND ON ERRDEV
;RETURNS CPOPJ, DESTROYS T1,T2,T3

ERRSTR:	SETZM	ERRDTM		;INIT SAVED ENTRY DATE/TIME
	MOVEI	T1,ERRBUF-1	;I/O BUFFER
	HRRM	T1,ILIST	;SAVE FOR LATER
	MOVX	T1,UU.PHS!.IODMP ;INIT A CHANNEL FOR LOOKUPS
	MOVE	T2,CERDEV	;GET DEVICE
	MOVEI	T3,0		;NO BUFFERS
	OPEN	FCT,T1		;OPEN THE CHANNEL
	  JRST	E.INF1		;CAN'T INIT ERRDEV
	MOVEI	T1,RBEXTE-RBCNTE ;LENGTH FOR EXTENDED LOOKUP
	MOVEM	T1,RBCNTE	;SAVE IN LOOKUP BLOCK
	MOVE	T1,[SIXBIT/ERROR/] ;AND SET UP NAME
	MOVEM	T1,RBNAME	; FOR LOOKUP
	MOVEI	T1,'SYS'	;START AT SYS
ERRST1:	HRLZM	T1,RBEXTE	;SAVE EXT FOR LOOKUP
	MOVE	T2,CERPPN	;SET UP DIRECTORY
	MOVEM	T2,RBPPNE	; FOR LOOKUP
	LOOKUP	FCT,EBLOCK	;LOOK UP THIS ONE
	  JRST	ERRST3		;FAILED
ERRST2:	HRRZM	T1,ERREXT	;SAVE EXTENSION
	PUSHJ	P,NXTEXT	;STEP TO NEXT EXTENSION
	  ERROR	<Too many error files found>,OPR,E.IEF
	INPUT	FCT,ILIST	;READ A BUFFER
	MOVE	T2,ERRBUF+1+.EHDTM ;GET FIRST ENTRY DATE/TIME
	MOVEM	T2,ERRDTM	;SAVE
	JRST	ERRST1		;TRY NEXT EXTENSION
ERRST3:	HRRZ	T2,RBEXTE	;GET ERROR CODE
	CAIE	T2,ERFNF%	;FILE NOT FOUND?
	JRST	ERRST2		;NO, KEEP LOOKING
	CAIN	T1,'SYS'	;FIND NO FILES AT ALL?
	POPJ	P,		;NO, USE DEFAULT SEQUENCE NUMBER
	PUSHJ	P,NBSDAT	;GET CURRENT UNIVERSAL DATE/TIME
	SUB	T1,ERRDTM	;COMPUTE DIFFERENCE
	HLRZS	T1		;KEEP ONLY DAYS
	CAIGE	T1,7		;OLDER THAN A WEEK?
	JRST	ERRST4		;NO
	PUSHJ	P,NXTEXT	;STEP TO NEXT EXTENSION
	  ERROR	<Too many error files found>,OPR,E.IEF
	MOVEM	T1,ERREXT	;SAVE STARTING EXTENSION
	TXZA	F,R.INF		;VIRGIN FILE
ERRST4:	TXO	F,R.INF		;FLAG SEQ NUMBER INITIALIZATION
	PUSHJ	P,INIFIN	;GO INITIALIZE ERRSEQ
	TXZ	F,R.INF		;TURN OFF FLAG
	POPJ	P,		;RETURN


;SUBROUTINE TO STEP TO NEXT EXTENSION
NXTEXT:	MOVE	T1,ERREXT	;GET EXTENSION
	CAIN	T1,'SYS'	;THIS THE FIRST ONE
	MOVEI	T1,'X01'-1	;YES, SETUP NEXT ONE
	ADDI	T1,1		;INCREMENT EXTENSION
	TRNN	T1,7		;SHOULD WE CARRY TO SECOND DIGIT?
	ADDI	T1,100-10	;YES, CAUSE THE SIXBIT TO CARRY
	CAIG	T1,'X77'	;HAS THE EXTENSION OVERFLOWED?
	AOS	(P)		;NO
	POPJ	P,		;RETURN


;SUBROUTINE TO RETURN DATE AND TIME IN NBS FORMAT,
; DAYS SINCE NOV 17, 1858 IN LH, FRACTION OF DAY IN RH
NBSDAT:	MOVE	T1,[%CNDTM]	;WAS %CNDAT
	PJRST	GTBDFL		;GET DATE AND RETURN
;ROUTINE TO FLUSH ANY IPCF PACKETS IN DAEMON'S INPUT QUEUE CAUSED
;BY PREVIOUS SENDS THAT FAILED.  ALSO CLEARS BLOCK BEFORE RETURN.
;CALL:	PUSHJ	P,FLSPAK
;	RETURN
;DESTROYS T1,T2

FLSPAK:	MOVEI	T2,0		;START WITH ZERO FLAGS
FLSPA1:	PUSHJ	P,CLRIPC	;CLEAR RECEIVE BLOCK
	TLO	T2,(IP.CFB!IP.CFT) ;NON-BLOCKING AND TRUNCATE LENGTH
	MOVEM	T2,IPCPDB+.IPCFL ;STORE IN FLAGS WORD
	MOVEI	T1,SSELBK	;LEN=0,,ADDRESS OF MESSAGE BLOCK
	MOVEM	T1,IPCPDB+.IPCFP ;STORE IN BLOCK
	MOVE	T1,[4,,IPCPDB]	;GET LEN,,ADDR OF BLOCK
	IPCFR.	T1,		;GET ANY PACKETS
	  JRST	[CAIE	T1,IPCPR%	;PAGE MODE?
		 PJRST	CLRIPC		;NO, CLEAR IPCF BLOCK AND RETURN
		 MOVEI	T2,IP.CFV	;GET PAGE MODE FLAG
		 JRST	FLSPA1]		;TRY IT THIS WAY
	JRST	FLSPAK		;GET ANY REMAINING PACKETS


;ROUTINE TO CLEAR THE IPCF MESSAGE AREA.
;CALL:	PUSHJ	P,CLRIPC
;	RETURN
;DESTROYS T1

CLRIPC:	MOVE	T1,[IPCPDB,,IPCPDB+1] ;SETUP FOR BLT
	SETZM	IPCPDB+.IPCFL	;CLR FIRST WD
	BLT	T1,SSELBK+7	;CLEAR THE REST
	POPJ	P,0		;RETURN


;ROUTINE TO SEND A MESSAGE TO THE ON-LINE SYSTEM ERROR LOGGER TO
;TELL IT THAT AN ERROR HAS OCCURRED.  CALL WITH DATA TO SEND IN SSELBK.
;CALL:	PUSHJ	P,SNDSEL
;	RETURN
;DESTROYS T1

SNDSEL:	MOVEI	T1,001001	;MSG TYPE,FORMAT
	HRLM	T1,SSELBK	;INTO THE MSG
	PUSHJ	P,FNDSEL	;FIND SEL
	  POPJ	P,		;NOT THERE, GIVE UP
;;	PJRST	SNDPAK		;AND SEND HIM A PRESENT
;ROUTINE TO SEND A MESSAGE TO ANOTHER PROCESS.
;CALL:	MOVEI	T1,PID OF RECEIVER
;	PUSHJ	P,SNDPAK
;	RETURN
;DESTROYS T1

SNDPAK:	MOVEM	T1,IPCPDB+.IPCFR ;SAVE RECEIVER'S PID
	MOVE	T1,[^D8,,SSELBK] ;GET POINTER TO MESSAGE
	MOVEM	T1,IPCPDB+.IPCFP;INTO THE PDB
	MOVE	T1,[6,,IPCPDB]	;GET LEN,,ADDR OF BLOCK
	IPCFS.	T1,		;SEND THE MSG
	 JFCL			;DON'T CARE ABOUT ERRORS
	POPJ	P,0		;RETURN


;ROUTINE TO FIND THE PID OF A SYSTEM PROCESS.  ENTER AT FNDSEL TO
;FIND THE SYSTEM ERROR LOGGER PID
;CALL:	PUSHJ	P,FNDXXX
;	  NOT THERE
;	RETURN WITH T1=PID

FNDSEL:	MOVE	T1,SELPTR	;SET UP TO GET SEL'S PID
	PUSHJ	P,GTBSPY	; GET IT
	  POPJ	P,		;NOT THERE, GIVE ERROR
	SKIPE	T1		;GET BACK ONE?
CPOPJ1:	AOS	(P)		;YES RETURN +1
CPOPJ:	POPJ	P,0		;NO RETURN NORMALLY
SUBTTL	AVAIL.SYS UPDATE ROUTINE

;ROUTINE TO UPDATE THE WORKING ENTRY IN AVAIL.SYS.

UPDAVL:	PUSHJ	P,SAVE4		;SAVE P1-P4
	MOVE	T1,[MRVBLK,,ERABUF] ;MAKE BLT POINTER
	BLT	T1,ERAEND	;MOVE THE ENTRY TO THE BUFFER
	LDB	P2,[POINT 9,ERABUF,35] ;GET LEN OF BODY FOR LATER
	MOVSI	T1,FCT+<(FO.PRV)> ;CHANNEL IS FCT
	HRRI	T1,.FOSAU	;FUNCTION IS SINGLE ACCESS UPDATE
	MOVEM	T1,D.FLOP+.FOFNC;STORE IN BLOCK
	MOVEI	T1,.IODMP	;USE DUMP MODE
	MOVEM	T1,D.FLOP+.FOIOS
	MOVE	T1,CERDEV	;DEVICE IS ERRDEV
	MOVEM	T1,D.FLOP+.FODEV
	SETZM	D.FLOP+.FOBRH	;NO BUFFERS
	SETZM	D.FLOP+.FONBF	;AND NO BUFFER COUNT
	MOVEI	T1,D.LKB	;POINT AT LOOKUP BLOCK
	MOVEM	T1,D.FLOP+.FOLEB
	SETZM	D.LKB		;CLEAR FIRST WORD OF LOOKUP BLOCK
	MOVE	T1,[D.LKB,,D.LKB+1] ;MAKE BLT POINTER
	BLT	T1,D.LKB+.RBSIZ ;CLEAR ENTIRE BLOCK
	MOVE	T1,[SIXBIT/AVAIL/] ;FILENAME IS AVAIL
	MOVEM	T1,D.LKB+.RBNAM
	MOVSI	T1,'SYS'	;EXTENSION IS SYS
	MOVEM	T1,D.LKB+.RBEXT
	MOVSI	T1,155000	;MAKE PROTECTION 155
	MOVEM	T1,D.LKB+.RBPRV
	MOVEI	T1,.RBSIZ	;COUNT IS .RBSIZ
	MOVEM	T1,D.LKB+.RBCNT
	MOVEI	P1,5		;NUMBER OF TIMES TO RETRY ON ERFBM%
UPDAV1:	MOVE	T1,CERPPN	;GET PPN INTO WHICH TO WRITE FILE
	MOVEM	T1,D.LKB+.RBPPN ;STORE IN BLOCK
	HLLZS	D.LKB+.RBEXT	;CLEAR ANY ERROR CODE
	MOVE	T1,[.FOLEB+1,,D.FLOP] ;POINT TO FILOP. BLOCK
	FILOP.	T1,		;OPEN THE FILE
	  JRST	[PUSHJ	P,FBMTRY ;CHECK FOR ERFBM%
		   JRST	UPDAV1	;IS, RETRY
		 POPJ	P,]	;NOT, GIVE UP
	MOVNI	T1,AWELEN(P2)	;GET -VE LENGTH OF BODY PLUS HEADER
	HRLI	T1,ERABUF-1	;MAKE SWAPPED IOWD
	MOVSS	T1		;MAKE IT RIGHT
	MOVEI	T2,0		;MAKE IOWD TERMINATE ON A ZERO
	OUTPUT	FCT,T1		;WRITE THE WORKING ENTRY
	STATZ	FCT,IO.ERR	;ANY ERRORS?
	JRST	RELFCT		;YES, RELEASE CHANNEL AND QUIT
	ADDI	P2,AWELEN+BLKSIZ-1 ;ROUND UP TO A BLOCK BOUNDARY
	LSH	P2,-B2WLSH	;COMPUTE NUMBER BLOCKS WRITTEN
	SUBI	P2,AVLWRK	;COMPUTE -NUMBER BLOCKS LEFT IN AVLWRK
	SETZM	ERABUF		;CLEAR FIRST WORD OF BLOCK
	MOVE	T3,[ERABUF,,ERABUF+1] ;MAKE BLT POINTER
	BLT	T3,ERABUF+BLKSIZ-1 ;CLEAR ENTIRE BLOCK
	HRLI	T1,-BLKSIZ	;SETUP IOWD TO ZERO FILL BLOCK
UPDAV2:	AOJG	P2,RELFCT	;LOOP UNTIL ALL AVLWRK BLOCKS ARE
	OUTPUT	FCT,T1		;  ZERO FILLED
	STATO	FCT,IO.ERR	;ANY ERRORS?
	JRST	UPDAV2		;NO, LOOP
	PJRST	RELFCT		;RELEASE CHANNEL AND RETURN
;ROUTINE TO SEE IF THE AGE OF AVAIL.SYS IS GREATER THAN 7 DAYS
;AND RENAME IT TO AVAIL.ANN IF IT IS.
;RETURNS CPOPJ ALWAYS WITH DISK COPY UPDATED IF LESS THAN 7 DAYS
;	 AND NEW FILE CREATED IF OLDER THAN 7 DAYS
;CHANGE TO RENAME .LWK TO .ANN, THEN RENAME .SYS TO .LWK

AVLAGE:	PUSHJ	P,NBSDAT	;GET CURRENT UNIVERSAL DATE/TIME
	SUB	T1,MRVBLK+AWECDA ;SUBTRACT CREATION DATE OF THIS AVAIL
	HLRZS	T1		;KEEP JUST DIFFERENCE IN DAYS
	CAIGE	T1,7		;OLDER THAN A WEEK?
	JRST	[PUSHJ	P,MRVSCN ;NO, UPDATE CORE COPY OF MRV ENTRY
		 PJRST	UPDAVL]	 ;AND UPDATE FILE COPY OF WORKING ENTRY
	PUSHJ	P,SAVE1		;GET A REGISTER TO USE
	PUSHJ	P,ENDAVL	;APPEND .ESEAV ENTRY TO FILE
	MOVEI	P1,'A01'	;FIRST EXTENSION TO TRY IS A01
AVLAG1:	MOVE	T1,[SIXBIT/AVAIL/] ;FILENAME IS AVAIL
	MOVEI	T4,(P1)		;EXTENSION IS XNN
	PUSHJ	P,OPNFIE	;TRY TO LOOKUP THE FILE
	  TRNE	T2,-1		;ERROR CODE MUST BE FILE NOT FOUND
	SKIPA			;TRY NEXT EXTENSION
	JRST	AVLAG2		;FOUND ONE, RENAME TO THAT
	ADDI	P1,1		;BUMP EXTENSION BY 1
	TRNN	P1,7		;NEED TO CARRY INTO THE 10S DIGIT?
	ADDI	P1,100-10	;YES, CARRY
	CAILE	P1,'A77'	;TRIED THEM ALL?
	WARN	<AVAIL.A77 already used, can't rename AVAIL.SYS>,OPR,RELFCT
				;YES, RELEASE CHANNEL AND GIVE UP AND COMPLAIN
	JRST	AVLAG1		;LOOP FOR NEXT
AVLAG2:	MOVE	T1,[SIXBIT/AVAIL/] ;FILENAME IS AVAIL
	MOVEI	T4,'LWK'	;SEE IF "LAST WEEK" FILE IS THERE
	PUSHJ	P,OPNFIE	;
	  JRST	AVLAG3		;IF CAN'T FIND IT, DON'T RENAME IT
	HRLI	T2,(P1)		;GET EXTENSION WE FOUND
	RENAME	FCT,T1		;RENAME THE FILE
	  PJRST	RELFCT		;FAILED, GIVE UP
AVLAG3:	MOVE	T1,[SIXBIT/AVAIL/] ;NOW SETUP
	PUSHJ	P,OPNFIL	;TO RENAME AVAIL.SYS
	  PJRST	RELFCT		;GIVE UP ON FAILURE
	HRLI	T2,'LWK'	;RENAME .SYS TO .LWK
	RENAME	FCT,T1		;
	  PJRST	RELFCT		;AND GIVE UP IF WE CAN'T
	PJRST	REDNAV		;CREATE NEW FILE AND RETURN


;ROUTINES TO APPEND BEGINNING/END OF AVAIL.SYS TIMESTAMPS.
;RETURNS CPOPJ ALWAY

BEGAVL:	SKIPA	T1,[.ESBAV_9,,0] ;GET CODE FOR BEGINNING OF AVAIL.SYS
ENDAVL:	MOVSI	T1,.ESEAV_9	;DITTO FOR END OF AVAIL.SYS
	PUSH	P,P4		;SAVE P4
	PUSHJ	P,ERRINI	;SETUP BUFFER, P4
	PUSH	P4,.JBVER	;BODY CONTAINS DAEMON VERSION #
	PUSHJ	P,AVLFIN	;APPEND ENTRY TO FILE
	POP	P,P4		;RESTORE P4
	POPJ	P,		;RETURN
;ROUTINE TO BUILD A NEW CORE COPY OF THE AVAIL.SYS WORKING ENTRY.
;RETURNS CPOPJ ALWAYS

MRVNEW:	MOVSI	T1,.ESMRV_9	;GET ENTRY CODE
	MOVEM	T1,MRVBLK+AWEHDR ;SAVE IN BLOCK
	SETZM	MRVBLK+AWECDM	;FORCE APPEND OF MAXCNF BY CLEARING DATE
	PUSHJ	P,NBSDAT	;GET CURRENT UNIVERSAL DATE/TIME
	HLRZS	T1		;KEEP ONLY DATE
	MOVEI	T2,-4(T1)	;MOVE TO T2 AND SHIFT SO THAT THE
				;IDIVI GIVES 0=SUNDAY, 1=MONDAY, ETC.
	IDIVI	T2,7		;DIVIDE BY DAYS/WEEK.  REMDR=DAY IN WEEK
	SUBI	T1,(T3)		;COMPUTE DATE OF PREVIOUS SUNDAY
	HRLZM	T1,MRVBLK+AWECDA ;MAKE CREATION DATE BE SUNDAY AT 00:00
	PUSHJ	P,MRVSCN	;BUILD NEW BODY
	MOVE	T1,MONVER	;GET CURRENT MONITOR VERSION
	MOVEM	T1,MRVBDY+MRVVER ;SAVE IN BLOCK
	SETZM	MRVBDY+MRVIDT	;START WITH NO DATE/TIME INCREMENT
	PJRST	SYSNAM		;GET NEW SYSTEM NAME AND RETURN

;ROUTINE TO UPDATE THE CORE COPY OF THE AVAIL.SYS WORKING ENTRY.
;RETURNS CPOPJ ALWAYS.

MRVSCN:	PUSH	P,P4		;SAVE CALLER'S P4
	MOVE	P4,[IOWD ERRSIZ,MRVBDY]	;SETUP BUFFER POINTER
	AOBJN	P4,.+1		;(R0) AOBJN POINTER TO SYSTEM NAME
	AOBJN	P4,.+1		;(R1) MONITOR VERSION NUMBER
	PUSHJ	P,UPTIME	;GET MONITOR UPTIME
	PUSH	P4,T1		;(R2) MONITOR UPTIME (UNIV DATE/TIME)
	PUSHJ	P,NBSDAT	;GET CURRENT UNIVERSAL DATE/TIME
	MOVEM	T1,MRVBLK+AWEDAT ;SAVE IN BLOCK
	PUSH	P4,T1		;(R3) MONITOR CRASH TIME (+/- 6 MIN)
	AOBJN	P4,.+1		;(R4) MONITOR RELOAD TIME
	AOBJN	P4,.+1		;(R5) SIXBIT WHY RELOAD CODE
	AOBJN	P4,.+1		;(R6) SUM OF INCREMENTAL DATE/TIME CHANGES
	ADD	P4,[5,,5]	;(R7-R13) ASCIZ MONITOR NAME
	SUBI	P4,MRVBDY-1	;COMPUTE LENGTH OF BODY
	DPB	P4,[POINT 9,MRVBLK,35] ;SAVE IN HEADER
	POP	P,P4		;RESTORE P4
	POPJ	P,		;AND RETURN
;ROUTINE TO READ THE WORKING ENTRY FROM AVAIL.SYS INTO CORE WHEN
;DAEMON STARTS UP.  IF AVAIL.SYS DOESN'T EXIST OR THE WORKING
;ENTRY IS BAD FOR SOME REASON, A NEW ENTRY IS BUILT.

REDAVL:	SETZM	MRVBLK		;CLEAR FIRST WORD OF BLOCK
	MOVE	T1,[MRVBLK,,MRVBLK+1] ;MAKE BLT POINTER
	BLT	T1,MRVEND	;CLEAR ENTIRE BLOCK
	MOVE	T1,[SIXBIT/AVAIL/] ;FILENAME IS AVAIL
	PUSHJ	P,OPNFIL	;OPEN THE FILE
	  JRST	REDNAV		;BUILD NEW ENTRY
	HLRES	T4		;GET 36 BIT NEGATIVE SIZE OF FILE
	MOVNS	T4		;MAKE IT POSITIVE
	CAIGE	T4,AVLWRK*BLKSIZ ;BETTER BE AT LEAST THIS LONG
	JRST	REDNAV		;MUST BE BAD
	MOVSI	T1,-<ERRSIZ+AWELEN> ;ONLY NEED THIS MANY WORDS
	HRRI	T1,MRVBLK-1	;MAKE AN IOWD
	MOVEI	T2,0		;TERMINATE LIST WITH A ZERO
	INPUT	FCT,T1		;READ THE OLD ENTRY
	STATZ	FCT,IO.ERR!IO.EOF ;ANY ERRORS?
	JRST	REDNAV		;YES, REBUILD IT
	LDB	T1,[POINT 9,MRVBLK,8] ;GET ENTRY CODE
	CAIN	T1,.ESMRV	;HAS TO BE THE RIGHT ONE
RELFCT:	RELEAS	FCT,		;RELEASE THE CHANNEL
	POPJ	P,		;RETURN

;HERE IF WE HAVE TO BUILD A NEW FILE FOR SOME REASON

REDNAV:	RELEAS	FCT,		;RELEASE THE CHANNEL
	PUSHJ	P,MRVNEW	;BUILD NEW ENTRY ON ERRORS
	PUSHJ	P,UPDAVL	;UPDATE WORKING ENTRY IN FILE
	PUSHJ	P,BEGAVL	;TIMESTAMP BEGINNING OF FILE
	POPJ	P,		;RETURN


;ROUTINE TO UPDATE THE SYSTEM NAME IN THE AVAIL.SYS WORKING
;ENTRY IN CORE.

SYSNAM:	MOVE	T2,[-5,,MRVSNM] ;GET AOBJN POINTER TO SYSTEM NAME
	MOVEM	T2,MRVBDY+MRVPSN ;SAVE AS POINTER TO NAME IN BLOCK
	MOVS	T3,[%CNFG0]	;GETTAB POINTER TO FIRST WORD OF NAME
SYSNA1:	MOVS	T1,T3		;GET CURRENT WORD POINTER
	PUSHJ	P,GTBDFL	;GET THE WORD
	MOVEM	T1,MRVBDY(T2)	;SAVE IN BLOCK
	ADDI	T3,1		;BUMP GETTAB ITEM NUMBER
	AOBJN	T2,SYSNA1	;LOOP FOR ALL 5 WORDS
	POPJ	P,		;RETURN
;ROUTINE TO LOOKUP ERRDEV:??????.SYS ON CHANNEL FCT.
;CALL WITH T1=FILENAME
;RETURNS CPOPJ IF OPEN OR LOOKUP FAILED,
;	 CPOPJ1 WITH CHANNEL OPEN AND LOOKUP BLOCK IN T1-T4

OPNFIL:	MOVEI	T4,'SYS'	;EXTENSION IS SYS
OPNFIE:	PUSH	P,T1		;SAVE FILENAME
	MOVX	T1,UU.PHS!.IODMP ;USE DUMP MODE
	MOVE	T2,CERDEV	;DEVICE IS ERRDEV
	MOVEI	T3,0		;NO BUFFERS
	OPEN	FCT,T1		;OPEN THE CHANNEL
	  JRST	TPOPJ		;CAN'T, GIVE ERROR RETURN
	POP	P,T1		;RESTORE FILENAME
	MOVSI	T2,(T4)		;SETUP EXTENSION
	MOVEI	T3,0		;NO PROTECTION
	MOVE	T4,CERPPN	;GET PPN
	LOOKUP	FCT,T1		;LOOKUP THE FILE
	  POPJ	P,		;NOT THERE, RETURN
	JRST	CPOPJ1		;GIVE SKIP RETURN


;ROUTINE TO ALLOCATE A BLOCK OF CORE.
;ARGS	T2=NUMBER OF WORDS TO ALLOCATE
;RETURNS CPOPJ IF CAN'T GET THE CORE
;	 CPOPJ1 WITH CORE ZEROED AND T2=ADDRESS OF FIRST WORD

GETCOR:	PUSH	P,.JBFF		;SAVE CURRENT VALUE OF .JBFF
	ADDB	T2,.JBFF	;COMPUTE NEW .JBFF
	CAMG	T2,.JBREL	;HAVE THAT MUCH?
	JRST	GETCO1		;YES
	CORE	T2,		;GET ANOTHER PAGE
	  JRST	[POP	P,.JBFF	;CAN'T, RESTORE ORIGINAL .JBFF
		 POPJ	P,]	;AND RETURN
GETCO1:	SETZM	@0(P)		;ZERO FIRST WORD OF CORE
	HRRZ	T2,(P)		;GET ADDRESS OF FIRST WORD
	HRLI	T2,1(T2)	;MAKE SWAPPED BLT POINTER
	MOVSS	T2		;MAKE IT ADDR,,ADDR+1
	BLT	T2,@.JBFF	;CLEAR THROUGH NEW .JBFF
	POP	P,T2		;RETURN ADDRESS OF FIRST WORD
	JRST	CPOPJ1		;GIVE GOOD RETURN
CRSHAC: BLOCK	20		;SAVE AC'S HERE ON REENTER (FOR DEBUGING)
CRSHPC:	BLOCK	1		;SAVE PC (.JBOPC) HERE ON REENTER
SUBTTL	DATA AND STORAGE, BUFFERS, ETC.

ILIST:	XWD	-200,0		;DUMP MODE INPUT COMMAND LIST
	0
OLIST:	XWD	-P2,0		;DUMP MODE OUTPUT COMMAND LIST (WORD COUNT WILL
	0 			; BE FILLED IN.)

PDL:	IOWD	PDLEN,PDLIST
HEADR0:	INSVL.(EL.FVR,EH.FFM)!INSVL.(EL.HVR,EH.HFM)!INSVL.(EL.HED,EH.HLN)
ERRSEQ:	EXP	0		;SEQUENCE NUMBER

QUECOD:	POINT	5,T1,14
SELPTR:	XWD	.IPCPL,.GTSID	;GETTAB ARG FOR SEL PID

JBTCLK:	Z	0(J)
IOERR:	BLOCK	1	;FLAG TO STOP IO TO ERROR FILE
IERCNT:	EXP	^D10	;COUNTER FOR TIMED RETRIES
NEWEXT:	EXP	^D10	;COUNT TO PREVENT TOO MANY NEW EXTS. IN ROW
CERDEV:	ERRDEV		;DEVICE FOR ERROR FILE
ERREXT:	'SYS'		;ERROR FILE EXTENSION
ERRDTM:	BLOCK	1	;OLDEST (FIRST) ENTRY DATE/TIME
CERPPN:	ERRPPN		;PPN FOR ERROR FILE

	XLIST		;LITERALS UNDER XLIST
	LIT
	LIST

ZER:!
CTYLIN:	BLOCK	1	;LINE NUMBER FOR CTY
RUNBLK:	BLOCK	6	;RUN UUO BLOCK
DAQNAM:	BLOCK	1
D.FLOP:	BLOCK	.FOPPN+1 ;BLOCK FOR FILOP. FUNCTIONS
D.LKB:	BLOCK	.RBSTS+1	;EXTENDED LOOKUP BLOCK

EBLOCK:				;BLOCK FOR EXTENDED LOOKUPS AND ENTERS
RBCNTE:	BLOCK	1
RBPPNE:	BLOCK	1
RBNAME:	BLOCK	1
RBEXTE:	BLOCK	1
RBPRVE:	BLOCK	1
RBSIZE:	BLOCK	1

LSTSTP:	BLOCK	1	;LAST SUBTABLE POINTER USED IN GSUBTB
LSTSTV:	BLOCK	1	;LAST BALUE OF SUBTABLE POINTER
GTBST:	BLOCK	1	;BEGINNING OF TABLE OF GETTAB PTRS
HJOB:	BLOCK	1	;HIGHEST JOB NO IN RH
INTADR:	BLOCK	1	;ADDR TO RESTART FOR ERROR INTERCEPT
INTBLK:	BLOCK	4	;ERROR INTERCEPT BLOCK
MAXGTB:	BLOCK	1	;HIGHEST LEGAL GETTAB TABLE
RNGTAB:	BLOCK	1	;ADDRESS OF RNGTAB
JBTMXL:	BLOCK	1	;JOBN+SEGN-1.  THIS IS THE MAXIMUM INDEX FOR
			;GETTAB TABLES INDEXED BY JOB OR SEGMENT NUMBER.
			;THE NUMTAB ENTRY IS NOT USED FOR THESE TABLES
			;BECAUSE THE 9 BIT FIELD OVERFLOWS ON SYSTEMS
			;WITH JOBN+SEGN-1 .GT. 511.
EVAADR:	BLOCK	1	;ADDRESS OF 1ST WORD IN EVABUF
EVABUF:	BLOCK	EVASIZ	;BUFFER FOR FUNNY SPACE DATA FROM PEKUSR
PDLIST:	BLOCK	PDLEN
SECBUF:	BLOCK	200
SEGSWP:	BLOCK	1
EXECT1:	BLOCK	1	;WORD FOR ADDRESS OF EXEC AC T1 IN USER'S JOBDAT
ERPTBK:	BLOCK	4	;4 WORD BLOCK FOR ERRPT. UUO
EVAPPB:	BLOCK	1	;START OF "PER PROCESS" ADDRESS SPACE
EVAPPE:	BLOCK	1	;END (+1) OF "PER PROCESS" ADDRESS SPACE

MONVER:	BLOCK	1	;MONITOR VERSION
THSJOB:	BLOCK	1	;OUR JOB NUMBER
THSLIN:	BLOCK	1	;OUR LINE NUMBER
USRCOR:	BLOCK	MAXUCR+BLKSIZ
PDBPTR:	BLOCK	1	;ADDRESS OF JBTPDB IN MONITOR FOR SPY
NEXTUP:	BLOCK	1	;NEXT TIME TO CAUSE A WAKE
NEXTJB:	BLOCK	1	;JOB TO WAKE
QPNTR:	BLOCK	1	;POINTER TO QUEUE
OPRPPN:	BLOCK	1	;PPN OF THE OPERATOR
JIFSEC:	BLOCK	1	;JIFFIES PER SECOND
JIFDAY:	BLOCK	1	;JIFFIES PER DAY
MILJIF:	BLOCK	1	;MILLISECONDS PER JIFFY
UUOJOB:	BLOCK	1	;JOB NUMBER OF JOB DOING LAST DAEMON UUO
UUOFNC:	BLOCK	1	;FUNCTION CODE FROM LAST DAEMON UUO
SAVXWD:	BLOCK	1	;SAVE UUO XWD (FROM EXEC AC T2)
DFNJOB:	BLOCK	1	;JOB NUMBER OF LAST JOB DAEFIN'ED
UUXCOD:	BLOCK	1	;ERROR CODE RETURNED TO JOB IN UUXJOB
UUXJOB:	BLOCK	1	;JOB WHICH LAST CAME THROUGH UUEXIT
DPMJOB:	BLOCK	1	;JOB NUMBER OF LAST JOB RELEASED BECAUSE
			;JS.DPM WAS ON
DPMCNT:	BLOCK	1	;COUNT OF JOBS RELEASED DUE TO ABOVE
ZZZDTM:	BLOCK	1	;UNIVERSAL DATE/TIME OF LAST WAKE AT ZZZ
DLKDTM:	BLOCK	1	;UNIVERSAL DATE/TIME LAST TIME THRU DALOOK
DLJDTM:	BLOCK	1	;UNIVERSAL DATE/TIME LAST TIME THRU DALOKJ
PDLGUF:	BLOCK	1	;DEBUG - CONTAINS ERRONEOUS P CONTENTS
GTBGUF:	BLOCK	1	;DEBUG - COUNTS MISTAKES IN GTBSPY
FINGUF:	BLOCK	1	;DEBUG - COUNTS DAEFIN ERROR RETURNS
WAKGUF:	BLOCK	1	;DEBUG - COUNTS ERROR RETURNS FROM WAKE UUO
INTGUF:	BLOCK	1	;DEBUG - COUNTS INTERCEPTS
CURJOB:	BLOCK	1	;DEBUG - LAST JOB SERVICED
INTFLG:	BLOCK	1	;DEBUG - REASONS FOR LAST JOBINT
INTPC:	BLOCK	1	;DEBUG - PC OF LAST JOBINT
WTUERR:	BLOCK	1	;FLAG TO PREVENT RECURSIVE CALLS OF WTUCOR WHEN
			;IT GETS IO ERRORS ON THE SWAPPING DEVICE WHILE
			;ATTEMPTING TO WRITE THE EXIT VALUES FOR A UUO.
IPCPDB:	BLOCK 6		;IPCF PACKET DESCRIPTOR BLK \*** KEEP
SSELBK:	BLOCK 10	;IPCF MSG AREA		 /*** TOGETHER
IFN FTDEBG,<		;DON'T WASTE SPACE
PATT:	BLOCK	100	;SPACE FOR PATCHES
>
SAVEP4:	BLOCK	1		;SAVE P4 HERE IN ERRFIN

NSYN==<ERRSIZ+EL.HED+BLKSIZ-1>/BLKSIZ	;MAX NUMBER OF RESYNC POINTERS
ERRBUF:	BLOCK	<<BLKSIZ+ERRSIZ+EL.HED+NSYN>!177>+1 ;LAST BLOCK OF ERROR.SYS
ERRBFE=.-1			;END OF THE BUFFER

MRVBLK:	BLOCK	ERRSIZ+AWELEN+2	;AVAIL.SYS WORKING ENTRY
MRVEND==.-1
MRVBDY==MRVBLK+AWELEN

EZER==.-1
ERABUF:	BLOCK	ERRSIZ+EL.HED+2	;FILL TO END OF BLOCK
ERAEND==.-1

;*****************************************************************
;SUB-BLOCK POINTERS USED IN THE ERROR ENTRIES ARE NOT CONSISTENT.
;SOMETIMES THE POINTER IS +LEN,,OFFSET WHILE OTHER TIMES IT IS
;-LEN,,OFFSET.  TO MAKE MATTERS WORSE, "OFFSET" IS SOMETIMES THE
;OFFSET FROM THE FIRST WORD OF THE HEADER AND SOMETIMES THE OFFSET
;FROM THE FIRST WORD OF THE ENTRY AFTER THE HEADER.  HOPEFULLY SOMEBODY
;WILL MAKE THEM ALL -LEN,,OFFSET FROM FIRST DATA WORD SOMEDAY.  IN
;ORDER TO MAKE THIS A LITTLE EASIER, THE FOLLOWING TWO SYMBOLS ARE
;DEFINED AND USED:
;	ERABFH - USED TO INDICATE THAT THE OFFSET IS FROM THE
;		 START OF THE HEADER.
;	ERABFS - USED TO INDICATE THAT THE OFFSET IS FROM THE
;		 FIRST WORD OF DATA AFTER THE HEADER.
ERABFH==ERABUF		;START OF THE HEADER
ERABFS==ERABUF+EL.HED	;FIRST WORD OF DATA AFTER THE HEADER
;*****************************************************************
	SUBTTL	ACCUMULATOR SAVERS

;ROUTINES TO SAVE P1-PX.  ALL ARE CALLED WITH PUSHJ P,SAVEX

SAVE1:	EXCH	P1,(P)		;COMMENTS SAME
	MOVEM	P1,1(P)
	MOVE	P1,(P)
	PUSHJ	P,@1(P)
	  JRST	RET1
	AOS	-1(P)
	JRST	RET1		;RESTORE P1 AND RETURN

SAVE2:	EXCH	P1,(P)		;GET RETURN, SAVE P1
	PUSH	P,P2		;SAVE P2
	MOVEM	P1,1(P)		;SAVE RETURN
	MOVE	P1,-1(P)	;RESTORE P1
	PUSHJ	P,@1(P)		;CALL CALLER
	  JRST	RET2
	AOS	-2(P)		;SKIP RET
	JRST	RET2		;RESTORE P1,P2 AND RETURN

SAVE3:	EXCH	P1,(P)		;GET RETURN, SAVE P1
	PUSH	P,P2		;SAVE P2
	PUSH	P,P3		;SAVE P3
	MOVEM	P1,1(P)		;SAVE RETURN
	MOVE	P1,-2(P)	;RESTORE P1
	PUSHJ	P,@1(P)		;CALL CALLER
	  JRST	RET3
	AOS	-3(P)		;SKIP RET
	JRST	RET3		;RESTORE P1-P3 AND RETURN

SAVE4:	EXCH	P1,(P)		;GET RETURN, SAVE P1
	PUSH	P,P2		;SAVE P2
	PUSH	P,P3		;SAVE P3
	PUSH	P,P4		;SAVE P4
	MOVEM	P1,1(P)		;SAVE RETURN
	MOVE	P1,-3(P)	;RESTORE P1
	PUSHJ	P,@1(P)		;CALL CALLER
	  JRST	RET4
	AOS	-4(P)		;SKIP RET
RET4:	POP	P,P4		;RESTORE P4
RET3:	POP	P,P3		;RESTORE P3
RET2:	POP	P,P2		;RESTORE P2
RET1:	POP	P,P1		;RESTORE P1
	POPJ	P,		;RETURN
;THALF - SUBROUTINE TO TYPE A NUMBER AS HALFWORD OCTAL
;CALL	MOVE	T1,NUMBER
;	PUSHJ	P,THALF
;USES T1,T2,T3,SC

THALF:	HRLM	T1,(P)		;KEEP RH FOR LATER
	HLRZS	T1		;KEEP JUST LEFT HALF
	PUSHJ	P,TOCT		;TYPE IT
	MOVEI	SC,","		;COMMA
	PUSHJ	P,TYPCHR
	HLRZ	T1,(P)		;RESTORE RH FROM STACK
;;	PJRST	TOCT		;FALL INTO TOCT TO TYPE AND RETURN

;TOCT - TYPE OCTAL VALUE GIVEN IN T1

TOCT:	IDIVI	T1,^D8		;DIVIDE OUT
	JUMPE	T1,TOCT1	;EXIT IF NO MORE
	HRLM	T2,(P)		;SAVE REMAINDER
	PUSHJ	P,TOCT		;LOOP
	HLRZ	T2,(P)		;RESTORE DIGIT
TOCT1:	MOVEI	SC,"0"(T2)	;MAKE ASCII
	PUSHJ	P,TYPCHR	;TYPE
	POPJ	P,		;RETURN

;TDEC - TYPE DECIMAL VALUE GIVEN IN T1

TDEC02:	MOVEI	SC,"0"		;GET A ZERO
	CAIG	T1,^D9		;IF GT 10
	 PUSHJ	P,TYPCHR	;FILL
TDEC:	IDIVI	T1,^D10		;DIVIDE OUT
	JUMPE	T1,TDEC1	;EXIT IF NO MORE
	HRLM	T2,(P)		;SAVE REMAINDER
	PUSHJ	P,TDEC		;LOOP
	HLRZ	T2,(P)		;RESTORE DIGIT
TDEC1:	MOVEI	SC,"0"(T2)	;MAKE ASCII
	PUSHJ	P,TYPCHR	;TYPE
	POPJ	P,		;RETURN

;TSIXN - TYPE C(T2) AS SIXBIT, NO TRAILING BLANKS

TSIX:	SETZ	T1,		;MAKE ROOM
	LSHC	T1,6		;GET CHAR IN T1
	MOVEI	SC,40(T1)	;MAKE ASCII IN SC
	PUSHJ	P,TYPCHR	;TYPE IT
TSIXN:	JUMPN	T2,TSIX		;LOOP IF MORE
	POPJ	P,		;DONE
;ROUTINE TO STORE A STRING IN THE MESSAGE BUFFER.
;CALL	MOVEI	T1,ADDRESS OF STRING
;	PUSHJ	P,TYPSTR
;	ALWAYS RETURN HERE

TYPSTR:	PUSH	P,SC		;SAVE SC
	HRLI	T1,(POINT 7,0)	;MAKE IT AN ASCII BYTE POINTER
TYPST1:	ILDB	SC,T1		;GET NEXT CHARACTER OF STRING
	JUMPE	SC,TYPST2	;DONE ON ZERO BYTE
	PUSHJ	P,TYPCHR	;STORE THE CHARACTER
	JRST	TYPST1		;LOOP FOR NEXT
TYPST2:	POP	P,SC		;RESTORE SC
	POPJ	P,		;  AND RETURN
;ROUTINE TO STORE ONE CHARACTER IN THE MESSAGE BUFFER AND FLUSH
;THE BUFFER WHEN A LINE FEED IS SEEN.
;CALL	MOVEI	SC,CHARACTER
;	PUSHJ	P,TYPCHR
;	ALWAYS RETURN HERE

TYPCHR:	CAIE	SC,.CHCRT	;END OF THE LINE?
	JRST	TYPCH1		;NO, CONTINUE
	EXCH	SC,MSGPTR	;SAVE CHARACTER, GET MSGPTR
	MOVEM	SC,EOLPTR	;SAVE AS END-OF-LINE POINTER
	EXCH	SC,MSGPTR	;RESTORE MSGPTR, SC
TYPCH1:	SOSL	MSGCNT		;ROOM IN THE BUFFER?
	IDPB	SC,MSGPTR	;YES, STORE THE CHARACTER
	TLNN	F,L.QUE		;IF QUEUE. ONLY, MAY BE MULTIPLE LINES
	CAIE	SC,.CHLFD	;LINE FEED?
	POPJ	P,		;NO, RETURN NOW

DMPBUF:	PUSHJ	P,SAVE1		;SAVE P1
	MOVEI	P1,0		;STORE A ZERO BYTE AT
	IDPB	P1,MSGPTR	;  THE END OF THE STRING
	TLNN	F,L.INI		;IN INITIALIZATION?
	SKIPE	.JBDDT		;OR DDT LOADED?
	CAIA			;WANT TO DO OUTSTR
	 JRST	TYPCH2		;DON'T
	TLNN	F,L.QUE		;DON'T EVER IN QUEUE. MODE
	 OUTSTR	MSGBUF		;TYPE THE STRING WITH OUTSTR
TYPCH2:	TLNE	F,L.QUE		;QUEUE. ONLY?
	 JRST	TYPCHQ		;YES, DON'T CHANGE FORMAT
	TLNN	F,L.OPR		;TELL OPERATOR ALSO?
	JRST	TYPCH3		;NO
	MOVEI	P1,"]"		;GET INFORMATIVE MSG END CHARACTER
	IDPB	P1,EOLPTR	;OVERWRITE THE LINE TERMINATOR
	MOVEI	P1,.CHCRT	;NOW PUT THE NEW LINE TERMINATOR
	IDPB	P1,EOLPTR	; IN PLACE AND TERMINATE THE LINE
	MOVEI	P1,.CHLFD	; WITH THE APPROPRIATE TERMINATOR
	IDPB	P1,EOLPTR	;...
	MOVEI	P1,0		;...
	IDPB	P1,EOLPTR	;...
	PUSH	P,T1		;SAVE T1
	PUSHJ	P,FNDOPR	;FIND OPR TTY
	POP	P,T1		;RESTORE T1
	MOVE	P1,[3,,OPRARG]	;GET ARG FOR TRMOP.
	SKIPN	.JBDDT		;DON'T TELL OPERATOR IF DEBUGGING
	TRMOP.	P1,		;TELL THE OPERATOR
	  JFCL			;WE TRIED
	HRRZ	P1,EOLPTR	;GET THE BYTE POINTER TO LAST BYTE
	SUBI	P1,MSGERR-1	;COMPUTE LENGTH (IN WORDS) OF STRING
	HRLM	P1,QUELEN	;STORE AS WTO LENGTH
	JRST	TYPCQ1		;ONWARDS
TYPCHQ:	HRRZ	P1,MSGPTR	;COUNT FROM END OF TEXT
	SUBI	P1,MSGBUF-1	;LENGTH TO END (SKIPPING SILLY ^G[DAEMON: )
	HRLM	P1,KLQTXT	;DIFFERENT BLOCK FOR KLINIK
	SKIPA	P1,[7,,KLKQUE]	;YES, NEED TO INCLUDE HEADER ARG
TYPCQ1:	 MOVE	P1,[7,,QUEARG]	;SETUP FOR QUEUE.
	SKIPN	.JBDDT		;DON'T DO WTO IF DEBUGGING
	QUEUE.	P1,		;TELL THE OPERATOR ALSO
	  JFCL			;WE TRIED
TYPCH3:	MOVE	P1,MSGPT1	;GET VIRGIN BYTE POINTER
	MOVEM	P1,MSGPTR	;AND STORE IT IN WORKING POINTER
	MOVEI	P1,LINSIZ	;GET MAX CHARS IN STRING
	MOVEM	P1,MSGCNT	;AND SAVE NEW COUNT
	POPJ	P,		;RETURN
;ROUTINE TO FIND THE TTY TO WHICH WE ARE TO WRITE OUR MESSAGES.
;CALL	PUSHJ	P,FNDOPR
;	RETURN HERE IF FOUND ONE WITH TTY UDX IN OPRLIN

FNDOPR:	MOVE	T1,[%CNOPR]	;TRY FOR DEVOPR
	PUSHJ	P,GTBSPY	;ASK MONITOR
	  CAIA			;???
	IONDX.	T1,UU.PHY	;TRANSLATE TO AN I/O INDEX
	  SKIPA	T1,CTYLIN	;FAILED--USE CTY LINE NUMBER
	CAIA			;GO STORE
	TLO	T1,.UXTRM	;MAKE IT A UDX
	MOVEM	T1,OPRLIN	;STORE VALUE IN OPRLIN
	POPJ	P,		;RETURN
	SUBTTL	STORAGE


; LOCATIONS FOR OUTPUT ROUTINES

EOLPTR:	BLOCK	1		;POINTER TO CR IN LAST MESSAGE
MSGPT1:	POINT	7,MSGBUF	;INITIAL POINTER FOR MESSAGE BUFFER
;***DMOVED
MSGPTR:	POINT	7,MSGBUF	;WORKING POINTER FOR MESSAGE BUFFER
MSGCNT:	EXP	LINSIZ		;COUNTER FOR MESSAGE BUFFER
;***END DMOVED

QUEARG:	EXP	QF.PIP!QF.NBR!.QUWTO ;FUNCTION FOR NON-BLOCKING WTO
	EXP	-1		;-1=NODE NUMBER FOR CENTRAL SITE
	EXP	0		;NO RESPONSE WANTED
	XWD	MSGHDL,.QBTYP	;LEN,,FUNCTION
	EXP	MSGHDR		;ADDRESS OF STRING
QUELEN:	XWD	0,.QBMSG	;LEN,,FUNCTION (LEN FILLED IN LATER)
	EXP	MSGBUF		;ADDRESS OF STRING

KLKQUE:	EXP	QF.PIP!QF.NBR!.QUWTO ;FUNCTION FOR NON-BLOCKING WTO
	EXP	-1		;-1=NODE NUMBER FOR CENTRAL SITE
	EXP	0		;NO RESPONSE WANTED
KLQLEN:	XWD	0,.QBTYP	;LEN,,FUNCTION (LEN FILLED IN LATER)
	EXP	KLQHDR		;ADDRESS OF STRING
KLQTXT:	XWD	0,.QBMSG	;JUST TEXT
	EXP	MSGBUF		;TEXT
KLQHDR:	BLOCK	<<^D80+3+4>/5>+1


OPRARG:	EXP	.TODSP		;TRMOP. ARG BLOCK TO TYPE MESSAGE
OPRLIN:	BLOCK	1		;  ON DEVICE OPR
	EXP	MSGERR

MSGHDR:	ASCIZ	/Message from DAEMON/
MSGHDL==.-MSGHDR

;***** KEEP THE FOLLOWING TOGETHER
MSGERR:	ASCII/[DAEMON: /	;MUST BE EXACT MULTIPLE OF 5 CHARS
MSGBUF:	BLOCK	<<LINSIZ+4>/5>+1
;***** END OF KEEP TOGETHER


	END	DAEMON