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