1 .\" $NetBSD: a.t,v 1.4 2003/08/07 10:30:44 agc Exp $
3 .\" Copyright (c) 1986 The Regents of the University of California.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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 .\" 3. Neither the name of the University nor the names of its contributors
15 .\" may be used to endorse or promote products derived from this software
16 .\" without specific prior written permission.
18 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" @(#)a.t 5.1 (Berkeley) 4/16/91
35 Appendix A \- Virtual Memory Interface
39 The system supports sharing of data between processes
40 by allowing pages to be mapped into memory. These mapped
41 pages may be \fIshared\fP with other processes or \fIprivate\fP
43 Protection and sharing options are defined in \fI<sys/mman.h>\fP as:
45 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
46 /* protections are chosen from these bits, or-ed together */
47 #define PROT_READ 0x04 /* pages can be read */
48 #define PROT_WRITE 0x02 /* pages can be written */
49 #define PROT_EXEC 0x01 /* pages can be executed */
52 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
53 /* flags contain mapping type, sharing type and options */
54 /* mapping type; choose one */
55 #define MAP_FILE 0x0001 /* mapped from a file or device */
56 #define MAP_ANON 0x0002 /* allocated from memory, swap space */
57 #define MAP_TYPE 0x000f /* mask for type field */
60 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
61 /* sharing types; choose one */
62 #define MAP_SHARED 0x0010 /* share changes */
63 #define MAP_PRIVATE 0x0000 /* changes are private */
66 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
68 #define MAP_FIXED 0x0020 /* map addr must be exactly as requested */
69 #define MAP_INHERIT 0x0040 /* region is retained after exec */
70 #define MAP_HASSEMAPHORE 0x0080 /* region may contain semaphores */
72 The CPU-dependent size of a page is returned by the
73 \fIgetpagesize\fP system call:
75 pagesize = getpagesize();
81 maddr = mmap(addr, len, prot, flags, fd, pos);
82 result caddr_t maddr; caddr_t addr; int *len, prot, flags, fd; off_t pos;
84 causes the pages starting at \fIaddr\fP and continuing
85 for at most \fIlen\fP bytes to be mapped from the object represented by
86 descriptor \fIfd\fP, starting at byte offset \fIpos\fP.
87 The starting address of the region is returned;
88 for the convenience of the system,
89 it may differ from that supplied
90 unless the MAP_FIXED flag is given,
91 in which case the exact address will be used or the call will fail.
92 The actual amount mapped is returned in \fIlen\fP.
93 The \fIaddr\fP, \fIlen\fP, and \fIpos\fP parameters
94 must all be multiples of the pagesize.
95 A successful \fImmap\fP will delete any previous mapping
96 in the allocated address range.
97 The parameter \fIprot\fP specifies the accessibility
99 The parameter \fIflags\fP specifies
100 the type of object to be mapped,
102 whether modifications made to
103 this mapped copy of the page
104 are to be kept \fIprivate\fP, or are to be \fIshared\fP with
106 Possible types include MAP_FILE,
107 mapping a regular file or character-special device memory,
108 and MAP_ANON, which maps memory not associated with any specific file.
109 The file descriptor used for creating MAP_ANON regions is used only
110 for naming, and may be given as \-1 if no name
111 is associated with the region.\(dg
113 \(dg The current design does not allow a process
114 to specify the location of swap space.
115 In the future we may define an additional mapping type, MAP_SWAP,
116 in which the file descriptor argument specifies a file
117 or device to which swapping should be done.
119 The MAP_INHERIT flag allows a region to be inherited after an \fIexec\fP.
120 The MAP_HASSEMAPHORE flag allows special handling for
121 regions that may contain semaphores.
123 A facility is provided to synchronize a mapped region with the file
127 caddr_t addr; int len;
129 writes any modified pages back to the filesystem and updates
130 the file modification time.
131 If \fIlen\fP is 0, all modified pages within the region containing \fIaddr\fP
133 if \fIlen\fP is non-zero, only the pages containing \fIaddr\fP and \fIlen\fP
134 succeeding locations will be examined.
135 Any required synchronization of memory caches
136 will also take place at this time.
137 Filesystem operations on a file that is mapped for shared modifications
138 are unpredictable except after an \fImsync\fP.
140 A mapping can be removed by the call
143 caddr_t addr; int len;
145 This call deletes the mappings for the specified address range,
146 and causes further references to addresses within the range
147 to generate invalid memory references.
149 Page protection control
151 A process can control the protection of pages using the call
153 mprotect(addr, len, prot);
154 caddr_t addr; int len, prot;
156 This call changes the specified pages to have protection \fIprot\fP\|.
157 Not all implementations will guarantee protection on a page basis;
158 the granularity of protection changes may be as large as an entire region.
160 Giving and getting advice
162 A process that has knowledge of its memory behavior may
163 use the \fImadvise\fP call:
165 madvise(addr, len, behav);
166 caddr_t addr; int len, behav;
168 \fIBehav\fP describes expected behavior, as given
169 in \fI<sys/mman.h>\fP:
171 .ta \w'#define\ \ 'u +\w'MADV_SEQUENTIAL\ \ 'u +\w'00\ \ \ \ 'u
172 #define MADV_NORMAL 0 /* no further special treatment */
173 #define MADV_RANDOM 1 /* expect random page references */
174 #define MADV_SEQUENTIAL 2 /* expect sequential references */
175 #define MADV_WILLNEED 3 /* will need these pages */
176 #define MADV_DONTNEED 4 /* don't need these pages */
177 #define MADV_SPACEAVAIL 5 /* insure that resources are reserved */
179 Finally, a process may obtain information about whether pages are
180 core resident by using the call
182 mincore(addr, len, vec)
183 caddr_t addr; int len; result char *vec;
185 Here the current core residency of the pages is returned
186 in the character array \fIvec\fP, with a value of 1 meaning
187 that the page is in-core.
189 Synchronization primitives
191 Primitives are provided for synchronization using semaphores in shared memory.
192 Semaphores must lie within a MAP_SHARED region with at least modes
193 PROT_READ and PROT_WRITE.
194 The MAP_HASSEMAPHORE flag must have been specified when the region was created.
195 To acquire a lock a process calls:
197 value = mset(sem, wait)
198 result int value; semaphore *sem; int wait;
200 \fIMset\fP indivisibly tests and sets the semaphore \fIsem\fP.
201 If the previous value is zero, the process has acquired the lock
202 and \fImset\fP returns true immediately.
203 Otherwise, if the \fIwait\fP flag is zero,
205 If \fIwait\fP is true and the previous value is non-zero,
206 \fImset\fP relinquishes the processor until notified that it should retry.
208 To release a lock a process calls:
213 \fIMclear\fP indivisibly tests and clears the semaphore \fIsem\fP.
214 If the ``WANT'' flag is zero in the previous value,
215 \fImclear\fP returns immediately.
216 If the ``WANT'' flag is non-zero in the previous value,
217 \fImclear\fP arranges for waiting processes to retry before returning.
219 Two routines provide services analogous to the kernel
220 \fIsleep\fP and \fIwakeup\fP functions interpreted in the domain of
222 A process may relinquish the processor by calling \fImsleep\fP
223 with a set semaphore:
228 If the semaphore is still set when it is checked by the kernel,
229 the process will be put in a sleeping state
230 until some other process issues an \fImwakeup\fP for the same semaphore
231 within the region using the call:
236 An \fImwakeup\fP may awaken all sleepers on the semaphore,
237 or may awaken only the next sleeper on a queue.