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