1*fb680e16SJohn Baldwin.\" 2*fb680e16SJohn Baldwin.\" Copyright (c) 2011 Advanced Computing Technologies LLC 3*fb680e16SJohn Baldwin.\" Written by: John H. Baldwin <jhb@FreeBSD.org> 4*fb680e16SJohn Baldwin.\" All rights reserved. 5*fb680e16SJohn Baldwin.\" 6*fb680e16SJohn Baldwin.\" Redistribution and use in source and binary forms, with or without 7*fb680e16SJohn Baldwin.\" modification, are permitted provided that the following conditions 8*fb680e16SJohn Baldwin.\" are met: 9*fb680e16SJohn Baldwin.\" 1. Redistributions of source code must retain the above copyright 10*fb680e16SJohn Baldwin.\" notice, this list of conditions and the following disclaimer. 11*fb680e16SJohn Baldwin.\" 2. Redistributions in binary form must reproduce the above copyright 12*fb680e16SJohn Baldwin.\" notice, this list of conditions and the following disclaimer in the 13*fb680e16SJohn Baldwin.\" documentation and/or other materials provided with the distribution. 14*fb680e16SJohn Baldwin.\" 15*fb680e16SJohn Baldwin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16*fb680e16SJohn Baldwin.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17*fb680e16SJohn Baldwin.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*fb680e16SJohn Baldwin.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19*fb680e16SJohn Baldwin.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*fb680e16SJohn Baldwin.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21*fb680e16SJohn Baldwin.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22*fb680e16SJohn Baldwin.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23*fb680e16SJohn Baldwin.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24*fb680e16SJohn Baldwin.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25*fb680e16SJohn Baldwin.\" SUCH DAMAGE. 26*fb680e16SJohn Baldwin.\" 27*fb680e16SJohn Baldwin.\" $FreeBSD$ 28*fb680e16SJohn Baldwin.\" 29*fb680e16SJohn Baldwin.Dd December 14, 2011 30*fb680e16SJohn Baldwin.Dt SHM_MAP 9 31*fb680e16SJohn Baldwin.Os 32*fb680e16SJohn Baldwin.Sh NAME 33*fb680e16SJohn Baldwin.Nm shm_map , 34*fb680e16SJohn Baldwin.Nm shm_unmap 35*fb680e16SJohn Baldwin.Nd map shared memory objects into the kernel's address space 36*fb680e16SJohn Baldwin.Sh SYNOPSIS 37*fb680e16SJohn Baldwin.In sys/types.h 38*fb680e16SJohn Baldwin.In sys/mman.h 39*fb680e16SJohn Baldwin.Ft int 40*fb680e16SJohn Baldwin.Fn shm_map "struct file *fp" "size_t size" "off_t offset" "void **memp" 41*fb680e16SJohn Baldwin.Ft int 42*fb680e16SJohn Baldwin.Fn shm_unmap "struct file *fp" "void *mem" "size_t size" 43*fb680e16SJohn Baldwin.Sh DESCRIPTION 44*fb680e16SJohn BaldwinThe 45*fb680e16SJohn Baldwin.Nm shm_map 46*fb680e16SJohn Baldwinand 47*fb680e16SJohn Baldwin.Nm shm_unmap 48*fb680e16SJohn Baldwinfunctions provide an API for mapping shared memory objects into the kernel. 49*fb680e16SJohn BaldwinShared memory objects are created by 50*fb680e16SJohn Baldwin.Xr shm_open 2 . 51*fb680e16SJohn BaldwinThese objects can then be passed into the kernel via file descriptors. 52*fb680e16SJohn Baldwin.Pp 53*fb680e16SJohn BaldwinA shared memory object cannot be shrunk while it is mapped into the kernel. 54*fb680e16SJohn BaldwinThis is to avoid invalidating any pages that may be wired into the kernel's 55*fb680e16SJohn Baldwinaddress space. 56*fb680e16SJohn BaldwinShared memory objects can still be grown while mapped into the kernel. 57*fb680e16SJohn Baldwin.Pp 58*fb680e16SJohn BaldwinTo simplify the accounting needed to enforce the above requirement, 59*fb680e16SJohn Baldwincallers of this API are required to unmap the entire region mapped by 60*fb680e16SJohn Baldwin.Nm shm_map 61*fb680e16SJohn Baldwinwhen calling 62*fb680e16SJohn Baldwin.Nm shm_unmap . 63*fb680e16SJohn BaldwinUnmapping only a portion of the region is not permitted. 64*fb680e16SJohn Baldwin.Pp 65*fb680e16SJohn BaldwinThe 66*fb680e16SJohn Baldwin.Nm shm_map 67*fb680e16SJohn Baldwinfunction locates the shared memory object associated with the open file 68*fb680e16SJohn Baldwin.Fa fp . 69*fb680e16SJohn BaldwinIt maps the region of that object described by 70*fb680e16SJohn Baldwin.Fa offset 71*fb680e16SJohn Baldwinand 72*fb680e16SJohn Baldwin.Fa size 73*fb680e16SJohn Baldwininto the kernel's address space. 74*fb680e16SJohn BaldwinIf it succeeds, 75*fb680e16SJohn Baldwin.Fa *memp 76*fb680e16SJohn Baldwinwill be set to the start of the mapping. 77*fb680e16SJohn BaldwinAll pages for the range will be wired into memory upon successful return. 78*fb680e16SJohn Baldwin.Pp 79*fb680e16SJohn BaldwinThe 80*fb680e16SJohn Baldwin.Nm shm_unmap 81*fb680e16SJohn Baldwinfunction unmaps a region previously mapped by 82*fb680e16SJohn Baldwin.Nm shm_map . 83*fb680e16SJohn BaldwinThe 84*fb680e16SJohn Baldwin.Fa mem 85*fb680e16SJohn Baldwinargument should match the value previously returned in 86*fb680e16SJohn Baldwin.Fa *memp , 87*fb680e16SJohn Baldwinand the 88*fb680e16SJohn Baldwin.Fa size 89*fb680e16SJohn Baldwinargument should match the value passed to 90*fb680e16SJohn Baldwin.Nm shm_map . 91*fb680e16SJohn Baldwin.Pp 92*fb680e16SJohn BaldwinNote that 93*fb680e16SJohn Baldwin.Nm shm_map 94*fb680e16SJohn Baldwinwill not hold an extra reference on the open file 95*fb680e16SJohn Baldwin.Fa fp 96*fb680e16SJohn Baldwinfor the lifetime of the mapping. 97*fb680e16SJohn BaldwinInstead, 98*fb680e16SJohn Baldwinthe calling code is required to do this if it wishes to use 99*fb680e16SJohn Baldwin.Nm shm_unmap 100*fb680e16SJohn Baldwinon the region in the future. 101*fb680e16SJohn Baldwin.Sh RETURN VALUES 102*fb680e16SJohn BaldwinThe 103*fb680e16SJohn Baldwin.Nm shm_map 104*fb680e16SJohn Baldwinand 105*fb680e16SJohn Baldwin.Nm shm_unmap 106*fb680e16SJohn Baldwinfunctions return zero on success or an error on failure. 107*fb680e16SJohn Baldwin.Sh EXAMPLES 108*fb680e16SJohn BaldwinThe following function accepts a file descriptor for a shared memory 109*fb680e16SJohn Baldwinobject. 110*fb680e16SJohn BaldwinIt maps the first sixteen kilobytes of the object into the kernel, 111*fb680e16SJohn Baldwinperforms some work on that address, 112*fb680e16SJohn Baldwinand then unmaps the address before returning. 113*fb680e16SJohn Baldwin.Bd -literal 114*fb680e16SJohn Baldwinint 115*fb680e16SJohn Baldwinshm_example(int fd) 116*fb680e16SJohn Baldwin{ 117*fb680e16SJohn Baldwin struct file *fp; 118*fb680e16SJohn Baldwin void *mem; 119*fb680e16SJohn Baldwin int error; 120*fb680e16SJohn Baldwin 121*fb680e16SJohn Baldwin error = fget(curthread, fd, CAP_MMAP, &fp) 122*fb680e16SJohn Baldwin if (error) 123*fb680e16SJohn Baldwin return (error); 124*fb680e16SJohn Baldwin error = shm_map(fp, 16384, 0, &mem); 125*fb680e16SJohn Baldwin if (error) { 126*fb680e16SJohn Baldwin fdrop(fp, curthread); 127*fb680e16SJohn Baldwin return (error); 128*fb680e16SJohn Baldwin } 129*fb680e16SJohn Baldwin 130*fb680e16SJohn Baldwin /* Do something with 'mem'. */ 131*fb680e16SJohn Baldwin 132*fb680e16SJohn Baldwin error = shm_unmap(fp, mem, 16384); 133*fb680e16SJohn Baldwin fdrop(fp, curthread); 134*fb680e16SJohn Baldwin return (error); 135*fb680e16SJohn Baldwin} 136*fb680e16SJohn Baldwin.Ed 137*fb680e16SJohn Baldwin.Sh ERRORS 138*fb680e16SJohn BaldwinThe 139*fb680e16SJohn Baldwin.Nm shm_map 140*fb680e16SJohn Baldwinfunction returns the following errors on failure: 141*fb680e16SJohn Baldwin.Bl -tag -width Er 142*fb680e16SJohn Baldwin.It Bq Er EINVAL 143*fb680e16SJohn BaldwinThe open file 144*fb680e16SJohn Baldwin.Fa fp 145*fb680e16SJohn Baldwinis not a shared memory object. 146*fb680e16SJohn Baldwin.It Bq Er EINVAL 147*fb680e16SJohn BaldwinThe requested region described by 148*fb680e16SJohn Baldwin.Fa offset 149*fb680e16SJohn Baldwinand 150*fb680e16SJohn Baldwin.Fa size 151*fb680e16SJohn Baldwinextends beyond the end of the shared memory object. 152*fb680e16SJohn Baldwin.It Bq Er ENOMEM 153*fb680e16SJohn BaldwinInsufficient address space was available. 154*fb680e16SJohn Baldwin.It Bq Er EACCES 155*fb680e16SJohn BaldwinThe shared memory object could not be mapped due to a protection error. 156*fb680e16SJohn Baldwin.It Bq Er EINVAL 157*fb680e16SJohn BaldwinThe shared memory object could not be mapped due to some other VM error. 158*fb680e16SJohn Baldwin.El 159*fb680e16SJohn Baldwin.Pp 160*fb680e16SJohn BaldwinThe 161*fb680e16SJohn Baldwin.Nm shm_unmap 162*fb680e16SJohn Baldwinfunction returns the following errors on failure: 163*fb680e16SJohn Baldwin.Bl -tag -width Er 164*fb680e16SJohn Baldwin.It Bq Er EINVAL 165*fb680e16SJohn BaldwinThe open file 166*fb680e16SJohn Baldwin.Fa fp 167*fb680e16SJohn Baldwinis not a shared memory object. 168*fb680e16SJohn Baldwin.It Bq Er EINVAL 169*fb680e16SJohn BaldwinThe address range described by 170*fb680e16SJohn Baldwin.Fa mem 171*fb680e16SJohn Baldwinand 172*fb680e16SJohn Baldwin.Fa size 173*fb680e16SJohn Baldwinis not a valid address range. 174*fb680e16SJohn Baldwin.It Bq Er EINVAL 175*fb680e16SJohn BaldwinThe address range described by 176*fb680e16SJohn Baldwin.Fa mem 177*fb680e16SJohn Baldwinand 178*fb680e16SJohn Baldwin.Fa size 179*fb680e16SJohn Baldwinis not backed by the shared memory object associated with the open file 180*fb680e16SJohn Baldwin.Fa fp , 181*fb680e16SJohn Baldwinor the address range does not cover the entire mapping of the object. 182*fb680e16SJohn Baldwin.El 183*fb680e16SJohn Baldwin.Sh SEE ALSO 184*fb680e16SJohn Baldwin.Xr shm_open 2 185*fb680e16SJohn Baldwin.Sh HISTORY 186*fb680e16SJohn BaldwinThis API was first introduced in 187*fb680e16SJohn Baldwin.Fx 10.0 . 188