2 * MicroBlaze helper routines.
4 * Copyright (c) 2009 Edgar E. Iglesias <edgar.iglesias@gmail.com>
5 * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
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 "exec/exec-all.h"
24 #include "qemu/host-utils.h"
27 #ifndef CONFIG_USER_ONLY
28 static bool mb_cpu_access_is_secure(MicroBlazeCPU
*cpu
,
29 MMUAccessType access_type
)
31 if (access_type
== MMU_INST_FETCH
) {
32 return !cpu
->ns_axi_ip
;
34 return !cpu
->ns_axi_dp
;
38 bool mb_cpu_tlb_fill(CPUState
*cs
, vaddr address
, int size
,
39 MMUAccessType access_type
, int mmu_idx
,
40 bool probe
, uintptr_t retaddr
)
42 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
43 CPUMBState
*env
= &cpu
->env
;
44 MicroBlazeMMULookup lu
;
47 MemTxAttrs attrs
= {};
49 attrs
.secure
= mb_cpu_access_is_secure(cpu
, access_type
);
51 if (mmu_idx
== MMU_NOMMU_IDX
) {
52 /* MMU disabled or not available. */
53 address
&= TARGET_PAGE_MASK
;
55 tlb_set_page_with_attrs(cs
, address
, address
, attrs
, prot
, mmu_idx
,
60 hit
= mmu_translate(cpu
, &lu
, address
, access_type
, mmu_idx
);
62 uint32_t vaddr
= address
& TARGET_PAGE_MASK
;
63 uint32_t paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
65 qemu_log_mask(CPU_LOG_MMU
, "MMU map mmu=%d v=%x p=%x prot=%x\n",
66 mmu_idx
, vaddr
, paddr
, lu
.prot
);
67 tlb_set_page_with_attrs(cs
, vaddr
, paddr
, attrs
, lu
.prot
, mmu_idx
,
77 qemu_log_mask(CPU_LOG_MMU
, "mmu=%d miss v=%" VADDR_PRIx
"\n",
83 env
->esr
= access_type
== MMU_INST_FETCH
? 17 : 16;
84 env
->esr
|= (access_type
== MMU_DATA_STORE
) << 10;
87 env
->esr
= access_type
== MMU_INST_FETCH
? 19 : 18;
88 env
->esr
|= (access_type
== MMU_DATA_STORE
) << 10;
94 if (cs
->exception_index
== EXCP_MMU
) {
95 cpu_abort(cs
, "recursive faults\n");
99 cs
->exception_index
= EXCP_MMU
;
100 cpu_loop_exit_restore(cs
, retaddr
);
103 void mb_cpu_do_interrupt(CPUState
*cs
)
105 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
106 CPUMBState
*env
= &cpu
->env
;
107 uint32_t t
, msr
= mb_cpu_read_msr(env
);
110 /* IMM flag cannot propagate across a branch and into the dslot. */
111 assert((env
->iflags
& (D_FLAG
| IMM_FLAG
)) != (D_FLAG
| IMM_FLAG
));
112 /* BIMM flag cannot be set without D_FLAG. */
113 assert((env
->iflags
& (D_FLAG
| BIMM_FLAG
)) != BIMM_FLAG
);
114 /* RTI flags are private to translate. */
115 assert(!(env
->iflags
& (DRTI_FLAG
| DRTE_FLAG
| DRTB_FLAG
)));
117 switch (cs
->exception_index
) {
119 if (!(cpu
->cfg
.pvr_regs
[0] & PVR0_USE_EXC_MASK
)) {
120 qemu_log_mask(LOG_GUEST_ERROR
,
121 "Exception raised on system without exceptions!\n");
125 qemu_log_mask(CPU_LOG_INT
,
126 "INT: HWE at pc=%08x msr=%08x iflags=%x\n",
127 env
->pc
, msr
, env
->iflags
);
129 /* Exception breaks branch + dslot sequence? */
132 if (env
->iflags
& D_FLAG
) {
134 env
->btr
= env
->btarget
;
137 /* Exception in progress. */
139 env
->regs
[17] = env
->pc
+ 4;
140 env
->pc
= cpu
->cfg
.base_vectors
+ 0x20;
144 qemu_log_mask(CPU_LOG_INT
,
145 "INT: MMU at pc=%08x msr=%08x "
146 "ear=%" PRIx64
" iflags=%x\n",
147 env
->pc
, msr
, env
->ear
, env
->iflags
);
149 /* Exception breaks branch + dslot sequence? */
152 if (env
->iflags
& D_FLAG
) {
154 env
->btr
= env
->btarget
;
155 /* Reexecute the branch. */
156 env
->regs
[17] = env
->pc
- (env
->iflags
& BIMM_FLAG
? 8 : 4);
157 } else if (env
->iflags
& IMM_FLAG
) {
158 /* Reexecute the imm. */
159 env
->regs
[17] = env
->pc
- 4;
161 env
->regs
[17] = env
->pc
;
164 /* Exception in progress. */
166 env
->pc
= cpu
->cfg
.base_vectors
+ 0x20;
170 assert(!(msr
& (MSR_EIP
| MSR_BIP
)));
171 assert(msr
& MSR_IE
);
172 assert(!(env
->iflags
& (D_FLAG
| IMM_FLAG
)));
174 qemu_log_mask(CPU_LOG_INT
,
175 "INT: DEV at pc=%08x msr=%08x iflags=%x\n",
176 env
->pc
, msr
, env
->iflags
);
179 /* Disable interrupts. */
181 env
->regs
[14] = env
->pc
;
182 env
->pc
= cpu
->cfg
.base_vectors
+ 0x10;
186 assert(!(env
->iflags
& (D_FLAG
| IMM_FLAG
)));
188 qemu_log_mask(CPU_LOG_INT
,
189 "INT: BRK at pc=%08x msr=%08x iflags=%x\n",
190 env
->pc
, msr
, env
->iflags
);
193 /* Break in progress. */
195 env
->regs
[16] = env
->pc
;
196 env
->pc
= cpu
->cfg
.base_vectors
+ 0x18;
200 cpu_abort(cs
, "unhandled exception type=%d\n", cs
->exception_index
);
204 /* Save previous mode, disable mmu, disable user-mode. */
205 t
= (msr
& (MSR_VM
| MSR_UM
)) << 1;
206 msr
&= ~(MSR_VMS
| MSR_UMS
| MSR_VM
| MSR_UM
);
208 mb_cpu_write_msr(env
, msr
);
210 env
->res_addr
= RES_ADDR_NONE
;
214 qemu_log_mask(CPU_LOG_INT
,
215 " to pc=%08x msr=%08x\n", env
->pc
, msr
);
216 } else if (env
->esr
& D_FLAG
) {
217 qemu_log_mask(CPU_LOG_INT
,
218 " to pc=%08x msr=%08x esr=%04x btr=%08x\n",
219 env
->pc
, msr
, env
->esr
, env
->btr
);
221 qemu_log_mask(CPU_LOG_INT
,
222 " to pc=%08x msr=%08x esr=%04x\n",
223 env
->pc
, msr
, env
->esr
);
227 hwaddr
mb_cpu_get_phys_page_attrs_debug(CPUState
*cs
, vaddr addr
,
230 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
231 CPUMBState
*env
= &cpu
->env
;
232 target_ulong vaddr
, paddr
= 0;
233 MicroBlazeMMULookup lu
;
234 int mmu_idx
= cpu_mmu_index(env
, false);
237 /* Caller doesn't initialize */
238 *attrs
= (MemTxAttrs
) {};
239 attrs
->secure
= mb_cpu_access_is_secure(cpu
, MMU_DATA_LOAD
);
241 if (mmu_idx
!= MMU_NOMMU_IDX
) {
242 hit
= mmu_translate(cpu
, &lu
, addr
, 0, 0);
244 vaddr
= addr
& TARGET_PAGE_MASK
;
245 paddr
= lu
.paddr
+ vaddr
- lu
.vaddr
;
247 paddr
= 0; /* ???. */
249 paddr
= addr
& TARGET_PAGE_MASK
;
254 bool mb_cpu_exec_interrupt(CPUState
*cs
, int interrupt_request
)
256 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
257 CPUMBState
*env
= &cpu
->env
;
259 if ((interrupt_request
& CPU_INTERRUPT_HARD
)
260 && (env
->msr
& MSR_IE
)
261 && !(env
->msr
& (MSR_EIP
| MSR_BIP
))
262 && !(env
->iflags
& (D_FLAG
| IMM_FLAG
))) {
263 cs
->exception_index
= EXCP_IRQ
;
264 mb_cpu_do_interrupt(cs
);
270 #endif /* !CONFIG_USER_ONLY */
272 void mb_cpu_do_unaligned_access(CPUState
*cs
, vaddr addr
,
273 MMUAccessType access_type
,
274 int mmu_idx
, uintptr_t retaddr
)
276 MicroBlazeCPU
*cpu
= MICROBLAZE_CPU(cs
);
277 uint32_t esr
, iflags
;
279 /* Recover the pc and iflags from the corresponding insn_start. */
280 cpu_restore_state(cs
, retaddr
);
281 iflags
= cpu
->env
.iflags
;
283 qemu_log_mask(CPU_LOG_INT
,
284 "Unaligned access addr=" TARGET_FMT_lx
" pc=%x iflags=%x\n",
285 (target_ulong
)addr
, cpu
->env
.pc
, iflags
);
287 esr
= ESR_EC_UNALIGNED_DATA
;
288 if (likely(iflags
& ESR_ESS_FLAG
)) {
289 esr
|= iflags
& ESR_ESS_MASK
;
291 qemu_log_mask(LOG_UNIMP
, "Unaligned access without ESR_ESS_FLAG\n");
296 cs
->exception_index
= EXCP_HW_EXCP
;