]> git.lizzy.rs Git - plan9front.git/blob - sys/src/9/xen/main.c
ether8169: support rtl8402 variant
[plan9front.git] / sys / src / 9 / xen / main.c
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 #include        "ureg.h"
9 #include        "pool.h"
10 #include        "rebootcode.i"
11
12 Mach *m;
13
14 #define BOOTARGS        (xenstart->cmd_line)
15 #define BOOTARGSLEN     (sizeof xenstart->cmd_line)
16 #define MAXCONF         64
17
18 Conf conf;
19 char *confname[MAXCONF];
20 char *confval[MAXCONF];
21 int nconf;
22 int idle_spin;
23
24 static void
25 options(void)
26 {
27         long i, n;
28         char *cp, *line[MAXCONF], *p, *q;
29
30         /*
31          *  parse configuration args from dos file plan9.ini
32          */
33         cp = BOOTARGS;  /* where b.com leaves its config */
34         cp[BOOTARGSLEN-1] = 0;
35
36         /*
37          * Strip out '\r', change '\t' -> ' '.
38          */
39         p = cp;
40         for(q = cp; *q; q++){
41                 if(*q == '\r')
42                         continue;
43                 if(*q == '\t')
44                         *q = ' ';
45                 *p++ = *q;
46         }
47         *p = 0;
48
49         n = getfields(cp, line, MAXCONF, 1, "\n");
50         for(i = 0; i < n; i++){
51                 if(*line[i] == '#')
52                         continue;
53                 cp = strchr(line[i], '=');
54                 if(cp == nil)
55                         continue;
56                 *cp++ = '\0';
57                 confname[nconf] = line[i];
58                 confval[nconf] = cp;
59                 nconf++;
60         }
61 }
62
63 void
64 main(void)
65 {
66         mach0init();
67         options();
68         ioinit();
69         xenconsinit();
70         quotefmtinstall();
71
72         //consdebug = rdb;
73         print("\nPlan 9 (%s)\n", xenstart->magic);
74
75         cpuidentify();
76         // meminit() is not for us
77         confinit();
78         archinit();
79         if(arch->clockinit)
80                 arch->clockinit();
81         xinit();
82         trapinit();
83         printinit();
84         cpuidprint();
85         mmuinit();
86         if(arch->intrinit)      /* launches other processors on an mp */
87                 arch->intrinit();
88         timersinit();
89         mathinit();
90         kbdenable();
91         xengrantinit();
92         if(arch->clockenable)
93                 arch->clockenable();
94         procinit0();
95         initseg();
96
97         links();
98 //      conf.monitor = 1;
99         chandevreset();
100         pageinit();
101         userinit();
102         schedinit();
103 }
104
105 void
106 mach0init(void)
107 {
108         m = (Mach*)MACHADDR;
109         m->machno = 0;
110         conf.nmach = 1;
111         MACHP(0) = (Mach*)CPU0MACH;
112         m->pdb = (ulong*)xenstart->pt_base;
113
114         machinit();
115
116         active.machs[0] = 1;
117         active.exiting = 0;
118 }
119
120 void
121 machinit(void)
122 {
123         int machno;
124         ulong *pdb;
125
126         machno = m->machno;
127         pdb = m->pdb;
128         memset(m, 0, sizeof(Mach));
129         m->machno = machno;
130         m->pdb = pdb;
131         m->perf.period = 1;
132
133         /*
134          * For polled uart output at boot, need
135          * a default delay constant. 100000 should
136          * be enough for a while. Cpuidentify will
137          * calculate the real value later.
138          */
139         m->loopconst = 100000;
140         m->cpumhz = 1000;                               // XXX! 
141
142         HYPERVISOR_shared_info = (shared_info_t*)mmumapframe(XENSHARED, (xenstart->shared_info)>>PGSHIFT);
143         
144         // XXX m->shared = &HYPERVISOR_shared_info->vcpu_data[m->machno];
145 }
146
147 void
148 init0(void)
149 {
150         char buf[2*KNAMELEN], **sp;
151         int i;
152
153         chandevinit();
154
155         if(!waserror()){
156                 snprint(buf, sizeof(buf), "%s %s", arch->id, conffile);
157                 ksetenv("terminal", buf, 0);
158                 ksetenv("cputype", "386", 0);
159                 if(cpuserver)
160                         ksetenv("service", "cpu", 0);
161                 else
162                         ksetenv("service", "terminal", 0);
163                 ksetenv("readparts", "1", 0);
164                 for(i = 0; i < nconf; i++){
165                         if(confname[i][0] != '*')
166                                 ksetenv(confname[i], confval[i], 0);
167                         ksetenv(confname[i], confval[i], 1);
168                 }
169                 poperror();
170         }
171         kproc("alarm", alarmkproc, 0);
172
173         sp = (char**)(USTKTOP - sizeof(Tos) - 8 - sizeof(sp[0])*4);
174         sp[3] = sp[2] = nil;
175         strcpy(sp[1] = (char*)&sp[4], "boot");
176         sp[0] = nil;
177         touser(sp);
178 }
179
180 char*
181 getconf(char *name)
182 {
183         int i;
184
185         for(i = 0; i < nconf; i++)
186                 if(cistrcmp(confname[i], name) == 0)
187                         return confval[i];
188         return 0;
189 }
190
191 static void
192 writeconf(void)
193 {
194         char *p, *q;
195         int n;
196
197         p = getconfenv();
198
199         if(waserror()) {
200                 free(p);
201                 nexterror();
202         }
203
204         /* convert to name=value\n format */
205         for(q=p; *q; q++) {
206                 q += strlen(q);
207                 *q = '=';
208                 q += strlen(q);
209                 *q = '\n';
210         }
211         n = q - p + 1;
212         if(n >= BOOTARGSLEN)
213                 error("kernel configuration too large");
214         memmove(BOOTARGS, p, n);
215         poperror();
216         free(p);
217 }
218
219 void
220 confinit(void)
221 {
222         char *p;
223         int i, userpcnt;
224         ulong kpages;
225
226         for(i = 0; i < nconf; i++)
227                 print("%s=%s\n", confname[i], confval[i]);
228         /* 
229          * all ram above xentop is free, but must be mappable
230          * to virt addrs less than VIRT_START.
231          */
232         kpages = PADDR(hypervisor_virt_start)>>PGSHIFT;
233         if(xenstart->nr_pages <= kpages)
234                 kpages = xenstart->nr_pages;
235         else
236                 print("Warning: Plan 9 / Xen limitation - "
237                           "using only %lud of %lud available RAM pages\n",
238                           kpages, xenstart->nr_pages);
239         xentop = PGROUND(PADDR(xentop));
240         conf.mem[0].npage = kpages - (xentop>>PGSHIFT);
241         conf.mem[0].base = xentop;
242
243         if(p = getconf("*kernelpercent"))
244                 userpcnt = 100 - strtol(p, 0, 0);
245         else
246                 userpcnt = 0;
247
248         conf.npage = 0;
249         for(i=0; i<nelem(conf.mem); i++)
250                 conf.npage += conf.mem[i].npage;
251
252         conf.nproc = 100 + ((conf.npage*BY2PG)/MB)*5;
253         if(cpuserver)
254                 conf.nproc *= 3;
255         if(conf.nproc > 2000)
256                 conf.nproc = 2000;
257         conf.nimage = 200;
258         conf.nswap = conf.nproc*80;
259         conf.nswppo = 4096;
260
261         if(cpuserver) {
262                 if(userpcnt < 10)
263                         userpcnt = 70;
264                 kpages = conf.npage - (conf.npage*userpcnt)/100;
265
266                 /*
267                  * Hack for the big boys. Only good while physmem < 4GB.
268                  * Give the kernel fixed max + enough to allocate the
269                  * page pool.
270                  * This is an overestimate as conf.upages < conf.npages.
271                  * The patch of nimage is a band-aid, scanning the whole
272                  * page list in imagereclaim just takes too long.
273                  */
274                 if(kpages > (64*MB + conf.npage*sizeof(Page))/BY2PG){
275                         kpages = (64*MB + conf.npage*sizeof(Page))/BY2PG;
276                         conf.nimage = 2000;
277                         kpages += (conf.nproc*KSTACK)/BY2PG;
278                 }
279         } else {
280                 if(userpcnt < 10) {
281                         if(conf.npage*BY2PG < 16*MB)
282                                 userpcnt = 40;
283                         else
284                                 userpcnt = 60;
285                 }
286                 kpages = conf.npage - (conf.npage*userpcnt)/100;
287
288                 /*
289                  * Make sure terminals with low memory get at least
290                  * 4MB on the first Image chunk allocation.
291                  */
292                 if(conf.npage*BY2PG < 16*MB)
293                         imagmem->minarena = 4*1024*1024;
294         }
295
296         /*
297          * can't go past the end of virtual memory
298          * (ulong)-KZERO is 2^32 - KZERO
299          */
300         if(kpages > ((ulong)-KZERO)/BY2PG)
301                 kpages = ((ulong)-KZERO)/BY2PG;
302
303         conf.upages = conf.npage - kpages;
304         conf.ialloc = (kpages/2)*BY2PG;
305
306         /*
307          * Guess how much is taken by the large permanent
308          * datastructures. Mntcache and Mntrpc are not accounted for.
309          */
310         kpages *= BY2PG;
311         kpages -= conf.upages*sizeof(Page)
312                 + conf.nproc*sizeof(Proc)
313                 + conf.nimage*sizeof(Image)
314                 + conf.nswap
315                 + conf.nswppo*sizeof(Page*);
316         mainmem->maxsize = kpages;
317         if(!cpuserver){
318                 /*
319                  * give terminals lots of image memory, too; the dynamic
320                  * allocation will balance the load properly, hopefully.
321                  * be careful with 32-bit overflow.
322                  */
323                 imagmem->maxsize = kpages;
324         }
325 }
326
327 void
328 procsetup(Proc *p)
329 {
330         fpuprocsetup(p);
331 }
332
333 void
334 procfork(Proc *p)
335 {
336         fpuprocfork(p);
337 }
338
339 void
340 procrestore(Proc *p)
341 {
342         fpuprocrestore(p);
343 }
344
345 /*
346  *  Save the mach dependent part of the process state.
347  */
348 void
349 procsave(Proc *p)
350 {
351         fpuprocsave(p);
352
353         /*
354          * While this processor is in the scheduler, the process could run
355          * on another processor and exit, returning the page tables to
356          * the free list where they could be reallocated and overwritten.
357          * When this processor eventually has to get an entry from the
358          * trashed page tables it will crash.
359          *
360          * If there's only one processor, this can't happen.
361          * You might think it would be a win not to do this in that case,
362          * especially on VMware, but it turns out not to matter.
363          */
364         mmuflushtlb(0);
365 }
366
367 void
368 reboot(void *entry, void *code, ulong size)
369 {
370         void (*f)(ulong, ulong, ulong);
371
372         writeconf();
373         cpushutdown();
374
375         splhi();
376
377         /* turn off buffered serial console */
378         serialoq = nil;
379
380         /* shutdown devices */
381         chandevshutdown();
382
383         /* reboot(0, ...) on Xen causes domU shutdown */
384         if(entry == 0)
385                 HYPERVISOR_shutdown(0);
386
387         mmuflushtlb(0);
388
389         /* setup reboot trampoline function */
390         f = (void*)REBOOTADDR;
391         memmove(f, rebootcode, sizeof(rebootcode));
392
393         /* off we go - never to return */
394         (*f)(PADDR(entry), PADDR(code), size);
395 }
396
397 void
398 exit(int)
399 {
400         cpushutdown();
401         arch->reset();
402 }