xref: /openbmc/qemu/target/i386/tcg/tcg-cpu.c (revision 958e1dd1300f37f18b2161dfb4eb806fc8c19b44)
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 {
52ed69e831SClaudio Fontana     X86CPU *cpu = X86_CPU(cs);
53ed69e831SClaudio Fontana 
54ed69e831SClaudio Fontana     cpu->env.eip = tb->pc - tb->cs_base;
55ed69e831SClaudio Fontana }
56ed69e831SClaudio Fontana 
577b9810eaSRichard Henderson #ifndef CONFIG_USER_ONLY
587b9810eaSRichard Henderson static bool x86_debug_check_breakpoint(CPUState *cs)
597b9810eaSRichard Henderson {
607b9810eaSRichard Henderson     X86CPU *cpu = X86_CPU(cs);
617b9810eaSRichard Henderson     CPUX86State *env = &cpu->env;
627b9810eaSRichard Henderson 
637b9810eaSRichard Henderson     /* RF disables all architectural breakpoints. */
647b9810eaSRichard Henderson     return !(env->eflags & RF_MASK);
657b9810eaSRichard Henderson }
667b9810eaSRichard Henderson #endif
677b9810eaSRichard Henderson 
6878271684SClaudio Fontana #include "hw/core/tcg-cpu-ops.h"
6978271684SClaudio Fontana 
7011906557SRichard Henderson static const struct TCGCPUOps x86_tcg_ops = {
7178271684SClaudio Fontana     .initialize = tcg_x86_init,
7278271684SClaudio Fontana     .synchronize_from_tb = x86_cpu_synchronize_from_tb,
7378271684SClaudio Fontana     .cpu_exec_enter = x86_cpu_exec_enter,
7478271684SClaudio Fontana     .cpu_exec_exit = x86_cpu_exec_exit,
7512096421SPhilippe Mathieu-Daudé #ifdef CONFIG_USER_ONLY
7612096421SPhilippe Mathieu-Daudé     .fake_user_interrupt = x86_cpu_do_interrupt,
77f74bd157SRichard Henderson     .record_sigsegv = x86_cpu_record_sigsegv,
78*958e1dd1SPaolo Bonzini     .record_sigbus = x86_cpu_record_sigbus,
7912096421SPhilippe Mathieu-Daudé #else
80f74bd157SRichard Henderson     .tlb_fill = x86_cpu_tlb_fill,
8112096421SPhilippe Mathieu-Daudé     .do_interrupt = x86_cpu_do_interrupt,
8260466472SPhilippe Mathieu-Daudé     .cpu_exec_interrupt = x86_cpu_exec_interrupt,
83*958e1dd1SPaolo Bonzini     .do_unaligned_access = x86_cpu_do_unaligned_access,
8478271684SClaudio Fontana     .debug_excp_handler = breakpoint_handler,
857b9810eaSRichard Henderson     .debug_check_breakpoint = x86_debug_check_breakpoint,
8678271684SClaudio Fontana #endif /* !CONFIG_USER_ONLY */
8778271684SClaudio Fontana };
8878271684SClaudio Fontana 
89cc3f2be6SClaudio Fontana static void tcg_cpu_init_ops(AccelCPUClass *accel_cpu, CPUClass *cc)
90cc3f2be6SClaudio Fontana {
91cc3f2be6SClaudio Fontana     /* for x86, all cpus use the same set of operations */
92cc3f2be6SClaudio Fontana     cc->tcg_ops = &x86_tcg_ops;
93cc3f2be6SClaudio Fontana }
94cc3f2be6SClaudio Fontana 
95f5cc5a5cSClaudio Fontana static void tcg_cpu_class_init(CPUClass *cc)
96ed69e831SClaudio Fontana {
97cc3f2be6SClaudio Fontana     cc->init_accel_cpu = tcg_cpu_init_ops;
98ed69e831SClaudio Fontana }
99f5cc5a5cSClaudio Fontana 
100fea45008SDavid Edmondson static void tcg_cpu_xsave_init(void)
101fea45008SDavid Edmondson {
102fea45008SDavid Edmondson #define XO(bit, field) \
103fea45008SDavid Edmondson     x86_ext_save_areas[bit].offset = offsetof(X86XSaveArea, field);
104fea45008SDavid Edmondson 
105fea45008SDavid Edmondson     XO(XSTATE_FP_BIT, legacy);
106fea45008SDavid Edmondson     XO(XSTATE_SSE_BIT, legacy);
107fea45008SDavid Edmondson     XO(XSTATE_YMM_BIT, avx_state);
108fea45008SDavid Edmondson     XO(XSTATE_BNDREGS_BIT, bndreg_state);
109fea45008SDavid Edmondson     XO(XSTATE_BNDCSR_BIT, bndcsr_state);
110fea45008SDavid Edmondson     XO(XSTATE_OPMASK_BIT, opmask_state);
111fea45008SDavid Edmondson     XO(XSTATE_ZMM_Hi256_BIT, zmm_hi256_state);
112fea45008SDavid Edmondson     XO(XSTATE_Hi16_ZMM_BIT, hi16_zmm_state);
113fea45008SDavid Edmondson     XO(XSTATE_PKRU_BIT, pkru_state);
114fea45008SDavid Edmondson 
115fea45008SDavid Edmondson #undef XO
116fea45008SDavid Edmondson }
117fea45008SDavid Edmondson 
118f5cc5a5cSClaudio Fontana /*
1195b8978d8SClaudio Fontana  * TCG-specific defaults that override cpudef models when using TCG.
1205b8978d8SClaudio Fontana  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
121f5cc5a5cSClaudio Fontana  */
122f5cc5a5cSClaudio Fontana static PropValue tcg_default_props[] = {
123f5cc5a5cSClaudio Fontana     { "vme", "off" },
124f5cc5a5cSClaudio Fontana     { NULL, NULL },
125f5cc5a5cSClaudio Fontana };
126f5cc5a5cSClaudio Fontana 
127f5cc5a5cSClaudio Fontana static void tcg_cpu_instance_init(CPUState *cs)
128f5cc5a5cSClaudio Fontana {
129f5cc5a5cSClaudio Fontana     X86CPU *cpu = X86_CPU(cs);
1305b8978d8SClaudio Fontana     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
1315b8978d8SClaudio Fontana 
1325b8978d8SClaudio Fontana     if (xcc->model) {
133f5cc5a5cSClaudio Fontana         /* Special cases not set in the X86CPUDefinition structs: */
134f5cc5a5cSClaudio Fontana         x86_cpu_apply_props(cpu, tcg_default_props);
1355b8978d8SClaudio Fontana     }
136fea45008SDavid Edmondson 
137fea45008SDavid Edmondson     tcg_cpu_xsave_init();
138f5cc5a5cSClaudio Fontana }
139f5cc5a5cSClaudio Fontana 
140f5cc5a5cSClaudio Fontana static void tcg_cpu_accel_class_init(ObjectClass *oc, void *data)
141f5cc5a5cSClaudio Fontana {
142f5cc5a5cSClaudio Fontana     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
143f5cc5a5cSClaudio Fontana 
144222f3e6fSPaolo Bonzini #ifndef CONFIG_USER_ONLY
145f5cc5a5cSClaudio Fontana     acc->cpu_realizefn = tcg_cpu_realizefn;
146222f3e6fSPaolo Bonzini #endif /* CONFIG_USER_ONLY */
147222f3e6fSPaolo Bonzini 
148f5cc5a5cSClaudio Fontana     acc->cpu_class_init = tcg_cpu_class_init;
149f5cc5a5cSClaudio Fontana     acc->cpu_instance_init = tcg_cpu_instance_init;
150f5cc5a5cSClaudio Fontana }
151f5cc5a5cSClaudio Fontana static const TypeInfo tcg_cpu_accel_type_info = {
152f5cc5a5cSClaudio Fontana     .name = ACCEL_CPU_NAME("tcg"),
153f5cc5a5cSClaudio Fontana 
154f5cc5a5cSClaudio Fontana     .parent = TYPE_ACCEL_CPU,
155f5cc5a5cSClaudio Fontana     .class_init = tcg_cpu_accel_class_init,
156f5cc5a5cSClaudio Fontana     .abstract = true,
157f5cc5a5cSClaudio Fontana };
158f5cc5a5cSClaudio Fontana static void tcg_cpu_accel_register_types(void)
159f5cc5a5cSClaudio Fontana {
160f5cc5a5cSClaudio Fontana     type_register_static(&tcg_cpu_accel_type_info);
161f5cc5a5cSClaudio Fontana }
162f5cc5a5cSClaudio Fontana type_init(tcg_cpu_accel_register_types);
163