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