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 5*07b65a64Saguzovsk * Common Development and Distribution License (the "License"). 6*07b65a64Saguzovsk * 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 */ 217c478bd9Sstevel@tonic-gate /* 22*07b65a64Saguzovsk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 317c478bd9Sstevel@tonic-gate * The Regents of the University of California 327c478bd9Sstevel@tonic-gate * All Rights Reserved 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 357c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 367c478bd9Sstevel@tonic-gate * contributors. 377c478bd9Sstevel@tonic-gate */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * Inter-Process Communication Shared Memory Facility. 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * See os/ipc.c for a description of common IPC functionality. 457c478bd9Sstevel@tonic-gate * 467c478bd9Sstevel@tonic-gate * Resource controls 477c478bd9Sstevel@tonic-gate * ----------------- 487c478bd9Sstevel@tonic-gate * 497c478bd9Sstevel@tonic-gate * Control: project.max-shm-ids (rc_project_shmmni) 507c478bd9Sstevel@tonic-gate * Description: Maximum number of shared memory ids allowed a project. 517c478bd9Sstevel@tonic-gate * 527c478bd9Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, one id 537c478bd9Sstevel@tonic-gate * is allocated. If the id allocation doesn't succeed, shmget() 547c478bd9Sstevel@tonic-gate * fails and errno is set to ENOSPC. Upon successful shmctl(, 557c478bd9Sstevel@tonic-gate * IPC_RMID) the id is deallocated. 567c478bd9Sstevel@tonic-gate * 577c478bd9Sstevel@tonic-gate * Control: project.max-shm-memory (rc_project_shmmax) 587c478bd9Sstevel@tonic-gate * Description: Total amount of shared memory allowed a project. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * When shmget() is used to allocate a shared memory segment, the 617c478bd9Sstevel@tonic-gate * segment's size is allocated against this limit. If the space 627c478bd9Sstevel@tonic-gate * allocation doesn't succeed, shmget() fails and errno is set to 637c478bd9Sstevel@tonic-gate * EINVAL. The size will be deallocated once the last process has 647c478bd9Sstevel@tonic-gate * detached the segment and the segment has been successfully 657c478bd9Sstevel@tonic-gate * shmctl(, IPC_RMID)ed. 667c478bd9Sstevel@tonic-gate */ 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate #include <sys/types.h> 697c478bd9Sstevel@tonic-gate #include <sys/param.h> 707c478bd9Sstevel@tonic-gate #include <sys/cred.h> 717c478bd9Sstevel@tonic-gate #include <sys/errno.h> 727c478bd9Sstevel@tonic-gate #include <sys/time.h> 737c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 747c478bd9Sstevel@tonic-gate #include <sys/user.h> 757c478bd9Sstevel@tonic-gate #include <sys/proc.h> 767c478bd9Sstevel@tonic-gate #include <sys/systm.h> 777c478bd9Sstevel@tonic-gate #include <sys/prsystm.h> 787c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 797c478bd9Sstevel@tonic-gate #include <sys/tuneable.h> 807c478bd9Sstevel@tonic-gate #include <sys/vm.h> 817c478bd9Sstevel@tonic-gate #include <sys/mman.h> 827c478bd9Sstevel@tonic-gate #include <sys/swap.h> 837c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 847c478bd9Sstevel@tonic-gate #include <sys/debug.h> 857c478bd9Sstevel@tonic-gate #include <sys/lwpchan_impl.h> 867c478bd9Sstevel@tonic-gate #include <sys/avl.h> 877c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 887c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 897c478bd9Sstevel@tonic-gate #include <sys/task.h> 907c478bd9Sstevel@tonic-gate #include <sys/project.h> 917c478bd9Sstevel@tonic-gate #include <sys/policy.h> 927c478bd9Sstevel@tonic-gate #include <sys/zone.h> 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate #include <sys/ipc.h> 957c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h> 967c478bd9Sstevel@tonic-gate #include <sys/shm.h> 977c478bd9Sstevel@tonic-gate #include <sys/shm_impl.h> 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate #include <vm/hat.h> 1007c478bd9Sstevel@tonic-gate #include <vm/seg.h> 1017c478bd9Sstevel@tonic-gate #include <vm/as.h> 1027c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h> 1037c478bd9Sstevel@tonic-gate #include <vm/anon.h> 1047c478bd9Sstevel@tonic-gate #include <vm/page.h> 1057c478bd9Sstevel@tonic-gate #include <vm/vpage.h> 1067c478bd9Sstevel@tonic-gate #include <vm/seg_spt.h> 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate #include <c2/audit.h> 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static int shmem_lock(struct anon_map *amp); 1117c478bd9Sstevel@tonic-gate static void shmem_unlock(struct anon_map *amp, uint_t lck); 1127c478bd9Sstevel@tonic-gate static void sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, 1137c478bd9Sstevel@tonic-gate kshmid_t *id); 1147c478bd9Sstevel@tonic-gate static void shm_rm_amp(struct anon_map *amp, uint_t lckflag); 1157c478bd9Sstevel@tonic-gate static void shm_dtor(kipc_perm_t *); 1167c478bd9Sstevel@tonic-gate static void shm_rmid(kipc_perm_t *); 1177c478bd9Sstevel@tonic-gate static void shm_remove_zone(zoneid_t, void *); 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* 1207c478bd9Sstevel@tonic-gate * Semantics for share_page_table and ism_off: 1217c478bd9Sstevel@tonic-gate * 1227c478bd9Sstevel@tonic-gate * These are hooks in /etc/system - only for internal testing purpose. 1237c478bd9Sstevel@tonic-gate * 1247c478bd9Sstevel@tonic-gate * Setting share_page_table automatically turns on the SHM_SHARE_MMU (ISM) flag 1257c478bd9Sstevel@tonic-gate * in a call to shmat(2). In other words, with share_page_table set, you always 1267c478bd9Sstevel@tonic-gate * get ISM, even if say, DISM is specified. It should really be called "ism_on". 1277c478bd9Sstevel@tonic-gate * 1287c478bd9Sstevel@tonic-gate * Setting ism_off turns off the SHM_SHARE_MMU flag from the flags passed to 1297c478bd9Sstevel@tonic-gate * shmat(2). 1307c478bd9Sstevel@tonic-gate * 1317c478bd9Sstevel@tonic-gate * If both share_page_table and ism_off are set, share_page_table prevails. 1327c478bd9Sstevel@tonic-gate * 1337c478bd9Sstevel@tonic-gate * Although these tunables should probably be removed, they do have some 1347c478bd9Sstevel@tonic-gate * external exposure; as long as they exist, they should at least work sensibly. 1357c478bd9Sstevel@tonic-gate */ 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate int share_page_table; 1387c478bd9Sstevel@tonic-gate int ism_off; 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * The following tunables are obsolete. Though for compatibility we 1427c478bd9Sstevel@tonic-gate * still read and interpret shminfo_shmmax and shminfo_shmmni (see 1437c478bd9Sstevel@tonic-gate * os/project.c), the preferred mechanism for administrating the IPC 1447c478bd9Sstevel@tonic-gate * Shared Memory facility is through the resource controls described at 1457c478bd9Sstevel@tonic-gate * the top of this file. 1467c478bd9Sstevel@tonic-gate */ 1477c478bd9Sstevel@tonic-gate size_t shminfo_shmmax = 0x800000; /* (obsolete) */ 1487c478bd9Sstevel@tonic-gate int shminfo_shmmni = 100; /* (obsolete) */ 1497c478bd9Sstevel@tonic-gate size_t shminfo_shmmin = 1; /* (obsolete) */ 1507c478bd9Sstevel@tonic-gate int shminfo_shmseg = 6; /* (obsolete) */ 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmax; 1537c478bd9Sstevel@tonic-gate extern rctl_hndl_t rc_project_shmmni; 1547c478bd9Sstevel@tonic-gate static ipc_service_t *shm_svc; 1557c478bd9Sstevel@tonic-gate static zone_key_t shm_zone_key; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * Module linkage information for the kernel. 1597c478bd9Sstevel@tonic-gate */ 1607c478bd9Sstevel@tonic-gate static uintptr_t shmsys(int, uintptr_t, uintptr_t, uintptr_t); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent = { 1637c478bd9Sstevel@tonic-gate 4, 1647c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1657c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_64RVAL, 1667c478bd9Sstevel@tonic-gate #else /* _SYSCALL32_IMPL */ 1677c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 1687c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 1697c478bd9Sstevel@tonic-gate (int (*)())shmsys 1707c478bd9Sstevel@tonic-gate }; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1737c478bd9Sstevel@tonic-gate static struct sysent ipcshm_sysent32 = { 1747c478bd9Sstevel@tonic-gate 4, 1757c478bd9Sstevel@tonic-gate SE_ARGC | SE_NOUNLOAD | SE_32RVAL1, 1767c478bd9Sstevel@tonic-gate (int (*)())shmsys 1777c478bd9Sstevel@tonic-gate }; 1787c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate static struct modlsys modlsys = { 1817c478bd9Sstevel@tonic-gate &mod_syscallops, "System V shared memory", &ipcshm_sysent 1827c478bd9Sstevel@tonic-gate }; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1857c478bd9Sstevel@tonic-gate static struct modlsys modlsys32 = { 1867c478bd9Sstevel@tonic-gate &mod_syscallops32, "32-bit System V shared memory", &ipcshm_sysent32 1877c478bd9Sstevel@tonic-gate }; 1887c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */ 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = { 1917c478bd9Sstevel@tonic-gate MODREV_1, 1927c478bd9Sstevel@tonic-gate &modlsys, 1937c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL 1947c478bd9Sstevel@tonic-gate &modlsys32, 1957c478bd9Sstevel@tonic-gate #endif 1967c478bd9Sstevel@tonic-gate NULL 1977c478bd9Sstevel@tonic-gate }; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate int 2017c478bd9Sstevel@tonic-gate _init(void) 2027c478bd9Sstevel@tonic-gate { 2037c478bd9Sstevel@tonic-gate int result; 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate shm_svc = ipcs_create("shmids", rc_project_shmmni, sizeof (kshmid_t), 2067c478bd9Sstevel@tonic-gate shm_dtor, shm_rmid, AT_IPC_SHM, 2077c478bd9Sstevel@tonic-gate offsetof(kproject_data_t, kpd_shmmni)); 2087c478bd9Sstevel@tonic-gate zone_key_create(&shm_zone_key, NULL, shm_remove_zone, NULL); 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate if ((result = mod_install(&modlinkage)) == 0) 2117c478bd9Sstevel@tonic-gate return (0); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate (void) zone_key_delete(shm_zone_key); 2147c478bd9Sstevel@tonic-gate ipcs_destroy(shm_svc); 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate return (result); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate int 2207c478bd9Sstevel@tonic-gate _fini(void) 2217c478bd9Sstevel@tonic-gate { 2227c478bd9Sstevel@tonic-gate return (EBUSY); 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate int 2267c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 2277c478bd9Sstevel@tonic-gate { 2287c478bd9Sstevel@tonic-gate return (mod_info(&modlinkage, modinfop)); 2297c478bd9Sstevel@tonic-gate } 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate /* 2327c478bd9Sstevel@tonic-gate * Shmat (attach shared segment) system call. 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate static int 2357c478bd9Sstevel@tonic-gate shmat(int shmid, caddr_t uaddr, int uflags, uintptr_t *rvp) 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */ 2387c478bd9Sstevel@tonic-gate size_t size; 2397c478bd9Sstevel@tonic-gate int error = 0; 2407c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 2417c478bd9Sstevel@tonic-gate struct as *as = pp->p_as; 2427c478bd9Sstevel@tonic-gate struct segvn_crargs crargs; /* segvn create arguments */ 2437c478bd9Sstevel@tonic-gate kmutex_t *lock; 2447c478bd9Sstevel@tonic-gate struct seg *segspt = NULL; 2457c478bd9Sstevel@tonic-gate caddr_t addr = uaddr; 2467c478bd9Sstevel@tonic-gate int flags = (uflags & SHMAT_VALID_FLAGS_MASK); 2477c478bd9Sstevel@tonic-gate int useISM; 2487c478bd9Sstevel@tonic-gate uchar_t prot = PROT_ALL; 2497c478bd9Sstevel@tonic-gate int result; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL) 2527c478bd9Sstevel@tonic-gate return (EINVAL); 2537c478bd9Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, CRED())) 2547c478bd9Sstevel@tonic-gate goto errret; 2557c478bd9Sstevel@tonic-gate if ((flags & SHM_RDONLY) == 0 && 2567c478bd9Sstevel@tonic-gate (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED()))) 2577c478bd9Sstevel@tonic-gate goto errret; 2587c478bd9Sstevel@tonic-gate if (spt_invalid(flags)) { 2597c478bd9Sstevel@tonic-gate error = EINVAL; 2607c478bd9Sstevel@tonic-gate goto errret; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate if (ism_off) 2637c478bd9Sstevel@tonic-gate flags = flags & ~SHM_SHARE_MMU; 2647c478bd9Sstevel@tonic-gate if (share_page_table) { 2657c478bd9Sstevel@tonic-gate flags = flags & ~SHM_PAGEABLE; 2667c478bd9Sstevel@tonic-gate flags = flags | SHM_SHARE_MMU; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate useISM = (spt_locked(flags) || spt_pageable(flags)); 2697c478bd9Sstevel@tonic-gate if (useISM && (error = ipcperm_access(&sp->shm_perm, SHM_W, CRED()))) 2707c478bd9Sstevel@tonic-gate goto errret; 2717c478bd9Sstevel@tonic-gate if (useISM && isspt(sp)) { 2727c478bd9Sstevel@tonic-gate uint_t newsptflags = flags | spt_flags(sp->shm_sptseg); 2737c478bd9Sstevel@tonic-gate /* 2747c478bd9Sstevel@tonic-gate * If trying to change an existing {D}ISM segment from ISM 2757c478bd9Sstevel@tonic-gate * to DISM or vice versa, return error. Note that this 2767c478bd9Sstevel@tonic-gate * validation of flags needs to be done after the effect of 2777c478bd9Sstevel@tonic-gate * tunables such as ism_off and share_page_table, for 2787c478bd9Sstevel@tonic-gate * semantics that are consistent with the tunables' settings. 2797c478bd9Sstevel@tonic-gate */ 2807c478bd9Sstevel@tonic-gate if (spt_invalid(newsptflags)) { 2817c478bd9Sstevel@tonic-gate error = EINVAL; 2827c478bd9Sstevel@tonic-gate goto errret; 2837c478bd9Sstevel@tonic-gate } 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 2867c478bd9Sstevel@tonic-gate size = sp->shm_amp->size; 2877c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate /* somewhere to record spt info for final detach */ 2907c478bd9Sstevel@tonic-gate if (sp->shm_sptinfo == NULL) 2917c478bd9Sstevel@tonic-gate sp->shm_sptinfo = kmem_zalloc(sizeof (sptinfo_t), KM_SLEEP); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate as_rangelock(as); 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if (useISM) { 2967c478bd9Sstevel@tonic-gate /* 2977c478bd9Sstevel@tonic-gate * Handle ISM 2987c478bd9Sstevel@tonic-gate */ 2997c478bd9Sstevel@tonic-gate uint_t n, share_szc; 3007c478bd9Sstevel@tonic-gate size_t share_size; 3017c478bd9Sstevel@tonic-gate struct shm_data ssd; 3027c478bd9Sstevel@tonic-gate uintptr_t align_hint; 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate n = page_num_pagesizes(); 3057c478bd9Sstevel@tonic-gate if (n < 2) { /* large pages aren't supported */ 3067c478bd9Sstevel@tonic-gate as_rangeunlock(as); 3077c478bd9Sstevel@tonic-gate error = EINVAL; 3087c478bd9Sstevel@tonic-gate goto errret; 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate 3117c478bd9Sstevel@tonic-gate /* 3127c478bd9Sstevel@tonic-gate * Pick a share pagesize to use, if (!isspt(sp)). 3137c478bd9Sstevel@tonic-gate * Otherwise use the already chosen page size. 3147c478bd9Sstevel@tonic-gate * 3157c478bd9Sstevel@tonic-gate * For the initial shmat (!isspt(sp)), where sptcreate is 3167c478bd9Sstevel@tonic-gate * called, map_pgsz is called to recommend a [D]ISM pagesize, 3177c478bd9Sstevel@tonic-gate * important for systems which offer more than one potential 3187c478bd9Sstevel@tonic-gate * [D]ISM pagesize. 3197c478bd9Sstevel@tonic-gate * If the shmat is just to attach to an already created 3207c478bd9Sstevel@tonic-gate * [D]ISM segment, then use the previously selected page size. 3217c478bd9Sstevel@tonic-gate */ 3227c478bd9Sstevel@tonic-gate if (!isspt(sp)) { 3237c478bd9Sstevel@tonic-gate share_size = map_pgsz(MAPPGSZ_ISM, 3247c478bd9Sstevel@tonic-gate pp, addr, size, NULL); 3257c478bd9Sstevel@tonic-gate if (share_size == 0) { 3267c478bd9Sstevel@tonic-gate as_rangeunlock(as); 3277c478bd9Sstevel@tonic-gate error = EINVAL; 3287c478bd9Sstevel@tonic-gate goto errret; 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate share_szc = page_szc(share_size); 3317c478bd9Sstevel@tonic-gate } else { 3327c478bd9Sstevel@tonic-gate share_szc = sp->shm_sptseg->s_szc; 3337c478bd9Sstevel@tonic-gate share_size = page_get_pagesize(share_szc); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate size = P2ROUNDUP(size, share_size); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate align_hint = share_size; 3387c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 3397c478bd9Sstevel@tonic-gate /* 3407c478bd9Sstevel@tonic-gate * For 64 bit amd64, we want to share an entire page table 3417c478bd9Sstevel@tonic-gate * if possible. We know (ugh) that there are 512 entries in 3427c478bd9Sstevel@tonic-gate * in a page table. The number for 32 bit non-PAE should be 3437c478bd9Sstevel@tonic-gate * 1024, but I'm not going to special case that. Note using 512 3447c478bd9Sstevel@tonic-gate * won't cause a failure below. It retries with align_hint set 3457c478bd9Sstevel@tonic-gate * to share_size 3467c478bd9Sstevel@tonic-gate */ 3477c478bd9Sstevel@tonic-gate while (size >= 512 * (uint64_t)align_hint) 3487c478bd9Sstevel@tonic-gate align_hint *= 512; 3497c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate #if defined(__sparcv9) 3527c478bd9Sstevel@tonic-gate if (addr == 0 && curproc->p_model == DATAMODEL_LP64) { 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * If no address has been passed in, and this is a 3557c478bd9Sstevel@tonic-gate * 64-bit process, we'll try to find an address 3567c478bd9Sstevel@tonic-gate * in the predict-ISM zone. 3577c478bd9Sstevel@tonic-gate */ 3587c478bd9Sstevel@tonic-gate caddr_t predbase = (caddr_t)PREDISM_1T_BASE; 3597c478bd9Sstevel@tonic-gate size_t len = PREDISM_BOUND - PREDISM_1T_BASE; 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate as_purge(as); 3627c478bd9Sstevel@tonic-gate if (as_gap(as, size + share_size, &predbase, &len, 3637c478bd9Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != -1) { 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * We found an address which looks like a 3667c478bd9Sstevel@tonic-gate * candidate. We want to round it up, and 3677c478bd9Sstevel@tonic-gate * then check that it's a valid user range. 3687c478bd9Sstevel@tonic-gate * This assures that we won't fail below. 3697c478bd9Sstevel@tonic-gate */ 3707c478bd9Sstevel@tonic-gate addr = (caddr_t)P2ROUNDUP((uintptr_t)predbase, 3717c478bd9Sstevel@tonic-gate share_size); 3727c478bd9Sstevel@tonic-gate 3737c478bd9Sstevel@tonic-gate if (valid_usr_range(addr, size, prot, 3747c478bd9Sstevel@tonic-gate as, as->a_userlimit) != RANGE_OKAY) { 3757c478bd9Sstevel@tonic-gate addr = 0; 3767c478bd9Sstevel@tonic-gate } 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate #endif /* __sparcv9 */ 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate if (addr == 0) { 3827c478bd9Sstevel@tonic-gate for (;;) { 3837c478bd9Sstevel@tonic-gate addr = (caddr_t)align_hint; 3847c478bd9Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, MAP_ALIGN); 3857c478bd9Sstevel@tonic-gate if (addr != NULL || align_hint == share_size) 3867c478bd9Sstevel@tonic-gate break; 3877c478bd9Sstevel@tonic-gate align_hint = share_size; 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate if (addr == NULL) { 3907c478bd9Sstevel@tonic-gate as_rangeunlock(as); 3917c478bd9Sstevel@tonic-gate error = ENOMEM; 3927c478bd9Sstevel@tonic-gate goto errret; 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate ASSERT(((uintptr_t)addr & (align_hint - 1)) == 0); 3957c478bd9Sstevel@tonic-gate } else { 3967c478bd9Sstevel@tonic-gate /* Use the user-supplied attach address */ 3977c478bd9Sstevel@tonic-gate caddr_t base; 3987c478bd9Sstevel@tonic-gate size_t len; 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate /* 4017c478bd9Sstevel@tonic-gate * Check that the address range 4027c478bd9Sstevel@tonic-gate * 1) is properly aligned 4037c478bd9Sstevel@tonic-gate * 2) is correct in unix terms 4047c478bd9Sstevel@tonic-gate * 3) is within an unmapped address segment 4057c478bd9Sstevel@tonic-gate */ 4067c478bd9Sstevel@tonic-gate base = addr; 4077c478bd9Sstevel@tonic-gate len = size; /* use spt aligned size */ 4087c478bd9Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */ 4097c478bd9Sstevel@tonic-gate if ((uintptr_t)base & (share_size - 1)) { 4107c478bd9Sstevel@tonic-gate error = EINVAL; 4117c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4127c478bd9Sstevel@tonic-gate goto errret; 4137c478bd9Sstevel@tonic-gate } 4147c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 4157c478bd9Sstevel@tonic-gate as->a_userlimit); 4167c478bd9Sstevel@tonic-gate if (result == RANGE_BADPROT) { 4177c478bd9Sstevel@tonic-gate /* 4187c478bd9Sstevel@tonic-gate * We try to accomodate processors which 4197c478bd9Sstevel@tonic-gate * may not support execute permissions on 4207c478bd9Sstevel@tonic-gate * all ISM segments by trying the check 4217c478bd9Sstevel@tonic-gate * again but without PROT_EXEC. 4227c478bd9Sstevel@tonic-gate */ 4237c478bd9Sstevel@tonic-gate prot &= ~PROT_EXEC; 4247c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 4257c478bd9Sstevel@tonic-gate as->a_userlimit); 4267c478bd9Sstevel@tonic-gate } 4277c478bd9Sstevel@tonic-gate as_purge(as); 4287c478bd9Sstevel@tonic-gate if (result != RANGE_OKAY || 4297c478bd9Sstevel@tonic-gate as_gap(as, len, &base, &len, AH_LO, 4307c478bd9Sstevel@tonic-gate (caddr_t)NULL) != 0) { 4317c478bd9Sstevel@tonic-gate error = EINVAL; 4327c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4337c478bd9Sstevel@tonic-gate goto errret; 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate if (!isspt(sp)) { 4387c478bd9Sstevel@tonic-gate error = sptcreate(size, &segspt, sp->shm_amp, prot, 4397c478bd9Sstevel@tonic-gate flags, share_szc); 4407c478bd9Sstevel@tonic-gate if (error) { 4417c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4427c478bd9Sstevel@tonic-gate goto errret; 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate sp->shm_sptinfo->sptas = segspt->s_as; 4457c478bd9Sstevel@tonic-gate sp->shm_sptseg = segspt; 4467c478bd9Sstevel@tonic-gate sp->shm_sptprot = prot; 4477c478bd9Sstevel@tonic-gate sp->shm_lkcnt = 0; 4487c478bd9Sstevel@tonic-gate } else if ((prot & sp->shm_sptprot) != sp->shm_sptprot) { 4497c478bd9Sstevel@tonic-gate /* 4507c478bd9Sstevel@tonic-gate * Ensure we're attaching to an ISM segment with 4517c478bd9Sstevel@tonic-gate * fewer or equal permissions than what we're 4527c478bd9Sstevel@tonic-gate * allowed. Fail if the segment has more 4537c478bd9Sstevel@tonic-gate * permissions than what we're allowed. 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate error = EACCES; 4567c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4577c478bd9Sstevel@tonic-gate goto errret; 4587c478bd9Sstevel@tonic-gate } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate ssd.shm_sptseg = sp->shm_sptseg; 4617c478bd9Sstevel@tonic-gate ssd.shm_sptas = sp->shm_sptinfo->sptas; 4627c478bd9Sstevel@tonic-gate ssd.shm_amp = sp->shm_amp; 4637c478bd9Sstevel@tonic-gate error = as_map(as, addr, size, segspt_shmattach, &ssd); 4647c478bd9Sstevel@tonic-gate if (error == 0) 4657c478bd9Sstevel@tonic-gate sp->shm_ismattch++; /* keep count of ISM attaches */ 4667c478bd9Sstevel@tonic-gate } else { 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate /* 4697c478bd9Sstevel@tonic-gate * Normal case. 4707c478bd9Sstevel@tonic-gate */ 4717c478bd9Sstevel@tonic-gate if (flags & SHM_RDONLY) 4727c478bd9Sstevel@tonic-gate prot &= ~PROT_WRITE; 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate if (addr == 0) { 4757c478bd9Sstevel@tonic-gate /* Let the system pick the attach address */ 4767c478bd9Sstevel@tonic-gate map_addr(&addr, size, 0ll, 1, 0); 4777c478bd9Sstevel@tonic-gate if (addr == NULL) { 4787c478bd9Sstevel@tonic-gate as_rangeunlock(as); 4797c478bd9Sstevel@tonic-gate error = ENOMEM; 4807c478bd9Sstevel@tonic-gate goto errret; 4817c478bd9Sstevel@tonic-gate } 4827c478bd9Sstevel@tonic-gate } else { 4837c478bd9Sstevel@tonic-gate /* Use the user-supplied attach address */ 4847c478bd9Sstevel@tonic-gate caddr_t base; 4857c478bd9Sstevel@tonic-gate size_t len; 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate if (flags & SHM_RND) 4887c478bd9Sstevel@tonic-gate addr = (caddr_t)((uintptr_t)addr & 4897c478bd9Sstevel@tonic-gate ~(SHMLBA - 1)); 4907c478bd9Sstevel@tonic-gate /* 4917c478bd9Sstevel@tonic-gate * Check that the address range 4927c478bd9Sstevel@tonic-gate * 1) is properly aligned 4937c478bd9Sstevel@tonic-gate * 2) is correct in unix terms 4947c478bd9Sstevel@tonic-gate * 3) is within an unmapped address segment 4957c478bd9Sstevel@tonic-gate */ 4967c478bd9Sstevel@tonic-gate base = addr; 4977c478bd9Sstevel@tonic-gate len = size; /* use aligned size */ 4987c478bd9Sstevel@tonic-gate /* XXX - in SunOS, is sp->shm_segsz */ 4997c478bd9Sstevel@tonic-gate if ((uintptr_t)base & PAGEOFFSET) { 5007c478bd9Sstevel@tonic-gate error = EINVAL; 5017c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5027c478bd9Sstevel@tonic-gate goto errret; 5037c478bd9Sstevel@tonic-gate } 5047c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 5057c478bd9Sstevel@tonic-gate as->a_userlimit); 5067c478bd9Sstevel@tonic-gate if (result == RANGE_BADPROT) { 5077c478bd9Sstevel@tonic-gate prot &= ~PROT_EXEC; 5087c478bd9Sstevel@tonic-gate result = valid_usr_range(base, len, prot, as, 5097c478bd9Sstevel@tonic-gate as->a_userlimit); 5107c478bd9Sstevel@tonic-gate } 5117c478bd9Sstevel@tonic-gate as_purge(as); 5127c478bd9Sstevel@tonic-gate if (result != RANGE_OKAY || 5137c478bd9Sstevel@tonic-gate as_gap(as, len, &base, &len, 5147c478bd9Sstevel@tonic-gate AH_LO, (caddr_t)NULL) != 0) { 5157c478bd9Sstevel@tonic-gate error = EINVAL; 5167c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5177c478bd9Sstevel@tonic-gate goto errret; 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate /* Initialize the create arguments and map the segment */ 5227c478bd9Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp; 5237c478bd9Sstevel@tonic-gate crargs.offset = 0; 5247c478bd9Sstevel@tonic-gate crargs.type = MAP_SHARED; 5257c478bd9Sstevel@tonic-gate crargs.amp = sp->shm_amp; 5267c478bd9Sstevel@tonic-gate crargs.prot = prot; 5277c478bd9Sstevel@tonic-gate crargs.maxprot = crargs.prot; 5287c478bd9Sstevel@tonic-gate crargs.flags = 0; 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate error = as_map(as, addr, size, segvn_create, &crargs); 5317c478bd9Sstevel@tonic-gate } 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate as_rangeunlock(as); 5347c478bd9Sstevel@tonic-gate if (error) 5357c478bd9Sstevel@tonic-gate goto errret; 5367c478bd9Sstevel@tonic-gate 5377c478bd9Sstevel@tonic-gate /* record shmem range for the detach */ 5387c478bd9Sstevel@tonic-gate sa_add(pp, addr, (size_t)size, useISM ? SHMSA_ISM : 0, sp); 5397c478bd9Sstevel@tonic-gate *rvp = (uintptr_t)addr; 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate sp->shm_atime = gethrestime_sec(); 5427c478bd9Sstevel@tonic-gate sp->shm_lpid = pp->p_pid; 5437c478bd9Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp); 5447c478bd9Sstevel@tonic-gate errret: 5457c478bd9Sstevel@tonic-gate mutex_exit(lock); 5467c478bd9Sstevel@tonic-gate return (error); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate static void 5507c478bd9Sstevel@tonic-gate shm_dtor(kipc_perm_t *perm) 5517c478bd9Sstevel@tonic-gate { 5527c478bd9Sstevel@tonic-gate kshmid_t *sp = (kshmid_t *)perm; 5537c478bd9Sstevel@tonic-gate uint_t cnt; 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate if (sp->shm_sptinfo) { 5567c478bd9Sstevel@tonic-gate if (isspt(sp)) 5577c478bd9Sstevel@tonic-gate sptdestroy(sp->shm_sptinfo->sptas, sp->shm_amp); 5587c478bd9Sstevel@tonic-gate kmem_free(sp->shm_sptinfo, sizeof (sptinfo_t)); 5597c478bd9Sstevel@tonic-gate } 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 5627c478bd9Sstevel@tonic-gate cnt = --sp->shm_amp->refcnt; 5637c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 5647c478bd9Sstevel@tonic-gate ASSERT(cnt == 0); 5657c478bd9Sstevel@tonic-gate shm_rm_amp(sp->shm_amp, sp->shm_lkcnt); 5667c478bd9Sstevel@tonic-gate 5677c478bd9Sstevel@tonic-gate if (sp->shm_perm.ipc_id != IPC_ID_INVAL) { 5687c478bd9Sstevel@tonic-gate ipcs_lock(shm_svc); 5697c478bd9Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax -= 5707c478bd9Sstevel@tonic-gate ptob(btopr(sp->shm_segsz)); 5717c478bd9Sstevel@tonic-gate ipcs_unlock(shm_svc); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate } 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate /* ARGSUSED */ 5767c478bd9Sstevel@tonic-gate static void 5777c478bd9Sstevel@tonic-gate shm_rmid(kipc_perm_t *perm) 5787c478bd9Sstevel@tonic-gate { 5797c478bd9Sstevel@tonic-gate /* nothing to do */ 5807c478bd9Sstevel@tonic-gate } 5817c478bd9Sstevel@tonic-gate 5827c478bd9Sstevel@tonic-gate /* 5837c478bd9Sstevel@tonic-gate * Shmctl system call. 5847c478bd9Sstevel@tonic-gate */ 5857c478bd9Sstevel@tonic-gate /* ARGSUSED */ 5867c478bd9Sstevel@tonic-gate static int 5877c478bd9Sstevel@tonic-gate shmctl(int shmid, int cmd, void *arg) 5887c478bd9Sstevel@tonic-gate { 5897c478bd9Sstevel@tonic-gate kshmid_t *sp; /* shared memory header ptr */ 5907c478bd9Sstevel@tonic-gate STRUCT_DECL(shmid_ds, ds); /* for SVR4 IPC_SET */ 5917c478bd9Sstevel@tonic-gate int error = 0; 5927c478bd9Sstevel@tonic-gate struct cred *cr = CRED(); 5937c478bd9Sstevel@tonic-gate kmutex_t *lock; 5947c478bd9Sstevel@tonic-gate model_t mdl = get_udatamodel(); 5957c478bd9Sstevel@tonic-gate struct shmid_ds64 ds64; 5967c478bd9Sstevel@tonic-gate shmatt_t nattch; 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate STRUCT_INIT(ds, mdl); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate /* 6017c478bd9Sstevel@tonic-gate * Perform pre- or non-lookup actions (e.g. copyins, RMID). 6027c478bd9Sstevel@tonic-gate */ 6037c478bd9Sstevel@tonic-gate switch (cmd) { 6047c478bd9Sstevel@tonic-gate case IPC_SET: 6057c478bd9Sstevel@tonic-gate if (copyin(arg, STRUCT_BUF(ds), STRUCT_SIZE(ds))) 6067c478bd9Sstevel@tonic-gate return (EFAULT); 6077c478bd9Sstevel@tonic-gate break; 6087c478bd9Sstevel@tonic-gate 6097c478bd9Sstevel@tonic-gate case IPC_SET64: 6107c478bd9Sstevel@tonic-gate if (copyin(arg, &ds64, sizeof (struct shmid_ds64))) 6117c478bd9Sstevel@tonic-gate return (EFAULT); 6127c478bd9Sstevel@tonic-gate break; 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate case IPC_RMID: 6157c478bd9Sstevel@tonic-gate return (ipc_rmid(shm_svc, shmid, cr)); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate if ((lock = ipc_lookup(shm_svc, shmid, (kipc_perm_t **)&sp)) == NULL) 6197c478bd9Sstevel@tonic-gate return (EINVAL); 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate switch (cmd) { 6227c478bd9Sstevel@tonic-gate /* Set ownership and permissions. */ 6237c478bd9Sstevel@tonic-gate case IPC_SET: 6247c478bd9Sstevel@tonic-gate if (error = ipcperm_set(shm_svc, cr, &sp->shm_perm, 6257c478bd9Sstevel@tonic-gate &STRUCT_BUF(ds)->shm_perm, mdl)) 6267c478bd9Sstevel@tonic-gate break; 6277c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 6287c478bd9Sstevel@tonic-gate break; 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate case IPC_STAT: 6317c478bd9Sstevel@tonic-gate if (error = ipcperm_access(&sp->shm_perm, SHM_R, cr)) 6327c478bd9Sstevel@tonic-gate break; 6337c478bd9Sstevel@tonic-gate 6347c478bd9Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1; 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate ipcperm_stat(&STRUCT_BUF(ds)->shm_perm, &sp->shm_perm, mdl); 6377c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_segsz, sp->shm_segsz); 6387c478bd9Sstevel@tonic-gate STRUCT_FSETP(ds, shm_amp, NULL); /* kernel addr */ 6397c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_lkcnt, sp->shm_lkcnt); 6407c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_lpid, sp->shm_lpid); 6417c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_cpid, sp->shm_cpid); 6427c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_nattch, nattch); 6437c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_cnattch, sp->shm_ismattch); 6447c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_atime, sp->shm_atime); 6457c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_dtime, sp->shm_dtime); 6467c478bd9Sstevel@tonic-gate STRUCT_FSET(ds, shm_ctime, sp->shm_ctime); 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate mutex_exit(lock); 6497c478bd9Sstevel@tonic-gate if (copyout(STRUCT_BUF(ds), arg, STRUCT_SIZE(ds))) 6507c478bd9Sstevel@tonic-gate return (EFAULT); 6517c478bd9Sstevel@tonic-gate 6527c478bd9Sstevel@tonic-gate return (0); 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate case IPC_SET64: 6557c478bd9Sstevel@tonic-gate if (error = ipcperm_set64(shm_svc, cr, 6567c478bd9Sstevel@tonic-gate &sp->shm_perm, &ds64.shmx_perm)) 6577c478bd9Sstevel@tonic-gate break; 6587c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 6597c478bd9Sstevel@tonic-gate break; 6607c478bd9Sstevel@tonic-gate 6617c478bd9Sstevel@tonic-gate case IPC_STAT64: 6627c478bd9Sstevel@tonic-gate nattch = sp->shm_perm.ipc_ref - 1; 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate ipcperm_stat64(&ds64.shmx_perm, &sp->shm_perm); 6657c478bd9Sstevel@tonic-gate ds64.shmx_segsz = sp->shm_segsz; 6667c478bd9Sstevel@tonic-gate ds64.shmx_lkcnt = sp->shm_lkcnt; 6677c478bd9Sstevel@tonic-gate ds64.shmx_lpid = sp->shm_lpid; 6687c478bd9Sstevel@tonic-gate ds64.shmx_cpid = sp->shm_cpid; 6697c478bd9Sstevel@tonic-gate ds64.shmx_nattch = nattch; 6707c478bd9Sstevel@tonic-gate ds64.shmx_cnattch = sp->shm_ismattch; 6717c478bd9Sstevel@tonic-gate ds64.shmx_atime = sp->shm_atime; 6727c478bd9Sstevel@tonic-gate ds64.shmx_dtime = sp->shm_dtime; 6737c478bd9Sstevel@tonic-gate ds64.shmx_ctime = sp->shm_ctime; 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate mutex_exit(lock); 6767c478bd9Sstevel@tonic-gate if (copyout(&ds64, arg, sizeof (struct shmid_ds64))) 6777c478bd9Sstevel@tonic-gate return (EFAULT); 6787c478bd9Sstevel@tonic-gate 6797c478bd9Sstevel@tonic-gate return (0); 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate /* Lock segment in memory */ 6827c478bd9Sstevel@tonic-gate case SHM_LOCK: 6837c478bd9Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0) 6847c478bd9Sstevel@tonic-gate break; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate if (!isspt(sp) && (sp->shm_lkcnt++ == 0)) { 6877c478bd9Sstevel@tonic-gate if (error = shmem_lock(sp->shm_amp)) { 6887c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&sp->shm_amp->a_rwlock, RW_WRITER); 6897c478bd9Sstevel@tonic-gate cmn_err(CE_NOTE, 6907c478bd9Sstevel@tonic-gate "shmctl - couldn't lock %ld pages into memory", 6917c478bd9Sstevel@tonic-gate sp->shm_amp->size); 6927c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&sp->shm_amp->a_rwlock); 6937c478bd9Sstevel@tonic-gate error = ENOMEM; 6947c478bd9Sstevel@tonic-gate sp->shm_lkcnt--; 6957c478bd9Sstevel@tonic-gate shmem_unlock(sp->shm_amp, 0); 6967c478bd9Sstevel@tonic-gate } 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate break; 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* Unlock segment */ 7017c478bd9Sstevel@tonic-gate case SHM_UNLOCK: 7027c478bd9Sstevel@tonic-gate if ((error = secpolicy_lock_memory(cr)) != 0) 7037c478bd9Sstevel@tonic-gate break; 7047c478bd9Sstevel@tonic-gate 7057c478bd9Sstevel@tonic-gate if (!isspt(sp)) { 7067c478bd9Sstevel@tonic-gate if (sp->shm_lkcnt && (--sp->shm_lkcnt == 0)) { 7077c478bd9Sstevel@tonic-gate shmem_unlock(sp->shm_amp, 1); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate } 7107c478bd9Sstevel@tonic-gate break; 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate default: 7137c478bd9Sstevel@tonic-gate error = EINVAL; 7147c478bd9Sstevel@tonic-gate break; 7157c478bd9Sstevel@tonic-gate } 7167c478bd9Sstevel@tonic-gate mutex_exit(lock); 7177c478bd9Sstevel@tonic-gate return (error); 7187c478bd9Sstevel@tonic-gate } 7197c478bd9Sstevel@tonic-gate 7207c478bd9Sstevel@tonic-gate static void 7217c478bd9Sstevel@tonic-gate shm_detach(proc_t *pp, segacct_t *sap) 7227c478bd9Sstevel@tonic-gate { 7237c478bd9Sstevel@tonic-gate kshmid_t *sp = sap->sa_id; 7247c478bd9Sstevel@tonic-gate size_t len = sap->sa_len; 7257c478bd9Sstevel@tonic-gate caddr_t addr = sap->sa_addr; 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate /* 7287c478bd9Sstevel@tonic-gate * Discard lwpchan mappings. 7297c478bd9Sstevel@tonic-gate */ 7307c478bd9Sstevel@tonic-gate if (pp->p_lcp != NULL) 7317c478bd9Sstevel@tonic-gate lwpchan_delete_mapping(pp, addr, addr + len); 7327c478bd9Sstevel@tonic-gate (void) as_unmap(pp->p_as, addr, len); 7337c478bd9Sstevel@tonic-gate 7347c478bd9Sstevel@tonic-gate /* 7357c478bd9Sstevel@tonic-gate * Perform some detach-time accounting. 7367c478bd9Sstevel@tonic-gate */ 7377c478bd9Sstevel@tonic-gate (void) ipc_lock(shm_svc, sp->shm_perm.ipc_id); 7387c478bd9Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM) 7397c478bd9Sstevel@tonic-gate sp->shm_ismattch--; 7407c478bd9Sstevel@tonic-gate sp->shm_dtime = gethrestime_sec(); 7417c478bd9Sstevel@tonic-gate sp->shm_lpid = pp->p_pid; 7427c478bd9Sstevel@tonic-gate ipc_rele(shm_svc, (kipc_perm_t *)sp); /* Drops lock */ 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate kmem_free(sap, sizeof (segacct_t)); 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate static int 7487c478bd9Sstevel@tonic-gate shmdt(caddr_t addr) 7497c478bd9Sstevel@tonic-gate { 7507c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 7517c478bd9Sstevel@tonic-gate segacct_t *sap, template; 7527c478bd9Sstevel@tonic-gate 7537c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 7547c478bd9Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */ 7557c478bd9Sstevel@tonic-gate 7567c478bd9Sstevel@tonic-gate template.sa_addr = addr; 7577c478bd9Sstevel@tonic-gate template.sa_len = 0; 7587c478bd9Sstevel@tonic-gate if ((pp->p_segacct == NULL) || 7597c478bd9Sstevel@tonic-gate ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL)) { 7607c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 7617c478bd9Sstevel@tonic-gate return (EINVAL); 7627c478bd9Sstevel@tonic-gate } 763*07b65a64Saguzovsk if (sap->sa_addr != addr) { 764*07b65a64Saguzovsk mutex_exit(&pp->p_lock); 765*07b65a64Saguzovsk return (EINVAL); 766*07b65a64Saguzovsk } 7677c478bd9Sstevel@tonic-gate avl_remove(pp->p_segacct, sap); 7687c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate shm_detach(pp, sap); 7717c478bd9Sstevel@tonic-gate 7727c478bd9Sstevel@tonic-gate return (0); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /* 7767c478bd9Sstevel@tonic-gate * Remove all shared memory segments associated with a given zone. 7777c478bd9Sstevel@tonic-gate * Called by zone_shutdown when the zone is halted. 7787c478bd9Sstevel@tonic-gate */ 7797c478bd9Sstevel@tonic-gate /*ARGSUSED1*/ 7807c478bd9Sstevel@tonic-gate static void 7817c478bd9Sstevel@tonic-gate shm_remove_zone(zoneid_t zoneid, void *arg) 7827c478bd9Sstevel@tonic-gate { 7837c478bd9Sstevel@tonic-gate ipc_remove_zone(shm_svc, zoneid); 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate /* 7877c478bd9Sstevel@tonic-gate * Shmget (create new shmem) system call. 7887c478bd9Sstevel@tonic-gate */ 7897c478bd9Sstevel@tonic-gate static int 7907c478bd9Sstevel@tonic-gate shmget(key_t key, size_t size, int shmflg, uintptr_t *rvp) 7917c478bd9Sstevel@tonic-gate { 7927c478bd9Sstevel@tonic-gate proc_t *pp = curproc; 7937c478bd9Sstevel@tonic-gate kshmid_t *sp; 7947c478bd9Sstevel@tonic-gate kmutex_t *lock; 7957c478bd9Sstevel@tonic-gate int error; 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate top: 7987c478bd9Sstevel@tonic-gate if (error = ipc_get(shm_svc, key, shmflg, (kipc_perm_t **)&sp, &lock)) 7997c478bd9Sstevel@tonic-gate return (error); 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate if (!IPC_FREE(&sp->shm_perm)) { 8027c478bd9Sstevel@tonic-gate /* 8037c478bd9Sstevel@tonic-gate * A segment with the requested key exists. 8047c478bd9Sstevel@tonic-gate */ 8057c478bd9Sstevel@tonic-gate if (size > sp->shm_segsz) { 8067c478bd9Sstevel@tonic-gate mutex_exit(lock); 8077c478bd9Sstevel@tonic-gate return (EINVAL); 8087c478bd9Sstevel@tonic-gate } 8097c478bd9Sstevel@tonic-gate } else { 8107c478bd9Sstevel@tonic-gate /* 8117c478bd9Sstevel@tonic-gate * A new segment should be created. 8127c478bd9Sstevel@tonic-gate */ 8137c478bd9Sstevel@tonic-gate size_t npages = btopr(size); 8147c478bd9Sstevel@tonic-gate size_t rsize = ptob(npages); 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate /* 8177c478bd9Sstevel@tonic-gate * Check rsize and the per-project limit on shared 8187c478bd9Sstevel@tonic-gate * memory. Checking rsize handles both the size == 0 8197c478bd9Sstevel@tonic-gate * case and the size < ULONG_MAX & PAGEMASK case (i.e. 8207c478bd9Sstevel@tonic-gate * rounding up wraps a size_t). 8217c478bd9Sstevel@tonic-gate */ 8227c478bd9Sstevel@tonic-gate if (rsize == 0 || (rctl_test(rc_project_shmmax, 8237c478bd9Sstevel@tonic-gate pp->p_task->tk_proj->kpj_rctls, pp, rsize, 8247c478bd9Sstevel@tonic-gate RCA_SAFE) & RCT_DENY)) { 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8277c478bd9Sstevel@tonic-gate mutex_exit(lock); 8287c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 8297c478bd9Sstevel@tonic-gate return (EINVAL); 8307c478bd9Sstevel@tonic-gate } 8317c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 8327c478bd9Sstevel@tonic-gate mutex_exit(lock); 8337c478bd9Sstevel@tonic-gate 8347c478bd9Sstevel@tonic-gate if (anon_resv(rsize) == 0) { 8357c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 8367c478bd9Sstevel@tonic-gate return (ENOMEM); 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate sp->shm_amp = anonmap_alloc(rsize, rsize); 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate /* 8427c478bd9Sstevel@tonic-gate * Store the original user's requested size, in bytes, 8437c478bd9Sstevel@tonic-gate * rather than the page-aligned size. The former is 8447c478bd9Sstevel@tonic-gate * used for IPC_STAT and shmget() lookups. The latter 8457c478bd9Sstevel@tonic-gate * is saved in the anon_map structure and is used for 8467c478bd9Sstevel@tonic-gate * calls to the vm layer. 8477c478bd9Sstevel@tonic-gate */ 8487c478bd9Sstevel@tonic-gate sp->shm_segsz = size; 8497c478bd9Sstevel@tonic-gate sp->shm_atime = sp->shm_dtime = 0; 8507c478bd9Sstevel@tonic-gate sp->shm_ctime = gethrestime_sec(); 8517c478bd9Sstevel@tonic-gate sp->shm_lpid = (pid_t)0; 8527c478bd9Sstevel@tonic-gate sp->shm_cpid = curproc->p_pid; 8537c478bd9Sstevel@tonic-gate sp->shm_ismattch = 0; 8547c478bd9Sstevel@tonic-gate sp->shm_sptinfo = NULL; 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate /* 8577c478bd9Sstevel@tonic-gate * Check limits one last time, push id into global 8587c478bd9Sstevel@tonic-gate * visibility, and update resource usage counts. 8597c478bd9Sstevel@tonic-gate */ 8607c478bd9Sstevel@tonic-gate if (error = ipc_commit_begin(shm_svc, key, shmflg, 8617c478bd9Sstevel@tonic-gate (kipc_perm_t *)sp)) { 8627c478bd9Sstevel@tonic-gate if (error == EAGAIN) 8637c478bd9Sstevel@tonic-gate goto top; 8647c478bd9Sstevel@tonic-gate return (error); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate if (rctl_test(rc_project_shmmax, 8687c478bd9Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_rctls, pp, rsize, 8697c478bd9Sstevel@tonic-gate RCA_SAFE) & RCT_DENY) { 8707c478bd9Sstevel@tonic-gate ipc_cleanup(shm_svc, (kipc_perm_t *)sp); 8717c478bd9Sstevel@tonic-gate return (EINVAL); 8727c478bd9Sstevel@tonic-gate } 8737c478bd9Sstevel@tonic-gate sp->shm_perm.ipc_proj->kpj_data.kpd_shmmax += rsize; 8747c478bd9Sstevel@tonic-gate 8757c478bd9Sstevel@tonic-gate lock = ipc_commit_end(shm_svc, &sp->shm_perm); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT 8797c478bd9Sstevel@tonic-gate if (audit_active) 8807c478bd9Sstevel@tonic-gate audit_ipcget(AT_IPC_SHM, (void *)sp); 8817c478bd9Sstevel@tonic-gate #endif 8827c478bd9Sstevel@tonic-gate 8837c478bd9Sstevel@tonic-gate *rvp = (uintptr_t)(sp->shm_perm.ipc_id); 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate mutex_exit(lock); 8867c478bd9Sstevel@tonic-gate return (0); 8877c478bd9Sstevel@tonic-gate } 8887c478bd9Sstevel@tonic-gate 8897c478bd9Sstevel@tonic-gate /* 8907c478bd9Sstevel@tonic-gate * shmids system call. 8917c478bd9Sstevel@tonic-gate */ 8927c478bd9Sstevel@tonic-gate static int 8937c478bd9Sstevel@tonic-gate shmids(int *buf, uint_t nids, uint_t *pnids) 8947c478bd9Sstevel@tonic-gate { 8957c478bd9Sstevel@tonic-gate return (ipc_ids(shm_svc, buf, nids, pnids)); 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate 8987c478bd9Sstevel@tonic-gate /* 8997c478bd9Sstevel@tonic-gate * System entry point for shmat, shmctl, shmdt, and shmget system calls. 9007c478bd9Sstevel@tonic-gate */ 9017c478bd9Sstevel@tonic-gate static uintptr_t 9027c478bd9Sstevel@tonic-gate shmsys(int opcode, uintptr_t a0, uintptr_t a1, uintptr_t a2) 9037c478bd9Sstevel@tonic-gate { 9047c478bd9Sstevel@tonic-gate int error; 9057c478bd9Sstevel@tonic-gate uintptr_t r_val = 0; 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate switch (opcode) { 9087c478bd9Sstevel@tonic-gate case SHMAT: 9097c478bd9Sstevel@tonic-gate error = shmat((int)a0, (caddr_t)a1, (int)a2, &r_val); 9107c478bd9Sstevel@tonic-gate break; 9117c478bd9Sstevel@tonic-gate case SHMCTL: 9127c478bd9Sstevel@tonic-gate error = shmctl((int)a0, (int)a1, (void *)a2); 9137c478bd9Sstevel@tonic-gate break; 9147c478bd9Sstevel@tonic-gate case SHMDT: 9157c478bd9Sstevel@tonic-gate error = shmdt((caddr_t)a0); 9167c478bd9Sstevel@tonic-gate break; 9177c478bd9Sstevel@tonic-gate case SHMGET: 9187c478bd9Sstevel@tonic-gate error = shmget((key_t)a0, (size_t)a1, (int)a2, &r_val); 9197c478bd9Sstevel@tonic-gate break; 9207c478bd9Sstevel@tonic-gate case SHMIDS: 9217c478bd9Sstevel@tonic-gate error = shmids((int *)a0, (uint_t)a1, (uint_t *)a2); 9227c478bd9Sstevel@tonic-gate break; 9237c478bd9Sstevel@tonic-gate default: 9247c478bd9Sstevel@tonic-gate error = EINVAL; 9257c478bd9Sstevel@tonic-gate break; 9267c478bd9Sstevel@tonic-gate } 9277c478bd9Sstevel@tonic-gate 9287c478bd9Sstevel@tonic-gate if (error) 9297c478bd9Sstevel@tonic-gate return ((uintptr_t)set_errno(error)); 9307c478bd9Sstevel@tonic-gate 9317c478bd9Sstevel@tonic-gate return (r_val); 9327c478bd9Sstevel@tonic-gate } 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate /* 9357c478bd9Sstevel@tonic-gate * segacct_t comparator 9367c478bd9Sstevel@tonic-gate * This works as expected, with one minor change: the first of two real 9377c478bd9Sstevel@tonic-gate * segments with equal addresses is considered to be 'greater than' the 9387c478bd9Sstevel@tonic-gate * second. We only return equal when searching using a template, in 9397c478bd9Sstevel@tonic-gate * which case we explicitly set the template segment's length to 0 9407c478bd9Sstevel@tonic-gate * (which is invalid for a real segment). 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate static int 9437c478bd9Sstevel@tonic-gate shm_sacompar(const void *x, const void *y) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate segacct_t *sa1 = (segacct_t *)x; 9467c478bd9Sstevel@tonic-gate segacct_t *sa2 = (segacct_t *)y; 9477c478bd9Sstevel@tonic-gate 948*07b65a64Saguzovsk if (sa1->sa_addr < sa2->sa_addr) { 9497c478bd9Sstevel@tonic-gate return (-1); 950*07b65a64Saguzovsk } else if (sa2->sa_len != 0) { 951*07b65a64Saguzovsk if (sa1->sa_addr >= sa2->sa_addr + sa2->sa_len) { 9527c478bd9Sstevel@tonic-gate return (1); 953*07b65a64Saguzovsk } else if (sa1->sa_len != 0) { 954*07b65a64Saguzovsk return (1); 955*07b65a64Saguzovsk } else { 9567c478bd9Sstevel@tonic-gate return (0); 957*07b65a64Saguzovsk } 958*07b65a64Saguzovsk } else if (sa1->sa_addr > sa2->sa_addr) { 9597c478bd9Sstevel@tonic-gate return (1); 960*07b65a64Saguzovsk } else { 961*07b65a64Saguzovsk return (0); 962*07b65a64Saguzovsk } 9637c478bd9Sstevel@tonic-gate } 9647c478bd9Sstevel@tonic-gate 9657c478bd9Sstevel@tonic-gate /* 9667c478bd9Sstevel@tonic-gate * add this record to the segacct list. 9677c478bd9Sstevel@tonic-gate */ 9687c478bd9Sstevel@tonic-gate static void 9697c478bd9Sstevel@tonic-gate sa_add(struct proc *pp, caddr_t addr, size_t len, ulong_t flags, kshmid_t *id) 9707c478bd9Sstevel@tonic-gate { 9717c478bd9Sstevel@tonic-gate segacct_t *nsap; 9727c478bd9Sstevel@tonic-gate avl_tree_t *tree = NULL; 9737c478bd9Sstevel@tonic-gate avl_index_t where; 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate nsap = kmem_alloc(sizeof (segacct_t), KM_SLEEP); 9767c478bd9Sstevel@tonic-gate nsap->sa_addr = addr; 9777c478bd9Sstevel@tonic-gate nsap->sa_len = len; 9787c478bd9Sstevel@tonic-gate nsap->sa_flags = flags; 9797c478bd9Sstevel@tonic-gate nsap->sa_id = id; 9807c478bd9Sstevel@tonic-gate 9817c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) 9827c478bd9Sstevel@tonic-gate tree = kmem_alloc(sizeof (avl_tree_t), KM_SLEEP); 9837c478bd9Sstevel@tonic-gate 9847c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 9857c478bd9Sstevel@tonic-gate prbarrier(pp); /* block /proc. See shmgetid(). */ 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) { 9887c478bd9Sstevel@tonic-gate avl_create(tree, shm_sacompar, sizeof (segacct_t), 9897c478bd9Sstevel@tonic-gate offsetof(segacct_t, sa_tree)); 9907c478bd9Sstevel@tonic-gate pp->p_segacct = tree; 9917c478bd9Sstevel@tonic-gate } else if (tree) { 9927c478bd9Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t)); 9937c478bd9Sstevel@tonic-gate } 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate /* 9967c478bd9Sstevel@tonic-gate * We can ignore the result of avl_find, as the comparator will 9977c478bd9Sstevel@tonic-gate * never return equal for segments with non-zero length. This 9987c478bd9Sstevel@tonic-gate * is a necessary hack to get around the fact that we do, in 9997c478bd9Sstevel@tonic-gate * fact, have duplicate keys. 10007c478bd9Sstevel@tonic-gate */ 10017c478bd9Sstevel@tonic-gate (void) avl_find(pp->p_segacct, nsap, &where); 10027c478bd9Sstevel@tonic-gate avl_insert(pp->p_segacct, nsap, where); 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate /* 10087c478bd9Sstevel@tonic-gate * Duplicate parent's segacct records in child. 10097c478bd9Sstevel@tonic-gate */ 10107c478bd9Sstevel@tonic-gate void 10117c478bd9Sstevel@tonic-gate shmfork(struct proc *ppp, struct proc *cpp) 10127c478bd9Sstevel@tonic-gate { 10137c478bd9Sstevel@tonic-gate segacct_t *sap; 10147c478bd9Sstevel@tonic-gate kshmid_t *sp; 10157c478bd9Sstevel@tonic-gate kmutex_t *mp; 10167c478bd9Sstevel@tonic-gate 10177c478bd9Sstevel@tonic-gate ASSERT(ppp->p_segacct != NULL); 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* 10207c478bd9Sstevel@tonic-gate * We are the only lwp running in the parent so nobody can 10217c478bd9Sstevel@tonic-gate * mess with our p_segacct list. Thus it is safe to traverse 10227c478bd9Sstevel@tonic-gate * the list without holding p_lock. This is essential because 10237c478bd9Sstevel@tonic-gate * we can't hold p_lock during a KM_SLEEP allocation. 10247c478bd9Sstevel@tonic-gate */ 10257c478bd9Sstevel@tonic-gate for (sap = (segacct_t *)avl_first(ppp->p_segacct); sap != NULL; 10267c478bd9Sstevel@tonic-gate sap = (segacct_t *)AVL_NEXT(ppp->p_segacct, sap)) { 10277c478bd9Sstevel@tonic-gate sa_add(cpp, sap->sa_addr, sap->sa_len, sap->sa_flags, 10287c478bd9Sstevel@tonic-gate sap->sa_id); 10297c478bd9Sstevel@tonic-gate sp = sap->sa_id; 10307c478bd9Sstevel@tonic-gate mp = ipc_lock(shm_svc, sp->shm_perm.ipc_id); 10317c478bd9Sstevel@tonic-gate if (sap->sa_flags & SHMSA_ISM) 10327c478bd9Sstevel@tonic-gate sp->shm_ismattch++; 10337c478bd9Sstevel@tonic-gate ipc_hold(shm_svc, (kipc_perm_t *)sp); 10347c478bd9Sstevel@tonic-gate mutex_exit(mp); 10357c478bd9Sstevel@tonic-gate } 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate 10387c478bd9Sstevel@tonic-gate /* 10397c478bd9Sstevel@tonic-gate * Detach shared memory segments from exiting process. 10407c478bd9Sstevel@tonic-gate */ 10417c478bd9Sstevel@tonic-gate void 10427c478bd9Sstevel@tonic-gate shmexit(struct proc *pp) 10437c478bd9Sstevel@tonic-gate { 10447c478bd9Sstevel@tonic-gate segacct_t *sap; 10457c478bd9Sstevel@tonic-gate avl_tree_t *tree; 10467c478bd9Sstevel@tonic-gate void *cookie = NULL; 10477c478bd9Sstevel@tonic-gate 10487c478bd9Sstevel@tonic-gate ASSERT(pp->p_segacct != NULL); 10497c478bd9Sstevel@tonic-gate 10507c478bd9Sstevel@tonic-gate mutex_enter(&pp->p_lock); 10517c478bd9Sstevel@tonic-gate prbarrier(pp); 10527c478bd9Sstevel@tonic-gate tree = pp->p_segacct; 10537c478bd9Sstevel@tonic-gate pp->p_segacct = NULL; 10547c478bd9Sstevel@tonic-gate mutex_exit(&pp->p_lock); 10557c478bd9Sstevel@tonic-gate 10567c478bd9Sstevel@tonic-gate while ((sap = avl_destroy_nodes(tree, &cookie)) != NULL) 10577c478bd9Sstevel@tonic-gate (void) shm_detach(pp, sap); 10587c478bd9Sstevel@tonic-gate 10597c478bd9Sstevel@tonic-gate avl_destroy(tree); 10607c478bd9Sstevel@tonic-gate kmem_free(tree, sizeof (avl_tree_t)); 10617c478bd9Sstevel@tonic-gate } 10627c478bd9Sstevel@tonic-gate 10637c478bd9Sstevel@tonic-gate /* 10647c478bd9Sstevel@tonic-gate * At this time pages should be in memory, so just lock them. 10657c478bd9Sstevel@tonic-gate */ 10667c478bd9Sstevel@tonic-gate static void 10677c478bd9Sstevel@tonic-gate lock_again(size_t npages, struct anon_map *amp) 10687c478bd9Sstevel@tonic-gate { 10697c478bd9Sstevel@tonic-gate struct anon *ap; 10707c478bd9Sstevel@tonic-gate struct page *pp; 10717c478bd9Sstevel@tonic-gate struct vnode *vp; 10727c478bd9Sstevel@tonic-gate anoff_t off; 10737c478bd9Sstevel@tonic-gate ulong_t anon_idx; 10747c478bd9Sstevel@tonic-gate anon_sync_obj_t cookie; 10757c478bd9Sstevel@tonic-gate 10767c478bd9Sstevel@tonic-gate ANON_LOCK_ENTER(&->a_rwlock, RW_READER); 10777c478bd9Sstevel@tonic-gate 10787c478bd9Sstevel@tonic-gate for (anon_idx = 0; npages != 0; anon_idx++, npages--) { 10797c478bd9Sstevel@tonic-gate 10807c478bd9Sstevel@tonic-gate anon_array_enter(amp, anon_idx, &cookie); 10817c478bd9Sstevel@tonic-gate ap = anon_get_ptr(amp->ahp, anon_idx); 10827c478bd9Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 10837c478bd9Sstevel@tonic-gate anon_array_exit(&cookie); 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate pp = page_lookup(vp, (u_offset_t)off, SE_SHARED); 10867c478bd9Sstevel@tonic-gate if (pp == NULL) { 10877c478bd9Sstevel@tonic-gate panic("lock_again: page not in the system"); 10887c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 10897c478bd9Sstevel@tonic-gate } 10907c478bd9Sstevel@tonic-gate (void) page_pp_lock(pp, 0, 0); 10917c478bd9Sstevel@tonic-gate page_unlock(pp); 10927c478bd9Sstevel@tonic-gate } 10937c478bd9Sstevel@tonic-gate ANON_LOCK_EXIT(&->a_rwlock); 10947c478bd9Sstevel@tonic-gate } 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate /* check if this segment is already locked. */ 10977c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 10987c478bd9Sstevel@tonic-gate static int 10997c478bd9Sstevel@tonic-gate check_locked(struct as *as, struct segvn_data *svd, size_t npages) 11007c478bd9Sstevel@tonic-gate { 11017c478bd9Sstevel@tonic-gate struct vpage *vpp = svd->vpage; 11027c478bd9Sstevel@tonic-gate size_t i; 11037c478bd9Sstevel@tonic-gate if (svd->vpage == NULL) 11047c478bd9Sstevel@tonic-gate return (0); /* unlocked */ 11057c478bd9Sstevel@tonic-gate 11067c478bd9Sstevel@tonic-gate SEGVN_LOCK_ENTER(as, &svd->lock, RW_READER); 11077c478bd9Sstevel@tonic-gate for (i = 0; i < npages; i++, vpp++) { 11087c478bd9Sstevel@tonic-gate if (VPP_ISPPLOCK(vpp) == 0) { 11097c478bd9Sstevel@tonic-gate SEGVN_LOCK_EXIT(as, &svd->lock); 11107c478bd9Sstevel@tonic-gate return (1); /* partially locked */ 11117c478bd9Sstevel@tonic-gate } 11127c478bd9Sstevel@tonic-gate } 11137c478bd9Sstevel@tonic-gate SEGVN_LOCK_EXIT(as, &svd->lock); 11147c478bd9Sstevel@tonic-gate return (2); /* locked */ 11157c478bd9Sstevel@tonic-gate } 11167c478bd9Sstevel@tonic-gate 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate /* 11197c478bd9Sstevel@tonic-gate * Attach the shared memory segment to the process 11207c478bd9Sstevel@tonic-gate * address space and lock the pages. 11217c478bd9Sstevel@tonic-gate */ 11227c478bd9Sstevel@tonic-gate static int 11237c478bd9Sstevel@tonic-gate shmem_lock(struct anon_map *amp) 11247c478bd9Sstevel@tonic-gate { 11257c478bd9Sstevel@tonic-gate size_t npages = btopr(amp->size); 11267c478bd9Sstevel@tonic-gate struct seg *seg; 11277c478bd9Sstevel@tonic-gate struct as *as; 11287c478bd9Sstevel@tonic-gate struct segvn_crargs crargs; 11297c478bd9Sstevel@tonic-gate struct segvn_data *svd; 11307c478bd9Sstevel@tonic-gate proc_t *p = curproc; 11317c478bd9Sstevel@tonic-gate caddr_t addr; 11327c478bd9Sstevel@tonic-gate uint_t error, ret; 11337c478bd9Sstevel@tonic-gate caddr_t seg_base; 11347c478bd9Sstevel@tonic-gate size_t seg_sz; 11357c478bd9Sstevel@tonic-gate 11367c478bd9Sstevel@tonic-gate as = p->p_as; 11377c478bd9Sstevel@tonic-gate AS_LOCK_ENTER(as, &as->a_lock, RW_READER); 11387c478bd9Sstevel@tonic-gate /* check if shared memory is already attached */ 11397c478bd9Sstevel@tonic-gate for (seg = AS_SEGFIRST(as); seg != NULL; seg = AS_SEGNEXT(as, seg)) { 11407c478bd9Sstevel@tonic-gate svd = (struct segvn_data *)seg->s_data; 11417c478bd9Sstevel@tonic-gate if ((seg->s_ops == &segvn_ops) && (svd->amp == amp) && 11427c478bd9Sstevel@tonic-gate (amp->size == seg->s_size)) { 11437c478bd9Sstevel@tonic-gate switch (ret = check_locked(as, svd, npages)) { 11447c478bd9Sstevel@tonic-gate case 0: /* unlocked */ 11457c478bd9Sstevel@tonic-gate case 1: /* partially locked */ 11467c478bd9Sstevel@tonic-gate seg_base = seg->s_base; 11477c478bd9Sstevel@tonic-gate seg_sz = seg->s_size; 11487c478bd9Sstevel@tonic-gate 11497c478bd9Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 11507c478bd9Sstevel@tonic-gate if ((error = as_ctl(as, seg_base, seg_sz, 11517c478bd9Sstevel@tonic-gate MC_LOCK, 0, 0, NULL, 0)) == 0) 11527c478bd9Sstevel@tonic-gate lock_again(npages, amp); 11537c478bd9Sstevel@tonic-gate (void) as_ctl(as, seg_base, seg_sz, MC_UNLOCK, 11547c478bd9Sstevel@tonic-gate 0, 0, NULL, NULL); 11557c478bd9Sstevel@tonic-gate return (error); 11567c478bd9Sstevel@tonic-gate case 2: /* locked */ 11577c478bd9Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 11587c478bd9Sstevel@tonic-gate lock_again(npages, amp); 11597c478bd9Sstevel@tonic-gate return (0); 11607c478bd9Sstevel@tonic-gate default: 11617c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "shmem_lock: deflt %d", ret); 11627c478bd9Sstevel@tonic-gate break; 11637c478bd9Sstevel@tonic-gate } 11647c478bd9Sstevel@tonic-gate } 11657c478bd9Sstevel@tonic-gate } 11667c478bd9Sstevel@tonic-gate AS_LOCK_EXIT(as, &as->a_lock); 11677c478bd9Sstevel@tonic-gate 11687c478bd9Sstevel@tonic-gate /* attach shm segment to our address space */ 11697c478bd9Sstevel@tonic-gate as_rangelock(as); 11707c478bd9Sstevel@tonic-gate map_addr(&addr, amp->size, 0ll, 1, 0); 11717c478bd9Sstevel@tonic-gate if (addr == NULL) { 11727c478bd9Sstevel@tonic-gate as_rangeunlock(as); 11737c478bd9Sstevel@tonic-gate return (ENOMEM); 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate 11767c478bd9Sstevel@tonic-gate /* Initialize the create arguments and map the segment */ 11777c478bd9Sstevel@tonic-gate crargs = *(struct segvn_crargs *)zfod_argsp; /* structure copy */ 11787c478bd9Sstevel@tonic-gate crargs.offset = (u_offset_t)0; 11797c478bd9Sstevel@tonic-gate crargs.type = MAP_SHARED; 11807c478bd9Sstevel@tonic-gate crargs.amp = amp; 11817c478bd9Sstevel@tonic-gate crargs.prot = PROT_ALL; 11827c478bd9Sstevel@tonic-gate crargs.maxprot = crargs.prot; 11837c478bd9Sstevel@tonic-gate crargs.flags = 0; 11847c478bd9Sstevel@tonic-gate 11857c478bd9Sstevel@tonic-gate error = as_map(as, addr, amp->size, segvn_create, &crargs); 11867c478bd9Sstevel@tonic-gate as_rangeunlock(as); 11877c478bd9Sstevel@tonic-gate if (!error) { 11887c478bd9Sstevel@tonic-gate if ((error = as_ctl(as, addr, amp->size, MC_LOCK, 0, 0, 11897c478bd9Sstevel@tonic-gate NULL, 0)) == 0) { 11907c478bd9Sstevel@tonic-gate lock_again(npages, amp); 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate (void) as_unmap(as, addr, amp->size); 11937c478bd9Sstevel@tonic-gate } 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 12027c478bd9Sstevel@tonic-gate shmem_unlock(struct anon_map *amp, uint_t lck) 12037c478bd9Sstevel@tonic-gate { 12047c478bd9Sstevel@tonic-gate struct anon *ap; 12057c478bd9Sstevel@tonic-gate pgcnt_t npages = btopr(amp->size); 12067c478bd9Sstevel@tonic-gate struct vnode *vp; 12077c478bd9Sstevel@tonic-gate struct page *pp; 12087c478bd9Sstevel@tonic-gate anoff_t off; 12097c478bd9Sstevel@tonic-gate ulong_t anon_idx; 12107c478bd9Sstevel@tonic-gate 12117c478bd9Sstevel@tonic-gate for (anon_idx = 0; anon_idx < npages; anon_idx++) { 12127c478bd9Sstevel@tonic-gate 12137c478bd9Sstevel@tonic-gate if ((ap = anon_get_ptr(amp->ahp, anon_idx)) == NULL) { 12147c478bd9Sstevel@tonic-gate if (lck) { 12157c478bd9Sstevel@tonic-gate panic("shmem_unlock: null app"); 12167c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 12177c478bd9Sstevel@tonic-gate } 12187c478bd9Sstevel@tonic-gate continue; 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate swap_xlate(ap, &vp, &off); 12217c478bd9Sstevel@tonic-gate pp = page_lookup(vp, off, SE_SHARED); 12227c478bd9Sstevel@tonic-gate if (pp == NULL) { 12237c478bd9Sstevel@tonic-gate if (lck) { 12247c478bd9Sstevel@tonic-gate panic("shmem_unlock: page not in the system"); 12257c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 12267c478bd9Sstevel@tonic-gate } 12277c478bd9Sstevel@tonic-gate continue; 12287c478bd9Sstevel@tonic-gate } 12297c478bd9Sstevel@tonic-gate if (pp->p_lckcnt) { 12307c478bd9Sstevel@tonic-gate page_pp_unlock(pp, 0, 0); 12317c478bd9Sstevel@tonic-gate } 12327c478bd9Sstevel@tonic-gate page_unlock(pp); 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate } 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate /* 12377c478bd9Sstevel@tonic-gate * We call this routine when we have removed all references to this 12387c478bd9Sstevel@tonic-gate * amp. This means all shmdt()s and the IPC_RMID have been done. 12397c478bd9Sstevel@tonic-gate */ 12407c478bd9Sstevel@tonic-gate static void 12417c478bd9Sstevel@tonic-gate shm_rm_amp(struct anon_map *amp, uint_t lckflag) 12427c478bd9Sstevel@tonic-gate { 12437c478bd9Sstevel@tonic-gate /* 12447c478bd9Sstevel@tonic-gate * If we are finally deleting the 12457c478bd9Sstevel@tonic-gate * shared memory, and if no one did 12467c478bd9Sstevel@tonic-gate * the SHM_UNLOCK, we must do it now. 12477c478bd9Sstevel@tonic-gate */ 12487c478bd9Sstevel@tonic-gate shmem_unlock(amp, lckflag); 12497c478bd9Sstevel@tonic-gate 12507c478bd9Sstevel@tonic-gate /* 12517c478bd9Sstevel@tonic-gate * Free up the anon_map. 12527c478bd9Sstevel@tonic-gate */ 12537c478bd9Sstevel@tonic-gate lgrp_shm_policy_fini(amp, NULL); 1254*07b65a64Saguzovsk if (amp->a_szc != 0) { 1255*07b65a64Saguzovsk ANON_LOCK_ENTER(&->a_rwlock, RW_WRITER); 1256*07b65a64Saguzovsk anon_shmap_free_pages(amp, 0, amp->size); 1257*07b65a64Saguzovsk ANON_LOCK_EXIT(&->a_rwlock); 1258*07b65a64Saguzovsk } else { 12597c478bd9Sstevel@tonic-gate anon_free(amp->ahp, 0, amp->size); 1260*07b65a64Saguzovsk } 12617c478bd9Sstevel@tonic-gate anon_unresv(amp->swresv); 12627c478bd9Sstevel@tonic-gate anonmap_free(amp); 12637c478bd9Sstevel@tonic-gate } 12647c478bd9Sstevel@tonic-gate 12657c478bd9Sstevel@tonic-gate /* 12667c478bd9Sstevel@tonic-gate * Return the shared memory id for the process's virtual address. 12677c478bd9Sstevel@tonic-gate * Return SHMID_NONE if addr is not within a SysV shared memory segment. 12687c478bd9Sstevel@tonic-gate * Return SHMID_FREE if addr's SysV shared memory segment's id has been freed. 12697c478bd9Sstevel@tonic-gate * 12707c478bd9Sstevel@tonic-gate * shmgetid() is called from code in /proc with the process locked but 12717c478bd9Sstevel@tonic-gate * with pp->p_lock not held. The address space lock is held, so we 12727c478bd9Sstevel@tonic-gate * cannot grab pp->p_lock here due to lock-ordering constraints. 12737c478bd9Sstevel@tonic-gate * Because of all this, modifications to the p_segacct list must only 12747c478bd9Sstevel@tonic-gate * be made after calling prbarrier() to ensure the process is not locked. 12757c478bd9Sstevel@tonic-gate * See shmdt() and sa_add(), above. shmgetid() may also be called on a 12767c478bd9Sstevel@tonic-gate * thread's own process without the process locked. 12777c478bd9Sstevel@tonic-gate */ 12787c478bd9Sstevel@tonic-gate int 12797c478bd9Sstevel@tonic-gate shmgetid(proc_t *pp, caddr_t addr) 12807c478bd9Sstevel@tonic-gate { 12817c478bd9Sstevel@tonic-gate segacct_t *sap, template; 12827c478bd9Sstevel@tonic-gate 12837c478bd9Sstevel@tonic-gate ASSERT(MUTEX_NOT_HELD(&pp->p_lock)); 12847c478bd9Sstevel@tonic-gate ASSERT((pp->p_proc_flag & P_PR_LOCK) || pp == curproc); 12857c478bd9Sstevel@tonic-gate 12867c478bd9Sstevel@tonic-gate if (pp->p_segacct == NULL) 12877c478bd9Sstevel@tonic-gate return (SHMID_NONE); 12887c478bd9Sstevel@tonic-gate 12897c478bd9Sstevel@tonic-gate template.sa_addr = addr; 12907c478bd9Sstevel@tonic-gate template.sa_len = 0; 12917c478bd9Sstevel@tonic-gate if ((sap = avl_find(pp->p_segacct, &template, NULL)) == NULL) 12927c478bd9Sstevel@tonic-gate return (SHMID_NONE); 12937c478bd9Sstevel@tonic-gate 12947c478bd9Sstevel@tonic-gate if (IPC_FREE(&sap->sa_id->shm_perm)) 12957c478bd9Sstevel@tonic-gate return (SHMID_FREE); 12967c478bd9Sstevel@tonic-gate 12977c478bd9Sstevel@tonic-gate return (sap->sa_id->shm_perm.ipc_id); 12987c478bd9Sstevel@tonic-gate } 1299