2 * MIPS emulation helpers for qemu.
4 * Copyright (c) 2004-2005 Jocelyn Mayer
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 /* MIPS32 4K MMU emulation */
40 #ifdef MIPS_USES_R4K_TLB
41 static int map_address (CPUState *env, target_ulong *physical, int *prot,
42 target_ulong address, int rw, int access_type)
44 uint8_t ASID = env->CP0_EntryHi & 0xFF;
47 for (i = 0; i < env->tlb_in_use; i++) {
48 tlb_t *tlb = &env->tlb[i];
49 /* 1k pages are not supported. */
50 target_ulong mask = tlb->PageMask | 0x1FFF;
51 target_ulong tag = address & ~mask;
54 /* Check ASID, virtual page number & size */
55 if ((tlb->G == 1 || tlb->ASID == ASID) &&
58 n = !!(address & mask & ~(mask >> 1));
59 /* Check access rights */
60 if (!(n ? tlb->V1 : tlb->V0))
61 return TLBRET_INVALID;
62 if (rw == 0 || (n ? tlb->D1 : tlb->D0)) {
63 *physical = tlb->PFN[n] | (address & (mask >> 1));
65 if (n ? tlb->D1 : tlb->D0)
72 return TLBRET_NOMATCH;
76 static int get_physical_address (CPUState *env, target_ulong *physical,
77 int *prot, target_ulong address,
78 int rw, int access_type)
80 /* User mode can only access useg/xuseg */
81 int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM;
83 int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0;
84 int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0;
85 int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0;
87 int ret = TLBRET_MATCH;
91 fprintf(logfile, "user mode %d h %08x\n",
92 user_mode, env->hflags);
97 if (user_mode && address > 0x3FFFFFFFFFFFFFFFULL)
98 return TLBRET_BADADDR;
100 if (user_mode && address > 0x7FFFFFFFUL)
101 return TLBRET_BADADDR;
104 if (address <= (int32_t)0x7FFFFFFFUL) {
106 if (!(env->CP0_Status & (1 << CP0St_ERL) && user_mode)) {
107 #ifdef MIPS_USES_R4K_TLB
108 ret = map_address(env, physical, prot, address, rw, access_type);
110 *physical = address + 0x40000000UL;
111 *prot = PAGE_READ | PAGE_WRITE;
115 *prot = PAGE_READ | PAGE_WRITE;
120 - PABITS = 36 (correct for MIPS64R1)
123 } else if (address < 0x3FFFFFFFFFFFFFFFULL) {
125 if (UX && address < 0x000000FFFFFFFFFFULL) {
126 ret = map_address(env, physical, prot, address, rw, access_type);
128 ret = TLBRET_BADADDR;
130 } else if (address < 0x7FFFFFFFFFFFFFFFULL) {
132 if (SX && address < 0x400000FFFFFFFFFFULL) {
133 ret = map_address(env, physical, prot, address, rw, access_type);
135 ret = TLBRET_BADADDR;
137 } else if (address < 0xBFFFFFFFFFFFFFFFULL) {
139 /* XXX: check supervisor mode */
140 if (KX && (address & 0x03FFFFFFFFFFFFFFULL) < 0X0000000FFFFFFFFFULL)
142 *physical = address & 0X000000FFFFFFFFFFULL;
143 *prot = PAGE_READ | PAGE_WRITE;
145 ret = TLBRET_BADADDR;
147 } else if (address < 0xFFFFFFFF7FFFFFFFULL) {
149 /* XXX: check supervisor mode */
150 if (KX && address < 0xC00000FF7FFFFFFFULL) {
151 ret = map_address(env, physical, prot, address, rw, access_type);
153 ret = TLBRET_BADADDR;
156 } else if (address < (int32_t)0xA0000000UL) {
158 /* XXX: check supervisor mode */
159 *physical = address - (int32_t)0x80000000UL;
160 *prot = PAGE_READ | PAGE_WRITE;
161 } else if (address < (int32_t)0xC0000000UL) {
163 /* XXX: check supervisor mode */
164 *physical = address - (int32_t)0xA0000000UL;
165 *prot = PAGE_READ | PAGE_WRITE;
166 } else if (address < (int32_t)0xE0000000UL) {
168 #ifdef MIPS_USES_R4K_TLB
169 ret = map_address(env, physical, prot, address, rw, access_type);
171 *physical = address & 0xFFFFFFFF;
172 *prot = PAGE_READ | PAGE_WRITE;
176 /* XXX: check supervisor mode */
177 /* XXX: debug segment is not emulated */
178 #ifdef MIPS_USES_R4K_TLB
179 ret = map_address(env, physical, prot, address, rw, access_type);
181 *physical = address & 0xFFFFFFFF;
182 *prot = PAGE_READ | PAGE_WRITE;
187 fprintf(logfile, TARGET_FMT_lx " %d %d => " TARGET_FMT_lx " %d (%d)\n",
188 address, rw, access_type, *physical, *prot, ret);
195 #if defined(CONFIG_USER_ONLY)
196 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
201 target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
203 target_ulong phys_addr;
206 if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
211 void cpu_mips_init_mmu (CPUState *env)
214 #endif /* !defined(CONFIG_USER_ONLY) */
216 int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
217 int is_user, int is_softmmu)
219 target_ulong physical;
221 int exception = 0, error_code = 0;
227 cpu_dump_state(env, logfile, fprintf, 0);
229 fprintf(logfile, "%s pc " TARGET_FMT_lx " ad " TARGET_FMT_lx " rw %d is_user %d smmu %d\n",
230 __func__, env->PC, address, rw, is_user, is_softmmu);
236 /* XXX: put correct access by using cpu_restore_state()
238 access_type = ACCESS_INT;
239 if (env->user_mode_only) {
240 /* user mode only emulation */
241 ret = TLBRET_NOMATCH;
244 ret = get_physical_address(env, &physical, &prot,
245 address, rw, access_type);
247 fprintf(logfile, "%s address=" TARGET_FMT_lx " ret %d physical " TARGET_FMT_lx " prot %d\n",
248 __func__, address, ret, physical, prot);
250 if (ret == TLBRET_MATCH) {
251 ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
252 physical & TARGET_PAGE_MASK, prot,
253 is_user, is_softmmu);
254 } else if (ret < 0) {
259 /* Reference to kernel address from user mode or supervisor mode */
260 /* Reference to supervisor address from user mode */
262 exception = EXCP_AdES;
264 exception = EXCP_AdEL;
267 /* No TLB match for a mapped address */
269 exception = EXCP_TLBS;
271 exception = EXCP_TLBL;
275 /* TLB match with no valid bit */
277 exception = EXCP_TLBS;
279 exception = EXCP_TLBL;
282 /* TLB match but 'D' bit is cleared */
283 exception = EXCP_LTLBL;
287 /* Raise exception */
288 env->CP0_BadVAddr = address;
289 env->CP0_Context = (env->CP0_Context & 0xff800000) |
290 ((address >> 9) & 0x007ffff0);
292 (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1));
293 env->exception_index = exception;
294 env->error_code = error_code;
301 #if defined(CONFIG_USER_ONLY)
302 void do_interrupt (CPUState *env)
304 env->exception_index = EXCP_NONE;
307 void do_interrupt (CPUState *env)
312 if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
313 fprintf(logfile, "%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d excp %d\n",
314 __func__, env->PC, env->CP0_EPC, cause, env->exception_index);
316 if (env->exception_index == EXCP_EXT_INTERRUPT &&
317 (env->hflags & MIPS_HFLAG_DM))
318 env->exception_index = EXCP_DINT;
320 switch (env->exception_index) {
322 env->CP0_Debug |= 1 << CP0DB_DSS;
323 /* Debug single step cannot be raised inside a delay slot and
324 * resume will always occur on the next instruction
325 * (but we assume the pc has always been updated during
328 env->CP0_DEPC = env->PC;
329 goto enter_debug_mode;
331 env->CP0_Debug |= 1 << CP0DB_DINT;
334 env->CP0_Debug |= 1 << CP0DB_DIB;
337 env->CP0_Debug |= 1 << CP0DB_DBp;
340 env->CP0_Debug |= 1 << CP0DB_DDBS;
343 env->CP0_Debug |= 1 << CP0DB_DDBL;
345 if (env->hflags & MIPS_HFLAG_BMASK) {
346 /* If the exception was raised from a delay slot,
347 come back to the jump. */
348 env->CP0_DEPC = env->PC - 4;
349 env->hflags &= ~MIPS_HFLAG_BMASK;
351 env->CP0_DEPC = env->PC;
354 env->hflags |= MIPS_HFLAG_DM;
355 env->hflags &= ~MIPS_HFLAG_UM;
356 /* EJTAG probe trap enable is not implemented... */
357 if (!(env->CP0_Status & (1 << CP0St_EXL)))
358 env->CP0_Cause &= ~(1 << CP0Ca_BD);
359 env->PC = (int32_t)0xBFC00480;
365 env->CP0_Status |= (1 << CP0St_SR);
366 env->CP0_WatchLo = 0;
369 env->CP0_Status |= (1 << CP0St_NMI);
371 if (env->hflags & MIPS_HFLAG_BMASK) {
372 /* If the exception was raised from a delay slot,
373 come back to the jump. */
374 env->CP0_ErrorEPC = env->PC - 4;
375 env->hflags &= ~MIPS_HFLAG_BMASK;
377 env->CP0_ErrorEPC = env->PC;
379 env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV);
380 env->hflags &= ~MIPS_HFLAG_UM;
381 if (!(env->CP0_Status & (1 << CP0St_EXL)))
382 env->CP0_Cause &= ~(1 << CP0Ca_BD);
383 env->PC = (int32_t)0xBFC00000;
388 case EXCP_EXT_INTERRUPT:
390 if (env->CP0_Cause & (1 << CP0Ca_IV))
395 /* XXX: TODO: manage defered watch exceptions */
405 if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL)))
425 env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) |
426 (env->error_code << CP0Ca_CE);
442 if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL)))
445 if (!(env->CP0_Status & (1 << CP0St_EXL))) {
446 if (env->hflags & MIPS_HFLAG_BMASK) {
447 /* If the exception was raised from a delay slot,
448 come back to the jump. */
449 env->CP0_EPC = env->PC - 4;
450 env->CP0_Cause |= (1 << CP0Ca_BD);
452 env->CP0_EPC = env->PC;
453 env->CP0_Cause &= ~(1 << CP0Ca_BD);
455 env->CP0_Status |= (1 << CP0St_EXL);
456 env->hflags &= ~MIPS_HFLAG_UM;
458 env->hflags &= ~MIPS_HFLAG_BMASK;
459 if (env->CP0_Status & (1 << CP0St_BEV)) {
460 env->PC = (int32_t)0xBFC00200;
462 env->PC = (int32_t)(env->CP0_EBase & ~0x3ff);
465 env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC);
469 fprintf(logfile, "Invalid MIPS exception %d. Exiting\n",
470 env->exception_index);
472 printf("Invalid MIPS exception %d. Exiting\n", env->exception_index);
475 if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
476 fprintf(logfile, "%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d excp %d\n"
477 " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n",
478 __func__, env->PC, env->CP0_EPC, cause, env->exception_index,
479 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
482 env->exception_index = EXCP_NONE;
484 #endif /* !defined(CONFIG_USER_ONLY) */
486 void invalidate_tlb (CPUState *env, int idx, int use_extra)
491 uint8_t ASID = env->CP0_EntryHi & 0xFF;
494 tlb = &env->tlb[idx];
495 /* The qemu TLB is flushed then the ASID changes, so no need to
496 flush these entries again. */
497 if (tlb->G == 0 && tlb->ASID != ASID) {
501 if (use_extra && env->tlb_in_use < MIPS_TLB_MAX) {
502 /* For tlbwr, we can shadow the discarded entry into
503 a new (fake) TLB entry, as long as the guest can not
504 tell that it's there. */
505 env->tlb[env->tlb_in_use] = *tlb;
510 /* 1k pages are not supported. */
511 mask = tlb->PageMask | 0x1FFF;
514 end = addr | (mask >> 1);
516 tlb_flush_page (env, addr);
517 addr += TARGET_PAGE_SIZE;
521 addr = tlb->VPN | ((mask >> 1) + 1);
522 addr = tlb->VPN + TARGET_PAGE_SIZE;
525 tlb_flush_page (env, addr);
526 addr += TARGET_PAGE_SIZE;