tests/acceptance: verify s390x device detection
[qemu/ar7.git] / target / i386 / hax-all.c
blobfecfe8cd6e40d4891562204ba1c479db50ee0e08
1 /*
2 * QEMU HAX support
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
11 * Copyright (c) 2011 Intel Corporation
12 * Written by:
13 * Jiang Yunhong<yunhong.jiang@intel.com>
14 * Xin Xiaohui<xiaohui.xin@intel.com>
15 * Zhang Xiantao<xiantao.zhang@intel.com>
17 * This work is licensed under the terms of the GNU GPL, version 2 or later.
18 * See the COPYING file in the top-level directory.
23 * HAX common code for both windows and darwin
26 #include "qemu/osdep.h"
27 #include "cpu.h"
28 #include "exec/address-spaces.h"
30 #include "qemu-common.h"
31 #include "sysemu/accel.h"
32 #include "sysemu/reset.h"
33 #include "sysemu/runstate.h"
34 #include "hw/boards.h"
36 #include "hax-cpus.h"
38 #define DEBUG_HAX 0
40 #define DPRINTF(fmt, ...) \
41 do { \
42 if (DEBUG_HAX) { \
43 fprintf(stdout, fmt, ## __VA_ARGS__); \
44 } \
45 } while (0)
47 /* Current version */
48 const uint32_t hax_cur_version = 0x4; /* API v4: unmapping and MMIO moves */
49 /* Minimum HAX kernel version */
50 const uint32_t hax_min_version = 0x4; /* API v4: supports unmapping */
52 static bool hax_allowed;
54 struct hax_state hax_global;
56 static void hax_vcpu_sync_state(CPUArchState *env, int modified);
57 static int hax_arch_get_registers(CPUArchState *env);
59 int hax_enabled(void)
61 return hax_allowed;
64 int valid_hax_tunnel_size(uint16_t size)
66 return size >= sizeof(struct hax_tunnel);
69 hax_fd hax_vcpu_get_fd(CPUArchState *env)
71 struct hax_vcpu_state *vcpu = env_cpu(env)->hax_vcpu;
72 if (!vcpu) {
73 return HAX_INVALID_FD;
75 return vcpu->fd;
78 static int hax_get_capability(struct hax_state *hax)
80 int ret;
81 struct hax_capabilityinfo capinfo, *cap = &capinfo;
83 ret = hax_capability(hax, cap);
84 if (ret) {
85 return ret;
88 if ((cap->wstatus & HAX_CAP_WORKSTATUS_MASK) == HAX_CAP_STATUS_NOTWORKING) {
89 if (cap->winfo & HAX_CAP_FAILREASON_VT) {
90 DPRINTF
91 ("VTX feature is not enabled, HAX driver will not work.\n");
92 } else if (cap->winfo & HAX_CAP_FAILREASON_NX) {
93 DPRINTF
94 ("NX feature is not enabled, HAX driver will not work.\n");
96 return -ENXIO;
100 if (!(cap->winfo & HAX_CAP_UG)) {
101 fprintf(stderr, "UG mode is not supported by the hardware.\n");
102 return -ENOTSUP;
105 hax->supports_64bit_ramblock = !!(cap->winfo & HAX_CAP_64BIT_RAMBLOCK);
107 if (cap->wstatus & HAX_CAP_MEMQUOTA) {
108 if (cap->mem_quota < hax->mem_quota) {
109 fprintf(stderr, "The VM memory needed exceeds the driver limit.\n");
110 return -ENOSPC;
113 return 0;
116 static int hax_version_support(struct hax_state *hax)
118 int ret;
119 struct hax_module_version version;
121 ret = hax_mod_version(hax, &version);
122 if (ret < 0) {
123 return 0;
126 if (hax_min_version > version.cur_version) {
127 fprintf(stderr, "Incompatible HAX module version %d,",
128 version.cur_version);
129 fprintf(stderr, "requires minimum version %d\n", hax_min_version);
130 return 0;
132 if (hax_cur_version < version.compat_version) {
133 fprintf(stderr, "Incompatible QEMU HAX API version %x,",
134 hax_cur_version);
135 fprintf(stderr, "requires minimum HAX API version %x\n",
136 version.compat_version);
137 return 0;
140 return 1;
143 int hax_vcpu_create(int id)
145 struct hax_vcpu_state *vcpu = NULL;
146 int ret;
148 if (!hax_global.vm) {
149 fprintf(stderr, "vcpu %x created failed, vm is null\n", id);
150 return -1;
153 if (hax_global.vm->vcpus[id]) {
154 fprintf(stderr, "vcpu %x allocated already\n", id);
155 return 0;
158 vcpu = g_new0(struct hax_vcpu_state, 1);
160 ret = hax_host_create_vcpu(hax_global.vm->fd, id);
161 if (ret) {
162 fprintf(stderr, "Failed to create vcpu %x\n", id);
163 goto error;
166 vcpu->vcpu_id = id;
167 vcpu->fd = hax_host_open_vcpu(hax_global.vm->id, id);
168 if (hax_invalid_fd(vcpu->fd)) {
169 fprintf(stderr, "Failed to open the vcpu\n");
170 ret = -ENODEV;
171 goto error;
174 hax_global.vm->vcpus[id] = vcpu;
176 ret = hax_host_setup_vcpu_channel(vcpu);
177 if (ret) {
178 fprintf(stderr, "Invalid hax tunnel size\n");
179 ret = -EINVAL;
180 goto error;
182 return 0;
184 error:
185 /* vcpu and tunnel will be closed automatically */
186 if (vcpu && !hax_invalid_fd(vcpu->fd)) {
187 hax_close_fd(vcpu->fd);
190 hax_global.vm->vcpus[id] = NULL;
191 g_free(vcpu);
192 return -1;
195 int hax_vcpu_destroy(CPUState *cpu)
197 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
199 if (!hax_global.vm) {
200 fprintf(stderr, "vcpu %x destroy failed, vm is null\n", vcpu->vcpu_id);
201 return -1;
204 if (!vcpu) {
205 return 0;
209 * 1. The hax_tunnel is also destroyed when vcpu is destroyed
210 * 2. close fd will cause hax module vcpu be cleaned
212 hax_close_fd(vcpu->fd);
213 hax_global.vm->vcpus[vcpu->vcpu_id] = NULL;
214 g_free(vcpu);
215 return 0;
218 int hax_init_vcpu(CPUState *cpu)
220 int ret;
222 ret = hax_vcpu_create(cpu->cpu_index);
223 if (ret < 0) {
224 fprintf(stderr, "Failed to create HAX vcpu\n");
225 exit(-1);
228 cpu->hax_vcpu = hax_global.vm->vcpus[cpu->cpu_index];
229 cpu->vcpu_dirty = true;
230 qemu_register_reset(hax_reset_vcpu_state, (CPUArchState *) (cpu->env_ptr));
232 return ret;
235 struct hax_vm *hax_vm_create(struct hax_state *hax, int max_cpus)
237 struct hax_vm *vm;
238 int vm_id = 0, ret, i;
240 if (hax_invalid_fd(hax->fd)) {
241 return NULL;
244 if (hax->vm) {
245 return hax->vm;
248 if (max_cpus > HAX_MAX_VCPU) {
249 fprintf(stderr, "Maximum VCPU number QEMU supported is %d\n", HAX_MAX_VCPU);
250 return NULL;
253 vm = g_new0(struct hax_vm, 1);
255 ret = hax_host_create_vm(hax, &vm_id);
256 if (ret) {
257 fprintf(stderr, "Failed to create vm %x\n", ret);
258 goto error;
260 vm->id = vm_id;
261 vm->fd = hax_host_open_vm(hax, vm_id);
262 if (hax_invalid_fd(vm->fd)) {
263 fprintf(stderr, "Failed to open vm %d\n", vm_id);
264 goto error;
267 vm->numvcpus = max_cpus;
268 vm->vcpus = g_new0(struct hax_vcpu_state *, vm->numvcpus);
269 for (i = 0; i < vm->numvcpus; i++) {
270 vm->vcpus[i] = NULL;
273 hax->vm = vm;
274 return vm;
276 error:
277 g_free(vm);
278 hax->vm = NULL;
279 return NULL;
282 int hax_vm_destroy(struct hax_vm *vm)
284 int i;
286 for (i = 0; i < vm->numvcpus; i++)
287 if (vm->vcpus[i]) {
288 fprintf(stderr, "VCPU should be cleaned before vm clean\n");
289 return -1;
291 hax_close_fd(vm->fd);
292 vm->numvcpus = 0;
293 g_free(vm->vcpus);
294 g_free(vm);
295 hax_global.vm = NULL;
296 return 0;
299 static int hax_init(ram_addr_t ram_size, int max_cpus)
301 struct hax_state *hax = NULL;
302 struct hax_qemu_version qversion;
303 int ret;
305 hax = &hax_global;
307 memset(hax, 0, sizeof(struct hax_state));
308 hax->mem_quota = ram_size;
310 hax->fd = hax_mod_open();
311 if (hax_invalid_fd(hax->fd)) {
312 hax->fd = 0;
313 ret = -ENODEV;
314 goto error;
317 ret = hax_get_capability(hax);
319 if (ret) {
320 if (ret != -ENOSPC) {
321 ret = -EINVAL;
323 goto error;
326 if (!hax_version_support(hax)) {
327 ret = -EINVAL;
328 goto error;
331 hax->vm = hax_vm_create(hax, max_cpus);
332 if (!hax->vm) {
333 fprintf(stderr, "Failed to create HAX VM\n");
334 ret = -EINVAL;
335 goto error;
338 hax_memory_init();
340 qversion.cur_version = hax_cur_version;
341 qversion.min_version = hax_min_version;
342 hax_notify_qemu_version(hax->vm->fd, &qversion);
344 return ret;
345 error:
346 if (hax->vm) {
347 hax_vm_destroy(hax->vm);
349 if (hax->fd) {
350 hax_mod_close(hax);
353 return ret;
356 static int hax_accel_init(MachineState *ms)
358 int ret = hax_init(ms->ram_size, (int)ms->smp.max_cpus);
360 if (ret && (ret != -ENOSPC)) {
361 fprintf(stderr, "No accelerator found.\n");
362 } else {
363 fprintf(stdout, "HAX is %s and emulator runs in %s mode.\n",
364 !ret ? "working" : "not working",
365 !ret ? "fast virt" : "emulation");
367 if (ret == 0) {
368 cpus_register_accel(&hax_cpus);
370 return ret;
373 static int hax_handle_fastmmio(CPUArchState *env, struct hax_fastmmio *hft)
375 if (hft->direction < 2) {
376 cpu_physical_memory_rw(hft->gpa, &hft->value, hft->size,
377 hft->direction);
378 } else {
380 * HAX API v4 supports transferring data between two MMIO addresses,
381 * hft->gpa and hft->gpa2 (instructions such as MOVS require this):
382 * hft->direction == 2: gpa ==> gpa2
384 uint64_t value;
385 cpu_physical_memory_read(hft->gpa, &value, hft->size);
386 cpu_physical_memory_write(hft->gpa2, &value, hft->size);
389 return 0;
392 static int hax_handle_io(CPUArchState *env, uint32_t df, uint16_t port,
393 int direction, int size, int count, void *buffer)
395 uint8_t *ptr;
396 int i;
397 MemTxAttrs attrs = { 0 };
399 if (!df) {
400 ptr = (uint8_t *) buffer;
401 } else {
402 ptr = buffer + size * count - size;
404 for (i = 0; i < count; i++) {
405 address_space_rw(&address_space_io, port, attrs,
406 ptr, size, direction == HAX_EXIT_IO_OUT);
407 if (!df) {
408 ptr += size;
409 } else {
410 ptr -= size;
414 return 0;
417 static int hax_vcpu_interrupt(CPUArchState *env)
419 CPUState *cpu = env_cpu(env);
420 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
421 struct hax_tunnel *ht = vcpu->tunnel;
424 * Try to inject an interrupt if the guest can accept it
425 * Unlike KVM, HAX kernel check for the eflags, instead of qemu
427 if (ht->ready_for_interrupt_injection &&
428 (cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
429 int irq;
431 irq = cpu_get_pic_interrupt(env);
432 if (irq >= 0) {
433 hax_inject_interrupt(env, irq);
434 cpu->interrupt_request &= ~CPU_INTERRUPT_HARD;
438 /* If we have an interrupt but the guest is not ready to receive an
439 * interrupt, request an interrupt window exit. This will
440 * cause a return to userspace as soon as the guest is ready to
441 * receive interrupts. */
442 if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
443 ht->request_interrupt_window = 1;
444 } else {
445 ht->request_interrupt_window = 0;
447 return 0;
450 void hax_raise_event(CPUState *cpu)
452 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
454 if (!vcpu) {
455 return;
457 vcpu->tunnel->user_event_pending = 1;
461 * Ask hax kernel module to run the CPU for us till:
462 * 1. Guest crash or shutdown
463 * 2. Need QEMU's emulation like guest execute MMIO instruction
464 * 3. Guest execute HLT
465 * 4. QEMU have Signal/event pending
466 * 5. An unknown VMX exit happens
468 static int hax_vcpu_hax_exec(CPUArchState *env)
470 int ret = 0;
471 CPUState *cpu = env_cpu(env);
472 X86CPU *x86_cpu = X86_CPU(cpu);
473 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
474 struct hax_tunnel *ht = vcpu->tunnel;
476 if (!hax_enabled()) {
477 DPRINTF("Trying to vcpu execute at eip:" TARGET_FMT_lx "\n", env->eip);
478 return 0;
481 if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
482 cpu->interrupt_request &= ~CPU_INTERRUPT_POLL;
483 apic_poll_irq(x86_cpu->apic_state);
486 /* After a vcpu is halted (either because it is an AP and has just been
487 * reset, or because it has executed the HLT instruction), it will not be
488 * run (hax_vcpu_run()) until it is unhalted. The next few if blocks check
489 * for events that may change the halted state of this vcpu:
490 * a) Maskable interrupt, when RFLAGS.IF is 1;
491 * Note: env->eflags may not reflect the current RFLAGS state, because
492 * it is not updated after each hax_vcpu_run(). We cannot afford
493 * to fail to recognize any unhalt-by-maskable-interrupt event
494 * (in which case the vcpu will halt forever), and yet we cannot
495 * afford the overhead of hax_vcpu_sync_state(). The current
496 * solution is to err on the side of caution and have the HLT
497 * handler (see case HAX_EXIT_HLT below) unconditionally set the
498 * IF_MASK bit in env->eflags, which, in effect, disables the
499 * RFLAGS.IF check.
500 * b) NMI;
501 * c) INIT signal;
502 * d) SIPI signal.
504 if (((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
505 (env->eflags & IF_MASK)) ||
506 (cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
507 cpu->halted = 0;
510 if (cpu->interrupt_request & CPU_INTERRUPT_INIT) {
511 DPRINTF("\nhax_vcpu_hax_exec: handling INIT for %d\n",
512 cpu->cpu_index);
513 do_cpu_init(x86_cpu);
514 hax_vcpu_sync_state(env, 1);
517 if (cpu->interrupt_request & CPU_INTERRUPT_SIPI) {
518 DPRINTF("hax_vcpu_hax_exec: handling SIPI for %d\n",
519 cpu->cpu_index);
520 hax_vcpu_sync_state(env, 0);
521 do_cpu_sipi(x86_cpu);
522 hax_vcpu_sync_state(env, 1);
525 if (cpu->halted) {
526 /* If this vcpu is halted, we must not ask HAXM to run it. Instead, we
527 * break out of hax_smp_cpu_exec() as if this vcpu had executed HLT.
528 * That way, this vcpu thread will be trapped in qemu_wait_io_event(),
529 * until the vcpu is unhalted.
531 cpu->exception_index = EXCP_HLT;
532 return 0;
535 do {
536 int hax_ret;
538 if (cpu->exit_request) {
539 ret = 1;
540 break;
543 hax_vcpu_interrupt(env);
545 qemu_mutex_unlock_iothread();
546 cpu_exec_start(cpu);
547 hax_ret = hax_vcpu_run(vcpu);
548 cpu_exec_end(cpu);
549 qemu_mutex_lock_iothread();
551 /* Simply continue the vcpu_run if system call interrupted */
552 if (hax_ret == -EINTR || hax_ret == -EAGAIN) {
553 DPRINTF("io window interrupted\n");
554 continue;
557 if (hax_ret < 0) {
558 fprintf(stderr, "vcpu run failed for vcpu %x\n", vcpu->vcpu_id);
559 abort();
561 switch (ht->_exit_status) {
562 case HAX_EXIT_IO:
563 ret = hax_handle_io(env, ht->pio._df, ht->pio._port,
564 ht->pio._direction,
565 ht->pio._size, ht->pio._count, vcpu->iobuf);
566 break;
567 case HAX_EXIT_FAST_MMIO:
568 ret = hax_handle_fastmmio(env, (struct hax_fastmmio *) vcpu->iobuf);
569 break;
570 /* Guest state changed, currently only for shutdown */
571 case HAX_EXIT_STATECHANGE:
572 fprintf(stdout, "VCPU shutdown request\n");
573 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
574 hax_vcpu_sync_state(env, 0);
575 ret = 1;
576 break;
577 case HAX_EXIT_UNKNOWN_VMEXIT:
578 fprintf(stderr, "Unknown VMX exit %x from guest\n",
579 ht->_exit_reason);
580 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
581 hax_vcpu_sync_state(env, 0);
582 cpu_dump_state(cpu, stderr, 0);
583 ret = -1;
584 break;
585 case HAX_EXIT_HLT:
586 if (!(cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
587 !(cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
588 /* hlt instruction with interrupt disabled is shutdown */
589 env->eflags |= IF_MASK;
590 cpu->halted = 1;
591 cpu->exception_index = EXCP_HLT;
592 ret = 1;
594 break;
595 /* these situations will continue to hax module */
596 case HAX_EXIT_INTERRUPT:
597 case HAX_EXIT_PAUSED:
598 break;
599 case HAX_EXIT_MMIO:
600 /* Should not happen on UG system */
601 fprintf(stderr, "HAX: unsupported MMIO emulation\n");
602 ret = -1;
603 break;
604 case HAX_EXIT_REAL:
605 /* Should not happen on UG system */
606 fprintf(stderr, "HAX: unimplemented real mode emulation\n");
607 ret = -1;
608 break;
609 default:
610 fprintf(stderr, "Unknown exit %x from HAX\n", ht->_exit_status);
611 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
612 hax_vcpu_sync_state(env, 0);
613 cpu_dump_state(cpu, stderr, 0);
614 ret = 1;
615 break;
617 } while (!ret);
619 if (cpu->exit_request) {
620 cpu->exit_request = 0;
621 cpu->exception_index = EXCP_INTERRUPT;
623 return ret < 0;
626 static void do_hax_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
628 CPUArchState *env = cpu->env_ptr;
630 hax_arch_get_registers(env);
631 cpu->vcpu_dirty = true;
634 void hax_cpu_synchronize_state(CPUState *cpu)
636 if (!cpu->vcpu_dirty) {
637 run_on_cpu(cpu, do_hax_cpu_synchronize_state, RUN_ON_CPU_NULL);
641 static void do_hax_cpu_synchronize_post_reset(CPUState *cpu,
642 run_on_cpu_data arg)
644 CPUArchState *env = cpu->env_ptr;
646 hax_vcpu_sync_state(env, 1);
647 cpu->vcpu_dirty = false;
650 void hax_cpu_synchronize_post_reset(CPUState *cpu)
652 run_on_cpu(cpu, do_hax_cpu_synchronize_post_reset, RUN_ON_CPU_NULL);
655 static void do_hax_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
657 CPUArchState *env = cpu->env_ptr;
659 hax_vcpu_sync_state(env, 1);
660 cpu->vcpu_dirty = false;
663 void hax_cpu_synchronize_post_init(CPUState *cpu)
665 run_on_cpu(cpu, do_hax_cpu_synchronize_post_init, RUN_ON_CPU_NULL);
668 static void do_hax_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg)
670 cpu->vcpu_dirty = true;
673 void hax_cpu_synchronize_pre_loadvm(CPUState *cpu)
675 run_on_cpu(cpu, do_hax_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL);
678 int hax_smp_cpu_exec(CPUState *cpu)
680 CPUArchState *env = (CPUArchState *) (cpu->env_ptr);
681 int fatal;
682 int ret;
684 while (1) {
685 if (cpu->exception_index >= EXCP_INTERRUPT) {
686 ret = cpu->exception_index;
687 cpu->exception_index = -1;
688 break;
691 fatal = hax_vcpu_hax_exec(env);
693 if (fatal) {
694 fprintf(stderr, "Unsupported HAX vcpu return\n");
695 abort();
699 return ret;
702 static void set_v8086_seg(struct segment_desc_t *lhs, const SegmentCache *rhs)
704 memset(lhs, 0, sizeof(struct segment_desc_t));
705 lhs->selector = rhs->selector;
706 lhs->base = rhs->base;
707 lhs->limit = rhs->limit;
708 lhs->type = 3;
709 lhs->present = 1;
710 lhs->dpl = 3;
711 lhs->operand_size = 0;
712 lhs->desc = 1;
713 lhs->long_mode = 0;
714 lhs->granularity = 0;
715 lhs->available = 0;
718 static void get_seg(SegmentCache *lhs, const struct segment_desc_t *rhs)
720 lhs->selector = rhs->selector;
721 lhs->base = rhs->base;
722 lhs->limit = rhs->limit;
723 lhs->flags = (rhs->type << DESC_TYPE_SHIFT)
724 | (rhs->present * DESC_P_MASK)
725 | (rhs->dpl << DESC_DPL_SHIFT)
726 | (rhs->operand_size << DESC_B_SHIFT)
727 | (rhs->desc * DESC_S_MASK)
728 | (rhs->long_mode << DESC_L_SHIFT)
729 | (rhs->granularity * DESC_G_MASK) | (rhs->available * DESC_AVL_MASK);
732 static void set_seg(struct segment_desc_t *lhs, const SegmentCache *rhs)
734 unsigned flags = rhs->flags;
736 memset(lhs, 0, sizeof(struct segment_desc_t));
737 lhs->selector = rhs->selector;
738 lhs->base = rhs->base;
739 lhs->limit = rhs->limit;
740 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
741 lhs->present = (flags & DESC_P_MASK) != 0;
742 lhs->dpl = rhs->selector & 3;
743 lhs->operand_size = (flags >> DESC_B_SHIFT) & 1;
744 lhs->desc = (flags & DESC_S_MASK) != 0;
745 lhs->long_mode = (flags >> DESC_L_SHIFT) & 1;
746 lhs->granularity = (flags & DESC_G_MASK) != 0;
747 lhs->available = (flags & DESC_AVL_MASK) != 0;
750 static void hax_getput_reg(uint64_t *hax_reg, target_ulong *qemu_reg, int set)
752 target_ulong reg = *hax_reg;
754 if (set) {
755 *hax_reg = *qemu_reg;
756 } else {
757 *qemu_reg = reg;
761 /* The sregs has been synced with HAX kernel already before this call */
762 static int hax_get_segments(CPUArchState *env, struct vcpu_state_t *sregs)
764 get_seg(&env->segs[R_CS], &sregs->_cs);
765 get_seg(&env->segs[R_DS], &sregs->_ds);
766 get_seg(&env->segs[R_ES], &sregs->_es);
767 get_seg(&env->segs[R_FS], &sregs->_fs);
768 get_seg(&env->segs[R_GS], &sregs->_gs);
769 get_seg(&env->segs[R_SS], &sregs->_ss);
771 get_seg(&env->tr, &sregs->_tr);
772 get_seg(&env->ldt, &sregs->_ldt);
773 env->idt.limit = sregs->_idt.limit;
774 env->idt.base = sregs->_idt.base;
775 env->gdt.limit = sregs->_gdt.limit;
776 env->gdt.base = sregs->_gdt.base;
777 return 0;
780 static int hax_set_segments(CPUArchState *env, struct vcpu_state_t *sregs)
782 if ((env->eflags & VM_MASK)) {
783 set_v8086_seg(&sregs->_cs, &env->segs[R_CS]);
784 set_v8086_seg(&sregs->_ds, &env->segs[R_DS]);
785 set_v8086_seg(&sregs->_es, &env->segs[R_ES]);
786 set_v8086_seg(&sregs->_fs, &env->segs[R_FS]);
787 set_v8086_seg(&sregs->_gs, &env->segs[R_GS]);
788 set_v8086_seg(&sregs->_ss, &env->segs[R_SS]);
789 } else {
790 set_seg(&sregs->_cs, &env->segs[R_CS]);
791 set_seg(&sregs->_ds, &env->segs[R_DS]);
792 set_seg(&sregs->_es, &env->segs[R_ES]);
793 set_seg(&sregs->_fs, &env->segs[R_FS]);
794 set_seg(&sregs->_gs, &env->segs[R_GS]);
795 set_seg(&sregs->_ss, &env->segs[R_SS]);
797 if (env->cr[0] & CR0_PE_MASK) {
798 /* force ss cpl to cs cpl */
799 sregs->_ss.selector = (sregs->_ss.selector & ~3) |
800 (sregs->_cs.selector & 3);
801 sregs->_ss.dpl = sregs->_ss.selector & 3;
805 set_seg(&sregs->_tr, &env->tr);
806 set_seg(&sregs->_ldt, &env->ldt);
807 sregs->_idt.limit = env->idt.limit;
808 sregs->_idt.base = env->idt.base;
809 sregs->_gdt.limit = env->gdt.limit;
810 sregs->_gdt.base = env->gdt.base;
811 return 0;
814 static int hax_sync_vcpu_register(CPUArchState *env, int set)
816 struct vcpu_state_t regs;
817 int ret;
818 memset(&regs, 0, sizeof(struct vcpu_state_t));
820 if (!set) {
821 ret = hax_sync_vcpu_state(env, &regs, 0);
822 if (ret < 0) {
823 return -1;
827 /* generic register */
828 hax_getput_reg(&regs._rax, &env->regs[R_EAX], set);
829 hax_getput_reg(&regs._rbx, &env->regs[R_EBX], set);
830 hax_getput_reg(&regs._rcx, &env->regs[R_ECX], set);
831 hax_getput_reg(&regs._rdx, &env->regs[R_EDX], set);
832 hax_getput_reg(&regs._rsi, &env->regs[R_ESI], set);
833 hax_getput_reg(&regs._rdi, &env->regs[R_EDI], set);
834 hax_getput_reg(&regs._rsp, &env->regs[R_ESP], set);
835 hax_getput_reg(&regs._rbp, &env->regs[R_EBP], set);
836 #ifdef TARGET_X86_64
837 hax_getput_reg(&regs._r8, &env->regs[8], set);
838 hax_getput_reg(&regs._r9, &env->regs[9], set);
839 hax_getput_reg(&regs._r10, &env->regs[10], set);
840 hax_getput_reg(&regs._r11, &env->regs[11], set);
841 hax_getput_reg(&regs._r12, &env->regs[12], set);
842 hax_getput_reg(&regs._r13, &env->regs[13], set);
843 hax_getput_reg(&regs._r14, &env->regs[14], set);
844 hax_getput_reg(&regs._r15, &env->regs[15], set);
845 #endif
846 hax_getput_reg(&regs._rflags, &env->eflags, set);
847 hax_getput_reg(&regs._rip, &env->eip, set);
849 if (set) {
850 regs._cr0 = env->cr[0];
851 regs._cr2 = env->cr[2];
852 regs._cr3 = env->cr[3];
853 regs._cr4 = env->cr[4];
854 hax_set_segments(env, &regs);
855 } else {
856 env->cr[0] = regs._cr0;
857 env->cr[2] = regs._cr2;
858 env->cr[3] = regs._cr3;
859 env->cr[4] = regs._cr4;
860 hax_get_segments(env, &regs);
863 if (set) {
864 ret = hax_sync_vcpu_state(env, &regs, 1);
865 if (ret < 0) {
866 return -1;
869 return 0;
872 static void hax_msr_entry_set(struct vmx_msr *item, uint32_t index,
873 uint64_t value)
875 item->entry = index;
876 item->value = value;
879 static int hax_get_msrs(CPUArchState *env)
881 struct hax_msr_data md;
882 struct vmx_msr *msrs = md.entries;
883 int ret, i, n;
885 n = 0;
886 msrs[n++].entry = MSR_IA32_SYSENTER_CS;
887 msrs[n++].entry = MSR_IA32_SYSENTER_ESP;
888 msrs[n++].entry = MSR_IA32_SYSENTER_EIP;
889 msrs[n++].entry = MSR_IA32_TSC;
890 #ifdef TARGET_X86_64
891 msrs[n++].entry = MSR_EFER;
892 msrs[n++].entry = MSR_STAR;
893 msrs[n++].entry = MSR_LSTAR;
894 msrs[n++].entry = MSR_CSTAR;
895 msrs[n++].entry = MSR_FMASK;
896 msrs[n++].entry = MSR_KERNELGSBASE;
897 #endif
898 md.nr_msr = n;
899 ret = hax_sync_msr(env, &md, 0);
900 if (ret < 0) {
901 return ret;
904 for (i = 0; i < md.done; i++) {
905 switch (msrs[i].entry) {
906 case MSR_IA32_SYSENTER_CS:
907 env->sysenter_cs = msrs[i].value;
908 break;
909 case MSR_IA32_SYSENTER_ESP:
910 env->sysenter_esp = msrs[i].value;
911 break;
912 case MSR_IA32_SYSENTER_EIP:
913 env->sysenter_eip = msrs[i].value;
914 break;
915 case MSR_IA32_TSC:
916 env->tsc = msrs[i].value;
917 break;
918 #ifdef TARGET_X86_64
919 case MSR_EFER:
920 env->efer = msrs[i].value;
921 break;
922 case MSR_STAR:
923 env->star = msrs[i].value;
924 break;
925 case MSR_LSTAR:
926 env->lstar = msrs[i].value;
927 break;
928 case MSR_CSTAR:
929 env->cstar = msrs[i].value;
930 break;
931 case MSR_FMASK:
932 env->fmask = msrs[i].value;
933 break;
934 case MSR_KERNELGSBASE:
935 env->kernelgsbase = msrs[i].value;
936 break;
937 #endif
941 return 0;
944 static int hax_set_msrs(CPUArchState *env)
946 struct hax_msr_data md;
947 struct vmx_msr *msrs;
948 msrs = md.entries;
949 int n = 0;
951 memset(&md, 0, sizeof(struct hax_msr_data));
952 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
953 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
954 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
955 hax_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
956 #ifdef TARGET_X86_64
957 hax_msr_entry_set(&msrs[n++], MSR_EFER, env->efer);
958 hax_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
959 hax_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
960 hax_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
961 hax_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
962 hax_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
963 #endif
964 md.nr_msr = n;
965 md.done = 0;
967 return hax_sync_msr(env, &md, 1);
970 static int hax_get_fpu(CPUArchState *env)
972 struct fx_layout fpu;
973 int i, ret;
975 ret = hax_sync_fpu(env, &fpu, 0);
976 if (ret < 0) {
977 return ret;
980 env->fpstt = (fpu.fsw >> 11) & 7;
981 env->fpus = fpu.fsw;
982 env->fpuc = fpu.fcw;
983 for (i = 0; i < 8; ++i) {
984 env->fptags[i] = !((fpu.ftw >> i) & 1);
986 memcpy(env->fpregs, fpu.st_mm, sizeof(env->fpregs));
988 for (i = 0; i < 8; i++) {
989 env->xmm_regs[i].ZMM_Q(0) = ldq_p(&fpu.mmx_1[i][0]);
990 env->xmm_regs[i].ZMM_Q(1) = ldq_p(&fpu.mmx_1[i][8]);
991 if (CPU_NB_REGS > 8) {
992 env->xmm_regs[i + 8].ZMM_Q(0) = ldq_p(&fpu.mmx_2[i][0]);
993 env->xmm_regs[i + 8].ZMM_Q(1) = ldq_p(&fpu.mmx_2[i][8]);
996 env->mxcsr = fpu.mxcsr;
998 return 0;
1001 static int hax_set_fpu(CPUArchState *env)
1003 struct fx_layout fpu;
1004 int i;
1006 memset(&fpu, 0, sizeof(fpu));
1007 fpu.fsw = env->fpus & ~(7 << 11);
1008 fpu.fsw |= (env->fpstt & 7) << 11;
1009 fpu.fcw = env->fpuc;
1011 for (i = 0; i < 8; ++i) {
1012 fpu.ftw |= (!env->fptags[i]) << i;
1015 memcpy(fpu.st_mm, env->fpregs, sizeof(env->fpregs));
1016 for (i = 0; i < 8; i++) {
1017 stq_p(&fpu.mmx_1[i][0], env->xmm_regs[i].ZMM_Q(0));
1018 stq_p(&fpu.mmx_1[i][8], env->xmm_regs[i].ZMM_Q(1));
1019 if (CPU_NB_REGS > 8) {
1020 stq_p(&fpu.mmx_2[i][0], env->xmm_regs[i + 8].ZMM_Q(0));
1021 stq_p(&fpu.mmx_2[i][8], env->xmm_regs[i + 8].ZMM_Q(1));
1025 fpu.mxcsr = env->mxcsr;
1027 return hax_sync_fpu(env, &fpu, 1);
1030 static int hax_arch_get_registers(CPUArchState *env)
1032 int ret;
1034 ret = hax_sync_vcpu_register(env, 0);
1035 if (ret < 0) {
1036 return ret;
1039 ret = hax_get_fpu(env);
1040 if (ret < 0) {
1041 return ret;
1044 ret = hax_get_msrs(env);
1045 if (ret < 0) {
1046 return ret;
1049 x86_update_hflags(env);
1050 return 0;
1053 static int hax_arch_set_registers(CPUArchState *env)
1055 int ret;
1056 ret = hax_sync_vcpu_register(env, 1);
1058 if (ret < 0) {
1059 fprintf(stderr, "Failed to sync vcpu reg\n");
1060 return ret;
1062 ret = hax_set_fpu(env);
1063 if (ret < 0) {
1064 fprintf(stderr, "FPU failed\n");
1065 return ret;
1067 ret = hax_set_msrs(env);
1068 if (ret < 0) {
1069 fprintf(stderr, "MSR failed\n");
1070 return ret;
1073 return 0;
1076 static void hax_vcpu_sync_state(CPUArchState *env, int modified)
1078 if (hax_enabled()) {
1079 if (modified) {
1080 hax_arch_set_registers(env);
1081 } else {
1082 hax_arch_get_registers(env);
1088 * much simpler than kvm, at least in first stage because:
1089 * We don't need consider the device pass-through, we don't need
1090 * consider the framebuffer, and we may even remove the bios at all
1092 int hax_sync_vcpus(void)
1094 if (hax_enabled()) {
1095 CPUState *cpu;
1097 cpu = first_cpu;
1098 if (!cpu) {
1099 return 0;
1102 for (; cpu != NULL; cpu = CPU_NEXT(cpu)) {
1103 int ret;
1105 ret = hax_arch_set_registers(cpu->env_ptr);
1106 if (ret < 0) {
1107 return ret;
1112 return 0;
1115 void hax_reset_vcpu_state(void *opaque)
1117 CPUState *cpu;
1118 for (cpu = first_cpu; cpu != NULL; cpu = CPU_NEXT(cpu)) {
1119 cpu->hax_vcpu->tunnel->user_event_pending = 0;
1120 cpu->hax_vcpu->tunnel->ready_for_interrupt_injection = 0;
1124 static void hax_accel_class_init(ObjectClass *oc, void *data)
1126 AccelClass *ac = ACCEL_CLASS(oc);
1127 ac->name = "HAX";
1128 ac->init_machine = hax_accel_init;
1129 ac->allowed = &hax_allowed;
1132 static const TypeInfo hax_accel_type = {
1133 .name = ACCEL_CLASS_NAME("hax"),
1134 .parent = TYPE_ACCEL,
1135 .class_init = hax_accel_class_init,
1138 static void hax_type_init(void)
1140 type_register_static(&hax_accel_type);
1143 type_init(hax_type_init);