#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/sysmsg.h>
#include <sys/kern_syscall.h>
#include <sys/mman.h>
#include <sys/thread.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/sysctl.h>
#include <sys/vkernel.h>
#include <sys/vmspace.h>
#include <vm/vm_extern.h>
#include <vm/pmap.h>
#include <machine/vmparam.h>
static struct vmspace_entry *vkernel_find_vmspace(struct vkernel_proc *vkp,
void *id, int havetoken);
static int vmspace_entry_delete(struct vmspace_entry *ve,
struct vkernel_proc *vkp, int refs);
static void vmspace_entry_cache_ref(struct vmspace_entry *ve);
static void vmspace_entry_cache_drop(struct vmspace_entry *ve);
static void vmspace_entry_drop(struct vmspace_entry *ve);
static void ve_cache_enter(struct vkernel_lwp *vklp, struct vmspace_entry *ve);
static struct vmspace_entry *ve_cache_find(struct vkernel_lwp *vklp, void *id);
static MALLOC_DEFINE(M_VKERNEL, "vkernel", "VKernel structures");
static
void
ve_cache_enter(struct vkernel_lwp *vklp, struct vmspace_entry *ve)
{
int i;
++vklp->ve_cache_rover;
i = vklp->ve_cache_rover & (VE_CACHE_COUNT - 1);
vmspace_entry_cache_ref(ve);
if (vklp->ve_cache[i])
vmspace_entry_cache_drop(vklp->ve_cache[i]);
vklp->ve_cache[i] = ve;
}
static
struct vmspace_entry *
ve_cache_find(struct vkernel_lwp *vklp, void *id)
{
struct vmspace_entry *ve;
uint32_t n;
int i;
for (i = 0; i < VE_CACHE_COUNT; ++i) {
ve = vklp->ve_cache[i];
if (ve == NULL)
continue;
if (ve->id == id) {
n = atomic_fetchadd_int(&ve->refs, 1);
if ((n & VKE_REF_DELETED) == 0) {
KKASSERT(ve->vmspace);
return ve;
}
vklp->ve_cache[i] = NULL;
vmspace_entry_drop(ve);
vmspace_entry_cache_drop(ve);
} else if (ve->refs & VKE_REF_DELETED) {
n = atomic_fetchadd_int(&ve->refs, 1);
if (n & VKE_REF_DELETED) {
vklp->ve_cache[i] = NULL;
vmspace_entry_drop(ve);
vmspace_entry_cache_drop(ve);
} else {
vmspace_entry_drop(ve);
}
}
}
return NULL;
}
int
sys_vmspace_create(struct sysmsg *sysmsg,
const struct vmspace_create_args *uap)
{
struct vmspace_entry *ve;
struct vkernel_proc *vkp;
struct proc *p = curproc;
int error;
if (vkernel_enable == 0)
return (EOPNOTSUPP);
if ((vkp = p->p_vkernel) == NULL) {
vkp = kmalloc(sizeof(*vkp), M_VKERNEL, M_WAITOK|M_ZERO);
lwkt_gettoken(&p->p_token);
if (p->p_vkernel == NULL) {
vkp->refs = 1;
lwkt_token_init(&vkp->token, "vkernel");
RB_INIT(&vkp->root);
p->p_vkernel = vkp;
} else {
kfree(vkp, M_VKERNEL);
vkp = p->p_vkernel;
}
lwkt_reltoken(&p->p_token);
}
ve = kmalloc(sizeof(struct vmspace_entry), M_VKERNEL, M_WAITOK|M_ZERO);
ve->vmspace = vmspace_alloc(VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
ve->id = uap->id;
ve->refs = 0;
ve->cache_refs = 1;
pmap_pinit2(vmspace_pmap(ve->vmspace));
lwkt_gettoken(&vkp->token);
if (RB_INSERT(vmspace_rb_tree, &vkp->root, ve)) {
vmspace_rel(ve->vmspace);
ve->vmspace = NULL;
kfree(ve, M_VKERNEL);
error = EEXIST;
} else {
error = 0;
}
lwkt_reltoken(&vkp->token);
return (error);
}
int
sys_vmspace_destroy(struct sysmsg *sysmsg,
const struct vmspace_destroy_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
int error;
if ((vkp = curproc->p_vkernel) == NULL)
return EINVAL;
lwkt_gettoken(&vkp->token);
error = ENOENT;
if ((ve = vkernel_find_vmspace(vkp, uap->id, 1)) != NULL) {
error = vmspace_entry_delete(ve, vkp, 1);
if (error == 0)
vmspace_entry_cache_drop(ve);
}
lwkt_reltoken(&vkp->token);
return(error);
}
int
sys_vmspace_ctl(struct sysmsg *sysmsg,
const struct vmspace_ctl_args *uap)
{
struct vmspace_ctl_args ua = *uap;
struct vkernel_proc *vkp;
struct vkernel_lwp *vklp;
struct vmspace_entry *ve = NULL;
struct lwp *lp;
struct proc *p;
int framesz;
int error;
lp = curthread->td_lwp;
p = lp->lwp_proc;
if ((vkp = p->p_vkernel) == NULL)
return (EINVAL);
if ((ve = vkernel_find_vmspace(vkp, ua.id, 0)) == NULL) {
error = ENOENT;
goto done;
}
switch(ua.cmd) {
case VMSPACE_CTL_RUN:
framesz = sizeof(struct trapframe);
if ((vklp = lp->lwp_vkernel) == NULL) {
vklp = kmalloc(sizeof(*vklp), M_VKERNEL,
M_WAITOK|M_ZERO);
lp->lwp_vkernel = vklp;
ve_cache_enter(vklp, ve);
}
vklp->user_trapframe = ua.tframe;
vklp->user_vextframe = ua.vframe;
bcopy(sysmsg->sysmsg_frame, &vklp->save_trapframe, framesz);
bcopy(&curthread->td_tls, &vklp->save_vextframe.vx_tls,
sizeof(vklp->save_vextframe.vx_tls));
error = copyin(ua.tframe, sysmsg->sysmsg_frame, framesz);
if (error == 0) {
error = copyin(&ua.vframe->vx_tls,
&curthread->td_tls,
sizeof(struct savetls));
}
if (error == 0)
error = cpu_sanitize_frame(sysmsg->sysmsg_frame);
if (error == 0)
error = cpu_sanitize_tls(&curthread->td_tls);
if (error) {
bcopy(&vklp->save_trapframe, sysmsg->sysmsg_frame,
framesz);
bcopy(&vklp->save_vextframe.vx_tls, &curthread->td_tls,
sizeof(vklp->save_vextframe.vx_tls));
set_user_TLS();
} else {
vklp->ve = ve;
atomic_add_int(&ve->refs, 1);
pmap_setlwpvm(lp, ve->vmspace);
set_user_TLS();
set_vkernel_fp(sysmsg->sysmsg_frame);
error = EJUSTRETURN;
}
break;
default:
error = EOPNOTSUPP;
break;
}
done:
if (ve)
vmspace_entry_drop(ve);
return(error);
}
int
sys_vmspace_mmap(struct sysmsg *sysmsg,
const struct vmspace_mmap_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
int error;
if ((vkp = curproc->p_vkernel) == NULL) {
error = EINVAL;
goto done2;
}
if ((ve = vkernel_find_vmspace(vkp, uap->id, 0)) == NULL) {
error = ENOENT;
goto done2;
}
error = kern_mmap(ve->vmspace, uap->addr, uap->len,
uap->prot, uap->flags,
uap->fd, uap->offset, &sysmsg->sysmsg_resultp);
vmspace_entry_drop(ve);
done2:
return (error);
}
int
sys_vmspace_munmap(struct sysmsg *sysmsg,
const struct vmspace_munmap_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
vm_offset_t addr;
vm_offset_t tmpaddr;
vm_size_t size, pageoff;
vm_map_t map;
int error;
if ((vkp = curproc->p_vkernel) == NULL) {
error = EINVAL;
goto done2;
}
if ((ve = vkernel_find_vmspace(vkp, uap->id, 0)) == NULL) {
error = ENOENT;
goto done2;
}
addr = (vm_offset_t)uap->addr;
size = uap->len;
pageoff = (addr & PAGE_MASK);
addr -= pageoff;
size += pageoff;
size = (vm_size_t)round_page(size);
if (size < uap->len) {
error = EINVAL;
goto done1;
}
tmpaddr = addr + size;
if (tmpaddr < addr) {
error = EINVAL;
goto done1;
}
if (size == 0) {
error = 0;
goto done1;
}
if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) {
error = EINVAL;
goto done1;
}
if (VM_MIN_USER_ADDRESS > 0 && addr < VM_MIN_USER_ADDRESS) {
error = EINVAL;
goto done1;
}
map = &ve->vmspace->vm_map;
if (!vm_map_check_protection(map, addr, tmpaddr, VM_PROT_NONE, FALSE)) {
error = EINVAL;
goto done1;
}
vm_map_remove(map, addr, addr + size);
error = 0;
done1:
vmspace_entry_drop(ve);
done2:
return (error);
}
int
sys_vmspace_pread(struct sysmsg *sysmsg,
const struct vmspace_pread_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
int error;
if ((vkp = curproc->p_vkernel) == NULL) {
error = EINVAL;
goto done3;
}
if ((ve = vkernel_find_vmspace(vkp, uap->id, 0)) == NULL) {
error = ENOENT;
goto done3;
}
vmspace_entry_drop(ve);
error = EINVAL;
done3:
return (error);
}
int
sys_vmspace_pwrite(struct sysmsg *sysmsg,
const struct vmspace_pwrite_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
int error;
if ((vkp = curproc->p_vkernel) == NULL) {
error = EINVAL;
goto done3;
}
if ((ve = vkernel_find_vmspace(vkp, uap->id, 0)) == NULL) {
error = ENOENT;
goto done3;
}
vmspace_entry_drop(ve);
error = EINVAL;
done3:
return (error);
}
int
sys_vmspace_mcontrol(struct sysmsg *sysmsg,
const struct vmspace_mcontrol_args *uap)
{
struct vkernel_proc *vkp;
struct vmspace_entry *ve;
struct lwp *lp;
vm_offset_t start, end;
vm_offset_t tmpaddr = (vm_offset_t)uap->addr + uap->len;
int error;
lp = curthread->td_lwp;
if ((vkp = curproc->p_vkernel) == NULL) {
error = EINVAL;
goto done3;
}
if ((ve = vkernel_find_vmspace(vkp, uap->id, 0)) == NULL) {
error = ENOENT;
goto done3;
}
if (uap->behav < 0 || uap->behav > MADV_CONTROL_END) {
error = EINVAL;
goto done1;
}
if (tmpaddr < (vm_offset_t)uap->addr) {
error = EINVAL;
goto done1;
}
if (VM_MAX_USER_ADDRESS > 0 && tmpaddr > VM_MAX_USER_ADDRESS) {
error = EINVAL;
goto done1;
}
if (VM_MIN_USER_ADDRESS > 0 && uap->addr < VM_MIN_USER_ADDRESS) {
error = EINVAL;
goto done1;
}
start = trunc_page((vm_offset_t) uap->addr);
end = round_page(tmpaddr);
error = vm_map_madvise(&ve->vmspace->vm_map, start, end,
uap->behav, uap->value);
done1:
vmspace_entry_drop(ve);
done3:
return (error);
}
static int rb_vmspace_compare(struct vmspace_entry *, struct vmspace_entry *);
RB_GENERATE(vmspace_rb_tree, vmspace_entry, rb_entry, rb_vmspace_compare);
static int
rb_vmspace_compare(struct vmspace_entry *a, struct vmspace_entry *b)
{
if ((char *)a->id < (char *)b->id)
return(-1);
else if ((char *)a->id > (char *)b->id)
return(1);
return(0);
}
static
int
rb_vmspace_delete(struct vmspace_entry *ve, void *data)
{
struct vkernel_proc *vkp = data;
if (vmspace_entry_delete(ve, vkp, 0) == 0)
vmspace_entry_cache_drop(ve);
else
panic("rb_vmspace_delete: invalid refs %d", ve->refs);
return(0);
}
static
int
vmspace_entry_delete(struct vmspace_entry *ve, struct vkernel_proc *vkp,
int refs)
{
if (atomic_cmpset_int(&ve->refs, refs, VKE_REF_DELETED) == 0) {
KKASSERT(ve->refs >= refs);
return EBUSY;
}
RB_REMOVE(vmspace_rb_tree, &vkp->root, ve);
lwkt_reltoken(&vkp->token);
pmap_remove_pages(vmspace_pmap(ve->vmspace),
VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
vm_map_remove(&ve->vmspace->vm_map,
VM_MIN_USER_ADDRESS, VM_MAX_USER_ADDRESS);
vmspace_rel(ve->vmspace);
ve->vmspace = NULL;
lwkt_gettoken(&vkp->token);
return 0;
}
static
void
vmspace_entry_cache_ref(struct vmspace_entry *ve)
{
atomic_add_int(&ve->cache_refs, 1);
}
static
void
vmspace_entry_cache_drop(struct vmspace_entry *ve)
{
if (atomic_fetchadd_int(&ve->cache_refs, -1) == 1) {
KKASSERT(ve->refs & VKE_REF_DELETED);
kfree(ve, M_VKERNEL);
}
}
static
void
vmspace_entry_drop(struct vmspace_entry *ve)
{
atomic_fetchadd_int(&ve->refs, -1);
}
static
struct vmspace_entry *
vkernel_find_vmspace(struct vkernel_proc *vkp, void *id, int excl)
{
struct vmspace_entry *ve;
struct vmspace_entry key;
struct vkernel_lwp *vklp;
struct lwp *lp = curthread->td_lwp;
if ((vklp = lp->lwp_vkernel) != NULL) {
ve = ve_cache_find(vklp, id);
if (ve) {
KKASSERT(ve->vmspace);
return ve;
}
}
if (excl == 0)
lwkt_gettoken_shared(&vkp->token);
key.id = id;
ve = RB_FIND(vmspace_rb_tree, &vkp->root, &key);
if (ve) {
if (atomic_fetchadd_int(&ve->refs, 1) & VKE_REF_DELETED) {
vmspace_entry_drop(ve);
ve = NULL;
}
}
if (excl == 0)
lwkt_reltoken(&vkp->token);
if (vklp && ve)
ve_cache_enter(vklp, ve);
return (ve);
}
void
vkernel_inherit(struct proc *p1, struct proc *p2)
{
struct vkernel_proc *vkp;
vkp = p1->p_vkernel;
KKASSERT(vkp->refs > 0);
atomic_add_int(&vkp->refs, 1);
p2->p_vkernel = vkp;
}
void
vkernel_exit(struct proc *p)
{
struct vkernel_proc *vkp;
struct lwp *lp;
vkp = p->p_vkernel;
RB_FOREACH(lp, lwp_rb_tree, &p->p_lwp_tree) {
vkernel_lwp_exit(lp);
}
p->p_vkernel = NULL;
KKASSERT(vkp->refs > 0);
if (atomic_fetchadd_int(&vkp->refs, -1) == 1) {
lwkt_gettoken(&vkp->token);
RB_SCAN(vmspace_rb_tree, &vkp->root, NULL,
rb_vmspace_delete, vkp);
lwkt_reltoken(&vkp->token);
kfree(vkp, M_VKERNEL);
}
}
void
vkernel_lwp_exit(struct lwp *lp)
{
struct vkernel_lwp *vklp;
struct vmspace_entry *ve;
int i;
if ((vklp = lp->lwp_vkernel) != NULL) {
if ((ve = vklp->ve) != NULL) {
kprintf("Warning, pid %d killed with "
"active VC!\n", lp->lwp_proc->p_pid);
pmap_setlwpvm(lp, lp->lwp_proc->p_vmspace);
vklp->ve = NULL;
KKASSERT(ve->refs > 0);
vmspace_entry_drop(ve);
}
for (i = 0; i < VE_CACHE_COUNT; ++i) {
ve = vklp->ve_cache[i];
if (ve) {
vklp->ve_cache[i] = NULL;
vmspace_entry_cache_drop(ve);
}
}
lp->lwp_vkernel = NULL;
kfree(vklp, M_VKERNEL);
}
}
void
vkernel_trap(struct lwp *lp, struct trapframe *frame)
{
struct proc *p = lp->lwp_proc;
struct vmspace_entry *ve;
struct vkernel_lwp *vklp;
int error;
vklp = lp->lwp_vkernel;
KKASSERT(vklp);
ve = vklp->ve;
KKASSERT(ve != NULL);
vklp->ve = NULL;
pmap_setlwpvm(lp, p->p_vmspace);
KKASSERT(ve->refs > 0);
vmspace_entry_drop(ve);
error = copyout(frame, vklp->user_trapframe, sizeof(*frame));
bcopy(&vklp->save_trapframe, frame, sizeof(*frame));
bcopy(&vklp->save_vextframe.vx_tls, &curthread->td_tls,
sizeof(vklp->save_vextframe.vx_tls));
set_user_TLS();
cpu_vkernel_trap(frame, error);
}