#include <machine/mmu.h>
#include <machine/asm_linkage.h>
#include <machine/psl.h>

	.seg	"text"
/* C usage is local_sys1_s(segmap,pagemap) where segmap and pagemap are kernel pointers */
/* segmap is assumed to be unsigned short int *, pointing to 16*4096=65536 shorts */
/* pagemap is assumed to be unsigned int *, pointing to 512*32=16394 ints */
	ENTRY(local_sys1_s)
	save		%sp,-SA(MINFRAME),%sp
#define zero g0
/* g1 g2 g3 g4 g5 g6 g7 */
#define segmap i0
#define pagemap i1
/* i2 i3 i4 i5 i6 i7 */
#define ctxnum l0
#define segaddr l1
#define segptr l2
#define ctxbase l3
#define oldctx l4
#define temp l7
/* l5 l6 */
/* o0 o1 o2 o3 o4 o5 o6 o7 */
	/* clear segmap */
	set		65536,%temp
	mov		%segmap,%segptr
0:	sth		%zero,[%segptr]
	dec		%temp
	tst		%temp
	bg,a		0b
	add		%segptr,2,%segptr
	/* eventually clear pagemap here */
	/* do it */
	set		CONTEXT_REG,%ctxbase
	lduba		[%ctxbase]ASI_CTL,%oldctx
	clr		%ctxnum
0:	stba		%ctxnum,[%ctxbase]ASI_CTL
	/* now in appropriate context */
	clr		%segaddr
	sll		%ctxnum,13,%segptr
	add		%segptr,%segmap,%segptr
1:	lduha		[%segaddr]ASI_SM,%temp
	sth		%temp,[%segptr]
	set		1<<18,%temp
	add		%segaddr,%temp,%segaddr
	set		0xc0000000,%temp
	andcc		%temp,%segaddr,%zero
	be,a		1b
	add		%segptr,2,%segptr
	/* now finished with this context */
	cmp		%ctxnum,15
	bl		0b
	add		%ctxnum,1,%ctxnum
	/* now finished with all contexts */
	/* eventually read pagemap here */
	stba		%oldctx,[%ctxbase]ASI_CTL
	mov		%oldctx,%i0
	nop /* paranoia */
	ret
	restore

	ENTRY(atspl8)
	mov	%psr,%o2
	and	%o2,PSR_PIL,%o1
	clr	%o0
	cmp	%o1,PSR_PIL
	be	1f
	nop
2:	retl
	nop
1:	inc	%o0
	b	2b
	nop

	ENTRY(getspl)
	mov	%psr, %o0
	and	%o0, PSR_PIL, %o0
	retl
	nop

	ENTRY(atsplhigh)
	mov	%psr,%o2
	and	%o2,PSR_PIL,%o1
	clr	%o0
	cmp	%o1,PSR_PIL
	be	1f
	nop
2:	retl
	nop
1:	inc	%o0
	b	2b
	nop
