hw/arm: Move virt's PSCI DT fixup code to arm/boot.c
[qemu/ar7.git] / target / i386 / hax-darwin.c
blobee9417454c2c093c616c78c7a84337ec771d7c6f
1 /*
2 * QEMU HAXM support
4 * Copyright (c) 2011 Intel Corporation
5 * Written by:
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 "qemu/osdep.h"
15 #include <sys/ioctl.h>
17 #include "target/i386/hax-i386.h"
19 hax_fd hax_mod_open(void)
21 int fd = open("/dev/HAX", O_RDWR);
22 if (fd == -1) {
23 fprintf(stderr, "Failed to open the hax module\n");
26 fcntl(fd, F_SETFD, FD_CLOEXEC);
28 return fd;
31 int hax_populate_ram(uint64_t va, uint32_t size)
33 int ret;
34 struct hax_alloc_ram_info info;
36 if (!hax_global.vm || !hax_global.vm->fd) {
37 fprintf(stderr, "Allocate memory before vm create?\n");
38 return -EINVAL;
41 info.size = size;
42 info.va = va;
43 ret = ioctl(hax_global.vm->fd, HAX_VM_IOCTL_ALLOC_RAM, &info);
44 if (ret < 0) {
45 fprintf(stderr, "Failed to allocate %x memory\n", size);
46 return ret;
48 return 0;
51 int hax_set_ram(uint64_t start_pa, uint32_t size, uint64_t host_va, int flags)
53 struct hax_set_ram_info info;
54 int ret;
56 info.pa_start = start_pa;
57 info.size = size;
58 info.va = host_va;
59 info.flags = (uint8_t) flags;
61 ret = ioctl(hax_global.vm->fd, HAX_VM_IOCTL_SET_RAM, &info);
62 if (ret < 0) {
63 return -errno;
65 return 0;
68 int hax_capability(struct hax_state *hax, struct hax_capabilityinfo *cap)
70 int ret;
72 ret = ioctl(hax->fd, HAX_IOCTL_CAPABILITY, cap);
73 if (ret == -1) {
74 fprintf(stderr, "Failed to get HAX capability\n");
75 return -errno;
78 return 0;
81 int hax_mod_version(struct hax_state *hax, struct hax_module_version *version)
83 int ret;
85 ret = ioctl(hax->fd, HAX_IOCTL_VERSION, version);
86 if (ret == -1) {
87 fprintf(stderr, "Failed to get HAX version\n");
88 return -errno;
91 return 0;
94 static char *hax_vm_devfs_string(int vm_id)
96 char *name;
98 if (vm_id > MAX_VM_ID) {
99 fprintf(stderr, "Too big VM id\n");
100 return NULL;
103 #define HAX_VM_DEVFS "/dev/hax_vm/vmxx"
104 name = g_strdup(HAX_VM_DEVFS);
105 if (!name) {
106 return NULL;
109 snprintf(name, sizeof HAX_VM_DEVFS, "/dev/hax_vm/vm%02d", vm_id);
110 return name;
113 static char *hax_vcpu_devfs_string(int vm_id, int vcpu_id)
115 char *name;
117 if (vm_id > MAX_VM_ID || vcpu_id > MAX_VCPU_ID) {
118 fprintf(stderr, "Too big vm id %x or vcpu id %x\n", vm_id, vcpu_id);
119 return NULL;
122 #define HAX_VCPU_DEVFS "/dev/hax_vmxx/vcpuxx"
123 name = g_strdup(HAX_VCPU_DEVFS);
124 if (!name) {
125 return NULL;
128 snprintf(name, sizeof HAX_VCPU_DEVFS, "/dev/hax_vm%02d/vcpu%02d",
129 vm_id, vcpu_id);
130 return name;
133 int hax_host_create_vm(struct hax_state *hax, int *vmid)
135 int ret;
136 int vm_id = 0;
138 if (hax_invalid_fd(hax->fd)) {
139 return -EINVAL;
142 if (hax->vm) {
143 return 0;
146 ret = ioctl(hax->fd, HAX_IOCTL_CREATE_VM, &vm_id);
147 *vmid = vm_id;
148 return ret;
151 hax_fd hax_host_open_vm(struct hax_state *hax, int vm_id)
153 hax_fd fd;
154 char *vm_name = NULL;
156 vm_name = hax_vm_devfs_string(vm_id);
157 if (!vm_name) {
158 return -1;
161 fd = open(vm_name, O_RDWR);
162 g_free(vm_name);
164 fcntl(fd, F_SETFD, FD_CLOEXEC);
166 return fd;
169 int hax_notify_qemu_version(hax_fd vm_fd, struct hax_qemu_version *qversion)
171 int ret;
173 if (hax_invalid_fd(vm_fd)) {
174 return -EINVAL;
177 ret = ioctl(vm_fd, HAX_VM_IOCTL_NOTIFY_QEMU_VERSION, qversion);
179 if (ret < 0) {
180 fprintf(stderr, "Failed to notify qemu API version\n");
181 return ret;
183 return 0;
186 /* Simply assume the size should be bigger than the hax_tunnel,
187 * since the hax_tunnel can be extended later with compatibility considered
189 int hax_host_create_vcpu(hax_fd vm_fd, int vcpuid)
191 int ret;
193 ret = ioctl(vm_fd, HAX_VM_IOCTL_VCPU_CREATE, &vcpuid);
194 if (ret < 0) {
195 fprintf(stderr, "Failed to create vcpu %x\n", vcpuid);
198 return ret;
201 hax_fd hax_host_open_vcpu(int vmid, int vcpuid)
203 char *devfs_path = NULL;
204 hax_fd fd;
206 devfs_path = hax_vcpu_devfs_string(vmid, vcpuid);
207 if (!devfs_path) {
208 fprintf(stderr, "Failed to get the devfs\n");
209 return -EINVAL;
212 fd = open(devfs_path, O_RDWR);
213 g_free(devfs_path);
214 if (fd < 0) {
215 fprintf(stderr, "Failed to open the vcpu devfs\n");
217 fcntl(fd, F_SETFD, FD_CLOEXEC);
218 return fd;
221 int hax_host_setup_vcpu_channel(struct hax_vcpu_state *vcpu)
223 int ret;
224 struct hax_tunnel_info info;
226 ret = ioctl(vcpu->fd, HAX_VCPU_IOCTL_SETUP_TUNNEL, &info);
227 if (ret) {
228 fprintf(stderr, "Failed to setup the hax tunnel\n");
229 return ret;
232 if (!valid_hax_tunnel_size(info.size)) {
233 fprintf(stderr, "Invalid hax tunnel size %x\n", info.size);
234 ret = -EINVAL;
235 return ret;
238 vcpu->tunnel = (struct hax_tunnel *) (intptr_t) (info.va);
239 vcpu->iobuf = (unsigned char *) (intptr_t) (info.io_va);
240 return 0;
243 int hax_vcpu_run(struct hax_vcpu_state *vcpu)
245 int ret;
247 ret = ioctl(vcpu->fd, HAX_VCPU_IOCTL_RUN, NULL);
248 return ret;
251 int hax_sync_fpu(CPUArchState *env, struct fx_layout *fl, int set)
253 int ret, fd;
255 fd = hax_vcpu_get_fd(env);
256 if (fd <= 0) {
257 return -1;
260 if (set) {
261 ret = ioctl(fd, HAX_VCPU_IOCTL_SET_FPU, fl);
262 } else {
263 ret = ioctl(fd, HAX_VCPU_IOCTL_GET_FPU, fl);
265 return ret;
268 int hax_sync_msr(CPUArchState *env, struct hax_msr_data *msrs, int set)
270 int ret, fd;
272 fd = hax_vcpu_get_fd(env);
273 if (fd <= 0) {
274 return -1;
276 if (set) {
277 ret = ioctl(fd, HAX_VCPU_IOCTL_SET_MSRS, msrs);
278 } else {
279 ret = ioctl(fd, HAX_VCPU_IOCTL_GET_MSRS, msrs);
281 return ret;
284 int hax_sync_vcpu_state(CPUArchState *env, struct vcpu_state_t *state, int set)
286 int ret, fd;
288 fd = hax_vcpu_get_fd(env);
289 if (fd <= 0) {
290 return -1;
293 if (set) {
294 ret = ioctl(fd, HAX_VCPU_SET_REGS, state);
295 } else {
296 ret = ioctl(fd, HAX_VCPU_GET_REGS, state);
298 return ret;
301 int hax_inject_interrupt(CPUArchState *env, int vector)
303 int ret, fd;
305 fd = hax_vcpu_get_fd(env);
306 if (fd <= 0) {
307 return -1;
310 ret = ioctl(fd, HAX_VCPU_IOCTL_INTERRUPT, &vector);
311 return ret;