xref: /freebsd/share/man/man9/shm_map.9 (revision fa9896e082a1046ff4fbc75fcba4d18d1f2efc19)
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.Dd December 14, 2011
28fb680e16SJohn Baldwin.Dt SHM_MAP 9
29fb680e16SJohn Baldwin.Os
30fb680e16SJohn Baldwin.Sh NAME
31e9a20044SRuslan Ermilov.Nm shm_map , shm_unmap
32e9a20044SRuslan Ermilov.Nd "map shared memory objects into the kernel's address space"
33fb680e16SJohn Baldwin.Sh SYNOPSIS
34fb680e16SJohn Baldwin.In sys/types.h
35fb680e16SJohn Baldwin.In sys/mman.h
36fb680e16SJohn Baldwin.Ft int
37fb680e16SJohn Baldwin.Fn shm_map "struct file *fp" "size_t size" "off_t offset" "void **memp"
38fb680e16SJohn Baldwin.Ft int
39fb680e16SJohn Baldwin.Fn shm_unmap "struct file *fp" "void *mem" "size_t size"
40fb680e16SJohn Baldwin.Sh DESCRIPTION
41fb680e16SJohn BaldwinThe
42e9a20044SRuslan Ermilov.Fn shm_map
43fb680e16SJohn Baldwinand
44e9a20044SRuslan Ermilov.Fn shm_unmap
45fb680e16SJohn Baldwinfunctions provide an API for mapping shared memory objects into the kernel.
46fb680e16SJohn BaldwinShared memory objects are created by
47fb680e16SJohn Baldwin.Xr shm_open 2 .
48fb680e16SJohn BaldwinThese objects can then be passed into the kernel via file descriptors.
49fb680e16SJohn Baldwin.Pp
50fb680e16SJohn BaldwinA shared memory object cannot be shrunk while it is mapped into the kernel.
51fb680e16SJohn BaldwinThis is to avoid invalidating any pages that may be wired into the kernel's
52fb680e16SJohn Baldwinaddress space.
53fb680e16SJohn BaldwinShared memory objects can still be grown while mapped into the kernel.
54fb680e16SJohn Baldwin.Pp
55fb680e16SJohn BaldwinTo simplify the accounting needed to enforce the above requirement,
56fb680e16SJohn Baldwincallers of this API are required to unmap the entire region mapped by
57e9a20044SRuslan Ermilov.Fn shm_map
58fb680e16SJohn Baldwinwhen calling
59e9a20044SRuslan Ermilov.Fn shm_unmap .
60fb680e16SJohn BaldwinUnmapping only a portion of the region is not permitted.
61fb680e16SJohn Baldwin.Pp
62fb680e16SJohn BaldwinThe
63e9a20044SRuslan Ermilov.Fn shm_map
64fb680e16SJohn Baldwinfunction locates the shared memory object associated with the open file
65fb680e16SJohn Baldwin.Fa fp .
66fb680e16SJohn BaldwinIt maps the region of that object described by
67fb680e16SJohn Baldwin.Fa offset
68fb680e16SJohn Baldwinand
69fb680e16SJohn Baldwin.Fa size
70fb680e16SJohn Baldwininto the kernel's address space.
71fb680e16SJohn BaldwinIf it succeeds,
72fb680e16SJohn Baldwin.Fa *memp
73fb680e16SJohn Baldwinwill be set to the start of the mapping.
74fb680e16SJohn BaldwinAll pages for the range will be wired into memory upon successful return.
75fb680e16SJohn Baldwin.Pp
76fb680e16SJohn BaldwinThe
77e9a20044SRuslan Ermilov.Fn shm_unmap
78fb680e16SJohn Baldwinfunction unmaps a region previously mapped by
79e9a20044SRuslan Ermilov.Fn shm_map .
80fb680e16SJohn BaldwinThe
81fb680e16SJohn Baldwin.Fa mem
82fb680e16SJohn Baldwinargument should match the value previously returned in
83fb680e16SJohn Baldwin.Fa *memp ,
84fb680e16SJohn Baldwinand the
85fb680e16SJohn Baldwin.Fa size
86fb680e16SJohn Baldwinargument should match the value passed to
87e9a20044SRuslan Ermilov.Fn shm_map .
88fb680e16SJohn Baldwin.Pp
89fb680e16SJohn BaldwinNote that
90e9a20044SRuslan Ermilov.Fn shm_map
91fb680e16SJohn Baldwinwill not hold an extra reference on the open file
92fb680e16SJohn Baldwin.Fa fp
93fb680e16SJohn Baldwinfor the lifetime of the mapping.
94fb680e16SJohn BaldwinInstead,
95fb680e16SJohn Baldwinthe calling code is required to do this if it wishes to use
96e9a20044SRuslan Ermilov.Fn shm_unmap
97fb680e16SJohn Baldwinon the region in the future.
98fb680e16SJohn Baldwin.Sh RETURN VALUES
99fb680e16SJohn BaldwinThe
100e9a20044SRuslan Ermilov.Fn shm_map
101fb680e16SJohn Baldwinand
102e9a20044SRuslan Ermilov.Fn shm_unmap
103fb680e16SJohn Baldwinfunctions return zero on success or an error on failure.
104fb680e16SJohn Baldwin.Sh EXAMPLES
105fb680e16SJohn BaldwinThe following function accepts a file descriptor for a shared memory
106fb680e16SJohn Baldwinobject.
107fb680e16SJohn BaldwinIt maps the first sixteen kilobytes of the object into the kernel,
108fb680e16SJohn Baldwinperforms some work on that address,
109fb680e16SJohn Baldwinand then unmaps the address before returning.
110e9a20044SRuslan Ermilov.Bd -literal -offset indent
111fb680e16SJohn Baldwinint
112fb680e16SJohn Baldwinshm_example(int fd)
113fb680e16SJohn Baldwin{
114fb680e16SJohn Baldwin	struct file *fp;
115fb680e16SJohn Baldwin	void *mem;
116fb680e16SJohn Baldwin	int error;
117fb680e16SJohn Baldwin
118e9a20044SRuslan Ermilov	error = fget(curthread, fd, CAP_MMAP, &fp);
119fb680e16SJohn Baldwin	if (error)
120fb680e16SJohn Baldwin		return (error);
121fb680e16SJohn Baldwin	error = shm_map(fp, 16384, 0, &mem);
122fb680e16SJohn Baldwin	if (error) {
123fb680e16SJohn Baldwin		fdrop(fp, curthread);
124fb680e16SJohn Baldwin		return (error);
125fb680e16SJohn Baldwin	}
126fb680e16SJohn Baldwin
127fb680e16SJohn Baldwin	/* Do something with 'mem'. */
128fb680e16SJohn Baldwin
129fb680e16SJohn Baldwin	error = shm_unmap(fp, mem, 16384);
130fb680e16SJohn Baldwin	fdrop(fp, curthread);
131fb680e16SJohn Baldwin	return (error);
132fb680e16SJohn Baldwin}
133fb680e16SJohn Baldwin.Ed
134fb680e16SJohn Baldwin.Sh ERRORS
135fb680e16SJohn BaldwinThe
136e9a20044SRuslan Ermilov.Fn shm_map
137fb680e16SJohn Baldwinfunction returns the following errors on failure:
138fb680e16SJohn Baldwin.Bl -tag -width Er
139fb680e16SJohn Baldwin.It Bq Er EINVAL
140fb680e16SJohn BaldwinThe open file
141fb680e16SJohn Baldwin.Fa fp
142fb680e16SJohn Baldwinis not a shared memory object.
143fb680e16SJohn Baldwin.It Bq Er EINVAL
144fb680e16SJohn BaldwinThe requested region described by
145fb680e16SJohn Baldwin.Fa offset
146fb680e16SJohn Baldwinand
147fb680e16SJohn Baldwin.Fa size
148fb680e16SJohn Baldwinextends beyond the end of the shared memory object.
149fb680e16SJohn Baldwin.It Bq Er ENOMEM
150fb680e16SJohn BaldwinInsufficient address space was available.
151fb680e16SJohn Baldwin.It Bq Er EACCES
152fb680e16SJohn BaldwinThe shared memory object could not be mapped due to a protection error.
153fb680e16SJohn Baldwin.It Bq Er EINVAL
154fb680e16SJohn BaldwinThe shared memory object could not be mapped due to some other VM error.
155fb680e16SJohn Baldwin.El
156fb680e16SJohn Baldwin.Pp
157fb680e16SJohn BaldwinThe
158e9a20044SRuslan Ermilov.Fn shm_unmap
159fb680e16SJohn Baldwinfunction returns the following errors on failure:
160fb680e16SJohn Baldwin.Bl -tag -width Er
161fb680e16SJohn Baldwin.It Bq Er EINVAL
162fb680e16SJohn BaldwinThe open file
163fb680e16SJohn Baldwin.Fa fp
164fb680e16SJohn Baldwinis not a shared memory object.
165fb680e16SJohn Baldwin.It Bq Er EINVAL
166fb680e16SJohn BaldwinThe address range described by
167fb680e16SJohn Baldwin.Fa mem
168fb680e16SJohn Baldwinand
169fb680e16SJohn Baldwin.Fa size
170fb680e16SJohn Baldwinis not a valid address range.
171fb680e16SJohn Baldwin.It Bq Er EINVAL
172fb680e16SJohn BaldwinThe address range described by
173fb680e16SJohn Baldwin.Fa mem
174fb680e16SJohn Baldwinand
175fb680e16SJohn Baldwin.Fa size
176fb680e16SJohn Baldwinis not backed by the shared memory object associated with the open file
177fb680e16SJohn Baldwin.Fa fp ,
178fb680e16SJohn Baldwinor the address range does not cover the entire mapping of the object.
179fb680e16SJohn Baldwin.El
180fb680e16SJohn Baldwin.Sh SEE ALSO
181fb680e16SJohn Baldwin.Xr shm_open 2
182fb680e16SJohn Baldwin.Sh HISTORY
183fb680e16SJohn BaldwinThis API was first introduced in
184fb680e16SJohn Baldwin.Fx 10.0 .
185