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IOFN,CQ$RNS,LITIME,S$HICP,QCOMPL
	.GLOBL	USWPCH,CTXBUF,CXBOWN,CXBJOB,CXBBAS,CXBSIZ
	.GLOBL	CORUSR,S$QCXB,QNSPNX,CHKABT,UREGO,$INCOR,LSW,CXBMOV
	.GLOBL	CXTBAS,LSWPBK,USREMT,PR7,INTPRI,CXTRMN
	.GLOBL	R$CHN,R$XCHN,CHNCIZ,MVSIZ

;------------------------------------------------
;	MACROS TO ENABLE AND DESABLE INTERRUTS
;
	.MACRO	DISABL		; DISABLE INTERRUPTS
	BIS	#340,@#P3
	.ENDM	DISABL

	.MACRO	ENABL		; ENABLE INTERRUPT
	BIC	INTPRI,@#PS
	.ENDM	ENABL
;
;	Macro to print an error message when a system crash occurs.
;
;	Arguments:
;	 MSG = Name of error message to print
;	 ARG = (Optional) argument value to display with error message.
;
	.GLOBL	DIEMSG,DIEARG,SYSHLT
	.MACRO	DIE	MSG,ARG
	MOV	MSG,@#DIEMSG
	.IF	NB,ARG
	MOV	ARG,@#DIEARG
	.ENDC
	CALL	@#SYSHLT
	.ENDM	DIE
;
;	Macro definition for calling global routines residing in mapped
;	system regions.
;
	.MCALL	OCALL	ENTADD
	.IF	B,ENTADD
		.ERROR	;OCALL SPECIFIED WITH NO ENTRY ADDRESS
		.MEXIT
	.ENDC
	CALL	OVRHC	;CALL THE OVERLAY HANDLER
	.WORD	ENTADD	;SPECIFY THE ENTRY POINT
	.ENDM
;--------------------------------------------------
;	Data areas
;
R50DK:	.RAD50	/DK /

	.SBTTL	SETPRI
;-----------------------------------------------------
;  Set execution priority or privilege flags for job.
;
SETPRI:	TSTB	EMTBLK		; Set priotity or privilegy?
	BNE	SETPRV		; Set privilegy
;
; Set job execution priority
;
	MOV	EMTBLK+2,R2	;Get specified execution priority
	BGT	1$		;BR if value specified
	MOVB	VPRIDF,R2	;If not specified, set default
1$:	CMPB	R2,MXJPRI	;Compare with max authorized priority
	BLOS	2$		;BR if ok
	MOVB	MXJPRI,R2	;Get max authorized priority
2$:	MOVB	R2,LBSPRI(R1)	;Set base priority for job
	MOVB	R2,LPRI(R1)	;Set current priority for job
	BIT	#$VNOTT,LSW(R1)	;Is this a subprocess not connected to term?
	BEQ	9$		;BR if not
	OCALL	SPPRED		;Reduce priority of dosxonnected subprocesses
9$:	JMP	EMTXIT		;Finished

;----------------------------------------------------------------
;	Set privilegy flags for job
;
;	The form of the EMT argument block is as follows:
;
;	.BYTE	1,150
;	.BYTE	fun,perm	;Fun: 0=get,1=clear,2=set Perm: 0=temp,1=perm
;	.WORD	buffer		;Buffer address
;	.WORD	0		;(RESERVED)
;
SETPRV:
;
;	Validate the user's buffer address
;
	MOV	EMTBLK+4,R2	;Get user's buffer address
	MOV	R2,R0
	CALL	VALADW		;Validate the address
;
;	Only allow changes to authotization privilege flags
;
	CMPB	EMTBLK+3,#2	;Does he want to access auth priveleges?
	BNE	10$		;BR if not
	BIT	#PO$SPV,PRIVCO	;Does he have SETPRV privilege?
	BNE	10$		;BR if yes
	CMPB	EMTBLK+2,#2	;Does he want to set bits?
	BNE	10$		;BR if not (allow access or clear-bits)
	DECB	EMTBLK+3	;Can't change auth priv without SERPRV
;
;	See what the subfunction is
;
10$:	MOVB	EMTBLK+2,R0	;Get subfunction code
	CMP	R0,#1		;Clear bits?
	BEQ	2$		;BR if yes
	CMP	R0,#2		;Set bits?
	BEQ	5$		;BR if yes
;
;	Return current privilegy flags to user
;
	MOV	#PRIVCO,R3	;Point to vector with current flags
	MOVB	EMTBLK+3,R0	;Get type of priv flags wanted
	BEQ	11$		;BR if wants current priv flags
	MOV	#PRIVSO,R3	;Point to table of perm flags
	DEC	R0		;Want perm flags?
	BEQ	11$		;BR if yes
	MOV	#PRIVAO,R3	;Must want auth. priv.
11$:	MOV	#PVNPW,R0	;Get # words to move
1$:	MOV	(R3)+,-(SP)	;Get word with current flags
	MTPD	(R2)+		;Move to user's buffer
	SOB	R0,1$		;Move all words with privilege flags
	BR	9$
;
;	User wants to clear some privilege flags
;
2$:	CLR	R5		;Init index into privilege words
	MOVB	EMTBLK+3,R4	;Get type of privilege change wanted
3$:	MFPD	(R2)+		;Get flags from user
	MOV	(SP)+,R0	;Pop flags
