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.6 2006/12/23 00:41:31 swildner 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>
47 #include <sys/vmspace.h>
48 #include <sys/spinlock2.h>
50 #include <vm/vm_extern.h>
53 static struct vmspace_entry *vkernel_find_vmspace(struct vkernel_common *vc,
55 static void vmspace_entry_delete(struct vmspace_entry *ve,
56 struct vkernel_common *vc);
58 static MALLOC_DEFINE(M_VKERNEL, "vkernel", "VKernel structures");
61 * vmspace_create (void *id, int type, void *data)
63 * Create a VMSPACE under the control of the caller with the specified id.
64 * An id of NULL cannot be used. The type and data fields must currently
67 * The vmspace starts out completely empty. Memory may be mapped into the
68 * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
69 * with vmspace_mcontrol().
72 sys_vmspace_create(struct vmspace_create_args *uap)
74 struct vkernel_common *vc;
75 struct vmspace_entry *ve;
78 if (vkernel_enable == 0)
82 * Create a virtual kernel side-structure for the process if one
85 if ((vk = curproc->p_vkernel) == NULL) {
86 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
87 vc = kmalloc(sizeof(*vc), M_VKERNEL, M_WAITOK|M_ZERO);
89 spin_init(&vc->vc_spin);
90 RB_INIT(&vc->vc_root);
92 curproc->p_vkernel = vk;
97 * Create a new VMSPACE
99 if (vkernel_find_vmspace(vc, uap->id))
101 ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
102 ve->vmspace = vmspace_alloc(VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
104 pmap_pinit2(vmspace_pmap(ve->vmspace));
105 RB_INSERT(vmspace_rb_tree, &vc->vc_root, ve);
110 * vmspace_destroy (void *id)
115 sys_vmspace_destroy(struct vmspace_destroy_args *uap)
117 struct vkernel_common *vc;
118 struct vmspace_entry *ve;
121 if ((vk = curproc->p_vkernel) == NULL)
124 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
128 vmspace_entry_delete(ve, vc);
133 * vmspace_ctl (void *id, int cmd, void *ctx, int ctx_bytes, int timeout_us)
135 * Transfer control to a VMSPACE. Control is returned after the specified
136 * number of microseconds or if a page fault, signal, trap, or system call
137 * occurs. The context is updated as appropriate.
140 sys_vmspace_ctl(struct vmspace_ctl_args *uap)
142 struct vkernel_common *vc;
143 struct vmspace_entry *ve;
149 if ((vk = curproc->p_vkernel) == NULL)
152 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
156 case VMSPACE_CTL_RUN:
158 * Save the caller's register context, swap VM spaces, and
159 * install the passed register context. Return with
160 * EJUSTRETURN so the syscall code doesn't adjust the context.
164 framesz = sizeof(struct trapframe);
166 vk->vk_save_vmspace = p->p_vmspace;
167 vk->vk_user_frame = uap->ctx;
168 bcopy(uap->sysmsg_frame, &vk->vk_save_frame, framesz);
169 error = copyin(uap->ctx, uap->sysmsg_frame, framesz);
171 error = cpu_sanitize_frame(uap->sysmsg_frame);
173 bcopy(&vk->vk_save_frame, uap->sysmsg_frame, framesz);
174 vk->vk_current = NULL;
175 vk->vk_save_vmspace = NULL;
179 p->p_vmspace = ve->vmspace;
192 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
194 * map memory within a VMSPACE. This function is just like a normal mmap()
195 * but operates on the vmspace's memory map. Most callers use this to create
196 * a MAP_VPAGETABLE mapping.
199 sys_vmspace_mmap(struct vmspace_mmap_args *uap)
201 struct vkernel_common *vc;
202 struct vmspace_entry *ve;
206 if ((vk = curproc->p_vkernel) == NULL)
209 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
211 error = kern_mmap(ve->vmspace, uap->addr, uap->len,
212 uap->prot, uap->flags,
213 uap->fd, uap->offset, &uap->sysmsg_resultp);
218 * vmspace_munmap(id, addr, len)
220 * unmap memory within a VMSPACE.
223 sys_vmspace_munmap(struct vmspace_munmap_args *uap)
225 struct vkernel_common *vc;
226 struct vmspace_entry *ve;
229 vm_size_t size, pageoff;
232 if ((vk = curproc->p_vkernel) == NULL)
235 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
239 * Copied from sys_munmap()
241 addr = (vm_offset_t)uap->addr;
244 pageoff = (addr & PAGE_MASK);
247 size = (vm_size_t)round_page(size);
248 if (addr + size < addr)
253 if (VM_MAX_USER_ADDRESS > 0 && addr + size > VM_MAX_USER_ADDRESS)
255 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS)
257 map = &ve->vmspace->vm_map;
258 if (!vm_map_check_protection(map, addr, addr + size, VM_PROT_NONE))
260 vm_map_remove(map, addr, addr + size);
265 * vmspace_pread(id, buf, nbyte, flags, offset)
267 * Read data from a vmspace. The number of bytes read is returned or
268 * -1 if an unrecoverable error occured. If the number of bytes read is
269 * less then the request size, a page fault occured in the VMSPACE which
270 * the caller must resolve in order to proceed.
273 sys_vmspace_pread(struct vmspace_pread_args *uap)
275 struct vkernel_common *vc;
276 struct vmspace_entry *ve;
279 if ((vk = curproc->p_vkernel) == NULL)
282 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
288 * vmspace_pwrite(id, buf, nbyte, flags, offset)
290 * Write data to a vmspace. The number of bytes written is returned or
291 * -1 if an unrecoverable error occured. If the number of bytes written is
292 * less then the request size, a page fault occured in the VMSPACE which
293 * the caller must resolve in order to proceed.
296 sys_vmspace_pwrite(struct vmspace_pwrite_args *uap)
298 struct vkernel_common *vc;
299 struct vmspace_entry *ve;
302 if ((vk = curproc->p_vkernel) == NULL)
305 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
311 * vmspace_mcontrol(id, addr, len, behav, value)
313 * madvise/mcontrol support for a vmspace.
316 sys_vmspace_mcontrol(struct vmspace_mcontrol_args *uap)
318 struct vkernel_common *vc;
319 struct vmspace_entry *ve;
321 vm_offset_t start, end;
323 if ((vk = curproc->p_vkernel) == NULL)
326 if ((ve = vkernel_find_vmspace(vc, uap->id)) == NULL)
330 * This code is basically copied from sys_mcontrol()
332 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END)
335 if (VM_MAX_USER_ADDRESS > 0 &&
336 ((vm_offset_t) uap->addr + uap->len) > VM_MAX_USER_ADDRESS)
338 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS)
340 if (((vm_offset_t) uap->addr + uap->len) < (vm_offset_t) uap->addr)
343 start = trunc_page((vm_offset_t) uap->addr);
344 end = round_page((vm_offset_t) uap->addr + uap->len);
346 return (vm_map_madvise(&ve->vmspace->vm_map, start, end,
347 uap->behav, uap->value));
351 * Red black tree functions
353 static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
354 RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
356 /* a->start is address, and the only field has to be initialized */
358 rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
360 if ((char *)a->id < (char *)b->id)
362 else if ((char *)a->id > (char *)b->id)
369 rb_vmspace_delete(struct vmspace_entry *ve, void *data)
371 struct vkernel_common *vc = data;
373 KKASSERT(ve->refs == 0);
374 vmspace_entry_delete(ve, vc);
379 * Remove a vmspace_entry from the RB tree and destroy it. We have to clean
380 * up the pmap, the vm_map, then destroy the vmspace.
384 vmspace_entry_delete(struct vmspace_entry *ve, struct vkernel_common *vc)
386 RB_REMOVE(vmspace_rb_tree, &vc->vc_root, ve);
388 pmap_remove_pages(vmspace_pmap(ve->vmspace),
389 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
390 vm_map_remove(&ve->vmspace->vm_map,
391 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
392 vmspace_free(ve->vmspace);
393 kfree(ve, M_VKERNEL);
398 struct vmspace_entry *
399 vkernel_find_vmspace(struct vkernel_common *vc, void *id)
401 struct vmspace_entry *ve;
402 struct vmspace_entry key;
405 ve = RB_FIND(vmspace_rb_tree, &vc->vc_root, &key);
410 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
414 vkernel_inherit(struct proc *p1, struct proc *p2)
416 struct vkernel_common *vc;
421 KKASSERT(vc->vc_refs > 0);
422 atomic_add_int(&vc->vc_refs, 1);
423 vk = kmalloc(sizeof(*vk), M_VKERNEL, M_WAITOK|M_ZERO);
429 vkernel_exit(struct proc *p)
431 struct vkernel_common *vc;
432 struct vmspace_entry *ve;
439 vk->vk_common = NULL;
442 * Restore the original VM context if we are killed while running
445 if ((ve = vk->vk_current) != NULL) {
446 kprintf("killed with active VC\n");
447 vk->vk_current = NULL;
449 p->p_vmspace = vk->vk_save_vmspace;
451 vk->vk_save_vmspace = NULL;
452 KKASSERT(ve->refs > 0);
457 * Dereference the common area
459 KKASSERT(vc->vc_refs > 0);
460 spin_lock_wr(&vc->vc_spin);
461 if (--vc->vc_refs == 0)
463 spin_unlock_wr(&vc->vc_spin);
466 RB_SCAN(vmspace_rb_tree, &vc->vc_root, NULL,
467 rb_vmspace_delete, vc);
468 kfree(vc, M_VKERNEL);
470 kfree(vk, M_VKERNEL);
474 * A VM space under virtual kernel control trapped out or made a system call
475 * or otherwise needs to return control to the virtual kernel context.
478 vkernel_trap(struct proc *p, struct trapframe *frame)
480 struct vmspace_entry *ve;
484 kprintf("trap for vkernel type %d wm=%d\n",
485 frame->tf_trapno & 0x7FFFFFFF,
486 ((frame->tf_trapno & 0x80000000) ? 1 : 0));
489 * Which vmspace entry was running?
493 vk->vk_current = NULL;
494 KKASSERT(ve != NULL);
497 * Switch the process context back to the virtual kernel's VM space.
500 p->p_vmspace = vk->vk_save_vmspace;
502 vk->vk_save_vmspace = NULL;
503 KKASSERT(ve->refs > 0);
507 * Copy the trapframe to the virtual kernel's userspace, then
508 * restore virtual kernel's original syscall trap frame so we
509 * can 'return' from the system call that ran the custom VM space.
511 error = copyout(frame, vk->vk_user_frame, sizeof(*frame));
512 bcopy(&vk->vk_save_frame, frame, sizeof(*frame));