xref: /titanic_52/usr/src/psm/stand/cpr/sparcv9/sun4u/cprboot.c (revision 8793b36b40d14ad0a0fecc97738dc118a928f46c)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ae115bc7Smrj  * Common Development and Distribution License (the "License").
6ae115bc7Smrj  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22e7cbe64fSgw25295  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * cprboot - prom client that restores kadb/kernel pages
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * simple cprboot overview:
307c478bd9Sstevel@tonic-gate  *	reset boot-file/boot-device to their original values
317c478bd9Sstevel@tonic-gate  * 	open cpr statefile, usually "/.CPR"
327c478bd9Sstevel@tonic-gate  *	read in statefile
337c478bd9Sstevel@tonic-gate  *	close statefile
347c478bd9Sstevel@tonic-gate  *	restore kernel pages
357c478bd9Sstevel@tonic-gate  *	jump back into kernel text
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  * cprboot supports a restartable statefile for FAA/STARS,
397c478bd9Sstevel@tonic-gate  * Federal Aviation Administration
407c478bd9Sstevel@tonic-gate  * Standard Terminal Automation Replacement System
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/cpr.h>
457c478bd9Sstevel@tonic-gate #include <sys/promimpl.h>
467c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
477c478bd9Sstevel@tonic-gate #include "cprboot.h"
487c478bd9Sstevel@tonic-gate 
49e7cbe64fSgw25295 char *volname = NULL;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * local defs
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate #define	CB_MAXPROP	256
557c478bd9Sstevel@tonic-gate #define	CB_MAXARGS	8
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * globals
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate struct statefile sfile;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate char cpr_statefile[OBP_MAXPATHLEN];
647c478bd9Sstevel@tonic-gate char cpr_filesystem[OBP_MAXPATHLEN];
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate int cpr_debug;				/* cpr debug, set with uadmin 3 10x */
677c478bd9Sstevel@tonic-gate uint_t cb_msec;				/* cprboot start runtime */
687c478bd9Sstevel@tonic-gate uint_t cb_dents;			/* number of dtlb entries */
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate int do_halt = 0;			/* halt (enter mon) after load */
717c478bd9Sstevel@tonic-gate int verbose = 0;			/* verbose, traces cprboot ops */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate char rsvp[] = "please reboot";
747c478bd9Sstevel@tonic-gate char prog[] = "cprboot";
757c478bd9Sstevel@tonic-gate char entry[] = "ENTRY";
767c478bd9Sstevel@tonic-gate char ent_fmt[] = "\n%s %s\n";
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate  * file scope
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate static char cb_argbuf[CB_MAXPROP];
837c478bd9Sstevel@tonic-gate static char *cb_args[CB_MAXARGS];
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static int reusable;
86986fd29aSsetje char *specialstate;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static int
907c478bd9Sstevel@tonic-gate cb_intro(void)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	static char cstr[] = "\014" "\033[1P" "\033[18;21H";
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_intro);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * build/debug aid; this condition should not occur
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	if ((uintptr_t)_end > CB_SRC_VIRT) {
1007c478bd9Sstevel@tonic-gate 		prom_printf("\ndata collision:\n"
10153391bafSeota 		    "(_end=0x%p > CB_LOW_VIRT=0x%x), recompile...\n",
102*8793b36bSNick Todd 		    (void *)_end, CB_SRC_VIRT);
1037c478bd9Sstevel@tonic-gate 		return (ERR);
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	/* clear console */
1077c478bd9Sstevel@tonic-gate 	prom_printf(cstr);
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	prom_printf("Restoring the System. Please Wait... ");
1107c478bd9Sstevel@tonic-gate 	return (0);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * read bootargs and convert to arg vector
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  * sets globals:
1187c478bd9Sstevel@tonic-gate  *	cb_argbuf
1197c478bd9Sstevel@tonic-gate  *	cb_args
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate static void
1227c478bd9Sstevel@tonic-gate get_bootargs(void)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	char *cp, *tail, *argp, **argv;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	CB_VENTRY(get_bootargs);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) prom_strcpy(cb_argbuf, prom_bootargs());
1297c478bd9Sstevel@tonic-gate 	tail = cb_argbuf + prom_strlen(cb_argbuf);
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * scan to the trailing NULL so the last arg
1337c478bd9Sstevel@tonic-gate 	 * will be found without any special-case code
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	argv = cb_args;
1367c478bd9Sstevel@tonic-gate 	for (cp = argp = cb_argbuf; cp <= tail; cp++) {
1377c478bd9Sstevel@tonic-gate 		if (prom_strchr(" \t\n\r", *cp) == NULL)
1387c478bd9Sstevel@tonic-gate 			continue;
1397c478bd9Sstevel@tonic-gate 		*cp = '\0';
1407c478bd9Sstevel@tonic-gate 		if (cp - argp) {
1417c478bd9Sstevel@tonic-gate 			*argv++ = argp;
1427c478bd9Sstevel@tonic-gate 			if ((argv - cb_args) == (CB_MAXARGS - 1))
1437c478bd9Sstevel@tonic-gate 				break;
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 		argp = cp + 1;
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	*argv = NULLP;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (verbose) {
1507c478bd9Sstevel@tonic-gate 		for (argv = cb_args; *argv; argv++) {
151*8793b36bSNick Todd 			prom_printf("    %d: \"%s\"\n",
152*8793b36bSNick Todd 			    (int)(argv - cb_args), *argv);
1537c478bd9Sstevel@tonic-gate 		}
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static void
1597c478bd9Sstevel@tonic-gate usage(char *expect, char *got)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	if (got == NULL)
1627c478bd9Sstevel@tonic-gate 		got = "(NULL)";
1637c478bd9Sstevel@tonic-gate 	prom_printf("\nbad OBP boot args: expect %s, got %s\n"
1647c478bd9Sstevel@tonic-gate 	    "Usage: boot -F %s [-R] [-S <diskpath>]\n%s\n\n",
1657c478bd9Sstevel@tonic-gate 	    expect, got, prog, rsvp);
1667c478bd9Sstevel@tonic-gate 	prom_exit_to_mon();
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * bootargs should start with "-F cprboot"
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * may set globals:
1747c478bd9Sstevel@tonic-gate  *	specialstate
1757c478bd9Sstevel@tonic-gate  *	reusable
1767c478bd9Sstevel@tonic-gate  *	do_halt
1777c478bd9Sstevel@tonic-gate  *	verbose
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate static void
1807c478bd9Sstevel@tonic-gate check_bootargs(void)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	char **argv, *str, *cp;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	argv = cb_args;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/* expect "-F" */
1877c478bd9Sstevel@tonic-gate 	str = "-F";
1887c478bd9Sstevel@tonic-gate 	if (*argv == NULL || prom_strcmp(*argv, str))
1897c478bd9Sstevel@tonic-gate 		usage(str, *argv);
1907c478bd9Sstevel@tonic-gate 	argv++;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/* expect "cprboot*" */
1937c478bd9Sstevel@tonic-gate 	if (*argv == NULL || prom_strncmp(*argv, prog, sizeof (prog) - 1))
1947c478bd9Sstevel@tonic-gate 		usage(prog, *argv);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * optional args
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	str = "-[SR]";
2007c478bd9Sstevel@tonic-gate 	for (argv++; *argv; argv++) {
2017c478bd9Sstevel@tonic-gate 		cp = *argv;
2027c478bd9Sstevel@tonic-gate 		if (*cp != '-')
2037c478bd9Sstevel@tonic-gate 			usage(str, *argv);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 		switch (*++cp) {
2067c478bd9Sstevel@tonic-gate 		case 'R':
2077c478bd9Sstevel@tonic-gate 		case 'r':
2087c478bd9Sstevel@tonic-gate 			reusable = 1;
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 		case 'S':
2117c478bd9Sstevel@tonic-gate 		case 's':
2127c478bd9Sstevel@tonic-gate 			if (*++argv)
2137c478bd9Sstevel@tonic-gate 				specialstate = *argv;
2147c478bd9Sstevel@tonic-gate 			else
2157c478bd9Sstevel@tonic-gate 				usage("statefile-path", *argv);
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 		case 'h':
2187c478bd9Sstevel@tonic-gate 			do_halt = 1;
2197c478bd9Sstevel@tonic-gate 			break;
2207c478bd9Sstevel@tonic-gate 		case 'v':
2217c478bd9Sstevel@tonic-gate 			verbose = 1;
2227c478bd9Sstevel@tonic-gate 			break;
2237c478bd9Sstevel@tonic-gate 		default:
2247c478bd9Sstevel@tonic-gate 			usage(str, *argv);
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * reset prom props and get statefile info
2337c478bd9Sstevel@tonic-gate  *
2347c478bd9Sstevel@tonic-gate  * sets globals:
2357c478bd9Sstevel@tonic-gate  *	cpr_filesystem
2367c478bd9Sstevel@tonic-gate  *	cpr_statefile
2377c478bd9Sstevel@tonic-gate  */
2387c478bd9Sstevel@tonic-gate static int
2397c478bd9Sstevel@tonic-gate cb_startup(void)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_startup);
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if (!reusable) {
2447c478bd9Sstevel@tonic-gate 		/*
2457c478bd9Sstevel@tonic-gate 		 * Restore the original values of the nvram properties modified
2467c478bd9Sstevel@tonic-gate 		 * during suspend.  Note: if we can't get this info from the
2477c478bd9Sstevel@tonic-gate 		 * defaults file, the state file may be obsolete or bad, so we
2487c478bd9Sstevel@tonic-gate 		 * abort.  However, failure to restore one or more properties
2497c478bd9Sstevel@tonic-gate 		 * is NOT fatal (better to continue the resume).
2507c478bd9Sstevel@tonic-gate 		 */
2517c478bd9Sstevel@tonic-gate 		if (cpr_reset_properties() == -1) {
2527c478bd9Sstevel@tonic-gate 			prom_printf("\n%s: cannot read saved "
2537c478bd9Sstevel@tonic-gate 			    "nvram info, %s\n", prog, rsvp);
2547c478bd9Sstevel@tonic-gate 			return (ERR);
2557c478bd9Sstevel@tonic-gate 		}
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	/*
2597c478bd9Sstevel@tonic-gate 	 * simple copy if using specialstate,
2607c478bd9Sstevel@tonic-gate 	 * otherwise read in fs and statefile from a config file
2617c478bd9Sstevel@tonic-gate 	 */
2627c478bd9Sstevel@tonic-gate 	if (specialstate)
2637c478bd9Sstevel@tonic-gate 		(void) prom_strcpy(cpr_statefile, specialstate);
2647c478bd9Sstevel@tonic-gate 	else if (cpr_locate_statefile(cpr_statefile, cpr_filesystem) == -1) {
2657c478bd9Sstevel@tonic-gate 		prom_printf("\n%s: cannot find cpr statefile, %s\n",
2667c478bd9Sstevel@tonic-gate 		    prog, rsvp);
2677c478bd9Sstevel@tonic-gate 		return (ERR);
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	return (0);
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate static int
2757c478bd9Sstevel@tonic-gate cb_open_sf(void)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_open_sf);
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	sfile.fd = cpr_statefile_open(cpr_statefile, cpr_filesystem);
2807c478bd9Sstevel@tonic-gate 	if (sfile.fd == -1) {
2817c478bd9Sstevel@tonic-gate 		prom_printf("\n%s: can't open %s", prog, cpr_statefile);
2827c478bd9Sstevel@tonic-gate 		if (specialstate)
2837c478bd9Sstevel@tonic-gate 			prom_printf(" on %s", cpr_filesystem);
2847c478bd9Sstevel@tonic-gate 		prom_printf("\n%s\n", rsvp);
2857c478bd9Sstevel@tonic-gate 		return (ERR);
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	/*
2897c478bd9Sstevel@tonic-gate 	 * for block devices, seek past the disk label and bootblock
2907c478bd9Sstevel@tonic-gate 	 */
291e7cbe64fSgw25295 	if (volname)
292e7cbe64fSgw25295 		(void) cpr_fs_seek(sfile.fd, CPR_SPEC_OFFSET);
293e7cbe64fSgw25295 	else if (specialstate)
2947c478bd9Sstevel@tonic-gate 		(void) prom_seek(sfile.fd, CPR_SPEC_OFFSET);
2957c478bd9Sstevel@tonic-gate 	return (0);
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate static int
3007c478bd9Sstevel@tonic-gate cb_close_sf(void)
3017c478bd9Sstevel@tonic-gate {
3027c478bd9Sstevel@tonic-gate 	CB_VENTRY(cb_close_sf);
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * close the device so the prom will free up 20+ pages
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	(void) cpr_statefile_close(sfile.fd);
3087c478bd9Sstevel@tonic-gate 	return (0);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * to restore kernel pages, we have to open a prom device to read-in
3147c478bd9Sstevel@tonic-gate  * the statefile contents; a prom "open" request triggers the driver
3157c478bd9Sstevel@tonic-gate  * and various packages to allocate 20+ pages; unfortunately, some or
3167c478bd9Sstevel@tonic-gate  * all of those pages always clash with kernel pages, and we cant write
3177c478bd9Sstevel@tonic-gate  * to them without corrupting the prom.
3187c478bd9Sstevel@tonic-gate  *
3197c478bd9Sstevel@tonic-gate  * to solve that problem, the only real solution is to close the device
3207c478bd9Sstevel@tonic-gate  * to free up those pages; this means we need to open, read-in the entire
3217c478bd9Sstevel@tonic-gate  * statefile, and close; and to store the statefile, we need to allocate
3227c478bd9Sstevel@tonic-gate  * plenty of space, usually around 2 to 60 MB.
3237c478bd9Sstevel@tonic-gate  *
3247c478bd9Sstevel@tonic-gate  * the simplest alloc means is prom_alloc(), which will "claim" both
3257c478bd9Sstevel@tonic-gate  * virt and phys pages, and creates mappings with a "map" request;
3267c478bd9Sstevel@tonic-gate  * "map" also causes the prom to alloc pages, and again these clash
3277c478bd9Sstevel@tonic-gate  * with kernel pages...
3287c478bd9Sstevel@tonic-gate  *
3297c478bd9Sstevel@tonic-gate  * to solve the "map" problem, we just reserve virt and phys pages and
3307c478bd9Sstevel@tonic-gate  * manage the translations by creating our own tlb entries instead of
3317c478bd9Sstevel@tonic-gate  * relying on the prom.
3327c478bd9Sstevel@tonic-gate  *
3337c478bd9Sstevel@tonic-gate  * sets globals:
3347c478bd9Sstevel@tonic-gate  *	cpr_test_mode
3357c478bd9Sstevel@tonic-gate  *	sfile.kpages
3367c478bd9Sstevel@tonic-gate  *	sfile.size
3377c478bd9Sstevel@tonic-gate  * 	sfile.buf
3387c478bd9Sstevel@tonic-gate  * 	sfile.low_ppn
3397c478bd9Sstevel@tonic-gate  * 	sfile.high_ppn
3407c478bd9Sstevel@tonic-gate  */
3417c478bd9Sstevel@tonic-gate static int
3427c478bd9Sstevel@tonic-gate cb_read_statefile(void)
3437c478bd9Sstevel@tonic-gate {
3447c478bd9Sstevel@tonic-gate 	size_t alsize, len, resid;
3457c478bd9Sstevel@tonic-gate 	physaddr_t phys, dst_phys;
3467c478bd9Sstevel@tonic-gate 	char *str, *dst_virt;
3477c478bd9Sstevel@tonic-gate 	int err, cnt, mmask;
3487c478bd9Sstevel@tonic-gate 	uint_t dtlb_index;
3497c478bd9Sstevel@tonic-gate 	ssize_t nread;
3507c478bd9Sstevel@tonic-gate 	cdd_t cdump;
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	str = "cb_read_statefile";
3537c478bd9Sstevel@tonic-gate 	CB_VPRINTF((ent_fmt, str, entry));
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/*
3567c478bd9Sstevel@tonic-gate 	 * read-in and check cpr dump header
3577c478bd9Sstevel@tonic-gate 	 */
358e7cbe64fSgw25295 	if (cpr_read_cdump(sfile.fd, &cdump, CPR_MACHTYPE_4U) == -1)
3597c478bd9Sstevel@tonic-gate 		return (ERR);
360e7cbe64fSgw25295 
3617c478bd9Sstevel@tonic-gate 	if (cpr_debug)
3627c478bd9Sstevel@tonic-gate 		prom_printf("\n");
3637c478bd9Sstevel@tonic-gate 	cb_nbitmaps = cdump.cdd_bitmaprec;
3647c478bd9Sstevel@tonic-gate 	cpr_test_mode = cdump.cdd_test_mode;
3657c478bd9Sstevel@tonic-gate 	sfile.kpages = cdump.cdd_dumppgsize;
366ae115bc7Smrj 	CPR_DEBUG(CPR_DEBUG4, "%s: total kpages %d\n", prog, sfile.kpages);
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	/*
3697c478bd9Sstevel@tonic-gate 	 * alloc virt and phys space with 512K alignment;
3707c478bd9Sstevel@tonic-gate 	 * alloc size should be (n * tte size);
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	sfile.size = PAGE_ROUNDUP(cdump.cdd_filesize);
3737c478bd9Sstevel@tonic-gate 	alsize = (cdump.cdd_filesize + MMU_PAGEOFFSET512K) &
3747c478bd9Sstevel@tonic-gate 	    MMU_PAGEMASK512K;
3757c478bd9Sstevel@tonic-gate 	phys = 0;
3767c478bd9Sstevel@tonic-gate 	err = cb_alloc(alsize, MMU_PAGESIZE512K, &sfile.buf, &phys);
3777c478bd9Sstevel@tonic-gate 	CB_VPRINTF(("%s:\n    alloc size 0x%lx, buf size 0x%lx\n"
37853391bafSeota 	    "    virt 0x%p, phys 0x%llx\n",
379*8793b36bSNick Todd 	    str, alsize, sfile.size, (void *)sfile.buf, phys));
3807c478bd9Sstevel@tonic-gate 	if (err) {
3817c478bd9Sstevel@tonic-gate 		prom_printf("%s: cant alloc statefile buf, size 0x%lx\n%s\n",
3827c478bd9Sstevel@tonic-gate 		    str, sfile.size, rsvp);
3837c478bd9Sstevel@tonic-gate 		return (ERR);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	/*
3877c478bd9Sstevel@tonic-gate 	 * record low and high phys page numbers for sfile.buf
3887c478bd9Sstevel@tonic-gate 	 */
3897c478bd9Sstevel@tonic-gate 	sfile.low_ppn = ADDR_TO_PN(phys);
3907c478bd9Sstevel@tonic-gate 	sfile.high_ppn = sfile.low_ppn + mmu_btop(sfile.size) - 1;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	/*
3937c478bd9Sstevel@tonic-gate 	 * setup destination virt and phys addrs for reads;
3947c478bd9Sstevel@tonic-gate 	 * mapin-mask tells when to create a new tlb entry for the
3957c478bd9Sstevel@tonic-gate 	 * next set of reads;  NB: the read and tlb method needs
3967c478bd9Sstevel@tonic-gate 	 * ((big-pagesize % read-size) == 0)
3977c478bd9Sstevel@tonic-gate 	 */
3987c478bd9Sstevel@tonic-gate 	dst_phys = phys;
3997c478bd9Sstevel@tonic-gate 	mmask = (MMU_PAGESIZE512K / PROM_MAX_READ) - 1;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	cnt = 0;
4027c478bd9Sstevel@tonic-gate 	dtlb_index = cb_dents - 1;
403e7cbe64fSgw25295 	if (volname)
404e7cbe64fSgw25295 		(void) cpr_fs_seek(sfile.fd, CPR_SPEC_OFFSET);
405e7cbe64fSgw25295 	else if (specialstate)
406986fd29aSsetje 		(void) prom_seek(sfile.fd, CPR_SPEC_OFFSET);
407986fd29aSsetje 	else
408986fd29aSsetje 		(void) cpr_fs_seek(sfile.fd, 0);
409ae115bc7Smrj 	CPR_DEBUG(CPR_DEBUG1, "%s: reading statefile... ", prog);
4107c478bd9Sstevel@tonic-gate 	for (resid = cdump.cdd_filesize; resid; resid -= len) {
4117c478bd9Sstevel@tonic-gate 		/*
4127c478bd9Sstevel@tonic-gate 		 * do a full spin (4 spin chars)
4137c478bd9Sstevel@tonic-gate 		 * for every MB read (8 reads = 256K)
4147c478bd9Sstevel@tonic-gate 		 */
4157c478bd9Sstevel@tonic-gate 		if ((cnt & 0x7) == 0)
4167c478bd9Sstevel@tonic-gate 			cb_spin();
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 		/*
4197c478bd9Sstevel@tonic-gate 		 * map-in statefile buf pages in 512K blocks;
4207c478bd9Sstevel@tonic-gate 		 * see MMU_PAGESIZE512K above
4217c478bd9Sstevel@tonic-gate 		 */
4227c478bd9Sstevel@tonic-gate 		if ((cnt & mmask) == 0) {
4237c478bd9Sstevel@tonic-gate 			dst_virt = sfile.buf;
4247c478bd9Sstevel@tonic-gate 			cb_mapin(dst_virt, ADDR_TO_PN(dst_phys),
4257c478bd9Sstevel@tonic-gate 			    TTE512K, TTE_HWWR_INT, dtlb_index);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 		cnt++;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 		len = min(PROM_MAX_READ, resid);
431986fd29aSsetje 		nread = cpr_read(sfile.fd, dst_virt, len);
4327c478bd9Sstevel@tonic-gate 		if (nread != (ssize_t)len) {
4337c478bd9Sstevel@tonic-gate 			prom_printf("\n%s: prom read error, "
4347c478bd9Sstevel@tonic-gate 			    "expect %ld, got %ld\n", str, len, nread);
4357c478bd9Sstevel@tonic-gate 			return (ERR);
4367c478bd9Sstevel@tonic-gate 		}
4377c478bd9Sstevel@tonic-gate 		dst_virt += len;
4387c478bd9Sstevel@tonic-gate 		dst_phys += len;
4397c478bd9Sstevel@tonic-gate 	}
440ae115bc7Smrj 	CPR_DEBUG(CPR_DEBUG1, " \b\n");
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	/*
4437c478bd9Sstevel@tonic-gate 	 * free up any unused phys pages trailing the statefile buffer;
4447c478bd9Sstevel@tonic-gate 	 * these pages will later appear on the physavail list
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	if (alsize > sfile.size) {
4477c478bd9Sstevel@tonic-gate 		len = alsize - sfile.size;
4487c478bd9Sstevel@tonic-gate 		prom_free_phys(len, phys + sfile.size);
449*8793b36bSNick Todd 		CB_VPRINTF(("%s: freed %ld phys pages (0x%llx - 0x%llx)\n",
450*8793b36bSNick Todd 		    str, mmu_btop(len),
451*8793b36bSNick Todd 		    (unsigned long long)(phys + sfile.size),
452*8793b36bSNick Todd 		    (unsigned long long)(phys + alsize)));
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 	/*
4567c478bd9Sstevel@tonic-gate 	 * start the statefile buffer offset at the base of
4577c478bd9Sstevel@tonic-gate 	 * the statefile buffer and skip past the dump header
4587c478bd9Sstevel@tonic-gate 	 */
4597c478bd9Sstevel@tonic-gate 	sfile.buf_offset = 0;
4607c478bd9Sstevel@tonic-gate 	SF_ADV(sizeof (cdump));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	/*
4637c478bd9Sstevel@tonic-gate 	 * finish with the first block mapped-in to provide easy virt access
4647c478bd9Sstevel@tonic-gate 	 * to machdep structs and the bitmap; for 2.8, the combined size of
4657c478bd9Sstevel@tonic-gate 	 * (cdd_t + cmd_t + csu_md_t + prom_words + cbd_t) is about 1K,
4667c478bd9Sstevel@tonic-gate 	 * leaving room for a bitmap representing nearly 32GB
4677c478bd9Sstevel@tonic-gate 	 */
4687c478bd9Sstevel@tonic-gate 	cb_mapin(sfile.buf, sfile.low_ppn,
4697c478bd9Sstevel@tonic-gate 	    TTE512K, TTE_HWWR_INT, dtlb_index);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	return (0);
4727c478bd9Sstevel@tonic-gate }
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate /*
4767c478bd9Sstevel@tonic-gate  * cprboot first stage worklist
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate static int (*first_worklist[])(void) = {
4797c478bd9Sstevel@tonic-gate 	cb_intro,
480986fd29aSsetje 	cb_mountroot,
4817c478bd9Sstevel@tonic-gate 	cb_startup,
4827c478bd9Sstevel@tonic-gate 	cb_get_props,
4837c478bd9Sstevel@tonic-gate 	cb_usb_setup,
484986fd29aSsetje 	cb_unmountroot,
4857c478bd9Sstevel@tonic-gate 	cb_open_sf,
4867c478bd9Sstevel@tonic-gate 	cb_read_statefile,
4877c478bd9Sstevel@tonic-gate 	cb_close_sf,
4887c478bd9Sstevel@tonic-gate 	cb_check_machdep,
4897c478bd9Sstevel@tonic-gate 	cb_interpret,
4907c478bd9Sstevel@tonic-gate 	cb_get_physavail,
4917c478bd9Sstevel@tonic-gate 	cb_set_bitmap,
4927c478bd9Sstevel@tonic-gate 	cb_get_newstack,
4937c478bd9Sstevel@tonic-gate 	NULL
4947c478bd9Sstevel@tonic-gate };
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate /*
4977c478bd9Sstevel@tonic-gate  * cprboot second stage worklist
4987c478bd9Sstevel@tonic-gate  */
4997c478bd9Sstevel@tonic-gate static int (*second_worklist[])(void) = {
5007c478bd9Sstevel@tonic-gate 	cb_relocate,
5017c478bd9Sstevel@tonic-gate 	cb_tracking_setup,
5027c478bd9Sstevel@tonic-gate 	cb_restore_kpages,
5037c478bd9Sstevel@tonic-gate 	cb_terminator,
5047c478bd9Sstevel@tonic-gate 	cb_ksetup,
5057c478bd9Sstevel@tonic-gate 	cb_mpsetup,
5067c478bd9Sstevel@tonic-gate 	NULL
5077c478bd9Sstevel@tonic-gate };
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate /*
5117c478bd9Sstevel@tonic-gate  * simple loop driving major cprboot operations;
5127c478bd9Sstevel@tonic-gate  * exits to prom if any error is returned
5137c478bd9Sstevel@tonic-gate  */
5147c478bd9Sstevel@tonic-gate static void
5157c478bd9Sstevel@tonic-gate cb_drive(int (**worklist)(void))
5167c478bd9Sstevel@tonic-gate {
5177c478bd9Sstevel@tonic-gate 	int i;
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 	for (i = 0; worklist[i] != NULL; i++) {
5207c478bd9Sstevel@tonic-gate 		if (worklist[i]())
5217c478bd9Sstevel@tonic-gate 			cb_exit_to_mon();
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate }
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate /*
5277c478bd9Sstevel@tonic-gate  * debugging support: drop to prom if do_halt is set
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate static void
5307c478bd9Sstevel@tonic-gate check_halt(char *str)
5317c478bd9Sstevel@tonic-gate {
5327c478bd9Sstevel@tonic-gate 	if (do_halt) {
5337c478bd9Sstevel@tonic-gate 		prom_printf("\n%s halted by -h flag\n==> before %s\n\n",
5347c478bd9Sstevel@tonic-gate 		    prog, str);
5357c478bd9Sstevel@tonic-gate 		cb_enter_mon();
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  * main is called twice from "cb_srt0.s", args are:
5427c478bd9Sstevel@tonic-gate  *	cookie	  ieee1275 cif handle
5437c478bd9Sstevel@tonic-gate  *	first	  (true): first stage, (false): second stage
5447c478bd9Sstevel@tonic-gate  *
5457c478bd9Sstevel@tonic-gate  * first stage summary:
5467c478bd9Sstevel@tonic-gate  *	various setup
5477c478bd9Sstevel@tonic-gate  *	allocate a big statefile buffer
5487c478bd9Sstevel@tonic-gate  *	read in the statefile
5497c478bd9Sstevel@tonic-gate  *	setup the bitmap
5507c478bd9Sstevel@tonic-gate  *	create a new stack
5517c478bd9Sstevel@tonic-gate  *
5527c478bd9Sstevel@tonic-gate  * return to "cb_srt0.s", switch to new stack
5537c478bd9Sstevel@tonic-gate  *
5547c478bd9Sstevel@tonic-gate  * second stage summary:
5557c478bd9Sstevel@tonic-gate  *	relocate cprboot phys pages
5567c478bd9Sstevel@tonic-gate  *	setup tracking for statefile buffer pages
5577c478bd9Sstevel@tonic-gate  *	restore kernel pages
5587c478bd9Sstevel@tonic-gate  *	various cleanup
5597c478bd9Sstevel@tonic-gate  *	install tlb entries for the nucleus and cpr module
5607c478bd9Sstevel@tonic-gate  *	restore registers and jump into cpr module
5617c478bd9Sstevel@tonic-gate  */
5627c478bd9Sstevel@tonic-gate int
5637c478bd9Sstevel@tonic-gate main(void *cookie, int first)
5647c478bd9Sstevel@tonic-gate {
5657c478bd9Sstevel@tonic-gate 	if (first) {
5667c478bd9Sstevel@tonic-gate 		prom_init(prog, cookie);
5677c478bd9Sstevel@tonic-gate 		cb_msec = prom_gettime();
5687c478bd9Sstevel@tonic-gate 		get_bootargs();
5697c478bd9Sstevel@tonic-gate 		check_bootargs();
5707c478bd9Sstevel@tonic-gate 		check_halt("first_worklist");
5717c478bd9Sstevel@tonic-gate 		cb_drive(first_worklist);
5727c478bd9Sstevel@tonic-gate 		return (0);
5737c478bd9Sstevel@tonic-gate 	} else {
5747c478bd9Sstevel@tonic-gate 		cb_drive(second_worklist);
5757c478bd9Sstevel@tonic-gate 		if (verbose || CPR_DBG(1)) {
5767c478bd9Sstevel@tonic-gate 			prom_printf("%s: milliseconds %d\n",
5777c478bd9Sstevel@tonic-gate 			    prog, prom_gettime() - cb_msec);
57853391bafSeota 			prom_printf("%s: resume pc 0x%lx\n",
57953391bafSeota 			    prog, mdinfo.func);
5807c478bd9Sstevel@tonic-gate 			prom_printf("%s: exit_to_kernel(0x%p, 0x%p)\n\n",
581*8793b36bSNick Todd 			    prog, cookie, (void *)&mdinfo);
5827c478bd9Sstevel@tonic-gate 		}
5837c478bd9Sstevel@tonic-gate 		check_halt("exit_to_kernel");
5847c478bd9Sstevel@tonic-gate 		exit_to_kernel(cookie, &mdinfo);
5857c478bd9Sstevel@tonic-gate 		return (ERR);
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate }
588