xref: /openbmc/qemu/target/i386/tcg/tcg-cpu.c (revision 1c8e621f097362d0a28c65fa2f31054b47a5282b)
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 
x86_cpu_exec_enter(CPUState * cs)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 
x86_cpu_exec_exit(CPUState * cs)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 
x86_cpu_synchronize_from_tb(CPUState * cs,const TranslationBlock * tb)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 
x86_restore_state_to_opc(CPUState * cs,const TranslationBlock * tb,const uint64_t * data)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];
71729ba8e9SPaolo Bonzini     uint64_t new_pc;
72434382e6SRichard Henderson 
732e3afe8eSAnton Johansson     if (tb_cflags(tb) & CF_PCREL) {
74729ba8e9SPaolo Bonzini         /*
75729ba8e9SPaolo Bonzini          * data[0] in PC-relative TBs is also a linear address, i.e. an address with
76729ba8e9SPaolo Bonzini          * the CS base added, because it is not guaranteed that EIP bits 12 and higher
77729ba8e9SPaolo Bonzini          * stay the same across the translation block.  Add the CS base back before
78729ba8e9SPaolo Bonzini          * replacing the low bits, and subtract it below just like for !CF_PCREL.
79729ba8e9SPaolo Bonzini          */
80729ba8e9SPaolo Bonzini         uint64_t pc = env->eip + tb->cs_base;
81729ba8e9SPaolo Bonzini         new_pc = (pc & TARGET_PAGE_MASK) | data[0];
82434382e6SRichard Henderson     } else {
83729ba8e9SPaolo Bonzini         new_pc = data[0];
84434382e6SRichard Henderson     }
85729ba8e9SPaolo Bonzini     if (tb->flags & HF_CS64_MASK) {
86729ba8e9SPaolo Bonzini         env->eip = new_pc;
87729ba8e9SPaolo Bonzini     } else {
88729ba8e9SPaolo Bonzini         env->eip = (uint32_t)(new_pc - tb->cs_base);
89729ba8e9SPaolo Bonzini     }
90729ba8e9SPaolo Bonzini 
91434382e6SRichard Henderson     if (cc_op != CC_OP_DYNAMIC) {
92434382e6SRichard Henderson         env->cc_op = cc_op;
93434382e6SRichard Henderson     }
94434382e6SRichard Henderson }
95434382e6SRichard Henderson 
967b9810eaSRichard Henderson #ifndef CONFIG_USER_ONLY
x86_debug_check_breakpoint(CPUState * cs)977b9810eaSRichard Henderson static bool x86_debug_check_breakpoint(CPUState *cs)
987b9810eaSRichard Henderson {
997b9810eaSRichard Henderson     X86CPU *cpu = X86_CPU(cs);
1007b9810eaSRichard Henderson     CPUX86State *env = &cpu->env;
1017b9810eaSRichard Henderson 
1027b9810eaSRichard Henderson     /* RF disables all architectural breakpoints. */
1037b9810eaSRichard Henderson     return !(env->eflags & RF_MASK);
1047b9810eaSRichard Henderson }
1057b9810eaSRichard Henderson #endif
1067b9810eaSRichard Henderson 
10778271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
10878271684SClaudio Fontana 
1091764ad70SRichard Henderson static const TCGCPUOps x86_tcg_ops = {
11078271684SClaudio Fontana     .initialize = tcg_x86_init,
11178271684SClaudio Fontana     .synchronize_from_tb = x86_cpu_synchronize_from_tb,
112434382e6SRichard Henderson     .restore_state_to_opc = x86_restore_state_to_opc,
11378271684SClaudio Fontana     .cpu_exec_enter = x86_cpu_exec_enter,
11478271684SClaudio Fontana     .cpu_exec_exit = x86_cpu_exec_exit,
11512096421SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY
11612096421SPhilippe Mathieu-Daudé     .fake_user_interrupt = x86_cpu_do_interrupt,
117f74bd157SRichard Henderson     .record_sigsegv = x86_cpu_record_sigsegv,
118958e1dd1SPaolo Bonzini     .record_sigbus = x86_cpu_record_sigbus,
11912096421SPhilippe Mathieu-Daudé #else
120f74bd157SRichard Henderson     .tlb_fill = x86_cpu_tlb_fill,
12112096421SPhilippe Mathieu-Daudé     .do_interrupt = x86_cpu_do_interrupt,
122*ec1d32afSPhilippe Mathieu-Daudé     .cpu_exec_halt = x86_cpu_exec_halt,
12360466472SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = x86_cpu_exec_interrupt,
124958e1dd1SPaolo Bonzini     .do_unaligned_access = x86_cpu_do_unaligned_access,
12578271684SClaudio Fontana     .debug_excp_handler = breakpoint_handler,
1267b9810eaSRichard Henderson     .debug_check_breakpoint = x86_debug_check_breakpoint,
1276ae75481SPhilippe Mathieu-Daudé     .need_replay_interrupt = x86_need_replay_interrupt,
12878271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
12978271684SClaudio Fontana };
13078271684SClaudio Fontana 
x86_tcg_cpu_init_ops(AccelCPUClass * accel_cpu,CPUClass * cc)131e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_init_ops(AccelCPUClass *accel_cpu, CPUClass *cc)
132cc3f2be6SClaudio Fontana {
133cc3f2be6SClaudio Fontana     /* for x86, all cpus use the same set of operations */
134cc3f2be6SClaudio Fontana     cc->tcg_ops = &x86_tcg_ops;
135cc3f2be6SClaudio Fontana }
136cc3f2be6SClaudio Fontana 
x86_tcg_cpu_class_init(CPUClass * cc)137e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_class_init(CPUClass *cc)
138ed69e831SClaudio Fontana {
139e129593fSPhilippe Mathieu-Daudé     cc->init_accel_cpu = x86_tcg_cpu_init_ops;
140ed69e831SClaudio Fontana }
141f5cc5a5cSClaudio Fontana 
x86_tcg_cpu_xsave_init(void)142e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_xsave_init(void)
143fea45008SDavid Edmondson {
144fea45008SDavid Edmondson #define XO(bit, field) \
145fea45008SDavid Edmondson     x86_ext_save_areas[bit].offset = offsetof(X86XSaveArea, field);
146fea45008SDavid Edmondson 
147fea45008SDavid Edmondson     XO(XSTATE_FP_BIT, legacy);
148fea45008SDavid Edmondson     XO(XSTATE_SSE_BIT, legacy);
149fea45008SDavid Edmondson     XO(XSTATE_YMM_BIT, avx_state);
150fea45008SDavid Edmondson     XO(XSTATE_BNDREGS_BIT, bndreg_state);
151fea45008SDavid Edmondson     XO(XSTATE_BNDCSR_BIT, bndcsr_state);
152fea45008SDavid Edmondson     XO(XSTATE_OPMASK_BIT, opmask_state);
153fea45008SDavid Edmondson     XO(XSTATE_ZMM_Hi256_BIT, zmm_hi256_state);
154fea45008SDavid Edmondson     XO(XSTATE_Hi16_ZMM_BIT, hi16_zmm_state);
155fea45008SDavid Edmondson     XO(XSTATE_PKRU_BIT, pkru_state);
156fea45008SDavid Edmondson 
157fea45008SDavid Edmondson #undef XO
158fea45008SDavid Edmondson }
159fea45008SDavid Edmondson 
160f5cc5a5cSClaudio Fontana /*
1615b8978d8SClaudio Fontana  * TCG-specific defaults that override cpudef models when using TCG.
1625b8978d8SClaudio Fontana  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
163f5cc5a5cSClaudio Fontana  */
164e129593fSPhilippe Mathieu-Daudé static PropValue x86_tcg_default_props[] = {
165f5cc5a5cSClaudio Fontana     { "vme", "off" },
166f5cc5a5cSClaudio Fontana     { NULL, NULL },
167f5cc5a5cSClaudio Fontana };
168f5cc5a5cSClaudio Fontana 
x86_tcg_cpu_instance_init(CPUState * cs)169e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_instance_init(CPUState *cs)
170f5cc5a5cSClaudio Fontana {
171f5cc5a5cSClaudio Fontana     X86CPU *cpu = X86_CPU(cs);
1725b8978d8SClaudio Fontana     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1735b8978d8SClaudio Fontana 
1745b8978d8SClaudio Fontana     if (xcc->model) {
175f5cc5a5cSClaudio Fontana         /* Special cases not set in the X86CPUDefinition structs: */
176e129593fSPhilippe Mathieu-Daudé         x86_cpu_apply_props(cpu, x86_tcg_default_props);
1775b8978d8SClaudio Fontana     }
178fea45008SDavid Edmondson 
179e129593fSPhilippe Mathieu-Daudé     x86_tcg_cpu_xsave_init();
180f5cc5a5cSClaudio Fontana }
181f5cc5a5cSClaudio Fontana 
x86_tcg_cpu_accel_class_init(ObjectClass * oc,void * data)182e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_accel_class_init(ObjectClass *oc, void *data)
183f5cc5a5cSClaudio Fontana {
184f5cc5a5cSClaudio Fontana     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
185f5cc5a5cSClaudio Fontana 
186222f3e6fSPaolo Bonzini #ifndef CONFIG_USER_ONLY
1876294e502SPhilippe Mathieu-Daudé     acc->cpu_target_realize = tcg_cpu_realizefn;
188222f3e6fSPaolo Bonzini #endif /* CONFIG_USER_ONLY */
189222f3e6fSPaolo Bonzini 
190e129593fSPhilippe Mathieu-Daudé     acc->cpu_class_init = x86_tcg_cpu_class_init;
191e129593fSPhilippe Mathieu-Daudé     acc->cpu_instance_init = x86_tcg_cpu_instance_init;
192f5cc5a5cSClaudio Fontana }
193e129593fSPhilippe Mathieu-Daudé static const TypeInfo x86_tcg_cpu_accel_type_info = {
194f5cc5a5cSClaudio Fontana     .name = ACCEL_CPU_NAME("tcg"),
195f5cc5a5cSClaudio Fontana 
196f5cc5a5cSClaudio Fontana     .parent = TYPE_ACCEL_CPU,
197e129593fSPhilippe Mathieu-Daudé     .class_init = x86_tcg_cpu_accel_class_init,
198f5cc5a5cSClaudio Fontana     .abstract = true,
199f5cc5a5cSClaudio Fontana };
x86_tcg_cpu_accel_register_types(void)200e129593fSPhilippe Mathieu-Daudé static void x86_tcg_cpu_accel_register_types(void)
201f5cc5a5cSClaudio Fontana {
202e129593fSPhilippe Mathieu-Daudé     type_register_static(&x86_tcg_cpu_accel_type_info);
203f5cc5a5cSClaudio Fontana }
204e129593fSPhilippe Mathieu-Daudé type_init(x86_tcg_cpu_accel_register_types);
205