]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/teg2/main.c
kernel: change active.machs from bitmap to char array to support up to 64 cpus on...
[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         lock(&active);
212         if (active.machs[cpu] == 0) {   /* currently off? */
213                 active.machs[cpu] = 1;
214                 conf.nmach++;
215         }
216         unlock(&active);
217 }
218
219 /* disable scheduling of this cpu */
220 void
221 machoff(uint cpu)
222 {
223         lock(&active);
224         if (active.machs[cpu]) {                /* currently on? */
225                 active.machs[cpu] = 0;
226                 conf.nmach--;
227         }
228         unlock(&active);
229 }
230
231 void
232 machinit(void)
233 {
234         Mach *m0;
235
236         if (m == 0) {
237                 serialputc('?');
238                 serialputc('m');
239                 serialputc('0');
240         }
241         if(machaddr[m->machno] != m) {
242                 serialputc('?');
243                 serialputc('m');
244                 serialputc('m');
245         }
246
247         if (canlock(&testlock)) {
248                 serialputc('?');
249                 serialputc('l');
250                 panic("cpu%d: locks don't work", m->machno);
251         }
252
253         m->ticks = 1;
254         m->perf.period = 1;
255         m0 = MACHP(0);
256         if (m->machno != 0) {
257                 /* synchronise with cpu 0 */
258                 m->ticks = m0->ticks;
259                 m->fastclock = m0->fastclock;
260                 m->cpuhz = m0->cpuhz;
261                 m->delayloop = m0->delayloop;
262         }
263         if (m->machno != 0 &&
264             (m->fastclock == 0 || m->cpuhz == 0 || m->delayloop == 0))
265                 panic("buggered cpu 0 Mach");
266
267         machon(m->machno);
268         fpoff();
269 }
270
271 /* l.s has already zeroed Mach, which now contains our stack. */
272 void
273 mach0init(void)
274 {
275         if (m == 0) {
276                 serialputc('?');
277                 serialputc('m');
278         }
279         conf.nmach = 0;
280
281         m->machno = 0;
282         machaddr[0] = m;
283
284         lock(&testlock);                /* hold this forever */
285         machinit();
286
287         active.exiting = 0;
288         l1cache->wbse(&active, sizeof active);
289         up = nil;
290 }
291
292 /*
293  *  count CPU's, set up their mach structures and l1 ptes.
294  *  we're running on cpu 0 and our data structures were
295  *  statically allocated.
296  */
297 void
298 launchinit(void)
299 {
300         int mach;
301         Mach *mm;
302         PTE *l1;
303
304         for(mach = 1; mach < MAXMACH; mach++){
305                 machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
306                 l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
307                 if(mm == nil || l1 == nil)
308                         panic("launchinit");
309                 memset(mm, 0, MACHSIZE);
310                 mm->machno = mach;
311
312                 memmove(l1, (void *)L1, L1SIZE);  /* clone cpu0's l1 table */
313                 l1cache->wbse(l1, L1SIZE);
314
315                 mm->mmul1 = l1;
316                 l1cache->wbse(mm, MACHSIZE);
317         }
318         l1cache->wbse(machaddr, sizeof machaddr);
319         conf.nmach = 1;
320 }
321
322 void
323 dump(void *vaddr, int words)
324 {
325         ulong *addr;
326
327         addr = vaddr;
328         while (words-- > 0)
329                 iprint("%.8lux%c", *addr++, words % 8 == 0? '\n': ' ');
330 }
331
332 static void
333 cacheinit(void)
334 {
335         allcacheinfo(cachel);
336         cacheconf = (Lowmemcache *)CACHECONF;
337         cacheconf->l1waysh = cachel[1].waysh;
338         cacheconf->l1setsh = cachel[1].setsh;
339         /* on the tegra 2, l2 is unarchitected */
340         cacheconf->l2waysh = cachel[2].waysh;
341         cacheconf->l2setsh = cachel[2].setsh;
342
343         l2pl310init();
344         allcacheson();
345         allcache->wb();
346 }
347
348 void
349 l2pageinit(void)
350 {
351         l2pages = KADDR(PHYSDRAM + DRAMSIZE - RESRVDHIMEM);
352 }
353
354 /*
355  * at entry, l.s has set m for cpu0 and printed "Plan 9 from Be"
356  * but has not zeroed bss.
357  */
358 void
359 main(void)
360 {
361         int cpu;
362         static ulong vfy = 0xcafebabe;
363
364         up = nil;
365         if (vfy != 0xcafebabe) {
366                 serialputc('?');
367                 serialputc('d');
368                 panic("data segment misaligned");
369         }
370
371         memset(edata, 0, end - edata);
372
373         /*
374          * we can't lock until smpon has run, but we're supposed to wait
375          * until l1 & l2 are on.  too bad.  l1 is on, l2 will soon be.
376          */
377         smpon();
378         iprint("ll Labs ");
379         cacheinit();
380
381         /*
382          * data segment is aligned, bss is zeroed, caches' characteristics
383          * are known.  begin initialisation.
384          */
385         mach0init();
386         l2pageinit();
387         mmuinit();
388
389         optionsinit("/boot/boot boot");
390         quotefmtinstall();
391
392         /* want plan9.ini to be able to affect memory sizing in confinit */
393         plan9iniinit();         /* before we step on plan9.ini in low memory */
394
395         /* l2 looks for *l2off= in plan9.ini */
396         l2cache->on();          /* l2->on requires locks to work, thus smpon */
397         l2cache->info(&cachel[2]);
398         allcache->on();
399
400         cortexa9cachecfg();
401
402         trapinit();             /* so confinit can probe memory to size it */
403         confinit();             /* figures out amount of memory */
404         /* xinit prints (if it can), so finish up the banner here. */
405         delay(100);
406         navailcpus = getncpus();
407         iprint("(mp arm; %d cpus)\n\n", navailcpus);
408         delay(100);
409
410         for (cpu = 1; cpu < navailcpus; cpu++)
411                 stopcpu(cpu);
412
413         xinit();
414         irqtooearly = 0;        /* now that xinit and trapinit have run */
415
416         mainmem->flags |= POOL_ANTAGONISM /* | POOL_PARANOIA */ ;
417
418         /*
419          * Printinit will cause the first malloc call.
420          * (printinit->qopen->malloc) unless any of the
421          * above (like clockinit) do an irqenable, which
422          * will call malloc.
423          * If the system dies here it's probably due
424          * to malloc(->xalloc) not being initialised
425          * correctly, or the data segment is misaligned
426          * (it's amazing how far you can get with
427          * things like that completely broken).
428          *
429          * (Should be) boilerplate from here on.
430          */
431
432         archreset();                    /* cfg clock signals, print cache cfg */
433         clockinit();                    /* start clocks */
434         timersinit();
435
436         delay(50);                      /* let uart catch up */
437         printinit();
438
439         cpuidprint();
440         chkmissing();
441
442         procinit0();
443         initseg();
444
445 //      dmainit();
446         links();
447         conf.monitor = 1;
448 //      screeninit();
449
450         iprint("pcireset...");
451         pcireset();                     /* this tends to hang after a reboot */
452         iprint("ok\n");
453
454         chandevreset();                 /* most devices are discovered here */
455 //      i8250console();                 /* too early; see init0 */
456
457         pageinit();                     /* prints "1020M memory: â‹¯ */
458         swapinit();
459         userinit();
460
461         /*
462          * starting a cpu will eventually result in it calling schedinit,
463          * so everything necessary to run user processes should be set up
464          * before starting secondary cpus.
465          */
466         launchinit();
467         /* SMP & FW are already on when we get here; u-boot set them? */
468         for (cpu = 1; cpu < navailcpus; cpu++)
469                 if (startcpu(cpu) < 0)
470                         panic("cpu%d didn't start", cpu);
471         l1diag();
472
473         schedinit();
474         panic("cpu%d: schedinit returned", m->machno);
475 }
476
477 /*
478  *  exit kernel either on a panic or user request
479  */
480 void
481 exit(int)
482 {
483         cpushutdown();
484         splhi();
485         if (m->machno == 0)
486                 archreboot();
487         else {
488                 intrcpushutdown();
489                 stopcpu(m->machno);
490                 for (;;)
491                         idlehands();
492         }
493 }
494
495 int
496 isaconfig(char *class, int ctlrno, ISAConf *isa)
497 {
498         char cc[32], *p;
499         int i;
500
501         snprint(cc, sizeof cc, "%s%d", class, ctlrno);
502         p = getconf(cc);
503         if(p == nil)
504                 return 0;
505
506         isa->type = "";
507         isa->nopt = tokenize(p, isa->opt, NISAOPT);
508         for(i = 0; i < isa->nopt; i++){
509                 p = isa->opt[i];
510                 if(cistrncmp(p, "type=", 5) == 0)
511                         isa->type = p + 5;
512                 else if(cistrncmp(p, "port=", 5) == 0)
513                         isa->port = strtoul(p+5, &p, 0);
514                 else if(cistrncmp(p, "irq=", 4) == 0)
515                         isa->irq = strtoul(p+4, &p, 0);
516                 else if(cistrncmp(p, "dma=", 4) == 0)
517                         isa->dma = strtoul(p+4, &p, 0);
518                 else if(cistrncmp(p, "mem=", 4) == 0)
519                         isa->mem = strtoul(p+4, &p, 0);
520                 else if(cistrncmp(p, "size=", 5) == 0)
521                         isa->size = strtoul(p+5, &p, 0);
522                 else if(cistrncmp(p, "freq=", 5) == 0)
523                         isa->freq = strtoul(p+5, &p, 0);
524         }
525         return 1;
526 }
527
528 /*
529  * the new kernel is already loaded at address `code'
530  * of size `size' and entry point `entry'.
531  */
532 void
533 reboot(void *entry, void *code, ulong size)
534 {
535         void (*f)(ulong, ulong, ulong);
536
537         writeconf();
538
539         /*
540          * the boot processor is cpu0.  execute this function on it
541          * so that the new kernel has the same cpu0.
542          */
543         if (m->machno != 0) {
544                 procwired(up, 0);
545                 sched();
546         }
547         cpushutdown();
548
549         /*
550          * should be the only processor running now
551          */
552         pcireset();
553
554         /* turn off buffered serial console */
555         serialoq = nil;
556         kprintoq = nil;
557         screenputs = nil;
558
559         /* shutdown devices */
560         chandevshutdown();
561
562         /* call off the dog */
563         clockshutdown();
564
565         splhi();
566         intrshutdown();
567
568         /* setup reboot trampoline function */
569         f = (void*)REBOOTADDR;
570         memmove(f, rebootcode, sizeof(rebootcode));
571         cachedwb();
572         l2cache->wbinv();
573         l2cache->off();
574         cacheuwbinv();
575
576         /* off we go - never to return */
577         (*f)(PADDR(entry), PADDR(code), size);
578 }
579
580 /*
581  *  starting place for first process
582  */
583 void
584 init0(void)
585 {
586         int i;
587         char buf[2*KNAMELEN];
588
589         up->nerrlab = 0;
590         coherence();
591         spllo();
592
593         /*
594          * These are o.k. because rootinit is null.
595          * Then early kproc's will have a root and dot.
596          */
597         up->slash = namec("#/", Atodir, 0, 0);
598         pathclose(up->slash->path);
599         up->slash->path = newpath("/");
600         up->dot = cclone(up->slash);
601
602         chandevinit();
603         i8250console();         /* might be redundant, but harmless */
604         if(serialoq == nil)
605                 panic("init0: nil serialoq");
606         normalprint = 1;
607
608         if(!waserror()){
609                 snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
610                 ksetenv("terminal", buf, 0);
611                 ksetenv("cputype", "arm", 0);
612                 if(cpuserver)
613                         ksetenv("service", "cpu", 0);
614                 else
615                         ksetenv("service", "terminal", 0);
616
617                 /* convert plan9.ini variables to #e and #ec */
618                 for(i = 0; i < nconf; i++) {
619                         ksetenv(confname[i], confval[i], 0);
620                         ksetenv(confname[i], confval[i], 1);
621                 }
622                 poperror();
623         }
624         kproc("alarm", alarmkproc, 0);
625
626         touser(sp);
627 }
628
629 static void
630 bootargs(uintptr base)
631 {
632         int i;
633         ulong ssize;
634         char **av, *p;
635
636         /*
637          * Push the boot args onto the stack.
638          * The initial value of the user stack must be such
639          * that the total used is larger than the maximum size
640          * of the argument list checked in syscall.
641          */
642         i = oargblen+1;
643         p = UINT2PTR(STACKALIGN(base + BY2PG - sizeof(up->s.args) - i));
644         memmove(p, oargb, i);
645
646         /*
647          * Now push argc and the argv pointers.
648          * This isn't strictly correct as the code jumped to by
649          * touser in init9.s calls startboot (port/initcode.c) which
650          * expects arguments
651          *      startboot(char *argv0, char **argv)
652          * not the usual (int argc, char* argv[]), but argv0 is
653          * unused so it doesn't matter (at the moment...).
654          */
655         av = (char**)(p - (oargc+2)*sizeof(char*));
656         ssize = base + BY2PG - PTR2UINT(av);
657         *av++ = (char*)oargc;
658         for(i = 0; i < oargc; i++)
659                 *av++ = (oargv[i] - oargb) + (p - base) + (USTKTOP - BY2PG);
660         *av = nil;
661
662         /*
663          * Leave space for the return PC of the
664          * caller of initcode.
665          */
666         sp = USTKTOP - ssize - sizeof(void*);
667 }
668
669 /*
670  *  create the first process
671  */
672 void
673 userinit(void)
674 {
675         Proc *p;
676         Segment *s;
677         KMap *k;
678         Page *pg;
679
680         /* no processes yet */
681         up = nil;
682
683         p = newproc();
684         p->pgrp = newpgrp();
685         p->egrp = smalloc(sizeof(Egrp));
686         p->egrp->ref = 1;
687         p->fgrp = dupfgrp(nil);
688         p->rgrp = newrgrp();
689         p->procmode = 0640;
690
691         kstrdup(&eve, "");
692         kstrdup(&p->text, "*init*");
693         kstrdup(&p->user, eve);
694
695         /*
696          * Kernel Stack
697          */
698         p->sched.pc = PTR2UINT(init0);
699         p->sched.sp = PTR2UINT(p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr));
700         p->sched.sp = STACKALIGN(p->sched.sp);
701
702         /*
703          * User Stack
704          *
705          * Technically, newpage can't be called here because it
706          * should only be called when in a user context as it may
707          * try to sleep if there are no pages available, but that
708          * shouldn't be the case here.
709          */
710         s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
711         s->flushme++;
712         p->seg[SSEG] = s;
713         pg = newpage(1, 0, USTKTOP-BY2PG);
714         segpage(s, pg);
715         k = kmap(pg);
716         bootargs(VA(k));
717         kunmap(k);
718
719         /*
720          * Text
721          */
722         s = newseg(SG_TEXT, UTZERO, 1);
723         p->seg[TSEG] = s;
724         pg = newpage(1, 0, UTZERO);
725         pg->txtflush = ~0;
726         segpage(s, pg);
727         k = kmap(s->map[0]->pages[0]);
728         memmove(UINT2PTR(VA(k)), initcode, sizeof initcode);
729         kunmap(k);
730
731         ready(p);
732 }
733
734 Conf conf;                      /* XXX - must go - gag */
735
736 Confmem tsmem[nelem(conf.mem)] = {
737         /*
738          * Memory available to Plan 9:
739          */
740         { .base = PHYSDRAM, .limit = PHYSDRAM + Minmem, },
741 };
742 ulong memsize = DRAMSIZE;
743
744 static int
745 gotmem(uintptr sz)
746 {
747         uintptr addr;
748
749         /* back off a little from the end */
750         addr = (uintptr)KADDR(PHYSDRAM + sz - BY2WD);
751         if (probeaddr(addr) >= 0) {     /* didn't trap? memory present */
752                 memsize = sz;
753                 return 0;
754         }
755         return -1;
756 }
757
758 void
759 confinit(void)
760 {
761         int i;
762         ulong kpages;
763         uintptr pa;
764         char *p;
765
766         if(p = getconf("service")){
767                 if(strcmp(p, "cpu") == 0)
768                         cpuserver = 1;
769                 else if(strcmp(p,"terminal") == 0)
770                         cpuserver = 0;
771         }
772
773         /*
774          * Copy the physical memory configuration to Conf.mem.
775          */
776         if(nelem(tsmem) > nelem(conf.mem)){
777                 iprint("memory configuration botch\n");
778                 exit(1);
779         }
780         if(0 && (p = getconf("*maxmem")) != nil) {
781                 memsize = strtoul(p, 0, 0) - PHYSDRAM;
782                 if (memsize < 16*MB)            /* sanity */
783                         memsize = 16*MB;
784         }
785
786         /*
787          * see if all that memory exists; if not, find out how much does.
788          * trapinit must have been called first.
789          */
790         if (gotmem(memsize - RESRVDHIMEM) < 0)
791                 panic("can't find 1GB of memory");
792
793         tsmem[0].limit = PHYSDRAM + memsize;
794         memmove(conf.mem, tsmem, sizeof(tsmem));
795
796         conf.npage = 0;
797         pa = PADDR(PGROUND(PTR2UINT(end)));
798
799         /*
800          *  we assume that the kernel is at the beginning of one of the
801          *  contiguous chunks of memory and fits therein.
802          */
803         for(i=0; i<nelem(conf.mem); i++){
804                 /* take kernel out of allocatable space */
805                 if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
806                         conf.mem[i].base = pa;
807
808                 conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
809                 conf.npage += conf.mem[i].npage;
810         }
811
812         conf.upages = (conf.npage*80)/100;
813         conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;
814
815         /* set up other configuration parameters */
816         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
817         if(cpuserver)
818                 conf.nproc *= 3;
819         if(conf.nproc > 2000)
820                 conf.nproc = 2000;
821         conf.nswap = conf.npage*3;
822         conf.nswppo = 4096;
823         conf.nimage = 200;
824
825         /*
826          * it's simpler on mp systems to take page-faults early,
827          * on reference, rather than later, on write, which might
828          * require tlb shootdowns.
829          */
830         conf.copymode = 1;              /* copy on reference */
831
832         /*
833          * Guess how much is taken by the large permanent
834          * datastructures. Mntcache and Mntrpc are not accounted for.
835          */
836         kpages = conf.npage - conf.upages;
837         kpages *= BY2PG;
838         kpages -= conf.upages*sizeof(Page)
839                 + conf.nproc*sizeof(Proc)
840                 + conf.nimage*sizeof(Image)
841                 + conf.nswap
842                 + conf.nswppo*sizeof(Page*);
843         mainmem->maxsize = kpages;
844         if(!cpuserver)
845                 /*
846                  * give terminals lots of image memory, too; the dynamic
847                  * allocation will balance the load properly, hopefully.
848                  * be careful with 32-bit overflow.
849                  */
850                 imagmem->maxsize = kpages;
851 }
852
853 void
854 advertwfi(void)                 /* advertise my wfi status */
855 {
856         ilock(&active);
857         active.wfi |= 1 << m->machno;
858         iunlock(&active);
859 }
860
861 void
862 unadvertwfi(void)               /* do not advertise my wfi status */
863 {
864         ilock(&active);
865         active.wfi &= ~(1 << m->machno);
866         iunlock(&active);
867 }
868
869 void
870 idlehands(void)
871 {
872 #ifdef use_ipi
873         int advertised;
874
875         /* don't go into wfi until my local timer is ticking */
876         if (m->ticks <= 1)
877                 return;
878
879         advertised = 0;
880         m->inidlehands++;
881         /* avoid recursion via ilock, advertise iff this cpu is initialised */
882         if (m->inidlehands == 1 && m->syscall > 0) {
883                 advertwfi();
884                 advertised = 1;
885         }
886
887         wfi();
888
889         if (advertised)
890                 unadvertwfi();
891         m->inidlehands--;
892 #endif
893 }
894
895 void
896 wakewfi(void)
897 {
898 #ifdef use_ipi
899         uint cpu;
900
901         /*
902          * find any cpu other than me currently in wfi.
903          * need not be exact.
904          */
905         cpu = BI2BY*BY2WD - 1 - clz(active.wfi & ~(1 << m->machno));
906         if (cpu < MAXMACH)
907                 intrcpu(cpu);
908 #endif
909 }