uvm_mmap.c revision 1.80 1 1.80 jdolecek /* $NetBSD: uvm_mmap.c,v 1.80 2004/02/14 12:20:14 jdolecek 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.1 mrg * 3. All advertising materials mentioning features or use of this software
23 1.1 mrg * must display the following acknowledgement:
24 1.1 mrg * This product includes software developed by the Charles D. Cranor,
25 1.51 chs * Washington University, University of California, Berkeley and
26 1.1 mrg * its contributors.
27 1.1 mrg * 4. Neither the name of the University nor the names of its contributors
28 1.1 mrg * may be used to endorse or promote products derived from this software
29 1.1 mrg * without specific prior written permission.
30 1.1 mrg *
31 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 1.1 mrg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 1.1 mrg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 1.1 mrg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 1.1 mrg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1 mrg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1 mrg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1 mrg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 1.1 mrg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 1.1 mrg * SUCH DAMAGE.
42 1.1 mrg *
43 1.1 mrg * from: Utah $Hdr: vm_mmap.c 1.6 91/10/21$
44 1.1 mrg * @(#)vm_mmap.c 8.5 (Berkeley) 5/19/94
45 1.3 mrg * from: Id: uvm_mmap.c,v 1.1.2.14 1998/01/05 21:04:26 chuck Exp
46 1.1 mrg */
47 1.1 mrg
48 1.1 mrg /*
49 1.1 mrg * uvm_mmap.c: system call interface into VM system, plus kernel vm_mmap
50 1.1 mrg * function.
51 1.1 mrg */
52 1.60 lukem
53 1.60 lukem #include <sys/cdefs.h>
54 1.80 jdolecek __KERNEL_RCSID(0, "$NetBSD: uvm_mmap.c,v 1.80 2004/02/14 12:20:14 jdolecek Exp $");
55 1.80 jdolecek
56 1.80 jdolecek #include "opt_compat_netbsd.h"
57 1.60 lukem
58 1.1 mrg #include <sys/param.h>
59 1.1 mrg #include <sys/systm.h>
60 1.1 mrg #include <sys/file.h>
61 1.1 mrg #include <sys/filedesc.h>
62 1.1 mrg #include <sys/resourcevar.h>
63 1.1 mrg #include <sys/mman.h>
64 1.1 mrg #include <sys/mount.h>
65 1.1 mrg #include <sys/proc.h>
66 1.1 mrg #include <sys/malloc.h>
67 1.1 mrg #include <sys/vnode.h>
68 1.1 mrg #include <sys/conf.h>
69 1.9 mrg #include <sys/stat.h>
70 1.1 mrg
71 1.1 mrg #include <miscfs/specfs/specdev.h>
72 1.1 mrg
73 1.67 thorpej #include <sys/sa.h>
74 1.1 mrg #include <sys/syscallargs.h>
75 1.1 mrg
76 1.1 mrg #include <uvm/uvm.h>
77 1.1 mrg #include <uvm/uvm_device.h>
78 1.1 mrg
79 1.80 jdolecek #ifndef COMPAT_ZERODEV
80 1.80 jdolecek #define COMPAT_ZERODEV (0)
81 1.80 jdolecek #endif
82 1.1 mrg
83 1.1 mrg /*
84 1.1 mrg * unimplemented VM system calls:
85 1.1 mrg */
86 1.1 mrg
87 1.1 mrg /*
88 1.1 mrg * sys_sbrk: sbrk system call.
89 1.1 mrg */
90 1.1 mrg
91 1.1 mrg /* ARGSUSED */
92 1.6 mrg int
93 1.67 thorpej sys_sbrk(l, v, retval)
94 1.67 thorpej struct lwp *l;
95 1.6 mrg void *v;
96 1.6 mrg register_t *retval;
97 1.1 mrg {
98 1.1 mrg #if 0
99 1.6 mrg struct sys_sbrk_args /* {
100 1.33 kleink syscallarg(intptr_t) incr;
101 1.20 mrg } */ *uap = v;
102 1.1 mrg #endif
103 1.6 mrg
104 1.17 kleink return (ENOSYS);
105 1.1 mrg }
106 1.1 mrg
107 1.1 mrg /*
108 1.1 mrg * sys_sstk: sstk system call.
109 1.1 mrg */
110 1.1 mrg
111 1.1 mrg /* ARGSUSED */
112 1.6 mrg int
113 1.67 thorpej sys_sstk(l, v, retval)
114 1.67 thorpej struct lwp *l;
115 1.6 mrg void *v;
116 1.6 mrg register_t *retval;
117 1.1 mrg {
118 1.1 mrg #if 0
119 1.6 mrg struct sys_sstk_args /* {
120 1.20 mrg syscallarg(int) incr;
121 1.20 mrg } */ *uap = v;
122 1.1 mrg #endif
123 1.6 mrg
124 1.17 kleink return (ENOSYS);
125 1.1 mrg }
126 1.1 mrg
127 1.1 mrg /*
128 1.1 mrg * sys_mincore: determine if pages are in core or not.
129 1.1 mrg */
130 1.1 mrg
131 1.1 mrg /* ARGSUSED */
132 1.6 mrg int
133 1.67 thorpej sys_mincore(l, v, retval)
134 1.67 thorpej struct lwp *l;
135 1.6 mrg void *v;
136 1.6 mrg register_t *retval;
137 1.1 mrg {
138 1.6 mrg struct sys_mincore_args /* {
139 1.22 thorpej syscallarg(void *) addr;
140 1.20 mrg syscallarg(size_t) len;
141 1.20 mrg syscallarg(char *) vec;
142 1.20 mrg } */ *uap = v;
143 1.67 thorpej struct proc *p = l->l_proc;
144 1.56 chs struct vm_page *pg;
145 1.22 thorpej char *vec, pgi;
146 1.22 thorpej struct uvm_object *uobj;
147 1.22 thorpej struct vm_amap *amap;
148 1.22 thorpej struct vm_anon *anon;
149 1.53 chs struct vm_map_entry *entry;
150 1.22 thorpej vaddr_t start, end, lim;
151 1.53 chs struct vm_map *map;
152 1.22 thorpej vsize_t len;
153 1.22 thorpej int error = 0, npgs;
154 1.22 thorpej
155 1.22 thorpej map = &p->p_vmspace->vm_map;
156 1.22 thorpej
157 1.22 thorpej start = (vaddr_t)SCARG(uap, addr);
158 1.22 thorpej len = SCARG(uap, len);
159 1.22 thorpej vec = SCARG(uap, vec);
160 1.22 thorpej
161 1.22 thorpej if (start & PAGE_MASK)
162 1.22 thorpej return (EINVAL);
163 1.22 thorpej len = round_page(len);
164 1.22 thorpej end = start + len;
165 1.22 thorpej if (end <= start)
166 1.22 thorpej return (EINVAL);
167 1.22 thorpej
168 1.22 thorpej /*
169 1.22 thorpej * Lock down vec, so our returned status isn't outdated by
170 1.22 thorpej * storing the status byte for a page.
171 1.22 thorpej */
172 1.50 chs
173 1.62 chs npgs = len >> PAGE_SHIFT;
174 1.62 chs error = uvm_vslock(p, vec, npgs, VM_PROT_WRITE);
175 1.62 chs if (error) {
176 1.62 chs return error;
177 1.62 chs }
178 1.22 thorpej vm_map_lock_read(map);
179 1.22 thorpej
180 1.22 thorpej if (uvm_map_lookup_entry(map, start, &entry) == FALSE) {
181 1.22 thorpej error = ENOMEM;
182 1.22 thorpej goto out;
183 1.22 thorpej }
184 1.22 thorpej
185 1.22 thorpej for (/* nothing */;
186 1.22 thorpej entry != &map->header && entry->start < end;
187 1.22 thorpej entry = entry->next) {
188 1.49 chs KASSERT(!UVM_ET_ISSUBMAP(entry));
189 1.49 chs KASSERT(start >= entry->start);
190 1.49 chs
191 1.22 thorpej /* Make sure there are no holes. */
192 1.22 thorpej if (entry->end < end &&
193 1.22 thorpej (entry->next == &map->header ||
194 1.22 thorpej entry->next->start > entry->end)) {
195 1.22 thorpej error = ENOMEM;
196 1.22 thorpej goto out;
197 1.22 thorpej }
198 1.6 mrg
199 1.22 thorpej lim = end < entry->end ? end : entry->end;
200 1.22 thorpej
201 1.22 thorpej /*
202 1.31 thorpej * Special case for objects with no "real" pages. Those
203 1.31 thorpej * are always considered resident (mapped devices).
204 1.22 thorpej */
205 1.50 chs
206 1.22 thorpej if (UVM_ET_ISOBJ(entry)) {
207 1.49 chs KASSERT(!UVM_OBJ_IS_KERN_OBJECT(entry->object.uvm_obj));
208 1.79 yamt if (UVM_OBJ_IS_DEVICE(entry->object.uvm_obj)) {
209 1.22 thorpej for (/* nothing */; start < lim;
210 1.22 thorpej start += PAGE_SIZE, vec++)
211 1.22 thorpej subyte(vec, 1);
212 1.22 thorpej continue;
213 1.22 thorpej }
214 1.22 thorpej }
215 1.22 thorpej
216 1.32 thorpej amap = entry->aref.ar_amap; /* top layer */
217 1.32 thorpej uobj = entry->object.uvm_obj; /* bottom layer */
218 1.22 thorpej
219 1.22 thorpej if (amap != NULL)
220 1.22 thorpej amap_lock(amap);
221 1.22 thorpej if (uobj != NULL)
222 1.22 thorpej simple_lock(&uobj->vmobjlock);
223 1.22 thorpej
224 1.22 thorpej for (/* nothing */; start < lim; start += PAGE_SIZE, vec++) {
225 1.22 thorpej pgi = 0;
226 1.22 thorpej if (amap != NULL) {
227 1.22 thorpej /* Check the top layer first. */
228 1.22 thorpej anon = amap_lookup(&entry->aref,
229 1.22 thorpej start - entry->start);
230 1.22 thorpej /* Don't need to lock anon here. */
231 1.22 thorpej if (anon != NULL && anon->u.an_page != NULL) {
232 1.50 chs
233 1.22 thorpej /*
234 1.22 thorpej * Anon has the page for this entry
235 1.22 thorpej * offset.
236 1.22 thorpej */
237 1.50 chs
238 1.22 thorpej pgi = 1;
239 1.22 thorpej }
240 1.22 thorpej }
241 1.22 thorpej if (uobj != NULL && pgi == 0) {
242 1.22 thorpej /* Check the bottom layer. */
243 1.56 chs pg = uvm_pagelookup(uobj,
244 1.22 thorpej entry->offset + (start - entry->start));
245 1.56 chs if (pg != NULL) {
246 1.50 chs
247 1.22 thorpej /*
248 1.22 thorpej * Object has the page for this entry
249 1.22 thorpej * offset.
250 1.22 thorpej */
251 1.50 chs
252 1.22 thorpej pgi = 1;
253 1.22 thorpej }
254 1.22 thorpej }
255 1.22 thorpej (void) subyte(vec, pgi);
256 1.22 thorpej }
257 1.22 thorpej if (uobj != NULL)
258 1.27 thorpej simple_unlock(&uobj->vmobjlock);
259 1.22 thorpej if (amap != NULL)
260 1.22 thorpej amap_unlock(amap);
261 1.22 thorpej }
262 1.22 thorpej
263 1.22 thorpej out:
264 1.22 thorpej vm_map_unlock_read(map);
265 1.22 thorpej uvm_vsunlock(p, SCARG(uap, vec), npgs);
266 1.22 thorpej return (error);
267 1.1 mrg }
268 1.1 mrg
269 1.1 mrg /*
270 1.1 mrg * sys_mmap: mmap system call.
271 1.1 mrg *
272 1.64 atatat * => file offset and address may not be page aligned
273 1.1 mrg * - if MAP_FIXED, offset and address must have remainder mod PAGE_SIZE
274 1.1 mrg * - if address isn't page aligned the mapping starts at trunc_page(addr)
275 1.1 mrg * and the return value is adjusted up by the page offset.
276 1.1 mrg */
277 1.1 mrg
278 1.6 mrg int
279 1.67 thorpej sys_mmap(l, v, retval)
280 1.67 thorpej struct lwp *l;
281 1.6 mrg void *v;
282 1.6 mrg register_t *retval;
283 1.6 mrg {
284 1.40 augustss struct sys_mmap_args /* {
285 1.6 mrg syscallarg(caddr_t) addr;
286 1.6 mrg syscallarg(size_t) len;
287 1.6 mrg syscallarg(int) prot;
288 1.6 mrg syscallarg(int) flags;
289 1.6 mrg syscallarg(int) fd;
290 1.6 mrg syscallarg(long) pad;
291 1.6 mrg syscallarg(off_t) pos;
292 1.6 mrg } */ *uap = v;
293 1.67 thorpej struct proc *p = l->l_proc;
294 1.12 eeh vaddr_t addr;
295 1.9 mrg struct vattr va;
296 1.6 mrg off_t pos;
297 1.12 eeh vsize_t size, pageoff;
298 1.6 mrg vm_prot_t prot, maxprot;
299 1.6 mrg int flags, fd;
300 1.12 eeh vaddr_t vm_min_address = VM_MIN_ADDRESS;
301 1.40 augustss struct filedesc *fdp = p->p_fd;
302 1.40 augustss struct file *fp;
303 1.6 mrg struct vnode *vp;
304 1.50 chs void *handle;
305 1.6 mrg int error;
306 1.6 mrg
307 1.6 mrg /*
308 1.6 mrg * first, extract syscall args from the uap.
309 1.6 mrg */
310 1.6 mrg
311 1.50 chs addr = (vaddr_t)SCARG(uap, addr);
312 1.50 chs size = (vsize_t)SCARG(uap, len);
313 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
314 1.6 mrg flags = SCARG(uap, flags);
315 1.6 mrg fd = SCARG(uap, fd);
316 1.6 mrg pos = SCARG(uap, pos);
317 1.6 mrg
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.24 thorpej if (flags & MAP_COPY)
323 1.24 thorpej flags = (flags & ~MAP_COPY) | MAP_PRIVATE;
324 1.24 thorpej if ((flags & (MAP_SHARED|MAP_PRIVATE)) == (MAP_SHARED|MAP_PRIVATE))
325 1.24 thorpej return (EINVAL);
326 1.24 thorpej
327 1.24 thorpej /*
328 1.6 mrg * align file position and save offset. adjust size.
329 1.6 mrg */
330 1.6 mrg
331 1.6 mrg pageoff = (pos & PAGE_MASK);
332 1.6 mrg pos -= pageoff;
333 1.6 mrg size += pageoff; /* add offset */
334 1.50 chs size = (vsize_t)round_page(size); /* round up */
335 1.6 mrg if ((ssize_t) size < 0)
336 1.6 mrg return (EINVAL); /* don't allow wrap */
337 1.6 mrg
338 1.78 thorpej #ifndef pmap_wired_count
339 1.78 thorpej /*
340 1.78 thorpej * if we're going to wire the mapping, restrict it to superuser.
341 1.78 thorpej */
342 1.78 thorpej
343 1.78 thorpej if ((flags & MAP_WIRED) != 0 &&
344 1.78 thorpej (error = suser(p->p_ucred, &p->p_acflag)) != 0)
345 1.78 thorpej return (error);
346 1.78 thorpej #endif
347 1.78 thorpej
348 1.6 mrg /*
349 1.51 chs * now check (MAP_FIXED) or get (!MAP_FIXED) the "addr"
350 1.6 mrg */
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.6 mrg if (VM_MAXUSER_ADDRESS > 0 &&
360 1.6 mrg (addr + size) > VM_MAXUSER_ADDRESS)
361 1.63 darrenr return (EFBIG);
362 1.6 mrg if (vm_min_address > 0 && addr < vm_min_address)
363 1.6 mrg return (EINVAL);
364 1.6 mrg if (addr > addr + size)
365 1.63 darrenr return (EOVERFLOW); /* no wrapping! */
366 1.6 mrg
367 1.75 christos } else if (addr == 0 || !(flags & MAP_TRYFIXED)) {
368 1.6 mrg
369 1.6 mrg /*
370 1.68 atatat * not fixed: make sure we skip over the largest
371 1.68 atatat * possible heap for non-topdown mapping arrangements.
372 1.68 atatat * we will refine our guess later (e.g. to account for
373 1.68 atatat * VAC, etc)
374 1.6 mrg */
375 1.46 chs
376 1.68 atatat if (addr == 0 ||
377 1.68 atatat !(p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN))
378 1.68 atatat addr = MAX(addr,
379 1.68 atatat VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size));
380 1.68 atatat else
381 1.68 atatat addr = MIN(addr,
382 1.68 atatat VM_DEFAULT_ADDRESS(p->p_vmspace->vm_daddr, size));
383 1.6 mrg }
384 1.6 mrg
385 1.6 mrg /*
386 1.6 mrg * check for file mappings (i.e. not anonymous) and verify file.
387 1.6 mrg */
388 1.6 mrg
389 1.6 mrg if ((flags & MAP_ANON) == 0) {
390 1.6 mrg
391 1.54 thorpej if ((fp = fd_getfile(fdp, fd)) == NULL)
392 1.54 thorpej return (EBADF);
393 1.69 pk
394 1.69 pk simple_unlock(&fp->f_slock);
395 1.6 mrg
396 1.6 mrg if (fp->f_type != DTYPE_VNODE)
397 1.7 kleink return (ENODEV); /* only mmap vnodes! */
398 1.6 mrg vp = (struct vnode *)fp->f_data; /* convert to vnode */
399 1.6 mrg
400 1.11 thorpej if (vp->v_type != VREG && vp->v_type != VCHR &&
401 1.11 thorpej vp->v_type != VBLK)
402 1.11 thorpej return (ENODEV); /* only REG/CHR/BLK support mmap */
403 1.39 kleink
404 1.61 chs if (vp->v_type != VCHR && pos < 0)
405 1.61 chs return (EINVAL);
406 1.61 chs
407 1.61 chs if (vp->v_type != VCHR && (pos + size) < pos)
408 1.39 kleink return (EOVERFLOW); /* no offset wrapping */
409 1.6 mrg
410 1.6 mrg /* special case: catch SunOS style /dev/zero */
411 1.80 jdolecek if (vp->v_type == VCHR
412 1.80 jdolecek && (vp->v_rdev == zerodev || COMPAT_ZERODEV(vp->v_rdev))) {
413 1.6 mrg flags |= MAP_ANON;
414 1.6 mrg goto is_anon;
415 1.6 mrg }
416 1.6 mrg
417 1.6 mrg /*
418 1.6 mrg * Old programs may not select a specific sharing type, so
419 1.6 mrg * default to an appropriate one.
420 1.6 mrg *
421 1.6 mrg * XXX: how does MAP_ANON fit in the picture?
422 1.6 mrg */
423 1.24 thorpej if ((flags & (MAP_SHARED|MAP_PRIVATE)) == 0) {
424 1.8 tv #if defined(DEBUG)
425 1.6 mrg printf("WARNING: defaulted mmap() share type to "
426 1.71 gmcgarry "%s (pid %d command %s)\n", vp->v_type == VCHR ?
427 1.6 mrg "MAP_SHARED" : "MAP_PRIVATE", p->p_pid,
428 1.6 mrg p->p_comm);
429 1.1 mrg #endif
430 1.6 mrg if (vp->v_type == VCHR)
431 1.6 mrg flags |= MAP_SHARED; /* for a device */
432 1.6 mrg else
433 1.6 mrg flags |= MAP_PRIVATE; /* for a file */
434 1.6 mrg }
435 1.6 mrg
436 1.51 chs /*
437 1.6 mrg * MAP_PRIVATE device mappings don't make sense (and aren't
438 1.6 mrg * supported anyway). However, some programs rely on this,
439 1.6 mrg * so just change it to MAP_SHARED.
440 1.6 mrg */
441 1.6 mrg if (vp->v_type == VCHR && (flags & MAP_PRIVATE) != 0) {
442 1.6 mrg flags = (flags & ~MAP_PRIVATE) | MAP_SHARED;
443 1.6 mrg }
444 1.1 mrg
445 1.6 mrg /*
446 1.6 mrg * now check protection
447 1.6 mrg */
448 1.6 mrg
449 1.48 thorpej maxprot = VM_PROT_EXECUTE;
450 1.6 mrg
451 1.6 mrg /* check read access */
452 1.6 mrg if (fp->f_flag & FREAD)
453 1.6 mrg maxprot |= VM_PROT_READ;
454 1.6 mrg else if (prot & PROT_READ)
455 1.6 mrg return (EACCES);
456 1.6 mrg
457 1.9 mrg /* check write access, shared case first */
458 1.6 mrg if (flags & MAP_SHARED) {
459 1.9 mrg /*
460 1.9 mrg * if the file is writable, only add PROT_WRITE to
461 1.9 mrg * maxprot if the file is not immutable, append-only.
462 1.9 mrg * otherwise, if we have asked for PROT_WRITE, return
463 1.9 mrg * EPERM.
464 1.9 mrg */
465 1.9 mrg if (fp->f_flag & FWRITE) {
466 1.9 mrg if ((error =
467 1.74 fvdl VOP_GETATTR(vp, &va, p->p_ucred, p)))
468 1.9 mrg return (error);
469 1.9 mrg if ((va.va_flags & (IMMUTABLE|APPEND)) == 0)
470 1.9 mrg maxprot |= VM_PROT_WRITE;
471 1.9 mrg else if (prot & PROT_WRITE)
472 1.9 mrg return (EPERM);
473 1.9 mrg }
474 1.6 mrg else if (prot & PROT_WRITE)
475 1.6 mrg return (EACCES);
476 1.6 mrg } else {
477 1.6 mrg /* MAP_PRIVATE mappings can always write to */
478 1.6 mrg maxprot |= VM_PROT_WRITE;
479 1.6 mrg }
480 1.50 chs handle = vp;
481 1.1 mrg
482 1.6 mrg } else { /* MAP_ANON case */
483 1.24 thorpej /*
484 1.24 thorpej * XXX What do we do about (MAP_SHARED|MAP_PRIVATE) == 0?
485 1.24 thorpej */
486 1.6 mrg if (fd != -1)
487 1.6 mrg return (EINVAL);
488 1.1 mrg
489 1.24 thorpej is_anon: /* label for SunOS style /dev/zero */
490 1.6 mrg handle = NULL;
491 1.6 mrg maxprot = VM_PROT_ALL;
492 1.6 mrg pos = 0;
493 1.28 cgd }
494 1.28 cgd
495 1.28 cgd /*
496 1.28 cgd * XXX (in)sanity check. We don't do proper datasize checking
497 1.28 cgd * XXX for anonymous (or private writable) mmap(). However,
498 1.28 cgd * XXX know that if we're trying to allocate more than the amount
499 1.28 cgd * XXX remaining under our current data size limit, _that_ should
500 1.28 cgd * XXX be disallowed.
501 1.28 cgd */
502 1.28 cgd if ((flags & MAP_ANON) != 0 ||
503 1.28 cgd ((flags & MAP_PRIVATE) != 0 && (prot & PROT_WRITE) != 0)) {
504 1.28 cgd if (size >
505 1.50 chs (p->p_rlimit[RLIMIT_DATA].rlim_cur -
506 1.50 chs ctob(p->p_vmspace->vm_dsize))) {
507 1.28 cgd return (ENOMEM);
508 1.28 cgd }
509 1.6 mrg }
510 1.6 mrg
511 1.6 mrg /*
512 1.6 mrg * now let kernel internal function uvm_mmap do the work.
513 1.6 mrg */
514 1.6 mrg
515 1.6 mrg error = uvm_mmap(&p->p_vmspace->vm_map, &addr, size, prot, maxprot,
516 1.25 thorpej flags, handle, pos, p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
517 1.6 mrg
518 1.6 mrg if (error == 0)
519 1.6 mrg /* remember to add offset */
520 1.6 mrg *retval = (register_t)(addr + pageoff);
521 1.1 mrg
522 1.6 mrg return (error);
523 1.1 mrg }
524 1.1 mrg
525 1.1 mrg /*
526 1.1 mrg * sys___msync13: the msync system call (a front-end for flush)
527 1.1 mrg */
528 1.1 mrg
529 1.6 mrg int
530 1.67 thorpej sys___msync13(l, v, retval)
531 1.67 thorpej struct lwp *l;
532 1.6 mrg void *v;
533 1.6 mrg register_t *retval;
534 1.6 mrg {
535 1.6 mrg struct sys___msync13_args /* {
536 1.6 mrg syscallarg(caddr_t) addr;
537 1.6 mrg syscallarg(size_t) len;
538 1.6 mrg syscallarg(int) flags;
539 1.6 mrg } */ *uap = v;
540 1.67 thorpej struct proc *p = l->l_proc;
541 1.12 eeh vaddr_t addr;
542 1.12 eeh vsize_t size, pageoff;
543 1.53 chs struct vm_map *map;
544 1.50 chs int error, rv, flags, uvmflags;
545 1.6 mrg
546 1.6 mrg /*
547 1.6 mrg * extract syscall args from the uap
548 1.6 mrg */
549 1.6 mrg
550 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
551 1.12 eeh size = (vsize_t)SCARG(uap, len);
552 1.6 mrg flags = SCARG(uap, flags);
553 1.6 mrg
554 1.6 mrg /* sanity check flags */
555 1.6 mrg if ((flags & ~(MS_ASYNC | MS_SYNC | MS_INVALIDATE)) != 0 ||
556 1.77 chs (flags & (MS_ASYNC | MS_SYNC | MS_INVALIDATE)) == 0 ||
557 1.77 chs (flags & (MS_ASYNC | MS_SYNC)) == (MS_ASYNC | MS_SYNC))
558 1.77 chs return (EINVAL);
559 1.6 mrg if ((flags & (MS_ASYNC | MS_SYNC)) == 0)
560 1.77 chs flags |= MS_SYNC;
561 1.1 mrg
562 1.6 mrg /*
563 1.50 chs * align the address to a page boundary and adjust the size accordingly.
564 1.6 mrg */
565 1.6 mrg
566 1.6 mrg pageoff = (addr & PAGE_MASK);
567 1.6 mrg addr -= pageoff;
568 1.6 mrg size += pageoff;
569 1.50 chs size = (vsize_t)round_page(size);
570 1.6 mrg
571 1.6 mrg /* disallow wrap-around. */
572 1.6 mrg if (addr + size < addr)
573 1.6 mrg return (EINVAL);
574 1.6 mrg
575 1.6 mrg /*
576 1.6 mrg * get map
577 1.6 mrg */
578 1.6 mrg
579 1.6 mrg map = &p->p_vmspace->vm_map;
580 1.6 mrg
581 1.6 mrg /*
582 1.6 mrg * XXXCDC: do we really need this semantic?
583 1.6 mrg *
584 1.6 mrg * XXX Gak! If size is zero we are supposed to sync "all modified
585 1.6 mrg * pages with the region containing addr". Unfortunately, we
586 1.6 mrg * don't really keep track of individual mmaps so we approximate
587 1.6 mrg * by flushing the range of the map entry containing addr.
588 1.6 mrg * This can be incorrect if the region splits or is coalesced
589 1.6 mrg * with a neighbor.
590 1.6 mrg */
591 1.50 chs
592 1.6 mrg if (size == 0) {
593 1.53 chs struct vm_map_entry *entry;
594 1.51 chs
595 1.6 mrg vm_map_lock_read(map);
596 1.6 mrg rv = uvm_map_lookup_entry(map, addr, &entry);
597 1.6 mrg if (rv == TRUE) {
598 1.6 mrg addr = entry->start;
599 1.6 mrg size = entry->end - entry->start;
600 1.6 mrg }
601 1.6 mrg vm_map_unlock_read(map);
602 1.6 mrg if (rv == FALSE)
603 1.6 mrg return (EINVAL);
604 1.6 mrg }
605 1.6 mrg
606 1.6 mrg /*
607 1.6 mrg * translate MS_ flags into PGO_ flags
608 1.6 mrg */
609 1.50 chs
610 1.34 thorpej uvmflags = PGO_CLEANIT;
611 1.34 thorpej if (flags & MS_INVALIDATE)
612 1.34 thorpej uvmflags |= PGO_FREE;
613 1.6 mrg if (flags & MS_SYNC)
614 1.6 mrg uvmflags |= PGO_SYNCIO;
615 1.6 mrg else
616 1.6 mrg uvmflags |= PGO_SYNCIO; /* XXXCDC: force sync for now! */
617 1.6 mrg
618 1.50 chs error = uvm_map_clean(map, addr, addr+size, uvmflags);
619 1.50 chs return error;
620 1.1 mrg }
621 1.1 mrg
622 1.1 mrg /*
623 1.1 mrg * sys_munmap: unmap a users memory
624 1.1 mrg */
625 1.1 mrg
626 1.6 mrg int
627 1.67 thorpej sys_munmap(l, v, retval)
628 1.67 thorpej struct lwp *l;
629 1.6 mrg void *v;
630 1.6 mrg register_t *retval;
631 1.6 mrg {
632 1.40 augustss struct sys_munmap_args /* {
633 1.6 mrg syscallarg(caddr_t) addr;
634 1.6 mrg syscallarg(size_t) len;
635 1.6 mrg } */ *uap = v;
636 1.67 thorpej struct proc *p = l->l_proc;
637 1.12 eeh vaddr_t addr;
638 1.12 eeh vsize_t size, pageoff;
639 1.53 chs struct vm_map *map;
640 1.12 eeh vaddr_t vm_min_address = VM_MIN_ADDRESS;
641 1.6 mrg struct vm_map_entry *dead_entries;
642 1.6 mrg
643 1.6 mrg /*
644 1.50 chs * get syscall args.
645 1.6 mrg */
646 1.6 mrg
647 1.50 chs addr = (vaddr_t)SCARG(uap, addr);
648 1.50 chs size = (vsize_t)SCARG(uap, len);
649 1.51 chs
650 1.6 mrg /*
651 1.50 chs * align the address to a page boundary and adjust the size accordingly.
652 1.6 mrg */
653 1.6 mrg
654 1.6 mrg pageoff = (addr & PAGE_MASK);
655 1.6 mrg addr -= pageoff;
656 1.6 mrg size += pageoff;
657 1.50 chs size = (vsize_t)round_page(size);
658 1.6 mrg
659 1.6 mrg if ((int)size < 0)
660 1.6 mrg return (EINVAL);
661 1.6 mrg if (size == 0)
662 1.6 mrg return (0);
663 1.6 mrg
664 1.6 mrg /*
665 1.6 mrg * Check for illegal addresses. Watch out for address wrap...
666 1.6 mrg * Note that VM_*_ADDRESS are not constants due to casts (argh).
667 1.6 mrg */
668 1.6 mrg if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
669 1.6 mrg return (EINVAL);
670 1.6 mrg if (vm_min_address > 0 && addr < vm_min_address)
671 1.6 mrg return (EINVAL);
672 1.6 mrg if (addr > addr + size)
673 1.6 mrg return (EINVAL);
674 1.6 mrg map = &p->p_vmspace->vm_map;
675 1.6 mrg
676 1.6 mrg /*
677 1.51 chs * interesting system call semantic: make sure entire range is
678 1.6 mrg * allocated before allowing an unmap.
679 1.6 mrg */
680 1.6 mrg
681 1.50 chs vm_map_lock(map);
682 1.66 mycroft #if 0
683 1.6 mrg if (!uvm_map_checkprot(map, addr, addr + size, VM_PROT_NONE)) {
684 1.6 mrg vm_map_unlock(map);
685 1.6 mrg return (EINVAL);
686 1.6 mrg }
687 1.66 mycroft #endif
688 1.50 chs uvm_unmap_remove(map, addr, addr + size, &dead_entries);
689 1.50 chs vm_map_unlock(map);
690 1.6 mrg if (dead_entries != NULL)
691 1.6 mrg uvm_unmap_detach(dead_entries, 0);
692 1.6 mrg return (0);
693 1.1 mrg }
694 1.1 mrg
695 1.1 mrg /*
696 1.1 mrg * sys_mprotect: the mprotect system call
697 1.1 mrg */
698 1.1 mrg
699 1.6 mrg int
700 1.67 thorpej sys_mprotect(l, v, retval)
701 1.67 thorpej struct lwp *l;
702 1.6 mrg void *v;
703 1.6 mrg register_t *retval;
704 1.6 mrg {
705 1.6 mrg struct sys_mprotect_args /* {
706 1.6 mrg syscallarg(caddr_t) addr;
707 1.76 chs syscallarg(size_t) len;
708 1.6 mrg syscallarg(int) prot;
709 1.6 mrg } */ *uap = v;
710 1.67 thorpej struct proc *p = l->l_proc;
711 1.12 eeh vaddr_t addr;
712 1.12 eeh vsize_t size, pageoff;
713 1.6 mrg vm_prot_t prot;
714 1.50 chs int error;
715 1.6 mrg
716 1.6 mrg /*
717 1.6 mrg * extract syscall args from uap
718 1.6 mrg */
719 1.6 mrg
720 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
721 1.12 eeh size = (vsize_t)SCARG(uap, len);
722 1.6 mrg prot = SCARG(uap, prot) & VM_PROT_ALL;
723 1.6 mrg
724 1.6 mrg /*
725 1.50 chs * align the address to a page boundary and adjust the size accordingly.
726 1.6 mrg */
727 1.50 chs
728 1.6 mrg pageoff = (addr & PAGE_MASK);
729 1.6 mrg addr -= pageoff;
730 1.6 mrg size += pageoff;
731 1.76 chs size = round_page(size);
732 1.50 chs
733 1.50 chs error = uvm_map_protect(&p->p_vmspace->vm_map, addr, addr + size, prot,
734 1.50 chs FALSE);
735 1.50 chs return error;
736 1.1 mrg }
737 1.1 mrg
738 1.1 mrg /*
739 1.1 mrg * sys_minherit: the minherit system call
740 1.1 mrg */
741 1.1 mrg
742 1.6 mrg int
743 1.67 thorpej sys_minherit(l, v, retval)
744 1.67 thorpej struct lwp *l;
745 1.6 mrg void *v;
746 1.6 mrg register_t *retval;
747 1.6 mrg {
748 1.6 mrg struct sys_minherit_args /* {
749 1.6 mrg syscallarg(caddr_t) addr;
750 1.6 mrg syscallarg(int) len;
751 1.6 mrg syscallarg(int) inherit;
752 1.6 mrg } */ *uap = v;
753 1.67 thorpej struct proc *p = l->l_proc;
754 1.12 eeh vaddr_t addr;
755 1.12 eeh vsize_t size, pageoff;
756 1.40 augustss vm_inherit_t inherit;
757 1.50 chs int error;
758 1.51 chs
759 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
760 1.12 eeh size = (vsize_t)SCARG(uap, len);
761 1.6 mrg inherit = SCARG(uap, inherit);
762 1.50 chs
763 1.6 mrg /*
764 1.50 chs * align the address to a page boundary and adjust the size accordingly.
765 1.6 mrg */
766 1.6 mrg
767 1.6 mrg pageoff = (addr & PAGE_MASK);
768 1.6 mrg addr -= pageoff;
769 1.6 mrg size += pageoff;
770 1.50 chs size = (vsize_t)round_page(size);
771 1.6 mrg
772 1.6 mrg if ((int)size < 0)
773 1.6 mrg return (EINVAL);
774 1.50 chs error = uvm_map_inherit(&p->p_vmspace->vm_map, addr, addr + size,
775 1.50 chs inherit);
776 1.50 chs return error;
777 1.21 mrg }
778 1.21 mrg
779 1.21 mrg /*
780 1.21 mrg * sys_madvise: give advice about memory usage.
781 1.21 mrg */
782 1.21 mrg
783 1.21 mrg /* ARGSUSED */
784 1.21 mrg int
785 1.67 thorpej sys_madvise(l, v, retval)
786 1.67 thorpej struct lwp *l;
787 1.21 mrg void *v;
788 1.21 mrg register_t *retval;
789 1.21 mrg {
790 1.21 mrg struct sys_madvise_args /* {
791 1.21 mrg syscallarg(caddr_t) addr;
792 1.21 mrg syscallarg(size_t) len;
793 1.21 mrg syscallarg(int) behav;
794 1.21 mrg } */ *uap = v;
795 1.67 thorpej struct proc *p = l->l_proc;
796 1.21 mrg vaddr_t addr;
797 1.21 mrg vsize_t size, pageoff;
798 1.50 chs int advice, error;
799 1.51 chs
800 1.21 mrg addr = (vaddr_t)SCARG(uap, addr);
801 1.21 mrg size = (vsize_t)SCARG(uap, len);
802 1.21 mrg advice = SCARG(uap, behav);
803 1.21 mrg
804 1.21 mrg /*
805 1.21 mrg * align the address to a page boundary, and adjust the size accordingly
806 1.21 mrg */
807 1.50 chs
808 1.21 mrg pageoff = (addr & PAGE_MASK);
809 1.21 mrg addr -= pageoff;
810 1.21 mrg size += pageoff;
811 1.50 chs size = (vsize_t)round_page(size);
812 1.21 mrg
813 1.29 thorpej if ((ssize_t)size <= 0)
814 1.29 thorpej return (EINVAL);
815 1.29 thorpej
816 1.29 thorpej switch (advice) {
817 1.29 thorpej case MADV_NORMAL:
818 1.29 thorpej case MADV_RANDOM:
819 1.29 thorpej case MADV_SEQUENTIAL:
820 1.50 chs error = uvm_map_advice(&p->p_vmspace->vm_map, addr, addr + size,
821 1.29 thorpej advice);
822 1.29 thorpej break;
823 1.29 thorpej
824 1.29 thorpej case MADV_WILLNEED:
825 1.50 chs
826 1.29 thorpej /*
827 1.29 thorpej * Activate all these pages, pre-faulting them in if
828 1.29 thorpej * necessary.
829 1.29 thorpej */
830 1.29 thorpej /*
831 1.29 thorpej * XXX IMPLEMENT ME.
832 1.29 thorpej * Should invent a "weak" mode for uvm_fault()
833 1.29 thorpej * which would only do the PGO_LOCKED pgo_get().
834 1.29 thorpej */
835 1.50 chs
836 1.29 thorpej return (0);
837 1.29 thorpej
838 1.29 thorpej case MADV_DONTNEED:
839 1.50 chs
840 1.29 thorpej /*
841 1.29 thorpej * Deactivate all these pages. We don't need them
842 1.29 thorpej * any more. We don't, however, toss the data in
843 1.29 thorpej * the pages.
844 1.29 thorpej */
845 1.50 chs
846 1.50 chs error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
847 1.29 thorpej PGO_DEACTIVATE);
848 1.29 thorpej break;
849 1.29 thorpej
850 1.29 thorpej case MADV_FREE:
851 1.50 chs
852 1.29 thorpej /*
853 1.29 thorpej * These pages contain no valid data, and may be
854 1.45 soren * garbage-collected. Toss all resources, including
855 1.30 thorpej * any swap space in use.
856 1.29 thorpej */
857 1.50 chs
858 1.50 chs error = uvm_map_clean(&p->p_vmspace->vm_map, addr, addr + size,
859 1.29 thorpej PGO_FREE);
860 1.29 thorpej break;
861 1.29 thorpej
862 1.29 thorpej case MADV_SPACEAVAIL:
863 1.50 chs
864 1.29 thorpej /*
865 1.29 thorpej * XXXMRG What is this? I think it's:
866 1.29 thorpej *
867 1.29 thorpej * Ensure that we have allocated backing-store
868 1.29 thorpej * for these pages.
869 1.29 thorpej *
870 1.29 thorpej * This is going to require changes to the page daemon,
871 1.29 thorpej * as it will free swap space allocated to pages in core.
872 1.29 thorpej * There's also what to do for device/file/anonymous memory.
873 1.29 thorpej */
874 1.50 chs
875 1.29 thorpej return (EINVAL);
876 1.29 thorpej
877 1.29 thorpej default:
878 1.21 mrg return (EINVAL);
879 1.29 thorpej }
880 1.29 thorpej
881 1.50 chs return error;
882 1.1 mrg }
883 1.1 mrg
884 1.1 mrg /*
885 1.1 mrg * sys_mlock: memory lock
886 1.1 mrg */
887 1.1 mrg
888 1.6 mrg int
889 1.67 thorpej sys_mlock(l, v, retval)
890 1.67 thorpej struct lwp *l;
891 1.6 mrg void *v;
892 1.6 mrg register_t *retval;
893 1.6 mrg {
894 1.6 mrg struct sys_mlock_args /* {
895 1.10 kleink syscallarg(const void *) addr;
896 1.6 mrg syscallarg(size_t) len;
897 1.6 mrg } */ *uap = v;
898 1.67 thorpej struct proc *p = l->l_proc;
899 1.12 eeh vaddr_t addr;
900 1.12 eeh vsize_t size, pageoff;
901 1.6 mrg int error;
902 1.6 mrg
903 1.6 mrg /*
904 1.6 mrg * extract syscall args from uap
905 1.6 mrg */
906 1.50 chs
907 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
908 1.12 eeh size = (vsize_t)SCARG(uap, len);
909 1.6 mrg
910 1.6 mrg /*
911 1.6 mrg * align the address to a page boundary and adjust the size accordingly
912 1.6 mrg */
913 1.50 chs
914 1.6 mrg pageoff = (addr & PAGE_MASK);
915 1.6 mrg addr -= pageoff;
916 1.6 mrg size += pageoff;
917 1.50 chs size = (vsize_t)round_page(size);
918 1.51 chs
919 1.6 mrg /* disallow wrap-around. */
920 1.50 chs if (addr + size < addr)
921 1.6 mrg return (EINVAL);
922 1.1 mrg
923 1.6 mrg if (atop(size) + uvmexp.wired > uvmexp.wiredmax)
924 1.6 mrg return (EAGAIN);
925 1.1 mrg
926 1.1 mrg #ifdef pmap_wired_count
927 1.6 mrg if (size + ptoa(pmap_wired_count(vm_map_pmap(&p->p_vmspace->vm_map))) >
928 1.6 mrg p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur)
929 1.6 mrg return (EAGAIN);
930 1.1 mrg #else
931 1.6 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
932 1.6 mrg return (error);
933 1.1 mrg #endif
934 1.1 mrg
935 1.25 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, FALSE,
936 1.35 thorpej 0);
937 1.50 chs return error;
938 1.1 mrg }
939 1.1 mrg
940 1.1 mrg /*
941 1.1 mrg * sys_munlock: unlock wired pages
942 1.1 mrg */
943 1.1 mrg
944 1.6 mrg int
945 1.67 thorpej sys_munlock(l, v, retval)
946 1.67 thorpej struct lwp *l;
947 1.6 mrg void *v;
948 1.6 mrg register_t *retval;
949 1.6 mrg {
950 1.6 mrg struct sys_munlock_args /* {
951 1.10 kleink syscallarg(const void *) addr;
952 1.6 mrg syscallarg(size_t) len;
953 1.6 mrg } */ *uap = v;
954 1.67 thorpej struct proc *p = l->l_proc;
955 1.12 eeh vaddr_t addr;
956 1.12 eeh vsize_t size, pageoff;
957 1.6 mrg int error;
958 1.6 mrg
959 1.6 mrg /*
960 1.6 mrg * extract syscall args from uap
961 1.6 mrg */
962 1.6 mrg
963 1.12 eeh addr = (vaddr_t)SCARG(uap, addr);
964 1.12 eeh size = (vsize_t)SCARG(uap, len);
965 1.6 mrg
966 1.6 mrg /*
967 1.6 mrg * align the address to a page boundary, and adjust the size accordingly
968 1.6 mrg */
969 1.50 chs
970 1.6 mrg pageoff = (addr & PAGE_MASK);
971 1.6 mrg addr -= pageoff;
972 1.6 mrg size += pageoff;
973 1.50 chs size = (vsize_t)round_page(size);
974 1.6 mrg
975 1.6 mrg /* disallow wrap-around. */
976 1.50 chs if (addr + size < addr)
977 1.6 mrg return (EINVAL);
978 1.1 mrg
979 1.1 mrg #ifndef pmap_wired_count
980 1.6 mrg if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
981 1.6 mrg return (error);
982 1.1 mrg #endif
983 1.1 mrg
984 1.25 thorpej error = uvm_map_pageable(&p->p_vmspace->vm_map, addr, addr+size, TRUE,
985 1.35 thorpej 0);
986 1.50 chs return error;
987 1.22 thorpej }
988 1.22 thorpej
989 1.22 thorpej /*
990 1.22 thorpej * sys_mlockall: lock all pages mapped into an address space.
991 1.22 thorpej */
992 1.22 thorpej
993 1.22 thorpej int
994 1.67 thorpej sys_mlockall(l, v, retval)
995 1.67 thorpej struct lwp *l;
996 1.22 thorpej void *v;
997 1.22 thorpej register_t *retval;
998 1.22 thorpej {
999 1.22 thorpej struct sys_mlockall_args /* {
1000 1.22 thorpej syscallarg(int) flags;
1001 1.22 thorpej } */ *uap = v;
1002 1.67 thorpej struct proc *p = l->l_proc;
1003 1.22 thorpej int error, flags;
1004 1.22 thorpej
1005 1.22 thorpej flags = SCARG(uap, flags);
1006 1.22 thorpej
1007 1.22 thorpej if (flags == 0 ||
1008 1.22 thorpej (flags & ~(MCL_CURRENT|MCL_FUTURE)) != 0)
1009 1.22 thorpej return (EINVAL);
1010 1.22 thorpej
1011 1.25 thorpej #ifndef pmap_wired_count
1012 1.22 thorpej if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
1013 1.22 thorpej return (error);
1014 1.22 thorpej #endif
1015 1.22 thorpej
1016 1.25 thorpej error = uvm_map_pageable_all(&p->p_vmspace->vm_map, flags,
1017 1.25 thorpej p->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1018 1.22 thorpej return (error);
1019 1.22 thorpej }
1020 1.22 thorpej
1021 1.22 thorpej /*
1022 1.22 thorpej * sys_munlockall: unlock all pages mapped into an address space.
1023 1.22 thorpej */
1024 1.22 thorpej
1025 1.22 thorpej int
1026 1.67 thorpej sys_munlockall(l, v, retval)
1027 1.67 thorpej struct lwp *l;
1028 1.22 thorpej void *v;
1029 1.22 thorpej register_t *retval;
1030 1.22 thorpej {
1031 1.67 thorpej struct proc *p = l->l_proc;
1032 1.22 thorpej
1033 1.22 thorpej (void) uvm_map_pageable_all(&p->p_vmspace->vm_map, 0, 0);
1034 1.22 thorpej return (0);
1035 1.1 mrg }
1036 1.1 mrg
1037 1.1 mrg /*
1038 1.1 mrg * uvm_mmap: internal version of mmap
1039 1.1 mrg *
1040 1.56 chs * - used by sys_mmap and various framebuffers
1041 1.56 chs * - handle is a vnode pointer or NULL for MAP_ANON
1042 1.1 mrg * - caller must page-align the file offset
1043 1.1 mrg */
1044 1.1 mrg
1045 1.6 mrg int
1046 1.25 thorpej uvm_mmap(map, addr, size, prot, maxprot, flags, handle, foff, locklimit)
1047 1.53 chs struct vm_map *map;
1048 1.12 eeh vaddr_t *addr;
1049 1.12 eeh vsize_t size;
1050 1.6 mrg vm_prot_t prot, maxprot;
1051 1.6 mrg int flags;
1052 1.50 chs void *handle;
1053 1.38 kleink voff_t foff;
1054 1.25 thorpej vsize_t locklimit;
1055 1.6 mrg {
1056 1.6 mrg struct uvm_object *uobj;
1057 1.6 mrg struct vnode *vp;
1058 1.70 matt vaddr_t align = 0;
1059 1.50 chs int error;
1060 1.6 mrg int advice = UVM_ADV_NORMAL;
1061 1.6 mrg uvm_flag_t uvmflag = 0;
1062 1.6 mrg
1063 1.6 mrg /*
1064 1.6 mrg * check params
1065 1.6 mrg */
1066 1.6 mrg
1067 1.6 mrg if (size == 0)
1068 1.6 mrg return(0);
1069 1.6 mrg if (foff & PAGE_MASK)
1070 1.6 mrg return(EINVAL);
1071 1.6 mrg if ((prot & maxprot) != prot)
1072 1.6 mrg return(EINVAL);
1073 1.6 mrg
1074 1.6 mrg /*
1075 1.6 mrg * for non-fixed mappings, round off the suggested address.
1076 1.6 mrg * for fixed mappings, check alignment and zap old mappings.
1077 1.6 mrg */
1078 1.6 mrg
1079 1.6 mrg if ((flags & MAP_FIXED) == 0) {
1080 1.56 chs *addr = round_page(*addr);
1081 1.6 mrg } else {
1082 1.6 mrg if (*addr & PAGE_MASK)
1083 1.6 mrg return(EINVAL);
1084 1.6 mrg uvmflag |= UVM_FLAG_FIXED;
1085 1.56 chs (void) uvm_unmap(map, *addr, *addr + size);
1086 1.6 mrg }
1087 1.6 mrg
1088 1.6 mrg /*
1089 1.70 matt * Try to see if any requested alignment can even be attemped.
1090 1.70 matt * Make sure we can express the alignment (asking for a >= 4GB
1091 1.70 matt * alignment on an ILP32 architecure make no sense) and the
1092 1.70 matt * alignment is at least for a page sized quanitiy. If the
1093 1.70 matt * request was for a fixed mapping, make sure supplied address
1094 1.70 matt * adheres to the request alignment.
1095 1.70 matt */
1096 1.70 matt align = (flags & MAP_ALIGNMENT_MASK) >> MAP_ALIGNMENT_SHIFT;
1097 1.70 matt if (align) {
1098 1.70 matt if (align >= sizeof(vaddr_t) * NBBY)
1099 1.70 matt return(EINVAL);
1100 1.70 matt align = 1L << align;
1101 1.70 matt if (align < PAGE_SIZE)
1102 1.70 matt return(EINVAL);
1103 1.70 matt if (align >= map->max_offset)
1104 1.70 matt return(ENOMEM);
1105 1.70 matt if (flags & MAP_FIXED) {
1106 1.70 matt if ((*addr & (align-1)) != 0)
1107 1.70 matt return(EINVAL);
1108 1.70 matt align = 0;
1109 1.70 matt }
1110 1.70 matt }
1111 1.70 matt
1112 1.70 matt /*
1113 1.6 mrg * handle anon vs. non-anon mappings. for non-anon mappings attach
1114 1.6 mrg * to underlying vm object.
1115 1.6 mrg */
1116 1.6 mrg
1117 1.6 mrg if (flags & MAP_ANON) {
1118 1.36 thorpej foff = UVM_UNKNOWN_OFFSET;
1119 1.6 mrg uobj = NULL;
1120 1.6 mrg if ((flags & MAP_SHARED) == 0)
1121 1.6 mrg /* XXX: defer amap create */
1122 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1123 1.6 mrg else
1124 1.6 mrg /* shared: create amap now */
1125 1.6 mrg uvmflag |= UVM_FLAG_OVERLAY;
1126 1.6 mrg
1127 1.6 mrg } else {
1128 1.50 chs vp = (struct vnode *)handle;
1129 1.59 thorpej
1130 1.59 thorpej /*
1131 1.59 thorpej * Don't allow mmap for EXEC if the file system
1132 1.59 thorpej * is mounted NOEXEC.
1133 1.59 thorpej */
1134 1.59 thorpej if ((prot & PROT_EXEC) != 0 &&
1135 1.59 thorpej (vp->v_mount->mnt_flag & MNT_NOEXEC) != 0)
1136 1.59 thorpej return (EACCES);
1137 1.59 thorpej
1138 1.6 mrg if (vp->v_type != VCHR) {
1139 1.74 fvdl error = VOP_MMAP(vp, 0, curproc->p_ucred, curproc);
1140 1.55 chs if (error) {
1141 1.55 chs return error;
1142 1.55 chs }
1143 1.55 chs
1144 1.50 chs uobj = uvn_attach((void *)vp, (flags & MAP_SHARED) ?
1145 1.6 mrg maxprot : (maxprot & ~VM_PROT_WRITE));
1146 1.6 mrg
1147 1.46 chs /* XXX for now, attach doesn't gain a ref */
1148 1.46 chs VREF(vp);
1149 1.57 thorpej
1150 1.57 thorpej /*
1151 1.57 thorpej * If the vnode is being mapped with PROT_EXEC,
1152 1.57 thorpej * then mark it as text.
1153 1.57 thorpej */
1154 1.57 thorpej if (prot & PROT_EXEC)
1155 1.58 thorpej vn_markexec(vp);
1156 1.6 mrg } else {
1157 1.6 mrg uobj = udv_attach((void *) &vp->v_rdev,
1158 1.48 thorpej (flags & MAP_SHARED) ? maxprot :
1159 1.48 thorpej (maxprot & ~VM_PROT_WRITE), foff, size);
1160 1.48 thorpej /*
1161 1.48 thorpej * XXX Some devices don't like to be mapped with
1162 1.48 thorpej * XXX PROT_EXEC, but we don't really have a
1163 1.48 thorpej * XXX better way of handling this, right now
1164 1.48 thorpej */
1165 1.48 thorpej if (uobj == NULL && (prot & PROT_EXEC) == 0) {
1166 1.48 thorpej maxprot &= ~VM_PROT_EXECUTE;
1167 1.50 chs uobj = udv_attach((void *)&vp->v_rdev,
1168 1.48 thorpej (flags & MAP_SHARED) ? maxprot :
1169 1.48 thorpej (maxprot & ~VM_PROT_WRITE), foff, size);
1170 1.48 thorpej }
1171 1.6 mrg advice = UVM_ADV_RANDOM;
1172 1.6 mrg }
1173 1.6 mrg if (uobj == NULL)
1174 1.11 thorpej return((vp->v_type == VREG) ? ENOMEM : EINVAL);
1175 1.6 mrg if ((flags & MAP_SHARED) == 0)
1176 1.6 mrg uvmflag |= UVM_FLAG_COPYONW;
1177 1.6 mrg }
1178 1.6 mrg
1179 1.51 chs uvmflag = UVM_MAPFLAG(prot, maxprot,
1180 1.1 mrg (flags & MAP_SHARED) ? UVM_INH_SHARE : UVM_INH_COPY,
1181 1.1 mrg advice, uvmflag);
1182 1.70 matt error = uvm_map(map, addr, size, uobj, foff, align, uvmflag);
1183 1.50 chs if (error) {
1184 1.50 chs if (uobj)
1185 1.50 chs uobj->pgops->pgo_detach(uobj);
1186 1.50 chs return error;
1187 1.50 chs }
1188 1.1 mrg
1189 1.6 mrg /*
1190 1.50 chs * POSIX 1003.1b -- if our address space was configured
1191 1.50 chs * to lock all future mappings, wire the one we just made.
1192 1.78 thorpej *
1193 1.78 thorpej * Also handle the MAP_WIRED flag here.
1194 1.6 mrg */
1195 1.6 mrg
1196 1.50 chs if (prot == VM_PROT_NONE) {
1197 1.6 mrg
1198 1.25 thorpej /*
1199 1.50 chs * No more work to do in this case.
1200 1.25 thorpej */
1201 1.25 thorpej
1202 1.50 chs return (0);
1203 1.50 chs }
1204 1.50 chs vm_map_lock(map);
1205 1.78 thorpej if ((flags & MAP_WIRED) != 0 || (map->flags & VM_MAP_WIREFUTURE) != 0) {
1206 1.50 chs if ((atop(size) + uvmexp.wired) > uvmexp.wiredmax
1207 1.25 thorpej #ifdef pmap_wired_count
1208 1.50 chs || (locklimit != 0 && (size +
1209 1.50 chs ptoa(pmap_wired_count(vm_map_pmap(map)))) >
1210 1.50 chs locklimit)
1211 1.25 thorpej #endif
1212 1.50 chs ) {
1213 1.50 chs vm_map_unlock(map);
1214 1.50 chs uvm_unmap(map, *addr, *addr + size);
1215 1.50 chs return ENOMEM;
1216 1.25 thorpej }
1217 1.25 thorpej
1218 1.50 chs /*
1219 1.50 chs * uvm_map_pageable() always returns the map unlocked.
1220 1.50 chs */
1221 1.25 thorpej
1222 1.50 chs error = uvm_map_pageable(map, *addr, *addr + size,
1223 1.50 chs FALSE, UVM_LK_ENTER);
1224 1.50 chs if (error) {
1225 1.50 chs uvm_unmap(map, *addr, *addr + size);
1226 1.50 chs return error;
1227 1.50 chs }
1228 1.25 thorpej return (0);
1229 1.25 thorpej }
1230 1.50 chs vm_map_unlock(map);
1231 1.50 chs return 0;
1232 1.1 mrg }
1233