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