2 * CRIS helper routines.
4 * Copyright (c) 2007 AXIS Communications AB
5 * Written by Edgar E. Iglesias.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
24 #include "qemu/host-utils.h"
25 #include "exec/exec-all.h"
26 #include "exec/cpu_ldst.h"
27 #include "exec/helper-proto.h"
30 //#define CRIS_HELPER_DEBUG
33 #ifdef CRIS_HELPER_DEBUG
35 #define D_LOG(...) qemu_log(__VA_ARGS__)
38 #define D_LOG(...) do { } while (0)
41 #if defined(CONFIG_USER_ONLY)
43 void cris_cpu_do_interrupt(CPUState *cs)
45 CRISCPU *cpu = CRIS_CPU(cs);
46 CPUCRISState *env = &cpu->env;
48 cs->exception_index = -1;
49 env->pregs[PR_ERP] = env->pc;
52 void crisv10_cpu_do_interrupt(CPUState *cs)
54 cris_cpu_do_interrupt(cs);
57 bool cris_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
58 MMUAccessType access_type, int mmu_idx,
59 bool probe, uintptr_t retaddr)
61 CRISCPU *cpu = CRIS_CPU(cs);
63 cs->exception_index = 0xaa;
64 cpu->env.pregs[PR_EDA] = address;
65 cpu_loop_exit_restore(cs, retaddr);
68 #else /* !CONFIG_USER_ONLY */
71 static void cris_shift_ccs(CPUCRISState *env)
74 /* Apply the ccs shift. */
75 ccs = env->pregs[PR_CCS];
76 ccs = ((ccs & 0xc0000000) | ((ccs << 12) >> 2)) & ~0x3ff;
77 env->pregs[PR_CCS] = ccs;
80 bool cris_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
81 MMUAccessType access_type, int mmu_idx,
82 bool probe, uintptr_t retaddr)
84 CRISCPU *cpu = CRIS_CPU(cs);
85 CPUCRISState *env = &cpu->env;
86 struct cris_mmu_result res;
90 miss = cris_mmu_translate(&res, env, address & TARGET_PAGE_MASK,
91 access_type, mmu_idx, 0);
94 * Mask off the cache selection bit. The ETRAX busses do not
97 phy = res.phy & ~0x80000000;
99 tlb_set_page(cs, address & TARGET_PAGE_MASK, phy,
100 prot, mmu_idx, TARGET_PAGE_SIZE);
108 if (cs->exception_index == EXCP_BUSFAULT) {
109 cpu_abort(cs, "CRIS: Illegal recursive bus fault."
110 "addr=%" VADDR_PRIx " access_type=%d\n",
111 address, access_type);
114 env->pregs[PR_EDA] = address;
115 cs->exception_index = EXCP_BUSFAULT;
116 env->fault_vector = res.bf_vec;
118 if (cpu_restore_state(cs, retaddr, true)) {
119 /* Evaluate flags after retranslation. */
120 helper_top_evaluate_flags(env);
126 void crisv10_cpu_do_interrupt(CPUState *cs)
128 CRISCPU *cpu = CRIS_CPU(cs);
129 CPUCRISState *env = &cpu->env;
132 D_LOG("exception index=%d interrupt_req=%d\n",
134 cs->interrupt_request);
137 /* CRISv10 never takes interrupts while in a delay-slot. */
138 cpu_abort(cs, "CRIS: Interrupt on delay-slot\n");
141 assert(!(env->pregs[PR_CCS] & PFIX_FLAG));
142 switch (cs->exception_index) {
144 /* These exceptions are genereated by the core itself.
145 ERP should point to the insn following the brk. */
146 ex_vec = env->trap_vector;
147 env->pregs[PRV10_BRP] = env->pc;
151 /* NMI is hardwired to vector zero. */
153 env->pregs[PR_CCS] &= ~M_FLAG_V10;
154 env->pregs[PRV10_BRP] = env->pc;
158 cpu_abort(cs, "Unhandled busfault");
162 /* The interrupt controller gives us the vector. */
163 ex_vec = env->interrupt_vector;
164 /* Normal interrupts are taken between
165 TB's. env->pc is valid here. */
166 env->pregs[PR_ERP] = env->pc;
170 if (env->pregs[PR_CCS] & U_FLAG) {
171 /* Swap stack pointers. */
172 env->pregs[PR_USP] = env->regs[R_SP];
173 env->regs[R_SP] = env->ksp;
176 /* Now that we are in kernel mode, load the handlers address. */
177 env->pc = cpu_ldl_code(env, env->pregs[PR_EBP] + ex_vec * 4);
179 env->pregs[PR_CCS] |= F_FLAG_V10; /* set F. */
181 qemu_log_mask(CPU_LOG_INT, "%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
182 __func__, env->pc, ex_vec,
188 void cris_cpu_do_interrupt(CPUState *cs)
190 CRISCPU *cpu = CRIS_CPU(cs);
191 CPUCRISState *env = &cpu->env;
194 D_LOG("exception index=%d interrupt_req=%d\n",
196 cs->interrupt_request);
198 switch (cs->exception_index) {
200 /* These exceptions are genereated by the core itself.
201 ERP should point to the insn following the brk. */
202 ex_vec = env->trap_vector;
203 env->pregs[PR_ERP] = env->pc;
207 /* NMI is hardwired to vector zero. */
209 env->pregs[PR_CCS] &= ~M_FLAG_V32;
210 env->pregs[PR_NRP] = env->pc;
214 ex_vec = env->fault_vector;
215 env->pregs[PR_ERP] = env->pc;
219 /* The interrupt controller gives us the vector. */
220 ex_vec = env->interrupt_vector;
221 /* Normal interrupts are taken between
222 TB's. env->pc is valid here. */
223 env->pregs[PR_ERP] = env->pc;
227 /* Fill in the IDX field. */
228 env->pregs[PR_EXS] = (ex_vec & 0xff) << 8;
231 D_LOG("excp isr=%x PC=%x ds=%d SP=%x"
232 " ERP=%x pid=%x ccs=%x cc=%d %x\n",
233 ex_vec, env->pc, env->dslot,
235 env->pregs[PR_ERP], env->pregs[PR_PID],
237 env->cc_op, env->cc_mask);
238 /* We loose the btarget, btaken state here so rexec the
240 env->pregs[PR_ERP] -= env->dslot;
241 /* Exception starts with dslot cleared. */
245 if (env->pregs[PR_CCS] & U_FLAG) {
246 /* Swap stack pointers. */
247 env->pregs[PR_USP] = env->regs[R_SP];
248 env->regs[R_SP] = env->ksp;
251 /* Apply the CRIS CCS shift. Clears U if set. */
254 /* Now that we are in kernel mode, load the handlers address.
255 This load may not fault, real hw leaves that behaviour as
257 env->pc = cpu_ldl_code(env, env->pregs[PR_EBP] + ex_vec * 4);
259 /* Clear the excption_index to avoid spurios hw_aborts for recursive
261 cs->exception_index = -1;
263 D_LOG("%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
264 __func__, env->pc, ex_vec,
270 hwaddr cris_cpu_get_phys_page_debug(CPUState *cs, vaddr addr)
272 CRISCPU *cpu = CRIS_CPU(cs);
274 struct cris_mmu_result res;
277 miss = cris_mmu_translate(&res, &cpu->env, addr, 0, 0, 1);
278 /* If D TLB misses, try I TLB. */
280 miss = cris_mmu_translate(&res, &cpu->env, addr, 2, 0, 1);
286 D(fprintf(stderr, "%s %x -> %x\n", __func__, addr, phy));
291 bool cris_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
293 CPUClass *cc = CPU_GET_CLASS(cs);
294 CRISCPU *cpu = CRIS_CPU(cs);
295 CPUCRISState *env = &cpu->env;
298 if (interrupt_request & CPU_INTERRUPT_HARD
299 && (env->pregs[PR_CCS] & I_FLAG)
300 && !env->locked_irq) {
301 cs->exception_index = EXCP_IRQ;
302 cc->do_interrupt(cs);
305 if (interrupt_request & CPU_INTERRUPT_NMI) {
306 unsigned int m_flag_archval;
307 if (env->pregs[PR_VR] < 32) {
308 m_flag_archval = M_FLAG_V10;
310 m_flag_archval = M_FLAG_V32;
312 if ((env->pregs[PR_CCS] & m_flag_archval)) {
313 cs->exception_index = EXCP_NMI;
314 cc->do_interrupt(cs);