target/mips: Convert MSA 3R instruction format to decodetree (part 3/4)
[qemu/rayw.git] / target / microblaze / helper.c
blobdd2aecd1d582832da16f1ef2a63b6a8e44f4caf5
1 /*
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"
22 #include "cpu.h"
23 #include "exec/exec-all.h"
24 #include "qemu/host-utils.h"
25 #include "exec/log.h"
27 #if defined(CONFIG_USER_ONLY)
29 bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
30 MMUAccessType access_type, int mmu_idx,
31 bool probe, uintptr_t retaddr)
33 cs->exception_index = 0xaa;
34 cpu_loop_exit_restore(cs, retaddr);
37 #else /* !CONFIG_USER_ONLY */
39 static bool mb_cpu_access_is_secure(MicroBlazeCPU *cpu,
40 MMUAccessType access_type)
42 if (access_type == MMU_INST_FETCH) {
43 return !cpu->ns_axi_ip;
44 } else {
45 return !cpu->ns_axi_dp;
49 bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
50 MMUAccessType access_type, int mmu_idx,
51 bool probe, uintptr_t retaddr)
53 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
54 CPUMBState *env = &cpu->env;
55 MicroBlazeMMULookup lu;
56 unsigned int hit;
57 int prot;
58 MemTxAttrs attrs = {};
60 attrs.secure = mb_cpu_access_is_secure(cpu, access_type);
62 if (mmu_idx == MMU_NOMMU_IDX) {
63 /* MMU disabled or not available. */
64 address &= TARGET_PAGE_MASK;
65 prot = PAGE_BITS;
66 tlb_set_page_with_attrs(cs, address, address, attrs, prot, mmu_idx,
67 TARGET_PAGE_SIZE);
68 return true;
71 hit = mmu_translate(cpu, &lu, address, access_type, mmu_idx);
72 if (likely(hit)) {
73 uint32_t vaddr = address & TARGET_PAGE_MASK;
74 uint32_t paddr = lu.paddr + vaddr - lu.vaddr;
76 qemu_log_mask(CPU_LOG_MMU, "MMU map mmu=%d v=%x p=%x prot=%x\n",
77 mmu_idx, vaddr, paddr, lu.prot);
78 tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, mmu_idx,
79 TARGET_PAGE_SIZE);
80 return true;
83 /* TLB miss. */
84 if (probe) {
85 return false;
88 qemu_log_mask(CPU_LOG_MMU, "mmu=%d miss v=%" VADDR_PRIx "\n",
89 mmu_idx, address);
91 env->ear = address;
92 switch (lu.err) {
93 case ERR_PROT:
94 env->esr = access_type == MMU_INST_FETCH ? 17 : 16;
95 env->esr |= (access_type == MMU_DATA_STORE) << 10;
96 break;
97 case ERR_MISS:
98 env->esr = access_type == MMU_INST_FETCH ? 19 : 18;
99 env->esr |= (access_type == MMU_DATA_STORE) << 10;
100 break;
101 default:
102 abort();
105 if (cs->exception_index == EXCP_MMU) {
106 cpu_abort(cs, "recursive faults\n");
109 /* TLB miss. */
110 cs->exception_index = EXCP_MMU;
111 cpu_loop_exit_restore(cs, retaddr);
114 void mb_cpu_do_interrupt(CPUState *cs)
116 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
117 CPUMBState *env = &cpu->env;
118 uint32_t t, msr = mb_cpu_read_msr(env);
119 bool set_esr;
121 /* IMM flag cannot propagate across a branch and into the dslot. */
122 assert((env->iflags & (D_FLAG | IMM_FLAG)) != (D_FLAG | IMM_FLAG));
123 /* BIMM flag cannot be set without D_FLAG. */
124 assert((env->iflags & (D_FLAG | BIMM_FLAG)) != BIMM_FLAG);
125 /* RTI flags are private to translate. */
126 assert(!(env->iflags & (DRTI_FLAG | DRTE_FLAG | DRTB_FLAG)));
128 switch (cs->exception_index) {
129 case EXCP_HW_EXCP:
130 if (!(cpu->cfg.pvr_regs[0] & PVR0_USE_EXC_MASK)) {
131 qemu_log_mask(LOG_GUEST_ERROR,
132 "Exception raised on system without exceptions!\n");
133 return;
136 qemu_log_mask(CPU_LOG_INT,
137 "INT: HWE at pc=%08x msr=%08x iflags=%x\n",
138 env->pc, msr, env->iflags);
140 /* Exception breaks branch + dslot sequence? */
141 set_esr = true;
142 env->esr &= ~D_FLAG;
143 if (env->iflags & D_FLAG) {
144 env->esr |= D_FLAG;
145 env->btr = env->btarget;
148 /* Exception in progress. */
149 msr |= MSR_EIP;
150 env->regs[17] = env->pc + 4;
151 env->pc = cpu->cfg.base_vectors + 0x20;
152 break;
154 case EXCP_MMU:
155 qemu_log_mask(CPU_LOG_INT,
156 "INT: MMU at pc=%08x msr=%08x "
157 "ear=%" PRIx64 " iflags=%x\n",
158 env->pc, msr, env->ear, env->iflags);
160 /* Exception breaks branch + dslot sequence? */
161 set_esr = true;
162 env->esr &= ~D_FLAG;
163 if (env->iflags & D_FLAG) {
164 env->esr |= D_FLAG;
165 env->btr = env->btarget;
166 /* Reexecute the branch. */
167 env->regs[17] = env->pc - (env->iflags & BIMM_FLAG ? 8 : 4);
168 } else if (env->iflags & IMM_FLAG) {
169 /* Reexecute the imm. */
170 env->regs[17] = env->pc - 4;
171 } else {
172 env->regs[17] = env->pc;
175 /* Exception in progress. */
176 msr |= MSR_EIP;
177 env->pc = cpu->cfg.base_vectors + 0x20;
178 break;
180 case EXCP_IRQ:
181 assert(!(msr & (MSR_EIP | MSR_BIP)));
182 assert(msr & MSR_IE);
183 assert(!(env->iflags & (D_FLAG | IMM_FLAG)));
185 qemu_log_mask(CPU_LOG_INT,
186 "INT: DEV at pc=%08x msr=%08x iflags=%x\n",
187 env->pc, msr, env->iflags);
188 set_esr = false;
190 /* Disable interrupts. */
191 msr &= ~MSR_IE;
192 env->regs[14] = env->pc;
193 env->pc = cpu->cfg.base_vectors + 0x10;
194 break;
196 case EXCP_HW_BREAK:
197 assert(!(env->iflags & (D_FLAG | IMM_FLAG)));
199 qemu_log_mask(CPU_LOG_INT,
200 "INT: BRK at pc=%08x msr=%08x iflags=%x\n",
201 env->pc, msr, env->iflags);
202 set_esr = false;
204 /* Break in progress. */
205 msr |= MSR_BIP;
206 env->regs[16] = env->pc;
207 env->pc = cpu->cfg.base_vectors + 0x18;
208 break;
210 default:
211 cpu_abort(cs, "unhandled exception type=%d\n", cs->exception_index);
212 /* not reached */
215 /* Save previous mode, disable mmu, disable user-mode. */
216 t = (msr & (MSR_VM | MSR_UM)) << 1;
217 msr &= ~(MSR_VMS | MSR_UMS | MSR_VM | MSR_UM);
218 msr |= t;
219 mb_cpu_write_msr(env, msr);
221 env->res_addr = RES_ADDR_NONE;
222 env->iflags = 0;
224 if (!set_esr) {
225 qemu_log_mask(CPU_LOG_INT,
226 " to pc=%08x msr=%08x\n", env->pc, msr);
227 } else if (env->esr & D_FLAG) {
228 qemu_log_mask(CPU_LOG_INT,
229 " to pc=%08x msr=%08x esr=%04x btr=%08x\n",
230 env->pc, msr, env->esr, env->btr);
231 } else {
232 qemu_log_mask(CPU_LOG_INT,
233 " to pc=%08x msr=%08x esr=%04x\n",
234 env->pc, msr, env->esr);
238 hwaddr mb_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
239 MemTxAttrs *attrs)
241 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
242 CPUMBState *env = &cpu->env;
243 target_ulong vaddr, paddr = 0;
244 MicroBlazeMMULookup lu;
245 int mmu_idx = cpu_mmu_index(env, false);
246 unsigned int hit;
248 /* Caller doesn't initialize */
249 *attrs = (MemTxAttrs) {};
250 attrs->secure = mb_cpu_access_is_secure(cpu, MMU_DATA_LOAD);
252 if (mmu_idx != MMU_NOMMU_IDX) {
253 hit = mmu_translate(cpu, &lu, addr, 0, 0);
254 if (hit) {
255 vaddr = addr & TARGET_PAGE_MASK;
256 paddr = lu.paddr + vaddr - lu.vaddr;
257 } else
258 paddr = 0; /* ???. */
259 } else
260 paddr = addr & TARGET_PAGE_MASK;
262 return paddr;
265 bool mb_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
267 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
268 CPUMBState *env = &cpu->env;
270 if ((interrupt_request & CPU_INTERRUPT_HARD)
271 && (env->msr & MSR_IE)
272 && !(env->msr & (MSR_EIP | MSR_BIP))
273 && !(env->iflags & (D_FLAG | IMM_FLAG))) {
274 cs->exception_index = EXCP_IRQ;
275 mb_cpu_do_interrupt(cs);
276 return true;
278 return false;
281 #endif /* !CONFIG_USER_ONLY */
283 void mb_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
284 MMUAccessType access_type,
285 int mmu_idx, uintptr_t retaddr)
287 MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
288 uint32_t esr, iflags;
290 /* Recover the pc and iflags from the corresponding insn_start. */
291 cpu_restore_state(cs, retaddr, true);
292 iflags = cpu->env.iflags;
294 qemu_log_mask(CPU_LOG_INT,
295 "Unaligned access addr=" TARGET_FMT_lx " pc=%x iflags=%x\n",
296 (target_ulong)addr, cpu->env.pc, iflags);
298 esr = ESR_EC_UNALIGNED_DATA;
299 if (likely(iflags & ESR_ESS_FLAG)) {
300 esr |= iflags & ESR_ESS_MASK;
301 } else {
302 qemu_log_mask(LOG_UNIMP, "Unaligned access without ESR_ESS_FLAG\n");
305 cpu->env.ear = addr;
306 cpu->env.esr = esr;
307 cs->exception_index = EXCP_HW_EXCP;
308 cpu_loop_exit(cs);