17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 507b65a64Saguzovsk * Common Development and Distribution License (the "License"). 607b65a64Saguzovsk * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21a0de58d6SRoger A. Faulkner 227c478bd9Sstevel@tonic-gate /* 23*57820e88SNick Todd * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate /* 317c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 327c478bd9Sstevel@tonic-gate * The Regents of the University of California 337c478bd9Sstevel@tonic-gate * All Rights Reserved 347c478bd9Sstevel@tonic-gate * 357c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 367c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 377c478bd9Sstevel@tonic-gate * contributors. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * Inter-Process Communication Shared Memory Facility. 427c478bd9Sstevel@tonic-gate * 437c478bd9Sstevel@tonic-gate * See os/ipc.c for a description of common IPC functionality. 447c478bd9Sstevel@tonic-gate * 457c478bd9Sstevel@tonic-gate * Resource controls 467c478bd9Sstevel@tonic-gate * ----------------- 477c478bd9Sstevel@tonic-gate * 48824c205fSml93401 * Control: zone.max-shm-ids (rc_zone_shmmni) 49824c205fSml93401 * Description: Maximum number of shared memory ids allowed a zone. 50824c205fSml93401 * 51824c205fSml93401 * When shmget() is used to allocate a shared memory segment, one id 52824c205fSml93401 * is allocated. If the id allocation doesn't succeed, shmget() 53824c205fSml93401 * fails and errno is set to ENOSPC. Upon successful shmctl(, 54824c205fSml93401 * IPC_RMID) the id is deallocated. 55824c205fSml93401 * 567c478bd9Sstevel@tonic-gate * Control: project.max-shm-ids (rc_project_shmmni) 577c478bd9Sstevel@tonic-gate * Description: Maximum number of shared memory ids allowed a project. 587c478bd9Sstevel@tonic-gate * 597c478bd9Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, one id 607c478bd9Sstevel@tonic-gate * is allocated. If the id allocation doesn't succeed, shmget() 617c478bd9Sstevel@tonic-gate * fails and errno is set to ENOSPC. Upon successful shmctl(, 627c478bd9Sstevel@tonic-gate * IPC_RMID) the id is deallocated. 637c478bd9Sstevel@tonic-gate * 64824c205fSml93401 * Control: zone.max-shm-memory (rc_zone_shmmax) 65824c205fSml93401 * Description: Total amount of shared memory allowed a zone. 66824c205fSml93401 * 67824c205fSml93401 * When shmget() is used to allocate a shared memory segment, the 68824c205fSml93401 * segment's size is allocated against this limit. If the space 69824c205fSml93401 * allocation doesn't succeed, shmget() fails and errno is set to 70824c205fSml93401 * EINVAL. The size will be deallocated once the last process has 71824c205fSml93401 * detached the segment and the segment has been successfully 72824c205fSml93401 * shmctl(, IPC_RMID)ed. 73824c205fSml93401 * 747c478bd9Sstevel@tonic-gate * Control: project.max-shm-memory (rc_project_shmmax) 757c478bd9Sstevel@tonic-gate * Description: Total amount of shared memory allowed a project. 767c478bd9Sstevel@tonic-gate * 777c478bd9Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, the 787c478bd9Sstevel@tonic-gate * segment's size is allocated against this limit. If the space 797c478bd9Sstevel@tonic-gate * allocation doesn't succeed, shmget() fails and errno is set to 807c478bd9Sstevel@tonic-gate * EINVAL. The size will be deallocated once the last process has 817c478bd9Sstevel@tonic-gate * detached the segment and the segment has been successfully 827c478bd9Sstevel@tonic-gate * shmctl(, IPC_RMID)ed. 837c478bd9Sstevel@tonic-gate */ 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate #include <sys/types.h> 867c478bd9Sstevel@tonic-gate #include <sys/param.h> 877c478bd9Sstevel@tonic-gate #include <sys/cred.h> 887c478bd9Sstevel@tonic-gate #include <sys/errno.h> 897c478bd9Sstevel@tonic-gate #include <sys/time.h> 907c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 917c478bd9Sstevel@tonic-gate #include <sys/user.h> 927c478bd9Sstevel@tonic-gate #include <sys/proc.h> 937c478bd9Sstevel@tonic-gate #include <sys/systm.h> 947c478bd9Sstevel@tonic-gate #include <sys/prsystm.h> 957c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 967c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 977c478bd9Sstevel@tonic-gate #include <sys/vm.h> 987c478bd9Sstevel@tonic-gate #include <sys/mman.h> 997c478bd9Sstevel@tonic-gate #include <sys/swap.h> 1007c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 1017c478bd9Sstevel@tonic-gate #include <sys/debug.h> 1027c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h> 1037c478bd9Sstevel@tonic-gate #include <sys/avl.h> 1047c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 1057c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 1067c478bd9Sstevel@tonic-gate #include <sys/task.h> 1077c478bd9Sstevel@tonic-gate #include <sys/project.h> 1087c478bd9Sstevel@tonic-gate #include <sys/policy.h> 1097c478bd9Sstevel@tonic-gate #include <sys/zone.h> 110c6939658Ssl108498 #include <sys/rctl.h> 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate #include <sys/ipc.h> 1137c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h> 1147c478bd9Sstevel@tonic-gate #include <sys/shm.h> 1157c478bd9Sstevel@tonic-gate #include <sys/shm_impl.h> 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #include <vm/hat.h> 1187c478bd9Sstevel@tonic-gate #include <vm/seg.h> 1197c478bd9Sstevel@tonic-gate #include <vm/as.h> 1207c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 1217c478bd9Sstevel@tonic-gate #include <vm/anon.h> 1227c478bd9Sstevel@tonic-gate #include <vm/page.h> 1237c478bd9Sstevel@tonic-gate #include <vm/vpage.h> 1247c478bd9Sstevel@tonic-gate #include <vm/seg_spt.h> 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate #include <c2/audit.h> 1277c478bd9Sstevel@tonic-gate 128c6939658Ssl108498 static int shmem_lock(kshmid_t *sp, struct anon_map *amp); 129c6939658Ssl108498 static void shmem_unlock(kshmid_t *sp, struct anon_map *amp); 1307c478bd9Sstevel@tonic-gate static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, 1317c478bd9Sstevel@tonic-gate kshmid_t *id); 13268803f2dSsl108498 static void shm_rm_amp(kshmid_t *sp); 1337c478bd9Sstevel@tonic-gate static void shm_dtor(kipc_perm_t *); 1347c478bd9Sstevel@tonic-gate static void shm_rmid(kipc_perm_t *); 1357c478bd9Sstevel@tonic-gate static void shm_remove_zone(zoneid_t, void *); 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate /* 1387c478bd9Sstevel@tonic-gate * Semantics for share_page_table and ism_off: 1397c478bd9Sstevel@tonic-gate * 1407c478bd9Sstevel@tonic-gate * These are hooks in /etc/system - only for internal testing purpose. 1417c478bd9Sstevel@tonic-gate * 1427c478bd9Sstevel@tonic-gate * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag 1437c478bd9Sstevel@tonic-gate * in a call to shmat(2). In other words, with share_page_table set, you always 1447c478bd9Sstevel@tonic-gate * get ISM, even if say, DISM is specified. It should really be called "ism_on". 1457c478bd9Sstevel@tonic-gate * 1467c478bd9Sstevel@tonic-gate * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to 1477c478bd9Sstevel@tonic-gate * shmat(2). 1487c478bd9Sstevel@tonic-gate * 1497c478bd9Sstevel@tonic-gate * If both share_page_table and ism_off are set, share_page_table prevails. 1507c478bd9Sstevel@tonic-gate * 1517c478bd9Sstevel@tonic-gate * Although these tunables should probably be removed, they do have some 1527c478bd9Sstevel@tonic-gate * external exposure; as long as they exist, they should at least work sensibly. 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate int share_page_table; 1567c478bd9Sstevel@tonic-gate int ism_off; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * The following tunables are obsolete. Though for compatibility we 1607c478bd9Sstevel@tonic-gate * still read and interpret shminfo_shmmax and shminfo_shmmni (see 1617c478bd9Sstevel@tonic-gate * os/project.c), the preferred mechanism for administrating the IPC 1627c478bd9Sstevel@tonic-gate * Shared Memory facility is through the resource controls described at 1637c478bd9Sstevel@tonic-gate * the top of this file. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate size_t shminfo_shmmax = 0x800000; /* (obsolete) */ 1667c478bd9Sstevel@tonic-gate int shminfo_shmmni = 100; /* (obsolete) */ 1677c478bd9Sstevel@tonic-gate size_t shminfo_shmmin = 1; /* (obsolete) */ 1687c478bd9Sstevel@tonic-gate int shminfo_shmseg = 6; /* (obsolete) */ 1697c478bd9Sstevel@tonic-gate 170824c205fSml93401 extern rctl_hndl_t rc_zone_shmmax; 171824c205fSml93401 extern rctl_hndl_t rc_zone_shmmni; 1727c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmax; 1737c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmni; 1747c478bd9Sstevel@tonic-gate static ipc_service_t *shm_svc; 1757c478bd9Sstevel@tonic-gate static zone_key_t shm_zone_key; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t); 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent = { 1837c478bd9Sstevel@tonic-gate 4, 1847c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1857c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_64RVAL, 1867c478bd9Sstevel@tonic-gate #else /* _SYSCALL32_IMPL */ 1877c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 1887c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 1897c478bd9Sstevel@tonic-gate (int (*)())shmsys 1907c478bd9Sstevel@tonic-gate }; 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1937c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent32 = { 1947c478bd9Sstevel@tonic-gate 4, 1957c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 1967c478bd9Sstevel@tonic-gate (int (*)())shmsys 1977c478bd9Sstevel@tonic-gate }; 1987c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate static struct modlsys modlsys = { 2017c478bd9Sstevel@tonic-gate &mod_syscallops, "System V shared memory", &ipcshm_sysent 2027c478bd9Sstevel@tonic-gate }; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2057c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = { 2067c478bd9Sstevel@tonic-gate &mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32 2077c478bd9Sstevel@tonic-gate }; 2087c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 2117c478bd9Sstevel@tonic-gate MODREV_1, 2127c478bd9Sstevel@tonic-gate &modlsys, 2137c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 2147c478bd9Sstevel@tonic-gate &modlsys32, 2157c478bd9Sstevel@tonic-gate #endif 2167c478bd9Sstevel@tonic-gate NULL 2177c478bd9Sstevel@tonic-gate }; 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate int 2217c478bd9Sstevel@tonic-gate _init(void) 2227c478bd9Sstevel@tonic-gate { 2237c478bd9Sstevel@tonic-gate int result; 2247c478bd9Sstevel@tonic-gate 225824c205fSml93401 shm_svc = ipcs_create("shmids", rc_project_shmmni, rc_zone_shmmni, 226824c205fSml93401 sizeof (kshmid_t), shm_dtor, shm_rmid, AT_IPC_SHM, 227824c205fSml93401 offsetof(ipc_rqty_t, ipcq_shmmni)); 2287c478bd9Sstevel@tonic-gate zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL); 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate if ((result = mod_install(&modlinkage)) == 0) 2317c478bd9Sstevel@tonic-gate return (0); 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate (void) zone_key_delete(shm_zone_key); 2347c478bd9Sstevel@tonic-gate ipcs_destroy(shm_svc); 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate return (result); 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate int 2407c478bd9Sstevel@tonic-gate _fini(void) 2417c478bd9Sstevel@tonic-gate { 2427c478bd9Sstevel@tonic-gate return (EBUSY); 2437c478bd9Sstevel@tonic-gate } 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate int 2467c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 2477c478bd9Sstevel@tonic-gate { 2487c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Shmat (attach shared segment) system call. 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate static int 2557c478bd9Sstevel@tonic-gate shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp) 2567c478bd9Sstevel@tonic-gate { 2577c478bd9Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */ 2587c478bd9Sstevel@tonic-gate size_t size; 2597c478bd9Sstevel@tonic-gate int error = 0; 2607c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 2617c478bd9Sstevel@tonic-gate struct as *as = pp->p_as; 2627c478bd9Sstevel@tonic-gate struct segvn_crargs crargs; /* segvn create arguments */ 2637c478bd9Sstevel@tonic-gate kmutex_t *lock; 2647c478bd9Sstevel@tonic-gate struct seg *segspt = NULL; 2657c478bd9Sstevel@tonic-gate caddr_t addr = uaddr; 2667c478bd9Sstevel@tonic-gate int flags = (uflags & SHMAT_VALID_FLAGS_MASK); 2677c478bd9Sstevel@tonic-gate int useISM; 2687c478bd9Sstevel@tonic-gate uchar_t prot = PROT_ALL; 2697c478bd9Sstevel@tonic-gate int result; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL) 2727c478bd9Sstevel@tonic-gate return (EINVAL); 2737c478bd9Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED())) 2747c478bd9Sstevel@tonic-gate goto errret; 2757c478bd9Sstevel@tonic-gate if ((flags & SHM_RDONLY) == 0 && 2767c478bd9Sstevel@tonic-gate (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED()))) 2777c478bd9Sstevel@tonic-gate goto errret; 2787c478bd9Sstevel@tonic-gate if (spt_invalid(flags)) { 2797c478bd9Sstevel@tonic-gate error = EINVAL; 2807c478bd9Sstevel@tonic-gate goto errret; 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate if (ism_off) 2837c478bd9Sstevel@tonic-gate flags = flags & ~SHM_SHARE_MMU; 2847c478bd9Sstevel@tonic-gate if (share_page_table) { 2857c478bd9Sstevel@tonic-gate flags = flags & ~SHM_PAGEABLE; 2867c478bd9Sstevel@tonic-gate flags = flags | SHM_SHARE_MMU; 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate useISM = (spt_locked(flags) || spt_pageable(flags)); 2897c478bd9Sstevel@tonic-gate if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED()))) 2907c478bd9Sstevel@tonic-gate goto errret; 2917c478bd9Sstevel@tonic-gate if (useISM && isspt(sp)) { 2927c478bd9Sstevel@tonic-gate uint_t newsptflags = flags | spt_flags(sp->shm_sptseg); 2937c478bd9Sstevel@tonic-gate /* 2947c478bd9Sstevel@tonic-gate * If trying to change an existing {D}ISM segment from ISM 2957c478bd9Sstevel@tonic-gate * to DISM or vice versa, return error. Note that this 2967c478bd9Sstevel@tonic-gate * validation of flags needs to be done after the effect of 2977c478bd9Sstevel@tonic-gate * tunables such as ism_off and share_page_table, for 2987c478bd9Sstevel@tonic-gate * semantics that are consistent with the tunables' settings. 2997c478bd9Sstevel@tonic-gate */ 3007c478bd9Sstevel@tonic-gate if (spt_invalid(newsptflags)) { 3017c478bd9Sstevel@tonic-gate error = EINVAL; 3027c478bd9Sstevel@tonic-gate goto errret; 3037c478bd9Sstevel@tonic-gate } 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 3067c478bd9Sstevel@tonic-gate size = sp->shm_amp->size; 3077c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate /* somewhere to record spt info for final detach */ 3107c478bd9Sstevel@tonic-gate if (sp->shm_sptinfo == NULL) 3117c478bd9Sstevel@tonic-gate sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate as_rangelock(as); 3147c478bd9Sstevel@tonic-gate 3157c478bd9Sstevel@tonic-gate if (useISM) { 3167c478bd9Sstevel@tonic-gate /* 3177c478bd9Sstevel@tonic-gate * Handle ISM 3187c478bd9Sstevel@tonic-gate */ 319ae115bc7Smrj uint_t share_szc; 3207c478bd9Sstevel@tonic-gate size_t share_size; 3217c478bd9Sstevel@tonic-gate struct shm_data ssd; 3227c478bd9Sstevel@tonic-gate uintptr_t align_hint; 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate /* 3257c478bd9Sstevel@tonic-gate * Pick a share pagesize to use, if (!isspt(sp)). 3267c478bd9Sstevel@tonic-gate * Otherwise use the already chosen page size. 3277c478bd9Sstevel@tonic-gate * 3287c478bd9Sstevel@tonic-gate * For the initial shmat (!isspt(sp)), where sptcreate is 3297c478bd9Sstevel@tonic-gate * called, map_pgsz is called to recommend a [D]ISM pagesize, 3307c478bd9Sstevel@tonic-gate * important for systems which offer more than one potential 3317c478bd9Sstevel@tonic-gate * [D]ISM pagesize. 3327c478bd9Sstevel@tonic-gate * If the shmat is just to attach to an already created 3337c478bd9Sstevel@tonic-gate * [D]ISM segment, then use the previously selected page size. 3347c478bd9Sstevel@tonic-gate */ 3357c478bd9Sstevel@tonic-gate if (!isspt(sp)) { 336ec25b48fSsusans share_size = map_pgsz(MAPPGSZ_ISM, pp, addr, size, 0); 3377c478bd9Sstevel@tonic-gate if (share_size == 0) { 3387c478bd9Sstevel@tonic-gate as_rangeunlock(as); 3397c478bd9Sstevel@tonic-gate error = EINVAL; 3407c478bd9Sstevel@tonic-gate goto errret; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate share_szc = page_szc(share_size); 3437c478bd9Sstevel@tonic-gate } else { 3447c478bd9Sstevel@tonic-gate share_szc = sp->shm_sptseg->s_szc; 3457c478bd9Sstevel@tonic-gate share_size = page_get_pagesize(share_szc); 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate size = P2ROUNDUP(size, share_size); 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate align_hint = share_size; 3507c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 3517c478bd9Sstevel@tonic-gate /* 352ae115bc7Smrj * For x86, we want to share as much of the page table tree 353ae115bc7Smrj * as possible. We use a large align_hint at first, but 354ae115bc7Smrj * if that fails, then the code below retries with align_hint 355ae115bc7Smrj * set to share_size. 356ae115bc7Smrj * 357ae115bc7Smrj * The explicit extern here is due to the difficulties 358ae115bc7Smrj * of getting to platform dependent includes. When/if the 359ae115bc7Smrj * platform dependent bits of this function are cleaned up, 360ae115bc7Smrj * another way of doing this should found. 3617c478bd9Sstevel@tonic-gate */ 362ae115bc7Smrj { 363ae115bc7Smrj extern uint_t ptes_per_table; 364ae115bc7Smrj 365ae115bc7Smrj while (size >= ptes_per_table * (uint64_t)align_hint) 366ae115bc7Smrj align_hint *= ptes_per_table; 367ae115bc7Smrj } 3687c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate #if defined(__sparcv9) 371a0de58d6SRoger A. Faulkner if (addr == 0 && 372a0de58d6SRoger A. Faulkner pp->p_model == DATAMODEL_LP64 && AS_TYPE_64BIT(as)) { 3737c478bd9Sstevel@tonic-gate /* 3747c478bd9Sstevel@tonic-gate * If no address has been passed in, and this is a 3757c478bd9Sstevel@tonic-gate * 64-bit process, we'll try to find an address 3767c478bd9Sstevel@tonic-gate * in the predict-ISM zone. 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate caddr_t predbase = (caddr_t)PREDISM_1T_BASE; 3797c478bd9Sstevel@tonic-gate size_t len = PREDISM_BOUND - PREDISM_1T_BASE; 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate as_purge(as); 3827c478bd9Sstevel@tonic-gate if (as_gap(as, size + share_size, &predbase, &len, 3837c478bd9Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != -1) { 3847c478bd9Sstevel@tonic-gate /* 3857c478bd9Sstevel@tonic-gate * We found an address which looks like a 3867c478bd9Sstevel@tonic-gate * candidate. We want to round it up, and 3877c478bd9Sstevel@tonic-gate * then check that it's a valid user range. 3887c478bd9Sstevel@tonic-gate * This assures that we won't fail below. 3897c478bd9Sstevel@tonic-gate */ 3907c478bd9Sstevel@tonic-gate addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase, 3917c478bd9Sstevel@tonic-gate share_size); 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, size, prot, 3947c478bd9Sstevel@tonic-gate as, as->a_userlimit) != RANGE_OKAY) { 3957c478bd9Sstevel@tonic-gate addr = 0; 3967c478bd9Sstevel@tonic-gate } 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate #endif /* __sparcv9 */ 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if (addr == 0) { 4027c478bd9Sstevel@tonic-gate for (;;) { 4037c478bd9Sstevel@tonic-gate addr = (caddr_t)align_hint; 4047c478bd9Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, MAP_ALIGN); 4057c478bd9Sstevel@tonic-gate if (addr != NULL || align_hint == share_size) 4067c478bd9Sstevel@tonic-gate break; 4077c478bd9Sstevel@tonic-gate align_hint = share_size; 4087c478bd9Sstevel@tonic-gate } 4097c478bd9Sstevel@tonic-gate if (addr == NULL) { 4107c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4117c478bd9Sstevel@tonic-gate error = ENOMEM; 4127c478bd9Sstevel@tonic-gate goto errret; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0); 4157c478bd9Sstevel@tonic-gate } else { 4167c478bd9Sstevel@tonic-gate /* Use the user-supplied attach address */ 4177c478bd9Sstevel@tonic-gate caddr_t base; 4187c478bd9Sstevel@tonic-gate size_t len; 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /* 4217c478bd9Sstevel@tonic-gate * Check that the address range 4227c478bd9Sstevel@tonic-gate * 1) is properly aligned 4237c478bd9Sstevel@tonic-gate * 2) is correct in unix terms 4247c478bd9Sstevel@tonic-gate * 3) is within an unmapped address segment 4257c478bd9Sstevel@tonic-gate */ 4267c478bd9Sstevel@tonic-gate base = addr; 4277c478bd9Sstevel@tonic-gate len = size; /* use spt aligned size */ 4287c478bd9Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */ 4297c478bd9Sstevel@tonic-gate if ((uintptr_t)base & (share_size - 1)) { 4307c478bd9Sstevel@tonic-gate error = EINVAL; 4317c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4327c478bd9Sstevel@tonic-gate goto errret; 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 4357c478bd9Sstevel@tonic-gate as->a_userlimit); 4367c478bd9Sstevel@tonic-gate if (result == RANGE_BADPROT) { 4377c478bd9Sstevel@tonic-gate /* 4387c478bd9Sstevel@tonic-gate * We try to accomodate processors which 4397c478bd9Sstevel@tonic-gate * may not support execute permissions on 4407c478bd9Sstevel@tonic-gate * all ISM segments by trying the check 4417c478bd9Sstevel@tonic-gate * again but without PROT_EXEC. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate prot &= ~PROT_EXEC; 4447c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 4457c478bd9Sstevel@tonic-gate as->a_userlimit); 4467c478bd9Sstevel@tonic-gate } 4477c478bd9Sstevel@tonic-gate as_purge(as); 4487c478bd9Sstevel@tonic-gate if (result != RANGE_OKAY || 4497c478bd9Sstevel@tonic-gate as_gap(as, len, &base, &len, AH_LO, 4507c478bd9Sstevel@tonic-gate (caddr_t)NULL) != 0) { 4517c478bd9Sstevel@tonic-gate error = EINVAL; 4527c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4537c478bd9Sstevel@tonic-gate goto errret; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate if (!isspt(sp)) { 4587c478bd9Sstevel@tonic-gate error = sptcreate(size, &segspt, sp->shm_amp, prot, 4597c478bd9Sstevel@tonic-gate flags, share_szc); 4607c478bd9Sstevel@tonic-gate if (error) { 4617c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4627c478bd9Sstevel@tonic-gate goto errret; 4637c478bd9Sstevel@tonic-gate } 4647c478bd9Sstevel@tonic-gate sp->shm_sptinfo->sptas = segspt->s_as; 4657c478bd9Sstevel@tonic-gate sp->shm_sptseg = segspt; 4667c478bd9Sstevel@tonic-gate sp->shm_sptprot = prot; 4677c478bd9Sstevel@tonic-gate } else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) { 4687c478bd9Sstevel@tonic-gate /* 4697c478bd9Sstevel@tonic-gate * Ensure we're attaching to an ISM segment with 4707c478bd9Sstevel@tonic-gate * fewer or equal permissions than what we're 4717c478bd9Sstevel@tonic-gate * allowed. Fail if the segment has more 4727c478bd9Sstevel@tonic-gate * permissions than what we're allowed. 4737c478bd9Sstevel@tonic-gate */ 4747c478bd9Sstevel@tonic-gate error = EACCES; 4757c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4767c478bd9Sstevel@tonic-gate goto errret; 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate ssd.shm_sptseg = sp->shm_sptseg; 4807c478bd9Sstevel@tonic-gate ssd.shm_sptas = sp->shm_sptinfo->sptas; 4817c478bd9Sstevel@tonic-gate ssd.shm_amp = sp->shm_amp; 4827c478bd9Sstevel@tonic-gate error = as_map(as, addr, size, segspt_shmattach, &ssd); 4837c478bd9Sstevel@tonic-gate if (error == 0) 4847c478bd9Sstevel@tonic-gate sp->shm_ismattch++; /* keep count of ISM attaches */ 4857c478bd9Sstevel@tonic-gate } else { 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate /* 4887c478bd9Sstevel@tonic-gate * Normal case. 4897c478bd9Sstevel@tonic-gate */ 4907c478bd9Sstevel@tonic-gate if (flags & SHM_RDONLY) 4917c478bd9Sstevel@tonic-gate prot &= ~PROT_WRITE; 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate if (addr == 0) { 4947c478bd9Sstevel@tonic-gate /* Let the system pick the attach address */ 4957c478bd9Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, 0); 4967c478bd9Sstevel@tonic-gate if (addr == NULL) { 4977c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4987c478bd9Sstevel@tonic-gate error = ENOMEM; 4997c478bd9Sstevel@tonic-gate goto errret; 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate } else { 5027c478bd9Sstevel@tonic-gate /* Use the user-supplied attach address */ 5037c478bd9Sstevel@tonic-gate caddr_t base; 5047c478bd9Sstevel@tonic-gate size_t len; 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate if (flags & SHM_RND) 5077c478bd9Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & 5087c478bd9Sstevel@tonic-gate ~(SHMLBA - 1)); 5097c478bd9Sstevel@tonic-gate /* 5107c478bd9Sstevel@tonic-gate * Check that the address range 5117c478bd9Sstevel@tonic-gate * 1) is properly aligned 5127c478bd9Sstevel@tonic-gate * 2) is correct in unix terms 5137c478bd9Sstevel@tonic-gate * 3) is within an unmapped address segment 5147c478bd9Sstevel@tonic-gate */ 5157c478bd9Sstevel@tonic-gate base = addr; 5167c478bd9Sstevel@tonic-gate len = size; /* use aligned size */ 5177c478bd9Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */ 5187c478bd9Sstevel@tonic-gate if ((uintptr_t)base & PAGEOFFSET) { 5197c478bd9Sstevel@tonic-gate error = EINVAL; 5207c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5217c478bd9Sstevel@tonic-gate goto errret; 5227c478bd9Sstevel@tonic-gate } 5237c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 5247c478bd9Sstevel@tonic-gate as->a_userlimit); 5257c478bd9Sstevel@tonic-gate if (result == RANGE_BADPROT) { 5267c478bd9Sstevel@tonic-gate prot &= ~PROT_EXEC; 5277c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 5287c478bd9Sstevel@tonic-gate as->a_userlimit); 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate as_purge(as); 5317c478bd9Sstevel@tonic-gate if (result != RANGE_OKAY || 5327c478bd9Sstevel@tonic-gate as_gap(as, len, &base, &len, 5337c478bd9Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != 0) { 5347c478bd9Sstevel@tonic-gate error = EINVAL; 5357c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5367c478bd9Sstevel@tonic-gate goto errret; 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* Initialize the create arguments and map the segment */ 5417c478bd9Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp; 5427c478bd9Sstevel@tonic-gate crargs.offset = 0; 5437c478bd9Sstevel@tonic-gate crargs.type = MAP_SHARED; 5447c478bd9Sstevel@tonic-gate crargs.amp = sp->shm_amp; 5457c478bd9Sstevel@tonic-gate crargs.prot = prot; 5467c478bd9Sstevel@tonic-gate crargs.maxprot = crargs.prot; 5477c478bd9Sstevel@tonic-gate crargs.flags = 0; 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate error = as_map(as, addr, size, segvn_create, &crargs); 5507c478bd9Sstevel@tonic-gate } 5517c478bd9Sstevel@tonic-gate 5527c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5537c478bd9Sstevel@tonic-gate if (error) 5547c478bd9Sstevel@tonic-gate goto errret; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate /* record shmem range for the detach */ 5577c478bd9Sstevel@tonic-gate sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp); 5587c478bd9Sstevel@tonic-gate *rvp = (uintptr_t)addr; 5597c478bd9Sstevel@tonic-gate 5607c478bd9Sstevel@tonic-gate sp->shm_atime = gethrestime_sec(); 5617c478bd9Sstevel@tonic-gate sp->shm_lpid = pp->p_pid; 5627c478bd9Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp); 5632c5124a1SPrashanth Sreenivasa 5642c5124a1SPrashanth Sreenivasa /* 5652c5124a1SPrashanth Sreenivasa * Tell machine specific code that lwp has mapped shared memory 5662c5124a1SPrashanth Sreenivasa */ 5672c5124a1SPrashanth Sreenivasa LWP_MMODEL_SHARED_AS(addr, size); 5682c5124a1SPrashanth Sreenivasa 5697c478bd9Sstevel@tonic-gate errret: 5707c478bd9Sstevel@tonic-gate mutex_exit(lock); 5717c478bd9Sstevel@tonic-gate return (error); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate static void 5757c478bd9Sstevel@tonic-gate shm_dtor(kipc_perm_t *perm) 5767c478bd9Sstevel@tonic-gate { 5777c478bd9Sstevel@tonic-gate kshmid_t *sp = (kshmid_t *)perm; 5787c478bd9Sstevel@tonic-gate uint_t cnt; 579824c205fSml93401 size_t rsize; 5807c478bd9Sstevel@tonic-gate 581*57820e88SNick Todd ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 582*57820e88SNick Todd anonmap_purge(sp->shm_amp); 583*57820e88SNick Todd ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 584*57820e88SNick Todd 585552507c5SGangadhar Mylapuram if (sp->shm_sptinfo) { 586552507c5SGangadhar Mylapuram if (isspt(sp)) { 587552507c5SGangadhar Mylapuram sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp); 588552507c5SGangadhar Mylapuram sp->shm_lkcnt = 0; 589552507c5SGangadhar Mylapuram } 590552507c5SGangadhar Mylapuram kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t)); 591552507c5SGangadhar Mylapuram } 592552507c5SGangadhar Mylapuram 593c6939658Ssl108498 if (sp->shm_lkcnt > 0) { 594c6939658Ssl108498 shmem_unlock(sp, sp->shm_amp); 595c6939658Ssl108498 sp->shm_lkcnt = 0; 596c6939658Ssl108498 } 597c6939658Ssl108498 5987c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 5997c478bd9Sstevel@tonic-gate cnt = --sp->shm_amp->refcnt; 6007c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 6017c478bd9Sstevel@tonic-gate ASSERT(cnt == 0); 60268803f2dSsl108498 shm_rm_amp(sp); 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate if (sp->shm_perm.ipc_id != IPC_ID_INVAL) { 605824c205fSml93401 rsize = ptob(btopr(sp->shm_segsz)); 6067c478bd9Sstevel@tonic-gate ipcs_lock(shm_svc); 607824c205fSml93401 sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= rsize; 608824c205fSml93401 sp->shm_perm.ipc_zone->zone_shmmax -= rsize; 6097c478bd9Sstevel@tonic-gate ipcs_unlock(shm_svc); 6107c478bd9Sstevel@tonic-gate } 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* ARGSUSED */ 6147c478bd9Sstevel@tonic-gate static void 6157c478bd9Sstevel@tonic-gate shm_rmid(kipc_perm_t *perm) 6167c478bd9Sstevel@tonic-gate { 6177c478bd9Sstevel@tonic-gate /* nothing to do */ 6187c478bd9Sstevel@tonic-gate } 6197c478bd9Sstevel@tonic-gate 6207c478bd9Sstevel@tonic-gate /* 6217c478bd9Sstevel@tonic-gate * Shmctl system call. 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate /* ARGSUSED */ 6247c478bd9Sstevel@tonic-gate static int 6257c478bd9Sstevel@tonic-gate shmctl(int shmid, int cmd, void *arg) 6267c478bd9Sstevel@tonic-gate { 6277c478bd9Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */ 6287c478bd9Sstevel@tonic-gate STRUCT_DECL(shmid_ds, ds); /* for SVR4 IPC_SET */ 6297c478bd9Sstevel@tonic-gate int error = 0; 6307c478bd9Sstevel@tonic-gate struct cred *cr = CRED(); 6317c478bd9Sstevel@tonic-gate kmutex_t *lock; 6327c478bd9Sstevel@tonic-gate model_t mdl = get_udatamodel(); 6337c478bd9Sstevel@tonic-gate struct shmid_ds64 ds64; 6347c478bd9Sstevel@tonic-gate shmatt_t nattch; 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate STRUCT_INIT(ds, mdl); 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate /* 6397c478bd9Sstevel@tonic-gate * Perform pre- or non-lookup actions (e.g. copyins, RMID). 6407c478bd9Sstevel@tonic-gate */ 6417c478bd9Sstevel@tonic-gate switch (cmd) { 6427c478bd9Sstevel@tonic-gate case IPC_SET: 6437c478bd9Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds))) 6447c478bd9Sstevel@tonic-gate return (EFAULT); 6457c478bd9Sstevel@tonic-gate break; 6467c478bd9Sstevel@tonic-gate 6477c478bd9Sstevel@tonic-gate case IPC_SET64: 6487c478bd9Sstevel@tonic-gate if (copyin(arg, &ds64, sizeof (struct shmid_ds64))) 6497c478bd9Sstevel@tonic-gate return (EFAULT); 6507c478bd9Sstevel@tonic-gate break; 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate case IPC_RMID: 6537c478bd9Sstevel@tonic-gate return (ipc_rmid(shm_svc, shmid, cr)); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate 6567c478bd9Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL) 6577c478bd9Sstevel@tonic-gate return (EINVAL); 6587c478bd9Sstevel@tonic-gate 6597c478bd9Sstevel@tonic-gate switch (cmd) { 6607c478bd9Sstevel@tonic-gate /* Set ownership and permissions. */ 6617c478bd9Sstevel@tonic-gate case IPC_SET: 6627c478bd9Sstevel@tonic-gate if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm, 6637c478bd9Sstevel@tonic-gate &STRUCT_BUF(ds)->shm_perm, mdl)) 6647c478bd9Sstevel@tonic-gate break; 6657c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 6667c478bd9Sstevel@tonic-gate break; 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate case IPC_STAT: 6697c478bd9Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr)) 6707c478bd9Sstevel@tonic-gate break; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1; 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl); 6757c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_segsz, sp->shm_segsz); 6767c478bd9Sstevel@tonic-gate STRUCT_FSETP(ds, shm_amp, NULL); /* kernel addr */ 6777c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt); 6787c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_lpid, sp->shm_lpid); 6797c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_cpid, sp->shm_cpid); 6807c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_nattch, nattch); 6817c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch); 6827c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_atime, sp->shm_atime); 6837c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_dtime, sp->shm_dtime); 6847c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_ctime, sp->shm_ctime); 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate mutex_exit(lock); 6877c478bd9Sstevel@tonic-gate if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds))) 6887c478bd9Sstevel@tonic-gate return (EFAULT); 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate return (0); 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate case IPC_SET64: 6937c478bd9Sstevel@tonic-gate if (error = ipcperm_set64(shm_svc, cr, 6947c478bd9Sstevel@tonic-gate &sp->shm_perm, &ds64.shmx_perm)) 6957c478bd9Sstevel@tonic-gate break; 6967c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 6977c478bd9Sstevel@tonic-gate break; 6987c478bd9Sstevel@tonic-gate 6997c478bd9Sstevel@tonic-gate case IPC_STAT64: 7007c478bd9Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm); 7037c478bd9Sstevel@tonic-gate ds64.shmx_segsz = sp->shm_segsz; 7047c478bd9Sstevel@tonic-gate ds64.shmx_lkcnt = sp->shm_lkcnt; 7057c478bd9Sstevel@tonic-gate ds64.shmx_lpid = sp->shm_lpid; 7067c478bd9Sstevel@tonic-gate ds64.shmx_cpid = sp->shm_cpid; 7077c478bd9Sstevel@tonic-gate ds64.shmx_nattch = nattch; 7087c478bd9Sstevel@tonic-gate ds64.shmx_cnattch = sp->shm_ismattch; 7097c478bd9Sstevel@tonic-gate ds64.shmx_atime = sp->shm_atime; 7107c478bd9Sstevel@tonic-gate ds64.shmx_dtime = sp->shm_dtime; 7117c478bd9Sstevel@tonic-gate ds64.shmx_ctime = sp->shm_ctime; 7127c478bd9Sstevel@tonic-gate 7137c478bd9Sstevel@tonic-gate mutex_exit(lock); 7147c478bd9Sstevel@tonic-gate if (copyout(&ds64, arg, sizeof (struct shmid_ds64))) 7157c478bd9Sstevel@tonic-gate return (EFAULT); 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate return (0); 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate /* Lock segment in memory */ 7207c478bd9Sstevel@tonic-gate case SHM_LOCK: 7217c478bd9Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0) 7227c478bd9Sstevel@tonic-gate break; 7237c478bd9Sstevel@tonic-gate 724c6939658Ssl108498 /* protect against overflow */ 725c6939658Ssl108498 if (sp->shm_lkcnt >= USHRT_MAX) { 726c6939658Ssl108498 error = ENOMEM; 727c6939658Ssl108498 break; 728c6939658Ssl108498 } 7297c478bd9Sstevel@tonic-gate if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) { 730c6939658Ssl108498 if (error = shmem_lock(sp, sp->shm_amp)) { 731d3e55dcdSgww ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, 732d3e55dcdSgww RW_WRITER); 733a98e9dbfSaguzovsk cmn_err(CE_NOTE, "shmctl - couldn't lock %ld" 734a98e9dbfSaguzovsk " pages into memory", sp->shm_amp->size); 7357c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 7367c478bd9Sstevel@tonic-gate error = ENOMEM; 7377c478bd9Sstevel@tonic-gate sp->shm_lkcnt--; 7387c478bd9Sstevel@tonic-gate } 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate break; 7417c478bd9Sstevel@tonic-gate 7427c478bd9Sstevel@tonic-gate /* Unlock segment */ 7437c478bd9Sstevel@tonic-gate case SHM_UNLOCK: 7447c478bd9Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0) 7457c478bd9Sstevel@tonic-gate break; 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) { 748c6939658Ssl108498 shmem_unlock(sp, sp->shm_amp); 7497c478bd9Sstevel@tonic-gate } 7507c478bd9Sstevel@tonic-gate break; 7517c478bd9Sstevel@tonic-gate 7527c478bd9Sstevel@tonic-gate default: 7537c478bd9Sstevel@tonic-gate error = EINVAL; 7547c478bd9Sstevel@tonic-gate break; 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate mutex_exit(lock); 7577c478bd9Sstevel@tonic-gate return (error); 7587c478bd9Sstevel@tonic-gate } 7597c478bd9Sstevel@tonic-gate 7607c478bd9Sstevel@tonic-gate static void 7617c478bd9Sstevel@tonic-gate shm_detach(proc_t *pp, segacct_t *sap) 7627c478bd9Sstevel@tonic-gate { 7637c478bd9Sstevel@tonic-gate kshmid_t *sp = sap->sa_id; 7647c478bd9Sstevel@tonic-gate size_t len = sap->sa_len; 7657c478bd9Sstevel@tonic-gate caddr_t addr = sap->sa_addr; 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate /* 7687c478bd9Sstevel@tonic-gate * Discard lwpchan mappings. 7697c478bd9Sstevel@tonic-gate */ 7707c478bd9Sstevel@tonic-gate if (pp->p_lcp != NULL) 7717c478bd9Sstevel@tonic-gate lwpchan_delete_mapping(pp, addr, addr + len); 7727c478bd9Sstevel@tonic-gate (void) as_unmap(pp->p_as, addr, len); 7737c478bd9Sstevel@tonic-gate 7747c478bd9Sstevel@tonic-gate /* 7757c478bd9Sstevel@tonic-gate * Perform some detach-time accounting. 7767c478bd9Sstevel@tonic-gate */ 7777c478bd9Sstevel@tonic-gate (void) ipc_lock(shm_svc, sp->shm_perm.ipc_id); 7787c478bd9Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM) 7797c478bd9Sstevel@tonic-gate sp->shm_ismattch--; 7807c478bd9Sstevel@tonic-gate sp->shm_dtime = gethrestime_sec(); 7817c478bd9Sstevel@tonic-gate sp->shm_lpid = pp->p_pid; 7827c478bd9Sstevel@tonic-gate ipc_rele(shm_svc, (kipc_perm_t *)sp); /* Drops lock */ 7837c478bd9Sstevel@tonic-gate 7847c478bd9Sstevel@tonic-gate kmem_free(sap, sizeof (segacct_t)); 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate static int 7887c478bd9Sstevel@tonic-gate shmdt(caddr_t addr) 7897c478bd9Sstevel@tonic-gate { 7907c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 7917c478bd9Sstevel@tonic-gate segacct_t *sap, template; 7927c478bd9Sstevel@tonic-gate 7937c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 7947c478bd9Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */ 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate template.sa_addr = addr; 7977c478bd9Sstevel@tonic-gate template.sa_len = 0; 7987c478bd9Sstevel@tonic-gate if ((pp->p_segacct == NULL) || 7997c478bd9Sstevel@tonic-gate ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) { 8007c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8017c478bd9Sstevel@tonic-gate return (EINVAL); 8027c478bd9Sstevel@tonic-gate } 80307b65a64Saguzovsk if (sap->sa_addr != addr) { 80407b65a64Saguzovsk mutex_exit(&pp->p_lock); 80507b65a64Saguzovsk return (EINVAL); 80607b65a64Saguzovsk } 8077c478bd9Sstevel@tonic-gate avl_remove(pp->p_segacct, sap); 8087c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate shm_detach(pp, sap); 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate return (0); 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * Remove all shared memory segments associated with a given zone. 8177c478bd9Sstevel@tonic-gate * Called by zone_shutdown when the zone is halted. 8187c478bd9Sstevel@tonic-gate */ 8197c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 8207c478bd9Sstevel@tonic-gate static void 8217c478bd9Sstevel@tonic-gate shm_remove_zone(zoneid_t zoneid, void *arg) 8227c478bd9Sstevel@tonic-gate { 8237c478bd9Sstevel@tonic-gate ipc_remove_zone(shm_svc, zoneid); 8247c478bd9Sstevel@tonic-gate } 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate /* 8277c478bd9Sstevel@tonic-gate * Shmget (create new shmem) system call. 8287c478bd9Sstevel@tonic-gate */ 8297c478bd9Sstevel@tonic-gate static int 8307c478bd9Sstevel@tonic-gate shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp) 8317c478bd9Sstevel@tonic-gate { 8327c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 8337c478bd9Sstevel@tonic-gate kshmid_t *sp; 8347c478bd9Sstevel@tonic-gate kmutex_t *lock; 8357c478bd9Sstevel@tonic-gate int error; 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate top: 8387c478bd9Sstevel@tonic-gate if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock)) 8397c478bd9Sstevel@tonic-gate return (error); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate if (!IPC_FREE(&sp->shm_perm)) { 8427c478bd9Sstevel@tonic-gate /* 8437c478bd9Sstevel@tonic-gate * A segment with the requested key exists. 8447c478bd9Sstevel@tonic-gate */ 8457c478bd9Sstevel@tonic-gate if (size > sp->shm_segsz) { 8467c478bd9Sstevel@tonic-gate mutex_exit(lock); 8477c478bd9Sstevel@tonic-gate return (EINVAL); 8487c478bd9Sstevel@tonic-gate } 8497c478bd9Sstevel@tonic-gate } else { 8507c478bd9Sstevel@tonic-gate /* 8517c478bd9Sstevel@tonic-gate * A new segment should be created. 8527c478bd9Sstevel@tonic-gate */ 8537c478bd9Sstevel@tonic-gate size_t npages = btopr(size); 8547c478bd9Sstevel@tonic-gate size_t rsize = ptob(npages); 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* 857824c205fSml93401 * Check rsize and the per-project and per-zone limit on 858824c205fSml93401 * shared memory. Checking rsize handles both the size == 0 8597c478bd9Sstevel@tonic-gate * case and the size < ULONG_MAX & PAGEMASK case (i.e. 8607c478bd9Sstevel@tonic-gate * rounding up wraps a size_t). 8617c478bd9Sstevel@tonic-gate */ 862824c205fSml93401 if (rsize == 0 || 863824c205fSml93401 (rctl_test(rc_project_shmmax, 8647c478bd9Sstevel@tonic-gate pp->p_task->tk_proj->kpj_rctls, pp, rsize, 865824c205fSml93401 RCA_SAFE) & RCT_DENY) || 866824c205fSml93401 (rctl_test(rc_zone_shmmax, 867824c205fSml93401 pp->p_zone->zone_rctls, pp, rsize, 8687c478bd9Sstevel@tonic-gate RCA_SAFE) & RCT_DENY)) { 8697c478bd9Sstevel@tonic-gate 8707c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8717c478bd9Sstevel@tonic-gate mutex_exit(lock); 8727c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 8737c478bd9Sstevel@tonic-gate return (EINVAL); 8747c478bd9Sstevel@tonic-gate } 8757c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8767c478bd9Sstevel@tonic-gate mutex_exit(lock); 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate if (anon_resv(rsize) == 0) { 8797c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 8807c478bd9Sstevel@tonic-gate return (ENOMEM); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate 88367253d2cSsl108498 /* 88467253d2cSsl108498 * If any new failure points are introduced between the 88567253d2cSsl108498 * the above anon_resv() and the below ipc_commit_begin(), 88667253d2cSsl108498 * these failure points will need to unreserve the anon 88767253d2cSsl108498 * reserved using anon_unresv(). 88867253d2cSsl108498 * 88967253d2cSsl108498 * Once ipc_commit_begin() is called, the anon reserved 89067253d2cSsl108498 * above will be automatically unreserved by future calls to 89167253d2cSsl108498 * ipcs_cleanup() -> shm_dtor() -> shm_rm_amp(). If 89267253d2cSsl108498 * ipc_commit_begin() fails, it internally calls shm_dtor(), 89367253d2cSsl108498 * unreserving the above anon, and freeing the below amp. 89467253d2cSsl108498 */ 89567253d2cSsl108498 8962cb27123Saguzovsk sp->shm_amp = anonmap_alloc(rsize, rsize, ANON_SLEEP); 897c6939658Ssl108498 sp->shm_amp->a_sp = sp; 8987c478bd9Sstevel@tonic-gate /* 8997c478bd9Sstevel@tonic-gate * Store the original user's requested size, in bytes, 9007c478bd9Sstevel@tonic-gate * rather than the page-aligned size. The former is 9017c478bd9Sstevel@tonic-gate * used for IPC_STAT and shmget() lookups. The latter 9027c478bd9Sstevel@tonic-gate * is saved in the anon_map structure and is used for 9037c478bd9Sstevel@tonic-gate * calls to the vm layer. 9047c478bd9Sstevel@tonic-gate */ 9057c478bd9Sstevel@tonic-gate sp->shm_segsz = size; 9067c478bd9Sstevel@tonic-gate sp->shm_atime = sp->shm_dtime = 0; 9077c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 9087c478bd9Sstevel@tonic-gate sp->shm_lpid = (pid_t)0; 9097c478bd9Sstevel@tonic-gate sp->shm_cpid = curproc->p_pid; 9107c478bd9Sstevel@tonic-gate sp->shm_ismattch = 0; 9117c478bd9Sstevel@tonic-gate sp->shm_sptinfo = NULL; 9127c478bd9Sstevel@tonic-gate /* 9137c478bd9Sstevel@tonic-gate * Check limits one last time, push id into global 9147c478bd9Sstevel@tonic-gate * visibility, and update resource usage counts. 9157c478bd9Sstevel@tonic-gate */ 9167c478bd9Sstevel@tonic-gate if (error = ipc_commit_begin(shm_svc, key, shmflg, 9177c478bd9Sstevel@tonic-gate (kipc_perm_t *)sp)) { 9187c478bd9Sstevel@tonic-gate if (error == EAGAIN) 9197c478bd9Sstevel@tonic-gate goto top; 9207c478bd9Sstevel@tonic-gate return (error); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate 923824c205fSml93401 if ((rctl_test(rc_project_shmmax, 9247c478bd9Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize, 925824c205fSml93401 RCA_SAFE) & RCT_DENY) || 926824c205fSml93401 (rctl_test(rc_zone_shmmax, 927824c205fSml93401 sp->shm_perm.ipc_zone->zone_rctls, pp, rsize, 928824c205fSml93401 RCA_SAFE) & RCT_DENY)) { 9297c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 9307c478bd9Sstevel@tonic-gate return (EINVAL); 9317c478bd9Sstevel@tonic-gate } 9327c478bd9Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize; 933824c205fSml93401 sp->shm_perm.ipc_zone->zone_shmmax += rsize; 9347c478bd9Sstevel@tonic-gate 9357c478bd9Sstevel@tonic-gate lock = ipc_commit_end(shm_svc, &sp->shm_perm); 9367c478bd9Sstevel@tonic-gate } 9377c478bd9Sstevel@tonic-gate 9387c478bd9Sstevel@tonic-gate if (audit_active) 9397c478bd9Sstevel@tonic-gate audit_ipcget(AT_IPC_SHM, (void *)sp); 9407c478bd9Sstevel@tonic-gate 9417c478bd9Sstevel@tonic-gate *rvp = (uintptr_t)(sp->shm_perm.ipc_id); 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate mutex_exit(lock); 9447c478bd9Sstevel@tonic-gate return (0); 9457c478bd9Sstevel@tonic-gate } 9467c478bd9Sstevel@tonic-gate 9477c478bd9Sstevel@tonic-gate /* 9487c478bd9Sstevel@tonic-gate * shmids system call. 9497c478bd9Sstevel@tonic-gate */ 9507c478bd9Sstevel@tonic-gate static int 9517c478bd9Sstevel@tonic-gate shmids(int *buf, uint_t nids, uint_t *pnids) 9527c478bd9Sstevel@tonic-gate { 9537c478bd9Sstevel@tonic-gate return (ipc_ids(shm_svc, buf, nids, pnids)); 9547c478bd9Sstevel@tonic-gate } 9557c478bd9Sstevel@tonic-gate 9567c478bd9Sstevel@tonic-gate /* 9577c478bd9Sstevel@tonic-gate * System entry point for shmat, shmctl, shmdt, and shmget system calls. 9587c478bd9Sstevel@tonic-gate */ 9597c478bd9Sstevel@tonic-gate static uintptr_t 9607c478bd9Sstevel@tonic-gate shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2) 9617c478bd9Sstevel@tonic-gate { 9627c478bd9Sstevel@tonic-gate int error; 9637c478bd9Sstevel@tonic-gate uintptr_t r_val = 0; 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate switch (opcode) { 9667c478bd9Sstevel@tonic-gate case SHMAT: 9677c478bd9Sstevel@tonic-gate error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val); 9687c478bd9Sstevel@tonic-gate break; 9697c478bd9Sstevel@tonic-gate case SHMCTL: 9707c478bd9Sstevel@tonic-gate error = shmctl((int)a0, (int)a1, (void *)a2); 9717c478bd9Sstevel@tonic-gate break; 9727c478bd9Sstevel@tonic-gate case SHMDT: 9737c478bd9Sstevel@tonic-gate error = shmdt((caddr_t)a0); 9747c478bd9Sstevel@tonic-gate break; 9757c478bd9Sstevel@tonic-gate case SHMGET: 9767c478bd9Sstevel@tonic-gate error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val); 9777c478bd9Sstevel@tonic-gate break; 9787c478bd9Sstevel@tonic-gate case SHMIDS: 9797c478bd9Sstevel@tonic-gate error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2); 9807c478bd9Sstevel@tonic-gate break; 9817c478bd9Sstevel@tonic-gate default: 9827c478bd9Sstevel@tonic-gate error = EINVAL; 9837c478bd9Sstevel@tonic-gate break; 9847c478bd9Sstevel@tonic-gate } 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate if (error) 9877c478bd9Sstevel@tonic-gate return ((uintptr_t)set_errno(error)); 9887c478bd9Sstevel@tonic-gate 9897c478bd9Sstevel@tonic-gate return (r_val); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate /* 9937c478bd9Sstevel@tonic-gate * segacct_t comparator 9947c478bd9Sstevel@tonic-gate * This works as expected, with one minor change: the first of two real 9957c478bd9Sstevel@tonic-gate * segments with equal addresses is considered to be 'greater than' the 9967c478bd9Sstevel@tonic-gate * second. We only return equal when searching using a template, in 9977c478bd9Sstevel@tonic-gate * which case we explicitly set the template segment's length to 0 9987c478bd9Sstevel@tonic-gate * (which is invalid for a real segment). 9997c478bd9Sstevel@tonic-gate */ 10007c478bd9Sstevel@tonic-gate static int 10017c478bd9Sstevel@tonic-gate shm_sacompar(const void *x, const void *y) 10027c478bd9Sstevel@tonic-gate { 10037c478bd9Sstevel@tonic-gate segacct_t *sa1 = (segacct_t *)x; 10047c478bd9Sstevel@tonic-gate segacct_t *sa2 = (segacct_t *)y; 10057c478bd9Sstevel@tonic-gate 100607b65a64Saguzovsk if (sa1->sa_addr < sa2->sa_addr) { 10077c478bd9Sstevel@tonic-gate return (-1); 100807b65a64Saguzovsk } else if (sa2->sa_len != 0) { 100907b65a64Saguzovsk if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) { 10107c478bd9Sstevel@tonic-gate return (1); 101107b65a64Saguzovsk } else if (sa1->sa_len != 0) { 101207b65a64Saguzovsk return (1); 101307b65a64Saguzovsk } else { 10147c478bd9Sstevel@tonic-gate return (0); 101507b65a64Saguzovsk } 101607b65a64Saguzovsk } else if (sa1->sa_addr > sa2->sa_addr) { 10177c478bd9Sstevel@tonic-gate return (1); 101807b65a64Saguzovsk } else { 101907b65a64Saguzovsk return (0); 102007b65a64Saguzovsk } 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate 10237c478bd9Sstevel@tonic-gate /* 10247c478bd9Sstevel@tonic-gate * add this record to the segacct list. 10257c478bd9Sstevel@tonic-gate */ 10267c478bd9Sstevel@tonic-gate static void 10277c478bd9Sstevel@tonic-gate sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id) 10287c478bd9Sstevel@tonic-gate { 10297c478bd9Sstevel@tonic-gate segacct_t *nsap; 10307c478bd9Sstevel@tonic-gate avl_tree_t *tree = NULL; 10317c478bd9Sstevel@tonic-gate avl_index_t where; 10327c478bd9Sstevel@tonic-gate 10337c478bd9Sstevel@tonic-gate nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP); 10347c478bd9Sstevel@tonic-gate nsap->sa_addr = addr; 10357c478bd9Sstevel@tonic-gate nsap->sa_len = len; 10367c478bd9Sstevel@tonic-gate nsap->sa_flags = flags; 10377c478bd9Sstevel@tonic-gate nsap->sa_id = id; 10387c478bd9Sstevel@tonic-gate 10397c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) 10407c478bd9Sstevel@tonic-gate tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP); 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 10437c478bd9Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */ 10447c478bd9Sstevel@tonic-gate 10457c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) { 10467c478bd9Sstevel@tonic-gate avl_create(tree, shm_sacompar, sizeof (segacct_t), 10477c478bd9Sstevel@tonic-gate offsetof(segacct_t, sa_tree)); 10487c478bd9Sstevel@tonic-gate pp->p_segacct = tree; 10497c478bd9Sstevel@tonic-gate } else if (tree) { 10507c478bd9Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t)); 10517c478bd9Sstevel@tonic-gate } 10527c478bd9Sstevel@tonic-gate 10537c478bd9Sstevel@tonic-gate /* 10547c478bd9Sstevel@tonic-gate * We can ignore the result of avl_find, as the comparator will 10557c478bd9Sstevel@tonic-gate * never return equal for segments with non-zero length. This 10567c478bd9Sstevel@tonic-gate * is a necessary hack to get around the fact that we do, in 10577c478bd9Sstevel@tonic-gate * fact, have duplicate keys. 10587c478bd9Sstevel@tonic-gate */ 10597c478bd9Sstevel@tonic-gate (void) avl_find(pp->p_segacct, nsap, &where); 10607c478bd9Sstevel@tonic-gate avl_insert(pp->p_segacct, nsap, where); 10617c478bd9Sstevel@tonic-gate 10627c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate /* 10667c478bd9Sstevel@tonic-gate * Duplicate parent's segacct records in child. 10677c478bd9Sstevel@tonic-gate */ 10687c478bd9Sstevel@tonic-gate void 10697c478bd9Sstevel@tonic-gate shmfork(struct proc *ppp, struct proc *cpp) 10707c478bd9Sstevel@tonic-gate { 10717c478bd9Sstevel@tonic-gate segacct_t *sap; 10727c478bd9Sstevel@tonic-gate kshmid_t *sp; 10737c478bd9Sstevel@tonic-gate kmutex_t *mp; 10747c478bd9Sstevel@tonic-gate 10757c478bd9Sstevel@tonic-gate ASSERT(ppp->p_segacct != NULL); 10767c478bd9Sstevel@tonic-gate 10777c478bd9Sstevel@tonic-gate /* 10787c478bd9Sstevel@tonic-gate * We are the only lwp running in the parent so nobody can 10797c478bd9Sstevel@tonic-gate * mess with our p_segacct list. Thus it is safe to traverse 10807c478bd9Sstevel@tonic-gate * the list without holding p_lock. This is essential because 10817c478bd9Sstevel@tonic-gate * we can't hold p_lock during a KM_SLEEP allocation. 10827c478bd9Sstevel@tonic-gate */ 10837c478bd9Sstevel@tonic-gate for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL; 10847c478bd9Sstevel@tonic-gate sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) { 10857c478bd9Sstevel@tonic-gate sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags, 10867c478bd9Sstevel@tonic-gate sap->sa_id); 10877c478bd9Sstevel@tonic-gate sp = sap->sa_id; 10887c478bd9Sstevel@tonic-gate mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id); 10897c478bd9Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM) 10907c478bd9Sstevel@tonic-gate sp->shm_ismattch++; 10917c478bd9Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp); 10927c478bd9Sstevel@tonic-gate mutex_exit(mp); 10937c478bd9Sstevel@tonic-gate } 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate /* 10977c478bd9Sstevel@tonic-gate * Detach shared memory segments from exiting process. 10987c478bd9Sstevel@tonic-gate */ 10997c478bd9Sstevel@tonic-gate void 11007c478bd9Sstevel@tonic-gate shmexit(struct proc *pp) 11017c478bd9Sstevel@tonic-gate { 11027c478bd9Sstevel@tonic-gate segacct_t *sap; 11037c478bd9Sstevel@tonic-gate avl_tree_t *tree; 11047c478bd9Sstevel@tonic-gate void *cookie = NULL; 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate ASSERT(pp->p_segacct != NULL); 11077c478bd9Sstevel@tonic-gate 11087c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 11097c478bd9Sstevel@tonic-gate prbarrier(pp); 11107c478bd9Sstevel@tonic-gate tree = pp->p_segacct; 11117c478bd9Sstevel@tonic-gate pp->p_segacct = NULL; 11127c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 11137c478bd9Sstevel@tonic-gate 11147c478bd9Sstevel@tonic-gate while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL) 11157c478bd9Sstevel@tonic-gate (void) shm_detach(pp, sap); 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate avl_destroy(tree); 11187c478bd9Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t)); 11197c478bd9Sstevel@tonic-gate } 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate /* 11227c478bd9Sstevel@tonic-gate * At this time pages should be in memory, so just lock them. 11237c478bd9Sstevel@tonic-gate */ 11247c478bd9Sstevel@tonic-gate static void 1125c6939658Ssl108498 lock_again(size_t npages, kshmid_t *sp, struct anon_map *amp) 11267c478bd9Sstevel@tonic-gate { 11277c478bd9Sstevel@tonic-gate struct anon *ap; 11287c478bd9Sstevel@tonic-gate struct page *pp; 11297c478bd9Sstevel@tonic-gate struct vnode *vp; 1130c6939658Ssl108498 u_offset_t off; 11317c478bd9Sstevel@tonic-gate ulong_t anon_idx; 11327c478bd9Sstevel@tonic-gate anon_sync_obj_t cookie; 11337c478bd9Sstevel@tonic-gate 1134c6939658Ssl108498 mutex_enter(&sp->shm_mlock); 11357c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 11367c478bd9Sstevel@tonic-gate for (anon_idx = 0; npages != 0; anon_idx++, npages--) { 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate anon_array_enter(amp, anon_idx, &cookie); 11397c478bd9Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, anon_idx); 1140c6939658Ssl108498 ASSERT(ap != NULL); 11417c478bd9Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 11427c478bd9Sstevel@tonic-gate anon_array_exit(&cookie); 11437c478bd9Sstevel@tonic-gate 1144c6939658Ssl108498 pp = page_lookup(vp, off, SE_SHARED); 11457c478bd9Sstevel@tonic-gate if (pp == NULL) { 11467c478bd9Sstevel@tonic-gate panic("lock_again: page not in the system"); 11477c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 11487c478bd9Sstevel@tonic-gate } 1149c6939658Ssl108498 /* page should already be locked by caller */ 1150c6939658Ssl108498 ASSERT(pp->p_lckcnt > 0); 11517c478bd9Sstevel@tonic-gate (void) page_pp_lock(pp, 0, 0); 11527c478bd9Sstevel@tonic-gate page_unlock(pp); 11537c478bd9Sstevel@tonic-gate } 11547c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 1155c6939658Ssl108498 mutex_exit(&sp->shm_mlock); 11567c478bd9Sstevel@tonic-gate } 11577c478bd9Sstevel@tonic-gate 11587c478bd9Sstevel@tonic-gate /* 11597c478bd9Sstevel@tonic-gate * Attach the shared memory segment to the process 11607c478bd9Sstevel@tonic-gate * address space and lock the pages. 11617c478bd9Sstevel@tonic-gate */ 11627c478bd9Sstevel@tonic-gate static int 1163c6939658Ssl108498 shmem_lock(kshmid_t *sp, struct anon_map *amp) 11647c478bd9Sstevel@tonic-gate { 11657c478bd9Sstevel@tonic-gate size_t npages = btopr(amp->size); 11667c478bd9Sstevel@tonic-gate struct as *as; 11677c478bd9Sstevel@tonic-gate struct segvn_crargs crargs; 1168c6939658Ssl108498 uint_t error; 11697c478bd9Sstevel@tonic-gate 1170c6939658Ssl108498 /* 1171c6939658Ssl108498 * A later ISM/DISM attach may increase the size of the amp, so 1172c6939658Ssl108498 * cache the number of pages locked for the future shmem_unlock() 1173c6939658Ssl108498 */ 1174c6939658Ssl108498 sp->shm_lkpages = npages; 11757c478bd9Sstevel@tonic-gate 1176c6939658Ssl108498 as = as_alloc(); 11777c478bd9Sstevel@tonic-gate /* Initialize the create arguments and map the segment */ 11787c478bd9Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp; /* structure copy */ 11797c478bd9Sstevel@tonic-gate crargs.offset = (u_offset_t)0; 11807c478bd9Sstevel@tonic-gate crargs.type = MAP_SHARED; 11817c478bd9Sstevel@tonic-gate crargs.amp = amp; 11827c478bd9Sstevel@tonic-gate crargs.prot = PROT_ALL; 11837c478bd9Sstevel@tonic-gate crargs.maxprot = crargs.prot; 11847c478bd9Sstevel@tonic-gate crargs.flags = 0; 1185c6939658Ssl108498 error = as_map(as, 0x0, amp->size, segvn_create, &crargs); 11867c478bd9Sstevel@tonic-gate if (!error) { 1187c6939658Ssl108498 if ((error = as_ctl(as, 0x0, amp->size, MC_LOCK, 0, 0, 11887c478bd9Sstevel@tonic-gate NULL, 0)) == 0) { 1189c6939658Ssl108498 lock_again(npages, sp, amp); 11907c478bd9Sstevel@tonic-gate } 1191c6939658Ssl108498 (void) as_unmap(as, 0x0, amp->size); 11927c478bd9Sstevel@tonic-gate } 1193c6939658Ssl108498 as_free(as); 11947c478bd9Sstevel@tonic-gate return (error); 11957c478bd9Sstevel@tonic-gate } 11967c478bd9Sstevel@tonic-gate 11977c478bd9Sstevel@tonic-gate 11987c478bd9Sstevel@tonic-gate /* 11997c478bd9Sstevel@tonic-gate * Unlock shared memory 12007c478bd9Sstevel@tonic-gate */ 12017c478bd9Sstevel@tonic-gate static void 1202c6939658Ssl108498 shmem_unlock(kshmid_t *sp, struct anon_map *amp) 12037c478bd9Sstevel@tonic-gate { 12047c478bd9Sstevel@tonic-gate struct anon *ap; 1205c6939658Ssl108498 pgcnt_t npages = sp->shm_lkpages; 12067c478bd9Sstevel@tonic-gate struct vnode *vp; 12077c478bd9Sstevel@tonic-gate struct page *pp; 1208c6939658Ssl108498 u_offset_t off; 12097c478bd9Sstevel@tonic-gate ulong_t anon_idx; 1210c6939658Ssl108498 size_t unlocked_bytes = 0; 1211c6939658Ssl108498 kproject_t *proj; 1212c6939658Ssl108498 anon_sync_obj_t cookie; 12137c478bd9Sstevel@tonic-gate 1214c6939658Ssl108498 proj = sp->shm_perm.ipc_proj; 1215c6939658Ssl108498 mutex_enter(&sp->shm_mlock); 1216c6939658Ssl108498 ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 12177c478bd9Sstevel@tonic-gate for (anon_idx = 0; anon_idx < npages; anon_idx++) { 12187c478bd9Sstevel@tonic-gate 1219c6939658Ssl108498 anon_array_enter(amp, anon_idx, &cookie); 12207c478bd9Sstevel@tonic-gate if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) { 12217c478bd9Sstevel@tonic-gate panic("shmem_unlock: null app"); 12227c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 12237c478bd9Sstevel@tonic-gate } 12247c478bd9Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 1225c6939658Ssl108498 anon_array_exit(&cookie); 12267c478bd9Sstevel@tonic-gate pp = page_lookup(vp, off, SE_SHARED); 12277c478bd9Sstevel@tonic-gate if (pp == NULL) { 12287c478bd9Sstevel@tonic-gate panic("shmem_unlock: page not in the system"); 12297c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 12307c478bd9Sstevel@tonic-gate } 1231c6939658Ssl108498 /* 1232c6939658Ssl108498 * Page should at least have once lock from previous 1233c6939658Ssl108498 * shmem_lock 1234c6939658Ssl108498 */ 1235c6939658Ssl108498 ASSERT(pp->p_lckcnt > 0); 12367c478bd9Sstevel@tonic-gate page_pp_unlock(pp, 0, 0); 1237c6939658Ssl108498 if (pp->p_lckcnt == 0) 1238c6939658Ssl108498 unlocked_bytes += PAGESIZE; 1239c6939658Ssl108498 12407c478bd9Sstevel@tonic-gate page_unlock(pp); 12417c478bd9Sstevel@tonic-gate } 1242c6939658Ssl108498 1243c6939658Ssl108498 if (unlocked_bytes > 0) { 1244c6939658Ssl108498 rctl_decr_locked_mem(NULL, proj, unlocked_bytes, 0); 1245c6939658Ssl108498 } 1246c6939658Ssl108498 1247c6939658Ssl108498 ANON_LOCK_EXIT(&->a_rwlock); 1248c6939658Ssl108498 mutex_exit(&sp->shm_mlock); 12497c478bd9Sstevel@tonic-gate } 12507c478bd9Sstevel@tonic-gate 12517c478bd9Sstevel@tonic-gate /* 12527c478bd9Sstevel@tonic-gate * We call this routine when we have removed all references to this 12537c478bd9Sstevel@tonic-gate * amp. This means all shmdt()s and the IPC_RMID have been done. 12547c478bd9Sstevel@tonic-gate */ 12557c478bd9Sstevel@tonic-gate static void 125668803f2dSsl108498 shm_rm_amp(kshmid_t *sp) 12577c478bd9Sstevel@tonic-gate { 125868803f2dSsl108498 struct anon_map *amp = sp->shm_amp; 125968803f2dSsl108498 zone_t *zone; 126068803f2dSsl108498 126167253d2cSsl108498 zone = sp->shm_perm.ipc_zone; 126267253d2cSsl108498 ASSERT(zone != NULL); 12637c478bd9Sstevel@tonic-gate /* 12647c478bd9Sstevel@tonic-gate * Free up the anon_map. 12657c478bd9Sstevel@tonic-gate */ 12667c478bd9Sstevel@tonic-gate lgrp_shm_policy_fini(amp, NULL); 126707b65a64Saguzovsk ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER); 1268a98e9dbfSaguzovsk if (amp->a_szc != 0) { 126907b65a64Saguzovsk anon_shmap_free_pages(amp, 0, amp->size); 127007b65a64Saguzovsk } else { 12717c478bd9Sstevel@tonic-gate anon_free(amp->ahp, 0, amp->size); 127207b65a64Saguzovsk } 1273a98e9dbfSaguzovsk ANON_LOCK_EXIT(&->a_rwlock); 127468803f2dSsl108498 anon_unresv_zone(amp->swresv, zone); 12757c478bd9Sstevel@tonic-gate anonmap_free(amp); 12767c478bd9Sstevel@tonic-gate } 12777c478bd9Sstevel@tonic-gate 12787c478bd9Sstevel@tonic-gate /* 12797c478bd9Sstevel@tonic-gate * Return the shared memory id for the process's virtual address. 12807c478bd9Sstevel@tonic-gate * Return SHMID_NONE if addr is not within a SysV shared memory segment. 12817c478bd9Sstevel@tonic-gate * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed. 12827c478bd9Sstevel@tonic-gate * 12837c478bd9Sstevel@tonic-gate * shmgetid() is called from code in /proc with the process locked but 12847c478bd9Sstevel@tonic-gate * with pp->p_lock not held. The address space lock is held, so we 12857c478bd9Sstevel@tonic-gate * cannot grab pp->p_lock here due to lock-ordering constraints. 12867c478bd9Sstevel@tonic-gate * Because of all this, modifications to the p_segacct list must only 12877c478bd9Sstevel@tonic-gate * be made after calling prbarrier() to ensure the process is not locked. 12887c478bd9Sstevel@tonic-gate * See shmdt() and sa_add(), above. shmgetid() may also be called on a 12897c478bd9Sstevel@tonic-gate * thread's own process without the process locked. 12907c478bd9Sstevel@tonic-gate */ 12917c478bd9Sstevel@tonic-gate int 12927c478bd9Sstevel@tonic-gate shmgetid(proc_t *pp, caddr_t addr) 12937c478bd9Sstevel@tonic-gate { 12947c478bd9Sstevel@tonic-gate segacct_t *sap, template; 12957c478bd9Sstevel@tonic-gate 12967c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&pp->p_lock)); 12977c478bd9Sstevel@tonic-gate ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc); 12987c478bd9Sstevel@tonic-gate 12997c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) 13007c478bd9Sstevel@tonic-gate return (SHMID_NONE); 13017c478bd9Sstevel@tonic-gate 13027c478bd9Sstevel@tonic-gate template.sa_addr = addr; 13037c478bd9Sstevel@tonic-gate template.sa_len = 0; 13047c478bd9Sstevel@tonic-gate if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL) 13057c478bd9Sstevel@tonic-gate return (SHMID_NONE); 13067c478bd9Sstevel@tonic-gate 13077c478bd9Sstevel@tonic-gate if (IPC_FREE(&sap->sa_id->shm_perm)) 13087c478bd9Sstevel@tonic-gate return (SHMID_FREE); 13097c478bd9Sstevel@tonic-gate 13107c478bd9Sstevel@tonic-gate return (sap->sa_id->shm_perm.ipc_id); 13117c478bd9Sstevel@tonic-gate } 1312