xref: /qemu/target/rx/cpu.c (revision 3d881164d4fb2b0f6791cf28d9725926b8ded0d6)
1 /*
2  * QEMU RX CPU
3  *
4  * Copyright (c) 2019 Yoshinori Sato
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2 or later, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include "qemu/qemu-print.h"
21 #include "qapi/error.h"
22 #include "cpu.h"
23 #include "migration/vmstate.h"
24 #include "exec/cputlb.h"
25 #include "exec/page-protection.h"
26 #include "exec/translation-block.h"
27 #include "exec/target_page.h"
28 #include "hw/loader.h"
29 #include "fpu/softfloat.h"
30 #include "tcg/debug-assert.h"
31 
32 static void rx_cpu_set_pc(CPUState *cs, vaddr value)
33 {
34     RXCPU *cpu = RX_CPU(cs);
35 
36     cpu->env.pc = value;
37 }
38 
39 static vaddr rx_cpu_get_pc(CPUState *cs)
40 {
41     RXCPU *cpu = RX_CPU(cs);
42 
43     return cpu->env.pc;
44 }
45 
46 static void rx_cpu_synchronize_from_tb(CPUState *cs,
47                                        const TranslationBlock *tb)
48 {
49     RXCPU *cpu = RX_CPU(cs);
50 
51     tcg_debug_assert(!tcg_cflags_has(cs, CF_PCREL));
52     cpu->env.pc = tb->pc;
53 }
54 
55 static void rx_restore_state_to_opc(CPUState *cs,
56                                     const TranslationBlock *tb,
57                                     const uint64_t *data)
58 {
59     RXCPU *cpu = RX_CPU(cs);
60 
61     cpu->env.pc = data[0];
62 }
63 
64 static bool rx_cpu_has_work(CPUState *cs)
65 {
66     return cs->interrupt_request &
67         (CPU_INTERRUPT_HARD | CPU_INTERRUPT_FIR);
68 }
69 
70 static int rx_cpu_mmu_index(CPUState *cs, bool ifunc)
71 {
72     return 0;
73 }
74 
75 static void rx_cpu_reset_hold(Object *obj, ResetType type)
76 {
77     CPUState *cs = CPU(obj);
78     RXCPUClass *rcc = RX_CPU_GET_CLASS(obj);
79     CPURXState *env = cpu_env(cs);
80     uint32_t *resetvec;
81 
82     if (rcc->parent_phases.hold) {
83         rcc->parent_phases.hold(obj, type);
84     }
85 
86     memset(env, 0, offsetof(CPURXState, end_reset_fields));
87 
88     resetvec = rom_ptr(0xfffffffc, 4);
89     if (resetvec) {
90         /* In the case of kernel, it is ignored because it is not set. */
91         env->pc = ldl_p(resetvec);
92     }
93     rx_cpu_unpack_psw(env, 0, 1);
94     env->regs[0] = env->isp = env->usp = 0;
95     env->fpsw = 0;
96     set_flush_to_zero(1, &env->fp_status);
97     set_flush_inputs_to_zero(1, &env->fp_status);
98     /*
99      * TODO: this is not the correct NaN propagation rule for this
100      * architecture. The "RX Family User's Manual: Software" table 1.6
101      * defines the propagation rules as "prefer SNaN over QNaN;
102      * then prefer dest over source", which is float_2nan_prop_s_ab.
103      */
104     set_float_2nan_prop_rule(float_2nan_prop_x87, &env->fp_status);
105     /* Default NaN value: sign bit clear, set frac msb */
106     set_float_default_nan_pattern(0b01000000, &env->fp_status);
107     /*
108      * TODO: "RX Family RXv1 Instruction Set Architecture" is not 100% clear
109      * on whether flush-to-zero should happen before or after rounding, but
110      * section 1.3.2 says that it happens when underflow is detected, and
111      * implies that underflow is detected after rounding. So this may not
112      * be the correct setting.
113      */
114     set_float_ftz_detection(float_ftz_before_rounding, &env->fp_status);
115 }
116 
117 static ObjectClass *rx_cpu_class_by_name(const char *cpu_model)
118 {
119     ObjectClass *oc;
120     char *typename;
121 
122     oc = object_class_by_name(cpu_model);
123     if (oc != NULL && object_class_dynamic_cast(oc, TYPE_RX_CPU) != NULL) {
124         return oc;
125     }
126     typename = g_strdup_printf(RX_CPU_TYPE_NAME("%s"), cpu_model);
127     oc = object_class_by_name(typename);
128     g_free(typename);
129 
130     return oc;
131 }
132 
133 static void rx_cpu_realize(DeviceState *dev, Error **errp)
134 {
135     CPUState *cs = CPU(dev);
136     RXCPUClass *rcc = RX_CPU_GET_CLASS(dev);
137     Error *local_err = NULL;
138 
139     cpu_exec_realizefn(cs, &local_err);
140     if (local_err != NULL) {
141         error_propagate(errp, local_err);
142         return;
143     }
144 
145     qemu_init_vcpu(cs);
146     cpu_reset(cs);
147 
148     rcc->parent_realize(dev, errp);
149 }
150 
151 static void rx_cpu_set_irq(void *opaque, int no, int request)
152 {
153     RXCPU *cpu = opaque;
154     CPUState *cs = CPU(cpu);
155     int irq = request & 0xff;
156 
157     static const int mask[] = {
158         [RX_CPU_IRQ] = CPU_INTERRUPT_HARD,
159         [RX_CPU_FIR] = CPU_INTERRUPT_FIR,
160     };
161     if (irq) {
162         cpu->env.req_irq = irq;
163         cpu->env.req_ipl = (request >> 8) & 0x0f;
164         cpu_interrupt(cs, mask[no]);
165     } else {
166         cpu_reset_interrupt(cs, mask[no]);
167     }
168 }
169 
170 static void rx_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
171 {
172     info->endian = BFD_ENDIAN_LITTLE;
173     info->mach = bfd_mach_rx;
174     info->print_insn = print_insn_rx;
175 }
176 
177 static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,
178                             MMUAccessType access_type, int mmu_idx,
179                             bool probe, uintptr_t retaddr)
180 {
181     uint32_t address, physical, prot;
182 
183     /* Linear mapping */
184     address = physical = addr & TARGET_PAGE_MASK;
185     prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
186     tlb_set_page(cs, address, physical, prot, mmu_idx, TARGET_PAGE_SIZE);
187     return true;
188 }
189 
190 static void rx_cpu_init(Object *obj)
191 {
192     RXCPU *cpu = RX_CPU(obj);
193 
194     qdev_init_gpio_in(DEVICE(cpu), rx_cpu_set_irq, 2);
195 }
196 
197 #include "hw/core/sysemu-cpu-ops.h"
198 
199 static const struct SysemuCPUOps rx_sysemu_ops = {
200     .has_work = rx_cpu_has_work,
201     .get_phys_page_debug = rx_cpu_get_phys_page_debug,
202 };
203 
204 #include "accel/tcg/cpu-ops.h"
205 
206 static const TCGCPUOps rx_tcg_ops = {
207     /* MTTCG not yet supported: require strict ordering */
208     .guest_default_memory_order = TCG_MO_ALL,
209     .mttcg_supported = false,
210 
211     .initialize = rx_translate_init,
212     .translate_code = rx_translate_code,
213     .synchronize_from_tb = rx_cpu_synchronize_from_tb,
214     .restore_state_to_opc = rx_restore_state_to_opc,
215     .mmu_index = rx_cpu_mmu_index,
216     .tlb_fill = rx_cpu_tlb_fill,
217 
218     .cpu_exec_interrupt = rx_cpu_exec_interrupt,
219     .cpu_exec_halt = rx_cpu_has_work,
220     .do_interrupt = rx_cpu_do_interrupt,
221 };
222 
223 static void rx_cpu_class_init(ObjectClass *klass, const void *data)
224 {
225     DeviceClass *dc = DEVICE_CLASS(klass);
226     CPUClass *cc = CPU_CLASS(klass);
227     RXCPUClass *rcc = RX_CPU_CLASS(klass);
228     ResettableClass *rc = RESETTABLE_CLASS(klass);
229 
230     device_class_set_parent_realize(dc, rx_cpu_realize,
231                                     &rcc->parent_realize);
232     resettable_class_set_parent_phases(rc, NULL, rx_cpu_reset_hold, NULL,
233                                        &rcc->parent_phases);
234 
235     cc->class_by_name = rx_cpu_class_by_name;
236     cc->dump_state = rx_cpu_dump_state;
237     cc->set_pc = rx_cpu_set_pc;
238     cc->get_pc = rx_cpu_get_pc;
239 
240     cc->sysemu_ops = &rx_sysemu_ops;
241     cc->gdb_read_register = rx_cpu_gdb_read_register;
242     cc->gdb_write_register = rx_cpu_gdb_write_register;
243     cc->disas_set_info = rx_cpu_disas_set_info;
244 
245     cc->gdb_core_xml_file = "rx-core.xml";
246     cc->tcg_ops = &rx_tcg_ops;
247 }
248 
249 static const TypeInfo rx_cpu_info = {
250     .name = TYPE_RX_CPU,
251     .parent = TYPE_CPU,
252     .instance_size = sizeof(RXCPU),
253     .instance_align = __alignof(RXCPU),
254     .instance_init = rx_cpu_init,
255     .abstract = true,
256     .class_size = sizeof(RXCPUClass),
257     .class_init = rx_cpu_class_init,
258 };
259 
260 static const TypeInfo rx62n_rx_cpu_info = {
261     .name = TYPE_RX62N_CPU,
262     .parent = TYPE_RX_CPU,
263 };
264 
265 static void rx_cpu_register_types(void)
266 {
267     type_register_static(&rx_cpu_info);
268     type_register_static(&rx62n_rx_cpu_info);
269 }
270 
271 type_init(rx_cpu_register_types)
272