17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*903a11ebSrh87107 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/thread.h> 307c478bd9Sstevel@tonic-gate #include <sys/conf.h> 317c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h> 327c478bd9Sstevel@tonic-gate #include <sys/cpr.h> 337c478bd9Sstevel@tonic-gate #include <sys/user.h> 347c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 357c478bd9Sstevel@tonic-gate #include <sys/callb.h> 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate extern void utstop_init(void); 387c478bd9Sstevel@tonic-gate extern void add_one_utstop(void); 397c478bd9Sstevel@tonic-gate extern void utstop_timedwait(long ticks); 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate static void cpr_stop_user(int); 427c478bd9Sstevel@tonic-gate static int cpr_check_user_threads(void); 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate /* 457c478bd9Sstevel@tonic-gate * CPR user thread related support routines 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate void 487c478bd9Sstevel@tonic-gate cpr_signal_user(int sig) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate /* 517c478bd9Sstevel@tonic-gate * The signal SIGTHAW and SIGFREEZE cannot be sent to every thread yet 527c478bd9Sstevel@tonic-gate * since openwin is catching every signal and default action is to exit. 537c478bd9Sstevel@tonic-gate * We also need to implement the true SIGFREEZE and SIGTHAW to stop threads. 547c478bd9Sstevel@tonic-gate */ 557c478bd9Sstevel@tonic-gate struct proc *p; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate for (p = practive; p; p = p->p_next) { 607c478bd9Sstevel@tonic-gate /* only user threads */ 617c478bd9Sstevel@tonic-gate if (p->p_exec == NULL || p->p_stat == SZOMB || 627c478bd9Sstevel@tonic-gate p == proc_init || p == ttoproc(curthread)) 637c478bd9Sstevel@tonic-gate continue; 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 667c478bd9Sstevel@tonic-gate sigtoproc(p, NULL, sig); 677c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate DELAY(MICROSEC); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* max wait time for user thread stop */ 757c478bd9Sstevel@tonic-gate #define CPR_UTSTOP_WAIT hz 767c478bd9Sstevel@tonic-gate #define CPR_UTSTOP_RETRY 4 777c478bd9Sstevel@tonic-gate static int count; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate int 807c478bd9Sstevel@tonic-gate cpr_stop_user_threads() 817c478bd9Sstevel@tonic-gate { 827c478bd9Sstevel@tonic-gate utstop_init(); 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate count = 0; 857c478bd9Sstevel@tonic-gate do { 867c478bd9Sstevel@tonic-gate if (++count > CPR_UTSTOP_RETRY) 877c478bd9Sstevel@tonic-gate return (ESRCH); 887c478bd9Sstevel@tonic-gate cpr_stop_user(count * count * CPR_UTSTOP_WAIT); 897c478bd9Sstevel@tonic-gate } while (cpr_check_user_threads() && 907c478bd9Sstevel@tonic-gate (count < CPR_UTSTOP_RETRY || CPR->c_fcn != AD_CPR_FORCE)); 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate return (0); 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate /* 967c478bd9Sstevel@tonic-gate * This routine tries to stop all user threads before we get rid of all 977c478bd9Sstevel@tonic-gate * its pages.It goes through allthreads list and set the TP_CHKPT flag 987c478bd9Sstevel@tonic-gate * for all user threads and make them runnable. If all of the threads 997c478bd9Sstevel@tonic-gate * can be stopped within the max wait time, CPR will proceed. Otherwise 1007c478bd9Sstevel@tonic-gate * CPR is aborted after a few of similiar retries. 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate static void 1037c478bd9Sstevel@tonic-gate cpr_stop_user(int wait) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate kthread_id_t tp; 1067c478bd9Sstevel@tonic-gate proc_t *p; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate /* The whole loop below needs to be atomic */ 1097c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /* faster this way */ 1127c478bd9Sstevel@tonic-gate tp = curthread->t_next; 1137c478bd9Sstevel@tonic-gate do { 1147c478bd9Sstevel@tonic-gate /* kernel threads will be handled later */ 1157c478bd9Sstevel@tonic-gate p = ttoproc(tp); 1167c478bd9Sstevel@tonic-gate if (p->p_as == &kas || p->p_stat == SZOMB) 1177c478bd9Sstevel@tonic-gate continue; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * If the thread is stopped (by CPR) already, do nothing; 1217c478bd9Sstevel@tonic-gate * if running, mark TP_CHKPT; 1227c478bd9Sstevel@tonic-gate * if sleeping normally, mark TP_CHKPT and setrun; 1237c478bd9Sstevel@tonic-gate * if sleeping non-interruptable, mark TP_CHKPT only for now; 1247c478bd9Sstevel@tonic-gate * if sleeping with t_wchan0 != 0 etc, virtually stopped, 1257c478bd9Sstevel@tonic-gate * do nothing. 1267c478bd9Sstevel@tonic-gate */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* p_lock is needed for modifying t_proc_flag */ 1297c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1307c478bd9Sstevel@tonic-gate thread_lock(tp); /* needed to check CPR_ISTOPPED */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if (tp->t_state == TS_STOPPED) { 1337c478bd9Sstevel@tonic-gate /* 1347c478bd9Sstevel@tonic-gate * if already stopped by other reasons, add this new 1357c478bd9Sstevel@tonic-gate * reason to it. 1367c478bd9Sstevel@tonic-gate */ 1377c478bd9Sstevel@tonic-gate if (tp->t_schedflag & TS_RESUME) 1387c478bd9Sstevel@tonic-gate tp->t_schedflag &= ~TS_RESUME; 1397c478bd9Sstevel@tonic-gate } else { 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate tp->t_proc_flag |= TP_CHKPT; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate thread_unlock(tp); 1447c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1457c478bd9Sstevel@tonic-gate add_one_utstop(); 1467c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 1477c478bd9Sstevel@tonic-gate thread_lock(tp); 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate aston(tp); 1507c478bd9Sstevel@tonic-gate 151c97ad5cdSakolb if (ISWAKEABLE(tp) || ISWAITING(tp)) { 1527c478bd9Sstevel@tonic-gate setrun_locked(tp); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate /* 1567c478bd9Sstevel@tonic-gate * force the thread into the kernel if it is not already there. 1577c478bd9Sstevel@tonic-gate */ 1587c478bd9Sstevel@tonic-gate if (tp->t_state == TS_ONPROC && tp->t_cpu != CPU) 1597c478bd9Sstevel@tonic-gate poke_cpu(tp->t_cpu->cpu_id); 1607c478bd9Sstevel@tonic-gate thread_unlock(tp); 1617c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate } while ((tp = tp->t_next) != curthread); 1647c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate utstop_timedwait(wait); 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * Checks and makes sure all user threads are stopped 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate static int 1737c478bd9Sstevel@tonic-gate cpr_check_user_threads() 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate kthread_id_t tp; 1767c478bd9Sstevel@tonic-gate int rc = 0; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 1797c478bd9Sstevel@tonic-gate tp = curthread->t_next; 1807c478bd9Sstevel@tonic-gate do { 1817c478bd9Sstevel@tonic-gate if (ttoproc(tp)->p_as == &kas || ttoproc(tp)->p_stat == SZOMB) 1827c478bd9Sstevel@tonic-gate continue; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate thread_lock(tp); 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * make sure that we are off all the queues and in a stopped 1877c478bd9Sstevel@tonic-gate * state. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate if (!CPR_ISTOPPED(tp)) { 1907c478bd9Sstevel@tonic-gate thread_unlock(tp); 1917c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (count == CPR_UTSTOP_RETRY) { 194ae115bc7Smrj CPR_DEBUG(CPR_DEBUG1, "Suspend failed: " 195ae115bc7Smrj "cannot stop uthread\n"); 1967c478bd9Sstevel@tonic-gate cpr_err(CE_WARN, "Suspend cannot stop " 197bf30efa4Smathue "process %s (%p:%x).", 198bf30efa4Smathue ttoproc(tp)->p_user.u_psargs, (void *)tp, 1997c478bd9Sstevel@tonic-gate tp->t_state); 2007c478bd9Sstevel@tonic-gate cpr_err(CE_WARN, "Process may be waiting for" 2017c478bd9Sstevel@tonic-gate " network request, please try again."); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 204ae115bc7Smrj CPR_DEBUG(CPR_DEBUG2, "cant stop t=%p state=%x pfg=%x " 205*903a11ebSrh87107 "sched=%x\n", (void *)tp, tp->t_state, 206*903a11ebSrh87107 tp->t_proc_flag, tp->t_schedflag); 207ae115bc7Smrj CPR_DEBUG(CPR_DEBUG2, "proc %p state=%x pid=%d\n", 208*903a11ebSrh87107 (void *)ttoproc(tp), ttoproc(tp)->p_stat, 209ae115bc7Smrj ttoproc(tp)->p_pidp->pid_id); 2107c478bd9Sstevel@tonic-gate return (1); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate thread_unlock(tp); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate } while ((tp = tp->t_next) != curthread && rc == 0); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 2177c478bd9Sstevel@tonic-gate return (0); 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * start all threads that were stopped for checkpoint. 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate void 2257c478bd9Sstevel@tonic-gate cpr_start_user_threads() 2267c478bd9Sstevel@tonic-gate { 2277c478bd9Sstevel@tonic-gate kthread_id_t tp; 2287c478bd9Sstevel@tonic-gate proc_t *p; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 2317c478bd9Sstevel@tonic-gate tp = curthread->t_next; 2327c478bd9Sstevel@tonic-gate do { 2337c478bd9Sstevel@tonic-gate p = ttoproc(tp); 2347c478bd9Sstevel@tonic-gate /* 2357c478bd9Sstevel@tonic-gate * kernel threads are callback'ed rather than setrun. 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate if (ttoproc(tp)->p_as == &kas) continue; 2387c478bd9Sstevel@tonic-gate /* 2397c478bd9Sstevel@tonic-gate * t_proc_flag should have been cleared. Just to make sure here 2407c478bd9Sstevel@tonic-gate */ 2417c478bd9Sstevel@tonic-gate mutex_enter(&p->p_lock); 2427c478bd9Sstevel@tonic-gate tp->t_proc_flag &= ~TP_CHKPT; 2437c478bd9Sstevel@tonic-gate mutex_exit(&p->p_lock); 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate thread_lock(tp); 2467c478bd9Sstevel@tonic-gate if (CPR_ISTOPPED(tp)) { 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate /* 2497c478bd9Sstevel@tonic-gate * put it back on the runq 2507c478bd9Sstevel@tonic-gate */ 2517c478bd9Sstevel@tonic-gate tp->t_schedflag |= TS_RESUME; 2527c478bd9Sstevel@tonic-gate setrun_locked(tp); 2537c478bd9Sstevel@tonic-gate } 2547c478bd9Sstevel@tonic-gate thread_unlock(tp); 2557c478bd9Sstevel@tonic-gate /* 2567c478bd9Sstevel@tonic-gate * DEBUG - Keep track of current and next thread pointer. 2577c478bd9Sstevel@tonic-gate */ 2587c478bd9Sstevel@tonic-gate } while ((tp = tp->t_next) != curthread); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate /* 2657c478bd9Sstevel@tonic-gate * re/start kernel threads 2667c478bd9Sstevel@tonic-gate */ 2677c478bd9Sstevel@tonic-gate void 2687c478bd9Sstevel@tonic-gate cpr_start_kernel_threads(void) 2697c478bd9Sstevel@tonic-gate { 270ae115bc7Smrj CPR_DEBUG(CPR_DEBUG1, "starting kernel daemons..."); 2717c478bd9Sstevel@tonic-gate (void) callb_execute_class(CB_CL_CPR_DAEMON, CB_CODE_CPR_RESUME); 272ae115bc7Smrj CPR_DEBUG(CPR_DEBUG1, "done\n"); 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate /* see table lock below */ 2757c478bd9Sstevel@tonic-gate callb_unlock_table(); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * Stop kernel threads by using the callback mechanism. If any thread 2817c478bd9Sstevel@tonic-gate * cannot be stopped, return failure. 2827c478bd9Sstevel@tonic-gate */ 2837c478bd9Sstevel@tonic-gate int 2847c478bd9Sstevel@tonic-gate cpr_stop_kernel_threads(void) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate caddr_t name; 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate callb_lock_table(); /* Note: we unlock the table in resume. */ 2897c478bd9Sstevel@tonic-gate 290ae115bc7Smrj CPR_DEBUG(CPR_DEBUG1, "stopping kernel daemons..."); 2917c478bd9Sstevel@tonic-gate if ((name = callb_execute_class(CB_CL_CPR_DAEMON, 2927c478bd9Sstevel@tonic-gate CB_CODE_CPR_CHKPT)) != (caddr_t)NULL) { 2937c478bd9Sstevel@tonic-gate cpr_err(CE_WARN, 2947c478bd9Sstevel@tonic-gate "Could not stop \"%s\" kernel thread. " 2957c478bd9Sstevel@tonic-gate "Please try again later.", name); 2967c478bd9Sstevel@tonic-gate return (EBUSY); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2992df1fe9cSrandyf CPR_DEBUG(CPR_DEBUG1, ("done\n")); 3002df1fe9cSrandyf return (0); 3012df1fe9cSrandyf } 3022df1fe9cSrandyf 3032df1fe9cSrandyf /* 3042df1fe9cSrandyf * Check to see that kernel threads are stopped. 3052df1fe9cSrandyf * This should be called while CPU's are paused, and the caller is 3062df1fe9cSrandyf * effectively running single user, or else we are virtually guaranteed 3072df1fe9cSrandyf * to fail. The routine should not ASSERT on the paused state or spl 3082df1fe9cSrandyf * level, as there may be a use for this to verify that things are running 3092df1fe9cSrandyf * again. 3102df1fe9cSrandyf */ 3112df1fe9cSrandyf int 3122df1fe9cSrandyf cpr_threads_are_stopped(void) 3132df1fe9cSrandyf { 3142df1fe9cSrandyf caddr_t name; 3152df1fe9cSrandyf kthread_id_t tp; 3162df1fe9cSrandyf proc_t *p; 3172df1fe9cSrandyf 3187c478bd9Sstevel@tonic-gate /* 3197c478bd9Sstevel@tonic-gate * We think we stopped all the kernel threads. Just in case 3207c478bd9Sstevel@tonic-gate * someone is not playing by the rules, take a spin through 3217c478bd9Sstevel@tonic-gate * the threadlist and see if we can account for everybody. 3227c478bd9Sstevel@tonic-gate */ 3237c478bd9Sstevel@tonic-gate mutex_enter(&pidlock); 3247c478bd9Sstevel@tonic-gate tp = curthread->t_next; 3257c478bd9Sstevel@tonic-gate do { 3267c478bd9Sstevel@tonic-gate p = ttoproc(tp); 3277c478bd9Sstevel@tonic-gate if (p->p_as != &kas) 3287c478bd9Sstevel@tonic-gate continue; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate if (tp->t_flag & T_INTR_THREAD) 3317c478bd9Sstevel@tonic-gate continue; 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate if (! callb_is_stopped(tp, &name)) { 3347c478bd9Sstevel@tonic-gate mutex_exit(&pidlock); 3357c478bd9Sstevel@tonic-gate cpr_err(CE_WARN, 3367c478bd9Sstevel@tonic-gate "\"%s\" kernel thread not stopped.", name); 3377c478bd9Sstevel@tonic-gate return (EBUSY); 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate } while ((tp = tp->t_next) != curthread); 3407c478bd9Sstevel@tonic-gate 3412df1fe9cSrandyf mutex_exit(&pidlock); 3427c478bd9Sstevel@tonic-gate return (0); 3437c478bd9Sstevel@tonic-gate } 344