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

1992/0715/pc/main.c (diff list | history)

pc/main.c on 1991/0702
1991/0702    
#include	"u.h" 
1992/0321    
#include	"../port/lib.h" 
1991/0706    
#include	"mem.h" 
1991/0702    
#include	"dat.h" 
#include	"fns.h" 
1991/0706    
#include	"io.h" 
1991/0716    
#include	"ureg.h" 
#include	"init.h" 
1991/0629    
 
1991/1210    
int machtype; 
1992/0323    
uchar	*sp;	/* stack pointer for /boot */ 
1991/0711    
 
1991/0716    
void 
1991/0702    
main(void) 
1991/0625    
{ 
1991/1210    
	ident(); 
1991/0827    
	meminit(); 
1991/0711    
	machinit(); 
1991/1113    
	active.exiting = 0; 
	active.machs = 1; 
1991/0711    
	confinit(); 
1992/0625    
	xinit(); 
1991/0703    
	screeninit(); 
1991/0716    
	printinit(); 
1991/0711    
	mmuinit(); 
1992/0625    
	pageinit(); 
1991/0718    
	trapinit(); 
1991/0906    
	mathinit(); 
1991/0718    
	clockinit(); 
	faultinit(); 
1992/0409    
	kbdinit(); 
1991/0716    
	procinit0(); 
	initseg(); 
	chandevreset(); 
	streaminit(); 
	swapinit(); 
	userinit(); 
1991/0711    
 
1991/0716    
	schedinit(); 
1991/0712    
} 
1991/0706    
 
1991/0716    
void 
1991/1210    
ident(void) 
{ 
	char *id = (char*)(ROMBIOS + 0xFF40); 
 
	/* check for a safari (tres special) */ 
	if(strncmp(id, "AT&TNSX", 7) == 0) 
		machtype = Attnsx; 
	else 
		machtype = At; 
} 
 
void 
1991/0716    
machinit(void) 
1991/0712    
{ 
1991/0716    
	int n; 
1991/0712    
 
1991/0716    
	n = m->machno; 
	memset(m, 0, sizeof(Mach)); 
	m->machno = n; 
	m->mmask = 1<<m->machno; 
1991/0625    
} 
 
1991/0719    
ulong garbage; 
1991/0718    
 
1991/0716    
void 
init0(void) 
1991/0712    
{ 
1991/0716    
	u->nerrlab = 0; 
	m->proc = u->p; 
	u->p->state = Running; 
	u->p->mach = m; 
1991/0712    
 
1991/0720    
	spllo(); 
 
1991/0716    
	/* 
	 * These are o.k. because rootinit is null. 
	 * Then early kproc's will have a root and dot. 
	 */ 
	u->slash = (*devtab[0].attach)(0); 
	u->dot = clone(u->slash, 0); 
1991/0712    
 
1992/0516    
	kproc("alarm", alarmkproc, 0); 
1991/0716    
	chandevinit(); 
 
1991/0926    
	if(!waserror()){ 
1992/0429    
		ksetpcinfo(); 
1991/0927    
		ksetenv("cputype", "386"); 
1991/0926    
		poperror(); 
	} 
1992/0323    
	touser(sp); 
1991/0712    
} 
 
1991/0629    
void 
1991/0716    
userinit(void) 
1991/0711    
{ 
1991/0716    
	Proc *p; 
	Segment *s; 
	User *up; 
	KMap *k; 
1992/0323    
	Page *pg; 
1991/0905    
 
1991/0716    
	p = newproc(); 
	p->pgrp = newpgrp(); 
1992/0625    
	p->egrp = smalloc(sizeof(Egrp)); 
	p->egrp->ref = 1; 
	p->fgrp = smalloc(sizeof(Fgrp)); 
	p->fgrp->ref = 1; 
1991/1112    
	p->procmode = 0640; 
1991/0716    
 
	strcpy(p->text, "*init*"); 
1991/1109    
	strcpy(p->user, eve); 
1991/0716    
	p->fpstate = FPinit; 
1991/0906    
	fpoff(); 
1991/0716    
 
	/* 
	 * Kernel Stack 
1991/0717    
	 * 
1991/0719    
	 * N.B. The -12 for the stack pointer is important. 
	 *	4 bytes for gotolabel's return PC 
1991/0716    
	 */ 
	p->sched.pc = (ulong)init0; 
1991/0720    
	p->sched.sp = USERADDR + BY2PG - 4; 
1991/0716    
	p->upage = newpage(1, 0, USERADDR|(p->pid&0xFFFF)); 
 
	/* 
	 * User 
	 */ 
	k = kmap(p->upage); 
	up = (User*)VA(k); 
	up->p = p; 
	kunmap(k); 
 
	/* 
	 * User Stack 
	 */ 
	s = newseg(SG_STACK, USTKTOP-BY2PG, 1); 
	p->seg[SSEG] = s; 
1992/0323    
	pg = newpage(1, 0, USTKTOP-BY2PG); 
	segpage(s, pg); 
	k = kmap(pg); 
	bootargs(VA(k)); 
	kunmap(k); 
1991/0716    
 
	/* 
	 * Text 
	 */ 
	s = newseg(SG_TEXT, UTZERO, 1); 
	p->seg[TSEG] = s; 
	segpage(s, newpage(1, 0, UTZERO)); 
	k = kmap(s->map[0]->pages[0]); 
	memmove((ulong*)VA(k), initcode, sizeof initcode); 
	kunmap(k); 
 
	ready(p); 
1992/0323    
} 
 
uchar * 
pusharg(char *p) 
{ 
	int n; 
 
	n = strlen(p)+1; 
	sp -= n; 
	memmove(sp, p, n); 
	return sp; 
} 
 
void 
bootargs(ulong base) 
{ 
 	int i, ac; 
	uchar *av[32]; 
	uchar **lsp; 
 
	sp = (uchar*)base + BY2PG - MAXSYSARG*BY2WD; 
 
	ac = 0; 
	av[ac++] = pusharg("/386/9safari"); 
	av[ac++] = pusharg("-p"); 
 
	/* 4 byte word align stack */ 
	sp = (uchar*)((ulong)sp & ~3); 
 
	/* build argc, argv on stack */ 
	sp -= (ac+1)*sizeof(sp); 
	lsp = (uchar**)sp; 
	for(i = 0; i < ac; i++) 
		*lsp++ = av[i] + ((USTKTOP - BY2PG) - base); 
	*lsp = 0; 
	sp += (USTKTOP - BY2PG) - base - sizeof(ulong); 
1991/0711    
} 
 
1991/0716    
Conf	conf; 
 
1991/0711    
void 
confinit(void) 
{ 
	long x, i, j, *l; 
	int mul; 
1992/0715    
	ulong ktop; 
1991/0711    
 
	/* 
1991/0823    
	 *  the first 640k is the standard useful memory 
	 *  the next 128K is the display 
1992/0429    
	 *  the last 256k belongs to the roms and other devices 
1991/0711    
	 */ 
1991/0823    
	conf.npage0 = 640/4; 
	conf.base0 = 0; 
 
	/* 
	 *  size the non-standard memory 
	 */ 
1991/0711    
	x = 0x12345678; 
	for(i=1; i<16; i++){ 
		/* 
		 *  write the word 
		 */ 
1992/0625    
		l = (long*)(KZERO|(i*MB)); 
1991/0711    
		*l = x; 
		/* 
		 *  take care of wraps 
		 */ 
		for(j = 0; j < i; j++){ 
1992/0625    
			l = (long*)(KZERO|(j*MB)); 
1991/0711    
			*l = 0; 
		} 
		/* 
		 *  check 
		 */ 
1992/0625    
		l = (long*)(KZERO|(i*MB)); 
1991/0711    
		if(*l != x) 
			break; 
		x += 0x3141526; 
	} 
1992/0625    
	conf.base1 = 1*MB; 
	conf.npage1 = ((i-1)*MB - conf.base1)/BY2PG; 
1991/0711    
	conf.npage = conf.npage0 + conf.npage1; 
1992/0625    
	conf.upages = (conf.npage*70)/100; 
1992/0715    
 
	ktop = PGROUND((ulong)end); 
	ktop = PADDR(ktop); 
	conf.npage0 -= ktop/BY2PG; 
	conf.base0 += ktop; 
1991/0711    
 
1992/0625    
	/* for meminit() */ 
	conf.topofmem = i*MB; 
 
1991/0711    
	mul = 1; 
1992/0103    
	conf.nproc = 30 + i*5; 
1991/0711    
	conf.nswap = conf.nproc*80; 
	conf.nimage = 50; 
	conf.copymode = 0;			/* copy on write */ 
	conf.ipif = 8; 
	conf.ip = 64; 
	conf.arp = 32; 
	conf.frag = 32; 
	conf.cntrlp = 0; 
1992/0429    
	conf.nfloppy = 2; 
1991/0809    
	conf.nhard = 1; 
1992/0625    
 
1992/0609    
	confinit1(mul); 
1991/0711    
} 
 
1991/0913    
char *mathmsg[] = 
1991/0906    
{ 
1991/0913    
	"invalid", 
	"denormalized", 
	"div-by-zero", 
	"overflow", 
	"underflow", 
	"precision", 
	"stack", 
	"error", 
}; 
1991/0906    
 
/* 
1991/0912    
 *  math coprocessor error 
 */ 
void 
1991/0913    
matherror(Ureg *ur) 
1991/0912    
{ 
1991/0913    
	ulong status; 
	int i; 
	char *msg; 
	char note[ERRLEN]; 
 
	/* 
	 *  a write cycle to port 0xF0 clears the interrupt latch attached 
	 *  to the error# line from the 387 
	 */ 
	outb(0xF0, 0xFF); 
 
	status = fpstatus() & 0xffff; 
	msg = "unknown"; 
	for(i = 0; i < 8; i++) 
		if((1<<i) & status){ 
			msg = mathmsg[i]; 
			break; 
		} 
1991/1220    
	sprint(note, "sys: fp: %s, status 0x%ux, pc 0x%lux", msg, status, ur->pc); 
1991/0913    
	postnote(u->p, 1, note, NDebug); 
1991/0912    
} 
 
/* 
1991/0906    
 *  math coprocessor emulation fault 
 */ 
void 
mathemu(Ureg *ur) 
{ 
1992/0711    
	USED(ur); 
1991/0906    
	switch(u->p->fpstate){ 
	case FPinit: 
		fpinit(); 
		u->p->fpstate = FPactive; 
		break; 
	case FPinactive: 
		fprestore(&u->fpsave); 
		u->p->fpstate = FPactive; 
		break; 
	case FPactive: 
1991/0907    
		pexit("Math emu", 0); 
1991/0906    
		break; 
	} 
} 
 
/* 
 *  math coprocessor segment overrun 
 */ 
void 
mathover(Ureg *ur) 
{ 
1992/0711    
	USED(ur); 
1991/0907    
	pexit("Math overrun", 0); 
1991/0906    
} 
 
void 
mathinit(void) 
{ 
1991/0913    
	setvec(Matherr1vec, matherror); 
	setvec(Matherr2vec, matherror); 
1991/0906    
	setvec(Mathemuvec, mathemu); 
	setvec(Mathovervec, mathover); 
} 
 
/* 
1991/0716    
 *  set up floating point for a new process 
 */ 
1991/0703    
void 
1991/0716    
procsetup(Proc *p) 
1991/0703    
{ 
1991/0716    
	p->fpstate = FPinit; 
1991/0906    
	fpoff(); 
1991/0705    
} 
 
1991/0712    
/* 
1991/0906    
 *  Save the mach dependent part of the process state. 
1991/0712    
 */ 
1991/0705    
void 
1992/0122    
procsave(Proc *p) 
1991/0705    
{ 
1992/0711    
	USED(p); 
1991/0906    
	if(u->p->fpstate == FPactive){ 
		fpsave(&u->fpsave); 
		u->p->fpstate = FPinactive; 
	} 
1991/0712    
} 
 
/* 
1991/0716    
 *  Restore what procsave() saves 
1991/0712    
 */ 
1991/0716    
void 
1992/0122    
procrestore(Proc *p) 
1991/0712    
{ 
1992/0711    
	USED(p); 
1991/0712    
} 
 
 
1991/0906    
/* 
1991/1210    
 *  the following functions all are slightly different from 
 *  PC to PC. 
1991/0803    
 */ 
 
1991/1210    
/* enable address bit 20 (extended memory) */ 
1991/0803    
void 
1991/0827    
meminit(void) 
1991/0803    
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		heada20();		/* via headland chip */ 
		break; 
	case At: 
		i8042a20();		/* via keyboard controller */ 
		break; 
	} 
1991/0803    
} 
 
/* 
1991/1210    
 *  reset the i387 chip 
1991/0803    
 */ 
void 
exit(void) 
{ 
	u = 0; 
	print("exiting\n"); 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		headreset();		/* via headland chip */ 
		break; 
	case At: 
1992/0411    
		print("hit the button..."); 
1992/0404    
		for(;;); 
1992/0408    
		putcr3(0);		/* crash and burn */ 
1991/1210    
	} 
1991/0803    
} 
 
/* 
1991/1210    
 *  set cpu speed 
 *	0 == low speed 
 *	1 == high speed 
1991/0803    
 */ 
1991/1210    
int 
cpuspeed(int speed) 
1991/0803    
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		return pmucpuspeed(speed); 
	default: 
		return 0; 
	} 
1991/0803    
} 
 
/* 
1991/1210    
 *  f == frequency (Hz) 
 *  d == duration (ms) 
1991/0803    
 */ 
1991/1210    
void 
buzz(int f, int d) 
1991/0803    
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		pmubuzz(f, d); 
		break; 
	default: 
		break; 
	} 
1991/0803    
} 
 
/* 
1991/1210    
 *  each bit in val stands for a light 
1991/0803    
 */ 
1991/1210    
void 
lights(int val) 
1991/0803    
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		pmulights(val); 
		break; 
	default: 
		break; 
1991/0809    
	} 
1991/0803    
} 
 
/* 
1991/0913    
 *  power to serial port 
1991/1210    
 *	onoff == 1 means on 
 *	onoff == 0 means off 
1991/0803    
 */ 
int 
serial(int onoff) 
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		return pmuserial(onoff); 
	default: 
		return 0; 
	} 
1991/0803    
} 
 
1991/1001    
/* 
 *  power to modem 
1991/1210    
 *	onoff == 1 means on 
 *	onoff == 0 means off 
1991/1001    
 */ 
int 
modem(int onoff) 
{ 
1991/1210    
	switch(machtype){ 
	case Attnsx: 
		return pmumodem(onoff); 
	default: 
		return 0; 
	} 
1991/0803    
} 


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