1fcf5ef2aSThomas Huth /*
2fcf5ef2aSThomas Huth * MicroBlaze helper routines.
3fcf5ef2aSThomas Huth *
4fcf5ef2aSThomas Huth * Copyright (c) 2009 Edgar E. Iglesias <edgar.iglesias@gmail.com>
5fcf5ef2aSThomas Huth * Copyright (c) 2009-2012 PetaLogix Qld Pty Ltd.
6fcf5ef2aSThomas Huth *
7fcf5ef2aSThomas Huth * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth * License as published by the Free Software Foundation; either
10ee452036SChetan Pant * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth *
12fcf5ef2aSThomas Huth * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15fcf5ef2aSThomas Huth * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth *
17fcf5ef2aSThomas Huth * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth */
20fcf5ef2aSThomas Huth
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23fcf5ef2aSThomas Huth #include "exec/exec-all.h"
24*74781c08SPhilippe Mathieu-Daudé #include "exec/page-protection.h"
25fcf5ef2aSThomas Huth #include "qemu/host-utils.h"
26fcf5ef2aSThomas Huth #include "exec/log.h"
27fcf5ef2aSThomas Huth
28fd297732SRichard Henderson #ifndef CONFIG_USER_ONLY
mb_cpu_access_is_secure(MicroBlazeCPU * cpu,MMUAccessType access_type)2943a9ede1SJoe Komlodi static bool mb_cpu_access_is_secure(MicroBlazeCPU *cpu,
3043a9ede1SJoe Komlodi MMUAccessType access_type)
3143a9ede1SJoe Komlodi {
3243a9ede1SJoe Komlodi if (access_type == MMU_INST_FETCH) {
3343a9ede1SJoe Komlodi return !cpu->ns_axi_ip;
3443a9ede1SJoe Komlodi } else {
3543a9ede1SJoe Komlodi return !cpu->ns_axi_dp;
3643a9ede1SJoe Komlodi }
3743a9ede1SJoe Komlodi }
3843a9ede1SJoe Komlodi
mb_cpu_tlb_fill(CPUState * cs,vaddr address,int size,MMUAccessType access_type,int mmu_idx,bool probe,uintptr_t retaddr)39f429d607SRichard Henderson bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,
40f429d607SRichard Henderson MMUAccessType access_type, int mmu_idx,
41f429d607SRichard Henderson bool probe, uintptr_t retaddr)
42fcf5ef2aSThomas Huth {
43fcf5ef2aSThomas Huth MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
44fcf5ef2aSThomas Huth CPUMBState *env = &cpu->env;
458ce97bc1SRichard Henderson MicroBlazeMMULookup lu;
46fcf5ef2aSThomas Huth unsigned int hit;
47fcf5ef2aSThomas Huth int prot;
4843a9ede1SJoe Komlodi MemTxAttrs attrs = {};
4943a9ede1SJoe Komlodi
5043a9ede1SJoe Komlodi attrs.secure = mb_cpu_access_is_secure(cpu, access_type);
51fcf5ef2aSThomas Huth
52f429d607SRichard Henderson if (mmu_idx == MMU_NOMMU_IDX) {
53f429d607SRichard Henderson /* MMU disabled or not available. */
54f429d607SRichard Henderson address &= TARGET_PAGE_MASK;
5586b7c551SBALATON Zoltan prot = PAGE_RWX;
5643a9ede1SJoe Komlodi tlb_set_page_with_attrs(cs, address, address, attrs, prot, mmu_idx,
5743a9ede1SJoe Komlodi TARGET_PAGE_SIZE);
58f429d607SRichard Henderson return true;
59f429d607SRichard Henderson }
60fcf5ef2aSThomas Huth
61de73ee1aSRichard Henderson hit = mmu_translate(cpu, &lu, address, access_type, mmu_idx);
62f429d607SRichard Henderson if (likely(hit)) {
63f429d607SRichard Henderson uint32_t vaddr = address & TARGET_PAGE_MASK;
64f429d607SRichard Henderson uint32_t paddr = lu.paddr + vaddr - lu.vaddr;
65fcf5ef2aSThomas Huth
66fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "MMU map mmu=%d v=%x p=%x prot=%x\n",
67fcf5ef2aSThomas Huth mmu_idx, vaddr, paddr, lu.prot);
6843a9ede1SJoe Komlodi tlb_set_page_with_attrs(cs, vaddr, paddr, attrs, lu.prot, mmu_idx,
6943a9ede1SJoe Komlodi TARGET_PAGE_SIZE);
70f429d607SRichard Henderson return true;
71f429d607SRichard Henderson }
72f429d607SRichard Henderson
73f429d607SRichard Henderson /* TLB miss. */
74f429d607SRichard Henderson if (probe) {
75f429d607SRichard Henderson return false;
76f429d607SRichard Henderson }
77f429d607SRichard Henderson
78fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_MMU, "mmu=%d miss v=%" VADDR_PRIx "\n",
79fcf5ef2aSThomas Huth mmu_idx, address);
80fcf5ef2aSThomas Huth
81b2e80a3cSRichard Henderson env->ear = address;
82fcf5ef2aSThomas Huth switch (lu.err) {
83fcf5ef2aSThomas Huth case ERR_PROT:
8478e9caf2SRichard Henderson env->esr = access_type == MMU_INST_FETCH ? 17 : 16;
8578e9caf2SRichard Henderson env->esr |= (access_type == MMU_DATA_STORE) << 10;
86fcf5ef2aSThomas Huth break;
87fcf5ef2aSThomas Huth case ERR_MISS:
8878e9caf2SRichard Henderson env->esr = access_type == MMU_INST_FETCH ? 19 : 18;
8978e9caf2SRichard Henderson env->esr |= (access_type == MMU_DATA_STORE) << 10;
90fcf5ef2aSThomas Huth break;
91fcf5ef2aSThomas Huth default:
92fcf5ef2aSThomas Huth abort();
93fcf5ef2aSThomas Huth }
94fcf5ef2aSThomas Huth
95fcf5ef2aSThomas Huth if (cs->exception_index == EXCP_MMU) {
96fcf5ef2aSThomas Huth cpu_abort(cs, "recursive faults\n");
97fcf5ef2aSThomas Huth }
98fcf5ef2aSThomas Huth
99fcf5ef2aSThomas Huth /* TLB miss. */
100fcf5ef2aSThomas Huth cs->exception_index = EXCP_MMU;
101f429d607SRichard Henderson cpu_loop_exit_restore(cs, retaddr);
102fcf5ef2aSThomas Huth }
103f429d607SRichard Henderson
mb_cpu_do_interrupt(CPUState * cs)104fcf5ef2aSThomas Huth void mb_cpu_do_interrupt(CPUState *cs)
105fcf5ef2aSThomas Huth {
106fcf5ef2aSThomas Huth MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
107fcf5ef2aSThomas Huth CPUMBState *env = &cpu->env;
1081074c0fbSRichard Henderson uint32_t t, msr = mb_cpu_read_msr(env);
109a9f61458SRichard Henderson bool set_esr;
110fcf5ef2aSThomas Huth
111fcf5ef2aSThomas Huth /* IMM flag cannot propagate across a branch and into the dslot. */
11288e74b61SRichard Henderson assert((env->iflags & (D_FLAG | IMM_FLAG)) != (D_FLAG | IMM_FLAG));
11388e74b61SRichard Henderson /* BIMM flag cannot be set without D_FLAG. */
11488e74b61SRichard Henderson assert((env->iflags & (D_FLAG | BIMM_FLAG)) != BIMM_FLAG);
11588e74b61SRichard Henderson /* RTI flags are private to translate. */
116fcf5ef2aSThomas Huth assert(!(env->iflags & (DRTI_FLAG | DRTE_FLAG | DRTB_FLAG)));
117a9f61458SRichard Henderson
118fcf5ef2aSThomas Huth switch (cs->exception_index) {
119fcf5ef2aSThomas Huth case EXCP_HW_EXCP:
120a4bcfc33SRichard Henderson if (!(cpu->cfg.pvr_regs[0] & PVR0_USE_EXC_MASK)) {
121a9f61458SRichard Henderson qemu_log_mask(LOG_GUEST_ERROR,
122a9f61458SRichard Henderson "Exception raised on system without exceptions!\n");
123fcf5ef2aSThomas Huth return;
124fcf5ef2aSThomas Huth }
125fcf5ef2aSThomas Huth
126a9f61458SRichard Henderson qemu_log_mask(CPU_LOG_INT,
127a9f61458SRichard Henderson "INT: HWE at pc=%08x msr=%08x iflags=%x\n",
128a9f61458SRichard Henderson env->pc, msr, env->iflags);
129fcf5ef2aSThomas Huth
130fcf5ef2aSThomas Huth /* Exception breaks branch + dslot sequence? */
131a9f61458SRichard Henderson set_esr = true;
132a9f61458SRichard Henderson env->esr &= ~D_FLAG;
133fcf5ef2aSThomas Huth if (env->iflags & D_FLAG) {
134a9f61458SRichard Henderson env->esr |= D_FLAG;
1356fbf78f2SRichard Henderson env->btr = env->btarget;
136fcf5ef2aSThomas Huth }
137fcf5ef2aSThomas Huth
138fcf5ef2aSThomas Huth /* Exception in progress. */
1391074c0fbSRichard Henderson msr |= MSR_EIP;
140a9f61458SRichard Henderson env->regs[17] = env->pc + 4;
14176e8187dSRichard Henderson env->pc = cpu->cfg.base_vectors + 0x20;
142fcf5ef2aSThomas Huth break;
143fcf5ef2aSThomas Huth
144fcf5ef2aSThomas Huth case EXCP_MMU:
145e3f8d192SRichard Henderson qemu_log_mask(CPU_LOG_INT,
146a9f61458SRichard Henderson "INT: MMU at pc=%08x msr=%08x "
147a9f61458SRichard Henderson "ear=%" PRIx64 " iflags=%x\n",
148a9f61458SRichard Henderson env->pc, msr, env->ear, env->iflags);
149e3f8d192SRichard Henderson
150fcf5ef2aSThomas Huth /* Exception breaks branch + dslot sequence? */
151a9f61458SRichard Henderson set_esr = true;
152a9f61458SRichard Henderson env->esr &= ~D_FLAG;
153fcf5ef2aSThomas Huth if (env->iflags & D_FLAG) {
154a9f61458SRichard Henderson env->esr |= D_FLAG;
1556fbf78f2SRichard Henderson env->btr = env->btarget;
156fcf5ef2aSThomas Huth /* Reexecute the branch. */
157a9f61458SRichard Henderson env->regs[17] = env->pc - (env->iflags & BIMM_FLAG ? 8 : 4);
158fcf5ef2aSThomas Huth } else if (env->iflags & IMM_FLAG) {
159a9f61458SRichard Henderson /* Reexecute the imm. */
160a9f61458SRichard Henderson env->regs[17] = env->pc - 4;
161a9f61458SRichard Henderson } else {
162a9f61458SRichard Henderson env->regs[17] = env->pc;
163fcf5ef2aSThomas Huth }
164fcf5ef2aSThomas Huth
165fcf5ef2aSThomas Huth /* Exception in progress. */
1661074c0fbSRichard Henderson msr |= MSR_EIP;
16776e8187dSRichard Henderson env->pc = cpu->cfg.base_vectors + 0x20;
168fcf5ef2aSThomas Huth break;
169fcf5ef2aSThomas Huth
170fcf5ef2aSThomas Huth case EXCP_IRQ:
1711074c0fbSRichard Henderson assert(!(msr & (MSR_EIP | MSR_BIP)));
1721074c0fbSRichard Henderson assert(msr & MSR_IE);
17388e74b61SRichard Henderson assert(!(env->iflags & (D_FLAG | IMM_FLAG)));
174fcf5ef2aSThomas Huth
175fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_INT,
176a9f61458SRichard Henderson "INT: DEV at pc=%08x msr=%08x iflags=%x\n",
177a9f61458SRichard Henderson env->pc, msr, env->iflags);
178a9f61458SRichard Henderson set_esr = false;
179fcf5ef2aSThomas Huth
180a9f61458SRichard Henderson /* Disable interrupts. */
181a9f61458SRichard Henderson msr &= ~MSR_IE;
18276e8187dSRichard Henderson env->regs[14] = env->pc;
18376e8187dSRichard Henderson env->pc = cpu->cfg.base_vectors + 0x10;
184fcf5ef2aSThomas Huth break;
185fcf5ef2aSThomas Huth
186fcf5ef2aSThomas Huth case EXCP_HW_BREAK:
18788e74b61SRichard Henderson assert(!(env->iflags & (D_FLAG | IMM_FLAG)));
18888e74b61SRichard Henderson
189fcf5ef2aSThomas Huth qemu_log_mask(CPU_LOG_INT,
190a9f61458SRichard Henderson "INT: BRK at pc=%08x msr=%08x iflags=%x\n",
191a9f61458SRichard Henderson env->pc, msr, env->iflags);
192a9f61458SRichard Henderson set_esr = false;
193a9f61458SRichard Henderson
194a9f61458SRichard Henderson /* Break in progress. */
1951074c0fbSRichard Henderson msr |= MSR_BIP;
19676e8187dSRichard Henderson env->regs[16] = env->pc;
19776e8187dSRichard Henderson env->pc = cpu->cfg.base_vectors + 0x18;
198fcf5ef2aSThomas Huth break;
199a9f61458SRichard Henderson
200fcf5ef2aSThomas Huth default:
201a9f61458SRichard Henderson cpu_abort(cs, "unhandled exception type=%d\n", cs->exception_index);
202a9f61458SRichard Henderson /* not reached */
203a9f61458SRichard Henderson }
204a9f61458SRichard Henderson
205a9f61458SRichard Henderson /* Save previous mode, disable mmu, disable user-mode. */
206a9f61458SRichard Henderson t = (msr & (MSR_VM | MSR_UM)) << 1;
207a9f61458SRichard Henderson msr &= ~(MSR_VMS | MSR_UMS | MSR_VM | MSR_UM);
208a9f61458SRichard Henderson msr |= t;
209a9f61458SRichard Henderson mb_cpu_write_msr(env, msr);
210a9f61458SRichard Henderson
211a9f61458SRichard Henderson env->res_addr = RES_ADDR_NONE;
212a9f61458SRichard Henderson env->iflags = 0;
213a9f61458SRichard Henderson
214a9f61458SRichard Henderson if (!set_esr) {
215a9f61458SRichard Henderson qemu_log_mask(CPU_LOG_INT,
216a9f61458SRichard Henderson " to pc=%08x msr=%08x\n", env->pc, msr);
217a9f61458SRichard Henderson } else if (env->esr & D_FLAG) {
218a9f61458SRichard Henderson qemu_log_mask(CPU_LOG_INT,
219a9f61458SRichard Henderson " to pc=%08x msr=%08x esr=%04x btr=%08x\n",
220a9f61458SRichard Henderson env->pc, msr, env->esr, env->btr);
221a9f61458SRichard Henderson } else {
222a9f61458SRichard Henderson qemu_log_mask(CPU_LOG_INT,
223a9f61458SRichard Henderson " to pc=%08x msr=%08x esr=%04x\n",
224a9f61458SRichard Henderson env->pc, msr, env->esr);
225fcf5ef2aSThomas Huth }
226fcf5ef2aSThomas Huth }
227fcf5ef2aSThomas Huth
mb_cpu_get_phys_page_attrs_debug(CPUState * cs,vaddr addr,MemTxAttrs * attrs)22843a9ede1SJoe Komlodi hwaddr mb_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
22943a9ede1SJoe Komlodi MemTxAttrs *attrs)
230fcf5ef2aSThomas Huth {
231fcf5ef2aSThomas Huth MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
232fcf5ef2aSThomas Huth target_ulong vaddr, paddr = 0;
2338ce97bc1SRichard Henderson MicroBlazeMMULookup lu;
2343b916140SRichard Henderson int mmu_idx = cpu_mmu_index(cs, false);
235fcf5ef2aSThomas Huth unsigned int hit;
236fcf5ef2aSThomas Huth
23743a9ede1SJoe Komlodi /* Caller doesn't initialize */
23843a9ede1SJoe Komlodi *attrs = (MemTxAttrs) {};
23943a9ede1SJoe Komlodi attrs->secure = mb_cpu_access_is_secure(cpu, MMU_DATA_LOAD);
24043a9ede1SJoe Komlodi
241d10367e0SEdgar E. Iglesias if (mmu_idx != MMU_NOMMU_IDX) {
242de73ee1aSRichard Henderson hit = mmu_translate(cpu, &lu, addr, 0, 0);
243fcf5ef2aSThomas Huth if (hit) {
244fcf5ef2aSThomas Huth vaddr = addr & TARGET_PAGE_MASK;
245fcf5ef2aSThomas Huth paddr = lu.paddr + vaddr - lu.vaddr;
246fcf5ef2aSThomas Huth } else
247fcf5ef2aSThomas Huth paddr = 0; /* ???. */
248fcf5ef2aSThomas Huth } else
249fcf5ef2aSThomas Huth paddr = addr & TARGET_PAGE_MASK;
250fcf5ef2aSThomas Huth
251fcf5ef2aSThomas Huth return paddr;
252fcf5ef2aSThomas Huth }
253fcf5ef2aSThomas Huth
mb_cpu_exec_interrupt(CPUState * cs,int interrupt_request)254fcf5ef2aSThomas Huth bool mb_cpu_exec_interrupt(CPUState *cs, int interrupt_request)
255fcf5ef2aSThomas Huth {
256da953643SPhilippe Mathieu-Daudé CPUMBState *env = cpu_env(cs);
257fcf5ef2aSThomas Huth
258fcf5ef2aSThomas Huth if ((interrupt_request & CPU_INTERRUPT_HARD)
2592e5282caSRichard Henderson && (env->msr & MSR_IE)
2602e5282caSRichard Henderson && !(env->msr & (MSR_EIP | MSR_BIP))
261fcf5ef2aSThomas Huth && !(env->iflags & (D_FLAG | IMM_FLAG))) {
262fcf5ef2aSThomas Huth cs->exception_index = EXCP_IRQ;
263fcf5ef2aSThomas Huth mb_cpu_do_interrupt(cs);
264fcf5ef2aSThomas Huth return true;
265fcf5ef2aSThomas Huth }
266fcf5ef2aSThomas Huth return false;
267fcf5ef2aSThomas Huth }
268ab0c8d0fSRichard Henderson
269eb3ef313SPhilippe Mathieu-Daudé #endif /* !CONFIG_USER_ONLY */
270eb3ef313SPhilippe Mathieu-Daudé
mb_cpu_do_unaligned_access(CPUState * cs,vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr)271ab0c8d0fSRichard Henderson void mb_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
272ab0c8d0fSRichard Henderson MMUAccessType access_type,
273ab0c8d0fSRichard Henderson int mmu_idx, uintptr_t retaddr)
274ab0c8d0fSRichard Henderson {
275ab0c8d0fSRichard Henderson MicroBlazeCPU *cpu = MICROBLAZE_CPU(cs);
276ab0c8d0fSRichard Henderson uint32_t esr, iflags;
277ab0c8d0fSRichard Henderson
278ab0c8d0fSRichard Henderson /* Recover the pc and iflags from the corresponding insn_start. */
2793d419a4dSRichard Henderson cpu_restore_state(cs, retaddr);
280ab0c8d0fSRichard Henderson iflags = cpu->env.iflags;
281ab0c8d0fSRichard Henderson
282ab0c8d0fSRichard Henderson qemu_log_mask(CPU_LOG_INT,
28319f27b6cSRichard Henderson "Unaligned access addr=" TARGET_FMT_lx " pc=%x iflags=%x\n",
28419f27b6cSRichard Henderson (target_ulong)addr, cpu->env.pc, iflags);
285ab0c8d0fSRichard Henderson
286ab0c8d0fSRichard Henderson esr = ESR_EC_UNALIGNED_DATA;
287ab0c8d0fSRichard Henderson if (likely(iflags & ESR_ESS_FLAG)) {
288ab0c8d0fSRichard Henderson esr |= iflags & ESR_ESS_MASK;
289ab0c8d0fSRichard Henderson } else {
290ab0c8d0fSRichard Henderson qemu_log_mask(LOG_UNIMP, "Unaligned access without ESR_ESS_FLAG\n");
291ab0c8d0fSRichard Henderson }
292ab0c8d0fSRichard Henderson
293ab0c8d0fSRichard Henderson cpu->env.ear = addr;
294ab0c8d0fSRichard Henderson cpu->env.esr = esr;
295ab0c8d0fSRichard Henderson cs->exception_index = EXCP_HW_EXCP;
296ab0c8d0fSRichard Henderson cpu_loop_exit(cs);
297ab0c8d0fSRichard Henderson }
298