xref: /illumos-gate/usr/src/uts/sparc/os/bootops.c (revision 508de9f313d4260d23954085ed866e727901964c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Definitions of interfaces that provide services from the secondary
28  * boot program to its clients (primarily Solaris, krtld, kmdb and their
29  * successors.) This interface replaces the bootops (BOP) implementation
30  * as the interface to be called by boot clients.
31  *
32  */
33 
34 #include <sys/types.h>
35 #include <sys/systm.h>
36 #include <sys/reboot.h>
37 #include <sys/param.h>
38 #include <sys/varargs.h>
39 #include <sys/obpdefs.h>
40 #include <sys/promimpl.h>
41 #include <sys/prom_plat.h>
42 #include <sys/bootconf.h>
43 #include <sys/bootstat.h>
44 #include <sys/kobj_impl.h>
45 
46 struct bootops *bootops;
47 struct bootops kbootops;
48 
49 pnode_t chosennode;
50 /*
51  * Flag to disable the use of real ramdisks (in the OBP - on Sparc) when
52  * the associated memory is no longer available.
53  */
54 int bootops_obp_ramdisk_disabled = 0;
55 
56 #define	FAKE_ROOT	(pnode_t)1
57 
58 struct fakeprop {
59 	char	*bootname;
60 	pnode_t	promnode;
61 	char	*promname;
62 } fakeprops[] = {
63 	{ "mfg-name", FAKE_ROOT, "name" },
64 	{ NULL, 0, NULL }
65 };
66 
67 static void
fakelook_init(void)68 fakelook_init(void)
69 {
70 	struct fakeprop *fpp = fakeprops;
71 
72 	while (fpp->bootname != NULL) {
73 		switch (fpp->promnode) {
74 		case FAKE_ROOT:
75 			fpp->promnode = prom_rootnode();
76 			break;
77 		}
78 		fpp++;
79 	}
80 }
81 
82 static struct fakeprop *
fakelook(const char * prop)83 fakelook(const char *prop)
84 {
85 	struct fakeprop *fpp = fakeprops;
86 
87 	while (fpp->bootname != NULL) {
88 		if (strcmp(prop, fpp->bootname) == 0)
89 			return (fpp);
90 		fpp++;
91 	}
92 	return (NULL);
93 }
94 
95 ihandle_t bfs_ih = OBP_BADNODE;
96 ihandle_t afs_ih = OBP_BADNODE;
97 
98 void
bop_init(void)99 bop_init(void)
100 {
101 	chosennode = prom_chosennode();
102 
103 	fakelook_init();
104 
105 	/* fake bootops - it needs to point to non-NULL */
106 	bootops = &kbootops;
107 }
108 
109 #define	MAXPROMFD	16
110 
111 static ihandle_t prom_ihs[MAXPROMFD];
112 int filter_etc = 1;
113 
114 /*
115  * Implementation of the "open" boot service.
116  */
117 /*ARGSUSED*/
118 int
bop_open(const char * name,int flags)119 bop_open(const char *name, int flags)
120 {
121 	int fd = -1, layered;
122 	ihandle_t ih;
123 
124 	/*
125 	 * Only look underneath archive for /etc files
126 	 */
127 	layered = filter_etc ?
128 	    strncmp(name, "/etc", sizeof ("/etc") - 1) == 0 : 1;
129 
130 	if (afs_ih != OBP_BADNODE) {
131 		ih = afs_ih;
132 		fd = prom_fopen(ih, (char *)name);
133 		if (fd == -1 && !layered)
134 			return (BOOT_SVC_FAIL);
135 	}
136 	if (fd == -1 && bfs_ih != OBP_BADNODE) {
137 		ih = bfs_ih;
138 		fd = prom_fopen(ih, (char *)name);
139 	}
140 	if (fd == -1)
141 		return (BOOT_SVC_FAIL);
142 	ASSERT(fd < MAXPROMFD);
143 	ASSERT(prom_ihs[fd] == 0);
144 	prom_ihs[fd] = ih;
145 	return (fd);
146 }
147 
148 static void
spinner(void)149 spinner(void)
150 {
151 	static int pos;
152 	static char ind[] = "|/-\\";	/* that's entertainment? */
153 	static int blks_read;
154 
155 	if ((blks_read++ & 0x3) == 0)
156 		prom_printf("%c\b", ind[pos++ & 3]);
157 }
158 
159 /*
160  * Implementation of the "read" boot service.
161  */
162 int
bop_read(int fd,caddr_t buf,size_t size)163 bop_read(int fd, caddr_t buf, size_t size)
164 {
165 	ASSERT(prom_ihs[fd] != 0);
166 	spinner();
167 	return (prom_fread(prom_ihs[fd], fd, buf, size));
168 }
169 
170 /*
171  * Implementation of the "seek" boot service.
172  */
173 int
bop_seek(int fd,off_t off)174 bop_seek(int fd, off_t off)
175 {
176 	ASSERT(prom_ihs[fd] != 0);
177 	return (prom_fseek(prom_ihs[fd], fd, off));
178 }
179 
180 /*
181  * Implementation of the "close" boot service.
182  */
183 int
bop_close(int fd)184 bop_close(int fd)
185 {
186 	ASSERT(prom_ihs[fd] != 0);
187 	prom_fclose(prom_ihs[fd], fd);
188 	prom_ihs[fd] = 0;
189 	return (0);
190 }
191 
192 /*
193  * Simple temp memory allocator
194  *
195  * >PAGESIZE allocations are gotten directly from prom at bighand
196  * smaller ones are satisfied from littlehand, which does a
197  *  1 page bighand allocation when it runs out of memory
198  */
199 static	caddr_t bighand = (caddr_t)BOOTTMPBASE;
200 static	caddr_t littlehand = (caddr_t)BOOTTMPBASE;
201 
202 #define	NTMPALLOC	128
203 
204 static	caddr_t temp_base[NTMPALLOC];
205 static	size_t	temp_size[NTMPALLOC];
206 static	int temp_indx;
207 
208 #if defined(C_OBP)
209 void	cobp_free_mem(caddr_t, size_t);
210 #endif	/* C_OBP */
211 
212 
213 /*
214  * temporary memory storage until bop_tmp_freeall is called
215  * (after the kernel heap is initialized)
216  */
217 caddr_t
bop_temp_alloc(size_t size,int align)218 bop_temp_alloc(size_t size, int align)
219 {
220 	caddr_t ret;
221 
222 	/*
223 	 * OBP allocs 10MB to boot, which is where virthint = 0
224 	 * memory was allocated from.  Without boot, we allocate
225 	 * from BOOTTMPBASE and free when we're ready to take
226 	 * the machine from OBP
227 	 */
228 	if (size < PAGESIZE) {
229 		size_t left =
230 		    ALIGN(littlehand, PAGESIZE) - (uintptr_t)littlehand;
231 
232 		size = roundup(size, MAX(align, 8));
233 		if (size <= left) {
234 			ret = littlehand;
235 			littlehand += size;
236 			return (ret);
237 		}
238 		littlehand = bighand + size;
239 	}
240 	size = roundup(size, PAGESIZE);
241 	ret = prom_alloc(bighand, size, align);
242 	if (ret == NULL)
243 		prom_panic("boot temp overflow");
244 	bighand += size;
245 
246 	/* log it for bop_fini() */
247 	temp_base[temp_indx] = ret;
248 	temp_size[temp_indx] = size;
249 	if (++temp_indx == NTMPALLOC)
250 		prom_panic("out of bop temp space");
251 
252 	return (ret);
253 }
254 
255 void
bop_temp_freeall(void)256 bop_temp_freeall(void)
257 {
258 	int i;
259 
260 	/*
261 	 * We have to call prom_free() with the same args
262 	 * as we used in prom_alloc()
263 	 */
264 	for (i = 0; i < NTMPALLOC; i++) {
265 		if (temp_base[i] == NULL)
266 			break;
267 #if !defined(C_OBP)
268 		prom_free(temp_base[i], temp_size[i]);
269 #else	/* !C_OBP */
270 		cobp_free_mem(temp_base[i], temp_size[i]);
271 #endif	/* !C_OBP */
272 	}
273 }
274 
275 
276 /*
277  * Implementation of the "alloc" boot service.
278  */
279 caddr_t
bop_alloc(caddr_t virthint,size_t size,int align)280 bop_alloc(caddr_t virthint, size_t size, int align)
281 {
282 	if (virthint == NULL)
283 		return (bop_temp_alloc(size, align));
284 	return (prom_alloc(virthint, size, align));
285 }
286 
287 
288 /*
289  * Similar to bop_alloc functionality except that
290  * it will try to breakup into PAGESIZE chunk allocations
291  * if the original single chunk request failed.
292  * This routine does not guarantee physical contig
293  * allocation.
294  */
295 caddr_t
bop_alloc_chunk(caddr_t virthint,size_t size,int align)296 bop_alloc_chunk(caddr_t virthint, size_t size, int align)
297 {
298 	caddr_t ret;
299 	size_t chunksz;
300 
301 	if (virthint == NULL)
302 		return (bop_temp_alloc(size, align));
303 
304 	if ((ret = prom_alloc(virthint, size, align)))
305 		return (ret);
306 
307 	/*
308 	 * Normal request to prom_alloc has failed.
309 	 * We will attempt to satisfy the request by allocating
310 	 * smaller chunks resulting in allocation that
311 	 * will be virtually contiguous but potentially
312 	 * not physically contiguous. There are additional
313 	 * requirements before we want to do this:
314 	 * 1. virthirt must be PAGESIZE aligned.
315 	 * 2. align must not be greater than PAGESIZE
316 	 * 3. size request must be at least PAGESIZE
317 	 * Otherwise, we will revert back to the original
318 	 * bop_alloc behavior i.e. return failure.
319 	 */
320 	if (P2PHASE_TYPED(virthint, PAGESIZE, size_t) != 0 ||
321 	    align > PAGESIZE || size < PAGESIZE)
322 		return (ret);
323 
324 	/*
325 	 * Now we will break up the allocation
326 	 * request in smaller chunks that are
327 	 * always PAGESIZE aligned.
328 	 */
329 	ret = virthint;
330 	chunksz = P2ALIGN((size >> 1), PAGESIZE);
331 	chunksz = MAX(chunksz, PAGESIZE);
332 
333 	while (size) {
334 		do {
335 			/*LINTED E_FUNC_SET_NOT_USED*/
336 			caddr_t res;
337 			if ((res = prom_alloc(virthint, chunksz,
338 			    PAGESIZE))) {
339 				ASSERT(virthint == res);
340 				break;
341 			}
342 
343 			chunksz >>= 1;
344 			chunksz = P2ALIGN(chunksz, PAGESIZE);
345 		} while (chunksz >= PAGESIZE);
346 
347 		if (chunksz < PAGESIZE)
348 			/* Can't really happen.. */
349 			prom_panic("bop_alloc_chunk failed");
350 
351 		virthint += chunksz;
352 		size -= chunksz;
353 		if (size < chunksz)
354 			chunksz = size;
355 	}
356 	return (ret);
357 }
358 
359 
360 /*
361  * Implementation of the "alloc_virt" boot service
362  */
363 caddr_t
bop_alloc_virt(caddr_t virt,size_t size)364 bop_alloc_virt(caddr_t virt, size_t size)
365 {
366 	return (prom_claim_virt(size, virt));
367 }
368 
369 /*
370  * Implementation of the "free" boot service.
371  */
372 /*ARGSUSED*/
373 void
bop_free(caddr_t virt,size_t size)374 bop_free(caddr_t virt, size_t size)
375 {
376 	prom_free(virt, size);
377 }
378 
379 
380 
381 /*
382  * Implementation of the "getproplen" boot service.
383  */
384 /*ARGSUSED*/
385 int
bop_getproplen(const char * name)386 bop_getproplen(const char *name)
387 {
388 	struct fakeprop *fpp;
389 	pnode_t node;
390 	char *prop;
391 
392 	fpp = fakelook(name);
393 	if (fpp != NULL) {
394 		node = fpp->promnode;
395 		prop = fpp->promname;
396 	} else {
397 		node = chosennode;
398 		prop = (char *)name;
399 	}
400 	return (prom_getproplen(node, prop));
401 }
402 
403 /*
404  * Implementation of the "getprop" boot service.
405  */
406 /*ARGSUSED*/
407 int
bop_getprop(const char * name,void * value)408 bop_getprop(const char *name, void *value)
409 {
410 	struct fakeprop *fpp;
411 	pnode_t node;
412 	char *prop;
413 
414 	fpp = fakelook(name);
415 	if (fpp != NULL) {
416 		node = fpp->promnode;
417 		prop = fpp->promname;
418 	} else {
419 		node = chosennode;
420 		prop = (char *)name;
421 	}
422 	return (prom_getprop(node, prop, value));
423 }
424 
425 /*
426  * Implementation of the "print" boot service.
427  */
428 /*ARGSUSED*/
429 void
vbop_printf(void * ptr,const char * fmt,va_list ap)430 vbop_printf(void *ptr, const char *fmt, va_list ap)
431 {
432 	prom_vprintf(fmt, ap);
433 }
434 
435 void
bop_printf(void * ops,const char * fmt,...)436 bop_printf(void *ops, const char *fmt, ...)
437 {
438 	va_list adx;
439 
440 	va_start(adx, fmt);
441 	vbop_printf(ops, fmt, adx);
442 	va_end(adx);
443 }
444 
445 /*
446  * Special routine for kmdb
447  */
448 void
bop_putsarg(const char * fmt,char * arg)449 bop_putsarg(const char *fmt, char *arg)
450 {
451 	prom_printf(fmt, arg);
452 }
453 
454 /*
455  * panic for krtld only
456  */
457 void
bop_panic(const char * s)458 bop_panic(const char *s)
459 {
460 	prom_panic((char *)s);
461 }
462 
463 /*
464  * Implementation of the "mount" boot service.
465  *
466  */
467 /*ARGSUSED*/
468 int
bop_mountroot(void)469 bop_mountroot(void)
470 {
471 	(void) prom_getprop(chosennode, "bootfs", (caddr_t)&bfs_ih);
472 	(void) prom_getprop(chosennode, "archfs", (caddr_t)&afs_ih);
473 	return ((bfs_ih == -1 && afs_ih == -1) ? BOOT_SVC_FAIL : BOOT_SVC_OK);
474 }
475 
476 /*
477  * Implementation of the "unmountroot" boot service.
478  */
479 /*ARGSUSED*/
480 int
bop_unmountroot(void)481 bop_unmountroot(void)
482 {
483 
484 	if (bfs_ih != OBP_BADNODE) {
485 		(void) prom_close(bfs_ih);
486 		bfs_ih = OBP_BADNODE;
487 	}
488 	if (afs_ih != OBP_BADNODE) {
489 		(void) prom_close(afs_ih);
490 		afs_ih = OBP_BADNODE;
491 	}
492 	return (BOOT_SVC_OK);
493 }
494 
495 /*
496  * Implementation of the "fstat" boot service.
497  */
498 int
bop_fstat(int fd,struct bootstat * st)499 bop_fstat(int fd, struct bootstat *st)
500 {
501 	ASSERT(prom_ihs[fd] != 0);
502 	return (prom_fsize(prom_ihs[fd], fd, (size_t *)&st->st_size));
503 }
504 
505 int
boot_compinfo(int fd,struct compinfo * cb)506 boot_compinfo(int fd, struct compinfo *cb)
507 {
508 	ASSERT(prom_ihs[fd] != 0);
509 	return (prom_compinfo(prom_ihs[fd], fd,
510 	    &cb->iscmp, &cb->fsize, &cb->blksize));
511 }
512 
513 void
bop_free_archive(void)514 bop_free_archive(void)
515 {
516 	char archive[OBP_MAXPATHLEN];
517 	pnode_t arph;
518 	uint32_t arbase, arsize, alloc_size;
519 
520 	/*
521 	 * If the ramdisk will eventually be root, or we weren't
522 	 * booted via the archive, then nothing to do here
523 	 */
524 	if (root_is_ramdisk == B_TRUE ||
525 	    prom_getprop(chosennode, "bootarchive", archive) == -1)
526 		return;
527 	arph = prom_finddevice(archive);
528 	if (arph == -1 ||
529 	    prom_getprop(arph, OBP_ALLOCSIZE, (caddr_t)&alloc_size) == -1 ||
530 	    prom_getprop(arph, OBP_SIZE, (caddr_t)&arsize) == -1 ||
531 	    prom_getprop(arph, OBP_ADDRESS, (caddr_t)&arbase) == -1)
532 		prom_panic("can't free boot archive");
533 
534 	bootops_obp_ramdisk_disabled = 1;
535 
536 #if !defined(C_OBP)
537 	if (alloc_size == 0)
538 		prom_free((caddr_t)(uintptr_t)arbase, arsize);
539 	else {
540 		uint32_t arend = arbase + arsize;
541 
542 		while (arbase < arend) {
543 			prom_free((caddr_t)(uintptr_t)arbase,
544 			    MIN(alloc_size, arend - arbase));
545 			arbase += alloc_size;
546 		}
547 	}
548 #else	/* !C_OBP */
549 	cobp_free_mem((caddr_t)(uintptr_t)arbase, arsize);
550 #endif	/* !C_OBP */
551 }
552 
553 #if defined(C_OBP)
554 /*
555  * Blech.  The C proms have a bug when freeing areas that cross
556  * page sizes, so we have to break up the free into sections
557  * bounded by the various pagesizes.
558  */
559 void
cobp_free_mem(caddr_t base,size_t size)560 cobp_free_mem(caddr_t base, size_t size)
561 {
562 	int i;
563 	size_t len, pgsz;
564 
565 	/*
566 	 * Large pages only used when size > 512k
567 	 */
568 	if (size < MMU_PAGESIZE512K ||
569 	    ((uintptr_t)base & MMU_PAGEOFFSET512K) != 0) {
570 		prom_free(base, size);
571 		return;
572 	}
573 	for (i = 3; i >= 0; i--) {
574 		pgsz = page_get_pagesize(i);
575 		if (size < pgsz)
576 			continue;
577 		len = size & ~(pgsz - 1);
578 		prom_free(base, len);
579 		base += len;
580 		size -= len;
581 	}
582 }
583 #endif	/* C_OBP */
584 
585 
586 /*
587  * Implementation of the "enter_mon" boot service.
588  */
589 void
bop_enter_mon(void)590 bop_enter_mon(void)
591 {
592 	prom_enter_mon();
593 }
594 
595 /*
596  * free elf info allocated by booter
597  */
598 void
bop_free_elf(void)599 bop_free_elf(void)
600 {
601 	uint32_t eadr;
602 	uint32_t esize;
603 	extern Addr dynseg;
604 	extern size_t dynsize;
605 
606 	if (bop_getprop("elfheader-address", (caddr_t)&eadr) == -1 ||
607 	    bop_getprop("elfheader-length", (caddr_t)&esize) == -1)
608 		prom_panic("missing elfheader");
609 	prom_free((caddr_t)(uintptr_t)eadr, roundup(esize, PAGESIZE));
610 
611 	prom_free((caddr_t)(uintptr_t)dynseg, roundup(dynsize, PAGESIZE));
612 }
613 
614 
615 /* Simple message to indicate that the bootops pointer has been zeroed */
616 #ifdef DEBUG
617 int bootops_gone_on = 0;
618 #define	BOOTOPS_GONE() \
619 	if (bootops_gone_on) \
620 		prom_printf("The bootops vec is zeroed now!\n");
621 #else
622 #define	BOOTOPS_GONE()
623 #endif	/* DEBUG */
624 
625 void
bop_fini(void)626 bop_fini(void)
627 {
628 	bop_free_archive();
629 	(void) bop_unmountroot();
630 	bop_free_elf();
631 	bop_temp_freeall();
632 
633 	bootops = (struct bootops *)NULL;
634 	BOOTOPS_GONE();
635 }
636