xref: /titanic_41/usr/src/psm/stand/cpr/sparcv9/sun4u/cprboot.c (revision 3125ebfc35130d243e775dc38a6a59be4df0b137)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * cprboot - prom client that restores kadb/kernel pages
30  *
31  * simple cprboot overview:
32  *	reset boot-file/boot-device to their original values
33  * 	open cpr statefile, usually "/.CPR"
34  *	read in statefile
35  *	close statefile
36  *	restore kernel pages
37  *	jump back into kernel text
38  *
39  *
40  * cprboot supports a restartable statefile for FAA/STARS,
41  * Federal Aviation Administration
42  * Standard Terminal Automation Replacement System
43  */
44 
45 #include <sys/types.h>
46 #include <sys/cpr.h>
47 #include <sys/promimpl.h>
48 #include <sys/ddi.h>
49 #include "cprboot.h"
50 
51 
52 /*
53  * local defs
54  */
55 #define	CB_MAXPROP	256
56 #define	CB_MAXARGS	8
57 
58 
59 /*
60  * globals
61  */
62 struct statefile sfile;
63 
64 char cpr_statefile[OBP_MAXPATHLEN];
65 char cpr_filesystem[OBP_MAXPATHLEN];
66 
67 int cpr_debug;				/* cpr debug, set with uadmin 3 10x */
68 uint_t cb_msec;				/* cprboot start runtime */
69 uint_t cb_dents;			/* number of dtlb entries */
70 
71 int do_halt = 0;			/* halt (enter mon) after load */
72 int verbose = 0;			/* verbose, traces cprboot ops */
73 
74 char rsvp[] = "please reboot";
75 char prog[] = "cprboot";
76 char entry[] = "ENTRY";
77 char ent_fmt[] = "\n%s %s\n";
78 
79 
80 /*
81  * file scope
82  */
83 static char cb_argbuf[CB_MAXPROP];
84 static char *cb_args[CB_MAXARGS];
85 
86 static int reusable;
87 static char *specialstate;
88 
89 
90 static int
91 cb_intro(void)
92 {
93 	static char cstr[] = "\014" "\033[1P" "\033[18;21H";
94 
95 	CB_VENTRY(cb_intro);
96 
97 	/*
98 	 * build/debug aid; this condition should not occur
99 	 */
100 	if ((uintptr_t)_end > CB_SRC_VIRT) {
101 		prom_printf("\ndata collision:\n"
102 		    "(_end=0x%p > CB_LOW_VIRT=0x%x), recompile...\n",
103 		    _end, CB_SRC_VIRT);
104 		return (ERR);
105 	}
106 
107 	/* clear console */
108 	prom_printf(cstr);
109 
110 	prom_printf("Restoring the System. Please Wait... ");
111 	return (0);
112 }
113 
114 
115 /*
116  * read bootargs and convert to arg vector
117  *
118  * sets globals:
119  *	cb_argbuf
120  *	cb_args
121  */
122 static void
123 get_bootargs(void)
124 {
125 	char *cp, *tail, *argp, **argv;
126 
127 	CB_VENTRY(get_bootargs);
128 
129 	(void) prom_strcpy(cb_argbuf, prom_bootargs());
130 	tail = cb_argbuf + prom_strlen(cb_argbuf);
131 
132 	/*
133 	 * scan to the trailing NULL so the last arg
134 	 * will be found without any special-case code
135 	 */
136 	argv = cb_args;
137 	for (cp = argp = cb_argbuf; cp <= tail; cp++) {
138 		if (prom_strchr(" \t\n\r", *cp) == NULL)
139 			continue;
140 		*cp = '\0';
141 		if (cp - argp) {
142 			*argv++ = argp;
143 			if ((argv - cb_args) == (CB_MAXARGS - 1))
144 				break;
145 		}
146 		argp = cp + 1;
147 	}
148 	*argv = NULLP;
149 
150 	if (verbose) {
151 		for (argv = cb_args; *argv; argv++) {
152 			prom_printf("    %ld: \"%s\"\n",
153 			    (argv - cb_args), *argv);
154 		}
155 	}
156 }
157 
158 
159 static void
160 usage(char *expect, char *got)
161 {
162 	if (got == NULL)
163 		got = "(NULL)";
164 	prom_printf("\nbad OBP boot args: expect %s, got %s\n"
165 	    "Usage: boot -F %s [-R] [-S <diskpath>]\n%s\n\n",
166 	    expect, got, prog, rsvp);
167 	prom_exit_to_mon();
168 }
169 
170 
171 /*
172  * bootargs should start with "-F cprboot"
173  *
174  * may set globals:
175  *	specialstate
176  *	reusable
177  *	do_halt
178  *	verbose
179  */
180 static void
181 check_bootargs(void)
182 {
183 	char **argv, *str, *cp;
184 
185 	argv = cb_args;
186 
187 	/* expect "-F" */
188 	str = "-F";
189 	if (*argv == NULL || prom_strcmp(*argv, str))
190 		usage(str, *argv);
191 	argv++;
192 
193 	/* expect "cprboot*" */
194 	if (*argv == NULL || prom_strncmp(*argv, prog, sizeof (prog) - 1))
195 		usage(prog, *argv);
196 
197 	/*
198 	 * optional args
199 	 */
200 	str = "-[SR]";
201 	for (argv++; *argv; argv++) {
202 		cp = *argv;
203 		if (*cp != '-')
204 			usage(str, *argv);
205 
206 		switch (*++cp) {
207 		case 'R':
208 		case 'r':
209 			reusable = 1;
210 			break;
211 		case 'S':
212 		case 's':
213 			if (*++argv)
214 				specialstate = *argv;
215 			else
216 				usage("statefile-path", *argv);
217 			break;
218 		case 'h':
219 			do_halt = 1;
220 			break;
221 		case 'v':
222 			verbose = 1;
223 			break;
224 		default:
225 			usage(str, *argv);
226 			break;
227 		}
228 	}
229 }
230 
231 
232 /*
233  * reset prom props and get statefile info
234  *
235  * sets globals:
236  *	cpr_filesystem
237  *	cpr_statefile
238  */
239 static int
240 cb_startup(void)
241 {
242 	CB_VENTRY(cb_startup);
243 
244 	if (!reusable) {
245 		/*
246 		 * Restore the original values of the nvram properties modified
247 		 * during suspend.  Note: if we can't get this info from the
248 		 * defaults file, the state file may be obsolete or bad, so we
249 		 * abort.  However, failure to restore one or more properties
250 		 * is NOT fatal (better to continue the resume).
251 		 */
252 		if (cpr_reset_properties() == -1) {
253 			prom_printf("\n%s: cannot read saved "
254 			    "nvram info, %s\n", prog, rsvp);
255 			return (ERR);
256 		}
257 	}
258 
259 	/*
260 	 * simple copy if using specialstate,
261 	 * otherwise read in fs and statefile from a config file
262 	 */
263 	if (specialstate)
264 		(void) prom_strcpy(cpr_statefile, specialstate);
265 	else if (cpr_locate_statefile(cpr_statefile, cpr_filesystem) == -1) {
266 		prom_printf("\n%s: cannot find cpr statefile, %s\n",
267 		    prog, rsvp);
268 		return (ERR);
269 	}
270 
271 	return (0);
272 }
273 
274 
275 static int
276 cb_open_sf(void)
277 {
278 	CB_VENTRY(cb_open_sf);
279 
280 	sfile.fd = cpr_statefile_open(cpr_statefile, cpr_filesystem);
281 	if (sfile.fd == -1) {
282 		prom_printf("\n%s: can't open %s", prog, cpr_statefile);
283 		if (specialstate)
284 			prom_printf(" on %s", cpr_filesystem);
285 		prom_printf("\n%s\n", rsvp);
286 		return (ERR);
287 	}
288 
289 	/*
290 	 * for block devices, seek past the disk label and bootblock
291 	 */
292 	if (specialstate)
293 		(void) prom_seek(sfile.fd, CPR_SPEC_OFFSET);
294 
295 	return (0);
296 }
297 
298 
299 static int
300 cb_close_sf(void)
301 {
302 	CB_VENTRY(cb_close_sf);
303 
304 	/*
305 	 * close the device so the prom will free up 20+ pages
306 	 */
307 	(void) cpr_statefile_close(sfile.fd);
308 	return (0);
309 }
310 
311 
312 /*
313  * to restore kernel pages, we have to open a prom device to read-in
314  * the statefile contents; a prom "open" request triggers the driver
315  * and various packages to allocate 20+ pages; unfortunately, some or
316  * all of those pages always clash with kernel pages, and we cant write
317  * to them without corrupting the prom.
318  *
319  * to solve that problem, the only real solution is to close the device
320  * to free up those pages; this means we need to open, read-in the entire
321  * statefile, and close; and to store the statefile, we need to allocate
322  * plenty of space, usually around 2 to 60 MB.
323  *
324  * the simplest alloc means is prom_alloc(), which will "claim" both
325  * virt and phys pages, and creates mappings with a "map" request;
326  * "map" also causes the prom to alloc pages, and again these clash
327  * with kernel pages...
328  *
329  * to solve the "map" problem, we just reserve virt and phys pages and
330  * manage the translations by creating our own tlb entries instead of
331  * relying on the prom.
332  *
333  * sets globals:
334  *	cpr_test_mode
335  *	sfile.kpages
336  *	sfile.size
337  * 	sfile.buf
338  * 	sfile.low_ppn
339  * 	sfile.high_ppn
340  */
341 static int
342 cb_read_statefile(void)
343 {
344 	size_t alsize, len, resid;
345 	physaddr_t phys, dst_phys;
346 	char *str, *dst_virt;
347 	int err, cnt, mmask;
348 	uint_t dtlb_index;
349 	ssize_t nread;
350 	cdd_t cdump;
351 
352 	str = "cb_read_statefile";
353 	CB_VPRINTF((ent_fmt, str, entry));
354 
355 	/*
356 	 * read-in and check cpr dump header
357 	 */
358 	if (cpr_read_cdump(sfile.fd, &cdump, CPR_MACHTYPE_4U))
359 		return (ERR);
360 	if (cpr_debug)
361 		prom_printf("\n");
362 	cb_nbitmaps = cdump.cdd_bitmaprec;
363 	cpr_test_mode = cdump.cdd_test_mode;
364 	sfile.kpages = cdump.cdd_dumppgsize;
365 	CPR_DEBUG(CPR_DEBUG4, "%s: total kpages %d\n", prog, sfile.kpages);
366 
367 	/*
368 	 * alloc virt and phys space with 512K alignment;
369 	 * alloc size should be (n * tte size);
370 	 */
371 	sfile.size = PAGE_ROUNDUP(cdump.cdd_filesize);
372 	alsize = (cdump.cdd_filesize + MMU_PAGEOFFSET512K) &
373 	    MMU_PAGEMASK512K;
374 	phys = 0;
375 	err = cb_alloc(alsize, MMU_PAGESIZE512K, &sfile.buf, &phys);
376 	CB_VPRINTF(("%s:\n    alloc size 0x%lx, buf size 0x%lx\n"
377 	    "    virt 0x%p, phys 0x%llx\n",
378 	    str, alsize, sfile.size, sfile.buf, phys));
379 	if (err) {
380 		prom_printf("%s: cant alloc statefile buf, size 0x%lx\n%s\n",
381 		    str, sfile.size, rsvp);
382 		return (ERR);
383 	}
384 
385 	/*
386 	 * record low and high phys page numbers for sfile.buf
387 	 */
388 	sfile.low_ppn = ADDR_TO_PN(phys);
389 	sfile.high_ppn = sfile.low_ppn + mmu_btop(sfile.size) - 1;
390 
391 	/*
392 	 * setup destination virt and phys addrs for reads;
393 	 * mapin-mask tells when to create a new tlb entry for the
394 	 * next set of reads;  NB: the read and tlb method needs
395 	 * ((big-pagesize % read-size) == 0)
396 	 */
397 	dst_phys = phys;
398 	mmask = (MMU_PAGESIZE512K / PROM_MAX_READ) - 1;
399 
400 	cnt = 0;
401 	dtlb_index = cb_dents - 1;
402 	(void) prom_seek(sfile.fd, specialstate ? CPR_SPEC_OFFSET : 0);
403 	CPR_DEBUG(CPR_DEBUG1, "%s: reading statefile... ", prog);
404 	for (resid = cdump.cdd_filesize; resid; resid -= len) {
405 		/*
406 		 * do a full spin (4 spin chars)
407 		 * for every MB read (8 reads = 256K)
408 		 */
409 		if ((cnt & 0x7) == 0)
410 			cb_spin();
411 
412 		/*
413 		 * map-in statefile buf pages in 512K blocks;
414 		 * see MMU_PAGESIZE512K above
415 		 */
416 		if ((cnt & mmask) == 0) {
417 			dst_virt = sfile.buf;
418 			cb_mapin(dst_virt, ADDR_TO_PN(dst_phys),
419 			    TTE512K, TTE_HWWR_INT, dtlb_index);
420 		}
421 
422 		cnt++;
423 
424 		len = min(PROM_MAX_READ, resid);
425 		nread = prom_read(sfile.fd, dst_virt, len, 0, 0);
426 		if (nread != (ssize_t)len) {
427 			prom_printf("\n%s: prom read error, "
428 			    "expect %ld, got %ld\n", str, len, nread);
429 			return (ERR);
430 		}
431 		dst_virt += len;
432 		dst_phys += len;
433 	}
434 	CPR_DEBUG(CPR_DEBUG1, " \b\n");
435 
436 	/*
437 	 * free up any unused phys pages trailing the statefile buffer;
438 	 * these pages will later appear on the physavail list
439 	 */
440 	if (alsize > sfile.size) {
441 		len = alsize - sfile.size;
442 		prom_free_phys(len, phys + sfile.size);
443 		CB_VPRINTF(("%s: freed %ld phys pages (0x%lx - 0x%lx)\n",
444 		    str, mmu_btop(len), phys + sfile.size, phys + alsize));
445 	}
446 
447 	/*
448 	 * start the statefile buffer offset at the base of
449 	 * the statefile buffer and skip past the dump header
450 	 */
451 	sfile.buf_offset = 0;
452 	SF_ADV(sizeof (cdump));
453 
454 	/*
455 	 * finish with the first block mapped-in to provide easy virt access
456 	 * to machdep structs and the bitmap; for 2.8, the combined size of
457 	 * (cdd_t + cmd_t + csu_md_t + prom_words + cbd_t) is about 1K,
458 	 * leaving room for a bitmap representing nearly 32GB
459 	 */
460 	cb_mapin(sfile.buf, sfile.low_ppn,
461 	    TTE512K, TTE_HWWR_INT, dtlb_index);
462 
463 	return (0);
464 }
465 
466 
467 /*
468  * cprboot first stage worklist
469  */
470 static int (*first_worklist[])(void) = {
471 	cb_intro,
472 	cb_startup,
473 	cb_get_props,
474 	cb_usb_setup,
475 	cb_open_sf,
476 	cb_read_statefile,
477 	cb_close_sf,
478 	cb_check_machdep,
479 	cb_interpret,
480 	cb_get_physavail,
481 	cb_set_bitmap,
482 	cb_get_newstack,
483 	NULL
484 };
485 
486 /*
487  * cprboot second stage worklist
488  */
489 static int (*second_worklist[])(void) = {
490 	cb_relocate,
491 	cb_tracking_setup,
492 	cb_restore_kpages,
493 	cb_terminator,
494 	cb_ksetup,
495 	cb_mpsetup,
496 	NULL
497 };
498 
499 
500 /*
501  * simple loop driving major cprboot operations;
502  * exits to prom if any error is returned
503  */
504 static void
505 cb_drive(int (**worklist)(void))
506 {
507 	int i;
508 
509 	for (i = 0; worklist[i] != NULL; i++) {
510 		if (worklist[i]())
511 			cb_exit_to_mon();
512 	}
513 }
514 
515 
516 /*
517  * debugging support: drop to prom if do_halt is set
518  */
519 static void
520 check_halt(char *str)
521 {
522 	if (do_halt) {
523 		prom_printf("\n%s halted by -h flag\n==> before %s\n\n",
524 		    prog, str);
525 		cb_enter_mon();
526 	}
527 }
528 
529 
530 /*
531  * main is called twice from "cb_srt0.s", args are:
532  *	cookie	  ieee1275 cif handle
533  *	first	  (true): first stage, (false): second stage
534  *
535  * first stage summary:
536  *	various setup
537  *	allocate a big statefile buffer
538  *	read in the statefile
539  *	setup the bitmap
540  *	create a new stack
541  *
542  * return to "cb_srt0.s", switch to new stack
543  *
544  * second stage summary:
545  *	relocate cprboot phys pages
546  *	setup tracking for statefile buffer pages
547  *	restore kernel pages
548  *	various cleanup
549  *	install tlb entries for the nucleus and cpr module
550  *	restore registers and jump into cpr module
551  */
552 int
553 main(void *cookie, int first)
554 {
555 	if (first) {
556 		prom_init(prog, cookie);
557 		cb_msec = prom_gettime();
558 		get_bootargs();
559 		check_bootargs();
560 		check_halt("first_worklist");
561 		cb_drive(first_worklist);
562 		return (0);
563 	} else {
564 		cb_drive(second_worklist);
565 		if (verbose || CPR_DBG(1)) {
566 			prom_printf("%s: milliseconds %d\n",
567 			    prog, prom_gettime() - cb_msec);
568 			prom_printf("%s: resume pc 0x%lx\n",
569 			    prog, mdinfo.func);
570 			prom_printf("%s: exit_to_kernel(0x%p, 0x%p)\n\n",
571 			    prog, cookie, &mdinfo);
572 		}
573 		check_halt("exit_to_kernel");
574 		exit_to_kernel(cookie, &mdinfo);
575 		return (ERR);
576 	}
577 }
578