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

1993/0220/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); 
1991/0904    
void	intr16(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/0220    
int	badtrap[256]; 
1991/0703    
 
1993/0217    
typedef struct Handler	Handler; 
struct Handler 
{ 
	void	(*r)(void*); 
	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 
1991/0716    
setvec(int v, void (*r)(Ureg*)) 
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; 
	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 
debugbpt(Ureg *ur) 
{ 
	char buf[ERRLEN]; 
 
	if(u == 0) 
		panic("kernel bpt"); 
	/* restore pc to instruction that caused the trap */ 
	ur->pc--; 
1991/1218    
	sprint(buf, "sys: breakpoint"); 
1991/1112    
	postnote(u->p, 1, buf, NDebug); 
} 
 
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++) 
1991/0823    
		sethvec(i, intrbad, SEGTG, 0); 
1991/0731    
 
	/* 
1991/0904    
	 *  80386 processor (and coprocessor) traps 
1991/0703    
	 */ 
1991/0823    
	sethvec(0, intr0, SEGTG, 0); 
	sethvec(1, intr1, SEGTG, 0); 
	sethvec(2, intr2, SEGTG, 0); 
	sethvec(4, intr4, SEGTG, 0); 
	sethvec(5, intr5, SEGTG, 0); 
	sethvec(6, intr6, SEGTG, 0); 
	sethvec(7, intr7, SEGTG, 0); 
	sethvec(8, intr8, SEGTG, 0); 
	sethvec(9, intr9, SEGTG, 0); 
	sethvec(10, intr10, SEGTG, 0); 
	sethvec(11, intr11, SEGTG, 0); 
	sethvec(12, intr12, SEGTG, 0); 
	sethvec(13, intr13, SEGTG, 0); 
1992/0806    
	sethvec(14, intr14, SEGIG, 0);	/* page fault, interrupts off */ 
1991/0823    
	sethvec(15, intr15, SEGTG, 0); 
1992/0806    
	sethvec(16, intr16, SEGIG, 0);	/* math coprocessor, interrupts off */ 
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    
	 */ 
1991/0910    
	sethvec(Syscallvec, intr64, SEGTG, 3); 
	setvec(Syscallvec, (void (*)(Ureg*))syscall); 
1991/1112    
	sethvec(Bptvec, intr3, SEGTG, 3); 
	setvec(Bptvec, debugbpt); 
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    
	 */ 
1991/0709    
	outb(Int0ctl, 0x11);		/* ICW1 - edge triggered, master, 
					   ICW4 will be sent */ 
	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. 
	 *  Make 8259 interrupts start at CPU vector Int0vec. 
	 *  Set the 8259 as master with edge triggered 
	 *  input with fully nested interrupts. 
	 */ 
	outb(Int1ctl, 0x11);		/* ICW1 - edge triggered, master, 
					   ICW4 will be sent */ 
	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    
} 
 
1991/1113    
char *excname[] = 
{ 
[0]	"divide error", 
[1]	"debug exception", 
[4]	"overflow", 
[5]	"bounds check", 
[6]	"invalid opcode", 
[8]	"double fault", 
[10]	"invalid TSS", 
[11]	"segment not present", 
[12]	"stack exception", 
[13]	"general protection violation", 
}; 
 
1991/0614    
/* 
1991/0703    
 *  All traps 
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; 
1991/0703    
 
1991/0731    
	v = ur->trap; 
 
1991/1112    
	user = ((ur->cs)&0xffff)!=KESEL && v!=Syscallvec; 
	if(user) 
		u->dbgreg = ur; 
 
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){ 
		if(c == Int1vec) 
			outb(Int1ctl, EOI); 
1993/0219    
		outb(Int0ctl, EOI); 
1991/0823    
		if(v != Uart0vec) 
			uartintr0(ur); 
1991/1113    
	} 
 
1993/0217    
	if(v>=256 || (h = halloc.ivec[v]) == 0){ 
1991/1214    
		if(v == 13) 
			return; 
1991/1113    
		if(v <= 16){ 
			if(user){ 
1991/1218    
				sprint(buf, "sys: trap: %s", excname[v]); 
1991/1113    
				postnote(u->p, 1, buf, NDebug); 
				return; 
			} else { 
				dumpregs(ur); 
				panic("%s pc=0x%lux", excname[v], ur->pc); 
			} 
		} 
1993/0220    
		if(badtrap[v]++ < 10 || (badtrap[v]%1000) == 0) 
			print("bad trap type %d pc=0x%lux: total %d\n", v, ur->pc, 
				badtrap[v]); 
1992/0918    
		return; 
1991/0731    
	} 
1991/0801    
 
1993/0217    
	do { 
		(*h->r)(ur); 
		h = h->next; 
	} while(h); 
1991/0910    
 
1991/0913    
	/* 
1992/0108    
	 *  check user since syscall does its own notifying 
1991/0913    
	 */ 
1992/0104    
	if(user && (u->p->procctl || u->nnote)) 
1991/0910    
		notify(ur); 
1991/0806    
} 
 
/* 
1991/0718    
 *  dump registers 
 */ 
void 
dumpregs(Ureg *ur) 
{ 
	if(u) 
		print("registers for %s %d\n", u->p->text, u->p->pid); 
	else 
		print("registers for kernel\n"); 
1991/1214    
	print("FLAGS=%lux ECODE=%lux CS=%lux PC=%lux", ur->flags, 
		ur->ecode, ur->cs&0xff, ur->pc); 
	if(ur == (Ureg*)UREGADDR) 
		print(" SS=%lux USP=%lux\n", ur->ss&0xff, ur->usp); 
	else 
		print("\n"); 
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); 
	print("  DS %4.4ux  ES %4.4ux  FS %4.4ux  GS %4.4ux\n", 
		ur->ds&0xffff, ur->es&0xffff, ur->fs&0xffff, ur->gs&0xffff); 
1991/0718    
} 
 
1991/0720    
void 
dumpstack(void) 
{ 
1992/0804    
	ulong l, v, i; 
	extern ulong etext; 
 
	if(u == 0) 
		return; 
 
	i = 0; 
	for(l=(ulong)&l; l<USERADDR+BY2PG; l+=4){ 
		v = *(ulong*)l; 
		if(KTZERO < v && v < (ulong)&etext){ 
			print("%lux ", v); 
			i++; 
		} 
		if(i == 4){ 
			i = 0; 
			print("\n"); 
		} 
	} 
 
1991/0720    
} 
 
1991/1214    
long 
execregs(ulong entry, ulong ssize, ulong nargs) 
1991/0720    
{ 
1991/1214    
	ulong *sp; 
 
	sp = (ulong*)(USTKTOP - ssize); 
	*--sp = nargs; 
	((Ureg*)UREGADDR)->usp = (ulong)sp; 
1991/0720    
	((Ureg*)UREGADDR)->pc = entry; 
1991/1214    
	return USTKTOP-BY2WD;			/* address of user-level clock */ 
1991/0720    
} 
 
1992/0120    
ulong 
userpc(void) 
{ 
	return ((Ureg*)UREGADDR)->pc; 
} 
 
1991/0718    
/* 
1991/0710    
 *  system calls 
 */ 
1991/0731    
#include "../port/systab.h" 
1991/0720    
 
1992/0103    
/* 
 *  syscall is called spllo() 
 */ 
1991/0710    
long 
syscall(Ureg *ur) 
{ 
1991/0720    
	ulong	sp; 
	long	ret; 
	int	i; 
 
1991/0718    
	u->p->insyscall = 1; 
	u->p->pc = ur->pc; 
1991/0720    
	if((ur->cs)&0xffff == KESEL) 
		panic("recursive system call"); 
1991/0718    
 
1992/06271    
	u->scallnr = ur->ax; 
1992/0805    
	if(u->scallnr == RFORK && u->p->fpstate == FPactive){ 
		/* 
		 *  so that the child starts out with the 
		 *  same registers as the parent 
		 */ 
		splhi(); 
		if(u->p->fpstate == FPactive){ 
			fpsave(&u->fpsave); 
			u->p->fpstate = FPinactive; 
		} 
		spllo(); 
	} 
1991/0720    
	sp = ur->usp; 
	u->nerrlab = 0; 
	ret = -1; 
	if(!waserror()){ 
1992/06271    
		if(u->scallnr >= sizeof systab/BY2WD){ 
			pprint("bad sys call number %d pc %lux\n", u->scallnr, ur->pc); 
1991/0720    
			postnote(u->p, 1, "sys: bad sys call", NDebug); 
			error(Ebadarg); 
		} 
1992/0625    
 
1991/0720    
		if(sp<(USTKTOP-BY2PG) || sp>(USTKTOP-(1+MAXSYSARG)*BY2WD)) 
			validaddr(sp, (1+MAXSYSARG)*BY2WD, 0); 
1992/0625    
 
		u->s = *((Sargs*)(sp+1*BY2WD)); 
1992/06271    
		u->p->psstate = sysctab[u->scallnr]; 
1992/0625    
 
1992/06271    
		ret = (*systab[u->scallnr])(u->s.args); 
1991/0720    
		poperror(); 
	} 
	if(u->nerrlab){ 
1992/06271    
		print("bad errstack [%d]: %d extra\n", u->scallnr, u->nerrlab); 
1991/0720    
		for(i = 0; i < NERR; i++) 
			print("sp=%lux pc=%lux\n", u->errlab[i].sp, u->errlab[i].pc); 
		panic("error stack"); 
	} 
1991/1114    
 
1991/0720    
	u->p->insyscall = 0; 
1991/0926    
	u->p->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    
 
1992/06271    
	if(u->scallnr == NOTED) 
1991/0720    
		noted(ur, *(ulong*)(sp+BY2WD)); 
1991/1114    
 
1992/0103    
	splhi(); /* avoid interrupts during the iret */ 
1992/06271    
	if(u->scallnr!=RFORK && (u->p->procctl || u->nnote)) 
1991/0720    
		notify(ur); 
1992/0804    
 
1991/0720    
	return ret; 
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    
 
1991/1112    
	if(u->p->procctl) 
		procctl(u->p); 
1992/0108    
	if(u->nnote == 0) 
		return 0; 
1991/1114    
 
	s = spllo(); 
1991/1216    
	qlock(&u->p->debug); 
1991/0727    
	u->p->notepending = 0; 
1991/1218    
	n = &u->note[0]; 
	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); 
	} 
	if(n->flag!=NUser && (u->notified || u->notify==0)){ 
1992/0714    
		if(n->flag == NDebug) 
1991/1218    
			pprint("suicide: %s\n", n->msg); 
1991/1216    
		qunlock(&u->p->debug); 
1991/1218    
		pexit(n->msg, n->flag!=NDebug); 
1991/0720    
	} 
1992/0124    
	sent = 0; 
1991/0720    
	if(!u->notified){ 
1992/1203    
		if(!u->notify){ 
			qunlock(&u->p->debug); 
			pexit(n->msg, n->flag!=NDebug); 
		} 
1992/0124    
		sent = 1; 
1991/0720    
		u->svcs = ur->cs; 
		u->svss = ur->ss; 
		u->svflags = ur->flags; 
		sp = ur->usp; 
		sp -= sizeof(Ureg); 
1992/0616    
		if(!okaddr((ulong)u->notify, 1, 0) 
		|| !okaddr(sp-ERRLEN-3*BY2WD, sizeof(Ureg)+ERRLEN-3*BY2WD, 0)){ 
			pprint("suicide: bad address in notify\n"); 
1991/1216    
			qunlock(&u->p->debug); 
1991/0722    
			pexit("Suicide", 0); 
		} 
1991/0720    
		u->ureg = (void*)sp; 
		memmove((Ureg*)sp, ur, sizeof(Ureg)); 
		sp -= ERRLEN; 
		memmove((char*)sp, u->note[0].msg, ERRLEN); 
		sp -= 3*BY2WD; 
		*(ulong*)(sp+2*BY2WD) = sp+3*BY2WD;	/* arg 2 is string */ 
		*(ulong*)(sp+1*BY2WD) = (ulong)u->ureg;	/* arg 1 is ureg* */ 
		*(ulong*)(sp+0*BY2WD) = 0;		/* arg 0 is pc */ 
		ur->usp = sp; 
		ur->pc = (ulong)u->notify; 
		u->notified = 1; 
		u->nnote--; 
		memmove(&u->lastnote, &u->note[0], sizeof(Note)); 
		memmove(&u->note[0], &u->note[1], u->nnote*sizeof(Note)); 
	} 
1991/1216    
	qunlock(&u->p->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; 
 
1992/0609    
	nur = u->ureg;		/* pointer to user returned Ureg struct */ 
1991/0720    
	if(nur->cs!=u->svcs || nur->ss!=u->svss 
1991/0802    
	|| (nur->flags&0xff00)!=(u->svflags&0xff00)){ 
1991/0720    
		pprint("bad noted ureg cs %ux ss %ux flags %ux\n", nur->cs, nur->ss, 
			nur->flags); 
1991/0814    
    Die: 
1991/0720    
		pexit("Suicide", 0); 
	} 
1991/1216    
	qlock(&u->p->debug); 
1991/0720    
	if(!u->notified){ 
1991/0814    
		pprint("call to noted() when not notified\n"); 
1991/1216    
		qunlock(&u->p->debug); 
1991/0720    
		return; 
	} 
	u->notified = 0; 
1992/0609    
	nur->flags = (u->svflags&0xffffff00) | (nur->flags&0xff); 
	memmove(ur, nur, sizeof(Ureg)); 
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"); 
1991/1216    
			qunlock(&u->p->debug); 
1991/0814    
			goto Die; 
		} 
1991/1216    
		qunlock(&u->p->debug); 
1991/0722    
		return; 
1991/0720    
 
	default: 
		pprint("unknown noted arg 0x%lux\n", arg0); 
		u->lastnote.flag = NDebug; 
		/* fall through */ 
		 
	case NDFLT: 
		if(u->lastnote.flag == NDebug) 
			pprint("suicide: %s\n", u->lastnote.msg); 
1991/1216    
		qunlock(&u->p->debug); 
1991/0720    
		pexit(u->lastnote.msg, u->lastnote.flag!=NDebug); 
	} 
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; 
1991/0614    
} 


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