2 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vm/vm_vmspace.c,v 1.3 2006/10/10 15:43:16 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/systm.h>
40 #include <sys/sysproto.h>
41 #include <sys/kern_syscall.h>
44 #include <sys/malloc.h>
45 #include <sys/sysctl.h>
46 #include <sys/vkernel.h>
48 #include <vm/vm_extern.h>
51 static struct vmspace_entry *vkernel_find_vmspace(struct vkernel *vk, void *id);
53 static MALLOC_DEFINE(M_VKERNEL, "vkernel", "VKernel structures");
56 * vmspace_create (void *id, int type, void *data)
58 * Create a VMSPACE under the control of the caller with the specified id.
59 * An id of NULL cannot be used. The type and data fields must currently
62 * The vmspace starts out completely empty. Memory may be mapped into the
63 * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
64 * with vmspace_mcontrol().
67 sys_vmspace_create(struct vmspace_create_args *uap)
70 struct vmspace_entry *ve;
72 if (vkernel_enable == 0)
76 * Create a virtual kernel side-structure for the process if one
79 if ((vk = curproc->p_vkernel) == NULL) {
80 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
82 RB_INIT(&vk->vk_root);
83 curproc->p_vkernel = vk;
87 * Create a new VMSPACE
89 if (vkernel_find_vmspace(vk, uap->id))
91 ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
92 ve->vmspace = vmspace_alloc(VM_MIN_ADDRESS, VM_MAXUSER_ADDRESS);
94 pmap_pinit2(vmspace_pmap(ve->vmspace));
95 RB_INSERT(vmspace_rb_tree, &vk->vk_root, ve);
100 * vmspace_destroy (void *id)
105 sys_vmspace_destroy(struct vmspace_destroy_args *uap)
108 struct vmspace_entry *ve;
110 if ((vk = curproc->p_vkernel) == NULL)
112 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
114 /* XXX check if active */
115 RB_REMOVE(vmspace_rb_tree, &vk->vk_root, ve);
116 vmspace_free(ve->vmspace);
117 kfree(ve, M_VKERNEL);
122 * vmspace_ctl (void *id, int cmd, void *ctx, int ctx_bytes, int timeout_us)
124 * Transfer control to a VMSPACE. Control is returned after the specified
125 * number of microseconds or if a page fault, signal, trap, or system call
126 * occurs. The context is updated as appropriate.
129 sys_vmspace_ctl(struct vmspace_ctl_args *uap)
132 struct vmspace_entry *ve;
134 if ((vk = curproc->p_vkernel) == NULL)
136 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
142 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
144 * map memory within a VMSPACE. This function is just like a normal mmap()
145 * but operates on the vmspace's memory map. Most callers use this to create
146 * a MAP_VPAGETABLE mapping.
149 sys_vmspace_mmap(struct vmspace_mmap_args *uap)
152 struct vmspace_entry *ve;
155 if ((vk = curproc->p_vkernel) == NULL)
157 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
159 error = kern_mmap(ve->vmspace, uap->addr, uap->len,
160 uap->prot, uap->flags,
161 uap->fd, uap->offset, &uap->sysmsg_resultp);
166 * vmspace_munmap(id, addr, len)
168 * unmap memory within a VMSPACE.
171 sys_vmspace_munmap(struct vmspace_munmap_args *uap)
174 struct vmspace_entry *ve;
176 vm_size_t size, pageoff;
179 if ((vk = curproc->p_vkernel) == NULL)
181 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
185 * Copied from sys_munmap()
187 addr = (vm_offset_t)uap->addr;
190 pageoff = (addr & PAGE_MASK);
193 size = (vm_size_t)round_page(size);
194 if (addr + size < addr)
199 if (VM_MAXUSER_ADDRESS > 0 && addr + size > VM_MAXUSER_ADDRESS)
202 if (VM_MIN_ADDRESS > 0 && addr < VM_MIN_ADDRESS)
205 map = &ve->vmspace->vm_map;
206 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
208 vm_map_remove(map, addr, addr + size);
213 * vmspace_pread(id, buf, nbyte, flags, offset)
215 * Read data from a vmspace. The number of bytes read is returned or
216 * -1 if an unrecoverable error occured. If the number of bytes read is
217 * less then the request size, a page fault occured in the VMSPACE which
218 * the caller must resolve in order to proceed.
221 sys_vmspace_pread(struct vmspace_pread_args *uap)
224 struct vmspace_entry *ve;
226 if ((vk = curproc->p_vkernel) == NULL)
228 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
234 * vmspace_pwrite(id, buf, nbyte, flags, offset)
236 * Write data to a vmspace. The number of bytes written is returned or
237 * -1 if an unrecoverable error occured. If the number of bytes written is
238 * less then the request size, a page fault occured in the VMSPACE which
239 * the caller must resolve in order to proceed.
242 sys_vmspace_pwrite(struct vmspace_pwrite_args *uap)
245 struct vmspace_entry *ve;
247 if ((vk = curproc->p_vkernel) == NULL)
249 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
255 * vmspace_mcontrol(id, addr, len, behav, value)
257 * madvise/mcontrol support for a vmspace.
260 sys_vmspace_mcontrol(struct vmspace_mcontrol_args *uap)
263 struct vmspace_entry *ve;
264 vm_offset_t start, end;
266 if ((vk = curproc->p_vkernel) == NULL)
268 if ((ve = vkernel_find_vmspace(vk, uap->id)) == NULL)
272 * This code is basically copied from sys_mcontrol()
274 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
277 if (VM_MAXUSER_ADDRESS > 0 &&
278 ((vm_offset_t) uap->addr + uap->len) > VM_MAXUSER_ADDRESS)
281 if (VM_MIN_ADDRESS > 0 && uap->addr < VM_MIN_ADDRESS)
284 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
287 start = trunc_page((vm_offset_t) uap->addr);
288 end = round_page((vm_offset_t) uap->addr + uap->len);
290 return (vm_map_madvise(&ve->vmspace->vm_map, start, end,
291 uap->behav, uap->value));
295 * Red black tree functions
297 static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
298 RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
300 /* a->start is address, and the only field has to be initialized */
302 rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
304 if ((char *)a->id < (char *)b->id)
306 else if ((char *)a->id > (char *)b->id)
313 rb_vmspace_delete(struct vmspace_entry *ve, void *data)
315 struct vkernel *vk = data;
317 RB_REMOVE(vmspace_rb_tree, &vk->vk_root, ve);
318 vmspace_free(ve->vmspace);
319 kfree(ve, M_VKERNEL);
324 struct vmspace_entry *
325 vkernel_find_vmspace(struct vkernel *vk, void *id)
327 struct vmspace_entry *ve;
328 struct vmspace_entry key;
331 ve = RB_FIND(vmspace_rb_tree, &vk->vk_root, &key);
336 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
340 vkernel_hold(struct vkernel *vk)
346 vkernel_drop(struct vkernel *vk)
348 KKASSERT(vk->vk_refs > 0);
349 if (--vk->vk_refs == 0) {
350 RB_SCAN(vmspace_rb_tree, &vk->vk_root, NULL,
351 rb_vmspace_delete, vk);
352 kfree(vk, M_VKERNEL);