Remove advertising header from share/
[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. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)a.t 5.1 (Berkeley) 4/16/91
29 .\" $FreeBSD: src/share/doc/papers/newvm/a.t,v 1.6 1999/08/28 00:18:14 peter Exp $
30 .\" $DragonFly: src/share/doc/papers/newvm/a.t,v 1.2 2003/06/17 04:36:56 dillon Exp $
31 .\"
32 .sp 2
33 .ne 2i
34 .NH
35 Appendix A \- Virtual Memory Interface
36 .NH 2
37 Mapping pages
38 .PP
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
42 to the process.
43 Protection and sharing options are defined in \fI<sys/mman.h>\fP as:
44 .DS
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 */
50 .DE
51 .DS
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 */
58 .DE
59 .DS
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 */
64 .DE
65 .DS
66 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
67 /* other flags */
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 */
71 .DE
72 The cpu-dependent size of a page is returned by the
73 \fIgetpagesize\fP system call:
74 .DS
75 pagesize = getpagesize();
76 result int pagesize;
77 .DE
78 .LP
79 The call:
80 .DS
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;
83 .DE
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
98 of the mapped pages.
99 The parameter \fIflags\fP specifies
100 the type of object to be mapped,
101 mapping options, and
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
105 other references.
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
112 .FS
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.
118 .FE
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.
122 .PP
123 A facility is provided to synchronize a mapped region with the file
124 it maps; the call
125 .DS
126 msync(addr, len);
127 caddr_t addr; int len;
128 .DE
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
132 will be flushed;
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.
139 .PP
140 A mapping can be removed by the call
141 .DS
142 munmap(addr, len);
143 caddr_t addr; int len;
144 .DE
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.
148 .NH 2
149 Page protection control
150 .PP
151 A process can control the protection of pages using the call
152 .DS
153 mprotect(addr, len, prot);
154 caddr_t addr; int len, prot;
155 .DE
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.
159 .NH 2
160 Giving and getting advice
161 .PP
162 A process that has knowledge of its memory behavior may
163 use the \fImadvise\fP call:
164 .DS
165 madvise(addr, len, behav);
166 caddr_t addr; int len, behav;
167 .DE
168 \fIBehav\fP describes expected behavior, as given
169 in \fI<sys/mman.h>\fP:
170 .DS
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 */
178 .DE
179 Finally, a process may obtain information about whether pages are
180 core resident by using the call
181 .DS
182 mincore(addr, len, vec)
183 caddr_t addr; int len; result char *vec;
184 .DE
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.
188 .NH 2
189 Synchronization primitives
190 .PP
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:
196 .DS
197 value = mset(sem, wait)
198 result int value; semaphore *sem; int wait;
199 .DE
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,
204 failure is returned.
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.
207 .LP
208 To release a lock a process calls:
209 .DS
210 mclear(sem)
211 semaphore *sem;
212 .DE
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.
218 .PP
219 Two routines provide services analogous to the kernel
220 \fIsleep\fP and \fIwakeup\fP functions interpreted in the domain of
221 shared memory.
222 A process may relinquish the processor by calling \fImsleep\fP
223 with a set semaphore:
224 .DS
225 msleep(sem)
226 semaphore *sem;
227 .DE
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:
232 .DS
233 mwakeup(sem)
234 semaphore *sem;
235 .DE
236 An \fImwakeup\fP may awaken all sleepers on the semaphore,
237 or may awaken only the next sleeper on a queue.