8 #if defined(CONFIG_USER_ONLY)
10 void do_interrupt (CPUState *env)
12 env->exception_index = -1;
15 int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
16 int is_user, int is_softmmu)
19 env->exception_index = EXCP_PREFETCH_ABORT;
20 env->cp15.c6_insn = address;
22 env->exception_index = EXCP_DATA_ABORT;
23 env->cp15.c6_data = address;
28 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
33 /* These should probably raise undefined insn exceptions. */
34 void helper_set_cp15(CPUState *env, uint32_t insn, uint32_t val)
36 cpu_abort(env, "cp15 insn %08x\n", insn);
39 uint32_t helper_get_cp15(CPUState *env, uint32_t insn)
41 cpu_abort(env, "cp15 insn %08x\n", insn);
45 void switch_mode(CPUState *env, int mode)
47 if (mode != ARM_CPU_MODE_USR)
48 cpu_abort(env, "Tried to switch out of user mode\n");
53 /* Map CPU modes onto saved register banks. */
54 static inline int bank_number (int mode)
57 case ARM_CPU_MODE_USR:
58 case ARM_CPU_MODE_SYS:
60 case ARM_CPU_MODE_SVC:
62 case ARM_CPU_MODE_ABT:
64 case ARM_CPU_MODE_UND:
66 case ARM_CPU_MODE_IRQ:
68 case ARM_CPU_MODE_FIQ:
71 cpu_abort(cpu_single_env, "Bad mode %x\n", mode);
75 void switch_mode(CPUState *env, int mode)
80 old_mode = env->uncached_cpsr & CPSR_M;
84 if (old_mode == ARM_CPU_MODE_FIQ) {
85 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
86 memcpy (env->regs, env->usr_regs + 8, 5 * sizeof(uint32_t));
87 } else if (mode == ARM_CPU_MODE_FIQ) {
88 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
89 memcpy (env->regs, env->fiq_regs + 8, 5 * sizeof(uint32_t));
92 i = bank_number(old_mode);
93 env->banked_r13[i] = env->regs[13];
94 env->banked_r14[i] = env->regs[14];
95 env->banked_spsr[i] = env->spsr;
97 i = bank_number(mode);
98 env->regs[13] = env->banked_r13[i];
99 env->regs[14] = env->banked_r14[i];
100 env->spsr = env->banked_spsr[i];
103 /* Handle a CPU exception. */
104 void do_interrupt(CPUARMState *env)
111 /* TODO: Vectored interrupt controller. */
112 switch (env->exception_index) {
114 new_mode = ARM_CPU_MODE_UND;
123 new_mode = ARM_CPU_MODE_SVC;
126 /* The PC already points to the next instructon. */
129 case EXCP_PREFETCH_ABORT:
130 new_mode = ARM_CPU_MODE_ABT;
132 mask = CPSR_A | CPSR_I;
135 case EXCP_DATA_ABORT:
136 new_mode = ARM_CPU_MODE_ABT;
138 mask = CPSR_A | CPSR_I;
142 new_mode = ARM_CPU_MODE_IRQ;
144 /* Disable IRQ and imprecise data aborts. */
145 mask = CPSR_A | CPSR_I;
149 new_mode = ARM_CPU_MODE_FIQ;
151 /* Disable FIQ, IRQ and imprecise data aborts. */
152 mask = CPSR_A | CPSR_I | CPSR_F;
156 cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index);
157 return; /* Never happens. Keep compiler happy. */
160 if (env->cp15.c1_sys & (1 << 13)) {
163 switch_mode (env, new_mode);
164 env->spsr = cpsr_read(env);
165 /* Switch to the new mode, and switch to Arm mode. */
166 /* ??? Thumb interrupt handlers not implemented. */
167 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
168 env->uncached_cpsr |= mask;
170 env->regs[14] = env->regs[15] + offset;
171 env->regs[15] = addr;
172 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
175 /* Check section/page access permissions.
176 Returns the page protection flags, or zero if the access is not
178 static inline int check_ap(CPUState *env, int ap, int domain, int access_type,
182 return PAGE_READ | PAGE_WRITE;
186 if (access_type != 1)
188 switch ((env->cp15.c1_sys >> 8) & 3) {
190 return is_user ? 0 : PAGE_READ;
197 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
200 return (access_type == 1) ? 0 : PAGE_READ;
202 return PAGE_READ | PAGE_WRITE;
204 return PAGE_READ | PAGE_WRITE;
210 static int get_phys_addr(CPUState *env, uint32_t address, int access_type,
211 int is_user, uint32_t *phys_ptr, int *prot)
221 /* Fast Context Switch Extension. */
222 if (address < 0x02000000)
223 address += env->cp15.c13_fcse;
225 if ((env->cp15.c1_sys & 1) == 0) {
228 *prot = PAGE_READ | PAGE_WRITE;
230 /* Pagetable walk. */
231 /* Lookup l1 descriptor. */
232 table = (env->cp15.c2 & 0xffffc000) | ((address >> 18) & 0x3ffc);
233 desc = ldl_phys(table);
235 domain = (env->cp15.c3 >> ((desc >> 4) & 0x1e)) & 3;
237 /* Secton translation fault. */
241 if (domain == 0 || domain == 2) {
243 code = 9; /* Section domain fault. */
245 code = 11; /* Page domain fault. */
250 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
251 ap = (desc >> 10) & 3;
254 /* Lookup l2 entry. */
255 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
256 desc = ldl_phys(table);
258 case 0: /* Page translation fault. */
261 case 1: /* 64k page. */
262 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
263 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
265 case 2: /* 4k page. */
266 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
267 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
269 case 3: /* 1k page. */
271 /* Page translation fault. */
275 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
276 ap = (desc >> 4) & 3;
279 /* Never happens, but compiler isn't smart enough to tell. */
284 *prot = check_ap(env, ap, domain, access_type, is_user);
286 /* Access permission fault. */
289 *phys_ptr = phys_addr;
293 return code | (domain << 4);
296 int cpu_arm_handle_mmu_fault (CPUState *env, target_ulong address,
297 int access_type, int is_user, int is_softmmu)
303 ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot);
305 /* Map a single [sub]page. */
306 phys_addr &= ~(uint32_t)0x3ff;
307 address &= ~(uint32_t)0x3ff;
308 return tlb_set_page (env, address, phys_addr, prot, is_user,
312 if (access_type == 2) {
313 env->cp15.c5_insn = ret;
314 env->cp15.c6_insn = address;
315 env->exception_index = EXCP_PREFETCH_ABORT;
317 env->cp15.c5_data = ret;
318 env->cp15.c6_data = address;
319 env->exception_index = EXCP_DATA_ABORT;
324 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
330 ret = get_phys_addr(env, addr, 0, 0, &phys_addr, &prot);
338 void helper_set_cp15(CPUState *env, uint32_t insn, uint32_t val)
342 op2 = (insn >> 5) & 7;
343 switch ((insn >> 16) & 0xf) {
344 case 0: /* ID codes. */
346 case 1: /* System configuration. */
349 env->cp15.c1_sys = val;
350 /* ??? Lots of these bits are not implemented. */
351 /* This may enable/disable the MMU, so do a TLB flush. */
355 env->cp15.c1_coproc = val;
356 /* ??? Is this safe when called from within a TB? */
362 case 2: /* MMU Page table control. */
365 case 3: /* MMU Domain access control. */
368 case 4: /* Reserved. */
370 case 5: /* MMU Fault status. */
373 env->cp15.c5_data = val;
376 env->cp15.c5_insn = val;
382 case 6: /* MMU Fault address. */
385 env->cp15.c6_data = val;
388 env->cp15.c6_insn = val;
394 case 7: /* Cache control. */
395 /* No cache, so nothing to do. */
397 case 8: /* MMU TLB control. */
399 case 0: /* Invalidate all. */
402 case 1: /* Invalidate single TLB entry. */
404 /* ??? This is wrong for large pages and sections. */
405 /* As an ugly hack to make linux work we always flush a 4K
408 tlb_flush_page(env, val);
409 tlb_flush_page(env, val + 0x400);
410 tlb_flush_page(env, val + 0x800);
411 tlb_flush_page(env, val + 0xc00);
420 case 9: /* Cache lockdown. */
423 env->cp15.c9_data = val;
426 env->cp15.c9_insn = val;
432 case 10: /* MMU TLB lockdown. */
433 /* ??? TLB lockdown not implemented. */
435 case 11: /* TCM DMA control. */
436 case 12: /* Reserved. */
438 case 13: /* Process ID. */
441 env->cp15.c9_data = val;
444 env->cp15.c9_insn = val;
450 case 14: /* Reserved. */
452 case 15: /* Implementation specific. */
453 /* ??? Internal registers not implemented. */
458 /* ??? For debugging only. Should raise illegal instruction exception. */
459 cpu_abort(env, "Unimplemented cp15 register read\n");
462 uint32_t helper_get_cp15(CPUState *env, uint32_t insn)
466 op2 = (insn >> 5) & 7;
467 switch ((insn >> 16) & 0xf) {
468 case 0: /* ID codes. */
470 default: /* Device ID. */
472 case 1: /* Cache Type. */
474 case 2: /* TCM status. */
477 case 1: /* System configuration. */
479 case 0: /* Control register. */
480 return env->cp15.c1_sys;
481 case 1: /* Auxiliary control register. */
483 case 2: /* Coprocessor access register. */
484 return env->cp15.c1_coproc;
488 case 2: /* MMU Page table control. */
490 case 3: /* MMU Domain access control. */
492 case 4: /* Reserved. */
494 case 5: /* MMU Fault status. */
497 return env->cp15.c5_data;
499 return env->cp15.c5_insn;
503 case 6: /* MMU Fault address. */
506 return env->cp15.c6_data;
508 return env->cp15.c6_insn;
512 case 7: /* Cache control. */
513 /* ??? This is for test, clean and invaidate operations that set the
514 Z flag. We can't represent N = Z = 1, so it also clears clears
515 the N flag. Oh well. */
518 case 8: /* MMU TLB control. */
520 case 9: /* Cache lockdown. */
523 return env->cp15.c9_data;
525 return env->cp15.c9_insn;
529 case 10: /* MMU TLB lockdown. */
530 /* ??? TLB lockdown not implemented. */
532 case 11: /* TCM DMA control. */
533 case 12: /* Reserved. */
535 case 13: /* Process ID. */
538 return env->cp15.c13_fcse;
540 return env->cp15.c13_context;
544 case 14: /* Reserved. */
546 case 15: /* Implementation specific. */
547 /* ??? Internal registers not implemented. */
551 /* ??? For debugging only. Should raise illegal instruction exception. */
552 cpu_abort(env, "Unimplemented cp15 register read\n");