summaryrefslogtreecommitdiff
path: root/usr/src/uts/sparc/os/bootops.c
blob: dd11142738deaf943313cfdbbce03bea3638fd99 (plain)
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Definitions of interfaces that provide services from the secondary
 * boot program to its clients (primarily Solaris, krtld, kmdb and their
 * successors.) This interface replaces the bootops (BOP) implementation
 * as the interface to be called by boot clients.
 *
 */

#include <sys/types.h>
#include <sys/systm.h>
#include <sys/reboot.h>
#include <sys/param.h>
#include <sys/varargs.h>
#include <sys/obpdefs.h>
#include <sys/promimpl.h>
#include <sys/prom_plat.h>
#include <sys/bootconf.h>
#include <sys/bootstat.h>
#include <sys/kobj_impl.h>

struct bootops *bootops;
struct bootops kbootops;

pnode_t chosennode;
/*
 * Flag to disable the use of real ramdisks (in the OBP - on Sparc) when
 * the associated memory is no longer available.
 */
int bootops_obp_ramdisk_disabled = 0;

#define	FAKE_ROOT	(pnode_t)1

struct fakeprop {
	char	*bootname;
	pnode_t	promnode;
	char	*promname;
} fakeprops[] = {
	{ "mfg-name", FAKE_ROOT, "name" },
	{ NULL, 0, NULL }
};

static void
fakelook_init(void)
{
	struct fakeprop *fpp = fakeprops;

	while (fpp->bootname != NULL) {
		switch (fpp->promnode) {
		case FAKE_ROOT:
			fpp->promnode = prom_rootnode();
			break;
		}
		fpp++;
	}
}

static struct fakeprop *
fakelook(const char *prop)
{
	struct fakeprop *fpp = fakeprops;

	while (fpp->bootname != NULL) {
		if (strcmp(prop, fpp->bootname) == 0)
			return (fpp);
		fpp++;
	}
	return (NULL);
}

ihandle_t bfs_ih = OBP_BADNODE;
ihandle_t afs_ih = OBP_BADNODE;

void
bop_init(void)
{
	chosennode = prom_chosennode();

	fakelook_init();

	/* fake bootops - it needs to point to non-NULL */
	bootops = &kbootops;
}

#define	MAXPROMFD	16

static ihandle_t prom_ihs[MAXPROMFD];
int filter_etc = 1;

/*
 * Implementation of the "open" boot service.
 */
/*ARGSUSED*/
int
bop_open(const char *name, int flags)
{
	int fd = -1, layered;
	ihandle_t ih;

	/*
	 * Only look underneath archive for /etc files
	 */
	layered = filter_etc ?
	    strncmp(name, "/etc", sizeof ("/etc") - 1) == 0 : 1;

	if (afs_ih != OBP_BADNODE) {
		ih = afs_ih;
		fd = prom_fopen(ih, (char *)name);
		if (fd == -1 && !layered)
			return (BOOT_SVC_FAIL);
	}
	if (fd == -1 && bfs_ih != OBP_BADNODE) {
		ih = bfs_ih;
		fd = prom_fopen(ih, (char *)name);
	}
	if (fd == -1)
		return (BOOT_SVC_FAIL);
	ASSERT(fd < MAXPROMFD);
	ASSERT(prom_ihs[fd] == 0);
	prom_ihs[fd] = ih;
	return (fd);
}

static void
spinner(void)
{
	static int pos;
	static char ind[] = "|/-\\";	/* that's entertainment? */
	static int blks_read;

	if ((blks_read++ & 0x3) == 0)
		prom_printf("%c\b", ind[pos++ & 3]);
}

/*
 * Implementation of the "read" boot service.
 */
int
bop_read(int fd, caddr_t buf, size_t size)
{
	ASSERT(prom_ihs[fd] != 0);
	spinner();
	return (prom_fread(prom_ihs[fd], fd, buf, size));
}

/*
 * Implementation of the "seek" boot service.
 */
int
bop_seek(int fd, off_t off)
{
	ASSERT(prom_ihs[fd] != 0);
	return (prom_fseek(prom_ihs[fd], fd, off));
}

/*
 * Implementation of the "close" boot service.
 */
int
bop_close(int fd)
{
	ASSERT(prom_ihs[fd] != 0);
	prom_fclose(prom_ihs[fd], fd);
	prom_ihs[fd] = 0;
	return (0);
}

/*
 * Simple temp memory allocator
 *
 * >PAGESIZE allocations are gotten directly from prom at bighand
 * smaller ones are satisfied from littlehand, which does a
 *  1 page bighand allocation when it runs out of memory
 */
static	caddr_t bighand = (caddr_t)BOOTTMPBASE;
static	caddr_t littlehand = (caddr_t)BOOTTMPBASE;

#define	NTMPALLOC	128

static	caddr_t temp_base[NTMPALLOC];
static	size_t	temp_size[NTMPALLOC];
static	int temp_indx;

#if defined(C_OBP)
void	cobp_free_mem(caddr_t, size_t);
#endif	/* C_OBP */


/*
 * temporary memory storage until bop_tmp_freeall is called
 * (after the kernel heap is initialized)
 */
caddr_t
bop_temp_alloc(size_t size, int align)
{
	caddr_t ret;

	/*
	 * OBP allocs 10MB to boot, which is where virthint = 0
	 * memory was allocated from.  Without boot, we allocate
	 * from BOOTTMPBASE and free when we're ready to take
	 * the machine from OBP
	 */
	if (size < PAGESIZE) {
		size_t left =
		    ALIGN(littlehand, PAGESIZE) - (uintptr_t)littlehand;

		size = roundup(size, MAX(align, 8));
		if (size <= left) {
			ret = littlehand;
			littlehand += size;
			return (ret);
		}
		littlehand = bighand + size;
	}
	size = roundup(size, PAGESIZE);
	ret = prom_alloc(bighand, size, align);
	if (ret == NULL)
		prom_panic("boot temp overflow");
	bighand += size;

	/* log it for bop_fini() */
	temp_base[temp_indx] = ret;
	temp_size[temp_indx] = size;
	if (++temp_indx == NTMPALLOC)
		prom_panic("out of bop temp space");

	return (ret);
}

void
bop_temp_freeall(void)
{
	int i;

	/*
	 * We have to call prom_free() with the same args
	 * as we used in prom_alloc()
	 */
	for (i = 0; i < NTMPALLOC; i++) {
		if (temp_base[i] == NULL)
			break;
#if !defined(C_OBP)
		prom_free(temp_base[i], temp_size[i]);
#else	/* !C_OBP */
		cobp_free_mem(temp_base[i], temp_size[i]);
#endif	/* !C_OBP */
	}
}


/*
 * Implementation of the "alloc" boot service.
 */
caddr_t
bop_alloc(caddr_t virthint, size_t size, int align)
{
	if (virthint == NULL)
		return (bop_temp_alloc(size, align));
	return (prom_alloc(virthint, size, align));
}


/*
 * Similar to bop_alloc functionality except that
 * it will try to breakup into PAGESIZE chunk allocations
 * if the original single chunk request failed.
 * This routine does not guarantee physical contig
 * allocation.
 */
caddr_t
bop_alloc_chunk(caddr_t virthint, size_t size, int align)
{
	caddr_t ret;
	size_t chunksz;

	if (virthint == NULL)
		return (bop_temp_alloc(size, align));

	if ((ret = prom_alloc(virthint, size, align)))
		return (ret);

	/*
	 * Normal request to prom_alloc has failed.
	 * We will attempt to satisfy the request by allocating
	 * smaller chunks resulting in allocation that
	 * will be virtually contiguous but potentially
	 * not physically contiguous. There are additional
	 * requirements before we want to do this:
	 * 1. virthirt must be PAGESIZE aligned.
	 * 2. align must not be greater than PAGESIZE
	 * 3. size request must be at least PAGESIZE
	 * Otherwise, we will revert back to the original
	 * bop_alloc behavior i.e. return failure.
	 */
	if (P2PHASE_TYPED(virthint, PAGESIZE, size_t) != 0 ||
	    align > PAGESIZE || size < PAGESIZE)
		return (ret);

	/*
	 * Now we will break up the allocation
	 * request in smaller chunks that are
	 * always PAGESIZE aligned.
	 */
	ret = virthint;
	chunksz = P2ALIGN((size >> 1), PAGESIZE);
	chunksz = MAX(chunksz, PAGESIZE);

	while (size) {
		do {
			/*LINTED E_FUNC_SET_NOT_USED*/
			caddr_t res;
			if ((res = prom_alloc(virthint, chunksz,
			    PAGESIZE))) {
				ASSERT(virthint == res);
				break;
			}

			chunksz >>= 1;
			chunksz = P2ALIGN(chunksz, PAGESIZE);
		} while (chunksz >= PAGESIZE);

		if (chunksz < PAGESIZE)
			/* Can't really happen.. */
			prom_panic("bop_alloc_chunk failed");

		virthint += chunksz;
		size -= chunksz;
		if (size < chunksz)
			chunksz = size;
	}
	return (ret);
}


/*
 * Implementation of the "alloc_virt" boot service
 */
caddr_t
bop_alloc_virt(caddr_t virt, size_t size)
{
	return (prom_claim_virt(size, virt));
}

/*
 * Implementation of the "free" boot service.
 */
/*ARGSUSED*/
void
bop_free(caddr_t virt, size_t size)
{
	prom_free(virt, size);
}



/*
 * Implementation of the "getproplen" boot service.
 */
/*ARGSUSED*/
int
bop_getproplen(const char *name)
{
	struct fakeprop *fpp;
	pnode_t node;
	char *prop;

	fpp = fakelook(name);
	if (fpp != NULL) {
		node = fpp->promnode;
		prop = fpp->promname;
	} else {
		node = chosennode;
		prop = (char *)name;
	}
	return (prom_getproplen(node, prop));
}

/*
 * Implementation of the "getprop" boot service.
 */
/*ARGSUSED*/
int
bop_getprop(const char *name, void *value)
{
	struct fakeprop *fpp;
	pnode_t node;
	char *prop;

	fpp = fakelook(name);
	if (fpp != NULL) {
		node = fpp->promnode;
		prop = fpp->promname;
	} else {
		node = chosennode;
		prop = (char *)name;
	}
	return (prom_getprop(node, prop, value));
}

/*
 * Implementation of the "print" boot service.
 */
/*ARGSUSED*/
void
vbop_printf(void *ptr, const char *fmt, va_list ap)
{
	prom_vprintf(fmt, ap);
}

void
bop_printf(void *ops, const char *fmt, ...)
{
	va_list adx;

	va_start(adx, fmt);
	vbop_printf(ops, fmt, adx);
	va_end(adx);
}

/*
 * Special routine for kmdb
 */
void
bop_putsarg(const char *fmt, char *arg)
{
	prom_printf(fmt, arg);
}

/*
 * panic for krtld only
 */
void
bop_panic(const char *s)
{
	prom_panic((char *)s);
}

/*
 * Implementation of the "mount" boot service.
 *
 */
/*ARGSUSED*/
int
bop_mountroot(void)
{
	(void) prom_getprop(chosennode, "bootfs", (caddr_t)&bfs_ih);
	(void) prom_getprop(chosennode, "archfs", (caddr_t)&afs_ih);
	return ((bfs_ih == -1 && afs_ih == -1) ? BOOT_SVC_FAIL : BOOT_SVC_OK);
}

/*
 * Implementation of the "unmountroot" boot service.
 */
/*ARGSUSED*/
int
bop_unmountroot(void)
{

	if (bfs_ih != OBP_BADNODE) {
		(void) prom_close(bfs_ih);
		bfs_ih = OBP_BADNODE;
	}
	if (afs_ih != OBP_BADNODE) {
		(void) prom_close(afs_ih);
		afs_ih = OBP_BADNODE;
	}
	return (BOOT_SVC_OK);
}

/*
 * Implementation of the "fstat" boot service.
 */
int
bop_fstat(int fd, struct bootstat *st)
{
	ASSERT(prom_ihs[fd] != 0);
	return (prom_fsize(prom_ihs[fd], fd, (size_t *)&st->st_size));
}

int
boot_compinfo(int fd, struct compinfo *cb)
{
	ASSERT(prom_ihs[fd] != 0);
	return (prom_compinfo(prom_ihs[fd], fd,
	    &cb->iscmp, &cb->fsize, &cb->blksize));
}

void
bop_free_archive(void)
{
	char archive[OBP_MAXPATHLEN];
	pnode_t arph;
	uint32_t arbase, arsize, alloc_size;

	/*
	 * If the ramdisk will eventually be root, or we weren't
	 * booted via the archive, then nothing to do here
	 */
	if (root_is_ramdisk == B_TRUE ||
	    prom_getprop(chosennode, "bootarchive", archive) == -1)
		return;
	arph = prom_finddevice(archive);
	if (arph == -1 ||
	    prom_getprop(arph, OBP_ALLOCSIZE, (caddr_t)&alloc_size) == -1 ||
	    prom_getprop(arph, OBP_SIZE, (caddr_t)&arsize) == -1 ||
	    prom_getprop(arph, OBP_ADDRESS, (caddr_t)&arbase) == -1)
		prom_panic("can't free boot archive");

	bootops_obp_ramdisk_disabled = 1;

#if !defined(C_OBP)
	if (alloc_size == 0)
		prom_free((caddr_t)(uintptr_t)arbase, arsize);
	else {
		uint32_t arend = arbase + arsize;

		while (arbase < arend) {
			prom_free((caddr_t)(uintptr_t)arbase,
			    MIN(alloc_size, arend - arbase));
			arbase += alloc_size;
		}
	}
#else	/* !C_OBP */
	cobp_free_mem((caddr_t)(uintptr_t)arbase, arsize);
#endif	/* !C_OBP */
}

#if defined(C_OBP)
/*
 * Blech.  The C proms have a bug when freeing areas that cross
 * page sizes, so we have to break up the free into sections
 * bounded by the various pagesizes.
 */
void
cobp_free_mem(caddr_t base, size_t size)
{
	int i;
	size_t len, pgsz;

	/*
	 * Large pages only used when size > 512k
	 */
	if (size < MMU_PAGESIZE512K ||
	    ((uintptr_t)base & MMU_PAGEOFFSET512K) != 0) {
		prom_free(base, size);
		return;
	}
	for (i = 3; i >= 0; i--) {
		pgsz = page_get_pagesize(i);
		if (size < pgsz)
			continue;
		len = size & ~(pgsz - 1);
		prom_free(base, len);
		base += len;
		size -= len;
	}
}
#endif	/* C_OBP */


/*
 * Implementation of the "enter_mon" boot service.
 */
void
bop_enter_mon(void)
{
	prom_enter_mon();
}

/*
 * free elf info allocated by booter
 */
void
bop_free_elf(void)
{
	uint32_t eadr;
	uint32_t esize;
	extern Addr dynseg;
	extern size_t dynsize;

	if (bop_getprop("elfheader-address", (caddr_t)&eadr) == -1 ||
	    bop_getprop("elfheader-length", (caddr_t)&esize) == -1)
		prom_panic("missing elfheader");
	prom_free((caddr_t)(uintptr_t)eadr, roundup(esize, PAGESIZE));

	prom_free((caddr_t)(uintptr_t)dynseg, roundup(dynsize, PAGESIZE));
}


/* Simple message to indicate that the bootops pointer has been zeroed */
#ifdef DEBUG
int bootops_gone_on = 0;
#define	BOOTOPS_GONE() \
	if (bootops_gone_on) \
		prom_printf("The bootops vec is zeroed now!\n");
#else
#define	BOOTOPS_GONE()
#endif	/* DEBUG */

void
bop_fini(void)
{
	bop_free_archive();
	(void) bop_unmountroot();
	bop_free_elf();
	bop_temp_freeall();

	bootops = (struct bootops *)NULL;
	BOOTOPS_GONE();
}