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