uvm_mmap.c revision 1.153 1 1.153 maxv /* $NetBSD: uvm_mmap.c,v 1.153 2015/08/04 18:28:10 maxv Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.1 mrg * Copyright (c) 1997 Charles D. Cranor and Washington University.
5 1.51 chs * Copyright (c) 1991, 1993 The Regents of the University of California.
6 1.1 mrg * Copyright (c) 1988 University of Utah.
7 1.51 chs *
8 1.1 mrg * All rights reserved.
9 1.1 mrg *
10 1.1 mrg * This code is derived from software contributed to Berkeley by
11 1.1 mrg * the Systems Programming Group of the University of Utah Computer
12 1.1 mrg * Science Department.
13 1.1 mrg *
14 1.1 mrg * Redistribution and use in source and binary forms, with or without
15 1.1 mrg * modification, are permitted provided that the following conditions
16 1.1 mrg * are met:
17 1.1 mrg * 1. Redistributions of source code must retain the above copyright
18 1.1 mrg * notice, this list of conditions and the following disclaimer.
19 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 mrg * notice, this list of conditions and the following disclaimer in the
21 1.1 mrg * documentation and/or other materials provided with the distribution.
22 1.134 chuck * 3. Neither the name of the University nor the names of its contributors
23 1.1 mrg * may be used to endorse or promote products derived from this software
24 1.1 mrg * without specific prior written permission.
25 1.1 mrg *
26 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 mrg * SUCH DAMAGE.
37 1.1 mrg *
38 1.1 mrg * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
39 1.1 mrg * @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94
40 1.3 mrg * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
41 1.1 mrg */
42 1.1 mrg
43 1.1 mrg /*
44 1.1 mrg * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
45 1.1 mrg * function.
46 1.1 mrg */
47 1.60 lukem
48 1.60 lukem #include <sys/cdefs.h>
49 1.153 maxv __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.153 2015/08/04 18:28:10 maxv Exp $");
50 1.80 jdolecek
51 1.80 jdolecek #include "opt_compat_netbsd.h"
52 1.97 elad #include "opt_pax.h"
53 1.60 lukem
54 1.150 chs #include <sys/types.h>
55 1.1 mrg #include <sys/file.h>
56 1.1 mrg #include <sys/filedesc.h>
57 1.1 mrg #include <sys/resourcevar.h>
58 1.1 mrg #include <sys/mman.h>
59 1.150 chs
60 1.137 matt #if defined(PAX_ASLR) || defined(PAX_MPROTECT)
61 1.97 elad #include <sys/pax.h>
62 1.137 matt #endif /* PAX_ASLR || PAX_MPROTECT */
63 1.1 mrg
64 1.1 mrg #include <sys/syscallargs.h>
65 1.1 mrg
66 1.1 mrg #include <uvm/uvm.h>
67 1.1 mrg #include <uvm/uvm_device.h>
68 1.1 mrg
69 1.150 chs static int uvm_mmap(struct vm_map *, vaddr_t *, vsize_t, vm_prot_t, vm_prot_t,
70 1.150 chs int, int, struct uvm_object *, voff_t, vsize_t);
71 1.1 mrg
72 1.115 yamt static int
73 1.115 yamt range_test(vaddr_t addr, vsize_t size, bool ismmap)
74 1.115 yamt {
75 1.115 yamt vaddr_t vm_min_address = VM_MIN_ADDRESS;
76 1.115 yamt vaddr_t vm_max_address = VM_MAXUSER_ADDRESS;
77 1.115 yamt vaddr_t eaddr = addr + size;
78 1.145 martin int res = 0;
79 1.115 yamt
80 1.115 yamt if (addr < vm_min_address)
81 1.115 yamt return EINVAL;
82 1.115 yamt if (eaddr > vm_max_address)
83 1.115 yamt return ismmap ? EFBIG : EINVAL;
84 1.115 yamt if (addr > eaddr) /* no wrapping! */
85 1.115 yamt return ismmap ? EOVERFLOW : EINVAL;
86 1.145 martin
87 1.145 martin #ifdef MD_MMAP_RANGE_TEST
88 1.145 martin res = MD_MMAP_RANGE_TEST(addr, eaddr);
89 1.145 martin #endif
90 1.145 martin
91 1.145 martin return res;
92 1.115 yamt }
93 1.110 christos
94 1.1 mrg /*
95 1.1 mrg * unimplemented VM system calls:
96 1.1 mrg */
97 1.1 mrg
98 1.1 mrg /*
99 1.1 mrg * sys_sbrk: sbrk system call.
100 1.1 mrg */
101 1.1 mrg
102 1.1 mrg /* ARGSUSED */
103 1.6 mrg int
104 1.119 dsl sys_sbrk(struct lwp *l, const struct sys_sbrk_args *uap, register_t *retval)
105 1.1 mrg {
106 1.119 dsl /* {
107 1.33 kleink syscallarg(intptr_t) incr;
108 1.119 dsl } */
109 1.6 mrg
110 1.17 kleink return (ENOSYS);
111 1.1 mrg }
112 1.1 mrg
113 1.1 mrg /*
114 1.1 mrg * sys_sstk: sstk system call.
115 1.1 mrg */
116 1.1 mrg
117 1.1 mrg /* ARGSUSED */
118 1.6 mrg int
119 1.119 dsl sys_sstk(struct lwp *l, const struct sys_sstk_args *uap, register_t *retval)
120 1.1 mrg {
121 1.119 dsl /* {
122 1.20 mrg syscallarg(int) incr;
123 1.119 dsl } */
124 1.6 mrg
125 1.17 kleink return (ENOSYS);
126 1.1 mrg }
127 1.1 mrg
128 1.1 mrg /*
129 1.1 mrg * sys_mincore: determine if pages are in core or not.
130 1.1 mrg */
131 1.1 mrg
132 1.1 mrg /* ARGSUSED */
133 1.6 mrg int
134 1.129 yamt sys_mincore(struct lwp *l, const struct sys_mincore_args *uap,
135 1.129 yamt register_t *retval)
136 1.1 mrg {
137 1.119 dsl /* {
138 1.22 thorpej syscallarg(void *) addr;
139 1.20 mrg syscallarg(size_t) len;
140 1.20 mrg syscallarg(char *) vec;
141 1.119 dsl } */
142 1.67 thorpej struct proc *p = l->l_proc;
143 1.56 chs struct vm_page *pg;
144 1.22 thorpej char *vec, pgi;
145 1.22 thorpej struct uvm_object *uobj;
146 1.22 thorpej struct vm_amap *amap;
147 1.22 thorpej struct vm_anon *anon;
148 1.53 chs struct vm_map_entry *entry;
149 1.22 thorpej vaddr_t start, end, lim;
150 1.53 chs struct vm_map *map;
151 1.22 thorpej vsize_t len;
152 1.22 thorpej int error = 0, npgs;
153 1.22 thorpej
154 1.22 thorpej map = &p->p_vmspace->vm_map;
155 1.22 thorpej
156 1.22 thorpej start = (vaddr_t)SCARG(uap, addr);
157 1.22 thorpej len = SCARG(uap, len);
158 1.22 thorpej vec = SCARG(uap, vec);
159 1.22 thorpej
160 1.22 thorpej if (start & PAGE_MASK)
161 1.22 thorpej return (EINVAL);
162 1.22 thorpej len = round_page(len);
163 1.22 thorpej end = start + len;
164 1.22 thorpej if (end <= start)
165 1.22 thorpej return (EINVAL);
166 1.22 thorpej
167 1.22 thorpej /*
168 1.22 thorpej * Lock down vec, so our returned status isn't outdated by
169 1.22 thorpej * storing the status byte for a page.
170 1.22 thorpej */
171 1.50 chs
172 1.62 chs npgs = len >> PAGE_SHIFT;
173 1.100 chs error = uvm_vslock(p->p_vmspace, vec, npgs, VM_PROT_WRITE);
174 1.62 chs if (error) {
175 1.62 chs return error;
176 1.62 chs }
177 1.22 thorpej vm_map_lock_read(map);
178 1.22 thorpej
179 1.107 thorpej if (uvm_map_lookup_entry(map, start, &entry) == false) {
180 1.22 thorpej error = ENOMEM;
181 1.22 thorpej goto out;
182 1.22 thorpej }
183 1.22 thorpej
184 1.22 thorpej for (/* nothing */;
185 1.22 thorpej entry != &map->header && entry->start < end;
186 1.22 thorpej entry = entry->next) {
187 1.49 chs KASSERT(!UVM_ET_ISSUBMAP(entry));
188 1.49 chs KASSERT(start >= entry->start);
189 1.49 chs
190 1.22 thorpej /* Make sure there are no holes. */
191 1.22 thorpej if (entry->end < end &&
192 1.22 thorpej (entry->next == &map->header ||
193 1.22 thorpej entry->next->start > entry->end)) {
194 1.22 thorpej error = ENOMEM;
195 1.22 thorpej goto out;
196 1.22 thorpej }
197 1.6 mrg
198 1.22 thorpej lim = end < entry->end ? end : entry->end;
199 1.22 thorpej
200 1.22 thorpej /*
201 1.31 thorpej * Special case for objects with no "real" pages. Those
202 1.31 thorpej * are always considered resident (mapped devices).
203 1.22 thorpej */
204 1.50 chs
205 1.22 thorpej if (UVM_ET_ISOBJ(entry)) {
206 1.49 chs KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
207 1.79 yamt if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
208 1.22 thorpej for (/* nothing */; start < lim;
209 1.22 thorpej start += PAGE_SIZE, vec++)
210 1.22 thorpej subyte(vec, 1);
211 1.22 thorpej continue;
212 1.22 thorpej }
213 1.22 thorpej }
214 1.22 thorpej
215 1.132 uebayasi amap = entry->aref.ar_amap; /* upper layer */
216 1.132 uebayasi uobj = entry->object.uvm_obj; /* lower layer */
217 1.22 thorpej
218 1.22 thorpej if (amap != NULL)
219 1.22 thorpej amap_lock(amap);
220 1.22 thorpej if (uobj != NULL)
221 1.136 rmind mutex_enter(uobj->vmobjlock);
222 1.22 thorpej
223 1.22 thorpej for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
224 1.22 thorpej pgi = 0;
225 1.22 thorpej if (amap != NULL) {
226 1.132 uebayasi /* Check the upper layer first. */
227 1.22 thorpej anon = amap_lookup(&entry->aref,
228 1.22 thorpej start - entry->start);
229 1.22 thorpej /* Don't need to lock anon here. */
230 1.91 yamt if (anon != NULL && anon->an_page != NULL) {
231 1.50 chs
232 1.22 thorpej /*
233 1.22 thorpej * Anon has the page for this entry
234 1.22 thorpej * offset.
235 1.22 thorpej */
236 1.50 chs
237 1.22 thorpej pgi = 1;
238 1.22 thorpej }
239 1.22 thorpej }
240 1.22 thorpej if (uobj != NULL && pgi == 0) {
241 1.132 uebayasi /* Check the lower layer. */
242 1.56 chs pg = uvm_pagelookup(uobj,
243 1.22 thorpej entry->offset + (start - entry->start));
244 1.56 chs if (pg != NULL) {
245 1.50 chs
246 1.22 thorpej /*
247 1.22 thorpej * Object has the page for this entry
248 1.22 thorpej * offset.
249 1.22 thorpej */
250 1.50 chs
251 1.22 thorpej pgi = 1;
252 1.22 thorpej }
253 1.22 thorpej }
254 1.22 thorpej (void) subyte(vec, pgi);
255 1.22 thorpej }
256 1.22 thorpej if (uobj != NULL)
257 1.136 rmind mutex_exit(uobj->vmobjlock);
258 1.22 thorpej if (amap != NULL)
259 1.22 thorpej amap_unlock(amap);
260 1.22 thorpej }
261 1.22 thorpej
262 1.22 thorpej out:
263 1.22 thorpej vm_map_unlock_read(map);
264 1.100 chs uvm_vsunlock(p->p_vmspace, SCARG(uap, vec), npgs);
265 1.22 thorpej return (error);
266 1.1 mrg }
267 1.1 mrg
268 1.1 mrg /*
269 1.1 mrg * sys_mmap: mmap system call.
270 1.1 mrg *
271 1.64 atatat * => file offset and address may not be page aligned
272 1.1 mrg * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
273 1.1 mrg * - if address isn't page aligned the mapping starts at trunc_page(addr)
274 1.1 mrg * and the return value is adjusted up by the page offset.
275 1.1 mrg */
276 1.1 mrg
277 1.6 mrg int
278 1.119 dsl sys_mmap(struct lwp *l, const struct sys_mmap_args *uap, register_t *retval)
279 1.6 mrg {
280 1.119 dsl /* {
281 1.108 christos syscallarg(void *) addr;
282 1.6 mrg syscallarg(size_t) len;
283 1.6 mrg syscallarg(int) prot;
284 1.6 mrg syscallarg(int) flags;
285 1.6 mrg syscallarg(int) fd;
286 1.6 mrg syscallarg(long) pad;
287 1.6 mrg syscallarg(off_t) pos;
288 1.119 dsl } */
289 1.67 thorpej struct proc *p = l->l_proc;
290 1.12 eeh vaddr_t addr;
291 1.6 mrg off_t pos;
292 1.152 mlelstv vsize_t size, pageoff, newsize;
293 1.6 mrg vm_prot_t prot, maxprot;
294 1.150 chs int flags, fd, advice;
295 1.110 christos vaddr_t defaddr;
296 1.116 ad struct file *fp = NULL;
297 1.150 chs struct uvm_object *uobj;
298 1.6 mrg int error;
299 1.120 christos #ifdef PAX_ASLR
300 1.120 christos vaddr_t orig_addr;
301 1.120 christos #endif /* PAX_ASLR */
302 1.6 mrg
303 1.6 mrg /*
304 1.6 mrg * first, extract syscall args from the uap.
305 1.6 mrg */
306 1.6 mrg
307 1.50 chs addr = (vaddr_t)SCARG(uap, addr);
308 1.50 chs size = (vsize_t)SCARG(uap, len);
309 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
310 1.6 mrg flags = SCARG(uap, flags);
311 1.6 mrg fd = SCARG(uap, fd);
312 1.6 mrg pos = SCARG(uap, pos);
313 1.6 mrg
314 1.120 christos #ifdef PAX_ASLR
315 1.120 christos orig_addr = addr;
316 1.120 christos #endif /* PAX_ASLR */
317 1.120 christos
318 1.6 mrg /*
319 1.24 thorpej * Fixup the old deprecated MAP_COPY into MAP_PRIVATE, and
320 1.24 thorpej * validate the flags.
321 1.24 thorpej */
322 1.147 christos if (flags & MAP_COPY) {
323 1.24 thorpej flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
324 1.147 christos #if defined(COMPAT_10) && defined(__i386__)
325 1.147 christos /*
326 1.147 christos * Ancient kernel on x86 did not obey PROT_EXEC on i386 at least
327 1.147 christos * and ld.so did not turn it on. We take care of this on amd64
328 1.147 christos * in compat32.
329 1.147 christos */
330 1.148 christos prot |= PROT_EXEC;
331 1.147 christos #endif
332 1.147 christos }
333 1.24 thorpej if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
334 1.24 thorpej return (EINVAL);
335 1.24 thorpej
336 1.24 thorpej /*
337 1.6 mrg * align file position and save offset. adjust size.
338 1.6 mrg */
339 1.6 mrg
340 1.6 mrg pageoff = (pos & PAGE_MASK);
341 1.152 mlelstv pos -= pageoff;
342 1.152 mlelstv newsize = size + pageoff; /* add offset */
343 1.152 mlelstv newsize = (vsize_t)round_page(newsize); /* round up */
344 1.152 mlelstv
345 1.152 mlelstv if (newsize < size)
346 1.152 mlelstv return (ENOMEM);
347 1.152 mlelstv size = newsize;
348 1.6 mrg
349 1.6 mrg /*
350 1.51 chs * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
351 1.6 mrg */
352 1.6 mrg if (flags & MAP_FIXED) {
353 1.6 mrg
354 1.6 mrg /* ensure address and file offset are aligned properly */
355 1.6 mrg addr -= pageoff;
356 1.6 mrg if (addr & PAGE_MASK)
357 1.6 mrg return (EINVAL);
358 1.6 mrg
359 1.115 yamt error = range_test(addr, size, true);
360 1.150 chs if (error) {
361 1.115 yamt return error;
362 1.150 chs }
363 1.150 chs
364 1.75 christos } else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
365 1.6 mrg
366 1.6 mrg /*
367 1.68 atatat * not fixed: make sure we skip over the largest
368 1.68 atatat * possible heap for non-topdown mapping arrangements.
369 1.68 atatat * we will refine our guess later (e.g. to account for
370 1.68 atatat * VAC, etc)
371 1.6 mrg */
372 1.46 chs
373 1.89 fvdl defaddr = p->p_emul->e_vm_default_addr(p,
374 1.89 fvdl (vaddr_t)p->p_vmspace->vm_daddr, size);
375 1.89 fvdl
376 1.68 atatat if (addr == 0 ||
377 1.68 atatat !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
378 1.89 fvdl addr = MAX(addr, defaddr);
379 1.68 atatat else
380 1.89 fvdl addr = MIN(addr, defaddr);
381 1.6 mrg }
382 1.6 mrg
383 1.6 mrg /*
384 1.6 mrg * check for file mappings (i.e. not anonymous) and verify file.
385 1.6 mrg */
386 1.6 mrg
387 1.150 chs advice = UVM_ADV_NORMAL;
388 1.6 mrg if ((flags & MAP_ANON) == 0) {
389 1.122 ad if ((fp = fd_getfile(fd)) == NULL)
390 1.54 thorpej return (EBADF);
391 1.150 chs
392 1.150 chs if (fp->f_ops->fo_mmap == NULL) {
393 1.150 chs error = ENODEV;
394 1.150 chs goto out;
395 1.116 ad }
396 1.150 chs error = (*fp->f_ops->fo_mmap)(fp, &pos, size, prot, &flags,
397 1.150 chs &advice, &uobj, &maxprot);
398 1.150 chs if (error) {
399 1.150 chs goto out;
400 1.116 ad }
401 1.150 chs if (uobj == NULL) {
402 1.6 mrg flags |= MAP_ANON;
403 1.122 ad fd_putfile(fd);
404 1.116 ad fp = NULL;
405 1.6 mrg goto is_anon;
406 1.6 mrg }
407 1.6 mrg } else { /* MAP_ANON case */
408 1.24 thorpej /*
409 1.24 thorpej * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
410 1.24 thorpej */
411 1.6 mrg if (fd != -1)
412 1.6 mrg return (EINVAL);
413 1.1 mrg
414 1.24 thorpej is_anon: /* label for SunOS style /dev/zero */
415 1.150 chs uobj = NULL;
416 1.6 mrg maxprot = VM_PROT_ALL;
417 1.6 mrg pos = 0;
418 1.28 cgd }
419 1.28 cgd
420 1.97 elad #ifdef PAX_MPROTECT
421 1.97 elad pax_mprotect(l, &prot, &maxprot);
422 1.97 elad #endif /* PAX_MPROTECT */
423 1.97 elad
424 1.120 christos #ifdef PAX_ASLR
425 1.153 maxv pax_aslr_mmap(l, &addr, orig_addr, flags);
426 1.120 christos #endif /* PAX_ASLR */
427 1.120 christos
428 1.6 mrg /*
429 1.6 mrg * now let kernel internal function uvm_mmap do the work.
430 1.6 mrg */
431 1.6 mrg
432 1.6 mrg error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
433 1.150 chs flags, advice, uobj, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
434 1.6 mrg
435 1.150 chs /* remember to add offset */
436 1.150 chs *retval = (register_t)(addr + pageoff);
437 1.1 mrg
438 1.150 chs out:
439 1.116 ad if (fp != NULL)
440 1.122 ad fd_putfile(fd);
441 1.116 ad
442 1.6 mrg return (error);
443 1.1 mrg }
444 1.1 mrg
445 1.1 mrg /*
446 1.1 mrg * sys___msync13: the msync system call (a front-end for flush)
447 1.1 mrg */
448 1.1 mrg
449 1.6 mrg int
450 1.129 yamt sys___msync13(struct lwp *l, const struct sys___msync13_args *uap,
451 1.129 yamt register_t *retval)
452 1.6 mrg {
453 1.119 dsl /* {
454 1.108 christos syscallarg(void *) addr;
455 1.6 mrg syscallarg(size_t) len;
456 1.6 mrg syscallarg(int) flags;
457 1.119 dsl } */
458 1.67 thorpej struct proc *p = l->l_proc;
459 1.12 eeh vaddr_t addr;
460 1.12 eeh vsize_t size, pageoff;
461 1.53 chs struct vm_map *map;
462 1.50 chs int error, rv, flags, uvmflags;
463 1.6 mrg
464 1.6 mrg /*
465 1.6 mrg * extract syscall args from the uap
466 1.6 mrg */
467 1.6 mrg
468 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
469 1.12 eeh size = (vsize_t)SCARG(uap, len);
470 1.6 mrg flags = SCARG(uap, flags);
471 1.6 mrg
472 1.6 mrg /* sanity check flags */
473 1.6 mrg if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
474 1.77 chs (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
475 1.77 chs (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
476 1.77 chs return (EINVAL);
477 1.6 mrg if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
478 1.77 chs flags |= MS_SYNC;
479 1.1 mrg
480 1.6 mrg /*
481 1.50 chs * align the address to a page boundary and adjust the size accordingly.
482 1.6 mrg */
483 1.6 mrg
484 1.6 mrg pageoff = (addr & PAGE_MASK);
485 1.6 mrg addr -= pageoff;
486 1.6 mrg size += pageoff;
487 1.50 chs size = (vsize_t)round_page(size);
488 1.6 mrg
489 1.115 yamt error = range_test(addr, size, false);
490 1.115 yamt if (error)
491 1.115 yamt return error;
492 1.6 mrg
493 1.6 mrg /*
494 1.6 mrg * get map
495 1.6 mrg */
496 1.6 mrg
497 1.6 mrg map = &p->p_vmspace->vm_map;
498 1.6 mrg
499 1.6 mrg /*
500 1.6 mrg * XXXCDC: do we really need this semantic?
501 1.6 mrg *
502 1.6 mrg * XXX Gak! If size is zero we are supposed to sync "all modified
503 1.6 mrg * pages with the region containing addr". Unfortunately, we
504 1.6 mrg * don't really keep track of individual mmaps so we approximate
505 1.6 mrg * by flushing the range of the map entry containing addr.
506 1.6 mrg * This can be incorrect if the region splits or is coalesced
507 1.6 mrg * with a neighbor.
508 1.6 mrg */
509 1.50 chs
510 1.6 mrg if (size == 0) {
511 1.53 chs struct vm_map_entry *entry;
512 1.51 chs
513 1.6 mrg vm_map_lock_read(map);
514 1.6 mrg rv = uvm_map_lookup_entry(map, addr, &entry);
515 1.107 thorpej if (rv == true) {
516 1.6 mrg addr = entry->start;
517 1.6 mrg size = entry->end - entry->start;
518 1.6 mrg }
519 1.6 mrg vm_map_unlock_read(map);
520 1.107 thorpej if (rv == false)
521 1.6 mrg return (EINVAL);
522 1.6 mrg }
523 1.6 mrg
524 1.6 mrg /*
525 1.6 mrg * translate MS_ flags into PGO_ flags
526 1.6 mrg */
527 1.50 chs
528 1.34 thorpej uvmflags = PGO_CLEANIT;
529 1.34 thorpej if (flags & MS_INVALIDATE)
530 1.34 thorpej uvmflags |= PGO_FREE;
531 1.6 mrg if (flags & MS_SYNC)
532 1.6 mrg uvmflags |= PGO_SYNCIO;
533 1.6 mrg
534 1.50 chs error = uvm_map_clean(map, addr, addr+size, uvmflags);
535 1.50 chs return error;
536 1.1 mrg }
537 1.1 mrg
538 1.1 mrg /*
539 1.1 mrg * sys_munmap: unmap a users memory
540 1.1 mrg */
541 1.1 mrg
542 1.6 mrg int
543 1.119 dsl sys_munmap(struct lwp *l, const struct sys_munmap_args *uap, register_t *retval)
544 1.6 mrg {
545 1.119 dsl /* {
546 1.108 christos syscallarg(void *) addr;
547 1.6 mrg syscallarg(size_t) len;
548 1.119 dsl } */
549 1.67 thorpej struct proc *p = l->l_proc;
550 1.12 eeh vaddr_t addr;
551 1.12 eeh vsize_t size, pageoff;
552 1.53 chs struct vm_map *map;
553 1.6 mrg struct vm_map_entry *dead_entries;
554 1.115 yamt int error;
555 1.6 mrg
556 1.6 mrg /*
557 1.50 chs * get syscall args.
558 1.6 mrg */
559 1.6 mrg
560 1.50 chs addr = (vaddr_t)SCARG(uap, addr);
561 1.50 chs size = (vsize_t)SCARG(uap, len);
562 1.51 chs
563 1.6 mrg /*
564 1.50 chs * align the address to a page boundary and adjust the size accordingly.
565 1.6 mrg */
566 1.6 mrg
567 1.6 mrg pageoff = (addr & PAGE_MASK);
568 1.6 mrg addr -= pageoff;
569 1.6 mrg size += pageoff;
570 1.50 chs size = (vsize_t)round_page(size);
571 1.6 mrg
572 1.6 mrg if (size == 0)
573 1.6 mrg return (0);
574 1.6 mrg
575 1.115 yamt error = range_test(addr, size, false);
576 1.115 yamt if (error)
577 1.115 yamt return error;
578 1.110 christos
579 1.6 mrg map = &p->p_vmspace->vm_map;
580 1.6 mrg
581 1.6 mrg /*
582 1.51 chs * interesting system call semantic: make sure entire range is
583 1.6 mrg * allocated before allowing an unmap.
584 1.6 mrg */
585 1.6 mrg
586 1.50 chs vm_map_lock(map);
587 1.66 mycroft #if 0
588 1.6 mrg if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
589 1.6 mrg vm_map_unlock(map);
590 1.6 mrg return (EINVAL);
591 1.6 mrg }
592 1.66 mycroft #endif
593 1.144 para uvm_unmap_remove(map, addr, addr + size, &dead_entries, 0);
594 1.50 chs vm_map_unlock(map);
595 1.6 mrg if (dead_entries != NULL)
596 1.6 mrg uvm_unmap_detach(dead_entries, 0);
597 1.6 mrg return (0);
598 1.1 mrg }
599 1.1 mrg
600 1.1 mrg /*
601 1.1 mrg * sys_mprotect: the mprotect system call
602 1.1 mrg */
603 1.1 mrg
604 1.6 mrg int
605 1.129 yamt sys_mprotect(struct lwp *l, const struct sys_mprotect_args *uap,
606 1.129 yamt register_t *retval)
607 1.6 mrg {
608 1.119 dsl /* {
609 1.108 christos syscallarg(void *) addr;
610 1.76 chs syscallarg(size_t) len;
611 1.6 mrg syscallarg(int) prot;
612 1.119 dsl } */
613 1.67 thorpej struct proc *p = l->l_proc;
614 1.12 eeh vaddr_t addr;
615 1.12 eeh vsize_t size, pageoff;
616 1.6 mrg vm_prot_t prot;
617 1.50 chs int error;
618 1.6 mrg
619 1.6 mrg /*
620 1.6 mrg * extract syscall args from uap
621 1.6 mrg */
622 1.6 mrg
623 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
624 1.12 eeh size = (vsize_t)SCARG(uap, len);
625 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
626 1.6 mrg
627 1.6 mrg /*
628 1.50 chs * align the address to a page boundary and adjust the size accordingly.
629 1.6 mrg */
630 1.50 chs
631 1.6 mrg pageoff = (addr & PAGE_MASK);
632 1.6 mrg addr -= pageoff;
633 1.6 mrg size += pageoff;
634 1.76 chs size = round_page(size);
635 1.50 chs
636 1.115 yamt error = range_test(addr, size, false);
637 1.115 yamt if (error)
638 1.115 yamt return error;
639 1.110 christos
640 1.50 chs error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
641 1.107 thorpej false);
642 1.50 chs return error;
643 1.1 mrg }
644 1.1 mrg
645 1.1 mrg /*
646 1.1 mrg * sys_minherit: the minherit system call
647 1.1 mrg */
648 1.1 mrg
649 1.6 mrg int
650 1.129 yamt sys_minherit(struct lwp *l, const struct sys_minherit_args *uap,
651 1.129 yamt register_t *retval)
652 1.6 mrg {
653 1.119 dsl /* {
654 1.108 christos syscallarg(void *) addr;
655 1.6 mrg syscallarg(int) len;
656 1.6 mrg syscallarg(int) inherit;
657 1.119 dsl } */
658 1.67 thorpej struct proc *p = l->l_proc;
659 1.12 eeh vaddr_t addr;
660 1.12 eeh vsize_t size, pageoff;
661 1.40 augustss vm_inherit_t inherit;
662 1.50 chs int error;
663 1.51 chs
664 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
665 1.12 eeh size = (vsize_t)SCARG(uap, len);
666 1.6 mrg inherit = SCARG(uap, inherit);
667 1.50 chs
668 1.6 mrg /*
669 1.50 chs * align the address to a page boundary and adjust the size accordingly.
670 1.6 mrg */
671 1.6 mrg
672 1.6 mrg pageoff = (addr & PAGE_MASK);
673 1.6 mrg addr -= pageoff;
674 1.6 mrg size += pageoff;
675 1.50 chs size = (vsize_t)round_page(size);
676 1.6 mrg
677 1.115 yamt error = range_test(addr, size, false);
678 1.115 yamt if (error)
679 1.115 yamt return error;
680 1.110 christos
681 1.50 chs error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
682 1.50 chs inherit);
683 1.50 chs return error;
684 1.21 mrg }
685 1.21 mrg
686 1.21 mrg /*
687 1.21 mrg * sys_madvise: give advice about memory usage.
688 1.21 mrg */
689 1.21 mrg
690 1.21 mrg /* ARGSUSED */
691 1.21 mrg int
692 1.129 yamt sys_madvise(struct lwp *l, const struct sys_madvise_args *uap,
693 1.129 yamt register_t *retval)
694 1.21 mrg {
695 1.119 dsl /* {
696 1.108 christos syscallarg(void *) addr;
697 1.21 mrg syscallarg(size_t) len;
698 1.21 mrg syscallarg(int) behav;
699 1.119 dsl } */
700 1.67 thorpej struct proc *p = l->l_proc;
701 1.21 mrg vaddr_t addr;
702 1.21 mrg vsize_t size, pageoff;
703 1.50 chs int advice, error;
704 1.51 chs
705 1.21 mrg addr = (vaddr_t)SCARG(uap, addr);
706 1.21 mrg size = (vsize_t)SCARG(uap, len);
707 1.21 mrg advice = SCARG(uap, behav);
708 1.21 mrg
709 1.21 mrg /*
710 1.21 mrg * align the address to a page boundary, and adjust the size accordingly
711 1.21 mrg */
712 1.50 chs
713 1.21 mrg pageoff = (addr & PAGE_MASK);
714 1.21 mrg addr -= pageoff;
715 1.21 mrg size += pageoff;
716 1.50 chs size = (vsize_t)round_page(size);
717 1.21 mrg
718 1.115 yamt error = range_test(addr, size, false);
719 1.115 yamt if (error)
720 1.115 yamt return error;
721 1.29 thorpej
722 1.29 thorpej switch (advice) {
723 1.29 thorpej case MADV_NORMAL:
724 1.29 thorpej case MADV_RANDOM:
725 1.29 thorpej case MADV_SEQUENTIAL:
726 1.50 chs error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
727 1.29 thorpej advice);
728 1.29 thorpej break;
729 1.29 thorpej
730 1.29 thorpej case MADV_WILLNEED:
731 1.50 chs
732 1.29 thorpej /*
733 1.29 thorpej * Activate all these pages, pre-faulting them in if
734 1.29 thorpej * necessary.
735 1.29 thorpej */
736 1.130 yamt error = uvm_map_willneed(&p->p_vmspace->vm_map,
737 1.130 yamt addr, addr + size);
738 1.130 yamt break;
739 1.29 thorpej
740 1.29 thorpej case MADV_DONTNEED:
741 1.50 chs
742 1.29 thorpej /*
743 1.29 thorpej * Deactivate all these pages. We don't need them
744 1.29 thorpej * any more. We don't, however, toss the data in
745 1.29 thorpej * the pages.
746 1.29 thorpej */
747 1.50 chs
748 1.50 chs error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
749 1.29 thorpej PGO_DEACTIVATE);
750 1.29 thorpej break;
751 1.29 thorpej
752 1.29 thorpej case MADV_FREE:
753 1.50 chs
754 1.29 thorpej /*
755 1.29 thorpej * These pages contain no valid data, and may be
756 1.45 soren * garbage-collected. Toss all resources, including
757 1.30 thorpej * any swap space in use.
758 1.29 thorpej */
759 1.50 chs
760 1.50 chs error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
761 1.29 thorpej PGO_FREE);
762 1.29 thorpej break;
763 1.29 thorpej
764 1.29 thorpej case MADV_SPACEAVAIL:
765 1.50 chs
766 1.29 thorpej /*
767 1.29 thorpej * XXXMRG What is this? I think it's:
768 1.29 thorpej *
769 1.29 thorpej * Ensure that we have allocated backing-store
770 1.29 thorpej * for these pages.
771 1.29 thorpej *
772 1.29 thorpej * This is going to require changes to the page daemon,
773 1.29 thorpej * as it will free swap space allocated to pages in core.
774 1.29 thorpej * There's also what to do for device/file/anonymous memory.
775 1.29 thorpej */
776 1.50 chs
777 1.29 thorpej return (EINVAL);
778 1.29 thorpej
779 1.29 thorpej default:
780 1.21 mrg return (EINVAL);
781 1.29 thorpej }
782 1.29 thorpej
783 1.50 chs return error;
784 1.1 mrg }
785 1.1 mrg
786 1.1 mrg /*
787 1.1 mrg * sys_mlock: memory lock
788 1.1 mrg */
789 1.1 mrg
790 1.6 mrg int
791 1.119 dsl sys_mlock(struct lwp *l, const struct sys_mlock_args *uap, register_t *retval)
792 1.6 mrg {
793 1.119 dsl /* {
794 1.10 kleink syscallarg(const void *) addr;
795 1.6 mrg syscallarg(size_t) len;
796 1.119 dsl } */
797 1.67 thorpej struct proc *p = l->l_proc;
798 1.12 eeh vaddr_t addr;
799 1.12 eeh vsize_t size, pageoff;
800 1.6 mrg int error;
801 1.6 mrg
802 1.6 mrg /*
803 1.6 mrg * extract syscall args from uap
804 1.6 mrg */
805 1.50 chs
806 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
807 1.12 eeh size = (vsize_t)SCARG(uap, len);
808 1.6 mrg
809 1.6 mrg /*
810 1.6 mrg * align the address to a page boundary and adjust the size accordingly
811 1.6 mrg */
812 1.50 chs
813 1.6 mrg pageoff = (addr & PAGE_MASK);
814 1.6 mrg addr -= pageoff;
815 1.6 mrg size += pageoff;
816 1.50 chs size = (vsize_t)round_page(size);
817 1.51 chs
818 1.115 yamt error = range_test(addr, size, false);
819 1.115 yamt if (error)
820 1.115 yamt return error;
821 1.1 mrg
822 1.6 mrg if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
823 1.6 mrg return (EAGAIN);
824 1.1 mrg
825 1.6 mrg if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
826 1.6 mrg p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
827 1.6 mrg return (EAGAIN);
828 1.1 mrg
829 1.107 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, false,
830 1.35 thorpej 0);
831 1.85 briggs if (error == EFAULT)
832 1.85 briggs error = ENOMEM;
833 1.50 chs return error;
834 1.1 mrg }
835 1.1 mrg
836 1.1 mrg /*
837 1.1 mrg * sys_munlock: unlock wired pages
838 1.1 mrg */
839 1.1 mrg
840 1.6 mrg int
841 1.129 yamt sys_munlock(struct lwp *l, const struct sys_munlock_args *uap,
842 1.129 yamt register_t *retval)
843 1.6 mrg {
844 1.119 dsl /* {
845 1.10 kleink syscallarg(const void *) addr;
846 1.6 mrg syscallarg(size_t) len;
847 1.119 dsl } */
848 1.67 thorpej struct proc *p = l->l_proc;
849 1.12 eeh vaddr_t addr;
850 1.12 eeh vsize_t size, pageoff;
851 1.6 mrg int error;
852 1.6 mrg
853 1.6 mrg /*
854 1.6 mrg * extract syscall args from uap
855 1.6 mrg */
856 1.6 mrg
857 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
858 1.12 eeh size = (vsize_t)SCARG(uap, len);
859 1.6 mrg
860 1.6 mrg /*
861 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
862 1.6 mrg */
863 1.50 chs
864 1.6 mrg pageoff = (addr & PAGE_MASK);
865 1.6 mrg addr -= pageoff;
866 1.6 mrg size += pageoff;
867 1.50 chs size = (vsize_t)round_page(size);
868 1.6 mrg
869 1.115 yamt error = range_test(addr, size, false);
870 1.115 yamt if (error)
871 1.115 yamt return error;
872 1.1 mrg
873 1.107 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, true,
874 1.35 thorpej 0);
875 1.85 briggs if (error == EFAULT)
876 1.85 briggs error = ENOMEM;
877 1.50 chs return error;
878 1.22 thorpej }
879 1.22 thorpej
880 1.22 thorpej /*
881 1.22 thorpej * sys_mlockall: lock all pages mapped into an address space.
882 1.22 thorpej */
883 1.22 thorpej
884 1.22 thorpej int
885 1.129 yamt sys_mlockall(struct lwp *l, const struct sys_mlockall_args *uap,
886 1.129 yamt register_t *retval)
887 1.22 thorpej {
888 1.119 dsl /* {
889 1.22 thorpej syscallarg(int) flags;
890 1.119 dsl } */
891 1.67 thorpej struct proc *p = l->l_proc;
892 1.22 thorpej int error, flags;
893 1.22 thorpej
894 1.22 thorpej flags = SCARG(uap, flags);
895 1.22 thorpej
896 1.22 thorpej if (flags == 0 ||
897 1.22 thorpej (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
898 1.22 thorpej return (EINVAL);
899 1.22 thorpej
900 1.25 thorpej error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
901 1.25 thorpej p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
902 1.22 thorpej return (error);
903 1.22 thorpej }
904 1.22 thorpej
905 1.22 thorpej /*
906 1.22 thorpej * sys_munlockall: unlock all pages mapped into an address space.
907 1.22 thorpej */
908 1.22 thorpej
909 1.22 thorpej int
910 1.119 dsl sys_munlockall(struct lwp *l, const void *v, register_t *retval)
911 1.22 thorpej {
912 1.67 thorpej struct proc *p = l->l_proc;
913 1.22 thorpej
914 1.22 thorpej (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
915 1.22 thorpej return (0);
916 1.1 mrg }
917 1.1 mrg
918 1.1 mrg /*
919 1.1 mrg * uvm_mmap: internal version of mmap
920 1.1 mrg *
921 1.56 chs * - used by sys_mmap and various framebuffers
922 1.150 chs * - uobj is a struct uvm_object pointer or NULL for MAP_ANON
923 1.1 mrg * - caller must page-align the file offset
924 1.1 mrg */
925 1.1 mrg
926 1.6 mrg int
927 1.129 yamt uvm_mmap(struct vm_map *map, vaddr_t *addr, vsize_t size, vm_prot_t prot,
928 1.150 chs vm_prot_t maxprot, int flags, int advice, struct uvm_object *uobj,
929 1.150 chs voff_t foff, vsize_t locklimit)
930 1.6 mrg {
931 1.70 matt vaddr_t align = 0;
932 1.50 chs int error;
933 1.6 mrg uvm_flag_t uvmflag = 0;
934 1.6 mrg
935 1.6 mrg /*
936 1.6 mrg * check params
937 1.6 mrg */
938 1.6 mrg
939 1.6 mrg if (size == 0)
940 1.6 mrg return(0);
941 1.6 mrg if (foff & PAGE_MASK)
942 1.6 mrg return(EINVAL);
943 1.6 mrg if ((prot & maxprot) != prot)
944 1.6 mrg return(EINVAL);
945 1.6 mrg
946 1.6 mrg /*
947 1.6 mrg * for non-fixed mappings, round off the suggested address.
948 1.6 mrg * for fixed mappings, check alignment and zap old mappings.
949 1.6 mrg */
950 1.6 mrg
951 1.6 mrg if ((flags & MAP_FIXED) == 0) {
952 1.56 chs *addr = round_page(*addr);
953 1.6 mrg } else {
954 1.6 mrg if (*addr & PAGE_MASK)
955 1.6 mrg return(EINVAL);
956 1.6 mrg uvmflag |= UVM_FLAG_FIXED;
957 1.56 chs (void) uvm_unmap(map, *addr, *addr + size);
958 1.6 mrg }
959 1.6 mrg
960 1.6 mrg /*
961 1.70 matt * Try to see if any requested alignment can even be attemped.
962 1.70 matt * Make sure we can express the alignment (asking for a >= 4GB
963 1.70 matt * alignment on an ILP32 architecure make no sense) and the
964 1.70 matt * alignment is at least for a page sized quanitiy. If the
965 1.70 matt * request was for a fixed mapping, make sure supplied address
966 1.70 matt * adheres to the request alignment.
967 1.70 matt */
968 1.70 matt align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
969 1.70 matt if (align) {
970 1.70 matt if (align >= sizeof(vaddr_t) * NBBY)
971 1.70 matt return(EINVAL);
972 1.70 matt align = 1L << align;
973 1.70 matt if (align < PAGE_SIZE)
974 1.70 matt return(EINVAL);
975 1.88 chs if (align >= vm_map_max(map))
976 1.70 matt return(ENOMEM);
977 1.70 matt if (flags & MAP_FIXED) {
978 1.70 matt if ((*addr & (align-1)) != 0)
979 1.70 matt return(EINVAL);
980 1.70 matt align = 0;
981 1.70 matt }
982 1.70 matt }
983 1.70 matt
984 1.70 matt /*
985 1.128 mrg * check resource limits
986 1.128 mrg */
987 1.128 mrg
988 1.128 mrg if (!VM_MAP_IS_KERNEL(map) &&
989 1.128 mrg (((rlim_t)curproc->p_vmspace->vm_map.size + (rlim_t)size) >
990 1.128 mrg curproc->p_rlimit[RLIMIT_AS].rlim_cur))
991 1.128 mrg return ENOMEM;
992 1.128 mrg
993 1.128 mrg /*
994 1.6 mrg * handle anon vs. non-anon mappings. for non-anon mappings attach
995 1.6 mrg * to underlying vm object.
996 1.6 mrg */
997 1.6 mrg
998 1.6 mrg if (flags & MAP_ANON) {
999 1.150 chs KASSERT(uobj == NULL);
1000 1.36 thorpej foff = UVM_UNKNOWN_OFFSET;
1001 1.6 mrg if ((flags & MAP_SHARED) == 0)
1002 1.6 mrg /* XXX: defer amap create */
1003 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1004 1.6 mrg else
1005 1.6 mrg /* shared: create amap now */
1006 1.6 mrg uvmflag |= UVM_FLAG_OVERLAY;
1007 1.6 mrg
1008 1.6 mrg } else {
1009 1.150 chs KASSERT(uobj != NULL);
1010 1.92 yamt if ((flags & MAP_SHARED) == 0) {
1011 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1012 1.100 chs }
1013 1.6 mrg }
1014 1.6 mrg
1015 1.51 chs uvmflag = UVM_MAPFLAG(prot, maxprot,
1016 1.1 mrg (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
1017 1.1 mrg advice, uvmflag);
1018 1.70 matt error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
1019 1.50 chs if (error) {
1020 1.50 chs if (uobj)
1021 1.50 chs uobj->pgops->pgo_detach(uobj);
1022 1.50 chs return error;
1023 1.50 chs }
1024 1.1 mrg
1025 1.6 mrg /*
1026 1.50 chs * POSIX 1003.1b -- if our address space was configured
1027 1.50 chs * to lock all future mappings, wire the one we just made.
1028 1.78 thorpej *
1029 1.78 thorpej * Also handle the MAP_WIRED flag here.
1030 1.6 mrg */
1031 1.6 mrg
1032 1.50 chs if (prot == VM_PROT_NONE) {
1033 1.6 mrg
1034 1.25 thorpej /*
1035 1.50 chs * No more work to do in this case.
1036 1.25 thorpej */
1037 1.25 thorpej
1038 1.50 chs return (0);
1039 1.50 chs }
1040 1.78 thorpej if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
1041 1.126 ad vm_map_lock(map);
1042 1.87 chs if (atop(size) + uvmexp.wired > uvmexp.wiredmax ||
1043 1.87 chs (locklimit != 0 &&
1044 1.87 chs size + ptoa(pmap_wired_count(vm_map_pmap(map))) >
1045 1.87 chs locklimit)) {
1046 1.50 chs vm_map_unlock(map);
1047 1.50 chs uvm_unmap(map, *addr, *addr + size);
1048 1.50 chs return ENOMEM;
1049 1.25 thorpej }
1050 1.25 thorpej
1051 1.50 chs /*
1052 1.50 chs * uvm_map_pageable() always returns the map unlocked.
1053 1.50 chs */
1054 1.25 thorpej
1055 1.50 chs error = uvm_map_pageable(map, *addr, *addr + size,
1056 1.107 thorpej false, UVM_LK_ENTER);
1057 1.50 chs if (error) {
1058 1.50 chs uvm_unmap(map, *addr, *addr + size);
1059 1.50 chs return error;
1060 1.50 chs }
1061 1.25 thorpej return (0);
1062 1.25 thorpej }
1063 1.50 chs return 0;
1064 1.1 mrg }
1065 1.89 fvdl
1066 1.89 fvdl vaddr_t
1067 1.102 yamt uvm_default_mapaddr(struct proc *p, vaddr_t base, vsize_t sz)
1068 1.89 fvdl {
1069 1.102 yamt
1070 1.146 christos if (p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN)
1071 1.146 christos return VM_DEFAULT_ADDRESS_TOPDOWN(base, sz);
1072 1.146 christos else
1073 1.146 christos return VM_DEFAULT_ADDRESS_BOTTOMUP(base, sz);
1074 1.89 fvdl }
1075 1.150 chs
1076 1.150 chs int
1077 1.150 chs uvm_mmap_dev(struct proc *p, void **addrp, size_t len, dev_t dev,
1078 1.150 chs off_t off)
1079 1.150 chs {
1080 1.150 chs struct uvm_object *uobj;
1081 1.150 chs int error, flags, prot;
1082 1.150 chs
1083 1.150 chs flags = MAP_SHARED;
1084 1.150 chs prot = VM_PROT_READ | VM_PROT_WRITE;
1085 1.150 chs if (*addrp)
1086 1.150 chs flags |= MAP_FIXED;
1087 1.150 chs else
1088 1.150 chs *addrp = (void *)p->p_emul->e_vm_default_addr(p,
1089 1.150 chs (vaddr_t)p->p_vmspace->vm_daddr, len);
1090 1.150 chs
1091 1.151 chs uobj = udv_attach(dev, prot, off, len);
1092 1.150 chs if (uobj == NULL)
1093 1.150 chs return EINVAL;
1094 1.150 chs
1095 1.150 chs error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
1096 1.150 chs (vsize_t)len, prot, prot, flags, UVM_ADV_RANDOM,
1097 1.151 chs uobj, off, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1098 1.150 chs return error;
1099 1.150 chs }
1100 1.150 chs
1101 1.150 chs int
1102 1.150 chs uvm_mmap_anon(struct proc *p, void **addrp, size_t len)
1103 1.150 chs {
1104 1.150 chs int error, flags, prot;
1105 1.150 chs
1106 1.150 chs flags = MAP_PRIVATE | MAP_ANON;
1107 1.150 chs prot = VM_PROT_READ | VM_PROT_WRITE;
1108 1.150 chs if (*addrp)
1109 1.150 chs flags |= MAP_FIXED;
1110 1.150 chs else
1111 1.150 chs *addrp = (void *)p->p_emul->e_vm_default_addr(p,
1112 1.150 chs (vaddr_t)p->p_vmspace->vm_daddr, len);
1113 1.150 chs
1114 1.150 chs error = uvm_mmap(&p->p_vmspace->vm_map, (vaddr_t *)addrp,
1115 1.150 chs (vsize_t)len, prot, prot, flags, UVM_ADV_NORMAL,
1116 1.150 chs NULL, 0, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1117 1.150 chs return error;
1118 1.150 chs }
1119