]>
Commit | Line | Data |
---|---|---|
6af0bf9c FB |
1 | /* |
2 | * MIPS emulation helpers for qemu. | |
3 | * | |
4 | * Copyright (c) 2004-2005 Jocelyn Mayer | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
e37e863f FB |
20 | #include <stdarg.h> |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <inttypes.h> | |
25 | #include <signal.h> | |
26 | #include <assert.h> | |
27 | ||
28 | #include "cpu.h" | |
29 | #include "exec-all.h" | |
6af0bf9c | 30 | |
43057ab1 FB |
31 | enum { |
32 | TLBRET_DIRTY = -4, | |
33 | TLBRET_INVALID = -3, | |
34 | TLBRET_NOMATCH = -2, | |
35 | TLBRET_BADADDR = -1, | |
36 | TLBRET_MATCH = 0 | |
37 | }; | |
38 | ||
29929e34 TS |
39 | /* no MMU emulation */ |
40 | int no_mmu_map_address (CPUState *env, target_ulong *physical, int *prot, | |
6af0bf9c | 41 | target_ulong address, int rw, int access_type) |
29929e34 TS |
42 | { |
43 | *physical = address; | |
44 | *prot = PAGE_READ | PAGE_WRITE; | |
45 | return TLBRET_MATCH; | |
46 | } | |
47 | ||
48 | /* fixed mapping MMU emulation */ | |
49 | int fixed_mmu_map_address (CPUState *env, target_ulong *physical, int *prot, | |
50 | target_ulong address, int rw, int access_type) | |
51 | { | |
52 | if (address <= (int32_t)0x7FFFFFFFUL) { | |
53 | if (!(env->CP0_Status & (1 << CP0St_ERL))) | |
54 | *physical = address + 0x40000000UL; | |
55 | else | |
56 | *physical = address; | |
57 | } else if (address <= (int32_t)0xBFFFFFFFUL) | |
58 | *physical = address & 0x1FFFFFFF; | |
59 | else | |
60 | *physical = address; | |
61 | ||
62 | *prot = PAGE_READ | PAGE_WRITE; | |
63 | return TLBRET_MATCH; | |
64 | } | |
65 | ||
66 | /* MIPS32/MIPS64 R4000-style MMU emulation */ | |
67 | int r4k_map_address (CPUState *env, target_ulong *physical, int *prot, | |
68 | target_ulong address, int rw, int access_type) | |
6af0bf9c | 69 | { |
925fd0f2 | 70 | uint8_t ASID = env->CP0_EntryHi & 0xFF; |
3b1c8be4 | 71 | int i; |
6af0bf9c | 72 | |
814b9a47 | 73 | for (i = 0; i < env->tlb_in_use; i++) { |
29929e34 | 74 | r4k_tlb_t *tlb = &env->mmu.r4k.tlb[i]; |
3b1c8be4 | 75 | /* 1k pages are not supported. */ |
f2e9ebef | 76 | target_ulong mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1); |
3b1c8be4 | 77 | target_ulong tag = address & ~mask; |
f2e9ebef | 78 | target_ulong VPN = tlb->VPN & ~mask; |
100ce988 TS |
79 | #ifdef TARGET_MIPS64 |
80 | tag &= 0xC00000FFFFFFFFFFULL; | |
81 | #endif | |
3b1c8be4 | 82 | |
6af0bf9c | 83 | /* Check ASID, virtual page number & size */ |
f2e9ebef | 84 | if ((tlb->G == 1 || tlb->ASID == ASID) && VPN == tag) { |
6af0bf9c | 85 | /* TLB match */ |
f2e9ebef | 86 | int n = !!(address & mask & ~(mask >> 1)); |
6af0bf9c | 87 | /* Check access rights */ |
f2e9ebef | 88 | if (!(n ? tlb->V1 : tlb->V0)) |
43057ab1 | 89 | return TLBRET_INVALID; |
f2e9ebef | 90 | if (rw == 0 || (n ? tlb->D1 : tlb->D0)) { |
3b1c8be4 | 91 | *physical = tlb->PFN[n] | (address & (mask >> 1)); |
9fb63ac2 | 92 | *prot = PAGE_READ; |
98c1b82b | 93 | if (n ? tlb->D1 : tlb->D0) |
9fb63ac2 | 94 | *prot |= PAGE_WRITE; |
43057ab1 | 95 | return TLBRET_MATCH; |
6af0bf9c | 96 | } |
43057ab1 | 97 | return TLBRET_DIRTY; |
6af0bf9c FB |
98 | } |
99 | } | |
43057ab1 | 100 | return TLBRET_NOMATCH; |
6af0bf9c | 101 | } |
6af0bf9c | 102 | |
43057ab1 FB |
103 | static int get_physical_address (CPUState *env, target_ulong *physical, |
104 | int *prot, target_ulong address, | |
105 | int rw, int access_type) | |
6af0bf9c | 106 | { |
b4ab4b4e | 107 | /* User mode can only access useg/xuseg */ |
43057ab1 | 108 | int user_mode = (env->hflags & MIPS_HFLAG_MODE) == MIPS_HFLAG_UM; |
b4ab4b4e TS |
109 | #ifdef TARGET_MIPS64 |
110 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; | |
111 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; | |
112 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; | |
113 | #endif | |
43057ab1 FB |
114 | int ret = TLBRET_MATCH; |
115 | ||
6af0bf9c FB |
116 | #if 0 |
117 | if (logfile) { | |
118 | fprintf(logfile, "user mode %d h %08x\n", | |
119 | user_mode, env->hflags); | |
120 | } | |
121 | #endif | |
b4ab4b4e TS |
122 | |
123 | #ifdef TARGET_MIPS64 | |
124 | if (user_mode && address > 0x3FFFFFFFFFFFFFFFULL) | |
125 | return TLBRET_BADADDR; | |
126 | #else | |
6af0bf9c | 127 | if (user_mode && address > 0x7FFFFFFFUL) |
43057ab1 | 128 | return TLBRET_BADADDR; |
b4ab4b4e TS |
129 | #endif |
130 | ||
131 | if (address <= (int32_t)0x7FFFFFFFUL) { | |
132 | /* useg */ | |
133 | if (!(env->CP0_Status & (1 << CP0St_ERL) && user_mode)) { | |
29929e34 | 134 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
6af0bf9c | 135 | } else { |
29929e34 | 136 | *physical = address & 0xFFFFFFFF; |
6af0bf9c FB |
137 | *prot = PAGE_READ | PAGE_WRITE; |
138 | } | |
b4ab4b4e TS |
139 | #ifdef TARGET_MIPS64 |
140 | /* | |
141 | XXX: Assuming : | |
142 | - PABITS = 36 (correct for MIPS64R1) | |
143 | - SEGBITS = 40 | |
144 | */ | |
145 | } else if (address < 0x3FFFFFFFFFFFFFFFULL) { | |
146 | /* xuseg */ | |
147 | if (UX && address < 0x000000FFFFFFFFFFULL) { | |
29929e34 | 148 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
b4ab4b4e TS |
149 | } else { |
150 | ret = TLBRET_BADADDR; | |
151 | } | |
152 | } else if (address < 0x7FFFFFFFFFFFFFFFULL) { | |
153 | /* xsseg */ | |
154 | if (SX && address < 0x400000FFFFFFFFFFULL) { | |
29929e34 | 155 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
b4ab4b4e TS |
156 | } else { |
157 | ret = TLBRET_BADADDR; | |
158 | } | |
159 | } else if (address < 0xBFFFFFFFFFFFFFFFULL) { | |
160 | /* xkphys */ | |
161 | /* XXX: check supervisor mode */ | |
162 | if (KX && (address & 0x03FFFFFFFFFFFFFFULL) < 0X0000000FFFFFFFFFULL) | |
163 | { | |
164 | *physical = address & 0X000000FFFFFFFFFFULL; | |
165 | *prot = PAGE_READ | PAGE_WRITE; | |
166 | } else { | |
167 | ret = TLBRET_BADADDR; | |
168 | } | |
169 | } else if (address < 0xFFFFFFFF7FFFFFFFULL) { | |
170 | /* xkseg */ | |
171 | /* XXX: check supervisor mode */ | |
172 | if (KX && address < 0xC00000FF7FFFFFFFULL) { | |
29929e34 | 173 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
b4ab4b4e TS |
174 | } else { |
175 | ret = TLBRET_BADADDR; | |
176 | } | |
177 | #endif | |
5dc4b744 | 178 | } else if (address < (int32_t)0xA0000000UL) { |
6af0bf9c FB |
179 | /* kseg0 */ |
180 | /* XXX: check supervisor mode */ | |
5dc4b744 | 181 | *physical = address - (int32_t)0x80000000UL; |
6af0bf9c | 182 | *prot = PAGE_READ | PAGE_WRITE; |
5dc4b744 | 183 | } else if (address < (int32_t)0xC0000000UL) { |
6af0bf9c FB |
184 | /* kseg1 */ |
185 | /* XXX: check supervisor mode */ | |
5dc4b744 | 186 | *physical = address - (int32_t)0xA0000000UL; |
6af0bf9c | 187 | *prot = PAGE_READ | PAGE_WRITE; |
5dc4b744 | 188 | } else if (address < (int32_t)0xE0000000UL) { |
6af0bf9c | 189 | /* kseg2 */ |
29929e34 | 190 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
6af0bf9c FB |
191 | } else { |
192 | /* kseg3 */ | |
193 | /* XXX: check supervisor mode */ | |
194 | /* XXX: debug segment is not emulated */ | |
29929e34 | 195 | ret = env->map_address(env, physical, prot, address, rw, access_type); |
6af0bf9c FB |
196 | } |
197 | #if 0 | |
198 | if (logfile) { | |
3594c774 | 199 | fprintf(logfile, TARGET_FMT_lx " %d %d => " TARGET_FMT_lx " %d (%d)\n", |
c570fd16 | 200 | address, rw, access_type, *physical, *prot, ret); |
6af0bf9c FB |
201 | } |
202 | #endif | |
203 | ||
204 | return ret; | |
205 | } | |
206 | ||
207 | #if defined(CONFIG_USER_ONLY) | |
9b3c35e0 | 208 | target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr) |
6af0bf9c FB |
209 | { |
210 | return addr; | |
211 | } | |
212 | #else | |
9b3c35e0 | 213 | target_phys_addr_t cpu_get_phys_page_debug(CPUState *env, target_ulong addr) |
6af0bf9c FB |
214 | { |
215 | target_ulong phys_addr; | |
216 | int prot; | |
217 | ||
218 | if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0) | |
219 | return -1; | |
220 | return phys_addr; | |
221 | } | |
6af0bf9c FB |
222 | |
223 | void cpu_mips_init_mmu (CPUState *env) | |
224 | { | |
225 | } | |
6af0bf9c FB |
226 | #endif /* !defined(CONFIG_USER_ONLY) */ |
227 | ||
228 | int cpu_mips_handle_mmu_fault (CPUState *env, target_ulong address, int rw, | |
229 | int is_user, int is_softmmu) | |
230 | { | |
231 | target_ulong physical; | |
232 | int prot; | |
233 | int exception = 0, error_code = 0; | |
234 | int access_type; | |
235 | int ret = 0; | |
236 | ||
237 | if (logfile) { | |
4ad40f36 | 238 | #if 0 |
6af0bf9c | 239 | cpu_dump_state(env, logfile, fprintf, 0); |
4ad40f36 | 240 | #endif |
3594c774 | 241 | fprintf(logfile, "%s pc " TARGET_FMT_lx " ad " TARGET_FMT_lx " rw %d is_user %d smmu %d\n", |
6af0bf9c FB |
242 | __func__, env->PC, address, rw, is_user, is_softmmu); |
243 | } | |
4ad40f36 FB |
244 | |
245 | rw &= 1; | |
246 | ||
6af0bf9c FB |
247 | /* data access */ |
248 | /* XXX: put correct access by using cpu_restore_state() | |
249 | correctly */ | |
250 | access_type = ACCESS_INT; | |
251 | if (env->user_mode_only) { | |
252 | /* user mode only emulation */ | |
43057ab1 | 253 | ret = TLBRET_NOMATCH; |
6af0bf9c FB |
254 | goto do_fault; |
255 | } | |
256 | ret = get_physical_address(env, &physical, &prot, | |
257 | address, rw, access_type); | |
258 | if (logfile) { | |
3594c774 | 259 | fprintf(logfile, "%s address=" TARGET_FMT_lx " ret %d physical " TARGET_FMT_lx " prot %d\n", |
6af0bf9c FB |
260 | __func__, address, ret, physical, prot); |
261 | } | |
43057ab1 FB |
262 | if (ret == TLBRET_MATCH) { |
263 | ret = tlb_set_page(env, address & TARGET_PAGE_MASK, | |
264 | physical & TARGET_PAGE_MASK, prot, | |
265 | is_user, is_softmmu); | |
6af0bf9c FB |
266 | } else if (ret < 0) { |
267 | do_fault: | |
268 | switch (ret) { | |
269 | default: | |
43057ab1 | 270 | case TLBRET_BADADDR: |
6af0bf9c FB |
271 | /* Reference to kernel address from user mode or supervisor mode */ |
272 | /* Reference to supervisor address from user mode */ | |
273 | if (rw) | |
274 | exception = EXCP_AdES; | |
275 | else | |
276 | exception = EXCP_AdEL; | |
277 | break; | |
43057ab1 | 278 | case TLBRET_NOMATCH: |
6af0bf9c FB |
279 | /* No TLB match for a mapped address */ |
280 | if (rw) | |
281 | exception = EXCP_TLBS; | |
282 | else | |
283 | exception = EXCP_TLBL; | |
284 | error_code = 1; | |
285 | break; | |
43057ab1 | 286 | case TLBRET_INVALID: |
6af0bf9c FB |
287 | /* TLB match with no valid bit */ |
288 | if (rw) | |
289 | exception = EXCP_TLBS; | |
290 | else | |
291 | exception = EXCP_TLBL; | |
6af0bf9c | 292 | break; |
43057ab1 | 293 | case TLBRET_DIRTY: |
6af0bf9c FB |
294 | /* TLB match but 'D' bit is cleared */ |
295 | exception = EXCP_LTLBL; | |
296 | break; | |
297 | ||
298 | } | |
6af0bf9c FB |
299 | /* Raise exception */ |
300 | env->CP0_BadVAddr = address; | |
100ce988 | 301 | env->CP0_Context = (env->CP0_Context & ~0x007fffff) | |
4ad40f36 | 302 | ((address >> 9) & 0x007ffff0); |
6af0bf9c | 303 | env->CP0_EntryHi = |
43057ab1 | 304 | (env->CP0_EntryHi & 0xFF) | (address & (TARGET_PAGE_MASK << 1)); |
100ce988 TS |
305 | #ifdef TARGET_MIPS64 |
306 | env->CP0_EntryHi &= 0xc00000ffffffffffULL; | |
307 | env->CP0_XContext = (env->CP0_XContext & 0xfffffffe00000000ULL) | | |
308 | ((address >> 31) & 0x0000000180000000ULL) | | |
309 | ((address >> 9) & 0x000000007ffffff0ULL); | |
310 | #endif | |
6af0bf9c FB |
311 | env->exception_index = exception; |
312 | env->error_code = error_code; | |
313 | ret = 1; | |
314 | } | |
315 | ||
316 | return ret; | |
317 | } | |
318 | ||
ca7c2b1b TS |
319 | #if defined(CONFIG_USER_ONLY) |
320 | void do_interrupt (CPUState *env) | |
321 | { | |
322 | env->exception_index = EXCP_NONE; | |
323 | } | |
324 | #else | |
6af0bf9c FB |
325 | void do_interrupt (CPUState *env) |
326 | { | |
aa328add | 327 | target_ulong offset; |
6af0bf9c FB |
328 | int cause = -1; |
329 | ||
330 | if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) { | |
3594c774 | 331 | fprintf(logfile, "%s enter: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d excp %d\n", |
6af0bf9c FB |
332 | __func__, env->PC, env->CP0_EPC, cause, env->exception_index); |
333 | } | |
334 | if (env->exception_index == EXCP_EXT_INTERRUPT && | |
335 | (env->hflags & MIPS_HFLAG_DM)) | |
336 | env->exception_index = EXCP_DINT; | |
337 | offset = 0x180; | |
338 | switch (env->exception_index) { | |
339 | case EXCP_DSS: | |
340 | env->CP0_Debug |= 1 << CP0DB_DSS; | |
341 | /* Debug single step cannot be raised inside a delay slot and | |
342 | * resume will always occur on the next instruction | |
343 | * (but we assume the pc has always been updated during | |
344 | * code translation). | |
345 | */ | |
346 | env->CP0_DEPC = env->PC; | |
347 | goto enter_debug_mode; | |
348 | case EXCP_DINT: | |
349 | env->CP0_Debug |= 1 << CP0DB_DINT; | |
350 | goto set_DEPC; | |
351 | case EXCP_DIB: | |
352 | env->CP0_Debug |= 1 << CP0DB_DIB; | |
353 | goto set_DEPC; | |
354 | case EXCP_DBp: | |
355 | env->CP0_Debug |= 1 << CP0DB_DBp; | |
356 | goto set_DEPC; | |
357 | case EXCP_DDBS: | |
358 | env->CP0_Debug |= 1 << CP0DB_DDBS; | |
359 | goto set_DEPC; | |
360 | case EXCP_DDBL: | |
361 | env->CP0_Debug |= 1 << CP0DB_DDBL; | |
6af0bf9c | 362 | set_DEPC: |
4ad40f36 | 363 | if (env->hflags & MIPS_HFLAG_BMASK) { |
6af0bf9c | 364 | /* If the exception was raised from a delay slot, |
aa328add | 365 | come back to the jump. */ |
6af0bf9c | 366 | env->CP0_DEPC = env->PC - 4; |
4ad40f36 | 367 | env->hflags &= ~MIPS_HFLAG_BMASK; |
6af0bf9c FB |
368 | } else { |
369 | env->CP0_DEPC = env->PC; | |
370 | } | |
371 | enter_debug_mode: | |
372 | env->hflags |= MIPS_HFLAG_DM; | |
24c7b0e3 | 373 | env->hflags &= ~MIPS_HFLAG_UM; |
6af0bf9c | 374 | /* EJTAG probe trap enable is not implemented... */ |
0a6de750 TS |
375 | if (!(env->CP0_Status & (1 << CP0St_EXL))) |
376 | env->CP0_Cause &= ~(1 << CP0Ca_BD); | |
5dc4b744 | 377 | env->PC = (int32_t)0xBFC00480; |
6af0bf9c FB |
378 | break; |
379 | case EXCP_RESET: | |
aa328add TS |
380 | cpu_reset(env); |
381 | break; | |
6af0bf9c | 382 | case EXCP_SRESET: |
24c7b0e3 | 383 | env->CP0_Status |= (1 << CP0St_SR); |
6af0bf9c FB |
384 | env->CP0_WatchLo = 0; |
385 | goto set_error_EPC; | |
386 | case EXCP_NMI: | |
24c7b0e3 | 387 | env->CP0_Status |= (1 << CP0St_NMI); |
6af0bf9c | 388 | set_error_EPC: |
4ad40f36 | 389 | if (env->hflags & MIPS_HFLAG_BMASK) { |
6af0bf9c | 390 | /* If the exception was raised from a delay slot, |
aa328add | 391 | come back to the jump. */ |
6af0bf9c | 392 | env->CP0_ErrorEPC = env->PC - 4; |
ecd78a0a | 393 | env->hflags &= ~MIPS_HFLAG_BMASK; |
6af0bf9c FB |
394 | } else { |
395 | env->CP0_ErrorEPC = env->PC; | |
396 | } | |
24c7b0e3 TS |
397 | env->CP0_Status |= (1 << CP0St_ERL) | (1 << CP0St_BEV); |
398 | env->hflags &= ~MIPS_HFLAG_UM; | |
0a6de750 TS |
399 | if (!(env->CP0_Status & (1 << CP0St_EXL))) |
400 | env->CP0_Cause &= ~(1 << CP0Ca_BD); | |
5dc4b744 | 401 | env->PC = (int32_t)0xBFC00000; |
6af0bf9c FB |
402 | break; |
403 | case EXCP_MCHECK: | |
404 | cause = 24; | |
405 | goto set_EPC; | |
406 | case EXCP_EXT_INTERRUPT: | |
407 | cause = 0; | |
408 | if (env->CP0_Cause & (1 << CP0Ca_IV)) | |
409 | offset = 0x200; | |
410 | goto set_EPC; | |
411 | case EXCP_DWATCH: | |
412 | cause = 23; | |
413 | /* XXX: TODO: manage defered watch exceptions */ | |
414 | goto set_EPC; | |
415 | case EXCP_AdEL: | |
6af0bf9c FB |
416 | cause = 4; |
417 | goto set_EPC; | |
beb811bd TS |
418 | case EXCP_AdES: |
419 | cause = 5; | |
420 | goto set_EPC; | |
6af0bf9c | 421 | case EXCP_TLBL: |
6af0bf9c | 422 | cause = 2; |
100ce988 TS |
423 | if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) { |
424 | #ifdef TARGET_MIPS64 | |
425 | int R = env->CP0_BadVAddr >> 62; | |
426 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; | |
427 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; | |
428 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; | |
429 | ||
430 | if ((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) | |
431 | offset = 0x080; | |
432 | else | |
433 | #endif | |
434 | offset = 0x000; | |
435 | } | |
6af0bf9c FB |
436 | goto set_EPC; |
437 | case EXCP_IBE: | |
438 | cause = 6; | |
439 | goto set_EPC; | |
440 | case EXCP_DBE: | |
441 | cause = 7; | |
442 | goto set_EPC; | |
443 | case EXCP_SYSCALL: | |
444 | cause = 8; | |
445 | goto set_EPC; | |
446 | case EXCP_BREAK: | |
447 | cause = 9; | |
448 | goto set_EPC; | |
449 | case EXCP_RI: | |
450 | cause = 10; | |
451 | goto set_EPC; | |
452 | case EXCP_CpU: | |
453 | cause = 11; | |
39d51eb8 TS |
454 | env->CP0_Cause = (env->CP0_Cause & ~(0x3 << CP0Ca_CE)) | |
455 | (env->error_code << CP0Ca_CE); | |
6af0bf9c FB |
456 | goto set_EPC; |
457 | case EXCP_OVERFLOW: | |
458 | cause = 12; | |
459 | goto set_EPC; | |
460 | case EXCP_TRAP: | |
461 | cause = 13; | |
462 | goto set_EPC; | |
5a5012ec TS |
463 | case EXCP_FPE: |
464 | cause = 15; | |
465 | goto set_EPC; | |
6af0bf9c FB |
466 | case EXCP_LTLBL: |
467 | cause = 1; | |
468 | goto set_EPC; | |
469 | case EXCP_TLBS: | |
470 | cause = 3; | |
100ce988 TS |
471 | if (env->error_code == 1 && !(env->CP0_Status & (1 << CP0St_EXL))) { |
472 | #ifdef TARGET_MIPS64 | |
473 | int R = env->CP0_BadVAddr >> 62; | |
474 | int UX = (env->CP0_Status & (1 << CP0St_UX)) != 0; | |
475 | int SX = (env->CP0_Status & (1 << CP0St_SX)) != 0; | |
476 | int KX = (env->CP0_Status & (1 << CP0St_KX)) != 0; | |
477 | ||
478 | if ((R == 0 && UX) || (R == 1 && SX) || (R == 3 && KX)) | |
479 | offset = 0x080; | |
480 | else | |
481 | #endif | |
482 | offset = 0x000; | |
483 | } | |
6af0bf9c | 484 | set_EPC: |
24c7b0e3 TS |
485 | if (!(env->CP0_Status & (1 << CP0St_EXL))) { |
486 | if (env->hflags & MIPS_HFLAG_BMASK) { | |
487 | /* If the exception was raised from a delay slot, | |
488 | come back to the jump. */ | |
489 | env->CP0_EPC = env->PC - 4; | |
39d51eb8 | 490 | env->CP0_Cause |= (1 << CP0Ca_BD); |
24c7b0e3 TS |
491 | } else { |
492 | env->CP0_EPC = env->PC; | |
493 | env->CP0_Cause &= ~(1 << CP0Ca_BD); | |
494 | } | |
24c7b0e3 TS |
495 | env->CP0_Status |= (1 << CP0St_EXL); |
496 | env->hflags &= ~MIPS_HFLAG_UM; | |
6af0bf9c | 497 | } |
c53f4a62 | 498 | env->hflags &= ~MIPS_HFLAG_BMASK; |
aa328add | 499 | if (env->CP0_Status & (1 << CP0St_BEV)) { |
5dc4b744 | 500 | env->PC = (int32_t)0xBFC00200; |
aa328add | 501 | } else { |
acd858d9 | 502 | env->PC = (int32_t)(env->CP0_EBase & ~0x3ff); |
aa328add | 503 | } |
aa328add | 504 | env->PC += offset; |
e58c8ba5 | 505 | env->CP0_Cause = (env->CP0_Cause & ~(0x1f << CP0Ca_EC)) | (cause << CP0Ca_EC); |
6af0bf9c FB |
506 | break; |
507 | default: | |
508 | if (logfile) { | |
509 | fprintf(logfile, "Invalid MIPS exception %d. Exiting\n", | |
510 | env->exception_index); | |
511 | } | |
512 | printf("Invalid MIPS exception %d. Exiting\n", env->exception_index); | |
513 | exit(1); | |
514 | } | |
6af0bf9c | 515 | if (logfile && env->exception_index != EXCP_EXT_INTERRUPT) { |
3594c774 TS |
516 | fprintf(logfile, "%s: PC " TARGET_FMT_lx " EPC " TARGET_FMT_lx " cause %d excp %d\n" |
517 | " S %08x C %08x A " TARGET_FMT_lx " D " TARGET_FMT_lx "\n", | |
6af0bf9c FB |
518 | __func__, env->PC, env->CP0_EPC, cause, env->exception_index, |
519 | env->CP0_Status, env->CP0_Cause, env->CP0_BadVAddr, | |
520 | env->CP0_DEPC); | |
521 | } | |
522 | env->exception_index = EXCP_NONE; | |
523 | } | |
ca7c2b1b | 524 | #endif /* !defined(CONFIG_USER_ONLY) */ |
2ee4aed8 | 525 | |
29929e34 | 526 | void r4k_invalidate_tlb (CPUState *env, int idx, int use_extra) |
2ee4aed8 | 527 | { |
29929e34 | 528 | r4k_tlb_t *tlb; |
3b1c8be4 TS |
529 | target_ulong addr; |
530 | target_ulong end; | |
531 | uint8_t ASID = env->CP0_EntryHi & 0xFF; | |
532 | target_ulong mask; | |
2ee4aed8 | 533 | |
29929e34 | 534 | tlb = &env->mmu.r4k.tlb[idx]; |
f2e9ebef | 535 | /* The qemu TLB is flushed when the ASID changes, so no need to |
2ee4aed8 FB |
536 | flush these entries again. */ |
537 | if (tlb->G == 0 && tlb->ASID != ASID) { | |
538 | return; | |
539 | } | |
540 | ||
541 | if (use_extra && env->tlb_in_use < MIPS_TLB_MAX) { | |
542 | /* For tlbwr, we can shadow the discarded entry into | |
543 | a new (fake) TLB entry, as long as the guest can not | |
544 | tell that it's there. */ | |
29929e34 | 545 | env->mmu.r4k.tlb[env->tlb_in_use] = *tlb; |
2ee4aed8 FB |
546 | env->tlb_in_use++; |
547 | return; | |
548 | } | |
549 | ||
3b1c8be4 | 550 | /* 1k pages are not supported. */ |
f2e9ebef | 551 | mask = tlb->PageMask | ~(TARGET_PAGE_MASK << 1); |
3b1c8be4 | 552 | if (tlb->V0) { |
f2e9ebef | 553 | addr = tlb->VPN & ~mask; |
100ce988 TS |
554 | #ifdef TARGET_MIPS64 |
555 | if (addr >= 0xC00000FF80000000ULL) { | |
556 | addr |= 0x3FFFFF0000000000ULL; | |
557 | } | |
558 | #endif | |
3b1c8be4 TS |
559 | end = addr | (mask >> 1); |
560 | while (addr < end) { | |
561 | tlb_flush_page (env, addr); | |
562 | addr += TARGET_PAGE_SIZE; | |
563 | } | |
564 | } | |
565 | if (tlb->V1) { | |
f2e9ebef | 566 | addr = (tlb->VPN & ~mask) | ((mask >> 1) + 1); |
100ce988 TS |
567 | #ifdef TARGET_MIPS64 |
568 | if (addr >= 0xC00000FF80000000ULL) { | |
569 | addr |= 0x3FFFFF0000000000ULL; | |
570 | } | |
571 | #endif | |
3b1c8be4 TS |
572 | end = addr | mask; |
573 | while (addr < end) { | |
574 | tlb_flush_page (env, addr); | |
575 | addr += TARGET_PAGE_SIZE; | |
576 | } | |
577 | } | |
2ee4aed8 | 578 | } |