xref: /plan9-contrib/sys/src/9/rb/main.c (revision dc100ed4690b2e24ee94b6fe674abc915c477a6d)
1 #include	"u.h"
2 #include	"../port/lib.h"
3 #include	"mem.h"
4 #include	"dat.h"
5 #include	"fns.h"
6 #include	"io.h"
7 #include	"init.h"
8 #include	"pool.h"
9 #include	"../ip/ip.h"
10 #include	<tos.h>
11 #include	<bootexec.h>
12 #include	"reboot.h"
13 
14 enum {
15 	/* space for syscall args, return PC, top-of-stack struct */
16 	Stkheadroom	= sizeof(Sargs) + sizeof(uintptr) + sizeof(Tos),
17 };
18 
19 typedef struct mipsexec Mipsexec;
20 
21 /*
22  * Option arguments from the command line.
23  * oargv[0] is the boot file.
24  */
25 static int oargc;
26 static char* oargv[20];
27 static char oargb[128];
28 static int oargblen;
29 
30 static uintptr sp;		/* XXX - must go - user stack of init proc */
31 
32 /*
33  * software tlb simulation
34  */
35 static Softtlb stlb[MAXMACH][STLBSIZE];
36 
37 Conf	conf;
38 FPsave	initfp;
39 Rbconf	rbconf;
40 
41 int normalprint;
42 ulong memsize;
43 
44 char *
getconf(char *)45 getconf(char *)
46 {
47 	return nil;	/* stub */
48 }
49 
50 static void
optionsinit(char * s)51 optionsinit(char* s)
52 {
53 	strecpy(oargb, oargb+sizeof(oargb), s);
54 
55 	oargblen = strlen(oargb);
56 	oargc = tokenize(oargb, oargv, nelem(oargv)-1);
57 	oargv[oargc] = nil;
58 }
59 
60 static void
prcpuid(void)61 prcpuid(void)
62 {
63 	ulong cpuid, cfg1;
64 	char *cpu;
65 
66 	cpuid = prid();
67 	if (((cpuid>>16) & MASK(8)) == 0)		/* vendor */
68 		cpu = "old mips";
69 	else if (((cpuid>>16) & MASK(8)) == 1)
70 		switch ((cpuid>>8) & MASK(8)) {		/* processor */
71 		case 0x93:
72 			cpu = "mips 24k";
73 			break;
74 		case 0x96:
75 			cpu = "mips 24ke";
76 			break;
77 		default:
78 			cpu = "mips";
79 			break;
80 		}
81 	else
82 		cpu = "other mips";
83 	delay(20);
84 	print("cpu%d: %ldMHz %s %se v%ld.%ld rev %ld, ",
85 		m->machno, m->hz / Mhz, cpu, getconfig() & (1<<15)? "b": "l",
86 		(cpuid>>5) & MASK(3), (cpuid>>2) & MASK(3), cpuid & MASK(2));
87 	delay(200);
88 	cfg1 = getconfig1();
89 	print("%s fpu\n", (cfg1 & 1? "has": "no"));
90 	print("cpu%d: %ld tlb entries, using %dK pages\n", m->machno,
91 		((cfg1>>25) & MASK(6)) + 1, BY2PG/1024);
92 	delay(50);
93 	print("cpu%d: l1 i cache: %d sets 4 ways 32 bytes/line\n", m->machno,
94 		64 << ((cfg1>>22) & MASK(3)));
95 	delay(50);
96 	print("cpu%d: l1 d cache: %d sets 4 ways 32 bytes/line\n", m->machno,
97 		64 << ((cfg1>>13) & MASK(3)));
98 	delay(500);
99 	if (0)
100 		print("cpu%d: cycle counter res = %ld\n",
101 			m->machno, gethwreg3());
102 }
103 
104 static void
fmtinit(void)105 fmtinit(void)
106 {
107 	printinit();
108 	quotefmtinstall();
109 	/* ipreset installs these when chandevreset runs */
110 	fmtinstall('i', eipfmt);
111 	fmtinstall('I', eipfmt);
112 	fmtinstall('E', eipfmt);
113 	fmtinstall('V', eipfmt);
114 	fmtinstall('M', eipfmt);
115 }
116 
117 static int
ckpagemask(ulong mask,ulong size)118 ckpagemask(ulong mask, ulong size)
119 {
120 	int s;
121 	ulong pm;
122 
123 	s = splhi();
124 	setpagemask(mask);
125 	pm = getpagemask();
126 	splx(s);
127 	if(pm != mask){
128 		iprint("page size %ldK not supported on this cpu; "
129 			"mask %#lux read back as %#lux\n", size/1024, mask, pm);
130 		return -1;
131 	}
132 	return 0;
133 }
134 
135 /* called from rebootcmd() */
136 int
parsemipsboothdr(Chan * c,ulong magic,Execvals * evp)137 parsemipsboothdr(Chan *c, ulong magic, Execvals *evp)
138 {
139 	long extra;
140 	Mipsexec me;
141 
142 	/*
143 	 * BOOT_MAGIC is sometimes defined like this:
144 	 * #define BOOT_MAGIC	(0x160<<16) || magic == ((0x160<<16)|3)
145 	 * so we can only use it in a fairly stylized manner.
146 	 */
147 	if(magic == BOOT_MAGIC) {
148 		c->offset = 0;			/* back up */
149 		readn(c, &me, sizeof me);
150 		/* if binary is -H1, read an extra long */
151 		if (l2be(me.amagic) == 0407 && me.nscns == 0)
152 			readn(c, &extra, sizeof extra);
153 		evp->entry = l2be(me.mentry);
154 		evp->textsize = l2be(me.tsize);
155 		evp->datasize = l2be(me.dsize);
156 		return 0;
157 	} else
158 		return -1;
159 }
160 
161 /*
162  * parse args kmac=AA:BB:CC:DD:EE:FF for ether0 mac, mem=256M,
163  * HZ=340000000, console=ttyS0,115200.
164  * args seem to live above 8MB, where they are likely to be clobbered
165  * by kernel allocations, so make copies for rebooting.
166  *
167  * at this early stage, we can't use the usual kernel memory allocators,
168  * thus we can't use kstrdup.
169  */
170 static void
saverbconfig(int argc,char ** argv)171 saverbconfig(int argc, char **argv)
172 {
173 	int i;
174 	char *p;
175 	static char ether0mac[32], mem[16], hz[16], console[32];
176 
177 	if (argv == nil)
178 		return;
179 	rbconf.ether0mac = ether0mac;
180 	rbconf.memsize = mem;
181 	rbconf.hz = hz;
182 	rbconf.console = console;
183 	for (i = 0; i < argc; i++) {
184 		p = argv[i];
185 		if (strncmp(p, "kmac=", 5) == 0)
186 			strncpy(rbconf.ether0mac, p, sizeof ether0mac);
187 		else if (strncmp(p, "mem=", 4) == 0)
188 			strncpy(rbconf.memsize, p, sizeof mem);
189 		else if (strncmp(p, "HZ=", 3) == 0)
190 			strncpy(rbconf.hz, p, sizeof hz);
191 		else if (strncmp(p, "console=", 8) == 0)
192 			strncpy(rbconf.console, p, sizeof console);
193 	}
194 	memsize = 256*MB;			/* routerboard default */
195 	p = strchr(rbconf.memsize, '=');
196 	if (p)
197 		memsize = atoi(p+1) * MB;
198 }
199 
200 /* arguments come from routerboot; only argc and argv are non-zero. */
201 void
main(int argc,char ** argv,char ** envp,ulong memsz)202 main(int argc, char **argv, char **envp, ulong memsz)
203 {
204 	stopwdog();			/* tranquilise the dog */
205 	optionsinit("/boot/boot boot");
206 	saverbconfig(argc, argv);
207 	USED(envp, memsz);
208 
209 	confinit();
210 	savefpregs(&initfp);
211 	machinit();			/* calls clockinit */
212 	active.exiting = 0;
213 	cpuactive(0);
214 
215 	kmapinit();
216 	xinit();
217 
218 	timersinit();
219 	fmtinit();
220 	vecinit();
221 
222 	normalprint = 1;
223 	print("\nPlan 9 from Bell Labs (mips)\n");
224 	prcpuid();
225 	if (PTECACHABILITY == PTENONCOHERWT)
226 		print("caches configured as write-through\n");
227 	if (0)
228 		xsummary();
229 
230 	ckpagemask(PGSZ, BY2PG);
231 	tlbinit();
232 	machwire();
233 	pageinit();
234 	procinit0();
235 	initseg();
236 	links();
237 	chandevreset();
238 
239 	swapinit();
240 	userinit();
241 	sicwdog();
242 	parseboothdr = parsemipsboothdr;
243 	schedinit();
244 	panic("schedinit returned");
245 }
246 
247 /*
248  *  initialize a processor's mach structure.  each processor does this
249  *  for itself.
250  */
251 void
machinit(void)252 machinit(void)
253 {
254 	/* Ensure CU1 is off */
255 	clrfpintr();
256 
257 	m->stb = &stlb[m->machno][0];
258 
259 	clockinit();
260 }
261 
262 /*
263  *  setup MIPS trap vectors
264  */
265 void
vecinit(void)266 vecinit(void)
267 {
268 	memmove((ulong*)UTLBMISS, (ulong*)vector0, 0x80);
269 	memmove((ulong*)XEXCEPTION, (ulong*)vector0, 0x80);
270 	memmove((ulong*)CACHETRAP, (ulong*)vector100, 0x80);
271 	memmove((ulong*)EXCEPTION, (ulong*)vector180, 0x80);
272 	memmove((ulong*)(KSEG0+0x200), (ulong*)vector180, 0x80);
273 	icflush((ulong*)UTLBMISS, 4*1024);
274 
275 	setstatus(getstatus() & ~BEV);
276 }
277 
278 void
init0(void)279 init0(void)
280 {
281 	char buf[128];
282 
283 	up->nerrlab = 0;
284 
285 	spllo();
286 
287 	/*
288 	 * These are o.k. because rootinit is null.
289 	 * Then early kproc's will have a root and dot.
290 	 */
291 	up->slash = namec("#/", Atodir, 0, 0);
292 	pathclose(up->slash->path);
293 	up->slash->path = newpath("/");
294 	up->dot = cclone(up->slash);
295 
296 	chandevinit();
297 
298 	if(!waserror()){
299 		ksetenv("cputype", "mips", 0);
300 		snprint(buf, sizeof buf, "mips %s", conffile);
301 		ksetenv("terminal", buf, 0);
302 		if(cpuserver)
303 			ksetenv("service", "cpu", 0);
304 		else
305 			ksetenv("service", "terminal", 0);
306 		/*
307 		 * we don't have a good way to read our cfg file in
308 		 * RouterBOOT, so set the configuration here.
309 		 */
310 		ksetenv("nobootprompt", "tcp", 0);
311 		ksetenv("nvram", "/boot/nvram", 0);
312 		poperror();
313 	}
314 	kproc("alarm", alarmkproc, 0);
315 	i8250console();
316 	touser(sp);
317 }
318 
319 FPsave	initfp;
320 
321 static void
bootargs(uintptr base)322 bootargs(uintptr base)
323 {
324 	int i;
325 	ulong ssize;
326 	char **av, *p;
327 
328 	/*
329 	 * Push the boot args onto the stack.
330 	 * The initial value of the user stack must be such
331 	 * that the total used is larger than the maximum size
332 	 * of the argument list checked in syscall.
333 	 */
334 	i = oargblen+1;
335 	p = (char *)STACKALIGN(base + BY2PG - Stkheadroom - i);
336 	memmove(p, oargb, i);
337 
338 	/*
339 	 * Now push the argv pointers.
340 	 * The code jumped to by touser in lproc.s expects arguments
341 	 *	main(char* argv0, ...)
342 	 * and calls
343 	 * 	startboot("/boot/boot", &argv0)
344 	 * not the usual (int argc, char* argv[])
345 	 */
346 	av = (char**)(p - (oargc+1)*sizeof(char*));
347 	ssize = base + BY2PG - (uintptr)av;
348 	for(i = 0; i < oargc; i++)
349 		*av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
350 	*av = nil;
351 	sp = USTKTOP - ssize;
352 }
353 
354 void
userinit(void)355 userinit(void)
356 {
357 	Proc *p;
358 	KMap *k;
359 	Page *pg;
360 	Segment *s;
361 
362 	p = newproc();
363 	p->pgrp = newpgrp();
364 	p->egrp = smalloc(sizeof(Egrp));
365 	p->egrp->ref = 1;
366 	p->fgrp = dupfgrp(nil);
367 	p->rgrp = newrgrp();
368 	p->procmode = 0640;
369 
370 	kstrdup(&eve, "");
371 	kstrdup(&p->text, "*init*");
372 	kstrdup(&p->user, eve);
373 
374 	p->fpstate = FPinit;
375 	p->fpsave.fpstatus = initfp.fpstatus;
376 
377 	/*
378 	 * Kernel Stack
379 	 */
380 	p->sched.pc = (ulong)init0;
381 	p->sched.sp = (ulong)p->kstack+KSTACK-Stkheadroom;
382 	p->sched.sp = STACKALIGN(p->sched.sp);
383 
384 	/*
385 	 * User Stack
386 	 *
387 	 * Technically, newpage can't be called here because it
388 	 * should only be called when in a user context as it may
389 	 * try to sleep if there are no pages available, but that
390 	 * shouldn't be the case here.
391 	 */
392 	s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
393 	p->seg[SSEG] = s;
394 	pg = newpage(1, 0, USTKTOP-BY2PG);
395 	segpage(s, pg);
396 	k = kmap(pg);
397 	bootargs(VA(k));
398 	kunmap(k);
399 
400 	/*
401 	 * Text
402 	 */
403 	s = newseg(SG_TEXT, UTZERO, 1);
404 	s->flushme++;
405 	p->seg[TSEG] = s;
406 	pg = newpage(1, 0, UTZERO);
407 	memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
408 	segpage(s, pg);
409 	k = kmap(s->map[0]->pages[0]);
410 	memset((void *)VA(k), 0, BY2PG);
411 	memmove((ulong*)VA(k), initcode, sizeof initcode);
412 	kunmap(k);
413 
414 	ready(p);
415 }
416 
417 void
procrestore(Proc * p)418 procrestore(Proc *p)
419 {
420 	uvlong t;
421 
422 	if(p->kp)
423 		return;
424 	cycles(&t);
425 	p->pcycles -= t;
426 }
427 
428 /*
429  *  Save the mach dependent part of the process state.
430  */
431 void
procsave(Proc * p)432 procsave(Proc *p)
433 {
434 	uvlong t;
435 
436 	cycles(&t);
437 	p->pcycles += t;
438 	/* no fpu, so no fp state to save */
439 }
440 
441 static void
writeconf(void)442 writeconf(void)
443 {
444 	char *p, *q;
445 	int n;
446 
447 	p = getconfenv();
448 
449 	if(waserror()) {
450 		free(p);
451 		nexterror();
452 	}
453 
454 	/* convert to name=value\n format */
455 	for(q=p; *q; q++) {
456 		q += strlen(q);
457 		*q = '=';
458 		q += strlen(q);
459 		*q = '\n';
460 	}
461 	n = q - p + 1;
462 #ifdef BOOTARGS_EXIST
463 	if(n >= BOOTARGSLEN)
464 		error("kernel configuration too large");
465 	memmove(BOOTARGS, p, n);
466 	memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
467 #endif
468 	USED(n);
469 	poperror();
470 	free(p);
471 }
472 
473 static void
shutdown(int ispanic)474 shutdown(int ispanic)
475 {
476 	int ms, once;
477 
478 	ilock(&active);
479 	if(ispanic)
480 		active.ispanic = ispanic;
481 	else if(m->machno == 0 && !iscpuactive(m->machno))
482 		active.ispanic = 0;
483 	once = iscpuactive(m->machno);
484 	/*
485 	 * setting exiting will make hzclock() on each processor call exit(0),
486 	 * which calls shutdown(0) and idles non-bootstrap cpus and returns
487 	 * on bootstrap processors (to permit a reboot).  clearing our bit
488 	 * in machs avoids calling exit(0) from hzclock() on this processor.
489 	 */
490 	cpuinactive(m->machno);
491 	active.exiting = 1;
492 	iunlock(&active);
493 
494 	if(once) {
495 		delay(m->machno*1000);		/* stagger them */
496 		iprint("cpu%d: exiting\n", m->machno);
497 	}
498 	spllo();
499 	ms = MAXMACH * 1000;
500 	for(; ms > 0; ms -= TK2MS(2)){
501 		delay(TK2MS(2));
502 		if(active.nmachs == 0 && consactive() == 0)
503 			break;
504 	}
505 	delay(100);
506 }
507 
508 /*
509  * the new kernel is already loaded at address `code'
510  * of size `size' and physical entry point `entry'.
511  */
512 void
reboot(void * entry,void * code,ulong size)513 reboot(void *entry, void *code, ulong size)
514 {
515 	Rbconf *rbc;
516 	void (*f)(void *, ulong, ulong, ulong);
517 
518 	writeconf();
519 	/*
520 	 * copy rbconf and contents into allocated memory, thus safe from
521 	 * being overwritten by the new kernel in the reboot trampoline
522 	 * code below.
523 	 */
524 	rbc = smalloc(sizeof *rbc);
525 	kstrdup(&rbc->ether0mac, rbconf.ether0mac);
526 	kstrdup(&rbc->memsize, rbconf.memsize);
527 	kstrdup(&rbc->hz, rbconf.hz);
528 	kstrdup(&rbc->console, rbconf.console);
529 
530 	/*
531 	 * the boot processor is cpu0.  execute this function on it
532 	 * so that the new kernel has the same cpu0.
533 	 */
534 	if (m->machno != 0) {
535 		procwired(up, 0);
536 		sched();
537 	}
538 	if (m->machno != 0)
539 		print("on cpu%d (not 0)!\n", m->machno);
540 
541 	shutdown(0);
542 
543 	/*
544 	 * should be the only processor running now
545 	 */
546 //	iprint("reboot: entry %#p code %#p size %ld\n", entry, code, size);
547 //	iprint("code[0] = %#lux\n", *(ulong *)code);
548 
549 	/* turn off buffered serial console */
550 	serialoq = nil;
551 	kprintoq = nil;
552 	screenputs = nil;
553 
554 	/* shutdown devices */
555 	chandevshutdown();
556 
557 	/* call off the dog */
558 	clockshutdown();
559 
560 	splhi();
561 	intrshutdown();
562 
563 	/* is the watchdog tied into the usb machinery? */
564 //	*Reset |= Rstusbohcidll | Rstusbhost | Rstusbphy;
565 //		Rstge0mac | Rstge0phy |
566 //		Rstge1mac | Rstge1phy;
567 
568 	/* setup reboot trampoline function */
569 	f = (void*)REBOOTADDR;
570 	memmove(f, rebootcode, sizeof(rebootcode));
571 	dcflush(f, sizeof(rebootcode));
572 	icflush(f, sizeof(rebootcode));
573 
574 	setstatus(BEV);		/* also, kernel mode, no interrupts */
575 	coherence();
576 
577 	/* off we go - never to return */
578 	if (((ulong)entry & KSEGM) == 0)	/* physical address? */
579 		entry = KADDR(entry);		/* make it kernel virtual */
580 	(*f)(rbc, (ulong)entry, (ulong)code, size);
581 
582 	panic("loaded kernel returned!");
583 }
584 
585 void
exit(int type)586 exit(int type)
587 {
588 	int timer;
589 	void (*fnp)(void);
590 
591 	stopwdog();
592 
593 	delay(1000);
594 	lock(&active);
595 	cpuinactive(m->machno);
596 	active.exiting = 1;
597 	unlock(&active);
598 	spllo();
599 
600 	print("cpu %d exiting\n", m->machno);
601 	timer = 0;
602 	while(active.nmachs || consactive()) {
603 		if(timer++ > 400)
604 			break;
605 		delay(10);
606 	}
607 	delay(1000);
608 	splhi();
609 	USED(type);
610 
611 	setstatus(BEV);
612 	coherence();
613 
614 	iprint("exit: awaiting reset\n");
615 	wdogreset();			/* wake the dog with v. short timeout */
616 
617 //	*Reset |= Rstfullchip;
618 //	*Reset |= Rstcpucold;
619 
620 	delay(1000);			/* await a reset */
621 
622 	iprint("exit: jumping to rom\n");
623 	fnp = (void (*)(void))ROM;
624 	(*fnp)();
625 
626 	iprint("exit: looping\n");
627 	for (;;)
628 		;
629 }
630 
631 void
idlehands(void)632 idlehands(void)
633 {
634 	stopwdog();
635 	idle();
636 	sicwdog();			/* wake the dog */
637 }
638 
639 void
confinit(void)640 confinit(void)
641 {
642 	char *p;
643 	ulong kpages, ktop;
644 
645 	/*
646 	 *  divide memory twixt user pages and kernel.
647 	 */
648 	conf.mem[0].base = ktop = PADDR(PGROUND((ulong)end));
649 	assert(memsize > 16*MB);
650 	conf.mem[0].npage = memsize/BY2PG - ktop/BY2PG;
651 	conf.npage = conf.mem[0].npage;
652 	conf.nuart = 1;
653 
654 	kpages = conf.npage - (conf.npage*80)/100;
655 	if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
656 		kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
657 		kpages += (conf.nproc*KSTACK)/BY2PG;
658 	}
659 	conf.upages = conf.npage - kpages;
660 	conf.ialloc = (kpages/2)*BY2PG;
661 
662 	kpages *= BY2PG;
663 	kpages -= conf.upages*sizeof(Page)	/* palloc.pages in pageinit */
664 		+ conf.nproc*sizeof(Proc)  /* procalloc.free in procinit0 */
665 		+ conf.nimage*sizeof(Image)	/* imagealloc.free in initseg */
666 		+ conf.nswap		/* swapalloc.swmap in swapinit */
667 		+ conf.nswppo*sizeof(Page*);	/* iolist in swapinit */
668 	mainmem->maxsize = kpages;
669 
670 	/*
671 	 *  set up CPU's mach structure
672 	 *  cpu0's was zeroed in l.s and our stack is in Mach, so don't zero it.
673 	 */
674 	m->machno = 0;
675 	m->speed = 680;			/* initial guess at MHz, for rb450g */
676 	m->hz = 680 * Mhz;
677 	p = strchr(rbconf.hz, '=');
678 	if (p) {
679 		m->hz = 2 * strtol(p+1, 0, 10);
680 		m->speed = m->hz / Mhz;
681 	}
682 	conf.nmach = 1;
683 
684 	/* set up other configuration parameters */
685 	conf.nproc = 2000;
686 	conf.nswap = 262144;
687 	conf.nswppo = 4096;
688 	conf.nimage = 200;
689 
690 	conf.copymode = 0;		/* copy on write */
691 }
692