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
52414Saguzovsk * Common Development and Distribution License (the "License").
62414Saguzovsk * 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 */
217838SRoger.Faulkner@Sun.COM
220Sstevel@tonic-gate /*
23*13096SJordan.Vaughan@Sun.com * Copyright (c) 1986, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate * The Regents of the University of California
320Sstevel@tonic-gate * All Rights Reserved
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate * contributors.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate * Inter-Process Communication Shared Memory Facility.
410Sstevel@tonic-gate *
420Sstevel@tonic-gate * See os/ipc.c for a description of common IPC functionality.
430Sstevel@tonic-gate *
440Sstevel@tonic-gate * Resource controls
450Sstevel@tonic-gate * -----------------
460Sstevel@tonic-gate *
472677Sml93401 * Control: zone.max-shm-ids (rc_zone_shmmni)
482677Sml93401 * Description: Maximum number of shared memory ids allowed a zone.
492677Sml93401 *
502677Sml93401 * When shmget() is used to allocate a shared memory segment, one id
512677Sml93401 * is allocated. If the id allocation doesn't succeed, shmget()
522677Sml93401 * fails and errno is set to ENOSPC. Upon successful shmctl(,
532677Sml93401 * IPC_RMID) the id is deallocated.
542677Sml93401 *
550Sstevel@tonic-gate * Control: project.max-shm-ids (rc_project_shmmni)
560Sstevel@tonic-gate * Description: Maximum number of shared memory ids allowed a project.
570Sstevel@tonic-gate *
580Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, one id
590Sstevel@tonic-gate * is allocated. If the id allocation doesn't succeed, shmget()
600Sstevel@tonic-gate * fails and errno is set to ENOSPC. Upon successful shmctl(,
610Sstevel@tonic-gate * IPC_RMID) the id is deallocated.
620Sstevel@tonic-gate *
632677Sml93401 * Control: zone.max-shm-memory (rc_zone_shmmax)
642677Sml93401 * Description: Total amount of shared memory allowed a zone.
652677Sml93401 *
662677Sml93401 * When shmget() is used to allocate a shared memory segment, the
672677Sml93401 * segment's size is allocated against this limit. If the space
682677Sml93401 * allocation doesn't succeed, shmget() fails and errno is set to
692677Sml93401 * EINVAL. The size will be deallocated once the last process has
702677Sml93401 * detached the segment and the segment has been successfully
712677Sml93401 * shmctl(, IPC_RMID)ed.
722677Sml93401 *
730Sstevel@tonic-gate * Control: project.max-shm-memory (rc_project_shmmax)
740Sstevel@tonic-gate * Description: Total amount of shared memory allowed a project.
750Sstevel@tonic-gate *
760Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, the
770Sstevel@tonic-gate * segment's size is allocated against this limit. If the space
780Sstevel@tonic-gate * allocation doesn't succeed, shmget() fails and errno is set to
790Sstevel@tonic-gate * EINVAL. The size will be deallocated once the last process has
800Sstevel@tonic-gate * detached the segment and the segment has been successfully
810Sstevel@tonic-gate * shmctl(, IPC_RMID)ed.
820Sstevel@tonic-gate */
830Sstevel@tonic-gate
840Sstevel@tonic-gate #include <sys/types.h>
850Sstevel@tonic-gate #include <sys/param.h>
860Sstevel@tonic-gate #include <sys/cred.h>
870Sstevel@tonic-gate #include <sys/errno.h>
880Sstevel@tonic-gate #include <sys/time.h>
890Sstevel@tonic-gate #include <sys/kmem.h>
900Sstevel@tonic-gate #include <sys/user.h>
910Sstevel@tonic-gate #include <sys/proc.h>
920Sstevel@tonic-gate #include <sys/systm.h>
930Sstevel@tonic-gate #include <sys/prsystm.h>
940Sstevel@tonic-gate #include <sys/sysmacros.h>
950Sstevel@tonic-gate #include <sys/tuneable.h>
960Sstevel@tonic-gate #include <sys/vm.h>
970Sstevel@tonic-gate #include <sys/mman.h>
980Sstevel@tonic-gate #include <sys/swap.h>
990Sstevel@tonic-gate #include <sys/cmn_err.h>
1000Sstevel@tonic-gate #include <sys/debug.h>
1010Sstevel@tonic-gate #include <sys/lwpchan_impl.h>
1020Sstevel@tonic-gate #include <sys/avl.h>
1030Sstevel@tonic-gate #include <sys/modctl.h>
1040Sstevel@tonic-gate #include <sys/syscall.h>
1050Sstevel@tonic-gate #include <sys/task.h>
1060Sstevel@tonic-gate #include <sys/project.h>
1070Sstevel@tonic-gate #include <sys/policy.h>
1080Sstevel@tonic-gate #include <sys/zone.h>
1092768Ssl108498 #include <sys/rctl.h>
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate #include <sys/ipc.h>
1120Sstevel@tonic-gate #include <sys/ipc_impl.h>
1130Sstevel@tonic-gate #include <sys/shm.h>
1140Sstevel@tonic-gate #include <sys/shm_impl.h>
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate #include <vm/hat.h>
1170Sstevel@tonic-gate #include <vm/seg.h>
1180Sstevel@tonic-gate #include <vm/as.h>
1190Sstevel@tonic-gate #include <vm/seg_vn.h>
1200Sstevel@tonic-gate #include <vm/anon.h>
1210Sstevel@tonic-gate #include <vm/page.h>
1220Sstevel@tonic-gate #include <vm/vpage.h>
1230Sstevel@tonic-gate #include <vm/seg_spt.h>
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate #include <c2/audit.h>
1260Sstevel@tonic-gate
1272768Ssl108498 static int shmem_lock(kshmid_t *sp, struct anon_map *amp);
1282768Ssl108498 static void shmem_unlock(kshmid_t *sp, struct anon_map *amp);
1290Sstevel@tonic-gate static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags,
1300Sstevel@tonic-gate kshmid_t *id);
1313379Ssl108498 static void shm_rm_amp(kshmid_t *sp);
1320Sstevel@tonic-gate static void shm_dtor(kipc_perm_t *);
1330Sstevel@tonic-gate static void shm_rmid(kipc_perm_t *);
1340Sstevel@tonic-gate static void shm_remove_zone(zoneid_t, void *);
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate * Semantics for share_page_table and ism_off:
1380Sstevel@tonic-gate *
1390Sstevel@tonic-gate * These are hooks in /etc/system - only for internal testing purpose.
1400Sstevel@tonic-gate *
1410Sstevel@tonic-gate * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag
1420Sstevel@tonic-gate * in a call to shmat(2). In other words, with share_page_table set, you always
1430Sstevel@tonic-gate * get ISM, even if say, DISM is specified. It should really be called "ism_on".
1440Sstevel@tonic-gate *
1450Sstevel@tonic-gate * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to
1460Sstevel@tonic-gate * shmat(2).
1470Sstevel@tonic-gate *
1480Sstevel@tonic-gate * If both share_page_table and ism_off are set, share_page_table prevails.
1490Sstevel@tonic-gate *
1500Sstevel@tonic-gate * Although these tunables should probably be removed, they do have some
1510Sstevel@tonic-gate * external exposure; as long as they exist, they should at least work sensibly.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate int share_page_table;
1550Sstevel@tonic-gate int ism_off;
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate /*
1580Sstevel@tonic-gate * The following tunables are obsolete. Though for compatibility we
1590Sstevel@tonic-gate * still read and interpret shminfo_shmmax and shminfo_shmmni (see
1600Sstevel@tonic-gate * os/project.c), the preferred mechanism for administrating the IPC
1610Sstevel@tonic-gate * Shared Memory facility is through the resource controls described at
1620Sstevel@tonic-gate * the top of this file.
1630Sstevel@tonic-gate */
1640Sstevel@tonic-gate size_t shminfo_shmmax = 0x800000; /* (obsolete) */
1650Sstevel@tonic-gate int shminfo_shmmni = 100; /* (obsolete) */
1660Sstevel@tonic-gate size_t shminfo_shmmin = 1; /* (obsolete) */
1670Sstevel@tonic-gate int shminfo_shmseg = 6; /* (obsolete) */
1680Sstevel@tonic-gate
1692677Sml93401 extern rctl_hndl_t rc_zone_shmmax;
1702677Sml93401 extern rctl_hndl_t rc_zone_shmmni;
1710Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmax;
1720Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmni;
1730Sstevel@tonic-gate static ipc_service_t *shm_svc;
1740Sstevel@tonic-gate static zone_key_t shm_zone_key;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * Module linkage information for the kernel.
1780Sstevel@tonic-gate */
1790Sstevel@tonic-gate static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t);
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate static struct sysent ipcshm_sysent = {
1820Sstevel@tonic-gate 4,
1830Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1840Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_64RVAL,
1850Sstevel@tonic-gate #else /* _SYSCALL32_IMPL */
1860Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1870Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1880Sstevel@tonic-gate (int (*)())shmsys
1890Sstevel@tonic-gate };
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
1920Sstevel@tonic-gate static struct sysent ipcshm_sysent32 = {
1930Sstevel@tonic-gate 4,
1940Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1,
1950Sstevel@tonic-gate (int (*)())shmsys
1960Sstevel@tonic-gate };
1970Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate static struct modlsys modlsys = {
2000Sstevel@tonic-gate &mod_syscallops, "System V shared memory", &ipcshm_sysent
2010Sstevel@tonic-gate };
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2040Sstevel@tonic-gate static struct modlsys modlsys32 = {
2050Sstevel@tonic-gate &mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32
2060Sstevel@tonic-gate };
2070Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate static struct modlinkage modlinkage = {
2100Sstevel@tonic-gate MODREV_1,
2110Sstevel@tonic-gate &modlsys,
2120Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
2130Sstevel@tonic-gate &modlsys32,
2140Sstevel@tonic-gate #endif
2150Sstevel@tonic-gate NULL
2160Sstevel@tonic-gate };
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate
2190Sstevel@tonic-gate int
_init(void)2200Sstevel@tonic-gate _init(void)
2210Sstevel@tonic-gate {
2220Sstevel@tonic-gate int result;
2230Sstevel@tonic-gate
2242677Sml93401 shm_svc = ipcs_create("shmids", rc_project_shmmni, rc_zone_shmmni,
2252677Sml93401 sizeof (kshmid_t), shm_dtor, shm_rmid, AT_IPC_SHM,
2262677Sml93401 offsetof(ipc_rqty_t, ipcq_shmmni));
2270Sstevel@tonic-gate zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL);
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if ((result = mod_install(&modlinkage)) == 0)
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate (void) zone_key_delete(shm_zone_key);
2330Sstevel@tonic-gate ipcs_destroy(shm_svc);
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate return (result);
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate int
_fini(void)2390Sstevel@tonic-gate _fini(void)
2400Sstevel@tonic-gate {
2410Sstevel@tonic-gate return (EBUSY);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate int
_info(struct modinfo * modinfop)2450Sstevel@tonic-gate _info(struct modinfo *modinfop)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop));
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate * Shmat (attach shared segment) system call.
2520Sstevel@tonic-gate */
2530Sstevel@tonic-gate static int
shmat(int shmid,caddr_t uaddr,int uflags,uintptr_t * rvp)2540Sstevel@tonic-gate shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp)
2550Sstevel@tonic-gate {
2560Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */
2570Sstevel@tonic-gate size_t size;
2580Sstevel@tonic-gate int error = 0;
2590Sstevel@tonic-gate proc_t *pp = curproc;
2600Sstevel@tonic-gate struct as *as = pp->p_as;
2610Sstevel@tonic-gate struct segvn_crargs crargs; /* segvn create arguments */
2620Sstevel@tonic-gate kmutex_t *lock;
2630Sstevel@tonic-gate struct seg *segspt = NULL;
2640Sstevel@tonic-gate caddr_t addr = uaddr;
2650Sstevel@tonic-gate int flags = (uflags & SHMAT_VALID_FLAGS_MASK);
2660Sstevel@tonic-gate int useISM;
2670Sstevel@tonic-gate uchar_t prot = PROT_ALL;
2680Sstevel@tonic-gate int result;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
2710Sstevel@tonic-gate return (EINVAL);
2720Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED()))
2730Sstevel@tonic-gate goto errret;
2740Sstevel@tonic-gate if ((flags & SHM_RDONLY) == 0 &&
2750Sstevel@tonic-gate (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2760Sstevel@tonic-gate goto errret;
2770Sstevel@tonic-gate if (spt_invalid(flags)) {
2780Sstevel@tonic-gate error = EINVAL;
2790Sstevel@tonic-gate goto errret;
2800Sstevel@tonic-gate }
2810Sstevel@tonic-gate if (ism_off)
2820Sstevel@tonic-gate flags = flags & ~SHM_SHARE_MMU;
2830Sstevel@tonic-gate if (share_page_table) {
2840Sstevel@tonic-gate flags = flags & ~SHM_PAGEABLE;
2850Sstevel@tonic-gate flags = flags | SHM_SHARE_MMU;
2860Sstevel@tonic-gate }
2870Sstevel@tonic-gate useISM = (spt_locked(flags) || spt_pageable(flags));
2880Sstevel@tonic-gate if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED())))
2890Sstevel@tonic-gate goto errret;
2900Sstevel@tonic-gate if (useISM && isspt(sp)) {
2910Sstevel@tonic-gate uint_t newsptflags = flags | spt_flags(sp->shm_sptseg);
2920Sstevel@tonic-gate /*
2930Sstevel@tonic-gate * If trying to change an existing {D}ISM segment from ISM
2940Sstevel@tonic-gate * to DISM or vice versa, return error. Note that this
2950Sstevel@tonic-gate * validation of flags needs to be done after the effect of
2960Sstevel@tonic-gate * tunables such as ism_off and share_page_table, for
2970Sstevel@tonic-gate * semantics that are consistent with the tunables' settings.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate if (spt_invalid(newsptflags)) {
3000Sstevel@tonic-gate error = EINVAL;
3010Sstevel@tonic-gate goto errret;
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
3050Sstevel@tonic-gate size = sp->shm_amp->size;
3060Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate /* somewhere to record spt info for final detach */
3090Sstevel@tonic-gate if (sp->shm_sptinfo == NULL)
3100Sstevel@tonic-gate sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP);
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate as_rangelock(as);
3130Sstevel@tonic-gate
3140Sstevel@tonic-gate if (useISM) {
3150Sstevel@tonic-gate /*
3160Sstevel@tonic-gate * Handle ISM
3170Sstevel@tonic-gate */
3183446Smrj uint_t share_szc;
3190Sstevel@tonic-gate size_t share_size;
3200Sstevel@tonic-gate struct shm_data ssd;
3210Sstevel@tonic-gate uintptr_t align_hint;
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate * Pick a share pagesize to use, if (!isspt(sp)).
3250Sstevel@tonic-gate * Otherwise use the already chosen page size.
3260Sstevel@tonic-gate *
3270Sstevel@tonic-gate * For the initial shmat (!isspt(sp)), where sptcreate is
3280Sstevel@tonic-gate * called, map_pgsz is called to recommend a [D]ISM pagesize,
3290Sstevel@tonic-gate * important for systems which offer more than one potential
3300Sstevel@tonic-gate * [D]ISM pagesize.
3310Sstevel@tonic-gate * If the shmat is just to attach to an already created
3320Sstevel@tonic-gate * [D]ISM segment, then use the previously selected page size.
3330Sstevel@tonic-gate */
3340Sstevel@tonic-gate if (!isspt(sp)) {
3352991Ssusans share_size = map_pgsz(MAPPGSZ_ISM, pp, addr, size, 0);
3360Sstevel@tonic-gate if (share_size == 0) {
3370Sstevel@tonic-gate as_rangeunlock(as);
3380Sstevel@tonic-gate error = EINVAL;
3390Sstevel@tonic-gate goto errret;
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate share_szc = page_szc(share_size);
3420Sstevel@tonic-gate } else {
3430Sstevel@tonic-gate share_szc = sp->shm_sptseg->s_szc;
3440Sstevel@tonic-gate share_size = page_get_pagesize(share_szc);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate size = P2ROUNDUP(size, share_size);
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate align_hint = share_size;
3490Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
3500Sstevel@tonic-gate /*
3513446Smrj * For x86, we want to share as much of the page table tree
3523446Smrj * as possible. We use a large align_hint at first, but
3533446Smrj * if that fails, then the code below retries with align_hint
3543446Smrj * set to share_size.
3553446Smrj *
3563446Smrj * The explicit extern here is due to the difficulties
3573446Smrj * of getting to platform dependent includes. When/if the
3583446Smrj * platform dependent bits of this function are cleaned up,
3593446Smrj * another way of doing this should found.
3600Sstevel@tonic-gate */
3613446Smrj {
3623446Smrj extern uint_t ptes_per_table;
3633446Smrj
3643446Smrj while (size >= ptes_per_table * (uint64_t)align_hint)
3653446Smrj align_hint *= ptes_per_table;
3663446Smrj }
3670Sstevel@tonic-gate #endif /* __i386 || __amd64 */
3680Sstevel@tonic-gate
3690Sstevel@tonic-gate #if defined(__sparcv9)
3707838SRoger.Faulkner@Sun.COM if (addr == 0 &&
3717838SRoger.Faulkner@Sun.COM pp->p_model == DATAMODEL_LP64 && AS_TYPE_64BIT(as)) {
3720Sstevel@tonic-gate /*
3730Sstevel@tonic-gate * If no address has been passed in, and this is a
3740Sstevel@tonic-gate * 64-bit process, we'll try to find an address
3750Sstevel@tonic-gate * in the predict-ISM zone.
3760Sstevel@tonic-gate */
3770Sstevel@tonic-gate caddr_t predbase = (caddr_t)PREDISM_1T_BASE;
3780Sstevel@tonic-gate size_t len = PREDISM_BOUND - PREDISM_1T_BASE;
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate as_purge(as);
3810Sstevel@tonic-gate if (as_gap(as, size + share_size, &predbase, &len,
3820Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != -1) {
3830Sstevel@tonic-gate /*
3840Sstevel@tonic-gate * We found an address which looks like a
3850Sstevel@tonic-gate * candidate. We want to round it up, and
3860Sstevel@tonic-gate * then check that it's a valid user range.
3870Sstevel@tonic-gate * This assures that we won't fail below.
3880Sstevel@tonic-gate */
3890Sstevel@tonic-gate addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase,
3900Sstevel@tonic-gate share_size);
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate if (valid_usr_range(addr, size, prot,
3930Sstevel@tonic-gate as, as->a_userlimit) != RANGE_OKAY) {
3940Sstevel@tonic-gate addr = 0;
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate #endif /* __sparcv9 */
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate if (addr == 0) {
4010Sstevel@tonic-gate for (;;) {
4020Sstevel@tonic-gate addr = (caddr_t)align_hint;
4030Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, MAP_ALIGN);
4040Sstevel@tonic-gate if (addr != NULL || align_hint == share_size)
4050Sstevel@tonic-gate break;
4060Sstevel@tonic-gate align_hint = share_size;
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate if (addr == NULL) {
4090Sstevel@tonic-gate as_rangeunlock(as);
4100Sstevel@tonic-gate error = ENOMEM;
4110Sstevel@tonic-gate goto errret;
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0);
4140Sstevel@tonic-gate } else {
4150Sstevel@tonic-gate /* Use the user-supplied attach address */
4160Sstevel@tonic-gate caddr_t base;
4170Sstevel@tonic-gate size_t len;
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate /*
4200Sstevel@tonic-gate * Check that the address range
4210Sstevel@tonic-gate * 1) is properly aligned
4220Sstevel@tonic-gate * 2) is correct in unix terms
4230Sstevel@tonic-gate * 3) is within an unmapped address segment
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate base = addr;
4260Sstevel@tonic-gate len = size; /* use spt aligned size */
4270Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */
4280Sstevel@tonic-gate if ((uintptr_t)base & (share_size - 1)) {
4290Sstevel@tonic-gate error = EINVAL;
4300Sstevel@tonic-gate as_rangeunlock(as);
4310Sstevel@tonic-gate goto errret;
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as,
4340Sstevel@tonic-gate as->a_userlimit);
4350Sstevel@tonic-gate if (result == RANGE_BADPROT) {
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate * We try to accomodate processors which
4380Sstevel@tonic-gate * may not support execute permissions on
4390Sstevel@tonic-gate * all ISM segments by trying the check
4400Sstevel@tonic-gate * again but without PROT_EXEC.
4410Sstevel@tonic-gate */
4420Sstevel@tonic-gate prot &= ~PROT_EXEC;
4430Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as,
4440Sstevel@tonic-gate as->a_userlimit);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate as_purge(as);
4470Sstevel@tonic-gate if (result != RANGE_OKAY ||
4480Sstevel@tonic-gate as_gap(as, len, &base, &len, AH_LO,
4490Sstevel@tonic-gate (caddr_t)NULL) != 0) {
4500Sstevel@tonic-gate error = EINVAL;
4510Sstevel@tonic-gate as_rangeunlock(as);
4520Sstevel@tonic-gate goto errret;
4530Sstevel@tonic-gate }
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate if (!isspt(sp)) {
4570Sstevel@tonic-gate error = sptcreate(size, &segspt, sp->shm_amp, prot,
4580Sstevel@tonic-gate flags, share_szc);
4590Sstevel@tonic-gate if (error) {
4600Sstevel@tonic-gate as_rangeunlock(as);
4610Sstevel@tonic-gate goto errret;
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate sp->shm_sptinfo->sptas = segspt->s_as;
4640Sstevel@tonic-gate sp->shm_sptseg = segspt;
4650Sstevel@tonic-gate sp->shm_sptprot = prot;
4660Sstevel@tonic-gate } else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) {
4670Sstevel@tonic-gate /*
4680Sstevel@tonic-gate * Ensure we're attaching to an ISM segment with
4690Sstevel@tonic-gate * fewer or equal permissions than what we're
4700Sstevel@tonic-gate * allowed. Fail if the segment has more
4710Sstevel@tonic-gate * permissions than what we're allowed.
4720Sstevel@tonic-gate */
4730Sstevel@tonic-gate error = EACCES;
4740Sstevel@tonic-gate as_rangeunlock(as);
4750Sstevel@tonic-gate goto errret;
4760Sstevel@tonic-gate }
4770Sstevel@tonic-gate
4780Sstevel@tonic-gate ssd.shm_sptseg = sp->shm_sptseg;
4790Sstevel@tonic-gate ssd.shm_sptas = sp->shm_sptinfo->sptas;
4800Sstevel@tonic-gate ssd.shm_amp = sp->shm_amp;
4810Sstevel@tonic-gate error = as_map(as, addr, size, segspt_shmattach, &ssd);
4820Sstevel@tonic-gate if (error == 0)
4830Sstevel@tonic-gate sp->shm_ismattch++; /* keep count of ISM attaches */
4840Sstevel@tonic-gate } else {
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate /*
4870Sstevel@tonic-gate * Normal case.
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate if (flags & SHM_RDONLY)
4900Sstevel@tonic-gate prot &= ~PROT_WRITE;
4910Sstevel@tonic-gate
4920Sstevel@tonic-gate if (addr == 0) {
4930Sstevel@tonic-gate /* Let the system pick the attach address */
4940Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, 0);
4950Sstevel@tonic-gate if (addr == NULL) {
4960Sstevel@tonic-gate as_rangeunlock(as);
4970Sstevel@tonic-gate error = ENOMEM;
4980Sstevel@tonic-gate goto errret;
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate } else {
5010Sstevel@tonic-gate /* Use the user-supplied attach address */
5020Sstevel@tonic-gate caddr_t base;
5030Sstevel@tonic-gate size_t len;
5040Sstevel@tonic-gate
5050Sstevel@tonic-gate if (flags & SHM_RND)
5060Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr &
5070Sstevel@tonic-gate ~(SHMLBA - 1));
5080Sstevel@tonic-gate /*
5090Sstevel@tonic-gate * Check that the address range
5100Sstevel@tonic-gate * 1) is properly aligned
5110Sstevel@tonic-gate * 2) is correct in unix terms
5120Sstevel@tonic-gate * 3) is within an unmapped address segment
5130Sstevel@tonic-gate */
5140Sstevel@tonic-gate base = addr;
5150Sstevel@tonic-gate len = size; /* use aligned size */
5160Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */
5170Sstevel@tonic-gate if ((uintptr_t)base & PAGEOFFSET) {
5180Sstevel@tonic-gate error = EINVAL;
5190Sstevel@tonic-gate as_rangeunlock(as);
5200Sstevel@tonic-gate goto errret;
5210Sstevel@tonic-gate }
5220Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as,
5230Sstevel@tonic-gate as->a_userlimit);
5240Sstevel@tonic-gate if (result == RANGE_BADPROT) {
5250Sstevel@tonic-gate prot &= ~PROT_EXEC;
5260Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as,
5270Sstevel@tonic-gate as->a_userlimit);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate as_purge(as);
5300Sstevel@tonic-gate if (result != RANGE_OKAY ||
5310Sstevel@tonic-gate as_gap(as, len, &base, &len,
5320Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != 0) {
5330Sstevel@tonic-gate error = EINVAL;
5340Sstevel@tonic-gate as_rangeunlock(as);
5350Sstevel@tonic-gate goto errret;
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate
5390Sstevel@tonic-gate /* Initialize the create arguments and map the segment */
5400Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp;
5410Sstevel@tonic-gate crargs.offset = 0;
5420Sstevel@tonic-gate crargs.type = MAP_SHARED;
5430Sstevel@tonic-gate crargs.amp = sp->shm_amp;
5440Sstevel@tonic-gate crargs.prot = prot;
5450Sstevel@tonic-gate crargs.maxprot = crargs.prot;
5460Sstevel@tonic-gate crargs.flags = 0;
5470Sstevel@tonic-gate
5480Sstevel@tonic-gate error = as_map(as, addr, size, segvn_create, &crargs);
5490Sstevel@tonic-gate }
5500Sstevel@tonic-gate
5510Sstevel@tonic-gate as_rangeunlock(as);
5520Sstevel@tonic-gate if (error)
5530Sstevel@tonic-gate goto errret;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate /* record shmem range for the detach */
5560Sstevel@tonic-gate sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp);
5570Sstevel@tonic-gate *rvp = (uintptr_t)addr;
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate sp->shm_atime = gethrestime_sec();
5600Sstevel@tonic-gate sp->shm_lpid = pp->p_pid;
5610Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp);
5629351SPrashanth.Sreenivasa@Sun.COM
5639351SPrashanth.Sreenivasa@Sun.COM /*
5649351SPrashanth.Sreenivasa@Sun.COM * Tell machine specific code that lwp has mapped shared memory
5659351SPrashanth.Sreenivasa@Sun.COM */
5669351SPrashanth.Sreenivasa@Sun.COM LWP_MMODEL_SHARED_AS(addr, size);
5679351SPrashanth.Sreenivasa@Sun.COM
5680Sstevel@tonic-gate errret:
5690Sstevel@tonic-gate mutex_exit(lock);
5700Sstevel@tonic-gate return (error);
5710Sstevel@tonic-gate }
5720Sstevel@tonic-gate
5730Sstevel@tonic-gate static void
shm_dtor(kipc_perm_t * perm)5740Sstevel@tonic-gate shm_dtor(kipc_perm_t *perm)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate kshmid_t *sp = (kshmid_t *)perm;
5770Sstevel@tonic-gate uint_t cnt;
5782677Sml93401 size_t rsize;
5790Sstevel@tonic-gate
58011512SNick.Todd@Sun.COM ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
58111512SNick.Todd@Sun.COM anonmap_purge(sp->shm_amp);
58211512SNick.Todd@Sun.COM ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
58311512SNick.Todd@Sun.COM
5849975SGangadhar.M@Sun.COM if (sp->shm_sptinfo) {
5859975SGangadhar.M@Sun.COM if (isspt(sp)) {
5869975SGangadhar.M@Sun.COM sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp);
5879975SGangadhar.M@Sun.COM sp->shm_lkcnt = 0;
5889975SGangadhar.M@Sun.COM }
5899975SGangadhar.M@Sun.COM kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t));
5909975SGangadhar.M@Sun.COM }
5919975SGangadhar.M@Sun.COM
5922768Ssl108498 if (sp->shm_lkcnt > 0) {
5932768Ssl108498 shmem_unlock(sp, sp->shm_amp);
5942768Ssl108498 sp->shm_lkcnt = 0;
5952768Ssl108498 }
5962768Ssl108498
5970Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER);
5980Sstevel@tonic-gate cnt = --sp->shm_amp->refcnt;
5990Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
6000Sstevel@tonic-gate ASSERT(cnt == 0);
6013379Ssl108498 shm_rm_amp(sp);
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate if (sp->shm_perm.ipc_id != IPC_ID_INVAL) {
6042677Sml93401 rsize = ptob(btopr(sp->shm_segsz));
6050Sstevel@tonic-gate ipcs_lock(shm_svc);
6062677Sml93401 sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= rsize;
607*13096SJordan.Vaughan@Sun.com sp->shm_perm.ipc_zone_ref.zref_zone->zone_shmmax -= rsize;
6080Sstevel@tonic-gate ipcs_unlock(shm_svc);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate
6120Sstevel@tonic-gate /* ARGSUSED */
6130Sstevel@tonic-gate static void
shm_rmid(kipc_perm_t * perm)6140Sstevel@tonic-gate shm_rmid(kipc_perm_t *perm)
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate /* nothing to do */
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate /*
6200Sstevel@tonic-gate * Shmctl system call.
6210Sstevel@tonic-gate */
6220Sstevel@tonic-gate /* ARGSUSED */
6230Sstevel@tonic-gate static int
shmctl(int shmid,int cmd,void * arg)6240Sstevel@tonic-gate shmctl(int shmid, int cmd, void *arg)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */
6270Sstevel@tonic-gate STRUCT_DECL(shmid_ds, ds); /* for SVR4 IPC_SET */
6280Sstevel@tonic-gate int error = 0;
6290Sstevel@tonic-gate struct cred *cr = CRED();
6300Sstevel@tonic-gate kmutex_t *lock;
6310Sstevel@tonic-gate model_t mdl = get_udatamodel();
6320Sstevel@tonic-gate struct shmid_ds64 ds64;
6330Sstevel@tonic-gate shmatt_t nattch;
6340Sstevel@tonic-gate
6350Sstevel@tonic-gate STRUCT_INIT(ds, mdl);
6360Sstevel@tonic-gate
6370Sstevel@tonic-gate /*
6380Sstevel@tonic-gate * Perform pre- or non-lookup actions (e.g. copyins, RMID).
6390Sstevel@tonic-gate */
6400Sstevel@tonic-gate switch (cmd) {
6410Sstevel@tonic-gate case IPC_SET:
6420Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds)))
6430Sstevel@tonic-gate return (EFAULT);
6440Sstevel@tonic-gate break;
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate case IPC_SET64:
6470Sstevel@tonic-gate if (copyin(arg, &ds64, sizeof (struct shmid_ds64)))
6480Sstevel@tonic-gate return (EFAULT);
6490Sstevel@tonic-gate break;
6500Sstevel@tonic-gate
6510Sstevel@tonic-gate case IPC_RMID:
6520Sstevel@tonic-gate return (ipc_rmid(shm_svc, shmid, cr));
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate
6550Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL)
6560Sstevel@tonic-gate return (EINVAL);
6570Sstevel@tonic-gate
6580Sstevel@tonic-gate switch (cmd) {
6590Sstevel@tonic-gate /* Set ownership and permissions. */
6600Sstevel@tonic-gate case IPC_SET:
6610Sstevel@tonic-gate if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm,
6620Sstevel@tonic-gate &STRUCT_BUF(ds)->shm_perm, mdl))
6630Sstevel@tonic-gate break;
6640Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec();
6650Sstevel@tonic-gate break;
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate case IPC_STAT:
6680Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr))
6690Sstevel@tonic-gate break;
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1;
6720Sstevel@tonic-gate
6730Sstevel@tonic-gate ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl);
6740Sstevel@tonic-gate STRUCT_FSET(ds, shm_segsz, sp->shm_segsz);
6750Sstevel@tonic-gate STRUCT_FSETP(ds, shm_amp, NULL); /* kernel addr */
6760Sstevel@tonic-gate STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt);
6770Sstevel@tonic-gate STRUCT_FSET(ds, shm_lpid, sp->shm_lpid);
6780Sstevel@tonic-gate STRUCT_FSET(ds, shm_cpid, sp->shm_cpid);
6790Sstevel@tonic-gate STRUCT_FSET(ds, shm_nattch, nattch);
6800Sstevel@tonic-gate STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch);
6810Sstevel@tonic-gate STRUCT_FSET(ds, shm_atime, sp->shm_atime);
6820Sstevel@tonic-gate STRUCT_FSET(ds, shm_dtime, sp->shm_dtime);
6830Sstevel@tonic-gate STRUCT_FSET(ds, shm_ctime, sp->shm_ctime);
6840Sstevel@tonic-gate
6850Sstevel@tonic-gate mutex_exit(lock);
6860Sstevel@tonic-gate if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds)))
6870Sstevel@tonic-gate return (EFAULT);
6880Sstevel@tonic-gate
6890Sstevel@tonic-gate return (0);
6900Sstevel@tonic-gate
6910Sstevel@tonic-gate case IPC_SET64:
6920Sstevel@tonic-gate if (error = ipcperm_set64(shm_svc, cr,
6930Sstevel@tonic-gate &sp->shm_perm, &ds64.shmx_perm))
6940Sstevel@tonic-gate break;
6950Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec();
6960Sstevel@tonic-gate break;
6970Sstevel@tonic-gate
6980Sstevel@tonic-gate case IPC_STAT64:
6990Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1;
7000Sstevel@tonic-gate
7010Sstevel@tonic-gate ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm);
7020Sstevel@tonic-gate ds64.shmx_segsz = sp->shm_segsz;
7030Sstevel@tonic-gate ds64.shmx_lkcnt = sp->shm_lkcnt;
7040Sstevel@tonic-gate ds64.shmx_lpid = sp->shm_lpid;
7050Sstevel@tonic-gate ds64.shmx_cpid = sp->shm_cpid;
7060Sstevel@tonic-gate ds64.shmx_nattch = nattch;
7070Sstevel@tonic-gate ds64.shmx_cnattch = sp->shm_ismattch;
7080Sstevel@tonic-gate ds64.shmx_atime = sp->shm_atime;
7090Sstevel@tonic-gate ds64.shmx_dtime = sp->shm_dtime;
7100Sstevel@tonic-gate ds64.shmx_ctime = sp->shm_ctime;
7110Sstevel@tonic-gate
7120Sstevel@tonic-gate mutex_exit(lock);
7130Sstevel@tonic-gate if (copyout(&ds64, arg, sizeof (struct shmid_ds64)))
7140Sstevel@tonic-gate return (EFAULT);
7150Sstevel@tonic-gate
7160Sstevel@tonic-gate return (0);
7170Sstevel@tonic-gate
7180Sstevel@tonic-gate /* Lock segment in memory */
7190Sstevel@tonic-gate case SHM_LOCK:
7200Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0)
7210Sstevel@tonic-gate break;
7220Sstevel@tonic-gate
7232768Ssl108498 /* protect against overflow */
7242768Ssl108498 if (sp->shm_lkcnt >= USHRT_MAX) {
7252768Ssl108498 error = ENOMEM;
7262768Ssl108498 break;
7272768Ssl108498 }
7280Sstevel@tonic-gate if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) {
7292768Ssl108498 if (error = shmem_lock(sp, sp->shm_amp)) {
7305753Sgww ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock,
7315753Sgww RW_WRITER);
7326695Saguzovsk cmn_err(CE_NOTE, "shmctl - couldn't lock %ld"
7336695Saguzovsk " pages into memory", sp->shm_amp->size);
7345753Sgww ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock);
7355753Sgww error = ENOMEM;
7365753Sgww sp->shm_lkcnt--;
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate }
7390Sstevel@tonic-gate break;
7400Sstevel@tonic-gate
7410Sstevel@tonic-gate /* Unlock segment */
7420Sstevel@tonic-gate case SHM_UNLOCK:
7430Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0)
7440Sstevel@tonic-gate break;
7450Sstevel@tonic-gate
7462768Ssl108498 if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) {
7472768Ssl108498 shmem_unlock(sp, sp->shm_amp);
7480Sstevel@tonic-gate }
7490Sstevel@tonic-gate break;
7500Sstevel@tonic-gate
7510Sstevel@tonic-gate default:
7520Sstevel@tonic-gate error = EINVAL;
7530Sstevel@tonic-gate break;
7540Sstevel@tonic-gate }
7550Sstevel@tonic-gate mutex_exit(lock);
7560Sstevel@tonic-gate return (error);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate
7590Sstevel@tonic-gate static void
shm_detach(proc_t * pp,segacct_t * sap)7600Sstevel@tonic-gate shm_detach(proc_t *pp, segacct_t *sap)
7610Sstevel@tonic-gate {
7620Sstevel@tonic-gate kshmid_t *sp = sap->sa_id;
7630Sstevel@tonic-gate size_t len = sap->sa_len;
7640Sstevel@tonic-gate caddr_t addr = sap->sa_addr;
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate /*
7670Sstevel@tonic-gate * Discard lwpchan mappings.
7680Sstevel@tonic-gate */
7690Sstevel@tonic-gate if (pp->p_lcp != NULL)
7700Sstevel@tonic-gate lwpchan_delete_mapping(pp, addr, addr + len);
7710Sstevel@tonic-gate (void) as_unmap(pp->p_as, addr, len);
7720Sstevel@tonic-gate
7730Sstevel@tonic-gate /*
7740Sstevel@tonic-gate * Perform some detach-time accounting.
7750Sstevel@tonic-gate */
7760Sstevel@tonic-gate (void) ipc_lock(shm_svc, sp->shm_perm.ipc_id);
7770Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM)
7780Sstevel@tonic-gate sp->shm_ismattch--;
7790Sstevel@tonic-gate sp->shm_dtime = gethrestime_sec();
7800Sstevel@tonic-gate sp->shm_lpid = pp->p_pid;
7810Sstevel@tonic-gate ipc_rele(shm_svc, (kipc_perm_t *)sp); /* Drops lock */
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate kmem_free(sap, sizeof (segacct_t));
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate
7860Sstevel@tonic-gate static int
shmdt(caddr_t addr)7870Sstevel@tonic-gate shmdt(caddr_t addr)
7880Sstevel@tonic-gate {
7890Sstevel@tonic-gate proc_t *pp = curproc;
7900Sstevel@tonic-gate segacct_t *sap, template;
7910Sstevel@tonic-gate
7920Sstevel@tonic-gate mutex_enter(&pp->p_lock);
7930Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */
7940Sstevel@tonic-gate
7950Sstevel@tonic-gate template.sa_addr = addr;
7960Sstevel@tonic-gate template.sa_len = 0;
7970Sstevel@tonic-gate if ((pp->p_segacct == NULL) ||
7980Sstevel@tonic-gate ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) {
7990Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8000Sstevel@tonic-gate return (EINVAL);
8010Sstevel@tonic-gate }
8022414Saguzovsk if (sap->sa_addr != addr) {
8032414Saguzovsk mutex_exit(&pp->p_lock);
8042414Saguzovsk return (EINVAL);
8052414Saguzovsk }
8060Sstevel@tonic-gate avl_remove(pp->p_segacct, sap);
8070Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate shm_detach(pp, sap);
8100Sstevel@tonic-gate
8110Sstevel@tonic-gate return (0);
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate * Remove all shared memory segments associated with a given zone.
8160Sstevel@tonic-gate * Called by zone_shutdown when the zone is halted.
8170Sstevel@tonic-gate */
8180Sstevel@tonic-gate /*ARGSUSED1*/
8190Sstevel@tonic-gate static void
shm_remove_zone(zoneid_t zoneid,void * arg)8200Sstevel@tonic-gate shm_remove_zone(zoneid_t zoneid, void *arg)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate ipc_remove_zone(shm_svc, zoneid);
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate
8250Sstevel@tonic-gate /*
8260Sstevel@tonic-gate * Shmget (create new shmem) system call.
8270Sstevel@tonic-gate */
8280Sstevel@tonic-gate static int
shmget(key_t key,size_t size,int shmflg,uintptr_t * rvp)8290Sstevel@tonic-gate shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate proc_t *pp = curproc;
8320Sstevel@tonic-gate kshmid_t *sp;
8330Sstevel@tonic-gate kmutex_t *lock;
8340Sstevel@tonic-gate int error;
8350Sstevel@tonic-gate
8360Sstevel@tonic-gate top:
8370Sstevel@tonic-gate if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock))
8380Sstevel@tonic-gate return (error);
8390Sstevel@tonic-gate
8400Sstevel@tonic-gate if (!IPC_FREE(&sp->shm_perm)) {
8410Sstevel@tonic-gate /*
8420Sstevel@tonic-gate * A segment with the requested key exists.
8430Sstevel@tonic-gate */
8440Sstevel@tonic-gate if (size > sp->shm_segsz) {
8450Sstevel@tonic-gate mutex_exit(lock);
8460Sstevel@tonic-gate return (EINVAL);
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate } else {
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate * A new segment should be created.
8510Sstevel@tonic-gate */
8520Sstevel@tonic-gate size_t npages = btopr(size);
8530Sstevel@tonic-gate size_t rsize = ptob(npages);
8540Sstevel@tonic-gate
8550Sstevel@tonic-gate /*
8562677Sml93401 * Check rsize and the per-project and per-zone limit on
8572677Sml93401 * shared memory. Checking rsize handles both the size == 0
8580Sstevel@tonic-gate * case and the size < ULONG_MAX & PAGEMASK case (i.e.
8590Sstevel@tonic-gate * rounding up wraps a size_t).
8600Sstevel@tonic-gate */
8612677Sml93401 if (rsize == 0 ||
8622677Sml93401 (rctl_test(rc_project_shmmax,
8630Sstevel@tonic-gate pp->p_task->tk_proj->kpj_rctls, pp, rsize,
8642677Sml93401 RCA_SAFE) & RCT_DENY) ||
8652677Sml93401 (rctl_test(rc_zone_shmmax,
8662677Sml93401 pp->p_zone->zone_rctls, pp, rsize,
8670Sstevel@tonic-gate RCA_SAFE) & RCT_DENY)) {
8680Sstevel@tonic-gate
8690Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8700Sstevel@tonic-gate mutex_exit(lock);
8710Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8720Sstevel@tonic-gate return (EINVAL);
8730Sstevel@tonic-gate }
8740Sstevel@tonic-gate mutex_exit(&pp->p_lock);
8750Sstevel@tonic-gate mutex_exit(lock);
8760Sstevel@tonic-gate
8770Sstevel@tonic-gate if (anon_resv(rsize) == 0) {
8780Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
8790Sstevel@tonic-gate return (ENOMEM);
8800Sstevel@tonic-gate }
8810Sstevel@tonic-gate
8823458Ssl108498 /*
8833458Ssl108498 * If any new failure points are introduced between the
8843458Ssl108498 * the above anon_resv() and the below ipc_commit_begin(),
8853458Ssl108498 * these failure points will need to unreserve the anon
8863458Ssl108498 * reserved using anon_unresv().
8873458Ssl108498 *
8883458Ssl108498 * Once ipc_commit_begin() is called, the anon reserved
8893458Ssl108498 * above will be automatically unreserved by future calls to
8903458Ssl108498 * ipcs_cleanup() -> shm_dtor() -> shm_rm_amp(). If
8913458Ssl108498 * ipc_commit_begin() fails, it internally calls shm_dtor(),
8923458Ssl108498 * unreserving the above anon, and freeing the below amp.
8933458Ssl108498 */
8943458Ssl108498
8954426Saguzovsk sp->shm_amp = anonmap_alloc(rsize, rsize, ANON_SLEEP);
8962768Ssl108498 sp->shm_amp->a_sp = sp;
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate * Store the original user's requested size, in bytes,
8990Sstevel@tonic-gate * rather than the page-aligned size. The former is
9000Sstevel@tonic-gate * used for IPC_STAT and shmget() lookups. The latter
9010Sstevel@tonic-gate * is saved in the anon_map structure and is used for
9020Sstevel@tonic-gate * calls to the vm layer.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate sp->shm_segsz = size;
9050Sstevel@tonic-gate sp->shm_atime = sp->shm_dtime = 0;
9060Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec();
9070Sstevel@tonic-gate sp->shm_lpid = (pid_t)0;
9080Sstevel@tonic-gate sp->shm_cpid = curproc->p_pid;
9090Sstevel@tonic-gate sp->shm_ismattch = 0;
9100Sstevel@tonic-gate sp->shm_sptinfo = NULL;
9110Sstevel@tonic-gate /*
9120Sstevel@tonic-gate * Check limits one last time, push id into global
9130Sstevel@tonic-gate * visibility, and update resource usage counts.
9140Sstevel@tonic-gate */
9150Sstevel@tonic-gate if (error = ipc_commit_begin(shm_svc, key, shmflg,
9160Sstevel@tonic-gate (kipc_perm_t *)sp)) {
9170Sstevel@tonic-gate if (error == EAGAIN)
9180Sstevel@tonic-gate goto top;
9190Sstevel@tonic-gate return (error);
9200Sstevel@tonic-gate }
9210Sstevel@tonic-gate
9222677Sml93401 if ((rctl_test(rc_project_shmmax,
9230Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize,
9242677Sml93401 RCA_SAFE) & RCT_DENY) ||
9252677Sml93401 (rctl_test(rc_zone_shmmax,
926*13096SJordan.Vaughan@Sun.com sp->shm_perm.ipc_zone_ref.zref_zone->zone_rctls, pp, rsize,
9272677Sml93401 RCA_SAFE) & RCT_DENY)) {
9280Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp);
9290Sstevel@tonic-gate return (EINVAL);
9300Sstevel@tonic-gate }
9310Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize;
932*13096SJordan.Vaughan@Sun.com sp->shm_perm.ipc_zone_ref.zref_zone->zone_shmmax += rsize;
9330Sstevel@tonic-gate
9340Sstevel@tonic-gate lock = ipc_commit_end(shm_svc, &sp->shm_perm);
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate
93711861SMarek.Pospisil@Sun.COM if (AU_AUDITING())
9380Sstevel@tonic-gate audit_ipcget(AT_IPC_SHM, (void *)sp);
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate *rvp = (uintptr_t)(sp->shm_perm.ipc_id);
9410Sstevel@tonic-gate
9420Sstevel@tonic-gate mutex_exit(lock);
9430Sstevel@tonic-gate return (0);
9440Sstevel@tonic-gate }
9450Sstevel@tonic-gate
9460Sstevel@tonic-gate /*
9470Sstevel@tonic-gate * shmids system call.
9480Sstevel@tonic-gate */
9490Sstevel@tonic-gate static int
shmids(int * buf,uint_t nids,uint_t * pnids)9500Sstevel@tonic-gate shmids(int *buf, uint_t nids, uint_t *pnids)
9510Sstevel@tonic-gate {
9520Sstevel@tonic-gate return (ipc_ids(shm_svc, buf, nids, pnids));
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate /*
9560Sstevel@tonic-gate * System entry point for shmat, shmctl, shmdt, and shmget system calls.
9570Sstevel@tonic-gate */
9580Sstevel@tonic-gate static uintptr_t
shmsys(int opcode,uintptr_t a0,uintptr_t a1,uintptr_t a2)9590Sstevel@tonic-gate shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2)
9600Sstevel@tonic-gate {
9610Sstevel@tonic-gate int error;
9620Sstevel@tonic-gate uintptr_t r_val = 0;
9630Sstevel@tonic-gate
9640Sstevel@tonic-gate switch (opcode) {
9650Sstevel@tonic-gate case SHMAT:
9660Sstevel@tonic-gate error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val);
9670Sstevel@tonic-gate break;
9680Sstevel@tonic-gate case SHMCTL:
9690Sstevel@tonic-gate error = shmctl((int)a0, (int)a1, (void *)a2);
9700Sstevel@tonic-gate break;
9710Sstevel@tonic-gate case SHMDT:
9720Sstevel@tonic-gate error = shmdt((caddr_t)a0);
9730Sstevel@tonic-gate break;
9740Sstevel@tonic-gate case SHMGET:
9750Sstevel@tonic-gate error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val);
9760Sstevel@tonic-gate break;
9770Sstevel@tonic-gate case SHMIDS:
9780Sstevel@tonic-gate error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2);
9790Sstevel@tonic-gate break;
9800Sstevel@tonic-gate default:
9810Sstevel@tonic-gate error = EINVAL;
9820Sstevel@tonic-gate break;
9830Sstevel@tonic-gate }
9840Sstevel@tonic-gate
9850Sstevel@tonic-gate if (error)
9860Sstevel@tonic-gate return ((uintptr_t)set_errno(error));
9870Sstevel@tonic-gate
9880Sstevel@tonic-gate return (r_val);
9890Sstevel@tonic-gate }
9900Sstevel@tonic-gate
9910Sstevel@tonic-gate /*
9920Sstevel@tonic-gate * segacct_t comparator
9930Sstevel@tonic-gate * This works as expected, with one minor change: the first of two real
9940Sstevel@tonic-gate * segments with equal addresses is considered to be 'greater than' the
9950Sstevel@tonic-gate * second. We only return equal when searching using a template, in
9960Sstevel@tonic-gate * which case we explicitly set the template segment's length to 0
9970Sstevel@tonic-gate * (which is invalid for a real segment).
9980Sstevel@tonic-gate */
9990Sstevel@tonic-gate static int
shm_sacompar(const void * x,const void * y)10000Sstevel@tonic-gate shm_sacompar(const void *x, const void *y)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate segacct_t *sa1 = (segacct_t *)x;
10030Sstevel@tonic-gate segacct_t *sa2 = (segacct_t *)y;
10040Sstevel@tonic-gate
10052414Saguzovsk if (sa1->sa_addr < sa2->sa_addr) {
10060Sstevel@tonic-gate return (-1);
10072414Saguzovsk } else if (sa2->sa_len != 0) {
10082414Saguzovsk if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) {
10092414Saguzovsk return (1);
10102414Saguzovsk } else if (sa1->sa_len != 0) {
10112414Saguzovsk return (1);
10122414Saguzovsk } else {
10132414Saguzovsk return (0);
10142414Saguzovsk }
10152414Saguzovsk } else if (sa1->sa_addr > sa2->sa_addr) {
10160Sstevel@tonic-gate return (1);
10172414Saguzovsk } else {
10180Sstevel@tonic-gate return (0);
10192414Saguzovsk }
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate * add this record to the segacct list.
10240Sstevel@tonic-gate */
10250Sstevel@tonic-gate static void
sa_add(struct proc * pp,caddr_t addr,size_t len,ulong_t flags,kshmid_t * id)10260Sstevel@tonic-gate sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id)
10270Sstevel@tonic-gate {
10280Sstevel@tonic-gate segacct_t *nsap;
10290Sstevel@tonic-gate avl_tree_t *tree = NULL;
10300Sstevel@tonic-gate avl_index_t where;
10310Sstevel@tonic-gate
10320Sstevel@tonic-gate nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP);
10330Sstevel@tonic-gate nsap->sa_addr = addr;
10340Sstevel@tonic-gate nsap->sa_len = len;
10350Sstevel@tonic-gate nsap->sa_flags = flags;
10360Sstevel@tonic-gate nsap->sa_id = id;
10370Sstevel@tonic-gate
10380Sstevel@tonic-gate if (pp->p_segacct == NULL)
10390Sstevel@tonic-gate tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
10400Sstevel@tonic-gate
10410Sstevel@tonic-gate mutex_enter(&pp->p_lock);
10420Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */
10430Sstevel@tonic-gate
10440Sstevel@tonic-gate if (pp->p_segacct == NULL) {
10450Sstevel@tonic-gate avl_create(tree, shm_sacompar, sizeof (segacct_t),
10460Sstevel@tonic-gate offsetof(segacct_t, sa_tree));
10470Sstevel@tonic-gate pp->p_segacct = tree;
10480Sstevel@tonic-gate } else if (tree) {
10490Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t));
10500Sstevel@tonic-gate }
10510Sstevel@tonic-gate
10520Sstevel@tonic-gate /*
10530Sstevel@tonic-gate * We can ignore the result of avl_find, as the comparator will
10540Sstevel@tonic-gate * never return equal for segments with non-zero length. This
10550Sstevel@tonic-gate * is a necessary hack to get around the fact that we do, in
10560Sstevel@tonic-gate * fact, have duplicate keys.
10570Sstevel@tonic-gate */
10580Sstevel@tonic-gate (void) avl_find(pp->p_segacct, nsap, &where);
10590Sstevel@tonic-gate avl_insert(pp->p_segacct, nsap, where);
10600Sstevel@tonic-gate
10610Sstevel@tonic-gate mutex_exit(&pp->p_lock);
10620Sstevel@tonic-gate }
10630Sstevel@tonic-gate
10640Sstevel@tonic-gate /*
10650Sstevel@tonic-gate * Duplicate parent's segacct records in child.
10660Sstevel@tonic-gate */
10670Sstevel@tonic-gate void
shmfork(struct proc * ppp,struct proc * cpp)10680Sstevel@tonic-gate shmfork(struct proc *ppp, struct proc *cpp)
10690Sstevel@tonic-gate {
10700Sstevel@tonic-gate segacct_t *sap;
10710Sstevel@tonic-gate kshmid_t *sp;
10720Sstevel@tonic-gate kmutex_t *mp;
10730Sstevel@tonic-gate
10740Sstevel@tonic-gate ASSERT(ppp->p_segacct != NULL);
10750Sstevel@tonic-gate
10760Sstevel@tonic-gate /*
10770Sstevel@tonic-gate * We are the only lwp running in the parent so nobody can
10780Sstevel@tonic-gate * mess with our p_segacct list. Thus it is safe to traverse
10790Sstevel@tonic-gate * the list without holding p_lock. This is essential because
10800Sstevel@tonic-gate * we can't hold p_lock during a KM_SLEEP allocation.
10810Sstevel@tonic-gate */
10820Sstevel@tonic-gate for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL;
10830Sstevel@tonic-gate sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) {
10840Sstevel@tonic-gate sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags,
10850Sstevel@tonic-gate sap->sa_id);
10860Sstevel@tonic-gate sp = sap->sa_id;
10870Sstevel@tonic-gate mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id);
10880Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM)
10890Sstevel@tonic-gate sp->shm_ismattch++;
10900Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp);
10910Sstevel@tonic-gate mutex_exit(mp);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate }
10940Sstevel@tonic-gate
10950Sstevel@tonic-gate /*
10960Sstevel@tonic-gate * Detach shared memory segments from exiting process.
10970Sstevel@tonic-gate */
10980Sstevel@tonic-gate void
shmexit(struct proc * pp)10990Sstevel@tonic-gate shmexit(struct proc *pp)
11000Sstevel@tonic-gate {
11010Sstevel@tonic-gate segacct_t *sap;
11020Sstevel@tonic-gate avl_tree_t *tree;
11030Sstevel@tonic-gate void *cookie = NULL;
11040Sstevel@tonic-gate
11050Sstevel@tonic-gate ASSERT(pp->p_segacct != NULL);
11060Sstevel@tonic-gate
11070Sstevel@tonic-gate mutex_enter(&pp->p_lock);
11080Sstevel@tonic-gate prbarrier(pp);
11090Sstevel@tonic-gate tree = pp->p_segacct;
11100Sstevel@tonic-gate pp->p_segacct = NULL;
11110Sstevel@tonic-gate mutex_exit(&pp->p_lock);
11120Sstevel@tonic-gate
11130Sstevel@tonic-gate while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL)
11140Sstevel@tonic-gate (void) shm_detach(pp, sap);
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate avl_destroy(tree);
11170Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t));
11180Sstevel@tonic-gate }
11190Sstevel@tonic-gate
11200Sstevel@tonic-gate /*
11210Sstevel@tonic-gate * At this time pages should be in memory, so just lock them.
11220Sstevel@tonic-gate */
11230Sstevel@tonic-gate static void
lock_again(size_t npages,kshmid_t * sp,struct anon_map * amp)11242768Ssl108498 lock_again(size_t npages, kshmid_t *sp, struct anon_map *amp)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate struct anon *ap;
11270Sstevel@tonic-gate struct page *pp;
11280Sstevel@tonic-gate struct vnode *vp;
11292768Ssl108498 u_offset_t off;
11300Sstevel@tonic-gate ulong_t anon_idx;
11310Sstevel@tonic-gate anon_sync_obj_t cookie;
11320Sstevel@tonic-gate
11332768Ssl108498 mutex_enter(&sp->shm_mlock);
11340Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
11350Sstevel@tonic-gate for (anon_idx = 0; npages != 0; anon_idx++, npages--) {
11360Sstevel@tonic-gate
11370Sstevel@tonic-gate anon_array_enter(amp, anon_idx, &cookie);
11380Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, anon_idx);
11392768Ssl108498 ASSERT(ap != NULL);
11400Sstevel@tonic-gate swap_xlate(ap, &vp, &off);
11410Sstevel@tonic-gate anon_array_exit(&cookie);
11420Sstevel@tonic-gate
11432768Ssl108498 pp = page_lookup(vp, off, SE_SHARED);
11440Sstevel@tonic-gate if (pp == NULL) {
11450Sstevel@tonic-gate panic("lock_again: page not in the system");
11460Sstevel@tonic-gate /*NOTREACHED*/
11470Sstevel@tonic-gate }
11482768Ssl108498 /* page should already be locked by caller */
11492768Ssl108498 ASSERT(pp->p_lckcnt > 0);
11500Sstevel@tonic-gate (void) page_pp_lock(pp, 0, 0);
11510Sstevel@tonic-gate page_unlock(pp);
11520Sstevel@tonic-gate }
11530Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock);
11542768Ssl108498 mutex_exit(&sp->shm_mlock);
11550Sstevel@tonic-gate }
11560Sstevel@tonic-gate
11570Sstevel@tonic-gate /*
11580Sstevel@tonic-gate * Attach the shared memory segment to the process
11590Sstevel@tonic-gate * address space and lock the pages.
11600Sstevel@tonic-gate */
11610Sstevel@tonic-gate static int
shmem_lock(kshmid_t * sp,struct anon_map * amp)11622768Ssl108498 shmem_lock(kshmid_t *sp, struct anon_map *amp)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate size_t npages = btopr(amp->size);
11650Sstevel@tonic-gate struct as *as;
11660Sstevel@tonic-gate struct segvn_crargs crargs;
11672768Ssl108498 uint_t error;
11680Sstevel@tonic-gate
11692768Ssl108498 /*
11702768Ssl108498 * A later ISM/DISM attach may increase the size of the amp, so
11712768Ssl108498 * cache the number of pages locked for the future shmem_unlock()
11722768Ssl108498 */
11732768Ssl108498 sp->shm_lkpages = npages;
11740Sstevel@tonic-gate
11752768Ssl108498 as = as_alloc();
11760Sstevel@tonic-gate /* Initialize the create arguments and map the segment */
11770Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp; /* structure copy */
11780Sstevel@tonic-gate crargs.offset = (u_offset_t)0;
11790Sstevel@tonic-gate crargs.type = MAP_SHARED;
11800Sstevel@tonic-gate crargs.amp = amp;
11810Sstevel@tonic-gate crargs.prot = PROT_ALL;
11820Sstevel@tonic-gate crargs.maxprot = crargs.prot;
11830Sstevel@tonic-gate crargs.flags = 0;
11842768Ssl108498 error = as_map(as, 0x0, amp->size, segvn_create, &crargs);
11850Sstevel@tonic-gate if (!error) {
11862768Ssl108498 if ((error = as_ctl(as, 0x0, amp->size, MC_LOCK, 0, 0,
11875753Sgww NULL, 0)) == 0) {
11882768Ssl108498 lock_again(npages, sp, amp);
11890Sstevel@tonic-gate }
11902768Ssl108498 (void) as_unmap(as, 0x0, amp->size);
11910Sstevel@tonic-gate }
11922768Ssl108498 as_free(as);
11930Sstevel@tonic-gate return (error);
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate /*
11980Sstevel@tonic-gate * Unlock shared memory
11990Sstevel@tonic-gate */
12000Sstevel@tonic-gate static void
shmem_unlock(kshmid_t * sp,struct anon_map * amp)12012768Ssl108498 shmem_unlock(kshmid_t *sp, struct anon_map *amp)
12020Sstevel@tonic-gate {
12030Sstevel@tonic-gate struct anon *ap;
12042768Ssl108498 pgcnt_t npages = sp->shm_lkpages;
12050Sstevel@tonic-gate struct vnode *vp;
12060Sstevel@tonic-gate struct page *pp;
12072768Ssl108498 u_offset_t off;
12080Sstevel@tonic-gate ulong_t anon_idx;
12092768Ssl108498 size_t unlocked_bytes = 0;
12102768Ssl108498 kproject_t *proj;
12112768Ssl108498 anon_sync_obj_t cookie;
12120Sstevel@tonic-gate
12132768Ssl108498 proj = sp->shm_perm.ipc_proj;
12142768Ssl108498 mutex_enter(&sp->shm_mlock);
12152768Ssl108498 ANON_LOCK_ENTER(&->a_rwlock, RW_READER);
12160Sstevel@tonic-gate for (anon_idx = 0; anon_idx < npages; anon_idx++) {
12170Sstevel@tonic-gate
12182768Ssl108498 anon_array_enter(amp, anon_idx, &cookie);
12190Sstevel@tonic-gate if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) {
12202768Ssl108498 panic("shmem_unlock: null app");
12212768Ssl108498 /*NOTREACHED*/
12220Sstevel@tonic-gate }
12230Sstevel@tonic-gate swap_xlate(ap, &vp, &off);
12242768Ssl108498 anon_array_exit(&cookie);
12250Sstevel@tonic-gate pp = page_lookup(vp, off, SE_SHARED);
12260Sstevel@tonic-gate if (pp == NULL) {
12272768Ssl108498 panic("shmem_unlock: page not in the system");
12282768Ssl108498 /*NOTREACHED*/
12290Sstevel@tonic-gate }
12302768Ssl108498 /*
12312768Ssl108498 * Page should at least have once lock from previous
12322768Ssl108498 * shmem_lock
12332768Ssl108498 */
12342768Ssl108498 ASSERT(pp->p_lckcnt > 0);
12352768Ssl108498 page_pp_unlock(pp, 0, 0);
12362768Ssl108498 if (pp->p_lckcnt == 0)
12372768Ssl108498 unlocked_bytes += PAGESIZE;
12382768Ssl108498
12390Sstevel@tonic-gate page_unlock(pp);
12400Sstevel@tonic-gate }
12412768Ssl108498
12422768Ssl108498 if (unlocked_bytes > 0) {
12432768Ssl108498 rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0);
12442768Ssl108498 }
12452768Ssl108498
12462768Ssl108498 ANON_LOCK_EXIT(&->a_rwlock);
12472768Ssl108498 mutex_exit(&sp->shm_mlock);
12480Sstevel@tonic-gate }
12490Sstevel@tonic-gate
12500Sstevel@tonic-gate /*
12510Sstevel@tonic-gate * We call this routine when we have removed all references to this
12520Sstevel@tonic-gate * amp. This means all shmdt()s and the IPC_RMID have been done.
12530Sstevel@tonic-gate */
12540Sstevel@tonic-gate static void
shm_rm_amp(kshmid_t * sp)12553379Ssl108498 shm_rm_amp(kshmid_t *sp)
12560Sstevel@tonic-gate {
12573379Ssl108498 struct anon_map *amp = sp->shm_amp;
12583379Ssl108498 zone_t *zone;
12593379Ssl108498
1260*13096SJordan.Vaughan@Sun.com zone = sp->shm_perm.ipc_zone_ref.zref_zone;
12613458Ssl108498 ASSERT(zone != NULL);
12620Sstevel@tonic-gate /*
12630Sstevel@tonic-gate * Free up the anon_map.
12640Sstevel@tonic-gate */
12650Sstevel@tonic-gate lgrp_shm_policy_fini(amp, NULL);
12666695Saguzovsk ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER);
12672414Saguzovsk if (amp->a_szc != 0) {
12682414Saguzovsk anon_shmap_free_pages(amp, 0, amp->size);
12692414Saguzovsk } else {
12702414Saguzovsk anon_free(amp->ahp, 0, amp->size);
12712414Saguzovsk }
12726695Saguzovsk ANON_LOCK_EXIT(&->a_rwlock);
12733379Ssl108498 anon_unresv_zone(amp->swresv, zone);
12740Sstevel@tonic-gate anonmap_free(amp);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate
12770Sstevel@tonic-gate /*
12780Sstevel@tonic-gate * Return the shared memory id for the process's virtual address.
12790Sstevel@tonic-gate * Return SHMID_NONE if addr is not within a SysV shared memory segment.
12800Sstevel@tonic-gate * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed.
12810Sstevel@tonic-gate *
12820Sstevel@tonic-gate * shmgetid() is called from code in /proc with the process locked but
12830Sstevel@tonic-gate * with pp->p_lock not held. The address space lock is held, so we
12840Sstevel@tonic-gate * cannot grab pp->p_lock here due to lock-ordering constraints.
12850Sstevel@tonic-gate * Because of all this, modifications to the p_segacct list must only
12860Sstevel@tonic-gate * be made after calling prbarrier() to ensure the process is not locked.
12870Sstevel@tonic-gate * See shmdt() and sa_add(), above. shmgetid() may also be called on a
12880Sstevel@tonic-gate * thread's own process without the process locked.
12890Sstevel@tonic-gate */
12900Sstevel@tonic-gate int
shmgetid(proc_t * pp,caddr_t addr)12910Sstevel@tonic-gate shmgetid(proc_t *pp, caddr_t addr)
12920Sstevel@tonic-gate {
12930Sstevel@tonic-gate segacct_t *sap, template;
12940Sstevel@tonic-gate
12950Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&pp->p_lock));
12960Sstevel@tonic-gate ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc);
12970Sstevel@tonic-gate
12980Sstevel@tonic-gate if (pp->p_segacct == NULL)
12990Sstevel@tonic-gate return (SHMID_NONE);
13000Sstevel@tonic-gate
13010Sstevel@tonic-gate template.sa_addr = addr;
13020Sstevel@tonic-gate template.sa_len = 0;
13030Sstevel@tonic-gate if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)
13040Sstevel@tonic-gate return (SHMID_NONE);
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate if (IPC_FREE(&sap->sa_id->shm_perm))
13070Sstevel@tonic-gate return (SHMID_FREE);
13080Sstevel@tonic-gate
13090Sstevel@tonic-gate return (sap->sa_id->shm_perm.ipc_id);
13100Sstevel@tonic-gate }
1311