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