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