xref: /qemu/target/xtensa/cpu.c (revision b6aeb8d243c5ab8b914b55f0036e8289a99322c8)
1 /*
2  * QEMU Xtensa CPU
3  *
4  * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
5  * Copyright (c) 2012 SUSE LINUX Products GmbH
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in the
14  *       documentation and/or other materials provided with the distribution.
15  *     * Neither the name of the Open Source and Linux Lab nor the
16  *       names of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "qemu/osdep.h"
32 #include "qapi/error.h"
33 #include "cpu.h"
34 #include "fpu/softfloat.h"
35 #include "qemu/module.h"
36 #include "migration/vmstate.h"
37 #include "hw/qdev-clock.h"
38 #include "accel/tcg/cpu-ops.h"
39 #ifndef CONFIG_USER_ONLY
40 #include "system/memory.h"
41 #endif
42 
43 
44 static void xtensa_cpu_set_pc(CPUState *cs, vaddr value)
45 {
46     XtensaCPU *cpu = XTENSA_CPU(cs);
47 
48     cpu->env.pc = value;
49 }
50 
51 static vaddr xtensa_cpu_get_pc(CPUState *cs)
52 {
53     XtensaCPU *cpu = XTENSA_CPU(cs);
54 
55     return cpu->env.pc;
56 }
57 
58 void cpu_get_tb_cpu_state(CPUXtensaState *env, vaddr *pc,
59                           uint64_t *cs_base, uint32_t *flags)
60 {
61     *pc = env->pc;
62     *cs_base = 0;
63     *flags = 0;
64     *flags |= xtensa_get_ring(env);
65     if (env->sregs[PS] & PS_EXCM) {
66         *flags |= XTENSA_TBFLAG_EXCM;
67     } else if (xtensa_option_enabled(env->config, XTENSA_OPTION_LOOP)) {
68         target_ulong lend_dist =
69             env->sregs[LEND] - (env->pc & -(1u << TARGET_PAGE_BITS));
70 
71         /*
72          * 0 in the csbase_lend field means that there may not be a loopback
73          * for any instruction that starts inside this page. Any other value
74          * means that an instruction that ends at this offset from the page
75          * start may loop back and will need loopback code to be generated.
76          *
77          * lend_dist is 0 when LEND points to the start of the page, but
78          * no instruction that starts inside this page may end at offset 0,
79          * so it's still correct.
80          *
81          * When an instruction ends at a page boundary it may only start in
82          * the previous page. lend_dist will be encoded as TARGET_PAGE_SIZE
83          * for the TB that contains this instruction.
84          */
85         if (lend_dist < (1u << TARGET_PAGE_BITS) + env->config->max_insn_size) {
86             target_ulong lbeg_off = env->sregs[LEND] - env->sregs[LBEG];
87 
88             *cs_base = lend_dist;
89             if (lbeg_off < 256) {
90                 *cs_base |= lbeg_off << XTENSA_CSBASE_LBEG_OFF_SHIFT;
91             }
92         }
93     }
94     if (xtensa_option_enabled(env->config, XTENSA_OPTION_EXTENDED_L32R) &&
95             (env->sregs[LITBASE] & 1)) {
96         *flags |= XTENSA_TBFLAG_LITBASE;
97     }
98     if (xtensa_option_enabled(env->config, XTENSA_OPTION_DEBUG)) {
99         if (xtensa_get_cintlevel(env) < env->config->debug_level) {
100             *flags |= XTENSA_TBFLAG_DEBUG;
101         }
102         if (xtensa_get_cintlevel(env) < env->sregs[ICOUNTLEVEL]) {
103             *flags |= XTENSA_TBFLAG_ICOUNT;
104         }
105     }
106     if (xtensa_option_enabled(env->config, XTENSA_OPTION_COPROCESSOR)) {
107         *flags |= env->sregs[CPENABLE] << XTENSA_TBFLAG_CPENABLE_SHIFT;
108     }
109     if (xtensa_option_enabled(env->config, XTENSA_OPTION_WINDOWED_REGISTER) &&
110         (env->sregs[PS] & (PS_WOE | PS_EXCM)) == PS_WOE) {
111         uint32_t windowstart = xtensa_replicate_windowstart(env) >>
112             (env->sregs[WINDOW_BASE] + 1);
113         uint32_t w = ctz32(windowstart | 0x8);
114 
115         *flags |= (w << XTENSA_TBFLAG_WINDOW_SHIFT) | XTENSA_TBFLAG_CWOE;
116         *flags |= extract32(env->sregs[PS], PS_CALLINC_SHIFT,
117                             PS_CALLINC_LEN) << XTENSA_TBFLAG_CALLINC_SHIFT;
118     } else {
119         *flags |= 3 << XTENSA_TBFLAG_WINDOW_SHIFT;
120     }
121     if (env->yield_needed) {
122         *flags |= XTENSA_TBFLAG_YIELD;
123     }
124 }
125 
126 static void xtensa_restore_state_to_opc(CPUState *cs,
127                                         const TranslationBlock *tb,
128                                         const uint64_t *data)
129 {
130     XtensaCPU *cpu = XTENSA_CPU(cs);
131 
132     cpu->env.pc = data[0];
133 }
134 
135 #ifndef CONFIG_USER_ONLY
136 static bool xtensa_cpu_has_work(CPUState *cs)
137 {
138     CPUXtensaState *env = cpu_env(cs);
139 
140     return !env->runstall && env->pending_irq_level;
141 }
142 #endif /* !CONFIG_USER_ONLY */
143 
144 static int xtensa_cpu_mmu_index(CPUState *cs, bool ifetch)
145 {
146     return xtensa_get_cring(cpu_env(cs));
147 }
148 
149 #ifdef CONFIG_USER_ONLY
150 static bool abi_call0;
151 
152 void xtensa_set_abi_call0(void)
153 {
154     abi_call0 = true;
155 }
156 
157 bool xtensa_abi_call0(void)
158 {
159     return abi_call0;
160 }
161 #endif
162 
163 static void xtensa_cpu_reset_hold(Object *obj, ResetType type)
164 {
165     CPUState *cs = CPU(obj);
166     XtensaCPUClass *xcc = XTENSA_CPU_GET_CLASS(obj);
167     CPUXtensaState *env = cpu_env(cs);
168     bool dfpu = xtensa_option_enabled(env->config,
169                                       XTENSA_OPTION_DFP_COPROCESSOR);
170 
171     if (xcc->parent_phases.hold) {
172         xcc->parent_phases.hold(obj, type);
173     }
174 
175     env->pc = env->config->exception_vector[EXC_RESET0 + env->static_vectors];
176     env->sregs[LITBASE] &= ~1;
177 #ifndef CONFIG_USER_ONLY
178     env->sregs[PS] = xtensa_option_enabled(env->config,
179             XTENSA_OPTION_INTERRUPT) ? 0x1f : 0x10;
180     env->pending_irq_level = 0;
181 #else
182     env->sregs[PS] = PS_UM | (3 << PS_RING_SHIFT);
183     if (xtensa_option_enabled(env->config,
184                               XTENSA_OPTION_WINDOWED_REGISTER) &&
185         !xtensa_abi_call0()) {
186         env->sregs[PS] |= PS_WOE;
187     }
188     env->sregs[CPENABLE] = 0xff;
189 #endif
190     env->sregs[VECBASE] = env->config->vecbase;
191     env->sregs[IBREAKENABLE] = 0;
192     env->sregs[MEMCTL] = MEMCTL_IL0EN & env->config->memctl_mask;
193     env->sregs[ATOMCTL] = xtensa_option_enabled(env->config,
194             XTENSA_OPTION_ATOMCTL) ? 0x28 : 0x15;
195     env->sregs[CONFIGID0] = env->config->configid[0];
196     env->sregs[CONFIGID1] = env->config->configid[1];
197     env->exclusive_addr = -1;
198 
199 #ifndef CONFIG_USER_ONLY
200     reset_mmu(env);
201     cs->halted = env->runstall;
202 #endif
203     /* For inf * 0 + NaN, return the input NaN */
204     set_float_infzeronan_rule(float_infzeronan_dnan_never, &env->fp_status);
205     set_no_signaling_nans(!dfpu, &env->fp_status);
206     /* Default NaN value: sign bit clear, set frac msb */
207     set_float_default_nan_pattern(0b01000000, &env->fp_status);
208     xtensa_use_first_nan(env, !dfpu);
209 }
210 
211 static ObjectClass *xtensa_cpu_class_by_name(const char *cpu_model)
212 {
213     ObjectClass *oc;
214     char *typename;
215 
216     typename = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), cpu_model);
217     oc = object_class_by_name(typename);
218     g_free(typename);
219 
220     return oc;
221 }
222 
223 static void xtensa_cpu_disas_set_info(CPUState *cs, disassemble_info *info)
224 {
225     XtensaCPU *cpu = XTENSA_CPU(cs);
226 
227     info->private_data = cpu->env.config->isa;
228     info->print_insn = print_insn_xtensa;
229     info->endian = TARGET_BIG_ENDIAN ? BFD_ENDIAN_BIG
230                                      : BFD_ENDIAN_LITTLE;
231 }
232 
233 static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
234 {
235     CPUState *cs = CPU(dev);
236     XtensaCPUClass *xcc = XTENSA_CPU_GET_CLASS(dev);
237     Error *local_err = NULL;
238 
239 #ifndef CONFIG_USER_ONLY
240     xtensa_irq_init(&XTENSA_CPU(dev)->env);
241 #endif
242 
243     cpu_exec_realizefn(cs, &local_err);
244     if (local_err != NULL) {
245         error_propagate(errp, local_err);
246         return;
247     }
248 
249     cs->gdb_num_regs = xcc->config->gdb_regmap.num_regs;
250 
251     qemu_init_vcpu(cs);
252 
253     xcc->parent_realize(dev, errp);
254 }
255 
256 static void xtensa_cpu_initfn(Object *obj)
257 {
258     XtensaCPU *cpu = XTENSA_CPU(obj);
259     XtensaCPUClass *xcc = XTENSA_CPU_GET_CLASS(obj);
260     CPUXtensaState *env = &cpu->env;
261 
262     env->config = xcc->config;
263 
264 #ifndef CONFIG_USER_ONLY
265     env->address_space_er = g_malloc(sizeof(*env->address_space_er));
266     env->system_er = g_malloc(sizeof(*env->system_er));
267     memory_region_init_io(env->system_er, obj, NULL, env, "er",
268                           UINT64_C(0x100000000));
269     address_space_init(env->address_space_er, env->system_er, "ER");
270 
271     cpu->clock = qdev_init_clock_in(DEVICE(obj), "clk-in", NULL, cpu, 0);
272     clock_set_hz(cpu->clock, env->config->clock_freq_khz * 1000);
273 #endif
274 }
275 
276 XtensaCPU *xtensa_cpu_create_with_clock(const char *cpu_type, Clock *cpu_refclk)
277 {
278     DeviceState *cpu;
279 
280     cpu = qdev_new(cpu_type);
281     qdev_connect_clock_in(cpu, "clk-in", cpu_refclk);
282     qdev_realize(cpu, NULL, &error_abort);
283 
284     return XTENSA_CPU(cpu);
285 }
286 
287 #ifndef CONFIG_USER_ONLY
288 static const VMStateDescription vmstate_xtensa_cpu = {
289     .name = "cpu",
290     .unmigratable = 1,
291 };
292 
293 #include "hw/core/sysemu-cpu-ops.h"
294 
295 static const struct SysemuCPUOps xtensa_sysemu_ops = {
296     .has_work = xtensa_cpu_has_work,
297     .get_phys_page_debug = xtensa_cpu_get_phys_page_debug,
298 };
299 #endif
300 
301 static const TCGCPUOps xtensa_tcg_ops = {
302     /* Xtensa processors have a weak memory model */
303     .guest_default_memory_order = 0,
304     .mttcg_supported = true,
305 
306     .initialize = xtensa_translate_init,
307     .translate_code = xtensa_translate_code,
308     .debug_excp_handler = xtensa_breakpoint_handler,
309     .restore_state_to_opc = xtensa_restore_state_to_opc,
310     .mmu_index = xtensa_cpu_mmu_index,
311 
312 #ifndef CONFIG_USER_ONLY
313     .tlb_fill = xtensa_cpu_tlb_fill,
314     .cpu_exec_interrupt = xtensa_cpu_exec_interrupt,
315     .cpu_exec_halt = xtensa_cpu_has_work,
316     .cpu_exec_reset = cpu_reset,
317     .do_interrupt = xtensa_cpu_do_interrupt,
318     .do_transaction_failed = xtensa_cpu_do_transaction_failed,
319     .do_unaligned_access = xtensa_cpu_do_unaligned_access,
320     .debug_check_breakpoint = xtensa_debug_check_breakpoint,
321 #endif /* !CONFIG_USER_ONLY */
322 };
323 
324 static void xtensa_cpu_class_init(ObjectClass *oc, const void *data)
325 {
326     DeviceClass *dc = DEVICE_CLASS(oc);
327     CPUClass *cc = CPU_CLASS(oc);
328     XtensaCPUClass *xcc = XTENSA_CPU_CLASS(cc);
329     ResettableClass *rc = RESETTABLE_CLASS(oc);
330 
331     device_class_set_parent_realize(dc, xtensa_cpu_realizefn,
332                                     &xcc->parent_realize);
333 
334     resettable_class_set_parent_phases(rc, NULL, xtensa_cpu_reset_hold, NULL,
335                                        &xcc->parent_phases);
336 
337     cc->class_by_name = xtensa_cpu_class_by_name;
338     cc->dump_state = xtensa_cpu_dump_state;
339     cc->set_pc = xtensa_cpu_set_pc;
340     cc->get_pc = xtensa_cpu_get_pc;
341     cc->gdb_read_register = xtensa_cpu_gdb_read_register;
342     cc->gdb_write_register = xtensa_cpu_gdb_write_register;
343     cc->gdb_stop_before_watchpoint = true;
344 #ifndef CONFIG_USER_ONLY
345     cc->sysemu_ops = &xtensa_sysemu_ops;
346     dc->vmsd = &vmstate_xtensa_cpu;
347 #endif
348     cc->disas_set_info = xtensa_cpu_disas_set_info;
349     cc->tcg_ops = &xtensa_tcg_ops;
350 }
351 
352 static const TypeInfo xtensa_cpu_type_info = {
353     .name = TYPE_XTENSA_CPU,
354     .parent = TYPE_CPU,
355     .instance_size = sizeof(XtensaCPU),
356     .instance_align = __alignof(XtensaCPU),
357     .instance_init = xtensa_cpu_initfn,
358     .abstract = true,
359     .class_size = sizeof(XtensaCPUClass),
360     .class_init = xtensa_cpu_class_init,
361 };
362 
363 static void xtensa_cpu_register_types(void)
364 {
365     type_register_static(&xtensa_cpu_type_info);
366 }
367 
368 type_init(xtensa_cpu_register_types)
369