1ed69e831SClaudio Fontana /* 2ed69e831SClaudio Fontana * i386 TCG cpu class initialization 3ed69e831SClaudio Fontana * 4ed69e831SClaudio Fontana * Copyright (c) 2003 Fabrice Bellard 5ed69e831SClaudio Fontana * 6ed69e831SClaudio Fontana * This library is free software; you can redistribute it and/or 7ed69e831SClaudio Fontana * modify it under the terms of the GNU Lesser General Public 8ed69e831SClaudio Fontana * License as published by the Free Software Foundation; either 9ed69e831SClaudio Fontana * version 2 of the License, or (at your option) any later version. 10ed69e831SClaudio Fontana * 11ed69e831SClaudio Fontana * This library is distributed in the hope that it will be useful, 12ed69e831SClaudio Fontana * but WITHOUT ANY WARRANTY; without even the implied warranty of 13ed69e831SClaudio Fontana * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14ed69e831SClaudio Fontana * Lesser General Public License for more details. 15ed69e831SClaudio Fontana * 16ed69e831SClaudio Fontana * You should have received a copy of the GNU Lesser General Public 17ed69e831SClaudio Fontana * License along with this library; if not, see <http://www.gnu.org/licenses/>. 18ed69e831SClaudio Fontana */ 19ed69e831SClaudio Fontana 20ed69e831SClaudio Fontana #include "qemu/osdep.h" 21ed69e831SClaudio Fontana #include "cpu.h" 22ed69e831SClaudio Fontana #include "helper-tcg.h" 23f5cc5a5cSClaudio Fontana #include "qemu/accel.h" 24f5cc5a5cSClaudio Fontana #include "hw/core/accel-cpu.h" 25ed69e831SClaudio Fontana 26222f3e6fSPaolo Bonzini #include "tcg-cpu.h" 27ed69e831SClaudio Fontana 28ed69e831SClaudio Fontana /* Frob eflags into and out of the CPU temporary format. */ 29ed69e831SClaudio Fontana 30ed69e831SClaudio Fontana static void x86_cpu_exec_enter(CPUState *cs) 31ed69e831SClaudio Fontana { 32ed69e831SClaudio Fontana X86CPU *cpu = X86_CPU(cs); 33ed69e831SClaudio Fontana CPUX86State *env = &cpu->env; 34ed69e831SClaudio Fontana 35ed69e831SClaudio Fontana CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); 36ed69e831SClaudio Fontana env->df = 1 - (2 * ((env->eflags >> 10) & 1)); 37ed69e831SClaudio Fontana CC_OP = CC_OP_EFLAGS; 38ed69e831SClaudio Fontana env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C); 39ed69e831SClaudio Fontana } 40ed69e831SClaudio Fontana 41ed69e831SClaudio Fontana static void x86_cpu_exec_exit(CPUState *cs) 42ed69e831SClaudio Fontana { 43ed69e831SClaudio Fontana X86CPU *cpu = X86_CPU(cs); 44ed69e831SClaudio Fontana CPUX86State *env = &cpu->env; 45ed69e831SClaudio Fontana 46ed69e831SClaudio Fontana env->eflags = cpu_compute_eflags(env); 47ed69e831SClaudio Fontana } 48ed69e831SClaudio Fontana 4904a37d4cSRichard Henderson static void x86_cpu_synchronize_from_tb(CPUState *cs, 5004a37d4cSRichard Henderson const TranslationBlock *tb) 51ed69e831SClaudio Fontana { 522e3afe8eSAnton Johansson /* The instruction pointer is always up to date with CF_PCREL. */ 532e3afe8eSAnton Johansson if (!(tb_cflags(tb) & CF_PCREL)) { 54b77af26eSRichard Henderson CPUX86State *env = cpu_env(cs); 55b5e0d5d2SRichard Henderson 56b5e0d5d2SRichard Henderson if (tb->flags & HF_CS64_MASK) { 57b5e0d5d2SRichard Henderson env->eip = tb->pc; 58b5e0d5d2SRichard Henderson } else { 59b5e0d5d2SRichard Henderson env->eip = (uint32_t)(tb->pc - tb->cs_base); 60b5e0d5d2SRichard Henderson } 61e3a79e0eSRichard Henderson } 62ed69e831SClaudio Fontana } 63ed69e831SClaudio Fontana 64434382e6SRichard Henderson static void x86_restore_state_to_opc(CPUState *cs, 65434382e6SRichard Henderson const TranslationBlock *tb, 66434382e6SRichard Henderson const uint64_t *data) 67434382e6SRichard Henderson { 68434382e6SRichard Henderson X86CPU *cpu = X86_CPU(cs); 69434382e6SRichard Henderson CPUX86State *env = &cpu->env; 70434382e6SRichard Henderson int cc_op = data[1]; 71434382e6SRichard Henderson 722e3afe8eSAnton Johansson if (tb_cflags(tb) & CF_PCREL) { 73434382e6SRichard Henderson env->eip = (env->eip & TARGET_PAGE_MASK) | data[0]; 74b5e0d5d2SRichard Henderson } else if (tb->flags & HF_CS64_MASK) { 75b5e0d5d2SRichard Henderson env->eip = data[0]; 76434382e6SRichard Henderson } else { 77b5e0d5d2SRichard Henderson env->eip = (uint32_t)(data[0] - tb->cs_base); 78434382e6SRichard Henderson } 79434382e6SRichard Henderson if (cc_op != CC_OP_DYNAMIC) { 80434382e6SRichard Henderson env->cc_op = cc_op; 81434382e6SRichard Henderson } 82434382e6SRichard Henderson } 83434382e6SRichard Henderson 847b9810eaSRichard Henderson #ifndef CONFIG_USER_ONLY 857b9810eaSRichard Henderson static bool x86_debug_check_breakpoint(CPUState *cs) 867b9810eaSRichard Henderson { 877b9810eaSRichard Henderson X86CPU *cpu = X86_CPU(cs); 887b9810eaSRichard Henderson CPUX86State *env = &cpu->env; 897b9810eaSRichard Henderson 907b9810eaSRichard Henderson /* RF disables all architectural breakpoints. */ 917b9810eaSRichard Henderson return !(env->eflags & RF_MASK); 927b9810eaSRichard Henderson } 937b9810eaSRichard Henderson #endif 947b9810eaSRichard Henderson 9578271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h" 9678271684SClaudio Fontana 9711906557SRichard Henderson static const struct TCGCPUOps x86_tcg_ops = { 9878271684SClaudio Fontana .initialize = tcg_x86_init, 9978271684SClaudio Fontana .synchronize_from_tb = x86_cpu_synchronize_from_tb, 100434382e6SRichard Henderson .restore_state_to_opc = x86_restore_state_to_opc, 10178271684SClaudio Fontana .cpu_exec_enter = x86_cpu_exec_enter, 10278271684SClaudio Fontana .cpu_exec_exit = x86_cpu_exec_exit, 10312096421SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY 10412096421SPhilippe Mathieu-Daudé .fake_user_interrupt = x86_cpu_do_interrupt, 105f74bd157SRichard Henderson .record_sigsegv = x86_cpu_record_sigsegv, 106958e1dd1SPaolo Bonzini .record_sigbus = x86_cpu_record_sigbus, 10712096421SPhilippe Mathieu-Daudé #else 108f74bd157SRichard Henderson .tlb_fill = x86_cpu_tlb_fill, 10912096421SPhilippe Mathieu-Daudé .do_interrupt = x86_cpu_do_interrupt, 11060466472SPhilippe Mathieu-Daudé .cpu_exec_interrupt = x86_cpu_exec_interrupt, 111958e1dd1SPaolo Bonzini .do_unaligned_access = x86_cpu_do_unaligned_access, 11278271684SClaudio Fontana .debug_excp_handler = breakpoint_handler, 1137b9810eaSRichard Henderson .debug_check_breakpoint = x86_debug_check_breakpoint, 11478271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */ 11578271684SClaudio Fontana }; 11678271684SClaudio Fontana 117*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_init_ops(AccelCPUClass *accel_cpu, CPUClass *cc) 118cc3f2be6SClaudio Fontana { 119cc3f2be6SClaudio Fontana /* for x86, all cpus use the same set of operations */ 120cc3f2be6SClaudio Fontana cc->tcg_ops = &x86_tcg_ops; 121cc3f2be6SClaudio Fontana } 122cc3f2be6SClaudio Fontana 123*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_class_init(CPUClass *cc) 124ed69e831SClaudio Fontana { 125*e129593fSPhilippe Mathieu-Daudé cc->init_accel_cpu = x86_tcg_cpu_init_ops; 126ed69e831SClaudio Fontana } 127f5cc5a5cSClaudio Fontana 128*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_xsave_init(void) 129fea45008SDavid Edmondson { 130fea45008SDavid Edmondson #define XO(bit, field) \ 131fea45008SDavid Edmondson x86_ext_save_areas[bit].offset = offsetof(X86XSaveArea, field); 132fea45008SDavid Edmondson 133fea45008SDavid Edmondson XO(XSTATE_FP_BIT, legacy); 134fea45008SDavid Edmondson XO(XSTATE_SSE_BIT, legacy); 135fea45008SDavid Edmondson XO(XSTATE_YMM_BIT, avx_state); 136fea45008SDavid Edmondson XO(XSTATE_BNDREGS_BIT, bndreg_state); 137fea45008SDavid Edmondson XO(XSTATE_BNDCSR_BIT, bndcsr_state); 138fea45008SDavid Edmondson XO(XSTATE_OPMASK_BIT, opmask_state); 139fea45008SDavid Edmondson XO(XSTATE_ZMM_Hi256_BIT, zmm_hi256_state); 140fea45008SDavid Edmondson XO(XSTATE_Hi16_ZMM_BIT, hi16_zmm_state); 141fea45008SDavid Edmondson XO(XSTATE_PKRU_BIT, pkru_state); 142fea45008SDavid Edmondson 143fea45008SDavid Edmondson #undef XO 144fea45008SDavid Edmondson } 145fea45008SDavid Edmondson 146f5cc5a5cSClaudio Fontana /* 1475b8978d8SClaudio Fontana * TCG-specific defaults that override cpudef models when using TCG. 1485b8978d8SClaudio Fontana * Only for builtin_x86_defs models initialized with x86_register_cpudef_types. 149f5cc5a5cSClaudio Fontana */ 150*e129593fSPhilippe Mathieu-Daudé static PropValue x86_tcg_default_props[] = { 151f5cc5a5cSClaudio Fontana { "vme", "off" }, 152f5cc5a5cSClaudio Fontana { NULL, NULL }, 153f5cc5a5cSClaudio Fontana }; 154f5cc5a5cSClaudio Fontana 155*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_instance_init(CPUState *cs) 156f5cc5a5cSClaudio Fontana { 157f5cc5a5cSClaudio Fontana X86CPU *cpu = X86_CPU(cs); 1585b8978d8SClaudio Fontana X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu); 1595b8978d8SClaudio Fontana 1605b8978d8SClaudio Fontana if (xcc->model) { 161f5cc5a5cSClaudio Fontana /* Special cases not set in the X86CPUDefinition structs: */ 162*e129593fSPhilippe Mathieu-Daudé x86_cpu_apply_props(cpu, x86_tcg_default_props); 1635b8978d8SClaudio Fontana } 164fea45008SDavid Edmondson 165*e129593fSPhilippe Mathieu-Daudé x86_tcg_cpu_xsave_init(); 166f5cc5a5cSClaudio Fontana } 167f5cc5a5cSClaudio Fontana 168*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_accel_class_init(ObjectClass *oc, void *data) 169f5cc5a5cSClaudio Fontana { 170f5cc5a5cSClaudio Fontana AccelCPUClass *acc = ACCEL_CPU_CLASS(oc); 171f5cc5a5cSClaudio Fontana 172222f3e6fSPaolo Bonzini #ifndef CONFIG_USER_ONLY 1736294e502SPhilippe Mathieu-Daudé acc->cpu_target_realize = tcg_cpu_realizefn; 174222f3e6fSPaolo Bonzini #endif /* CONFIG_USER_ONLY */ 175222f3e6fSPaolo Bonzini 176*e129593fSPhilippe Mathieu-Daudé acc->cpu_class_init = x86_tcg_cpu_class_init; 177*e129593fSPhilippe Mathieu-Daudé acc->cpu_instance_init = x86_tcg_cpu_instance_init; 178f5cc5a5cSClaudio Fontana } 179*e129593fSPhilippe Mathieu-Daudé static const TypeInfo x86_tcg_cpu_accel_type_info = { 180f5cc5a5cSClaudio Fontana .name = ACCEL_CPU_NAME("tcg"), 181f5cc5a5cSClaudio Fontana 182f5cc5a5cSClaudio Fontana .parent = TYPE_ACCEL_CPU, 183*e129593fSPhilippe Mathieu-Daudé .class_init = x86_tcg_cpu_accel_class_init, 184f5cc5a5cSClaudio Fontana .abstract = true, 185f5cc5a5cSClaudio Fontana }; 186*e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_accel_register_types(void) 187f5cc5a5cSClaudio Fontana { 188*e129593fSPhilippe Mathieu-Daudé type_register_static(&x86_tcg_cpu_accel_type_info); 189f5cc5a5cSClaudio Fontana } 190*e129593fSPhilippe Mathieu-Daudé type_init(x86_tcg_cpu_accel_register_types); 191