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"
23 #include "hw/core/tcg-cpu-ops.h"
25 #include "qemu/host-utils.h"
26 #include "exec/exec-all.h"
27 #include "exec/cpu_ldst.h"
28 #include "exec/helper-proto.h"
31 //#define CRIS_HELPER_DEBUG
34 #ifdef CRIS_HELPER_DEBUG
36 #define D_LOG(...) qemu_log(__VA_ARGS__)
39 #define D_LOG(...) do { } while (0)
42 static void cris_shift_ccs(CPUCRISState
*env
)
45 /* Apply the ccs shift. */
46 ccs
= env
->pregs
[PR_CCS
];
47 ccs
= ((ccs
& 0xc0000000) | ((ccs
<< 12) >> 2)) & ~0x3ff;
48 env
->pregs
[PR_CCS
] = ccs
;
51 bool cris_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
52 MMUAccessType access_type
, int mmu_idx
,
53 bool probe
, uintptr_t retaddr
)
55 CRISCPU
*cpu
= CRIS_CPU(cs
);
56 CPUCRISState
*env
= &cpu
->env
;
57 struct cris_mmu_result res
;
61 miss
= cris_mmu_translate(&res
, env
, address
& TARGET_PAGE_MASK
,
62 access_type
, mmu_idx
, 0);
65 * Mask off the cache selection bit. The ETRAX busses do not
68 phy
= res
.phy
& ~0x80000000;
70 tlb_set_page(cs
, address
& TARGET_PAGE_MASK
, phy
,
71 prot
, mmu_idx
, TARGET_PAGE_SIZE
);
79 if (cs
->exception_index
== EXCP_BUSFAULT
) {
80 cpu_abort(cs
, "CRIS: Illegal recursive bus fault."
81 "addr=%" VADDR_PRIx
" access_type=%d\n",
82 address
, access_type
);
85 env
->pregs
[PR_EDA
] = address
;
86 cs
->exception_index
= EXCP_BUSFAULT
;
87 env
->fault_vector
= res
.bf_vec
;
89 if (cpu_restore_state(cs
, retaddr
, true)) {
90 /* Evaluate flags after retranslation. */
91 helper_top_evaluate_flags(env
);
97 void crisv10_cpu_do_interrupt(CPUState
*cs
)
99 CRISCPU
*cpu
= CRIS_CPU(cs
);
100 CPUCRISState
*env
= &cpu
->env
;
103 D_LOG("exception index=%d interrupt_req=%d\n",
105 cs
->interrupt_request
);
108 /* CRISv10 never takes interrupts while in a delay-slot. */
109 cpu_abort(cs
, "CRIS: Interrupt on delay-slot\n");
112 assert(!(env
->pregs
[PR_CCS
] & PFIX_FLAG
));
113 switch (cs
->exception_index
) {
115 /* These exceptions are genereated by the core itself.
116 ERP should point to the insn following the brk. */
117 ex_vec
= env
->trap_vector
;
118 env
->pregs
[PRV10_BRP
] = env
->pc
;
122 /* NMI is hardwired to vector zero. */
124 env
->pregs
[PR_CCS
] &= ~M_FLAG_V10
;
125 env
->pregs
[PRV10_BRP
] = env
->pc
;
129 cpu_abort(cs
, "Unhandled busfault");
133 /* The interrupt controller gives us the vector. */
134 ex_vec
= env
->interrupt_vector
;
135 /* Normal interrupts are taken between
136 TB's. env->pc is valid here. */
137 env
->pregs
[PR_ERP
] = env
->pc
;
141 if (env
->pregs
[PR_CCS
] & U_FLAG
) {
142 /* Swap stack pointers. */
143 env
->pregs
[PR_USP
] = env
->regs
[R_SP
];
144 env
->regs
[R_SP
] = env
->ksp
;
147 /* Now that we are in kernel mode, load the handlers address. */
148 env
->pc
= cpu_ldl_code(env
, env
->pregs
[PR_EBP
] + ex_vec
* 4);
150 env
->pregs
[PR_CCS
] |= F_FLAG_V10
; /* set F. */
152 qemu_log_mask(CPU_LOG_INT
, "%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
153 __func__
, env
->pc
, ex_vec
,
159 void cris_cpu_do_interrupt(CPUState
*cs
)
161 CRISCPU
*cpu
= CRIS_CPU(cs
);
162 CPUCRISState
*env
= &cpu
->env
;
165 D_LOG("exception index=%d interrupt_req=%d\n",
167 cs
->interrupt_request
);
169 switch (cs
->exception_index
) {
171 /* These exceptions are genereated by the core itself.
172 ERP should point to the insn following the brk. */
173 ex_vec
= env
->trap_vector
;
174 env
->pregs
[PR_ERP
] = env
->pc
;
178 /* NMI is hardwired to vector zero. */
180 env
->pregs
[PR_CCS
] &= ~M_FLAG_V32
;
181 env
->pregs
[PR_NRP
] = env
->pc
;
185 ex_vec
= env
->fault_vector
;
186 env
->pregs
[PR_ERP
] = env
->pc
;
190 /* The interrupt controller gives us the vector. */
191 ex_vec
= env
->interrupt_vector
;
192 /* Normal interrupts are taken between
193 TB's. env->pc is valid here. */
194 env
->pregs
[PR_ERP
] = env
->pc
;
198 /* Fill in the IDX field. */
199 env
->pregs
[PR_EXS
] = (ex_vec
& 0xff) << 8;
202 D_LOG("excp isr=%x PC=%x ds=%d SP=%x"
203 " ERP=%x pid=%x ccs=%x cc=%d %x\n",
204 ex_vec
, env
->pc
, env
->dslot
,
206 env
->pregs
[PR_ERP
], env
->pregs
[PR_PID
],
208 env
->cc_op
, env
->cc_mask
);
209 /* We loose the btarget, btaken state here so rexec the
211 env
->pregs
[PR_ERP
] -= env
->dslot
;
212 /* Exception starts with dslot cleared. */
216 if (env
->pregs
[PR_CCS
] & U_FLAG
) {
217 /* Swap stack pointers. */
218 env
->pregs
[PR_USP
] = env
->regs
[R_SP
];
219 env
->regs
[R_SP
] = env
->ksp
;
222 /* Apply the CRIS CCS shift. Clears U if set. */
225 /* Now that we are in kernel mode, load the handlers address.
226 This load may not fault, real hw leaves that behaviour as
228 env
->pc
= cpu_ldl_code(env
, env
->pregs
[PR_EBP
] + ex_vec
* 4);
230 /* Clear the excption_index to avoid spurios hw_aborts for recursive
232 cs
->exception_index
= -1;
234 D_LOG("%s isr=%x vec=%x ccs=%x pid=%d erp=%x\n",
235 __func__
, env
->pc
, ex_vec
,
241 hwaddr
cris_cpu_get_phys_page_debug(CPUState
*cs
, vaddr addr
)
243 CRISCPU
*cpu
= CRIS_CPU(cs
);
245 struct cris_mmu_result res
;
248 miss
= cris_mmu_translate(&res
, &cpu
->env
, addr
, MMU_DATA_LOAD
, 0, 1);
249 /* If D TLB misses, try I TLB. */
251 miss
= cris_mmu_translate(&res
, &cpu
->env
, addr
, MMU_INST_FETCH
, 0, 1);
257 D(fprintf(stderr
, "%s %x -> %x\n", __func__
, addr
, phy
));
261 bool cris_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
263 CPUClass
*cc
= CPU_GET_CLASS(cs
);
264 CRISCPU
*cpu
= CRIS_CPU(cs
);
265 CPUCRISState
*env
= &cpu
->env
;
268 if (interrupt_request
& CPU_INTERRUPT_HARD
269 && (env
->pregs
[PR_CCS
] & I_FLAG
)
270 && !env
->locked_irq
) {
271 cs
->exception_index
= EXCP_IRQ
;
272 cc
->tcg_ops
->do_interrupt(cs
);
275 if (interrupt_request
& CPU_INTERRUPT_NMI
) {
276 unsigned int m_flag_archval
;
277 if (env
->pregs
[PR_VR
] < 32) {
278 m_flag_archval
= M_FLAG_V10
;
280 m_flag_archval
= M_FLAG_V32
;
282 if ((env
->pregs
[PR_CCS
] & m_flag_archval
)) {
283 cs
->exception_index
= EXCP_NMI
;
284 cc
->tcg_ops
->do_interrupt(cs
);