xref: /openbmc/qemu/target/i386/tcg/tcg-cpu.c (revision 434382e640ba1b6fdd06f70e0fe70270bab9cce3)
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 {
52e3a79e0eSRichard Henderson     /* The instruction pointer is always up to date with TARGET_TB_PCREL. */
53e3a79e0eSRichard Henderson     if (!TARGET_TB_PCREL) {
54e3a79e0eSRichard Henderson         CPUX86State *env = cs->env_ptr;
55e3a79e0eSRichard Henderson         env->eip = tb_pc(tb) - tb->cs_base;
56e3a79e0eSRichard Henderson     }
57ed69e831SClaudio Fontana }
58ed69e831SClaudio Fontana 
59*434382e6SRichard Henderson static void x86_restore_state_to_opc(CPUState *cs,
60*434382e6SRichard Henderson                                      const TranslationBlock *tb,
61*434382e6SRichard Henderson                                      const uint64_t *data)
62*434382e6SRichard Henderson {
63*434382e6SRichard Henderson     X86CPU *cpu = X86_CPU(cs);
64*434382e6SRichard Henderson     CPUX86State *env = &cpu->env;
65*434382e6SRichard Henderson     int cc_op = data[1];
66*434382e6SRichard Henderson 
67*434382e6SRichard Henderson     if (TARGET_TB_PCREL) {
68*434382e6SRichard Henderson         env->eip = (env->eip & TARGET_PAGE_MASK) | data[0];
69*434382e6SRichard Henderson     } else {
70*434382e6SRichard Henderson         env->eip = data[0] - tb->cs_base;
71*434382e6SRichard Henderson     }
72*434382e6SRichard Henderson     if (cc_op != CC_OP_DYNAMIC) {
73*434382e6SRichard Henderson         env->cc_op = cc_op;
74*434382e6SRichard Henderson     }
75*434382e6SRichard Henderson }
76*434382e6SRichard Henderson 
777b9810eaSRichard Henderson #ifndef CONFIG_USER_ONLY
787b9810eaSRichard Henderson static bool x86_debug_check_breakpoint(CPUState *cs)
797b9810eaSRichard Henderson {
807b9810eaSRichard Henderson     X86CPU *cpu = X86_CPU(cs);
817b9810eaSRichard Henderson     CPUX86State *env = &cpu->env;
827b9810eaSRichard Henderson 
837b9810eaSRichard Henderson     /* RF disables all architectural breakpoints. */
847b9810eaSRichard Henderson     return !(env->eflags & RF_MASK);
857b9810eaSRichard Henderson }
867b9810eaSRichard Henderson #endif
877b9810eaSRichard Henderson 
8878271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
8978271684SClaudio Fontana 
9011906557SRichard Henderson static const struct TCGCPUOps x86_tcg_ops = {
9178271684SClaudio Fontana     .initialize = tcg_x86_init,
9278271684SClaudio Fontana     .synchronize_from_tb = x86_cpu_synchronize_from_tb,
93*434382e6SRichard Henderson     .restore_state_to_opc = x86_restore_state_to_opc,
9478271684SClaudio Fontana     .cpu_exec_enter = x86_cpu_exec_enter,
9578271684SClaudio Fontana     .cpu_exec_exit = x86_cpu_exec_exit,
9612096421SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY
9712096421SPhilippe Mathieu-Daudé     .fake_user_interrupt = x86_cpu_do_interrupt,
98f74bd157SRichard Henderson     .record_sigsegv = x86_cpu_record_sigsegv,
99958e1dd1SPaolo Bonzini     .record_sigbus = x86_cpu_record_sigbus,
10012096421SPhilippe Mathieu-Daudé #else
101f74bd157SRichard Henderson     .tlb_fill = x86_cpu_tlb_fill,
10212096421SPhilippe Mathieu-Daudé     .do_interrupt = x86_cpu_do_interrupt,
10360466472SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = x86_cpu_exec_interrupt,
104958e1dd1SPaolo Bonzini     .do_unaligned_access = x86_cpu_do_unaligned_access,
10578271684SClaudio Fontana     .debug_excp_handler = breakpoint_handler,
1067b9810eaSRichard Henderson     .debug_check_breakpoint = x86_debug_check_breakpoint,
10778271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
10878271684SClaudio Fontana };
10978271684SClaudio Fontana 
110cc3f2be6SClaudio Fontana static void tcg_cpu_init_ops(AccelCPUClass *accel_cpu, CPUClass *cc)
111cc3f2be6SClaudio Fontana {
112cc3f2be6SClaudio Fontana     /* for x86, all cpus use the same set of operations */
113cc3f2be6SClaudio Fontana     cc->tcg_ops = &x86_tcg_ops;
114cc3f2be6SClaudio Fontana }
115cc3f2be6SClaudio Fontana 
116f5cc5a5cSClaudio Fontana static void tcg_cpu_class_init(CPUClass *cc)
117ed69e831SClaudio Fontana {
118cc3f2be6SClaudio Fontana     cc->init_accel_cpu = tcg_cpu_init_ops;
119ed69e831SClaudio Fontana }
120f5cc5a5cSClaudio Fontana 
121fea45008SDavid Edmondson static void tcg_cpu_xsave_init(void)
122fea45008SDavid Edmondson {
123fea45008SDavid Edmondson #define XO(bit, field) \
124fea45008SDavid Edmondson     x86_ext_save_areas[bit].offset = offsetof(X86XSaveArea, field);
125fea45008SDavid Edmondson 
126fea45008SDavid Edmondson     XO(XSTATE_FP_BIT, legacy);
127fea45008SDavid Edmondson     XO(XSTATE_SSE_BIT, legacy);
128fea45008SDavid Edmondson     XO(XSTATE_YMM_BIT, avx_state);
129fea45008SDavid Edmondson     XO(XSTATE_BNDREGS_BIT, bndreg_state);
130fea45008SDavid Edmondson     XO(XSTATE_BNDCSR_BIT, bndcsr_state);
131fea45008SDavid Edmondson     XO(XSTATE_OPMASK_BIT, opmask_state);
132fea45008SDavid Edmondson     XO(XSTATE_ZMM_Hi256_BIT, zmm_hi256_state);
133fea45008SDavid Edmondson     XO(XSTATE_Hi16_ZMM_BIT, hi16_zmm_state);
134fea45008SDavid Edmondson     XO(XSTATE_PKRU_BIT, pkru_state);
135fea45008SDavid Edmondson 
136fea45008SDavid Edmondson #undef XO
137fea45008SDavid Edmondson }
138fea45008SDavid Edmondson 
139f5cc5a5cSClaudio Fontana /*
1405b8978d8SClaudio Fontana  * TCG-specific defaults that override cpudef models when using TCG.
1415b8978d8SClaudio Fontana  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
142f5cc5a5cSClaudio Fontana  */
143f5cc5a5cSClaudio Fontana static PropValue tcg_default_props[] = {
144f5cc5a5cSClaudio Fontana     { "vme", "off" },
145f5cc5a5cSClaudio Fontana     { NULL, NULL },
146f5cc5a5cSClaudio Fontana };
147f5cc5a5cSClaudio Fontana 
148f5cc5a5cSClaudio Fontana static void tcg_cpu_instance_init(CPUState *cs)
149f5cc5a5cSClaudio Fontana {
150f5cc5a5cSClaudio Fontana     X86CPU *cpu = X86_CPU(cs);
1515b8978d8SClaudio Fontana     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1525b8978d8SClaudio Fontana 
1535b8978d8SClaudio Fontana     if (xcc->model) {
154f5cc5a5cSClaudio Fontana         /* Special cases not set in the X86CPUDefinition structs: */
155f5cc5a5cSClaudio Fontana         x86_cpu_apply_props(cpu, tcg_default_props);
1565b8978d8SClaudio Fontana     }
157fea45008SDavid Edmondson 
158fea45008SDavid Edmondson     tcg_cpu_xsave_init();
159f5cc5a5cSClaudio Fontana }
160f5cc5a5cSClaudio Fontana 
161f5cc5a5cSClaudio Fontana static void tcg_cpu_accel_class_init(ObjectClass *oc, void *data)
162f5cc5a5cSClaudio Fontana {
163f5cc5a5cSClaudio Fontana     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
164f5cc5a5cSClaudio Fontana 
165222f3e6fSPaolo Bonzini #ifndef CONFIG_USER_ONLY
166f5cc5a5cSClaudio Fontana     acc->cpu_realizefn = tcg_cpu_realizefn;
167222f3e6fSPaolo Bonzini #endif /* CONFIG_USER_ONLY */
168222f3e6fSPaolo Bonzini 
169f5cc5a5cSClaudio Fontana     acc->cpu_class_init = tcg_cpu_class_init;
170f5cc5a5cSClaudio Fontana     acc->cpu_instance_init = tcg_cpu_instance_init;
171f5cc5a5cSClaudio Fontana }
172f5cc5a5cSClaudio Fontana static const TypeInfo tcg_cpu_accel_type_info = {
173f5cc5a5cSClaudio Fontana     .name = ACCEL_CPU_NAME("tcg"),
174f5cc5a5cSClaudio Fontana 
175f5cc5a5cSClaudio Fontana     .parent = TYPE_ACCEL_CPU,
176f5cc5a5cSClaudio Fontana     .class_init = tcg_cpu_accel_class_init,
177f5cc5a5cSClaudio Fontana     .abstract = true,
178f5cc5a5cSClaudio Fontana };
179f5cc5a5cSClaudio Fontana static void tcg_cpu_accel_register_types(void)
180f5cc5a5cSClaudio Fontana {
181f5cc5a5cSClaudio Fontana     type_register_static(&tcg_cpu_accel_type_info);
182f5cc5a5cSClaudio Fontana }
183f5cc5a5cSClaudio Fontana type_init(tcg_cpu_accel_register_types);
184