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>
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;
83 struct proc *p = curproc;
86 if (vkernel_enable == 0)
90 * Create a virtual kernel side-structure for the process if one
93 * Implement a simple resolution for SMP races.
95 if ((vkp = p->p_vkernel) == NULL) {
96 vkp = kmalloc(sizeof(*vkp), M_VKERNEL, M_WAITOK|M_ZERO);
97 lwkt_gettoken(&proc_token);
98 if (p->p_vkernel == NULL) {
100 lwkt_token_init(&vkp->token, "vkernel");
104 kfree(vkp, M_VKERNEL);
107 lwkt_reltoken(&proc_token);
113 * Create a new VMSPACE, disallow conflicting ids
115 ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
116 ve->vmspace = vmspace_alloc(VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
118 pmap_pinit2(vmspace_pmap(ve->vmspace));
120 lwkt_gettoken(&vkp->token);
121 if (RB_INSERT(vmspace_rb_tree, &vkp->root, ve)) {
122 sysref_put(&ve->vmspace->vm_sysref);
123 kfree(ve, M_VKERNEL);
128 lwkt_reltoken(&vkp->token);
134 * Destroy a VMSPACE given its identifier.
139 sys_vmspace_destroy(struct vmspace_destroy_args *uap)
141 struct vkernel_proc *vkp;
142 struct vmspace_entry *ve;
146 if ((vkp = curproc->p_vkernel) == NULL) {
150 lwkt_gettoken(&vkp->token);
151 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
159 vmspace_entry_delete(ve, vkp);
162 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;
189 lp = curthread->td_lwp;
192 if ((vkp = p->p_vkernel) == NULL)
196 lwkt_gettoken(&vkp->token);
197 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
203 case VMSPACE_CTL_RUN:
205 * Save the caller's register context, swap VM spaces, and
206 * install the passed register context. Return with
207 * EJUSTRETURN so the syscall code doesn't adjust the context.
209 atomic_add_int(&ve->refs, 1);
210 framesz = sizeof(struct trapframe);
211 if ((vklp = lp->lwp_vkernel) == NULL) {
212 vklp = kmalloc(sizeof(*vklp), M_VKERNEL,
214 lp->lwp_vkernel = vklp;
216 vklp->user_trapframe = uap->tframe;
217 vklp->user_vextframe = uap->vframe;
218 bcopy(uap->sysmsg_frame, &vklp->save_trapframe, framesz);
219 bcopy(&curthread->td_tls, &vklp->save_vextframe.vx_tls,
220 sizeof(vklp->save_vextframe.vx_tls));
221 error = copyin(uap->tframe, uap->sysmsg_frame, framesz);
223 error = copyin(&uap->vframe->vx_tls,
225 sizeof(struct savetls));
228 error = cpu_sanitize_frame(uap->sysmsg_frame);
230 error = cpu_sanitize_tls(&curthread->td_tls);
232 bcopy(&vklp->save_trapframe, uap->sysmsg_frame,
234 bcopy(&vklp->save_vextframe.vx_tls, &curthread->td_tls,
235 sizeof(vklp->save_vextframe.vx_tls));
237 atomic_subtract_int(&ve->refs, 1);
240 pmap_setlwpvm(lp, ve->vmspace);
242 set_vkernel_fp(uap->sysmsg_frame);
251 lwkt_reltoken(&vkp->token);
257 * vmspace_mmap(id, addr, len, prot, flags, fd, offset)
259 * map memory within a VMSPACE. This function is just like a normal mmap()
260 * but operates on the vmspace's memory map. Most callers use this to create
261 * a MAP_VPAGETABLE mapping.
266 sys_vmspace_mmap(struct vmspace_mmap_args *uap)
268 struct vkernel_proc *vkp;
269 struct vmspace_entry *ve;
273 * We hold the vmspace token to serialize calls to vkernel_find_vmspace.
275 lwkt_gettoken(&vmspace_token);
276 if ((vkp = curproc->p_vkernel) == NULL) {
282 * NOTE: kern_mmap() can block so we need to temporarily ref ve->refs.
284 lwkt_gettoken(&vkp->token);
285 if ((ve = vkernel_find_vmspace(vkp, uap->id)) != NULL) {
286 atomic_add_int(&ve->refs, 1);
287 error = kern_mmap(ve->vmspace, uap->addr, uap->len,
288 uap->prot, uap->flags,
289 uap->fd, uap->offset, &uap->sysmsg_resultp);
290 atomic_subtract_int(&ve->refs, 1);
294 lwkt_reltoken(&vkp->token);
296 lwkt_reltoken(&vmspace_token);
301 * vmspace_munmap(id, addr, len)
303 * unmap memory within a VMSPACE.
308 sys_vmspace_munmap(struct vmspace_munmap_args *uap)
310 struct vkernel_proc *vkp;
311 struct vmspace_entry *ve;
314 vm_size_t size, pageoff;
319 if ((vkp = curproc->p_vkernel) == NULL) {
323 lwkt_gettoken(&vkp->token);
324 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
330 * NOTE: kern_munmap() can block so we need to temporarily
333 atomic_add_int(&ve->refs, 1);
336 * Copied from sys_munmap()
338 addr = (vm_offset_t)uap->addr;
341 pageoff = (addr & PAGE_MASK);
344 size = (vm_size_t)round_page(size);
345 if (size < uap->len) { /* wrap */
349 tmpaddr = addr + size; /* workaround gcc4 opt */
350 if (tmpaddr < addr) { /* wrap */
359 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) {
363 if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) {
367 map = &ve->vmspace->vm_map;
368 if (!vm_map_check_protection(map, addr, tmpaddr, VM_PROT_NONE, FALSE)) {
372 vm_map_remove(map, addr, addr + size);
375 atomic_subtract_int(&ve->refs, 1);
377 lwkt_reltoken(&vkp->token);
384 * vmspace_pread(id, buf, nbyte, flags, offset)
386 * Read data from a vmspace. The number of bytes read is returned or
387 * -1 if an unrecoverable error occured. If the number of bytes read is
388 * less then the request size, a page fault occured in the VMSPACE which
389 * the caller must resolve in order to proceed.
391 * (not implemented yet)
395 sys_vmspace_pread(struct vmspace_pread_args *uap)
397 struct vkernel_proc *vkp;
398 struct vmspace_entry *ve;
402 if ((vkp = curproc->p_vkernel) == NULL) {
406 lwkt_gettoken(&vkp->token);
407 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
413 lwkt_reltoken(&vkp->token);
420 * vmspace_pwrite(id, buf, nbyte, flags, offset)
422 * Write data to a vmspace. The number of bytes written is returned or
423 * -1 if an unrecoverable error occured. If the number of bytes written is
424 * less then the request size, a page fault occured in the VMSPACE which
425 * the caller must resolve in order to proceed.
427 * (not implemented yet)
431 sys_vmspace_pwrite(struct vmspace_pwrite_args *uap)
433 struct vkernel_proc *vkp;
434 struct vmspace_entry *ve;
438 if ((vkp = curproc->p_vkernel) == NULL) {
442 lwkt_gettoken(&vkp->token);
443 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
449 lwkt_reltoken(&vkp->token);
456 * vmspace_mcontrol(id, addr, len, behav, value)
458 * madvise/mcontrol support for a vmspace.
463 sys_vmspace_mcontrol(struct vmspace_mcontrol_args *uap)
465 struct vkernel_proc *vkp;
466 struct vmspace_entry *ve;
467 vm_offset_t start, end;
468 vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
472 if ((vkp = curproc->p_vkernel) == NULL) {
476 lwkt_gettoken(&vkp->token);
477 if ((ve = vkernel_find_vmspace(vkp, uap->id)) == NULL) {
483 * NOTE: kern_madvise() can block so we need to temporarily
486 atomic_add_int(&ve->refs, 1);
489 * This code is basically copied from sys_mcontrol()
491 if (uap->behav < 0 || uap->behav > MADV_CONTROL_END) {
496 if (tmpaddr < (vm_offset_t)uap->addr) {
500 if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) {
504 if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) {
509 start = trunc_page((vm_offset_t) uap->addr);
510 end = round_page(tmpaddr);
512 error = vm_map_madvise(&ve->vmspace->vm_map, start, end,
513 uap->behav, uap->value);
515 atomic_subtract_int(&ve->refs, 1);
517 lwkt_reltoken(&vkp->token);
524 * Red black tree functions
526 static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
527 RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
530 * a->start is address, and the only field has to be initialized.
531 * The caller must hold vkp->token.
533 * The caller must hold vkp->token.
536 rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
538 if ((char *)a->id < (char *)b->id)
540 else if ((char *)a->id > (char *)b->id)
546 * The caller must hold vkp->token.
550 rb_vmspace_delete(struct vmspace_entry *ve, void *data)
552 struct vkernel_proc *vkp = data;
554 KKASSERT(ve->refs == 0);
555 vmspace_entry_delete(ve, vkp);
560 * Remove a vmspace_entry from the RB tree and destroy it. We have to clean
561 * up the pmap, the vm_map, then destroy the vmspace.
563 * This function must remove the ve immediately before it might potentially
566 * The caller must hold vkp->token.
570 vmspace_entry_delete(struct vmspace_entry *ve, struct vkernel_proc *vkp)
572 RB_REMOVE(vmspace_rb_tree, &vkp->root, ve);
574 pmap_remove_pages(vmspace_pmap(ve->vmspace),
575 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
576 vm_map_remove(&ve->vmspace->vm_map,
577 VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
578 sysref_put(&ve->vmspace->vm_sysref);
579 kfree(ve, M_VKERNEL);
583 * Locate the ve for (id), return the ve or NULL. If found this function
584 * will bump ve->refs which prevents the ve from being immediately destroyed
585 * (but it can still be removed).
587 * The caller must hold vkp->token.
590 struct vmspace_entry *
591 vkernel_find_vmspace(struct vkernel_proc *vkp, void *id)
593 struct vmspace_entry *ve;
594 struct vmspace_entry key;
597 ve = RB_FIND(vmspace_rb_tree, &vkp->root, &key);
602 * Manage vkernel refs, used by the kernel when fork()ing or exit()ing
608 vkernel_inherit(struct proc *p1, struct proc *p2)
610 struct vkernel_proc *vkp;
613 KKASSERT(vkp->refs > 0);
614 atomic_add_int(&vkp->refs, 1);
622 vkernel_exit(struct proc *p)
624 struct vkernel_proc *vkp;
630 * Restore the original VM context if we are killed while running
633 * This isn't supposed to happen. What is supposed to happen is
634 * that the process should enter vkernel_trap() before the handling
637 RB_FOREACH(lp, lwp_rb_tree, &p->p_lwp_tree) {
638 vkernel_lwp_exit(lp);
642 * Dereference the common area
645 KKASSERT(vkp->refs > 0);
647 if (atomic_fetchadd_int(&vkp->refs, -1) == 1) {
648 lwkt_gettoken(&vkp->token);
649 RB_SCAN(vmspace_rb_tree, &vkp->root, NULL,
650 rb_vmspace_delete, vkp);
651 lwkt_reltoken(&vkp->token);
652 kfree(vkp, M_VKERNEL);
660 vkernel_lwp_exit(struct lwp *lp)
662 struct vkernel_lwp *vklp;
663 struct vmspace_entry *ve;
665 if ((vklp = lp->lwp_vkernel) != NULL) {
666 if ((ve = vklp->ve) != NULL) {
667 kprintf("Warning, pid %d killed with "
668 "active VC!\n", lp->lwp_proc->p_pid);
669 pmap_setlwpvm(lp, lp->lwp_proc->p_vmspace);
671 KKASSERT(ve->refs > 0);
672 atomic_subtract_int(&ve->refs, 1);
674 lp->lwp_vkernel = NULL;
675 kfree(vklp, M_VKERNEL);
680 * A VM space under virtual kernel control trapped out or made a system call
681 * or otherwise needs to return control to the virtual kernel context.
686 vkernel_trap(struct lwp *lp, struct trapframe *frame)
688 struct proc *p = lp->lwp_proc;
689 struct vmspace_entry *ve;
690 struct vkernel_lwp *vklp;
694 * Which vmspace entry was running?
696 vklp = lp->lwp_vkernel;
699 KKASSERT(ve != NULL);
702 * Switch the LWP vmspace back to the virtual kernel's VM space.
705 pmap_setlwpvm(lp, p->p_vmspace);
706 KKASSERT(ve->refs > 0);
707 atomic_subtract_int(&ve->refs, 1);
708 /* ve is invalid once we kill our ref */
711 * Copy the emulated process frame to the virtual kernel process.
712 * The emulated process cannot change TLS descriptors so don't
713 * bother saving them, we already have a copy.
715 * Restore the virtual kernel's saved context so the virtual kernel
716 * process can resume.
718 error = copyout(frame, vklp->user_trapframe, sizeof(*frame));
719 bcopy(&vklp->save_trapframe, frame, sizeof(*frame));
720 bcopy(&vklp->save_vextframe.vx_tls, &curthread->td_tls,
721 sizeof(vklp->save_vextframe.vx_tls));
723 cpu_vkernel_trap(frame, error);