4 * Copyright (c) 2011 Intel Corporation
6 * Jiang Yunhong<yunhong.jiang@intel.com>
8 * This work is licensed under the terms of the GNU GPL, version 2 or later.
9 * See the COPYING file in the top-level directory.
13 /* HAX module interface - darwin version */
14 #include <sys/types.h>
18 #include <sys/ioctl.h>
20 #include "qemu/osdep.h"
21 #include "target/i386/hax-i386.h"
23 hax_fd
hax_mod_open(void)
25 int fd
= open("/dev/HAX", O_RDWR
);
27 fprintf(stderr
, "Failed to open the hax module\n");
30 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
35 int hax_populate_ram(uint64_t va
, uint32_t size
)
38 struct hax_alloc_ram_info info
;
40 if (!hax_global
.vm
|| !hax_global
.vm
->fd
) {
41 fprintf(stderr
, "Allocate memory before vm create?\n");
47 ret
= ioctl(hax_global
.vm
->fd
, HAX_VM_IOCTL_ALLOC_RAM
, &info
);
49 fprintf(stderr
, "Failed to allocate %x memory\n", size
);
55 int hax_set_ram(uint64_t start_pa
, uint32_t size
, uint64_t host_va
, int flags
)
57 struct hax_set_ram_info info
;
60 info
.pa_start
= start_pa
;
63 info
.flags
= (uint8_t) flags
;
65 ret
= ioctl(hax_global
.vm
->fd
, HAX_VM_IOCTL_SET_RAM
, &info
);
72 int hax_capability(struct hax_state
*hax
, struct hax_capabilityinfo
*cap
)
76 ret
= ioctl(hax
->fd
, HAX_IOCTL_CAPABILITY
, cap
);
78 fprintf(stderr
, "Failed to get HAX capability\n");
85 int hax_mod_version(struct hax_state
*hax
, struct hax_module_version
*version
)
89 ret
= ioctl(hax
->fd
, HAX_IOCTL_VERSION
, version
);
91 fprintf(stderr
, "Failed to get HAX version\n");
98 static char *hax_vm_devfs_string(int vm_id
)
102 if (vm_id
> MAX_VM_ID
) {
103 fprintf(stderr
, "Too big VM id\n");
107 #define HAX_VM_DEVFS "/dev/hax_vm/vmxx"
108 name
= g_strdup(HAX_VM_DEVFS
);
113 snprintf(name
, sizeof HAX_VM_DEVFS
, "/dev/hax_vm/vm%02d", vm_id
);
117 static char *hax_vcpu_devfs_string(int vm_id
, int vcpu_id
)
121 if (vm_id
> MAX_VM_ID
|| vcpu_id
> MAX_VCPU_ID
) {
122 fprintf(stderr
, "Too big vm id %x or vcpu id %x\n", vm_id
, vcpu_id
);
126 #define HAX_VCPU_DEVFS "/dev/hax_vmxx/vcpuxx"
127 name
= g_strdup(HAX_VCPU_DEVFS
);
132 snprintf(name
, sizeof HAX_VCPU_DEVFS
, "/dev/hax_vm%02d/vcpu%02d",
137 int hax_host_create_vm(struct hax_state
*hax
, int *vmid
)
142 if (hax_invalid_fd(hax
->fd
)) {
150 ret
= ioctl(hax
->fd
, HAX_IOCTL_CREATE_VM
, &vm_id
);
155 hax_fd
hax_host_open_vm(struct hax_state
*hax
, int vm_id
)
158 char *vm_name
= NULL
;
160 vm_name
= hax_vm_devfs_string(vm_id
);
165 fd
= open(vm_name
, O_RDWR
);
168 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
173 int hax_notify_qemu_version(hax_fd vm_fd
, struct hax_qemu_version
*qversion
)
177 if (hax_invalid_fd(vm_fd
)) {
181 ret
= ioctl(vm_fd
, HAX_VM_IOCTL_NOTIFY_QEMU_VERSION
, qversion
);
184 fprintf(stderr
, "Failed to notify qemu API version\n");
190 /* Simply assume the size should be bigger than the hax_tunnel,
191 * since the hax_tunnel can be extended later with compatibility considered
193 int hax_host_create_vcpu(hax_fd vm_fd
, int vcpuid
)
197 ret
= ioctl(vm_fd
, HAX_VM_IOCTL_VCPU_CREATE
, &vcpuid
);
199 fprintf(stderr
, "Failed to create vcpu %x\n", vcpuid
);
205 hax_fd
hax_host_open_vcpu(int vmid
, int vcpuid
)
207 char *devfs_path
= NULL
;
210 devfs_path
= hax_vcpu_devfs_string(vmid
, vcpuid
);
212 fprintf(stderr
, "Failed to get the devfs\n");
216 fd
= open(devfs_path
, O_RDWR
);
219 fprintf(stderr
, "Failed to open the vcpu devfs\n");
221 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
225 int hax_host_setup_vcpu_channel(struct hax_vcpu_state
*vcpu
)
228 struct hax_tunnel_info info
;
230 ret
= ioctl(vcpu
->fd
, HAX_VCPU_IOCTL_SETUP_TUNNEL
, &info
);
232 fprintf(stderr
, "Failed to setup the hax tunnel\n");
236 if (!valid_hax_tunnel_size(info
.size
)) {
237 fprintf(stderr
, "Invalid hax tunnel size %x\n", info
.size
);
242 vcpu
->tunnel
= (struct hax_tunnel
*) (intptr_t) (info
.va
);
243 vcpu
->iobuf
= (unsigned char *) (intptr_t) (info
.io_va
);
247 int hax_vcpu_run(struct hax_vcpu_state
*vcpu
)
251 ret
= ioctl(vcpu
->fd
, HAX_VCPU_IOCTL_RUN
, NULL
);
255 int hax_sync_fpu(CPUArchState
*env
, struct fx_layout
*fl
, int set
)
259 fd
= hax_vcpu_get_fd(env
);
265 ret
= ioctl(fd
, HAX_VCPU_IOCTL_SET_FPU
, fl
);
267 ret
= ioctl(fd
, HAX_VCPU_IOCTL_GET_FPU
, fl
);
272 int hax_sync_msr(CPUArchState
*env
, struct hax_msr_data
*msrs
, int set
)
276 fd
= hax_vcpu_get_fd(env
);
281 ret
= ioctl(fd
, HAX_VCPU_IOCTL_SET_MSRS
, msrs
);
283 ret
= ioctl(fd
, HAX_VCPU_IOCTL_GET_MSRS
, msrs
);
288 int hax_sync_vcpu_state(CPUArchState
*env
, struct vcpu_state_t
*state
, int set
)
292 fd
= hax_vcpu_get_fd(env
);
298 ret
= ioctl(fd
, HAX_VCPU_SET_REGS
, state
);
300 ret
= ioctl(fd
, HAX_VCPU_GET_REGS
, state
);
305 int hax_inject_interrupt(CPUArchState
*env
, int vector
)
309 fd
= hax_vcpu_get_fd(env
);
314 ret
= ioctl(fd
, HAX_VCPU_IOCTL_INTERRUPT
, &vector
);