Changeset View
Changeset View
Standalone View
Standalone View
head/sys/amd64/vmm/vmm_dev.c
| Show All 27 Lines | |||||
| * $FreeBSD$ | * $FreeBSD$ | ||||
| */ | */ | ||||
| #include <sys/cdefs.h> | #include <sys/cdefs.h> | ||||
| __FBSDID("$FreeBSD$"); | __FBSDID("$FreeBSD$"); | ||||
| #include <sys/param.h> | #include <sys/param.h> | ||||
| #include <sys/kernel.h> | #include <sys/kernel.h> | ||||
| #include <sys/jail.h> | |||||
| #include <sys/queue.h> | #include <sys/queue.h> | ||||
| #include <sys/lock.h> | #include <sys/lock.h> | ||||
| #include <sys/mutex.h> | #include <sys/mutex.h> | ||||
| #include <sys/malloc.h> | #include <sys/malloc.h> | ||||
| #include <sys/conf.h> | #include <sys/conf.h> | ||||
| #include <sys/sysctl.h> | #include <sys/sysctl.h> | ||||
| #include <sys/libkern.h> | #include <sys/libkern.h> | ||||
| #include <sys/ioccom.h> | #include <sys/ioccom.h> | ||||
| #include <sys/mman.h> | #include <sys/mman.h> | ||||
| #include <sys/uio.h> | #include <sys/uio.h> | ||||
| #include <sys/proc.h> | |||||
| #include <vm/vm.h> | #include <vm/vm.h> | ||||
| #include <vm/pmap.h> | #include <vm/pmap.h> | ||||
| #include <vm/vm_map.h> | #include <vm/vm_map.h> | ||||
| #include <vm/vm_object.h> | #include <vm/vm_object.h> | ||||
| #include <machine/vmparam.h> | #include <machine/vmparam.h> | ||||
| #include <machine/vmm.h> | #include <machine/vmm.h> | ||||
| Show All 23 Lines | struct vmmdev_softc { | ||||
| SLIST_ENTRY(vmmdev_softc) link; | SLIST_ENTRY(vmmdev_softc) link; | ||||
| SLIST_HEAD(, devmem_softc) devmem; | SLIST_HEAD(, devmem_softc) devmem; | ||||
| int flags; | int flags; | ||||
| }; | }; | ||||
| #define VSC_LINKED 0x01 | #define VSC_LINKED 0x01 | ||||
| static SLIST_HEAD(, vmmdev_softc) head; | static SLIST_HEAD(, vmmdev_softc) head; | ||||
| static unsigned pr_allow_flag; | |||||
| static struct mtx vmmdev_mtx; | static struct mtx vmmdev_mtx; | ||||
| static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev"); | static MALLOC_DEFINE(M_VMMDEV, "vmmdev", "vmmdev"); | ||||
| SYSCTL_DECL(_hw_vmm); | SYSCTL_DECL(_hw_vmm); | ||||
| static int vmm_priv_check(struct ucred *ucred); | |||||
| static int devmem_create_cdev(const char *vmname, int id, char *devmem); | static int devmem_create_cdev(const char *vmname, int id, char *devmem); | ||||
| static void devmem_destroy(void *arg); | static void devmem_destroy(void *arg); | ||||
| static int | |||||
| vmm_priv_check(struct ucred *ucred) | |||||
| { | |||||
| if (jailed(ucred) && | |||||
| !(ucred->cr_prison->pr_allow & pr_allow_flag)) | |||||
| return (EPERM); | |||||
| return (0); | |||||
| } | |||||
| static int | static int | ||||
| vcpu_lock_one(struct vmmdev_softc *sc, int vcpu) | vcpu_lock_one(struct vmmdev_softc *sc, int vcpu) | ||||
| { | { | ||||
| int error; | int error; | ||||
| if (vcpu < 0 || vcpu >= VM_MAXCPU) | if (vcpu < 0 || vcpu >= VM_MAXCPU) | ||||
| return (EINVAL); | return (EINVAL); | ||||
| ▲ Show 20 Lines • Show All 69 Lines • ▼ Show 20 Lines | |||||
| static int | static int | ||||
| vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags) | vmmdev_rw(struct cdev *cdev, struct uio *uio, int flags) | ||||
| { | { | ||||
| int error, off, c, prot; | int error, off, c, prot; | ||||
| vm_paddr_t gpa, maxaddr; | vm_paddr_t gpa, maxaddr; | ||||
| void *hpa, *cookie; | void *hpa, *cookie; | ||||
| struct vmmdev_softc *sc; | struct vmmdev_softc *sc; | ||||
| error = vmm_priv_check(curthread->td_ucred); | |||||
| if (error) | |||||
| return (error); | |||||
| sc = vmmdev_lookup2(cdev); | sc = vmmdev_lookup2(cdev); | ||||
| if (sc == NULL) | if (sc == NULL) | ||||
| return (ENXIO); | return (ENXIO); | ||||
| /* | /* | ||||
| * Get a read lock on the guest memory map by freezing any vcpu. | * Get a read lock on the guest memory map by freezing any vcpu. | ||||
| */ | */ | ||||
| ▲ Show 20 Lines • Show All 158 Lines • ▼ Show 20 Lines | vmmdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, | ||||
| struct vm_cpuset *vm_cpuset; | struct vm_cpuset *vm_cpuset; | ||||
| struct vm_intinfo *vmii; | struct vm_intinfo *vmii; | ||||
| struct vm_rtc_time *rtctime; | struct vm_rtc_time *rtctime; | ||||
| struct vm_rtc_data *rtcdata; | struct vm_rtc_data *rtcdata; | ||||
| struct vm_memmap *mm; | struct vm_memmap *mm; | ||||
| struct vm_cpu_topology *topology; | struct vm_cpu_topology *topology; | ||||
| uint64_t *regvals; | uint64_t *regvals; | ||||
| int *regnums; | int *regnums; | ||||
| error = vmm_priv_check(curthread->td_ucred); | |||||
| if (error) | |||||
| return (error); | |||||
| sc = vmmdev_lookup2(cdev); | sc = vmmdev_lookup2(cdev); | ||||
| if (sc == NULL) | if (sc == NULL) | ||||
| return (ENXIO); | return (ENXIO); | ||||
| error = 0; | |||||
| vcpu = -1; | vcpu = -1; | ||||
| state_changed = 0; | state_changed = 0; | ||||
| /* | /* | ||||
| * Some VMM ioctls can operate only on vcpus that are not running. | * Some VMM ioctls can operate only on vcpus that are not running. | ||||
| */ | */ | ||||
| switch (cmd) { | switch (cmd) { | ||||
| case VM_RUN: | case VM_RUN: | ||||
| ▲ Show 20 Lines • Show All 404 Lines • ▼ Show 20 Lines | vmmdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t mapsize, | ||||
| struct vm_object **objp, int nprot) | struct vm_object **objp, int nprot) | ||||
| { | { | ||||
| struct vmmdev_softc *sc; | struct vmmdev_softc *sc; | ||||
| vm_paddr_t gpa; | vm_paddr_t gpa; | ||||
| size_t len; | size_t len; | ||||
| vm_ooffset_t segoff, first, last; | vm_ooffset_t segoff, first, last; | ||||
| int error, found, segid; | int error, found, segid; | ||||
| bool sysmem; | bool sysmem; | ||||
| error = vmm_priv_check(curthread->td_ucred); | |||||
| if (error) | |||||
| return (error); | |||||
| first = *offset; | first = *offset; | ||||
| last = first + mapsize; | last = first + mapsize; | ||||
| if ((nprot & PROT_EXEC) || first < 0 || first >= last) | if ((nprot & PROT_EXEC) || first < 0 || first >= last) | ||||
| return (EINVAL); | return (EINVAL); | ||||
| sc = vmmdev_lookup2(cdev); | sc = vmmdev_lookup2(cdev); | ||||
| if (sc == NULL) { | if (sc == NULL) { | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | |||||
| sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) | sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) | ||||
| { | { | ||||
| int error; | int error; | ||||
| char buf[VM_MAX_NAMELEN]; | char buf[VM_MAX_NAMELEN]; | ||||
| struct devmem_softc *dsc; | struct devmem_softc *dsc; | ||||
| struct vmmdev_softc *sc; | struct vmmdev_softc *sc; | ||||
| struct cdev *cdev; | struct cdev *cdev; | ||||
| error = vmm_priv_check(req->td->td_ucred); | |||||
| if (error) | |||||
| return (error); | |||||
| strlcpy(buf, "beavis", sizeof(buf)); | strlcpy(buf, "beavis", sizeof(buf)); | ||||
| error = sysctl_handle_string(oidp, buf, sizeof(buf), req); | error = sysctl_handle_string(oidp, buf, sizeof(buf), req); | ||||
| if (error != 0 || req->newptr == NULL) | if (error != 0 || req->newptr == NULL) | ||||
| return (error); | return (error); | ||||
| mtx_lock(&vmmdev_mtx); | mtx_lock(&vmmdev_mtx); | ||||
| sc = vmmdev_lookup(buf); | sc = vmmdev_lookup(buf); | ||||
| if (sc == NULL || sc->cdev == NULL) { | if (sc == NULL || sc->cdev == NULL) { | ||||
| Show All 25 Lines | sysctl_vmm_destroy(SYSCTL_HANDLER_ARGS) | ||||
| */ | */ | ||||
| SLIST_FOREACH(dsc, &sc->devmem, link) { | SLIST_FOREACH(dsc, &sc->devmem, link) { | ||||
| KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed")); | KASSERT(dsc->cdev != NULL, ("devmem cdev already destroyed")); | ||||
| destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc); | destroy_dev_sched_cb(dsc->cdev, devmem_destroy, dsc); | ||||
| } | } | ||||
| destroy_dev_sched_cb(cdev, vmmdev_destroy, sc); | destroy_dev_sched_cb(cdev, vmmdev_destroy, sc); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, CTLTYPE_STRING | CTLFLAG_RW, | SYSCTL_PROC(_hw_vmm, OID_AUTO, destroy, | ||||
| CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON, | |||||
| NULL, 0, sysctl_vmm_destroy, "A", NULL); | NULL, 0, sysctl_vmm_destroy, "A", NULL); | ||||
| static struct cdevsw vmmdevsw = { | static struct cdevsw vmmdevsw = { | ||||
| .d_name = "vmmdev", | .d_name = "vmmdev", | ||||
| .d_version = D_VERSION, | .d_version = D_VERSION, | ||||
| .d_ioctl = vmmdev_ioctl, | .d_ioctl = vmmdev_ioctl, | ||||
| .d_mmap_single = vmmdev_mmap_single, | .d_mmap_single = vmmdev_mmap_single, | ||||
| .d_read = vmmdev_rw, | .d_read = vmmdev_rw, | ||||
| .d_write = vmmdev_rw, | .d_write = vmmdev_rw, | ||||
| }; | }; | ||||
| static int | static int | ||||
| sysctl_vmm_create(SYSCTL_HANDLER_ARGS) | sysctl_vmm_create(SYSCTL_HANDLER_ARGS) | ||||
| { | { | ||||
| int error; | int error; | ||||
| struct vm *vm; | struct vm *vm; | ||||
| struct cdev *cdev; | struct cdev *cdev; | ||||
| struct vmmdev_softc *sc, *sc2; | struct vmmdev_softc *sc, *sc2; | ||||
| char buf[VM_MAX_NAMELEN]; | char buf[VM_MAX_NAMELEN]; | ||||
| error = vmm_priv_check(req->td->td_ucred); | |||||
| if (error) | |||||
| return (error); | |||||
| strlcpy(buf, "beavis", sizeof(buf)); | strlcpy(buf, "beavis", sizeof(buf)); | ||||
| error = sysctl_handle_string(oidp, buf, sizeof(buf), req); | error = sysctl_handle_string(oidp, buf, sizeof(buf), req); | ||||
| if (error != 0 || req->newptr == NULL) | if (error != 0 || req->newptr == NULL) | ||||
| return (error); | return (error); | ||||
| mtx_lock(&vmmdev_mtx); | mtx_lock(&vmmdev_mtx); | ||||
| sc = vmmdev_lookup(buf); | sc = vmmdev_lookup(buf); | ||||
| mtx_unlock(&vmmdev_mtx); | mtx_unlock(&vmmdev_mtx); | ||||
| Show All 34 Lines | sysctl_vmm_create(SYSCTL_HANDLER_ARGS) | ||||
| mtx_lock(&vmmdev_mtx); | mtx_lock(&vmmdev_mtx); | ||||
| sc->cdev = cdev; | sc->cdev = cdev; | ||||
| sc->cdev->si_drv1 = sc; | sc->cdev->si_drv1 = sc; | ||||
| mtx_unlock(&vmmdev_mtx); | mtx_unlock(&vmmdev_mtx); | ||||
| return (0); | return (0); | ||||
| } | } | ||||
| SYSCTL_PROC(_hw_vmm, OID_AUTO, create, CTLTYPE_STRING | CTLFLAG_RW, | SYSCTL_PROC(_hw_vmm, OID_AUTO, create, | ||||
| CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_PRISON, | |||||
| NULL, 0, sysctl_vmm_create, "A", NULL); | NULL, 0, sysctl_vmm_create, "A", NULL); | ||||
| void | void | ||||
| vmmdev_init(void) | vmmdev_init(void) | ||||
| { | { | ||||
| mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF); | mtx_init(&vmmdev_mtx, "vmm device mutex", NULL, MTX_DEF); | ||||
| pr_allow_flag = prison_add_allow(NULL, "vmm", NULL, | |||||
| "Allow use of vmm in a jail."); | |||||
| } | } | ||||
| int | int | ||||
| vmmdev_cleanup(void) | vmmdev_cleanup(void) | ||||
| { | { | ||||
| int error; | int error; | ||||
| if (SLIST_EMPTY(&head)) | if (SLIST_EMPTY(&head)) | ||||
| ▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines | |||||