plan 9 kernel history: overview | file list | diff list

1995/0115/pc/trap.c (diff list | history)

pc/trap.c on 1991/0613
1991/0613    
#include	"u.h" 
1992/0321    
#include	"../port/lib.h" 
1991/0613    
#include	"mem.h" 
#include	"dat.h" 
#include	"fns.h" 
#include	"io.h" 
1991/0703    
#include	"ureg.h" 
1992/0111    
#include	"../port/error.h" 
1991/0613    
 
1991/0720    
void	noted(Ureg*, ulong); 
 
1991/0731    
void	intr0(void), intr1(void), intr2(void), intr3(void); 
void	intr4(void), intr5(void), intr6(void), intr7(void); 
void	intr8(void), intr9(void), intr10(void), intr11(void); 
void	intr12(void), intr13(void), intr14(void), intr15(void); 
1994/1029    
void	intr16(void), intr17(void), intr18(void); 
1991/0731    
void	intr24(void), intr25(void), intr26(void), intr27(void); 
void	intr28(void), intr29(void), intr30(void), intr31(void); 
1991/0904    
void	intr32(void), intr33(void), intr34(void), intr35(void); 
void	intr36(void), intr37(void), intr38(void), intr39(void); 
1991/0731    
void	intr64(void); 
void	intrbad(void); 
 
1991/0613    
/* 
1991/0709    
 *  8259 interrupt controllers 
 */ 
enum 
{ 
	Int0ctl=	0x20,		/* control port (ICW1, OCW2, OCW3) */ 
	Int0aux=	0x21,		/* everything else (ICW2, ICW3, ICW4, OCW1) */ 
	Int1ctl=	0xA0,		/* control port */ 
	Int1aux=	0xA1,		/* everything else (ICW2, ICW3, ICW4, OCW1) */ 
 
	EOI=		0x20,		/* non-specific end of interrupt */ 
1993/0217    
 
	Maxhandler=	128,		/* max number of interrupt handlers */ 
1991/0709    
}; 
 
1991/1001    
int	int0mask = 0xff;	/* interrupts enabled for first 8259 */ 
int	int1mask = 0xff;	/* interrupts enabled for second 8259 */ 
1991/0709    
 
/* 
1991/0614    
 *  trap/interrupt gates 
1991/0613    
 */ 
1991/0703    
Segdesc ilt[256]; 
1993/0224    
int badintr[16]; 
1991/0703    
 
1993/0217    
typedef struct Handler	Handler; 
struct Handler 
{ 
1993/1124    
	void	(*r)(Ureg*, void*); 
	void	*arg; 
1993/0217    
	Handler	*next; 
}; 
 
struct 
{ 
	Lock; 
	Handler	*ivec[256]; 
	Handler	h[Maxhandler]; 
	int	free; 
} halloc; 
 
1991/0703    
void 
1991/0710    
sethvec(int v, void (*r)(void), int type, int pri) 
1991/0613    
{ 
1991/0703    
	ilt[v].d0 = ((ulong)r)&0xFFFF|(KESEL<<16); 
1991/0710    
	ilt[v].d1 = ((ulong)r)&0xFFFF0000|SEGP|SEGPL(pri)|type; 
1991/0703    
} 
1991/0614    
 
1991/0703    
void 
1993/1124    
setvec(int v, void (*r)(Ureg*, void*), void *arg) 
1991/0703    
{ 
1993/0217    
	Handler *h; 
1991/0709    
 
1993/0217    
	lock(&halloc); 
	if(halloc.free >= Maxhandler) 
		panic("out of interrupt handlers"); 
	h = &halloc.h[halloc.free++]; 
	h->next = halloc.ivec[v]; 
	h->r = r; 
1993/1124    
	h->arg = arg; 
1993/0217    
	halloc.ivec[v] = h; 
	unlock(&halloc); 
 
1991/0709    
	/* 
	 *  enable corresponding interrupt in 8259 
	 */ 
	if((v&~0x7) == Int0vec){ 
		int0mask &= ~(1<<(v&7)); 
		outb(Int0aux, int0mask); 
1991/0731    
	} else if((v&~0x7) == Int1vec){ 
		int1mask &= ~(1<<(v&7)); 
		outb(Int1aux, int1mask); 
1991/0709    
	} 
1991/0703    
} 
 
1991/1112    
void 
1993/1124    
debugbpt(Ureg *ur, void *arg) 
1991/1112    
{ 
	char buf[ERRLEN]; 
 
1993/1124    
	USED(arg); 
 
1993/0915    
	if(up == 0) 
1991/1112    
		panic("kernel bpt"); 
	/* restore pc to instruction that caused the trap */ 
	ur->pc--; 
1991/1218    
	sprint(buf, "sys: breakpoint"); 
1993/1013    
	postnote(up, 1, buf, NDebug); 
1991/1112    
} 
 
1991/0614    
/* 
1991/0703    
 *  set up the interrupt/trap gates 
1991/0614    
 */ 
1991/0703    
void 
trapinit(void) 
{ 
	int i; 
1991/0614    
 
1991/0703    
	/* 
1991/0731    
	 *  set all interrupts to panics 
	 */ 
1991/0904    
	for(i = 0; i < 256; i++) 
1993/1114    
		sethvec(i, intrbad, SEGIG, 0); 
1991/0731    
 
	/* 
1991/0904    
	 *  80386 processor (and coprocessor) traps 
1991/0703    
	 */ 
1993/1114    
	sethvec(0, intr0, SEGIG, 0); 
	sethvec(1, intr1, SEGIG, 0); 
	sethvec(2, intr2, SEGIG, 0); 
	sethvec(4, intr4, SEGIG, 0); 
	sethvec(5, intr5, SEGIG, 0); 
	sethvec(6, intr6, SEGIG, 0); 
	sethvec(7, intr7, SEGIG, 0); 
	sethvec(8, intr8, SEGIG, 0); 
	sethvec(9, intr9, SEGIG, 0); 
	sethvec(10, intr10, SEGIG, 0); 
	sethvec(11, intr11, SEGIG, 0); 
	sethvec(12, intr12, SEGIG, 0); 
	sethvec(13, intr13, SEGIG, 0); 
1993/1115    
	sethvec(14, intr14, SEGIG, 0);	/* page fault */ 
1993/1114    
	sethvec(15, intr15, SEGIG, 0); 
1993/1115    
	sethvec(16, intr16, SEGIG, 0);	/* math coprocessor */ 
1994/1029    
	sethvec(17, intr17, SEGIG, 0); 
	sethvec(18, intr18, SEGIG, 0); 
1991/0731    
 
	/* 
1991/0904    
	 *  device interrupts 
1991/0731    
	 */ 
1991/0904    
	sethvec(24, intr24, SEGIG, 0); 
	sethvec(25, intr25, SEGIG, 0); 
	sethvec(26, intr26, SEGIG, 0); 
	sethvec(27, intr27, SEGIG, 0); 
	sethvec(28, intr28, SEGIG, 0); 
	sethvec(29, intr29, SEGIG, 0); 
	sethvec(30, intr30, SEGIG, 0); 
	sethvec(31, intr31, SEGIG, 0); 
	sethvec(32, intr32, SEGIG, 0); 
	sethvec(33, intr33, SEGIG, 0); 
	sethvec(34, intr34, SEGIG, 0); 
	sethvec(35, intr35, SEGIG, 0); 
	sethvec(36, intr36, SEGIG, 0); 
1991/0913    
	sethvec(37, intr37, SEGIG, 0); 
1991/0904    
	sethvec(38, intr38, SEGIG, 0); 
	sethvec(39, intr39, SEGIG, 0); 
1991/0703    
 
	/* 
1991/1214    
	 *  system calls and break points 
1991/0710    
	 */ 
1993/1114    
	sethvec(Syscallvec, intr64, SEGIG, 3); 
1993/1124    
	setvec(Syscallvec, syscall, 0); 
1993/1114    
	sethvec(Bptvec, intr3, SEGIG, 3); 
1993/1124    
	setvec(Bptvec, debugbpt, 0); 
1991/0710    
 
1994/1007    
 
1991/0710    
	/* 
1991/0703    
	 *  tell the hardware where the table is (and how long) 
	 */ 
1991/0718    
	putidt(ilt, sizeof(ilt)); 
1991/0704    
 
	/* 
1991/0709    
	 *  Set up the first 8259 interrupt processor. 
	 *  Make 8259 interrupts start at CPU vector Int0vec. 
	 *  Set the 8259 as master with edge triggered 
	 *  input with fully nested interrupts. 
1991/0704    
	 */ 
1994/0603    
	outb(Int0ctl, (1<<4)|(0<<3)|(1<<0));	/* ICW1 - master, edge triggered, 
1994/0512    
					  	 ICW4 will be sent */ 
1991/0709    
	outb(Int0aux, Int0vec);		/* ICW2 - interrupt vector offset */ 
1991/0731    
	outb(Int0aux, 0x04);		/* ICW3 - have slave on level 2 */ 
1991/0709    
	outb(Int0aux, 0x01);		/* ICW4 - 8086 mode, not buffered */ 
1991/0731    
 
	/* 
	 *  Set up the second 8259 interrupt processor. 
1994/0824    
	 *  Make 8259 interrupts start at CPU vector Int1vec. 
1994/0513    
	 *  Set the 8259 as master with level triggered 
1991/0731    
	 *  input with fully nested interrupts. 
	 */ 
1994/0512    
	outb(Int1ctl, (1<<4)|(1<<3)|(1<<0));	/* ICW1 - master, level triggered, 
					  	 ICW4 will be sent */ 
1991/0731    
	outb(Int1aux, Int1vec);		/* ICW2 - interrupt vector offset */ 
	outb(Int1aux, 0x02);		/* ICW3 - I am a slave on level 2 */ 
	outb(Int1aux, 0x01);		/* ICW4 - 8086 mode, not buffered */ 
 
	/* 
	 *  pass #2 8259 interrupts to #1 
	 */ 
	int0mask &= ~0x04; 
	outb(Int0aux, int0mask); 
1991/0703    
} 
 
1994/0722    
char *excname[] = { 
	[0]	"divide error", 
	[1]	"debug exception", 
	[2]	" nonmaskable interrupt", 
	[3]	"breakpoint", 
	[4]	"overflow", 
	[5]	"bounds check", 
	[6]	"invalid opcode", 
	[7]	"coprocessor not available", 
	[8]	"double fault", 
	[9]	"9 (reserved)", 
	[10]	"invalid TSS", 
	[11]	"segment not present", 
	[12]	"stack exception", 
	[13]	"general protection violation", 
	[14]	"page fault", 
	[15]	"15 (reserved)", 
	[16]	"coprocessor error", 
1994/1029    
	[17]	"alignment check", 
	[18]	"something bad happened", 
1991/1113    
}; 
 
1994/1029    
Ureg lastur; 
Ureg scndlastur; 
 
1991/0614    
/* 
1993/1124    
 *  All traps come here.  It is slower to have all traps call trap() rather than 
1993/1115    
 *  directly vectoring the handler.  However, this avoids a lot of code duplication 
1994/0715    
 *  and possible bugs.  trap is called splhi(). 
1991/0614    
 */ 
1991/0710    
void 
1991/0703    
trap(Ureg *ur) 
1991/0614    
{ 
1991/1112    
	int v, user; 
1991/0731    
	int c; 
1991/1113    
	char buf[ERRLEN]; 
1993/0217    
	Handler *h; 
1993/1116    
	static int iret_traps; 
1991/0703    
 
1991/0731    
	v = ur->trap; 
 
1993/1113    
	user = (ur->cs&0xffff) == UESEL; 
1991/1112    
	if(user) 
1993/0915    
		up->dbgreg = ur; 
1993/1116    
	else if(ur->pc <= (ulong)end && *(uchar*)ur->pc == 0xCF) { 
		if(iret_traps++ > 10) 
			panic("iret trap"); 
1994/1029    
		goto out; 
1993/1116    
	} 
	iret_traps = 0; 
1991/1112    
 
1991/0709    
	/* 
	 *  tell the 8259 that we're done with the 
1991/0801    
	 *  highest level interrupt (interrupts are still 
	 *  off at this point) 
1991/0709    
	 */ 
1991/0731    
	c = v&~0x7; 
	if(c==Int0vec || c==Int1vec){ 
1993/0225    
		outb(Int0ctl, EOI); 
1991/0731    
		if(c == Int1vec) 
			outb(Int1ctl, EOI); 
1991/1113    
	} 
 
1993/0217    
	if(v>=256 || (h = halloc.ivec[v]) == 0){ 
1993/0224    
		/* an old 386 generates these fairly often, no idea why */ 
1991/1214    
		if(v == 13) 
1994/1029    
			goto out; 
1993/0224    
 
		/* a processor or coprocessor error */ 
1991/1113    
		if(v <= 16){ 
			if(user){ 
1991/1218    
				sprint(buf, "sys: trap: %s", excname[v]); 
1993/1013    
				postnote(up, 1, buf, NDebug); 
1994/1029    
				goto out; 
1991/1113    
			} else { 
				dumpregs(ur); 
1994/1029    
print("%s pc=0x%lux", excname[v], ur->pc); 
for(;;); 
1991/1113    
				panic("%s pc=0x%lux", excname[v], ur->pc); 
			} 
		} 
1993/0224    
 
		if(v >= Int0vec || v < Int0vec+16){ 
1994/0722    
			/* an unknown interrupt */ 
1993/0224    
			v -= Int0vec; 
1993/0915    
			if(badintr[v]++ == 0 || (badintr[v]%100000) == 0) 
1993/0224    
				print("unknown interrupt %d pc=0x%lux: total %d\n", v, 
					ur->pc, badintr[v]); 
		} else { 
			/* unimplemented traps */ 
			print("illegal trap %d pc=0x%lux\n", v, ur->pc); 
		} 
1994/1029    
		goto out; 
1991/0731    
	} 
1991/0801    
 
1993/0224    
	/* there may be multiple handlers on one interrupt level */ 
1993/0217    
	do { 
1993/1124    
		(*h->r)(ur, h->arg); 
1993/0217    
		h = h->next; 
	} while(h); 
1991/0910    
 
1994/0715    
	/* 
	 *  check user since syscall does its own notifying 
	 */ 
1993/0915    
	splhi(); 
1994/0715    
	if(v != Syscallvec && user && (up->procctl || up->nnote)) 
1991/0910    
		notify(ur); 
1994/1029    
out: 
	scndlastur = lastur; 
	lastur = *ur; 
1991/0806    
} 
 
/* 
1991/0718    
 *  dump registers 
 */ 
void 
1993/0915    
dumpregs2(Ureg *ur) 
1991/0718    
{ 
1993/1115    
	ur->cs &= 0xffff; 
	ur->ds &= 0xffff; 
	ur->es &= 0xffff; 
	ur->fs &= 0xffff; 
	ur->gs &= 0xffff; 
 
1993/0915    
	if(up) 
		print("registers for %s %d\n", up->text, up->pid); 
1991/0718    
	else 
		print("registers for kernel\n"); 
1993/1115    
	print("FLAGS=%lux TRAP=%lux ECODE=%lux CS=%4.4lux PC=%lux", ur->flags, ur->trap, 
1993/1113    
		ur->ecode, ur->cs, ur->pc); 
1993/1115    
	print(" SS=%4.4lux USP=%lux\n", ur->ss&0xffff, ur->usp); 
1991/0718    
	print("  AX %8.8lux  BX %8.8lux  CX %8.8lux  DX %8.8lux\n", 
		ur->ax, ur->bx, ur->cx, ur->dx); 
1991/1214    
	print("  SI %8.8lux  DI %8.8lux  BP %8.8lux\n", 
		ur->si, ur->di, ur->bp); 
1993/1113    
	print("  DS %4.4lux  ES %4.4lux  FS %4.4lux  GS %4.4lux\n", 
		ur->ds, ur->es, ur->fs, ur->gs); 
1991/0718    
} 
 
1991/0720    
void 
1993/0915    
dumpregs(Ureg *ur) 
{ 
1994/0813    
	extern ulong etext; 
1993/1113    
	ulong *x; 
 
1994/0813    
 
1993/1113    
	x = (ulong*)(ur+1); 
1994/1029    
	dumpregs2(&scndlastur); 
	dumpregs2(&lastur); 
1993/0915    
	dumpregs2(ur); 
1994/0902    
	print("  CR0 %8.8lux CR2 %8.8lux\n", getcr0(), getcr2()); 
1993/1113    
	print("  magic %lux %lux %lux\n", x[0], x[1], x[2]); 
	print("  ur %lux up %lux\n", ur, up); 
1993/0915    
} 
 
void 
1991/0720    
dumpstack(void) 
{ 
1992/0804    
	ulong l, v, i; 
	extern ulong etext; 
 
1993/0915    
	if(up == 0) 
1992/0804    
		return; 
 
	i = 0; 
1994/0816    
	for(l=(ulong)&l; l<(ulong)(up->kstack+KSTACK); l+=4){ 
1992/0804    
		v = *(ulong*)l; 
		if(KTZERO < v && v < (ulong)&etext){ 
			print("%lux ", v); 
			i++; 
		} 
1993/1113    
		if(i == 8){ 
1992/0804    
			i = 0; 
			print("\n"); 
		} 
	} 
1991/0720    
} 
 
1991/0718    
/* 
1991/0710    
 *  system calls 
 */ 
1991/0731    
#include "../port/systab.h" 
1991/0720    
 
1992/0103    
/* 
1994/0715    
 *  syscall is called splhi() 
1992/0103    
 */ 
1993/1124    
void 
syscall(Ureg *ur, void *arg) 
1991/0710    
{ 
1991/0720    
	ulong	sp; 
	long	ret; 
	int	i; 
 
1993/1124    
	USED(arg); 
 
1994/0715    
 
1993/0915    
	up->insyscall = 1; 
	up->pc = ur->pc; 
1993/1225    
	up->dbgreg = ur; 
 
1991/0720    
	if((ur->cs)&0xffff == KESEL) 
		panic("recursive system call"); 
1991/0718    
 
1993/0915    
	up->scallnr = ur->ax; 
	if(up->scallnr == RFORK && up->fpstate == FPactive){ 
1992/0805    
		/* 
		 *  so that the child starts out with the 
1994/0715    
		 *  same registers as the parent. 
		 *  this must be atomic relative to this CPU, hence 
		 *  the spl's. 
1992/0805    
		 */ 
1993/0915    
		if(up->fpstate == FPactive){ 
			fpsave(&up->fpsave); 
			up->fpstate = FPinactive; 
1992/0805    
		} 
	} 
1994/0715    
	spllo(); 
 
1991/0720    
	sp = ur->usp; 
1993/0915    
	up->nerrlab = 0; 
1991/0720    
	ret = -1; 
	if(!waserror()){ 
1994/0407    
		if(up->scallnr >= nsyscall){ 
1993/0915    
			pprint("bad sys call number %d pc %lux\n", up->scallnr, ur->pc); 
1993/1013    
			postnote(up, 1, "sys: bad sys call", NDebug); 
1991/0720    
			error(Ebadarg); 
		} 
1992/0625    
 
1991/0720    
		if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-(1+MAXSYSARG)*BY2WD)) 
			validaddr(sp, (1+MAXSYSARG)*BY2WD, 0); 
1992/0625    
 
1993/0915    
		up->s = *((Sargs*)(sp+1*BY2WD)); 
		up->psstate = sysctab[up->scallnr]; 
1992/0625    
 
1993/0915    
		ret = (*systab[up->scallnr])(up->s.args); 
1991/0720    
		poperror(); 
	} 
1993/0915    
	if(up->nerrlab){ 
		print("bad errstack [%d]: %d extra\n", up->scallnr, up->nerrlab); 
1991/0720    
		for(i = 0; i < NERR; i++) 
1993/0915    
			print("sp=%lux pc=%lux\n", up->errlab[i].sp, up->errlab[i].pc); 
1991/0720    
		panic("error stack"); 
	} 
1991/1114    
 
1993/0915    
	up->insyscall = 0; 
	up->psstate = 0; 
1992/0609    
 
	/* 
	 *  Put return value in frame.  On the safari the syscall is 
	 *  just another trap and the return value from syscall is 
	 *  ignored.  On other machines the return value is put into 
	 *  the results register by caller of syscall. 
	 */ 
1991/0801    
	ur->ax = ret; 
1992/0609    
 
1993/0915    
	if(up->scallnr == NOTED) 
1991/0720    
		noted(ur, *(ulong*)(sp+BY2WD)); 
1995/0105    
	splhi(); 
1991/1114    
 
1993/0915    
	if(up->scallnr!=RFORK && (up->procctl || up->nnote)) 
1991/0720    
		notify(ur); 
1995/0115    
 
1991/0710    
} 
 
1991/0720    
/* 
 *  Call user, if necessary, with note. 
 *  Pass user the Ureg struct and the note on his stack. 
 */ 
1992/0108    
int 
1991/0720    
notify(Ureg *ur) 
1991/0710    
{ 
1992/0124    
	int l, sent; 
1991/1114    
	ulong s, sp; 
1991/1218    
	Note *n; 
1991/0720    
 
1993/0915    
	if(up->procctl) 
1993/1013    
		procctl(up); 
1993/0915    
	if(up->nnote == 0) 
1992/0108    
		return 0; 
1991/1114    
 
	s = spllo(); 
1993/0915    
	qlock(&up->debug); 
	up->notepending = 0; 
	n = &up->note[0]; 
1991/1218    
	if(strncmp(n->msg, "sys:", 4) == 0){ 
		l = strlen(n->msg); 
		if(l > ERRLEN-15)	/* " pc=0x12345678\0" */ 
			l = ERRLEN-15; 
		sprint(n->msg+l, " pc=0x%.8lux", ur->pc); 
	} 
1993/0915    
	if(n->flag!=NUser && (up->notified || up->notify==0)){ 
		qunlock(&up->debug); 
1992/0714    
		if(n->flag == NDebug) 
1991/1218    
			pprint("suicide: %s\n", n->msg); 
		pexit(n->msg, n->flag!=NDebug); 
1991/0720    
	} 
1992/0124    
	sent = 0; 
1993/0915    
	if(!up->notified){ 
		if(!up->notify){ 
			qunlock(&up->debug); 
1992/1203    
			pexit(n->msg, n->flag!=NDebug); 
		} 
1992/0124    
		sent = 1; 
1993/0915    
		up->svcs = ur->cs; 
		up->svss = ur->ss; 
		up->svflags = ur->flags; 
1991/0720    
		sp = ur->usp; 
		sp -= sizeof(Ureg); 
1993/0915    
		if(!okaddr((ulong)up->notify, 1, 0) 
1992/0616    
		|| !okaddr(sp-ERRLEN-3*BY2WD, sizeof(Ureg)+ERRLEN-3*BY2WD, 0)){ 
1993/0915    
			qunlock(&up->debug); 
1992/0616    
			pprint("suicide: bad address in notify\n"); 
1991/0722    
			pexit("Suicide", 0); 
		} 
1993/0915    
		up->ureg = (void*)sp; 
1991/0720    
		memmove((Ureg*)sp, ur, sizeof(Ureg)); 
		sp -= ERRLEN; 
1993/0915    
		memmove((char*)sp, up->note[0].msg, ERRLEN); 
1991/0720    
		sp -= 3*BY2WD; 
		*(ulong*)(sp+2*BY2WD) = sp+3*BY2WD;	/* arg 2 is string */ 
1993/0915    
		*(ulong*)(sp+1*BY2WD) = (ulong)up->ureg;	/* arg 1 is ureg* */ 
1991/0720    
		*(ulong*)(sp+0*BY2WD) = 0;		/* arg 0 is pc */ 
		ur->usp = sp; 
1993/0915    
		ur->pc = (ulong)up->notify; 
		up->notified = 1; 
		up->nnote--; 
		memmove(&up->lastnote, &up->note[0], sizeof(Note)); 
		memmove(&up->note[0], &up->note[1], up->nnote*sizeof(Note)); 
1991/0720    
	} 
1993/0915    
	qunlock(&up->debug); 
1991/1114    
	splx(s); 
1992/0124    
	return sent; 
1991/0710    
} 
 
1991/0720    
/* 
 *   Return user to state before notify() 
 */ 
1991/0710    
void 
1991/0720    
noted(Ureg *ur, ulong arg0) 
1991/0710    
{ 
1991/0720    
	Ureg *nur; 
 
1993/0915    
	qlock(&up->debug); 
1994/0513    
	if(!up->notified) { 
1993/0915    
		qunlock(&up->debug); 
1994/0513    
		pprint("call to noted() when not notified\n"); 
1995/0105    
		pexit("Suicide", 0); 
1991/0720    
	} 
1993/0915    
	up->notified = 0; 
1994/0513    
 
	nur = up->ureg;		/* pointer to user returned Ureg struct */ 
	if(nur->cs!=up->svcs || nur->ss!=up->svss || 
	  (nur->flags&0xff00)!=(up->svflags&0xff00)) { 
		qunlock(&up->debug); 
		pprint("bad noted ureg cs %ux ss %ux flags %ux\n", 
				nur->cs, nur->ss, nur->flags); 
			pexit("Suicide", 0); 
	} 
 
1993/0915    
	nur->flags = (up->svflags&0xffffff00) | (nur->flags&0xff); 
1992/0609    
	memmove(ur, nur, sizeof(Ureg)); 
1995/0105    
	qunlock(&up->debug); 
 
1991/0720    
	switch(arg0){ 
	case NCONT: 
1992/0616    
		if(!okaddr(nur->pc, 1, 0) || !okaddr(nur->usp, BY2WD, 0)){ 
1991/0814    
			pprint("suicide: trap in noted\n"); 
1994/0513    
			pexit("Suicide", 0); 
1991/0814    
		} 
1995/0105    
		break; 
1991/0720    
 
	default: 
		pprint("unknown noted arg 0x%lux\n", arg0); 
1993/0915    
		up->lastnote.flag = NDebug; 
1991/0720    
		/* fall through */ 
		 
	case NDFLT: 
1993/0915    
		if(up->lastnote.flag == NDebug) 
			pprint("suicide: %s\n", up->lastnote.msg); 
		pexit(up->lastnote.msg, up->lastnote.flag!=NDebug); 
1991/0720    
	} 
1991/1112    
} 
 
1993/1113    
long 
execregs(ulong entry, ulong ssize, ulong nargs) 
{ 
	ulong *sp; 
	Ureg *ur; 
 
	sp = (ulong*)(USTKTOP - ssize); 
	*--sp = nargs; 
 
	ur = up->dbgreg; 
	ur->usp = (ulong)sp; 
	ur->pc = entry; 
1994/0513    
	return USTKTOP-BY2WD;		/* address of user-level clock */ 
1993/1113    
} 
 
ulong 
userpc(void) 
{ 
	Ureg *ur; 
 
	ur = (Ureg*)up->dbgreg; 
	return ur->pc; 
} 
 
1991/1112    
/* This routine must save the values of registers the user is not permitted to write 
 * from devproc and the restore the saved values before returning 
 */ 
void 
setregisters(Ureg *xp, char *pureg, char *uva, int n) 
{ 
	ulong flags; 
	ulong cs; 
	ulong ss; 
 
	flags = xp->flags; 
	cs = xp->cs; 
	ss = xp->ss; 
	memmove(pureg, uva, n); 
	xp->flags = (xp->flags & 0xff) | (flags & 0xff00); 
	xp->cs = cs; 
	xp->ss = ss; 
1993/1113    
} 
 
static void 
linkproc(void) 
{ 
	spllo(); 
	(*up->kpfun)(up->kparg); 
} 
 
void 
kprocchild(Proc *p, void (*func)(void*), void *arg) 
{ 
	p->sched.pc = (ulong)linkproc; 
	p->sched.sp = (ulong)p->kstack+KSTACK; 
 
	p->kpfun = func; 
	p->kparg = arg; 
} 
 
void 
forkchild(Proc *p, Ureg *ur) 
{ 
	Ureg *cur; 
 
	/* 
	 * We add 2*BY2Wd to the stack because we have to account for 
	 *  - the return PC 
	 *  - trap's argument (ur) 
	 */ 
	p->sched.sp = (ulong)p->kstack+KSTACK-(sizeof(Ureg)+2*BY2WD); 
	p->sched.pc = (ulong)forkret; 
 
	cur = (Ureg*)(p->sched.sp+2*BY2WD); 
	memmove(cur, ur, sizeof(Ureg)); 
	cur->ax = 0;				/* return value of syscall in child */ 
 
	/* Things from bottom of syscall we never got to execute */ 
	p->psstate = 0; 
	p->insyscall = 0; 
1991/0614    
} 
1993/1022    
 
/* Give enough context in the ureg to produce a kernel stack for 
 * a sleeping process 
 */ 
void 
setkernur(Ureg *xp, Proc *p) 
{ 
	xp->pc = p->sched.pc; 
1994/0219    
	xp->sp = p->sched.sp+4; 
1993/1022    
} 


source code copyright © 1990-2005 Lucent Technologies; see license
Plan 9 distribution
comments to russ cox (rsc@swtch.com)