changelog shortlog tags branches changeset files revisions annotate raw help

Mercurial > hg > plan9front / sys/src/9/bcm/main.c

changeset 7234: fc141b91ed8a
parent: 66132ebbe687
child: 889b634159e5
author: cinap_lenrek@felloff.net
date: Mon, 13 May 2019 19:12:41 +0200
permissions: -rw-r--r--
description: bcm, bcm64: preserve memsize across reboots, avoid trashing atags while parsing cmdline

we override atag memory on reboot, so preserve
the memsize learned from atag as *maxmem plan9
variable. the global memsize variable is not
needed anymore.

avoid trashing the following atag when zero
terminating the cmdline string.

zero memory after plan9.ini variables.
1 #include "u.h"
2 #include "tos.h"
3 #include "../port/lib.h"
4 #include "mem.h"
5 #include "dat.h"
6 #include "fns.h"
7 #include "io.h"
8 
9 #include "init.h"
10 #include <pool.h>
11 
12 #include "reboot.h"
13 
14 /* Firmware compatibility */
15 #define Minfirmrev 326770
16 #define Minfirmdate "22 Jul 2012"
17 
18 uintptr kseg0 = KZERO;
19 Mach* machaddr[MAXMACH];
20 Conf conf;
21 
22 void
23 machinit(void)
24 {
25  Mach *m0;
26 
27  m->ticks = 1;
28  m->perf.period = 1;
29  m0 = MACHP(0);
30  if (m->machno != 0) {
31  /* synchronise with cpu 0 */
32  m->ticks = m0->ticks;
33  }
34 }
35 
36 void
37 mach0init(void)
38 {
39  m->mmul1 = (PTE*)L1;
40  m->machno = 0;
41  machaddr[m->machno] = m;
42 
43  m->ticks = 1;
44  m->perf.period = 1;
45 
46  active.machs[0] = 1;
47  active.exiting = 0;
48 
49  up = nil;
50 }
51 
52 static void
53 launchinit(void)
54 {
55  int mach;
56  Mach *mm;
57  PTE *l1;
58 
59  for(mach = 1; mach < conf.nmach; mach++){
60  machaddr[mach] = mm = mallocalign(MACHSIZE, MACHSIZE, 0, 0);
61  l1 = mallocalign(L1SIZE, L1SIZE, 0, 0);
62  if(mm == nil || l1 == nil)
63  panic("launchinit");
64  memset(mm, 0, MACHSIZE);
65  mm->machno = mach;
66 
67  memmove(l1, m->mmul1, L1SIZE); /* clone cpu0's l1 table */
68  cachedwbse(l1, L1SIZE);
69  mm->mmul1 = l1;
70  cachedwbse(mm, MACHSIZE);
71 
72  }
73  cachedwbse(machaddr, sizeof machaddr);
74  if((mach = startcpus(conf.nmach)) < conf.nmach)
75  print("only %d cpu%s started\n", mach, mach == 1? "" : "s");
76 }
77 
78 void
79 main(void)
80 {
81  extern char edata[], end[];
82  uint fw, board;
83 
84  m = (Mach*)MACHADDR;
85  memset(edata, 0, end - edata); /* clear bss */
86  mach0init();
87  quotefmtinstall();
88  bootargsinit();
89  confinit(); /* figures out amount of memory */
90  xinit();
91  uartconsinit();
92  screeninit();
93 
94  print("\nPlan 9 from Bell Labs\n");
95  board = getboardrev();
96  fw = getfirmware();
97  print("board rev: %#ux firmware rev: %d\n", board, fw);
98  if(fw < Minfirmrev){
99  print("Sorry, firmware (start*.elf) must be at least rev %d"
100  " or newer than %s\n", Minfirmrev, Minfirmdate);
101  for(;;)
102  ;
103  }
104  /* set clock rate to arm_freq from config.txt (default pi1:700Mhz pi2:900MHz) */
105  setclkrate(ClkArm, 0);
106  trapinit();
107  clockinit();
108  printinit();
109  timersinit();
110  cpuidprint();
111  archreset();
112  vgpinit();
113 
114  procinit0();
115  initseg();
116  links();
117  chandevreset(); /* most devices are discovered here */
118  pageinit();
119  userinit();
120  launchinit();
121  mmuinit1(0);
122  schedinit();
123  assert(0); /* shouldn't have returned */
124 }
125 
126 /*
127  * starting place for first process
128  */
129 void
130 init0(void)
131 {
132  char buf[2*KNAMELEN], **sp;
133 
134  up->nerrlab = 0;
135  coherence();
136  spllo();
137 
138  /*
139  * These are o.k. because rootinit is null.
140  * Then early kproc's will have a root and dot.
141  */
142  up->slash = namec("#/", Atodir, 0, 0);
143  pathclose(up->slash->path);
144  up->slash->path = newpath("/");
145  up->dot = cclone(up->slash);
146 
147  chandevinit();
148 
149  if(!waserror()){
150  snprint(buf, sizeof(buf), "%s %s", "ARM", conffile);
151  ksetenv("terminal", buf, 0);
152  ksetenv("cputype", "arm", 0);
153  if(cpuserver)
154  ksetenv("service", "cpu", 0);
155  else
156  ksetenv("service", "terminal", 0);
157  snprint(buf, sizeof(buf), "-a %s", getethermac());
158  ksetenv("etherargs", buf, 0);
159 
160  /* convert plan9.ini variables to #e and #ec */
161  setconfenv();
162  poperror();
163  }
164  kproc("alarm", alarmkproc, 0);
165 
166  sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
167  sp[3] = sp[2] = sp[1] = nil;
168  strcpy(sp[0] = (char*)&sp[4], "boot");
169 
170  touser((uintptr)sp);
171  assert(0); /* shouldn't have returned */
172 }
173 
174 /*
175  * create the first process
176  */
177 void
178 userinit(void)
179 {
180  Proc *p;
181  Segment *s;
182  KMap *k;
183  Page *pg;
184 
185  /* no processes yet */
186  up = nil;
187 
188  p = newproc();
189  p->pgrp = newpgrp();
190  p->egrp = smalloc(sizeof(Egrp));
191  p->egrp->ref = 1;
192  p->fgrp = dupfgrp(nil);
193  p->rgrp = newrgrp();
194  p->procmode = 0640;
195 
196  kstrdup(&eve, "");
197  kstrdup(&p->text, "*init*");
198  kstrdup(&p->user, eve);
199 
200  /*
201  * Kernel Stack
202  */
203  p->sched.pc = (uintptr)init0;
204  p->sched.sp = (uintptr)p->kstack+KSTACK-sizeof(up->s.args)-sizeof(uintptr);
205  p->sched.sp = STACKALIGN(p->sched.sp);
206 
207  /*
208  * User Stack
209  *
210  * Technically, newpage can't be called here because it
211  * should only be called when in a user context as it may
212  * try to sleep if there are no pages available, but that
213  * shouldn't be the case here.
214  */
215  s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
216  s->flushme++;
217  p->seg[SSEG] = s;
218  pg = newpage(1, 0, USTKTOP-BY2PG);
219  segpage(s, pg);
220  k = kmap(pg);
221  memset((void*)VA(k), 0, BY2PG);
222  kunmap(k);
223 
224  /*
225  * Text
226  */
227  s = newseg(SG_TEXT, UTZERO, 1);
228  p->seg[TSEG] = s;
229  pg = newpage(1, 0, UTZERO);
230  pg->txtflush = ~0;
231  segpage(s, pg);
232  k = kmap(s->map[0]->pages[0]);
233  memmove((void*)VA(k), initcode, sizeof initcode);
234  kunmap(k);
235 
236  ready(p);
237 }
238 
239 void
240 confinit(void)
241 {
242  int i, userpcnt;
243  ulong kpages, memsize = 0;
244  uintptr pa;
245  char *p;
246 
247  if(p = getconf("service")){
248  if(strcmp(p, "cpu") == 0)
249  cpuserver = 1;
250  else if(strcmp(p,"terminal") == 0)
251  cpuserver = 0;
252  }
253 
254  if(p = getconf("*kernelpercent"))
255  userpcnt = 100 - strtol(p, 0, 0);
256  else
257  userpcnt = 0;
258 
259  if(p = getconf("*maxmem"))
260  memsize = strtoul(p, 0, 0) - PHYSDRAM;
261  if (memsize < 16*MB) /* sanity */
262  memsize = 16*MB;
263  getramsize(&conf.mem[0]);
264  if(conf.mem[0].limit == 0){
265  conf.mem[0].base = PHYSDRAM;
266  conf.mem[0].limit = PHYSDRAM + memsize;
267  }else if(p != nil)
268  conf.mem[0].limit = conf.mem[0].base + memsize;
269 
270  conf.npage = 0;
271  pa = PADDR(PGROUND((uintptr)end));
272 
273  /*
274  * we assume that the kernel is at the beginning of one of the
275  * contiguous chunks of memory and fits therein.
276  */
277  for(i=0; i<nelem(conf.mem); i++){
278  /* take kernel out of allocatable space */
279  if(pa > conf.mem[i].base && pa < conf.mem[i].limit)
280  conf.mem[i].base = pa;
281 
282  conf.mem[i].npage = (conf.mem[i].limit - conf.mem[i].base)/BY2PG;
283  conf.npage += conf.mem[i].npage;
284  }
285 
286  if(userpcnt < 10)
287  userpcnt = 60 + cpuserver*10;
288  kpages = conf.npage - (conf.npage*userpcnt)/100;
289 
290  /*
291  * can't go past the end of virtual memory
292  * (ulong)-KZERO is 2^32 - KZERO
293  */
294  if(kpages > ((ulong)-KZERO)/BY2PG)
295  kpages = ((ulong)-KZERO)/BY2PG;
296 
297  conf.upages = conf.npage - kpages;
298  conf.ialloc = (kpages/2)*BY2PG;
299 
300  conf.nmach = getncpus();
301 
302  /* set up other configuration parameters */
303  conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
304  if(cpuserver)
305  conf.nproc *= 3;
306  if(conf.nproc > 2000)
307  conf.nproc = 2000;
308  conf.nswap = conf.npage*3;
309  conf.nswppo = 4096;
310  conf.nimage = 200;
311 
312  conf.copymode = conf.nmach > 1;
313 
314  /*
315  * Guess how much is taken by the large permanent
316  * datastructures. Mntcache and Mntrpc are not accounted for.
317  */
318  kpages = conf.npage - conf.upages;
319  kpages *= BY2PG;
320  kpages -= conf.upages*sizeof(Page)
321  + conf.nproc*sizeof(Proc)
322  + conf.nimage*sizeof(Image)
323  + conf.nswap
324  + conf.nswppo*sizeof(Page*);
325  mainmem->maxsize = kpages;
326  if(!cpuserver)
327  /*
328  * give terminals lots of image memory, too; the dynamic
329  * allocation will balance the load properly, hopefully.
330  * be careful with 32-bit overflow.
331  */
332  imagmem->maxsize = kpages;
333 
334 }
335 
336 static void
337 rebootjump(void *entry, void *code, ulong size)
338 {
339  void (*f)(void*, void*, ulong);
340 
341  intrsoff();
342  intrcpushutdown();
343 
344  /* redo identity map */
345  mmuinit1(1);
346 
347  /* setup reboot trampoline function */
348  f = (void*)REBOOTADDR;
349  memmove(f, rebootcode, sizeof(rebootcode));
350  cacheuwbinv();
351 
352  (*f)(entry, code, size);
353 
354  for(;;);
355 }
356 
357 /*
358  * exit kernel either on a panic or user request
359  */
360 void
361 exit(int)
362 {
363  cpushutdown();
364  splfhi();
365  if(m->machno == 0)
366  archreboot();
367  rebootjump(0, 0, 0);
368 }
369 
370 /*
371  * stub for ../omap/devether.c
372  */
373 int
374 isaconfig(char *, int, ISAConf *)
375 {
376  return 0;
377 }
378 
379 /*
380  * the new kernel is already loaded at address `code'
381  * of size `size' and entry point `entry'.
382  */
383 void
384 reboot(void *entry, void *code, ulong size)
385 {
386  writeconf();
387  while(m->machno != 0){
388  procwired(up, 0);
389  sched();
390  }
391 
392  cpushutdown();
393  delay(2000);
394 
395  splfhi();
396 
397  /* turn off buffered serial console */
398  serialoq = nil;
399 
400  /* shutdown devices */
401  chandevshutdown();
402 
403  /* stop the clock (and watchdog if any) */
404  clockshutdown();
405  wdogoff();
406 
407  /* off we go - never to return */
408  rebootjump(entry, code, size);
409 }
410 
411 void
412 setupwatchpts(Proc *, Watchpt *, int n)
413 {
414  if(n > 0)
415  error("no watchpoints");
416 }