~kris/9p

9hist

ref: 0895365659e3318b8d2ce6c29a362b5378564f8d 9hist/carrera/main.c -rw-r--r-- 9.4 KiB
08953656 — David du Colombier Plan 9 from Bell Labs 1994-07-27 32 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include	"u.h"
#include	"../port/lib.h"
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"io.h"
#include	"init.h"

/*
 *  args passed by boot process
 */
int _argc; char **_argv; char **_env;

/*
 *  arguments passed to initcode and /boot
 */
char argbuf[128];

/*
 *  environment passed to boot -- sysname, consname, diskid
 */
char consname[NAMELEN];
char bootdisk[NAMELEN];
char screenldepth[NAMELEN];

/*
 * software tlb simulation
 */
Softtlb stlb[MAXMACH][STLBSIZE];

Conf	conf;
FPsave	initfp;

int	int0mask = 0xff;	/* interrupts enabled for first 8259 */
int	int1mask = 0xff;	/* interrupts enabled for second 8259 */

extern	uchar rdbgcode[];
extern	ulong	rdbglen;

void
main(void)
{
	tlbinit();
	ioinit(1);		/* Very early to establish IO mappings */
	rdbginit();
	arginit();
	confinit();
	savefpregs(&initfp);
	machinit();
	kmapinit();
	xinit();
	iomapinit();
	printinit();
	serialinit();
	vecinit();
	screeninit();
	iprint("\n\nBrazil\n");
	pageinit();
	procinit0();
	initseg();
	links();
	chandevreset();
	swapinit();
	userinit();
	schedinit();
}


/*
 *  copy arguments passed by the boot kernel (or ROM) into a temporary buffer.
 *  we do this because the arguments are in memory that may be allocated
 *  to processes or kernel buffers.
 *
 *  also grab any environment variables that might be useful
 */
struct
{
	char	*name;
	char	*val;
}bootenv[] =
{
	{"netaddr=",	sysname},
	{"console=",	consname},
	{"bootdisk=",	bootdisk},
	{"ldepth=",	screenldepth},
};
char *sp;

char *
pusharg(char *p)
{
	int n;

	n = strlen(p)+1;
	sp -= n;
	memmove(sp, p, n);
	return sp;
}

void
arginit(void)
{
	int i, n;
	char **av;

	/*
	 *  get boot env variables
	 */
	if(*sysname == 0)
		for(av = _env; *av; av++)
			for(i=0; i < sizeof bootenv/sizeof bootenv[0]; i++){
				n = strlen(bootenv[i].name);
				if(strncmp(*av, bootenv[i].name, n) == 0){
					strncpy(bootenv[i].val, (*av)+n, NAMELEN);
					bootenv[i].val[NAMELEN-1] = '\0';
					break;
				}
			}

	/*
	 *  pack args into buffer
	 */
	av = (char**)argbuf;
	sp = argbuf + sizeof(argbuf);
	for(i = 0; i < _argc; i++){
		if(strchr(_argv[i], '='))
			break;
		av[i] = pusharg(_argv[i]);
	}
	av[i] = 0;
}

/*
 *  initialize a processor's mach structure.  each processor does this
 *  for itself.
 */
void
machinit(void)
{
	int n;

	/* Ensure CU1 is off */
	clrfpintr();

	/* scrub cache */
	((void(*)(void))((ulong)cleancache|0xA0000000))();

	memset(m, 0, sizeof(Mach));

	n = m->machno;
	m->stb = &stlb[n][0];

	m->speed = 50;
	clockinit();

	active.exiting = 0;
	active.machs = 1;
}

/*
 * Set up a console on serial port 2
 */
void
serialinit(void)
{
	NS16552setup(Uart1, UartFREQ);
	kbdinit();
}

/*
 * Map IO address space in wired down TLB entry 1
 */
void
ioinit(int mapeisa)
{
	ulong devphys, isaphys, intphys, isamphys, promphys, ptec, ptes, v;

	/*
	 * If you want to segattach the eisa space these
	 * mappings must be turned off to prevent duplication
	 * of the tlb entries
	 */
	if(mapeisa) {
		isaphys = IOPTE|PPN(Eisaphys)|PTEGLOBL;
		isamphys = 0x04000000|IOPTE|PTEGLOBL;
	}
	else {
		isaphys = PTEGLOBL;
		isamphys = PTEGLOBL;
	}

	/*
	 * Map devices and the Eisa control space
	 */
	devphys = IOPTE|PPN(Devicephys);
	puttlbx(1, Devicevirt, devphys, isaphys, PGSZ64K);

	/*
	 * Map Interrupt control & Eisa memory
	 */
	intphys  = IOPTE|PPN(Intctlphys);
	puttlbx(2, Intctlvirt, intphys, isamphys, PGSZ1M);

	/* Enable all device interrupts */
	IO(ushort, Intenareg) = 0xffff;

	/* Map the rom back into Promvirt to allow NMI handling */
	promphys = IOPTE|PPN(Promphys);
	puttlbx(3, Promvirt, promphys, PTEGLOBL, PGSZ1M);

	/* 8 MB video ram config */
	v = IO(ulong, 0xE0000004);
	v &= ~(3<<8);
	v |= (2<<8);
	IO(ulong, 0xE0000004) = v;

	/* Map the display hardware */
	ptec = PPN(VideoCTL)|PTEGLOBL|PTEVALID|PTEWRITE|PTEUNCACHED;
	ptes = PPN(VideoMEM)|PTEGLOBL|PTEVALID|PTEWRITE|PTEUNCACHED;
	puttlbx(4, Screenvirt, ptes, ptec, PGSZ4M);

	/* for PC weenies */
	/*
	 *  Set up the first 8259 interrupt processor.
	 *  Make 8259 interrupts start at CPU vector Int0vec.
	 *  Set the 8259 as master with edge triggered
	 *  input with fully nested interrupts.
	 */
	EISAOUTB(Int0ctl, 0x11);	/* ICW1 - edge triggered, master,
					   ICW4 will be sent */
	EISAOUTB(Int0aux, Int0vec);	/* ICW2 - interrupt vector offset */
	EISAOUTB(Int0aux, 0x04);	/* ICW3 - have slave on level 2 */
	EISAOUTB(Int0aux, 0x01);	/* ICW4 - 8086 mode, not buffered */

	/*
	 *  Set up the second 8259 interrupt processor.
	 *  Make 8259 interrupts start at CPU vector Int0vec.
	 *  Set the 8259 as master with edge triggered
	 *  input with fully nested interrupts.
	 */
	EISAOUTB(Int1ctl, 0x11);	/* ICW1 - edge triggered, master,
					   ICW4 will be sent */
	EISAOUTB(Int1aux, Int1vec);	/* ICW2 - interrupt vector offset */
	EISAOUTB(Int1aux, 0x02);	/* ICW3 - I am a slave on level 2 */
	EISAOUTB(Int1aux, 0x01);	/* ICW4 - 8086 mode, not buffered */

	/*
	 *  pass #2 8259 interrupts to #1
	 */

	/* enable all PC interrupts except the clock */
	int0mask = 0;
	int0mask |= 1<<(Clockvec&7);
	EISAOUTB(Int0aux, int0mask);

	int1mask = 0;
	EISAOUTB(Int1aux, int1mask);

	IO(ulong, R4030ier) = 0xf;	/* enable eisa interrupts */
}

/*
 * Pull the ethernet address out of NVRAM
 */
void
enetaddr(uchar *ea)
{
	int i;
	uchar tbuf[8];

	for(i = 0; i < 8; i++)
		tbuf[i] = ((uchar*)(NvramRO+Enetoffset))[i];

	print("ether:");
	for(i = 0; i < 6; i++) {
		ea[i] = tbuf[7-i];
		print("%2.2ux", ea[i]);
	}
	print("\n");
}

/*
 * All DMA and ether IO buffers must reside in the first 16M bytes of
 * memory to be covered by the translation registers
 */
void
iomapinit(void)
{
	int i;
	Tte *t;

	t = xspanalloc(Ntranslation*sizeof(Tte), BY2PG, 0);

	for(i = 0; i < Ntranslation; i++)
		t[i].lo = i<<PGSHIFT;

	/* Set the translation table */
	IO(ulong, Ttbr) = PADDR(t);
	IO(ulong, Tlrb) = Ntranslation*sizeof(Tte);

	/* Invalidate the old entries */
	IO(ulong, Tir) = 0;
}

/*
 *  setup MIPS trap vectors
 */
void
vecinit(void)
{
	memmove((ulong*)UTLBMISS, (ulong*)vector0, 0x80);
	memmove((ulong*)XEXCEPTION, (ulong*)vector0, 0x80);
	memmove((ulong*)CACHETRAP, (ulong*)vector100, 0x80);
	memmove((ulong*)EXCEPTION, (ulong*)vector180, 0x80);

	icflush((ulong*)UTLBMISS, 8*1024);
}

void
init0(void)
{
	char buf[2*NAMELEN];

	spllo();

	/*
	 * These are o.k. because rootinit is null.
	 * Then early kproc's will have a root and dot.
	 */
	up->slash = namec("#/", Atodir, 0, 0);
	up->dot = clone(up->slash, 0);

	chandevinit();

	if(!waserror()){
		ksetenv("cputype", "mips");
		sprint(buf, "carrera %s R4400PC", conffile);
		ksetenv("terminal", buf);
		ksetenv("sysname", sysname);
		poperror();
	}

	kproc("alarm", alarmkproc, 0);
	touser((uchar*)(USTKTOP-sizeof(argbuf)));
}

void
userinit(void)
{
	Proc *p;
	KMap *k;
	Page *pg;
	char **av;
	Segment *s;

	p = newproc();
	p->pgrp = newpgrp();
	p->egrp = smalloc(sizeof(Egrp));
	p->egrp->ref = 1;
	p->fgrp = smalloc(sizeof(Fgrp));
	p->fgrp->ref = 1;
	p->procmode = 0640;

	strcpy(p->text, "*init*");
	strcpy(p->user, eve);

	p->fpstate = FPinit;
	p->fpsave.fpstatus = initfp.fpstatus;

	/*
	 * Kernel Stack
	 */
	p->sched.pc = (ulong)init0;
	p->sched.sp = (ulong)p->kstack+KSTACK-(1+MAXSYSARG)*BY2WD;
	/*
	 * User Stack, pass input arguments to boot process
	 */
	s = newseg(SG_STACK, USTKTOP-USTKSIZE, USTKSIZE/BY2PG);
	p->seg[SSEG] = s;
	pg = newpage(1, 0, USTKTOP-BY2PG);
	segpage(s, pg);
	k = kmap(pg);
	for(av = (char**)argbuf; *av; av++)
		*av += (USTKTOP - sizeof(argbuf)) - (ulong)argbuf;

	memmove((uchar*)VA(k) + BY2PG - sizeof(argbuf), argbuf, sizeof argbuf);
	kunmap(k);

	/* Text */
	s = newseg(SG_TEXT, UTZERO, 1);
	s->flushme++;
	p->seg[TSEG] = s;
	pg = newpage(1, 0, UTZERO);
	memset(pg->cachectl, PG_TXTFLUSH, sizeof(pg->cachectl));
	segpage(s, pg);
	k = kmap(s->map[0]->pages[0]);
	memmove((ulong*)VA(k), initcode, sizeof initcode);
	kunmap(k);

	ready(p);
}

void
exit(long type)
{
	uchar *vec;

	USED(type);

	spllo();
	print("cpu%d exiting\n", m->machno);
	while(consactive())
		delay(10);

	splhi();
	/* Turn off the NMI hander for the debugger */
	vec = (uchar*)0xA0000420;
	vec[0] = 0;

	firmware(type);
}

void
confinit(void)
{
	ulong ktop, top;

	ktop = PGROUND((ulong)end);
	ktop = PADDR(ktop);
	top = (16*1024*1024)/BY2PG;

	conf.base0 = 0;
	conf.npage0 = top;
	conf.npage = conf.npage0;
	conf.npage0 -= ktop/BY2PG;
	conf.base0 += ktop;
	conf.npage1 = 0;
	conf.base1 = 0;

	conf.upages = (conf.npage*70)/100;
	conf.ialloc = ((conf.npage-conf.upages)/2)*BY2PG;

	conf.nmach = 1;

	/* set up other configuration parameters */
	conf.nproc = 100;
	conf.nswap = conf.npage*3;
	conf.nimage = 200;

	conf.monitor = 1;

	conf.copymode = 0;		/* copy on write */
}

void
procsave(Proc *p)
{
	USED(p);

	/* keep track of tlbfaults */
	m->otlbfault = m->tlbfault;
}

void
buzz(int f, int d)
{
	USED(f);
	USED(d);
}

/*
	register offsets of ARCS prom jmpbuf
		JB_PC		0
		JB_SP		1
		JB_FP		2
		JB_S0		3
		JB_S1		4
		JB_S2		5
		JB_S3		6
		JB_S4		7
		JB_S5		8
		JB_S6		9
		JB_S7		10
*/

struct 
{
	ulong	pc;
	ulong	sp;
	ulong	fp;
	ulong	s[7];
} Mipsjmpbuf;

void
rdbginit(void)
{
	uchar *vec;
	ulong jba;
return;/**/
	/* Only interested in the PC */
	Mipsjmpbuf.pc = 0xA001C020;

	/* Link an NMI handler to the debugger
	 * - addresses from the ARCS rom source
	 */
	vec = (uchar*)0xA0000420;
	jba = (ulong)UNCACHED(void, &Mipsjmpbuf);

	vec[0] = 'N';
	vec[1] = 'm';
	vec[2] = 'i';
	vec[3] = 's';
	vec[4] = jba>>24;
	vec[5] = jba>>16;
	vec[6] = jba>>8;
	vec[7] = jba;

	/* Install the debugger code in a known place */
	memmove((void*)0xA001C000, rdbgcode, rdbglen);
}