]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/kw/main.c
move devusb to port
[plan9front.git] / sys / src / 9 / kw / main.c
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6
7 #include "init.h"
8 #include "arm.h"
9 #include <pool.h>
10
11 #include "reboot.h"
12
13 /*
14  * Where configuration info is left for the loaded programme.
15  * This will turn into a structure as more is done by the boot loader
16  * (e.g. why parse the .ini file twice?).
17  * There are 3584 bytes available at CONFADDR.
18  */
19 #define BOOTARGS        ((char*)CONFADDR)
20 #define BOOTARGSLEN     (16*KiB)                /* limit in devenv.c */
21 #define MAXCONF         64
22 #define MAXCONFLINE     160
23
24 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
25
26 uintptr kseg0 = KZERO;
27 Mach* machaddr[MAXMACH];
28
29 /*
30  * Option arguments from the command line.
31  * oargv[0] is the boot file.
32  * Optionsinit() is called from multiboot()
33  * or some other machine-dependent place
34  * to set it all up.
35  */
36 static int oargc;
37 static char* oargv[20];
38 static char oargb[128];
39 static int oargblen;
40 static char oenv[4096];
41
42 static uintptr sp;              /* XXX - must go - user stack of init proc */
43
44 int vflag;
45 char debug[256];
46
47 /* store plan9.ini contents here at least until we stash them in #ec */
48 static char confname[MAXCONF][KNAMELEN];
49 static char confval[MAXCONF][MAXCONFLINE];
50 static int nconf;
51
52 #ifdef CRYPTOSANDBOX
53 uchar sandbox[64*1024+BY2PG];
54 #endif
55
56 static int
57 findconf(char *name)
58 {
59         int i;
60
61         for(i = 0; i < nconf; i++)
62                 if(cistrcmp(confname[i], name) == 0)
63                         return i;
64         return -1;
65 }
66
67 char*
68 getconf(char *name)
69 {
70         int i;
71
72         i = findconf(name);
73         if(i >= 0)
74                 return confval[i];
75         return nil;
76 }
77
78 void
79 addconf(char *name, char *val)
80 {
81         int i;
82
83         i = findconf(name);
84         if(i < 0){
85                 if(val == nil || nconf >= MAXCONF)
86                         return;
87                 i = nconf++;
88                 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
89         }
90 //      confval[i] = val;
91         strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
92 }
93
94 static void
95 writeconf(void)
96 {
97         char *p, *q;
98         int n;
99
100         p = getconfenv();
101
102         if(waserror()) {
103                 free(p);
104                 nexterror();
105         }
106
107         /* convert to name=value\n format */
108         for(q=p; *q; q++) {
109                 q += strlen(q);
110                 *q = '=';
111                 q += strlen(q);
112                 *q = '\n';
113         }
114         n = q - p + 1;
115         if(n >= BOOTARGSLEN)
116                 error("kernel configuration too large");
117         memmove(BOOTARGS, p, n);
118         poperror();
119         free(p);
120 }
121
122 /*
123  * assumes that we have loaded our /cfg/pxe/mac file at 0x1000 with
124  * tftp in u-boot.  no longer uses malloc, so can be called early.
125  */
126 static void
127 plan9iniinit(void)
128 {
129         char *k, *v, *next;
130
131         k = (char *)CONFADDR;
132         if(!isascii(*k))
133                 return;
134
135         for(; k && *k != '\0'; k = next) {
136                 if (!isascii(*k))               /* sanity check */
137                         break;
138                 next = strchr(k, '\n');
139                 if (next)
140                         *next++ = '\0';
141
142                 if (*k == '\0' || *k == '\n' || *k == '#')
143                         continue;
144                 v = strchr(k, '=');
145                 if(v == nil)
146                         continue;               /* mal-formed line */
147                 *v++ = '\0';
148
149                 addconf(k, v);
150         }
151 }
152
153 static void
154 optionsinit(char* s)
155 {
156         char *o;
157
158         o = strecpy(oargb, oargb+sizeof(oargb), s)+1;
159         if(getenv("bootargs", o, o - oargb) != nil)
160                 *(o-1) = ' ';
161
162         oargblen = strlen(oargb);
163         oargc = tokenize(oargb, oargv, nelem(oargv)-1);
164         oargv[oargc] = nil;
165 }
166
167 char*
168 getenv(char* name, char* buf, int n)
169 {
170         char *e, *p, *q;
171
172         p = oenv;
173         while(*p != 0){
174                 if((e = strchr(p, '=')) == nil)
175                         break;
176                 for(q = name; p < e; p++){
177                         if(*p != *q)
178                                 break;
179                         q++;
180                 }
181                 if(p == e && *q == 0){
182                         strecpy(buf, buf+n, e+1);
183                         return buf;
184                 }
185                 p += strlen(p)+1;
186         }
187
188         return nil;
189 }
190
191 #include "io.h"
192
193 typedef struct Spiregs Spiregs;
194 struct Spiregs {
195         ulong   ictl;           /* interface ctl */
196         ulong   icfg;           /* interface config */
197         ulong   out;            /* data out */
198         ulong   in;             /* data in */
199         ulong   ic;             /* interrupt cause */
200         ulong   im;             /* interrupt mask */
201         ulong   _pad[2];
202         ulong   dwrcfg;         /* direct write config */
203         ulong   dwrhdr;         /* direct write header */
204 };
205
206 enum {
207         /* ictl bits */
208         Csnact  = 1<<0,         /* serial memory activated */
209
210         /* icfg bits */
211         Bytelen = 1<<5,         /* 2^(this_bit) bytes per transfer */
212         Dirrdcmd= 1<<10,        /* flag: fast read */
213 };
214
215 static void
216 dumpbytes(uchar *bp, long max)
217 {
218         iprint("%#p: ", bp);
219         for (; max > 0; max--)
220                 iprint("%02.2ux ", *bp++);
221         iprint("...\n");
222 }
223
224 static void
225 spiprobe(void)
226 {
227         Spiregs *rp = (Spiregs *)soc.spi;
228
229         rp->ictl |= Csnact;
230         coherence();
231         rp->icfg |= Dirrdcmd | 3<<8;    /* fast reads, 4-byte addresses */
232         rp->icfg &= ~Bytelen;           /* one-byte reads */
233         coherence();
234
235 //      print("spi flash at %#ux: memory reads enabled\n", PHYSSPIFLASH);
236 }
237
238 void    archconsole(void);
239
240 /* dummy for usb */
241 int
242 isaconfig(char *, int, ISAConf *)
243 {
244         return 0;
245 }
246
247 /*
248  * entered from l.s with mmu enabled.
249  *
250  * we may have to realign the data segment; apparently 5l -H0 -R4096
251  * does not pad the text segment.  on the other hand, we may have been
252  * loaded by another kernel.
253  *
254  * be careful not to touch the data segment until we know it's aligned.
255  */
256 void
257 main(Mach* mach)
258 {
259         extern char bdata[], edata[], end[], etext[];
260         static ulong vfy = 0xcafebabe;
261
262         m = mach;
263         if (vfy != 0xcafebabe)
264                 memmove(bdata, etext, edata - bdata);
265         if (vfy != 0xcafebabe) {
266                 wave('?');
267                 panic("misaligned data segment");
268         }
269         memset(edata, 0, end - edata);          /* zero bss */
270         vfy = 0;
271
272 wave('9');
273         machinit();
274         archreset();
275         mmuinit();
276
277         optionsinit("/boot/boot boot");
278         quotefmtinstall();
279         archconsole();
280 wave(' ');
281
282         /* want plan9.ini to be able to affect memory sizing in confinit */
283         plan9iniinit();         /* before we step on plan9.ini in low memory */
284
285         confinit();
286         /* xinit would print if it could */
287         xinit();
288
289         /*
290          * Printinit will cause the first malloc call.
291          * (printinit->qopen->malloc) unless any of the
292          * above (like clockintr) do an irqenable, which
293          * will call malloc.
294          * If the system dies here it's probably due
295          * to malloc(->xalloc) not being initialised
296          * correctly, or the data segment is misaligned
297          * (it's amazing how far you can get with
298          * things like that completely broken).
299          *
300          * (Should be) boilerplate from here on.
301          */
302         trapinit();
303         clockinit();
304
305         printinit();
306         uartkirkwoodconsole();
307         /* only now can we print */
308         print("from Bell Labs\n\n");
309
310 #ifdef CRYPTOSANDBOX
311         print("sandbox: 64K at physical %#lux, mapped to 0xf10b0000\n",
312                 PADDR((uintptr)sandbox & ~(BY2PG-1)));
313 #endif
314
315         archconfinit();
316         cpuidprint();
317         timersinit();
318
319         procinit0();
320         initseg();
321         links();
322         chandevreset();                 /* most devices are discovered here */
323
324         pageinit();
325         swapinit();
326         userinit();
327         schedinit();
328         panic("schedinit returned");
329 }
330
331 void
332 cpuidprint(void)
333 {
334         char name[64];
335
336         cputype2name(name, sizeof name);
337         print("cpu%d: %lldMHz ARM %s\n", m->machno, m->cpuhz/1000000, name);
338 }
339
340 void
341 machinit(void)
342 {
343         memset(m, 0, sizeof(Mach));
344         m->machno = 0;
345         machaddr[m->machno] = m;
346
347         m->ticks = 1;
348         m->perf.period = 1;
349
350         conf.nmach = 1;
351
352         active.machs = 1;
353         active.exiting = 0;
354
355         up = nil;
356 }
357
358 static void
359 shutdown(int ispanic)
360 {
361         int ms, once;
362
363         lock(&active);
364         if(ispanic)
365                 active.ispanic = ispanic;
366         else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
367                 active.ispanic = 0;
368         once = active.machs & (1<<m->machno);
369         active.machs &= ~(1<<m->machno);
370         active.exiting = 1;
371         unlock(&active);
372
373         if(once)
374                 iprint("cpu%d: exiting\n", m->machno);
375         spllo();
376         for(ms = 5*1000; ms > 0; ms -= TK2MS(2)){
377                 delay(TK2MS(2));
378                 if(active.machs == 0 && consactive() == 0)
379                         break;
380         }
381         delay(1000);
382 }
383
384 /*
385  *  exit kernel either on a panic or user request
386  */
387 void
388 exit(int code)
389 {
390         shutdown(code);
391         splhi();
392         archreboot();
393 }
394
395 /*
396  * the new kernel is already loaded at address `code'
397  * of size `size' and entry point `entry'.
398  */
399 void
400 reboot(void *entry, void *code, ulong size)
401 {
402         void (*f)(ulong, ulong, ulong);
403
404         iprint("starting reboot...");
405         writeconf();
406         
407         shutdown(0);
408
409         /*
410          * should be the only processor running now
411          */
412
413         print("shutting down...\n");
414         delay(200);
415
416         /* turn off buffered serial console */
417         serialoq = nil;
418
419         /* shutdown devices */
420         chandevshutdown();
421
422         /* call off the dog */
423         clockshutdown();
424
425         splhi();
426
427         /* setup reboot trampoline function */
428         f = (void*)REBOOTADDR;
429         memmove(f, rebootcode, sizeof(rebootcode));
430         cacheuwbinv();
431         l2cacheuwb();
432
433         print("rebooting...");
434         iprint("entry %#lux code %#lux size %ld\n",
435                 PADDR(entry), PADDR(code), size);
436         delay(100);             /* wait for uart to quiesce */
437
438         /* off we go - never to return */
439         cacheuwbinv();
440         l2cacheuwb();
441         (*f)(PADDR(entry), PADDR(code), size);
442
443         iprint("loaded kernel returned!\n");
444         delay(1000);
445         archreboot();
446 }
447
448 /*
449  *  starting place for first process
450  */
451 void
452 init0(void)
453 {
454         int i;
455         char buf[2*KNAMELEN];
456
457         assert(up != nil);
458         up->nerrlab = 0;
459         coherence();
460         spllo();
461
462         /*
463          * These are o.k. because rootinit is null.
464          * Then early kproc's will have a root and dot.
465          */
466         up->slash = namec("#/", Atodir, 0, 0);
467         pathclose(up->slash->path);
468         up->slash->path = newpath("/");
469         up->dot = cclone(up->slash);
470
471         chandevinit();
472
473         if(!waserror()){
474                 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
475                 ksetenv("terminal", buf, 0);
476                 ksetenv("cputype", "arm", 0);
477                 if(cpuserver)
478                         ksetenv("service", "cpu", 0);
479                 else
480                         ksetenv("service", "terminal", 0);
481
482                 /* convert plan9.ini variables to #e and #ec */
483                 for(i = 0; i < nconf; i++) {
484                         ksetenv(confname[i], confval[i], 0);
485                         ksetenv(confname[i], confval[i], 1);
486                 }
487                 poperror();
488         }
489         kproc("alarm", alarmkproc, 0);
490
491         touser(sp);
492 }
493
494 static void
495 bootargs(uintptr base)
496 {
497         int i;
498         ulong ssize;
499         char **av, *p;
500
501         /*
502          * Push the boot args onto the stack.
503          * The initial value of the user stack must be such
504          * that the total used is larger than the maximum size
505          * of the argument list checked in syscall.
506          */
507         i = oargblen+1;
508         p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
509         memmove(p, oargb, i);
510
511         /*
512          * Now push argc and the argv pointers.
513          * This isn't strictly correct as the code jumped to by
514          * touser in init9.s calls startboot (port/initcode.c) which
515          * expects arguments
516          *      startboot(char *argv0, char **argv)
517          * not the usual (int argc, char* argv[]), but argv0 is
518          * unused so it doesn't matter (at the moment...).
519          */
520         av = (char**)(p - (oargc+2)*sizeof(char*));
521         ssize = base + BY2PG - PTR2UINT(av);
522         *av++ = (char*)oargc;
523         for(i = 0; i < oargc; i++)
524                 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
525         *av = nil;
526
527         /*
528          * Leave space for the return PC of the
529          * caller of initcode.
530          */
531         sp = USTKTOP - ssize - sizeof(void*);
532 }
533
534 /*
535  *  create the first process
536  */
537 void
538 userinit(void)
539 {
540         Proc *p;
541         Segment *s;
542         KMap *k;
543         Page *pg;
544
545         /* no processes yet */
546         up = nil;
547
548         p = newproc();
549         p->pgrp = newpgrp();
550         p->egrp = smalloc(sizeof(Egrp));
551         p->egrp->ref = 1;
552         p->fgrp = dupfgrp(nil);
553         p->rgrp = newrgrp();
554         p->procmode = 0640;
555
556         kstrdup(&eve, "");
557         kstrdup(&p->text, "*init*");
558         kstrdup(&p->user, eve);
559
560         /*
561          * Kernel Stack
562          */
563         p->sched.pc = PTR2UINT(init0);
564         p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
565         p->sched.sp = STACKALIGN(p->sched.sp);
566
567         /*
568          * User Stack
569          *
570          * Technically, newpage can't be called here because it
571          * should only be called when in a user context as it may
572          * try to sleep if there are no pages available, but that
573          * shouldn't be the case here.
574          */
575         s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
576         p->seg[SSEG] = s;
577         pg = newpage(1, 0, USTKTOP-BY2PG);
578         segpage(s, pg);
579         k = kmap(pg);
580         bootargs(VA(k));
581         kunmap(k);
582
583         /*
584          * Text
585          */
586         s = newseg(SG_TEXT, UTZERO, 1);
587         s->flushme++;
588         p->seg[TSEG] = s;
589         pg = newpage(1, 0, UTZERO);
590         memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
591         segpage(s, pg);
592         k = kmap(s->map[0]->pages[0]);
593         memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
594         kunmap(k);
595
596         ready(p);
597 }
598
599 Conf conf;                      /* XXX - must go - gag */
600
601 Confmem sheevamem[] = {
602         /*
603          * Memory available to Plan 9:
604          * the 8K is reserved for ethernet dma access violations to scribble on.
605          */
606         { .base = 0, .limit = 512*MB - 8*1024, },
607 };
608
609 void
610 confinit(void)
611 {
612         int i;
613         ulong kpages;
614         uintptr pa;
615
616         /*
617          * Copy the physical memory configuration to Conf.mem.
618          */
619         if(nelem(sheevamem) > nelem(conf.mem)){
620                 iprint("memory configuration botch\n");
621                 exit(1);
622         }
623         memmove(conf.mem, sheevamem, sizeof(sheevamem));
624
625         conf.npage = 0;
626         pa = PADDR(PGROUND(PTR2UINT(end)));
627
628         /*
629          *  we assume that the kernel is at the beginning of one of the
630          *  contiguous chunks of memory and fits therein.
631          */
632         for(i=0; i<nelem(conf.mem); i++){
633                 /* take kernel out of allocatable space */
634                 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
635                         conf.mem[i].base = pa;
636
637                 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
638                 conf.npage += conf.mem[i].npage;
639         }
640
641         conf.upages = (conf.npage*90)/100;
642         conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
643
644         /* only one processor */
645         conf.nmach = 1;
646
647         /* set up other configuration parameters */
648         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
649         if(cpuserver)
650                 conf.nproc *= 3;
651         if(conf.nproc > 2000)
652                 conf.nproc = 2000;
653         conf.nswap = conf.npage*3;
654         conf.nswppo = 4096;
655         conf.nimage = 200;
656
657         conf.copymode = 0;              /* copy on write */
658
659         /*
660          * Guess how much is taken by the large permanent
661          * datastructures. Mntcache and Mntrpc are not accounted for
662          * (probably ~300KB).
663          */
664         kpages = conf.npage - conf.upages;
665         kpages *= BY2PG;
666         kpages -= conf.upages*sizeof(Page)
667                 + conf.nproc*sizeof(Proc)
668                 + conf.nimage*sizeof(Image)
669                 + conf.nswap
670                 + conf.nswppo*sizeof(Page*);
671         mainmem->maxsize = kpages;
672         if(!cpuserver)
673                 /*
674                  * give terminals lots of image memory, too; the dynamic
675                  * allocation will balance the load properly, hopefully.
676                  * be careful with 32-bit overflow.
677                  */
678                 imagmem->maxsize = kpages;
679 }
680
681 int
682 cmpswap(long *addr, long old, long new)
683 {
684         return cas32(addr, old, new);
685 }