1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * System call to checkpoint and resume the currently running kernel 31*7c478bd9Sstevel@tonic-gate */ 32*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/errno.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/cred.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/uadmin.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/systm.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/cpr.h> 41*7c478bd9Sstevel@tonic-gate #include <sys/swap.h> 42*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 43*7c478bd9Sstevel@tonic-gate #include <sys/autoconf.h> 44*7c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate extern int i_cpr_is_supported(void); 47*7c478bd9Sstevel@tonic-gate extern int cpr_is_ufs(struct vfs *); 48*7c478bd9Sstevel@tonic-gate extern int cpr_check_spec_statefile(void); 49*7c478bd9Sstevel@tonic-gate extern int cpr_reusable_mount_check(void); 50*7c478bd9Sstevel@tonic-gate extern void cpr_forget_cprconfig(void); 51*7c478bd9Sstevel@tonic-gate extern int i_cpr_reusable_supported(void); 52*7c478bd9Sstevel@tonic-gate extern int i_cpr_reusefini(void); 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate extern struct mod_ops mod_miscops; 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = { 57*7c478bd9Sstevel@tonic-gate &mod_miscops, "checkpoint resume" 58*7c478bd9Sstevel@tonic-gate }; 59*7c478bd9Sstevel@tonic-gate 60*7c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 61*7c478bd9Sstevel@tonic-gate MODREV_1, (void *)&modlmisc, NULL 62*7c478bd9Sstevel@tonic-gate }; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate char _depends_on[] = "misc/bootdev"; /* i_devname_to_promname() */ 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate int cpr_reusable_mode; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate kmutex_t cpr_slock; /* cpr serial lock */ 69*7c478bd9Sstevel@tonic-gate cpr_t cpr_state; 70*7c478bd9Sstevel@tonic-gate int cpr_debug; 71*7c478bd9Sstevel@tonic-gate int cpr_test_mode; /* true if called via uadmin testmode */ 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* 74*7c478bd9Sstevel@tonic-gate * All the loadable module related code follows 75*7c478bd9Sstevel@tonic-gate */ 76*7c478bd9Sstevel@tonic-gate int 77*7c478bd9Sstevel@tonic-gate _init(void) 78*7c478bd9Sstevel@tonic-gate { 79*7c478bd9Sstevel@tonic-gate register int e; 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate if ((e = mod_install(&modlinkage)) == 0) { 82*7c478bd9Sstevel@tonic-gate mutex_init(&cpr_slock, NULL, MUTEX_DEFAULT, NULL); 83*7c478bd9Sstevel@tonic-gate } 84*7c478bd9Sstevel@tonic-gate return (e); 85*7c478bd9Sstevel@tonic-gate } 86*7c478bd9Sstevel@tonic-gate 87*7c478bd9Sstevel@tonic-gate int 88*7c478bd9Sstevel@tonic-gate _fini(void) 89*7c478bd9Sstevel@tonic-gate { 90*7c478bd9Sstevel@tonic-gate register int e; 91*7c478bd9Sstevel@tonic-gate 92*7c478bd9Sstevel@tonic-gate if ((e = mod_remove(&modlinkage)) == 0) { 93*7c478bd9Sstevel@tonic-gate mutex_destroy(&cpr_slock); 94*7c478bd9Sstevel@tonic-gate } 95*7c478bd9Sstevel@tonic-gate return (e); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate int 99*7c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 100*7c478bd9Sstevel@tonic-gate { 101*7c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate int 105*7c478bd9Sstevel@tonic-gate cpr(int fcn) 106*7c478bd9Sstevel@tonic-gate { 107*7c478bd9Sstevel@tonic-gate static const char noswapstr[] = "reusable statefile requires " 108*7c478bd9Sstevel@tonic-gate "that no swap area be configured.\n"; 109*7c478bd9Sstevel@tonic-gate static const char blockstr[] = "reusable statefile must be " 110*7c478bd9Sstevel@tonic-gate "a block device. See power.conf(4) and pmconfig(1M).\n"; 111*7c478bd9Sstevel@tonic-gate static const char normalfmt[] = "cannot run normal " 112*7c478bd9Sstevel@tonic-gate "checkpoint/resume when in reusable statefile mode. " 113*7c478bd9Sstevel@tonic-gate "use uadmin A_FREEZE AD_REUSEFINI (uadmin %d %d) " 114*7c478bd9Sstevel@tonic-gate "to exit reusable statefile mode.\n"; 115*7c478bd9Sstevel@tonic-gate static const char modefmt[] = "%s in reusable mode.\n"; 116*7c478bd9Sstevel@tonic-gate register int rc = 0; 117*7c478bd9Sstevel@tonic-gate extern int cpr_init(int); 118*7c478bd9Sstevel@tonic-gate extern void cpr_done(void); 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate /* 121*7c478bd9Sstevel@tonic-gate * Need to know if we're in reusable mode, but we will likely have 122*7c478bd9Sstevel@tonic-gate * rebooted since REUSEINIT, so we have to get the info from the 123*7c478bd9Sstevel@tonic-gate * file system 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate if (!cpr_reusable_mode) 126*7c478bd9Sstevel@tonic-gate cpr_reusable_mode = cpr_get_reusable_mode(); 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate cpr_forget_cprconfig(); 129*7c478bd9Sstevel@tonic-gate switch (fcn) { 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate case AD_CPR_REUSEINIT: 132*7c478bd9Sstevel@tonic-gate if (!i_cpr_reusable_supported()) 133*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 134*7c478bd9Sstevel@tonic-gate if (!cpr_statefile_is_spec()) { 135*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, blockstr); 136*7c478bd9Sstevel@tonic-gate return (EINVAL); 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate if ((rc = cpr_check_spec_statefile()) != 0) 139*7c478bd9Sstevel@tonic-gate return (rc); 140*7c478bd9Sstevel@tonic-gate if (swapinfo) { 141*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, noswapstr); 142*7c478bd9Sstevel@tonic-gate return (EINVAL); 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate cpr_test_mode = 0; 145*7c478bd9Sstevel@tonic-gate break; 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate case AD_CPR_NOCOMPRESS: 148*7c478bd9Sstevel@tonic-gate case AD_CPR_COMPRESS: 149*7c478bd9Sstevel@tonic-gate case AD_CPR_FORCE: 150*7c478bd9Sstevel@tonic-gate if (cpr_reusable_mode) { 151*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, normalfmt, A_FREEZE, AD_REUSEFINI); 152*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 153*7c478bd9Sstevel@tonic-gate } 154*7c478bd9Sstevel@tonic-gate cpr_test_mode = 0; 155*7c478bd9Sstevel@tonic-gate break; 156*7c478bd9Sstevel@tonic-gate 157*7c478bd9Sstevel@tonic-gate case AD_CPR_REUSABLE: 158*7c478bd9Sstevel@tonic-gate if (!i_cpr_reusable_supported()) 159*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 160*7c478bd9Sstevel@tonic-gate if (!cpr_statefile_is_spec()) { 161*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, blockstr); 162*7c478bd9Sstevel@tonic-gate return (EINVAL); 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate if ((rc = cpr_check_spec_statefile()) != 0) 165*7c478bd9Sstevel@tonic-gate return (rc); 166*7c478bd9Sstevel@tonic-gate if (swapinfo) { 167*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, noswapstr); 168*7c478bd9Sstevel@tonic-gate return (EINVAL); 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate if ((rc = cpr_reusable_mount_check()) != 0) 171*7c478bd9Sstevel@tonic-gate return (rc); 172*7c478bd9Sstevel@tonic-gate cpr_test_mode = 0; 173*7c478bd9Sstevel@tonic-gate break; 174*7c478bd9Sstevel@tonic-gate 175*7c478bd9Sstevel@tonic-gate case AD_CPR_REUSEFINI: 176*7c478bd9Sstevel@tonic-gate if (!i_cpr_reusable_supported()) 177*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 178*7c478bd9Sstevel@tonic-gate cpr_test_mode = 0; 179*7c478bd9Sstevel@tonic-gate break; 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate case AD_CPR_TESTZ: 182*7c478bd9Sstevel@tonic-gate case AD_CPR_TESTNOZ: 183*7c478bd9Sstevel@tonic-gate case AD_CPR_TESTHALT: 184*7c478bd9Sstevel@tonic-gate if (cpr_reusable_mode) { 185*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, normalfmt, A_FREEZE, AD_REUSEFINI); 186*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 187*7c478bd9Sstevel@tonic-gate } 188*7c478bd9Sstevel@tonic-gate cpr_test_mode = 1; 189*7c478bd9Sstevel@tonic-gate break; 190*7c478bd9Sstevel@tonic-gate 191*7c478bd9Sstevel@tonic-gate case AD_CPR_CHECK: 192*7c478bd9Sstevel@tonic-gate if (!i_cpr_is_supported() || cpr_reusable_mode) 193*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 194*7c478bd9Sstevel@tonic-gate return (0); 195*7c478bd9Sstevel@tonic-gate 196*7c478bd9Sstevel@tonic-gate case AD_CPR_PRINT: 197*7c478bd9Sstevel@tonic-gate CPR_STAT_EVENT_END("POST CPR DELAY"); 198*7c478bd9Sstevel@tonic-gate cpr_stat_event_print(); 199*7c478bd9Sstevel@tonic-gate return (0); 200*7c478bd9Sstevel@tonic-gate 201*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG0: 202*7c478bd9Sstevel@tonic-gate cpr_debug = 0; 203*7c478bd9Sstevel@tonic-gate return (0); 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG1: 206*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG2: 207*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG3: 208*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG4: 209*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG5: 210*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG7: 211*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG8: 212*7c478bd9Sstevel@tonic-gate cpr_debug |= CPR_DEBUG_BIT(fcn); 213*7c478bd9Sstevel@tonic-gate return (0); 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate case AD_CPR_DEBUG9: 216*7c478bd9Sstevel@tonic-gate cpr_debug |= LEVEL6; 217*7c478bd9Sstevel@tonic-gate return (0); 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate default: 220*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 221*7c478bd9Sstevel@tonic-gate } 222*7c478bd9Sstevel@tonic-gate 223*7c478bd9Sstevel@tonic-gate if (!i_cpr_is_supported() || !cpr_is_ufs(rootvfs)) 224*7c478bd9Sstevel@tonic-gate return (ENOTSUP); 225*7c478bd9Sstevel@tonic-gate 226*7c478bd9Sstevel@tonic-gate if (fcn == AD_CPR_REUSEINIT) { 227*7c478bd9Sstevel@tonic-gate if (mutex_tryenter(&cpr_slock) == 0) 228*7c478bd9Sstevel@tonic-gate return (EBUSY); 229*7c478bd9Sstevel@tonic-gate if (cpr_reusable_mode) { 230*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, modefmt, "already"); 231*7c478bd9Sstevel@tonic-gate mutex_exit(&cpr_slock); 232*7c478bd9Sstevel@tonic-gate return (EBUSY); 233*7c478bd9Sstevel@tonic-gate } 234*7c478bd9Sstevel@tonic-gate rc = i_cpr_reuseinit(); 235*7c478bd9Sstevel@tonic-gate mutex_exit(&cpr_slock); 236*7c478bd9Sstevel@tonic-gate return (rc); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate if (fcn == AD_CPR_REUSEFINI) { 240*7c478bd9Sstevel@tonic-gate if (mutex_tryenter(&cpr_slock) == 0) 241*7c478bd9Sstevel@tonic-gate return (EBUSY); 242*7c478bd9Sstevel@tonic-gate if (!cpr_reusable_mode) { 243*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, modefmt, "not"); 244*7c478bd9Sstevel@tonic-gate mutex_exit(&cpr_slock); 245*7c478bd9Sstevel@tonic-gate return (EINVAL); 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate rc = i_cpr_reusefini(); 248*7c478bd9Sstevel@tonic-gate mutex_exit(&cpr_slock); 249*7c478bd9Sstevel@tonic-gate return (rc); 250*7c478bd9Sstevel@tonic-gate } 251*7c478bd9Sstevel@tonic-gate 252*7c478bd9Sstevel@tonic-gate /* 253*7c478bd9Sstevel@tonic-gate * acquire cpr serial lock and init cpr state structure. 254*7c478bd9Sstevel@tonic-gate */ 255*7c478bd9Sstevel@tonic-gate if (rc = cpr_init(fcn)) 256*7c478bd9Sstevel@tonic-gate return (rc); 257*7c478bd9Sstevel@tonic-gate 258*7c478bd9Sstevel@tonic-gate if (fcn == AD_CPR_REUSABLE) { 259*7c478bd9Sstevel@tonic-gate if ((rc = i_cpr_check_cprinfo()) != 0) { 260*7c478bd9Sstevel@tonic-gate mutex_exit(&cpr_slock); 261*7c478bd9Sstevel@tonic-gate return (rc); 262*7c478bd9Sstevel@tonic-gate } 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate 265*7c478bd9Sstevel@tonic-gate /* 266*7c478bd9Sstevel@tonic-gate * Call the main cpr routine. If we are successful, we will be coming 267*7c478bd9Sstevel@tonic-gate * down from the resume side, otherwise we are still in suspend. 268*7c478bd9Sstevel@tonic-gate */ 269*7c478bd9Sstevel@tonic-gate cpr_err(CE_CONT, "System is being suspended"); 270*7c478bd9Sstevel@tonic-gate if (rc = cpr_main()) { 271*7c478bd9Sstevel@tonic-gate CPR->c_flags |= C_ERROR; 272*7c478bd9Sstevel@tonic-gate cpr_err(CE_NOTE, "Suspend operation failed."); 273*7c478bd9Sstevel@tonic-gate } else if (CPR->c_flags & C_SUSPENDING) { 274*7c478bd9Sstevel@tonic-gate extern void cpr_power_down(); 275*7c478bd9Sstevel@tonic-gate /* 276*7c478bd9Sstevel@tonic-gate * Back from a successful checkpoint 277*7c478bd9Sstevel@tonic-gate */ 278*7c478bd9Sstevel@tonic-gate if (fcn == AD_CPR_TESTZ || fcn == AD_CPR_TESTNOZ) { 279*7c478bd9Sstevel@tonic-gate mdboot(0, AD_BOOT, ""); 280*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate /* make sure there are no more changes to the device tree */ 284*7c478bd9Sstevel@tonic-gate devtree_freeze(); 285*7c478bd9Sstevel@tonic-gate 286*7c478bd9Sstevel@tonic-gate /* 287*7c478bd9Sstevel@tonic-gate * stop other cpus and raise our priority. since there is only 288*7c478bd9Sstevel@tonic-gate * one active cpu after this, and our priority will be too high 289*7c478bd9Sstevel@tonic-gate * for us to be preempted, we're essentially single threaded 290*7c478bd9Sstevel@tonic-gate * from here on out. 291*7c478bd9Sstevel@tonic-gate */ 292*7c478bd9Sstevel@tonic-gate stop_other_cpus(); 293*7c478bd9Sstevel@tonic-gate (void) spl6(); 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate /* 296*7c478bd9Sstevel@tonic-gate * try and reset leaf devices. reset_leaves() should only 297*7c478bd9Sstevel@tonic-gate * be called when there are no other threads that could be 298*7c478bd9Sstevel@tonic-gate * accessing devices 299*7c478bd9Sstevel@tonic-gate */ 300*7c478bd9Sstevel@tonic-gate reset_leaves(); 301*7c478bd9Sstevel@tonic-gate 302*7c478bd9Sstevel@tonic-gate /* 303*7c478bd9Sstevel@tonic-gate * If cpr_power_down() succeeds, it'll not return. 304*7c478bd9Sstevel@tonic-gate * 305*7c478bd9Sstevel@tonic-gate * Drives with write-cache enabled need to flush 306*7c478bd9Sstevel@tonic-gate * their cache. 307*7c478bd9Sstevel@tonic-gate */ 308*7c478bd9Sstevel@tonic-gate if (fcn != AD_CPR_TESTHALT) 309*7c478bd9Sstevel@tonic-gate cpr_power_down(); 310*7c478bd9Sstevel@tonic-gate 311*7c478bd9Sstevel@tonic-gate errp("(Done. Please Switch Off)\n"); 312*7c478bd9Sstevel@tonic-gate halt(NULL); 313*7c478bd9Sstevel@tonic-gate /* NOTREACHED */ 314*7c478bd9Sstevel@tonic-gate } 315*7c478bd9Sstevel@tonic-gate /* 316*7c478bd9Sstevel@tonic-gate * For resuming: release resources and the serial lock. 317*7c478bd9Sstevel@tonic-gate */ 318*7c478bd9Sstevel@tonic-gate cpr_done(); 319*7c478bd9Sstevel@tonic-gate return (rc); 320*7c478bd9Sstevel@tonic-gate } 321