sync code with last fixes and improvements from OpenBSD

This commit is contained in:
purplerain 2023-05-14 12:20:04 +00:00
parent 62073e0295
commit 0f55503fed
Signed by: purplerain
GPG key ID: F42C07F07E2E35B7
72 changed files with 797 additions and 1127 deletions

View file

@ -90,7 +90,7 @@ DEFINE_STATIC_SRCU(drm_unplug_srcu);
* Some functions are only called once on init regardless of how many times
* drm attaches. In linux this is handled via module_init()/module_exit()
*/
int drm_refcnt;
int drm_refcnt;
struct drm_softc {
struct device sc_dev;
@ -1225,7 +1225,7 @@ drm_attach_pci(const struct drm_driver *driver, struct pci_attach_args *pa,
sc = (struct drm_softc *)config_found_sm(dev, &arg, drmprint, drmsubmatch);
if (sc == NULL)
return NULL;
return sc->sc_drm;
}
@ -1522,7 +1522,7 @@ const struct pci_device_id *
drm_find_description(int vendor, int device, const struct pci_device_id *idlist)
{
int i = 0;
for (i = 0; idlist[i].vendor != 0; i++) {
if ((idlist[i].vendor == vendor) &&
(idlist[i].device == device ||
@ -1546,7 +1546,7 @@ struct drm_file *
drm_find_file_by_minor(struct drm_device *dev, int minor)
{
struct drm_file key;
key.fminor = minor;
return (SPLAY_FIND(drm_file_tree, &dev->files, &key));
}
@ -1886,7 +1886,7 @@ drm_dmamem_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t alignment,
struct drm_dmamem *mem;
size_t strsize;
/*
* segs is the last member of the struct since we modify the size
* segs is the last member of the struct since we modify the size
* to allow extra segments if more than one are allowed.
*/
strsize = sizeof(*mem) + (sizeof(bus_dma_segment_t) * (nsegments - 1));
@ -1904,7 +1904,7 @@ drm_dmamem_alloc(bus_dma_tag_t dmat, bus_size_t size, bus_size_t alignment,
&mem->nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO) != 0)
goto destroy;
if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, size,
if (bus_dmamem_map(dmat, mem->segs, mem->nsegs, size,
&mem->kva, BUS_DMA_NOWAIT | mapflags) != 0)
goto free;

View file

@ -176,10 +176,10 @@ vmmci_config_change(struct virtio_softc *vsc)
case VMMCI_SYNCRTC:
inittodr(gettime());
sc->sc_cmd = VMMCI_NONE;
break;
break;
default:
printf("%s: invalid command %d\n", sc->sc_dev.dv_xname, cmd);
cmd = VMMCI_NONE;
cmd = VMMCI_NONE;
break;
}

View file

@ -1338,7 +1338,7 @@ vmt_xdr_nic_info(char *data)
nictotal = vmt_xdr_nic_entry(iface, data);
if (nictotal == 0)
continue;
if (data != NULL)
data += nictotal;

View file

@ -1,4 +1,4 @@
/* $OpenBSD: vmm.c,v 1.1 2023/04/26 15:11:21 mlarkin Exp $ */
/* $OpenBSD: vmm.c,v 1.2 2023/05/13 23:15:28 dv Exp $ */
/*
* Copyright (c) 2014-2023 Mike Larkin <mlarkin@openbsd.org>
*
@ -262,6 +262,9 @@ vmmioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
case VMM_IOC_WRITEVMPARAMS:
ret = vm_rwvmparams((struct vm_rwvmparams_params *)data, 1);
break;
case VMM_IOC_SHAREMEM:
ret = vm_share_mem((struct vm_sharemem_params *)data, p);
break;
default:
ret = vmmioctl_machdep(dev, cmd, data, flag, p);
break;
@ -286,6 +289,7 @@ pledge_ioctl_vmm(struct proc *p, long com)
switch (com) {
case VMM_IOC_CREATE:
case VMM_IOC_INFO:
case VMM_IOC_SHAREMEM:
/* The "parent" process in vmd forks and manages VMs */
if (p->p_p->ps_pledge & PLEDGE_PROC)
return (0);
@ -780,3 +784,82 @@ vcpu_must_stop(struct vcpu *vcpu)
return (1);
return (0);
}
/*
* vm_share_mem
*
* Share a uvm mapping for the vm guest memory ranges into the calling process.
*
* Return values:
* 0: if successful
* ENOENT: if the vm cannot be found by vm_find
* EPERM: if the vm cannot be accessed by the current process
* EINVAL: if the provide memory ranges fail checks
* ENOMEM: if uvm_share fails to find available memory in the destination map
*/
int
vm_share_mem(struct vm_sharemem_params *vsp, struct proc *p)
{
int ret = EINVAL;
size_t i, n;
struct vm *vm;
struct vm_mem_range *src, *dst;
ret = vm_find(vsp->vsp_vm_id, &vm);
if (ret)
return (ret);
/* Check we have the expected number of ranges. */
if (vm->vm_nmemranges != vsp->vsp_nmemranges)
goto out;
n = vm->vm_nmemranges;
/* Check their types, sizes, and gpa's (implying page alignment). */
for (i = 0; i < n; i++) {
src = &vm->vm_memranges[i];
dst = &vsp->vsp_memranges[i];
/*
* The vm memranges were already checked during creation, so
* compare to them to confirm validity of mapping request.
*/
if (src->vmr_type != dst->vmr_type)
goto out;
if (src->vmr_gpa != dst->vmr_gpa)
goto out;
if (src->vmr_size != dst->vmr_size)
goto out;
/* Check our intended destination is page-aligned. */
if (dst->vmr_va & PAGE_MASK)
goto out;
}
/*
* Share each range individually with the calling process. We do
* not need PROC_EXEC as the emulated devices do not need to execute
* instructions from guest memory.
*/
for (i = 0; i < n; i++) {
src = &vm->vm_memranges[i];
dst = &vsp->vsp_memranges[i];
/* Skip MMIO range. */
if (src->vmr_type == VM_MEM_MMIO)
continue;
DPRINTF("sharing gpa=0x%lx for pid %d @ va=0x%lx\n",
src->vmr_gpa, p->p_p->ps_pid, dst->vmr_va);
ret = uvm_share(&p->p_vmspace->vm_map, dst->vmr_va,
PROT_READ | PROT_WRITE, vm->vm_map, src->vmr_gpa,
src->vmr_size);
if (ret) {
printf("%s: uvm_share failed (%d)\n", __func__, ret);
break;
}
}
ret = 0;
out:
refcnt_rele_wake(&vm->vm_refcnt);
return (ret);
}

View file

@ -1,4 +1,4 @@
/* $OpenBSD: vmm.h,v 1.2 2023/04/26 16:13:19 mlarkin Exp $ */
/* $OpenBSD: vmm.h,v 1.3 2023/05/13 23:15:28 dv Exp $ */
/*
* Copyright (c) 2014-2023 Mike Larkin <mlarkin@openbsd.org>
*
@ -76,6 +76,13 @@ struct vm_resetcpu_params {
struct vcpu_reg_state vrp_init_state;
};
struct vm_sharemem_params {
/* Input parameters to VMM_IOC_SHAREMEM */
uint32_t vsp_vm_id;
size_t vsp_nmemranges;
struct vm_mem_range vsp_memranges[VMM_MAX_MEM_RANGES];
};
/* IOCTL definitions */
#define VMM_IOC_CREATE _IOWR('V', 1, struct vm_create_params) /* Create VM */
#define VMM_IOC_RUN _IOWR('V', 2, struct vm_run_params) /* Run VCPU */
@ -88,7 +95,7 @@ struct vm_resetcpu_params {
#define VMM_IOC_READVMPARAMS _IOWR('V', 9, struct vm_rwvmparams_params)
/* Set VM params */
#define VMM_IOC_WRITEVMPARAMS _IOW('V', 10, struct vm_rwvmparams_params)
#define VMM_IOC_SHAREMEM _IOW('V', 11, struct vm_sharemem_params)
#ifdef _KERNEL
@ -194,6 +201,7 @@ int vm_get_info(struct vm_info_params *);
int vm_terminate(struct vm_terminate_params *);
int vm_resetcpu(struct vm_resetcpu_params *);
int vcpu_must_stop(struct vcpu *);
int vm_share_mem(struct vm_sharemem_params *, struct proc *);
#endif /* _KERNEL */
#endif /* DEV_VMM_H */

File diff suppressed because it is too large Load diff

View file

@ -123,8 +123,8 @@ struct x86emu_regs {
* Delayed flag set 3 bits (zero, signed, parity)
* reserved 6 bits
* interrupt # 8 bits instruction raised interrupt
* BIOS video segregs 4 bits
* Interrupt Pending 1 bits
* BIOS video segregs 4 bits
* Interrupt Pending 1 bits
* Extern interrupt 1 bits
* Halted 1 bits
*/

View file

@ -42,10 +42,10 @@
/*
* PARAMETERS:
* addr - Emulator memory address to read
*
*
* RETURNS:
* Byte value read from emulator memory.
*
*
* REMARKS:
* Reads a byte value from the emulator memory.
*/
@ -60,10 +60,10 @@ rdb(struct x86emu *emu, uint32_t addr)
/*
* PARAMETERS:
* addr - Emulator memory address to read
*
*
* RETURNS:
* Word value read from emulator memory.
*
*
* REMARKS:
* Reads a word value from the emulator memory.
*/
@ -90,7 +90,7 @@ rdw(struct x86emu *emu, uint32_t addr)
/*
* PARAMETERS:
* addr - Emulator memory address to read
*
*
* RETURNS:
* Long value read from emulator memory.
* REMARKS:
@ -122,7 +122,7 @@ rdl(struct x86emu *emu, uint32_t addr)
* PARAMETERS:
* addr - Emulator memory address to read
* val - Value to store
*
*
* REMARKS:
* Writes a byte value to emulator memory.
*/
@ -138,7 +138,7 @@ wrb(struct x86emu *emu, uint32_t addr, uint8_t val)
* PARAMETERS:
* addr - Emulator memory address to read
* val - Value to store
*
*
* REMARKS:
* Writes a word value to emulator memory.
*/
@ -164,7 +164,7 @@ wrw(struct x86emu *emu, uint32_t addr, uint16_t val)
* PARAMETERS:
* addr - Emulator memory address to read
* val - Value to store
*
*
* REMARKS:
* Writes a long value to emulator memory.
*/