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