xref: /onnv-gate/usr/src/uts/common/os/proc.c (revision 1217:f95ffdc997b7)
1*1217Srab /*
2*1217Srab  * CDDL HEADER START
3*1217Srab  *
4*1217Srab  * The contents of this file are subject to the terms of the
5*1217Srab  * Common Development and Distribution License, Version 1.0 only
6*1217Srab  * (the "License").  You may not use this file except in compliance
7*1217Srab  * with the License.
8*1217Srab  *
9*1217Srab  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*1217Srab  * or http://www.opensolaris.org/os/licensing.
11*1217Srab  * See the License for the specific language governing permissions
12*1217Srab  * and limitations under the License.
13*1217Srab  *
14*1217Srab  * When distributing Covered Code, include this CDDL HEADER in each
15*1217Srab  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*1217Srab  * If applicable, add the following below this CDDL HEADER, with the
17*1217Srab  * fields enclosed by brackets "[]" replaced with your own identifying
18*1217Srab  * information: Portions Copyright [yyyy] [name of copyright owner]
19*1217Srab  *
20*1217Srab  * CDDL HEADER END
21*1217Srab  */
22*1217Srab /*
23*1217Srab  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1217Srab  * Use is subject to license terms.
25*1217Srab  */
26*1217Srab 
27*1217Srab #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*1217Srab 
29*1217Srab #include <sys/proc.h>
30*1217Srab 
31*1217Srab /*
32*1217Srab  * Install process context ops for the current process.
33*1217Srab  */
34*1217Srab void
installpctx(proc_t * p,void * arg,void (* save)(void *),void (* restore)(void *),void (* fork)(void *,void *),void (* exit)(void *),void (* free)(void *,int))35*1217Srab installpctx(
36*1217Srab 	proc_t *p,
37*1217Srab 	void	*arg,
38*1217Srab 	void	(*save)(void *),
39*1217Srab 	void	(*restore)(void *),
40*1217Srab 	void	(*fork)(void *, void *),
41*1217Srab 	void	(*exit)(void *),
42*1217Srab 	void	(*free)(void *, int))
43*1217Srab {
44*1217Srab 	struct pctxop *pctx;
45*1217Srab 
46*1217Srab 	pctx = kmem_alloc(sizeof (struct pctxop), KM_SLEEP);
47*1217Srab 	pctx->save_op = save;
48*1217Srab 	pctx->restore_op = restore;
49*1217Srab 	pctx->fork_op = fork;
50*1217Srab 	pctx->exit_op = exit;
51*1217Srab 	pctx->free_op = free;
52*1217Srab 	pctx->arg = arg;
53*1217Srab 	pctx->next = p->p_pctx;
54*1217Srab 	p->p_pctx = pctx;
55*1217Srab }
56*1217Srab 
57*1217Srab /*
58*1217Srab  * Remove a process context ops from the current process.
59*1217Srab  */
60*1217Srab int
removepctx(proc_t * p,void * arg,void (* save)(void *),void (* restore)(void *),void (* fork)(void *,void *),void (* exit)(void *),void (* free)(void *,int))61*1217Srab removepctx(
62*1217Srab 	proc_t *p,
63*1217Srab 	void	*arg,
64*1217Srab 	void	(*save)(void *),
65*1217Srab 	void	(*restore)(void *),
66*1217Srab 	void	(*fork)(void *, void *),
67*1217Srab 	void	(*exit)(void *),
68*1217Srab 	void	(*free)(void *, int))
69*1217Srab {
70*1217Srab 	struct pctxop *pctx, *prev_pctx;
71*1217Srab 
72*1217Srab 	prev_pctx = NULL;
73*1217Srab 	for (pctx = p->p_pctx; pctx != NULL; pctx = pctx->next) {
74*1217Srab 		if (pctx->save_op == save && pctx->restore_op == restore &&
75*1217Srab 		    pctx->fork_op == fork &&
76*1217Srab 		    pctx->exit_op == exit && pctx->free_op == free &&
77*1217Srab 		    pctx->arg == arg) {
78*1217Srab 			if (prev_pctx)
79*1217Srab 				prev_pctx->next = pctx->next;
80*1217Srab 			else
81*1217Srab 				p->p_pctx = pctx->next;
82*1217Srab 			if (pctx->free_op != NULL)
83*1217Srab 				(pctx->free_op)(pctx->arg, 0);
84*1217Srab 			kmem_free(pctx, sizeof (struct pctxop));
85*1217Srab 			return (1);
86*1217Srab 		}
87*1217Srab 		prev_pctx = pctx;
88*1217Srab 	}
89*1217Srab 	return (0);
90*1217Srab }
91*1217Srab 
92*1217Srab void
savepctx(proc_t * p)93*1217Srab savepctx(proc_t *p)
94*1217Srab {
95*1217Srab 	struct pctxop *pctx;
96*1217Srab 
97*1217Srab 	ASSERT(p == curthread->t_procp);
98*1217Srab 	for (pctx = p->p_pctx; pctx != 0; pctx = pctx->next)
99*1217Srab 		if (pctx->save_op != NULL)
100*1217Srab 			(pctx->save_op)(pctx->arg);
101*1217Srab }
102*1217Srab 
103*1217Srab void
restorepctx(proc_t * p)104*1217Srab restorepctx(proc_t *p)
105*1217Srab {
106*1217Srab 	struct pctxop *pctx;
107*1217Srab 
108*1217Srab 	ASSERT(p == curthread->t_procp);
109*1217Srab 	for (pctx = p->p_pctx; pctx != 0; pctx = pctx->next)
110*1217Srab 		if (pctx->restore_op != NULL)
111*1217Srab 			(pctx->restore_op)(pctx->arg);
112*1217Srab }
113*1217Srab 
114*1217Srab void
forkpctx(proc_t * p,proc_t * cp)115*1217Srab forkpctx(proc_t *p, proc_t *cp)
116*1217Srab {
117*1217Srab 	struct pctxop *pctx;
118*1217Srab 
119*1217Srab 	for (pctx = p->p_pctx; pctx != NULL; pctx = pctx->next)
120*1217Srab 		if (pctx->fork_op != NULL)
121*1217Srab 			(pctx->fork_op)(p, cp);
122*1217Srab }
123*1217Srab 
124*1217Srab /*
125*1217Srab  * exitpctx is called during thread/lwp exit to perform any actions
126*1217Srab  * needed when an LWP in the process leaves the processor for the last
127*1217Srab  * time. This routine is not intended to deal with freeing memory; freepctx()
128*1217Srab  * is used for that purpose during proc_exit(). This routine is provided to
129*1217Srab  * allow for clean-up that can't wait until thread_free().
130*1217Srab  */
131*1217Srab void
exitpctx(proc_t * p)132*1217Srab exitpctx(proc_t *p)
133*1217Srab {
134*1217Srab 	struct pctxop *pctx;
135*1217Srab 
136*1217Srab 	for (pctx = p->p_pctx; pctx != NULL; pctx = pctx->next)
137*1217Srab 		if (pctx->exit_op != NULL)
138*1217Srab 			(pctx->exit_op)(p);
139*1217Srab }
140*1217Srab 
141*1217Srab /*
142*1217Srab  * freepctx is called from proc_exit() to get rid of the actual context ops.
143*1217Srab  */
144*1217Srab void
freepctx(proc_t * p,int isexec)145*1217Srab freepctx(proc_t *p, int isexec)
146*1217Srab {
147*1217Srab 	struct pctxop *pctx;
148*1217Srab 
149*1217Srab 	while ((pctx = p->p_pctx) != NULL) {
150*1217Srab 		p->p_pctx = pctx->next;
151*1217Srab 		if (pctx->free_op != NULL)
152*1217Srab 			(pctx->free_op)(pctx->arg, isexec);
153*1217Srab 		kmem_free(pctx, sizeof (struct pctxop));
154*1217Srab 	}
155*1217Srab }
156