Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / doc / psd / 05.sysman / 1.2.t
1 .\" Copyright (c) 1983, 1993
2 .\"     The Regents of the University of California.  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 .\"     @(#)1.2.t       8.1 (Berkeley) 6/8/93
33 .\" $FreeBSD: src/share/doc/psd/05.sysman/1.2.t,v 1.5 1999/08/28 00:18:18 peter Exp $
34 .\"
35 .sh "Memory management\(dg
36 .NH 3
37 Text, data and stack
38 .PP
39 .FS
40 \(dg This section represents the interface planned for later
41 releases of the system.  Of the calls described in this section,
42 only \fIsbrk\fP and \fIgetpagesize\fP are included in 4.3BSD.
43 .FE
44 Each process begins execution with three logical areas of memory
45 called text, data and stack.  
46 The text area is read-only and shared, while the data and stack
47 areas are private to the process.  Both the data and stack areas may
48 be extended and contracted on program request.  The call
49 .DS
50 addr = sbrk(incr);
51 result caddr_t addr; int incr;
52 .DE
53 changes the size of the data area by \fIincr\fP bytes and
54 returns the new end of the data area, while
55 .DS
56 addr = sstk(incr);
57 result caddr_t addr; int incr;
58 .DE
59 changes the size of the stack area.
60 The stack area is also automatically extended as needed.
61 On the VAX the text and data areas are adjacent in the P0 region,
62 while the stack section is in the P1 region, and grows downward.
63 .NH 3
64 Mapping pages
65 .PP
66 The system supports sharing of data between processes
67 by allowing pages to be mapped into memory.  These mapped
68 pages may be \fIshared\fP with other processes or \fIprivate\fP
69 to the process.
70 Protection and sharing options are defined in \fI<sys/mman.h>\fP as:
71 .DS
72 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
73 /* protections are chosen from these bits, or-ed together */
74 #define PROT_READ       0x04    /* pages can be read */
75 #define PROT_WRITE      0x02    /* pages can be written */
76 #define PROT_EXEC       0x01    /* pages can be executed */
77 .DE
78 .DS
79 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
80 /* flags contain mapping type, sharing type and options */
81 /* mapping type; choose one */
82 #define MAP_FILE        0x0001  /* mapped from a file or device */
83 #define MAP_ANON        0x0002  /* allocated from memory, swap space */
84 #define MAP_TYPE        0x000f  /* mask for type field */
85 .DE
86 .DS
87 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
88 /* sharing types; choose one */
89 #define MAP_SHARED      0x0010  /* share changes */
90 #define MAP_PRIVATE     0x0000  /* changes are private */
91 .DE
92 .DS
93 .ta \w'#define\ \ 'u +\w'MAP_HASSEMAPHORE\ \ 'u +\w'0x0080\ \ 'u
94 /* other flags */
95 #define MAP_FIXED       0x0020  /* map addr must be exactly as requested */
96 #define MAP_INHERIT     0x0040  /* region is retained after exec */
97 #define MAP_HASSEMAPHORE        0x0080  /* region may contain semaphores */
98 #define MAP_NOPREALLOC  0x0100  /* do not preallocate space */
99 .DE
100 The cpu-dependent size of a page is returned by the
101 \fIgetpagesize\fP system call:
102 .DS
103 pagesize = getpagesize();
104 result int pagesize;
105 .DE
106 .LP
107 The call:
108 .DS
109 maddr = mmap(addr, len, prot, flags, fd, pos);
110 result caddr_t maddr; caddr_t addr; int *len, prot, flags, fd; off_t pos;
111 .DE
112 causes the pages starting at \fIaddr\fP and continuing
113 for at most \fIlen\fP bytes to be mapped from the object represented by
114 descriptor \fIfd\fP, starting at byte offset \fIpos\fP.
115 The starting address of the region is returned;
116 for the convenience of the system,
117 it may differ from that supplied
118 unless the MAP_FIXED flag is given,
119 in which case the exact address will be used or the call will fail.
120 The actual amount mapped is returned in \fIlen\fP.
121 The \fIaddr\fP, \fIlen\fP, and \fIpos\fP parameters
122 must all be multiples of the pagesize.
123 A successful \fImmap\fP will delete any previous mapping
124 in the allocated address range.
125 The parameter \fIprot\fP specifies the accessibility
126 of the mapped pages.
127 The parameter \fIflags\fP specifies
128 the type of object to be mapped,
129 mapping options, and
130 whether modifications made to
131 this mapped copy of the page
132 are to be kept \fIprivate\fP, or are to be \fIshared\fP with
133 other references.
134 Possible types include MAP_FILE,
135 mapping a regular file or character-special device memory,
136 and MAP_ANON, which maps memory not associated with any specific file.
137 The file descriptor used for creating MAP_ANON regions is used only
138 for naming, and may be given as \-1 if no name
139 is associated with the region.\(dd
140 .FS
141 \(dd The current design does not allow a process
142 to specify the location of swap space.
143 In the future we may define an additional mapping type, MAP_SWAP,
144 in which the file descriptor argument specifies a file
145 or device to which swapping should be done.
146 .FE
147 The MAP_INHERIT flag allows a region to be inherited after an \fIexec\fP.
148 The MAP_HASSEMAPHORE flag allows special handling for
149 regions that may contain semaphores.
150 The MAP_NOPREALLOC flag allows processes to allocate regions whose
151 virtual address space, if fully allocated,
152 would exceed the available memory plus swap resources.
153 Such regions may get a SIGSEGV signal if they page fault and resources
154 are not available to service their request;
155 typically they would free up some resources via \fIunmap\fP so that
156 when they return from the signal the page
157 fault could be successfully completed.
158 .PP
159 A facility is provided to synchronize a mapped region with the file
160 it maps; the call
161 .DS
162 msync(addr, len);
163 caddr_t addr; int len;
164 .DE
165 writes any modified pages back to the filesystem and updates
166 the file modification time.
167 If \fIlen\fP is 0, all modified pages within the region containing \fIaddr\fP
168 will be flushed;
169 if \fIlen\fP is non-zero, only the pages containing \fIaddr\fP and \fIlen\fP
170 succeeding locations will be examined.
171 Any required synchronization of memory caches
172 will also take place at this time.
173 Filesystem operations on a file that is mapped for shared modifications
174 are unpredictable except after an \fImsync\fP.
175 .PP
176 A mapping can be removed by the call
177 .DS
178 munmap(addr, len);
179 caddr_t addr; int len;
180 .DE
181 This call deletes the mappings for the specified address range,
182 and causes further references to addresses within the range
183 to generate invalid memory references.
184 .NH 3
185 Page protection control
186 .PP
187 A process can control the protection of pages using the call
188 .DS
189 mprotect(addr, len, prot);
190 caddr_t addr; int len, prot;
191 .DE
192 This call changes the specified pages to have protection \fIprot\fP\|.
193 Not all implementations will guarantee protection on a page basis;
194 the granularity of protection changes may be as large as an entire region.
195 .NH 3
196 Giving and getting advice
197 .PP
198 A process that has knowledge of its memory behavior may
199 use the \fImadvise\fP call:
200 .DS
201 madvise(addr, len, behav);
202 caddr_t addr; int len, behav;
203 .DE
204 \fIBehav\fP describes expected behavior, as given
205 in \fI<sys/mman.h>\fP:
206 .DS
207 .ta \w'#define\ \ 'u +\w'MADV_SEQUENTIAL\ \ 'u +\w'00\ \ \ \ 'u
208 #define MADV_NORMAL     0       /* no further special treatment */
209 #define MADV_RANDOM     1       /* expect random page references */
210 #define MADV_SEQUENTIAL 2       /* expect sequential references */
211 #define MADV_WILLNEED   3       /* will need these pages */
212 #define MADV_DONTNEED   4       /* don't need these pages */
213 #define MADV_SPACEAVAIL 5       /* insure that resources are reserved */
214 .DE
215 Finally, a process may obtain information about whether pages are
216 core resident by using the call
217 .DS
218 mincore(addr, len, vec)
219 caddr_t addr; int len; result char *vec;
220 .DE
221 Here the current core residency of the pages is returned
222 in the character array \fIvec\fP, with a value of 1 meaning
223 that the page is in-core.
224 .NH 3
225 Synchronization primitives
226 .PP
227 Primitives are provided for synchronization using semaphores in shared memory.
228 Semaphores must lie within a MAP_SHARED region with at least modes
229 PROT_READ and PROT_WRITE.
230 The MAP_HASSEMAPHORE flag must have been specified when the region was created.
231 To acquire a lock a process calls:
232 .DS
233 value = mset(sem, wait)
234 result int value; semaphore *sem; int wait;
235 .DE
236 \fIMset\fP indivisibly tests and sets the semaphore \fIsem\fP.
237 If the previous value is zero, the process has acquired the lock
238 and \fImset\fP returns true immediately.
239 Otherwise, if the \fIwait\fP flag is zero,
240 failure is returned.
241 If \fIwait\fP is true and the previous value is non-zero,
242 \fImset\fP relinquishes the processor until notified that it should retry.
243 .LP
244 To release a lock a process calls:
245 .DS
246 mclear(sem)
247 semaphore *sem;
248 .DE
249 \fIMclear\fP indivisibly tests and clears the semaphore \fIsem\fP.
250 If the ``WANT'' flag is zero in the previous value,
251 \fImclear\fP returns immediately.
252 If the ``WANT'' flag is non-zero in the previous value,
253 \fImclear\fP arranges for waiting processes to retry before returning.
254 .PP
255 Two routines provide services analogous to the kernel
256 \fIsleep\fP and \fIwakeup\fP functions interpreted in the domain of
257 shared memory.
258 A process may relinquish the processor by calling \fImsleep\fP
259 with a set semaphore:
260 .DS
261 msleep(sem)
262 semaphore *sem;
263 .DE
264 If the semaphore is still set when it is checked by the kernel,
265 the process will be put in a sleeping state
266 until some other process issues an \fImwakeup\fP for the same semaphore
267 within the region using the call:
268 .DS
269 mwakeup(sem)
270 semaphore *sem;
271 .DE
272 An \fImwakeup\fP may awaken all sleepers on the semaphore,
273 or may awaken only the next sleeper on a queue.