From: mpark@stein.u.washington.edu (Michael Park)
Subject: Re: ctxsw routine for M68000
Organization: University of Washington, Seattle
Date: Tue, 6 Oct 1992 07:48:25 GMT

Thanks to John Reed for posting his 68000 ctxsw (listing follows);
I have a few comments and questions about it:

1. The comments are inconsistent as to which stack pointer is actually
   saved, the usp or the ssp.  Since the routine is executed in kernel
   (aka supervisor) mode, all references to A7 or SP must refer to the
   SSP (unless I am very much mistaken).

2. Given (1) above, two questions spring to mind:
   a. since the USP is never saved, does that mean that the 68000
      is ALWAYS in supervisor mode?  Or,
   b. the 68000 can switch between user and supervisor modes, but
      it's perfectly safe not to save USP between context switches.
      (I seriously doubt that it's case b)

3. I was leafing through the Mac edition of the xinu book, and was
   forced to conclude that Mac xinu runs completely in supervisor
   mode (this was never stated explicitly as far as I could tell;
   there wasn't even an index entry for "supervisor").  Is that
   deduction correct, and is it true for Mac programs in general
   that everything is done in supervisor state?

4. Isn't running solely in supervisor state a bit of a lose?

I hope Mr. Reed or some other knowledgeable person can help me out.

------- listing follows ------

In article <1992Oct5.164946.19346@pacdata.uucp> johnr@pacdata.uucp (John Reed) writes:
...
>*-----------------------------------------------------------------------------
>* ctxsw  --	actually perform context switch, saving and loading registers
>*		This routine is executed in kernel mode.
>*
>*		Upon entry, d0-d7, a0-a6, the usp, the sr, and the pc are
>*		saved.
>*
>*		Upon exit, all registers saved are restored, and the value
>*		of the user stack pointer is copied into the user 
>*		stack pointer.
>*-----------------------------------------------------------------------------
>
>	section	14
>
>	xdef	.cnt_ctxsw
>
>.cnt_ctxsw	dc.l	0
>
>	section	9
>
>	xdef	.ctxsw
>
>.ctxsw:
>	addq.l	#1,.cnt_ctxsw
>	move.l	a0,-(sp)	 		; Save A0 onto old stack	
>	move.l	8(sp),a0			; Move address of old area into A0 
>	movem.l	d0-d7/a0-a7,(a0)	; Save all registers		
>	move.l	(sp),32(a0)			; Put original A0 in old savearea	
>	addq.l	#8,60(a0)			; Move SP beyond return address
>								; 	as if a return has occured
>	add.l	#64,a0				; Skip past registers d0-7,a0-7
>	move.w	#0,(a0)+ 			; Pad SR savearea, since SR is a word
>	move.w	sr,(a0)+			; Save SR in old savearea
>	move.l	4(sp),(a0)+			; Save PC in old savearea
>
>
>*Save image:d0,d1,d2,d3,d4,d5,d6,d7,a0,a1,a2,a3,a4,a5,a6,ssp,0x00, sr, pc
>*            0  4  8 12 16 20 24 28 32 36 40 44 48 52 56  60  64   66  68
>
>	move.l	12(sp),a0			; Move address of new area into A0
>	movem.l	(a0),d0-d7/a0-a6	; Restore all regs (even A0) except SP
>	move.l	12(sp),a0			; Move address of new area into A0 again
>	move.l	60(a0),sp			; Put  SSP into kernel stack
>	move.l	68(a0),-(sp)		; Move PC onto current stack	
>	move.w	66(a0),sr			; Restore the status register
>	move.l	32(a0),a0			; Restore A0 from new area
>	rts							; since we pushed the pc onto the stack,
>								;   we just pretend to return
>
>
>
>
>
>
>---JR---
>
>
>-- 
>
>
>   /------------------------------------------------------------------\
>  |  John Reed                           {ucsd,uunet}!pacdata!johnr    |
>  |  Pacific Data Products               johnr@pacdata.com             |
>  |                    ---------------------                           |
>  |  George Herbert Walker Bush, with the letters re-arranged spells:  |
>  |                                                                    |
>  |                 Huge Berserk Rebel Warthog                         |
>  |                                     -- From Cosmic Trigger II      |
>   \------------------------------------------------------------------/


-- 
Ciao-abunga!           +-------------------------------------+
Michael Park           | This space intentionally left blank |
mpark@u.washington.edu +-------------------------------------+