4 * Copyright (c) 2006 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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>
43 #include <sys/thread.h>
45 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/vkernel.h>
48 #include <sys/vmspace.h>
50 #include <vm/vm_extern.h>
53 #include <machine/vmparam.h>
54 #include <machine/vmm.h>
56 static struct vmspace_entry
*vkernel_find_vmspace(struct vkernel_proc
*vkp
,
57 void *id
, int havetoken
);
58 static int vmspace_entry_delete(struct vmspace_entry
*ve
,
59 struct vkernel_proc
*vkp
, int refs
);
60 static void vmspace_entry_cache_ref(struct vmspace_entry
*ve
);
61 static void vmspace_entry_cache_drop(struct vmspace_entry
*ve
);
62 static void vmspace_entry_drop(struct vmspace_entry
*ve
);
64 static MALLOC_DEFINE(M_VKERNEL
, "vkernel", "VKernel structures");
67 * vmspace_create (void *id, int type, void *data)
69 * Create a VMSPACE under the control of the caller with the specified id.
70 * An id of NULL cannot be used. The type and data fields must currently
73 * The vmspace starts out completely empty. Memory may be mapped into the
74 * VMSPACE with vmspace_mmap() and MAP_VPAGETABLE section(s) controlled
75 * with vmspace_mcontrol().
80 sys_vmspace_create(struct vmspace_create_args
*uap
)
82 struct vmspace_entry
*ve
;
83 struct vkernel_proc
*vkp
;
84 struct proc
*p
= curproc
;
87 if (vkernel_enable
== 0)
91 * Create a virtual kernel side-structure for the process if one
94 * Implement a simple resolution for SMP races.
96 if ((vkp
= p
->p_vkernel
) == NULL
) {
97 vkp
= kmalloc(sizeof(*vkp
), M_VKERNEL
, M_WAITOK
|M_ZERO
);
98 lwkt_gettoken(&p
->p_token
);
99 if (p
->p_vkernel
== NULL
) {
101 lwkt_token_init(&vkp
->token
, "vkernel");
105 kfree(vkp
, M_VKERNEL
);
108 lwkt_reltoken(&p
->p_token
);
111 if (curthread
->td_vmm
)
115 * Create a new VMSPACE, disallow conflicting ids
117 ve
= kmalloc(sizeof(struct vmspace_entry
), M_VKERNEL
, M_WAITOK
|M_ZERO
);
118 ve
->vmspace
= vmspace_alloc(VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
120 ve
->refs
= 0; /* active refs (none) */
121 ve
->cache_refs
= 1; /* on-tree, not deleted (prevent kfree) */
122 pmap_pinit2(vmspace_pmap(ve
->vmspace
));
124 lwkt_gettoken(&vkp
->token
);
125 if (RB_INSERT(vmspace_rb_tree
, &vkp
->root
, ve
)) {
126 vmspace_rel(ve
->vmspace
);
127 ve
->vmspace
= NULL
; /* safety */
128 kfree(ve
, M_VKERNEL
);
133 lwkt_reltoken(&vkp
->token
);
139 * Destroy a VMSPACE given its identifier.
144 sys_vmspace_destroy(struct vmspace_destroy_args
*uap
)
146 struct vkernel_proc
*vkp
;
147 struct vmspace_entry
*ve
;
150 if ((vkp
= curproc
->p_vkernel
) == NULL
)
154 * vkp->token protects the deletion against a new RB tree search.
156 lwkt_gettoken(&vkp
->token
);
158 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 1)) != NULL
) {
159 error
= vmspace_entry_delete(ve
, vkp
, 1);
161 vmspace_entry_cache_drop(ve
);
163 lwkt_reltoken(&vkp
->token
);
169 * vmspace_ctl (void *id, int cmd, struct trapframe *tframe,
170 * struct vextframe *vframe);
172 * Transfer control to a VMSPACE. Control is returned after the specified
173 * number of microseconds or if a page fault, signal, trap, or system call
174 * occurs. The context is updated as appropriate.
179 sys_vmspace_ctl(struct vmspace_ctl_args
*uap
)
181 struct vkernel_proc
*vkp
;
182 struct vkernel_lwp
*vklp
;
183 struct vmspace_entry
*ve
= NULL
;
189 lp
= curthread
->td_lwp
;
192 if ((vkp
= p
->p_vkernel
) == NULL
)
196 * ve only matters when VMM is not used.
198 if (curthread
->td_vmm
== NULL
) {
199 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
206 case VMSPACE_CTL_RUN
:
208 * Save the caller's register context, swap VM spaces, and
209 * install the passed register context. Return with
210 * EJUSTRETURN so the syscall code doesn't adjust the context.
212 framesz
= sizeof(struct trapframe
);
213 if ((vklp
= lp
->lwp_vkernel
) == NULL
) {
214 vklp
= kmalloc(sizeof(*vklp
), M_VKERNEL
,
216 lp
->lwp_vkernel
= vklp
;
218 if (ve
&& vklp
->ve_cache
!= ve
) {
219 vmspace_entry_cache_ref(ve
);
221 vmspace_entry_cache_drop(vklp
->ve_cache
);
224 vklp
->user_trapframe
= uap
->tframe
;
225 vklp
->user_vextframe
= uap
->vframe
;
226 bcopy(uap
->sysmsg_frame
, &vklp
->save_trapframe
, framesz
);
227 bcopy(&curthread
->td_tls
, &vklp
->save_vextframe
.vx_tls
,
228 sizeof(vklp
->save_vextframe
.vx_tls
));
229 error
= copyin(uap
->tframe
, uap
->sysmsg_frame
, framesz
);
231 error
= copyin(&uap
->vframe
->vx_tls
,
233 sizeof(struct savetls
));
236 error
= cpu_sanitize_frame(uap
->sysmsg_frame
);
238 error
= cpu_sanitize_tls(&curthread
->td_tls
);
240 bcopy(&vklp
->save_trapframe
, uap
->sysmsg_frame
,
242 bcopy(&vklp
->save_vextframe
.vx_tls
, &curthread
->td_tls
,
243 sizeof(vklp
->save_vextframe
.vx_tls
));
247 * If it's a VMM thread just set the CR3. We also set
248 * the vklp->ve to a key to be able to distinguish
249 * when a vkernel user process runs and when not
252 if (curthread
->td_vmm
== NULL
) {
254 atomic_add_int(&ve
->refs
, 1);
255 pmap_setlwpvm(lp
, ve
->vmspace
);
258 vmm_vm_set_guest_cr3((register_t
)uap
->id
);
261 set_vkernel_fp(uap
->sysmsg_frame
);
271 vmspace_entry_drop(ve
);
277 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
279 * map memory within a VMSPACE. This function is just like a normal mmap()
280 * but operates on the vmspace's memory map. Most callers use this to create
281 * a MAP_VPAGETABLE mapping.
286 sys_vmspace_mmap(struct vmspace_mmap_args
*uap
)
288 struct vkernel_proc
*vkp
;
289 struct vmspace_entry
*ve
;
292 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
297 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
302 error
= kern_mmap(ve
->vmspace
, uap
->addr
, uap
->len
,
303 uap
->prot
, uap
->flags
,
304 uap
->fd
, uap
->offset
, &uap
->sysmsg_resultp
);
306 vmspace_entry_drop(ve
);
312 * vmspace_munmap(id, addr, len)
314 * unmap memory within a VMSPACE.
319 sys_vmspace_munmap(struct vmspace_munmap_args
*uap
)
321 struct vkernel_proc
*vkp
;
322 struct vmspace_entry
*ve
;
325 vm_size_t size
, pageoff
;
329 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
334 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
340 * NOTE: kern_munmap() can block so we need to temporarily
345 * Copied from sys_munmap()
347 addr
= (vm_offset_t
)uap
->addr
;
350 pageoff
= (addr
& PAGE_MASK
);
353 size
= (vm_size_t
)round_page(size
);
354 if (size
< uap
->len
) { /* wrap */
358 tmpaddr
= addr
+ size
; /* workaround gcc4 opt */
359 if (tmpaddr
< addr
) { /* wrap */
368 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
) {
372 if (VM_MIN_USER_ADDRESS
> 0 && addr
< VM_MIN_USER_ADDRESS
) {
376 map
= &ve
->vmspace
->vm_map
;
377 if (!vm_map_check_protection(map
, addr
, tmpaddr
, VM_PROT_NONE
, FALSE
)) {
381 vm_map_remove(map
, addr
, addr
+ size
);
384 vmspace_entry_drop(ve
);
390 * vmspace_pread(id, buf, nbyte, flags, offset)
392 * Read data from a vmspace. The number of bytes read is returned or
393 * -1 if an unrecoverable error occured. If the number of bytes read is
394 * less then the request size, a page fault occured in the VMSPACE which
395 * the caller must resolve in order to proceed.
397 * (not implemented yet)
401 sys_vmspace_pread(struct vmspace_pread_args
*uap
)
403 struct vkernel_proc
*vkp
;
404 struct vmspace_entry
*ve
;
407 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
412 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
416 vmspace_entry_drop(ve
);
423 * vmspace_pwrite(id, buf, nbyte, flags, offset)
425 * Write data to a vmspace. The number of bytes written is returned or
426 * -1 if an unrecoverable error occured. If the number of bytes written is
427 * less then the request size, a page fault occured in the VMSPACE which
428 * the caller must resolve in order to proceed.
430 * (not implemented yet)
434 sys_vmspace_pwrite(struct vmspace_pwrite_args
*uap
)
436 struct vkernel_proc
*vkp
;
437 struct vmspace_entry
*ve
;
440 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
444 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
448 vmspace_entry_drop(ve
);
455 * vmspace_mcontrol(id, addr, len, behav, value)
457 * madvise/mcontrol support for a vmspace.
462 sys_vmspace_mcontrol(struct vmspace_mcontrol_args
*uap
)
464 struct vkernel_proc
*vkp
;
465 struct vmspace_entry
*ve
;
467 vm_offset_t start
, end
;
468 vm_offset_t tmpaddr
= (vm_offset_t
)uap
->addr
+ uap
->len
;
471 lp
= curthread
->td_lwp
;
472 if ((vkp
= curproc
->p_vkernel
) == NULL
) {
477 if ((ve
= vkernel_find_vmspace(vkp
, uap
->id
, 0)) == NULL
) {
483 * This code is basically copied from sys_mcontrol()
485 if (uap
->behav
< 0 || uap
->behav
> MADV_CONTROL_END
) {
490 if (tmpaddr
< (vm_offset_t
)uap
->addr
) {
494 if (VM_MAX_USER_ADDRESS
> 0 && tmpaddr
> VM_MAX_USER_ADDRESS
) {
498 if (VM_MIN_USER_ADDRESS
> 0 && uap
->addr
< VM_MIN_USER_ADDRESS
) {
503 start
= trunc_page((vm_offset_t
) uap
->addr
);
504 end
= round_page(tmpaddr
);
506 error
= vm_map_madvise(&ve
->vmspace
->vm_map
, start
, end
,
507 uap
->behav
, uap
->value
);
509 vmspace_entry_drop(ve
);
515 * Red black tree functions
517 static int rb_vmspace_compare(struct vmspace_entry
*, struct vmspace_entry
*);
518 RB_GENERATE(vmspace_rb_tree
, vmspace_entry
, rb_entry
, rb_vmspace_compare
);
521 * a->start is address, and the only field has to be initialized.
522 * The caller must hold vkp->token.
524 * The caller must hold vkp->token.
527 rb_vmspace_compare(struct vmspace_entry
*a
, struct vmspace_entry
*b
)
529 if ((char *)a
->id
< (char *)b
->id
)
531 else if ((char *)a
->id
> (char *)b
->id
)
537 * The caller must hold vkp->token.
541 rb_vmspace_delete(struct vmspace_entry
*ve
, void *data
)
543 struct vkernel_proc
*vkp
= data
;
545 if (vmspace_entry_delete(ve
, vkp
, 0) == 0)
546 vmspace_entry_cache_drop(ve
);
548 panic("rb_vmspace_delete: invalid refs %d", ve
->refs
);
553 * Remove a vmspace_entry from the RB tree and destroy it. We have to clean
554 * up the pmap, the vm_map, then destroy the vmspace. We gain control of
555 * the associated cache_refs ref, which the caller will drop for us.
557 * The ve must not have any active references other than those from the
558 * caller. If it does, EBUSY is returned. The ve may still maintain
559 * any number of cache references which will drop as the related LWPs
560 * execute vmspace operations or exit.
562 * 0 is returned on success, EBUSY on failure. On success the caller must
563 * drop the last cache_refs. We have dropped the callers active refs.
565 * The caller must hold vkp->token.
569 vmspace_entry_delete(struct vmspace_entry
*ve
, struct vkernel_proc
*vkp
,
573 * Interlocked by vkp->token.
575 * Drop the callers refs and set VKE_REF_DELETED atomically, if
576 * the remaining refs match exactly. Dropping refs and setting
577 * the DELETED flag atomically protects other threads from trying
580 * The caller now owns the final cache_ref that was previously
581 * associated with the live state of the ve.
583 if (atomic_cmpset_int(&ve
->refs
, refs
, VKE_REF_DELETED
) == 0) {
584 KKASSERT(ve
->refs
>= refs
);
587 RB_REMOVE(vmspace_rb_tree
, &vkp
->root
, ve
);
589 pmap_remove_pages(vmspace_pmap(ve
->vmspace
),
590 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
591 vm_map_remove(&ve
->vmspace
->vm_map
,
592 VM_MIN_USER_ADDRESS
, VM_MAX_USER_ADDRESS
);
593 vmspace_rel(ve
->vmspace
);
594 ve
->vmspace
= NULL
; /* safety */
600 * Ref a ve for cache purposes
604 vmspace_entry_cache_ref(struct vmspace_entry
*ve
)
606 atomic_add_int(&ve
->cache_refs
, 1);
610 * The ve cache_drop is the final word for a ve. It gains an extra ref
611 * representing it being on the RB tree and not being in a deleted state.
612 * Removal from the RB tree and deletion manipulate this ref. The last
613 * drop will thus include full deletion of the ve in addition to the last
614 * cached user going away.
618 vmspace_entry_cache_drop(struct vmspace_entry
*ve
)
620 if (atomic_fetchadd_int(&ve
->cache_refs
, -1) == 1) {
621 KKASSERT(ve
->refs
& VKE_REF_DELETED
);
622 kfree(ve
, M_VKERNEL
);
627 * Drop primary reference. The ve cannot be freed on the 1->0 transition.
628 * Instead, ve deletion interlocks the final kfree() via cache_refs.
632 vmspace_entry_drop(struct vmspace_entry
*ve
)
634 atomic_fetchadd_int(&ve
->refs
, -1);
638 * Locate the ve for (id), return the ve or NULL. If found this function
639 * will bump ve->refs which prevents the ve from being immediately destroyed
640 * (but it can still be removed).
642 * The cache can potentially contain a stale ve, check by testing ve->vmspace.
644 * The caller must hold vkp->token if excl is non-zero.
647 struct vmspace_entry
*
648 vkernel_find_vmspace(struct vkernel_proc
*vkp
, void *id
, int excl
)
650 struct vmspace_entry
*ve
;
651 struct vmspace_entry key
;
652 struct vkernel_lwp
*vklp
;
653 struct lwp
*lp
= curthread
->td_lwp
;
656 * Cache check. Since we already hold a ref on the cache entry
657 * the ve cannot be ripped out from under us while we cycle
660 if ((vklp
= lp
->lwp_vkernel
) != NULL
) {
662 if (ve
&& ve
->id
== id
) {
666 * Bump active refs, check to see if the cache
667 * entry is stale. If not, we are good.
669 n
= atomic_fetchadd_int(&ve
->refs
, 1);
670 if ((n
& VKE_REF_DELETED
) == 0) {
671 KKASSERT(ve
->vmspace
);
676 * Cache is stale, clean it out and fall through
677 * to a normal search.
679 vklp
->ve_cache
= NULL
;
680 vmspace_entry_drop(ve
);
681 vmspace_entry_cache_drop(ve
);
686 * Normal search protected by vkp->token. No new ve's can be marked
687 * DELETED while we hold the token so we are safe.
690 lwkt_gettoken_shared(&vkp
->token
);
692 ve
= RB_FIND(vmspace_rb_tree
, &vkp
->root
, &key
);
694 if (atomic_fetchadd_int(&ve
->refs
, 1) & VKE_REF_DELETED
) {
695 vmspace_entry_drop(ve
);
700 lwkt_reltoken(&vkp
->token
);
705 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
711 vkernel_inherit(struct proc
*p1
, struct proc
*p2
)
713 struct vkernel_proc
*vkp
;
716 KKASSERT(vkp
->refs
> 0);
717 atomic_add_int(&vkp
->refs
, 1);
725 vkernel_exit(struct proc
*p
)
727 struct vkernel_proc
*vkp
;
733 * Restore the original VM context if we are killed while running
736 * This isn't supposed to happen. What is supposed to happen is
737 * that the process should enter vkernel_trap() before the handling
740 RB_FOREACH(lp
, lwp_rb_tree
, &p
->p_lwp_tree
) {
741 vkernel_lwp_exit(lp
);
745 * Dereference the common area
748 KKASSERT(vkp
->refs
> 0);
750 if (atomic_fetchadd_int(&vkp
->refs
, -1) == 1) {
751 lwkt_gettoken(&vkp
->token
);
752 RB_SCAN(vmspace_rb_tree
, &vkp
->root
, NULL
,
753 rb_vmspace_delete
, vkp
);
754 lwkt_reltoken(&vkp
->token
);
755 kfree(vkp
, M_VKERNEL
);
763 vkernel_lwp_exit(struct lwp
*lp
)
765 struct vkernel_lwp
*vklp
;
766 struct vmspace_entry
*ve
;
768 if ((vklp
= lp
->lwp_vkernel
) != NULL
) {
769 if (lp
->lwp_thread
->td_vmm
== NULL
) {
773 if ((ve
= vklp
->ve
) != NULL
) {
774 kprintf("Warning, pid %d killed with "
775 "active VC!\n", lp
->lwp_proc
->p_pid
);
776 pmap_setlwpvm(lp
, lp
->lwp_proc
->p_vmspace
);
778 KKASSERT(ve
->refs
> 0);
779 vmspace_entry_drop(ve
);
787 if ((ve
= vklp
->ve_cache
) != NULL
) {
788 vklp
->ve_cache
= NULL
;
789 vmspace_entry_cache_drop(ve
);
792 lp
->lwp_vkernel
= NULL
;
793 kfree(vklp
, M_VKERNEL
);
798 * A VM space under virtual kernel control trapped out or made a system call
799 * or otherwise needs to return control to the virtual kernel context.
804 vkernel_trap(struct lwp
*lp
, struct trapframe
*frame
)
806 struct proc
*p
= lp
->lwp_proc
;
807 struct vmspace_entry
*ve
;
808 struct vkernel_lwp
*vklp
;
812 * Which vmspace entry was running?
814 vklp
= lp
->lwp_vkernel
;
817 /* If it's a VMM thread just set the vkernel CR3 back */
818 if (curthread
->td_vmm
== NULL
) {
820 KKASSERT(ve
!= NULL
);
823 * Switch the LWP vmspace back to the virtual kernel's VM space.
826 pmap_setlwpvm(lp
, p
->p_vmspace
);
827 KKASSERT(ve
->refs
> 0);
828 vmspace_entry_drop(ve
);
829 /* ve is invalid once we kill our ref */
832 vmm_vm_set_guest_cr3(p
->p_vkernel
->vkernel_cr3
);
836 * Copy the emulated process frame to the virtual kernel process.
837 * The emulated process cannot change TLS descriptors so don't
838 * bother saving them, we already have a copy.
840 * Restore the virtual kernel's saved context so the virtual kernel
841 * process can resume.
843 error
= copyout(frame
, vklp
->user_trapframe
, sizeof(*frame
));
844 bcopy(&vklp
->save_trapframe
, frame
, sizeof(*frame
));
845 bcopy(&vklp
->save_vextframe
.vx_tls
, &curthread
->td_tls
,
846 sizeof(vklp
->save_vextframe
.vx_tls
));
848 cpu_vkernel_trap(frame
, error
);