]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/kw/main.c
kernel: introduce devswap #ΒΆ to serve /dev/swap and handle swapfile encryption
[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         userinit();
326         schedinit();
327         panic("schedinit returned");
328 }
329
330 void
331 cpuidprint(void)
332 {
333         char name[64];
334
335         cputype2name(name, sizeof name);
336         print("cpu%d: %lldMHz ARM %s\n", m->machno, m->cpuhz/1000000, name);
337 }
338
339 void
340 machinit(void)
341 {
342         memset(m, 0, sizeof(Mach));
343         m->machno = 0;
344         machaddr[m->machno] = m;
345
346         m->ticks = 1;
347         m->perf.period = 1;
348
349         conf.nmach = 1;
350
351         active.machs[0] = 1;
352         active.exiting = 0;
353
354         up = nil;
355 }
356
357 /*
358  *  exit kernel either on a panic or user request
359  */
360 void
361 exit(int)
362 {
363         cpushutdown();
364         splhi();
365         archreboot();
366 }
367
368 /*
369  * the new kernel is already loaded at address `code'
370  * of size `size' and entry point `entry'.
371  */
372 void
373 reboot(void *entry, void *code, ulong size)
374 {
375         void (*f)(ulong, ulong, ulong);
376
377         writeconf();
378         cpushutdown();
379
380         /* turn off buffered serial console */
381         serialoq = nil;
382
383         /* shutdown devices */
384         chandevshutdown();
385
386         /* call off the dog */
387         clockshutdown();
388
389         splhi();
390
391         /* setup reboot trampoline function */
392         f = (void*)REBOOTADDR;
393         memmove(f, rebootcode, sizeof(rebootcode));
394         cacheuwbinv();
395         l2cacheuwb();
396
397         /* off we go - never to return */
398         cacheuwbinv();
399         l2cacheuwb();
400         (*f)(PADDR(entry), PADDR(code), size);
401 }
402
403 /*
404  *  starting place for first process
405  */
406 void
407 init0(void)
408 {
409         int i;
410         char buf[2*KNAMELEN];
411
412         assert(up != nil);
413         up->nerrlab = 0;
414         coherence();
415         spllo();
416
417         /*
418          * These are o.k. because rootinit is null.
419          * Then early kproc's will have a root and dot.
420          */
421         up->slash = namec("#/", Atodir, 0, 0);
422         pathclose(up->slash->path);
423         up->slash->path = newpath("/");
424         up->dot = cclone(up->slash);
425
426         chandevinit();
427
428         if(!waserror()){
429                 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
430                 ksetenv("terminal", buf, 0);
431                 ksetenv("cputype", "arm", 0);
432                 if(cpuserver)
433                         ksetenv("service", "cpu", 0);
434                 else
435                         ksetenv("service", "terminal", 0);
436
437                 /* convert plan9.ini variables to #e and #ec */
438                 for(i = 0; i < nconf; i++) {
439                         ksetenv(confname[i], confval[i], 0);
440                         ksetenv(confname[i], confval[i], 1);
441                 }
442                 poperror();
443         }
444         kproc("alarm", alarmkproc, 0);
445
446         touser(sp);
447 }
448
449 static void
450 bootargs(uintptr base)
451 {
452         int i;
453         ulong ssize;
454         char **av, *p;
455
456         /*
457          * Push the boot args onto the stack.
458          * The initial value of the user stack must be such
459          * that the total used is larger than the maximum size
460          * of the argument list checked in syscall.
461          */
462         i = oargblen+1;
463         p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
464         memmove(p, oargb, i);
465
466         /*
467          * Now push argc and the argv pointers.
468          * This isn't strictly correct as the code jumped to by
469          * touser in init9.s calls startboot (port/initcode.c) which
470          * expects arguments
471          *      startboot(char *argv0, char **argv)
472          * not the usual (int argc, char* argv[]), but argv0 is
473          * unused so it doesn't matter (at the moment...).
474          */
475         av = (char**)(p - (oargc+2)*sizeof(char*));
476         ssize = base + BY2PG - PTR2UINT(av);
477         *av++ = (char*)oargc;
478         for(i = 0; i < oargc; i++)
479                 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
480         *av = nil;
481
482         /*
483          * Leave space for the return PC of the
484          * caller of initcode.
485          */
486         sp = USTKTOP - ssize - sizeof(void*);
487 }
488
489 /*
490  *  create the first process
491  */
492 void
493 userinit(void)
494 {
495         Proc *p;
496         Segment *s;
497         KMap *k;
498         Page *pg;
499
500         /* no processes yet */
501         up = nil;
502
503         p = newproc();
504         p->pgrp = newpgrp();
505         p->egrp = smalloc(sizeof(Egrp));
506         p->egrp->ref = 1;
507         p->fgrp = dupfgrp(nil);
508         p->rgrp = newrgrp();
509         p->procmode = 0640;
510
511         kstrdup(&eve, "");
512         kstrdup(&p->text, "*init*");
513         kstrdup(&p->user, eve);
514
515         /*
516          * Kernel Stack
517          */
518         p->sched.pc = PTR2UINT(init0);
519         p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
520         p->sched.sp = STACKALIGN(p->sched.sp);
521
522         /*
523          * User Stack
524          *
525          * Technically, newpage can't be called here because it
526          * should only be called when in a user context as it may
527          * try to sleep if there are no pages available, but that
528          * shouldn't be the case here.
529          */
530         s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
531         p->seg[SSEG] = s;
532         pg = newpage(1, 0, USTKTOP-BY2PG);
533         segpage(s, pg);
534         k = kmap(pg);
535         bootargs(VA(k));
536         kunmap(k);
537
538         /*
539          * Text
540          */
541         s = newseg(SG_TEXT, UTZERO, 1);
542         s->flushme++;
543         p->seg[TSEG] = s;
544         pg = newpage(1, 0, UTZERO);
545         pg->txtflush = ~0;
546         segpage(s, pg);
547         k = kmap(s->map[0]->pages[0]);
548         memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
549         kunmap(k);
550
551         ready(p);
552 }
553
554 Conf conf;                      /* XXX - must go - gag */
555
556 Confmem sheevamem[] = {
557         /*
558          * Memory available to Plan 9:
559          * the 8K is reserved for ethernet dma access violations to scribble on.
560          */
561         { .base = 0, .limit = 512*MB - 8*1024, },
562 };
563
564 void
565 confinit(void)
566 {
567         int i;
568         ulong kpages;
569         uintptr pa;
570
571         /*
572          * Copy the physical memory configuration to Conf.mem.
573          */
574         if(nelem(sheevamem) > nelem(conf.mem)){
575                 iprint("memory configuration botch\n");
576                 exit(1);
577         }
578         memmove(conf.mem, sheevamem, sizeof(sheevamem));
579
580         conf.npage = 0;
581         pa = PADDR(PGROUND(PTR2UINT(end)));
582
583         /*
584          *  we assume that the kernel is at the beginning of one of the
585          *  contiguous chunks of memory and fits therein.
586          */
587         for(i=0; i<nelem(conf.mem); i++){
588                 /* take kernel out of allocatable space */
589                 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
590                         conf.mem[i].base = pa;
591
592                 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
593                 conf.npage += conf.mem[i].npage;
594         }
595
596         conf.upages = (conf.npage*90)/100;
597         conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
598
599         /* only one processor */
600         conf.nmach = 1;
601
602         /* set up other configuration parameters */
603         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
604         if(cpuserver)
605                 conf.nproc *= 3;
606         if(conf.nproc > 2000)
607                 conf.nproc = 2000;
608         conf.nswap = conf.npage*3;
609         conf.nswppo = 4096;
610         conf.nimage = 200;
611
612         conf.copymode = 0;              /* copy on write */
613
614         /*
615          * Guess how much is taken by the large permanent
616          * datastructures. Mntcache and Mntrpc are not accounted for.
617          */
618         kpages = conf.npage - conf.upages;
619         kpages *= BY2PG;
620         kpages -= conf.upages*sizeof(Page)
621                 + conf.nproc*sizeof(Proc)
622                 + conf.nimage*sizeof(Image)
623                 + conf.nswap
624                 + conf.nswppo*sizeof(Page*);
625         mainmem->maxsize = kpages;
626         if(!cpuserver)
627                 /*
628                  * give terminals lots of image memory, too; the dynamic
629                  * allocation will balance the load properly, hopefully.
630                  * be careful with 32-bit overflow.
631                  */
632                 imagmem->maxsize = kpages;
633 }
634
635 int
636 cmpswap(long *addr, long old, long new)
637 {
638         return cas32(addr, old, new);
639 }
640
641 void
642 setupwatchpts(Proc *, Watchpt *, int n)
643 {
644         if(n > 0)
645                 error("no watchpoints");
646 }