4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
29 * plock - lock "segments" in physical memory.
31 * Supports SVID-compatible plock, taking into account dynamically linked
32 * objects (such as shared libraries).
37 #include <sys/types.h>
47 * Module-scope variables.
49 static int lock_state
= 0; /* lock state */
50 static pid_t state_pid
= -1; /* pid to which state belongs */
51 static mutex_t plock_lock
= DEFAULTMUTEX
;
57 plock(int op
) /* desired operation */
59 int e
; /* return value */
60 pid_t pid
; /* current pid */
62 lmutex_lock(&plock_lock
);
65 * Validate state of lock's. If parent has forked, then
66 * the lock state needs to be reset (children do not inherit
67 * memory locks, and thus do not inherit their state).
69 if ((pid
= getpid()) != state_pid
) {
75 * Dispatch on operation. Note: plock and its relatives depend
76 * upon "op" being bit encoded.
81 * UNLOCK: remove all memory locks. Requires that some be set!
84 if (lock_state
== 0) {
86 lmutex_unlock(&plock_lock
);
91 lmutex_unlock(&plock_lock
);
95 lmutex_unlock(&plock_lock
);
101 * TXTLOCK: locks text segments.
106 * If a text or process lock is already set, then fail.
108 if ((lock_state
& TXTLOCK
) || (lock_state
& PROCLOCK
)) {
110 lmutex_unlock(&plock_lock
);
115 * Try to apply the lock(s). If a failure occurs,
116 * memcntl backs them out automatically.
118 e
= memcntl(NULL
, 0, MC_LOCKAS
, (caddr_t
)MCL_CURRENT
,
119 PROC_TEXT
|PRIVATE
, 0);
121 lock_state
|= TXTLOCK
;
122 lmutex_unlock(&plock_lock
);
127 * DATLOCK: locks data segment(s), including the stack and all
128 * future growth in the address space.
133 * If a data or process lock is already set, then fail.
135 if ((lock_state
& DATLOCK
) || (lock_state
& PROCLOCK
)) {
137 lmutex_unlock(&plock_lock
);
142 * Try to lock the data and stack segments. On failure
143 * memcntl undoes the locks internally.
145 e
= memcntl(NULL
, 0, MC_LOCKAS
, (caddr_t
)MCL_CURRENT
,
146 PROC_DATA
|PRIVATE
, 0);
148 lmutex_unlock(&plock_lock
);
152 /* try to set a lock for all future mappings. */
153 e
= mlockall(MCL_FUTURE
);
156 * If failures have occurred, back out the locks
157 * and return failure.
161 (void) memcntl(NULL
, 0, MC_UNLOCKAS
,
162 (caddr_t
)MCL_CURRENT
, PROC_DATA
|PRIVATE
, 0);
164 lmutex_unlock(&plock_lock
);
169 * Data, stack, and growth have been locked. Set state
170 * and return success.
172 lock_state
|= DATLOCK
;
173 lmutex_unlock(&plock_lock
);
178 * PROCLOCK: lock everything, and all future things as well.
179 * There should be nothing locked when this is called.
184 lmutex_unlock(&plock_lock
);
187 if (mlockall(MCL_CURRENT
| MCL_FUTURE
) == 0) {
188 lock_state
|= PROCLOCK
;
189 lmutex_unlock(&plock_lock
);
192 lmutex_unlock(&plock_lock
);
202 lmutex_unlock(&plock_lock
);