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