xref: /qemu/target/sh4/cpu.c (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
1 /*
2  * QEMU SuperH CPU
3  *
4  * Copyright (c) 2005 Samuel Tardieu
5  * Copyright (c) 2012 SUSE LINUX Products GmbH
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see
19  * <http://www.gnu.org/licenses/lgpl-2.1.html>
20  */
21 
22 #include "qemu/osdep.h"
23 #include "qapi/error.h"
24 #include "qemu/qemu-print.h"
25 #include "cpu.h"
26 #include "migration/vmstate.h"
27 #include "exec/exec-all.h"
28 #include "exec/translation-block.h"
29 #include "fpu/softfloat-helpers.h"
30 #include "tcg/tcg.h"
31 
32 static void superh_cpu_set_pc(CPUState *cs, vaddr value)
33 {
34     SuperHCPU *cpu = SUPERH_CPU(cs);
35 
36     cpu->env.pc = value;
37 }
38 
39 static vaddr superh_cpu_get_pc(CPUState *cs)
40 {
41     SuperHCPU *cpu = SUPERH_CPU(cs);
42 
43     return cpu->env.pc;
44 }
45 
46 static void superh_cpu_synchronize_from_tb(CPUState *cs,
47                                            const TranslationBlock *tb)
48 {
49     SuperHCPU *cpu = SUPERH_CPU(cs);
50 
51     tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL));
52     cpu->env.pc = tb->pc;
53     cpu->env.flags = tb->flags & TB_FLAG_ENVFLAGS_MASK;
54 }
55 
56 static void superh_restore_state_to_opc(CPUState *cs,
57                                         const TranslationBlock *tb,
58                                         const uint64_t *data)
59 {
60     SuperHCPU *cpu = SUPERH_CPU(cs);
61 
62     cpu->env.pc = data[0];
63     cpu->env.flags = data[1];
64     /*
65      * Theoretically delayed_pc should also be restored. In practice the
66      * branch instruction is re-executed after exception, so the delayed
67      * branch target will be recomputed.
68      */
69 }
70 
71 #ifndef CONFIG_USER_ONLY
72 static bool superh_io_recompile_replay_branch(CPUState *cs,
73                                               const TranslationBlock *tb)
74 {
75     CPUSH4State *env = cpu_env(cs);
76 
77     if ((env->flags & (TB_FLAG_DELAY_SLOT | TB_FLAG_DELAY_SLOT_COND))
78         && !tcg_cflags_has(cs, CF_PCREL) && env->pc != tb->pc) {
79         env->pc -= 2;
80         env->flags &= ~(TB_FLAG_DELAY_SLOT | TB_FLAG_DELAY_SLOT_COND);
81         return true;
82     }
83     return false;
84 }
85 #endif
86 
87 static bool superh_cpu_has_work(CPUState *cs)
88 {
89     return cs->interrupt_request & CPU_INTERRUPT_HARD;
90 }
91 
92 static int sh4_cpu_mmu_index(CPUState *cs, bool ifetch)
93 {
94     CPUSH4State *env = cpu_env(cs);
95 
96     /*
97      * The instruction in a RTE delay slot is fetched in privileged mode,
98      * but executed in user mode.
99      */
100     if (ifetch && (env->flags & TB_FLAG_DELAY_SLOT_RTE)) {
101         return 0;
102     } else {
103         return (env->sr & (1u << SR_MD)) == 0 ? 1 : 0;
104     }
105 }
106 
107 static void superh_cpu_reset_hold(Object *obj, ResetType type)
108 {
109     CPUState *cs = CPU(obj);
110     SuperHCPUClass *scc = SUPERH_CPU_GET_CLASS(obj);
111     CPUSH4State *env = cpu_env(cs);
112 
113     if (scc->parent_phases.hold) {
114         scc->parent_phases.hold(obj, type);
115     }
116 
117     memset(env, 0, offsetof(CPUSH4State, end_reset_fields));
118 
119     env->pc = 0xA0000000;
120 #if defined(CONFIG_USER_ONLY)
121     env->fpscr = FPSCR_PR; /* value for userspace according to the kernel */
122     set_float_rounding_mode(float_round_nearest_even, &env->fp_status); /* ?! */
123 #else
124     env->sr = (1u << SR_MD) | (1u << SR_RB) | (1u << SR_BL) |
125               (1u << SR_I3) | (1u << SR_I2) | (1u << SR_I1) | (1u << SR_I0);
126     env->fpscr = FPSCR_DN | FPSCR_RM_ZERO; /* CPU reset value according to SH4 manual */
127     set_float_rounding_mode(float_round_to_zero, &env->fp_status);
128     set_flush_to_zero(1, &env->fp_status);
129 #endif
130     set_default_nan_mode(1, &env->fp_status);
131     /* sign bit clear, set all frac bits other than msb */
132     set_float_default_nan_pattern(0b00111111, &env->fp_status);
133     /*
134      * TODO: "SH-4 CPU Core Architecture ADCS 7182230F" doesn't say whether
135      * it detects tininess before or after rounding. Section 6.4 is clear
136      * that flush-to-zero happens when the result underflows, though, so
137      * either this should be "detect ftz after rounding" or else we should
138      * be setting "detect tininess before rounding".
139      */
140     set_float_ftz_detection(float_ftz_before_rounding, &env->fp_status);
141 }
142 
143 static void superh_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
144 {
145     info->mach = bfd_mach_sh4;
146     info->print_insn = print_insn_sh;
147 }
148 
149 static ObjectClass *superh_cpu_class_by_name(const char *cpu_model)
150 {
151     ObjectClass *oc;
152     char *s, *typename = NULL;
153 
154     s = g_ascii_strdown(cpu_model, -1);
155     if (strcmp(s, "any") == 0) {
156         oc = object_class_by_name(TYPE_SH7750R_CPU);
157         goto out;
158     }
159 
160     typename = g_strdup_printf(SUPERH_CPU_TYPE_NAME("%s"), s);
161     oc = object_class_by_name(typename);
162 
163 out:
164     g_free(s);
165     g_free(typename);
166     return oc;
167 }
168 
169 static void sh7750r_cpu_initfn(Object *obj)
170 {
171     CPUSH4State *env = cpu_env(CPU(obj));
172 
173     env->id = SH_CPU_SH7750R;
174     env->features = SH_FEATURE_BCR3_AND_BCR4;
175 }
176 
177 static void sh7750r_class_init(ObjectClass *oc, void *data)
178 {
179     SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
180 
181     scc->pvr = 0x00050000;
182     scc->prr = 0x00000100;
183     scc->cvr = 0x00110000;
184 }
185 
186 static void sh7751r_cpu_initfn(Object *obj)
187 {
188     CPUSH4State *env = cpu_env(CPU(obj));
189 
190     env->id = SH_CPU_SH7751R;
191     env->features = SH_FEATURE_BCR3_AND_BCR4;
192 }
193 
194 static void sh7751r_class_init(ObjectClass *oc, void *data)
195 {
196     SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
197 
198     scc->pvr = 0x04050005;
199     scc->prr = 0x00000113;
200     scc->cvr = 0x00110000; /* Neutered caches, should be 0x20480000 */
201 }
202 
203 static void sh7785_cpu_initfn(Object *obj)
204 {
205     CPUSH4State *env = cpu_env(CPU(obj));
206 
207     env->id = SH_CPU_SH7785;
208     env->features = SH_FEATURE_SH4A;
209 }
210 
211 static void sh7785_class_init(ObjectClass *oc, void *data)
212 {
213     SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
214 
215     scc->pvr = 0x10300700;
216     scc->prr = 0x00000200;
217     scc->cvr = 0x71440211;
218 }
219 
220 static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
221 {
222     CPUState *cs = CPU(dev);
223     SuperHCPUClass *scc = SUPERH_CPU_GET_CLASS(dev);
224     Error *local_err = NULL;
225 
226     cpu_exec_realizefn(cs, &local_err);
227     if (local_err != NULL) {
228         error_propagate(errp, local_err);
229         return;
230     }
231 
232     cpu_reset(cs);
233     qemu_init_vcpu(cs);
234 
235     scc->parent_realize(dev, errp);
236 }
237 
238 static void superh_cpu_initfn(Object *obj)
239 {
240     CPUSH4State *env = cpu_env(CPU(obj));
241 
242     env->movcal_backup_tail = &(env->movcal_backup);
243 }
244 
245 #ifndef CONFIG_USER_ONLY
246 static const VMStateDescription vmstate_sh_cpu = {
247     .name = "cpu",
248     .unmigratable = 1,
249 };
250 
251 #include "hw/core/sysemu-cpu-ops.h"
252 
253 static const struct SysemuCPUOps sh4_sysemu_ops = {
254     .get_phys_page_debug = superh_cpu_get_phys_page_debug,
255 };
256 #endif
257 
258 #include "hw/core/tcg-cpu-ops.h"
259 
260 static const TCGCPUOps superh_tcg_ops = {
261     .initialize = sh4_translate_init,
262     .translate_code = sh4_translate_code,
263     .synchronize_from_tb = superh_cpu_synchronize_from_tb,
264     .restore_state_to_opc = superh_restore_state_to_opc,
265 
266 #ifndef CONFIG_USER_ONLY
267     .tlb_fill = superh_cpu_tlb_fill,
268     .cpu_exec_interrupt = superh_cpu_exec_interrupt,
269     .cpu_exec_halt = superh_cpu_has_work,
270     .do_interrupt = superh_cpu_do_interrupt,
271     .do_unaligned_access = superh_cpu_do_unaligned_access,
272     .io_recompile_replay_branch = superh_io_recompile_replay_branch,
273 #endif /* !CONFIG_USER_ONLY */
274 };
275 
276 static void superh_cpu_class_init(ObjectClass *oc, void *data)
277 {
278     DeviceClass *dc = DEVICE_CLASS(oc);
279     CPUClass *cc = CPU_CLASS(oc);
280     SuperHCPUClass *scc = SUPERH_CPU_CLASS(oc);
281     ResettableClass *rc = RESETTABLE_CLASS(oc);
282 
283     device_class_set_parent_realize(dc, superh_cpu_realizefn,
284                                     &scc->parent_realize);
285 
286     resettable_class_set_parent_phases(rc, NULL, superh_cpu_reset_hold, NULL,
287                                        &scc->parent_phases);
288 
289     cc->class_by_name = superh_cpu_class_by_name;
290     cc->has_work = superh_cpu_has_work;
291     cc->mmu_index = sh4_cpu_mmu_index;
292     cc->dump_state = superh_cpu_dump_state;
293     cc->set_pc = superh_cpu_set_pc;
294     cc->get_pc = superh_cpu_get_pc;
295     cc->gdb_read_register = superh_cpu_gdb_read_register;
296     cc->gdb_write_register = superh_cpu_gdb_write_register;
297 #ifndef CONFIG_USER_ONLY
298     cc->sysemu_ops = &sh4_sysemu_ops;
299     dc->vmsd = &vmstate_sh_cpu;
300 #endif
301     cc->disas_set_info = superh_cpu_disas_set_info;
302 
303     cc->gdb_num_core_regs = 59;
304     cc->tcg_ops = &superh_tcg_ops;
305 }
306 
307 #define DEFINE_SUPERH_CPU_TYPE(type_name, cinit, initfn) \
308     {                                                    \
309         .name = type_name,                               \
310         .parent = TYPE_SUPERH_CPU,                       \
311         .class_init = cinit,                             \
312         .instance_init = initfn,                         \
313     }
314 static const TypeInfo superh_cpu_type_infos[] = {
315     {
316         .name = TYPE_SUPERH_CPU,
317         .parent = TYPE_CPU,
318         .instance_size = sizeof(SuperHCPU),
319         .instance_align = __alignof(SuperHCPU),
320         .instance_init = superh_cpu_initfn,
321         .abstract = true,
322         .class_size = sizeof(SuperHCPUClass),
323         .class_init = superh_cpu_class_init,
324     },
325     DEFINE_SUPERH_CPU_TYPE(TYPE_SH7750R_CPU, sh7750r_class_init,
326                            sh7750r_cpu_initfn),
327     DEFINE_SUPERH_CPU_TYPE(TYPE_SH7751R_CPU, sh7751r_class_init,
328                            sh7751r_cpu_initfn),
329     DEFINE_SUPERH_CPU_TYPE(TYPE_SH7785_CPU, sh7785_class_init,
330                            sh7785_cpu_initfn),
331 
332 };
333 
334 DEFINE_TYPES(superh_cpu_type_infos)
335