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.14 2007/08/15 03:15:07 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>
47 #include <sys/vmspace.h>
49 #include <vm/vm_extern.h>
52 #include <machine/vmparam.h>
54 #include <sys/spinlock2.h>
55 #include <sys/sysref2.h>
56 #include <sys/mplock2.h>
58 static struct vmspace_entry
*vkernel_find_vmspace(struct vkernel_proc
*vkp
,
60 static void vmspace_entry_delete(struct vmspace_entry
*ve
,
61 struct vkernel_proc
*vkp
);
63 static MALLOC_DEFINE(M_VKERNEL
, "vkernel", "VKernel structures");
66 * vmspace_create (void *id, int type, void *data)
68 * Create a VMSPACE under the control of the caller with the specified id.
69 * An id of NULL cannot be used. The type and data fields must currently
72 * The vmspace starts out completely empty. Memory may be mapped into the
73 * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
74 * with vmspace_mcontrol().
79 sys_vmspace_create(struct vmspace_create_args
*uap
)
81 struct vmspace_entry
*ve
;
82 struct vkernel_proc
*vkp
;
85 if (vkernel_enable
== 0)
89 * Create a virtual kernel side-structure for the process if one
93 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
94 vkp
= kmalloc(sizeof(*vkp
), M_VKERNEL
, M_WAITOK
|M_ZERO
);
96 spin_init(&vkp
->spin
);
98 curproc
->p_vkernel
= vkp
;
102 * Create a new VMSPACE
104 * XXX race if kmalloc blocks
106 if (vkernel_find_vmspace(vkp
, uap
->id
)) {
110 ve
= kmalloc(sizeof(struct vmspace_entry
), M_VKERNEL
, M_WAITOK
|M_ZERO
);
111 ve
->vmspace
= vmspace_alloc(VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
113 pmap_pinit2(vmspace_pmap(ve
->vmspace
));
114 RB_INSERT(vmspace_rb_tree
, &vkp
->root
, ve
);
122 * vmspace_destroy (void *id)
129 sys_vmspace_destroy(struct vmspace_destroy_args
*uap
)
131 struct vkernel_proc
*vkp
;
132 struct vmspace_entry
*ve
;
136 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
140 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
148 vmspace_entry_delete(ve
, vkp
);
156 * vmspace_ctl (void *id, int cmd, struct trapframe *tframe,
157 * struct vextframe *vframe);
159 * Transfer control to a VMSPACE. Control is returned after the specified
160 * number of microseconds or if a page fault, signal, trap, or system call
161 * occurs. The context is updated as appropriate.
166 sys_vmspace_ctl(struct vmspace_ctl_args
*uap
)
168 struct vkernel_proc
*vkp
;
169 struct vkernel_lwp
*vklp
;
170 struct vmspace_entry
*ve
;
176 lp
= curthread
->td_lwp
;
180 if ((vkp
= p
->p_vkernel
) == NULL
) {
184 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
190 * Signal mailbox interlock
192 if (p
->p_flag
& P_MAILBOX
) {
193 p
->p_flag
&= ~P_MAILBOX
;
199 case VMSPACE_CTL_RUN
:
201 * Save the caller's register context, swap VM spaces, and
202 * install the passed register context. Return with
203 * EJUSTRETURN so the syscall code doesn't adjust the context.
205 atomic_add_int(&ve
->refs
, 1);
206 framesz
= sizeof(struct trapframe
);
207 if ((vklp
= lp
->lwp_vkernel
) == NULL
) {
208 vklp
= kmalloc(sizeof(*vklp
), M_VKERNEL
,
210 lp
->lwp_vkernel
= vklp
;
212 vklp
->user_trapframe
= uap
->tframe
;
213 vklp
->user_vextframe
= uap
->vframe
;
214 bcopy(uap
->sysmsg_frame
, &vklp
->save_trapframe
, framesz
);
215 bcopy(&curthread
->td_tls
, &vklp
->save_vextframe
.vx_tls
,
216 sizeof(vklp
->save_vextframe
.vx_tls
));
217 error
= copyin(uap
->tframe
, uap
->sysmsg_frame
, framesz
);
219 error
= copyin(&uap
->vframe
->vx_tls
, &curthread
->td_tls
, sizeof(struct savetls
));
221 error
= cpu_sanitize_frame(uap
->sysmsg_frame
);
223 error
= cpu_sanitize_tls(&curthread
->td_tls
);
225 bcopy(&vklp
->save_trapframe
, uap
->sysmsg_frame
, framesz
);
226 bcopy(&vklp
->save_vextframe
.vx_tls
, &curthread
->td_tls
,
227 sizeof(vklp
->save_vextframe
.vx_tls
));
229 atomic_subtract_int(&ve
->refs
, 1);
232 pmap_setlwpvm(lp
, ve
->vmspace
);
234 set_vkernel_fp(uap
->sysmsg_frame
);
248 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
250 * map memory within a VMSPACE. This function is just like a normal mmap()
251 * but operates on the vmspace's memory map. Most callers use this to create
252 * a MAP_VPAGETABLE mapping.
257 sys_vmspace_mmap(struct vmspace_mmap_args
*uap
)
259 struct vkernel_proc
*vkp
;
260 struct vmspace_entry
*ve
;
264 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
268 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
272 error
= kern_mmap(ve
->vmspace
, uap
->addr
, uap
->len
,
273 uap
->prot
, uap
->flags
,
274 uap
->fd
, uap
->offset
, &uap
->sysmsg_resultp
);
281 * vmspace_munmap(id, addr, len)
283 * unmap memory within a VMSPACE.
288 sys_vmspace_munmap(struct vmspace_munmap_args
*uap
)
290 struct vkernel_proc
*vkp
;
291 struct vmspace_entry
*ve
;
294 vm_size_t size
, pageoff
;
299 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
303 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
309 * Copied from sys_munmap()
311 addr
= (vm_offset_t
)uap
->addr
;
314 pageoff
= (addr
& PAGE_MASK
);
317 size
= (vm_size_t
)round_page(size
);
318 if (size
< uap
->len
) { /* wrap */
322 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
323 if (tmpaddr
< addr
) { /* wrap */
332 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
) {
336 if (VM_MIN_USER_ADDRESS
> 0 && addr
< VM_MIN_USER_ADDRESS
) {
340 map
= &ve
->vmspace
->vm_map
;
341 if (!vm_map_check_protection(map
, addr
, tmpaddr
, VM_PROT_NONE
)) {
345 vm_map_remove(map
, addr
, addr
+ size
);
353 * vmspace_pread(id, buf, nbyte, flags, offset)
355 * Read data from a vmspace. The number of bytes read is returned or
356 * -1 if an unrecoverable error occured. If the number of bytes read is
357 * less then the request size, a page fault occured in the VMSPACE which
358 * the caller must resolve in order to proceed.
360 * (not implemented yet)
365 sys_vmspace_pread(struct vmspace_pread_args
*uap
)
367 struct vkernel_proc
*vkp
;
368 struct vmspace_entry
*ve
;
372 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
376 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
387 * vmspace_pwrite(id, buf, nbyte, flags, offset)
389 * Write data to a vmspace. The number of bytes written is returned or
390 * -1 if an unrecoverable error occured. If the number of bytes written is
391 * less then the request size, a page fault occured in the VMSPACE which
392 * the caller must resolve in order to proceed.
394 * (not implemented yet)
399 sys_vmspace_pwrite(struct vmspace_pwrite_args
*uap
)
401 struct vkernel_proc
*vkp
;
402 struct vmspace_entry
*ve
;
406 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
410 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
421 * vmspace_mcontrol(id, addr, len, behav, value)
423 * madvise/mcontrol support for a vmspace.
428 sys_vmspace_mcontrol(struct vmspace_mcontrol_args
*uap
)
430 struct vkernel_proc
*vkp
;
431 struct vmspace_entry
*ve
;
432 vm_offset_t start
, end
;
433 vm_offset_t tmpaddr
= (vm_offset_t
)uap
->addr
+ uap
->len
;
437 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
441 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
)) == NULL
) {
447 * This code is basically copied from sys_mcontrol()
449 if (uap
->behav
< 0 || uap
->behav
> MADV_CONTROL_END
) {
454 if (tmpaddr
< (vm_offset_t
)uap
->addr
) {
458 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
) {
462 if (VM_MIN_USER_ADDRESS
> 0 && uap
->addr
< VM_MIN_USER_ADDRESS
) {
467 start
= trunc_page((vm_offset_t
) uap
->addr
);
468 end
= round_page(tmpaddr
);
470 error
= vm_map_madvise(&ve
->vmspace
->vm_map
, start
, end
,
471 uap
->behav
, uap
->value
);
478 * Red black tree functions
480 static int rb_vmspace_compare(struct vmspace_entry
*, struct vmspace_entry
*);
481 RB_GENERATE(vmspace_rb_tree
, vmspace_entry
, rb_entry
, rb_vmspace_compare
);
483 /* a->start is address, and the only field has to be initialized */
485 rb_vmspace_compare(struct vmspace_entry
*a
, struct vmspace_entry
*b
)
487 if ((char *)a
->id
< (char *)b
->id
)
489 else if ((char *)a
->id
> (char *)b
->id
)
496 rb_vmspace_delete(struct vmspace_entry
*ve
, void *data
)
498 struct vkernel_proc
*vkp
= data
;
500 KKASSERT(ve
->refs
== 0);
501 vmspace_entry_delete(ve
, vkp
);
506 * Remove a vmspace_entry from the RB tree and destroy it. We have to clean
507 * up the pmap, the vm_map, then destroy the vmspace.
511 vmspace_entry_delete(struct vmspace_entry
*ve
, struct vkernel_proc
*vkp
)
513 RB_REMOVE(vmspace_rb_tree
, &vkp
->root
, ve
);
515 pmap_remove_pages(vmspace_pmap(ve
->vmspace
),
516 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
517 vm_map_remove(&ve
->vmspace
->vm_map
,
518 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
519 sysref_put(&ve
->vmspace
->vm_sysref
);
520 kfree(ve
, M_VKERNEL
);
525 struct vmspace_entry
*
526 vkernel_find_vmspace(struct vkernel_proc
*vkp
, void *id
)
528 struct vmspace_entry
*ve
;
529 struct vmspace_entry key
;
532 ve
= RB_FIND(vmspace_rb_tree
, &vkp
->root
, &key
);
537 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
541 vkernel_inherit(struct proc
*p1
, struct proc
*p2
)
543 struct vkernel_proc
*vkp
;
546 KKASSERT(vkp
->refs
> 0);
547 atomic_add_int(&vkp
->refs
, 1);
552 vkernel_exit(struct proc
*p
)
554 struct vkernel_proc
*vkp
;
560 * Restore the original VM context if we are killed while running
563 * This isn't supposed to happen. What is supposed to happen is
564 * that the process should enter vkernel_trap() before the handling
567 RB_FOREACH(lp
, lwp_rb_tree
, &p
->p_lwp_tree
) {
568 vkernel_lwp_exit(lp
);
572 * Dereference the common area
575 KKASSERT(vkp
->refs
> 0);
576 spin_lock_wr(&vkp
->spin
);
577 if (--vkp
->refs
== 0)
579 spin_unlock_wr(&vkp
->spin
);
582 RB_SCAN(vmspace_rb_tree
, &vkp
->root
, NULL
,
583 rb_vmspace_delete
, vkp
);
584 kfree(vkp
, M_VKERNEL
);
589 vkernel_lwp_exit(struct lwp
*lp
)
591 struct vkernel_lwp
*vklp
;
592 struct vmspace_entry
*ve
;
594 if ((vklp
= lp
->lwp_vkernel
) != NULL
) {
595 if ((ve
= vklp
->ve
) != NULL
) {
596 kprintf("Warning, pid %d killed with "
597 "active VC!\n", lp
->lwp_proc
->p_pid
);
599 pmap_setlwpvm(lp
, lp
->lwp_proc
->p_vmspace
);
601 KKASSERT(ve
->refs
> 0);
602 atomic_subtract_int(&ve
->refs
, 1);
604 lp
->lwp_vkernel
= NULL
;
605 kfree(vklp
, M_VKERNEL
);
610 * A VM space under virtual kernel control trapped out or made a system call
611 * or otherwise needs to return control to the virtual kernel context.
614 vkernel_trap(struct lwp
*lp
, struct trapframe
*frame
)
616 struct proc
*p
= lp
->lwp_proc
;
617 struct vmspace_entry
*ve
;
618 struct vkernel_lwp
*vklp
;
622 * Which vmspace entry was running?
624 vklp
= lp
->lwp_vkernel
;
627 KKASSERT(ve
!= NULL
);
630 * Switch the LWP vmspace back to the virtual kernel's VM space.
633 pmap_setlwpvm(lp
, p
->p_vmspace
);
634 KKASSERT(ve
->refs
> 0);
635 atomic_subtract_int(&ve
->refs
, 1);
638 * Copy the emulated process frame to the virtual kernel process.
639 * The emulated process cannot change TLS descriptors so don't
640 * bother saving them, we already have a copy.
642 * Restore the virtual kernel's saved context so the virtual kernel
643 * process can resume.
645 error
= copyout(frame
, vklp
->user_trapframe
, sizeof(*frame
));
646 bcopy(&vklp
->save_trapframe
, frame
, sizeof(*frame
));
647 bcopy(&vklp
->save_vextframe
.vx_tls
, &curthread
->td_tls
,
648 sizeof(vklp
->save_vextframe
.vx_tls
));
650 cpu_vkernel_trap(frame
, error
);