]> Git Repo - qemu.git/blob - target/xtensa/helper.c
target/xtensa: don't require opcode table sorting
[qemu.git] / target / xtensa / helper.c
1 /*
2  * Copyright (c) 2011, Max Filippov, Open Source and Linux Lab.
3  * All rights reserved.
4  *
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.
15  *
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.
26  */
27
28 #include "qemu/osdep.h"
29 #include "cpu.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"
35
36 static struct XtensaConfigList *xtensa_cores;
37
38 static void add_translator_to_hash(GHashTable *translator,
39                                    const char *name,
40                                    const XtensaOpcodeOps *opcode)
41 {
42     if (!g_hash_table_insert(translator, (void *)name, (void *)opcode)) {
43         error_report("Multiple definitions of '%s' opcode in a single table",
44                      name);
45     }
46 }
47
48 static GHashTable *hash_opcode_translators(const XtensaOpcodeTranslators *t)
49 {
50     unsigned i, j;
51     GHashTable *translator = g_hash_table_new(g_str_hash, g_str_equal);
52
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));
57     }
58     return translator;
59 }
60
61 static XtensaOpcodeOps *
62 xtensa_find_opcode_ops(const XtensaOpcodeTranslators *t,
63                        const char *name)
64 {
65     static GHashTable *translators;
66     GHashTable *translator;
67
68     if (translators == NULL) {
69         translators = g_hash_table_new(g_direct_hash, g_direct_equal);
70     }
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);
75     }
76     return g_hash_table_lookup(translator, name);
77 }
78
79 static void init_libisa(XtensaConfig *config)
80 {
81     unsigned i, j;
82     unsigned opcodes;
83     unsigned formats;
84
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);
90
91     for (i = 0; i < formats; ++i) {
92         assert(xtensa_format_num_slots(config->isa, i) <= MAX_INSN_SLOTS);
93     }
94
95     for (i = 0; i < opcodes; ++i) {
96         const char *opc_name = xtensa_opcode_name(config->isa, i);
97         XtensaOpcodeOps *ops = NULL;
98
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);
102         } else {
103             for (j = 0; !ops && config->opcode_translators[j]; ++j) {
104                 ops = xtensa_find_opcode_ops(config->opcode_translators[j],
105                                              opc_name);
106             }
107         }
108 #ifdef DEBUG
109         if (ops == NULL) {
110             fprintf(stderr,
111                     "opcode translator not found for %s's opcode '%s'\n",
112                     config->name, opc_name);
113         }
114 #endif
115         config->opcode_ops[i] = ops;
116     }
117     config->a_regfile = xtensa_regfile_lookup(config->isa, "AR");
118 }
119
120 static void xtensa_finalize_config(XtensaConfig *config)
121 {
122     if (config->isa_internal) {
123         init_libisa(config);
124     }
125
126     if (config->gdb_regmap.num_regs == 0 ||
127         config->gdb_regmap.num_core_regs == 0) {
128         unsigned n_regs = 0;
129         unsigned n_core_regs = 0;
130
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;
134         }
135         if (config->gdb_regmap.num_core_regs == 0) {
136             config->gdb_regmap.num_core_regs = n_core_regs;
137         }
138     }
139 }
140
141 static void xtensa_core_class_init(ObjectClass *oc, void *data)
142 {
143     CPUClass *cc = CPU_CLASS(oc);
144     XtensaCPUClass *xcc = XTENSA_CPU_CLASS(oc);
145     XtensaConfig *config = data;
146
147     xtensa_finalize_config(config);
148     xcc->config = config;
149
150     /*
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.
155      */
156     cc->gdb_num_core_regs = config->gdb_regmap.num_regs;
157 }
158
159 void xtensa_register_core(XtensaConfigList *node)
160 {
161     TypeInfo type = {
162         .parent = TYPE_XTENSA_CPU,
163         .class_init = xtensa_core_class_init,
164         .class_data = (void *)node->config,
165     };
166
167     node->next = xtensa_cores;
168     xtensa_cores = node;
169     type.name = g_strdup_printf(XTENSA_CPU_TYPE_NAME("%s"), node->config->name);
170     type_register(&type);
171     g_free((gpointer)type.name);
172 }
173
174 static uint32_t check_hw_breakpoints(CPUXtensaState *env)
175 {
176     unsigned i;
177
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);
182         }
183     }
184     return 0;
185 }
186
187 void xtensa_breakpoint_handler(CPUState *cs)
188 {
189     XtensaCPU *cpu = XTENSA_CPU(cs);
190     CPUXtensaState *env = &cpu->env;
191
192     if (cs->watchpoint_hit) {
193         if (cs->watchpoint_hit->flags & BP_CPU) {
194             uint32_t cause;
195
196             cs->watchpoint_hit = NULL;
197             cause = check_hw_breakpoints(env);
198             if (cause) {
199                 debug_exception_env(env, cause);
200             }
201             cpu_loop_exit_noexc(cs);
202         }
203     }
204 }
205
206 void xtensa_cpu_list(FILE *f, fprintf_function cpu_fprintf)
207 {
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);
212     }
213 }
214
215 #ifdef CONFIG_USER_ONLY
216
217 int xtensa_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int size, int rw,
218                                 int mmu_idx)
219 {
220     XtensaCPU *cpu = XTENSA_CPU(cs);
221     CPUXtensaState *env = &cpu->env;
222
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;
229     return 1;
230 }
231
232 #else
233
234 void xtensa_cpu_do_unaligned_access(CPUState *cs,
235                                     vaddr addr, MMUAccessType access_type,
236                                     int mmu_idx, uintptr_t retaddr)
237 {
238     XtensaCPU *cpu = XTENSA_CPU(cs);
239     CPUXtensaState *env = &cpu->env;
240
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,
246                                       addr);
247     }
248 }
249
250 void tlb_fill(CPUState *cs, target_ulong vaddr, int size,
251               MMUAccessType access_type, int mmu_idx, uintptr_t retaddr)
252 {
253     XtensaCPU *cpu = XTENSA_CPU(cs);
254     CPUXtensaState *env = &cpu->env;
255     uint32_t paddr;
256     uint32_t page_size;
257     unsigned access;
258     int ret = xtensa_get_physical_addr(env, true, vaddr, access_type, mmu_idx,
259                                        &paddr, &page_size, &access);
260
261     qemu_log_mask(CPU_LOG_MMU, "%s(%08x, %d, %d) -> %08x, ret = %d\n",
262                   __func__, vaddr, access_type, mmu_idx, paddr, ret);
263
264     if (ret == 0) {
265         tlb_set_page(cs,
266                      vaddr & TARGET_PAGE_MASK,
267                      paddr & TARGET_PAGE_MASK,
268                      access, mmu_idx, page_size);
269     } else {
270         cpu_restore_state(cs, retaddr, true);
271         HELPER(exception_cause_vaddr)(env, env->pc, ret, vaddr);
272     }
273 }
274
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)
279 {
280     XtensaCPU *cpu = XTENSA_CPU(cs);
281     CPUXtensaState *env = &cpu->env;
282
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,
288                                   addr);
289 }
290
291 void xtensa_runstall(CPUXtensaState *env, bool runstall)
292 {
293     CPUState *cpu = CPU(xtensa_env_get_cpu(env));
294
295     env->runstall = runstall;
296     cpu->halted = runstall;
297     if (runstall) {
298         cpu_interrupt(cpu, CPU_INTERRUPT_HALT);
299     } else {
300         qemu_cpu_kick(cpu);
301     }
302 }
303 #endif
This page took 0.040631 seconds and 4 git commands to generate.