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