]> Git Repo - qemu.git/blame - target-ppc/helper.c
target-ppc: enable SPE and Altivec in user mode
[qemu.git] / target-ppc / helper.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation helpers for qemu.
5fafdf24 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
79aceca5
FB
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 */
fdabc366
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"
0411a972 30#include "helper_regs.h"
ca10f867 31#include "qemu-common.h"
a7812ae4 32#include "helper.h"
9a64fbe4
FB
33
34//#define DEBUG_MMU
35//#define DEBUG_BATS
6b542af7 36//#define DEBUG_SLB
76a66253 37//#define DEBUG_SOFTWARE_TLB
0411a972 38//#define DUMP_PAGE_TABLES
9a64fbe4 39//#define DEBUG_EXCEPTIONS
fdabc366 40//#define FLUSH_ALL_TLBS
9a64fbe4 41
64adab3f
AJ
42/*****************************************************************************/
43/* Exceptions processing */
44
45void raise_exception_err (CPUState *env, int exception, int error_code)
46{
47#if 0
48 printf("Raise exception %3x code : %d\n", exception, error_code);
49#endif
50 env->exception_index = exception;
51 env->error_code = error_code;
52 cpu_loop_exit();
53}
54
55void raise_exception (CPUState *env, int exception)
56{
57 helper_raise_exception_err(exception, 0);
58}
59
9a64fbe4 60/*****************************************************************************/
3fc6c082 61/* PowerPC MMU emulation */
a541f297 62
d9bce9d9 63#if defined(CONFIG_USER_ONLY)
e96efcfc 64int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
6ebbf390 65 int mmu_idx, int is_softmmu)
24741ef3
FB
66{
67 int exception, error_code;
d9bce9d9 68
24741ef3 69 if (rw == 2) {
e1833e1f 70 exception = POWERPC_EXCP_ISI;
8f793433 71 error_code = 0x40000000;
24741ef3 72 } else {
e1833e1f 73 exception = POWERPC_EXCP_DSI;
8f793433 74 error_code = 0x40000000;
24741ef3
FB
75 if (rw)
76 error_code |= 0x02000000;
77 env->spr[SPR_DAR] = address;
78 env->spr[SPR_DSISR] = error_code;
79 }
80 env->exception_index = exception;
81 env->error_code = error_code;
76a66253 82
24741ef3
FB
83 return 1;
84}
76a66253 85
9b3c35e0 86target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
24741ef3
FB
87{
88 return addr;
89}
36081602 90
24741ef3 91#else
76a66253 92/* Common routines used by software and hardware TLBs emulation */
b068d6a7 93static always_inline int pte_is_valid (target_ulong pte0)
76a66253
JM
94{
95 return pte0 & 0x80000000 ? 1 : 0;
96}
97
b068d6a7 98static always_inline void pte_invalidate (target_ulong *pte0)
76a66253
JM
99{
100 *pte0 &= ~0x80000000;
101}
102
caa4039c 103#if defined(TARGET_PPC64)
b068d6a7 104static always_inline int pte64_is_valid (target_ulong pte0)
caa4039c
JM
105{
106 return pte0 & 0x0000000000000001ULL ? 1 : 0;
107}
108
b068d6a7 109static always_inline void pte64_invalidate (target_ulong *pte0)
caa4039c
JM
110{
111 *pte0 &= ~0x0000000000000001ULL;
112}
113#endif
114
76a66253
JM
115#define PTE_PTEM_MASK 0x7FFFFFBF
116#define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
caa4039c
JM
117#if defined(TARGET_PPC64)
118#define PTE64_PTEM_MASK 0xFFFFFFFFFFFFFF80ULL
119#define PTE64_CHECK_MASK (TARGET_PAGE_MASK | 0x7F)
120#endif
76a66253 121
b227a8e9
JM
122static always_inline int pp_check (int key, int pp, int nx)
123{
124 int access;
125
126 /* Compute access rights */
127 /* When pp is 3/7, the result is undefined. Set it to noaccess */
128 access = 0;
129 if (key == 0) {
130 switch (pp) {
131 case 0x0:
132 case 0x1:
133 case 0x2:
134 access |= PAGE_WRITE;
135 /* No break here */
136 case 0x3:
137 case 0x6:
138 access |= PAGE_READ;
139 break;
140 }
141 } else {
142 switch (pp) {
143 case 0x0:
144 case 0x6:
145 access = 0;
146 break;
147 case 0x1:
148 case 0x3:
149 access = PAGE_READ;
150 break;
151 case 0x2:
152 access = PAGE_READ | PAGE_WRITE;
153 break;
154 }
155 }
156 if (nx == 0)
157 access |= PAGE_EXEC;
158
159 return access;
160}
161
162static always_inline int check_prot (int prot, int rw, int access_type)
163{
164 int ret;
165
166 if (access_type == ACCESS_CODE) {
167 if (prot & PAGE_EXEC)
168 ret = 0;
169 else
170 ret = -2;
171 } else if (rw) {
172 if (prot & PAGE_WRITE)
173 ret = 0;
174 else
175 ret = -2;
176 } else {
177 if (prot & PAGE_READ)
178 ret = 0;
179 else
180 ret = -2;
181 }
182
183 return ret;
184}
185
b068d6a7
JM
186static always_inline int _pte_check (mmu_ctx_t *ctx, int is_64b,
187 target_ulong pte0, target_ulong pte1,
b227a8e9 188 int h, int rw, int type)
76a66253 189{
caa4039c 190 target_ulong ptem, mmask;
b227a8e9 191 int access, ret, pteh, ptev, pp;
76a66253
JM
192
193 access = 0;
194 ret = -1;
195 /* Check validity and table match */
caa4039c
JM
196#if defined(TARGET_PPC64)
197 if (is_64b) {
198 ptev = pte64_is_valid(pte0);
199 pteh = (pte0 >> 1) & 1;
200 } else
201#endif
202 {
203 ptev = pte_is_valid(pte0);
204 pteh = (pte0 >> 6) & 1;
205 }
206 if (ptev && h == pteh) {
76a66253 207 /* Check vsid & api */
caa4039c
JM
208#if defined(TARGET_PPC64)
209 if (is_64b) {
210 ptem = pte0 & PTE64_PTEM_MASK;
211 mmask = PTE64_CHECK_MASK;
b227a8e9
JM
212 pp = (pte1 & 0x00000003) | ((pte1 >> 61) & 0x00000004);
213 ctx->nx |= (pte1 >> 2) & 1; /* No execute bit */
214 ctx->nx |= (pte1 >> 3) & 1; /* Guarded bit */
caa4039c
JM
215 } else
216#endif
217 {
218 ptem = pte0 & PTE_PTEM_MASK;
219 mmask = PTE_CHECK_MASK;
b227a8e9 220 pp = pte1 & 0x00000003;
caa4039c
JM
221 }
222 if (ptem == ctx->ptem) {
6f2d8978 223 if (ctx->raddr != (target_phys_addr_t)-1ULL) {
76a66253 224 /* all matches should have equal RPN, WIMG & PP */
caa4039c
JM
225 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
226 if (loglevel != 0)
76a66253
JM
227 fprintf(logfile, "Bad RPN/WIMG/PP\n");
228 return -3;
229 }
230 }
231 /* Compute access rights */
b227a8e9 232 access = pp_check(ctx->key, pp, ctx->nx);
76a66253
JM
233 /* Keep the matching PTE informations */
234 ctx->raddr = pte1;
235 ctx->prot = access;
b227a8e9
JM
236 ret = check_prot(ctx->prot, rw, type);
237 if (ret == 0) {
76a66253
JM
238 /* Access granted */
239#if defined (DEBUG_MMU)
4a057712 240 if (loglevel != 0)
76a66253
JM
241 fprintf(logfile, "PTE access granted !\n");
242#endif
76a66253
JM
243 } else {
244 /* Access right violation */
245#if defined (DEBUG_MMU)
4a057712 246 if (loglevel != 0)
76a66253
JM
247 fprintf(logfile, "PTE access rejected\n");
248#endif
76a66253
JM
249 }
250 }
251 }
252
253 return ret;
254}
255
a11b8151
JM
256static always_inline int pte32_check (mmu_ctx_t *ctx,
257 target_ulong pte0, target_ulong pte1,
258 int h, int rw, int type)
caa4039c 259{
b227a8e9 260 return _pte_check(ctx, 0, pte0, pte1, h, rw, type);
caa4039c
JM
261}
262
263#if defined(TARGET_PPC64)
a11b8151
JM
264static always_inline int pte64_check (mmu_ctx_t *ctx,
265 target_ulong pte0, target_ulong pte1,
266 int h, int rw, int type)
caa4039c 267{
b227a8e9 268 return _pte_check(ctx, 1, pte0, pte1, h, rw, type);
caa4039c
JM
269}
270#endif
271
a11b8151
JM
272static always_inline int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
273 int ret, int rw)
76a66253
JM
274{
275 int store = 0;
276
277 /* Update page flags */
278 if (!(*pte1p & 0x00000100)) {
279 /* Update accessed flag */
280 *pte1p |= 0x00000100;
281 store = 1;
282 }
283 if (!(*pte1p & 0x00000080)) {
284 if (rw == 1 && ret == 0) {
285 /* Update changed flag */
286 *pte1p |= 0x00000080;
287 store = 1;
288 } else {
289 /* Force page fault for first write access */
290 ctx->prot &= ~PAGE_WRITE;
291 }
292 }
293
294 return store;
295}
296
297/* Software driven TLB helpers */
a11b8151
JM
298static always_inline int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
299 int way, int is_code)
76a66253
JM
300{
301 int nr;
302
303 /* Select TLB num in a way from address */
304 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
305 /* Select TLB way */
306 nr += env->tlb_per_way * way;
307 /* 6xx have separate TLBs for instructions and data */
308 if (is_code && env->id_tlbs == 1)
309 nr += env->nb_tlb;
310
311 return nr;
312}
313
a11b8151 314static always_inline void ppc6xx_tlb_invalidate_all (CPUState *env)
76a66253 315{
1d0a48fb 316 ppc6xx_tlb_t *tlb;
76a66253
JM
317 int nr, max;
318
319#if defined (DEBUG_SOFTWARE_TLB) && 0
320 if (loglevel != 0) {
321 fprintf(logfile, "Invalidate all TLBs\n");
322 }
323#endif
324 /* Invalidate all defined software TLB */
325 max = env->nb_tlb;
326 if (env->id_tlbs == 1)
327 max *= 2;
328 for (nr = 0; nr < max; nr++) {
1d0a48fb 329 tlb = &env->tlb[nr].tlb6;
76a66253
JM
330 pte_invalidate(&tlb->pte0);
331 }
76a66253 332 tlb_flush(env, 1);
76a66253
JM
333}
334
b068d6a7
JM
335static always_inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
336 target_ulong eaddr,
337 int is_code,
338 int match_epn)
76a66253 339{
4a057712 340#if !defined(FLUSH_ALL_TLBS)
1d0a48fb 341 ppc6xx_tlb_t *tlb;
76a66253
JM
342 int way, nr;
343
76a66253
JM
344 /* Invalidate ITLB + DTLB, all ways */
345 for (way = 0; way < env->nb_ways; way++) {
346 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
1d0a48fb 347 tlb = &env->tlb[nr].tlb6;
76a66253
JM
348 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
349#if defined (DEBUG_SOFTWARE_TLB)
350 if (loglevel != 0) {
1b9eb036 351 fprintf(logfile, "TLB invalidate %d/%d " ADDRX "\n",
76a66253
JM
352 nr, env->nb_tlb, eaddr);
353 }
354#endif
355 pte_invalidate(&tlb->pte0);
356 tlb_flush_page(env, tlb->EPN);
357 }
358 }
359#else
360 /* XXX: PowerPC specification say this is valid as well */
361 ppc6xx_tlb_invalidate_all(env);
362#endif
363}
364
a11b8151
JM
365static always_inline void ppc6xx_tlb_invalidate_virt (CPUState *env,
366 target_ulong eaddr,
367 int is_code)
76a66253
JM
368{
369 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
370}
371
372void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
373 target_ulong pte0, target_ulong pte1)
374{
1d0a48fb 375 ppc6xx_tlb_t *tlb;
76a66253
JM
376 int nr;
377
378 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
1d0a48fb 379 tlb = &env->tlb[nr].tlb6;
76a66253
JM
380#if defined (DEBUG_SOFTWARE_TLB)
381 if (loglevel != 0) {
5fafdf24 382 fprintf(logfile, "Set TLB %d/%d EPN " ADDRX " PTE0 " ADDRX
1b9eb036 383 " PTE1 " ADDRX "\n", nr, env->nb_tlb, EPN, pte0, pte1);
76a66253
JM
384 }
385#endif
386 /* Invalidate any pending reference in Qemu for this virtual address */
387 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
388 tlb->pte0 = pte0;
389 tlb->pte1 = pte1;
390 tlb->EPN = EPN;
76a66253
JM
391 /* Store last way for LRU mechanism */
392 env->last_way = way;
393}
394
a11b8151
JM
395static always_inline int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
396 target_ulong eaddr, int rw,
397 int access_type)
76a66253 398{
1d0a48fb 399 ppc6xx_tlb_t *tlb;
76a66253
JM
400 int nr, best, way;
401 int ret;
d9bce9d9 402
76a66253
JM
403 best = -1;
404 ret = -1; /* No TLB found */
405 for (way = 0; way < env->nb_ways; way++) {
406 nr = ppc6xx_tlb_getnum(env, eaddr, way,
407 access_type == ACCESS_CODE ? 1 : 0);
1d0a48fb 408 tlb = &env->tlb[nr].tlb6;
76a66253
JM
409 /* This test "emulates" the PTE index match for hardware TLBs */
410 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
411#if defined (DEBUG_SOFTWARE_TLB)
412 if (loglevel != 0) {
1b9eb036
JM
413 fprintf(logfile, "TLB %d/%d %s [" ADDRX " " ADDRX
414 "] <> " ADDRX "\n",
76a66253
JM
415 nr, env->nb_tlb,
416 pte_is_valid(tlb->pte0) ? "valid" : "inval",
417 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
418 }
419#endif
420 continue;
421 }
422#if defined (DEBUG_SOFTWARE_TLB)
423 if (loglevel != 0) {
1b9eb036
JM
424 fprintf(logfile, "TLB %d/%d %s " ADDRX " <> " ADDRX " " ADDRX
425 " %c %c\n",
76a66253
JM
426 nr, env->nb_tlb,
427 pte_is_valid(tlb->pte0) ? "valid" : "inval",
428 tlb->EPN, eaddr, tlb->pte1,
429 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
430 }
431#endif
b227a8e9 432 switch (pte32_check(ctx, tlb->pte0, tlb->pte1, 0, rw, access_type)) {
76a66253
JM
433 case -3:
434 /* TLB inconsistency */
435 return -1;
436 case -2:
437 /* Access violation */
438 ret = -2;
439 best = nr;
440 break;
441 case -1:
442 default:
443 /* No match */
444 break;
445 case 0:
446 /* access granted */
447 /* XXX: we should go on looping to check all TLBs consistency
448 * but we can speed-up the whole thing as the
449 * result would be undefined if TLBs are not consistent.
450 */
451 ret = 0;
452 best = nr;
453 goto done;
454 }
455 }
456 if (best != -1) {
457 done:
458#if defined (DEBUG_SOFTWARE_TLB)
4a057712 459 if (loglevel != 0) {
6b542af7 460 fprintf(logfile, "found TLB at addr " PADDRX " prot=%01x ret=%d\n",
76a66253
JM
461 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
462 }
463#endif
464 /* Update page flags */
1d0a48fb 465 pte_update_flags(ctx, &env->tlb[best].tlb6.pte1, ret, rw);
76a66253
JM
466 }
467
468 return ret;
469}
470
9a64fbe4 471/* Perform BAT hit & translation */
faadf50e
JM
472static always_inline void bat_size_prot (CPUState *env, target_ulong *blp,
473 int *validp, int *protp,
474 target_ulong *BATu, target_ulong *BATl)
475{
476 target_ulong bl;
477 int pp, valid, prot;
478
479 bl = (*BATu & 0x00001FFC) << 15;
480 valid = 0;
481 prot = 0;
482 if (((msr_pr == 0) && (*BATu & 0x00000002)) ||
483 ((msr_pr != 0) && (*BATu & 0x00000001))) {
484 valid = 1;
485 pp = *BATl & 0x00000003;
486 if (pp != 0) {
487 prot = PAGE_READ | PAGE_EXEC;
488 if (pp == 0x2)
489 prot |= PAGE_WRITE;
490 }
491 }
492 *blp = bl;
493 *validp = valid;
494 *protp = prot;
495}
496
497static always_inline void bat_601_size_prot (CPUState *env,target_ulong *blp,
498 int *validp, int *protp,
499 target_ulong *BATu,
500 target_ulong *BATl)
501{
502 target_ulong bl;
503 int key, pp, valid, prot;
504
505 bl = (*BATl & 0x0000003F) << 17;
056401ea 506#if defined (DEBUG_BATS)
faadf50e 507 if (loglevel != 0) {
6b542af7
JM
508 fprintf(logfile, "b %02x ==> bl " ADDRX " msk " ADDRX "\n",
509 (uint8_t)(*BATl & 0x0000003F), bl, ~bl);
faadf50e 510 }
056401ea 511#endif
faadf50e
JM
512 prot = 0;
513 valid = (*BATl >> 6) & 1;
514 if (valid) {
515 pp = *BATu & 0x00000003;
516 if (msr_pr == 0)
517 key = (*BATu >> 3) & 1;
518 else
519 key = (*BATu >> 2) & 1;
520 prot = pp_check(key, pp, 0);
521 }
522 *blp = bl;
523 *validp = valid;
524 *protp = prot;
525}
526
a11b8151
JM
527static always_inline int get_bat (CPUState *env, mmu_ctx_t *ctx,
528 target_ulong virtual, int rw, int type)
9a64fbe4 529{
76a66253
JM
530 target_ulong *BATlt, *BATut, *BATu, *BATl;
531 target_ulong base, BEPIl, BEPIu, bl;
faadf50e 532 int i, valid, prot;
9a64fbe4
FB
533 int ret = -1;
534
535#if defined (DEBUG_BATS)
4a057712 536 if (loglevel != 0) {
6b542af7 537 fprintf(logfile, "%s: %cBAT v " ADDRX "\n", __func__,
76a66253 538 type == ACCESS_CODE ? 'I' : 'D', virtual);
9a64fbe4 539 }
9a64fbe4
FB
540#endif
541 switch (type) {
542 case ACCESS_CODE:
543 BATlt = env->IBAT[1];
544 BATut = env->IBAT[0];
545 break;
546 default:
547 BATlt = env->DBAT[1];
548 BATut = env->DBAT[0];
549 break;
550 }
9a64fbe4 551 base = virtual & 0xFFFC0000;
faadf50e 552 for (i = 0; i < env->nb_BATs; i++) {
9a64fbe4
FB
553 BATu = &BATut[i];
554 BATl = &BATlt[i];
555 BEPIu = *BATu & 0xF0000000;
556 BEPIl = *BATu & 0x0FFE0000;
faadf50e
JM
557 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
558 bat_601_size_prot(env, &bl, &valid, &prot, BATu, BATl);
559 } else {
560 bat_size_prot(env, &bl, &valid, &prot, BATu, BATl);
561 }
9a64fbe4 562#if defined (DEBUG_BATS)
4a057712 563 if (loglevel != 0) {
6b542af7
JM
564 fprintf(logfile, "%s: %cBAT%d v " ADDRX " BATu " ADDRX
565 " BATl " ADDRX "\n", __func__,
566 type == ACCESS_CODE ? 'I' : 'D', i, virtual, *BATu, *BATl);
9a64fbe4
FB
567 }
568#endif
569 if ((virtual & 0xF0000000) == BEPIu &&
570 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
571 /* BAT matches */
faadf50e 572 if (valid != 0) {
9a64fbe4 573 /* Get physical address */
76a66253 574 ctx->raddr = (*BATl & 0xF0000000) |
9a64fbe4 575 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
a541f297 576 (virtual & 0x0001F000);
b227a8e9 577 /* Compute access rights */
faadf50e 578 ctx->prot = prot;
b227a8e9 579 ret = check_prot(ctx->prot, rw, type);
9a64fbe4 580#if defined (DEBUG_BATS)
b227a8e9 581 if (ret == 0 && loglevel != 0) {
6b542af7 582 fprintf(logfile, "BAT %d match: r " PADDRX " prot=%c%c\n",
76a66253
JM
583 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
584 ctx->prot & PAGE_WRITE ? 'W' : '-');
9a64fbe4
FB
585 }
586#endif
9a64fbe4
FB
587 break;
588 }
589 }
590 }
591 if (ret < 0) {
592#if defined (DEBUG_BATS)
4a057712 593 if (loglevel != 0) {
6b542af7 594 fprintf(logfile, "no BAT match for " ADDRX ":\n", virtual);
4a057712
JM
595 for (i = 0; i < 4; i++) {
596 BATu = &BATut[i];
597 BATl = &BATlt[i];
598 BEPIu = *BATu & 0xF0000000;
599 BEPIl = *BATu & 0x0FFE0000;
600 bl = (*BATu & 0x00001FFC) << 15;
6b542af7
JM
601 fprintf(logfile, "%s: %cBAT%d v " ADDRX " BATu " ADDRX
602 " BATl " ADDRX " \n\t" ADDRX " " ADDRX " " ADDRX "\n",
4a057712
JM
603 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
604 *BATu, *BATl, BEPIu, BEPIl, bl);
605 }
9a64fbe4
FB
606 }
607#endif
9a64fbe4 608 }
b227a8e9 609
9a64fbe4
FB
610 /* No hit */
611 return ret;
612}
613
614/* PTE table lookup */
b227a8e9
JM
615static always_inline int _find_pte (mmu_ctx_t *ctx, int is_64b, int h,
616 int rw, int type)
9a64fbe4 617{
76a66253
JM
618 target_ulong base, pte0, pte1;
619 int i, good = -1;
caa4039c 620 int ret, r;
9a64fbe4 621
76a66253
JM
622 ret = -1; /* No entry found */
623 base = ctx->pg_addr[h];
9a64fbe4 624 for (i = 0; i < 8; i++) {
caa4039c
JM
625#if defined(TARGET_PPC64)
626 if (is_64b) {
627 pte0 = ldq_phys(base + (i * 16));
628 pte1 = ldq_phys(base + (i * 16) + 8);
b227a8e9 629 r = pte64_check(ctx, pte0, pte1, h, rw, type);
12de9a39
JM
630#if defined (DEBUG_MMU)
631 if (loglevel != 0) {
6b542af7
JM
632 fprintf(logfile, "Load pte from " ADDRX " => " ADDRX " " ADDRX
633 " %d %d %d " ADDRX "\n",
12de9a39
JM
634 base + (i * 16), pte0, pte1,
635 (int)(pte0 & 1), h, (int)((pte0 >> 1) & 1),
636 ctx->ptem);
637 }
638#endif
caa4039c
JM
639 } else
640#endif
641 {
642 pte0 = ldl_phys(base + (i * 8));
643 pte1 = ldl_phys(base + (i * 8) + 4);
b227a8e9 644 r = pte32_check(ctx, pte0, pte1, h, rw, type);
9a64fbe4 645#if defined (DEBUG_MMU)
12de9a39 646 if (loglevel != 0) {
6b542af7
JM
647 fprintf(logfile, "Load pte from " ADDRX " => " ADDRX " " ADDRX
648 " %d %d %d " ADDRX "\n",
12de9a39
JM
649 base + (i * 8), pte0, pte1,
650 (int)(pte0 >> 31), h, (int)((pte0 >> 6) & 1),
651 ctx->ptem);
652 }
9a64fbe4 653#endif
12de9a39 654 }
caa4039c 655 switch (r) {
76a66253
JM
656 case -3:
657 /* PTE inconsistency */
658 return -1;
659 case -2:
660 /* Access violation */
661 ret = -2;
662 good = i;
663 break;
664 case -1:
665 default:
666 /* No PTE match */
667 break;
668 case 0:
669 /* access granted */
670 /* XXX: we should go on looping to check all PTEs consistency
671 * but if we can speed-up the whole thing as the
672 * result would be undefined if PTEs are not consistent.
673 */
674 ret = 0;
675 good = i;
676 goto done;
9a64fbe4
FB
677 }
678 }
679 if (good != -1) {
76a66253 680 done:
9a64fbe4 681#if defined (DEBUG_MMU)
4a057712 682 if (loglevel != 0) {
6b542af7 683 fprintf(logfile, "found PTE at addr " PADDRX " prot=%01x ret=%d\n",
76a66253
JM
684 ctx->raddr, ctx->prot, ret);
685 }
9a64fbe4
FB
686#endif
687 /* Update page flags */
76a66253 688 pte1 = ctx->raddr;
caa4039c
JM
689 if (pte_update_flags(ctx, &pte1, ret, rw) == 1) {
690#if defined(TARGET_PPC64)
691 if (is_64b) {
692 stq_phys_notdirty(base + (good * 16) + 8, pte1);
693 } else
694#endif
695 {
696 stl_phys_notdirty(base + (good * 8) + 4, pte1);
697 }
698 }
9a64fbe4
FB
699 }
700
701 return ret;
79aceca5
FB
702}
703
a11b8151 704static always_inline int find_pte32 (mmu_ctx_t *ctx, int h, int rw, int type)
caa4039c 705{
b227a8e9 706 return _find_pte(ctx, 0, h, rw, type);
caa4039c
JM
707}
708
709#if defined(TARGET_PPC64)
a11b8151 710static always_inline int find_pte64 (mmu_ctx_t *ctx, int h, int rw, int type)
caa4039c 711{
b227a8e9 712 return _find_pte(ctx, 1, h, rw, type);
caa4039c
JM
713}
714#endif
715
b068d6a7 716static always_inline int find_pte (CPUState *env, mmu_ctx_t *ctx,
b227a8e9 717 int h, int rw, int type)
caa4039c
JM
718{
719#if defined(TARGET_PPC64)
add78955 720 if (env->mmu_model & POWERPC_MMU_64)
b227a8e9 721 return find_pte64(ctx, h, rw, type);
caa4039c
JM
722#endif
723
b227a8e9 724 return find_pte32(ctx, h, rw, type);
caa4039c
JM
725}
726
caa4039c 727#if defined(TARGET_PPC64)
a11b8151 728static always_inline int slb_is_valid (uint64_t slb64)
eacc3249
JM
729{
730 return slb64 & 0x0000000008000000ULL ? 1 : 0;
731}
732
a11b8151 733static always_inline void slb_invalidate (uint64_t *slb64)
eacc3249
JM
734{
735 *slb64 &= ~0x0000000008000000ULL;
736}
737
a11b8151
JM
738static always_inline int slb_lookup (CPUPPCState *env, target_ulong eaddr,
739 target_ulong *vsid,
740 target_ulong *page_mask, int *attr)
caa4039c
JM
741{
742 target_phys_addr_t sr_base;
743 target_ulong mask;
744 uint64_t tmp64;
745 uint32_t tmp;
746 int n, ret;
caa4039c
JM
747
748 ret = -5;
749 sr_base = env->spr[SPR_ASR];
12de9a39
JM
750#if defined(DEBUG_SLB)
751 if (loglevel != 0) {
752 fprintf(logfile, "%s: eaddr " ADDRX " base " PADDRX "\n",
753 __func__, eaddr, sr_base);
754 }
755#endif
caa4039c 756 mask = 0x0000000000000000ULL; /* Avoid gcc warning */
eacc3249 757 for (n = 0; n < env->slb_nr; n++) {
caa4039c 758 tmp64 = ldq_phys(sr_base);
12de9a39
JM
759 tmp = ldl_phys(sr_base + 8);
760#if defined(DEBUG_SLB)
761 if (loglevel != 0) {
b33c17e1
JM
762 fprintf(logfile, "%s: seg %d " PADDRX " %016" PRIx64 " %08"
763 PRIx32 "\n", __func__, n, sr_base, tmp64, tmp);
12de9a39
JM
764 }
765#endif
eacc3249 766 if (slb_is_valid(tmp64)) {
caa4039c
JM
767 /* SLB entry is valid */
768 switch (tmp64 & 0x0000000006000000ULL) {
769 case 0x0000000000000000ULL:
770 /* 256 MB segment */
771 mask = 0xFFFFFFFFF0000000ULL;
772 break;
773 case 0x0000000002000000ULL:
774 /* 1 TB segment */
775 mask = 0xFFFF000000000000ULL;
776 break;
777 case 0x0000000004000000ULL:
778 case 0x0000000006000000ULL:
779 /* Reserved => segment is invalid */
780 continue;
781 }
782 if ((eaddr & mask) == (tmp64 & mask)) {
783 /* SLB match */
caa4039c
JM
784 *vsid = ((tmp64 << 24) | (tmp >> 8)) & 0x0003FFFFFFFFFFFFULL;
785 *page_mask = ~mask;
786 *attr = tmp & 0xFF;
eacc3249 787 ret = n;
caa4039c
JM
788 break;
789 }
790 }
791 sr_base += 12;
792 }
793
794 return ret;
79aceca5 795}
12de9a39 796
eacc3249
JM
797void ppc_slb_invalidate_all (CPUPPCState *env)
798{
799 target_phys_addr_t sr_base;
800 uint64_t tmp64;
801 int n, do_invalidate;
802
803 do_invalidate = 0;
804 sr_base = env->spr[SPR_ASR];
2c1ee068
JM
805 /* XXX: Warning: slbia never invalidates the first segment */
806 for (n = 1; n < env->slb_nr; n++) {
eacc3249
JM
807 tmp64 = ldq_phys(sr_base);
808 if (slb_is_valid(tmp64)) {
809 slb_invalidate(&tmp64);
810 stq_phys(sr_base, tmp64);
811 /* XXX: given the fact that segment size is 256 MB or 1TB,
812 * and we still don't have a tlb_flush_mask(env, n, mask)
813 * in Qemu, we just invalidate all TLBs
814 */
815 do_invalidate = 1;
816 }
817 sr_base += 12;
818 }
819 if (do_invalidate)
820 tlb_flush(env, 1);
821}
822
823void ppc_slb_invalidate_one (CPUPPCState *env, uint64_t T0)
824{
825 target_phys_addr_t sr_base;
826 target_ulong vsid, page_mask;
827 uint64_t tmp64;
828 int attr;
829 int n;
830
831 n = slb_lookup(env, T0, &vsid, &page_mask, &attr);
832 if (n >= 0) {
833 sr_base = env->spr[SPR_ASR];
834 sr_base += 12 * n;
835 tmp64 = ldq_phys(sr_base);
836 if (slb_is_valid(tmp64)) {
837 slb_invalidate(&tmp64);
838 stq_phys(sr_base, tmp64);
839 /* XXX: given the fact that segment size is 256 MB or 1TB,
840 * and we still don't have a tlb_flush_mask(env, n, mask)
841 * in Qemu, we just invalidate all TLBs
842 */
843 tlb_flush(env, 1);
844 }
845 }
846}
847
12de9a39
JM
848target_ulong ppc_load_slb (CPUPPCState *env, int slb_nr)
849{
850 target_phys_addr_t sr_base;
851 target_ulong rt;
852 uint64_t tmp64;
853 uint32_t tmp;
854
855 sr_base = env->spr[SPR_ASR];
856 sr_base += 12 * slb_nr;
857 tmp64 = ldq_phys(sr_base);
858 tmp = ldl_phys(sr_base + 8);
859 if (tmp64 & 0x0000000008000000ULL) {
860 /* SLB entry is valid */
861 /* Copy SLB bits 62:88 to Rt 37:63 (VSID 23:49) */
862 rt = tmp >> 8; /* 65:88 => 40:63 */
863 rt |= (tmp64 & 0x7) << 24; /* 62:64 => 37:39 */
864 /* Copy SLB bits 89:92 to Rt 33:36 (KsKpNL) */
865 rt |= ((tmp >> 4) & 0xF) << 27;
866 } else {
867 rt = 0;
868 }
869#if defined(DEBUG_SLB)
870 if (loglevel != 0) {
871 fprintf(logfile, "%s: " PADDRX " %016" PRIx64 " %08" PRIx32 " => %d "
872 ADDRX "\n", __func__, sr_base, tmp64, tmp, slb_nr, rt);
873 }
874#endif
875
876 return rt;
877}
878
879void ppc_store_slb (CPUPPCState *env, int slb_nr, target_ulong rs)
880{
881 target_phys_addr_t sr_base;
882 uint64_t tmp64;
883 uint32_t tmp;
884
885 sr_base = env->spr[SPR_ASR];
886 sr_base += 12 * slb_nr;
887 /* Copy Rs bits 37:63 to SLB 62:88 */
888 tmp = rs << 8;
889 tmp64 = (rs >> 24) & 0x7;
890 /* Copy Rs bits 33:36 to SLB 89:92 */
891 tmp |= ((rs >> 27) & 0xF) << 4;
892 /* Set the valid bit */
893 tmp64 |= 1 << 27;
894 /* Set ESID */
895 tmp64 |= (uint32_t)slb_nr << 28;
896#if defined(DEBUG_SLB)
897 if (loglevel != 0) {
6b542af7
JM
898 fprintf(logfile, "%s: %d " ADDRX " => " PADDRX " %016" PRIx64
899 " %08" PRIx32 "\n", __func__,
900 slb_nr, rs, sr_base, tmp64, tmp);
12de9a39
JM
901 }
902#endif
903 /* Write SLB entry to memory */
904 stq_phys(sr_base, tmp64);
905 stl_phys(sr_base + 8, tmp);
906}
caa4039c 907#endif /* defined(TARGET_PPC64) */
79aceca5 908
9a64fbe4 909/* Perform segment based translation */
b068d6a7
JM
910static always_inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
911 int sdr_sh,
912 target_phys_addr_t hash,
913 target_phys_addr_t mask)
12de9a39 914{
6f2d8978 915 return (sdr1 & ((target_phys_addr_t)(-1ULL) << sdr_sh)) | (hash & mask);
12de9a39
JM
916}
917
a11b8151
JM
918static always_inline int get_segment (CPUState *env, mmu_ctx_t *ctx,
919 target_ulong eaddr, int rw, int type)
79aceca5 920{
12de9a39 921 target_phys_addr_t sdr, hash, mask, sdr_mask, htab_mask;
caa4039c
JM
922 target_ulong sr, vsid, vsid_mask, pgidx, page_mask;
923#if defined(TARGET_PPC64)
924 int attr;
9a64fbe4 925#endif
0411a972 926 int ds, vsid_sh, sdr_sh, pr;
caa4039c
JM
927 int ret, ret2;
928
0411a972 929 pr = msr_pr;
caa4039c 930#if defined(TARGET_PPC64)
add78955 931 if (env->mmu_model & POWERPC_MMU_64) {
12de9a39
JM
932#if defined (DEBUG_MMU)
933 if (loglevel != 0) {
934 fprintf(logfile, "Check SLBs\n");
935 }
936#endif
caa4039c
JM
937 ret = slb_lookup(env, eaddr, &vsid, &page_mask, &attr);
938 if (ret < 0)
939 return ret;
0411a972
JM
940 ctx->key = ((attr & 0x40) && (pr != 0)) ||
941 ((attr & 0x80) && (pr == 0)) ? 1 : 0;
caa4039c 942 ds = 0;
b227a8e9 943 ctx->nx = attr & 0x20 ? 1 : 0;
caa4039c
JM
944 vsid_mask = 0x00003FFFFFFFFF80ULL;
945 vsid_sh = 7;
946 sdr_sh = 18;
947 sdr_mask = 0x3FF80;
948 } else
949#endif /* defined(TARGET_PPC64) */
950 {
951 sr = env->sr[eaddr >> 28];
952 page_mask = 0x0FFFFFFF;
0411a972
JM
953 ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
954 ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
caa4039c 955 ds = sr & 0x80000000 ? 1 : 0;
b227a8e9 956 ctx->nx = sr & 0x10000000 ? 1 : 0;
caa4039c
JM
957 vsid = sr & 0x00FFFFFF;
958 vsid_mask = 0x01FFFFC0;
959 vsid_sh = 6;
960 sdr_sh = 16;
961 sdr_mask = 0xFFC0;
9a64fbe4 962#if defined (DEBUG_MMU)
caa4039c 963 if (loglevel != 0) {
6b542af7
JM
964 fprintf(logfile, "Check segment v=" ADDRX " %d " ADDRX
965 " nip=" ADDRX " lr=" ADDRX " ir=%d dr=%d pr=%d %d t=%d\n",
caa4039c 966 eaddr, (int)(eaddr >> 28), sr, env->nip,
0411a972
JM
967 env->lr, (int)msr_ir, (int)msr_dr, pr != 0 ? 1 : 0,
968 rw, type);
caa4039c 969 }
9a64fbe4 970#endif
caa4039c 971 }
12de9a39
JM
972#if defined (DEBUG_MMU)
973 if (loglevel != 0) {
974 fprintf(logfile, "pte segment: key=%d ds %d nx %d vsid " ADDRX "\n",
b227a8e9 975 ctx->key, ds, ctx->nx, vsid);
12de9a39
JM
976 }
977#endif
caa4039c
JM
978 ret = -1;
979 if (!ds) {
9a64fbe4 980 /* Check if instruction fetch is allowed, if needed */
b227a8e9 981 if (type != ACCESS_CODE || ctx->nx == 0) {
9a64fbe4 982 /* Page address translation */
76a66253
JM
983 /* Primary table address */
984 sdr = env->sdr1;
12de9a39
JM
985 pgidx = (eaddr & page_mask) >> TARGET_PAGE_BITS;
986#if defined(TARGET_PPC64)
add78955 987 if (env->mmu_model & POWERPC_MMU_64) {
12de9a39
JM
988 htab_mask = 0x0FFFFFFF >> (28 - (sdr & 0x1F));
989 /* XXX: this is false for 1 TB segments */
990 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
991 } else
992#endif
993 {
994 htab_mask = sdr & 0x000001FF;
995 hash = ((vsid ^ pgidx) << vsid_sh) & vsid_mask;
996 }
997 mask = (htab_mask << sdr_sh) | sdr_mask;
998#if defined (DEBUG_MMU)
999 if (loglevel != 0) {
6b542af7
JM
1000 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX
1001 " mask " PADDRX " " ADDRX "\n",
1002 sdr, sdr_sh, hash, mask, page_mask);
12de9a39
JM
1003 }
1004#endif
caa4039c 1005 ctx->pg_addr[0] = get_pgaddr(sdr, sdr_sh, hash, mask);
76a66253 1006 /* Secondary table address */
caa4039c 1007 hash = (~hash) & vsid_mask;
12de9a39
JM
1008#if defined (DEBUG_MMU)
1009 if (loglevel != 0) {
6b542af7
JM
1010 fprintf(logfile, "sdr " PADDRX " sh %d hash " PADDRX
1011 " mask " PADDRX "\n",
1012 sdr, sdr_sh, hash, mask);
12de9a39
JM
1013 }
1014#endif
caa4039c
JM
1015 ctx->pg_addr[1] = get_pgaddr(sdr, sdr_sh, hash, mask);
1016#if defined(TARGET_PPC64)
add78955 1017 if (env->mmu_model & POWERPC_MMU_64) {
caa4039c
JM
1018 /* Only 5 bits of the page index are used in the AVPN */
1019 ctx->ptem = (vsid << 12) | ((pgidx >> 4) & 0x0F80);
1020 } else
1021#endif
1022 {
1023 ctx->ptem = (vsid << 7) | (pgidx >> 10);
1024 }
76a66253 1025 /* Initialize real address with an invalid value */
6f2d8978 1026 ctx->raddr = (target_phys_addr_t)-1ULL;
7dbe11ac
JM
1027 if (unlikely(env->mmu_model == POWERPC_MMU_SOFT_6xx ||
1028 env->mmu_model == POWERPC_MMU_SOFT_74xx)) {
76a66253
JM
1029 /* Software TLB search */
1030 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
76a66253 1031 } else {
9a64fbe4 1032#if defined (DEBUG_MMU)
4a057712 1033 if (loglevel != 0) {
6b542af7
JM
1034 fprintf(logfile, "0 sdr1=" PADDRX " vsid=" ADDRX " "
1035 "api=" ADDRX " hash=" PADDRX
1036 " pg_addr=" PADDRX "\n",
1037 sdr, vsid, pgidx, hash, ctx->pg_addr[0]);
76a66253 1038 }
9a64fbe4 1039#endif
76a66253 1040 /* Primary table lookup */
b227a8e9 1041 ret = find_pte(env, ctx, 0, rw, type);
76a66253
JM
1042 if (ret < 0) {
1043 /* Secondary table lookup */
9a64fbe4 1044#if defined (DEBUG_MMU)
4a057712 1045 if (eaddr != 0xEFFFFFFF && loglevel != 0) {
6b542af7
JM
1046 fprintf(logfile, "1 sdr1=" PADDRX " vsid=" ADDRX " "
1047 "api=" ADDRX " hash=" PADDRX
1048 " pg_addr=" PADDRX "\n",
1049 sdr, vsid, pgidx, hash, ctx->pg_addr[1]);
76a66253 1050 }
9a64fbe4 1051#endif
b227a8e9 1052 ret2 = find_pte(env, ctx, 1, rw, type);
76a66253
JM
1053 if (ret2 != -1)
1054 ret = ret2;
1055 }
9a64fbe4 1056 }
0411a972 1057#if defined (DUMP_PAGE_TABLES)
b33c17e1
JM
1058 if (loglevel != 0) {
1059 target_phys_addr_t curaddr;
1060 uint32_t a0, a1, a2, a3;
6b542af7 1061 fprintf(logfile, "Page table: " PADDRX " len " PADDRX "\n",
b33c17e1
JM
1062 sdr, mask + 0x80);
1063 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
1064 curaddr += 16) {
1065 a0 = ldl_phys(curaddr);
1066 a1 = ldl_phys(curaddr + 4);
1067 a2 = ldl_phys(curaddr + 8);
1068 a3 = ldl_phys(curaddr + 12);
1069 if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
6b542af7 1070 fprintf(logfile, PADDRX ": %08x %08x %08x %08x\n",
b33c17e1 1071 curaddr, a0, a1, a2, a3);
12de9a39 1072 }
b33c17e1
JM
1073 }
1074 }
12de9a39 1075#endif
9a64fbe4
FB
1076 } else {
1077#if defined (DEBUG_MMU)
4a057712 1078 if (loglevel != 0)
76a66253 1079 fprintf(logfile, "No access allowed\n");
9a64fbe4 1080#endif
76a66253 1081 ret = -3;
9a64fbe4
FB
1082 }
1083 } else {
1084#if defined (DEBUG_MMU)
4a057712 1085 if (loglevel != 0)
76a66253 1086 fprintf(logfile, "direct store...\n");
9a64fbe4
FB
1087#endif
1088 /* Direct-store segment : absolutely *BUGGY* for now */
1089 switch (type) {
1090 case ACCESS_INT:
1091 /* Integer load/store : only access allowed */
1092 break;
1093 case ACCESS_CODE:
1094 /* No code fetch is allowed in direct-store areas */
1095 return -4;
1096 case ACCESS_FLOAT:
1097 /* Floating point load/store */
1098 return -4;
1099 case ACCESS_RES:
1100 /* lwarx, ldarx or srwcx. */
1101 return -4;
1102 case ACCESS_CACHE:
1103 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
1104 /* Should make the instruction do no-op.
1105 * As it already do no-op, it's quite easy :-)
1106 */
76a66253 1107 ctx->raddr = eaddr;
9a64fbe4
FB
1108 return 0;
1109 case ACCESS_EXT:
1110 /* eciwx or ecowx */
1111 return -4;
1112 default:
1113 if (logfile) {
1114 fprintf(logfile, "ERROR: instruction should not need "
1115 "address translation\n");
1116 }
9a64fbe4
FB
1117 return -4;
1118 }
76a66253
JM
1119 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
1120 ctx->raddr = eaddr;
9a64fbe4
FB
1121 ret = 2;
1122 } else {
1123 ret = -2;
1124 }
79aceca5 1125 }
9a64fbe4
FB
1126
1127 return ret;
79aceca5
FB
1128}
1129
c294fc58 1130/* Generic TLB check function for embedded PowerPC implementations */
a11b8151
JM
1131static always_inline int ppcemb_tlb_check (CPUState *env, ppcemb_tlb_t *tlb,
1132 target_phys_addr_t *raddrp,
1133 target_ulong address,
1134 uint32_t pid, int ext, int i)
c294fc58
JM
1135{
1136 target_ulong mask;
1137
1138 /* Check valid flag */
1139 if (!(tlb->prot & PAGE_VALID)) {
1140 if (loglevel != 0)
1141 fprintf(logfile, "%s: TLB %d not valid\n", __func__, i);
1142 return -1;
1143 }
1144 mask = ~(tlb->size - 1);
daf4f96e 1145#if defined (DEBUG_SOFTWARE_TLB)
c294fc58 1146 if (loglevel != 0) {
6b542af7
JM
1147 fprintf(logfile, "%s: TLB %d address " ADDRX " PID %u <=> " ADDRX
1148 " " ADDRX " %u\n",
1149 __func__, i, address, pid, tlb->EPN, mask, (uint32_t)tlb->PID);
c294fc58 1150 }
daf4f96e 1151#endif
c294fc58 1152 /* Check PID */
36081602 1153 if (tlb->PID != 0 && tlb->PID != pid)
c294fc58
JM
1154 return -1;
1155 /* Check effective address */
1156 if ((address & mask) != tlb->EPN)
1157 return -1;
1158 *raddrp = (tlb->RPN & mask) | (address & ~mask);
9706285b 1159#if (TARGET_PHYS_ADDR_BITS >= 36)
36081602
JM
1160 if (ext) {
1161 /* Extend the physical address to 36 bits */
1162 *raddrp |= (target_phys_addr_t)(tlb->RPN & 0xF) << 32;
1163 }
9706285b 1164#endif
c294fc58
JM
1165
1166 return 0;
1167}
1168
1169/* Generic TLB search function for PowerPC embedded implementations */
36081602 1170int ppcemb_tlb_search (CPUPPCState *env, target_ulong address, uint32_t pid)
c294fc58
JM
1171{
1172 ppcemb_tlb_t *tlb;
1173 target_phys_addr_t raddr;
1174 int i, ret;
1175
1176 /* Default return value is no match */
1177 ret = -1;
a750fc0b 1178 for (i = 0; i < env->nb_tlb; i++) {
c294fc58 1179 tlb = &env->tlb[i].tlbe;
36081602 1180 if (ppcemb_tlb_check(env, tlb, &raddr, address, pid, 0, i) == 0) {
c294fc58
JM
1181 ret = i;
1182 break;
1183 }
1184 }
1185
1186 return ret;
1187}
1188
daf4f96e 1189/* Helpers specific to PowerPC 40x implementations */
a11b8151 1190static always_inline void ppc4xx_tlb_invalidate_all (CPUState *env)
a750fc0b
JM
1191{
1192 ppcemb_tlb_t *tlb;
a750fc0b
JM
1193 int i;
1194
1195 for (i = 0; i < env->nb_tlb; i++) {
1196 tlb = &env->tlb[i].tlbe;
daf4f96e 1197 tlb->prot &= ~PAGE_VALID;
a750fc0b 1198 }
daf4f96e 1199 tlb_flush(env, 1);
a750fc0b
JM
1200}
1201
a11b8151
JM
1202static always_inline void ppc4xx_tlb_invalidate_virt (CPUState *env,
1203 target_ulong eaddr,
1204 uint32_t pid)
0a032cbe 1205{
daf4f96e 1206#if !defined(FLUSH_ALL_TLBS)
0a032cbe 1207 ppcemb_tlb_t *tlb;
daf4f96e
JM
1208 target_phys_addr_t raddr;
1209 target_ulong page, end;
0a032cbe
JM
1210 int i;
1211
1212 for (i = 0; i < env->nb_tlb; i++) {
1213 tlb = &env->tlb[i].tlbe;
daf4f96e 1214 if (ppcemb_tlb_check(env, tlb, &raddr, eaddr, pid, 0, i) == 0) {
0a032cbe
JM
1215 end = tlb->EPN + tlb->size;
1216 for (page = tlb->EPN; page < end; page += TARGET_PAGE_SIZE)
1217 tlb_flush_page(env, page);
0a032cbe 1218 tlb->prot &= ~PAGE_VALID;
daf4f96e 1219 break;
0a032cbe
JM
1220 }
1221 }
daf4f96e
JM
1222#else
1223 ppc4xx_tlb_invalidate_all(env);
1224#endif
0a032cbe
JM
1225}
1226
36081602 1227int mmu40x_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
e96efcfc 1228 target_ulong address, int rw, int access_type)
a8dea12f
JM
1229{
1230 ppcemb_tlb_t *tlb;
1231 target_phys_addr_t raddr;
0411a972 1232 int i, ret, zsel, zpr, pr;
3b46e624 1233
c55e9aef 1234 ret = -1;
6f2d8978 1235 raddr = (target_phys_addr_t)-1ULL;
0411a972 1236 pr = msr_pr;
a8dea12f
JM
1237 for (i = 0; i < env->nb_tlb; i++) {
1238 tlb = &env->tlb[i].tlbe;
36081602
JM
1239 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1240 env->spr[SPR_40x_PID], 0, i) < 0)
a8dea12f 1241 continue;
a8dea12f
JM
1242 zsel = (tlb->attr >> 4) & 0xF;
1243 zpr = (env->spr[SPR_40x_ZPR] >> (28 - (2 * zsel))) & 0x3;
daf4f96e 1244#if defined (DEBUG_SOFTWARE_TLB)
4a057712 1245 if (loglevel != 0) {
a8dea12f
JM
1246 fprintf(logfile, "%s: TLB %d zsel %d zpr %d rw %d attr %08x\n",
1247 __func__, i, zsel, zpr, rw, tlb->attr);
1248 }
daf4f96e 1249#endif
b227a8e9
JM
1250 /* Check execute enable bit */
1251 switch (zpr) {
1252 case 0x2:
0411a972 1253 if (pr != 0)
b227a8e9
JM
1254 goto check_perms;
1255 /* No break here */
1256 case 0x3:
1257 /* All accesses granted */
1258 ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
1259 ret = 0;
1260 break;
1261 case 0x0:
0411a972 1262 if (pr != 0) {
b227a8e9
JM
1263 ctx->prot = 0;
1264 ret = -2;
a8dea12f
JM
1265 break;
1266 }
b227a8e9
JM
1267 /* No break here */
1268 case 0x1:
1269 check_perms:
1270 /* Check from TLB entry */
1271 /* XXX: there is a problem here or in the TLB fill code... */
1272 ctx->prot = tlb->prot;
1273 ctx->prot |= PAGE_EXEC;
1274 ret = check_prot(ctx->prot, rw, access_type);
1275 break;
a8dea12f
JM
1276 }
1277 if (ret >= 0) {
1278 ctx->raddr = raddr;
daf4f96e 1279#if defined (DEBUG_SOFTWARE_TLB)
4a057712 1280 if (loglevel != 0) {
6b542af7 1281 fprintf(logfile, "%s: access granted " ADDRX " => " PADDRX
c55e9aef
JM
1282 " %d %d\n", __func__, address, ctx->raddr, ctx->prot,
1283 ret);
a8dea12f 1284 }
daf4f96e 1285#endif
c55e9aef 1286 return 0;
a8dea12f
JM
1287 }
1288 }
daf4f96e 1289#if defined (DEBUG_SOFTWARE_TLB)
4a057712 1290 if (loglevel != 0) {
6b542af7 1291 fprintf(logfile, "%s: access refused " ADDRX " => " PADDRX
c55e9aef
JM
1292 " %d %d\n", __func__, address, raddr, ctx->prot,
1293 ret);
1294 }
daf4f96e 1295#endif
3b46e624 1296
a8dea12f
JM
1297 return ret;
1298}
1299
c294fc58
JM
1300void store_40x_sler (CPUPPCState *env, uint32_t val)
1301{
1302 /* XXX: TO BE FIXED */
1303 if (val != 0x00000000) {
1304 cpu_abort(env, "Little-endian regions are not supported by now\n");
1305 }
1306 env->spr[SPR_405_SLER] = val;
1307}
1308
5eb7995e
JM
1309int mmubooke_get_physical_address (CPUState *env, mmu_ctx_t *ctx,
1310 target_ulong address, int rw,
1311 int access_type)
1312{
1313 ppcemb_tlb_t *tlb;
1314 target_phys_addr_t raddr;
1315 int i, prot, ret;
1316
1317 ret = -1;
6f2d8978 1318 raddr = (target_phys_addr_t)-1ULL;
5eb7995e
JM
1319 for (i = 0; i < env->nb_tlb; i++) {
1320 tlb = &env->tlb[i].tlbe;
1321 if (ppcemb_tlb_check(env, tlb, &raddr, address,
1322 env->spr[SPR_BOOKE_PID], 1, i) < 0)
1323 continue;
0411a972 1324 if (msr_pr != 0)
5eb7995e
JM
1325 prot = tlb->prot & 0xF;
1326 else
1327 prot = (tlb->prot >> 4) & 0xF;
1328 /* Check the address space */
1329 if (access_type == ACCESS_CODE) {
d26bfc9a 1330 if (msr_ir != (tlb->attr & 1))
5eb7995e
JM
1331 continue;
1332 ctx->prot = prot;
1333 if (prot & PAGE_EXEC) {
1334 ret = 0;
1335 break;
1336 }
1337 ret = -3;
1338 } else {
d26bfc9a 1339 if (msr_dr != (tlb->attr & 1))
5eb7995e
JM
1340 continue;
1341 ctx->prot = prot;
1342 if ((!rw && prot & PAGE_READ) || (rw && (prot & PAGE_WRITE))) {
1343 ret = 0;
1344 break;
1345 }
1346 ret = -2;
1347 }
1348 }
1349 if (ret >= 0)
1350 ctx->raddr = raddr;
1351
1352 return ret;
1353}
1354
a11b8151
JM
1355static always_inline int check_physical (CPUState *env, mmu_ctx_t *ctx,
1356 target_ulong eaddr, int rw)
76a66253
JM
1357{
1358 int in_plb, ret;
3b46e624 1359
76a66253 1360 ctx->raddr = eaddr;
b227a8e9 1361 ctx->prot = PAGE_READ | PAGE_EXEC;
76a66253 1362 ret = 0;
a750fc0b
JM
1363 switch (env->mmu_model) {
1364 case POWERPC_MMU_32B:
faadf50e 1365 case POWERPC_MMU_601:
a750fc0b 1366 case POWERPC_MMU_SOFT_6xx:
7dbe11ac 1367 case POWERPC_MMU_SOFT_74xx:
a750fc0b 1368 case POWERPC_MMU_SOFT_4xx:
b4095fed 1369 case POWERPC_MMU_REAL:
7dbe11ac 1370 case POWERPC_MMU_BOOKE:
caa4039c
JM
1371 ctx->prot |= PAGE_WRITE;
1372 break;
1373#if defined(TARGET_PPC64)
add78955 1374 case POWERPC_MMU_620:
a750fc0b 1375 case POWERPC_MMU_64B:
caa4039c 1376 /* Real address are 60 bits long */
a750fc0b 1377 ctx->raddr &= 0x0FFFFFFFFFFFFFFFULL;
caa4039c
JM
1378 ctx->prot |= PAGE_WRITE;
1379 break;
9706285b 1380#endif
a750fc0b 1381 case POWERPC_MMU_SOFT_4xx_Z:
caa4039c
JM
1382 if (unlikely(msr_pe != 0)) {
1383 /* 403 family add some particular protections,
1384 * using PBL/PBU registers for accesses with no translation.
1385 */
1386 in_plb =
1387 /* Check PLB validity */
1388 (env->pb[0] < env->pb[1] &&
1389 /* and address in plb area */
1390 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
1391 (env->pb[2] < env->pb[3] &&
1392 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
1393 if (in_plb ^ msr_px) {
1394 /* Access in protected area */
1395 if (rw == 1) {
1396 /* Access is not allowed */
1397 ret = -2;
1398 }
1399 } else {
1400 /* Read-write access is allowed */
1401 ctx->prot |= PAGE_WRITE;
76a66253 1402 }
76a66253 1403 }
e1833e1f 1404 break;
b4095fed
JM
1405 case POWERPC_MMU_MPC8xx:
1406 /* XXX: TODO */
1407 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1408 break;
a750fc0b 1409 case POWERPC_MMU_BOOKE_FSL:
caa4039c
JM
1410 /* XXX: TODO */
1411 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1412 break;
1413 default:
1414 cpu_abort(env, "Unknown or invalid MMU model\n");
1415 return -1;
76a66253
JM
1416 }
1417
1418 return ret;
1419}
1420
1421int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
faadf50e 1422 int rw, int access_type)
9a64fbe4
FB
1423{
1424 int ret;
0411a972 1425
514fb8c1 1426#if 0
4a057712 1427 if (loglevel != 0) {
9a64fbe4
FB
1428 fprintf(logfile, "%s\n", __func__);
1429 }
d9bce9d9 1430#endif
4b3686fa
FB
1431 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
1432 (access_type != ACCESS_CODE && msr_dr == 0)) {
9a64fbe4 1433 /* No address translation */
76a66253 1434 ret = check_physical(env, ctx, eaddr, rw);
9a64fbe4 1435 } else {
c55e9aef 1436 ret = -1;
a750fc0b
JM
1437 switch (env->mmu_model) {
1438 case POWERPC_MMU_32B:
faadf50e 1439 case POWERPC_MMU_601:
a750fc0b 1440 case POWERPC_MMU_SOFT_6xx:
7dbe11ac 1441 case POWERPC_MMU_SOFT_74xx:
c55e9aef 1442#if defined(TARGET_PPC64)
add78955 1443 case POWERPC_MMU_620:
a750fc0b 1444 case POWERPC_MMU_64B:
c55e9aef 1445#endif
faadf50e
JM
1446 /* Try to find a BAT */
1447 if (env->nb_BATs != 0)
1448 ret = get_bat(env, ctx, eaddr, rw, access_type);
a8dea12f 1449 if (ret < 0) {
c55e9aef 1450 /* We didn't match any BAT entry or don't have BATs */
a8dea12f
JM
1451 ret = get_segment(env, ctx, eaddr, rw, access_type);
1452 }
1453 break;
a750fc0b
JM
1454 case POWERPC_MMU_SOFT_4xx:
1455 case POWERPC_MMU_SOFT_4xx_Z:
36081602 1456 ret = mmu40x_get_physical_address(env, ctx, eaddr,
a8dea12f
JM
1457 rw, access_type);
1458 break;
a750fc0b 1459 case POWERPC_MMU_BOOKE:
5eb7995e
JM
1460 ret = mmubooke_get_physical_address(env, ctx, eaddr,
1461 rw, access_type);
1462 break;
b4095fed
JM
1463 case POWERPC_MMU_MPC8xx:
1464 /* XXX: TODO */
1465 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1466 break;
a750fc0b 1467 case POWERPC_MMU_BOOKE_FSL:
c55e9aef
JM
1468 /* XXX: TODO */
1469 cpu_abort(env, "BookE FSL MMU model not implemented\n");
1470 return -1;
b4095fed
JM
1471 case POWERPC_MMU_REAL:
1472 cpu_abort(env, "PowerPC in real mode do not do any translation\n");
2662a059 1473 return -1;
c55e9aef
JM
1474 default:
1475 cpu_abort(env, "Unknown or invalid MMU model\n");
a8dea12f 1476 return -1;
9a64fbe4
FB
1477 }
1478 }
514fb8c1 1479#if 0
4a057712
JM
1480 if (loglevel != 0) {
1481 fprintf(logfile, "%s address " ADDRX " => %d " PADDRX "\n",
c55e9aef 1482 __func__, eaddr, ret, ctx->raddr);
a541f297 1483 }
76a66253 1484#endif
d9bce9d9 1485
9a64fbe4
FB
1486 return ret;
1487}
1488
9b3c35e0 1489target_phys_addr_t cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
a6b025d3 1490{
76a66253 1491 mmu_ctx_t ctx;
a6b025d3 1492
faadf50e 1493 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT) != 0))
a6b025d3 1494 return -1;
76a66253
JM
1495
1496 return ctx.raddr & TARGET_PAGE_MASK;
a6b025d3 1497}
9a64fbe4 1498
9a64fbe4 1499/* Perform address translation */
e96efcfc 1500int cpu_ppc_handle_mmu_fault (CPUState *env, target_ulong address, int rw,
6ebbf390 1501 int mmu_idx, int is_softmmu)
9a64fbe4 1502{
76a66253 1503 mmu_ctx_t ctx;
a541f297 1504 int access_type;
9a64fbe4 1505 int ret = 0;
d9bce9d9 1506
b769d8fe
FB
1507 if (rw == 2) {
1508 /* code access */
1509 rw = 0;
1510 access_type = ACCESS_CODE;
1511 } else {
1512 /* data access */
b4cec7b4 1513 access_type = env->access_type;
b769d8fe 1514 }
faadf50e 1515 ret = get_physical_address(env, &ctx, address, rw, access_type);
9a64fbe4 1516 if (ret == 0) {
b227a8e9
JM
1517 ret = tlb_set_page_exec(env, address & TARGET_PAGE_MASK,
1518 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
1519 mmu_idx, is_softmmu);
9a64fbe4 1520 } else if (ret < 0) {
9a64fbe4 1521#if defined (DEBUG_MMU)
4a057712 1522 if (loglevel != 0)
76a66253 1523 cpu_dump_state(env, logfile, fprintf, 0);
9a64fbe4
FB
1524#endif
1525 if (access_type == ACCESS_CODE) {
9a64fbe4
FB
1526 switch (ret) {
1527 case -1:
76a66253 1528 /* No matches in page tables or TLB */
a750fc0b
JM
1529 switch (env->mmu_model) {
1530 case POWERPC_MMU_SOFT_6xx:
8f793433
JM
1531 env->exception_index = POWERPC_EXCP_IFTLB;
1532 env->error_code = 1 << 18;
76a66253
JM
1533 env->spr[SPR_IMISS] = address;
1534 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
76a66253 1535 goto tlb_miss;
7dbe11ac 1536 case POWERPC_MMU_SOFT_74xx:
8f793433 1537 env->exception_index = POWERPC_EXCP_IFTLB;
7dbe11ac 1538 goto tlb_miss_74xx;
a750fc0b
JM
1539 case POWERPC_MMU_SOFT_4xx:
1540 case POWERPC_MMU_SOFT_4xx_Z:
8f793433
JM
1541 env->exception_index = POWERPC_EXCP_ITLB;
1542 env->error_code = 0;
a8dea12f
JM
1543 env->spr[SPR_40x_DEAR] = address;
1544 env->spr[SPR_40x_ESR] = 0x00000000;
c55e9aef 1545 break;
a750fc0b 1546 case POWERPC_MMU_32B:
faadf50e 1547 case POWERPC_MMU_601:
c55e9aef 1548#if defined(TARGET_PPC64)
add78955 1549 case POWERPC_MMU_620:
a750fc0b 1550 case POWERPC_MMU_64B:
c55e9aef 1551#endif
8f793433
JM
1552 env->exception_index = POWERPC_EXCP_ISI;
1553 env->error_code = 0x40000000;
1554 break;
a750fc0b 1555 case POWERPC_MMU_BOOKE:
c55e9aef 1556 /* XXX: TODO */
b4095fed 1557 cpu_abort(env, "BookE MMU model is not implemented\n");
c55e9aef 1558 return -1;
a750fc0b 1559 case POWERPC_MMU_BOOKE_FSL:
c55e9aef 1560 /* XXX: TODO */
b4095fed 1561 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
c55e9aef 1562 return -1;
b4095fed
JM
1563 case POWERPC_MMU_MPC8xx:
1564 /* XXX: TODO */
1565 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1566 break;
1567 case POWERPC_MMU_REAL:
1568 cpu_abort(env, "PowerPC in real mode should never raise "
1569 "any MMU exceptions\n");
2662a059 1570 return -1;
c55e9aef
JM
1571 default:
1572 cpu_abort(env, "Unknown or invalid MMU model\n");
1573 return -1;
76a66253 1574 }
9a64fbe4
FB
1575 break;
1576 case -2:
1577 /* Access rights violation */
8f793433
JM
1578 env->exception_index = POWERPC_EXCP_ISI;
1579 env->error_code = 0x08000000;
9a64fbe4
FB
1580 break;
1581 case -3:
76a66253 1582 /* No execute protection violation */
8f793433
JM
1583 env->exception_index = POWERPC_EXCP_ISI;
1584 env->error_code = 0x10000000;
9a64fbe4
FB
1585 break;
1586 case -4:
1587 /* Direct store exception */
1588 /* No code fetch is allowed in direct-store areas */
8f793433
JM
1589 env->exception_index = POWERPC_EXCP_ISI;
1590 env->error_code = 0x10000000;
2be0071f 1591 break;
e1833e1f 1592#if defined(TARGET_PPC64)
2be0071f
FB
1593 case -5:
1594 /* No match in segment table */
add78955
JM
1595 if (env->mmu_model == POWERPC_MMU_620) {
1596 env->exception_index = POWERPC_EXCP_ISI;
1597 /* XXX: this might be incorrect */
1598 env->error_code = 0x40000000;
1599 } else {
1600 env->exception_index = POWERPC_EXCP_ISEG;
1601 env->error_code = 0;
1602 }
9a64fbe4 1603 break;
e1833e1f 1604#endif
9a64fbe4
FB
1605 }
1606 } else {
9a64fbe4
FB
1607 switch (ret) {
1608 case -1:
76a66253 1609 /* No matches in page tables or TLB */
a750fc0b
JM
1610 switch (env->mmu_model) {
1611 case POWERPC_MMU_SOFT_6xx:
76a66253 1612 if (rw == 1) {
8f793433
JM
1613 env->exception_index = POWERPC_EXCP_DSTLB;
1614 env->error_code = 1 << 16;
76a66253 1615 } else {
8f793433
JM
1616 env->exception_index = POWERPC_EXCP_DLTLB;
1617 env->error_code = 0;
76a66253
JM
1618 }
1619 env->spr[SPR_DMISS] = address;
1620 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
1621 tlb_miss:
8f793433 1622 env->error_code |= ctx.key << 19;
76a66253
JM
1623 env->spr[SPR_HASH1] = ctx.pg_addr[0];
1624 env->spr[SPR_HASH2] = ctx.pg_addr[1];
8f793433 1625 break;
7dbe11ac
JM
1626 case POWERPC_MMU_SOFT_74xx:
1627 if (rw == 1) {
8f793433 1628 env->exception_index = POWERPC_EXCP_DSTLB;
7dbe11ac 1629 } else {
8f793433 1630 env->exception_index = POWERPC_EXCP_DLTLB;
7dbe11ac
JM
1631 }
1632 tlb_miss_74xx:
1633 /* Implement LRU algorithm */
8f793433 1634 env->error_code = ctx.key << 19;
7dbe11ac
JM
1635 env->spr[SPR_TLBMISS] = (address & ~((target_ulong)0x3)) |
1636 ((env->last_way + 1) & (env->nb_ways - 1));
1637 env->spr[SPR_PTEHI] = 0x80000000 | ctx.ptem;
7dbe11ac 1638 break;
a750fc0b
JM
1639 case POWERPC_MMU_SOFT_4xx:
1640 case POWERPC_MMU_SOFT_4xx_Z:
8f793433
JM
1641 env->exception_index = POWERPC_EXCP_DTLB;
1642 env->error_code = 0;
a8dea12f
JM
1643 env->spr[SPR_40x_DEAR] = address;
1644 if (rw)
1645 env->spr[SPR_40x_ESR] = 0x00800000;
1646 else
1647 env->spr[SPR_40x_ESR] = 0x00000000;
c55e9aef 1648 break;
a750fc0b 1649 case POWERPC_MMU_32B:
faadf50e 1650 case POWERPC_MMU_601:
c55e9aef 1651#if defined(TARGET_PPC64)
add78955 1652 case POWERPC_MMU_620:
a750fc0b 1653 case POWERPC_MMU_64B:
c55e9aef 1654#endif
8f793433
JM
1655 env->exception_index = POWERPC_EXCP_DSI;
1656 env->error_code = 0;
1657 env->spr[SPR_DAR] = address;
1658 if (rw == 1)
1659 env->spr[SPR_DSISR] = 0x42000000;
1660 else
1661 env->spr[SPR_DSISR] = 0x40000000;
1662 break;
b4095fed
JM
1663 case POWERPC_MMU_MPC8xx:
1664 /* XXX: TODO */
1665 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1666 break;
a750fc0b 1667 case POWERPC_MMU_BOOKE:
c55e9aef 1668 /* XXX: TODO */
b4095fed 1669 cpu_abort(env, "BookE MMU model is not implemented\n");
c55e9aef 1670 return -1;
a750fc0b 1671 case POWERPC_MMU_BOOKE_FSL:
c55e9aef 1672 /* XXX: TODO */
b4095fed 1673 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
c55e9aef 1674 return -1;
b4095fed
JM
1675 case POWERPC_MMU_REAL:
1676 cpu_abort(env, "PowerPC in real mode should never raise "
1677 "any MMU exceptions\n");
2662a059 1678 return -1;
c55e9aef
JM
1679 default:
1680 cpu_abort(env, "Unknown or invalid MMU model\n");
1681 return -1;
76a66253 1682 }
9a64fbe4
FB
1683 break;
1684 case -2:
1685 /* Access rights violation */
8f793433
JM
1686 env->exception_index = POWERPC_EXCP_DSI;
1687 env->error_code = 0;
1688 env->spr[SPR_DAR] = address;
1689 if (rw == 1)
1690 env->spr[SPR_DSISR] = 0x0A000000;
1691 else
1692 env->spr[SPR_DSISR] = 0x08000000;
9a64fbe4
FB
1693 break;
1694 case -4:
1695 /* Direct store exception */
1696 switch (access_type) {
1697 case ACCESS_FLOAT:
1698 /* Floating point load/store */
8f793433
JM
1699 env->exception_index = POWERPC_EXCP_ALIGN;
1700 env->error_code = POWERPC_EXCP_ALIGN_FP;
1701 env->spr[SPR_DAR] = address;
9a64fbe4
FB
1702 break;
1703 case ACCESS_RES:
8f793433
JM
1704 /* lwarx, ldarx or stwcx. */
1705 env->exception_index = POWERPC_EXCP_DSI;
1706 env->error_code = 0;
1707 env->spr[SPR_DAR] = address;
1708 if (rw == 1)
1709 env->spr[SPR_DSISR] = 0x06000000;
1710 else
1711 env->spr[SPR_DSISR] = 0x04000000;
9a64fbe4
FB
1712 break;
1713 case ACCESS_EXT:
1714 /* eciwx or ecowx */
8f793433
JM
1715 env->exception_index = POWERPC_EXCP_DSI;
1716 env->error_code = 0;
1717 env->spr[SPR_DAR] = address;
1718 if (rw == 1)
1719 env->spr[SPR_DSISR] = 0x06100000;
1720 else
1721 env->spr[SPR_DSISR] = 0x04100000;
9a64fbe4
FB
1722 break;
1723 default:
76a66253 1724 printf("DSI: invalid exception (%d)\n", ret);
8f793433
JM
1725 env->exception_index = POWERPC_EXCP_PROGRAM;
1726 env->error_code =
1727 POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
1728 env->spr[SPR_DAR] = address;
9a64fbe4
FB
1729 break;
1730 }
fdabc366 1731 break;
e1833e1f 1732#if defined(TARGET_PPC64)
2be0071f
FB
1733 case -5:
1734 /* No match in segment table */
add78955
JM
1735 if (env->mmu_model == POWERPC_MMU_620) {
1736 env->exception_index = POWERPC_EXCP_DSI;
1737 env->error_code = 0;
1738 env->spr[SPR_DAR] = address;
1739 /* XXX: this might be incorrect */
1740 if (rw == 1)
1741 env->spr[SPR_DSISR] = 0x42000000;
1742 else
1743 env->spr[SPR_DSISR] = 0x40000000;
1744 } else {
1745 env->exception_index = POWERPC_EXCP_DSEG;
1746 env->error_code = 0;
1747 env->spr[SPR_DAR] = address;
1748 }
2be0071f 1749 break;
e1833e1f 1750#endif
9a64fbe4 1751 }
9a64fbe4
FB
1752 }
1753#if 0
8f793433
JM
1754 printf("%s: set exception to %d %02x\n", __func__,
1755 env->exception, env->error_code);
9a64fbe4 1756#endif
9a64fbe4
FB
1757 ret = 1;
1758 }
76a66253 1759
9a64fbe4
FB
1760 return ret;
1761}
1762
3fc6c082
FB
1763/*****************************************************************************/
1764/* BATs management */
1765#if !defined(FLUSH_ALL_TLBS)
b068d6a7
JM
1766static always_inline void do_invalidate_BAT (CPUPPCState *env,
1767 target_ulong BATu,
1768 target_ulong mask)
3fc6c082
FB
1769{
1770 target_ulong base, end, page;
76a66253 1771
3fc6c082
FB
1772 base = BATu & ~0x0001FFFF;
1773 end = base + mask + 0x00020000;
1774#if defined (DEBUG_BATS)
76a66253 1775 if (loglevel != 0) {
1b9eb036 1776 fprintf(logfile, "Flush BAT from " ADDRX " to " ADDRX " (" ADDRX ")\n",
76a66253
JM
1777 base, end, mask);
1778 }
3fc6c082
FB
1779#endif
1780 for (page = base; page != end; page += TARGET_PAGE_SIZE)
1781 tlb_flush_page(env, page);
1782#if defined (DEBUG_BATS)
1783 if (loglevel != 0)
1784 fprintf(logfile, "Flush done\n");
1785#endif
1786}
1787#endif
1788
b068d6a7
JM
1789static always_inline void dump_store_bat (CPUPPCState *env, char ID,
1790 int ul, int nr, target_ulong value)
3fc6c082
FB
1791{
1792#if defined (DEBUG_BATS)
1793 if (loglevel != 0) {
6b542af7 1794 fprintf(logfile, "Set %cBAT%d%c to " ADDRX " (" ADDRX ")\n",
1b9eb036 1795 ID, nr, ul == 0 ? 'u' : 'l', value, env->nip);
3fc6c082
FB
1796 }
1797#endif
1798}
1799
45d827d2 1800void ppc_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
3fc6c082
FB
1801{
1802 target_ulong mask;
1803
1804 dump_store_bat(env, 'I', 0, nr, value);
1805 if (env->IBAT[0][nr] != value) {
1806 mask = (value << 15) & 0x0FFE0000UL;
1807#if !defined(FLUSH_ALL_TLBS)
1808 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1809#endif
1810 /* When storing valid upper BAT, mask BEPI and BRPN
1811 * and invalidate all TLBs covered by this BAT
1812 */
1813 mask = (value << 15) & 0x0FFE0000UL;
1814 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1815 (value & ~0x0001FFFFUL & ~mask);
1816 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
1817 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
1818#if !defined(FLUSH_ALL_TLBS)
1819 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
76a66253 1820#else
3fc6c082
FB
1821 tlb_flush(env, 1);
1822#endif
1823 }
1824}
1825
45d827d2 1826void ppc_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
3fc6c082
FB
1827{
1828 dump_store_bat(env, 'I', 1, nr, value);
1829 env->IBAT[1][nr] = value;
1830}
1831
45d827d2 1832void ppc_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
3fc6c082
FB
1833{
1834 target_ulong mask;
1835
1836 dump_store_bat(env, 'D', 0, nr, value);
1837 if (env->DBAT[0][nr] != value) {
1838 /* When storing valid upper BAT, mask BEPI and BRPN
1839 * and invalidate all TLBs covered by this BAT
1840 */
1841 mask = (value << 15) & 0x0FFE0000UL;
1842#if !defined(FLUSH_ALL_TLBS)
1843 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1844#endif
1845 mask = (value << 15) & 0x0FFE0000UL;
1846 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
1847 (value & ~0x0001FFFFUL & ~mask);
1848 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
1849 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
1850#if !defined(FLUSH_ALL_TLBS)
1851 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
1852#else
1853 tlb_flush(env, 1);
1854#endif
1855 }
1856}
1857
45d827d2 1858void ppc_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
3fc6c082
FB
1859{
1860 dump_store_bat(env, 'D', 1, nr, value);
1861 env->DBAT[1][nr] = value;
1862}
1863
45d827d2 1864void ppc_store_ibatu_601 (CPUPPCState *env, int nr, target_ulong value)
056401ea
JM
1865{
1866 target_ulong mask;
1867 int do_inval;
1868
1869 dump_store_bat(env, 'I', 0, nr, value);
1870 if (env->IBAT[0][nr] != value) {
1871 do_inval = 0;
1872 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1873 if (env->IBAT[1][nr] & 0x40) {
1874 /* Invalidate BAT only if it is valid */
1875#if !defined(FLUSH_ALL_TLBS)
1876 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1877#else
1878 do_inval = 1;
1879#endif
1880 }
1881 /* When storing valid upper BAT, mask BEPI and BRPN
1882 * and invalidate all TLBs covered by this BAT
1883 */
1884 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
1885 (value & ~0x0001FFFFUL & ~mask);
1886 env->DBAT[0][nr] = env->IBAT[0][nr];
1887 if (env->IBAT[1][nr] & 0x40) {
1888#if !defined(FLUSH_ALL_TLBS)
1889 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1890#else
1891 do_inval = 1;
1892#endif
1893 }
1894#if defined(FLUSH_ALL_TLBS)
1895 if (do_inval)
1896 tlb_flush(env, 1);
1897#endif
1898 }
1899}
1900
45d827d2 1901void ppc_store_ibatl_601 (CPUPPCState *env, int nr, target_ulong value)
056401ea
JM
1902{
1903 target_ulong mask;
1904 int do_inval;
1905
1906 dump_store_bat(env, 'I', 1, nr, value);
1907 if (env->IBAT[1][nr] != value) {
1908 do_inval = 0;
1909 if (env->IBAT[1][nr] & 0x40) {
1910#if !defined(FLUSH_ALL_TLBS)
1911 mask = (env->IBAT[1][nr] << 17) & 0x0FFE0000UL;
1912 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1913#else
1914 do_inval = 1;
1915#endif
1916 }
1917 if (value & 0x40) {
1918#if !defined(FLUSH_ALL_TLBS)
1919 mask = (value << 17) & 0x0FFE0000UL;
1920 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
1921#else
1922 do_inval = 1;
1923#endif
1924 }
1925 env->IBAT[1][nr] = value;
1926 env->DBAT[1][nr] = value;
1927#if defined(FLUSH_ALL_TLBS)
1928 if (do_inval)
1929 tlb_flush(env, 1);
1930#endif
1931 }
1932}
1933
0a032cbe
JM
1934/*****************************************************************************/
1935/* TLB management */
1936void ppc_tlb_invalidate_all (CPUPPCState *env)
1937{
daf4f96e
JM
1938 switch (env->mmu_model) {
1939 case POWERPC_MMU_SOFT_6xx:
7dbe11ac 1940 case POWERPC_MMU_SOFT_74xx:
0a032cbe 1941 ppc6xx_tlb_invalidate_all(env);
daf4f96e
JM
1942 break;
1943 case POWERPC_MMU_SOFT_4xx:
1944 case POWERPC_MMU_SOFT_4xx_Z:
0a032cbe 1945 ppc4xx_tlb_invalidate_all(env);
daf4f96e 1946 break;
b4095fed 1947 case POWERPC_MMU_REAL:
7dbe11ac
JM
1948 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1949 break;
b4095fed
JM
1950 case POWERPC_MMU_MPC8xx:
1951 /* XXX: TODO */
1952 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1953 break;
7dbe11ac
JM
1954 case POWERPC_MMU_BOOKE:
1955 /* XXX: TODO */
b4095fed 1956 cpu_abort(env, "BookE MMU model is not implemented\n");
7dbe11ac
JM
1957 break;
1958 case POWERPC_MMU_BOOKE_FSL:
1959 /* XXX: TODO */
b4095fed 1960 cpu_abort(env, "BookE MMU model is not implemented\n");
7dbe11ac 1961 break;
7dbe11ac 1962 case POWERPC_MMU_32B:
faadf50e 1963 case POWERPC_MMU_601:
00af685f 1964#if defined(TARGET_PPC64)
add78955 1965 case POWERPC_MMU_620:
7dbe11ac 1966 case POWERPC_MMU_64B:
00af685f 1967#endif /* defined(TARGET_PPC64) */
0a032cbe 1968 tlb_flush(env, 1);
daf4f96e 1969 break;
00af685f
JM
1970 default:
1971 /* XXX: TODO */
12de9a39 1972 cpu_abort(env, "Unknown MMU model\n");
00af685f 1973 break;
0a032cbe
JM
1974 }
1975}
1976
daf4f96e
JM
1977void ppc_tlb_invalidate_one (CPUPPCState *env, target_ulong addr)
1978{
1979#if !defined(FLUSH_ALL_TLBS)
1980 addr &= TARGET_PAGE_MASK;
1981 switch (env->mmu_model) {
1982 case POWERPC_MMU_SOFT_6xx:
7dbe11ac 1983 case POWERPC_MMU_SOFT_74xx:
daf4f96e
JM
1984 ppc6xx_tlb_invalidate_virt(env, addr, 0);
1985 if (env->id_tlbs == 1)
1986 ppc6xx_tlb_invalidate_virt(env, addr, 1);
1987 break;
1988 case POWERPC_MMU_SOFT_4xx:
1989 case POWERPC_MMU_SOFT_4xx_Z:
1990 ppc4xx_tlb_invalidate_virt(env, addr, env->spr[SPR_40x_PID]);
1991 break;
b4095fed 1992 case POWERPC_MMU_REAL:
7dbe11ac
JM
1993 cpu_abort(env, "No TLB for PowerPC 4xx in real mode\n");
1994 break;
b4095fed
JM
1995 case POWERPC_MMU_MPC8xx:
1996 /* XXX: TODO */
1997 cpu_abort(env, "MPC8xx MMU model is not implemented\n");
1998 break;
7dbe11ac
JM
1999 case POWERPC_MMU_BOOKE:
2000 /* XXX: TODO */
b4095fed 2001 cpu_abort(env, "BookE MMU model is not implemented\n");
7dbe11ac
JM
2002 break;
2003 case POWERPC_MMU_BOOKE_FSL:
2004 /* XXX: TODO */
b4095fed 2005 cpu_abort(env, "BookE FSL MMU model is not implemented\n");
7dbe11ac
JM
2006 break;
2007 case POWERPC_MMU_32B:
faadf50e 2008 case POWERPC_MMU_601:
daf4f96e 2009 /* tlbie invalidate TLBs for all segments */
6f2d8978 2010 addr &= ~((target_ulong)-1ULL << 28);
daf4f96e
JM
2011 /* XXX: this case should be optimized,
2012 * giving a mask to tlb_flush_page
2013 */
2014 tlb_flush_page(env, addr | (0x0 << 28));
2015 tlb_flush_page(env, addr | (0x1 << 28));
2016 tlb_flush_page(env, addr | (0x2 << 28));
2017 tlb_flush_page(env, addr | (0x3 << 28));
2018 tlb_flush_page(env, addr | (0x4 << 28));
2019 tlb_flush_page(env, addr | (0x5 << 28));
2020 tlb_flush_page(env, addr | (0x6 << 28));
2021 tlb_flush_page(env, addr | (0x7 << 28));
2022 tlb_flush_page(env, addr | (0x8 << 28));
2023 tlb_flush_page(env, addr | (0x9 << 28));
2024 tlb_flush_page(env, addr | (0xA << 28));
2025 tlb_flush_page(env, addr | (0xB << 28));
2026 tlb_flush_page(env, addr | (0xC << 28));
2027 tlb_flush_page(env, addr | (0xD << 28));
2028 tlb_flush_page(env, addr | (0xE << 28));
2029 tlb_flush_page(env, addr | (0xF << 28));
7dbe11ac 2030 break;
00af685f 2031#if defined(TARGET_PPC64)
add78955 2032 case POWERPC_MMU_620:
7dbe11ac 2033 case POWERPC_MMU_64B:
7dbe11ac
JM
2034 /* tlbie invalidate TLBs for all segments */
2035 /* XXX: given the fact that there are too many segments to invalidate,
00af685f 2036 * and we still don't have a tlb_flush_mask(env, n, mask) in Qemu,
7dbe11ac
JM
2037 * we just invalidate all TLBs
2038 */
2039 tlb_flush(env, 1);
2040 break;
00af685f
JM
2041#endif /* defined(TARGET_PPC64) */
2042 default:
2043 /* XXX: TODO */
12de9a39 2044 cpu_abort(env, "Unknown MMU model\n");
00af685f 2045 break;
daf4f96e
JM
2046 }
2047#else
2048 ppc_tlb_invalidate_all(env);
2049#endif
2050}
2051
3fc6c082
FB
2052/*****************************************************************************/
2053/* Special registers manipulation */
d9bce9d9 2054#if defined(TARGET_PPC64)
d9bce9d9
JM
2055void ppc_store_asr (CPUPPCState *env, target_ulong value)
2056{
2057 if (env->asr != value) {
2058 env->asr = value;
2059 tlb_flush(env, 1);
2060 }
2061}
2062#endif
2063
45d827d2 2064void ppc_store_sdr1 (CPUPPCState *env, target_ulong value)
3fc6c082
FB
2065{
2066#if defined (DEBUG_MMU)
2067 if (loglevel != 0) {
6b542af7 2068 fprintf(logfile, "%s: " ADDRX "\n", __func__, value);
3fc6c082
FB
2069 }
2070#endif
2071 if (env->sdr1 != value) {
12de9a39
JM
2072 /* XXX: for PowerPC 64, should check that the HTABSIZE value
2073 * is <= 28
2074 */
3fc6c082 2075 env->sdr1 = value;
76a66253 2076 tlb_flush(env, 1);
3fc6c082
FB
2077 }
2078}
2079
45d827d2 2080void ppc_store_sr (CPUPPCState *env, int srnum, target_ulong value)
3fc6c082
FB
2081{
2082#if defined (DEBUG_MMU)
2083 if (loglevel != 0) {
6b542af7 2084 fprintf(logfile, "%s: reg=%d " ADDRX " " ADDRX "\n",
1b9eb036 2085 __func__, srnum, value, env->sr[srnum]);
3fc6c082
FB
2086 }
2087#endif
2088 if (env->sr[srnum] != value) {
2089 env->sr[srnum] = value;
2090#if !defined(FLUSH_ALL_TLBS) && 0
2091 {
2092 target_ulong page, end;
2093 /* Invalidate 256 MB of virtual memory */
2094 page = (16 << 20) * srnum;
2095 end = page + (16 << 20);
2096 for (; page != end; page += TARGET_PAGE_SIZE)
2097 tlb_flush_page(env, page);
2098 }
2099#else
76a66253 2100 tlb_flush(env, 1);
3fc6c082
FB
2101#endif
2102 }
2103}
76a66253 2104#endif /* !defined (CONFIG_USER_ONLY) */
3fc6c082 2105
76a66253 2106/* GDBstub can read and write MSR... */
0411a972 2107void ppc_store_msr (CPUPPCState *env, target_ulong value)
3fc6c082 2108{
a4f30719 2109 hreg_store_msr(env, value, 0);
3fc6c082
FB
2110}
2111
2112/*****************************************************************************/
2113/* Exception processing */
18fba28c 2114#if defined (CONFIG_USER_ONLY)
9a64fbe4 2115void do_interrupt (CPUState *env)
79aceca5 2116{
e1833e1f
JM
2117 env->exception_index = POWERPC_EXCP_NONE;
2118 env->error_code = 0;
18fba28c 2119}
47103572 2120
e9df014c 2121void ppc_hw_interrupt (CPUState *env)
47103572 2122{
e1833e1f
JM
2123 env->exception_index = POWERPC_EXCP_NONE;
2124 env->error_code = 0;
47103572 2125}
76a66253 2126#else /* defined (CONFIG_USER_ONLY) */
a11b8151 2127static always_inline void dump_syscall (CPUState *env)
d094807b 2128{
6b542af7
JM
2129 fprintf(logfile, "syscall r0=" REGX " r3=" REGX " r4=" REGX
2130 " r5=" REGX " r6=" REGX " nip=" ADDRX "\n",
2131 ppc_dump_gpr(env, 0), ppc_dump_gpr(env, 3), ppc_dump_gpr(env, 4),
2132 ppc_dump_gpr(env, 5), ppc_dump_gpr(env, 6), env->nip);
d094807b
FB
2133}
2134
e1833e1f
JM
2135/* Note that this function should be greatly optimized
2136 * when called with a constant excp, from ppc_hw_interrupt
2137 */
2138static always_inline void powerpc_excp (CPUState *env,
2139 int excp_model, int excp)
18fba28c 2140{
0411a972 2141 target_ulong msr, new_msr, vector;
e1833e1f 2142 int srr0, srr1, asrr0, asrr1;
a4f30719 2143 int lpes0, lpes1, lev;
79aceca5 2144
b172c56a
JM
2145 if (0) {
2146 /* XXX: find a suitable condition to enable the hypervisor mode */
2147 lpes0 = (env->spr[SPR_LPCR] >> 1) & 1;
2148 lpes1 = (env->spr[SPR_LPCR] >> 2) & 1;
2149 } else {
2150 /* Those values ensure we won't enter the hypervisor mode */
2151 lpes0 = 0;
2152 lpes1 = 1;
2153 }
2154
b769d8fe 2155 if (loglevel & CPU_LOG_INT) {
6b542af7 2156 fprintf(logfile, "Raise exception at " ADDRX " => %08x (%02x)\n",
1b9eb036 2157 env->nip, excp, env->error_code);
b769d8fe 2158 }
0411a972
JM
2159 msr = env->msr;
2160 new_msr = msr;
e1833e1f
JM
2161 srr0 = SPR_SRR0;
2162 srr1 = SPR_SRR1;
2163 asrr0 = -1;
2164 asrr1 = -1;
2165 msr &= ~((target_ulong)0x783F0000);
9a64fbe4 2166 switch (excp) {
e1833e1f
JM
2167 case POWERPC_EXCP_NONE:
2168 /* Should never happen */
2169 return;
2170 case POWERPC_EXCP_CRITICAL: /* Critical input */
0411a972 2171 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
e1833e1f 2172 switch (excp_model) {
a750fc0b 2173 case POWERPC_EXCP_40x:
e1833e1f
JM
2174 srr0 = SPR_40x_SRR2;
2175 srr1 = SPR_40x_SRR3;
c62db105 2176 break;
a750fc0b 2177 case POWERPC_EXCP_BOOKE:
e1833e1f
JM
2178 srr0 = SPR_BOOKE_CSRR0;
2179 srr1 = SPR_BOOKE_CSRR1;
c62db105 2180 break;
e1833e1f 2181 case POWERPC_EXCP_G2:
c62db105 2182 break;
e1833e1f
JM
2183 default:
2184 goto excp_invalid;
2be0071f 2185 }
9a64fbe4 2186 goto store_next;
e1833e1f
JM
2187 case POWERPC_EXCP_MCHECK: /* Machine check exception */
2188 if (msr_me == 0) {
e63ecc6f
JM
2189 /* Machine check exception is not enabled.
2190 * Enter checkstop state.
2191 */
2192 if (loglevel != 0) {
2193 fprintf(logfile, "Machine check while not allowed. "
2194 "Entering checkstop state\n");
2195 } else {
2196 fprintf(stderr, "Machine check while not allowed. "
2197 "Entering checkstop state\n");
2198 }
2199 env->halted = 1;
2200 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
e1833e1f 2201 }
0411a972
JM
2202 new_msr &= ~((target_ulong)1 << MSR_RI);
2203 new_msr &= ~((target_ulong)1 << MSR_ME);
b172c56a
JM
2204 if (0) {
2205 /* XXX: find a suitable condition to enable the hypervisor mode */
a4f30719 2206 new_msr |= (target_ulong)MSR_HVB;
b172c56a 2207 }
e1833e1f
JM
2208 /* XXX: should also have something loaded in DAR / DSISR */
2209 switch (excp_model) {
a750fc0b 2210 case POWERPC_EXCP_40x:
e1833e1f
JM
2211 srr0 = SPR_40x_SRR2;
2212 srr1 = SPR_40x_SRR3;
c62db105 2213 break;
a750fc0b 2214 case POWERPC_EXCP_BOOKE:
e1833e1f
JM
2215 srr0 = SPR_BOOKE_MCSRR0;
2216 srr1 = SPR_BOOKE_MCSRR1;
2217 asrr0 = SPR_BOOKE_CSRR0;
2218 asrr1 = SPR_BOOKE_CSRR1;
c62db105
JM
2219 break;
2220 default:
2221 break;
2be0071f 2222 }
e1833e1f
JM
2223 goto store_next;
2224 case POWERPC_EXCP_DSI: /* Data storage exception */
a541f297 2225#if defined (DEBUG_EXCEPTIONS)
4a057712 2226 if (loglevel != 0) {
6b542af7
JM
2227 fprintf(logfile, "DSI exception: DSISR=" ADDRX" DAR=" ADDRX "\n",
2228 env->spr[SPR_DSISR], env->spr[SPR_DAR]);
76a66253 2229 }
e1833e1f 2230#endif
0411a972 2231 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2232 if (lpes1 == 0)
a4f30719 2233 new_msr |= (target_ulong)MSR_HVB;
a541f297 2234 goto store_next;
e1833e1f 2235 case POWERPC_EXCP_ISI: /* Instruction storage exception */
a541f297 2236#if defined (DEBUG_EXCEPTIONS)
76a66253 2237 if (loglevel != 0) {
6b542af7
JM
2238 fprintf(logfile, "ISI exception: msr=" ADDRX ", nip=" ADDRX "\n",
2239 msr, env->nip);
76a66253 2240 }
a541f297 2241#endif
0411a972 2242 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2243 if (lpes1 == 0)
a4f30719 2244 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2245 msr |= env->error_code;
9a64fbe4 2246 goto store_next;
e1833e1f 2247 case POWERPC_EXCP_EXTERNAL: /* External input */
0411a972 2248 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2249 if (lpes0 == 1)
a4f30719 2250 new_msr |= (target_ulong)MSR_HVB;
9a64fbe4 2251 goto store_next;
e1833e1f 2252 case POWERPC_EXCP_ALIGN: /* Alignment exception */
0411a972 2253 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2254 if (lpes1 == 0)
a4f30719 2255 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2256 /* XXX: this is false */
2257 /* Get rS/rD and rA from faulting opcode */
2258 env->spr[SPR_DSISR] |= (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
9a64fbe4 2259 goto store_current;
e1833e1f 2260 case POWERPC_EXCP_PROGRAM: /* Program exception */
9a64fbe4 2261 switch (env->error_code & ~0xF) {
e1833e1f
JM
2262 case POWERPC_EXCP_FP:
2263 if ((msr_fe0 == 0 && msr_fe1 == 0) || msr_fp == 0) {
9a64fbe4 2264#if defined (DEBUG_EXCEPTIONS)
4a057712 2265 if (loglevel != 0) {
a496775f
JM
2266 fprintf(logfile, "Ignore floating point exception\n");
2267 }
9a64fbe4 2268#endif
7c58044c
JM
2269 env->exception_index = POWERPC_EXCP_NONE;
2270 env->error_code = 0;
9a64fbe4 2271 return;
76a66253 2272 }
0411a972 2273 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2274 if (lpes1 == 0)
a4f30719 2275 new_msr |= (target_ulong)MSR_HVB;
9a64fbe4 2276 msr |= 0x00100000;
5b52b991
JM
2277 if (msr_fe0 == msr_fe1)
2278 goto store_next;
2279 msr |= 0x00010000;
76a66253 2280 break;
e1833e1f 2281 case POWERPC_EXCP_INVAL:
a496775f 2282#if defined (DEBUG_EXCEPTIONS)
4a057712 2283 if (loglevel != 0) {
6b542af7 2284 fprintf(logfile, "Invalid instruction at " ADDRX "\n",
a496775f
JM
2285 env->nip);
2286 }
e1833e1f 2287#endif
0411a972 2288 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2289 if (lpes1 == 0)
a4f30719 2290 new_msr |= (target_ulong)MSR_HVB;
9a64fbe4 2291 msr |= 0x00080000;
76a66253 2292 break;
e1833e1f 2293 case POWERPC_EXCP_PRIV:
0411a972 2294 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2295 if (lpes1 == 0)
a4f30719 2296 new_msr |= (target_ulong)MSR_HVB;
9a64fbe4 2297 msr |= 0x00040000;
76a66253 2298 break;
e1833e1f 2299 case POWERPC_EXCP_TRAP:
0411a972 2300 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2301 if (lpes1 == 0)
a4f30719 2302 new_msr |= (target_ulong)MSR_HVB;
9a64fbe4
FB
2303 msr |= 0x00020000;
2304 break;
2305 default:
2306 /* Should never occur */
e1833e1f
JM
2307 cpu_abort(env, "Invalid program exception %d. Aborting\n",
2308 env->error_code);
76a66253
JM
2309 break;
2310 }
5b52b991 2311 goto store_current;
e1833e1f 2312 case POWERPC_EXCP_FPU: /* Floating-point unavailable exception */
0411a972 2313 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2314 if (lpes1 == 0)
a4f30719 2315 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2316 goto store_current;
2317 case POWERPC_EXCP_SYSCALL: /* System call exception */
d094807b
FB
2318 /* NOTE: this is a temporary hack to support graphics OSI
2319 calls from the MOL driver */
e1833e1f 2320 /* XXX: To be removed */
d094807b
FB
2321 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
2322 env->osi_call) {
7c58044c
JM
2323 if (env->osi_call(env) != 0) {
2324 env->exception_index = POWERPC_EXCP_NONE;
2325 env->error_code = 0;
d094807b 2326 return;
7c58044c 2327 }
d094807b 2328 }
b769d8fe 2329 if (loglevel & CPU_LOG_INT) {
d094807b 2330 dump_syscall(env);
b769d8fe 2331 }
0411a972 2332 new_msr &= ~((target_ulong)1 << MSR_RI);
f9fdea6b 2333 lev = env->error_code;
e1833e1f 2334 if (lev == 1 || (lpes0 == 0 && lpes1 == 0))
a4f30719 2335 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2336 goto store_next;
2337 case POWERPC_EXCP_APU: /* Auxiliary processor unavailable */
0411a972 2338 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f
JM
2339 goto store_current;
2340 case POWERPC_EXCP_DECR: /* Decrementer exception */
0411a972 2341 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2342 if (lpes1 == 0)
a4f30719 2343 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2344 goto store_next;
2345 case POWERPC_EXCP_FIT: /* Fixed-interval timer interrupt */
2346 /* FIT on 4xx */
2347#if defined (DEBUG_EXCEPTIONS)
2348 if (loglevel != 0)
2349 fprintf(logfile, "FIT exception\n");
2350#endif
0411a972 2351 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
9a64fbe4 2352 goto store_next;
e1833e1f
JM
2353 case POWERPC_EXCP_WDT: /* Watchdog timer interrupt */
2354#if defined (DEBUG_EXCEPTIONS)
2355 if (loglevel != 0)
2356 fprintf(logfile, "WDT exception\n");
2357#endif
2358 switch (excp_model) {
2359 case POWERPC_EXCP_BOOKE:
2360 srr0 = SPR_BOOKE_CSRR0;
2361 srr1 = SPR_BOOKE_CSRR1;
2362 break;
2363 default:
2364 break;
2365 }
0411a972 2366 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
2be0071f 2367 goto store_next;
e1833e1f 2368 case POWERPC_EXCP_DTLB: /* Data TLB error */
0411a972 2369 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
e1833e1f
JM
2370 goto store_next;
2371 case POWERPC_EXCP_ITLB: /* Instruction TLB error */
0411a972 2372 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
e1833e1f
JM
2373 goto store_next;
2374 case POWERPC_EXCP_DEBUG: /* Debug interrupt */
2375 switch (excp_model) {
2376 case POWERPC_EXCP_BOOKE:
2377 srr0 = SPR_BOOKE_DSRR0;
2378 srr1 = SPR_BOOKE_DSRR1;
2379 asrr0 = SPR_BOOKE_CSRR0;
2380 asrr1 = SPR_BOOKE_CSRR1;
2381 break;
2382 default:
2383 break;
2384 }
2be0071f 2385 /* XXX: TODO */
e1833e1f 2386 cpu_abort(env, "Debug exception is not implemented yet !\n");
2be0071f 2387 goto store_next;
e1833e1f 2388 case POWERPC_EXCP_SPEU: /* SPE/embedded floating-point unavailable */
0411a972 2389 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
e1833e1f
JM
2390 goto store_current;
2391 case POWERPC_EXCP_EFPDI: /* Embedded floating-point data interrupt */
2be0071f 2392 /* XXX: TODO */
e1833e1f 2393 cpu_abort(env, "Embedded floating point data exception "
2be0071f
FB
2394 "is not implemented yet !\n");
2395 goto store_next;
e1833e1f 2396 case POWERPC_EXCP_EFPRI: /* Embedded floating-point round interrupt */
2be0071f 2397 /* XXX: TODO */
e1833e1f
JM
2398 cpu_abort(env, "Embedded floating point round exception "
2399 "is not implemented yet !\n");
9a64fbe4 2400 goto store_next;
e1833e1f 2401 case POWERPC_EXCP_EPERFM: /* Embedded performance monitor interrupt */
0411a972 2402 new_msr &= ~((target_ulong)1 << MSR_RI);
2be0071f
FB
2403 /* XXX: TODO */
2404 cpu_abort(env,
e1833e1f 2405 "Performance counter exception is not implemented yet !\n");
9a64fbe4 2406 goto store_next;
e1833e1f 2407 case POWERPC_EXCP_DOORI: /* Embedded doorbell interrupt */
76a66253 2408 /* XXX: TODO */
e1833e1f
JM
2409 cpu_abort(env,
2410 "Embedded doorbell interrupt is not implemented yet !\n");
2be0071f 2411 goto store_next;
e1833e1f
JM
2412 case POWERPC_EXCP_DOORCI: /* Embedded doorbell critical interrupt */
2413 switch (excp_model) {
2414 case POWERPC_EXCP_BOOKE:
2415 srr0 = SPR_BOOKE_CSRR0;
2416 srr1 = SPR_BOOKE_CSRR1;
a750fc0b 2417 break;
2be0071f 2418 default:
2be0071f
FB
2419 break;
2420 }
e1833e1f
JM
2421 /* XXX: TODO */
2422 cpu_abort(env, "Embedded doorbell critical interrupt "
2423 "is not implemented yet !\n");
2424 goto store_next;
e1833e1f 2425 case POWERPC_EXCP_RESET: /* System reset exception */
0411a972 2426 new_msr &= ~((target_ulong)1 << MSR_RI);
a4f30719
JM
2427 if (0) {
2428 /* XXX: find a suitable condition to enable the hypervisor mode */
2429 new_msr |= (target_ulong)MSR_HVB;
2430 }
e1833e1f 2431 goto store_next;
e1833e1f 2432 case POWERPC_EXCP_DSEG: /* Data segment exception */
0411a972 2433 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2434 if (lpes1 == 0)
a4f30719 2435 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2436 goto store_next;
2437 case POWERPC_EXCP_ISEG: /* Instruction segment exception */
0411a972 2438 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2439 if (lpes1 == 0)
a4f30719 2440 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2441 goto store_next;
e1833e1f
JM
2442 case POWERPC_EXCP_HDECR: /* Hypervisor decrementer exception */
2443 srr0 = SPR_HSRR0;
f9fdea6b 2444 srr1 = SPR_HSRR1;
a4f30719 2445 new_msr |= (target_ulong)MSR_HVB;
b172c56a 2446 goto store_next;
e1833e1f 2447 case POWERPC_EXCP_TRACE: /* Trace exception */
0411a972 2448 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2449 if (lpes1 == 0)
a4f30719 2450 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2451 goto store_next;
e1833e1f
JM
2452 case POWERPC_EXCP_HDSI: /* Hypervisor data storage exception */
2453 srr0 = SPR_HSRR0;
f9fdea6b 2454 srr1 = SPR_HSRR1;
a4f30719 2455 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2456 goto store_next;
2457 case POWERPC_EXCP_HISI: /* Hypervisor instruction storage exception */
2458 srr0 = SPR_HSRR0;
f9fdea6b 2459 srr1 = SPR_HSRR1;
a4f30719 2460 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2461 goto store_next;
2462 case POWERPC_EXCP_HDSEG: /* Hypervisor data segment exception */
2463 srr0 = SPR_HSRR0;
f9fdea6b 2464 srr1 = SPR_HSRR1;
a4f30719 2465 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2466 goto store_next;
2467 case POWERPC_EXCP_HISEG: /* Hypervisor instruction segment exception */
2468 srr0 = SPR_HSRR0;
f9fdea6b 2469 srr1 = SPR_HSRR1;
a4f30719 2470 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2471 goto store_next;
e1833e1f 2472 case POWERPC_EXCP_VPU: /* Vector unavailable exception */
0411a972 2473 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2474 if (lpes1 == 0)
a4f30719 2475 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2476 goto store_current;
2477 case POWERPC_EXCP_PIT: /* Programmable interval timer interrupt */
a496775f 2478#if defined (DEBUG_EXCEPTIONS)
e1833e1f
JM
2479 if (loglevel != 0)
2480 fprintf(logfile, "PIT exception\n");
2481#endif
0411a972 2482 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
e1833e1f
JM
2483 goto store_next;
2484 case POWERPC_EXCP_IO: /* IO error exception */
2485 /* XXX: TODO */
2486 cpu_abort(env, "601 IO error exception is not implemented yet !\n");
2487 goto store_next;
2488 case POWERPC_EXCP_RUNM: /* Run mode exception */
2489 /* XXX: TODO */
2490 cpu_abort(env, "601 run mode exception is not implemented yet !\n");
2491 goto store_next;
2492 case POWERPC_EXCP_EMUL: /* Emulation trap exception */
2493 /* XXX: TODO */
2494 cpu_abort(env, "602 emulation trap exception "
2495 "is not implemented yet !\n");
2496 goto store_next;
2497 case POWERPC_EXCP_IFTLB: /* Instruction fetch TLB error */
0411a972 2498 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
a4f30719
JM
2499 if (lpes1 == 0) /* XXX: check this */
2500 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2501 switch (excp_model) {
a750fc0b
JM
2502 case POWERPC_EXCP_602:
2503 case POWERPC_EXCP_603:
2504 case POWERPC_EXCP_603E:
2505 case POWERPC_EXCP_G2:
e1833e1f 2506 goto tlb_miss_tgpr;
a750fc0b 2507 case POWERPC_EXCP_7x5:
76a66253 2508 goto tlb_miss;
7dbe11ac
JM
2509 case POWERPC_EXCP_74xx:
2510 goto tlb_miss_74xx;
2be0071f 2511 default:
e1833e1f 2512 cpu_abort(env, "Invalid instruction TLB miss exception\n");
2be0071f
FB
2513 break;
2514 }
e1833e1f
JM
2515 break;
2516 case POWERPC_EXCP_DLTLB: /* Data load TLB miss */
0411a972 2517 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
a4f30719
JM
2518 if (lpes1 == 0) /* XXX: check this */
2519 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2520 switch (excp_model) {
a750fc0b
JM
2521 case POWERPC_EXCP_602:
2522 case POWERPC_EXCP_603:
2523 case POWERPC_EXCP_603E:
2524 case POWERPC_EXCP_G2:
e1833e1f 2525 goto tlb_miss_tgpr;
a750fc0b 2526 case POWERPC_EXCP_7x5:
76a66253 2527 goto tlb_miss;
7dbe11ac
JM
2528 case POWERPC_EXCP_74xx:
2529 goto tlb_miss_74xx;
2be0071f 2530 default:
e1833e1f 2531 cpu_abort(env, "Invalid data load TLB miss exception\n");
2be0071f
FB
2532 break;
2533 }
e1833e1f
JM
2534 break;
2535 case POWERPC_EXCP_DSTLB: /* Data store TLB miss */
0411a972 2536 new_msr &= ~((target_ulong)1 << MSR_RI); /* XXX: check this */
a4f30719
JM
2537 if (lpes1 == 0) /* XXX: check this */
2538 new_msr |= (target_ulong)MSR_HVB;
e1833e1f 2539 switch (excp_model) {
a750fc0b
JM
2540 case POWERPC_EXCP_602:
2541 case POWERPC_EXCP_603:
2542 case POWERPC_EXCP_603E:
2543 case POWERPC_EXCP_G2:
e1833e1f 2544 tlb_miss_tgpr:
76a66253 2545 /* Swap temporary saved registers with GPRs */
0411a972
JM
2546 if (!(new_msr & ((target_ulong)1 << MSR_TGPR))) {
2547 new_msr |= (target_ulong)1 << MSR_TGPR;
2548 hreg_swap_gpr_tgpr(env);
2549 }
e1833e1f
JM
2550 goto tlb_miss;
2551 case POWERPC_EXCP_7x5:
2552 tlb_miss:
2be0071f
FB
2553#if defined (DEBUG_SOFTWARE_TLB)
2554 if (loglevel != 0) {
76a66253
JM
2555 const unsigned char *es;
2556 target_ulong *miss, *cmp;
2557 int en;
1e6784f9 2558 if (excp == POWERPC_EXCP_IFTLB) {
76a66253
JM
2559 es = "I";
2560 en = 'I';
2561 miss = &env->spr[SPR_IMISS];
2562 cmp = &env->spr[SPR_ICMP];
2563 } else {
1e6784f9 2564 if (excp == POWERPC_EXCP_DLTLB)
76a66253
JM
2565 es = "DL";
2566 else
2567 es = "DS";
2568 en = 'D';
2569 miss = &env->spr[SPR_DMISS];
2570 cmp = &env->spr[SPR_DCMP];
2571 }
1b9eb036 2572 fprintf(logfile, "6xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
4a057712 2573 " H1 " ADDRX " H2 " ADDRX " %08x\n",
1b9eb036 2574 es, en, *miss, en, *cmp,
76a66253 2575 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2be0071f
FB
2576 env->error_code);
2577 }
9a64fbe4 2578#endif
2be0071f
FB
2579 msr |= env->crf[0] << 28;
2580 msr |= env->error_code; /* key, D/I, S/L bits */
2581 /* Set way using a LRU mechanism */
76a66253 2582 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
c62db105 2583 break;
7dbe11ac
JM
2584 case POWERPC_EXCP_74xx:
2585 tlb_miss_74xx:
2586#if defined (DEBUG_SOFTWARE_TLB)
2587 if (loglevel != 0) {
2588 const unsigned char *es;
2589 target_ulong *miss, *cmp;
2590 int en;
2591 if (excp == POWERPC_EXCP_IFTLB) {
2592 es = "I";
2593 en = 'I';
0411a972
JM
2594 miss = &env->spr[SPR_TLBMISS];
2595 cmp = &env->spr[SPR_PTEHI];
7dbe11ac
JM
2596 } else {
2597 if (excp == POWERPC_EXCP_DLTLB)
2598 es = "DL";
2599 else
2600 es = "DS";
2601 en = 'D';
2602 miss = &env->spr[SPR_TLBMISS];
2603 cmp = &env->spr[SPR_PTEHI];
2604 }
2605 fprintf(logfile, "74xx %sTLB miss: %cM " ADDRX " %cC " ADDRX
2606 " %08x\n",
2607 es, en, *miss, en, *cmp, env->error_code);
2608 }
2609#endif
2610 msr |= env->error_code; /* key bit */
2611 break;
2be0071f 2612 default:
e1833e1f 2613 cpu_abort(env, "Invalid data store TLB miss exception\n");
2be0071f
FB
2614 break;
2615 }
e1833e1f
JM
2616 goto store_next;
2617 case POWERPC_EXCP_FPA: /* Floating-point assist exception */
2618 /* XXX: TODO */
2619 cpu_abort(env, "Floating point assist exception "
2620 "is not implemented yet !\n");
2621 goto store_next;
b4095fed
JM
2622 case POWERPC_EXCP_DABR: /* Data address breakpoint */
2623 /* XXX: TODO */
2624 cpu_abort(env, "DABR exception is not implemented yet !\n");
2625 goto store_next;
e1833e1f
JM
2626 case POWERPC_EXCP_IABR: /* Instruction address breakpoint */
2627 /* XXX: TODO */
2628 cpu_abort(env, "IABR exception is not implemented yet !\n");
2629 goto store_next;
2630 case POWERPC_EXCP_SMI: /* System management interrupt */
2631 /* XXX: TODO */
2632 cpu_abort(env, "SMI exception is not implemented yet !\n");
2633 goto store_next;
2634 case POWERPC_EXCP_THERM: /* Thermal interrupt */
2635 /* XXX: TODO */
2636 cpu_abort(env, "Thermal management exception "
2637 "is not implemented yet !\n");
2638 goto store_next;
2639 case POWERPC_EXCP_PERFM: /* Embedded performance monitor interrupt */
0411a972 2640 new_msr &= ~((target_ulong)1 << MSR_RI);
e1833e1f 2641 if (lpes1 == 0)
a4f30719 2642 new_msr |= (target_ulong)MSR_HVB;
e1833e1f
JM
2643 /* XXX: TODO */
2644 cpu_abort(env,
2645 "Performance counter exception is not implemented yet !\n");
2646 goto store_next;
2647 case POWERPC_EXCP_VPUA: /* Vector assist exception */
2648 /* XXX: TODO */
2649 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
2650 goto store_next;
2651 case POWERPC_EXCP_SOFTP: /* Soft patch exception */
2652 /* XXX: TODO */
2653 cpu_abort(env,
2654 "970 soft-patch exception is not implemented yet !\n");
2655 goto store_next;
2656 case POWERPC_EXCP_MAINT: /* Maintenance exception */
2657 /* XXX: TODO */
2658 cpu_abort(env,
2659 "970 maintenance exception is not implemented yet !\n");
2660 goto store_next;
b4095fed
JM
2661 case POWERPC_EXCP_MEXTBR: /* Maskable external breakpoint */
2662 /* XXX: TODO */
2663 cpu_abort(env, "Maskable external exception "
2664 "is not implemented yet !\n");
2665 goto store_next;
2666 case POWERPC_EXCP_NMEXTBR: /* Non maskable external breakpoint */
2667 /* XXX: TODO */
2668 cpu_abort(env, "Non maskable external exception "
2669 "is not implemented yet !\n");
2670 goto store_next;
2be0071f 2671 default:
e1833e1f
JM
2672 excp_invalid:
2673 cpu_abort(env, "Invalid PowerPC exception %d. Aborting\n", excp);
2674 break;
9a64fbe4 2675 store_current:
2be0071f 2676 /* save current instruction location */
e1833e1f 2677 env->spr[srr0] = env->nip - 4;
9a64fbe4
FB
2678 break;
2679 store_next:
2be0071f 2680 /* save next instruction location */
e1833e1f 2681 env->spr[srr0] = env->nip;
9a64fbe4
FB
2682 break;
2683 }
e1833e1f
JM
2684 /* Save MSR */
2685 env->spr[srr1] = msr;
2686 /* If any alternate SRR register are defined, duplicate saved values */
2687 if (asrr0 != -1)
2688 env->spr[asrr0] = env->spr[srr0];
2689 if (asrr1 != -1)
2690 env->spr[asrr1] = env->spr[srr1];
2be0071f 2691 /* If we disactivated any translation, flush TLBs */
0411a972 2692 if (new_msr & ((1 << MSR_IR) | (1 << MSR_DR)))
2be0071f 2693 tlb_flush(env, 1);
9a64fbe4 2694 /* reload MSR with correct bits */
0411a972
JM
2695 new_msr &= ~((target_ulong)1 << MSR_EE);
2696 new_msr &= ~((target_ulong)1 << MSR_PR);
2697 new_msr &= ~((target_ulong)1 << MSR_FP);
2698 new_msr &= ~((target_ulong)1 << MSR_FE0);
2699 new_msr &= ~((target_ulong)1 << MSR_SE);
2700 new_msr &= ~((target_ulong)1 << MSR_BE);
2701 new_msr &= ~((target_ulong)1 << MSR_FE1);
2702 new_msr &= ~((target_ulong)1 << MSR_IR);
2703 new_msr &= ~((target_ulong)1 << MSR_DR);
e1833e1f 2704#if 0 /* Fix this: not on all targets */
0411a972 2705 new_msr &= ~((target_ulong)1 << MSR_PMM);
e1833e1f 2706#endif
0411a972
JM
2707 new_msr &= ~((target_ulong)1 << MSR_LE);
2708 if (msr_ile)
2709 new_msr |= (target_ulong)1 << MSR_LE;
2710 else
2711 new_msr &= ~((target_ulong)1 << MSR_LE);
e1833e1f
JM
2712 /* Jump to handler */
2713 vector = env->excp_vectors[excp];
6f2d8978 2714 if (vector == (target_ulong)-1ULL) {
e1833e1f
JM
2715 cpu_abort(env, "Raised an exception without defined vector %d\n",
2716 excp);
2717 }
2718 vector |= env->excp_prefix;
c62db105 2719#if defined(TARGET_PPC64)
e1833e1f 2720 if (excp_model == POWERPC_EXCP_BOOKE) {
0411a972
JM
2721 if (!msr_icm) {
2722 new_msr &= ~((target_ulong)1 << MSR_CM);
e1833e1f 2723 vector = (uint32_t)vector;
0411a972
JM
2724 } else {
2725 new_msr |= (target_ulong)1 << MSR_CM;
2726 }
c62db105 2727 } else {
0411a972
JM
2728 if (!msr_isf) {
2729 new_msr &= ~((target_ulong)1 << MSR_SF);
e1833e1f 2730 vector = (uint32_t)vector;
0411a972
JM
2731 } else {
2732 new_msr |= (target_ulong)1 << MSR_SF;
2733 }
c62db105 2734 }
e1833e1f 2735#endif
0411a972
JM
2736 /* XXX: we don't use hreg_store_msr here as already have treated
2737 * any special case that could occur. Just store MSR and update hflags
2738 */
a4f30719 2739 env->msr = new_msr & env->msr_mask;
0411a972 2740 hreg_compute_hflags(env);
e1833e1f
JM
2741 env->nip = vector;
2742 /* Reset exception state */
2743 env->exception_index = POWERPC_EXCP_NONE;
2744 env->error_code = 0;
fb0eaffc 2745}
47103572 2746
e1833e1f 2747void do_interrupt (CPUState *env)
47103572 2748{
e1833e1f
JM
2749 powerpc_excp(env, env->excp_model, env->exception_index);
2750}
47103572 2751
e1833e1f
JM
2752void ppc_hw_interrupt (CPUPPCState *env)
2753{
f9fdea6b 2754 int hdice;
f9fdea6b 2755
0411a972 2756#if 0
a496775f
JM
2757 if (loglevel & CPU_LOG_INT) {
2758 fprintf(logfile, "%s: %p pending %08x req %08x me %d ee %d\n",
2759 __func__, env, env->pending_interrupts,
0411a972 2760 env->interrupt_request, (int)msr_me, (int)msr_ee);
a496775f 2761 }
47103572 2762#endif
e1833e1f 2763 /* External reset */
47103572 2764 if (env->pending_interrupts & (1 << PPC_INTERRUPT_RESET)) {
47103572 2765 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_RESET);
e1833e1f
JM
2766 powerpc_excp(env, env->excp_model, POWERPC_EXCP_RESET);
2767 return;
2768 }
2769 /* Machine check exception */
2770 if (env->pending_interrupts & (1 << PPC_INTERRUPT_MCK)) {
2771 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_MCK);
2772 powerpc_excp(env, env->excp_model, POWERPC_EXCP_MCHECK);
2773 return;
47103572 2774 }
e1833e1f
JM
2775#if 0 /* TODO */
2776 /* External debug exception */
2777 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DEBUG)) {
2778 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DEBUG);
2779 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DEBUG);
2780 return;
2781 }
2782#endif
b172c56a
JM
2783 if (0) {
2784 /* XXX: find a suitable condition to enable the hypervisor mode */
2785 hdice = env->spr[SPR_LPCR] & 1;
2786 } else {
2787 hdice = 0;
2788 }
f9fdea6b 2789 if ((msr_ee != 0 || msr_hv == 0 || msr_pr != 0) && hdice != 0) {
47103572
JM
2790 /* Hypervisor decrementer exception */
2791 if (env->pending_interrupts & (1 << PPC_INTERRUPT_HDECR)) {
47103572 2792 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_HDECR);
e1833e1f
JM
2793 powerpc_excp(env, env->excp_model, POWERPC_EXCP_HDECR);
2794 return;
2795 }
2796 }
e1833e1f
JM
2797 if (msr_ce != 0) {
2798 /* External critical interrupt */
2799 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CEXT)) {
2800 /* Taking a critical external interrupt does not clear the external
2801 * critical interrupt status
2802 */
2803#if 0
2804 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CEXT);
47103572 2805#endif
e1833e1f
JM
2806 powerpc_excp(env, env->excp_model, POWERPC_EXCP_CRITICAL);
2807 return;
2808 }
2809 }
2810 if (msr_ee != 0) {
2811 /* Watchdog timer on embedded PowerPC */
2812 if (env->pending_interrupts & (1 << PPC_INTERRUPT_WDT)) {
2813 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_WDT);
2814 powerpc_excp(env, env->excp_model, POWERPC_EXCP_WDT);
2815 return;
2816 }
e1833e1f
JM
2817 if (env->pending_interrupts & (1 << PPC_INTERRUPT_CDOORBELL)) {
2818 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_CDOORBELL);
2819 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORCI);
2820 return;
2821 }
e1833e1f
JM
2822 /* Fixed interval timer on embedded PowerPC */
2823 if (env->pending_interrupts & (1 << PPC_INTERRUPT_FIT)) {
2824 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_FIT);
2825 powerpc_excp(env, env->excp_model, POWERPC_EXCP_FIT);
2826 return;
2827 }
2828 /* Programmable interval timer on embedded PowerPC */
2829 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PIT)) {
2830 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PIT);
2831 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PIT);
2832 return;
2833 }
47103572
JM
2834 /* Decrementer exception */
2835 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DECR)) {
47103572 2836 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DECR);
e1833e1f
JM
2837 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DECR);
2838 return;
2839 }
47103572 2840 /* External interrupt */
e1833e1f 2841 if (env->pending_interrupts & (1 << PPC_INTERRUPT_EXT)) {
e9df014c
JM
2842 /* Taking an external interrupt does not clear the external
2843 * interrupt status
2844 */
2845#if 0
47103572 2846 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_EXT);
e9df014c 2847#endif
e1833e1f
JM
2848 powerpc_excp(env, env->excp_model, POWERPC_EXCP_EXTERNAL);
2849 return;
2850 }
e1833e1f
JM
2851 if (env->pending_interrupts & (1 << PPC_INTERRUPT_DOORBELL)) {
2852 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_DOORBELL);
2853 powerpc_excp(env, env->excp_model, POWERPC_EXCP_DOORI);
2854 return;
47103572 2855 }
e1833e1f
JM
2856 if (env->pending_interrupts & (1 << PPC_INTERRUPT_PERFM)) {
2857 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_PERFM);
2858 powerpc_excp(env, env->excp_model, POWERPC_EXCP_PERFM);
2859 return;
2860 }
2861 /* Thermal interrupt */
2862 if (env->pending_interrupts & (1 << PPC_INTERRUPT_THERM)) {
2863 env->pending_interrupts &= ~(1 << PPC_INTERRUPT_THERM);
2864 powerpc_excp(env, env->excp_model, POWERPC_EXCP_THERM);
2865 return;
2866 }
47103572 2867 }
47103572 2868}
18fba28c 2869#endif /* !CONFIG_USER_ONLY */
a496775f 2870
4a057712
JM
2871void cpu_dump_rfi (target_ulong RA, target_ulong msr)
2872{
2873 FILE *f;
2874
2875 if (logfile) {
2876 f = logfile;
2877 } else {
2878 f = stdout;
2879 return;
2880 }
2881 fprintf(f, "Return from exception at " ADDRX " with flags " ADDRX "\n",
2882 RA, msr);
a496775f
JM
2883}
2884
0a032cbe
JM
2885void cpu_ppc_reset (void *opaque)
2886{
2887 CPUPPCState *env;
0411a972 2888 target_ulong msr;
0a032cbe
JM
2889
2890 env = opaque;
0411a972 2891 msr = (target_ulong)0;
a4f30719
JM
2892 if (0) {
2893 /* XXX: find a suitable condition to enable the hypervisor mode */
2894 msr |= (target_ulong)MSR_HVB;
2895 }
0411a972
JM
2896 msr |= (target_ulong)0 << MSR_AP; /* TO BE CHECKED */
2897 msr |= (target_ulong)0 << MSR_SA; /* TO BE CHECKED */
2898 msr |= (target_ulong)1 << MSR_EP;
0a032cbe
JM
2899#if defined (DO_SINGLE_STEP) && 0
2900 /* Single step trace mode */
0411a972
JM
2901 msr |= (target_ulong)1 << MSR_SE;
2902 msr |= (target_ulong)1 << MSR_BE;
0a032cbe
JM
2903#endif
2904#if defined(CONFIG_USER_ONLY)
0411a972 2905 msr |= (target_ulong)1 << MSR_FP; /* Allow floating point usage */
4c2ab988
AJ
2906 msr |= (target_ulong)1 << MSR_VR; /* Allow altivec usage */
2907 msr |= (target_ulong)1 << MSR_SPE; /* Allow SPE usage */
0411a972 2908 msr |= (target_ulong)1 << MSR_PR;
fe463b7d 2909 env->msr = msr & env->msr_mask;
fe33cc71 2910#else
1c27f8fb 2911 env->nip = env->hreset_vector | env->excp_prefix;
b4095fed 2912 if (env->mmu_model != POWERPC_MMU_REAL)
141c8ae2 2913 ppc_tlb_invalidate_all(env);
0a032cbe 2914#endif
0411a972 2915 hreg_compute_hflags(env);
6f2d8978 2916 env->reserve = (target_ulong)-1ULL;
5eb7995e
JM
2917 /* Be sure no exception or interrupt is pending */
2918 env->pending_interrupts = 0;
e1833e1f
JM
2919 env->exception_index = POWERPC_EXCP_NONE;
2920 env->error_code = 0;
5eb7995e
JM
2921 /* Flush all TLBs */
2922 tlb_flush(env, 1);
0a032cbe
JM
2923}
2924
aaed909a 2925CPUPPCState *cpu_ppc_init (const char *cpu_model)
0a032cbe
JM
2926{
2927 CPUPPCState *env;
aaed909a
FB
2928 const ppc_def_t *def;
2929
2930 def = cpu_ppc_find_by_name(cpu_model);
2931 if (!def)
2932 return NULL;
0a032cbe
JM
2933
2934 env = qemu_mallocz(sizeof(CPUPPCState));
2935 if (!env)
2936 return NULL;
2937 cpu_exec_init(env);
2e70f6ef 2938 ppc_translate_init();
01ba9816 2939 env->cpu_model_str = cpu_model;
aaed909a
FB
2940 cpu_ppc_register_internal(env, def);
2941 cpu_ppc_reset(env);
0a032cbe
JM
2942 return env;
2943}
2944
2945void cpu_ppc_close (CPUPPCState *env)
2946{
2947 /* Should also remove all opcode tables... */
aaed909a 2948 qemu_free(env);
0a032cbe 2949}
This page took 0.667291 seconds and 4 git commands to generate.