xref: /onnv-gate/usr/src/uts/common/avs/ns/solaris/nskernd.c (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM #include <sys/types.h>
27*7836SJohn.Forte@Sun.COM #include <sys/ksynch.h>
28*7836SJohn.Forte@Sun.COM #include <sys/errno.h>
29*7836SJohn.Forte@Sun.COM #include <sys/ddi.h>
30*7836SJohn.Forte@Sun.COM #include <sys/sunddi.h>
31*7836SJohn.Forte@Sun.COM 
32*7836SJohn.Forte@Sun.COM #include "../nsctl.h"
33*7836SJohn.Forte@Sun.COM #include "../nsctl/nsc_ioctl.h"
34*7836SJohn.Forte@Sun.COM #include "nskernd.h"
35*7836SJohn.Forte@Sun.COM 
36*7836SJohn.Forte@Sun.COM void *proc_nskernd;
37*7836SJohn.Forte@Sun.COM int nskernd_iscluster;
38*7836SJohn.Forte@Sun.COM 
39*7836SJohn.Forte@Sun.COM static kmutex_t nskernd_lock;
40*7836SJohn.Forte@Sun.COM 
41*7836SJohn.Forte@Sun.COM static kcondvar_t nskernd_ask_cv;
42*7836SJohn.Forte@Sun.COM static kcondvar_t nskernd_k_cv;
43*7836SJohn.Forte@Sun.COM static kcondvar_t nskernd_u_cv;
44*7836SJohn.Forte@Sun.COM 
45*7836SJohn.Forte@Sun.COM static volatile int nskernd_k_wait;
46*7836SJohn.Forte@Sun.COM static volatile int nskernd_u_wait;
47*7836SJohn.Forte@Sun.COM 
48*7836SJohn.Forte@Sun.COM static int nskernd_norun;
49*7836SJohn.Forte@Sun.COM 
50*7836SJohn.Forte@Sun.COM static volatile int nskernd_ask;
51*7836SJohn.Forte@Sun.COM static struct nskernd nskernd_kdata;
52*7836SJohn.Forte@Sun.COM 
53*7836SJohn.Forte@Sun.COM void
nskernd_init(void)54*7836SJohn.Forte@Sun.COM nskernd_init(void)
55*7836SJohn.Forte@Sun.COM {
56*7836SJohn.Forte@Sun.COM 	mutex_init(&nskernd_lock, NULL, MUTEX_DRIVER, NULL);
57*7836SJohn.Forte@Sun.COM 	cv_init(&nskernd_ask_cv, NULL, CV_DRIVER, NULL);
58*7836SJohn.Forte@Sun.COM 	cv_init(&nskernd_k_cv, NULL, CV_DRIVER, NULL);
59*7836SJohn.Forte@Sun.COM 	cv_init(&nskernd_u_cv, NULL, CV_DRIVER, NULL);
60*7836SJohn.Forte@Sun.COM 
61*7836SJohn.Forte@Sun.COM 	nskernd_norun = 0;
62*7836SJohn.Forte@Sun.COM }
63*7836SJohn.Forte@Sun.COM 
64*7836SJohn.Forte@Sun.COM 
65*7836SJohn.Forte@Sun.COM void
nskernd_deinit(void)66*7836SJohn.Forte@Sun.COM nskernd_deinit(void)
67*7836SJohn.Forte@Sun.COM {
68*7836SJohn.Forte@Sun.COM 	mutex_destroy(&nskernd_lock);
69*7836SJohn.Forte@Sun.COM 	cv_destroy(&nskernd_ask_cv);
70*7836SJohn.Forte@Sun.COM 	cv_destroy(&nskernd_k_cv);
71*7836SJohn.Forte@Sun.COM 	cv_destroy(&nskernd_u_cv);
72*7836SJohn.Forte@Sun.COM }
73*7836SJohn.Forte@Sun.COM 
74*7836SJohn.Forte@Sun.COM 
75*7836SJohn.Forte@Sun.COM static int
nskernd_start(const int iscluster)76*7836SJohn.Forte@Sun.COM nskernd_start(const int iscluster)
77*7836SJohn.Forte@Sun.COM {
78*7836SJohn.Forte@Sun.COM 	int rc = 0;
79*7836SJohn.Forte@Sun.COM 
80*7836SJohn.Forte@Sun.COM 	mutex_enter(&nskernd_lock);
81*7836SJohn.Forte@Sun.COM 
82*7836SJohn.Forte@Sun.COM 	if (proc_nskernd != NULL) {
83*7836SJohn.Forte@Sun.COM 		rc = 1;
84*7836SJohn.Forte@Sun.COM 	} else if (nskernd_norun != 0) {
85*7836SJohn.Forte@Sun.COM 		rc = 2;
86*7836SJohn.Forte@Sun.COM 	} else {
87*7836SJohn.Forte@Sun.COM 		(void) drv_getparm(UPROCP, (void *)&proc_nskernd);
88*7836SJohn.Forte@Sun.COM 		nskernd_iscluster = iscluster;
89*7836SJohn.Forte@Sun.COM 	}
90*7836SJohn.Forte@Sun.COM 
91*7836SJohn.Forte@Sun.COM 	mutex_exit(&nskernd_lock);
92*7836SJohn.Forte@Sun.COM 
93*7836SJohn.Forte@Sun.COM 	return (rc);
94*7836SJohn.Forte@Sun.COM }
95*7836SJohn.Forte@Sun.COM 
96*7836SJohn.Forte@Sun.COM 
97*7836SJohn.Forte@Sun.COM /*
98*7836SJohn.Forte@Sun.COM  * must be called with nskernd_lock held.
99*7836SJohn.Forte@Sun.COM  */
100*7836SJohn.Forte@Sun.COM void
nskernd_cleanup(void)101*7836SJohn.Forte@Sun.COM nskernd_cleanup(void)
102*7836SJohn.Forte@Sun.COM {
103*7836SJohn.Forte@Sun.COM 	proc_nskernd = NULL;
104*7836SJohn.Forte@Sun.COM 	cv_broadcast(&nskernd_ask_cv);
105*7836SJohn.Forte@Sun.COM 	cv_broadcast(&nskernd_k_cv);
106*7836SJohn.Forte@Sun.COM }
107*7836SJohn.Forte@Sun.COM 
108*7836SJohn.Forte@Sun.COM 
109*7836SJohn.Forte@Sun.COM void
nskernd_stop(void)110*7836SJohn.Forte@Sun.COM nskernd_stop(void)
111*7836SJohn.Forte@Sun.COM {
112*7836SJohn.Forte@Sun.COM 	mutex_enter(&nskernd_lock);
113*7836SJohn.Forte@Sun.COM 
114*7836SJohn.Forte@Sun.COM 	if (proc_nskernd == NULL) {
115*7836SJohn.Forte@Sun.COM 		nskernd_norun = 1;
116*7836SJohn.Forte@Sun.COM 		mutex_exit(&nskernd_lock);
117*7836SJohn.Forte@Sun.COM 		return;
118*7836SJohn.Forte@Sun.COM 	}
119*7836SJohn.Forte@Sun.COM 
120*7836SJohn.Forte@Sun.COM 	while (nskernd_u_wait == 0) {
121*7836SJohn.Forte@Sun.COM 		nskernd_k_wait++;
122*7836SJohn.Forte@Sun.COM 		cv_wait(&nskernd_k_cv, &nskernd_lock);
123*7836SJohn.Forte@Sun.COM 		nskernd_k_wait--;
124*7836SJohn.Forte@Sun.COM 
125*7836SJohn.Forte@Sun.COM 		if (proc_nskernd == NULL) {
126*7836SJohn.Forte@Sun.COM 			mutex_exit(&nskernd_lock);
127*7836SJohn.Forte@Sun.COM 			return;
128*7836SJohn.Forte@Sun.COM 		}
129*7836SJohn.Forte@Sun.COM 	}
130*7836SJohn.Forte@Sun.COM 
131*7836SJohn.Forte@Sun.COM 	nskernd_kdata.command = NSKERND_STOP;
132*7836SJohn.Forte@Sun.COM 	nskernd_kdata.data1 = (uint64_t)1;	/* kernel has done cleanup */
133*7836SJohn.Forte@Sun.COM 
134*7836SJohn.Forte@Sun.COM 	nskernd_cleanup();
135*7836SJohn.Forte@Sun.COM 
136*7836SJohn.Forte@Sun.COM 	cv_signal(&nskernd_u_cv);
137*7836SJohn.Forte@Sun.COM 	mutex_exit(&nskernd_lock);
138*7836SJohn.Forte@Sun.COM }
139*7836SJohn.Forte@Sun.COM 
140*7836SJohn.Forte@Sun.COM 
141*7836SJohn.Forte@Sun.COM int
nskernd_get(struct nskernd * nskp)142*7836SJohn.Forte@Sun.COM nskernd_get(struct nskernd *nskp)
143*7836SJohn.Forte@Sun.COM {
144*7836SJohn.Forte@Sun.COM 	mutex_enter(&nskernd_lock);
145*7836SJohn.Forte@Sun.COM 
146*7836SJohn.Forte@Sun.COM 	if (proc_nskernd == NULL) {
147*7836SJohn.Forte@Sun.COM 		mutex_exit(&nskernd_lock);
148*7836SJohn.Forte@Sun.COM 		return (ENXIO);
149*7836SJohn.Forte@Sun.COM 	}
150*7836SJohn.Forte@Sun.COM 
151*7836SJohn.Forte@Sun.COM 	while (nskernd_u_wait == 0 || nskernd_ask) {
152*7836SJohn.Forte@Sun.COM 		nskernd_k_wait++;
153*7836SJohn.Forte@Sun.COM 		cv_wait(&nskernd_k_cv, &nskernd_lock);
154*7836SJohn.Forte@Sun.COM 		nskernd_k_wait--;
155*7836SJohn.Forte@Sun.COM 
156*7836SJohn.Forte@Sun.COM 		if (proc_nskernd == NULL) {
157*7836SJohn.Forte@Sun.COM 			mutex_exit(&nskernd_lock);
158*7836SJohn.Forte@Sun.COM 			return (ENXIO);
159*7836SJohn.Forte@Sun.COM 		}
160*7836SJohn.Forte@Sun.COM 	}
161*7836SJohn.Forte@Sun.COM 
162*7836SJohn.Forte@Sun.COM 	bcopy(nskp, &nskernd_kdata, sizeof (*nskp));
163*7836SJohn.Forte@Sun.COM 	nskernd_ask++;
164*7836SJohn.Forte@Sun.COM 
165*7836SJohn.Forte@Sun.COM 	cv_signal(&nskernd_u_cv);
166*7836SJohn.Forte@Sun.COM 
167*7836SJohn.Forte@Sun.COM 	cv_wait(&nskernd_ask_cv, &nskernd_lock);
168*7836SJohn.Forte@Sun.COM 
169*7836SJohn.Forte@Sun.COM 	if (proc_nskernd == NULL) {
170*7836SJohn.Forte@Sun.COM 		nskernd_ask--;
171*7836SJohn.Forte@Sun.COM 		mutex_exit(&nskernd_lock);
172*7836SJohn.Forte@Sun.COM 		return (ENXIO);
173*7836SJohn.Forte@Sun.COM 	}
174*7836SJohn.Forte@Sun.COM 
175*7836SJohn.Forte@Sun.COM 	bcopy(&nskernd_kdata, nskp, sizeof (*nskp));
176*7836SJohn.Forte@Sun.COM 	nskernd_ask--;
177*7836SJohn.Forte@Sun.COM 
178*7836SJohn.Forte@Sun.COM 	if (nskernd_k_wait > 0)
179*7836SJohn.Forte@Sun.COM 		cv_signal(&nskernd_k_cv);
180*7836SJohn.Forte@Sun.COM 
181*7836SJohn.Forte@Sun.COM 	mutex_exit(&nskernd_lock);
182*7836SJohn.Forte@Sun.COM 	return (0);
183*7836SJohn.Forte@Sun.COM }
184*7836SJohn.Forte@Sun.COM 
185*7836SJohn.Forte@Sun.COM 
186*7836SJohn.Forte@Sun.COM int
nskernd_command(intptr_t arg,int mode,int * rvalp)187*7836SJohn.Forte@Sun.COM nskernd_command(intptr_t arg, int mode, int *rvalp)
188*7836SJohn.Forte@Sun.COM {
189*7836SJohn.Forte@Sun.COM 	struct nskernd *udata = NULL;
190*7836SJohn.Forte@Sun.COM 	uint64_t arg1, arg2;
191*7836SJohn.Forte@Sun.COM 	int rc;
192*7836SJohn.Forte@Sun.COM 
193*7836SJohn.Forte@Sun.COM 	*rvalp = 0;
194*7836SJohn.Forte@Sun.COM 	rc = 0;
195*7836SJohn.Forte@Sun.COM 
196*7836SJohn.Forte@Sun.COM 	udata = kmem_alloc(sizeof (*udata), KM_SLEEP);
197*7836SJohn.Forte@Sun.COM 	if (ddi_copyin((void *)arg, udata, sizeof (*udata), mode) < 0) {
198*7836SJohn.Forte@Sun.COM 		kmem_free(udata, sizeof (*udata));
199*7836SJohn.Forte@Sun.COM 		return (EFAULT);
200*7836SJohn.Forte@Sun.COM 	}
201*7836SJohn.Forte@Sun.COM 
202*7836SJohn.Forte@Sun.COM 	switch (udata->command) {
203*7836SJohn.Forte@Sun.COM 	case NSKERND_START:		/* User program start */
204*7836SJohn.Forte@Sun.COM 		*rvalp = nskernd_start(udata->data1);
205*7836SJohn.Forte@Sun.COM 		break;
206*7836SJohn.Forte@Sun.COM 
207*7836SJohn.Forte@Sun.COM 	case NSKERND_STOP:		/* User program requesting stop */
208*7836SJohn.Forte@Sun.COM 		mutex_enter(&nskernd_lock);
209*7836SJohn.Forte@Sun.COM 		nskernd_cleanup();
210*7836SJohn.Forte@Sun.COM 		mutex_exit(&nskernd_lock);
211*7836SJohn.Forte@Sun.COM 		break;
212*7836SJohn.Forte@Sun.COM 
213*7836SJohn.Forte@Sun.COM 	case NSKERND_WAIT:
214*7836SJohn.Forte@Sun.COM 		mutex_enter(&nskernd_lock);
215*7836SJohn.Forte@Sun.COM 
216*7836SJohn.Forte@Sun.COM 		bcopy(udata, &nskernd_kdata, sizeof (*udata));
217*7836SJohn.Forte@Sun.COM 
218*7836SJohn.Forte@Sun.COM 		if (nskernd_ask > 0)
219*7836SJohn.Forte@Sun.COM 			cv_signal(&nskernd_ask_cv);
220*7836SJohn.Forte@Sun.COM 
221*7836SJohn.Forte@Sun.COM 		nskernd_u_wait++;
222*7836SJohn.Forte@Sun.COM 
223*7836SJohn.Forte@Sun.COM 		if (cv_wait_sig(&nskernd_u_cv, &nskernd_lock) != 0) {
224*7836SJohn.Forte@Sun.COM 			/*
225*7836SJohn.Forte@Sun.COM 			 * woken by cv_signal() or cv_broadcast()
226*7836SJohn.Forte@Sun.COM 			 */
227*7836SJohn.Forte@Sun.COM 			bcopy(&nskernd_kdata, udata, sizeof (*udata));
228*7836SJohn.Forte@Sun.COM 		} else {
229*7836SJohn.Forte@Sun.COM 			/*
230*7836SJohn.Forte@Sun.COM 			 * signal - the user process has blocked all
231*7836SJohn.Forte@Sun.COM 			 * signals except for SIGTERM and the
232*7836SJohn.Forte@Sun.COM 			 * uncatchables, so the process is about to die
233*7836SJohn.Forte@Sun.COM 			 * and we need to clean up.
234*7836SJohn.Forte@Sun.COM 			 */
235*7836SJohn.Forte@Sun.COM 			udata->command = NSKERND_STOP;
236*7836SJohn.Forte@Sun.COM 			udata->data1 = (uint64_t)1;	 /* cleanup done */
237*7836SJohn.Forte@Sun.COM 
238*7836SJohn.Forte@Sun.COM 			nskernd_cleanup();
239*7836SJohn.Forte@Sun.COM 		}
240*7836SJohn.Forte@Sun.COM 
241*7836SJohn.Forte@Sun.COM 		nskernd_u_wait--;
242*7836SJohn.Forte@Sun.COM 
243*7836SJohn.Forte@Sun.COM 		mutex_exit(&nskernd_lock);
244*7836SJohn.Forte@Sun.COM 
245*7836SJohn.Forte@Sun.COM 		if (ddi_copyout(udata, (void *)arg,
246*7836SJohn.Forte@Sun.COM 		    sizeof (*udata), mode) < 0) {
247*7836SJohn.Forte@Sun.COM 			rc = EFAULT;
248*7836SJohn.Forte@Sun.COM 			break;
249*7836SJohn.Forte@Sun.COM 		}
250*7836SJohn.Forte@Sun.COM 
251*7836SJohn.Forte@Sun.COM 		break;
252*7836SJohn.Forte@Sun.COM 
253*7836SJohn.Forte@Sun.COM 	case NSKERND_NEWLWP:
254*7836SJohn.Forte@Sun.COM 		/* save kmem by freeing the udata structure */
255*7836SJohn.Forte@Sun.COM 		arg1 = udata->data1;
256*7836SJohn.Forte@Sun.COM 		kmem_free(udata, sizeof (*udata));
257*7836SJohn.Forte@Sun.COM 		udata = NULL;
258*7836SJohn.Forte@Sun.COM 		nsc_runlwp(arg1);
259*7836SJohn.Forte@Sun.COM 		break;
260*7836SJohn.Forte@Sun.COM 
261*7836SJohn.Forte@Sun.COM 	case NSKERND_LOCK:
262*7836SJohn.Forte@Sun.COM 		/* save kmem by freeing the udata structure */
263*7836SJohn.Forte@Sun.COM 		arg1 = udata->data1;
264*7836SJohn.Forte@Sun.COM 		arg2 = udata->data2;
265*7836SJohn.Forte@Sun.COM 		kmem_free(udata, sizeof (*udata));
266*7836SJohn.Forte@Sun.COM 		udata = NULL;
267*7836SJohn.Forte@Sun.COM 		nsc_lockchild(arg1, arg2);
268*7836SJohn.Forte@Sun.COM 		break;
269*7836SJohn.Forte@Sun.COM 
270*7836SJohn.Forte@Sun.COM 	default:
271*7836SJohn.Forte@Sun.COM 		cmn_err(CE_WARN, "nskernd: unknown command %d", udata->command);
272*7836SJohn.Forte@Sun.COM 		rc = EINVAL;
273*7836SJohn.Forte@Sun.COM 		break;
274*7836SJohn.Forte@Sun.COM 	}
275*7836SJohn.Forte@Sun.COM 
276*7836SJohn.Forte@Sun.COM 	if (udata != NULL) {
277*7836SJohn.Forte@Sun.COM 		kmem_free(udata, sizeof (*udata));
278*7836SJohn.Forte@Sun.COM 		udata = NULL;
279*7836SJohn.Forte@Sun.COM 	}
280*7836SJohn.Forte@Sun.COM 
281*7836SJohn.Forte@Sun.COM 	return (rc);
282*7836SJohn.Forte@Sun.COM }
283*7836SJohn.Forte@Sun.COM 
284*7836SJohn.Forte@Sun.COM /*
285*7836SJohn.Forte@Sun.COM  * This function is included for SV ioctl processing only.
286*7836SJohn.Forte@Sun.COM  */
287*7836SJohn.Forte@Sun.COM 
288*7836SJohn.Forte@Sun.COM int
nskernd_isdaemon(void)289*7836SJohn.Forte@Sun.COM nskernd_isdaemon(void)
290*7836SJohn.Forte@Sun.COM {
291*7836SJohn.Forte@Sun.COM 	void *this_proc;
292*7836SJohn.Forte@Sun.COM 
293*7836SJohn.Forte@Sun.COM 	if (proc_nskernd == NULL)
294*7836SJohn.Forte@Sun.COM 		return (0);
295*7836SJohn.Forte@Sun.COM 	if (drv_getparm(UPROCP, (void *)&this_proc) != 0)
296*7836SJohn.Forte@Sun.COM 		return (0);
297*7836SJohn.Forte@Sun.COM 	return (proc_nskernd == this_proc);
298*7836SJohn.Forte@Sun.COM }
299