]> git.proxmox.com Git - qemu.git/blame - target-ppc/helper.c
Make it safe to use 64 bits GPR and/or 64 bits host registers.
[qemu.git] / target-ppc / helper.c
CommitLineData
79aceca5 1/*
3fc6c082 2 * PowerPC emulation helpers for qemu.
79aceca5 3 *
76a66253 4 * Copyright (c) 2003-2007 Jocelyn Mayer
79aceca5
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
fdabc366
FB
20#include <stdarg.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <string.h>
24#include <inttypes.h>
25#include <signal.h>
26#include <assert.h>
27
28#include "cpu.h"
29#include "exec-all.h"
9a64fbe4
FB
30
31//#define DEBUG_MMU
32//#define DEBUG_BATS
76a66253 33//#define DEBUG_SOFTWARE_TLB
9a64fbe4 34//#define DEBUG_EXCEPTIONS
fdabc366 35//#define FLUSH_ALL_TLBS
9a64fbe4 36
9a64fbe4 37/*****************************************************************************/
3fc6c082 38/* PowerPC MMU emulation */
a541f297 39
d9bce9d9 40#if defined(CONFIG_USER_ONLY)
24741ef3
FB
41int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
42 int is_user, int is_softmmu)
43{
44 int exception, error_code;
d9bce9d9 45
24741ef3
FB
46 if (rw == 2) {
47 exception = EXCP_ISI;
48 error_code = 0;
49 } else {
50 exception = EXCP_DSI;
51 error_code = 0;
52 if (rw)
53 error_code |= 0x02000000;
54 env->spr[SPR_DAR] = address;
55 env->spr[SPR_DSISR] = error_code;
56 }
57 env->exception_index = exception;
58 env->error_code = error_code;
76a66253 59
24741ef3
FB
60 return 1;
61}
76a66253
JM
62
63target_ulong cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
24741ef3
FB
64{
65 return addr;
66}
67#else
76a66253
JM
68/* Common routines used by software and hardware TLBs emulation */
69static inline int pte_is_valid (target_ulong pte0)
70{
71 return pte0 & 0x80000000 ? 1 : 0;
72}
73
74static inline void pte_invalidate (target_ulong *pte0)
75{
76 *pte0 &= ~0x80000000;
77}
78
79#define PTE_PTEM_MASK 0x7FFFFFBF
80#define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
81
82static int pte_check (mmu_ctx_t *ctx,
83 target_ulong pte0, target_ulong pte1, int h, int rw)
84{
85 int access, ret;
86
87 access = 0;
88 ret = -1;
89 /* Check validity and table match */
90 if (pte_is_valid(pte0) && (h == ((pte0 >> 6) & 1))) {
91 /* Check vsid & api */
92 if ((pte0 & PTE_PTEM_MASK) == ctx->ptem) {
93 if (ctx->raddr != (target_ulong)-1) {
94 /* all matches should have equal RPN, WIMG & PP */
95 if ((ctx->raddr & PTE_CHECK_MASK) != (pte1 & PTE_CHECK_MASK)) {
96 if (loglevel > 0)
97 fprintf(logfile, "Bad RPN/WIMG/PP\n");
98 return -3;
99 }
100 }
101 /* Compute access rights */
102 if (ctx->key == 0) {
103 access = PAGE_READ;
104 if ((pte1 & 0x00000003) != 0x3)
105 access |= PAGE_WRITE;
106 } else {
107 switch (pte1 & 0x00000003) {
108 case 0x0:
109 access = 0;
110 break;
111 case 0x1:
112 case 0x3:
113 access = PAGE_READ;
114 break;
115 case 0x2:
116 access = PAGE_READ | PAGE_WRITE;
117 break;
118 }
119 }
120 /* Keep the matching PTE informations */
121 ctx->raddr = pte1;
122 ctx->prot = access;
123 if ((rw == 0 && (access & PAGE_READ)) ||
124 (rw == 1 && (access & PAGE_WRITE))) {
125 /* Access granted */
126#if defined (DEBUG_MMU)
127 if (loglevel > 0)
128 fprintf(logfile, "PTE access granted !\n");
129#endif
130 ret = 0;
131 } else {
132 /* Access right violation */
133#if defined (DEBUG_MMU)
134 if (loglevel > 0)
135 fprintf(logfile, "PTE access rejected\n");
136#endif
137 ret = -2;
138 }
139 }
140 }
141
142 return ret;
143}
144
145static int pte_update_flags (mmu_ctx_t *ctx, target_ulong *pte1p,
146 int ret, int rw)
147{
148 int store = 0;
149
150 /* Update page flags */
151 if (!(*pte1p & 0x00000100)) {
152 /* Update accessed flag */
153 *pte1p |= 0x00000100;
154 store = 1;
155 }
156 if (!(*pte1p & 0x00000080)) {
157 if (rw == 1 && ret == 0) {
158 /* Update changed flag */
159 *pte1p |= 0x00000080;
160 store = 1;
161 } else {
162 /* Force page fault for first write access */
163 ctx->prot &= ~PAGE_WRITE;
164 }
165 }
166
167 return store;
168}
169
170/* Software driven TLB helpers */
171static int ppc6xx_tlb_getnum (CPUState *env, target_ulong eaddr,
172 int way, int is_code)
173{
174 int nr;
175
176 /* Select TLB num in a way from address */
177 nr = (eaddr >> TARGET_PAGE_BITS) & (env->tlb_per_way - 1);
178 /* Select TLB way */
179 nr += env->tlb_per_way * way;
180 /* 6xx have separate TLBs for instructions and data */
181 if (is_code && env->id_tlbs == 1)
182 nr += env->nb_tlb;
183
184 return nr;
185}
186
187void ppc6xx_tlb_invalidate_all (CPUState *env)
188{
189 ppc_tlb_t *tlb;
190 int nr, max;
191
192#if defined (DEBUG_SOFTWARE_TLB) && 0
193 if (loglevel != 0) {
194 fprintf(logfile, "Invalidate all TLBs\n");
195 }
196#endif
197 /* Invalidate all defined software TLB */
198 max = env->nb_tlb;
199 if (env->id_tlbs == 1)
200 max *= 2;
201 for (nr = 0; nr < max; nr++) {
202 tlb = &env->tlb[nr];
203#if !defined(FLUSH_ALL_TLBS)
204 tlb_flush_page(env, tlb->EPN);
205#endif
206 pte_invalidate(&tlb->pte0);
207 }
208#if defined(FLUSH_ALL_TLBS)
209 tlb_flush(env, 1);
210#endif
211}
212
213static inline void __ppc6xx_tlb_invalidate_virt (CPUState *env,
214 target_ulong eaddr,
215 int is_code, int match_epn)
216{
217 ppc_tlb_t *tlb;
218 int way, nr;
219
220#if !defined(FLUSH_ALL_TLBS)
221 /* Invalidate ITLB + DTLB, all ways */
222 for (way = 0; way < env->nb_ways; way++) {
223 nr = ppc6xx_tlb_getnum(env, eaddr, way, is_code);
224 tlb = &env->tlb[nr];
225 if (pte_is_valid(tlb->pte0) && (match_epn == 0 || eaddr == tlb->EPN)) {
226#if defined (DEBUG_SOFTWARE_TLB)
227 if (loglevel != 0) {
228 fprintf(logfile, "TLB invalidate %d/%d %08x\n",
229 nr, env->nb_tlb, eaddr);
230 }
231#endif
232 pte_invalidate(&tlb->pte0);
233 tlb_flush_page(env, tlb->EPN);
234 }
235 }
236#else
237 /* XXX: PowerPC specification say this is valid as well */
238 ppc6xx_tlb_invalidate_all(env);
239#endif
240}
241
242void ppc6xx_tlb_invalidate_virt (CPUState *env, target_ulong eaddr,
243 int is_code)
244{
245 __ppc6xx_tlb_invalidate_virt(env, eaddr, is_code, 0);
246}
247
248void ppc6xx_tlb_store (CPUState *env, target_ulong EPN, int way, int is_code,
249 target_ulong pte0, target_ulong pte1)
250{
251 ppc_tlb_t *tlb;
252 int nr;
253
254 nr = ppc6xx_tlb_getnum(env, EPN, way, is_code);
255 tlb = &env->tlb[nr];
256#if defined (DEBUG_SOFTWARE_TLB)
257 if (loglevel != 0) {
258 fprintf(logfile, "Set TLB %d/%d EPN %08lx PTE0 %08lx PTE1 %08lx\n",
259 nr, env->nb_tlb, (unsigned long)EPN,
260 (unsigned long)pte0, (unsigned long)pte1);
261 }
262#endif
263 /* Invalidate any pending reference in Qemu for this virtual address */
264 __ppc6xx_tlb_invalidate_virt(env, EPN, is_code, 1);
265 tlb->pte0 = pte0;
266 tlb->pte1 = pte1;
267 tlb->EPN = EPN;
268 tlb->PID = 0;
269 tlb->size = 1;
270 /* Store last way for LRU mechanism */
271 env->last_way = way;
272}
273
274static int ppc6xx_tlb_check (CPUState *env, mmu_ctx_t *ctx,
275 target_ulong eaddr, int rw, int access_type)
276{
277 ppc_tlb_t *tlb;
278 int nr, best, way;
279 int ret;
d9bce9d9 280
76a66253
JM
281 best = -1;
282 ret = -1; /* No TLB found */
283 for (way = 0; way < env->nb_ways; way++) {
284 nr = ppc6xx_tlb_getnum(env, eaddr, way,
285 access_type == ACCESS_CODE ? 1 : 0);
286 tlb = &env->tlb[nr];
287 /* This test "emulates" the PTE index match for hardware TLBs */
288 if ((eaddr & TARGET_PAGE_MASK) != tlb->EPN) {
289#if defined (DEBUG_SOFTWARE_TLB)
290 if (loglevel != 0) {
291 fprintf(logfile, "TLB %d/%d %s [%08x %08x] <> %08x\n",
292 nr, env->nb_tlb,
293 pte_is_valid(tlb->pte0) ? "valid" : "inval",
294 tlb->EPN, tlb->EPN + TARGET_PAGE_SIZE, eaddr);
295 }
296#endif
297 continue;
298 }
299#if defined (DEBUG_SOFTWARE_TLB)
300 if (loglevel != 0) {
301 fprintf(logfile, "TLB %d/%d %s %08x <> %08x %08x %c %c\n",
302 nr, env->nb_tlb,
303 pte_is_valid(tlb->pte0) ? "valid" : "inval",
304 tlb->EPN, eaddr, tlb->pte1,
305 rw ? 'S' : 'L', access_type == ACCESS_CODE ? 'I' : 'D');
306 }
307#endif
308 switch (pte_check(ctx, tlb->pte0, tlb->pte1, 0, rw)) {
309 case -3:
310 /* TLB inconsistency */
311 return -1;
312 case -2:
313 /* Access violation */
314 ret = -2;
315 best = nr;
316 break;
317 case -1:
318 default:
319 /* No match */
320 break;
321 case 0:
322 /* access granted */
323 /* XXX: we should go on looping to check all TLBs consistency
324 * but we can speed-up the whole thing as the
325 * result would be undefined if TLBs are not consistent.
326 */
327 ret = 0;
328 best = nr;
329 goto done;
330 }
331 }
332 if (best != -1) {
333 done:
334#if defined (DEBUG_SOFTWARE_TLB)
335 if (loglevel > 0) {
336 fprintf(logfile, "found TLB at addr 0x%08lx prot=0x%01x ret=%d\n",
337 ctx->raddr & TARGET_PAGE_MASK, ctx->prot, ret);
338 }
339#endif
340 /* Update page flags */
341 pte_update_flags(ctx, &env->tlb[best].pte1, ret, rw);
342 }
343
344 return ret;
345}
346
9a64fbe4 347/* Perform BAT hit & translation */
76a66253
JM
348static int get_bat (CPUState *env, mmu_ctx_t *ctx,
349 target_ulong virtual, int rw, int type)
9a64fbe4 350{
76a66253
JM
351 target_ulong *BATlt, *BATut, *BATu, *BATl;
352 target_ulong base, BEPIl, BEPIu, bl;
9a64fbe4
FB
353 int i;
354 int ret = -1;
355
356#if defined (DEBUG_BATS)
357 if (loglevel > 0) {
358 fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
76a66253 359 type == ACCESS_CODE ? 'I' : 'D', virtual);
9a64fbe4 360 }
9a64fbe4
FB
361#endif
362 switch (type) {
363 case ACCESS_CODE:
364 BATlt = env->IBAT[1];
365 BATut = env->IBAT[0];
366 break;
367 default:
368 BATlt = env->DBAT[1];
369 BATut = env->DBAT[0];
370 break;
371 }
372#if defined (DEBUG_BATS)
373 if (loglevel > 0) {
374 fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
76a66253 375 type == ACCESS_CODE ? 'I' : 'D', virtual);
9a64fbe4 376 }
9a64fbe4
FB
377#endif
378 base = virtual & 0xFFFC0000;
379 for (i = 0; i < 4; i++) {
380 BATu = &BATut[i];
381 BATl = &BATlt[i];
382 BEPIu = *BATu & 0xF0000000;
383 BEPIl = *BATu & 0x0FFE0000;
384 bl = (*BATu & 0x00001FFC) << 15;
385#if defined (DEBUG_BATS)
386 if (loglevel > 0) {
387 fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
388 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
389 *BATu, *BATl);
9a64fbe4
FB
390 }
391#endif
392 if ((virtual & 0xF0000000) == BEPIu &&
393 ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
394 /* BAT matches */
395 if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
396 (msr_pr == 1 && (*BATu & 0x00000001))) {
397 /* Get physical address */
76a66253 398 ctx->raddr = (*BATl & 0xF0000000) |
9a64fbe4 399 ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
a541f297 400 (virtual & 0x0001F000);
9a64fbe4 401 if (*BATl & 0x00000001)
76a66253 402 ctx->prot = PAGE_READ;
9a64fbe4 403 if (*BATl & 0x00000002)
76a66253 404 ctx->prot = PAGE_WRITE | PAGE_READ;
9a64fbe4
FB
405#if defined (DEBUG_BATS)
406 if (loglevel > 0) {
407 fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
76a66253
JM
408 i, ctx->raddr, ctx->prot & PAGE_READ ? 'R' : '-',
409 ctx->prot & PAGE_WRITE ? 'W' : '-');
9a64fbe4
FB
410 }
411#endif
412 ret = 0;
413 break;
414 }
415 }
416 }
417 if (ret < 0) {
418#if defined (DEBUG_BATS)
419 printf("no BAT match for 0x%08x:\n", virtual);
420 for (i = 0; i < 4; i++) {
421 BATu = &BATut[i];
422 BATl = &BATlt[i];
423 BEPIu = *BATu & 0xF0000000;
424 BEPIl = *BATu & 0x0FFE0000;
425 bl = (*BATu & 0x00001FFC) << 15;
426 printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
427 "0x%08x 0x%08x 0x%08x\n",
428 __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
429 *BATu, *BATl, BEPIu, BEPIl, bl);
430 }
431#endif
9a64fbe4
FB
432 }
433 /* No hit */
434 return ret;
435}
436
437/* PTE table lookup */
76a66253 438static int find_pte (mmu_ctx_t *ctx, int h, int rw)
9a64fbe4 439{
76a66253
JM
440 target_ulong base, pte0, pte1;
441 int i, good = -1;
442 int ret;
9a64fbe4 443
76a66253
JM
444 ret = -1; /* No entry found */
445 base = ctx->pg_addr[h];
9a64fbe4 446 for (i = 0; i < 8; i++) {
8df1cd07
FB
447 pte0 = ldl_phys(base + (i * 8));
448 pte1 = ldl_phys(base + (i * 8) + 4);
9a64fbe4 449#if defined (DEBUG_MMU)
d094807b 450 if (loglevel > 0) {
76a66253
JM
451 fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
452 "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
453 pte0 >> 31, h, (pte0 >> 6) & 1, ctx->ptem);
454 }
9a64fbe4 455#endif
76a66253
JM
456 switch (pte_check(ctx, pte0, pte1, h, rw)) {
457 case -3:
458 /* PTE inconsistency */
459 return -1;
460 case -2:
461 /* Access violation */
462 ret = -2;
463 good = i;
464 break;
465 case -1:
466 default:
467 /* No PTE match */
468 break;
469 case 0:
470 /* access granted */
471 /* XXX: we should go on looping to check all PTEs consistency
472 * but if we can speed-up the whole thing as the
473 * result would be undefined if PTEs are not consistent.
474 */
475 ret = 0;
476 good = i;
477 goto done;
9a64fbe4
FB
478 }
479 }
480 if (good != -1) {
76a66253 481 done:
9a64fbe4 482#if defined (DEBUG_MMU)
d094807b 483 if (loglevel > 0) {
76a66253
JM
484 fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
485 ctx->raddr, ctx->prot, ret);
486 }
9a64fbe4
FB
487#endif
488 /* Update page flags */
76a66253
JM
489 pte1 = ctx->raddr;
490 if (pte_update_flags(ctx, &pte1, ret, rw) == 1)
491 stl_phys_notdirty(base + (good * 8) + 4, pte1);
9a64fbe4
FB
492 }
493
494 return ret;
79aceca5
FB
495}
496
76a66253
JM
497static inline target_phys_addr_t get_pgaddr (target_phys_addr_t sdr1,
498 target_phys_addr_t hash,
499 target_phys_addr_t mask)
79aceca5 500{
9a64fbe4 501 return (sdr1 & 0xFFFF0000) | (hash & mask);
79aceca5
FB
502}
503
9a64fbe4 504/* Perform segment based translation */
76a66253
JM
505static int get_segment (CPUState *env, mmu_ctx_t *ctx,
506 target_ulong eaddr, int rw, int type)
79aceca5 507{
76a66253
JM
508 target_phys_addr_t sdr, hash, mask;
509 target_ulong sr, vsid, pgidx;
9a64fbe4 510 int ret = -1, ret2;
79aceca5 511
76a66253 512 sr = env->sr[eaddr >> 28];
9a64fbe4 513#if defined (DEBUG_MMU)
a541f297 514 if (loglevel > 0) {
76a66253
JM
515 fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
516 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
517 eaddr, eaddr >> 28, sr, env->nip,
518 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
a541f297 519 }
9a64fbe4 520#endif
76a66253
JM
521 ctx->key = (((sr & 0x20000000) && msr_pr == 1) ||
522 ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
9a64fbe4
FB
523 if ((sr & 0x80000000) == 0) {
524#if defined (DEBUG_MMU)
76a66253
JM
525 if (loglevel > 0)
526 fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
527 ctx->key, sr & 0x10000000);
9a64fbe4
FB
528#endif
529 /* Check if instruction fetch is allowed, if needed */
530 if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
531 /* Page address translation */
76a66253 532 pgidx = (eaddr >> TARGET_PAGE_BITS) & 0xFFFF;
9a64fbe4 533 vsid = sr & 0x00FFFFFF;
a541f297 534 hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
76a66253
JM
535 /* Primary table address */
536 sdr = env->sdr1;
9a64fbe4 537 mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
76a66253
JM
538 ctx->pg_addr[0] = get_pgaddr(sdr, hash, mask);
539 /* Secondary table address */
540 hash = (~hash) & 0x01FFFFC0;
541 ctx->pg_addr[1] = get_pgaddr(sdr, hash, mask);
542 ctx->ptem = (vsid << 7) | (pgidx >> 10);
543 /* Initialize real address with an invalid value */
544 ctx->raddr = (target_ulong)-1;
545 if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
546 /* Software TLB search */
547 ret = ppc6xx_tlb_check(env, ctx, eaddr, rw, type);
548 } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
549 /* XXX: TODO */
550 } else {
9a64fbe4 551#if defined (DEBUG_MMU)
76a66253
JM
552 if (loglevel > 0) {
553 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
554 "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx,
555 hash, ctx->pg_addr[0]);
556 }
9a64fbe4 557#endif
76a66253
JM
558 /* Primary table lookup */
559 ret = find_pte(ctx, 0, rw);
560 if (ret < 0) {
561 /* Secondary table lookup */
9a64fbe4 562#if defined (DEBUG_MMU)
76a66253
JM
563 if (eaddr != 0xEFFFFFFF && loglevel > 0) {
564 fprintf(logfile,
565 "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
566 "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid,
567 pgidx, hash, ctx->pg_addr[1]);
568 }
9a64fbe4 569#endif
76a66253
JM
570 ret2 = find_pte(ctx, 1, rw);
571 if (ret2 != -1)
572 ret = ret2;
573 }
9a64fbe4 574 }
9a64fbe4
FB
575 } else {
576#if defined (DEBUG_MMU)
76a66253
JM
577 if (loglevel > 0)
578 fprintf(logfile, "No access allowed\n");
9a64fbe4 579#endif
76a66253 580 ret = -3;
9a64fbe4
FB
581 }
582 } else {
583#if defined (DEBUG_MMU)
a541f297 584 if (loglevel > 0)
76a66253 585 fprintf(logfile, "direct store...\n");
9a64fbe4
FB
586#endif
587 /* Direct-store segment : absolutely *BUGGY* for now */
588 switch (type) {
589 case ACCESS_INT:
590 /* Integer load/store : only access allowed */
591 break;
592 case ACCESS_CODE:
593 /* No code fetch is allowed in direct-store areas */
594 return -4;
595 case ACCESS_FLOAT:
596 /* Floating point load/store */
597 return -4;
598 case ACCESS_RES:
599 /* lwarx, ldarx or srwcx. */
600 return -4;
601 case ACCESS_CACHE:
602 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
603 /* Should make the instruction do no-op.
604 * As it already do no-op, it's quite easy :-)
605 */
76a66253 606 ctx->raddr = eaddr;
9a64fbe4
FB
607 return 0;
608 case ACCESS_EXT:
609 /* eciwx or ecowx */
610 return -4;
611 default:
612 if (logfile) {
613 fprintf(logfile, "ERROR: instruction should not need "
614 "address translation\n");
615 }
616 printf("ERROR: instruction should not need "
617 "address translation\n");
618 return -4;
619 }
76a66253
JM
620 if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
621 ctx->raddr = eaddr;
9a64fbe4
FB
622 ret = 2;
623 } else {
624 ret = -2;
625 }
79aceca5 626 }
9a64fbe4
FB
627
628 return ret;
79aceca5
FB
629}
630
76a66253
JM
631static int check_physical (CPUState *env, mmu_ctx_t *ctx,
632 target_ulong eaddr, int rw)
633{
634 int in_plb, ret;
635
636 ctx->raddr = eaddr;
637 ctx->prot = PAGE_READ;
638 ret = 0;
639 if (unlikely(msr_pe != 0 && PPC_MMU(env) == PPC_FLAGS_MMU_403)) {
640 /* 403 family add some particular protections,
641 * using PBL/PBU registers for accesses with no translation.
642 */
643 in_plb =
644 /* Check PLB validity */
645 (env->pb[0] < env->pb[1] &&
646 /* and address in plb area */
647 eaddr >= env->pb[0] && eaddr < env->pb[1]) ||
648 (env->pb[2] < env->pb[3] &&
649 eaddr >= env->pb[2] && eaddr < env->pb[3]) ? 1 : 0;
650 if (in_plb ^ msr_px) {
651 /* Access in protected area */
652 if (rw == 1) {
653 /* Access is not allowed */
654 ret = -2;
655 }
656 } else {
657 /* Read-write access is allowed */
658 ctx->prot |= PAGE_WRITE;
659 }
660 } else {
661 ctx->prot |= PAGE_WRITE;
662 }
663
664 return ret;
665}
666
667int get_physical_address (CPUState *env, mmu_ctx_t *ctx, target_ulong eaddr,
668 int rw, int access_type, int check_BATs)
9a64fbe4
FB
669{
670 int ret;
514fb8c1 671#if 0
9a64fbe4
FB
672 if (loglevel > 0) {
673 fprintf(logfile, "%s\n", __func__);
674 }
d9bce9d9 675#endif
4b3686fa
FB
676 if ((access_type == ACCESS_CODE && msr_ir == 0) ||
677 (access_type != ACCESS_CODE && msr_dr == 0)) {
9a64fbe4 678 /* No address translation */
76a66253 679 ret = check_physical(env, ctx, eaddr, rw);
9a64fbe4
FB
680 } else {
681 /* Try to find a BAT */
76a66253
JM
682 ret = -1;
683 if (check_BATs)
684 ret = get_bat(env, ctx, eaddr, rw, access_type);
9a64fbe4
FB
685 if (ret < 0) {
686 /* We didn't match any BAT entry */
76a66253 687 ret = get_segment(env, ctx, eaddr, rw, access_type);
9a64fbe4
FB
688 }
689 }
514fb8c1 690#if 0
a541f297 691 if (loglevel > 0) {
76a66253
JM
692 fprintf(logfile, "%s address %08x => %08lx\n",
693 __func__, eaddr, ctx->raddr);
a541f297 694 }
76a66253 695#endif
d9bce9d9 696
9a64fbe4
FB
697 return ret;
698}
699
76a66253 700target_ulong cpu_get_phys_page_debug (CPUState *env, target_ulong addr)
a6b025d3 701{
76a66253 702 mmu_ctx_t ctx;
a6b025d3 703
76a66253 704 if (unlikely(get_physical_address(env, &ctx, addr, 0, ACCESS_INT, 1) != 0))
a6b025d3 705 return -1;
76a66253
JM
706
707 return ctx.raddr & TARGET_PAGE_MASK;
a6b025d3 708}
9a64fbe4 709
9a64fbe4
FB
710/* Perform address translation */
711int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
a541f297 712 int is_user, int is_softmmu)
9a64fbe4 713{
76a66253 714 mmu_ctx_t ctx;
9a64fbe4 715 int exception = 0, error_code = 0;
a541f297 716 int access_type;
9a64fbe4 717 int ret = 0;
d9bce9d9 718
b769d8fe
FB
719 if (rw == 2) {
720 /* code access */
721 rw = 0;
722 access_type = ACCESS_CODE;
723 } else {
724 /* data access */
725 /* XXX: put correct access by using cpu_restore_state()
726 correctly */
727 access_type = ACCESS_INT;
728 // access_type = env->access_type;
729 }
76a66253 730 ret = get_physical_address(env, &ctx, address, rw, access_type, 1);
9a64fbe4 731 if (ret == 0) {
76a66253
JM
732 ret = tlb_set_page(env, address & TARGET_PAGE_MASK,
733 ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
734 is_user, is_softmmu);
9a64fbe4 735 } else if (ret < 0) {
9a64fbe4 736#if defined (DEBUG_MMU)
76a66253
JM
737 if (loglevel > 0)
738 cpu_dump_state(env, logfile, fprintf, 0);
9a64fbe4
FB
739#endif
740 if (access_type == ACCESS_CODE) {
741 exception = EXCP_ISI;
742 switch (ret) {
743 case -1:
76a66253
JM
744 /* No matches in page tables or TLB */
745 if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
746 exception = EXCP_I_TLBMISS;
747 env->spr[SPR_IMISS] = address;
748 env->spr[SPR_ICMP] = 0x80000000 | ctx.ptem;
749 error_code = 1 << 18;
750 goto tlb_miss;
751 } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
752 /* XXX: TODO */
753 } else {
754 error_code = 0x40000000;
755 }
9a64fbe4
FB
756 break;
757 case -2:
758 /* Access rights violation */
2be0071f 759 error_code = 0x08000000;
9a64fbe4
FB
760 break;
761 case -3:
76a66253 762 /* No execute protection violation */
2be0071f 763 error_code = 0x10000000;
9a64fbe4
FB
764 break;
765 case -4:
766 /* Direct store exception */
767 /* No code fetch is allowed in direct-store areas */
2be0071f
FB
768 error_code = 0x10000000;
769 break;
770 case -5:
771 /* No match in segment table */
772 exception = EXCP_ISEG;
773 error_code = 0;
9a64fbe4
FB
774 break;
775 }
776 } else {
777 exception = EXCP_DSI;
778 switch (ret) {
779 case -1:
76a66253
JM
780 /* No matches in page tables or TLB */
781 if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_6xx)) {
782 if (rw == 1) {
783 exception = EXCP_DS_TLBMISS;
784 error_code = 1 << 16;
785 } else {
786 exception = EXCP_DL_TLBMISS;
787 error_code = 0;
788 }
789 env->spr[SPR_DMISS] = address;
790 env->spr[SPR_DCMP] = 0x80000000 | ctx.ptem;
791 tlb_miss:
792 error_code |= ctx.key << 19;
793 env->spr[SPR_HASH1] = ctx.pg_addr[0];
794 env->spr[SPR_HASH2] = ctx.pg_addr[1];
795 /* Do not alter DAR nor DSISR */
796 goto out;
797 } else if (unlikely(PPC_MMU(env) == PPC_FLAGS_MMU_SOFT_4xx)) {
798 /* XXX: TODO */
799 } else {
800 error_code = 0x40000000;
801 }
9a64fbe4
FB
802 break;
803 case -2:
804 /* Access rights violation */
2be0071f 805 error_code = 0x08000000;
9a64fbe4
FB
806 break;
807 case -4:
808 /* Direct store exception */
809 switch (access_type) {
810 case ACCESS_FLOAT:
811 /* Floating point load/store */
812 exception = EXCP_ALIGN;
813 error_code = EXCP_ALIGN_FP;
814 break;
815 case ACCESS_RES:
816 /* lwarx, ldarx or srwcx. */
2be0071f 817 error_code = 0x04000000;
9a64fbe4
FB
818 break;
819 case ACCESS_EXT:
820 /* eciwx or ecowx */
2be0071f 821 error_code = 0x04100000;
9a64fbe4
FB
822 break;
823 default:
76a66253 824 printf("DSI: invalid exception (%d)\n", ret);
9a64fbe4
FB
825 exception = EXCP_PROGRAM;
826 error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
827 break;
828 }
fdabc366 829 break;
2be0071f
FB
830 case -5:
831 /* No match in segment table */
832 exception = EXCP_DSEG;
833 error_code = 0;
834 break;
9a64fbe4 835 }
fdabc366 836 if (exception == EXCP_DSI && rw == 1)
2be0071f 837 error_code |= 0x02000000;
76a66253
JM
838 /* Store fault address */
839 env->spr[SPR_DAR] = address;
2be0071f 840 env->spr[SPR_DSISR] = error_code;
9a64fbe4 841 }
76a66253 842 out:
9a64fbe4
FB
843#if 0
844 printf("%s: set exception to %d %02x\n",
845 __func__, exception, error_code);
846#endif
847 env->exception_index = exception;
848 env->error_code = error_code;
9a64fbe4
FB
849 ret = 1;
850 }
76a66253 851
9a64fbe4
FB
852 return ret;
853}
854
3fc6c082
FB
855/*****************************************************************************/
856/* BATs management */
857#if !defined(FLUSH_ALL_TLBS)
858static inline void do_invalidate_BAT (CPUPPCState *env,
859 target_ulong BATu, target_ulong mask)
860{
861 target_ulong base, end, page;
76a66253 862
3fc6c082
FB
863 base = BATu & ~0x0001FFFF;
864 end = base + mask + 0x00020000;
865#if defined (DEBUG_BATS)
76a66253
JM
866 if (loglevel != 0) {
867 fprintf(logfile, "Flush BAT from %08x to %08x (%08x)\n",
868 base, end, mask);
869 }
3fc6c082
FB
870#endif
871 for (page = base; page != end; page += TARGET_PAGE_SIZE)
872 tlb_flush_page(env, page);
873#if defined (DEBUG_BATS)
874 if (loglevel != 0)
875 fprintf(logfile, "Flush done\n");
876#endif
877}
878#endif
879
880static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
881 target_ulong value)
882{
883#if defined (DEBUG_BATS)
884 if (loglevel != 0) {
885 fprintf(logfile, "Set %cBAT%d%c to 0x%08lx (0x%08lx)\n",
886 ID, nr, ul == 0 ? 'u' : 'l', (unsigned long)value,
887 (unsigned long)env->nip);
888 }
889#endif
890}
891
892target_ulong do_load_ibatu (CPUPPCState *env, int nr)
893{
894 return env->IBAT[0][nr];
895}
896
897target_ulong do_load_ibatl (CPUPPCState *env, int nr)
898{
899 return env->IBAT[1][nr];
900}
901
902void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
903{
904 target_ulong mask;
905
906 dump_store_bat(env, 'I', 0, nr, value);
907 if (env->IBAT[0][nr] != value) {
908 mask = (value << 15) & 0x0FFE0000UL;
909#if !defined(FLUSH_ALL_TLBS)
910 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
911#endif
912 /* When storing valid upper BAT, mask BEPI and BRPN
913 * and invalidate all TLBs covered by this BAT
914 */
915 mask = (value << 15) & 0x0FFE0000UL;
916 env->IBAT[0][nr] = (value & 0x00001FFFUL) |
917 (value & ~0x0001FFFFUL & ~mask);
918 env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
919 (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
920#if !defined(FLUSH_ALL_TLBS)
921 do_invalidate_BAT(env, env->IBAT[0][nr], mask);
76a66253 922#else
3fc6c082
FB
923 tlb_flush(env, 1);
924#endif
925 }
926}
927
928void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
929{
930 dump_store_bat(env, 'I', 1, nr, value);
931 env->IBAT[1][nr] = value;
932}
933
934target_ulong do_load_dbatu (CPUPPCState *env, int nr)
935{
936 return env->DBAT[0][nr];
937}
938
939target_ulong do_load_dbatl (CPUPPCState *env, int nr)
940{
941 return env->DBAT[1][nr];
942}
943
944void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
945{
946 target_ulong mask;
947
948 dump_store_bat(env, 'D', 0, nr, value);
949 if (env->DBAT[0][nr] != value) {
950 /* When storing valid upper BAT, mask BEPI and BRPN
951 * and invalidate all TLBs covered by this BAT
952 */
953 mask = (value << 15) & 0x0FFE0000UL;
954#if !defined(FLUSH_ALL_TLBS)
955 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
956#endif
957 mask = (value << 15) & 0x0FFE0000UL;
958 env->DBAT[0][nr] = (value & 0x00001FFFUL) |
959 (value & ~0x0001FFFFUL & ~mask);
960 env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
961 (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
962#if !defined(FLUSH_ALL_TLBS)
963 do_invalidate_BAT(env, env->DBAT[0][nr], mask);
964#else
965 tlb_flush(env, 1);
966#endif
967 }
968}
969
970void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
971{
972 dump_store_bat(env, 'D', 1, nr, value);
973 env->DBAT[1][nr] = value;
974}
975
3fc6c082
FB
976/*****************************************************************************/
977/* Special registers manipulation */
d9bce9d9
JM
978#if defined(TARGET_PPC64)
979target_ulong ppc_load_asr (CPUPPCState *env)
980{
981 return env->asr;
982}
983
984void ppc_store_asr (CPUPPCState *env, target_ulong value)
985{
986 if (env->asr != value) {
987 env->asr = value;
988 tlb_flush(env, 1);
989 }
990}
991#endif
992
3fc6c082
FB
993target_ulong do_load_sdr1 (CPUPPCState *env)
994{
995 return env->sdr1;
996}
997
998void do_store_sdr1 (CPUPPCState *env, target_ulong value)
999{
1000#if defined (DEBUG_MMU)
1001 if (loglevel != 0) {
1002 fprintf(logfile, "%s: 0x%08lx\n", __func__, (unsigned long)value);
1003 }
1004#endif
1005 if (env->sdr1 != value) {
1006 env->sdr1 = value;
76a66253 1007 tlb_flush(env, 1);
3fc6c082
FB
1008 }
1009}
1010
1011target_ulong do_load_sr (CPUPPCState *env, int srnum)
1012{
1013 return env->sr[srnum];
1014}
1015
1016void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
1017{
1018#if defined (DEBUG_MMU)
1019 if (loglevel != 0) {
1020 fprintf(logfile, "%s: reg=%d 0x%08lx %08lx\n",
1021 __func__, srnum, (unsigned long)value, env->sr[srnum]);
1022 }
1023#endif
1024 if (env->sr[srnum] != value) {
1025 env->sr[srnum] = value;
1026#if !defined(FLUSH_ALL_TLBS) && 0
1027 {
1028 target_ulong page, end;
1029 /* Invalidate 256 MB of virtual memory */
1030 page = (16 << 20) * srnum;
1031 end = page + (16 << 20);
1032 for (; page != end; page += TARGET_PAGE_SIZE)
1033 tlb_flush_page(env, page);
1034 }
1035#else
76a66253 1036 tlb_flush(env, 1);
3fc6c082
FB
1037#endif
1038 }
1039}
76a66253 1040#endif /* !defined (CONFIG_USER_ONLY) */
3fc6c082 1041
76a66253 1042uint32_t ppc_load_xer (CPUPPCState *env)
79aceca5
FB
1043{
1044 return (xer_so << XER_SO) |
1045 (xer_ov << XER_OV) |
1046 (xer_ca << XER_CA) |
3fc6c082
FB
1047 (xer_bc << XER_BC) |
1048 (xer_cmp << XER_CMP);
79aceca5
FB
1049}
1050
76a66253 1051void ppc_store_xer (CPUPPCState *env, uint32_t value)
79aceca5
FB
1052{
1053 xer_so = (value >> XER_SO) & 0x01;
1054 xer_ov = (value >> XER_OV) & 0x01;
1055 xer_ca = (value >> XER_CA) & 0x01;
3fc6c082 1056 xer_cmp = (value >> XER_CMP) & 0xFF;
d9bce9d9 1057 xer_bc = (value >> XER_BC) & 0x7F;
79aceca5
FB
1058}
1059
76a66253
JM
1060/* Swap temporary saved registers with GPRs */
1061static inline void swap_gpr_tgpr (CPUPPCState *env)
79aceca5 1062{
76a66253
JM
1063 ppc_gpr_t tmp;
1064
1065 tmp = env->gpr[0];
1066 env->gpr[0] = env->tgpr[0];
1067 env->tgpr[0] = tmp;
1068 tmp = env->gpr[1];
1069 env->gpr[1] = env->tgpr[1];
1070 env->tgpr[1] = tmp;
1071 tmp = env->gpr[2];
1072 env->gpr[2] = env->tgpr[2];
1073 env->tgpr[2] = tmp;
1074 tmp = env->gpr[3];
1075 env->gpr[3] = env->tgpr[3];
1076 env->tgpr[3] = tmp;
79aceca5
FB
1077}
1078
76a66253
JM
1079/* GDBstub can read and write MSR... */
1080target_ulong do_load_msr (CPUPPCState *env)
79aceca5 1081{
76a66253
JM
1082 return
1083#if defined (TARGET_PPC64)
d9bce9d9
JM
1084 ((target_ulong)msr_sf << MSR_SF) |
1085 ((target_ulong)msr_isf << MSR_ISF) |
1086 ((target_ulong)msr_hv << MSR_HV) |
76a66253 1087#endif
d9bce9d9
JM
1088 ((target_ulong)msr_ucle << MSR_UCLE) |
1089 ((target_ulong)msr_vr << MSR_VR) | /* VR / SPE */
1090 ((target_ulong)msr_ap << MSR_AP) |
1091 ((target_ulong)msr_sa << MSR_SA) |
1092 ((target_ulong)msr_key << MSR_KEY) |
1093 ((target_ulong)msr_pow << MSR_POW) | /* POW / WE */
1094 ((target_ulong)msr_tlb << MSR_TLB) | /* TLB / TGPE / CE */
1095 ((target_ulong)msr_ile << MSR_ILE) |
1096 ((target_ulong)msr_ee << MSR_EE) |
1097 ((target_ulong)msr_pr << MSR_PR) |
1098 ((target_ulong)msr_fp << MSR_FP) |
1099 ((target_ulong)msr_me << MSR_ME) |
1100 ((target_ulong)msr_fe0 << MSR_FE0) |
1101 ((target_ulong)msr_se << MSR_SE) | /* SE / DWE / UBLE */
1102 ((target_ulong)msr_be << MSR_BE) | /* BE / DE */
1103 ((target_ulong)msr_fe1 << MSR_FE1) |
1104 ((target_ulong)msr_al << MSR_AL) |
1105 ((target_ulong)msr_ip << MSR_IP) |
1106 ((target_ulong)msr_ir << MSR_IR) | /* IR / IS */
1107 ((target_ulong)msr_dr << MSR_DR) | /* DR / DS */
1108 ((target_ulong)msr_pe << MSR_PE) | /* PE / EP */
1109 ((target_ulong)msr_px << MSR_PX) | /* PX / PMM */
1110 ((target_ulong)msr_ri << MSR_RI) |
1111 ((target_ulong)msr_le << MSR_LE);
3fc6c082
FB
1112}
1113
1114void do_store_msr (CPUPPCState *env, target_ulong value)
313adae9 1115{
50443c98
FB
1116 int enter_pm;
1117
3fc6c082
FB
1118 value &= env->msr_mask;
1119 if (((value >> MSR_IR) & 1) != msr_ir ||
1120 ((value >> MSR_DR) & 1) != msr_dr) {
76a66253 1121 /* Flush all tlb when changing translation mode */
d094807b 1122 tlb_flush(env, 1);
3fc6c082 1123 env->interrupt_request |= CPU_INTERRUPT_EXITTB;
a541f297 1124 }
3fc6c082
FB
1125#if 0
1126 if (loglevel != 0) {
1127 fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
1128 }
1129#endif
76a66253
JM
1130 switch (PPC_EXCP(env)) {
1131 case PPC_FLAGS_EXCP_602:
1132 case PPC_FLAGS_EXCP_603:
1133 if (((value >> MSR_TGPR) & 1) != msr_tgpr) {
1134 /* Swap temporary saved registers with GPRs */
1135 swap_gpr_tgpr(env);
1136 }
1137 break;
1138 default:
1139 break;
1140 }
1141#if defined (TARGET_PPC64)
1142 msr_sf = (value >> MSR_SF) & 1;
1143 msr_isf = (value >> MSR_ISF) & 1;
1144 msr_hv = (value >> MSR_HV) & 1;
1145#endif
1146 msr_ucle = (value >> MSR_UCLE) & 1;
1147 msr_vr = (value >> MSR_VR) & 1; /* VR / SPE */
1148 msr_ap = (value >> MSR_AP) & 1;
1149 msr_sa = (value >> MSR_SA) & 1;
1150 msr_key = (value >> MSR_KEY) & 1;
1151 msr_pow = (value >> MSR_POW) & 1; /* POW / WE */
1152 msr_tlb = (value >> MSR_TLB) & 1; /* TLB / TGPR / CE */
1153 msr_ile = (value >> MSR_ILE) & 1;
1154 msr_ee = (value >> MSR_EE) & 1;
1155 msr_pr = (value >> MSR_PR) & 1;
1156 msr_fp = (value >> MSR_FP) & 1;
1157 msr_me = (value >> MSR_ME) & 1;
1158 msr_fe0 = (value >> MSR_FE0) & 1;
1159 msr_se = (value >> MSR_SE) & 1; /* SE / DWE / UBLE */
1160 msr_be = (value >> MSR_BE) & 1; /* BE / DE */
1161 msr_fe1 = (value >> MSR_FE1) & 1;
1162 msr_al = (value >> MSR_AL) & 1;
1163 msr_ip = (value >> MSR_IP) & 1;
1164 msr_ir = (value >> MSR_IR) & 1; /* IR / IS */
1165 msr_dr = (value >> MSR_DR) & 1; /* DR / DS */
1166 msr_pe = (value >> MSR_PE) & 1; /* PE / EP */
1167 msr_px = (value >> MSR_PX) & 1; /* PX / PMM */
1168 msr_ri = (value >> MSR_RI) & 1;
1169 msr_le = (value >> MSR_LE) & 1;
3fc6c082 1170 do_compute_hflags(env);
50443c98
FB
1171
1172 enter_pm = 0;
1173 switch (PPC_EXCP(env)) {
d9bce9d9
JM
1174 case PPC_FLAGS_EXCP_603:
1175 /* Don't handle SLEEP mode: we should disable all clocks...
1176 * No dynamic power-management.
1177 */
1178 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00C00000) != 0)
1179 enter_pm = 1;
1180 break;
1181 case PPC_FLAGS_EXCP_604:
1182 if (msr_pow == 1)
1183 enter_pm = 1;
1184 break;
50443c98 1185 case PPC_FLAGS_EXCP_7x0:
76a66253 1186 if (msr_pow == 1 && (env->spr[SPR_HID0] & 0x00E00000) != 0)
50443c98
FB
1187 enter_pm = 1;
1188 break;
1189 default:
1190 break;
1191 }
1192 if (enter_pm) {
e80e1cc4 1193 /* power save: exit cpu loop */
50443c98 1194 env->halted = 1;
e80e1cc4
FB
1195 env->exception_index = EXCP_HLT;
1196 cpu_loop_exit();
1197 }
3fc6c082
FB
1198}
1199
d9bce9d9
JM
1200#if defined(TARGET_PPC64)
1201void ppc_store_msr_32 (CPUPPCState *env, target_ulong value)
1202{
1203 do_store_msr(env, (uint32_t)value);
1204}
1205#endif
1206
76a66253 1207void do_compute_hflags (CPUPPCState *env)
3fc6c082 1208{
76a66253
JM
1209 /* Compute current hflags */
1210 env->hflags = (msr_pr << MSR_PR) | (msr_le << MSR_LE) |
1211 (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_fe1 << MSR_FE1) |
d9bce9d9 1212 (msr_vr << MSR_VR) | (msr_ap << MSR_AP) | (msr_sa << MSR_SA) |
76a66253
JM
1213 (msr_se << MSR_SE) | (msr_be << MSR_BE);
1214#if defined (TARGET_PPC64)
d9bce9d9 1215 env->hflags |= (msr_sf << (MSR_SF - 32)) | (msr_hv << (MSR_HV - 32));
4b3686fa 1216#endif
3fc6c082
FB
1217}
1218
1219/*****************************************************************************/
1220/* Exception processing */
18fba28c 1221#if defined (CONFIG_USER_ONLY)
9a64fbe4 1222void do_interrupt (CPUState *env)
79aceca5 1223{
18fba28c
FB
1224 env->exception_index = -1;
1225}
76a66253 1226#else /* defined (CONFIG_USER_ONLY) */
d094807b
FB
1227static void dump_syscall(CPUState *env)
1228{
d9bce9d9
JM
1229 fprintf(logfile, "syscall r0=0x" REGX " r3=0x" REGX " r4=0x" REGX
1230 " r5=0x" REGX " r6=0x" REGX " nip=0x" REGX "\n",
d094807b
FB
1231 env->gpr[0], env->gpr[3], env->gpr[4],
1232 env->gpr[5], env->gpr[6], env->nip);
1233}
1234
18fba28c
FB
1235void do_interrupt (CPUState *env)
1236{
76a66253 1237 target_ulong msr, *srr_0, *srr_1;
18fba28c 1238 int excp;
79aceca5 1239
18fba28c 1240 excp = env->exception_index;
3fc6c082 1241 msr = do_load_msr(env);
2be0071f
FB
1242 /* The default is to use SRR0 & SRR1 to save the exception context */
1243 srr_0 = &env->spr[SPR_SRR0];
1244 srr_1 = &env->spr[SPR_SRR1];
9a64fbe4 1245#if defined (DEBUG_EXCEPTIONS)
2be0071f
FB
1246 if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) {
1247 if (loglevel != 0) {
1248 fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
1249 (unsigned long)env->nip, excp, env->error_code);
76a66253 1250 cpu_dump_state(env, logfile, fprintf, 0);
b769d8fe 1251 }
79aceca5 1252 }
9a64fbe4 1253#endif
b769d8fe 1254 if (loglevel & CPU_LOG_INT) {
2be0071f
FB
1255 fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
1256 (unsigned long)env->nip, excp, env->error_code);
b769d8fe 1257 }
2be0071f 1258 msr_pow = 0;
9a64fbe4
FB
1259 /* Generate informations in save/restore registers */
1260 switch (excp) {
76a66253 1261 /* Generic PowerPC exceptions */
2be0071f
FB
1262 case EXCP_RESET: /* 0x0100 */
1263 if (PPC_EXCP(env) != PPC_FLAGS_EXCP_40x) {
1264 if (msr_ip)
1265 excp += 0xFFC00;
1266 excp |= 0xFFC00000;
1267 } else {
1268 srr_0 = &env->spr[SPR_40x_SRR2];
1269 srr_1 = &env->spr[SPR_40x_SRR3];
1270 }
9a64fbe4 1271 goto store_next;
2be0071f 1272 case EXCP_MACHINE_CHECK: /* 0x0200 */
9a64fbe4 1273 if (msr_me == 0) {
4b3686fa 1274 cpu_abort(env, "Machine check exception while not allowed\n");
79aceca5 1275 }
76a66253 1276 if (unlikely(PPC_EXCP(env) == PPC_FLAGS_EXCP_40x)) {
2be0071f
FB
1277 srr_0 = &env->spr[SPR_40x_SRR2];
1278 srr_1 = &env->spr[SPR_40x_SRR3];
1279 }
9a64fbe4
FB
1280 msr_me = 0;
1281 break;
2be0071f 1282 case EXCP_DSI: /* 0x0300 */
9a64fbe4
FB
1283 /* Store exception cause */
1284 /* data location address has been stored
1285 * when the fault has been detected
2be0071f 1286 */
76a66253 1287 msr &= ~0xFFFF0000;
a541f297 1288#if defined (DEBUG_EXCEPTIONS)
76a66253
JM
1289 if (loglevel) {
1290 fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1291 env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1292 } else {
1293 printf("DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1294 env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1295 }
a541f297
FB
1296#endif
1297 goto store_next;
2be0071f 1298 case EXCP_ISI: /* 0x0400 */
9a64fbe4 1299 /* Store exception cause */
76a66253 1300 msr &= ~0xFFFF0000;
2be0071f 1301 msr |= env->error_code;
a541f297 1302#if defined (DEBUG_EXCEPTIONS)
76a66253
JM
1303 if (loglevel != 0) {
1304 fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
1305 msr, env->nip);
1306 }
a541f297 1307#endif
9a64fbe4 1308 goto store_next;
2be0071f 1309 case EXCP_EXTERNAL: /* 0x0500 */
9a64fbe4
FB
1310 if (msr_ee == 0) {
1311#if defined (DEBUG_EXCEPTIONS)
1312 if (loglevel > 0) {
1313 fprintf(logfile, "Skipping hardware interrupt\n");
2be0071f 1314 }
9a64fbe4 1315#endif
a541f297 1316 /* Requeue it */
2be0071f 1317 env->interrupt_request |= CPU_INTERRUPT_HARD;
9a64fbe4 1318 return;
2be0071f 1319 }
9a64fbe4 1320 goto store_next;
2be0071f 1321 case EXCP_ALIGN: /* 0x0600 */
76a66253 1322 if (likely(PPC_EXCP(env) != PPC_FLAGS_EXCP_601)) {
2be0071f
FB
1323 /* Store exception cause */
1324 /* Get rS/rD and rA from faulting opcode */
1325 env->spr[SPR_DSISR] |=
1326 (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
1327 /* data location address has been stored
1328 * when the fault has been detected
1329 */
1330 } else {
1331 /* IO error exception on PowerPC 601 */
1332 /* XXX: TODO */
1333 cpu_abort(env,
1334 "601 IO error exception is not implemented yet !\n");
1335 }
9a64fbe4 1336 goto store_current;
2be0071f 1337 case EXCP_PROGRAM: /* 0x0700 */
9a64fbe4
FB
1338 msr &= ~0xFFFF0000;
1339 switch (env->error_code & ~0xF) {
1340 case EXCP_FP:
1341 if (msr_fe0 == 0 && msr_fe1 == 0) {
1342#if defined (DEBUG_EXCEPTIONS)
1343 printf("Ignore floating point exception\n");
1344#endif
1345 return;
76a66253 1346 }
9a64fbe4
FB
1347 msr |= 0x00100000;
1348 /* Set FX */
1349 env->fpscr[7] |= 0x8;
1350 /* Finally, update FEX */
1351 if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
1352 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
1353 env->fpscr[7] |= 0x4;
76a66253 1354 break;
9a64fbe4 1355 case EXCP_INVAL:
76a66253 1356 // printf("Invalid instruction at 0x%08x\n", env->nip);
9a64fbe4 1357 msr |= 0x00080000;
76a66253 1358 break;
9a64fbe4
FB
1359 case EXCP_PRIV:
1360 msr |= 0x00040000;
76a66253 1361 break;
9a64fbe4
FB
1362 case EXCP_TRAP:
1363 msr |= 0x00020000;
1364 break;
1365 default:
1366 /* Should never occur */
76a66253
JM
1367 break;
1368 }
9a64fbe4
FB
1369 msr |= 0x00010000;
1370 goto store_current;
2be0071f 1371 case EXCP_NO_FP: /* 0x0800 */
4ecc3190 1372 msr &= ~0xFFFF0000;
9a64fbe4
FB
1373 goto store_current;
1374 case EXCP_DECR:
1375 if (msr_ee == 0) {
2be0071f 1376#if 1
9a64fbe4 1377 /* Requeue it */
2be0071f
FB
1378 env->interrupt_request |= CPU_INTERRUPT_TIMER;
1379#endif
9a64fbe4
FB
1380 return;
1381 }
1382 goto store_next;
2be0071f 1383 case EXCP_SYSCALL: /* 0x0C00 */
d094807b
FB
1384 /* NOTE: this is a temporary hack to support graphics OSI
1385 calls from the MOL driver */
1386 if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
1387 env->osi_call) {
1388 if (env->osi_call(env) != 0)
1389 return;
1390 }
b769d8fe 1391 if (loglevel & CPU_LOG_INT) {
d094807b 1392 dump_syscall(env);
b769d8fe 1393 }
9a64fbe4 1394 goto store_next;
2be0071f 1395 case EXCP_TRACE: /* 0x0D00 */
2be0071f
FB
1396 goto store_next;
1397 case EXCP_PERF: /* 0x0F00 */
1398 /* XXX: TODO */
1399 cpu_abort(env,
1400 "Performance counter exception is not implemented yet !\n");
1401 goto store_next;
1402 /* 32 bits PowerPC specific exceptions */
1403 case EXCP_FP_ASSIST: /* 0x0E00 */
1404 /* XXX: TODO */
1405 cpu_abort(env, "Floating point assist exception "
1406 "is not implemented yet !\n");
1407 goto store_next;
76a66253 1408 /* 64 bits PowerPC exceptions */
2be0071f
FB
1409 case EXCP_DSEG: /* 0x0380 */
1410 /* XXX: TODO */
1411 cpu_abort(env, "Data segment exception is not implemented yet !\n");
9a64fbe4 1412 goto store_next;
2be0071f
FB
1413 case EXCP_ISEG: /* 0x0480 */
1414 /* XXX: TODO */
1415 cpu_abort(env,
1416 "Instruction segment exception is not implemented yet !\n");
9a64fbe4 1417 goto store_next;
2be0071f
FB
1418 case EXCP_HDECR: /* 0x0980 */
1419 if (msr_ee == 0) {
1420#if 1
1421 /* Requeue it */
1422 env->interrupt_request |= CPU_INTERRUPT_TIMER;
1423#endif
76a66253 1424 return;
2be0071f 1425 }
76a66253
JM
1426 /* XXX: TODO */
1427 cpu_abort(env, "Hypervisor decrementer exception is not implemented "
1428 "yet !\n");
2be0071f
FB
1429 goto store_next;
1430 /* Implementation specific exceptions */
1431 case 0x0A00:
76a66253
JM
1432 if (likely(env->spr[SPR_PVR] == CPU_PPC_G2 ||
1433 env->spr[SPR_PVR] == CPU_PPC_G2LE)) {
2be0071f
FB
1434 /* Critical interrupt on G2 */
1435 /* XXX: TODO */
1436 cpu_abort(env, "G2 critical interrupt is not implemented yet !\n");
1437 goto store_next;
1438 } else {
1439 cpu_abort(env, "Invalid exception 0x0A00 !\n");
1440 }
9a64fbe4 1441 return;
2be0071f
FB
1442 case 0x0F20:
1443 switch (PPC_EXCP(env)) {
1444 case PPC_FLAGS_EXCP_40x:
1445 /* APU unavailable on 405 */
1446 /* XXX: TODO */
1447 cpu_abort(env,
1448 "APU unavailable exception is not implemented yet !\n");
1449 goto store_next;
1450 case PPC_FLAGS_EXCP_74xx:
1451 /* Altivec unavailable */
1452 /* XXX: TODO */
1453 cpu_abort(env, "Altivec unavailable exception "
1454 "is not implemented yet !\n");
1455 goto store_next;
1456 default:
1457 cpu_abort(env, "Invalid exception 0x0F20 !\n");
1458 break;
1459 }
1460 return;
1461 case 0x1000:
1462 switch (PPC_EXCP(env)) {
1463 case PPC_FLAGS_EXCP_40x:
1464 /* PIT on 4xx */
1465 /* XXX: TODO */
1466 cpu_abort(env, "40x PIT exception is not implemented yet !\n");
1467 goto store_next;
1468 case PPC_FLAGS_EXCP_602:
1469 case PPC_FLAGS_EXCP_603:
1470 /* ITLBMISS on 602/603 */
2be0071f 1471 goto store_gprs;
76a66253
JM
1472 case PPC_FLAGS_EXCP_7x5:
1473 /* ITLBMISS on 745/755 */
1474 goto tlb_miss;
2be0071f
FB
1475 default:
1476 cpu_abort(env, "Invalid exception 0x1000 !\n");
1477 break;
1478 }
1479 return;
1480 case 0x1010:
1481 switch (PPC_EXCP(env)) {
1482 case PPC_FLAGS_EXCP_40x:
1483 /* FIT on 4xx */
2be0071f 1484 /* XXX: TODO */
76a66253 1485 cpu_abort(env, "40x FIT exception is not implemented yet !\n");
2be0071f
FB
1486 goto store_next;
1487 default:
1488 cpu_abort(env, "Invalid exception 0x1010 !\n");
1489 break;
1490 }
1491 return;
1492 case 0x1020:
1493 switch (PPC_EXCP(env)) {
1494 case PPC_FLAGS_EXCP_40x:
1495 /* Watchdog on 4xx */
1496 /* XXX: TODO */
1497 cpu_abort(env,
1498 "40x watchdog exception is not implemented yet !\n");
1499 goto store_next;
1500 default:
1501 cpu_abort(env, "Invalid exception 0x1020 !\n");
1502 break;
1503 }
1504 return;
1505 case 0x1100:
1506 switch (PPC_EXCP(env)) {
1507 case PPC_FLAGS_EXCP_40x:
1508 /* DTLBMISS on 4xx */
1509 /* XXX: TODO */
1510 cpu_abort(env,
1511 "40x DTLBMISS exception is not implemented yet !\n");
1512 goto store_next;
1513 case PPC_FLAGS_EXCP_602:
1514 case PPC_FLAGS_EXCP_603:
1515 /* DLTLBMISS on 602/603 */
2be0071f 1516 goto store_gprs;
76a66253
JM
1517 case PPC_FLAGS_EXCP_7x5:
1518 /* DLTLBMISS on 745/755 */
1519 goto tlb_miss;
2be0071f
FB
1520 default:
1521 cpu_abort(env, "Invalid exception 0x1100 !\n");
1522 break;
1523 }
1524 return;
1525 case 0x1200:
1526 switch (PPC_EXCP(env)) {
1527 case PPC_FLAGS_EXCP_40x:
1528 /* ITLBMISS on 4xx */
1529 /* XXX: TODO */
1530 cpu_abort(env,
1531 "40x ITLBMISS exception is not implemented yet !\n");
1532 goto store_next;
1533 case PPC_FLAGS_EXCP_602:
1534 case PPC_FLAGS_EXCP_603:
1535 /* DSTLBMISS on 602/603 */
2be0071f 1536 store_gprs:
76a66253
JM
1537 /* Swap temporary saved registers with GPRs */
1538 swap_gpr_tgpr(env);
1539 msr_tgpr = 1;
2be0071f
FB
1540#if defined (DEBUG_SOFTWARE_TLB)
1541 if (loglevel != 0) {
76a66253
JM
1542 const unsigned char *es;
1543 target_ulong *miss, *cmp;
1544 int en;
1545 if (excp == 0x1000) {
1546 es = "I";
1547 en = 'I';
1548 miss = &env->spr[SPR_IMISS];
1549 cmp = &env->spr[SPR_ICMP];
1550 } else {
1551 if (excp == 0x1100)
1552 es = "DL";
1553 else
1554 es = "DS";
1555 en = 'D';
1556 miss = &env->spr[SPR_DMISS];
1557 cmp = &env->spr[SPR_DCMP];
1558 }
1559 fprintf(logfile, "6xx %sTLB miss: %cM %08x %cC %08x "
1560 "H1 %08x H2 %08x %08x\n", es, en, *miss, en, *cmp,
1561 env->spr[SPR_HASH1], env->spr[SPR_HASH2],
2be0071f
FB
1562 env->error_code);
1563 }
9a64fbe4 1564#endif
76a66253
JM
1565 goto tlb_miss;
1566 case PPC_FLAGS_EXCP_7x5:
1567 /* DSTLBMISS on 745/755 */
1568 tlb_miss:
1569 msr &= ~0xF83F0000;
2be0071f
FB
1570 msr |= env->crf[0] << 28;
1571 msr |= env->error_code; /* key, D/I, S/L bits */
1572 /* Set way using a LRU mechanism */
76a66253 1573 msr |= ((env->last_way + 1) & (env->nb_ways - 1)) << 17;
2be0071f
FB
1574 goto store_next;
1575 default:
1576 cpu_abort(env, "Invalid exception 0x1200 !\n");
1577 break;
1578 }
1579 return;
1580 case 0x1300:
1581 switch (PPC_EXCP(env)) {
1582 case PPC_FLAGS_EXCP_601:
1583 case PPC_FLAGS_EXCP_602:
1584 case PPC_FLAGS_EXCP_603:
1585 case PPC_FLAGS_EXCP_604:
1586 case PPC_FLAGS_EXCP_7x0:
1587 case PPC_FLAGS_EXCP_7x5:
1588 /* IABR on 6xx/7xx */
1589 /* XXX: TODO */
1590 cpu_abort(env, "IABR exception is not implemented yet !\n");
1591 goto store_next;
1592 default:
1593 cpu_abort(env, "Invalid exception 0x1300 !\n");
1594 break;
1595 }
1596 return;
1597 case 0x1400:
1598 switch (PPC_EXCP(env)) {
1599 case PPC_FLAGS_EXCP_601:
1600 case PPC_FLAGS_EXCP_602:
1601 case PPC_FLAGS_EXCP_603:
1602 case PPC_FLAGS_EXCP_604:
1603 case PPC_FLAGS_EXCP_7x0:
1604 case PPC_FLAGS_EXCP_7x5:
1605 /* SMI on 6xx/7xx */
1606 /* XXX: TODO */
1607 cpu_abort(env, "SMI exception is not implemented yet !\n");
1608 goto store_next;
1609 default:
1610 cpu_abort(env, "Invalid exception 0x1400 !\n");
1611 break;
1612 }
1613 return;
1614 case 0x1500:
1615 switch (PPC_EXCP(env)) {
1616 case PPC_FLAGS_EXCP_602:
1617 /* Watchdog on 602 */
76a66253 1618 /* XXX: TODO */
2be0071f
FB
1619 cpu_abort(env,
1620 "602 watchdog exception is not implemented yet !\n");
1621 goto store_next;
1622 case PPC_FLAGS_EXCP_970:
1623 /* Soft patch exception on 970 */
1624 /* XXX: TODO */
1625 cpu_abort(env,
1626 "970 soft-patch exception is not implemented yet !\n");
1627 goto store_next;
1628 case PPC_FLAGS_EXCP_74xx:
1629 /* VPU assist on 74xx */
1630 /* XXX: TODO */
1631 cpu_abort(env, "VPU assist exception is not implemented yet !\n");
1632 goto store_next;
1633 default:
1634 cpu_abort(env, "Invalid exception 0x1500 !\n");
1635 break;
1636 }
1637 return;
1638 case 0x1600:
1639 switch (PPC_EXCP(env)) {
1640 case PPC_FLAGS_EXCP_602:
1641 /* Emulation trap on 602 */
1642 /* XXX: TODO */
1643 cpu_abort(env, "602 emulation trap exception "
1644 "is not implemented yet !\n");
1645 goto store_next;
1646 case PPC_FLAGS_EXCP_970:
1647 /* Maintenance exception on 970 */
1648 /* XXX: TODO */
1649 cpu_abort(env,
1650 "970 maintenance exception is not implemented yet !\n");
1651 goto store_next;
1652 default:
1653 cpu_abort(env, "Invalid exception 0x1600 !\n");
1654 break;
1655 }
1656 return;
1657 case 0x1700:
1658 switch (PPC_EXCP(env)) {
1659 case PPC_FLAGS_EXCP_7x0:
1660 case PPC_FLAGS_EXCP_7x5:
1661 /* Thermal management interrupt on G3 */
1662 /* XXX: TODO */
1663 cpu_abort(env, "G3 thermal management exception "
1664 "is not implemented yet !\n");
1665 goto store_next;
1666 case PPC_FLAGS_EXCP_970:
1667 /* VPU assist on 970 */
1668 /* XXX: TODO */
1669 cpu_abort(env,
1670 "970 VPU assist exception is not implemented yet !\n");
1671 goto store_next;
1672 default:
1673 cpu_abort(env, "Invalid exception 0x1700 !\n");
1674 break;
1675 }
1676 return;
1677 case 0x1800:
1678 switch (PPC_EXCP(env)) {
1679 case PPC_FLAGS_EXCP_970:
1680 /* Thermal exception on 970 */
1681 /* XXX: TODO */
1682 cpu_abort(env, "970 thermal management exception "
1683 "is not implemented yet !\n");
1684 goto store_next;
1685 default:
1686 cpu_abort(env, "Invalid exception 0x1800 !\n");
1687 break;
1688 }
1689 return;
1690 case 0x2000:
1691 switch (PPC_EXCP(env)) {
1692 case PPC_FLAGS_EXCP_40x:
1693 /* DEBUG on 4xx */
1694 /* XXX: TODO */
1695 cpu_abort(env, "40x debug exception is not implemented yet !\n");
1696 goto store_next;
1697 case PPC_FLAGS_EXCP_601:
1698 /* Run mode exception on 601 */
1699 /* XXX: TODO */
1700 cpu_abort(env,
1701 "601 run mode exception is not implemented yet !\n");
1702 goto store_next;
1703 default:
1704 cpu_abort(env, "Invalid exception 0x1800 !\n");
1705 break;
1706 }
1707 return;
1708 /* Other exceptions */
1709 /* Qemu internal exceptions:
1710 * we should never come here with those values: abort execution
1711 */
1712 default:
1713 cpu_abort(env, "Invalid exception: code %d (%04x)\n", excp, excp);
9a64fbe4
FB
1714 return;
1715 store_current:
2be0071f
FB
1716 /* save current instruction location */
1717 *srr_0 = (env->nip - 4) & 0xFFFFFFFFULL;
9a64fbe4
FB
1718 break;
1719 store_next:
2be0071f
FB
1720 /* save next instruction location */
1721 *srr_0 = env->nip & 0xFFFFFFFFULL;
9a64fbe4
FB
1722 break;
1723 }
2be0071f
FB
1724 /* Save msr */
1725 *srr_1 = msr;
1726 /* If we disactivated any translation, flush TLBs */
1727 if (msr_ir || msr_dr) {
1728 tlb_flush(env, 1);
1729 }
9a64fbe4 1730 /* reload MSR with correct bits */
9a64fbe4
FB
1731 msr_ee = 0;
1732 msr_pr = 0;
1733 msr_fp = 0;
1734 msr_fe0 = 0;
1735 msr_se = 0;
1736 msr_be = 0;
1737 msr_fe1 = 0;
1738 msr_ir = 0;
1739 msr_dr = 0;
1740 msr_ri = 0;
1741 msr_le = msr_ile;
2be0071f 1742 msr_sf = msr_isf;
3fc6c082 1743 do_compute_hflags(env);
9a64fbe4 1744 /* Jump to handler */
2be0071f 1745 env->nip = excp;
9a64fbe4 1746 env->exception_index = EXCP_NONE;
fb0eaffc 1747}
18fba28c 1748#endif /* !CONFIG_USER_ONLY */