kernel - Fix SMP race in procfs
[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: head/share/doc/papers/newvm/a.t 263142 2014-03-14 03:07:51Z eadler $
30 .\"
31 .sp 2
32 .ne 2i
33 .NH
34 Appendix A \- Virtual Memory Interface
35 .NH 2
36 Mapping pages
37 .PP
38 The system supports sharing of data between processes
39 by allowing pages to be mapped into memory.  These mapped
40 pages may be \fIshared\fP with other processes or \fIprivate\fP
41 to the process.
42 Protection and sharing options are defined in \fI<sys/mman.h>\fP as:
43 .DS
44 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
45 /* protections are chosen from these bits, or-ed together */
46 #define PROT_READ       0x04    /* pages can be read */
47 #define PROT_WRITE      0x02    /* pages can be written */
48 #define PROT_EXEC       0x01    /* pages can be executed */
49 .DE
50 .DS
51 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
52 /* flags contain mapping type, sharing type and options */
53 /* mapping type; choose one */
54 #define MAP_FILE        0x0001  /* mapped from a file or device */
55 #define MAP_ANON        0x0002  /* allocated from memory, swap space */
56 #define MAP_TYPE        0x000f  /* mask for type field */
57 .DE
58 .DS
59 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
60 /* sharing types; choose one */
61 #define MAP_SHARED      0x0010  /* share changes */
62 #define MAP_PRIVATE     0x0000  /* changes are private */
63 .DE
64 .DS
65 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
66 /* other flags */
67 #define MAP_FIXED       0x0020  /* map addr must be exactly as requested */
68 #define MAP_INHERIT     0x0040  /* region is retained after exec */
69 #define MAP_HASSEMAPHORE        0x0080  /* region may contain semaphores */
70 .DE
71 The cpu-dependent size of a page is returned by the
72 \fIgetpagesize\fP system call:
73 .DS
74 pagesize = getpagesize();
75 result int pagesize;
76 .DE
77 .LP
78 The call:
79 .DS
80 maddr = mmap(addr, len, prot, flags, fd, pos);
81 result caddr_t maddr; caddr_t addr; int *len, prot, flags, fd; off_t pos;
82 .DE
83 causes the pages starting at \fIaddr\fP and continuing
84 for at most \fIlen\fP bytes to be mapped from the object represented by
85 descriptor \fIfd\fP, starting at byte offset \fIpos\fP.
86 The starting address of the region is returned;
87 for the convenience of the system,
88 it may differ from that supplied
89 unless the MAP_FIXED flag is given,
90 in which case the exact address will be used or the call will fail.
91 The actual amount mapped is returned in \fIlen\fP.
92 The \fIaddr\fP, \fIlen\fP, and \fIpos\fP parameters
93 must all be multiples of the pagesize.
94 A successful \fImmap\fP will delete any previous mapping
95 in the allocated address range.
96 The parameter \fIprot\fP specifies the accessibility
97 of the mapped pages.
98 The parameter \fIflags\fP specifies
99 the type of object to be mapped,
100 mapping options, and
101 whether modifications made to
102 this mapped copy of the page
103 are to be kept \fIprivate\fP, or are to be \fIshared\fP with
104 other references.
105 Possible types include MAP_FILE,
106 mapping a regular file or character-special device memory,
107 and MAP_ANON, which maps memory not associated with any specific file.
108 The file descriptor used for creating MAP_ANON regions is used only
109 for naming, and may be given as \-1 if no name
110 is associated with the region.\(dg
111 .FS
112 \(dg The current design does not allow a process
113 to specify the location of swap space.
114 In the future we may define an additional mapping type, MAP_SWAP,
115 in which the file descriptor argument specifies a file
116 or device to which swapping should be done.
117 .FE
118 The MAP_INHERIT flag allows a region to be inherited after an \fIexec\fP.
119 The MAP_HASSEMAPHORE flag allows special handling for
120 regions that may contain semaphores.
121 .PP
122 A facility is provided to synchronize a mapped region with the file
123 it maps; the call
124 .DS
125 msync(addr, len);
126 caddr_t addr; int len;
127 .DE
128 writes any modified pages back to the filesystem and updates
129 the file modification time.
130 If \fIlen\fP is 0, all modified pages within the region containing \fIaddr\fP
131 will be flushed;
132 if \fIlen\fP is non-zero, only the pages containing \fIaddr\fP and \fIlen\fP
133 succeeding locations will be examined.
134 Any required synchronization of memory caches
135 will also take place at this time.
136 Filesystem operations on a file that is mapped for shared modifications
137 are unpredictable except after an \fImsync\fP.
138 .PP
139 A mapping can be removed by the call
140 .DS
141 munmap(addr, len);
142 caddr_t addr; int len;
143 .DE
144 This call deletes the mappings for the specified address range,
145 and causes further references to addresses within the range
146 to generate invalid memory references.
147 .NH 2
148 Page protection control
149 .PP
150 A process can control the protection of pages using the call
151 .DS
152 mprotect(addr, len, prot);
153 caddr_t addr; int len, prot;
154 .DE
155 This call changes the specified pages to have protection \fIprot\fP\|.
156 Not all implementations will guarantee protection on a page basis;
157 the granularity of protection changes may be as large as an entire region.
158 .NH 2
159 Giving and getting advice
160 .PP
161 A process that has knowledge of its memory behavior may
162 use the \fImadvise\fP call:
163 .DS
164 madvise(addr, len, behav);
165 caddr_t addr; int len, behav;
166 .DE
167 \fIBehav\fP describes expected behavior, as given
168 in \fI<sys/mman.h>\fP:
169 .DS
170 .ta \w'#define\ \ 'u +\w'MADV_SEQUENTIAL\ \ 'u +\w'00\ \ \ \ 'u
171 #define MADV_NORMAL     0       /* no further special treatment */
172 #define MADV_RANDOM     1       /* expect random page references */
173 #define MADV_SEQUENTIAL 2       /* expect sequential references */
174 #define MADV_WILLNEED   3       /* will need these pages */
175 #define MADV_DONTNEED   4       /* don't need these pages */
176 #define MADV_SPACEAVAIL 5       /* insure that resources are reserved */
177 .DE
178 Finally, a process may obtain information about whether pages are
179 core resident by using the call
180 .DS
181 mincore(addr, len, vec)
182 caddr_t addr; int len; result char *vec;
183 .DE
184 Here the current core residency of the pages is returned
185 in the character array \fIvec\fP, with a value of 1 meaning
186 that the page is in-core.
187 .NH 2
188 Synchronization primitives
189 .PP
190 Primitives are provided for synchronization using semaphores in shared memory.
191 Semaphores must lie within a MAP_SHARED region with at least modes
192 PROT_READ and PROT_WRITE.
193 The MAP_HASSEMAPHORE flag must have been specified when the region was created.
194 To acquire a lock a process calls:
195 .DS
196 value = mset(sem, wait)
197 result int value; semaphore *sem; int wait;
198 .DE
199 \fIMset\fP indivisibly tests and sets the semaphore \fIsem\fP.
200 If the previous value is zero, the process has acquired the lock
201 and \fImset\fP returns true immediately.
202 Otherwise, if the \fIwait\fP flag is zero,
203 failure is returned.
204 If \fIwait\fP is true and the previous value is non-zero,
205 \fImset\fP relinquishes the processor until notified that it should retry.
206 .LP
207 To release a lock a process calls:
208 .DS
209 mclear(sem)
210 semaphore *sem;
211 .DE
212 \fIMclear\fP indivisibly tests and clears the semaphore \fIsem\fP.
213 If the ``WANT'' flag is zero in the previous value,
214 \fImclear\fP returns immediately.
215 If the ``WANT'' flag is non-zero in the previous value,
216 \fImclear\fP arranges for waiting processes to retry before returning.
217 .PP
218 Two routines provide services analogous to the kernel
219 \fIsleep\fP and \fIwakeup\fP functions interpreted in the domain of
220 shared memory.
221 A process may relinquish the processor by calling \fImsleep\fP
222 with a set semaphore:
223 .DS
224 msleep(sem)
225 semaphore *sem;
226 .DE
227 If the semaphore is still set when it is checked by the kernel,
228 the process will be put in a sleeping state
229 until some other process issues an \fImwakeup\fP for the same semaphore
230 within the region using the call:
231 .DS
232 mwakeup(sem)
233 semaphore *sem;
234 .DE
235 An \fImwakeup\fP may awaken all sleepers on the semaphore,
236 or may awaken only the next sleeper on a queue.