]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/teg2/main.c
merge
[plan9front.git] / sys / src / 9 / teg2 / 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 "arm.h"
12 #include "reboot.h"
13
14 /*
15  * Where configuration info is left for the loaded programme.
16  * This will turn into a structure as more is done by the boot loader
17  * (e.g. why parse the .ini file twice?).
18  * There are 3584 bytes available at CONFADDR.
19  */
20 #define BOOTARGS        ((char*)CONFADDR)
21 #define BOOTARGSLEN     (16*KiB)                /* limit in devenv.c */
22 #define MAXCONF         64
23 #define MAXCONFLINE     160
24
25 enum {
26         Minmem  = 256*MB,                       /* conservative default */
27 };
28
29 #define isascii(c) ((uchar)(c) > 0 && (uchar)(c) < 0177)
30
31 extern char bdata[], edata[], end[], etext[];
32
33 uintptr kseg0 = KZERO;
34 Mach* machaddr[MAXMACH];
35 uchar *l2pages;
36
37 Memcache cachel[8];             /* arm arch v7 supports 1-7 */
38 /*
39  * these are used by the cache.v7.s routines.
40  */
41 Lowmemcache *cacheconf;
42
43 /*
44  * Option arguments from the command line.
45  * oargv[0] is the boot file.
46  * Optionsinit() is called from multiboot()
47  * or some other machine-dependent place
48  * to set it all up.
49  */
50 static int oargc;
51 static char* oargv[20];
52 static char oargb[128];
53 static int oargblen;
54 static char oenv[4096];
55
56 static uintptr sp;              /* XXX - must go - user stack of init proc */
57
58 int vflag;
59 int normalprint;
60 char debug[256];
61
62 static Lock testlock;
63
64 /* store plan9.ini contents here at least until we stash them in #ec */
65 static char confname[MAXCONF][KNAMELEN];
66 static char confval[MAXCONF][MAXCONFLINE];
67 static int nconf;
68
69 static int
70 findconf(char *name)
71 {
72         int i;
73
74         for(i = 0; i < nconf; i++)
75                 if(cistrcmp(confname[i], name) == 0)
76                         return i;
77         return -1;
78 }
79
80 char*
81 getconf(char *name)
82 {
83         int i;
84
85         i = findconf(name);
86         if(i >= 0)
87                 return confval[i];
88         return nil;
89 }
90
91 void
92 addconf(char *name, char *val)
93 {
94         int i;
95
96         i = findconf(name);
97         if(i < 0){
98                 if(val == nil || nconf >= MAXCONF)
99                         return;
100                 i = nconf++;
101                 strecpy(confname[i], confname[i]+sizeof(confname[i]), name);
102         }
103 //      confval[i] = val;
104         strecpy(confval[i], confval[i]+sizeof(confval[i]), val);
105 }
106
107 static void
108 writeconf(void)
109 {
110         char *p, *q;
111         int n;
112
113         p = getconfenv();
114
115         if(waserror()) {
116                 free(p);
117                 nexterror();
118         }
119
120         /* convert to name=value\n format */
121         for(q=p; *q; q++) {
122                 q += strlen(q);
123                 *q = '=';
124                 q += strlen(q);
125                 *q = '\n';
126         }
127         n = q - p + 1;
128         if(n >= BOOTARGSLEN)
129                 error("kernel configuration too large");
130         memmove(BOOTARGS, p, n);
131         memset(BOOTARGS + n, '\n', BOOTARGSLEN - n);
132         poperror();
133         free(p);
134 }
135
136 /*
137  * assumes that we have loaded our /cfg/pxe/mac file at CONFADDR
138  * (usually 0x1000) with tftp in u-boot.  no longer uses malloc, so
139  * can be called early.
140  */
141 static void
142 plan9iniinit(void)
143 {
144         char *k, *v, *next;
145
146         k = (char *)CONFADDR;
147         if(!isascii(*k))
148                 return;
149
150         for(; k && *k != '\0'; k = next) {
151                 if (!isascii(*k))               /* sanity check */
152                         break;
153                 next = strchr(k, '\n');
154                 if (next)
155                         *next++ = '\0';
156
157                 if (*k == '\0' || *k == '\n' || *k == '#')
158                         continue;
159                 v = strchr(k, '=');
160                 if(v == nil)
161                         continue;               /* mal-formed line */
162                 *v++ = '\0';
163
164                 addconf(k, v);
165         }
166 }
167
168 static void
169 optionsinit(char* s)
170 {
171         char *o;
172
173         strcpy(oenv, "");
174         o = strecpy(oargb, oargb+sizeof(oargb), s)+1;
175         if(getenv("bootargs", o, o - oargb) != nil)
176                 *(o-1) = ' ';
177
178         oargblen = strlen(oargb);
179         oargc = tokenize(oargb, oargv, nelem(oargv)-1);
180         oargv[oargc] = nil;
181 }
182
183 char*
184 getenv(char* name, char* buf, int n)
185 {
186         char *e, *p, *q;
187
188         p = oenv;
189         while(*p != 0){
190                 if((e = strchr(p, '=')) == nil)
191                         break;
192                 for(q = name; p < e; p++){
193                         if(*p != *q)
194                                 break;
195                         q++;
196                 }
197                 if(p == e && *q == 0){
198                         strecpy(buf, buf+n, e+1);
199                         return buf;
200                 }
201                 p += strlen(p)+1;
202         }
203
204         return nil;
205 }
206
207 /* enable scheduling of this cpu */
208 void
209 machon(uint cpu)
210 {
211         ulong cpubit;
212
213         cpubit = 1 << cpu;
214         lock(&active);
215         if ((active.machs & cpubit) == 0) {     /* currently off? */
216                 conf.nmach++;
217                 active.machs |= cpubit;
218         }
219         unlock(&active);
220 }
221
222 /* disable scheduling of this cpu */
223 void
224 machoff(uint cpu)
225 {
226         ulong cpubit;
227
228         cpubit = 1 << cpu;
229         lock(&active);
230         if (active.machs & cpubit) {            /* currently on? */
231                 conf.nmach--;
232                 active.machs &= ~cpubit;
233         }
234         unlock(&active);
235 }
236
237 void
238 machinit(void)
239 {
240         Mach *m0;
241
242         if (m == 0) {
243                 serialputc('?');
244                 serialputc('m');
245                 serialputc('0');
246         }
247         if(machaddr[m->machno] != m) {
248                 serialputc('?');
249                 serialputc('m');
250                 serialputc('m');
251         }
252
253         if (canlock(&testlock)) {
254                 serialputc('?');
255                 serialputc('l');
256                 panic("cpu%d: locks don't work", m->machno);
257         }
258
259         m->ticks = 1;
260         m->perf.period = 1;
261         m0 = MACHP(0);
262         if (m->machno != 0) {
263                 /* synchronise with cpu 0 */
264                 m->ticks = m0->ticks;
265                 m->fastclock = m0->fastclock;
266                 m->cpuhz = m0->cpuhz;
267                 m->delayloop = m0->delayloop;
268         }
269         if (m->machno != 0 &&
270             (m->fastclock == 0 || m->cpuhz == 0 || m->delayloop == 0))
271                 panic("buggered cpu 0 Mach");
272
273         machon(m->machno);
274         fpoff();
275 }
276
277 /* l.s has already zeroed Mach, which now contains our stack. */
278 void
279 mach0init(void)
280 {
281         if (m == 0) {
282                 serialputc('?');
283                 serialputc('m');
284         }
285         conf.nmach = 0;
286
287         m->machno = 0;
288         machaddr[0] = m;
289
290         lock(&testlock);                /* hold this forever */
291         machinit();
292
293         active.exiting = 0;
294         l1cache->wbse(&active, sizeof active);
295         up = nil;
296 }
297
298 /*
299  *  count CPU's, set up their mach structures and l1 ptes.
300  *  we're running on cpu 0 and our data structures were
301  *  statically allocated.
302  */
303 void
304 launchinit(void)
305 {
306         int mach;
307         Mach *mm;
308         PTE *l1;
309
310         for(mach = 1; mach < MAXMACH; mach++){
311                 machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
312                 l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
313                 if(mm == nil || l1 == nil)
314                         panic("launchinit");
315                 memset(mm, 0, MACHSIZE);
316                 mm->machno = mach;
317
318                 memmove(l1, (void *)L1, L1SIZE);  /* clone cpu0's l1 table */
319                 l1cache->wbse(l1, L1SIZE);
320
321                 mm->mmul1 = l1;
322                 l1cache->wbse(mm, MACHSIZE);
323         }
324         l1cache->wbse(machaddr, sizeof machaddr);
325         conf.nmach = 1;
326 }
327
328 void
329 dump(void *vaddr, int words)
330 {
331         ulong *addr;
332
333         addr = vaddr;
334         while (words-- > 0)
335                 iprint("%.8lux%c", *addr++, words % 8 == 0? '\n': ' ');
336 }
337
338 static void
339 cacheinit(void)
340 {
341         allcacheinfo(cachel);
342         cacheconf = (Lowmemcache *)CACHECONF;
343         cacheconf->l1waysh = cachel[1].waysh;
344         cacheconf->l1setsh = cachel[1].setsh;
345         /* on the tegra 2, l2 is unarchitected */
346         cacheconf->l2waysh = cachel[2].waysh;
347         cacheconf->l2setsh = cachel[2].setsh;
348
349         l2pl310init();
350         allcacheson();
351         allcache->wb();
352 }
353
354 void
355 l2pageinit(void)
356 {
357         l2pages = KADDR(PHYSDRAM + DRAMSIZE - RESRVDHIMEM);
358 }
359
360 /*
361  * at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
362  * but has not zeroed bss.
363  */
364 void
365 main(void)
366 {
367         int cpu;
368         static ulong vfy = 0xcafebabe;
369
370         up = nil;
371         if (vfy != 0xcafebabe) {
372                 serialputc('?');
373                 serialputc('d');
374                 panic("data segment misaligned");
375         }
376
377         memset(edata, 0, end - edata);
378
379         /*
380          * we can't lock until smpon has run, but we're supposed to wait
381          * until l1 & l2 are on.  too bad.  l1 is on, l2 will soon be.
382          */
383         smpon();
384         iprint("ll Labs ");
385         cacheinit();
386
387         /*
388          * data segment is aligned, bss is zeroed, caches' characteristics
389          * are known.  begin initialisation.
390          */
391         mach0init();
392         l2pageinit();
393         mmuinit();
394
395         optionsinit("/boot/boot boot");
396         quotefmtinstall();
397
398         /* want plan9.ini to be able to affect memory sizing in confinit */
399         plan9iniinit();         /* before we step on plan9.ini in low memory */
400
401         /* l2 looks for *l2off= in plan9.ini */
402         l2cache->on();          /* l2->on requires locks to work, thus smpon */
403         l2cache->info(&cachel[2]);
404         allcache->on();
405
406         cortexa9cachecfg();
407
408         trapinit();             /* so confinit can probe memory to size it */
409         confinit();             /* figures out amount of memory */
410         /* xinit prints (if it can), so finish up the banner here. */
411         delay(100);
412         navailcpus = getncpus();
413         iprint("(mp arm; %d cpus)\n\n", navailcpus);
414         delay(100);
415
416         for (cpu = 1; cpu < navailcpus; cpu++)
417                 stopcpu(cpu);
418
419         xinit();
420         irqtooearly = 0;        /* now that xinit and trapinit have run */
421
422         mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
423
424         /*
425          * Printinit will cause the first malloc call.
426          * (printinit->qopen->malloc) unless any of the
427          * above (like clockinit) do an irqenable, which
428          * will call malloc.
429          * If the system dies here it's probably due
430          * to malloc(->xalloc) not being initialised
431          * correctly, or the data segment is misaligned
432          * (it's amazing how far you can get with
433          * things like that completely broken).
434          *
435          * (Should be) boilerplate from here on.
436          */
437
438         archreset();                    /* cfg clock signals, print cache cfg */
439         clockinit();                    /* start clocks */
440         timersinit();
441
442         delay(50);                      /* let uart catch up */
443         printinit();
444
445         cpuidprint();
446         chkmissing();
447
448         procinit0();
449         initseg();
450
451 //      dmainit();
452         links();
453         conf.monitor = 1;
454 //      screeninit();
455
456         iprint("pcireset...");
457         pcireset();                     /* this tends to hang after a reboot */
458         iprint("ok\n");
459
460         chandevreset();                 /* most devices are discovered here */
461 //      i8250console();                 /* too early; see init0 */
462
463         pageinit();                     /* prints "1020M memory: â‹¯ */
464         swapinit();
465         userinit();
466
467         /*
468          * starting a cpu will eventually result in it calling schedinit,
469          * so everything necessary to run user processes should be set up
470          * before starting secondary cpus.
471          */
472         launchinit();
473         /* SMP & FW are already on when we get here; u-boot set them? */
474         for (cpu = 1; cpu < navailcpus; cpu++)
475                 if (startcpu(cpu) < 0)
476                         panic("cpu%d didn't start", cpu);
477         l1diag();
478
479         schedinit();
480         panic("cpu%d: schedinit returned", m->machno);
481 }
482
483 static void
484 shutdown(int ispanic)
485 {
486         int ms, once;
487
488         lock(&active);
489         if(ispanic)
490                 active.ispanic = ispanic;
491         else if(m->machno == 0 && (active.machs & (1<<m->machno)) == 0)
492                 active.ispanic = 0;
493         once = active.machs & (1<<m->machno);
494         /*
495          * setting exiting will make hzclock() on each processor call exit(0),
496          * which calls shutdown(0) and idles non-bootstrap cpus and returns
497          * on bootstrap processors (to permit a reboot).  clearing our bit
498          * in machs avoids calling exit(0) from hzclock() on this processor.
499          */
500         active.machs &= ~(1<<m->machno);
501         active.exiting = 1;
502         unlock(&active);
503
504         if(once) {
505                 delay(m->machno*1000);          /* stagger them */
506                 iprint("cpu%d: exiting\n", m->machno);
507         }
508         spllo();
509         if (m->machno == 0)
510                 ms = 5*1000;
511         else
512                 ms = 2*1000;
513         for(; ms > 0; ms -= TK2MS(2)){
514                 delay(TK2MS(2));
515                 if(active.machs == 0 && consactive() == 0)
516                         break;
517         }
518         delay(500);
519 }
520
521 /*
522  *  exit kernel either on a panic or user request
523  */
524 void
525 exit(int code)
526 {
527         shutdown(code);
528         splhi();
529         if (m->machno == 0)
530                 archreboot();
531         else {
532                 intrcpushutdown();
533                 stopcpu(m->machno);
534                 for (;;)
535                         idlehands();
536         }
537 }
538
539 int
540 isaconfig(char *class, int ctlrno, ISAConf *isa)
541 {
542         char cc[32], *p;
543         int i;
544
545         snprint(cc, sizeof cc, "%s%d", class, ctlrno);
546         p = getconf(cc);
547         if(p == nil)
548                 return 0;
549
550         isa->type = "";
551         isa->nopt = tokenize(p, isa->opt, NISAOPT);
552         for(i = 0; i < isa->nopt; i++){
553                 p = isa->opt[i];
554                 if(cistrncmp(p, "type=", 5) == 0)
555                         isa->type = p + 5;
556                 else if(cistrncmp(p, "port=", 5) == 0)
557                         isa->port = strtoul(p+5, &p, 0);
558                 else if(cistrncmp(p, "irq=", 4) == 0)
559                         isa->irq = strtoul(p+4, &p, 0);
560                 else if(cistrncmp(p, "dma=", 4) == 0)
561                         isa->dma = strtoul(p+4, &p, 0);
562                 else if(cistrncmp(p, "mem=", 4) == 0)
563                         isa->mem = strtoul(p+4, &p, 0);
564                 else if(cistrncmp(p, "size=", 5) == 0)
565                         isa->size = strtoul(p+5, &p, 0);
566                 else if(cistrncmp(p, "freq=", 5) == 0)
567                         isa->freq = strtoul(p+5, &p, 0);
568         }
569         return 1;
570 }
571
572 /*
573  * the new kernel is already loaded at address `code'
574  * of size `size' and entry point `entry'.
575  */
576 void
577 reboot(void *entry, void *code, ulong size)
578 {
579         int cpu, nmach, want, ms;
580         void (*f)(ulong, ulong, ulong);
581
582         nmach = conf.nmach;
583         writeconf();
584
585         /*
586          * the boot processor is cpu0.  execute this function on it
587          * so that the new kernel has the same cpu0.
588          */
589         if (m->machno != 0) {
590                 procwired(up, 0);
591                 sched();
592         }
593         if (m->machno != 0)
594                 print("on cpu%d (not 0)!\n", m->machno);
595
596         /*
597          * the other cpus could be holding locks that will never get
598          * released (e.g., in the print path) if we put them into
599          * reset now, so force them to shutdown gracefully first.
600          */
601         for (want = 0, cpu = 1; cpu < navailcpus; cpu++)
602                 want |= 1 << cpu;
603         active.stopped = 0;
604         shutdown(0);
605         for (ms = 15*1000; ms > 0 && active.stopped != want; ms -= 10)
606                 delay(10);
607         delay(20);
608         if (active.stopped != want) {
609                 for (cpu = 1; cpu < nmach; cpu++)
610                         stopcpu(cpu);           /* make really sure */
611                 delay(20);
612         }
613
614         /*
615          * should be the only processor running now
616          */
617         pcireset();
618 //      print("reboot entry %#lux code %#lux size %ld\n",
619 //              PADDR(entry), PADDR(code), size);
620
621         /* turn off buffered serial console */
622         serialoq = nil;
623         kprintoq = nil;
624         screenputs = nil;
625
626         /* shutdown devices */
627         chandevshutdown();
628
629         /* call off the dog */
630         clockshutdown();
631
632         splhi();
633         intrshutdown();
634
635         /* setup reboot trampoline function */
636         f = (void*)REBOOTADDR;
637         memmove(f, rebootcode, sizeof(rebootcode));
638         cachedwb();
639         l2cache->wbinv();
640         l2cache->off();
641         cacheuwbinv();
642
643         /* off we go - never to return */
644         (*f)(PADDR(entry), PADDR(code), size);
645
646         iprint("loaded kernel returned!\n");
647         archreboot();
648 }
649
650 /*
651  *  starting place for first process
652  */
653 void
654 init0(void)
655 {
656         int i;
657         char buf[2*KNAMELEN];
658
659         up->nerrlab = 0;
660         coherence();
661         spllo();
662
663         /*
664          * These are o.k. because rootinit is null.
665          * Then early kproc's will have a root and dot.
666          */
667         up->slash = namec("#/", Atodir, 0, 0);
668         pathclose(up->slash->path);
669         up->slash->path = newpath("/");
670         up->dot = cclone(up->slash);
671
672         chandevinit();
673         i8250console();         /* might be redundant, but harmless */
674         if(serialoq == nil)
675                 panic("init0: nil serialoq");
676         normalprint = 1;
677
678         if(!waserror()){
679                 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
680                 ksetenv("terminal", buf, 0);
681                 ksetenv("cputype", "arm", 0);
682                 if(cpuserver)
683                         ksetenv("service", "cpu", 0);
684                 else
685                         ksetenv("service", "terminal", 0);
686
687                 /* convert plan9.ini variables to #e and #ec */
688                 for(i = 0; i < nconf; i++) {
689                         ksetenv(confname[i], confval[i], 0);
690                         ksetenv(confname[i], confval[i], 1);
691                 }
692                 poperror();
693         }
694         kproc("alarm", alarmkproc, 0);
695
696         touser(sp);
697 }
698
699 static void
700 bootargs(uintptr base)
701 {
702         int i;
703         ulong ssize;
704         char **av, *p;
705
706         /*
707          * Push the boot args onto the stack.
708          * The initial value of the user stack must be such
709          * that the total used is larger than the maximum size
710          * of the argument list checked in syscall.
711          */
712         i = oargblen+1;
713         p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
714         memmove(p, oargb, i);
715
716         /*
717          * Now push argc and the argv pointers.
718          * This isn't strictly correct as the code jumped to by
719          * touser in init9.s calls startboot (port/initcode.c) which
720          * expects arguments
721          *      startboot(char *argv0, char **argv)
722          * not the usual (int argc, char* argv[]), but argv0 is
723          * unused so it doesn't matter (at the moment...).
724          */
725         av = (char**)(p - (oargc+2)*sizeof(char*));
726         ssize = base + BY2PG - PTR2UINT(av);
727         *av++ = (char*)oargc;
728         for(i = 0; i < oargc; i++)
729                 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
730         *av = nil;
731
732         /*
733          * Leave space for the return PC of the
734          * caller of initcode.
735          */
736         sp = USTKTOP - ssize - sizeof(void*);
737 }
738
739 /*
740  *  create the first process
741  */
742 void
743 userinit(void)
744 {
745         Proc *p;
746         Segment *s;
747         KMap *k;
748         Page *pg;
749
750         /* no processes yet */
751         up = nil;
752
753         p = newproc();
754         p->pgrp = newpgrp();
755         p->egrp = smalloc(sizeof(Egrp));
756         p->egrp->ref = 1;
757         p->fgrp = dupfgrp(nil);
758         p->rgrp = newrgrp();
759         p->procmode = 0640;
760
761         kstrdup(&eve, "");
762         kstrdup(&p->text, "*init*");
763         kstrdup(&p->user, eve);
764
765         /*
766          * Kernel Stack
767          */
768         p->sched.pc = PTR2UINT(init0);
769         p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
770         p->sched.sp = STACKALIGN(p->sched.sp);
771
772         /*
773          * User Stack
774          *
775          * Technically, newpage can't be called here because it
776          * should only be called when in a user context as it may
777          * try to sleep if there are no pages available, but that
778          * shouldn't be the case here.
779          */
780         s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
781         s->flushme++;
782         p->seg[SSEG] = s;
783         pg = newpage(1, 0, USTKTOP-BY2PG);
784         segpage(s, pg);
785         k = kmap(pg);
786         bootargs(VA(k));
787         kunmap(k);
788
789         /*
790          * Text
791          */
792         s = newseg(SG_TEXT, UTZERO, 1);
793         p->seg[TSEG] = s;
794         pg = newpage(1, 0, UTZERO);
795         memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
796         segpage(s, pg);
797         k = kmap(s->map[0]->pages[0]);
798         memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
799         kunmap(k);
800
801         ready(p);
802 }
803
804 Conf conf;                      /* XXX - must go - gag */
805
806 Confmem tsmem[nelem(conf.mem)] = {
807         /*
808          * Memory available to Plan 9:
809          */
810         { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
811 };
812 ulong memsize = DRAMSIZE;
813
814 static int
815 gotmem(uintptr sz)
816 {
817         uintptr addr;
818
819         /* back off a little from the end */
820         addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
821         if (probeaddr(addr) >= 0) {     /* didn't trap? memory present */
822                 memsize = sz;
823                 return 0;
824         }
825         return -1;
826 }
827
828 void
829 confinit(void)
830 {
831         int i;
832         ulong kpages;
833         uintptr pa;
834         char *p;
835
836         if(p = getconf("service")){
837                 if(strcmp(p, "cpu") == 0)
838                         cpuserver = 1;
839                 else if(strcmp(p,"terminal") == 0)
840                         cpuserver = 0;
841         }
842
843         /*
844          * Copy the physical memory configuration to Conf.mem.
845          */
846         if(nelem(tsmem) > nelem(conf.mem)){
847                 iprint("memory configuration botch\n");
848                 exit(1);
849         }
850         if(0 && (p = getconf("*maxmem")) != nil) {
851                 memsize = strtoul(p, 0, 0) - PHYSDRAM;
852                 if (memsize < 16*MB)            /* sanity */
853                         memsize = 16*MB;
854         }
855
856         /*
857          * see if all that memory exists; if not, find out how much does.
858          * trapinit must have been called first.
859          */
860         if (gotmem(memsize - RESRVDHIMEM) < 0)
861                 panic("can't find 1GB of memory");
862
863         tsmem[0].limit = PHYSDRAM + memsize;
864         memmove(conf.mem, tsmem, sizeof(tsmem));
865
866         conf.npage = 0;
867         pa = PADDR(PGROUND(PTR2UINT(end)));
868
869         /*
870          *  we assume that the kernel is at the beginning of one of the
871          *  contiguous chunks of memory and fits therein.
872          */
873         for(i=0; i<nelem(conf.mem); i++){
874                 /* take kernel out of allocatable space */
875                 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
876                         conf.mem[i].base = pa;
877
878                 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
879                 conf.npage += conf.mem[i].npage;
880         }
881
882         conf.upages = (conf.npage*80)/100;
883         conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
884
885         /* set up other configuration parameters */
886         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
887         if(cpuserver)
888                 conf.nproc *= 3;
889         if(conf.nproc > 2000)
890                 conf.nproc = 2000;
891         conf.nswap = conf.npage*3;
892         conf.nswppo = 4096;
893         conf.nimage = 200;
894
895         /*
896          * it's simpler on mp systems to take page-faults early,
897          * on reference, rather than later, on write, which might
898          * require tlb shootdowns.
899          */
900         conf.copymode = 1;              /* copy on reference */
901
902         /*
903          * Guess how much is taken by the large permanent
904          * datastructures. Mntcache and Mntrpc are not accounted for
905          * (probably ~300KB).
906          */
907         kpages = conf.npage - conf.upages;
908         kpages *= BY2PG;
909         kpages -= conf.upages*sizeof(Page)
910                 + conf.nproc*sizeof(Proc)
911                 + conf.nimage*sizeof(Image)
912                 + conf.nswap
913                 + conf.nswppo*sizeof(Page);
914         mainmem->maxsize = kpages;
915         if(!cpuserver)
916                 /*
917                  * give terminals lots of image memory, too; the dynamic
918                  * allocation will balance the load properly, hopefully.
919                  * be careful with 32-bit overflow.
920                  */
921                 imagmem->maxsize = kpages;
922 }
923
924 int
925 cmpswap(long *addr, long old, long new)
926 {
927         return cas((int *)addr, old, new);
928 }
929
930 void
931 advertwfi(void)                 /* advertise my wfi status */
932 {
933         ilock(&active);
934         active.wfi |= 1 << m->machno;
935         iunlock(&active);
936 }
937
938 void
939 unadvertwfi(void)               /* do not advertise my wfi status */
940 {
941         ilock(&active);
942         active.wfi &= ~(1 << m->machno);
943         iunlock(&active);
944 }
945
946 void
947 idlehands(void)
948 {
949 #ifdef use_ipi
950         int advertised;
951
952         /* don't go into wfi until my local timer is ticking */
953         if (m->ticks <= 1)
954                 return;
955
956         advertised = 0;
957         m->inidlehands++;
958         /* avoid recursion via ilock, advertise iff this cpu is initialised */
959         if (m->inidlehands == 1 && m->syscall > 0) {
960                 advertwfi();
961                 advertised = 1;
962         }
963
964         wfi();
965
966         if (advertised)
967                 unadvertwfi();
968         m->inidlehands--;
969 #endif
970 }
971
972 void
973 wakewfi(void)
974 {
975 #ifdef use_ipi
976         uint cpu;
977
978         /*
979          * find any cpu other than me currently in wfi.
980          * need not be exact.
981          */
982         cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));
983         if (cpu < MAXMACH)
984                 intrcpu(cpu);
985 #endif
986 }