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