10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * plock - lock "segments" in physical memory.
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * Supports SVID-compatible plock, taking into account dynamically linked
330Sstevel@tonic-gate * objects (such as shared libraries).
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
36*6812Sraf #include "lint.h"
370Sstevel@tonic-gate #include <mtlib.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/mman.h>
400Sstevel@tonic-gate #include <sys/lock.h>
410Sstevel@tonic-gate #include <errno.h>
420Sstevel@tonic-gate #include <stddef.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate #include <thread.h>
450Sstevel@tonic-gate #include <synch.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * Module-scope variables.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate static int lock_state = 0; /* lock state */
510Sstevel@tonic-gate static pid_t state_pid = -1; /* pid to which state belongs */
520Sstevel@tonic-gate static mutex_t plock_lock = DEFAULTMUTEX;
530Sstevel@tonic-gate
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate * plock
560Sstevel@tonic-gate */
570Sstevel@tonic-gate int
plock(int op)580Sstevel@tonic-gate plock(int op) /* desired operation */
590Sstevel@tonic-gate {
600Sstevel@tonic-gate int e; /* return value */
610Sstevel@tonic-gate pid_t pid; /* current pid */
620Sstevel@tonic-gate
630Sstevel@tonic-gate lmutex_lock(&plock_lock);
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * Validate state of lock's. If parent has forked, then
670Sstevel@tonic-gate * the lock state needs to be reset (children do not inherit
680Sstevel@tonic-gate * memory locks, and thus do not inherit their state).
690Sstevel@tonic-gate */
700Sstevel@tonic-gate if ((pid = getpid()) != state_pid) {
710Sstevel@tonic-gate lock_state = 0;
720Sstevel@tonic-gate state_pid = pid;
730Sstevel@tonic-gate }
740Sstevel@tonic-gate
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * Dispatch on operation. Note: plock and its relatives depend
770Sstevel@tonic-gate * upon "op" being bit encoded.
780Sstevel@tonic-gate */
790Sstevel@tonic-gate switch (op) {
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * UNLOCK: remove all memory locks. Requires that some be set!
830Sstevel@tonic-gate */
840Sstevel@tonic-gate case UNLOCK:
850Sstevel@tonic-gate if (lock_state == 0) {
860Sstevel@tonic-gate errno = EINVAL;
870Sstevel@tonic-gate lmutex_unlock(&plock_lock);
880Sstevel@tonic-gate return (-1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate e = munlockall();
910Sstevel@tonic-gate if (e) {
920Sstevel@tonic-gate lmutex_unlock(&plock_lock);
930Sstevel@tonic-gate return (-1);
940Sstevel@tonic-gate } else {
950Sstevel@tonic-gate lock_state = 0;
960Sstevel@tonic-gate lmutex_unlock(&plock_lock);
970Sstevel@tonic-gate return (0);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate /*NOTREACHED*/
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * TXTLOCK: locks text segments.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate case TXTLOCK:
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate * If a text or process lock is already set, then fail.
1080Sstevel@tonic-gate */
1090Sstevel@tonic-gate if ((lock_state & TXTLOCK) || (lock_state & PROCLOCK)) {
1100Sstevel@tonic-gate errno = EINVAL;
1110Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1120Sstevel@tonic-gate return (-1);
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * Try to apply the lock(s). If a failure occurs,
1170Sstevel@tonic-gate * memcntl backs them out automatically.
1180Sstevel@tonic-gate */
1190Sstevel@tonic-gate e = memcntl(NULL, 0, MC_LOCKAS, (caddr_t)MCL_CURRENT,
1200Sstevel@tonic-gate PROC_TEXT|PRIVATE, (int)NULL);
1210Sstevel@tonic-gate if (!e)
1220Sstevel@tonic-gate lock_state |= TXTLOCK;
1230Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1240Sstevel@tonic-gate return (e);
1250Sstevel@tonic-gate /*NOTREACHED*/
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate * DATLOCK: locks data segment(s), including the stack and all
1290Sstevel@tonic-gate * future growth in the address space.
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate case DATLOCK:
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate /*
1340Sstevel@tonic-gate * If a data or process lock is already set, then fail.
1350Sstevel@tonic-gate */
1360Sstevel@tonic-gate if ((lock_state & DATLOCK) || (lock_state & PROCLOCK)) {
1370Sstevel@tonic-gate errno = EINVAL;
1380Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1390Sstevel@tonic-gate return (-1);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * Try to lock the data and stack segments. On failure
1440Sstevel@tonic-gate * memcntl undoes the locks internally.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate e = memcntl(NULL, 0, MC_LOCKAS, (caddr_t)MCL_CURRENT,
1470Sstevel@tonic-gate PROC_DATA|PRIVATE, (int)NULL);
1480Sstevel@tonic-gate if (e) {
1490Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1500Sstevel@tonic-gate return (-1);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /* try to set a lock for all future mappings. */
1540Sstevel@tonic-gate e = mlockall(MCL_FUTURE);
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate * If failures have occurred, back out the locks
1580Sstevel@tonic-gate * and return failure.
1590Sstevel@tonic-gate */
1600Sstevel@tonic-gate if (e) {
1610Sstevel@tonic-gate e = errno;
1620Sstevel@tonic-gate (void) memcntl(NULL, 0, MC_UNLOCKAS,
1630Sstevel@tonic-gate (caddr_t)MCL_CURRENT, PROC_DATA|PRIVATE, (int)NULL);
1640Sstevel@tonic-gate errno = e;
1650Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1660Sstevel@tonic-gate return (-1);
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Data, stack, and growth have been locked. Set state
1710Sstevel@tonic-gate * and return success.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate lock_state |= DATLOCK;
1740Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1750Sstevel@tonic-gate return (0);
1760Sstevel@tonic-gate /*NOTREACHED*/
1770Sstevel@tonic-gate
1780Sstevel@tonic-gate /*
1790Sstevel@tonic-gate * PROCLOCK: lock everything, and all future things as well.
1800Sstevel@tonic-gate * There should be nothing locked when this is called.
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate case PROCLOCK:
1830Sstevel@tonic-gate if (lock_state) {
1840Sstevel@tonic-gate errno = EINVAL;
1850Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1860Sstevel@tonic-gate return (-1);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) {
1890Sstevel@tonic-gate lock_state |= PROCLOCK;
1900Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1910Sstevel@tonic-gate return (0);
1920Sstevel@tonic-gate } else {
1930Sstevel@tonic-gate lmutex_unlock(&plock_lock);
1940Sstevel@tonic-gate return (-1);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate /*NOTREACHED*/
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * Invalid operation.
2000Sstevel@tonic-gate */
2010Sstevel@tonic-gate default:
2020Sstevel@tonic-gate errno = EINVAL;
2030Sstevel@tonic-gate lmutex_unlock(&plock_lock);
2040Sstevel@tonic-gate return (-1);
2050Sstevel@tonic-gate /*NOTREACHED*/
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate }
208