xref: /qemu/target/i386/tcg/tcg-cpu.c (revision 9181ab452893a3f45cdc0f6196fbb9e389a4e5cd)
1 /*
2  * i386 TCG cpu class initialization
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "helper-tcg.h"
23 #include "qemu/accel.h"
24 #include "accel/accel-cpu-target.h"
25 #include "exec/translation-block.h"
26 #include "exec/target_page.h"
27 #include "tcg-cpu.h"
28 
29 /* Frob eflags into and out of the CPU temporary format.  */
30 
31 static void x86_cpu_exec_enter(CPUState *cs)
32 {
33     X86CPU *cpu = X86_CPU(cs);
34     CPUX86State *env = &cpu->env;
35 
36     CC_SRC = env->eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
37     env->df = 1 - (2 * ((env->eflags >> 10) & 1));
38     CC_OP = CC_OP_EFLAGS;
39     env->eflags &= ~(DF_MASK | CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
40 }
41 
42 static void x86_cpu_exec_exit(CPUState *cs)
43 {
44     X86CPU *cpu = X86_CPU(cs);
45     CPUX86State *env = &cpu->env;
46 
47     env->eflags = cpu_compute_eflags(env);
48 }
49 
50 static void x86_cpu_synchronize_from_tb(CPUState *cs,
51                                         const TranslationBlock *tb)
52 {
53     /* The instruction pointer is always up to date with CF_PCREL. */
54     if (!(tb_cflags(tb) & CF_PCREL)) {
55         CPUX86State *env = cpu_env(cs);
56 
57         if (tb->flags & HF_CS64_MASK) {
58             env->eip = tb->pc;
59         } else {
60             env->eip = (uint32_t)(tb->pc - tb->cs_base);
61         }
62     }
63 }
64 
65 static void x86_restore_state_to_opc(CPUState *cs,
66                                      const TranslationBlock *tb,
67                                      const uint64_t *data)
68 {
69     X86CPU *cpu = X86_CPU(cs);
70     CPUX86State *env = &cpu->env;
71     int cc_op = data[1];
72     uint64_t new_pc;
73 
74     if (tb_cflags(tb) & CF_PCREL) {
75         /*
76          * data[0] in PC-relative TBs is also a linear address, i.e. an address with
77          * the CS base added, because it is not guaranteed that EIP bits 12 and higher
78          * stay the same across the translation block.  Add the CS base back before
79          * replacing the low bits, and subtract it below just like for !CF_PCREL.
80          */
81         uint64_t pc = env->eip + tb->cs_base;
82         new_pc = (pc & TARGET_PAGE_MASK) | data[0];
83     } else {
84         new_pc = data[0];
85     }
86     if (tb->flags & HF_CS64_MASK) {
87         env->eip = new_pc;
88     } else {
89         env->eip = (uint32_t)(new_pc - tb->cs_base);
90     }
91 
92     if (cc_op != CC_OP_DYNAMIC) {
93         env->cc_op = cc_op;
94     }
95 }
96 
97 int x86_mmu_index_pl(CPUX86State *env, unsigned pl)
98 {
99     int mmu_index_32 = (env->hflags & HF_CS64_MASK) ? 0 : 1;
100     int mmu_index_base =
101         pl == 3 ? MMU_USER64_IDX :
102         !(env->hflags & HF_SMAP_MASK) ? MMU_KNOSMAP64_IDX :
103         (env->eflags & AC_MASK) ? MMU_KNOSMAP64_IDX : MMU_KSMAP64_IDX;
104 
105     return mmu_index_base + mmu_index_32;
106 }
107 
108 static int x86_cpu_mmu_index(CPUState *cs, bool ifetch)
109 {
110     CPUX86State *env = cpu_env(cs);
111     return x86_mmu_index_pl(env, env->hflags & HF_CPL_MASK);
112 }
113 
114 #ifndef CONFIG_USER_ONLY
115 static bool x86_debug_check_breakpoint(CPUState *cs)
116 {
117     X86CPU *cpu = X86_CPU(cs);
118     CPUX86State *env = &cpu->env;
119 
120     /* RF disables all architectural breakpoints. */
121     return !(env->eflags & RF_MASK);
122 }
123 #endif
124 
125 #include "accel/tcg/cpu-ops.h"
126 
127 const TCGCPUOps x86_tcg_ops = {
128     .mttcg_supported = true,
129     .precise_smc = true,
130     /*
131      * The x86 has a strong memory model with some store-after-load re-ordering
132      */
133     .guest_default_memory_order = TCG_MO_ALL & ~TCG_MO_ST_LD,
134     .initialize = tcg_x86_init,
135     .translate_code = x86_translate_code,
136     .synchronize_from_tb = x86_cpu_synchronize_from_tb,
137     .restore_state_to_opc = x86_restore_state_to_opc,
138     .mmu_index = x86_cpu_mmu_index,
139     .cpu_exec_enter = x86_cpu_exec_enter,
140     .cpu_exec_exit = x86_cpu_exec_exit,
141 #ifdef CONFIG_USER_ONLY
142     .fake_user_interrupt = x86_cpu_do_interrupt,
143     .record_sigsegv = x86_cpu_record_sigsegv,
144     .record_sigbus = x86_cpu_record_sigbus,
145 #else
146     .tlb_fill = x86_cpu_tlb_fill,
147     .do_interrupt = x86_cpu_do_interrupt,
148     .cpu_exec_halt = x86_cpu_exec_halt,
149     .cpu_exec_interrupt = x86_cpu_exec_interrupt,
150     .cpu_exec_reset = cpu_reset,
151     .do_unaligned_access = x86_cpu_do_unaligned_access,
152     .debug_excp_handler = breakpoint_handler,
153     .debug_check_breakpoint = x86_debug_check_breakpoint,
154     .need_replay_interrupt = x86_need_replay_interrupt,
155 #endif /* !CONFIG_USER_ONLY */
156 };
157 
158 static void x86_tcg_cpu_xsave_init(void)
159 {
160 #define XO(bit, field) \
161     x86_ext_save_areas[bit].offset = offsetof(X86XSaveArea, field);
162 
163     XO(XSTATE_FP_BIT, legacy);
164     XO(XSTATE_SSE_BIT, legacy);
165     XO(XSTATE_YMM_BIT, avx_state);
166     XO(XSTATE_BNDREGS_BIT, bndreg_state);
167     XO(XSTATE_BNDCSR_BIT, bndcsr_state);
168     XO(XSTATE_OPMASK_BIT, opmask_state);
169     XO(XSTATE_ZMM_Hi256_BIT, zmm_hi256_state);
170     XO(XSTATE_Hi16_ZMM_BIT, hi16_zmm_state);
171     XO(XSTATE_PKRU_BIT, pkru_state);
172 
173 #undef XO
174 }
175 
176 /*
177  * TCG-specific defaults that override cpudef models when using TCG.
178  * Only for builtin_x86_defs models initialized with x86_register_cpudef_types.
179  */
180 static PropValue x86_tcg_default_props[] = {
181     { "vme", "off" },
182     { NULL, NULL },
183 };
184 
185 static void x86_tcg_cpu_instance_init(CPUState *cs)
186 {
187     X86CPU *cpu = X86_CPU(cs);
188     X86CPUClass *xcc = X86_CPU_GET_CLASS(cpu);
189 
190     if (xcc->model) {
191         /* Special cases not set in the X86CPUDefinition structs: */
192         x86_cpu_apply_props(cpu, x86_tcg_default_props);
193     }
194 
195     x86_tcg_cpu_xsave_init();
196 }
197 
198 static void x86_tcg_cpu_accel_class_init(ObjectClass *oc, const void *data)
199 {
200     AccelCPUClass *acc = ACCEL_CPU_CLASS(oc);
201 
202 #ifndef CONFIG_USER_ONLY
203     acc->cpu_target_realize = tcg_cpu_realizefn;
204 #endif /* CONFIG_USER_ONLY */
205 
206     acc->cpu_instance_init = x86_tcg_cpu_instance_init;
207 }
208 static const TypeInfo x86_tcg_cpu_accel_type_info = {
209     .name = ACCEL_CPU_NAME("tcg"),
210 
211     .parent = TYPE_ACCEL_CPU,
212     .class_init = x86_tcg_cpu_accel_class_init,
213     .abstract = true,
214 };
215 static void x86_tcg_cpu_accel_register_types(void)
216 {
217     type_register_static(&x86_tcg_cpu_accel_type_info);
218 }
219 type_init(x86_tcg_cpu_accel_register_types);
220