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.1 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 "hw/core/tcg-cpu-ops.h"
26 #include "qemu/host-utils.h"
27 #include "exec/exec-all.h"
28 #include "exec/cpu_ldst.h"
29 #include "exec/helper-proto.h"
32 //#define CRIS_HELPER_DEBUG
35 #ifdef CRIS_HELPER_DEBUG
37 #define D_LOG(...) qemu_log(__VA_ARGS__)
40 #define D_LOG(...) do { } while (0)
43 static void cris_shift_ccs(CPUCRISState
*env
)
46 /* Apply the ccs shift. */
47 ccs
= env
->pregs
[PR_CCS
];
48 ccs
= ((ccs
& 0xc0000000) | ((ccs
<< 12) >> 2)) & ~0x3ff;
49 env
->pregs
[PR_CCS
] = ccs
;
52 bool cris_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
53 MMUAccessType access_type
, int mmu_idx
,
54 bool probe
, uintptr_t retaddr
)
56 CRISCPU
*cpu
= CRIS_CPU(cs
);
57 CPUCRISState
*env
= &cpu
->env
;
58 struct cris_mmu_result res
;
62 miss
= cris_mmu_translate(&res
, env
, address
& TARGET_PAGE_MASK
,
63 access_type
, mmu_idx
, 0);
66 * Mask off the cache selection bit. The ETRAX busses do not
69 phy
= res
.phy
& ~0x80000000;
71 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
, phy
,
72 prot
, mmu_idx
, TARGET_PAGE_SIZE
);
80 if (cs
->exception_index
== EXCP_BUSFAULT
) {
81 cpu_abort(cs
, "CRIS: Illegal recursive bus fault."
82 "addr=%" VADDR_PRIx
" access_type=%d\n",
83 address
, access_type
);
86 env
->pregs
[PR_EDA
] = address
;
87 cs
->exception_index
= EXCP_BUSFAULT
;
88 env
->fault_vector
= res
.bf_vec
;
90 if (cpu_restore_state(cs
, retaddr
)) {
91 /* Evaluate flags after retranslation. */
92 helper_top_evaluate_flags(env
);
98 void crisv10_cpu_do_interrupt(CPUState
*cs
)
100 CRISCPU
*cpu
= CRIS_CPU(cs
);
101 CPUCRISState
*env
= &cpu
->env
;
104 D_LOG("exception index=%d interrupt_req=%d\n",
106 cs
->interrupt_request
);
109 /* CRISv10 never takes interrupts while in a delay-slot. */
110 cpu_abort(cs
, "CRIS: Interrupt on delay-slot\n");
113 assert(!(env
->pregs
[PR_CCS
] & PFIX_FLAG
));
114 switch (cs
->exception_index
) {
116 /* These exceptions are generated by the core itself.
117 ERP should point to the insn following the brk. */
118 ex_vec
= env
->trap_vector
;
119 env
->pregs
[PRV10_BRP
] = env
->pc
;
123 /* NMI is hardwired to vector zero. */
125 env
->pregs
[PR_CCS
] &= ~M_FLAG_V10
;
126 env
->pregs
[PRV10_BRP
] = env
->pc
;
130 cpu_abort(cs
, "Unhandled busfault");
134 /* The interrupt controller gives us the vector. */
135 ex_vec
= env
->interrupt_vector
;
136 /* Normal interrupts are taken between
137 TB's. env->pc is valid here. */
138 env
->pregs
[PR_ERP
] = env
->pc
;
142 if (env
->pregs
[PR_CCS
] & U_FLAG
) {
143 /* Swap stack pointers. */
144 env
->pregs
[PR_USP
] = env
->regs
[R_SP
];
145 env
->regs
[R_SP
] = env
->ksp
;
148 /* Now that we are in kernel mode, load the handlers address. */
149 env
->pc
= cpu_ldl_code(env
, env
->pregs
[PR_EBP
] + ex_vec
* 4);
151 env
->pregs
[PR_CCS
] |= F_FLAG_V10
; /* set F. */
153 qemu_log_mask(CPU_LOG_INT
, "%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
154 __func__
, env
->pc
, ex_vec
,
160 void cris_cpu_do_interrupt(CPUState
*cs
)
162 CRISCPU
*cpu
= CRIS_CPU(cs
);
163 CPUCRISState
*env
= &cpu
->env
;
166 D_LOG("exception index=%d interrupt_req=%d\n",
168 cs
->interrupt_request
);
170 switch (cs
->exception_index
) {
172 /* These exceptions are generated by the core itself.
173 ERP should point to the insn following the brk. */
174 ex_vec
= env
->trap_vector
;
175 env
->pregs
[PR_ERP
] = env
->pc
;
179 /* NMI is hardwired to vector zero. */
181 env
->pregs
[PR_CCS
] &= ~M_FLAG_V32
;
182 env
->pregs
[PR_NRP
] = env
->pc
;
186 ex_vec
= env
->fault_vector
;
187 env
->pregs
[PR_ERP
] = env
->pc
;
191 /* The interrupt controller gives us the vector. */
192 ex_vec
= env
->interrupt_vector
;
193 /* Normal interrupts are taken between
194 TB's. env->pc is valid here. */
195 env
->pregs
[PR_ERP
] = env
->pc
;
199 /* Fill in the IDX field. */
200 env
->pregs
[PR_EXS
] = (ex_vec
& 0xff) << 8;
203 D_LOG("excp isr=%x PC=%x ds=%d SP=%x"
204 " ERP=%x pid=%x ccs=%x cc=%d %x\n",
205 ex_vec
, env
->pc
, env
->dslot
,
207 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
209 env
->cc_op
, env
->cc_mask
);
210 /* We loose the btarget, btaken state here so rexec the
212 env
->pregs
[PR_ERP
] -= env
->dslot
;
213 /* Exception starts with dslot cleared. */
217 if (env
->pregs
[PR_CCS
] & U_FLAG
) {
218 /* Swap stack pointers. */
219 env
->pregs
[PR_USP
] = env
->regs
[R_SP
];
220 env
->regs
[R_SP
] = env
->ksp
;
223 /* Apply the CRIS CCS shift. Clears U if set. */
226 /* Now that we are in kernel mode, load the handlers address.
227 This load may not fault, real hw leaves that behaviour as
229 env
->pc
= cpu_ldl_code(env
, env
->pregs
[PR_EBP
] + ex_vec
* 4);
231 /* Clear the excption_index to avoid spurious hw_aborts for recursive
233 cs
->exception_index
= -1;
235 D_LOG("%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
236 __func__
, env
->pc
, ex_vec
,
242 hwaddr
cris_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
244 CRISCPU
*cpu
= CRIS_CPU(cs
);
246 struct cris_mmu_result res
;
249 miss
= cris_mmu_translate(&res
, &cpu
->env
, addr
, MMU_DATA_LOAD
, 0, 1);
250 /* If D TLB misses, try I TLB. */
252 miss
= cris_mmu_translate(&res
, &cpu
->env
, addr
, MMU_INST_FETCH
, 0, 1);
258 D(fprintf(stderr
, "%s %x -> %x\n", __func__
, addr
, phy
));
262 bool cris_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
264 CPUClass
*cc
= CPU_GET_CLASS(cs
);
265 CRISCPU
*cpu
= CRIS_CPU(cs
);
266 CPUCRISState
*env
= &cpu
->env
;
269 if (interrupt_request
& CPU_INTERRUPT_HARD
270 && (env
->pregs
[PR_CCS
] & I_FLAG
)
271 && !env
->locked_irq
) {
272 cs
->exception_index
= EXCP_IRQ
;
273 cc
->tcg_ops
->do_interrupt(cs
);
276 if (interrupt_request
& CPU_INTERRUPT_NMI
) {
277 unsigned int m_flag_archval
;
278 if (env
->pregs
[PR_VR
] < 32) {
279 m_flag_archval
= M_FLAG_V10
;
281 m_flag_archval
= M_FLAG_V32
;
283 if ((env
->pregs
[PR_CCS
] & m_flag_archval
)) {
284 cs
->exception_index
= EXCP_NMI
;
285 cc
->tcg_ops
->do_interrupt(cs
);