2 * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the Open Source and Linux Lab nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "qemu/osdep.h"
30 #include "exec/exec-all.h"
31 #include "exec/gdbstub.h"
32 #include "exec/helper-proto.h"
33 #include "qemu/error-report.h"
34 #include "qemu/host-utils.h"
36 static struct XtensaConfigList *xtensa_cores;
38 static void add_translator_to_hash(GHashTable *translator,
40 const XtensaOpcodeOps *opcode)
42 if (!g_hash_table_insert(translator, (void *)name, (void *)opcode)) {
43 error_report("Multiple definitions of '%s' opcode in a single table",
48 static GHashTable *hash_opcode_translators(const XtensaOpcodeTranslators *t)
51 GHashTable *translator = g_hash_table_new(g_str_hash, g_str_equal);
53 for (i = 0; i < t->num_opcodes; ++i) {
54 add_translator_to_hash(translator,
55 (void *)t->opcode[i].name,
56 (void *)(t->opcode + i));
61 static XtensaOpcodeOps *
62 xtensa_find_opcode_ops(const XtensaOpcodeTranslators *t,
65 static GHashTable *translators;
66 GHashTable *translator;
68 if (translators == NULL) {
69 translators = g_hash_table_new(g_direct_hash, g_direct_equal);
71 translator = g_hash_table_lookup(translators, t);
72 if (translator == NULL) {
73 translator = hash_opcode_translators(t);
74 g_hash_table_insert(translators, (void *)t, translator);
76 return g_hash_table_lookup(translator, name);
79 static void init_libisa(XtensaConfig *config)
85 config->isa = xtensa_isa_init(config->isa_internal, NULL, NULL);
86 assert(xtensa_isa_maxlength(config->isa) <= MAX_INSN_LENGTH);
87 opcodes = xtensa_isa_num_opcodes(config->isa);
88 formats = xtensa_isa_num_formats(config->isa);
89 config->opcode_ops = g_new(XtensaOpcodeOps *, opcodes);
91 for (i = 0; i < formats; ++i) {
92 assert(xtensa_format_num_slots(config->isa, i) <= MAX_INSN_SLOTS);
95 for (i = 0; i < opcodes; ++i) {
96 const char *opc_name = xtensa_opcode_name(config->isa, i);
97 XtensaOpcodeOps *ops = NULL;
99 assert(xtensa_opcode_num_operands(config->isa, i) <= MAX_OPCODE_ARGS);
100 if (!config->opcode_translators) {
101 ops = xtensa_find_opcode_ops(&xtensa_core_opcodes, opc_name);
103 for (j = 0; !ops && config->opcode_translators[j]; ++j) {
104 ops = xtensa_find_opcode_ops(config->opcode_translators[j],
111 "opcode translator not found for %s's opcode '%s'\n",
112 config->name, opc_name);
115 config->opcode_ops[i] = ops;
117 config->a_regfile = xtensa_regfile_lookup(config->isa, "AR");
120 static void xtensa_finalize_config(XtensaConfig *config)
122 if (config->isa_internal) {
126 if (config->gdb_regmap.num_regs == 0 ||
127 config->gdb_regmap.num_core_regs == 0) {
129 unsigned n_core_regs = 0;
131 xtensa_count_regs(config, &n_regs, &n_core_regs);
132 if (config->gdb_regmap.num_regs == 0) {
133 config->gdb_regmap.num_regs = n_regs;
135 if (config->gdb_regmap.num_core_regs == 0) {
136 config->gdb_regmap.num_core_regs = n_core_regs;
141 static void xtensa_core_class_init(ObjectClass *oc, void *data)
143 CPUClass *cc = CPU_CLASS(oc);
144 XtensaCPUClass *xcc = XTENSA_CPU_CLASS(oc);
145 XtensaConfig *config = data;
147 xtensa_finalize_config(config);
148 xcc->config = config;
151 * Use num_core_regs to see only non-privileged registers in an unmodified
152 * gdb. Use num_regs to see all registers. gdb modification is required
153 * for that: reset bit 0 in the 'flags' field of the registers definitions
154 * in the gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
156 cc->gdb_num_core_regs = config->gdb_regmap.num_regs;
159 void xtensa_register_core(XtensaConfigList *node)
162 .parent = TYPE_XTENSA_CPU,
163 .class_init = xtensa_core_class_init,
164 .class_data = (void *)node->config,
167 node->next = xtensa_cores;
169 type.name = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), node->config->name);
170 type_register(&type);
171 g_free((gpointer)type.name);
174 static uint32_t check_hw_breakpoints(CPUXtensaState *env)
178 for (i = 0; i < env->config->ndbreak; ++i) {
179 if (env->cpu_watchpoint[i] &&
180 env->cpu_watchpoint[i]->flags & BP_WATCHPOINT_HIT) {
181 return DEBUGCAUSE_DB | (i << DEBUGCAUSE_DBNUM_SHIFT);
187 void xtensa_breakpoint_handler(CPUState *cs)
189 XtensaCPU *cpu = XTENSA_CPU(cs);
190 CPUXtensaState *env = &cpu->env;
192 if (cs->watchpoint_hit) {
193 if (cs->watchpoint_hit->flags & BP_CPU) {
196 cs->watchpoint_hit = NULL;
197 cause = check_hw_breakpoints(env);
199 debug_exception_env(env, cause);
201 cpu_loop_exit_noexc(cs);
206 void xtensa_cpu_list(FILE *f, fprintf_function cpu_fprintf)
208 XtensaConfigList *core = xtensa_cores;
209 cpu_fprintf(f, "Available CPUs:\n");
210 for (; core; core = core->next) {
211 cpu_fprintf(f, " %s\n", core->config->name);
215 #ifdef CONFIG_USER_ONLY
217 int xtensa_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw,
220 XtensaCPU *cpu = XTENSA_CPU(cs);
221 CPUXtensaState *env = &cpu->env;
223 qemu_log_mask(CPU_LOG_INT,
224 "%s: rw = %d, address = 0x%08" VADDR_PRIx ", size = %d\n",
225 __func__, rw, address, size);
226 env->sregs[EXCVADDR] = address;
227 env->sregs[EXCCAUSE] = rw ? STORE_PROHIBITED_CAUSE : LOAD_PROHIBITED_CAUSE;
228 cs->exception_index = EXC_USER;
234 void xtensa_cpu_do_unaligned_access(CPUState *cs,
235 vaddr addr, MMUAccessType access_type,
236 int mmu_idx, uintptr_t retaddr)
238 XtensaCPU *cpu = XTENSA_CPU(cs);
239 CPUXtensaState *env = &cpu->env;
241 if (xtensa_option_enabled(env->config, XTENSA_OPTION_UNALIGNED_EXCEPTION) &&
242 !xtensa_option_enabled(env->config, XTENSA_OPTION_HW_ALIGNMENT)) {
243 cpu_restore_state(CPU(cpu), retaddr, true);
244 HELPER(exception_cause_vaddr)(env,
245 env->pc, LOAD_STORE_ALIGNMENT_CAUSE,
250 void tlb_fill(CPUState *cs, target_ulong vaddr, int size,
251 MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
253 XtensaCPU *cpu = XTENSA_CPU(cs);
254 CPUXtensaState *env = &cpu->env;
258 int ret = xtensa_get_physical_addr(env, true, vaddr, access_type, mmu_idx,
259 &paddr, &page_size, &access);
261 qemu_log_mask(CPU_LOG_MMU, "%s(%08x, %d, %d) -> %08x, ret = %d\n",
262 __func__, vaddr, access_type, mmu_idx, paddr, ret);
266 vaddr & TARGET_PAGE_MASK,
267 paddr & TARGET_PAGE_MASK,
268 access, mmu_idx, page_size);
270 cpu_restore_state(cs, retaddr, true);
271 HELPER(exception_cause_vaddr)(env, env->pc, ret, vaddr);
275 void xtensa_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr, vaddr addr,
276 unsigned size, MMUAccessType access_type,
277 int mmu_idx, MemTxAttrs attrs,
278 MemTxResult response, uintptr_t retaddr)
280 XtensaCPU *cpu = XTENSA_CPU(cs);
281 CPUXtensaState *env = &cpu->env;
283 cpu_restore_state(cs, retaddr, true);
284 HELPER(exception_cause_vaddr)(env, env->pc,
285 access_type == MMU_INST_FETCH ?
286 INSTR_PIF_ADDR_ERROR_CAUSE :
287 LOAD_STORE_PIF_ADDR_ERROR_CAUSE,
291 void xtensa_runstall(CPUXtensaState *env, bool runstall)
293 CPUState *cpu = CPU(xtensa_env_get_cpu(env));
295 env->runstall = runstall;
296 cpu->halted = runstall;
298 cpu_interrupt(cpu, CPU_INTERRUPT_HALT);