xref: /titanic_51/usr/src/uts/sun4u/os/cpr_impl.c (revision f4b3ec61df05330d25f55a36b975b4d7519fdeb1)
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  * Platform specific implementation code
30  */
31 
32 #define	SUNDDI_IMPL
33 
34 #include <sys/types.h>
35 #include <sys/promif.h>
36 #include <sys/prom_isa.h>
37 #include <sys/prom_plat.h>
38 #include <sys/mmu.h>
39 #include <vm/hat_sfmmu.h>
40 #include <sys/iommu.h>
41 #include <sys/scb.h>
42 #include <sys/cpuvar.h>
43 #include <sys/intreg.h>
44 #include <sys/pte.h>
45 #include <vm/hat.h>
46 #include <vm/page.h>
47 #include <vm/as.h>
48 #include <sys/cpr.h>
49 #include <sys/kmem.h>
50 #include <sys/clock.h>
51 #include <sys/kmem.h>
52 #include <sys/panic.h>
53 #include <vm/seg_kmem.h>
54 #include <sys/cpu_module.h>
55 #include <sys/callb.h>
56 #include <sys/machsystm.h>
57 #include <sys/vmsystm.h>
58 #include <sys/systm.h>
59 #include <sys/archsystm.h>
60 #include <sys/stack.h>
61 #include <sys/fs/ufs_fs.h>
62 #include <sys/memlist.h>
63 #include <sys/bootconf.h>
64 #include <sys/thread.h>
65 #include <vm/vm_dep.h>
66 
67 extern	void cpr_clear_bitmaps(void);
68 extern	void dtlb_wr_entry(uint_t, tte_t *, uint64_t *);
69 extern	void itlb_wr_entry(uint_t, tte_t *, uint64_t *);
70 
71 static	int i_cpr_storage_desc_alloc(csd_t **, pgcnt_t *, csd_t **, int);
72 static	void i_cpr_storage_desc_init(csd_t *, pgcnt_t, csd_t *);
73 static	caddr_t i_cpr_storage_data_alloc(pgcnt_t, pgcnt_t *, int);
74 static	int cpr_dump_sensitive(vnode_t *, csd_t *);
75 static	void i_cpr_clear_entries(uint64_t, uint64_t);
76 static	void i_cpr_xcall(xcfunc_t);
77 
78 void	i_cpr_storage_free(void);
79 
80 extern void *i_cpr_data_page;
81 extern int cpr_test_mode;
82 extern int cpr_nbitmaps;
83 extern char cpr_default_path[];
84 extern caddr_t textva, datava;
85 
86 static struct cpr_map_info cpr_prom_retain[CPR_PROM_RETAIN_CNT];
87 caddr_t cpr_vaddr = NULL;
88 
89 static	uint_t sensitive_pages_saved;
90 static	uint_t sensitive_size_saved;
91 
92 caddr_t	i_cpr_storage_data_base;
93 caddr_t	i_cpr_storage_data_end;
94 csd_t *i_cpr_storage_desc_base;
95 csd_t *i_cpr_storage_desc_end;		/* one byte beyond last used descp */
96 csd_t *i_cpr_storage_desc_last_used;	/* last used descriptor */
97 caddr_t sensitive_write_ptr;		/* position for next storage write */
98 
99 size_t	i_cpr_sensitive_bytes_dumped;
100 pgcnt_t	i_cpr_sensitive_pgs_dumped;
101 pgcnt_t	i_cpr_storage_data_sz;		/* in pages */
102 pgcnt_t	i_cpr_storage_desc_pgcnt;	/* in pages */
103 
104 ushort_t cpr_mach_type = CPR_MACHTYPE_4U;
105 static	csu_md_t m_info;
106 
107 
108 #define	MAX_STORAGE_RETRY	3
109 #define	MAX_STORAGE_ALLOC_RETRY	3
110 #define	INITIAL_ALLOC_PCNT	40	/* starting allocation percentage */
111 #define	INTEGRAL		100	/* to get 1% precision */
112 
113 #define	EXTRA_RATE		2	/* add EXTRA_RATE% extra space */
114 #define	EXTRA_DESCS		10
115 
116 #define	CPR_NO_STORAGE_DESC	1
117 #define	CPR_NO_STORAGE_DATA	2
118 
119 #define	CIF_SPLICE		0
120 #define	CIF_UNLINK		1
121 
122 
123 /*
124  * CPR miscellaneous support routines
125  */
126 #define	cpr_open(path, mode,  vpp)	(vn_open(path, UIO_SYSSPACE, \
127 		mode, 0600, vpp, CRCREAT, 0))
128 #define	cpr_rdwr(rw, vp, basep, cnt)	(vn_rdwr(rw, vp,  (caddr_t)(basep), \
129 		cnt, 0LL, UIO_SYSSPACE, 0, (rlim64_t)MAXOFF_T, CRED(), \
130 		(ssize_t *)NULL))
131 
132 /*
133  * definitions for saving/restoring prom pages
134  */
135 static void	*ppage_buf;
136 static pgcnt_t	ppage_count;
137 static pfn_t	*pphys_list;
138 static size_t	pphys_list_size;
139 
140 typedef void (*tlb_rw_t)(uint_t, tte_t *, uint64_t *);
141 typedef void (*tlb_filter_t)(int, tte_t *, uint64_t, void *);
142 
143 /*
144  * private struct for tlb handling
145  */
146 struct cpr_trans_info {
147 	sutlb_t		*dst;
148 	sutlb_t		*tail;
149 	tlb_rw_t	reader;
150 	tlb_rw_t	writer;
151 	tlb_filter_t	filter;
152 	int		index;
153 	uint64_t	skip;		/* assumes TLB <= 64 locked entries */
154 };
155 typedef struct cpr_trans_info cti_t;
156 
157 
158 /*
159  * special handling for tlb info
160  */
161 #define	WITHIN_OFW(va) \
162 	(((va) > (uint64_t)OFW_START_ADDR) && ((va) < (uint64_t)OFW_END_ADDR))
163 
164 #define	WITHIN_NUCLEUS(va, base) \
165 	(((va) >= (base)) && \
166 	(((va) + MMU_PAGESIZE) <= ((base) + MMU_PAGESIZE4M)))
167 
168 #define	IS_BIGKTSB(va) \
169 	(enable_bigktsb && \
170 	((va) >= (uint64_t)ktsb_base) && \
171 	((va) < (uint64_t)(ktsb_base + ktsb_sz)))
172 
173 
174 /*
175  * WARNING:
176  * the text from this file is linked to follow cpr_resume_setup.o;
177  * only add text between here and i_cpr_end_jumpback when it needs
178  * to be called during resume before we switch back to the kernel
179  * trap table.  all the text in this range must fit within a page.
180  */
181 
182 
183 /*
184  * each time a machine is reset, the prom uses an inconsistent set of phys
185  * pages and the cif cookie may differ as well.  so prior to restoring the
186  * original prom, we have to use to use the new/tmp prom's translations
187  * when requesting prom services.
188  *
189  * cif_handler starts out as the original prom cookie, and that gets used
190  * by client_handler() to jump into the prom.  here we splice-in a wrapper
191  * routine by writing cif_handler; client_handler() will now jump to the
192  * wrapper which switches the %tba to the new/tmp prom's trap table then
193  * jumps to the new cookie.
194  */
195 void
196 i_cpr_cif_setup(int action)
197 {
198 	extern void *i_cpr_orig_cif, *cif_handler;
199 	extern int i_cpr_cif_wrapper(void *);
200 
201 	/*
202 	 * save the original cookie and change the current cookie to the
203 	 * wrapper routine.  later we just restore the original cookie.
204 	 */
205 	if (action == CIF_SPLICE) {
206 		i_cpr_orig_cif = cif_handler;
207 		cif_handler = (void *)i_cpr_cif_wrapper;
208 	} else if (action == CIF_UNLINK)
209 		cif_handler = i_cpr_orig_cif;
210 }
211 
212 
213 /*
214  * launch slave cpus into kernel text, pause them,
215  * and restore the original prom pages
216  */
217 void
218 i_cpr_mp_setup(void)
219 {
220 	extern void restart_other_cpu(int);
221 	ihandle_t tmpout = 0;
222 	char *str;
223 	cpu_t *cp;
224 
225 	uint64_t kctx = kcontextreg;
226 
227 	/*
228 	 * Do not allow setting page size codes in MMU primary context
229 	 * register while using cif wrapper. This is needed to work
230 	 * arround OBP incorrect handling of this MMU register.
231 	 */
232 	kcontextreg = 0;
233 
234 	/*
235 	 * reset cpu_ready_set so x_calls work properly
236 	 */
237 	CPUSET_ZERO(cpu_ready_set);
238 	CPUSET_ADD(cpu_ready_set, getprocessorid());
239 
240 	/*
241 	 * setup cif to use the cookie from the new/tmp prom
242 	 * and setup tmp handling for calling prom services.
243 	 */
244 	i_cpr_cif_setup(CIF_SPLICE);
245 
246 	/*
247 	 * at this point, only the nucleus and a few cpr pages are
248 	 * mapped in.  once we switch to the kernel trap table,
249 	 * we can access the rest of kernel space.
250 	 */
251 	prom_set_traptable(&trap_table);
252 
253 	if (ncpus > 1) {
254 		sfmmu_init_tsbs();
255 
256 		if (cpr_debug & CPR_DEBUG1) {
257 			prom_interpret("stdout @ swap l!", (uintptr_t)&tmpout,
258 			    0, 0, 0, 0);
259 			str = "MP startup...\r\n";
260 			(void) prom_write(tmpout, str, strlen(str), 0, 0);
261 		}
262 
263 		mutex_enter(&cpu_lock);
264 		/*
265 		 * All of the slave cpus are not ready at this time,
266 		 * yet the cpu structures have various cpu_flags set;
267 		 * clear cpu_flags and mutex_ready.
268 		 * Since we are coming up from a CPU suspend, the slave cpus
269 		 * are frozen.
270 		 */
271 		for (cp = CPU->cpu_next; cp != CPU; cp = cp->cpu_next) {
272 			cp->cpu_flags = CPU_FROZEN;
273 			cp->cpu_m.mutex_ready = 0;
274 		}
275 
276 		for (cp = CPU->cpu_next; cp != CPU; cp = cp->cpu_next)
277 			restart_other_cpu(cp->cpu_id);
278 
279 		pause_cpus(NULL);
280 		mutex_exit(&cpu_lock);
281 
282 		if (cpr_debug & CPR_DEBUG1) {
283 			str = "MP paused...\r\n";
284 			(void) prom_write(tmpout, str, strlen(str), 0, 0);
285 		}
286 
287 		i_cpr_xcall(i_cpr_clear_entries);
288 	} else
289 		i_cpr_clear_entries(0, 0);
290 
291 	/*
292 	 * now unlink the cif wrapper;  WARNING: do not call any
293 	 * prom_xxx() routines until after prom pages are restored.
294 	 */
295 	i_cpr_cif_setup(CIF_UNLINK);
296 
297 	if (!cpr_reusable_mode)
298 		(void) i_cpr_prom_pages(CPR_PROM_RESTORE);
299 
300 	/* allow setting page size codes in MMU primary context register */
301 	kcontextreg = kctx;
302 }
303 
304 
305 /*
306  * end marker for jumpback page;
307  * this symbol is used to check the size of i_cpr_resume_setup()
308  * and the above text.  For simplicity, the Makefile needs to
309  * link i_cpr_resume_setup.o and cpr_impl.o consecutively.
310  */
311 void
312 i_cpr_end_jumpback(void)
313 {
314 }
315 
316 
317 /*
318  * scan tlb entries with reader; when valid entries are found,
319  * the filter routine will selectively save/clear them
320  */
321 static void
322 i_cpr_scan_tlb(cti_t *ctip)
323 {
324 	uint64_t va_tag;
325 	int tlb_index;
326 	tte_t tte;
327 
328 	for (tlb_index = ctip->index; tlb_index >= 0; tlb_index--) {
329 		(*ctip->reader)((uint_t)tlb_index, &tte, &va_tag);
330 		if (va_tag && TTE_IS_VALID(&tte))
331 			(*ctip->filter)(tlb_index, &tte, va_tag, ctip);
332 	}
333 }
334 
335 
336 /*
337  * filter for locked tlb entries that reference the text/data nucleus
338  * and any bigktsb's; these will be reinstalled by cprboot on all cpus
339  */
340 /* ARGSUSED */
341 static void
342 i_cpr_lnb(int index, tte_t *ttep, uint64_t va_tag, void *ctrans)
343 {
344 	cti_t *ctip;
345 
346 	/*
347 	 * record tlb data at ctip->dst; the target tlb index starts
348 	 * at the highest tlb offset and moves towards 0.  the prom
349 	 * reserves both dtlb and itlb index 0.  any selected entry
350 	 * also gets marked to prevent being flushed during resume
351 	 */
352 	if (TTE_IS_LOCKED(ttep) && (va_tag == (uint64_t)textva ||
353 	    va_tag == (uint64_t)datava || IS_BIGKTSB(va_tag))) {
354 		ctip = ctrans;
355 		while ((1 << ctip->index) & ctip->skip)
356 			ctip->index--;
357 		ASSERT(ctip->index > 0);
358 		ASSERT(ctip->dst < ctip->tail);
359 		ctip->dst->tte.ll = ttep->ll;
360 		ctip->dst->va_tag = va_tag;
361 		ctip->dst->index = ctip->index--;
362 		ctip->dst->tmp = 0;
363 		ctip->dst++;
364 	}
365 }
366 
367 
368 /*
369  * some tlb entries are stale, filter for unlocked entries
370  * within the prom virt range and clear them
371  */
372 static void
373 i_cpr_ufw(int index, tte_t *ttep, uint64_t va_tag, void *ctrans)
374 {
375 	sutlb_t clr;
376 	cti_t *ctip;
377 
378 	if (!TTE_IS_LOCKED(ttep) && WITHIN_OFW(va_tag)) {
379 		ctip = ctrans;
380 		bzero(&clr, sizeof (clr));
381 		(*ctip->writer)((uint_t)index, &clr.tte, &clr.va_tag);
382 	}
383 }
384 
385 
386 /*
387  * some of the entries installed by cprboot are needed only on a
388  * short-term basis and need to be flushed to avoid clogging the tlbs.
389  * scan the dtte/itte arrays for items marked as temporary and clear
390  * dtlb/itlb entries using wrfunc.
391  */
392 static void
393 i_cpr_clear_tmp(sutlb_t *listp, int max, tlb_rw_t wrfunc)
394 {
395 	sutlb_t clr, *tail;
396 
397 	bzero(&clr, sizeof (clr));
398 	for (tail = listp + max; listp < tail && listp->va_tag; listp++) {
399 		if (listp->tmp)
400 			(*wrfunc)((uint_t)listp->index, &clr.tte, &clr.va_tag);
401 	}
402 }
403 
404 
405 /* ARGSUSED */
406 static void
407 i_cpr_clear_entries(uint64_t arg1, uint64_t arg2)
408 {
409 	extern void demap_all(void);
410 	cti_t cti;
411 
412 	i_cpr_clear_tmp(m_info.dtte, CPR_MAX_TLB, dtlb_wr_entry);
413 	i_cpr_clear_tmp(m_info.itte, CPR_MAX_TLB, itlb_wr_entry);
414 
415 	/*
416 	 * for newer cpus that implement DEMAP_ALL_TYPE, demap_all is
417 	 * a second label for vtag_flushall.  the call is made using
418 	 * vtag_flushall() instead of demap_all() due to runtime and
419 	 * krtld results with both older and newer cpu modules.
420 	 */
421 	if (&demap_all != 0) {
422 		vtag_flushall();
423 		return;
424 	}
425 
426 	/*
427 	 * for older V9 cpus, scan tlbs and clear stale entries
428 	 */
429 	bzero(&cti, sizeof (cti));
430 	cti.filter = i_cpr_ufw;
431 
432 	cti.index = cpunodes[CPU->cpu_id].dtlb_size - 1;
433 	cti.reader = dtlb_rd_entry;
434 	cti.writer = dtlb_wr_entry;
435 	i_cpr_scan_tlb(&cti);
436 
437 	cti.index = cpunodes[CPU->cpu_id].itlb_size - 1;
438 	cti.reader = itlb_rd_entry;
439 	cti.writer = itlb_wr_entry;
440 	i_cpr_scan_tlb(&cti);
441 }
442 
443 
444 /*
445  * craft tlb info for tmp use during resume; this data gets used by
446  * cprboot to install tlb entries.  we also mark each struct as tmp
447  * so those tlb entries will get flushed after switching to the kernel
448  * trap table.  no data needs to be recorded for vaddr when it falls
449  * within the nucleus since we've already recorded nucleus ttes and
450  * a 8K tte would conflict with a 4MB tte.  eg: the cpr module
451  * text/data may have been loaded into the text/data nucleus.
452  */
453 static void
454 i_cpr_make_tte(cti_t *ctip, void *vaddr, caddr_t nbase)
455 {
456 	pfn_t ppn;
457 	uint_t rw;
458 
459 	if (WITHIN_NUCLEUS((caddr_t)vaddr, nbase))
460 		return;
461 
462 	while ((1 << ctip->index) & ctip->skip)
463 		ctip->index--;
464 	ASSERT(ctip->index > 0);
465 	ASSERT(ctip->dst < ctip->tail);
466 
467 	/*
468 	 * without any global service available to lookup
469 	 * a tte by vaddr, we craft our own here:
470 	 */
471 	ppn = va_to_pfn(vaddr);
472 	rw = (nbase == datava) ? TTE_HWWR_INT : 0;
473 	ctip->dst->tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(ppn);
474 	ctip->dst->tte.tte_intlo = TTE_PFN_INTLO(ppn) | TTE_LCK_INT |
475 	    TTE_CP_INT | TTE_PRIV_INT | rw;
476 	ctip->dst->va_tag = ((uintptr_t)vaddr & MMU_PAGEMASK);
477 	ctip->dst->index = ctip->index--;
478 	ctip->dst->tmp = 1;
479 	ctip->dst++;
480 }
481 
482 
483 static void
484 i_cpr_xcall(xcfunc_t func)
485 {
486 	uint_t pil, reset_pil;
487 
488 	pil = getpil();
489 	if (pil < XCALL_PIL)
490 		reset_pil = 0;
491 	else {
492 		reset_pil = 1;
493 		setpil(XCALL_PIL - 1);
494 	}
495 	xc_some(cpu_ready_set, func, 0, 0);
496 	if (reset_pil)
497 		setpil(pil);
498 }
499 
500 
501 /*
502  * restart paused slave cpus
503  */
504 void
505 i_cpr_machdep_setup(void)
506 {
507 	if (ncpus > 1) {
508 		CPR_DEBUG(CPR_DEBUG1, "MP restarted...\n");
509 		mutex_enter(&cpu_lock);
510 		start_cpus();
511 		mutex_exit(&cpu_lock);
512 	}
513 }
514 
515 
516 /*
517  * Stop all interrupt activities in the system
518  */
519 void
520 i_cpr_stop_intr(void)
521 {
522 	(void) spl7();
523 }
524 
525 /*
526  * Set machine up to take interrupts
527  */
528 void
529 i_cpr_enable_intr(void)
530 {
531 	(void) spl0();
532 }
533 
534 
535 /*
536  * record cpu nodes and ids
537  */
538 static void
539 i_cpr_save_cpu_info(void)
540 {
541 	struct sun4u_cpu_info *scip;
542 	cpu_t *cp;
543 
544 	scip = m_info.sci;
545 	cp = CPU;
546 	do {
547 		ASSERT(scip < &m_info.sci[NCPU]);
548 		scip->cpu_id = cp->cpu_id;
549 		scip->node = cpunodes[cp->cpu_id].nodeid;
550 		scip++;
551 	} while ((cp = cp->cpu_next) != CPU);
552 }
553 
554 
555 /*
556  * Write necessary machine dependent information to cpr state file,
557  * eg. sun4u mmu ctx secondary for the current running process (cpr) ...
558  */
559 int
560 i_cpr_write_machdep(vnode_t *vp)
561 {
562 	extern uint_t getpstate(), getwstate();
563 	extern uint_t i_cpr_tstack_size;
564 	const char ustr[] = ": unix-tte 2drop false ;";
565 	uintptr_t tinfo;
566 	label_t *ltp;
567 	cmd_t cmach;
568 	char *fmt;
569 	int rc;
570 
571 	/*
572 	 * ustr[] is used as temporary forth words during
573 	 * slave startup sequence, see sfmmu_mp_startup()
574 	 */
575 
576 	cmach.md_magic = (uint_t)CPR_MACHDEP_MAGIC;
577 	cmach.md_size = sizeof (m_info) + sizeof (ustr);
578 
579 	if (rc = cpr_write(vp, (caddr_t)&cmach, sizeof (cmach))) {
580 		cpr_err(CE_WARN, "Failed to write descriptor.");
581 		return (rc);
582 	}
583 
584 	/*
585 	 * m_info is now cleared in i_cpr_dump_setup()
586 	 */
587 	m_info.ksb = (uint32_t)STACK_BIAS;
588 	m_info.kpstate = (uint16_t)getpstate();
589 	m_info.kwstate = (uint16_t)getwstate();
590 	CPR_DEBUG(CPR_DEBUG1, "stack bias 0x%x, pstate 0x%x, wstate 0x%x\n",
591 	    m_info.ksb, m_info.kpstate, m_info.kwstate);
592 
593 	ltp = &ttolwp(curthread)->lwp_qsav;
594 	m_info.qsav_pc = (cpr_ext)ltp->val[0];
595 	m_info.qsav_sp = (cpr_ext)ltp->val[1];
596 
597 	/*
598 	 * Set secondary context to INVALID_CONTEXT to force the HAT
599 	 * to re-setup the MMU registers and locked TTEs it needs for
600 	 * TLB miss handling.
601 	 */
602 	m_info.mmu_ctx_sec = INVALID_CONTEXT;
603 	m_info.mmu_ctx_pri = KCONTEXT;
604 
605 	tinfo = (uintptr_t)curthread;
606 	m_info.thrp = (cpr_ptr)tinfo;
607 
608 	tinfo = (uintptr_t)i_cpr_resume_setup;
609 	m_info.func = (cpr_ptr)tinfo;
610 
611 	/*
612 	 * i_cpr_data_page is comprised of a 4K stack area and a few
613 	 * trailing data symbols; the page is shared by the prom and
614 	 * kernel during resume.  the stack size is recorded here
615 	 * and used by cprboot to set %sp
616 	 */
617 	tinfo = (uintptr_t)&i_cpr_data_page;
618 	m_info.tmp_stack = (cpr_ptr)tinfo;
619 	m_info.tmp_stacksize = i_cpr_tstack_size;
620 
621 	m_info.test_mode = cpr_test_mode;
622 
623 	i_cpr_save_cpu_info();
624 
625 	if (rc = cpr_write(vp, (caddr_t)&m_info, sizeof (m_info))) {
626 		cpr_err(CE_WARN, "Failed to write machdep info.");
627 		return (rc);
628 	}
629 
630 	fmt = "error writing %s forth info";
631 	if (rc = cpr_write(vp, (caddr_t)ustr, sizeof (ustr)))
632 		cpr_err(CE_WARN, fmt, "unix-tte");
633 
634 	return (rc);
635 }
636 
637 
638 /*
639  * Save miscellaneous information which needs to be written to the
640  * state file.  This information is required to re-initialize
641  * kernel/prom handshaking.
642  */
643 void
644 i_cpr_save_machdep_info(void)
645 {
646 	CPR_DEBUG(CPR_DEBUG5, "jumpback size = 0x%lx\n",
647 	    (uintptr_t)&i_cpr_end_jumpback -
648 	    (uintptr_t)i_cpr_resume_setup);
649 
650 	/*
651 	 * Verify the jumpback code all falls in one page.
652 	 */
653 	if (((uintptr_t)&i_cpr_end_jumpback & MMU_PAGEMASK) !=
654 	    ((uintptr_t)i_cpr_resume_setup & MMU_PAGEMASK))
655 		cpr_err(CE_PANIC, "jumpback code exceeds one page.");
656 }
657 
658 
659 void
660 i_cpr_set_tbr(void)
661 {
662 }
663 
664 
665 /*
666  * cpu0 should contain bootcpu info
667  */
668 cpu_t *
669 i_cpr_bootcpu(void)
670 {
671 	return (&cpu0);
672 }
673 
674 
675 /*
676  * Return the virtual address of the mapping area
677  */
678 caddr_t
679 i_cpr_map_setup(void)
680 {
681 	/*
682 	 * Allocate a virtual memory range spanned by an hmeblk.
683 	 * This would be 8 hments or 64k bytes.  Starting VA
684 	 * must be 64k (8-page) aligned.
685 	 */
686 	cpr_vaddr = vmem_xalloc(heap_arena,
687 	    mmu_ptob(NHMENTS), mmu_ptob(NHMENTS),
688 	    0, 0, NULL, NULL, VM_NOSLEEP);
689 	return (cpr_vaddr);
690 }
691 
692 /*
693  * create tmp locked tlb entries for a group of phys pages;
694  *
695  * i_cpr_mapin/i_cpr_mapout should always be called in pairs,
696  * otherwise would fill up a tlb with locked entries
697  */
698 void
699 i_cpr_mapin(caddr_t vaddr, uint_t pages, pfn_t ppn)
700 {
701 	tte_t tte;
702 	extern pfn_t curthreadpfn;
703 	extern int curthreadremapped;
704 
705 	curthreadremapped = (ppn <= curthreadpfn && curthreadpfn < ppn + pages);
706 
707 	for (; pages--; ppn++, vaddr += MMU_PAGESIZE) {
708 		tte.tte_inthi = TTE_VALID_INT | TTE_PFN_INTHI(ppn);
709 		tte.tte_intlo = TTE_PFN_INTLO(ppn) | TTE_LCK_INT |
710 		    TTE_CP_INT | TTE_PRIV_INT | TTE_HWWR_INT;
711 		sfmmu_dtlb_ld_kva(vaddr, &tte);
712 	}
713 }
714 
715 void
716 i_cpr_mapout(caddr_t vaddr, uint_t pages)
717 {
718 	extern int curthreadremapped;
719 
720 	if (curthreadremapped && vaddr <= (caddr_t)curthread &&
721 	    (caddr_t)curthread < vaddr + pages * MMU_PAGESIZE)
722 		curthreadremapped = 0;
723 
724 	for (; pages--; vaddr += MMU_PAGESIZE)
725 		vtag_flushpage(vaddr, (uint64_t)ksfmmup);
726 }
727 
728 /*
729  * We're done using the mapping area; release virtual space
730  */
731 void
732 i_cpr_map_destroy(void)
733 {
734 	vmem_free(heap_arena, cpr_vaddr, mmu_ptob(NHMENTS));
735 	cpr_vaddr = NULL;
736 }
737 
738 /* ARGSUSED */
739 void
740 i_cpr_handle_xc(int flag)
741 {
742 }
743 
744 
745 /*
746  * This function takes care of pages which are not in kas or need to be
747  * taken care of in a special way.  For example, panicbuf pages are not
748  * in kas and their pages are allocated via prom_retain().
749  */
750 pgcnt_t
751 i_cpr_count_special_kpages(int mapflag, bitfunc_t bitfunc)
752 {
753 	struct cpr_map_info *pri, *tail;
754 	pgcnt_t pages, total = 0;
755 	pfn_t pfn;
756 
757 	/*
758 	 * Save information about prom retained panicbuf pages
759 	 */
760 	if (bitfunc == cpr_setbit) {
761 		pri = &cpr_prom_retain[CPR_PANICBUF];
762 		pri->virt = (cpr_ptr)panicbuf;
763 		pri->phys = va_to_pa(panicbuf);
764 		pri->size = sizeof (panicbuf);
765 	}
766 
767 	/*
768 	 * Go through the prom_retain array to tag those pages.
769 	 */
770 	tail = &cpr_prom_retain[CPR_PROM_RETAIN_CNT];
771 	for (pri = cpr_prom_retain; pri < tail; pri++) {
772 		pages = mmu_btopr(pri->size);
773 		for (pfn = ADDR_TO_PN(pri->phys); pages--; pfn++) {
774 			if (pf_is_memory(pfn)) {
775 				if (bitfunc == cpr_setbit) {
776 					if ((*bitfunc)(pfn, mapflag) == 0)
777 						total++;
778 				} else
779 					total++;
780 			}
781 		}
782 	}
783 
784 	return (total);
785 }
786 
787 
788 /*
789  * Free up memory-related resources here.  We start by freeing buffers
790  * allocated during suspend initialization.  Also, free up the mapping
791  * resources allocated in cpr_init().
792  */
793 void
794 i_cpr_free_memory_resources(void)
795 {
796 	(void) i_cpr_prom_pages(CPR_PROM_FREE);
797 	i_cpr_map_destroy();
798 	i_cpr_storage_free();
799 }
800 
801 
802 /*
803  * Derived from cpr_write_statefile().
804  * Save the sensitive pages to the storage area and do bookkeeping
805  * using the sensitive descriptors. Each descriptor will contain no more
806  * than CPR_MAXCONTIG amount of contiguous pages to match the max amount
807  * of pages that statefile gets written to disk at each write.
808  * XXX The CPR_MAXCONTIG can be changed to the size of the compression
809  * scratch area.
810  */
811 static int
812 i_cpr_save_to_storage(void)
813 {
814 	sensitive_size_saved = 0;
815 	sensitive_pages_saved = 0;
816 	sensitive_write_ptr = i_cpr_storage_data_base;
817 	return (cpr_contig_pages(NULL, SAVE_TO_STORAGE));
818 }
819 
820 
821 /*
822  * This routine allocates space to save the sensitive kernel pages,
823  * i.e. kernel data nucleus, kvalloc and kvseg segments.
824  * It's assumed that those segments are the only areas that can be
825  * contaminated by memory allocations during statefile dumping.
826  * The space allocated here contains:
827  * 	A list of descriptors describing the saved sensitive pages.
828  * 	The storage area for saving the compressed sensitive kernel pages.
829  * Since storage pages are allocated from segkmem, they need to be
830  * excluded when saving.
831  */
832 int
833 i_cpr_save_sensitive_kpages(void)
834 {
835 	static const char pages_fmt[] = "\n%s %s allocs\n"
836 	    "	spages %ld, vpages %ld, diff %ld\n";
837 	int retry_cnt;
838 	int error = 0;
839 	pgcnt_t pages, spages, vpages;
840 	caddr_t	addr;
841 	char *str;
842 
843 	/*
844 	 * Tag sensitive kpages. Allocate space for storage descriptors
845 	 * and storage data area based on the resulting bitmaps.
846 	 * Note: The storage space will be part of the sensitive
847 	 * segment, so we need to tag kpages here before the storage
848 	 * is actually allocated just so their space won't be accounted
849 	 * for. They will not be part of the statefile although those
850 	 * pages will be claimed by cprboot.
851 	 */
852 	cpr_clear_bitmaps();
853 
854 	spages = i_cpr_count_sensitive_kpages(REGULAR_BITMAP, cpr_setbit);
855 	vpages = cpr_count_volatile_pages(REGULAR_BITMAP, cpr_clrbit);
856 	pages = spages - vpages;
857 
858 	str = "i_cpr_save_sensitive_kpages:";
859 	CPR_DEBUG(CPR_DEBUG7, pages_fmt, "before", str, spages, vpages, pages);
860 
861 	/*
862 	 * Allocate space to save the clean sensitive kpages
863 	 */
864 	for (retry_cnt = 0; retry_cnt < MAX_STORAGE_ALLOC_RETRY; retry_cnt++) {
865 		/*
866 		 * Alloc on first pass or realloc if we are retrying because
867 		 * of insufficient storage for sensitive pages
868 		 */
869 		if (retry_cnt == 0 || error == ENOMEM) {
870 			if (i_cpr_storage_data_base) {
871 				kmem_free(i_cpr_storage_data_base,
872 				    mmu_ptob(i_cpr_storage_data_sz));
873 				i_cpr_storage_data_base = NULL;
874 				i_cpr_storage_data_sz = 0;
875 			}
876 			addr = i_cpr_storage_data_alloc(pages,
877 			    &i_cpr_storage_data_sz, retry_cnt);
878 			if (addr == NULL) {
879 				CPR_DEBUG(CPR_DEBUG7,
880 				    "\n%s can't allocate data storage space!\n",
881 				    str);
882 				return (ENOMEM);
883 			}
884 			i_cpr_storage_data_base = addr;
885 			i_cpr_storage_data_end =
886 			    addr + mmu_ptob(i_cpr_storage_data_sz);
887 		}
888 
889 		/*
890 		 * Allocate on first pass, only realloc if retry is because of
891 		 * insufficient descriptors, but reset contents on each pass
892 		 * (desc_alloc resets contents as well)
893 		 */
894 		if (retry_cnt == 0 || error == -1) {
895 			error = i_cpr_storage_desc_alloc(
896 			    &i_cpr_storage_desc_base, &i_cpr_storage_desc_pgcnt,
897 			    &i_cpr_storage_desc_end, retry_cnt);
898 			if (error != 0)
899 				return (error);
900 		} else {
901 			i_cpr_storage_desc_init(i_cpr_storage_desc_base,
902 			    i_cpr_storage_desc_pgcnt, i_cpr_storage_desc_end);
903 		}
904 
905 		/*
906 		 * We are ready to save the sensitive kpages to storage.
907 		 * We cannot trust what's tagged in the bitmaps anymore
908 		 * after storage allocations.  Clear up the bitmaps and
909 		 * retag the sensitive kpages again.  The storage pages
910 		 * should be untagged.
911 		 */
912 		cpr_clear_bitmaps();
913 
914 		spages =
915 		    i_cpr_count_sensitive_kpages(REGULAR_BITMAP, cpr_setbit);
916 		vpages = cpr_count_volatile_pages(REGULAR_BITMAP, cpr_clrbit);
917 
918 		CPR_DEBUG(CPR_DEBUG7, pages_fmt, "after ", str,
919 		    spages, vpages, spages - vpages);
920 
921 		/*
922 		 * Returns 0 on success, -1 if too few descriptors, and
923 		 * ENOMEM if not enough space to save sensitive pages
924 		 */
925 		CPR_DEBUG(CPR_DEBUG1, "compressing pages to storage...\n");
926 		error = i_cpr_save_to_storage();
927 		if (error == 0) {
928 			/* Saving to storage succeeded */
929 			CPR_DEBUG(CPR_DEBUG1, "compressed %d pages\n",
930 			    sensitive_pages_saved);
931 			break;
932 		} else if (error == -1)
933 			CPR_DEBUG(CPR_DEBUG1, "%s too few descriptors\n", str);
934 	}
935 	if (error == -1)
936 		error = ENOMEM;
937 	return (error);
938 }
939 
940 
941 /*
942  * Estimate how much memory we will need to save
943  * the sensitive pages with compression.
944  */
945 static caddr_t
946 i_cpr_storage_data_alloc(pgcnt_t pages, pgcnt_t *alloc_pages, int retry_cnt)
947 {
948 	pgcnt_t alloc_pcnt, last_pcnt;
949 	caddr_t addr;
950 	char *str;
951 
952 	str = "i_cpr_storage_data_alloc:";
953 	if (retry_cnt == 0) {
954 		/*
955 		 * common compression ratio is about 3:1
956 		 * initial storage allocation is estimated at 40%
957 		 * to cover the majority of cases
958 		 */
959 		alloc_pcnt = INITIAL_ALLOC_PCNT;
960 		*alloc_pages = (pages * alloc_pcnt) / INTEGRAL;
961 		CPR_DEBUG(CPR_DEBUG7, "%s sensitive pages: %ld\n", str, pages);
962 		CPR_DEBUG(CPR_DEBUG7,
963 		    "%s initial est pages: %ld, alloc %ld%%\n",
964 		    str, *alloc_pages, alloc_pcnt);
965 	} else {
966 		/*
967 		 * calculate the prior compression percentage (x100)
968 		 * from the last attempt to save sensitive pages
969 		 */
970 		ASSERT(sensitive_pages_saved != 0);
971 		last_pcnt = (mmu_btopr(sensitive_size_saved) * INTEGRAL) /
972 		    sensitive_pages_saved;
973 		CPR_DEBUG(CPR_DEBUG7, "%s last ratio %ld%%\n", str, last_pcnt);
974 
975 		/*
976 		 * new estimated storage size is based on
977 		 * the larger ratio + 5% for each retry:
978 		 * pages * (last + [5%, 10%])
979 		 */
980 		alloc_pcnt = MAX(last_pcnt, INITIAL_ALLOC_PCNT) +
981 		    (retry_cnt * 5);
982 		*alloc_pages = (pages * alloc_pcnt) / INTEGRAL;
983 		CPR_DEBUG(CPR_DEBUG7, "%s Retry est pages: %ld, alloc %ld%%\n",
984 		    str, *alloc_pages, alloc_pcnt);
985 	}
986 
987 	addr = kmem_alloc(mmu_ptob(*alloc_pages), KM_NOSLEEP);
988 	CPR_DEBUG(CPR_DEBUG7, "%s alloc %ld pages\n", str, *alloc_pages);
989 	return (addr);
990 }
991 
992 
993 void
994 i_cpr_storage_free(void)
995 {
996 	/* Free descriptors */
997 	if (i_cpr_storage_desc_base) {
998 		kmem_free(i_cpr_storage_desc_base,
999 		    mmu_ptob(i_cpr_storage_desc_pgcnt));
1000 		i_cpr_storage_desc_base = NULL;
1001 		i_cpr_storage_desc_pgcnt = 0;
1002 	}
1003 
1004 
1005 	/* Data storage */
1006 	if (i_cpr_storage_data_base) {
1007 		kmem_free(i_cpr_storage_data_base,
1008 		    mmu_ptob(i_cpr_storage_data_sz));
1009 		i_cpr_storage_data_base = NULL;
1010 		i_cpr_storage_data_sz = 0;
1011 	}
1012 }
1013 
1014 
1015 /*
1016  * This routine is derived from cpr_compress_and_write().
1017  * 1. Do bookkeeping in the descriptor for the contiguous sensitive chunk.
1018  * 2. Compress and save the clean sensitive pages into the storage area.
1019  */
1020 int
1021 i_cpr_compress_and_save(int chunks, pfn_t spfn, pgcnt_t pages)
1022 {
1023 	extern char *cpr_compress_pages(cpd_t *, pgcnt_t, int);
1024 	extern caddr_t i_cpr_storage_data_end;
1025 	uint_t remaining, datalen;
1026 	uint32_t test_usum;
1027 	char *datap;
1028 	csd_t *descp;
1029 	cpd_t cpd;
1030 	int error;
1031 
1032 	/*
1033 	 * Fill next empty storage descriptor
1034 	 */
1035 	descp = i_cpr_storage_desc_base + chunks - 1;
1036 	if (descp >= i_cpr_storage_desc_end) {
1037 		CPR_DEBUG(CPR_DEBUG1, "ran out of descriptors, base 0x%p, "
1038 		    "chunks %d, end 0x%p, descp 0x%p\n",
1039 		    i_cpr_storage_desc_base, chunks,
1040 		    i_cpr_storage_desc_end, descp);
1041 		return (-1);
1042 	}
1043 	ASSERT(descp->csd_dirty_spfn == (uint_t)-1);
1044 	i_cpr_storage_desc_last_used = descp;
1045 
1046 	descp->csd_dirty_spfn = spfn;
1047 	descp->csd_dirty_npages = pages;
1048 
1049 	i_cpr_mapin(CPR->c_mapping_area, pages, spfn);
1050 
1051 	/*
1052 	 * try compressing pages and copy cpd fields
1053 	 * pfn is copied for debug use
1054 	 */
1055 	cpd.cpd_pfn = spfn;
1056 	datap = cpr_compress_pages(&cpd, pages, C_COMPRESSING);
1057 	datalen = cpd.cpd_length;
1058 	descp->csd_clean_compressed = (cpd.cpd_flag & CPD_COMPRESS);
1059 #ifdef DEBUG
1060 	descp->csd_usum = cpd.cpd_usum;
1061 	descp->csd_csum = cpd.cpd_csum;
1062 #endif
1063 
1064 	error = 0;
1065 
1066 	/*
1067 	 * Save the raw or compressed data to the storage area pointed to by
1068 	 * sensitive_write_ptr. Make sure the storage space is big enough to
1069 	 * hold the result. Otherwise roll back to increase the storage space.
1070 	 */
1071 	descp->csd_clean_sva = (cpr_ptr)sensitive_write_ptr;
1072 	descp->csd_clean_sz = datalen;
1073 	if ((sensitive_write_ptr + datalen) < i_cpr_storage_data_end) {
1074 		extern	void cprbcopy(void *, void *, size_t);
1075 
1076 		cprbcopy(datap, sensitive_write_ptr, datalen);
1077 		sensitive_size_saved += datalen;
1078 		sensitive_pages_saved += descp->csd_dirty_npages;
1079 		sensitive_write_ptr += datalen;
1080 	} else {
1081 		remaining = (i_cpr_storage_data_end - sensitive_write_ptr);
1082 		CPR_DEBUG(CPR_DEBUG1, "i_cpr_compress_and_save: The storage "
1083 		    "space is too small!\ngot %d, want %d\n\n",
1084 		    remaining, (remaining + datalen));
1085 #ifdef	DEBUG
1086 		/*
1087 		 * Check to see if the content of the sensitive pages that we
1088 		 * just copied have changed during this small time window.
1089 		 */
1090 		test_usum = checksum32(CPR->c_mapping_area, mmu_ptob(pages));
1091 		descp->csd_usum = cpd.cpd_usum;
1092 		if (test_usum != descp->csd_usum) {
1093 			CPR_DEBUG(CPR_DEBUG1, "\nWARNING: "
1094 			    "i_cpr_compress_and_save: "
1095 			    "Data in the range of pfn 0x%lx to pfn "
1096 			    "0x%lx has changed after they are saved "
1097 			    "into storage.", spfn, (spfn + pages - 1));
1098 		}
1099 #endif
1100 		error = ENOMEM;
1101 	}
1102 
1103 	i_cpr_mapout(CPR->c_mapping_area, pages);
1104 	return (error);
1105 }
1106 
1107 
1108 /*
1109  * This routine is derived from cpr_count_kpages().
1110  * It goes through kernel data nucleus and segkmem segments to select
1111  * pages in use and mark them in the corresponding bitmap.
1112  */
1113 pgcnt_t
1114 i_cpr_count_sensitive_kpages(int mapflag, bitfunc_t bitfunc)
1115 {
1116 	pgcnt_t kdata_cnt = 0, segkmem_cnt = 0;
1117 	extern caddr_t e_moddata;
1118 	extern struct seg kvalloc;
1119 	extern struct seg kmem64;
1120 	size_t size;
1121 
1122 	/*
1123 	 * Kernel data nucleus pages
1124 	 */
1125 	size = e_moddata - s_data;
1126 	kdata_cnt += cpr_count_pages(s_data, size,
1127 	    mapflag, bitfunc, DBG_SHOWRANGE);
1128 
1129 	/*
1130 	 * kvseg and kvalloc pages
1131 	 */
1132 	segkmem_cnt += cpr_scan_kvseg(mapflag, bitfunc, &kvseg);
1133 	segkmem_cnt += cpr_count_pages(kvalloc.s_base, kvalloc.s_size,
1134 	    mapflag, bitfunc, DBG_SHOWRANGE);
1135 
1136 	/* segment to support kernel memory usage above 32-bit space (4GB) */
1137 	if (kmem64.s_base)
1138 		segkmem_cnt += cpr_count_pages(kmem64.s_base, kmem64.s_size,
1139 		    mapflag, bitfunc, DBG_SHOWRANGE);
1140 
1141 	CPR_DEBUG(CPR_DEBUG7, "\ni_cpr_count_sensitive_kpages:\n"
1142 	    "\tkdata_cnt %ld + segkmem_cnt %ld = %ld pages\n",
1143 	    kdata_cnt, segkmem_cnt, kdata_cnt + segkmem_cnt);
1144 
1145 	return (kdata_cnt + segkmem_cnt);
1146 }
1147 
1148 
1149 pgcnt_t
1150 i_cpr_count_storage_pages(int mapflag, bitfunc_t bitfunc)
1151 {
1152 	pgcnt_t count = 0;
1153 
1154 	if (i_cpr_storage_desc_base) {
1155 		count += cpr_count_pages((caddr_t)i_cpr_storage_desc_base,
1156 		    (size_t)mmu_ptob(i_cpr_storage_desc_pgcnt),
1157 		    mapflag, bitfunc, DBG_SHOWRANGE);
1158 	}
1159 	if (i_cpr_storage_data_base) {
1160 		count += cpr_count_pages(i_cpr_storage_data_base,
1161 		    (size_t)mmu_ptob(i_cpr_storage_data_sz),
1162 		    mapflag, bitfunc, DBG_SHOWRANGE);
1163 	}
1164 	return (count);
1165 }
1166 
1167 
1168 /*
1169  * Derived from cpr_write_statefile().
1170  * Allocate (or reallocate after exhausting the supply) descriptors for each
1171  * chunk of contiguous sensitive kpages.
1172  */
1173 static int
1174 i_cpr_storage_desc_alloc(csd_t **basepp, pgcnt_t *pgsp, csd_t **endpp,
1175     int retry)
1176 {
1177 	pgcnt_t npages;
1178 	int chunks;
1179 	csd_t	*descp, *end;
1180 	size_t	len;
1181 	char *str = "i_cpr_storage_desc_alloc:";
1182 
1183 	/*
1184 	 * On initial allocation, add some extra to cover overhead caused
1185 	 * by the allocation for the storage area later.
1186 	 */
1187 	if (retry == 0) {
1188 		chunks = cpr_contig_pages(NULL, STORAGE_DESC_ALLOC) +
1189 		    EXTRA_DESCS;
1190 		npages = mmu_btopr(sizeof (**basepp) * (pgcnt_t)chunks);
1191 		CPR_DEBUG(CPR_DEBUG7, "%s chunks %d, ", str, chunks);
1192 	} else {
1193 		CPR_DEBUG(CPR_DEBUG7, "%s retry %d: ", str, retry);
1194 		npages = *pgsp + 1;
1195 	}
1196 	/* Free old descriptors, if any */
1197 	if (*basepp)
1198 		kmem_free((caddr_t)*basepp, mmu_ptob(*pgsp));
1199 
1200 	descp = *basepp = kmem_alloc(mmu_ptob(npages), KM_NOSLEEP);
1201 	if (descp == NULL) {
1202 		CPR_DEBUG(CPR_DEBUG7, "%s no space for descriptors!\n", str);
1203 		return (ENOMEM);
1204 	}
1205 
1206 	*pgsp = npages;
1207 	len = mmu_ptob(npages);
1208 	end = *endpp = descp + (len / (sizeof (**basepp)));
1209 	CPR_DEBUG(CPR_DEBUG7, "npages 0x%lx, len 0x%lx, items 0x%lx\n\t*basepp "
1210 	    "%p, *endpp %p\n", npages, len, (len / (sizeof (**basepp))),
1211 	    *basepp, *endpp);
1212 	i_cpr_storage_desc_init(descp, npages, end);
1213 	return (0);
1214 }
1215 
1216 static void
1217 i_cpr_storage_desc_init(csd_t *descp, pgcnt_t npages, csd_t *end)
1218 {
1219 	size_t	len = mmu_ptob(npages);
1220 
1221 	/* Initialize the descriptors to something impossible. */
1222 	bzero(descp, len);
1223 #ifdef	DEBUG
1224 	/*
1225 	 * This condition is tested by an ASSERT
1226 	 */
1227 	for (; descp < end; descp++)
1228 		descp->csd_dirty_spfn = (uint_t)-1;
1229 #endif
1230 }
1231 
1232 int
1233 i_cpr_dump_sensitive_kpages(vnode_t *vp)
1234 {
1235 	int	error = 0;
1236 	uint_t	spin_cnt = 0;
1237 	csd_t	*descp;
1238 
1239 	/*
1240 	 * These following two variables need to be reinitialized
1241 	 * for each cpr cycle.
1242 	 */
1243 	i_cpr_sensitive_bytes_dumped = 0;
1244 	i_cpr_sensitive_pgs_dumped = 0;
1245 
1246 	if (i_cpr_storage_desc_base) {
1247 		for (descp = i_cpr_storage_desc_base;
1248 		    descp <= i_cpr_storage_desc_last_used; descp++) {
1249 			if (error = cpr_dump_sensitive(vp, descp))
1250 				return (error);
1251 			spin_cnt++;
1252 			if ((spin_cnt & 0x5F) == 1)
1253 				cpr_spinning_bar();
1254 		}
1255 		prom_printf(" \b");
1256 	}
1257 
1258 	CPR_DEBUG(CPR_DEBUG7, "\ni_cpr_dump_sensitive_kpages: dumped %ld\n",
1259 	    i_cpr_sensitive_pgs_dumped);
1260 	return (0);
1261 }
1262 
1263 
1264 /*
1265  * 1. Fill the cpr page descriptor with the info of the dirty pages
1266  *    and
1267  *    write the descriptor out. It will be used at resume.
1268  * 2. Write the clean data in stead of the dirty data out.
1269  *    Note: to save space, the clean data is already compressed.
1270  */
1271 static int
1272 cpr_dump_sensitive(vnode_t *vp, csd_t *descp)
1273 {
1274 	int error = 0;
1275 	caddr_t datap;
1276 	cpd_t cpd;	/* cpr page descriptor */
1277 	pfn_t	dirty_spfn;
1278 	pgcnt_t dirty_npages;
1279 	size_t clean_sz;
1280 	caddr_t	clean_sva;
1281 	int	clean_compressed;
1282 	extern uchar_t cpr_pagecopy[];
1283 
1284 	dirty_spfn = descp->csd_dirty_spfn;
1285 	dirty_npages = descp->csd_dirty_npages;
1286 	clean_sva = (caddr_t)descp->csd_clean_sva;
1287 	clean_sz = descp->csd_clean_sz;
1288 	clean_compressed = descp->csd_clean_compressed;
1289 
1290 	/* Fill cpr page descriptor. */
1291 	cpd.cpd_magic = (uint_t)CPR_PAGE_MAGIC;
1292 	cpd.cpd_pfn = dirty_spfn;
1293 	cpd.cpd_flag = 0;  /* must init to zero */
1294 	cpd.cpd_pages = dirty_npages;
1295 
1296 #ifdef	DEBUG
1297 	if ((cpd.cpd_usum = descp->csd_usum) != 0)
1298 		cpd.cpd_flag |= CPD_USUM;
1299 	if ((cpd.cpd_csum = descp->csd_csum) != 0)
1300 		cpd.cpd_flag |= CPD_CSUM;
1301 #endif
1302 
1303 	STAT->cs_dumped_statefsz += mmu_ptob(dirty_npages);
1304 
1305 	/*
1306 	 * The sensitive kpages are usually saved with compression
1307 	 * unless compression could not reduce the size of the data.
1308 	 * If user choose not to have the statefile compressed,
1309 	 * we need to decompress the data back before dumping it to disk.
1310 	 */
1311 	if (CPR->c_flags & C_COMPRESSING) {
1312 		cpd.cpd_length = clean_sz;
1313 		datap = clean_sva;
1314 		if (clean_compressed)
1315 			cpd.cpd_flag |= CPD_COMPRESS;
1316 	} else {
1317 		if (clean_compressed) {
1318 			cpd.cpd_length = decompress(clean_sva, cpr_pagecopy,
1319 			    clean_sz, mmu_ptob(dirty_npages));
1320 			datap = (caddr_t)cpr_pagecopy;
1321 			ASSERT(cpd.cpd_length == mmu_ptob(dirty_npages));
1322 		} else {
1323 			cpd.cpd_length = clean_sz;
1324 			datap = clean_sva;
1325 		}
1326 		cpd.cpd_csum = 0;
1327 	}
1328 
1329 	/* Write cpr page descriptor */
1330 	error = cpr_write(vp, (caddr_t)&cpd, sizeof (cpd));
1331 	if (error) {
1332 		CPR_DEBUG(CPR_DEBUG7, "descp: %p\n", descp);
1333 #ifdef DEBUG
1334 		debug_enter("cpr_dump_sensitive: cpr_write() page "
1335 			"descriptor failed!\n");
1336 #endif
1337 		return (error);
1338 	}
1339 
1340 	i_cpr_sensitive_bytes_dumped += sizeof (cpd_t);
1341 
1342 	/* Write page data */
1343 	error = cpr_write(vp, (caddr_t)datap, cpd.cpd_length);
1344 	if (error) {
1345 		CPR_DEBUG(CPR_DEBUG7, "error: %x\n", error);
1346 		CPR_DEBUG(CPR_DEBUG7, "descp: %p\n", descp);
1347 		CPR_DEBUG(CPR_DEBUG7, "cpr_write(%p, %p , %lx)\n", vp, datap,
1348 			cpd.cpd_length);
1349 #ifdef DEBUG
1350 		debug_enter("cpr_dump_sensitive: cpr_write() data failed!\n");
1351 #endif
1352 		return (error);
1353 	}
1354 
1355 	i_cpr_sensitive_bytes_dumped += cpd.cpd_length;
1356 	i_cpr_sensitive_pgs_dumped += dirty_npages;
1357 
1358 	return (error);
1359 }
1360 
1361 
1362 /*
1363  * Sanity check to make sure that we have dumped right amount
1364  * of pages from different sources to statefile.
1365  */
1366 int
1367 i_cpr_check_pgs_dumped(uint_t pgs_expected, uint_t regular_pgs_dumped)
1368 {
1369 	uint_t total_pgs_dumped;
1370 
1371 	total_pgs_dumped = regular_pgs_dumped + i_cpr_sensitive_pgs_dumped;
1372 
1373 	CPR_DEBUG(CPR_DEBUG7, "\ncheck_pgs: reg %d + sens %ld = %d, "
1374 	    "expect %d\n\n", regular_pgs_dumped, i_cpr_sensitive_pgs_dumped,
1375 	    total_pgs_dumped, pgs_expected);
1376 
1377 	if (pgs_expected == total_pgs_dumped)
1378 		return (0);
1379 
1380 	return (EINVAL);
1381 }
1382 
1383 
1384 int
1385 i_cpr_reusefini(void)
1386 {
1387 	struct vnode *vp;
1388 	cdef_t *cdef;
1389 	size_t size;
1390 	char *bufp;
1391 	int rc;
1392 
1393 	if (cpr_reusable_mode)
1394 		cpr_reusable_mode = 0;
1395 
1396 	if (rc = cpr_open_deffile(FREAD|FWRITE, &vp)) {
1397 		if (rc == EROFS) {
1398 			cpr_err(CE_CONT, "uadmin A_FREEZE AD_REUSEFINI "
1399 			    "(uadmin %d %d)\nmust be done with / mounted "
1400 			    "writeable.\n", A_FREEZE, AD_REUSEFINI);
1401 		}
1402 		return (rc);
1403 	}
1404 
1405 	cdef = kmem_alloc(sizeof (*cdef), KM_SLEEP);
1406 	rc = cpr_rdwr(UIO_READ, vp, cdef, sizeof (*cdef));
1407 
1408 	if (rc) {
1409 		cpr_err(CE_WARN, "Failed reading %s, errno = %d",
1410 		    cpr_default_path, rc);
1411 	} else if (cdef->mini.magic != CPR_DEFAULT_MAGIC) {
1412 		cpr_err(CE_WARN, "bad magic number in %s, cannot restore "
1413 		    "prom values for %s", cpr_default_path,
1414 		    cpr_enumerate_promprops(&bufp, &size));
1415 		kmem_free(bufp, size);
1416 		rc = EINVAL;
1417 	} else {
1418 		/*
1419 		 * clean up prom properties
1420 		 */
1421 		rc = cpr_update_nvram(cdef->props);
1422 		if (rc == 0) {
1423 			/*
1424 			 * invalidate the disk copy and turn off reusable
1425 			 */
1426 			cdef->mini.magic = 0;
1427 			cdef->mini.reusable = 0;
1428 			if (rc = cpr_rdwr(UIO_WRITE, vp,
1429 			    &cdef->mini, sizeof (cdef->mini))) {
1430 				cpr_err(CE_WARN, "Failed writing %s, errno %d",
1431 				    cpr_default_path, rc);
1432 			}
1433 		}
1434 	}
1435 
1436 	(void) VOP_CLOSE(vp, FREAD|FWRITE, 1, (offset_t)0, CRED());
1437 	VN_RELE(vp);
1438 	kmem_free(cdef, sizeof (*cdef));
1439 
1440 	return (rc);
1441 }
1442 
1443 
1444 int
1445 i_cpr_reuseinit(void)
1446 {
1447 	int rc = 0;
1448 
1449 	if (rc = cpr_default_setup(1))
1450 		return (rc);
1451 
1452 	/*
1453 	 * We need to validate default file
1454 	 */
1455 	rc = cpr_validate_definfo(1);
1456 	if (rc == 0)
1457 		cpr_reusable_mode = 1;
1458 	else if (rc == EROFS) {
1459 		cpr_err(CE_NOTE, "reuseinit must be performed "
1460 		    "while / is mounted writeable");
1461 	}
1462 
1463 	(void) cpr_default_setup(0);
1464 
1465 	return (rc);
1466 }
1467 
1468 
1469 int
1470 i_cpr_check_cprinfo(void)
1471 {
1472 	struct vnode *vp;
1473 	cmini_t mini;
1474 	int rc = 0;
1475 
1476 	if (rc = cpr_open_deffile(FREAD, &vp)) {
1477 		if (rc == ENOENT)
1478 			cpr_err(CE_NOTE, "cprinfo file does not "
1479 			    "exist.  You must run 'uadmin %d %d' "
1480 			    "command while / is mounted writeable,\n"
1481 			    "then reboot and run 'uadmin %d %d' "
1482 			    "to create a reusable statefile",
1483 			    A_FREEZE, AD_REUSEINIT, A_FREEZE, AD_REUSABLE);
1484 		return (rc);
1485 	}
1486 
1487 	rc = cpr_rdwr(UIO_READ, vp, &mini, sizeof (mini));
1488 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED());
1489 	VN_RELE(vp);
1490 
1491 	if (rc) {
1492 		cpr_err(CE_WARN, "Failed reading %s, errno = %d",
1493 		    cpr_default_path, rc);
1494 	} else if (mini.magic != CPR_DEFAULT_MAGIC) {
1495 		cpr_err(CE_CONT, "bad magic number in cprinfo file.\n"
1496 		    "You must run 'uadmin %d %d' while / is mounted "
1497 		    "writeable, then reboot and run 'uadmin %d %d' "
1498 		    "to create a reusable statefile\n",
1499 		    A_FREEZE, AD_REUSEINIT, A_FREEZE, AD_REUSABLE);
1500 		rc = EINVAL;
1501 	}
1502 
1503 	return (rc);
1504 }
1505 
1506 
1507 int
1508 i_cpr_reusable_supported(void)
1509 {
1510 	return (1);
1511 }
1512 
1513 
1514 /*
1515  * find prom phys pages and alloc space for a tmp copy
1516  */
1517 static int
1518 i_cpr_find_ppages(void)
1519 {
1520 	extern struct vnode prom_ppages;
1521 	struct page *pp;
1522 	struct memlist *pmem;
1523 	pgcnt_t npages, pcnt, scnt, vcnt;
1524 	pfn_t ppn, plast, *dst;
1525 	int mapflag;
1526 
1527 	cpr_clear_bitmaps();
1528 	mapflag = REGULAR_BITMAP;
1529 
1530 	/*
1531 	 * there should be a page_t for each phys page used by the kernel;
1532 	 * set a bit for each phys page not tracked by a page_t
1533 	 */
1534 	pcnt = 0;
1535 	memlist_read_lock();
1536 	for (pmem = phys_install; pmem; pmem = pmem->next) {
1537 		npages = mmu_btop(pmem->size);
1538 		ppn = mmu_btop(pmem->address);
1539 		for (plast = ppn + npages; ppn < plast; ppn++) {
1540 			if (page_numtopp_nolock(ppn))
1541 				continue;
1542 			(void) cpr_setbit(ppn, mapflag);
1543 			pcnt++;
1544 		}
1545 	}
1546 	memlist_read_unlock();
1547 
1548 	/*
1549 	 * clear bits for phys pages in each segment
1550 	 */
1551 	scnt = cpr_count_seg_pages(mapflag, cpr_clrbit);
1552 
1553 	/*
1554 	 * set bits for phys pages referenced by the prom_ppages vnode;
1555 	 * these pages are mostly comprised of forthdebug words
1556 	 */
1557 	vcnt = 0;
1558 	for (pp = prom_ppages.v_pages; pp; ) {
1559 		if (cpr_setbit(pp->p_offset, mapflag) == 0)
1560 			vcnt++;
1561 		pp = pp->p_vpnext;
1562 		if (pp == prom_ppages.v_pages)
1563 			break;
1564 	}
1565 
1566 	/*
1567 	 * total number of prom pages are:
1568 	 * (non-page_t pages - seg pages + vnode pages)
1569 	 */
1570 	ppage_count = pcnt - scnt + vcnt;
1571 	CPR_DEBUG(CPR_DEBUG1,
1572 	    "find_ppages: pcnt %ld - scnt %ld + vcnt %ld = %ld\n",
1573 	    pcnt, scnt, vcnt, ppage_count);
1574 
1575 	/*
1576 	 * alloc array of pfn_t to store phys page list
1577 	 */
1578 	pphys_list_size = ppage_count * sizeof (pfn_t);
1579 	pphys_list = kmem_alloc(pphys_list_size, KM_NOSLEEP);
1580 	if (pphys_list == NULL) {
1581 		cpr_err(CE_WARN, "cannot alloc pphys_list");
1582 		return (ENOMEM);
1583 	}
1584 
1585 	/*
1586 	 * phys pages referenced in the bitmap should be
1587 	 * those used by the prom; scan bitmap and save
1588 	 * a list of prom phys page numbers
1589 	 */
1590 	dst = pphys_list;
1591 	memlist_read_lock();
1592 	for (pmem = phys_install; pmem; pmem = pmem->next) {
1593 		npages = mmu_btop(pmem->size);
1594 		ppn = mmu_btop(pmem->address);
1595 		for (plast = ppn + npages; ppn < plast; ppn++) {
1596 			if (cpr_isset(ppn, mapflag)) {
1597 				ASSERT(dst < (pphys_list + ppage_count));
1598 				*dst++ = ppn;
1599 			}
1600 		}
1601 	}
1602 	memlist_read_unlock();
1603 
1604 	/*
1605 	 * allocate space to store prom pages
1606 	 */
1607 	ppage_buf = kmem_alloc(mmu_ptob(ppage_count), KM_NOSLEEP);
1608 	if (ppage_buf == NULL) {
1609 		kmem_free(pphys_list, pphys_list_size);
1610 		pphys_list = NULL;
1611 		cpr_err(CE_WARN, "cannot alloc ppage_buf");
1612 		return (ENOMEM);
1613 	}
1614 
1615 	return (0);
1616 }
1617 
1618 
1619 /*
1620  * save prom pages to kmem pages
1621  */
1622 static void
1623 i_cpr_save_ppages(void)
1624 {
1625 	pfn_t *pphys, *plast;
1626 	caddr_t dst;
1627 
1628 	/*
1629 	 * map in each prom page and copy to a kmem page
1630 	 */
1631 	dst = ppage_buf;
1632 	plast = pphys_list + ppage_count;
1633 	for (pphys = pphys_list; pphys < plast; pphys++) {
1634 		i_cpr_mapin(cpr_vaddr, 1, *pphys);
1635 		bcopy(cpr_vaddr, dst, MMU_PAGESIZE);
1636 		i_cpr_mapout(cpr_vaddr, 1);
1637 		dst += MMU_PAGESIZE;
1638 	}
1639 
1640 	CPR_DEBUG(CPR_DEBUG1, "saved %ld prom pages\n", ppage_count);
1641 }
1642 
1643 
1644 /*
1645  * restore prom pages from kmem pages
1646  */
1647 static void
1648 i_cpr_restore_ppages(void)
1649 {
1650 	pfn_t *pphys, *plast;
1651 	caddr_t src;
1652 
1653 	dcache_flushall();
1654 
1655 	/*
1656 	 * map in each prom page and copy from a kmem page
1657 	 */
1658 	src = ppage_buf;
1659 	plast = pphys_list + ppage_count;
1660 	for (pphys = pphys_list; pphys < plast; pphys++) {
1661 		i_cpr_mapin(cpr_vaddr, 1, *pphys);
1662 		bcopy(src, cpr_vaddr, MMU_PAGESIZE);
1663 		i_cpr_mapout(cpr_vaddr, 1);
1664 		src += MMU_PAGESIZE;
1665 	}
1666 
1667 	dcache_flushall();
1668 
1669 	CPR_DEBUG(CPR_DEBUG1, "restored %ld prom pages\n", ppage_count);
1670 }
1671 
1672 
1673 /*
1674  * save/restore prom pages or free related allocs
1675  */
1676 int
1677 i_cpr_prom_pages(int action)
1678 {
1679 	int error;
1680 
1681 	if (action == CPR_PROM_SAVE) {
1682 		if (ppage_buf == NULL) {
1683 			ASSERT(pphys_list == NULL);
1684 			if (error = i_cpr_find_ppages())
1685 				return (error);
1686 			i_cpr_save_ppages();
1687 		}
1688 	} else if (action == CPR_PROM_RESTORE) {
1689 		i_cpr_restore_ppages();
1690 	} else if (action == CPR_PROM_FREE) {
1691 		if (pphys_list) {
1692 			ASSERT(pphys_list_size);
1693 			kmem_free(pphys_list, pphys_list_size);
1694 			pphys_list = NULL;
1695 			pphys_list_size = 0;
1696 		}
1697 		if (ppage_buf) {
1698 			ASSERT(ppage_count);
1699 			kmem_free(ppage_buf, mmu_ptob(ppage_count));
1700 			CPR_DEBUG(CPR_DEBUG1, "freed %ld prom pages\n",
1701 			    ppage_count);
1702 			ppage_buf = NULL;
1703 			ppage_count = 0;
1704 		}
1705 	}
1706 	return (0);
1707 }
1708 
1709 
1710 /*
1711  * record tlb data for the nucleus, bigktsb's, and the cpr module;
1712  * this data is later used by cprboot to install dtlb/itlb entries.
1713  * when we jump into the cpr module during the resume phase, those
1714  * mappings are needed until switching to the kernel trap table.
1715  * to make the dtte/itte info available during resume, we need
1716  * the info recorded prior to saving sensitive pages, otherwise
1717  * all the data would appear as NULLs.
1718  */
1719 static void
1720 i_cpr_save_tlbinfo(void)
1721 {
1722 	cti_t cti = {0};
1723 
1724 	/*
1725 	 * during resume - shortly after jumping into the cpr module,
1726 	 * sfmmu_load_mmustate() will overwrite any dtlb entry at any
1727 	 * index used for TSBs; skip is set so that any saved tte will
1728 	 * target other tlb offsets and prevent being lost during
1729 	 * resume.  now scan the dtlb and save locked entries,
1730 	 * then add entries for the tmp stack / data page and the
1731 	 * cpr thread structure.
1732 	 */
1733 	cti.dst = m_info.dtte;
1734 	cti.tail = cti.dst + CPR_MAX_TLB;
1735 	cti.reader = dtlb_rd_entry;
1736 	cti.writer = NULL;
1737 	cti.filter = i_cpr_lnb;
1738 	cti.index = cpunodes[CPU->cpu_id].dtlb_size - 1;
1739 
1740 	if (utsb_dtlb_ttenum != -1)
1741 		cti.skip = (1 << utsb_dtlb_ttenum);
1742 
1743 	if (utsb4m_dtlb_ttenum != -1)
1744 		cti.skip |= (1 << utsb4m_dtlb_ttenum);
1745 
1746 	i_cpr_scan_tlb(&cti);
1747 	i_cpr_make_tte(&cti, &i_cpr_data_page, datava);
1748 	i_cpr_make_tte(&cti, curthread, datava);
1749 
1750 	/*
1751 	 * scan itlb and save locked entries; add an entry for
1752 	 * the first text page of the cpr module; cprboot will
1753 	 * jump to that page after restoring kernel pages.
1754 	 */
1755 	cti.dst = m_info.itte;
1756 	cti.tail = cti.dst + CPR_MAX_TLB;
1757 	cti.reader = itlb_rd_entry;
1758 	cti.index = cpunodes[CPU->cpu_id].itlb_size - 1;
1759 	cti.skip = 0;
1760 	i_cpr_scan_tlb(&cti);
1761 	i_cpr_make_tte(&cti, (void *)i_cpr_resume_setup, textva);
1762 }
1763 
1764 
1765 /* ARGSUSED */
1766 int
1767 i_cpr_dump_setup(vnode_t *vp)
1768 {
1769 	/*
1770 	 * zero out m_info and add info to dtte/itte arrays
1771 	 */
1772 	bzero(&m_info, sizeof (m_info));
1773 	i_cpr_save_tlbinfo();
1774 	return (0);
1775 }
1776 
1777 
1778 int
1779 i_cpr_is_supported(void)
1780 {
1781 	char es_prop[] = "energystar-v2";
1782 	pnode_t node;
1783 	int last;
1784 	extern int cpr_supported_override;
1785 	extern int cpr_platform_enable;
1786 
1787 	/*
1788 	 * The next statement tests if a specific platform has turned off
1789 	 * cpr support.
1790 	 */
1791 	if (cpr_supported_override)
1792 		return (0);
1793 
1794 	/*
1795 	 * Do not inspect energystar-v* property if a platform has
1796 	 * specifically turned on cpr support
1797 	 */
1798 	if (cpr_platform_enable)
1799 		return (1);
1800 
1801 	node = prom_rootnode();
1802 	if (prom_getproplen(node, es_prop) != -1)
1803 		return (1);
1804 	last = strlen(es_prop) - 1;
1805 	es_prop[last] = '3';
1806 	return (prom_getproplen(node, es_prop) != -1);
1807 }
1808 
1809 
1810 /*
1811  * the actual size of the statefile data isn't known until after all the
1812  * compressed pages are written; even the inode size doesn't reflect the
1813  * data size since there are usually many extra fs blocks.  for recording
1814  * the actual data size, the first sector of the statefile is copied to
1815  * a tmp buf, and the copy is later updated and flushed to disk.
1816  */
1817 int
1818 i_cpr_blockzero(char *base, char **bufpp, int *blkno, vnode_t *vp)
1819 {
1820 	extern int cpr_flush_write(vnode_t *);
1821 	static char cpr_sector[DEV_BSIZE];
1822 	cpr_ext bytes, *dst;
1823 
1824 	/*
1825 	 * this routine is called after cdd_t and csu_md_t are copied
1826 	 * to cpr_buf; mini-hack alert: the save/update method creates
1827 	 * a dependency on the combined struct size being >= one sector
1828 	 * or DEV_BSIZE; since introduction in Sol2.7, csu_md_t size is
1829 	 * over 1K bytes and will probably grow with any changes.
1830 	 *
1831 	 * copy when vp is NULL, flush when non-NULL
1832 	 */
1833 	if (vp == NULL) {
1834 		ASSERT((*bufpp - base) >= DEV_BSIZE);
1835 		bcopy(base, cpr_sector, sizeof (cpr_sector));
1836 		return (0);
1837 	} else {
1838 		bytes = dbtob(*blkno);
1839 		dst = &((cdd_t *)cpr_sector)->cdd_filesize;
1840 		bcopy(&bytes, dst, sizeof (bytes));
1841 		bcopy(cpr_sector, base, sizeof (cpr_sector));
1842 		*bufpp = base + sizeof (cpr_sector);
1843 		*blkno = cpr_statefile_offset();
1844 		CPR_DEBUG(CPR_DEBUG1, "statefile data size: %ld\n\n", bytes);
1845 		return (cpr_flush_write(vp));
1846 	}
1847 }
1848 
1849 
1850 /*
1851  * Allocate bitmaps according to the phys_install list.
1852  */
1853 static int
1854 i_cpr_bitmap_setup(void)
1855 {
1856 	struct memlist *pmem;
1857 	cbd_t *dp, *tail;
1858 	void *space;
1859 	size_t size;
1860 
1861 	/*
1862 	 * The number of bitmap descriptors will be the count of
1863 	 * phys_install ranges plus 1 for a trailing NULL struct.
1864 	 */
1865 	cpr_nbitmaps = 1;
1866 	for (pmem = phys_install; pmem; pmem = pmem->next)
1867 		cpr_nbitmaps++;
1868 
1869 	if (cpr_nbitmaps > (CPR_MAX_BMDESC - 1)) {
1870 		cpr_err(CE_WARN, "too many physical memory ranges %d, max %d",
1871 		    cpr_nbitmaps, CPR_MAX_BMDESC - 1);
1872 		return (EFBIG);
1873 	}
1874 
1875 	/* Alloc an array of bitmap descriptors. */
1876 	dp = kmem_zalloc(cpr_nbitmaps * sizeof (*dp), KM_NOSLEEP);
1877 	if (dp == NULL) {
1878 		cpr_nbitmaps = 0;
1879 		return (ENOMEM);
1880 	}
1881 	tail = dp + cpr_nbitmaps;
1882 
1883 	CPR->c_bmda = dp;
1884 	for (pmem = phys_install; pmem; pmem = pmem->next) {
1885 		size = BITMAP_BYTES(pmem->size);
1886 		space = kmem_zalloc(size * 2, KM_NOSLEEP);
1887 		if (space == NULL)
1888 			return (ENOMEM);
1889 		ASSERT(dp < tail);
1890 		dp->cbd_magic = CPR_BITMAP_MAGIC;
1891 		dp->cbd_spfn = mmu_btop(pmem->address);
1892 		dp->cbd_epfn = mmu_btop(pmem->address + pmem->size) - 1;
1893 		dp->cbd_size = size;
1894 		dp->cbd_reg_bitmap = (cpr_ptr)space;
1895 		dp->cbd_vlt_bitmap = (cpr_ptr)((caddr_t)space + size);
1896 		dp++;
1897 	}
1898 
1899 	/* set magic for the last descriptor */
1900 	ASSERT(dp == (tail - 1));
1901 	dp->cbd_magic = CPR_BITMAP_MAGIC;
1902 
1903 	return (0);
1904 }
1905 
1906 
1907 void
1908 i_cpr_bitmap_cleanup(void)
1909 {
1910 	cbd_t *dp;
1911 
1912 	if (CPR->c_bmda == NULL)
1913 		return;
1914 	for (dp = CPR->c_bmda; dp->cbd_size; dp++)
1915 		kmem_free((void *)dp->cbd_reg_bitmap, dp->cbd_size * 2);
1916 	kmem_free(CPR->c_bmda, cpr_nbitmaps * sizeof (*CPR->c_bmda));
1917 	CPR->c_bmda = NULL;
1918 	cpr_nbitmaps = 0;
1919 }
1920 
1921 
1922 /*
1923  * A "regular" and "volatile" bitmap are created for each range of
1924  * physical memory.  The volatile maps are used to count and track pages
1925  * susceptible to heap corruption - caused by drivers that allocate mem
1926  * during VOP_DUMP(); the regular maps are used for all the other non-
1927  * susceptible pages.  Before writing the bitmaps to the statefile,
1928  * each bitmap pair gets merged to simplify handling within cprboot.
1929  */
1930 int
1931 i_cpr_alloc_bitmaps(void)
1932 {
1933 	int err;
1934 
1935 	memlist_read_lock();
1936 	err = i_cpr_bitmap_setup();
1937 	memlist_read_unlock();
1938 	if (err)
1939 		i_cpr_bitmap_cleanup();
1940 	return (err);
1941 }
1942