xref: /illumos-gate/usr/src/uts/common/cpr/cpr_main.c (revision bea83d026ee1bd1b2a2419e1d0232f107a5d7d9b)
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 2008 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  * This module contains the guts of checkpoint-resume mechanism.
30  * All code in this module is platform independent.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/callb.h>
36 #include <sys/processor.h>
37 #include <sys/machsystm.h>
38 #include <sys/clock.h>
39 #include <sys/vfs.h>
40 #include <sys/kmem.h>
41 #include <nfs/lm.h>
42 #include <sys/systm.h>
43 #include <sys/cpr.h>
44 #include <sys/bootconf.h>
45 #include <sys/cyclic.h>
46 #include <sys/filio.h>
47 #include <sys/fs/ufs_filio.h>
48 #include <sys/epm.h>
49 #include <sys/modctl.h>
50 #include <sys/reboot.h>
51 #include <sys/kdi.h>
52 #include <sys/promif.h>
53 #include <sys/srn.h>
54 #include <sys/cpr_impl.h>
55 
56 #define	PPM(dip) ((dev_info_t *)DEVI(dip)->devi_pm_ppm)
57 
58 extern struct cpr_terminator cpr_term;
59 
60 extern int cpr_alloc_statefile(int);
61 extern void cpr_start_kernel_threads(void);
62 extern void cpr_abbreviate_devpath(char *, char *);
63 extern void cpr_convert_promtime(cpr_time_t *);
64 extern void cpr_send_notice(void);
65 extern void cpr_set_bitmap_size(void);
66 extern void cpr_stat_init();
67 extern void cpr_statef_close(void);
68 extern void flush_windows(void);
69 extern void (*srn_signal)(int, int);
70 extern void init_cpu_syscall(struct cpu *);
71 extern void i_cpr_pre_resume_cpus();
72 extern void i_cpr_post_resume_cpus();
73 
74 extern int pm_powering_down;
75 extern kmutex_t srn_clone_lock;
76 extern int srn_inuse;
77 
78 static int cpr_suspend(int);
79 static int cpr_resume(int);
80 static void cpr_suspend_init(int);
81 #if defined(__x86)
82 static int cpr_suspend_cpus(void);
83 static void cpr_resume_cpus(void);
84 #endif
85 static int cpr_all_online(void);
86 static void cpr_restore_offline(void);
87 
88 cpr_time_t wholecycle_tv;
89 int cpr_suspend_succeeded;
90 pfn_t curthreadpfn;
91 int curthreadremapped;
92 
93 extern cpuset_t cpu_ready_set;
94 extern void *(*cpu_pause_func)(void *);
95 
96 extern processorid_t i_cpr_bootcpuid(void);
97 extern cpu_t *i_cpr_bootcpu(void);
98 extern void tsc_adjust_delta(hrtime_t tdelta);
99 extern void tsc_resume(void);
100 extern int tsc_resume_in_cyclic;
101 
102 /*
103  * Set this variable to 1, to have device drivers resume in an
104  * uniprocessor environment. This is to allow drivers that assume
105  * that they resume on a UP machine to continue to work. Should be
106  * deprecated once the broken drivers are fixed
107  */
108 int cpr_resume_uniproc = 0;
109 
110 /*
111  * save or restore abort_enable;  this prevents a drop
112  * to kadb or prom during cpr_resume_devices() when
113  * there is no kbd present;  see abort_sequence_enter()
114  */
115 static void
116 cpr_sae(int stash)
117 {
118 	static int saved_ae = -1;
119 
120 	if (stash) {
121 		saved_ae = abort_enable;
122 		abort_enable = 0;
123 	} else if (saved_ae != -1) {
124 		abort_enable = saved_ae;
125 		saved_ae = -1;
126 	}
127 }
128 
129 
130 /*
131  * The main switching point for cpr, this routine starts the ckpt
132  * and state file saving routines; on resume the control is
133  * returned back to here and it then calls the resume routine.
134  */
135 int
136 cpr_main(int sleeptype)
137 {
138 	int rc, rc2;
139 	label_t saveq;
140 	klwp_t *tlwp = ttolwp(curthread);
141 
142 	if (sleeptype == CPR_TODISK) {
143 		if ((rc = cpr_default_setup(1)) != 0)
144 			return (rc);
145 		ASSERT(tlwp);
146 		saveq = tlwp->lwp_qsav;
147 	}
148 
149 	if (sleeptype == CPR_TORAM) {
150 		rc = cpr_suspend(sleeptype);
151 		PMD(PMD_SX, ("cpr_suspend rets %x\n", rc))
152 		if (rc == 0) {
153 			int i_cpr_power_down(int sleeptype);
154 
155 			/*
156 			 * From this point on, we should be at a high
157 			 * spl, interrupts disabled, and all but one
158 			 * cpu's paused (effectively UP/single threaded).
159 			 * So this is were we want to put ASSERTS()
160 			 * to let us know otherwise.
161 			 */
162 			ASSERT(cpus_paused());
163 
164 			/*
165 			 * Now do the work of actually putting this
166 			 * machine to sleep!
167 			 */
168 			rc = i_cpr_power_down(sleeptype);
169 			if (rc == 0) {
170 				PMD(PMD_SX, ("back from succssful suspend\n"))
171 			}
172 			/*
173 			 * We do care about the return value from cpr_resume
174 			 * at this point, as it will tell us if one of the
175 			 * resume functions failed (cpr_resume_devices())
176 			 * However, for this to return and _not_ panic, means
177 			 * that we must be in one of the test functions.  So
178 			 * check for that and return an appropriate message.
179 			 */
180 			rc2 = cpr_resume(sleeptype);
181 			if (rc2 != 0) {
182 				ASSERT(cpr_test_point > 0);
183 				cmn_err(CE_NOTE,
184 				    "cpr_resume returned non-zero: %d\n", rc2);
185 				PMD(PMD_SX, ("cpr_resume rets %x\n", rc2))
186 			}
187 			ASSERT(!cpus_paused());
188 		} else {
189 			PMD(PMD_SX, ("failed suspend, resuming\n"))
190 			rc = cpr_resume(sleeptype);
191 		}
192 		return (rc);
193 	}
194 	/*
195 	 * Remember where we are for resume after reboot
196 	 */
197 	if (!setjmp(&tlwp->lwp_qsav)) {
198 		/*
199 		 * try to checkpoint the system, if failed return back
200 		 * to userland, otherwise power off.
201 		 */
202 		rc = cpr_suspend(sleeptype);
203 		if (rc || cpr_reusable_mode) {
204 			/*
205 			 * We don't really want to go down, or
206 			 * something went wrong in suspend, do what we can
207 			 * to put the system back to an operable state then
208 			 * return back to userland.
209 			 */
210 			PMD(PMD_SX, ("failed suspend, resuming\n"))
211 			(void) cpr_resume(sleeptype);
212 			PMD(PMD_SX, ("back from failed suspend resume\n"))
213 		}
214 	} else {
215 		/*
216 		 * This is the resumed side of longjmp, restore the previous
217 		 * longjmp pointer if there is one so this will be transparent
218 		 * to the world.
219 		 * This path is only for CPR_TODISK, where we reboot
220 		 */
221 		ASSERT(sleeptype == CPR_TODISK);
222 		tlwp->lwp_qsav = saveq;
223 		CPR->c_flags &= ~C_SUSPENDING;
224 		CPR->c_flags |= C_RESUMING;
225 
226 		/*
227 		 * resume the system back to the original state
228 		 */
229 		rc = cpr_resume(sleeptype);
230 		PMD(PMD_SX, ("back from successful suspend; resume rets %x\n",
231 		    rc))
232 	}
233 
234 	(void) cpr_default_setup(0);
235 
236 	return (rc);
237 }
238 
239 
240 #if defined(__sparc)
241 
242 /*
243  * check/disable or re-enable UFS logging
244  */
245 static void
246 cpr_log_status(int enable, int *svstat, vnode_t *vp)
247 {
248 	int cmd, status, error;
249 	char *str, *able;
250 	fiolog_t fl;
251 	refstr_t *mntpt;
252 
253 	str = "cpr_log_status";
254 	bzero(&fl, sizeof (fl));
255 	fl.error = FIOLOG_ENONE;
256 
257 	/*
258 	 * when disabling, first get and save logging status (0 or 1)
259 	 */
260 	if (enable == 0) {
261 		if (error = VOP_IOCTL(vp, _FIOISLOG,
262 		    (uintptr_t)&status, FKIOCTL, CRED(), NULL, NULL)) {
263 			mntpt = vfs_getmntpoint(vp->v_vfsp);
264 			prom_printf("%s: \"%s\", cant get logging "
265 			    "status, error %d\n", str, refstr_value(mntpt),
266 			    error);
267 			refstr_rele(mntpt);
268 			return;
269 		}
270 		*svstat = status;
271 		if (cpr_debug & CPR_DEBUG5) {
272 			mntpt = vfs_getmntpoint(vp->v_vfsp);
273 			errp("%s: \"%s\", logging status = %d\n",
274 			    str, refstr_value(mntpt), status);
275 			refstr_rele(mntpt);
276 		};
277 
278 		able = "disable";
279 		cmd = _FIOLOGDISABLE;
280 	} else {
281 		able = "enable";
282 		cmd = _FIOLOGENABLE;
283 	}
284 
285 	/*
286 	 * disable or re-enable logging when the saved status is 1
287 	 */
288 	if (*svstat == 1) {
289 		error = VOP_IOCTL(vp, cmd, (uintptr_t)&fl,
290 		    FKIOCTL, CRED(), NULL, NULL);
291 		if (error) {
292 			mntpt = vfs_getmntpoint(vp->v_vfsp);
293 			prom_printf("%s: \"%s\", cant %s logging, error %d\n",
294 			    str, refstr_value(mntpt), able, error);
295 			refstr_rele(mntpt);
296 		} else {
297 			if (cpr_debug & CPR_DEBUG5) {
298 				mntpt = vfs_getmntpoint(vp->v_vfsp);
299 				errp("%s: \"%s\", logging is now %sd\n",
300 				    str, refstr_value(mntpt), able);
301 				refstr_rele(mntpt);
302 			};
303 		}
304 	}
305 
306 	/*
307 	 * when enabling logging, reset the saved status
308 	 * to unknown for next time
309 	 */
310 	if (enable)
311 		*svstat = -1;
312 }
313 
314 /*
315  * enable/disable UFS logging on filesystems containing cpr_default_path
316  * and cpr statefile.  since the statefile can be on any fs, that fs
317  * needs to be handled separately.  this routine and cprboot expect that
318  * CPR_CONFIG and CPR_DEFAULT both reside on the same fs, rootfs.  cprboot
319  * is loaded from the device with rootfs and uses the same device to open
320  * both CPR_CONFIG and CPR_DEFAULT (see common/support.c).  moving either
321  * file outside of rootfs would cause errors during cprboot, plus cpr and
322  * fsck problems with the new fs if logging were enabled.
323  */
324 
325 static int
326 cpr_ufs_logging(int enable)
327 {
328 	static int def_status = -1, sf_status = -1;
329 	struct vfs *vfsp;
330 	char *fname;
331 	vnode_t *vp;
332 	int error;
333 
334 	if (cpr_reusable_mode)
335 		return (0);
336 
337 	if (error = cpr_open_deffile(FREAD, &vp))
338 		return (error);
339 	cpr_log_status(enable, &def_status, vp);
340 	vfsp = vp->v_vfsp;
341 	(void) VOP_CLOSE(vp, FREAD, 1, (offset_t)0, CRED(), NULL);
342 	VN_RELE(vp);
343 
344 	fname = cpr_build_statefile_path();
345 	if (fname == NULL)
346 		return (ENOENT);
347 	if (error = vn_open(fname, UIO_SYSSPACE, FCREAT|FWRITE,
348 	    0600, &vp, CRCREAT, 0)) {
349 		prom_printf("cpr_ufs_logging: cant open/create \"%s\", "
350 		    "error %d\n", fname, error);
351 		return (error);
352 	}
353 
354 	/*
355 	 * check logging status for the statefile if it resides
356 	 * on a different fs and the type is a regular file
357 	 */
358 	if (vp->v_vfsp != vfsp && vp->v_type == VREG)
359 		cpr_log_status(enable, &sf_status, vp);
360 	(void) VOP_CLOSE(vp, FWRITE, 1, (offset_t)0, CRED(), NULL);
361 	VN_RELE(vp);
362 
363 	return (0);
364 }
365 #endif
366 
367 
368 /*
369  * Check if klmmod is loaded and call a lock manager service; if klmmod
370  * is not loaded, the services aren't needed and a call would trigger a
371  * modload, which would block since another thread would never run.
372  */
373 static void
374 cpr_lock_mgr(void (*service)(void))
375 {
376 	if (mod_find_by_filename(NULL, "misc/klmmod") != NULL)
377 		(*service)();
378 }
379 
380 int
381 cpr_suspend_cpus(void)
382 {
383 	int	ret = 0;
384 	extern void *i_cpr_save_context(void *arg);
385 
386 	mutex_enter(&cpu_lock);
387 
388 	/*
389 	 * the machine could not have booted without a bootcpu
390 	 */
391 	ASSERT(i_cpr_bootcpu() != NULL);
392 
393 	/*
394 	 * bring all the offline cpus online
395 	 */
396 	if ((ret = cpr_all_online())) {
397 		mutex_exit(&cpu_lock);
398 		return (ret);
399 	}
400 
401 	/*
402 	 * Set the affinity to be the boot processor
403 	 * This is cleared in either cpr_resume_cpus() or cpr_unpause_cpus()
404 	 */
405 	affinity_set(i_cpr_bootcpuid());
406 
407 	ASSERT(CPU->cpu_id == 0);
408 
409 	PMD(PMD_SX, ("curthread running on bootcpu\n"))
410 
411 	/*
412 	 * pause all other running CPUs and save the CPU state at the sametime
413 	 */
414 	cpu_pause_func = i_cpr_save_context;
415 	pause_cpus(NULL);
416 
417 	mutex_exit(&cpu_lock);
418 
419 	return (0);
420 }
421 
422 /*
423  * Take the system down to a checkpointable state and write
424  * the state file, the following are sequentially executed:
425  *
426  *    - Request all user threads to stop themselves
427  *    - push out and invalidate user pages
428  *    - bring statefile inode incore to prevent a miss later
429  *    - request all daemons to stop
430  *    - check and make sure all threads are stopped
431  *    - sync the file system
432  *    - suspend all devices
433  *    - block intrpts
434  *    - dump system state and memory to state file
435  *    - SPARC code will not be called with CPR_TORAM, caller filters
436  */
437 static int
438 cpr_suspend(int sleeptype)
439 {
440 #if defined(__sparc)
441 	int sf_realloc, nverr;
442 #endif
443 	int	rc = 0;
444 	int	skt_rc = 0;
445 
446 	PMD(PMD_SX, ("cpr_suspend %x\n", sleeptype))
447 	cpr_set_substate(C_ST_SUSPEND_BEGIN);
448 
449 	cpr_suspend_init(sleeptype);
450 
451 	cpr_save_time();
452 
453 	cpr_tod_get(&wholecycle_tv);
454 	CPR_STAT_EVENT_START("Suspend Total");
455 
456 	i_cpr_alloc_cpus();
457 
458 #if defined(__sparc)
459 	ASSERT(sleeptype == CPR_TODISK);
460 	if (!cpr_reusable_mode) {
461 		/*
462 		 * We need to validate default file before fs
463 		 * functionality is disabled.
464 		 */
465 		if (rc = cpr_validate_definfo(0))
466 			return (rc);
467 	}
468 	i_cpr_save_machdep_info();
469 #endif
470 
471 	PMD(PMD_SX, ("cpr_suspend: stop scans\n"))
472 	/* Stop PM scans ASAP */
473 	(void) callb_execute_class(CB_CL_CPR_PM, CB_CODE_CPR_CHKPT);
474 
475 	pm_dispatch_to_dep_thread(PM_DEP_WK_CPR_SUSPEND,
476 	    NULL, NULL, PM_DEP_WAIT, NULL, 0);
477 
478 #if defined(__sparc)
479 	ASSERT(sleeptype == CPR_TODISK);
480 	cpr_set_substate(C_ST_MP_OFFLINE);
481 	if (rc = cpr_mp_offline())
482 		return (rc);
483 #endif
484 	/*
485 	 * Ask Xorg to suspend the frame buffer, and wait for it to happen
486 	 */
487 	mutex_enter(&srn_clone_lock);
488 	if (srn_signal) {
489 		PMD(PMD_SX, ("cpr_suspend: (*srn_signal)(..., "
490 		    "SRN_SUSPEND_REQ)\n"))
491 		srn_inuse = 1;	/* because *(srn_signal) cv_waits */
492 		(*srn_signal)(SRN_TYPE_APM, SRN_SUSPEND_REQ);
493 		srn_inuse = 0;
494 	} else {
495 		PMD(PMD_SX, ("cpr_suspend: srn_signal NULL\n"))
496 	}
497 	mutex_exit(&srn_clone_lock);
498 
499 	/*
500 	 * Ask the user threads to stop by themselves, but
501 	 * if they don't or can't after 3 retries, we give up on CPR.
502 	 * The 3 retry is not a random number because 2 is possible if
503 	 * a thread has been forked before the parent thread is stopped.
504 	 */
505 	CPR_DEBUG(CPR_DEBUG1, "\nstopping user threads...");
506 	CPR_STAT_EVENT_START("  stop users");
507 	cpr_set_substate(C_ST_STOP_USER_THREADS);
508 	PMD(PMD_SX, ("cpr_suspend: stop user threads\n"))
509 	if (rc = cpr_stop_user_threads())
510 		return (rc);
511 	CPR_STAT_EVENT_END("  stop users");
512 	CPR_DEBUG(CPR_DEBUG1, "done\n");
513 
514 	PMD(PMD_SX, ("cpr_suspend: save direct levels\n"))
515 	pm_save_direct_levels();
516 
517 	/*
518 	 * User threads are stopped.  We will start communicating with the
519 	 * user via prom_printf (some debug output may have already happened)
520 	 * so let anybody who cares know about this (bug 4096122)
521 	 */
522 	(void) callb_execute_class(CB_CL_CPR_PROMPRINTF, CB_CODE_CPR_CHKPT);
523 
524 	PMD(PMD_SX, ("cpr_suspend: send notice\n"))
525 #ifndef DEBUG
526 	cpr_send_notice();
527 	if (cpr_debug)
528 		prom_printf("\n");
529 #endif
530 
531 	PMD(PMD_SX, ("cpr_suspend: POST USER callback\n"))
532 	(void) callb_execute_class(CB_CL_CPR_POST_USER, CB_CODE_CPR_CHKPT);
533 
534 	/*
535 	 * Reattach any drivers which originally exported the
536 	 * no-involuntary-power-cycles property.  We need to do this before
537 	 * stopping kernel threads because modload is implemented using
538 	 * a kernel thread.
539 	 */
540 	cpr_set_substate(C_ST_PM_REATTACH_NOINVOL);
541 	PMD(PMD_SX, ("cpr_suspend: reattach noinvol\n"))
542 	if (!pm_reattach_noinvol())
543 		return (ENXIO);
544 
545 #if defined(__sparc)
546 	ASSERT(sleeptype == CPR_TODISK);
547 	/*
548 	 * if ufs logging is enabled, we need to disable before
549 	 * stopping kernel threads so that ufs delete and roll
550 	 * threads can do the work.
551 	 */
552 	cpr_set_substate(C_ST_DISABLE_UFS_LOGGING);
553 	if (rc = cpr_ufs_logging(0))
554 		return (rc);
555 
556 	/*
557 	 * Use sync_all to swap out all user pages and find out how much
558 	 * extra space needed for user pages that don't have back store
559 	 * space left.
560 	 */
561 	CPR_STAT_EVENT_START("  swapout upages");
562 	vfs_sync(SYNC_ALL);
563 	CPR_STAT_EVENT_END("  swapout upages");
564 
565 	cpr_set_bitmap_size();
566 
567 alloc_statefile:
568 	/*
569 	 * If our last state was C_ST_DUMP_NOSPC, we're trying to
570 	 * realloc the statefile, otherwise this is the first attempt.
571 	 */
572 	sf_realloc = (CPR->c_substate == C_ST_DUMP_NOSPC) ? 1 : 0;
573 
574 	CPR_STAT_EVENT_START("  alloc statefile");
575 	cpr_set_substate(C_ST_STATEF_ALLOC);
576 	if (rc = cpr_alloc_statefile(sf_realloc)) {
577 		if (sf_realloc)
578 			errp("realloc failed\n");
579 		return (rc);
580 	}
581 	CPR_STAT_EVENT_END("  alloc statefile");
582 
583 	/*
584 	 * Sync the filesystem to preserve its integrity.
585 	 *
586 	 * This sync is also used to flush out all B_DELWRI buffers
587 	 * (fs cache) which are mapped and neither dirty nor referenced
588 	 * before cpr_invalidate_pages destroys them.
589 	 * fsflush does similar thing.
590 	 */
591 	sync();
592 
593 	/*
594 	 * destroy all clean file mapped kernel pages
595 	 */
596 	CPR_STAT_EVENT_START("  clean pages");
597 	CPR_DEBUG(CPR_DEBUG1, ("cleaning up mapped pages..."));
598 	(void) callb_execute_class(CB_CL_CPR_VM, CB_CODE_CPR_CHKPT);
599 	CPR_DEBUG(CPR_DEBUG1, ("done\n"));
600 	CPR_STAT_EVENT_END("  clean pages");
601 #endif
602 
603 
604 	/*
605 	 * Hooks needed by lock manager prior to suspending.
606 	 * Refer to code for more comments.
607 	 */
608 	PMD(PMD_SX, ("cpr_suspend: lock mgr\n"))
609 	cpr_lock_mgr(lm_cprsuspend);
610 
611 	/*
612 	 * Now suspend all the devices
613 	 */
614 	CPR_STAT_EVENT_START("  stop drivers");
615 	CPR_DEBUG(CPR_DEBUG1, "suspending drivers...");
616 	cpr_set_substate(C_ST_SUSPEND_DEVICES);
617 	pm_powering_down = 1;
618 	PMD(PMD_SX, ("cpr_suspend: suspending devices\n"))
619 	rc = cpr_suspend_devices(ddi_root_node());
620 	pm_powering_down = 0;
621 	if (rc)
622 		return (rc);
623 	CPR_DEBUG(CPR_DEBUG1, "done\n");
624 	CPR_STAT_EVENT_END("  stop drivers");
625 
626 	/*
627 	 * Stop all daemon activities
628 	 */
629 	cpr_set_substate(C_ST_STOP_KERNEL_THREADS);
630 	PMD(PMD_SX, ("cpr_suspend: stopping kernel threads\n"))
631 	if (skt_rc = cpr_stop_kernel_threads())
632 		return (skt_rc);
633 
634 	PMD(PMD_SX, ("cpr_suspend: POST KERNEL callback\n"))
635 	(void) callb_execute_class(CB_CL_CPR_POST_KERNEL, CB_CODE_CPR_CHKPT);
636 
637 	PMD(PMD_SX, ("cpr_suspend: reattach noinvol fini\n"))
638 	pm_reattach_noinvol_fini();
639 
640 	cpr_sae(1);
641 
642 	PMD(PMD_SX, ("cpr_suspend: CPR CALLOUT callback\n"))
643 	(void) callb_execute_class(CB_CL_CPR_CALLOUT, CB_CODE_CPR_CHKPT);
644 
645 	if (sleeptype == CPR_TODISK) {
646 		/*
647 		 * It's safer to do tod_get before we disable all intr.
648 		 */
649 		CPR_STAT_EVENT_START("  write statefile");
650 	}
651 
652 	/*
653 	 * it's time to ignore the outside world, stop the real time
654 	 * clock and disable any further intrpt activity.
655 	 */
656 	PMD(PMD_SX, ("cpr_suspend: handle xc\n"))
657 	i_cpr_handle_xc(1);	/* turn it on to disable xc assertion */
658 
659 	mutex_enter(&cpu_lock);
660 	PMD(PMD_SX, ("cpr_suspend: cyclic suspend\n"))
661 	cyclic_suspend();
662 	mutex_exit(&cpu_lock);
663 
664 	/*
665 	 * Due to the different methods of resuming the system between
666 	 * CPR_TODISK (boot cprboot on SPARC, which reloads kernel image)
667 	 * and CPR_TORAM (restart via reset into existing kernel image)
668 	 * cpus are not suspended and restored in the SPARC case, since it
669 	 * is necessary to restart the cpus and pause them before restoring
670 	 * the OBP image
671 	 */
672 
673 #if defined(__x86)
674 
675 	/* pause aux cpus */
676 	PMD(PMD_SX, ("pause aux cpus\n"))
677 
678 	cpr_set_substate(C_ST_MP_PAUSED);
679 
680 	if ((rc = cpr_suspend_cpus()) != 0)
681 		return (rc);
682 #endif
683 
684 	PMD(PMD_SX, ("cpr_suspend: stop intr\n"))
685 	i_cpr_stop_intr();
686 	CPR_DEBUG(CPR_DEBUG1, "interrupt is stopped\n");
687 
688 	/*
689 	 * Since we will now disable the mechanism that causes prom_printfs
690 	 * to power up (if needed) the console fb/monitor, we assert that
691 	 * it must be up now.
692 	 */
693 	ASSERT(pm_cfb_is_up());
694 	PMD(PMD_SX, ("cpr_suspend: prom suspend prepost\n"))
695 	prom_suspend_prepost();
696 
697 #if defined(__sparc)
698 	/*
699 	 * getting ready to write ourself out, flush the register
700 	 * windows to make sure that our stack is good when we
701 	 * come back on the resume side.
702 	 */
703 	flush_windows();
704 #endif
705 
706 	/*
707 	 * For S3, we're done
708 	 */
709 	if (sleeptype == CPR_TORAM) {
710 		PMD(PMD_SX, ("cpr_suspend rets %x\n", rc))
711 		cpr_set_substate(C_ST_NODUMP);
712 		return (rc);
713 	}
714 #if defined(__sparc)
715 	/*
716 	 * FATAL: NO MORE MEMORY ALLOCATION ALLOWED AFTER THIS POINT!!!
717 	 *
718 	 * The system is quiesced at this point, we are ready to either dump
719 	 * to the state file for a extended sleep or a simple shutdown for
720 	 * systems with non-volatile memory.
721 	 */
722 
723 	/*
724 	 * special handling for reusable:
725 	 */
726 	if (cpr_reusable_mode) {
727 		cpr_set_substate(C_ST_SETPROPS_1);
728 		if (nverr = cpr_set_properties(1))
729 			return (nverr);
730 	}
731 
732 	cpr_set_substate(C_ST_DUMP);
733 	rc = cpr_dump(C_VP);
734 
735 	/*
736 	 * if any error occurred during dump, more
737 	 * special handling for reusable:
738 	 */
739 	if (rc && cpr_reusable_mode) {
740 		cpr_set_substate(C_ST_SETPROPS_0);
741 		if (nverr = cpr_set_properties(0))
742 			return (nverr);
743 	}
744 
745 	if (rc == ENOSPC) {
746 		cpr_set_substate(C_ST_DUMP_NOSPC);
747 		(void) cpr_resume(sleeptype);
748 		goto alloc_statefile;
749 	} else if (rc == 0) {
750 		if (cpr_reusable_mode) {
751 			cpr_set_substate(C_ST_REUSABLE);
752 			longjmp(&ttolwp(curthread)->lwp_qsav);
753 		} else
754 			rc = cpr_set_properties(1);
755 	}
756 #endif
757 	PMD(PMD_SX, ("cpr_suspend: return %d\n", rc))
758 	return (rc);
759 }
760 
761 void
762 cpr_resume_cpus(void)
763 {
764 	/*
765 	 * this is a cut down version of start_other_cpus()
766 	 * just do the initialization to wake the other cpus
767 	 */
768 
769 #if defined(__x86)
770 	/*
771 	 * Initialize our syscall handlers
772 	 */
773 	init_cpu_syscall(CPU);
774 
775 #endif
776 
777 	i_cpr_pre_resume_cpus();
778 
779 	/*
780 	 * Restart the paused cpus
781 	 */
782 	mutex_enter(&cpu_lock);
783 	start_cpus();
784 	mutex_exit(&cpu_lock);
785 
786 	i_cpr_post_resume_cpus();
787 
788 	mutex_enter(&cpu_lock);
789 	/*
790 	 * Restore this cpu to use the regular cpu_pause(), so that
791 	 * online and offline will work correctly
792 	 */
793 	cpu_pause_func = NULL;
794 
795 	/*
796 	 * clear the affinity set in cpr_suspend_cpus()
797 	 */
798 	affinity_clear();
799 
800 	/*
801 	 * offline all the cpus that were brought online during suspend
802 	 */
803 	cpr_restore_offline();
804 
805 	mutex_exit(&cpu_lock);
806 }
807 
808 void
809 cpr_unpause_cpus(void)
810 {
811 	/*
812 	 * Now restore the system back to what it was before we suspended
813 	 */
814 
815 	PMD(PMD_SX, ("cpr_unpause_cpus: restoring system\n"))
816 
817 	mutex_enter(&cpu_lock);
818 
819 	/*
820 	 * Restore this cpu to use the regular cpu_pause(), so that
821 	 * online and offline will work correctly
822 	 */
823 	cpu_pause_func = NULL;
824 
825 	/*
826 	 * Restart the paused cpus
827 	 */
828 	start_cpus();
829 
830 	/*
831 	 * clear the affinity set in cpr_suspend_cpus()
832 	 */
833 	affinity_clear();
834 
835 	/*
836 	 * offline all the cpus that were brought online during suspend
837 	 */
838 	cpr_restore_offline();
839 
840 	mutex_exit(&cpu_lock);
841 }
842 
843 /*
844  * Bring the system back up from a checkpoint, at this point
845  * the VM has been minimally restored by boot, the following
846  * are executed sequentially:
847  *
848  *    - machdep setup and enable interrupts (mp startup if it's mp)
849  *    - resume all devices
850  *    - restart daemons
851  *    - put all threads back on run queue
852  */
853 static int
854 cpr_resume(int sleeptype)
855 {
856 	cpr_time_t pwron_tv, *ctp;
857 	char *str;
858 	int rc = 0;
859 
860 	/*
861 	 * The following switch is used to resume the system
862 	 * that was suspended to a different level.
863 	 */
864 	CPR_DEBUG(CPR_DEBUG1, "\nEntering cpr_resume...\n");
865 	PMD(PMD_SX, ("cpr_resume %x\n", sleeptype))
866 
867 	/*
868 	 * Note:
869 	 *
870 	 * The rollback labels rb_xyz do not represent the cpr resume
871 	 * state when event 'xyz' has happened. Instead they represent
872 	 * the state during cpr suspend when event 'xyz' was being
873 	 * entered (and where cpr suspend failed). The actual call that
874 	 * failed may also need to be partially rolled back, since they
875 	 * aren't atomic in most cases.  In other words, rb_xyz means
876 	 * "roll back all cpr suspend events that happened before 'xyz',
877 	 * and the one that caused the failure, if necessary."
878 	 */
879 	switch (CPR->c_substate) {
880 #if defined(__sparc)
881 	case C_ST_DUMP:
882 		/*
883 		 * This is most likely a full-fledged cpr_resume after
884 		 * a complete and successful cpr suspend. Just roll back
885 		 * everything.
886 		 */
887 		ASSERT(sleeptype == CPR_TODISK);
888 		break;
889 
890 	case C_ST_REUSABLE:
891 	case C_ST_DUMP_NOSPC:
892 	case C_ST_SETPROPS_0:
893 	case C_ST_SETPROPS_1:
894 		/*
895 		 * C_ST_REUSABLE and C_ST_DUMP_NOSPC are the only two
896 		 * special switch cases here. The other two do not have
897 		 * any state change during cpr_suspend() that needs to
898 		 * be rolled back. But these are exit points from
899 		 * cpr_suspend, so theoretically (or in the future), it
900 		 * is possible that a need for roll back of a state
901 		 * change arises between these exit points.
902 		 */
903 		ASSERT(sleeptype == CPR_TODISK);
904 		goto rb_dump;
905 #endif
906 
907 	case C_ST_NODUMP:
908 		PMD(PMD_SX, ("cpr_resume: NODUMP\n"))
909 		goto rb_nodump;
910 
911 	case C_ST_STOP_KERNEL_THREADS:
912 		PMD(PMD_SX, ("cpr_resume: STOP_KERNEL_THREADS\n"))
913 		goto rb_stop_kernel_threads;
914 
915 	case C_ST_SUSPEND_DEVICES:
916 		PMD(PMD_SX, ("cpr_resume: SUSPEND_DEVICES\n"))
917 		goto rb_suspend_devices;
918 
919 #if defined(__sparc)
920 	case C_ST_STATEF_ALLOC:
921 		ASSERT(sleeptype == CPR_TODISK);
922 		goto rb_statef_alloc;
923 
924 	case C_ST_DISABLE_UFS_LOGGING:
925 		ASSERT(sleeptype == CPR_TODISK);
926 		goto rb_disable_ufs_logging;
927 #endif
928 
929 	case C_ST_PM_REATTACH_NOINVOL:
930 		PMD(PMD_SX, ("cpr_resume: REATTACH_NOINVOL\n"))
931 		goto rb_pm_reattach_noinvol;
932 
933 	case C_ST_STOP_USER_THREADS:
934 		PMD(PMD_SX, ("cpr_resume: STOP_USER_THREADS\n"))
935 		goto rb_stop_user_threads;
936 
937 #if defined(__sparc)
938 	case C_ST_MP_OFFLINE:
939 		PMD(PMD_SX, ("cpr_resume: MP_OFFLINE\n"))
940 		goto rb_mp_offline;
941 #endif
942 
943 #if defined(__x86)
944 	case C_ST_MP_PAUSED:
945 		PMD(PMD_SX, ("cpr_resume: MP_PAUSED\n"))
946 		goto rb_mp_paused;
947 #endif
948 
949 
950 	default:
951 		PMD(PMD_SX, ("cpr_resume: others\n"))
952 		goto rb_others;
953 	}
954 
955 rb_all:
956 	/*
957 	 * perform platform-dependent initialization
958 	 */
959 	if (cpr_suspend_succeeded)
960 		i_cpr_machdep_setup();
961 
962 	/*
963 	 * system did not really go down if we jump here
964 	 */
965 rb_dump:
966 	/*
967 	 * IMPORTANT:  SENSITIVE RESUME SEQUENCE
968 	 *
969 	 * DO NOT ADD ANY INITIALIZATION STEP BEFORE THIS POINT!!
970 	 */
971 rb_nodump:
972 	/*
973 	 * If we did suspend to RAM, we didn't generate a dump
974 	 */
975 	PMD(PMD_SX, ("cpr_resume: CPR DMA callback\n"))
976 	(void) callb_execute_class(CB_CL_CPR_DMA, CB_CODE_CPR_RESUME);
977 	if (cpr_suspend_succeeded) {
978 		PMD(PMD_SX, ("cpr_resume: CPR RPC callback\n"))
979 		(void) callb_execute_class(CB_CL_CPR_RPC, CB_CODE_CPR_RESUME);
980 	}
981 
982 	prom_resume_prepost();
983 #if !defined(__sparc)
984 	/*
985 	 * Need to sync the software clock with the hardware clock.
986 	 * On Sparc, this occurs in the sparc-specific cbe.  However
987 	 * on x86 this needs to be handled _before_ we bring other cpu's
988 	 * back online.  So we call a resume function in timestamp.c
989 	 */
990 	if (tsc_resume_in_cyclic == 0)
991 		tsc_resume();
992 
993 #endif
994 
995 #if defined(__sparc)
996 	if (cpr_suspend_succeeded && (boothowto & RB_DEBUG))
997 		kdi_dvec_cpr_restart();
998 #endif
999 
1000 
1001 #if defined(__x86)
1002 rb_mp_paused:
1003 	PT(PT_RMPO);
1004 	PMD(PMD_SX, ("resume aux cpus\n"))
1005 
1006 	if (cpr_suspend_succeeded) {
1007 		cpr_resume_cpus();
1008 	} else {
1009 		cpr_unpause_cpus();
1010 	}
1011 #endif
1012 
1013 	/*
1014 	 * let the tmp callout catch up.
1015 	 */
1016 	PMD(PMD_SX, ("cpr_resume: CPR CALLOUT callback\n"))
1017 	(void) callb_execute_class(CB_CL_CPR_CALLOUT, CB_CODE_CPR_RESUME);
1018 
1019 	i_cpr_enable_intr();
1020 
1021 	mutex_enter(&cpu_lock);
1022 	PMD(PMD_SX, ("cpr_resume: cyclic resume\n"))
1023 	cyclic_resume();
1024 	mutex_exit(&cpu_lock);
1025 
1026 	PMD(PMD_SX, ("cpr_resume: handle xc\n"))
1027 	i_cpr_handle_xc(0);	/* turn it off to allow xc assertion */
1028 
1029 	PMD(PMD_SX, ("cpr_resume: CPR POST KERNEL callback\n"))
1030 	(void) callb_execute_class(CB_CL_CPR_POST_KERNEL, CB_CODE_CPR_RESUME);
1031 
1032 	/*
1033 	 * statistics gathering
1034 	 */
1035 	if (cpr_suspend_succeeded) {
1036 		/*
1037 		 * Prevent false alarm in tod_validate() due to tod
1038 		 * value change between suspend and resume
1039 		 */
1040 		cpr_tod_fault_reset();
1041 
1042 		cpr_convert_promtime(&pwron_tv);
1043 
1044 		ctp = &cpr_term.tm_shutdown;
1045 		if (sleeptype == CPR_TODISK)
1046 			CPR_STAT_EVENT_END_TMZ("  write statefile", ctp);
1047 		CPR_STAT_EVENT_END_TMZ("Suspend Total", ctp);
1048 
1049 		CPR_STAT_EVENT_START_TMZ("Resume Total", &pwron_tv);
1050 
1051 		str = "  prom time";
1052 		CPR_STAT_EVENT_START_TMZ(str, &pwron_tv);
1053 		ctp = &cpr_term.tm_cprboot_start;
1054 		CPR_STAT_EVENT_END_TMZ(str, ctp);
1055 
1056 		str = "  read statefile";
1057 		CPR_STAT_EVENT_START_TMZ(str, ctp);
1058 		ctp = &cpr_term.tm_cprboot_end;
1059 		CPR_STAT_EVENT_END_TMZ(str, ctp);
1060 	}
1061 
1062 rb_stop_kernel_threads:
1063 	/*
1064 	 * Put all threads back to where they belong; get the kernel
1065 	 * daemons straightened up too. Note that the callback table
1066 	 * locked during cpr_stop_kernel_threads() is released only
1067 	 * in cpr_start_kernel_threads(). Ensure modunloading is
1068 	 * disabled before starting kernel threads, we don't want
1069 	 * modunload thread to start changing device tree underneath.
1070 	 */
1071 	PMD(PMD_SX, ("cpr_resume: modunload disable\n"))
1072 	modunload_disable();
1073 	PMD(PMD_SX, ("cpr_resume: start kernel threads\n"))
1074 	cpr_start_kernel_threads();
1075 
1076 rb_suspend_devices:
1077 	CPR_DEBUG(CPR_DEBUG1, "resuming devices...");
1078 	CPR_STAT_EVENT_START("  start drivers");
1079 
1080 	PMD(PMD_SX,
1081 	    ("cpr_resume: rb_suspend_devices: cpr_resume_uniproc = %d\n",
1082 	    cpr_resume_uniproc))
1083 
1084 #if defined(__x86)
1085 	/*
1086 	 * If cpr_resume_uniproc is set, then pause all the other cpus
1087 	 * apart from the current cpu, so that broken drivers that think
1088 	 * that they are on a uniprocessor machine will resume
1089 	 */
1090 	if (cpr_resume_uniproc) {
1091 		mutex_enter(&cpu_lock);
1092 		pause_cpus(NULL);
1093 		mutex_exit(&cpu_lock);
1094 	}
1095 #endif
1096 
1097 	/*
1098 	 * The policy here is to continue resume everything we can if we did
1099 	 * not successfully finish suspend; and panic if we are coming back
1100 	 * from a fully suspended system.
1101 	 */
1102 	PMD(PMD_SX, ("cpr_resume: resume devices\n"))
1103 	rc = cpr_resume_devices(ddi_root_node(), 0);
1104 
1105 	cpr_sae(0);
1106 
1107 	str = "Failed to resume one or more devices.";
1108 
1109 	if (rc) {
1110 		if (CPR->c_substate == C_ST_DUMP ||
1111 		    (sleeptype == CPR_TORAM &&
1112 		    CPR->c_substate == C_ST_NODUMP)) {
1113 			if (cpr_test_point == FORCE_SUSPEND_TO_RAM) {
1114 				PMD(PMD_SX, ("cpr_resume: resume device "
1115 				    "warn\n"))
1116 				cpr_err(CE_WARN, str);
1117 			} else {
1118 				PMD(PMD_SX, ("cpr_resume: resume device "
1119 				    "panic\n"))
1120 				cpr_err(CE_PANIC, str);
1121 			}
1122 		} else {
1123 			PMD(PMD_SX, ("cpr_resume: resume device warn\n"))
1124 			cpr_err(CE_WARN, str);
1125 		}
1126 	}
1127 
1128 	CPR_STAT_EVENT_END("  start drivers");
1129 	CPR_DEBUG(CPR_DEBUG1, "done\n");
1130 
1131 #if defined(__x86)
1132 	/*
1133 	 * If cpr_resume_uniproc is set, then unpause all the processors
1134 	 * that were paused before resuming the drivers
1135 	 */
1136 	if (cpr_resume_uniproc) {
1137 		mutex_enter(&cpu_lock);
1138 		start_cpus();
1139 		mutex_exit(&cpu_lock);
1140 	}
1141 #endif
1142 
1143 	/*
1144 	 * If we had disabled modunloading in this cpr resume cycle (i.e. we
1145 	 * resumed from a state earlier than C_ST_SUSPEND_DEVICES), re-enable
1146 	 * modunloading now.
1147 	 */
1148 	if (CPR->c_substate != C_ST_SUSPEND_DEVICES) {
1149 		PMD(PMD_SX, ("cpr_resume: modload enable\n"))
1150 		modunload_enable();
1151 	}
1152 
1153 	/*
1154 	 * Hooks needed by lock manager prior to resuming.
1155 	 * Refer to code for more comments.
1156 	 */
1157 	PMD(PMD_SX, ("cpr_resume: lock mgr\n"))
1158 	cpr_lock_mgr(lm_cprresume);
1159 
1160 #if defined(__sparc)
1161 	/*
1162 	 * This is a partial (half) resume during cpr suspend, we
1163 	 * haven't yet given up on the suspend. On return from here,
1164 	 * cpr_suspend() will try to reallocate and retry the suspend.
1165 	 */
1166 	if (CPR->c_substate == C_ST_DUMP_NOSPC) {
1167 		return (0);
1168 	}
1169 
1170 	if (sleeptype == CPR_TODISK) {
1171 rb_statef_alloc:
1172 		cpr_statef_close();
1173 
1174 rb_disable_ufs_logging:
1175 		/*
1176 		 * if ufs logging was disabled, re-enable
1177 		 */
1178 		(void) cpr_ufs_logging(1);
1179 	}
1180 #endif
1181 
1182 rb_pm_reattach_noinvol:
1183 	/*
1184 	 * When pm_reattach_noinvol() succeeds, modunload_thread will
1185 	 * remain disabled until after cpr suspend passes the
1186 	 * C_ST_STOP_KERNEL_THREADS state. If any failure happens before
1187 	 * cpr suspend reaches this state, we'll need to enable modunload
1188 	 * thread during rollback.
1189 	 */
1190 	if (CPR->c_substate == C_ST_DISABLE_UFS_LOGGING ||
1191 	    CPR->c_substate == C_ST_STATEF_ALLOC ||
1192 	    CPR->c_substate == C_ST_SUSPEND_DEVICES ||
1193 	    CPR->c_substate == C_ST_STOP_KERNEL_THREADS) {
1194 		PMD(PMD_SX, ("cpr_resume: reattach noinvol fini\n"))
1195 		pm_reattach_noinvol_fini();
1196 	}
1197 
1198 	PMD(PMD_SX, ("cpr_resume: CPR POST USER callback\n"))
1199 	(void) callb_execute_class(CB_CL_CPR_POST_USER, CB_CODE_CPR_RESUME);
1200 	PMD(PMD_SX, ("cpr_resume: CPR PROMPRINTF callback\n"))
1201 	(void) callb_execute_class(CB_CL_CPR_PROMPRINTF, CB_CODE_CPR_RESUME);
1202 
1203 	PMD(PMD_SX, ("cpr_resume: restore direct levels\n"))
1204 	pm_restore_direct_levels();
1205 
1206 rb_stop_user_threads:
1207 	CPR_DEBUG(CPR_DEBUG1, "starting user threads...");
1208 	PMD(PMD_SX, ("cpr_resume: starting user threads\n"))
1209 	cpr_start_user_threads();
1210 	CPR_DEBUG(CPR_DEBUG1, "done\n");
1211 	/*
1212 	 * Ask Xorg to resume the frame buffer, and wait for it to happen
1213 	 */
1214 	mutex_enter(&srn_clone_lock);
1215 	if (srn_signal) {
1216 		PMD(PMD_SX, ("cpr_suspend: (*srn_signal)(..., "
1217 		    "SRN_NORMAL_RESUME)\n"))
1218 		srn_inuse = 1;		/* because (*srn_signal) cv_waits */
1219 		(*srn_signal)(SRN_TYPE_APM, SRN_NORMAL_RESUME);
1220 		srn_inuse = 0;
1221 	} else {
1222 		PMD(PMD_SX, ("cpr_suspend: srn_signal NULL\n"))
1223 	}
1224 	mutex_exit(&srn_clone_lock);
1225 
1226 #if defined(__sparc)
1227 rb_mp_offline:
1228 	if (cpr_mp_online())
1229 		cpr_err(CE_WARN, "Failed to online all the processors.");
1230 #endif
1231 
1232 rb_others:
1233 	PMD(PMD_SX, ("cpr_resume: dep thread\n"))
1234 	pm_dispatch_to_dep_thread(PM_DEP_WK_CPR_RESUME, NULL, NULL,
1235 	    PM_DEP_WAIT, NULL, 0);
1236 
1237 	PMD(PMD_SX, ("cpr_resume: CPR PM callback\n"))
1238 	(void) callb_execute_class(CB_CL_CPR_PM, CB_CODE_CPR_RESUME);
1239 
1240 	if (cpr_suspend_succeeded) {
1241 		cpr_stat_record_events();
1242 	}
1243 
1244 #if defined(__sparc)
1245 	if (sleeptype == CPR_TODISK && !cpr_reusable_mode)
1246 		cpr_clear_definfo();
1247 #endif
1248 
1249 	i_cpr_free_cpus();
1250 	CPR_DEBUG(CPR_DEBUG1, "Sending SIGTHAW...");
1251 	PMD(PMD_SX, ("cpr_resume: SIGTHAW\n"))
1252 	cpr_signal_user(SIGTHAW);
1253 	CPR_DEBUG(CPR_DEBUG1, "done\n");
1254 
1255 	CPR_STAT_EVENT_END("Resume Total");
1256 
1257 	CPR_STAT_EVENT_START_TMZ("WHOLE CYCLE", &wholecycle_tv);
1258 	CPR_STAT_EVENT_END("WHOLE CYCLE");
1259 
1260 	if (cpr_debug & CPR_DEBUG1)
1261 		cmn_err(CE_CONT, "\nThe system is back where you left!\n");
1262 
1263 	CPR_STAT_EVENT_START("POST CPR DELAY");
1264 
1265 #ifdef CPR_STAT
1266 	ctp = &cpr_term.tm_shutdown;
1267 	CPR_STAT_EVENT_START_TMZ("PWROFF TIME", ctp);
1268 	CPR_STAT_EVENT_END_TMZ("PWROFF TIME", &pwron_tv);
1269 
1270 	CPR_STAT_EVENT_PRINT();
1271 #endif /* CPR_STAT */
1272 
1273 	PMD(PMD_SX, ("cpr_resume returns %x\n", rc))
1274 	return (rc);
1275 }
1276 
1277 static void
1278 cpr_suspend_init(int sleeptype)
1279 {
1280 	cpr_time_t *ctp;
1281 
1282 	cpr_stat_init();
1283 
1284 	/*
1285 	 * If cpr_suspend() failed before cpr_dump() gets a chance
1286 	 * to reinitialize the terminator of the statefile,
1287 	 * the values of the old terminator will still linger around.
1288 	 * Since the terminator contains information that we need to
1289 	 * decide whether suspend succeeded or not, we need to
1290 	 * reinitialize it as early as possible.
1291 	 */
1292 	cpr_term.real_statef_size = 0;
1293 	ctp = &cpr_term.tm_shutdown;
1294 	bzero(ctp, sizeof (*ctp));
1295 	ctp = &cpr_term.tm_cprboot_start;
1296 	bzero(ctp, sizeof (*ctp));
1297 	ctp = &cpr_term.tm_cprboot_end;
1298 	bzero(ctp, sizeof (*ctp));
1299 
1300 	if (sleeptype == CPR_TODISK) {
1301 		/*
1302 		 * Lookup the physical address of our thread structure.
1303 		 * This should never be invalid and the entire thread structure
1304 		 * is expected to reside within the same pfn.
1305 		 */
1306 		curthreadpfn = hat_getpfnum(kas.a_hat, (caddr_t)curthread);
1307 		ASSERT(curthreadpfn != PFN_INVALID);
1308 		ASSERT(curthreadpfn == hat_getpfnum(kas.a_hat,
1309 		    (caddr_t)curthread + sizeof (kthread_t) - 1));
1310 	}
1311 
1312 	cpr_suspend_succeeded = 0;
1313 }
1314 
1315 /*
1316  * bring all the offline cpus online
1317  */
1318 static int
1319 cpr_all_online(void)
1320 {
1321 	int	rc = 0;
1322 
1323 #ifdef	__sparc
1324 	/*
1325 	 * do nothing
1326 	 */
1327 #else
1328 
1329 	cpu_t	*cp;
1330 
1331 	ASSERT(MUTEX_HELD(&cpu_lock));
1332 
1333 	cp = cpu_list;
1334 	do {
1335 		cp->cpu_cpr_flags &= ~CPU_CPR_ONLINE;
1336 		if (!CPU_ACTIVE(cp)) {
1337 			if ((rc = cpu_online(cp)) != 0)
1338 				break;
1339 			CPU_SET_CPR_FLAGS(cp, CPU_CPR_ONLINE);
1340 		}
1341 	} while ((cp = cp->cpu_next) != cpu_list);
1342 
1343 	if (rc) {
1344 		/*
1345 		 * an online operation failed so offline the cpus
1346 		 * that were onlined above to restore the system
1347 		 * to its original state
1348 		 */
1349 		cpr_restore_offline();
1350 	}
1351 #endif
1352 	return (rc);
1353 }
1354 
1355 /*
1356  * offline all the cpus that were brought online by cpr_all_online()
1357  */
1358 static void
1359 cpr_restore_offline(void)
1360 {
1361 
1362 #ifdef	__sparc
1363 	/*
1364 	 * do nothing
1365 	 */
1366 #else
1367 
1368 	cpu_t	*cp;
1369 	int	rc = 0;
1370 
1371 	ASSERT(MUTEX_HELD(&cpu_lock));
1372 
1373 	cp = cpu_list;
1374 	do {
1375 		if (CPU_CPR_IS_ONLINE(cp)) {
1376 			rc =  cpu_offline(cp, 0);
1377 			/*
1378 			 * this offline should work, since the cpu was
1379 			 * offline originally and was successfully onlined
1380 			 * by cpr_all_online()
1381 			 */
1382 			ASSERT(rc == 0);
1383 			cp->cpu_cpr_flags &= ~CPU_CPR_ONLINE;
1384 		}
1385 	} while ((cp = cp->cpu_next) != cpu_list);
1386 
1387 #endif
1388 
1389 }
1390