Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

Updating IOCTL interface #121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/include/hax_core_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ struct vm_t * hax_get_vm(int vm_id, int refer);
int hax_vm_core_open(struct vm_t *vm);
/* Corresponding hax_get_vm with refer == 1 */
int hax_put_vm(struct vm_t *vm);
int hax_vm_set_qemuversion(struct vm_t *vm, struct hax_qemu_version *ver);

struct vm_t * hax_create_vm(int *vm_id);
int hax_teardown_vm(struct vm_t *vm);
Expand Down
1 change: 0 additions & 1 deletion core/include/vcpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,6 @@ int hax_vm_create_host(struct vm_t *cvm, int vm_id);

int vcpu_pause(struct vcpu_t *vcpu);
int vcpu_unpause(struct vcpu_t *vcpu);
int vcpu_takeoff(struct vcpu_t *vcpu);

void *vcpu_vmcs_va(struct vcpu_t *vcpu);
hax_paddr_t vcpu_vmcs_pa(struct vcpu_t *vcpu);
Expand Down
3 changes: 0 additions & 3 deletions core/include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ struct vm_t {
#define VM_STATE_FLAGS_OPENED 0x1
#define VM_STATE_FLAGS_MEM_ALLOC 0x2
uint64_t flags;
#define VM_FEATURES_FASTMMIO_BASIC 0x1
#define VM_FEATURES_FASTMMIO_EXTRA 0x2
uint32_t features;
int vm_id;
#define VPID_SEED_BITS 64
uint8_t vpid_seed[VPID_SEED_BITS / 8];
Expand Down
21 changes: 4 additions & 17 deletions core/vcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -2053,6 +2053,7 @@ static void vcpu_exit_fpu_state(struct vcpu_t *vcpu)
// http://wiki.osdev.org/X86-64_Instruction_Encoding
#define INSTR_MAX_LEN 15

<<<<<<< HEAD
static bool qemu_support_fastmmio(struct vcpu_t *vcpu)
{
struct vm_t *vm = vcpu->vm;
Expand All @@ -2068,6 +2069,9 @@ static bool qemu_support_fastmmio_extra(struct vcpu_t *vcpu)
}

static bool is_mmio_address(struct vcpu_t *vcpu, hax_paddr_t gpa)
=======
static bool is_mmio_address(struct vcpu_t *vcpu, paddr_t gpa)
>>>>>>> Unified IOCTL definitions
{
hax_paddr_t hpa;
if (vtlb_active(vcpu)) {
Expand Down Expand Up @@ -4168,23 +4172,6 @@ int vcpu_pause(struct vcpu_t *vcpu)
return 0;
}

int vcpu_takeoff(struct vcpu_t *vcpu)
{
int cpu_id;
hax_cpumap_t targets;

// Don't change the sequence unless you are sure
if (vcpu->is_running) {
cpu_id = vcpu->cpu_id;
hax_assert(cpu_id != hax_cpuid());
targets = cpu2cpumap(cpu_id);
// If not considering Windows XP, definitely we don't need this
hax_smp_call_function(&targets, _vcpu_take_off, NULL);
}

return 0;
}

int vcpu_unpause(struct vcpu_t *vcpu)
{
vcpu->paused = 0;
Expand Down
11 changes: 0 additions & 11 deletions core/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ static int valid_vm_mid(int vm_id)
return (vm_id >= 0) && (vm_id < VM_MID_BIT);
}

int hax_vm_set_qemuversion(struct vm_t *vm, struct hax_qemu_version *ver)
{
if (ver->cur_version >= 0x2) {
vm->features |= VM_FEATURES_FASTMMIO_BASIC;
if (ver->cur_version >= 0x4) {
vm->features |= VM_FEATURES_FASTMMIO_EXTRA;
}
}
return 0;
}

uint64_t vm_get_eptp(struct vm_t *vm)
{
uint64_t eptp_value;
Expand Down
21 changes: 2 additions & 19 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,24 +345,7 @@ and its presence requires `va` to be set to 0.
* Error codes:
* `STATUS_INVALID_PARAMETER` (Windows): The input buffer provided by the
caller is smaller than the size of `struct hax_set_ram_info`, or any of the
input parameters .

#### HAX\_VM\_IOCTL\_NOTIFY\_QEMU\_VERSION
TODO: Describe

* Since: API v2
* Parameter: `struct hax_qemu_version qversion`, where
```
struct hax_qemu_version {
uint32_t cur_version;
uint32_t least_version;
} __attribute__ ((__packed__));
```
* (Input) `cur_version`:
* (Input) `least_version`:
* Error codes:
* `STATUS_INVALID_PARAMETER` (Windows): The input buffer provided by the
caller is smaller than the size of `struct hax_qemu_version`.
input parameters.

### VCPU IOCTLs
#### HAX\_VCPU\_IOCTL\_SETUP\_TUNNEL
Expand Down Expand Up @@ -664,7 +647,7 @@ caller is smaller than the size of `struct vcpu_state_t`.
TODO: Describe

* Since: API v1
* Parameter: `struct vcpu_state_t regs` (q.v. `HAX_VCPU_SET_REGS`)
* Parameter: `struct vcpu_state_t regs` (q.v. `HAX_VCPU_IOCTL_SET_REGS`)
* (Output) `regs`:
* Error codes:
* `STATUS_INVALID_PARAMETER` (Windows): The output buffer provided by the
Expand Down
51 changes: 15 additions & 36 deletions include/darwin/hax_interface_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,42 +33,21 @@

#include <mach/mach_types.h>

/* The mac specific interface to qemu because of mac's
* special handling like hax tunnel allocation etc */
/* HAX model level ioctl */
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)

// Only for backward compatibility with old Qemu.
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)

#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)

#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)

#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)

/* API 2.0 */
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)

#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)
#define HAX_IOCTL_GROUP 'H'

#define HAX_IOCTL_HAX_IO(code, type) \
_IO(HAX_IOCTL_GROUP, code)
#define HAX_IOCTL_HAX_IOR(code, type) \
_IOR(HAX_IOCTL_GROUP, code, type)
#define HAX_IOCTL_HAX_IOW(code, type) \
_IOW(HAX_IOCTL_GROUP, code, type)
#define HAX_IOCTL_HAX_IOWR(code, type) \
_IOWR(HAX_IOCTL_GROUP, code, type)

#define HAX_LEGACY_IOCTL(access, code_posix, code_windows, type) \
HAX_IOCTL_##access(code_posix, type)
#define HAX_IOCTL(access, code, type) \
HAX_IOCTL_##access(code, type)

#define HAX_KERNEL64_CS 0x80
#define HAX_KERNEL32_CS 0x08
Expand Down
115 changes: 115 additions & 0 deletions include/hax_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,120 @@
#include "windows/hax_interface_windows.h"
#endif

#define HAX_IOCTL_PLATFORM 0x40
#define HAX_IOCTL_EXTENSION 0x80

/* Legacy API
* TODO: Remove all legacy calls after grace period (2020-01-01).
*/
#define HAX_IOCTL_VERSION__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x20, 0x900, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x21, 0x901, uint32_t)
#define HAX_IOCTL_DESTROY_VM__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOW, 0x22, 0x902, uint32_t)
#define HAX_IOCTL_CAPABILITY__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOR, 0x23, 0x910, struct hax_capabilityinfo)
#define HAX_IOCTL_SET_MEMLIMIT__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x24, 0x911, struct hax_set_memlimit)

#define HAX_VM_IOCTL_VCPU_CREATE__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x80, 0x902, uint32_t)
#define HAX_VM_IOCTL_ALLOC_RAM__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x81, 0x903, struct hax_alloc_ram_info)
#define HAX_VM_IOCTL_SET_RAM__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x82, 0x904, struct hax_set_ram_info)
#define HAX_VM_IOCTL_VCPU_DESTROY__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOR, 0x83, 0x905, uint32_t)
#define HAX_VM_IOCTL_ADD_RAMBLOCK__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOW, 0x85, 0x913, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x86, 0x914, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0x87, 0x915, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN__LEGACY \
HAX_LEGACY_IOCTL(HAX_IO, 0xc0, 0x906, HAX_UNUSED)
#define HAX_VCPU_IOCTL_SETUP_TUNNEL__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc5, 0x90b, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_GET_REGS__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc8, 0x90e, struct vcpu_state_t)
#define HAX_VCPU_IOCTL_SET_REGS__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc7, 0x90d, struct vcpu_state_t)
#define HAX_VCPU_IOCTL_GET_FPU__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOR, 0xc4, 0x90a, struct fx_layout)
#define HAX_VCPU_IOCTL_SET_FPU__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOW, 0xc3, 0x909, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_MSRS__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc2, 0x908, struct hax_msr_data)
#define HAX_VCPU_IOCTL_SET_MSRS__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc1, 0x907, struct hax_msr_data)
#define HAX_VCPU_IOCTL_INTERRUPT__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOWR, 0xc6, 0x90c, uint32_t)

// API 2.0
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOW, 0x84, 0x910, struct hax_qemu_version)
#define HAX_VCPU_IOCTL_DEBUG__LEGACY \
HAX_LEGACY_IOCTL(HAX_IOW, 0xc9, 0x916, struct hax_debug_t)

/* API
* ===
* Each platform generates their own IOCTL-value by using the macro
* HAX_IOCTL(access, code, type) with the following arguments:
* - access: Arguments usage from userland perspective.
* - HAX_IO: Driver ignores user arguments.
* - HAX_IOR: Driver writes user arguments (read by user).
* - HAX_IOW: Driver reads user arguments (written by user).
* - HAX_IOWR: Driver reads+writes user arguments (written+read by user).
* - code: Sequential number in range 0x00-0x3F, and maskable via:
* - HAX_IOCTL_PLATFORM (0x40) Platform-specific ioctl.
* - HAX_IOCTL_EXTENSION (0x80) Extension-specific ioctl.
* - type: User argument type.
*/
#define HAX_IOCTL_GET_API_VERSION \
HAX_IOCTL(HAX_IOR, 0x00, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM \
HAX_IOCTL(HAX_IOR, 0x01, uint32_t)
#define HAX_IOCTL_DESTROY_VM \
HAX_IOCTL(HAX_IOW, 0x02, uint32_t)
#define HAX_IOCTL_CAPABILITY \
HAX_IOCTL(HAX_IOR, 0x03, struct hax_capabilityinfo)

#define HAX_VM_IOCTL_CREATE_VCPU \
HAX_IOCTL(HAX_IOW, 0x00, uint32_t)
#define HAX_VM_IOCTL_DESTROY_VCPU \
HAX_IOCTL(HAX_IOW, 0x01, uint32_t)
#define HAX_VM_IOCTL_SET_RAM \
HAX_IOCTL(HAX_IOW, 0x02, struct hax_set_ram_info)
#define HAX_VM_IOCTL_ADD_RAMBLOCK \
HAX_IOCTL(HAX_IOW, 0x03, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2 \
HAX_IOCTL(HAX_IOW, 0x04, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM \
HAX_IOCTL(HAX_IOW, 0x05, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN \
HAX_IOCTL(HAX_IO, 0x00, HAX_UNUSED)
#define HAX_VCPU_IOCTL_SETUP_TUNNEL \
HAX_IOCTL(HAX_IOR, 0x01, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_GET_REGS \
HAX_IOCTL(HAX_IOR, 0x02, struct vcpu_state_t)
#define HAX_VCPU_IOCTL_SET_REGS \
HAX_IOCTL(HAX_IOW, 0x03, struct vcpu_state_t)
#define HAX_VCPU_IOCTL_GET_FPU \
HAX_IOCTL(HAX_IOR, 0x04, struct fx_layout)
#define HAX_VCPU_IOCTL_SET_FPU \
HAX_IOCTL(HAX_IOW, 0x05, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_MSRS \
HAX_IOCTL(HAX_IOWR, 0x06, struct hax_msr_data)
#define HAX_VCPU_IOCTL_SET_MSRS \
HAX_IOCTL(HAX_IOWR, 0x07, struct hax_msr_data)
#define HAX_VCPU_IOCTL_INTERRUPT \
HAX_IOCTL(HAX_IOW, 0x08, uint32_t)
#define HAX_VCPU_IOCTL_DEBUG \
HAX_IOCTL(HAX_IOW, 0x09, struct hax_debug_t)

#include "vcpu_state.h"

struct vmx_msr {
Expand Down Expand Up @@ -262,6 +376,7 @@ struct hax_set_ram_info2 {
// All accesses (R/W/X) are allowed
#define HAX_RAM_PERM_RWX 0x7
#define HAX_RAM_PERM_MASK 0x7

struct hax_protect_ram_info {
uint64_t pa_start;
uint64_t size;
Expand Down
51 changes: 15 additions & 36 deletions include/linux/hax_interface_linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,21 @@

#include <asm/ioctl.h>

/* The mac specific interface to qemu because of mac's
* special handling like hax tunnel allocation etc */
/* HAX model level ioctl */
#define HAX_IOCTL_VERSION _IOWR(0, 0x20, struct hax_module_version)
#define HAX_IOCTL_CREATE_VM _IOWR(0, 0x21, uint32_t)
#define HAX_IOCTL_DESTROY_VM _IOW(0, 0x22, uint32_t)
#define HAX_IOCTL_CAPABILITY _IOR(0, 0x23, struct hax_capabilityinfo)
#define HAX_IOCTL_SET_MEMLIMIT _IOWR(0, 0x24, struct hax_set_memlimit)

// Only for backward compatibility with old Qemu.
#define HAX_VM_IOCTL_VCPU_CREATE_ORIG _IOR(0, 0x80, int)

#define HAX_VM_IOCTL_VCPU_CREATE _IOWR(0, 0x80, uint32_t)
#define HAX_VM_IOCTL_ALLOC_RAM _IOWR(0, 0x81, struct hax_alloc_ram_info)
#define HAX_VM_IOCTL_SET_RAM _IOWR(0, 0x82, struct hax_set_ram_info)
#define HAX_VM_IOCTL_VCPU_DESTROY _IOR(0, 0x83, uint32_t)
#define HAX_VM_IOCTL_ADD_RAMBLOCK _IOW(0, 0x85, struct hax_ramblock_info)
#define HAX_VM_IOCTL_SET_RAM2 _IOWR(0, 0x86, struct hax_set_ram_info2)
#define HAX_VM_IOCTL_PROTECT_RAM _IOWR(0, 0x87, struct hax_protect_ram_info)

#define HAX_VCPU_IOCTL_RUN _IO(0, 0xc0)
#define HAX_VCPU_IOCTL_SET_MSRS _IOWR(0, 0xc1, struct hax_msr_data)
#define HAX_VCPU_IOCTL_GET_MSRS _IOWR(0, 0xc2, struct hax_msr_data)

#define HAX_VCPU_IOCTL_SET_FPU _IOW(0, 0xc3, struct fx_layout)
#define HAX_VCPU_IOCTL_GET_FPU _IOR(0, 0xc4, struct fx_layout)

#define HAX_VCPU_IOCTL_SETUP_TUNNEL _IOWR(0, 0xc5, struct hax_tunnel_info)
#define HAX_VCPU_IOCTL_INTERRUPT _IOWR(0, 0xc6, uint32_t)
#define HAX_VCPU_SET_REGS _IOWR(0, 0xc7, struct vcpu_state_t)
#define HAX_VCPU_GET_REGS _IOWR(0, 0xc8, struct vcpu_state_t)

/* API 2.0 */
#define HAX_VM_IOCTL_NOTIFY_QEMU_VERSION _IOW(0, 0x84, struct hax_qemu_version)

#define HAX_IOCTL_VCPU_DEBUG _IOW(0, 0xc9, struct hax_debug_t)
#define HAX_DEVICE_TYPE 'H'

#define HAX_IOCTL_HAX_IO(code, type) \
_IO(HAX_DEVICE_TYPE, code)
#define HAX_IOCTL_HAX_IOR(code, type) \
_IOR(HAX_DEVICE_TYPE, code, type)
#define HAX_IOCTL_HAX_IOW(code, type) \
_IOW(HAX_DEVICE_TYPE, code, type)
#define HAX_IOCTL_HAX_IOWR(code, type) \
_IOWR(HAX_DEVICE_TYPE, code, type)

#define HAX_LEGACY_IOCTL(access, code_posix, code_windows, type) \
HAX_IOCTL_##access(code_posix, type)
#define HAX_IOCTL(access, code, type) \
HAX_IOCTL_##access(code, type)

#define HAX_KERNEL64_CS 0x80
#define HAX_KERNEL32_CS 0x08
Expand Down
16 changes: 16 additions & 0 deletions include/windows/hax_interface_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@
#ifndef HAX_WINDOWS_HAX_INTERFACE_WINDOWS_H_
#define HAX_WINDOWS_HAX_INTERFACE_WINDOWS_H_

#define HAX_DEVICE_TYPE 0x8000

#define HAX_IO \
(FILE_ANY_ACCESS)
#define HAX_IOR \
(FILE_READ_DATA)
#define HAX_IOW \
(FILE_WRITE_DATA)
#define HAX_IOWR \
(FILE_WRITE_DATA | FILE_READ_DATA)

#define HAX_LEGACY_IOCTL(access, code_posix, code_windows, type) \
CTL_CODE(0x4000, code_windows, METHOD_BUFFERED, FILE_ANY_ACCESS)
#define HAX_IOCTL(access, code, type) \
CTL_CODE(HAX_DEVICE_TYPE, (0x800 | code), METHOD_BUFFERED, access)

/*
* This is for MAC compatible mode, so should not be used
* But how can we make sure it is really not used??
Expand Down
Loading