xref: /onnv-gate/usr/src/psm/stand/cpr/sparcv9/sun4u/cprboot.c (revision 7632:91aa3d8541b5)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226423Sgw25295  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * cprboot - prom client that restores kadb/kernel pages
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * simple cprboot overview:
300Sstevel@tonic-gate  *	reset boot-file/boot-device to their original values
310Sstevel@tonic-gate  * 	open cpr statefile, usually "/.CPR"
320Sstevel@tonic-gate  *	read in statefile
330Sstevel@tonic-gate  *	close statefile
340Sstevel@tonic-gate  *	restore kernel pages
350Sstevel@tonic-gate  *	jump back into kernel text
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * cprboot supports a restartable statefile for FAA/STARS,
390Sstevel@tonic-gate  * Federal Aviation Administration
400Sstevel@tonic-gate  * Standard Terminal Automation Replacement System
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <sys/types.h>
440Sstevel@tonic-gate #include <sys/cpr.h>
450Sstevel@tonic-gate #include <sys/promimpl.h>
460Sstevel@tonic-gate #include <sys/ddi.h>
470Sstevel@tonic-gate #include "cprboot.h"
480Sstevel@tonic-gate 
496423Sgw25295 char *volname = NULL;
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  * local defs
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate #define	CB_MAXPROP	256
550Sstevel@tonic-gate #define	CB_MAXARGS	8
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * globals
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate struct statefile sfile;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate char cpr_statefile[OBP_MAXPATHLEN];
640Sstevel@tonic-gate char cpr_filesystem[OBP_MAXPATHLEN];
650Sstevel@tonic-gate 
660Sstevel@tonic-gate int cpr_debug;				/* cpr debug, set with uadmin 3 10x */
670Sstevel@tonic-gate uint_t cb_msec;				/* cprboot start runtime */
680Sstevel@tonic-gate uint_t cb_dents;			/* number of dtlb entries */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate int do_halt = 0;			/* halt (enter mon) after load */
710Sstevel@tonic-gate int verbose = 0;			/* verbose, traces cprboot ops */
720Sstevel@tonic-gate 
730Sstevel@tonic-gate char rsvp[] = "please reboot";
740Sstevel@tonic-gate char prog[] = "cprboot";
750Sstevel@tonic-gate char entry[] = "ENTRY";
760Sstevel@tonic-gate char ent_fmt[] = "\n%s %s\n";
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * file scope
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate static char cb_argbuf[CB_MAXPROP];
830Sstevel@tonic-gate static char *cb_args[CB_MAXARGS];
840Sstevel@tonic-gate 
850Sstevel@tonic-gate static int reusable;
865648Ssetje char *specialstate;
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 
890Sstevel@tonic-gate static int
cb_intro(void)900Sstevel@tonic-gate cb_intro(void)
910Sstevel@tonic-gate {
920Sstevel@tonic-gate 	static char cstr[] = "\014" "\033[1P" "\033[18;21H";
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	CB_VENTRY(cb_intro);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * build/debug aid; this condition should not occur
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	if ((uintptr_t)_end > CB_SRC_VIRT) {
1000Sstevel@tonic-gate 		prom_printf("\ndata collision:\n"
101785Seota 		    "(_end=0x%p > CB_LOW_VIRT=0x%x), recompile...\n",
102*7632SNick.Todd@Sun.COM 		    (void *)_end, CB_SRC_VIRT);
1030Sstevel@tonic-gate 		return (ERR);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/* clear console */
1070Sstevel@tonic-gate 	prom_printf(cstr);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	prom_printf("Restoring the System. Please Wait... ");
1100Sstevel@tonic-gate 	return (0);
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * read bootargs and convert to arg vector
1160Sstevel@tonic-gate  *
1170Sstevel@tonic-gate  * sets globals:
1180Sstevel@tonic-gate  *	cb_argbuf
1190Sstevel@tonic-gate  *	cb_args
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate static void
get_bootargs(void)1220Sstevel@tonic-gate get_bootargs(void)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	char *cp, *tail, *argp, **argv;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	CB_VENTRY(get_bootargs);
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	(void) prom_strcpy(cb_argbuf, prom_bootargs());
1290Sstevel@tonic-gate 	tail = cb_argbuf + prom_strlen(cb_argbuf);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * scan to the trailing NULL so the last arg
1330Sstevel@tonic-gate 	 * will be found without any special-case code
1340Sstevel@tonic-gate 	 */
1350Sstevel@tonic-gate 	argv = cb_args;
1360Sstevel@tonic-gate 	for (cp = argp = cb_argbuf; cp <= tail; cp++) {
1370Sstevel@tonic-gate 		if (prom_strchr(" \t\n\r", *cp) == NULL)
1380Sstevel@tonic-gate 			continue;
1390Sstevel@tonic-gate 		*cp = '\0';
1400Sstevel@tonic-gate 		if (cp - argp) {
1410Sstevel@tonic-gate 			*argv++ = argp;
1420Sstevel@tonic-gate 			if ((argv - cb_args) == (CB_MAXARGS - 1))
1430Sstevel@tonic-gate 				break;
1440Sstevel@tonic-gate 		}
1450Sstevel@tonic-gate 		argp = cp + 1;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	*argv = NULLP;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if (verbose) {
1500Sstevel@tonic-gate 		for (argv = cb_args; *argv; argv++) {
151*7632SNick.Todd@Sun.COM 			prom_printf("    %d: \"%s\"\n",
152*7632SNick.Todd@Sun.COM 			    (int)(argv - cb_args), *argv);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 	}
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static void
usage(char * expect,char * got)1590Sstevel@tonic-gate usage(char *expect, char *got)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate 	if (got == NULL)
1620Sstevel@tonic-gate 		got = "(NULL)";
1630Sstevel@tonic-gate 	prom_printf("\nbad OBP boot args: expect %s, got %s\n"
1640Sstevel@tonic-gate 	    "Usage: boot -F %s [-R] [-S <diskpath>]\n%s\n\n",
1650Sstevel@tonic-gate 	    expect, got, prog, rsvp);
1660Sstevel@tonic-gate 	prom_exit_to_mon();
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * bootargs should start with "-F cprboot"
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * may set globals:
1740Sstevel@tonic-gate  *	specialstate
1750Sstevel@tonic-gate  *	reusable
1760Sstevel@tonic-gate  *	do_halt
1770Sstevel@tonic-gate  *	verbose
1780Sstevel@tonic-gate  */
1790Sstevel@tonic-gate static void
check_bootargs(void)1800Sstevel@tonic-gate check_bootargs(void)
1810Sstevel@tonic-gate {
1820Sstevel@tonic-gate 	char **argv, *str, *cp;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	argv = cb_args;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/* expect "-F" */
1870Sstevel@tonic-gate 	str = "-F";
1880Sstevel@tonic-gate 	if (*argv == NULL || prom_strcmp(*argv, str))
1890Sstevel@tonic-gate 		usage(str, *argv);
1900Sstevel@tonic-gate 	argv++;
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	/* expect "cprboot*" */
1930Sstevel@tonic-gate 	if (*argv == NULL || prom_strncmp(*argv, prog, sizeof (prog) - 1))
1940Sstevel@tonic-gate 		usage(prog, *argv);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * optional args
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	str = "-[SR]";
2000Sstevel@tonic-gate 	for (argv++; *argv; argv++) {
2010Sstevel@tonic-gate 		cp = *argv;
2020Sstevel@tonic-gate 		if (*cp != '-')
2030Sstevel@tonic-gate 			usage(str, *argv);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		switch (*++cp) {
2060Sstevel@tonic-gate 		case 'R':
2070Sstevel@tonic-gate 		case 'r':
2080Sstevel@tonic-gate 			reusable = 1;
2090Sstevel@tonic-gate 			break;
2100Sstevel@tonic-gate 		case 'S':
2110Sstevel@tonic-gate 		case 's':
2120Sstevel@tonic-gate 			if (*++argv)
2130Sstevel@tonic-gate 				specialstate = *argv;
2140Sstevel@tonic-gate 			else
2150Sstevel@tonic-gate 				usage("statefile-path", *argv);
2160Sstevel@tonic-gate 			break;
2170Sstevel@tonic-gate 		case 'h':
2180Sstevel@tonic-gate 			do_halt = 1;
2190Sstevel@tonic-gate 			break;
2200Sstevel@tonic-gate 		case 'v':
2210Sstevel@tonic-gate 			verbose = 1;
2220Sstevel@tonic-gate 			break;
2230Sstevel@tonic-gate 		default:
2240Sstevel@tonic-gate 			usage(str, *argv);
2250Sstevel@tonic-gate 			break;
2260Sstevel@tonic-gate 		}
2270Sstevel@tonic-gate 	}
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate  * reset prom props and get statefile info
2330Sstevel@tonic-gate  *
2340Sstevel@tonic-gate  * sets globals:
2350Sstevel@tonic-gate  *	cpr_filesystem
2360Sstevel@tonic-gate  *	cpr_statefile
2370Sstevel@tonic-gate  */
2380Sstevel@tonic-gate static int
cb_startup(void)2390Sstevel@tonic-gate cb_startup(void)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate 	CB_VENTRY(cb_startup);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	if (!reusable) {
2440Sstevel@tonic-gate 		/*
2450Sstevel@tonic-gate 		 * Restore the original values of the nvram properties modified
2460Sstevel@tonic-gate 		 * during suspend.  Note: if we can't get this info from the
2470Sstevel@tonic-gate 		 * defaults file, the state file may be obsolete or bad, so we
2480Sstevel@tonic-gate 		 * abort.  However, failure to restore one or more properties
2490Sstevel@tonic-gate 		 * is NOT fatal (better to continue the resume).
2500Sstevel@tonic-gate 		 */
2510Sstevel@tonic-gate 		if (cpr_reset_properties() == -1) {
2520Sstevel@tonic-gate 			prom_printf("\n%s: cannot read saved "
2530Sstevel@tonic-gate 			    "nvram info, %s\n", prog, rsvp);
2540Sstevel@tonic-gate 			return (ERR);
2550Sstevel@tonic-gate 		}
2560Sstevel@tonic-gate 	}
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate 	/*
2590Sstevel@tonic-gate 	 * simple copy if using specialstate,
2600Sstevel@tonic-gate 	 * otherwise read in fs and statefile from a config file
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	if (specialstate)
2630Sstevel@tonic-gate 		(void) prom_strcpy(cpr_statefile, specialstate);
2640Sstevel@tonic-gate 	else if (cpr_locate_statefile(cpr_statefile, cpr_filesystem) == -1) {
2650Sstevel@tonic-gate 		prom_printf("\n%s: cannot find cpr statefile, %s\n",
2660Sstevel@tonic-gate 		    prog, rsvp);
2670Sstevel@tonic-gate 		return (ERR);
2680Sstevel@tonic-gate 	}
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	return (0);
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate 
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate static int
cb_open_sf(void)2750Sstevel@tonic-gate cb_open_sf(void)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	CB_VENTRY(cb_open_sf);
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	sfile.fd = cpr_statefile_open(cpr_statefile, cpr_filesystem);
2800Sstevel@tonic-gate 	if (sfile.fd == -1) {
2810Sstevel@tonic-gate 		prom_printf("\n%s: can't open %s", prog, cpr_statefile);
2820Sstevel@tonic-gate 		if (specialstate)
2830Sstevel@tonic-gate 			prom_printf(" on %s", cpr_filesystem);
2840Sstevel@tonic-gate 		prom_printf("\n%s\n", rsvp);
2850Sstevel@tonic-gate 		return (ERR);
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	/*
2890Sstevel@tonic-gate 	 * for block devices, seek past the disk label and bootblock
2900Sstevel@tonic-gate 	 */
2916423Sgw25295 	if (volname)
2926423Sgw25295 		(void) cpr_fs_seek(sfile.fd, CPR_SPEC_OFFSET);
2936423Sgw25295 	else if (specialstate)
2940Sstevel@tonic-gate 		(void) prom_seek(sfile.fd, CPR_SPEC_OFFSET);
2950Sstevel@tonic-gate 	return (0);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static int
cb_close_sf(void)3000Sstevel@tonic-gate cb_close_sf(void)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	CB_VENTRY(cb_close_sf);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * close the device so the prom will free up 20+ pages
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	(void) cpr_statefile_close(sfile.fd);
3080Sstevel@tonic-gate 	return (0);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * to restore kernel pages, we have to open a prom device to read-in
3140Sstevel@tonic-gate  * the statefile contents; a prom "open" request triggers the driver
3150Sstevel@tonic-gate  * and various packages to allocate 20+ pages; unfortunately, some or
3160Sstevel@tonic-gate  * all of those pages always clash with kernel pages, and we cant write
3170Sstevel@tonic-gate  * to them without corrupting the prom.
3180Sstevel@tonic-gate  *
3190Sstevel@tonic-gate  * to solve that problem, the only real solution is to close the device
3200Sstevel@tonic-gate  * to free up those pages; this means we need to open, read-in the entire
3210Sstevel@tonic-gate  * statefile, and close; and to store the statefile, we need to allocate
3220Sstevel@tonic-gate  * plenty of space, usually around 2 to 60 MB.
3230Sstevel@tonic-gate  *
3240Sstevel@tonic-gate  * the simplest alloc means is prom_alloc(), which will "claim" both
3250Sstevel@tonic-gate  * virt and phys pages, and creates mappings with a "map" request;
3260Sstevel@tonic-gate  * "map" also causes the prom to alloc pages, and again these clash
3270Sstevel@tonic-gate  * with kernel pages...
3280Sstevel@tonic-gate  *
3290Sstevel@tonic-gate  * to solve the "map" problem, we just reserve virt and phys pages and
3300Sstevel@tonic-gate  * manage the translations by creating our own tlb entries instead of
3310Sstevel@tonic-gate  * relying on the prom.
3320Sstevel@tonic-gate  *
3330Sstevel@tonic-gate  * sets globals:
3340Sstevel@tonic-gate  *	cpr_test_mode
3350Sstevel@tonic-gate  *	sfile.kpages
3360Sstevel@tonic-gate  *	sfile.size
3370Sstevel@tonic-gate  * 	sfile.buf
3380Sstevel@tonic-gate  * 	sfile.low_ppn
3390Sstevel@tonic-gate  * 	sfile.high_ppn
3400Sstevel@tonic-gate  */
3410Sstevel@tonic-gate static int
cb_read_statefile(void)3420Sstevel@tonic-gate cb_read_statefile(void)
3430Sstevel@tonic-gate {
3440Sstevel@tonic-gate 	size_t alsize, len, resid;
3450Sstevel@tonic-gate 	physaddr_t phys, dst_phys;
3460Sstevel@tonic-gate 	char *str, *dst_virt;
3470Sstevel@tonic-gate 	int err, cnt, mmask;
3480Sstevel@tonic-gate 	uint_t dtlb_index;
3490Sstevel@tonic-gate 	ssize_t nread;
3500Sstevel@tonic-gate 	cdd_t cdump;
3510Sstevel@tonic-gate 
3520Sstevel@tonic-gate 	str = "cb_read_statefile";
3530Sstevel@tonic-gate 	CB_VPRINTF((ent_fmt, str, entry));
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	/*
3560Sstevel@tonic-gate 	 * read-in and check cpr dump header
3570Sstevel@tonic-gate 	 */
3586423Sgw25295 	if (cpr_read_cdump(sfile.fd, &cdump, CPR_MACHTYPE_4U) == -1)
3590Sstevel@tonic-gate 		return (ERR);
3606423Sgw25295 
3610Sstevel@tonic-gate 	if (cpr_debug)
3620Sstevel@tonic-gate 		prom_printf("\n");
3630Sstevel@tonic-gate 	cb_nbitmaps = cdump.cdd_bitmaprec;
3640Sstevel@tonic-gate 	cpr_test_mode = cdump.cdd_test_mode;
3650Sstevel@tonic-gate 	sfile.kpages = cdump.cdd_dumppgsize;
3663446Smrj 	CPR_DEBUG(CPR_DEBUG4, "%s: total kpages %d\n", prog, sfile.kpages);
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate 	/*
3690Sstevel@tonic-gate 	 * alloc virt and phys space with 512K alignment;
3700Sstevel@tonic-gate 	 * alloc size should be (n * tte size);
3710Sstevel@tonic-gate 	 */
3720Sstevel@tonic-gate 	sfile.size = PAGE_ROUNDUP(cdump.cdd_filesize);
3730Sstevel@tonic-gate 	alsize = (cdump.cdd_filesize + MMU_PAGEOFFSET512K) &
3740Sstevel@tonic-gate 	    MMU_PAGEMASK512K;
3750Sstevel@tonic-gate 	phys = 0;
3760Sstevel@tonic-gate 	err = cb_alloc(alsize, MMU_PAGESIZE512K, &sfile.buf, &phys);
3770Sstevel@tonic-gate 	CB_VPRINTF(("%s:\n    alloc size 0x%lx, buf size 0x%lx\n"
378785Seota 	    "    virt 0x%p, phys 0x%llx\n",
379*7632SNick.Todd@Sun.COM 	    str, alsize, sfile.size, (void *)sfile.buf, phys));
3800Sstevel@tonic-gate 	if (err) {
3810Sstevel@tonic-gate 		prom_printf("%s: cant alloc statefile buf, size 0x%lx\n%s\n",
3820Sstevel@tonic-gate 		    str, sfile.size, rsvp);
3830Sstevel@tonic-gate 		return (ERR);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * record low and high phys page numbers for sfile.buf
3880Sstevel@tonic-gate 	 */
3890Sstevel@tonic-gate 	sfile.low_ppn = ADDR_TO_PN(phys);
3900Sstevel@tonic-gate 	sfile.high_ppn = sfile.low_ppn + mmu_btop(sfile.size) - 1;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	/*
3930Sstevel@tonic-gate 	 * setup destination virt and phys addrs for reads;
3940Sstevel@tonic-gate 	 * mapin-mask tells when to create a new tlb entry for the
3950Sstevel@tonic-gate 	 * next set of reads;  NB: the read and tlb method needs
3960Sstevel@tonic-gate 	 * ((big-pagesize % read-size) == 0)
3970Sstevel@tonic-gate 	 */
3980Sstevel@tonic-gate 	dst_phys = phys;
3990Sstevel@tonic-gate 	mmask = (MMU_PAGESIZE512K / PROM_MAX_READ) - 1;
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate 	cnt = 0;
4020Sstevel@tonic-gate 	dtlb_index = cb_dents - 1;
4036423Sgw25295 	if (volname)
4046423Sgw25295 		(void) cpr_fs_seek(sfile.fd, CPR_SPEC_OFFSET);
4056423Sgw25295 	else if (specialstate)
4065648Ssetje 		(void) prom_seek(sfile.fd, CPR_SPEC_OFFSET);
4075648Ssetje 	else
4085648Ssetje 		(void) cpr_fs_seek(sfile.fd, 0);
4093446Smrj 	CPR_DEBUG(CPR_DEBUG1, "%s: reading statefile... ", prog);
4100Sstevel@tonic-gate 	for (resid = cdump.cdd_filesize; resid; resid -= len) {
4110Sstevel@tonic-gate 		/*
4120Sstevel@tonic-gate 		 * do a full spin (4 spin chars)
4130Sstevel@tonic-gate 		 * for every MB read (8 reads = 256K)
4140Sstevel@tonic-gate 		 */
4150Sstevel@tonic-gate 		if ((cnt & 0x7) == 0)
4160Sstevel@tonic-gate 			cb_spin();
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 		/*
4190Sstevel@tonic-gate 		 * map-in statefile buf pages in 512K blocks;
4200Sstevel@tonic-gate 		 * see MMU_PAGESIZE512K above
4210Sstevel@tonic-gate 		 */
4220Sstevel@tonic-gate 		if ((cnt & mmask) == 0) {
4230Sstevel@tonic-gate 			dst_virt = sfile.buf;
4240Sstevel@tonic-gate 			cb_mapin(dst_virt, ADDR_TO_PN(dst_phys),
4250Sstevel@tonic-gate 			    TTE512K, TTE_HWWR_INT, dtlb_index);
4260Sstevel@tonic-gate 		}
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		cnt++;
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		len = min(PROM_MAX_READ, resid);
4315648Ssetje 		nread = cpr_read(sfile.fd, dst_virt, len);
4320Sstevel@tonic-gate 		if (nread != (ssize_t)len) {
4330Sstevel@tonic-gate 			prom_printf("\n%s: prom read error, "
4340Sstevel@tonic-gate 			    "expect %ld, got %ld\n", str, len, nread);
4350Sstevel@tonic-gate 			return (ERR);
4360Sstevel@tonic-gate 		}
4370Sstevel@tonic-gate 		dst_virt += len;
4380Sstevel@tonic-gate 		dst_phys += len;
4390Sstevel@tonic-gate 	}
4403446Smrj 	CPR_DEBUG(CPR_DEBUG1, " \b\n");
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * free up any unused phys pages trailing the statefile buffer;
4440Sstevel@tonic-gate 	 * these pages will later appear on the physavail list
4450Sstevel@tonic-gate 	 */
4460Sstevel@tonic-gate 	if (alsize > sfile.size) {
4470Sstevel@tonic-gate 		len = alsize - sfile.size;
4480Sstevel@tonic-gate 		prom_free_phys(len, phys + sfile.size);
449*7632SNick.Todd@Sun.COM 		CB_VPRINTF(("%s: freed %ld phys pages (0x%llx - 0x%llx)\n",
450*7632SNick.Todd@Sun.COM 		    str, mmu_btop(len),
451*7632SNick.Todd@Sun.COM 		    (unsigned long long)(phys + sfile.size),
452*7632SNick.Todd@Sun.COM 		    (unsigned long long)(phys + alsize)));
4530Sstevel@tonic-gate 	}
4540Sstevel@tonic-gate 
4550Sstevel@tonic-gate 	/*
4560Sstevel@tonic-gate 	 * start the statefile buffer offset at the base of
4570Sstevel@tonic-gate 	 * the statefile buffer and skip past the dump header
4580Sstevel@tonic-gate 	 */
4590Sstevel@tonic-gate 	sfile.buf_offset = 0;
4600Sstevel@tonic-gate 	SF_ADV(sizeof (cdump));
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * finish with the first block mapped-in to provide easy virt access
4640Sstevel@tonic-gate 	 * to machdep structs and the bitmap; for 2.8, the combined size of
4650Sstevel@tonic-gate 	 * (cdd_t + cmd_t + csu_md_t + prom_words + cbd_t) is about 1K,
4660Sstevel@tonic-gate 	 * leaving room for a bitmap representing nearly 32GB
4670Sstevel@tonic-gate 	 */
4680Sstevel@tonic-gate 	cb_mapin(sfile.buf, sfile.low_ppn,
4690Sstevel@tonic-gate 	    TTE512K, TTE_HWWR_INT, dtlb_index);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	return (0);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * cprboot first stage worklist
4770Sstevel@tonic-gate  */
4780Sstevel@tonic-gate static int (*first_worklist[])(void) = {
4790Sstevel@tonic-gate 	cb_intro,
4805648Ssetje 	cb_mountroot,
4810Sstevel@tonic-gate 	cb_startup,
4820Sstevel@tonic-gate 	cb_get_props,
4830Sstevel@tonic-gate 	cb_usb_setup,
4845648Ssetje 	cb_unmountroot,
4850Sstevel@tonic-gate 	cb_open_sf,
4860Sstevel@tonic-gate 	cb_read_statefile,
4870Sstevel@tonic-gate 	cb_close_sf,
4880Sstevel@tonic-gate 	cb_check_machdep,
4890Sstevel@tonic-gate 	cb_interpret,
4900Sstevel@tonic-gate 	cb_get_physavail,
4910Sstevel@tonic-gate 	cb_set_bitmap,
4920Sstevel@tonic-gate 	cb_get_newstack,
4930Sstevel@tonic-gate 	NULL
4940Sstevel@tonic-gate };
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate /*
4970Sstevel@tonic-gate  * cprboot second stage worklist
4980Sstevel@tonic-gate  */
4990Sstevel@tonic-gate static int (*second_worklist[])(void) = {
5000Sstevel@tonic-gate 	cb_relocate,
5010Sstevel@tonic-gate 	cb_tracking_setup,
5020Sstevel@tonic-gate 	cb_restore_kpages,
5030Sstevel@tonic-gate 	cb_terminator,
5040Sstevel@tonic-gate 	cb_ksetup,
5050Sstevel@tonic-gate 	cb_mpsetup,
5060Sstevel@tonic-gate 	NULL
5070Sstevel@tonic-gate };
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate /*
5110Sstevel@tonic-gate  * simple loop driving major cprboot operations;
5120Sstevel@tonic-gate  * exits to prom if any error is returned
5130Sstevel@tonic-gate  */
5140Sstevel@tonic-gate static void
cb_drive(int (** worklist)(void))5150Sstevel@tonic-gate cb_drive(int (**worklist)(void))
5160Sstevel@tonic-gate {
5170Sstevel@tonic-gate 	int i;
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	for (i = 0; worklist[i] != NULL; i++) {
5200Sstevel@tonic-gate 		if (worklist[i]())
5210Sstevel@tonic-gate 			cb_exit_to_mon();
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate }
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate /*
5270Sstevel@tonic-gate  * debugging support: drop to prom if do_halt is set
5280Sstevel@tonic-gate  */
5290Sstevel@tonic-gate static void
check_halt(char * str)5300Sstevel@tonic-gate check_halt(char *str)
5310Sstevel@tonic-gate {
5320Sstevel@tonic-gate 	if (do_halt) {
5330Sstevel@tonic-gate 		prom_printf("\n%s halted by -h flag\n==> before %s\n\n",
5340Sstevel@tonic-gate 		    prog, str);
5350Sstevel@tonic-gate 		cb_enter_mon();
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate 
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate /*
5410Sstevel@tonic-gate  * main is called twice from "cb_srt0.s", args are:
5420Sstevel@tonic-gate  *	cookie	  ieee1275 cif handle
5430Sstevel@tonic-gate  *	first	  (true): first stage, (false): second stage
5440Sstevel@tonic-gate  *
5450Sstevel@tonic-gate  * first stage summary:
5460Sstevel@tonic-gate  *	various setup
5470Sstevel@tonic-gate  *	allocate a big statefile buffer
5480Sstevel@tonic-gate  *	read in the statefile
5490Sstevel@tonic-gate  *	setup the bitmap
5500Sstevel@tonic-gate  *	create a new stack
5510Sstevel@tonic-gate  *
5520Sstevel@tonic-gate  * return to "cb_srt0.s", switch to new stack
5530Sstevel@tonic-gate  *
5540Sstevel@tonic-gate  * second stage summary:
5550Sstevel@tonic-gate  *	relocate cprboot phys pages
5560Sstevel@tonic-gate  *	setup tracking for statefile buffer pages
5570Sstevel@tonic-gate  *	restore kernel pages
5580Sstevel@tonic-gate  *	various cleanup
5590Sstevel@tonic-gate  *	install tlb entries for the nucleus and cpr module
5600Sstevel@tonic-gate  *	restore registers and jump into cpr module
5610Sstevel@tonic-gate  */
5620Sstevel@tonic-gate int
main(void * cookie,int first)5630Sstevel@tonic-gate main(void *cookie, int first)
5640Sstevel@tonic-gate {
5650Sstevel@tonic-gate 	if (first) {
5660Sstevel@tonic-gate 		prom_init(prog, cookie);
5670Sstevel@tonic-gate 		cb_msec = prom_gettime();
5680Sstevel@tonic-gate 		get_bootargs();
5690Sstevel@tonic-gate 		check_bootargs();
5700Sstevel@tonic-gate 		check_halt("first_worklist");
5710Sstevel@tonic-gate 		cb_drive(first_worklist);
5720Sstevel@tonic-gate 		return (0);
5730Sstevel@tonic-gate 	} else {
5740Sstevel@tonic-gate 		cb_drive(second_worklist);
5750Sstevel@tonic-gate 		if (verbose || CPR_DBG(1)) {
5760Sstevel@tonic-gate 			prom_printf("%s: milliseconds %d\n",
5770Sstevel@tonic-gate 			    prog, prom_gettime() - cb_msec);
578785Seota 			prom_printf("%s: resume pc 0x%lx\n",
579785Seota 			    prog, mdinfo.func);
5800Sstevel@tonic-gate 			prom_printf("%s: exit_to_kernel(0x%p, 0x%p)\n\n",
581*7632SNick.Todd@Sun.COM 			    prog, cookie, (void *)&mdinfo);
5820Sstevel@tonic-gate 		}
5830Sstevel@tonic-gate 		check_halt("exit_to_kernel");
5840Sstevel@tonic-gate 		exit_to_kernel(cookie, &mdinfo);
5850Sstevel@tonic-gate 		return (ERR);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate }
588