]> git.proxmox.com Git - mirror_qemu.git/blame - target/i386/hax/hax-all.c
target: Use ArchCPU as interface to target CPU
[mirror_qemu.git] / target / i386 / hax / hax-all.c
CommitLineData
47c1c8c1
VP
1/*
2 * QEMU HAX support
3 *
4 * Copyright IBM, Corp. 2008
5 * Red Hat, Inc. 2008
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Glauber Costa <gcosta@redhat.com>
10 *
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>
16 *
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.
19 *
20 */
21
22/*
23 * HAX common code for both windows and darwin
24 */
25
26#include "qemu/osdep.h"
27#include "cpu.h"
28#include "exec/address-spaces.h"
47c1c8c1
VP
29
30#include "qemu-common.h"
940e43aa 31#include "qemu/accel.h"
71e8a915 32#include "sysemu/reset.h"
54d31236 33#include "sysemu/runstate.h"
47c1c8c1
VP
34#include "hw/boards.h"
35
b86f59c7 36#include "hax-accel-ops.h"
e92558e4 37
47c1c8c1
VP
38#define DEBUG_HAX 0
39
40#define DPRINTF(fmt, ...) \
41 do { \
42 if (DEBUG_HAX) { \
43 fprintf(stdout, fmt, ## __VA_ARGS__); \
44 } \
45 } while (0)
46
47/* Current version */
48const uint32_t hax_cur_version = 0x4; /* API v4: unmapping and MMIO moves */
49/* Minimum HAX kernel version */
50const uint32_t hax_min_version = 0x4; /* API v4: supports unmapping */
51
b04363c2 52bool hax_allowed;
47c1c8c1
VP
53
54struct hax_state hax_global;
55
56static void hax_vcpu_sync_state(CPUArchState *env, int modified);
57static int hax_arch_get_registers(CPUArchState *env);
58
47c1c8c1
VP
59int valid_hax_tunnel_size(uint16_t size)
60{
61 return size >= sizeof(struct hax_tunnel);
62}
63
64hax_fd hax_vcpu_get_fd(CPUArchState *env)
65{
29a0af61 66 struct hax_vcpu_state *vcpu = env_cpu(env)->hax_vcpu;
47c1c8c1
VP
67 if (!vcpu) {
68 return HAX_INVALID_FD;
69 }
70 return vcpu->fd;
71}
72
73static int hax_get_capability(struct hax_state *hax)
74{
75 int ret;
76 struct hax_capabilityinfo capinfo, *cap = &capinfo;
77
78 ret = hax_capability(hax, cap);
79 if (ret) {
80 return ret;
81 }
82
83 if ((cap->wstatus & HAX_CAP_WORKSTATUS_MASK) == HAX_CAP_STATUS_NOTWORKING) {
84 if (cap->winfo & HAX_CAP_FAILREASON_VT) {
85 DPRINTF
86 ("VTX feature is not enabled, HAX driver will not work.\n");
87 } else if (cap->winfo & HAX_CAP_FAILREASON_NX) {
88 DPRINTF
89 ("NX feature is not enabled, HAX driver will not work.\n");
90 }
91 return -ENXIO;
92
93 }
94
95 if (!(cap->winfo & HAX_CAP_UG)) {
96 fprintf(stderr, "UG mode is not supported by the hardware.\n");
97 return -ENOTSUP;
98 }
99
7a5235c9
YN
100 hax->supports_64bit_ramblock = !!(cap->winfo & HAX_CAP_64BIT_RAMBLOCK);
101
47c1c8c1
VP
102 if (cap->wstatus & HAX_CAP_MEMQUOTA) {
103 if (cap->mem_quota < hax->mem_quota) {
104 fprintf(stderr, "The VM memory needed exceeds the driver limit.\n");
105 return -ENOSPC;
106 }
107 }
108 return 0;
109}
110
111static int hax_version_support(struct hax_state *hax)
112{
113 int ret;
114 struct hax_module_version version;
115
116 ret = hax_mod_version(hax, &version);
117 if (ret < 0) {
118 return 0;
119 }
120
121 if (hax_min_version > version.cur_version) {
122 fprintf(stderr, "Incompatible HAX module version %d,",
123 version.cur_version);
124 fprintf(stderr, "requires minimum version %d\n", hax_min_version);
125 return 0;
126 }
127 if (hax_cur_version < version.compat_version) {
128 fprintf(stderr, "Incompatible QEMU HAX API version %x,",
129 hax_cur_version);
130 fprintf(stderr, "requires minimum HAX API version %x\n",
131 version.compat_version);
132 return 0;
133 }
134
135 return 1;
136}
137
138int hax_vcpu_create(int id)
139{
140 struct hax_vcpu_state *vcpu = NULL;
141 int ret;
142
143 if (!hax_global.vm) {
144 fprintf(stderr, "vcpu %x created failed, vm is null\n", id);
145 return -1;
146 }
147
148 if (hax_global.vm->vcpus[id]) {
149 fprintf(stderr, "vcpu %x allocated already\n", id);
150 return 0;
151 }
152
090627a9 153 vcpu = g_new0(struct hax_vcpu_state, 1);
47c1c8c1
VP
154
155 ret = hax_host_create_vcpu(hax_global.vm->fd, id);
156 if (ret) {
157 fprintf(stderr, "Failed to create vcpu %x\n", id);
158 goto error;
159 }
160
161 vcpu->vcpu_id = id;
162 vcpu->fd = hax_host_open_vcpu(hax_global.vm->id, id);
163 if (hax_invalid_fd(vcpu->fd)) {
164 fprintf(stderr, "Failed to open the vcpu\n");
165 ret = -ENODEV;
166 goto error;
167 }
168
169 hax_global.vm->vcpus[id] = vcpu;
170
171 ret = hax_host_setup_vcpu_channel(vcpu);
172 if (ret) {
173 fprintf(stderr, "Invalid hax tunnel size\n");
174 ret = -EINVAL;
175 goto error;
176 }
177 return 0;
178
179 error:
180 /* vcpu and tunnel will be closed automatically */
181 if (vcpu && !hax_invalid_fd(vcpu->fd)) {
182 hax_close_fd(vcpu->fd);
183 }
184
185 hax_global.vm->vcpus[id] = NULL;
186 g_free(vcpu);
187 return -1;
188}
189
190int hax_vcpu_destroy(CPUState *cpu)
191{
192 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
193
194 if (!hax_global.vm) {
195 fprintf(stderr, "vcpu %x destroy failed, vm is null\n", vcpu->vcpu_id);
196 return -1;
197 }
198
199 if (!vcpu) {
200 return 0;
201 }
202
203 /*
1d4f78e9 204 * 1. The hax_tunnel is also destroyed when vcpu is destroyed
47c1c8c1
VP
205 * 2. close fd will cause hax module vcpu be cleaned
206 */
207 hax_close_fd(vcpu->fd);
208 hax_global.vm->vcpus[vcpu->vcpu_id] = NULL;
209 g_free(vcpu);
210 return 0;
211}
212
213int hax_init_vcpu(CPUState *cpu)
214{
215 int ret;
216
217 ret = hax_vcpu_create(cpu->cpu_index);
218 if (ret < 0) {
219 fprintf(stderr, "Failed to create HAX vcpu\n");
220 exit(-1);
221 }
222
223 cpu->hax_vcpu = hax_global.vm->vcpus[cpu->cpu_index];
99f31832 224 cpu->vcpu_dirty = true;
47c1c8c1
VP
225 qemu_register_reset(hax_reset_vcpu_state, (CPUArchState *) (cpu->env_ptr));
226
227 return ret;
228}
229
34a09506 230struct hax_vm *hax_vm_create(struct hax_state *hax, int max_cpus)
47c1c8c1
VP
231{
232 struct hax_vm *vm;
34a09506 233 int vm_id = 0, ret, i;
47c1c8c1
VP
234
235 if (hax_invalid_fd(hax->fd)) {
236 return NULL;
237 }
238
239 if (hax->vm) {
240 return hax->vm;
241 }
242
34a09506
W
243 if (max_cpus > HAX_MAX_VCPU) {
244 fprintf(stderr, "Maximum VCPU number QEMU supported is %d\n", HAX_MAX_VCPU);
245 return NULL;
246 }
247
090627a9
LQ
248 vm = g_new0(struct hax_vm, 1);
249
47c1c8c1
VP
250 ret = hax_host_create_vm(hax, &vm_id);
251 if (ret) {
252 fprintf(stderr, "Failed to create vm %x\n", ret);
253 goto error;
254 }
255 vm->id = vm_id;
256 vm->fd = hax_host_open_vm(hax, vm_id);
257 if (hax_invalid_fd(vm->fd)) {
258 fprintf(stderr, "Failed to open vm %d\n", vm_id);
259 goto error;
260 }
261
34a09506
W
262 vm->numvcpus = max_cpus;
263 vm->vcpus = g_new0(struct hax_vcpu_state *, vm->numvcpus);
264 for (i = 0; i < vm->numvcpus; i++) {
265 vm->vcpus[i] = NULL;
266 }
267
47c1c8c1
VP
268 hax->vm = vm;
269 return vm;
270
271 error:
272 g_free(vm);
273 hax->vm = NULL;
274 return NULL;
275}
276
277int hax_vm_destroy(struct hax_vm *vm)
278{
279 int i;
280
34a09506 281 for (i = 0; i < vm->numvcpus; i++)
47c1c8c1
VP
282 if (vm->vcpus[i]) {
283 fprintf(stderr, "VCPU should be cleaned before vm clean\n");
284 return -1;
285 }
286 hax_close_fd(vm->fd);
34a09506
W
287 vm->numvcpus = 0;
288 g_free(vm->vcpus);
47c1c8c1
VP
289 g_free(vm);
290 hax_global.vm = NULL;
291 return 0;
292}
293
34a09506 294static int hax_init(ram_addr_t ram_size, int max_cpus)
47c1c8c1
VP
295{
296 struct hax_state *hax = NULL;
297 struct hax_qemu_version qversion;
298 int ret;
299
300 hax = &hax_global;
301
302 memset(hax, 0, sizeof(struct hax_state));
303 hax->mem_quota = ram_size;
304
305 hax->fd = hax_mod_open();
306 if (hax_invalid_fd(hax->fd)) {
307 hax->fd = 0;
308 ret = -ENODEV;
309 goto error;
310 }
311
312 ret = hax_get_capability(hax);
313
314 if (ret) {
315 if (ret != -ENOSPC) {
316 ret = -EINVAL;
317 }
318 goto error;
319 }
320
321 if (!hax_version_support(hax)) {
322 ret = -EINVAL;
323 goto error;
324 }
325
34a09506 326 hax->vm = hax_vm_create(hax, max_cpus);
47c1c8c1
VP
327 if (!hax->vm) {
328 fprintf(stderr, "Failed to create HAX VM\n");
329 ret = -EINVAL;
330 goto error;
331 }
332
333 hax_memory_init();
334
335 qversion.cur_version = hax_cur_version;
336 qversion.min_version = hax_min_version;
337 hax_notify_qemu_version(hax->vm->fd, &qversion);
47c1c8c1
VP
338
339 return ret;
340 error:
341 if (hax->vm) {
342 hax_vm_destroy(hax->vm);
343 }
344 if (hax->fd) {
345 hax_mod_close(hax);
346 }
347
348 return ret;
349}
350
351static int hax_accel_init(MachineState *ms)
352{
34a09506 353 int ret = hax_init(ms->ram_size, (int)ms->smp.max_cpus);
47c1c8c1
VP
354
355 if (ret && (ret != -ENOSPC)) {
356 fprintf(stderr, "No accelerator found.\n");
357 } else {
358 fprintf(stdout, "HAX is %s and emulator runs in %s mode.\n",
359 !ret ? "working" : "not working",
360 !ret ? "fast virt" : "emulation");
361 }
362 return ret;
363}
364
365static int hax_handle_fastmmio(CPUArchState *env, struct hax_fastmmio *hft)
366{
367 if (hft->direction < 2) {
0eeef0a4 368 cpu_physical_memory_rw(hft->gpa, &hft->value, hft->size,
47c1c8c1
VP
369 hft->direction);
370 } else {
371 /*
372 * HAX API v4 supports transferring data between two MMIO addresses,
373 * hft->gpa and hft->gpa2 (instructions such as MOVS require this):
374 * hft->direction == 2: gpa ==> gpa2
375 */
376 uint64_t value;
adeefe01
PMD
377 cpu_physical_memory_read(hft->gpa, &value, hft->size);
378 cpu_physical_memory_write(hft->gpa2, &value, hft->size);
47c1c8c1
VP
379 }
380
381 return 0;
382}
383
384static int hax_handle_io(CPUArchState *env, uint32_t df, uint16_t port,
385 int direction, int size, int count, void *buffer)
386{
387 uint8_t *ptr;
388 int i;
389 MemTxAttrs attrs = { 0 };
390
391 if (!df) {
392 ptr = (uint8_t *) buffer;
393 } else {
394 ptr = buffer + size * count - size;
395 }
396 for (i = 0; i < count; i++) {
397 address_space_rw(&address_space_io, port, attrs,
398 ptr, size, direction == HAX_EXIT_IO_OUT);
399 if (!df) {
400 ptr += size;
401 } else {
402 ptr -= size;
403 }
404 }
405
406 return 0;
407}
408
409static int hax_vcpu_interrupt(CPUArchState *env)
410{
29a0af61 411 CPUState *cpu = env_cpu(env);
47c1c8c1
VP
412 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
413 struct hax_tunnel *ht = vcpu->tunnel;
414
415 /*
416 * Try to inject an interrupt if the guest can accept it
417 * Unlike KVM, HAX kernel check for the eflags, instead of qemu
418 */
419 if (ht->ready_for_interrupt_injection &&
420 (cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
421 int irq;
422
423 irq = cpu_get_pic_interrupt(env);
424 if (irq >= 0) {
425 hax_inject_interrupt(env, irq);
426 cpu->interrupt_request &= ~CPU_INTERRUPT_HARD;
427 }
428 }
429
430 /* If we have an interrupt but the guest is not ready to receive an
431 * interrupt, request an interrupt window exit. This will
432 * cause a return to userspace as soon as the guest is ready to
433 * receive interrupts. */
434 if ((cpu->interrupt_request & CPU_INTERRUPT_HARD)) {
435 ht->request_interrupt_window = 1;
436 } else {
437 ht->request_interrupt_window = 0;
438 }
439 return 0;
440}
441
442void hax_raise_event(CPUState *cpu)
443{
444 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
445
446 if (!vcpu) {
447 return;
448 }
449 vcpu->tunnel->user_event_pending = 1;
450}
451
452/*
453 * Ask hax kernel module to run the CPU for us till:
454 * 1. Guest crash or shutdown
455 * 2. Need QEMU's emulation like guest execute MMIO instruction
456 * 3. Guest execute HLT
457 * 4. QEMU have Signal/event pending
458 * 5. An unknown VMX exit happens
459 */
460static int hax_vcpu_hax_exec(CPUArchState *env)
461{
462 int ret = 0;
29a0af61 463 CPUState *cpu = env_cpu(env);
47c1c8c1
VP
464 X86CPU *x86_cpu = X86_CPU(cpu);
465 struct hax_vcpu_state *vcpu = cpu->hax_vcpu;
466 struct hax_tunnel *ht = vcpu->tunnel;
467
468 if (!hax_enabled()) {
469 DPRINTF("Trying to vcpu execute at eip:" TARGET_FMT_lx "\n", env->eip);
470 return 0;
471 }
472
47c1c8c1
VP
473 if (cpu->interrupt_request & CPU_INTERRUPT_POLL) {
474 cpu->interrupt_request &= ~CPU_INTERRUPT_POLL;
475 apic_poll_irq(x86_cpu->apic_state);
476 }
477
6f38dca6
CX
478 /* After a vcpu is halted (either because it is an AP and has just been
479 * reset, or because it has executed the HLT instruction), it will not be
480 * run (hax_vcpu_run()) until it is unhalted. The next few if blocks check
481 * for events that may change the halted state of this vcpu:
482 * a) Maskable interrupt, when RFLAGS.IF is 1;
483 * Note: env->eflags may not reflect the current RFLAGS state, because
484 * it is not updated after each hax_vcpu_run(). We cannot afford
485 * to fail to recognize any unhalt-by-maskable-interrupt event
486 * (in which case the vcpu will halt forever), and yet we cannot
487 * afford the overhead of hax_vcpu_sync_state(). The current
488 * solution is to err on the side of caution and have the HLT
489 * handler (see case HAX_EXIT_HLT below) unconditionally set the
490 * IF_MASK bit in env->eflags, which, in effect, disables the
491 * RFLAGS.IF check.
492 * b) NMI;
493 * c) INIT signal;
494 * d) SIPI signal.
495 */
496 if (((cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
497 (env->eflags & IF_MASK)) ||
498 (cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
499 cpu->halted = 0;
500 }
501
47c1c8c1
VP
502 if (cpu->interrupt_request & CPU_INTERRUPT_INIT) {
503 DPRINTF("\nhax_vcpu_hax_exec: handling INIT for %d\n",
504 cpu->cpu_index);
505 do_cpu_init(x86_cpu);
506 hax_vcpu_sync_state(env, 1);
507 }
508
509 if (cpu->interrupt_request & CPU_INTERRUPT_SIPI) {
510 DPRINTF("hax_vcpu_hax_exec: handling SIPI for %d\n",
511 cpu->cpu_index);
512 hax_vcpu_sync_state(env, 0);
513 do_cpu_sipi(x86_cpu);
514 hax_vcpu_sync_state(env, 1);
515 }
516
6f38dca6
CX
517 if (cpu->halted) {
518 /* If this vcpu is halted, we must not ask HAXM to run it. Instead, we
519 * break out of hax_smp_cpu_exec() as if this vcpu had executed HLT.
520 * That way, this vcpu thread will be trapped in qemu_wait_io_event(),
521 * until the vcpu is unhalted.
522 */
523 cpu->exception_index = EXCP_HLT;
524 return 0;
525 }
526
47c1c8c1
VP
527 do {
528 int hax_ret;
529
530 if (cpu->exit_request) {
531 ret = 1;
532 break;
533 }
534
535 hax_vcpu_interrupt(env);
536
537 qemu_mutex_unlock_iothread();
457e0355 538 cpu_exec_start(cpu);
47c1c8c1 539 hax_ret = hax_vcpu_run(vcpu);
457e0355 540 cpu_exec_end(cpu);
47c1c8c1 541 qemu_mutex_lock_iothread();
47c1c8c1
VP
542
543 /* Simply continue the vcpu_run if system call interrupted */
544 if (hax_ret == -EINTR || hax_ret == -EAGAIN) {
545 DPRINTF("io window interrupted\n");
546 continue;
547 }
548
549 if (hax_ret < 0) {
550 fprintf(stderr, "vcpu run failed for vcpu %x\n", vcpu->vcpu_id);
551 abort();
552 }
553 switch (ht->_exit_status) {
554 case HAX_EXIT_IO:
555 ret = hax_handle_io(env, ht->pio._df, ht->pio._port,
556 ht->pio._direction,
557 ht->pio._size, ht->pio._count, vcpu->iobuf);
558 break;
559 case HAX_EXIT_FAST_MMIO:
560 ret = hax_handle_fastmmio(env, (struct hax_fastmmio *) vcpu->iobuf);
561 break;
562 /* Guest state changed, currently only for shutdown */
563 case HAX_EXIT_STATECHANGE:
564 fprintf(stdout, "VCPU shutdown request\n");
cf83f140 565 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
47c1c8c1
VP
566 hax_vcpu_sync_state(env, 0);
567 ret = 1;
568 break;
569 case HAX_EXIT_UNKNOWN_VMEXIT:
570 fprintf(stderr, "Unknown VMX exit %x from guest\n",
571 ht->_exit_reason);
cf83f140 572 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
47c1c8c1 573 hax_vcpu_sync_state(env, 0);
90c84c56 574 cpu_dump_state(cpu, stderr, 0);
47c1c8c1
VP
575 ret = -1;
576 break;
577 case HAX_EXIT_HLT:
578 if (!(cpu->interrupt_request & CPU_INTERRUPT_HARD) &&
579 !(cpu->interrupt_request & CPU_INTERRUPT_NMI)) {
580 /* hlt instruction with interrupt disabled is shutdown */
581 env->eflags |= IF_MASK;
582 cpu->halted = 1;
583 cpu->exception_index = EXCP_HLT;
584 ret = 1;
585 }
586 break;
587 /* these situations will continue to hax module */
588 case HAX_EXIT_INTERRUPT:
589 case HAX_EXIT_PAUSED:
590 break;
591 case HAX_EXIT_MMIO:
592 /* Should not happen on UG system */
593 fprintf(stderr, "HAX: unsupported MMIO emulation\n");
594 ret = -1;
595 break;
596 case HAX_EXIT_REAL:
597 /* Should not happen on UG system */
598 fprintf(stderr, "HAX: unimplemented real mode emulation\n");
599 ret = -1;
600 break;
601 default:
602 fprintf(stderr, "Unknown exit %x from HAX\n", ht->_exit_status);
cf83f140 603 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
47c1c8c1 604 hax_vcpu_sync_state(env, 0);
90c84c56 605 cpu_dump_state(cpu, stderr, 0);
47c1c8c1
VP
606 ret = 1;
607 break;
608 }
609 } while (!ret);
610
611 if (cpu->exit_request) {
612 cpu->exit_request = 0;
613 cpu->exception_index = EXCP_INTERRUPT;
614 }
615 return ret < 0;
616}
617
618static void do_hax_cpu_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
619{
620 CPUArchState *env = cpu->env_ptr;
621
622 hax_arch_get_registers(env);
99f31832 623 cpu->vcpu_dirty = true;
47c1c8c1
VP
624}
625
626void hax_cpu_synchronize_state(CPUState *cpu)
627{
99f31832 628 if (!cpu->vcpu_dirty) {
47c1c8c1
VP
629 run_on_cpu(cpu, do_hax_cpu_synchronize_state, RUN_ON_CPU_NULL);
630 }
631}
632
633static void do_hax_cpu_synchronize_post_reset(CPUState *cpu,
634 run_on_cpu_data arg)
635{
636 CPUArchState *env = cpu->env_ptr;
637
638 hax_vcpu_sync_state(env, 1);
99f31832 639 cpu->vcpu_dirty = false;
47c1c8c1
VP
640}
641
642void hax_cpu_synchronize_post_reset(CPUState *cpu)
643{
644 run_on_cpu(cpu, do_hax_cpu_synchronize_post_reset, RUN_ON_CPU_NULL);
645}
646
647static void do_hax_cpu_synchronize_post_init(CPUState *cpu, run_on_cpu_data arg)
648{
649 CPUArchState *env = cpu->env_ptr;
650
651 hax_vcpu_sync_state(env, 1);
99f31832 652 cpu->vcpu_dirty = false;
47c1c8c1
VP
653}
654
655void hax_cpu_synchronize_post_init(CPUState *cpu)
656{
657 run_on_cpu(cpu, do_hax_cpu_synchronize_post_init, RUN_ON_CPU_NULL);
658}
659
75e972da
DG
660static void do_hax_cpu_synchronize_pre_loadvm(CPUState *cpu, run_on_cpu_data arg)
661{
99f31832 662 cpu->vcpu_dirty = true;
75e972da
DG
663}
664
665void hax_cpu_synchronize_pre_loadvm(CPUState *cpu)
666{
667 run_on_cpu(cpu, do_hax_cpu_synchronize_pre_loadvm, RUN_ON_CPU_NULL);
668}
669
47c1c8c1
VP
670int hax_smp_cpu_exec(CPUState *cpu)
671{
672 CPUArchState *env = (CPUArchState *) (cpu->env_ptr);
673 int fatal;
674 int ret;
675
676 while (1) {
677 if (cpu->exception_index >= EXCP_INTERRUPT) {
678 ret = cpu->exception_index;
679 cpu->exception_index = -1;
680 break;
681 }
682
683 fatal = hax_vcpu_hax_exec(env);
684
685 if (fatal) {
686 fprintf(stderr, "Unsupported HAX vcpu return\n");
687 abort();
688 }
689 }
690
691 return ret;
692}
693
694static void set_v8086_seg(struct segment_desc_t *lhs, const SegmentCache *rhs)
695{
696 memset(lhs, 0, sizeof(struct segment_desc_t));
697 lhs->selector = rhs->selector;
698 lhs->base = rhs->base;
699 lhs->limit = rhs->limit;
700 lhs->type = 3;
701 lhs->present = 1;
702 lhs->dpl = 3;
703 lhs->operand_size = 0;
704 lhs->desc = 1;
705 lhs->long_mode = 0;
706 lhs->granularity = 0;
707 lhs->available = 0;
708}
709
710static void get_seg(SegmentCache *lhs, const struct segment_desc_t *rhs)
711{
712 lhs->selector = rhs->selector;
713 lhs->base = rhs->base;
714 lhs->limit = rhs->limit;
715 lhs->flags = (rhs->type << DESC_TYPE_SHIFT)
716 | (rhs->present * DESC_P_MASK)
717 | (rhs->dpl << DESC_DPL_SHIFT)
718 | (rhs->operand_size << DESC_B_SHIFT)
719 | (rhs->desc * DESC_S_MASK)
720 | (rhs->long_mode << DESC_L_SHIFT)
721 | (rhs->granularity * DESC_G_MASK) | (rhs->available * DESC_AVL_MASK);
722}
723
724static void set_seg(struct segment_desc_t *lhs, const SegmentCache *rhs)
725{
726 unsigned flags = rhs->flags;
727
728 memset(lhs, 0, sizeof(struct segment_desc_t));
729 lhs->selector = rhs->selector;
730 lhs->base = rhs->base;
731 lhs->limit = rhs->limit;
732 lhs->type = (flags >> DESC_TYPE_SHIFT) & 15;
733 lhs->present = (flags & DESC_P_MASK) != 0;
734 lhs->dpl = rhs->selector & 3;
735 lhs->operand_size = (flags >> DESC_B_SHIFT) & 1;
736 lhs->desc = (flags & DESC_S_MASK) != 0;
737 lhs->long_mode = (flags >> DESC_L_SHIFT) & 1;
738 lhs->granularity = (flags & DESC_G_MASK) != 0;
739 lhs->available = (flags & DESC_AVL_MASK) != 0;
740}
741
742static void hax_getput_reg(uint64_t *hax_reg, target_ulong *qemu_reg, int set)
743{
744 target_ulong reg = *hax_reg;
745
746 if (set) {
747 *hax_reg = *qemu_reg;
748 } else {
749 *qemu_reg = reg;
750 }
751}
752
753/* The sregs has been synced with HAX kernel already before this call */
754static int hax_get_segments(CPUArchState *env, struct vcpu_state_t *sregs)
755{
756 get_seg(&env->segs[R_CS], &sregs->_cs);
757 get_seg(&env->segs[R_DS], &sregs->_ds);
758 get_seg(&env->segs[R_ES], &sregs->_es);
759 get_seg(&env->segs[R_FS], &sregs->_fs);
760 get_seg(&env->segs[R_GS], &sregs->_gs);
761 get_seg(&env->segs[R_SS], &sregs->_ss);
762
763 get_seg(&env->tr, &sregs->_tr);
764 get_seg(&env->ldt, &sregs->_ldt);
765 env->idt.limit = sregs->_idt.limit;
766 env->idt.base = sregs->_idt.base;
767 env->gdt.limit = sregs->_gdt.limit;
768 env->gdt.base = sregs->_gdt.base;
769 return 0;
770}
771
772static int hax_set_segments(CPUArchState *env, struct vcpu_state_t *sregs)
773{
774 if ((env->eflags & VM_MASK)) {
775 set_v8086_seg(&sregs->_cs, &env->segs[R_CS]);
776 set_v8086_seg(&sregs->_ds, &env->segs[R_DS]);
777 set_v8086_seg(&sregs->_es, &env->segs[R_ES]);
778 set_v8086_seg(&sregs->_fs, &env->segs[R_FS]);
779 set_v8086_seg(&sregs->_gs, &env->segs[R_GS]);
780 set_v8086_seg(&sregs->_ss, &env->segs[R_SS]);
781 } else {
782 set_seg(&sregs->_cs, &env->segs[R_CS]);
783 set_seg(&sregs->_ds, &env->segs[R_DS]);
784 set_seg(&sregs->_es, &env->segs[R_ES]);
785 set_seg(&sregs->_fs, &env->segs[R_FS]);
786 set_seg(&sregs->_gs, &env->segs[R_GS]);
787 set_seg(&sregs->_ss, &env->segs[R_SS]);
788
789 if (env->cr[0] & CR0_PE_MASK) {
790 /* force ss cpl to cs cpl */
791 sregs->_ss.selector = (sregs->_ss.selector & ~3) |
792 (sregs->_cs.selector & 3);
793 sregs->_ss.dpl = sregs->_ss.selector & 3;
794 }
795 }
796
797 set_seg(&sregs->_tr, &env->tr);
798 set_seg(&sregs->_ldt, &env->ldt);
799 sregs->_idt.limit = env->idt.limit;
800 sregs->_idt.base = env->idt.base;
801 sregs->_gdt.limit = env->gdt.limit;
802 sregs->_gdt.base = env->gdt.base;
803 return 0;
804}
805
47c1c8c1
VP
806static int hax_sync_vcpu_register(CPUArchState *env, int set)
807{
808 struct vcpu_state_t regs;
809 int ret;
810 memset(&regs, 0, sizeof(struct vcpu_state_t));
811
812 if (!set) {
813 ret = hax_sync_vcpu_state(env, &regs, 0);
814 if (ret < 0) {
815 return -1;
816 }
817 }
818
819 /* generic register */
820 hax_getput_reg(&regs._rax, &env->regs[R_EAX], set);
821 hax_getput_reg(&regs._rbx, &env->regs[R_EBX], set);
822 hax_getput_reg(&regs._rcx, &env->regs[R_ECX], set);
823 hax_getput_reg(&regs._rdx, &env->regs[R_EDX], set);
824 hax_getput_reg(&regs._rsi, &env->regs[R_ESI], set);
825 hax_getput_reg(&regs._rdi, &env->regs[R_EDI], set);
826 hax_getput_reg(&regs._rsp, &env->regs[R_ESP], set);
827 hax_getput_reg(&regs._rbp, &env->regs[R_EBP], set);
828#ifdef TARGET_X86_64
829 hax_getput_reg(&regs._r8, &env->regs[8], set);
830 hax_getput_reg(&regs._r9, &env->regs[9], set);
831 hax_getput_reg(&regs._r10, &env->regs[10], set);
832 hax_getput_reg(&regs._r11, &env->regs[11], set);
833 hax_getput_reg(&regs._r12, &env->regs[12], set);
834 hax_getput_reg(&regs._r13, &env->regs[13], set);
835 hax_getput_reg(&regs._r14, &env->regs[14], set);
836 hax_getput_reg(&regs._r15, &env->regs[15], set);
837#endif
838 hax_getput_reg(&regs._rflags, &env->eflags, set);
839 hax_getput_reg(&regs._rip, &env->eip, set);
840
841 if (set) {
842 regs._cr0 = env->cr[0];
843 regs._cr2 = env->cr[2];
844 regs._cr3 = env->cr[3];
845 regs._cr4 = env->cr[4];
846 hax_set_segments(env, &regs);
847 } else {
848 env->cr[0] = regs._cr0;
849 env->cr[2] = regs._cr2;
850 env->cr[3] = regs._cr3;
851 env->cr[4] = regs._cr4;
852 hax_get_segments(env, &regs);
853 }
854
855 if (set) {
856 ret = hax_sync_vcpu_state(env, &regs, 1);
857 if (ret < 0) {
858 return -1;
859 }
860 }
47c1c8c1
VP
861 return 0;
862}
863
864static void hax_msr_entry_set(struct vmx_msr *item, uint32_t index,
865 uint64_t value)
866{
867 item->entry = index;
868 item->value = value;
869}
870
871static int hax_get_msrs(CPUArchState *env)
872{
873 struct hax_msr_data md;
874 struct vmx_msr *msrs = md.entries;
875 int ret, i, n;
876
877 n = 0;
878 msrs[n++].entry = MSR_IA32_SYSENTER_CS;
879 msrs[n++].entry = MSR_IA32_SYSENTER_ESP;
880 msrs[n++].entry = MSR_IA32_SYSENTER_EIP;
881 msrs[n++].entry = MSR_IA32_TSC;
882#ifdef TARGET_X86_64
883 msrs[n++].entry = MSR_EFER;
884 msrs[n++].entry = MSR_STAR;
885 msrs[n++].entry = MSR_LSTAR;
886 msrs[n++].entry = MSR_CSTAR;
887 msrs[n++].entry = MSR_FMASK;
888 msrs[n++].entry = MSR_KERNELGSBASE;
889#endif
890 md.nr_msr = n;
891 ret = hax_sync_msr(env, &md, 0);
892 if (ret < 0) {
893 return ret;
894 }
895
896 for (i = 0; i < md.done; i++) {
897 switch (msrs[i].entry) {
898 case MSR_IA32_SYSENTER_CS:
899 env->sysenter_cs = msrs[i].value;
900 break;
901 case MSR_IA32_SYSENTER_ESP:
902 env->sysenter_esp = msrs[i].value;
903 break;
904 case MSR_IA32_SYSENTER_EIP:
905 env->sysenter_eip = msrs[i].value;
906 break;
907 case MSR_IA32_TSC:
908 env->tsc = msrs[i].value;
909 break;
910#ifdef TARGET_X86_64
911 case MSR_EFER:
912 env->efer = msrs[i].value;
913 break;
914 case MSR_STAR:
915 env->star = msrs[i].value;
916 break;
917 case MSR_LSTAR:
918 env->lstar = msrs[i].value;
919 break;
920 case MSR_CSTAR:
921 env->cstar = msrs[i].value;
922 break;
923 case MSR_FMASK:
924 env->fmask = msrs[i].value;
925 break;
926 case MSR_KERNELGSBASE:
927 env->kernelgsbase = msrs[i].value;
928 break;
929#endif
930 }
931 }
932
933 return 0;
934}
935
936static int hax_set_msrs(CPUArchState *env)
937{
938 struct hax_msr_data md;
939 struct vmx_msr *msrs;
940 msrs = md.entries;
941 int n = 0;
942
943 memset(&md, 0, sizeof(struct hax_msr_data));
944 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_CS, env->sysenter_cs);
945 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_ESP, env->sysenter_esp);
946 hax_msr_entry_set(&msrs[n++], MSR_IA32_SYSENTER_EIP, env->sysenter_eip);
947 hax_msr_entry_set(&msrs[n++], MSR_IA32_TSC, env->tsc);
948#ifdef TARGET_X86_64
949 hax_msr_entry_set(&msrs[n++], MSR_EFER, env->efer);
950 hax_msr_entry_set(&msrs[n++], MSR_STAR, env->star);
951 hax_msr_entry_set(&msrs[n++], MSR_LSTAR, env->lstar);
952 hax_msr_entry_set(&msrs[n++], MSR_CSTAR, env->cstar);
953 hax_msr_entry_set(&msrs[n++], MSR_FMASK, env->fmask);
954 hax_msr_entry_set(&msrs[n++], MSR_KERNELGSBASE, env->kernelgsbase);
955#endif
956 md.nr_msr = n;
957 md.done = 0;
958
959 return hax_sync_msr(env, &md, 1);
960}
961
962static int hax_get_fpu(CPUArchState *env)
963{
964 struct fx_layout fpu;
965 int i, ret;
966
967 ret = hax_sync_fpu(env, &fpu, 0);
968 if (ret < 0) {
969 return ret;
970 }
971
972 env->fpstt = (fpu.fsw >> 11) & 7;
973 env->fpus = fpu.fsw;
974 env->fpuc = fpu.fcw;
975 for (i = 0; i < 8; ++i) {
976 env->fptags[i] = !((fpu.ftw >> i) & 1);
977 }
978 memcpy(env->fpregs, fpu.st_mm, sizeof(env->fpregs));
979
980 for (i = 0; i < 8; i++) {
981 env->xmm_regs[i].ZMM_Q(0) = ldq_p(&fpu.mmx_1[i][0]);
982 env->xmm_regs[i].ZMM_Q(1) = ldq_p(&fpu.mmx_1[i][8]);
983 if (CPU_NB_REGS > 8) {
984 env->xmm_regs[i + 8].ZMM_Q(0) = ldq_p(&fpu.mmx_2[i][0]);
985 env->xmm_regs[i + 8].ZMM_Q(1) = ldq_p(&fpu.mmx_2[i][8]);
986 }
987 }
988 env->mxcsr = fpu.mxcsr;
989
990 return 0;
991}
992
993static int hax_set_fpu(CPUArchState *env)
994{
995 struct fx_layout fpu;
996 int i;
997
998 memset(&fpu, 0, sizeof(fpu));
999 fpu.fsw = env->fpus & ~(7 << 11);
1000 fpu.fsw |= (env->fpstt & 7) << 11;
1001 fpu.fcw = env->fpuc;
1002
1003 for (i = 0; i < 8; ++i) {
1004 fpu.ftw |= (!env->fptags[i]) << i;
1005 }
1006
1007 memcpy(fpu.st_mm, env->fpregs, sizeof(env->fpregs));
1008 for (i = 0; i < 8; i++) {
1009 stq_p(&fpu.mmx_1[i][0], env->xmm_regs[i].ZMM_Q(0));
1010 stq_p(&fpu.mmx_1[i][8], env->xmm_regs[i].ZMM_Q(1));
1011 if (CPU_NB_REGS > 8) {
1012 stq_p(&fpu.mmx_2[i][0], env->xmm_regs[i + 8].ZMM_Q(0));
1013 stq_p(&fpu.mmx_2[i][8], env->xmm_regs[i + 8].ZMM_Q(1));
1014 }
1015 }
1016
1017 fpu.mxcsr = env->mxcsr;
1018
1019 return hax_sync_fpu(env, &fpu, 1);
1020}
1021
1022static int hax_arch_get_registers(CPUArchState *env)
1023{
1024 int ret;
1025
1026 ret = hax_sync_vcpu_register(env, 0);
1027 if (ret < 0) {
1028 return ret;
1029 }
1030
1031 ret = hax_get_fpu(env);
1032 if (ret < 0) {
1033 return ret;
1034 }
1035
1036 ret = hax_get_msrs(env);
1037 if (ret < 0) {
1038 return ret;
1039 }
1040
df16af87 1041 x86_update_hflags(env);
47c1c8c1
VP
1042 return 0;
1043}
1044
1045static int hax_arch_set_registers(CPUArchState *env)
1046{
1047 int ret;
1048 ret = hax_sync_vcpu_register(env, 1);
1049
1050 if (ret < 0) {
1051 fprintf(stderr, "Failed to sync vcpu reg\n");
1052 return ret;
1053 }
1054 ret = hax_set_fpu(env);
1055 if (ret < 0) {
1056 fprintf(stderr, "FPU failed\n");
1057 return ret;
1058 }
1059 ret = hax_set_msrs(env);
1060 if (ret < 0) {
1061 fprintf(stderr, "MSR failed\n");
1062 return ret;
1063 }
1064
1065 return 0;
1066}
1067
1068static void hax_vcpu_sync_state(CPUArchState *env, int modified)
1069{
1070 if (hax_enabled()) {
1071 if (modified) {
1072 hax_arch_set_registers(env);
1073 } else {
1074 hax_arch_get_registers(env);
1075 }
1076 }
1077}
1078
1079/*
1080 * much simpler than kvm, at least in first stage because:
1081 * We don't need consider the device pass-through, we don't need
1082 * consider the framebuffer, and we may even remove the bios at all
1083 */
1084int hax_sync_vcpus(void)
1085{
1086 if (hax_enabled()) {
1087 CPUState *cpu;
1088
1089 cpu = first_cpu;
1090 if (!cpu) {
1091 return 0;
1092 }
1093
1094 for (; cpu != NULL; cpu = CPU_NEXT(cpu)) {
1095 int ret;
1096
1097 ret = hax_arch_set_registers(cpu->env_ptr);
1098 if (ret < 0) {
1099 return ret;
1100 }
1101 }
1102 }
1103
1104 return 0;
1105}
1106
1107void hax_reset_vcpu_state(void *opaque)
1108{
1109 CPUState *cpu;
1110 for (cpu = first_cpu; cpu != NULL; cpu = CPU_NEXT(cpu)) {
1111 cpu->hax_vcpu->tunnel->user_event_pending = 0;
1112 cpu->hax_vcpu->tunnel->ready_for_interrupt_injection = 0;
1113 }
1114}
1115
1116static void hax_accel_class_init(ObjectClass *oc, void *data)
1117{
1118 AccelClass *ac = ACCEL_CLASS(oc);
1119 ac->name = "HAX";
1120 ac->init_machine = hax_accel_init;
1121 ac->allowed = &hax_allowed;
1122}
1123
1124static const TypeInfo hax_accel_type = {
1125 .name = ACCEL_CLASS_NAME("hax"),
1126 .parent = TYPE_ACCEL,
1127 .class_init = hax_accel_class_init,
1128};
1129
1130static void hax_type_init(void)
1131{
1132 type_register_static(&hax_accel_type);
1133}
1134
1135type_init(hax_type_init);