#include "u.h"
#include "../port/lib.h"
#include "mem.h"
#include "dat.h"
#include "fns.h"
#include "io.h"
#include "init.h"
#include <libg.h>
#include <gnot.h>
uchar *intrreg;
void
main(void)
{
u = 0;
memset(edata, 0, (char*)end-(char*)edata);
machinit();
active.exiting = 0;
active.machs = 1;
confinit();
xinit();
mmuinit();
screeninit();
printinit();
pageinit();
trapinit();
kmapinit();
ioinit();
cacheinit();
intrinit();
procinit0();
initseg();
chandevreset();
streaminit();
userinit();
clockinit();
schedinit();
}
void
intrinit(void)
{
KMap *k;
k = kmappa(INTRREG, PTEIO|PTENOCACHE);
intrreg = (uchar*)k->va;
}
void
systemreset(void)
{
delay(100);
putb2(ENAB, ENABRESET);
}
void
machinit(void)
{
int n;
n = m->machno;
memset(m, 0, sizeof(Mach));
m->machno = n;
m->mmask = 1<<m->machno;
m->fpstate = FPinit;
}
void
ioinit(void)
{
KMap *k;
/* tell scc driver it's addresses */
k = kmappa(KMDUART, PTEIO|PTENOCACHE);
sccsetup((void*)(k->va), KMFREQ);
k = kmappa(EIADUART, PTEIO|PTENOCACHE);
sccsetup((void*)(k->va), EIAFREQ);
/* scc port 0 is the keyboard */
sccspecial(0, 0, &kbdq, 2400);
kbdq.putc = kbdstate;
/* scc port 1 is the mouse */
sccspecial(1, 0, &mouseq, 2400);
}
void
init0(void)
{
u->nerrlab = 0;
m->proc = u->p;
u->p->state = Running;
u->p->mach = m;
spllo();
u->slash = (*devtab[0].attach)(0);
u->dot = clone(u->slash, 0);
kproc("alarm", alarmkproc, 0);
chandevinit();
if(!waserror()){
ksetterm("sun %s");
ksetenv("cputype", "sparc");
poperror();
}
touser(USTKTOP-(1+MAXSYSARG)*BY2WD);
}
FPsave initfp;
void
userinit(void)
{
Proc *p;
Segment *s;
User *up;
KMap *k;
p = newproc();
p->pgrp = newpgrp();
p->egrp = smalloc(sizeof(Egrp));
p->egrp->ref = 1;
p->fgrp = smalloc(sizeof(Fgrp));
p->fgrp->ref = 1;
p->procmode = 0640;
strcpy(p->text, "*init*");
strcpy(p->user, eve);
savefpregs(&initfp);
/* initfp.fsr |= 0x1E << 23; /* trap on all but INEXACT */
p->fpstate = FPinit;
/*
* Kernel Stack
*/
p->sched.pc = (((ulong)init0) - 8); /* 8 because of RETURN in gotolabel */
p->sched.sp = USERADDR+BY2PG-(1+MAXSYSARG)*BY2WD;
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;
/*
* 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);
}
void
exit(void)
{
int i;
u = 0;
spllo();
print("cpu %d exiting\n", m->machno);
while(consactive())
for(i=0; i<1000; i++)
;
splhi();
systemreset();
}
Conf conf;
void
confinit(void)
{
int mul;
ulong ktop;
conf.nmach = 1;
if(conf.nmach > MAXMACH)
panic("confinit");
conf.npage0 = (4*1024*1024)/BY2PG; /* BUG */
conf.npage1 = (4*1024*1024)/BY2PG; /* BUG */
conf.base0 = 0;
conf.base1 = 32*1024*1024;
conf.npage = conf.npage0+conf.npage1;
conf.upages = 1400;
ktop = PGROUND((ulong)end);
ktop = PADDR(ktop);
conf.npage0 -= ktop/BY2PG;
conf.base0 += ktop;
mul = conf.upages/700;
conf.nproc = 50*mul;
conf.nswap = 4096;
conf.nimage = 50;
conf.copymode = 0; /* copy on write */
conf.ipif = 8;
conf.ip = 64;
conf.arp = 32;
conf.frag = 32;
conf.cntrlp = 0;
}
/*
* set up the lance
*/
void
lancesetup(Lance *lp)
{
KMap *k;
uchar *cp;
ulong pa, va;
int i, j;
k = kmappa(ETHER, PTEIO|PTENOCACHE);
lp->rdp = (void*)(k->va+0);
lp->rap = (void*)(k->va+2);
k = kmappa(EEPROM, PTEIO|PTENOCACHE);
cp = (uchar*)(k->va+0x7da);
for(i=0; i<6; i++)
lp->ea[i] = *cp++;
kunmap(k);
lp->lognrrb = 7;
lp->logntrb = 5;
lp->nrrb = 1<<lp->lognrrb;
lp->ntrb = 1<<lp->logntrb;
lp->sep = 1;
lp->busctl = BSWP | ACON | BCON;
/*
* Allocate area for lance init block and descriptor rings
*/
pa = (ulong)xspanalloc(BY2PG, BY2PG, 0);
/* map at LANCESEGM */
k = kmappa(pa, PTEMAINMEM|PTENOCACHE);
lp->lanceram = (ushort*)k->va;
lp->lm = (Lancemem*)k->va;
/*
* Allocate space in host memory for the io buffers.
* Allocate a block and kmap it page by page. kmap's are initially
* in reverse order so rearrange them.
*/
i = (lp->nrrb+lp->ntrb)*sizeof(Etherpkt);
i = (i+(BY2PG-1))/BY2PG;
pa = (ulong)xspanalloc(i*BY2PG, BY2PG, 0)&~KZERO;
va = 0;
for(j=i-1; j>=0; j--){
k = kmappa(pa+j*BY2PG, PTEMAINMEM|PTENOCACHE);
if(va){
if(va != k->va+BY2PG)
panic("lancesetup va unordered");
va = k->va;
}
}
/*
* k->va is the base of the region
*/
lp->lrp = (Etherpkt*)k->va;
lp->rp = (Etherpkt*)k->va;
lp->ltp = lp->lrp+lp->nrrb;
lp->tp = lp->rp+lp->nrrb;
}