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
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22*722Smuffin /*
23*722Smuffin * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
24*722Smuffin * Use is subject to license terms.
25*722Smuffin */
260Sstevel@tonic-gate
27*722Smuffin #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
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <sys/mman.h>
380Sstevel@tonic-gate #include <sys/lock.h>
390Sstevel@tonic-gate #include <sys/time.h>
400Sstevel@tonic-gate #include <sys/resource.h>
410Sstevel@tonic-gate #include <machine/param.h>
420Sstevel@tonic-gate #include <machine/vmparam.h>
430Sstevel@tonic-gate #include <a.out.h>
440Sstevel@tonic-gate #include <link.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * Globals we reference.
490Sstevel@tonic-gate */
500Sstevel@tonic-gate extern struct link_dynamic _DYNAMIC;
510Sstevel@tonic-gate extern int mlock();
520Sstevel@tonic-gate extern int munlock();
53*722Smuffin extern caddr_t sbrk(); /* find end of data segment */
54*722Smuffin extern caddr_t etext; /* end of text segment */
550Sstevel@tonic-gate
560Sstevel@tonic-gate /*
570Sstevel@tonic-gate * Module-scope variables.
580Sstevel@tonic-gate */
590Sstevel@tonic-gate static int page_size = 0; /* cached result of getpagesize() */
600Sstevel@tonic-gate static int lock_state = 0; /* lock state */
610Sstevel@tonic-gate static int state_pid = -1; /* pid to which state belongs */
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * Local worker routine to lock text and data segments. Handles
650Sstevel@tonic-gate * dynamically loaded objects. This routine is highly dependent
660Sstevel@tonic-gate * on executable format and layout.
67*722Smuffin *
68*722Smuffin * Arguments:
69*722Smuffin * op: desired operation
70*722Smuffin * f: function to perform
710Sstevel@tonic-gate */
720Sstevel@tonic-gate static int
apply_lock(int op,int (* f)(caddr_t,u_int))73*722Smuffin apply_lock(int op, int (*f)(caddr_t, u_int))
740Sstevel@tonic-gate {
750Sstevel@tonic-gate int e = 0; /* return value */
760Sstevel@tonic-gate caddr_t a; /* address of operation */
770Sstevel@tonic-gate u_int l; /* length of operation */
780Sstevel@tonic-gate struct link_map *lmp; /* link map walker */
790Sstevel@tonic-gate struct exec *eh; /* exec header */
800Sstevel@tonic-gate
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Operate on application segment first.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate switch (op) {
850Sstevel@tonic-gate case TXTLOCK:
860Sstevel@tonic-gate a = (caddr_t)USRTEXT; /* note: old Sun-2 not handled */
870Sstevel@tonic-gate l = (u_int)&etext - USRTEXT;
880Sstevel@tonic-gate break;
890Sstevel@tonic-gate case DATLOCK:
900Sstevel@tonic-gate a = (caddr_t)(((int)&etext + (SEGSIZ - 1)) & ~(SEGSIZ - 1));
910Sstevel@tonic-gate l = (u_int)(sbrk(0) - a);
920Sstevel@tonic-gate break;
930Sstevel@tonic-gate }
940Sstevel@tonic-gate l = (l + (page_size - 1)) & (u_int)~(page_size - 1);
950Sstevel@tonic-gate
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate * Perform the operation -- if failure, return immediately.
980Sstevel@tonic-gate */
990Sstevel@tonic-gate if (e = (*f)(a, l))
1000Sstevel@tonic-gate return (e);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate /*
1030Sstevel@tonic-gate * If we're not a dynamically linked program, we are finished.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate if (&_DYNAMIC == 0)
1060Sstevel@tonic-gate return (0);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Find the list of dynamically linked objects. If we get
1100Sstevel@tonic-gate * dynamic linking formats we don't recognize, then punt.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate switch (_DYNAMIC.ld_version) {
1130Sstevel@tonic-gate case 2:
114*722Smuffin #if defined(__sparc)
1150Sstevel@tonic-gate case 3:
116*722Smuffin #endif /* __sparc */
1170Sstevel@tonic-gate lmp = _DYNAMIC.ld_un.ld_2->ld_loaded;
1180Sstevel@tonic-gate break;
1190Sstevel@tonic-gate default:
1200Sstevel@tonic-gate return (0);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate * Loop over all objects. Extract the addresses and lengths as
1250Sstevel@tonic-gate * required, and perform the appropriate operation.
1260Sstevel@tonic-gate */
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate while (lmp) {
1290Sstevel@tonic-gate eh = (struct exec *)lmp->lm_addr;
1300Sstevel@tonic-gate switch (op) {
1310Sstevel@tonic-gate case TXTLOCK:
1320Sstevel@tonic-gate a = (caddr_t)eh;
1330Sstevel@tonic-gate l = (u_int)eh->a_text;
1340Sstevel@tonic-gate break;
1350Sstevel@tonic-gate case DATLOCK:
1360Sstevel@tonic-gate a = (caddr_t)((u_int)eh + N_DATADDR(*eh) -
1370Sstevel@tonic-gate N_TXTADDR(*eh));
1380Sstevel@tonic-gate l = (u_int)eh->a_data + (u_int)eh->a_bss;
1390Sstevel@tonic-gate break;
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate l = (l + (page_size - 1)) & ~(page_size - 1);
1420Sstevel@tonic-gate if (e = (*f)(a, l))
1430Sstevel@tonic-gate return (e);
1440Sstevel@tonic-gate lmp = lmp->lm_next;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate return (0);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate
1490Sstevel@tonic-gate /*
150*722Smuffin * plock
151*722Smuffin *
152*722Smuffin * Argument:
153*722Smuffin * op: desired operation
1540Sstevel@tonic-gate */
1550Sstevel@tonic-gate int
plock(int op)156*722Smuffin plock(int op)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate int e = 0; /* return value */
1590Sstevel@tonic-gate int pid; /* current pid */
1600Sstevel@tonic-gate caddr_t a1, a2; /* loop variables */
1610Sstevel@tonic-gate struct rlimit rl; /* resource limit */
1620Sstevel@tonic-gate
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate * Initialize static caches.
1650Sstevel@tonic-gate */
1660Sstevel@tonic-gate if (page_size == 0)
1670Sstevel@tonic-gate page_size = getpagesize();
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Validate state of lock's. If parent has forked, then
1710Sstevel@tonic-gate * the lock state needs to be reset (children do not inherit
1720Sstevel@tonic-gate * memory locks, and thus do not inherit their state).
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate if ((pid = getpid()) != state_pid) {
1750Sstevel@tonic-gate lock_state = 0;
1760Sstevel@tonic-gate state_pid = pid;
1770Sstevel@tonic-gate }
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate * Dispatch on operation. Note: plock and its relatives depend
1810Sstevel@tonic-gate * upon "op" being bit encoded.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate switch (op) {
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * UNLOCK: remove all memory locks. Requires that some be set!
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate case UNLOCK:
1890Sstevel@tonic-gate if (lock_state == 0) {
1900Sstevel@tonic-gate errno = EINVAL;
1910Sstevel@tonic-gate return (-1);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate if (e = munlockall())
1940Sstevel@tonic-gate return (-1);
1950Sstevel@tonic-gate else {
1960Sstevel@tonic-gate lock_state = 0;
1970Sstevel@tonic-gate return (0);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate /*NOTREACHED*/
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /*
2020Sstevel@tonic-gate * TXTLOCK: locks text segments.
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate case TXTLOCK:
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate * If a text or process lock is already set, then fail.
2080Sstevel@tonic-gate */
2090Sstevel@tonic-gate if ((lock_state & TXTLOCK) || (lock_state & PROCLOCK)) {
2100Sstevel@tonic-gate errno = EINVAL;
2110Sstevel@tonic-gate return (-1);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate * Try to apply the lock(s). If a failure occurs,
2160Sstevel@tonic-gate * back them out. On success, remember that a text
2170Sstevel@tonic-gate * lock was set.
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate if (e = apply_lock(op, mlock))
2200Sstevel@tonic-gate (void) apply_lock(op, munlock);
2210Sstevel@tonic-gate else
2220Sstevel@tonic-gate lock_state |= TXTLOCK;
2230Sstevel@tonic-gate return (e);
2240Sstevel@tonic-gate /*NOTREACHED*/
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate * DATLOCK: locks data segment(s), including the stack and all
2280Sstevel@tonic-gate * future growth in the address space.
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate case DATLOCK:
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * If a data or process lock is already set, then fail.
2340Sstevel@tonic-gate */
2350Sstevel@tonic-gate if ((lock_state & DATLOCK) || (lock_state & PROCLOCK)) {
2360Sstevel@tonic-gate errno = EINVAL;
2370Sstevel@tonic-gate return (-1);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /*
2410Sstevel@tonic-gate * Try to lock the data segments. On failure, back out
2420Sstevel@tonic-gate * the locks and return.
2430Sstevel@tonic-gate */
2440Sstevel@tonic-gate if (e = apply_lock(op, mlock)) {
2450Sstevel@tonic-gate (void) apply_lock(op, munlock);
2460Sstevel@tonic-gate return (-1);
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate
2490Sstevel@tonic-gate /*
2500Sstevel@tonic-gate * Try to lock the stack segment. Find out the extent
2510Sstevel@tonic-gate * and start of the stack (there should be a function for
2520Sstevel@tonic-gate * this!) and then iterate over the pages of the stack
2530Sstevel@tonic-gate * locking them. The stack *could* be sparely populated.
2540Sstevel@tonic-gate * Ignore lock failures resulting from the absence of a
2550Sstevel@tonic-gate * mapping.
2560Sstevel@tonic-gate */
2570Sstevel@tonic-gate (void) getrlimit(RLIMIT_STACK, &rl);
2580Sstevel@tonic-gate for (a1 = (caddr_t)USRSTACK - page_size;
2590Sstevel@tonic-gate a1 != (caddr_t)USRSTACK - rl.rlim_cur; a1 -= page_size)
2600Sstevel@tonic-gate if (e = mlock(a1, page_size)) {
2610Sstevel@tonic-gate if (errno == ENOMEM)
2620Sstevel@tonic-gate e = 0;
2630Sstevel@tonic-gate break;
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2670Sstevel@tonic-gate * If we were successful in locking the stack, then
2680Sstevel@tonic-gate * try to set a lock for all future mappings.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate if (!e)
2710Sstevel@tonic-gate e = mlockall(MCL_FUTURE);
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * If failures have occurred, back out the locks
2750Sstevel@tonic-gate * and return failure.
2760Sstevel@tonic-gate */
2770Sstevel@tonic-gate if (e) {
2780Sstevel@tonic-gate e = errno;
2790Sstevel@tonic-gate (void) apply_lock(op, munlock);
2800Sstevel@tonic-gate for (a2 = (caddr_t)USRSTACK - page_size; a2 != a1;
2810Sstevel@tonic-gate a2 -= page_size)
2820Sstevel@tonic-gate (void) munlock(a2, page_size);
2830Sstevel@tonic-gate errno = e;
2840Sstevel@tonic-gate return (-1);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * Data, stack, and growth have been locked. Set state
2890Sstevel@tonic-gate * and return success.
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate lock_state |= DATLOCK;
2920Sstevel@tonic-gate return (0);
2930Sstevel@tonic-gate /*NOTREACHED*/
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate /*
2960Sstevel@tonic-gate * PROCLOCK: lock everything, and all future things as well.
2970Sstevel@tonic-gate * There should be nothing locked when this is called.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate case PROCLOCK:
3000Sstevel@tonic-gate if (lock_state) {
3010Sstevel@tonic-gate errno = EINVAL;
3020Sstevel@tonic-gate return (-1);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate if (mlockall(MCL_CURRENT | MCL_FUTURE) == 0) {
3050Sstevel@tonic-gate lock_state |= PROCLOCK;
3060Sstevel@tonic-gate return (0);
3070Sstevel@tonic-gate } else
3080Sstevel@tonic-gate return (-1);
3090Sstevel@tonic-gate /*NOTREACHED*/
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate * Invalid operation.
3130Sstevel@tonic-gate */
3140Sstevel@tonic-gate default:
3150Sstevel@tonic-gate errno = EINVAL;
3160Sstevel@tonic-gate return (-1);
3170Sstevel@tonic-gate /*NOTREACHED*/
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate }
320