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
31 /* MIPS32 4K MMU emulation */
32 #ifdef MIPS_USES_R4K_TLB
33 static int map_address (CPUState *env, target_ulong *physical, int *prot,
34 target_ulong address, int rw, int access_type)
43 tag = (address & 0xFFFFE000);
44 ASID = env->CP0_EntryHi & 0x000000FF;
45 for (i = 0; i < MIPS_TLB_NB; i++) {
47 /* Check ASID, virtual page number & size */
48 if ((tlb->G == 1 || tlb->ASID == ASID) &&
49 tlb->VPN == tag && address < tlb->end) {
51 n = (address >> 12) & 1;
52 /* Check access rights */
53 if ((tlb->V[n] & 2) && (rw == 0 || (tlb->D[n] & 4))) {
54 *physical = tlb->PFN[n] | (address & 0xFFF);
59 } else if (!(tlb->V[n] & 2)) {
71 int get_physical_address (CPUState *env, target_ulong *physical, int *prot,
72 target_ulong address, int rw, int access_type)
77 /* User mode can only access useg */
78 user_mode = ((env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM) ? 1 : 0;
81 fprintf(logfile, "user mode %d h %08x\n",
82 user_mode, env->hflags);
85 if (user_mode && address > 0x7FFFFFFFUL)
88 if (address < 0x80000000UL) {
89 if (!(env->hflags & MIPS_HFLAG_ERL)) {
90 #ifdef MIPS_USES_R4K_TLB
91 ret = map_address(env, physical, prot, address, rw, access_type);
93 *physical = address + 0x40000000UL;
94 *prot = PAGE_READ | PAGE_WRITE;
98 *prot = PAGE_READ | PAGE_WRITE;
100 } else if (address < 0xA0000000UL) {
102 /* XXX: check supervisor mode */
103 *physical = address - 0x80000000UL;
104 *prot = PAGE_READ | PAGE_WRITE;
105 } else if (address < 0xC0000000UL) {
107 /* XXX: check supervisor mode */
108 *physical = address - 0xA0000000UL;
109 *prot = PAGE_READ | PAGE_WRITE;
110 } else if (address < 0xE0000000UL) {
112 #ifdef MIPS_USES_R4K_TLB
113 ret = map_address(env, physical, prot, address, rw, access_type);
116 *prot = PAGE_READ | PAGE_WRITE;
120 /* XXX: check supervisor mode */
121 /* XXX: debug segment is not emulated */
122 #ifdef MIPS_USES_R4K_TLB
123 ret = map_address(env, physical, prot, address, rw, access_type);
126 *prot = PAGE_READ | PAGE_WRITE;
131 fprintf(logfile, "%08x %d %d => %08x %d (%d)\n", address, rw,
132 access_type, *physical, *prot, ret);
139 #if defined(CONFIG_USER_ONLY)
140 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
145 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
147 target_ulong phys_addr;
150 if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
155 void cpu_mips_init_mmu (CPUState *env)
158 #endif /* !defined(CONFIG_USER_ONLY) */
160 int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
161 int is_user, int is_softmmu)
163 target_ulong physical;
165 int exception = 0, error_code = 0;
170 cpu_dump_state(env, logfile, fprintf, 0);
171 fprintf(logfile, "%s pc %08x ad %08x rw %d is_user %d smmu %d\n",
172 __func__, env->PC, address, rw, is_user, is_softmmu);
175 /* XXX: put correct access by using cpu_restore_state()
177 access_type = ACCESS_INT;
178 if (env->user_mode_only) {
179 /* user mode only emulation */
183 ret = get_physical_address(env, &physical, &prot,
184 address, rw, access_type);
186 fprintf(logfile, "%s address=%08x ret %d physical %08x prot %d\n",
187 __func__, address, ret, physical, prot);
190 ret = tlb_set_page(env, address & ~0xFFF, physical & ~0xFFF, prot,
191 is_user, is_softmmu);
192 } else if (ret < 0) {
197 /* Reference to kernel address from user mode or supervisor mode */
198 /* Reference to supervisor address from user mode */
200 exception = EXCP_AdES;
202 exception = EXCP_AdEL;
205 /* No TLB match for a mapped address */
207 exception = EXCP_TLBS;
209 exception = EXCP_TLBL;
213 /* TLB match with no valid bit */
215 exception = EXCP_TLBS;
217 exception = EXCP_TLBL;
221 /* TLB match but 'D' bit is cleared */
222 exception = EXCP_LTLBL;
226 /* Raise exception */
227 env->CP0_BadVAddr = address;
228 env->CP0_Context = (env->CP0_Context & 0xff800000) |
229 ((address >> 8) & 0x007ffff0);
231 (env->CP0_EntryHi & 0x000000FF) | (address & 0xFFFFF000);
232 env->exception_index = exception;
233 env->error_code = error_code;
240 void do_interrupt (CPUState *env)
242 target_ulong pc, offset;
245 if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
246 fprintf(logfile, "%s enter: PC %08x EPC %08x cause %d excp %d\n",
247 __func__, env->PC, env->CP0_EPC, cause, env->exception_index);
249 if (env->exception_index == EXCP_EXT_INTERRUPT &&
250 (env->hflags & MIPS_HFLAG_DM))
251 env->exception_index = EXCP_DINT;
253 switch (env->exception_index) {
255 env->CP0_Debug |= 1 << CP0DB_DSS;
256 /* Debug single step cannot be raised inside a delay slot and
257 * resume will always occur on the next instruction
258 * (but we assume the pc has always been updated during
261 env->CP0_DEPC = env->PC;
262 goto enter_debug_mode;
264 env->CP0_Debug |= 1 << CP0DB_DINT;
267 env->CP0_Debug |= 1 << CP0DB_DIB;
270 env->CP0_Debug |= 1 << CP0DB_DBp;
273 env->CP0_Debug |= 1 << CP0DB_DDBS;
276 env->CP0_Debug |= 1 << CP0DB_DDBL;
279 if (env->hflags & MIPS_HFLAG_DS) {
280 /* If the exception was raised from a delay slot,
281 * come back to the jump
283 env->CP0_DEPC = env->PC - 4;
285 env->CP0_DEPC = env->PC;
288 env->hflags |= MIPS_HFLAG_DM;
289 /* EJTAG probe trap enable is not implemented... */
293 #ifdef MIPS_USES_R4K_TLB
294 env->CP0_random = MIPS_TLB_NB - 1;
297 env->CP0_Config0 = MIPS_CONFIG0;
298 #if defined (MIPS_CONFIG1)
299 env->CP0_Config1 = MIPS_CONFIG1;
301 #if defined (MIPS_CONFIG2)
302 env->CP0_Config2 = MIPS_CONFIG2;
304 #if defined (MIPS_CONFIG3)
305 env->CP0_Config3 = MIPS_CONFIG3;
307 env->CP0_WatchLo = 0;
308 env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV);
311 env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
313 env->CP0_WatchLo = 0;
316 env->CP0_Status = (1 << CP0St_CU0) | (1 << CP0St_BEV) |
319 env->hflags = MIPS_HFLAG_ERL;
320 if (env->hflags & MIPS_HFLAG_DS) {
321 /* If the exception was raised from a delay slot,
322 * come back to the jump
324 env->CP0_ErrorEPC = env->PC - 4;
326 env->CP0_ErrorEPC = env->PC;
333 case EXCP_EXT_INTERRUPT:
335 if (env->CP0_Cause & (1 << CP0Ca_IV))
340 /* XXX: TODO: manage defered watch exceptions */
349 if (env->error_code == 1 && !(env->hflags & MIPS_HFLAG_EXL))
369 /* XXX: fill in the faulty unit number */
382 if (env->error_code == 1 && !(env->hflags & MIPS_HFLAG_EXL))
386 if (env->CP0_Status & (1 << CP0St_BEV)) {
391 env->hflags |= MIPS_HFLAG_EXL;
393 env->CP0_Cause = (env->CP0_Cause & ~0x7C) | (cause << 2);
394 if (env->hflags & MIPS_HFLAG_DS) {
395 /* If the exception was raised from a delay slot,
396 * come back to the jump
398 env->CP0_EPC = env->PC - 4;
399 env->CP0_Cause |= 0x80000000;
401 env->CP0_EPC = env->PC;
402 env->CP0_Cause &= ~0x80000000;
407 fprintf(logfile, "Invalid MIPS exception %d. Exiting\n",
408 env->exception_index);
410 printf("Invalid MIPS exception %d. Exiting\n", env->exception_index);
414 if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) {
415 fprintf(logfile, "%s: PC %08x EPC %08x cause %d excp %d\n"
416 " S %08x C %08x A %08x D %08x\n",
417 __func__, env->PC, env->CP0_EPC, cause, env->exception_index,
418 env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr,
421 env->exception_index = EXCP_NONE;