2 * Copyright (C) 2016 Veertu Inc,
3 * Copyright (C) 2017 Google Inc,
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 /////////////////////////////////////////////////////////////////////////
21 // Copyright (C) 2001-2012 The Bochs Project
23 // This library is free software; you can redistribute it and/or
24 // modify it under the terms of the GNU Lesser General Public
25 // License as published by the Free Software Foundation; either
26 // version 2 of the License, or (at your option) any later version.
28 // This library is distributed in the hope that it will be useful,
29 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 // Lesser General Public License for more details.
33 // You should have received a copy of the GNU Lesser General Public
34 // License along with this library; if not, write to the Free Software
35 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA B 02110-1301 USA
36 /////////////////////////////////////////////////////////////////////////
38 #include "qemu/osdep.h"
40 #include "qemu-common.h"
41 #include "x86_decode.h"
45 #include "x86_flags.h"
49 void hvf_handle_io(struct CPUState *cpu, uint16_t port, void *data,
50 int direction, int size, uint32_t count);
52 #define EXEC_2OP_FLAGS_CMD(env, decode, cmd, FLAGS_FUNC, save_res) \
54 fetch_operands(env, decode, 2, true, true, false); \
55 switch (decode->operand_size) { \
58 uint8_t v1 = (uint8_t)decode->op[0].val; \
59 uint8_t v2 = (uint8_t)decode->op[1].val; \
60 uint8_t diff = v1 cmd v2; \
62 write_val_ext(env, decode->op[0].ptr, diff, 1); \
64 FLAGS_FUNC##8(env, v1, v2, diff); \
69 uint16_t v1 = (uint16_t)decode->op[0].val; \
70 uint16_t v2 = (uint16_t)decode->op[1].val; \
71 uint16_t diff = v1 cmd v2; \
73 write_val_ext(env, decode->op[0].ptr, diff, 2); \
75 FLAGS_FUNC##16(env, v1, v2, diff); \
80 uint32_t v1 = (uint32_t)decode->op[0].val; \
81 uint32_t v2 = (uint32_t)decode->op[1].val; \
82 uint32_t diff = v1 cmd v2; \
84 write_val_ext(env, decode->op[0].ptr, diff, 4); \
86 FLAGS_FUNC##32(env, v1, v2, diff); \
90 VM_PANIC("bad size\n"); \
94 target_ulong read_reg(CPUX86State *env, int reg, int size)
98 return env->hvf_emul->regs[reg].lx;
100 return env->hvf_emul->regs[reg].rx;
102 return env->hvf_emul->regs[reg].erx;
104 return env->hvf_emul->regs[reg].rrx;
111 void write_reg(CPUX86State *env, int reg, target_ulong val, int size)
115 env->hvf_emul->regs[reg].lx = val;
118 env->hvf_emul->regs[reg].rx = val;
121 env->hvf_emul->regs[reg].rrx = (uint32_t)val;
124 env->hvf_emul->regs[reg].rrx = val;
131 target_ulong read_val_from_reg(target_ulong reg_ptr, int size)
137 val = *(uint8_t *)reg_ptr;
140 val = *(uint16_t *)reg_ptr;
143 val = *(uint32_t *)reg_ptr;
146 val = *(uint64_t *)reg_ptr;
154 void write_val_to_reg(target_ulong reg_ptr, target_ulong val, int size)
158 *(uint8_t *)reg_ptr = val;
161 *(uint16_t *)reg_ptr = val;
164 *(uint64_t *)reg_ptr = (uint32_t)val;
167 *(uint64_t *)reg_ptr = val;
174 static bool is_host_reg(struct CPUX86State *env, target_ulong ptr)
176 return (ptr - (target_ulong)&env->hvf_emul->regs[0]) < sizeof(env->hvf_emul->regs);
179 void write_val_ext(struct CPUX86State *env, target_ulong ptr, target_ulong val, int size)
181 if (is_host_reg(env, ptr)) {
182 write_val_to_reg(ptr, val, size);
185 vmx_write_mem(ENV_GET_CPU(env), ptr, &val, size);
188 uint8_t *read_mmio(struct CPUX86State *env, target_ulong ptr, int bytes)
190 vmx_read_mem(ENV_GET_CPU(env), env->hvf_emul->mmio_buf, ptr, bytes);
191 return env->hvf_emul->mmio_buf;
195 target_ulong read_val_ext(struct CPUX86State *env, target_ulong ptr, int size)
200 if (is_host_reg(env, ptr)) {
201 return read_val_from_reg(ptr, size);
204 mmio_ptr = read_mmio(env, ptr, size);
207 val = *(uint8_t *)mmio_ptr;
210 val = *(uint16_t *)mmio_ptr;
213 val = *(uint32_t *)mmio_ptr;
216 val = *(uint64_t *)mmio_ptr;
219 VM_PANIC("bad size\n");
225 static void fetch_operands(struct CPUX86State *env, struct x86_decode *decode,
226 int n, bool val_op0, bool val_op1, bool val_op2)
229 bool calc_val[3] = {val_op0, val_op1, val_op2};
231 for (i = 0; i < n; i++) {
232 switch (decode->op[i].type) {
233 case X86_VAR_IMMEDIATE:
236 VM_PANIC_ON(!decode->op[i].ptr);
238 decode->op[i].val = read_val_from_reg(decode->op[i].ptr,
239 decode->operand_size);
243 calc_modrm_operand(env, decode, &decode->op[i]);
245 decode->op[i].val = read_val_ext(env, decode->op[i].ptr,
246 decode->operand_size);
250 decode->op[i].ptr = decode_linear_addr(env, decode,
254 decode->op[i].val = read_val_ext(env, decode->op[i].ptr,
255 decode->operand_size);
264 static void exec_mov(struct CPUX86State *env, struct x86_decode *decode)
266 fetch_operands(env, decode, 2, false, true, false);
267 write_val_ext(env, decode->op[0].ptr, decode->op[1].val,
268 decode->operand_size);
270 RIP(env) += decode->len;
273 static void exec_add(struct CPUX86State *env, struct x86_decode *decode)
275 EXEC_2OP_FLAGS_CMD(env, decode, +, SET_FLAGS_OSZAPC_ADD, true);
276 RIP(env) += decode->len;
279 static void exec_or(struct CPUX86State *env, struct x86_decode *decode)
281 EXEC_2OP_FLAGS_CMD(env, decode, |, SET_FLAGS_OSZAPC_LOGIC, true);
282 RIP(env) += decode->len;
285 static void exec_adc(struct CPUX86State *env, struct x86_decode *decode)
287 EXEC_2OP_FLAGS_CMD(env, decode, +get_CF(env)+, SET_FLAGS_OSZAPC_ADD, true);
288 RIP(env) += decode->len;
291 static void exec_sbb(struct CPUX86State *env, struct x86_decode *decode)
293 EXEC_2OP_FLAGS_CMD(env, decode, -get_CF(env)-, SET_FLAGS_OSZAPC_SUB, true);
294 RIP(env) += decode->len;
297 static void exec_and(struct CPUX86State *env, struct x86_decode *decode)
299 EXEC_2OP_FLAGS_CMD(env, decode, &, SET_FLAGS_OSZAPC_LOGIC, true);
300 RIP(env) += decode->len;
303 static void exec_sub(struct CPUX86State *env, struct x86_decode *decode)
305 EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, true);
306 RIP(env) += decode->len;
309 static void exec_xor(struct CPUX86State *env, struct x86_decode *decode)
311 EXEC_2OP_FLAGS_CMD(env, decode, ^, SET_FLAGS_OSZAPC_LOGIC, true);
312 RIP(env) += decode->len;
315 static void exec_neg(struct CPUX86State *env, struct x86_decode *decode)
317 /*EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, false);*/
319 fetch_operands(env, decode, 2, true, true, false);
321 val = 0 - sign(decode->op[1].val, decode->operand_size);
322 write_val_ext(env, decode->op[1].ptr, val, decode->operand_size);
324 if (4 == decode->operand_size) {
325 SET_FLAGS_OSZAPC_SUB32(env, 0, 0 - val, val);
326 } else if (2 == decode->operand_size) {
327 SET_FLAGS_OSZAPC_SUB16(env, 0, 0 - val, val);
328 } else if (1 == decode->operand_size) {
329 SET_FLAGS_OSZAPC_SUB8(env, 0, 0 - val, val);
331 VM_PANIC("bad op size\n");
334 /*lflags_to_rflags(env);*/
335 RIP(env) += decode->len;
338 static void exec_cmp(struct CPUX86State *env, struct x86_decode *decode)
340 EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, false);
341 RIP(env) += decode->len;
344 static void exec_inc(struct CPUX86State *env, struct x86_decode *decode)
346 decode->op[1].type = X86_VAR_IMMEDIATE;
347 decode->op[1].val = 0;
349 EXEC_2OP_FLAGS_CMD(env, decode, +1+, SET_FLAGS_OSZAP_ADD, true);
351 RIP(env) += decode->len;
354 static void exec_dec(struct CPUX86State *env, struct x86_decode *decode)
356 decode->op[1].type = X86_VAR_IMMEDIATE;
357 decode->op[1].val = 0;
359 EXEC_2OP_FLAGS_CMD(env, decode, -1-, SET_FLAGS_OSZAP_SUB, true);
360 RIP(env) += decode->len;
363 static void exec_tst(struct CPUX86State *env, struct x86_decode *decode)
365 EXEC_2OP_FLAGS_CMD(env, decode, &, SET_FLAGS_OSZAPC_LOGIC, false);
366 RIP(env) += decode->len;
369 static void exec_not(struct CPUX86State *env, struct x86_decode *decode)
371 fetch_operands(env, decode, 1, true, false, false);
373 write_val_ext(env, decode->op[0].ptr, ~decode->op[0].val,
374 decode->operand_size);
375 RIP(env) += decode->len;
378 void exec_movzx(struct CPUX86State *env, struct x86_decode *decode)
381 int op_size = decode->operand_size;
383 fetch_operands(env, decode, 1, false, false, false);
385 if (0xb6 == decode->opcode[1]) {
390 decode->operand_size = src_op_size;
391 calc_modrm_operand(env, decode, &decode->op[1]);
392 decode->op[1].val = read_val_ext(env, decode->op[1].ptr, src_op_size);
393 write_val_ext(env, decode->op[0].ptr, decode->op[1].val, op_size);
395 RIP(env) += decode->len;
398 static void exec_out(struct CPUX86State *env, struct x86_decode *decode)
400 switch (decode->opcode[0]) {
402 hvf_handle_io(ENV_GET_CPU(env), decode->op[0].val, &AL(env), 1, 1, 1);
405 hvf_handle_io(ENV_GET_CPU(env), decode->op[0].val, &RAX(env), 1,
406 decode->operand_size, 1);
409 hvf_handle_io(ENV_GET_CPU(env), DX(env), &AL(env), 1, 1, 1);
412 hvf_handle_io(ENV_GET_CPU(env), DX(env), &RAX(env), 1, decode->operand_size, 1);
415 VM_PANIC("Bad out opcode\n");
418 RIP(env) += decode->len;
421 static void exec_in(struct CPUX86State *env, struct x86_decode *decode)
423 target_ulong val = 0;
424 switch (decode->opcode[0]) {
426 hvf_handle_io(ENV_GET_CPU(env), decode->op[0].val, &AL(env), 0, 1, 1);
429 hvf_handle_io(ENV_GET_CPU(env), decode->op[0].val, &val, 0, decode->operand_size, 1);
430 if (decode->operand_size == 2) {
433 RAX(env) = (uint32_t)val;
437 hvf_handle_io(ENV_GET_CPU(env), DX(env), &AL(env), 0, 1, 1);
440 hvf_handle_io(ENV_GET_CPU(env), DX(env), &val, 0, decode->operand_size, 1);
441 if (decode->operand_size == 2) {
444 RAX(env) = (uint32_t)val;
449 VM_PANIC("Bad in opcode\n");
453 RIP(env) += decode->len;
456 static inline void string_increment_reg(struct CPUX86State *env, int reg,
457 struct x86_decode *decode)
459 target_ulong val = read_reg(env, reg, decode->addressing_size);
460 if (env->hvf_emul->rflags.df) {
461 val -= decode->operand_size;
463 val += decode->operand_size;
465 write_reg(env, reg, val, decode->addressing_size);
468 static inline void string_rep(struct CPUX86State *env, struct x86_decode *decode,
469 void (*func)(struct CPUX86State *env,
470 struct x86_decode *ins), int rep)
472 target_ulong rcx = read_reg(env, R_ECX, decode->addressing_size);
475 write_reg(env, R_ECX, rcx, decode->addressing_size);
476 if ((PREFIX_REP == rep) && !get_ZF(env)) {
479 if ((PREFIX_REPN == rep) && get_ZF(env)) {
485 static void exec_ins_single(struct CPUX86State *env, struct x86_decode *decode)
487 target_ulong addr = linear_addr_size(ENV_GET_CPU(env), RDI(env), decode->addressing_size,
490 hvf_handle_io(ENV_GET_CPU(env), DX(env), env->hvf_emul->mmio_buf, 0,
491 decode->operand_size, 1);
492 vmx_write_mem(ENV_GET_CPU(env), addr, env->hvf_emul->mmio_buf, decode->operand_size);
494 string_increment_reg(env, R_EDI, decode);
497 static void exec_ins(struct CPUX86State *env, struct x86_decode *decode)
500 string_rep(env, decode, exec_ins_single, 0);
502 exec_ins_single(env, decode);
505 RIP(env) += decode->len;
508 static void exec_outs_single(struct CPUX86State *env, struct x86_decode *decode)
510 target_ulong addr = decode_linear_addr(env, decode, RSI(env), R_DS);
512 vmx_read_mem(ENV_GET_CPU(env), env->hvf_emul->mmio_buf, addr, decode->operand_size);
513 hvf_handle_io(ENV_GET_CPU(env), DX(env), env->hvf_emul->mmio_buf, 1,
514 decode->operand_size, 1);
516 string_increment_reg(env, R_ESI, decode);
519 static void exec_outs(struct CPUX86State *env, struct x86_decode *decode)
522 string_rep(env, decode, exec_outs_single, 0);
524 exec_outs_single(env, decode);
527 RIP(env) += decode->len;
530 static void exec_movs_single(struct CPUX86State *env, struct x86_decode *decode)
532 target_ulong src_addr;
533 target_ulong dst_addr;
536 src_addr = decode_linear_addr(env, decode, RSI(env), R_DS);
537 dst_addr = linear_addr_size(ENV_GET_CPU(env), RDI(env), decode->addressing_size,
540 val = read_val_ext(env, src_addr, decode->operand_size);
541 write_val_ext(env, dst_addr, val, decode->operand_size);
543 string_increment_reg(env, R_ESI, decode);
544 string_increment_reg(env, R_EDI, decode);
547 static void exec_movs(struct CPUX86State *env, struct x86_decode *decode)
550 string_rep(env, decode, exec_movs_single, 0);
552 exec_movs_single(env, decode);
555 RIP(env) += decode->len;
558 static void exec_cmps_single(struct CPUX86State *env, struct x86_decode *decode)
560 target_ulong src_addr;
561 target_ulong dst_addr;
563 src_addr = decode_linear_addr(env, decode, RSI(env), R_DS);
564 dst_addr = linear_addr_size(ENV_GET_CPU(env), RDI(env), decode->addressing_size,
567 decode->op[0].type = X86_VAR_IMMEDIATE;
568 decode->op[0].val = read_val_ext(env, src_addr, decode->operand_size);
569 decode->op[1].type = X86_VAR_IMMEDIATE;
570 decode->op[1].val = read_val_ext(env, dst_addr, decode->operand_size);
572 EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, false);
574 string_increment_reg(env, R_ESI, decode);
575 string_increment_reg(env, R_EDI, decode);
578 static void exec_cmps(struct CPUX86State *env, struct x86_decode *decode)
581 string_rep(env, decode, exec_cmps_single, decode->rep);
583 exec_cmps_single(env, decode);
585 RIP(env) += decode->len;
589 static void exec_stos_single(struct CPUX86State *env, struct x86_decode *decode)
594 addr = linear_addr_size(ENV_GET_CPU(env), RDI(env), decode->addressing_size, R_ES);
595 val = read_reg(env, R_EAX, decode->operand_size);
596 vmx_write_mem(ENV_GET_CPU(env), addr, &val, decode->operand_size);
598 string_increment_reg(env, R_EDI, decode);
602 static void exec_stos(struct CPUX86State *env, struct x86_decode *decode)
605 string_rep(env, decode, exec_stos_single, 0);
607 exec_stos_single(env, decode);
610 RIP(env) += decode->len;
613 static void exec_scas_single(struct CPUX86State *env, struct x86_decode *decode)
617 addr = linear_addr_size(ENV_GET_CPU(env), RDI(env), decode->addressing_size, R_ES);
618 decode->op[1].type = X86_VAR_IMMEDIATE;
619 vmx_read_mem(ENV_GET_CPU(env), &decode->op[1].val, addr, decode->operand_size);
621 EXEC_2OP_FLAGS_CMD(env, decode, -, SET_FLAGS_OSZAPC_SUB, false);
622 string_increment_reg(env, R_EDI, decode);
625 static void exec_scas(struct CPUX86State *env, struct x86_decode *decode)
627 decode->op[0].type = X86_VAR_REG;
628 decode->op[0].reg = R_EAX;
630 string_rep(env, decode, exec_scas_single, decode->rep);
632 exec_scas_single(env, decode);
635 RIP(env) += decode->len;
638 static void exec_lods_single(struct CPUX86State *env, struct x86_decode *decode)
641 target_ulong val = 0;
643 addr = decode_linear_addr(env, decode, RSI(env), R_DS);
644 vmx_read_mem(ENV_GET_CPU(env), &val, addr, decode->operand_size);
645 write_reg(env, R_EAX, val, decode->operand_size);
647 string_increment_reg(env, R_ESI, decode);
650 static void exec_lods(struct CPUX86State *env, struct x86_decode *decode)
653 string_rep(env, decode, exec_lods_single, 0);
655 exec_lods_single(env, decode);
658 RIP(env) += decode->len;
661 #define MSR_IA32_UCODE_REV 0x00000017
663 void simulate_rdmsr(struct CPUState *cpu)
665 X86CPU *x86_cpu = X86_CPU(cpu);
666 CPUX86State *env = &x86_cpu->env;
667 uint32_t msr = ECX(env);
672 val = rdtscp() + rvmcs(cpu->hvf_fd, VMCS_TSC_OFFSET);
674 case MSR_IA32_APICBASE:
675 val = cpu_get_apic_base(X86_CPU(cpu)->apic_state);
677 case MSR_IA32_UCODE_REV:
678 val = (0x100000000ULL << 32) | 0x100000000ULL;
681 val = rvmcs(cpu->hvf_fd, VMCS_GUEST_IA32_EFER);
684 val = rvmcs(cpu->hvf_fd, VMCS_GUEST_FS_BASE);
687 val = rvmcs(cpu->hvf_fd, VMCS_GUEST_GS_BASE);
689 case MSR_KERNELGSBASE:
690 val = rvmcs(cpu->hvf_fd, VMCS_HOST_FS_BASE);
701 case MSR_IA32_MISC_ENABLE:
702 val = env->msr_ia32_misc_enable;
704 case MSR_MTRRphysBase(0):
705 case MSR_MTRRphysBase(1):
706 case MSR_MTRRphysBase(2):
707 case MSR_MTRRphysBase(3):
708 case MSR_MTRRphysBase(4):
709 case MSR_MTRRphysBase(5):
710 case MSR_MTRRphysBase(6):
711 case MSR_MTRRphysBase(7):
712 val = env->mtrr_var[(ECX(env) - MSR_MTRRphysBase(0)) / 2].base;
714 case MSR_MTRRphysMask(0):
715 case MSR_MTRRphysMask(1):
716 case MSR_MTRRphysMask(2):
717 case MSR_MTRRphysMask(3):
718 case MSR_MTRRphysMask(4):
719 case MSR_MTRRphysMask(5):
720 case MSR_MTRRphysMask(6):
721 case MSR_MTRRphysMask(7):
722 val = env->mtrr_var[(ECX(env) - MSR_MTRRphysMask(0)) / 2].mask;
724 case MSR_MTRRfix64K_00000:
725 val = env->mtrr_fixed[0];
727 case MSR_MTRRfix16K_80000:
728 case MSR_MTRRfix16K_A0000:
729 val = env->mtrr_fixed[ECX(env) - MSR_MTRRfix16K_80000 + 1];
731 case MSR_MTRRfix4K_C0000:
732 case MSR_MTRRfix4K_C8000:
733 case MSR_MTRRfix4K_D0000:
734 case MSR_MTRRfix4K_D8000:
735 case MSR_MTRRfix4K_E0000:
736 case MSR_MTRRfix4K_E8000:
737 case MSR_MTRRfix4K_F0000:
738 case MSR_MTRRfix4K_F8000:
739 val = env->mtrr_fixed[ECX(env) - MSR_MTRRfix4K_C0000 + 3];
741 case MSR_MTRRdefType:
742 val = env->mtrr_deftype;
745 /* fprintf(stderr, "%s: unknown msr 0x%x\n", __func__, msr); */
750 RAX(env) = (uint32_t)val;
751 RDX(env) = (uint32_t)(val >> 32);
754 static void exec_rdmsr(struct CPUX86State *env, struct x86_decode *decode)
756 simulate_rdmsr(ENV_GET_CPU(env));
757 RIP(env) += decode->len;
760 void simulate_wrmsr(struct CPUState *cpu)
762 X86CPU *x86_cpu = X86_CPU(cpu);
763 CPUX86State *env = &x86_cpu->env;
764 uint32_t msr = ECX(env);
765 uint64_t data = ((uint64_t)EDX(env) << 32) | EAX(env);
769 /* if (!osx_is_sierra())
770 wvmcs(cpu->hvf_fd, VMCS_TSC_OFFSET, data - rdtscp());
771 hv_vm_sync_tsc(data);*/
773 case MSR_IA32_APICBASE:
774 cpu_set_apic_base(X86_CPU(cpu)->apic_state, data);
777 wvmcs(cpu->hvf_fd, VMCS_GUEST_FS_BASE, data);
780 wvmcs(cpu->hvf_fd, VMCS_GUEST_GS_BASE, data);
782 case MSR_KERNELGSBASE:
783 wvmcs(cpu->hvf_fd, VMCS_HOST_FS_BASE, data);
795 /*printf("new efer %llx\n", EFER(cpu));*/
796 wvmcs(cpu->hvf_fd, VMCS_GUEST_IA32_EFER, data);
797 if (data & MSR_EFER_NXE) {
798 hv_vcpu_invalidate_tlb(cpu->hvf_fd);
801 case MSR_MTRRphysBase(0):
802 case MSR_MTRRphysBase(1):
803 case MSR_MTRRphysBase(2):
804 case MSR_MTRRphysBase(3):
805 case MSR_MTRRphysBase(4):
806 case MSR_MTRRphysBase(5):
807 case MSR_MTRRphysBase(6):
808 case MSR_MTRRphysBase(7):
809 env->mtrr_var[(ECX(env) - MSR_MTRRphysBase(0)) / 2].base = data;
811 case MSR_MTRRphysMask(0):
812 case MSR_MTRRphysMask(1):
813 case MSR_MTRRphysMask(2):
814 case MSR_MTRRphysMask(3):
815 case MSR_MTRRphysMask(4):
816 case MSR_MTRRphysMask(5):
817 case MSR_MTRRphysMask(6):
818 case MSR_MTRRphysMask(7):
819 env->mtrr_var[(ECX(env) - MSR_MTRRphysMask(0)) / 2].mask = data;
821 case MSR_MTRRfix64K_00000:
822 env->mtrr_fixed[ECX(env) - MSR_MTRRfix64K_00000] = data;
824 case MSR_MTRRfix16K_80000:
825 case MSR_MTRRfix16K_A0000:
826 env->mtrr_fixed[ECX(env) - MSR_MTRRfix16K_80000 + 1] = data;
828 case MSR_MTRRfix4K_C0000:
829 case MSR_MTRRfix4K_C8000:
830 case MSR_MTRRfix4K_D0000:
831 case MSR_MTRRfix4K_D8000:
832 case MSR_MTRRfix4K_E0000:
833 case MSR_MTRRfix4K_E8000:
834 case MSR_MTRRfix4K_F0000:
835 case MSR_MTRRfix4K_F8000:
836 env->mtrr_fixed[ECX(env) - MSR_MTRRfix4K_C0000 + 3] = data;
838 case MSR_MTRRdefType:
839 env->mtrr_deftype = data;
845 /* Related to support known hypervisor interface */
846 /* if (g_hypervisor_iface)
847 g_hypervisor_iface->wrmsr_handler(cpu, msr, data);
849 printf("write msr %llx\n", RCX(cpu));*/
852 static void exec_wrmsr(struct CPUX86State *env, struct x86_decode *decode)
854 simulate_wrmsr(ENV_GET_CPU(env));
855 RIP(env) += decode->len;
860 * 0 - bt, 1 - btc, 2 - bts, 3 - btr
862 static void do_bt(struct CPUX86State *env, struct x86_decode *decode, int flag)
864 int32_t displacement;
867 int mask = (4 == decode->operand_size) ? 0x1f : 0xf;
869 VM_PANIC_ON(decode->rex.rex);
871 fetch_operands(env, decode, 2, false, true, false);
872 index = decode->op[1].val & mask;
874 if (decode->op[0].type != X86_VAR_REG) {
875 if (4 == decode->operand_size) {
876 displacement = ((int32_t) (decode->op[1].val & 0xffffffe0)) / 32;
877 decode->op[0].ptr += 4 * displacement;
878 } else if (2 == decode->operand_size) {
879 displacement = ((int16_t) (decode->op[1].val & 0xfff0)) / 16;
880 decode->op[0].ptr += 2 * displacement;
882 VM_PANIC("bt 64bit\n");
885 decode->op[0].val = read_val_ext(env, decode->op[0].ptr,
886 decode->operand_size);
887 cf = (decode->op[0].val >> index) & 0x01;
894 decode->op[0].val ^= (1u << index);
897 decode->op[0].val |= (1u << index);
900 decode->op[0].val &= ~(1u << index);
903 write_val_ext(env, decode->op[0].ptr, decode->op[0].val,
904 decode->operand_size);
908 static void exec_bt(struct CPUX86State *env, struct x86_decode *decode)
910 do_bt(env, decode, 0);
911 RIP(env) += decode->len;
914 static void exec_btc(struct CPUX86State *env, struct x86_decode *decode)
916 do_bt(env, decode, 1);
917 RIP(env) += decode->len;
920 static void exec_btr(struct CPUX86State *env, struct x86_decode *decode)
922 do_bt(env, decode, 3);
923 RIP(env) += decode->len;
926 static void exec_bts(struct CPUX86State *env, struct x86_decode *decode)
928 do_bt(env, decode, 2);
929 RIP(env) += decode->len;
932 void exec_shl(struct CPUX86State *env, struct x86_decode *decode)
937 fetch_operands(env, decode, 2, true, true, false);
939 count = decode->op[1].val;
940 count &= 0x1f; /* count is masked to 5 bits*/
945 switch (decode->operand_size) {
950 res = (decode->op[0].val << count);
951 cf = (decode->op[0].val >> (8 - count)) & 0x1;
952 of = cf ^ (res >> 7);
955 write_val_ext(env, decode->op[0].ptr, res, 1);
956 SET_FLAGS_OSZAPC_LOGIC8(env, 0, 0, res);
957 SET_FLAGS_OxxxxC(env, of, cf);
966 res = (decode->op[0].val << count);
967 cf = (decode->op[0].val >> (16 - count)) & 0x1;
968 of = cf ^ (res >> 15); /* of = cf ^ result15 */
971 write_val_ext(env, decode->op[0].ptr, res, 2);
972 SET_FLAGS_OSZAPC_LOGIC16(env, 0, 0, res);
973 SET_FLAGS_OxxxxC(env, of, cf);
978 uint32_t res = decode->op[0].val << count;
980 write_val_ext(env, decode->op[0].ptr, res, 4);
981 SET_FLAGS_OSZAPC_LOGIC32(env, 0, 0, res);
982 cf = (decode->op[0].val >> (32 - count)) & 0x1;
983 of = cf ^ (res >> 31); /* of = cf ^ result31 */
984 SET_FLAGS_OxxxxC(env, of, cf);
992 /* lflags_to_rflags(env); */
993 RIP(env) += decode->len;
996 void exec_movsx(CPUX86State *env, struct x86_decode *decode)
999 int op_size = decode->operand_size;
1001 fetch_operands(env, decode, 2, false, false, false);
1003 if (0xbe == decode->opcode[1]) {
1009 decode->operand_size = src_op_size;
1010 calc_modrm_operand(env, decode, &decode->op[1]);
1011 decode->op[1].val = sign(read_val_ext(env, decode->op[1].ptr, src_op_size),
1014 write_val_ext(env, decode->op[0].ptr, decode->op[1].val, op_size);
1016 RIP(env) += decode->len;
1019 void exec_ror(struct CPUX86State *env, struct x86_decode *decode)
1023 fetch_operands(env, decode, 2, true, true, false);
1024 count = decode->op[1].val;
1026 switch (decode->operand_size) {
1029 uint32_t bit6, bit7;
1032 if ((count & 0x07) == 0) {
1034 bit6 = ((uint8_t)decode->op[0].val >> 6) & 1;
1035 bit7 = ((uint8_t)decode->op[0].val >> 7) & 1;
1036 SET_FLAGS_OxxxxC(env, bit6 ^ bit7, bit7);
1039 count &= 0x7; /* use only bottom 3 bits */
1040 res = ((uint8_t)decode->op[0].val >> count) |
1041 ((uint8_t)decode->op[0].val << (8 - count));
1042 write_val_ext(env, decode->op[0].ptr, res, 1);
1043 bit6 = (res >> 6) & 1;
1044 bit7 = (res >> 7) & 1;
1045 /* set eflags: ROR count affects the following flags: C, O */
1046 SET_FLAGS_OxxxxC(env, bit6 ^ bit7, bit7);
1052 uint32_t bit14, bit15;
1055 if ((count & 0x0f) == 0) {
1057 bit14 = ((uint16_t)decode->op[0].val >> 14) & 1;
1058 bit15 = ((uint16_t)decode->op[0].val >> 15) & 1;
1059 /* of = result14 ^ result15 */
1060 SET_FLAGS_OxxxxC(env, bit14 ^ bit15, bit15);
1063 count &= 0x0f; /* use only 4 LSB's */
1064 res = ((uint16_t)decode->op[0].val >> count) |
1065 ((uint16_t)decode->op[0].val << (16 - count));
1066 write_val_ext(env, decode->op[0].ptr, res, 2);
1068 bit14 = (res >> 14) & 1;
1069 bit15 = (res >> 15) & 1;
1070 /* of = result14 ^ result15 */
1071 SET_FLAGS_OxxxxC(env, bit14 ^ bit15, bit15);
1077 uint32_t bit31, bit30;
1082 res = ((uint32_t)decode->op[0].val >> count) |
1083 ((uint32_t)decode->op[0].val << (32 - count));
1084 write_val_ext(env, decode->op[0].ptr, res, 4);
1086 bit31 = (res >> 31) & 1;
1087 bit30 = (res >> 30) & 1;
1088 /* of = result30 ^ result31 */
1089 SET_FLAGS_OxxxxC(env, bit30 ^ bit31, bit31);
1094 RIP(env) += decode->len;
1097 void exec_rol(struct CPUX86State *env, struct x86_decode *decode)
1101 fetch_operands(env, decode, 2, true, true, false);
1102 count = decode->op[1].val;
1104 switch (decode->operand_size) {
1107 uint32_t bit0, bit7;
1110 if ((count & 0x07) == 0) {
1112 bit0 = ((uint8_t)decode->op[0].val & 1);
1113 bit7 = ((uint8_t)decode->op[0].val >> 7);
1114 SET_FLAGS_OxxxxC(env, bit0 ^ bit7, bit0);
1117 count &= 0x7; /* use only lowest 3 bits */
1118 res = ((uint8_t)decode->op[0].val << count) |
1119 ((uint8_t)decode->op[0].val >> (8 - count));
1121 write_val_ext(env, decode->op[0].ptr, res, 1);
1123 * ROL count affects the following flags: C, O
1127 SET_FLAGS_OxxxxC(env, bit0 ^ bit7, bit0);
1133 uint32_t bit0, bit15;
1136 if ((count & 0x0f) == 0) {
1138 bit0 = ((uint16_t)decode->op[0].val & 0x1);
1139 bit15 = ((uint16_t)decode->op[0].val >> 15);
1140 /* of = cf ^ result15 */
1141 SET_FLAGS_OxxxxC(env, bit0 ^ bit15, bit0);
1144 count &= 0x0f; /* only use bottom 4 bits */
1145 res = ((uint16_t)decode->op[0].val << count) |
1146 ((uint16_t)decode->op[0].val >> (16 - count));
1148 write_val_ext(env, decode->op[0].ptr, res, 2);
1150 bit15 = (res >> 15);
1151 /* of = cf ^ result15 */
1152 SET_FLAGS_OxxxxC(env, bit0 ^ bit15, bit0);
1158 uint32_t bit0, bit31;
1163 res = ((uint32_t)decode->op[0].val << count) |
1164 ((uint32_t)decode->op[0].val >> (32 - count));
1166 write_val_ext(env, decode->op[0].ptr, res, 4);
1168 bit31 = (res >> 31);
1169 /* of = cf ^ result31 */
1170 SET_FLAGS_OxxxxC(env, bit0 ^ bit31, bit0);
1175 RIP(env) += decode->len;
1179 void exec_rcl(struct CPUX86State *env, struct x86_decode *decode)
1184 fetch_operands(env, decode, 2, true, true, false);
1185 count = decode->op[1].val & 0x1f;
1187 switch (decode->operand_size) {
1190 uint8_t op1_8 = decode->op[0].val;
1198 res = (op1_8 << 1) | get_CF(env);
1200 res = (op1_8 << count) | (get_CF(env) << (count - 1)) |
1201 (op1_8 >> (9 - count));
1204 write_val_ext(env, decode->op[0].ptr, res, 1);
1206 cf = (op1_8 >> (8 - count)) & 0x01;
1207 of = cf ^ (res >> 7); /* of = cf ^ result7 */
1208 SET_FLAGS_OxxxxC(env, of, cf);
1214 uint16_t op1_16 = decode->op[0].val;
1222 res = (op1_16 << 1) | get_CF(env);
1223 } else if (count == 16) {
1224 res = (get_CF(env) << 15) | (op1_16 >> 1);
1225 } else { /* 2..15 */
1226 res = (op1_16 << count) | (get_CF(env) << (count - 1)) |
1227 (op1_16 >> (17 - count));
1230 write_val_ext(env, decode->op[0].ptr, res, 2);
1232 cf = (op1_16 >> (16 - count)) & 0x1;
1233 of = cf ^ (res >> 15); /* of = cf ^ result15 */
1234 SET_FLAGS_OxxxxC(env, of, cf);
1240 uint32_t op1_32 = decode->op[0].val;
1247 res = (op1_32 << 1) | get_CF(env);
1249 res = (op1_32 << count) | (get_CF(env) << (count - 1)) |
1250 (op1_32 >> (33 - count));
1253 write_val_ext(env, decode->op[0].ptr, res, 4);
1255 cf = (op1_32 >> (32 - count)) & 0x1;
1256 of = cf ^ (res >> 31); /* of = cf ^ result31 */
1257 SET_FLAGS_OxxxxC(env, of, cf);
1261 RIP(env) += decode->len;
1264 void exec_rcr(struct CPUX86State *env, struct x86_decode *decode)
1269 fetch_operands(env, decode, 2, true, true, false);
1270 count = decode->op[1].val & 0x1f;
1272 switch (decode->operand_size) {
1275 uint8_t op1_8 = decode->op[0].val;
1282 res = (op1_8 >> count) | (get_CF(env) << (8 - count)) |
1283 (op1_8 << (9 - count));
1285 write_val_ext(env, decode->op[0].ptr, res, 1);
1287 cf = (op1_8 >> (count - 1)) & 0x1;
1288 of = (((res << 1) ^ res) >> 7) & 0x1; /* of = result6 ^ result7 */
1289 SET_FLAGS_OxxxxC(env, of, cf);
1294 uint16_t op1_16 = decode->op[0].val;
1301 res = (op1_16 >> count) | (get_CF(env) << (16 - count)) |
1302 (op1_16 << (17 - count));
1304 write_val_ext(env, decode->op[0].ptr, res, 2);
1306 cf = (op1_16 >> (count - 1)) & 0x1;
1307 of = ((uint16_t)((res << 1) ^ res) >> 15) & 0x1; /* of = result15 ^
1309 SET_FLAGS_OxxxxC(env, of, cf);
1315 uint32_t op1_32 = decode->op[0].val;
1322 res = (op1_32 >> 1) | (get_CF(env) << 31);
1324 res = (op1_32 >> count) | (get_CF(env) << (32 - count)) |
1325 (op1_32 << (33 - count));
1328 write_val_ext(env, decode->op[0].ptr, res, 4);
1330 cf = (op1_32 >> (count - 1)) & 0x1;
1331 of = ((res << 1) ^ res) >> 31; /* of = result30 ^ result31 */
1332 SET_FLAGS_OxxxxC(env, of, cf);
1336 RIP(env) += decode->len;
1339 static void exec_xchg(struct CPUX86State *env, struct x86_decode *decode)
1341 fetch_operands(env, decode, 2, true, true, false);
1343 write_val_ext(env, decode->op[0].ptr, decode->op[1].val,
1344 decode->operand_size);
1345 write_val_ext(env, decode->op[1].ptr, decode->op[0].val,
1346 decode->operand_size);
1348 RIP(env) += decode->len;
1351 static void exec_xadd(struct CPUX86State *env, struct x86_decode *decode)
1353 EXEC_2OP_FLAGS_CMD(env, decode, +, SET_FLAGS_OSZAPC_ADD, true);
1354 write_val_ext(env, decode->op[1].ptr, decode->op[0].val,
1355 decode->operand_size);
1357 RIP(env) += decode->len;
1360 static struct cmd_handler {
1361 enum x86_decode_cmd cmd;
1362 void (*handler)(struct CPUX86State *env, struct x86_decode *ins);
1364 {X86_DECODE_CMD_INVL, NULL,},
1365 {X86_DECODE_CMD_MOV, exec_mov},
1366 {X86_DECODE_CMD_ADD, exec_add},
1367 {X86_DECODE_CMD_OR, exec_or},
1368 {X86_DECODE_CMD_ADC, exec_adc},
1369 {X86_DECODE_CMD_SBB, exec_sbb},
1370 {X86_DECODE_CMD_AND, exec_and},
1371 {X86_DECODE_CMD_SUB, exec_sub},
1372 {X86_DECODE_CMD_NEG, exec_neg},
1373 {X86_DECODE_CMD_XOR, exec_xor},
1374 {X86_DECODE_CMD_CMP, exec_cmp},
1375 {X86_DECODE_CMD_INC, exec_inc},
1376 {X86_DECODE_CMD_DEC, exec_dec},
1377 {X86_DECODE_CMD_TST, exec_tst},
1378 {X86_DECODE_CMD_NOT, exec_not},
1379 {X86_DECODE_CMD_MOVZX, exec_movzx},
1380 {X86_DECODE_CMD_OUT, exec_out},
1381 {X86_DECODE_CMD_IN, exec_in},
1382 {X86_DECODE_CMD_INS, exec_ins},
1383 {X86_DECODE_CMD_OUTS, exec_outs},
1384 {X86_DECODE_CMD_RDMSR, exec_rdmsr},
1385 {X86_DECODE_CMD_WRMSR, exec_wrmsr},
1386 {X86_DECODE_CMD_BT, exec_bt},
1387 {X86_DECODE_CMD_BTR, exec_btr},
1388 {X86_DECODE_CMD_BTC, exec_btc},
1389 {X86_DECODE_CMD_BTS, exec_bts},
1390 {X86_DECODE_CMD_SHL, exec_shl},
1391 {X86_DECODE_CMD_ROL, exec_rol},
1392 {X86_DECODE_CMD_ROR, exec_ror},
1393 {X86_DECODE_CMD_RCR, exec_rcr},
1394 {X86_DECODE_CMD_RCL, exec_rcl},
1395 /*{X86_DECODE_CMD_CPUID, exec_cpuid},*/
1396 {X86_DECODE_CMD_MOVS, exec_movs},
1397 {X86_DECODE_CMD_CMPS, exec_cmps},
1398 {X86_DECODE_CMD_STOS, exec_stos},
1399 {X86_DECODE_CMD_SCAS, exec_scas},
1400 {X86_DECODE_CMD_LODS, exec_lods},
1401 {X86_DECODE_CMD_MOVSX, exec_movsx},
1402 {X86_DECODE_CMD_XCHG, exec_xchg},
1403 {X86_DECODE_CMD_XADD, exec_xadd},
1406 static struct cmd_handler _cmd_handler[X86_DECODE_CMD_LAST];
1408 static void init_cmd_handler()
1411 for (i = 0; i < ARRAY_SIZE(handlers); i++) {
1412 _cmd_handler[handlers[i].cmd] = handlers[i];
1416 void load_regs(struct CPUState *cpu)
1418 X86CPU *x86_cpu = X86_CPU(cpu);
1419 CPUX86State *env = &x86_cpu->env;
1422 RRX(env, R_EAX) = rreg(cpu->hvf_fd, HV_X86_RAX);
1423 RRX(env, R_EBX) = rreg(cpu->hvf_fd, HV_X86_RBX);
1424 RRX(env, R_ECX) = rreg(cpu->hvf_fd, HV_X86_RCX);
1425 RRX(env, R_EDX) = rreg(cpu->hvf_fd, HV_X86_RDX);
1426 RRX(env, R_ESI) = rreg(cpu->hvf_fd, HV_X86_RSI);
1427 RRX(env, R_EDI) = rreg(cpu->hvf_fd, HV_X86_RDI);
1428 RRX(env, R_ESP) = rreg(cpu->hvf_fd, HV_X86_RSP);
1429 RRX(env, R_EBP) = rreg(cpu->hvf_fd, HV_X86_RBP);
1430 for (i = 8; i < 16; i++) {
1431 RRX(env, i) = rreg(cpu->hvf_fd, HV_X86_RAX + i);
1434 RFLAGS(env) = rreg(cpu->hvf_fd, HV_X86_RFLAGS);
1435 rflags_to_lflags(env);
1436 RIP(env) = rreg(cpu->hvf_fd, HV_X86_RIP);
1439 void store_regs(struct CPUState *cpu)
1441 X86CPU *x86_cpu = X86_CPU(cpu);
1442 CPUX86State *env = &x86_cpu->env;
1445 wreg(cpu->hvf_fd, HV_X86_RAX, RAX(env));
1446 wreg(cpu->hvf_fd, HV_X86_RBX, RBX(env));
1447 wreg(cpu->hvf_fd, HV_X86_RCX, RCX(env));
1448 wreg(cpu->hvf_fd, HV_X86_RDX, RDX(env));
1449 wreg(cpu->hvf_fd, HV_X86_RSI, RSI(env));
1450 wreg(cpu->hvf_fd, HV_X86_RDI, RDI(env));
1451 wreg(cpu->hvf_fd, HV_X86_RBP, RBP(env));
1452 wreg(cpu->hvf_fd, HV_X86_RSP, RSP(env));
1453 for (i = 8; i < 16; i++) {
1454 wreg(cpu->hvf_fd, HV_X86_RAX + i, RRX(env, i));
1457 lflags_to_rflags(env);
1458 wreg(cpu->hvf_fd, HV_X86_RFLAGS, RFLAGS(env));
1459 macvm_set_rip(cpu, RIP(env));
1462 bool exec_instruction(struct CPUX86State *env, struct x86_decode *ins)
1464 /*if (hvf_vcpu_id(cpu))
1465 printf("%d, %llx: exec_instruction %s\n", hvf_vcpu_id(cpu), RIP(cpu),
1466 decode_cmd_to_string(ins->cmd));*/
1468 if (!_cmd_handler[ins->cmd].handler) {
1469 printf("Unimplemented handler (%llx) for %d (%x %x) \n", RIP(env),
1470 ins->cmd, ins->opcode[0],
1471 ins->opcode_len > 1 ? ins->opcode[1] : 0);
1472 RIP(env) += ins->len;
1476 _cmd_handler[ins->cmd].handler(env, ins);