halt state support for ppc
[qemu] / target-ppc / helper.c
1 /*
2  *  PowerPC emulation helpers for qemu.
3  * 
4  *  Copyright (c) 2003-2005 Jocelyn Mayer
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
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"
30
31 //#define DEBUG_MMU
32 //#define DEBUG_BATS
33 //#define DEBUG_EXCEPTIONS
34 //#define FLUSH_ALL_TLBS
35
36 /*****************************************************************************/
37 /* PowerPC MMU emulation */
38
39 #if defined(CONFIG_USER_ONLY) 
40 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
41                               int is_user, int is_softmmu)
42 {
43     int exception, error_code;
44     
45     if (rw == 2) {
46         exception = EXCP_ISI;
47         error_code = 0;
48     } else {
49         exception = EXCP_DSI;
50         error_code = 0;
51         if (rw)
52             error_code |= 0x02000000;
53         env->spr[SPR_DAR] = address;
54         env->spr[SPR_DSISR] = error_code;
55     }
56     env->exception_index = exception;
57     env->error_code = error_code;
58     return 1;
59 }
60 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
61 {
62     return addr;
63 }
64 #else
65 /* Perform BAT hit & translation */
66 static int get_bat (CPUState *env, uint32_t *real, int *prot,
67                     uint32_t virtual, int rw, int type)
68 {
69     uint32_t *BATlt, *BATut, *BATu, *BATl;
70     uint32_t base, BEPIl, BEPIu, bl;
71     int i;
72     int ret = -1;
73
74 #if defined (DEBUG_BATS)
75     if (loglevel > 0) {
76         fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
77                type == ACCESS_CODE ? 'I' : 'D', virtual);
78     }
79 #endif
80     switch (type) {
81     case ACCESS_CODE:
82         BATlt = env->IBAT[1];
83         BATut = env->IBAT[0];
84         break;
85     default:
86         BATlt = env->DBAT[1];
87         BATut = env->DBAT[0];
88         break;
89     }
90 #if defined (DEBUG_BATS)
91     if (loglevel > 0) {
92         fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
93                type == ACCESS_CODE ? 'I' : 'D', virtual);
94     }
95 #endif
96     base = virtual & 0xFFFC0000;
97     for (i = 0; i < 4; i++) {
98         BATu = &BATut[i];
99         BATl = &BATlt[i];
100         BEPIu = *BATu & 0xF0000000;
101         BEPIl = *BATu & 0x0FFE0000;
102         bl = (*BATu & 0x00001FFC) << 15;
103 #if defined (DEBUG_BATS)
104         if (loglevel > 0) {
105             fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
106                     __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
107                     *BATu, *BATl);
108         }
109 #endif
110         if ((virtual & 0xF0000000) == BEPIu &&
111             ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
112             /* BAT matches */
113             if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
114                 (msr_pr == 1 && (*BATu & 0x00000001))) {
115                 /* Get physical address */
116                 *real = (*BATl & 0xF0000000) |
117                     ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
118                     (virtual & 0x0001F000);
119                 if (*BATl & 0x00000001)
120                     *prot = PAGE_READ;
121                 if (*BATl & 0x00000002)
122                     *prot = PAGE_WRITE | PAGE_READ;
123 #if defined (DEBUG_BATS)
124                 if (loglevel > 0) {
125                     fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
126                             i, *real, *prot & PAGE_READ ? 'R' : '-',
127                             *prot & PAGE_WRITE ? 'W' : '-');
128                 }
129 #endif
130                 ret = 0;
131                 break;
132             }
133         }
134     }
135     if (ret < 0) {
136 #if defined (DEBUG_BATS)
137         printf("no BAT match for 0x%08x:\n", virtual);
138         for (i = 0; i < 4; i++) {
139             BATu = &BATut[i];
140             BATl = &BATlt[i];
141             BEPIu = *BATu & 0xF0000000;
142             BEPIl = *BATu & 0x0FFE0000;
143             bl = (*BATu & 0x00001FFC) << 15;
144             printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
145                    "0x%08x 0x%08x 0x%08x\n",
146                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
147                    *BATu, *BATl, BEPIu, BEPIl, bl);
148         }
149 #endif
150     }
151     /* No hit */
152     return ret;
153 }
154
155 /* PTE table lookup */
156 static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
157                      int h, int key, int rw)
158 {
159     uint32_t pte0, pte1, keep = 0, access = 0;
160     int i, good = -1, store = 0;
161     int ret = -1; /* No entry found */
162
163     for (i = 0; i < 8; i++) {
164         pte0 = ldl_phys(base + (i * 8));
165         pte1 =  ldl_phys(base + (i * 8) + 4);
166 #if defined (DEBUG_MMU)
167         if (loglevel > 0) {
168             fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
169                     "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
170                     pte0 >> 31, h, (pte0 >> 6) & 1, va);
171         }
172 #endif
173         /* Check validity and table match */
174         if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
175             /* Check vsid & api */
176             if ((pte0 & 0x7FFFFFBF) == va) {
177                 if (good == -1) {
178                     good = i;
179                     keep = pte1;
180                 } else {
181                     /* All matches should have equal RPN, WIMG & PP */
182                     if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
183                         if (loglevel > 0)
184                             fprintf(logfile, "Bad RPN/WIMG/PP\n");
185                         return -1;
186                     }
187                 }
188                 /* Check access rights */
189                 if (key == 0) {
190                     access = PAGE_READ;
191                     if ((pte1 & 0x00000003) != 0x3)
192                         access |= PAGE_WRITE;
193                 } else {
194                     switch (pte1 & 0x00000003) {
195                     case 0x0:
196                         access = 0;
197                         break;
198                     case 0x1:
199                     case 0x3:
200                         access = PAGE_READ;
201                         break;
202                     case 0x2:
203                         access = PAGE_READ | PAGE_WRITE;
204                         break;
205                     }
206                 }
207                 if (ret < 0) {
208                     if ((rw == 0 && (access & PAGE_READ)) ||
209                         (rw == 1 && (access & PAGE_WRITE))) {
210 #if defined (DEBUG_MMU)
211                         if (loglevel > 0)
212                             fprintf(logfile, "PTE access granted !\n");
213 #endif
214                         good = i;
215                         keep = pte1;
216                         ret = 0;
217                     } else {
218                         /* Access right violation */
219                         ret = -2;
220 #if defined (DEBUG_MMU)
221                         if (loglevel > 0)
222                             fprintf(logfile, "PTE access rejected\n");
223 #endif
224                     }
225                     *prot = access;
226                 }
227             }
228         }
229     }
230     if (good != -1) {
231         *RPN = keep & 0xFFFFF000;
232 #if defined (DEBUG_MMU)
233         if (loglevel > 0) {
234             fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
235                *RPN, *prot, ret);
236         }
237 #endif
238         /* Update page flags */
239         if (!(keep & 0x00000100)) {
240             /* Access flag */
241             keep |= 0x00000100;
242             store = 1;
243         }
244         if (!(keep & 0x00000080)) {
245             if (rw && ret == 0) {
246                 /* Change flag */
247                 keep |= 0x00000080;
248                 store = 1;
249             } else {
250                 /* Force page fault for first write access */
251                 *prot &= ~PAGE_WRITE;
252             }
253         }
254         if (store) {
255             stl_phys_notdirty(base + (good * 8) + 4, keep);
256         }
257     }
258
259     return ret;
260 }
261
262 static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
263 {
264     return (sdr1 & 0xFFFF0000) | (hash & mask);
265 }
266
267 /* Perform segment based translation */
268 static int get_segment (CPUState *env, uint32_t *real, int *prot,
269                         uint32_t virtual, int rw, int type)
270 {
271     uint32_t pg_addr, sdr, ptem, vsid, pgidx;
272     uint32_t hash, mask;
273     uint32_t sr;
274     int key;
275     int ret = -1, ret2;
276
277     sr = env->sr[virtual >> 28];
278 #if defined (DEBUG_MMU)
279     if (loglevel > 0) {
280         fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
281                 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
282                 virtual, virtual >> 28, sr, env->nip,
283                 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
284     }
285 #endif
286     key = (((sr & 0x20000000) && msr_pr == 1) ||
287         ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
288     if ((sr & 0x80000000) == 0) {
289 #if defined (DEBUG_MMU)
290     if (loglevel > 0) 
291             fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
292                     key, sr & 0x10000000);
293 #endif
294         /* Check if instruction fetch is allowed, if needed */
295         if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
296             /* Page address translation */
297             vsid = sr & 0x00FFFFFF;
298             pgidx = (virtual >> 12) & 0xFFFF;
299             sdr = env->sdr1;
300             hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
301             mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
302             pg_addr = get_pgaddr(sdr, hash, mask);
303             ptem = (vsid << 7) | (pgidx >> 10);
304 #if defined (DEBUG_MMU)
305             if (loglevel > 0) {
306                 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
307                         "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
308                         pg_addr);
309             }
310 #endif
311             /* Primary table lookup */
312             ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
313             if (ret < 0) {
314                 /* Secondary table lookup */
315                 hash = (~hash) & 0x01FFFFC0;
316                 pg_addr = get_pgaddr(sdr, hash, mask);
317 #if defined (DEBUG_MMU)
318                 if (virtual != 0xEFFFFFFF && loglevel > 0) {
319                     fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
320                             "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
321                             hash, pg_addr);
322                 }
323 #endif
324                 ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
325                 if (ret2 != -1)
326                     ret = ret2;
327             }
328         } else {
329 #if defined (DEBUG_MMU)
330             if (loglevel > 0)
331                 fprintf(logfile, "No access allowed\n");
332 #endif
333             ret = -3;
334         }
335     } else {
336 #if defined (DEBUG_MMU)
337         if (loglevel > 0)
338             fprintf(logfile, "direct store...\n");
339 #endif
340         /* Direct-store segment : absolutely *BUGGY* for now */
341         switch (type) {
342         case ACCESS_INT:
343             /* Integer load/store : only access allowed */
344             break;
345         case ACCESS_CODE:
346             /* No code fetch is allowed in direct-store areas */
347             return -4;
348         case ACCESS_FLOAT:
349             /* Floating point load/store */
350             return -4;
351         case ACCESS_RES:
352             /* lwarx, ldarx or srwcx. */
353             return -4;
354         case ACCESS_CACHE:
355             /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
356             /* Should make the instruction do no-op.
357              * As it already do no-op, it's quite easy :-)
358              */
359             *real = virtual;
360             return 0;
361         case ACCESS_EXT:
362             /* eciwx or ecowx */
363             return -4;
364         default:
365             if (logfile) {
366                 fprintf(logfile, "ERROR: instruction should not need "
367                         "address translation\n");
368             }
369             printf("ERROR: instruction should not need "
370                    "address translation\n");
371             return -4;
372         }
373         if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
374             *real = virtual;
375             ret = 2;
376         } else {
377             ret = -2;
378         }
379     }
380
381     return ret;
382 }
383
384 static int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
385                                  uint32_t address, int rw, int access_type)
386 {
387     int ret;
388 #if 0
389     if (loglevel > 0) {
390         fprintf(logfile, "%s\n", __func__);
391     }
392 #endif    
393     if ((access_type == ACCESS_CODE && msr_ir == 0) ||
394         (access_type != ACCESS_CODE && msr_dr == 0)) {
395         /* No address translation */
396         *physical = address & ~0xFFF;
397         *prot = PAGE_READ | PAGE_WRITE;
398         ret = 0;
399     } else {
400         /* Try to find a BAT */
401         ret = get_bat(env, physical, prot, address, rw, access_type);
402         if (ret < 0) {
403             /* We didn't match any BAT entry */
404             ret = get_segment(env, physical, prot, address, rw, access_type);
405         }
406     }
407 #if 0
408     if (loglevel > 0) {
409         fprintf(logfile, "%s address %08x => %08x\n",
410                 __func__, address, *physical);
411     }
412 #endif    
413     return ret;
414 }
415
416 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
417 {
418     uint32_t phys_addr;
419     int prot;
420
421     if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
422         return -1;
423     return phys_addr;
424 }
425
426 /* Perform address translation */
427 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
428                               int is_user, int is_softmmu)
429 {
430     uint32_t physical;
431     int prot;
432     int exception = 0, error_code = 0;
433     int access_type;
434     int ret = 0;
435
436     if (rw == 2) {
437         /* code access */
438         rw = 0;
439         access_type = ACCESS_CODE;
440     } else {
441         /* data access */
442         /* XXX: put correct access by using cpu_restore_state()
443            correctly */
444         access_type = ACCESS_INT;
445         //        access_type = env->access_type;
446     }
447     if (env->user_mode_only) {
448         /* user mode only emulation */
449         ret = -2;
450         goto do_fault;
451     }
452     ret = get_physical_address(env, &physical, &prot,
453                                address, rw, access_type);
454     if (ret == 0) {
455         ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
456                            is_user, is_softmmu);
457     } else if (ret < 0) {
458     do_fault:
459 #if defined (DEBUG_MMU)
460         if (loglevel > 0)
461             cpu_dump_state(env, logfile, fprintf, 0);
462 #endif
463         if (access_type == ACCESS_CODE) {
464             exception = EXCP_ISI;
465             switch (ret) {
466             case -1:
467                 /* No matches in page tables */
468                 error_code = 0x40000000;
469                 break;
470             case -2:
471                 /* Access rights violation */
472                 error_code = 0x08000000;
473                 break;
474             case -3:
475                 /* No execute protection violation */
476                 error_code = 0x10000000;
477                 break;
478             case -4:
479                 /* Direct store exception */
480                 /* No code fetch is allowed in direct-store areas */
481                 error_code = 0x10000000;
482                 break;
483             case -5:
484                 /* No match in segment table */
485                 exception = EXCP_ISEG;
486                 error_code = 0;
487                 break;
488             }
489         } else {
490             exception = EXCP_DSI;
491             switch (ret) {
492             case -1:
493                 /* No matches in page tables */
494                 error_code = 0x40000000;
495                 break;
496             case -2:
497                 /* Access rights violation */
498                 error_code = 0x08000000;
499                 break;
500             case -4:
501                 /* Direct store exception */
502                 switch (access_type) {
503                 case ACCESS_FLOAT:
504                     /* Floating point load/store */
505                     exception = EXCP_ALIGN;
506                     error_code = EXCP_ALIGN_FP;
507                     break;
508                 case ACCESS_RES:
509                     /* lwarx, ldarx or srwcx. */
510                     error_code = 0x04000000;
511                     break;
512                 case ACCESS_EXT:
513                     /* eciwx or ecowx */
514                     error_code = 0x04100000;
515                     break;
516                 default:
517                     printf("DSI: invalid exception (%d)\n", ret);
518                     exception = EXCP_PROGRAM;
519                     error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
520                     break;
521                 }
522                 break;
523             case -5:
524                 /* No match in segment table */
525                 exception = EXCP_DSEG;
526                 error_code = 0;
527                 break;
528             }
529             if (exception == EXCP_DSI && rw == 1)
530                 error_code |= 0x02000000;
531             /* Store fault address */
532             env->spr[SPR_DAR] = address;
533             env->spr[SPR_DSISR] = error_code;
534         }
535 #if 0
536         printf("%s: set exception to %d %02x\n",
537                __func__, exception, error_code);
538 #endif
539         env->exception_index = exception;
540         env->error_code = error_code;
541         ret = 1;
542     }
543     return ret;
544 }
545 #endif
546
547 /*****************************************************************************/
548 /* BATs management */
549 #if !defined(FLUSH_ALL_TLBS)
550 static inline void do_invalidate_BAT (CPUPPCState *env,
551                                       target_ulong BATu, target_ulong mask)
552 {
553     target_ulong base, end, page;
554     base = BATu & ~0x0001FFFF;
555     end = base + mask + 0x00020000;
556 #if defined (DEBUG_BATS)
557     if (loglevel != 0)
558         fprintf(logfile, "Flush BAT from %08x to %08x (%08x)\n", base, end, mask);
559 #endif
560     for (page = base; page != end; page += TARGET_PAGE_SIZE)
561         tlb_flush_page(env, page);
562 #if defined (DEBUG_BATS)
563     if (loglevel != 0)
564         fprintf(logfile, "Flush done\n");
565 #endif
566 }
567 #endif
568
569 static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
570                                    target_ulong value)
571 {
572 #if defined (DEBUG_BATS)
573     if (loglevel != 0) {
574         fprintf(logfile, "Set %cBAT%d%c to 0x%08lx (0x%08lx)\n",
575                 ID, nr, ul == 0 ? 'u' : 'l', (unsigned long)value,
576                 (unsigned long)env->nip);
577     }
578 #endif
579 }
580
581 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
582 {
583     return env->IBAT[0][nr];
584 }
585
586 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
587 {
588     return env->IBAT[1][nr];
589 }
590
591 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
592 {
593     target_ulong mask;
594
595     dump_store_bat(env, 'I', 0, nr, value);
596     if (env->IBAT[0][nr] != value) {
597         mask = (value << 15) & 0x0FFE0000UL;
598 #if !defined(FLUSH_ALL_TLBS)
599         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
600 #endif
601         /* When storing valid upper BAT, mask BEPI and BRPN
602          * and invalidate all TLBs covered by this BAT
603          */
604         mask = (value << 15) & 0x0FFE0000UL;
605         env->IBAT[0][nr] = (value & 0x00001FFFUL) |
606             (value & ~0x0001FFFFUL & ~mask);
607         env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
608             (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
609 #if !defined(FLUSH_ALL_TLBS)
610         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
611 #endif
612 #if defined(FLUSH_ALL_TLBS)
613         tlb_flush(env, 1);
614 #endif
615     }
616 }
617
618 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
619 {
620     dump_store_bat(env, 'I', 1, nr, value);
621     env->IBAT[1][nr] = value;
622 }
623
624 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
625 {
626     return env->DBAT[0][nr];
627 }
628
629 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
630 {
631     return env->DBAT[1][nr];
632 }
633
634 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
635 {
636     target_ulong mask;
637
638     dump_store_bat(env, 'D', 0, nr, value);
639     if (env->DBAT[0][nr] != value) {
640         /* When storing valid upper BAT, mask BEPI and BRPN
641          * and invalidate all TLBs covered by this BAT
642          */
643         mask = (value << 15) & 0x0FFE0000UL;
644 #if !defined(FLUSH_ALL_TLBS)
645         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
646 #endif
647         mask = (value << 15) & 0x0FFE0000UL;
648         env->DBAT[0][nr] = (value & 0x00001FFFUL) |
649             (value & ~0x0001FFFFUL & ~mask);
650         env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
651             (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
652 #if !defined(FLUSH_ALL_TLBS)
653         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
654 #else
655         tlb_flush(env, 1);
656 #endif
657     }
658 }
659
660 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
661 {
662     dump_store_bat(env, 'D', 1, nr, value);
663     env->DBAT[1][nr] = value;
664 }
665
666 static inline void invalidate_all_tlbs (CPUPPCState *env)
667 {
668     /* XXX: this needs to be completed for sotware driven TLB support */
669     tlb_flush(env, 1);
670 }
671
672 /*****************************************************************************/
673 /* Special registers manipulation */
674 target_ulong do_load_nip (CPUPPCState *env)
675 {
676     return env->nip;
677 }
678
679 void do_store_nip (CPUPPCState *env, target_ulong value)
680 {
681     env->nip = value;
682 }
683
684 target_ulong do_load_sdr1 (CPUPPCState *env)
685 {
686     return env->sdr1;
687 }
688
689 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
690 {
691 #if defined (DEBUG_MMU)
692     if (loglevel != 0) {
693         fprintf(logfile, "%s: 0x%08lx\n", __func__, (unsigned long)value);
694     }
695 #endif
696     if (env->sdr1 != value) {
697         env->sdr1 = value;
698         invalidate_all_tlbs(env);
699     }
700 }
701
702 target_ulong do_load_sr (CPUPPCState *env, int srnum)
703 {
704     return env->sr[srnum];
705 }
706
707 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
708 {
709 #if defined (DEBUG_MMU)
710     if (loglevel != 0) {
711         fprintf(logfile, "%s: reg=%d 0x%08lx %08lx\n",
712                 __func__, srnum, (unsigned long)value, env->sr[srnum]);
713     }
714 #endif
715     if (env->sr[srnum] != value) {
716         env->sr[srnum] = value;
717 #if !defined(FLUSH_ALL_TLBS) && 0
718         {
719             target_ulong page, end;
720             /* Invalidate 256 MB of virtual memory */
721             page = (16 << 20) * srnum;
722             end = page + (16 << 20);
723             for (; page != end; page += TARGET_PAGE_SIZE)
724                 tlb_flush_page(env, page);
725         }
726 #else
727         invalidate_all_tlbs(env);
728 #endif
729     }
730 }
731
732 uint32_t do_load_cr (CPUPPCState *env)
733 {
734     return (env->crf[0] << 28) |
735         (env->crf[1] << 24) |
736         (env->crf[2] << 20) |
737         (env->crf[3] << 16) |
738         (env->crf[4] << 12) |
739         (env->crf[5] << 8) |
740         (env->crf[6] << 4) |
741         (env->crf[7] << 0);
742 }
743
744 void do_store_cr (CPUPPCState *env, uint32_t value, uint32_t mask)
745 {
746     int i, sh;
747
748     for (i = 0, sh = 7; i < 8; i++, sh --) {
749         if (mask & (1 << sh))
750             env->crf[i] = (value >> (sh * 4)) & 0xFUL;
751     }
752 }
753
754 uint32_t do_load_xer (CPUPPCState *env)
755 {
756     return (xer_so << XER_SO) |
757         (xer_ov << XER_OV) |
758         (xer_ca << XER_CA) |
759         (xer_bc << XER_BC) |
760         (xer_cmp << XER_CMP);
761 }
762
763 void do_store_xer (CPUPPCState *env, uint32_t value)
764 {
765     xer_so = (value >> XER_SO) & 0x01;
766     xer_ov = (value >> XER_OV) & 0x01;
767     xer_ca = (value >> XER_CA) & 0x01;
768     xer_cmp = (value >> XER_CMP) & 0xFF;
769     xer_bc = (value >> XER_BC) & 0x3F;
770 }
771
772 target_ulong do_load_msr (CPUPPCState *env)
773 {
774     return (msr_vr << MSR_VR)  |
775         (msr_ap  << MSR_AP)  |
776         (msr_sa  << MSR_SA)  |
777         (msr_key << MSR_KEY) |
778         (msr_pow << MSR_POW) |
779         (msr_tlb << MSR_TLB) |
780         (msr_ile << MSR_ILE) |
781         (msr_ee << MSR_EE) |
782         (msr_pr << MSR_PR) |
783         (msr_fp << MSR_FP) |
784         (msr_me << MSR_ME) |
785         (msr_fe0 << MSR_FE0) |
786         (msr_se << MSR_SE) |
787         (msr_be << MSR_BE) |
788         (msr_fe1 << MSR_FE1) |
789         (msr_al  << MSR_AL)  |
790         (msr_ip << MSR_IP) |
791         (msr_ir << MSR_IR) |
792         (msr_dr << MSR_DR) |
793         (msr_pe  << MSR_PE)  |
794         (msr_px  << MSR_PX)  |
795         (msr_ri << MSR_RI) |
796         (msr_le << MSR_LE);
797 }
798
799 void do_compute_hflags (CPUPPCState *env)
800 {
801     /* Compute current hflags */
802     env->hflags = (msr_pr << MSR_PR) | (msr_le << MSR_LE) |
803         (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_fe1 << MSR_FE1) |
804         (msr_vr << MSR_VR) | (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | 
805         (msr_se << MSR_SE) | (msr_be << MSR_BE);
806 }
807
808 void do_store_msr (CPUPPCState *env, target_ulong value)
809 {
810     value &= env->msr_mask;
811     if (((value >> MSR_IR) & 1) != msr_ir ||
812         ((value >> MSR_DR) & 1) != msr_dr) {
813         /* Flush all tlb when changing translation mode
814          * When using software driven TLB, we may also need to reload
815          * all defined TLBs
816          */
817         tlb_flush(env, 1);
818         env->interrupt_request |= CPU_INTERRUPT_EXITTB;
819     }
820 #if 0
821     if (loglevel != 0) {
822         fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
823     }
824 #endif
825     msr_vr  = (value >> MSR_VR)  & 1;
826     msr_ap  = (value >> MSR_AP)  & 1;
827     msr_sa  = (value >> MSR_SA)  & 1;
828     msr_key = (value >> MSR_KEY) & 1;
829     msr_pow = (value >> MSR_POW) & 1;
830     msr_tlb = (value >> MSR_TLB)  & 1;
831     msr_ile = (value >> MSR_ILE) & 1;
832     msr_ee  = (value >> MSR_EE)  & 1;
833     msr_pr  = (value >> MSR_PR)  & 1;
834     msr_fp  = (value >> MSR_FP)  & 1;
835     msr_me  = (value >> MSR_ME)  & 1;
836     msr_fe0 = (value >> MSR_FE0) & 1;
837     msr_se  = (value >> MSR_SE)  & 1;
838     msr_be  = (value >> MSR_BE)  & 1;
839     msr_fe1 = (value >> MSR_FE1) & 1;
840     msr_al  = (value >> MSR_AL)  & 1;
841     msr_ip  = (value >> MSR_IP)  & 1;
842     msr_ir  = (value >> MSR_IR)  & 1;
843     msr_dr  = (value >> MSR_DR)  & 1;
844     msr_pe  = (value >> MSR_PE)  & 1;
845     msr_px  = (value >> MSR_PX)  & 1;
846     msr_ri  = (value >> MSR_RI)  & 1;
847     msr_le  = (value >> MSR_LE)  & 1;
848     do_compute_hflags(env);
849     if (msr_pow) {
850         /* power save: exit cpu loop */
851         env->exception_index = EXCP_HLT;
852         cpu_loop_exit();
853     }
854 }
855
856 float64 do_load_fpscr (CPUPPCState *env)
857 {
858     /* The 32 MSB of the target fpr are undefined.
859      * They'll be zero...
860      */
861     union {
862         float64 d;
863         struct {
864             uint32_t u[2];
865         } s;
866     } u;
867     int i;
868
869 #ifdef WORDS_BIGENDIAN
870 #define WORD0 0
871 #define WORD1 1
872 #else
873 #define WORD0 1
874 #define WORD1 0
875 #endif
876     u.s.u[WORD0] = 0;
877     u.s.u[WORD1] = 0;
878     for (i = 0; i < 8; i++)
879         u.s.u[WORD1] |= env->fpscr[i] << (4 * i);
880     return u.d;
881 }
882
883 void do_store_fpscr (CPUPPCState *env, float64 f, uint32_t mask)
884 {
885     /*
886      * We use only the 32 LSB of the incoming fpr
887      */
888     union {
889         double d;
890         struct {
891             uint32_t u[2];
892         } s;
893     } u;
894     int i, rnd_type;
895
896     u.d = f;
897     if (mask & 0x80)
898         env->fpscr[0] = (env->fpscr[0] & 0x9) | ((u.s.u[WORD1] >> 28) & ~0x9);
899     for (i = 1; i < 7; i++) {
900         if (mask & (1 << (7 - i)))
901             env->fpscr[i] = (u.s.u[WORD1] >> (4 * (7 - i))) & 0xF;
902     }
903     /* TODO: update FEX & VX */
904     /* Set rounding mode */
905     switch (env->fpscr[0] & 0x3) {
906     case 0:
907         /* Best approximation (round to nearest) */
908         rnd_type = float_round_nearest_even;
909         break;
910     case 1:
911         /* Smaller magnitude (round toward zero) */
912         rnd_type = float_round_to_zero;
913         break;
914     case 2:
915         /* Round toward +infinite */
916         rnd_type = float_round_up;
917         break;
918     default:
919     case 3:
920         /* Round toward -infinite */
921         rnd_type = float_round_down;
922         break;
923     }
924     set_float_rounding_mode(rnd_type, &env->fp_status);
925 }
926
927 /*****************************************************************************/
928 /* Exception processing */
929 #if defined (CONFIG_USER_ONLY)
930 void do_interrupt (CPUState *env)
931 {
932     env->exception_index = -1;
933 }
934 #else
935 static void dump_syscall(CPUState *env)
936 {
937     fprintf(logfile, "syscall r0=0x%08x r3=0x%08x r4=0x%08x r5=0x%08x r6=0x%08x nip=0x%08x\n",
938             env->gpr[0], env->gpr[3], env->gpr[4],
939             env->gpr[5], env->gpr[6], env->nip);
940 }
941
942 void do_interrupt (CPUState *env)
943 {
944     target_ulong msr, *srr_0, *srr_1, tmp;
945     int excp;
946
947     excp = env->exception_index;
948     msr = do_load_msr(env);
949     /* The default is to use SRR0 & SRR1 to save the exception context */
950     srr_0 = &env->spr[SPR_SRR0];
951     srr_1 = &env->spr[SPR_SRR1];
952 #if defined (DEBUG_EXCEPTIONS)
953     if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) {
954         if (loglevel != 0) {
955             fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
956                     (unsigned long)env->nip, excp, env->error_code);
957             cpu_dump_state(env, logfile, fprintf, 0);
958         }
959     }
960 #endif
961     if (loglevel & CPU_LOG_INT) {
962         fprintf(logfile, "Raise exception at 0x%08lx => 0x%08x (%02x)\n",
963                 (unsigned long)env->nip, excp, env->error_code);
964     }
965     msr_pow = 0;
966     /* Generate informations in save/restore registers */
967     switch (excp) {
968         /* Generic PowerPC exceptions */
969     case EXCP_RESET: /* 0x0100 */
970         if (PPC_EXCP(env) != PPC_FLAGS_EXCP_40x) {
971             if (msr_ip)
972                 excp += 0xFFC00;
973             excp |= 0xFFC00000;
974         } else {
975             srr_0 = &env->spr[SPR_40x_SRR2];
976             srr_1 = &env->spr[SPR_40x_SRR3];
977         }
978         goto store_next;
979     case EXCP_MACHINE_CHECK: /* 0x0200 */
980         if (msr_me == 0) {
981             cpu_abort(env, "Machine check exception while not allowed\n");
982         }
983         if (PPC_EXCP(env) == PPC_FLAGS_EXCP_40x) {
984             srr_0 = &env->spr[SPR_40x_SRR2];
985             srr_1 = &env->spr[SPR_40x_SRR3];
986         }
987         msr_me = 0;
988         break;
989     case EXCP_DSI: /* 0x0300 */
990         /* Store exception cause */
991         /* data location address has been stored
992          * when the fault has been detected
993          */
994         msr &= ~0xFFFF0000;
995 #if defined (DEBUG_EXCEPTIONS)
996         if (loglevel) {
997             fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
998                     env->spr[SPR_DSISR], env->spr[SPR_DAR]);
999         } else {
1000             printf("DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1001                    env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1002         }
1003 #endif
1004         goto store_next;
1005     case EXCP_ISI: /* 0x0400 */
1006         /* Store exception cause */
1007         msr &= ~0xFFFF0000;
1008         msr |= env->error_code;
1009 #if defined (DEBUG_EXCEPTIONS)
1010         if (loglevel != 0) {
1011             fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
1012                     msr, env->nip);
1013         }
1014 #endif
1015         goto store_next;
1016     case EXCP_EXTERNAL: /* 0x0500 */
1017         if (msr_ee == 0) {
1018 #if defined (DEBUG_EXCEPTIONS)
1019             if (loglevel > 0) {
1020                 fprintf(logfile, "Skipping hardware interrupt\n");
1021             }
1022 #endif
1023             /* Requeue it */
1024             env->interrupt_request |= CPU_INTERRUPT_HARD;
1025             return;
1026         }
1027         goto store_next;
1028     case EXCP_ALIGN: /* 0x0600 */
1029         if (PPC_EXCP(env) != PPC_FLAGS_EXCP_601) {
1030             /* Store exception cause */
1031             /* Get rS/rD and rA from faulting opcode */
1032             env->spr[SPR_DSISR] |=
1033                 (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
1034             /* data location address has been stored
1035              * when the fault has been detected
1036              */
1037         } else {
1038             /* IO error exception on PowerPC 601 */
1039             /* XXX: TODO */
1040             cpu_abort(env,
1041                       "601 IO error exception is not implemented yet !\n");
1042         }
1043         goto store_current;
1044     case EXCP_PROGRAM: /* 0x0700 */
1045         msr &= ~0xFFFF0000;
1046         switch (env->error_code & ~0xF) {
1047         case EXCP_FP:
1048             if (msr_fe0 == 0 && msr_fe1 == 0) {
1049 #if defined (DEBUG_EXCEPTIONS)
1050                 printf("Ignore floating point exception\n");
1051 #endif
1052                 return;
1053         }
1054             msr |= 0x00100000;
1055             /* Set FX */
1056             env->fpscr[7] |= 0x8;
1057             /* Finally, update FEX */
1058             if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
1059                 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
1060                 env->fpscr[7] |= 0x4;
1061         break;
1062         case EXCP_INVAL:
1063             //      printf("Invalid instruction at 0x%08x\n", env->nip);
1064             msr |= 0x00080000;
1065         break;
1066         case EXCP_PRIV:
1067             msr |= 0x00040000;
1068         break;
1069         case EXCP_TRAP:
1070             msr |= 0x00020000;
1071             break;
1072         default:
1073             /* Should never occur */
1074         break;
1075     }
1076         msr |= 0x00010000;
1077         goto store_current;
1078     case EXCP_NO_FP: /* 0x0800 */
1079         msr &= ~0xFFFF0000;
1080         goto store_current;
1081     case EXCP_DECR:
1082         if (msr_ee == 0) {
1083 #if 1
1084             /* Requeue it */
1085             env->interrupt_request |= CPU_INTERRUPT_TIMER;
1086 #endif
1087             return;
1088         }
1089         goto store_next;
1090     case EXCP_SYSCALL: /* 0x0C00 */
1091         /* NOTE: this is a temporary hack to support graphics OSI
1092            calls from the MOL driver */
1093         if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
1094             env->osi_call) {
1095             if (env->osi_call(env) != 0)
1096                 return;
1097         }
1098         if (loglevel & CPU_LOG_INT) {
1099             dump_syscall(env);
1100         }
1101         goto store_next;
1102     case EXCP_TRACE: /* 0x0D00 */
1103         /* XXX: TODO */
1104         cpu_abort(env, "Trace exception is not implemented yet !\n");
1105         goto store_next;
1106     case EXCP_PERF: /* 0x0F00 */
1107         /* XXX: TODO */
1108         cpu_abort(env,
1109                   "Performance counter exception is not implemented yet !\n");
1110         goto store_next;
1111     /* 32 bits PowerPC specific exceptions */
1112     case EXCP_FP_ASSIST: /* 0x0E00 */
1113         /* XXX: TODO */
1114         cpu_abort(env, "Floating point assist exception "
1115                   "is not implemented yet !\n");
1116         goto store_next;
1117     /* 64 bits PowerPC exceptions */
1118     case EXCP_DSEG: /* 0x0380 */
1119         /* XXX: TODO */
1120         cpu_abort(env, "Data segment exception is not implemented yet !\n");
1121         goto store_next;
1122     case EXCP_ISEG: /* 0x0480 */
1123         /* XXX: TODO */
1124         cpu_abort(env,
1125                   "Instruction segment exception is not implemented yet !\n");
1126         goto store_next;
1127     case EXCP_HDECR: /* 0x0980 */
1128         if (msr_ee == 0) {
1129 #if 1
1130             /* Requeue it */
1131             env->interrupt_request |= CPU_INTERRUPT_TIMER;
1132 #endif
1133         return;
1134         }
1135         cpu_abort(env,
1136                   "Hypervisor decrementer exception is not implemented yet !\n");
1137         goto store_next;
1138     /* Implementation specific exceptions */
1139     case 0x0A00:
1140         if (PPC_EXCP(env) != PPC_FLAGS_EXCP_602) {
1141             /* Critical interrupt on G2 */
1142             /* XXX: TODO */
1143             cpu_abort(env, "G2 critical interrupt is not implemented yet !\n");
1144             goto store_next;
1145         } else {
1146             cpu_abort(env, "Invalid exception 0x0A00 !\n");
1147         }
1148         return;
1149     case 0x0F20:
1150         switch (PPC_EXCP(env)) {
1151         case PPC_FLAGS_EXCP_40x:
1152             /* APU unavailable on 405 */
1153             /* XXX: TODO */
1154             cpu_abort(env,
1155                       "APU unavailable exception is not implemented yet !\n");
1156             goto store_next;
1157         case PPC_FLAGS_EXCP_74xx:
1158             /* Altivec unavailable */
1159             /* XXX: TODO */
1160             cpu_abort(env, "Altivec unavailable exception "
1161                       "is not implemented yet !\n");
1162             goto store_next;
1163         default:
1164             cpu_abort(env, "Invalid exception 0x0F20 !\n");
1165             break;
1166         }
1167         return;
1168     case 0x1000:
1169         switch (PPC_EXCP(env)) {
1170         case PPC_FLAGS_EXCP_40x:
1171             /* PIT on 4xx */
1172             /* XXX: TODO */
1173             cpu_abort(env, "40x PIT exception is not implemented yet !\n");
1174             goto store_next;
1175         case PPC_FLAGS_EXCP_602:
1176         case PPC_FLAGS_EXCP_603:
1177             /* ITLBMISS on 602/603 */
1178             msr &= ~0xF00F0000;
1179             msr_tgpr = 1;
1180             goto store_gprs;
1181         default:
1182             cpu_abort(env, "Invalid exception 0x1000 !\n");
1183             break;
1184         }
1185         return;
1186     case 0x1010:
1187         switch (PPC_EXCP(env)) {
1188         case PPC_FLAGS_EXCP_40x:
1189             /* FIT on 4xx */
1190             cpu_abort(env, "40x FIT exception is not implemented yet !\n");
1191             /* XXX: TODO */
1192             goto store_next;
1193         default:
1194             cpu_abort(env, "Invalid exception 0x1010 !\n");
1195             break;
1196         }
1197         return;
1198     case 0x1020:
1199         switch (PPC_EXCP(env)) {
1200         case PPC_FLAGS_EXCP_40x:
1201             /* Watchdog on 4xx */
1202             /* XXX: TODO */
1203             cpu_abort(env,
1204                       "40x watchdog exception is not implemented yet !\n");
1205             goto store_next;
1206         default:
1207             cpu_abort(env, "Invalid exception 0x1020 !\n");
1208             break;
1209         }
1210         return;
1211     case 0x1100:
1212         switch (PPC_EXCP(env)) {
1213         case PPC_FLAGS_EXCP_40x:
1214             /* DTLBMISS on 4xx */
1215             /* XXX: TODO */
1216             cpu_abort(env,
1217                       "40x DTLBMISS exception is not implemented yet !\n");
1218             goto store_next;
1219         case PPC_FLAGS_EXCP_602:
1220         case PPC_FLAGS_EXCP_603:
1221             /* DLTLBMISS on 602/603 */
1222             msr &= ~0xF00F0000;
1223             msr_tgpr = 1;
1224             goto store_gprs;
1225         default:
1226             cpu_abort(env, "Invalid exception 0x1100 !\n");
1227             break;
1228         }
1229         return;
1230     case 0x1200:
1231         switch (PPC_EXCP(env)) {
1232         case PPC_FLAGS_EXCP_40x:
1233             /* ITLBMISS on 4xx */
1234             /* XXX: TODO */
1235             cpu_abort(env,
1236                       "40x ITLBMISS exception is not implemented yet !\n");
1237             goto store_next;
1238         case PPC_FLAGS_EXCP_602:
1239         case PPC_FLAGS_EXCP_603:
1240             /* DSTLBMISS on 602/603 */
1241             msr &= ~0xF00F0000;
1242             msr_tgpr = 1;
1243         store_gprs:
1244 #if defined (DEBUG_SOFTWARE_TLB)
1245             if (loglevel != 0) {
1246                 fprintf(logfile, "6xx %sTLB miss: IM %08x DM %08x IC %08x "
1247                         "DC %08x H1 %08x H2 %08x %08x\n",
1248                         excp == 0x1000 ? "I" : excp == 0x1100 ? "DL" : "DS",
1249                         env->spr[SPR_IMISS], env->spr[SPR_DMISS],
1250                         env->spr[SPR_ICMP], env->spr[SPR_DCMP],
1251                         env->spr[SPR_DHASH1], env->spr[SPR_DHASH2],
1252                         env->error_code);
1253             }
1254 #endif
1255             /* Swap temporary saved registers with GPRs */
1256             tmp = env->gpr[0];
1257             env->gpr[0] = env->tgpr[0];
1258             env->tgpr[0] = tmp;
1259             tmp = env->gpr[1];
1260             env->gpr[1] = env->tgpr[1];
1261             env->tgpr[1] = tmp;
1262             tmp = env->gpr[2];
1263             env->gpr[2] = env->tgpr[2];
1264             env->tgpr[2] = tmp;
1265             tmp = env->gpr[3];
1266             env->gpr[3] = env->tgpr[3];
1267             env->tgpr[3] = tmp;
1268             msr |= env->crf[0] << 28;
1269             msr |= env->error_code; /* key, D/I, S/L bits */
1270             /* Set way using a LRU mechanism */
1271             msr |= (env->last_way ^ 1) << 17;
1272             goto store_next;
1273         default:
1274             cpu_abort(env, "Invalid exception 0x1200 !\n");
1275             break;
1276         }
1277         return;
1278     case 0x1300:
1279         switch (PPC_EXCP(env)) {
1280         case PPC_FLAGS_EXCP_601:
1281         case PPC_FLAGS_EXCP_602:
1282         case PPC_FLAGS_EXCP_603:
1283         case PPC_FLAGS_EXCP_604:
1284         case PPC_FLAGS_EXCP_7x0:
1285         case PPC_FLAGS_EXCP_7x5:
1286             /* IABR on 6xx/7xx */
1287             /* XXX: TODO */
1288             cpu_abort(env, "IABR exception is not implemented yet !\n");
1289             goto store_next;
1290         default:
1291             cpu_abort(env, "Invalid exception 0x1300 !\n");
1292             break;
1293         }
1294         return;
1295     case 0x1400:
1296         switch (PPC_EXCP(env)) {
1297         case PPC_FLAGS_EXCP_601:
1298         case PPC_FLAGS_EXCP_602:
1299         case PPC_FLAGS_EXCP_603:
1300         case PPC_FLAGS_EXCP_604:
1301         case PPC_FLAGS_EXCP_7x0:
1302         case PPC_FLAGS_EXCP_7x5:
1303             /* SMI on 6xx/7xx */
1304             /* XXX: TODO */
1305             cpu_abort(env, "SMI exception is not implemented yet !\n");
1306             goto store_next;
1307         default:
1308             cpu_abort(env, "Invalid exception 0x1400 !\n");
1309             break;
1310         }
1311         return;
1312     case 0x1500:
1313         switch (PPC_EXCP(env)) {
1314         case PPC_FLAGS_EXCP_602:
1315             /* Watchdog on 602 */
1316             cpu_abort(env,
1317                       "602 watchdog exception is not implemented yet !\n");
1318             goto store_next;
1319         case PPC_FLAGS_EXCP_970:
1320             /* Soft patch exception on 970 */
1321             /* XXX: TODO */
1322             cpu_abort(env,
1323                       "970 soft-patch exception is not implemented yet !\n");
1324             goto store_next;
1325         case PPC_FLAGS_EXCP_74xx:
1326             /* VPU assist on 74xx */
1327             /* XXX: TODO */
1328             cpu_abort(env, "VPU assist exception is not implemented yet !\n");
1329             goto store_next;
1330         default:
1331             cpu_abort(env, "Invalid exception 0x1500 !\n");
1332             break;
1333         }
1334         return;
1335     case 0x1600:
1336         switch (PPC_EXCP(env)) {
1337         case PPC_FLAGS_EXCP_602:
1338             /* Emulation trap on 602 */
1339             /* XXX: TODO */
1340             cpu_abort(env, "602 emulation trap exception "
1341                       "is not implemented yet !\n");
1342             goto store_next;
1343         case PPC_FLAGS_EXCP_970:
1344             /* Maintenance exception on 970 */
1345             /* XXX: TODO */
1346             cpu_abort(env,
1347                       "970 maintenance exception is not implemented yet !\n");
1348             goto store_next;
1349         default:
1350             cpu_abort(env, "Invalid exception 0x1600 !\n");
1351             break;
1352         }
1353         return;
1354     case 0x1700:
1355         switch (PPC_EXCP(env)) {
1356         case PPC_FLAGS_EXCP_7x0:
1357         case PPC_FLAGS_EXCP_7x5:
1358             /* Thermal management interrupt on G3 */
1359             /* XXX: TODO */
1360             cpu_abort(env, "G3 thermal management exception "
1361                       "is not implemented yet !\n");
1362             goto store_next;
1363         case PPC_FLAGS_EXCP_970:
1364             /* VPU assist on 970 */
1365             /* XXX: TODO */
1366             cpu_abort(env,
1367                       "970 VPU assist exception is not implemented yet !\n");
1368             goto store_next;
1369         default:
1370             cpu_abort(env, "Invalid exception 0x1700 !\n");
1371             break;
1372         }
1373         return;
1374     case 0x1800:
1375         switch (PPC_EXCP(env)) {
1376         case PPC_FLAGS_EXCP_970:
1377             /* Thermal exception on 970 */
1378             /* XXX: TODO */
1379             cpu_abort(env, "970 thermal management exception "
1380                       "is not implemented yet !\n");
1381             goto store_next;
1382         default:
1383             cpu_abort(env, "Invalid exception 0x1800 !\n");
1384             break;
1385         }
1386         return;
1387     case 0x2000:
1388         switch (PPC_EXCP(env)) {
1389         case PPC_FLAGS_EXCP_40x:
1390             /* DEBUG on 4xx */
1391             /* XXX: TODO */
1392             cpu_abort(env, "40x debug exception is not implemented yet !\n");
1393             goto store_next;
1394         case PPC_FLAGS_EXCP_601:
1395             /* Run mode exception on 601 */
1396             /* XXX: TODO */
1397             cpu_abort(env,
1398                       "601 run mode exception is not implemented yet !\n");
1399             goto store_next;
1400         default:
1401             cpu_abort(env, "Invalid exception 0x1800 !\n");
1402             break;
1403         }
1404         return;
1405     /* Other exceptions */
1406     /* Qemu internal exceptions:
1407      * we should never come here with those values: abort execution
1408      */
1409     default:
1410         cpu_abort(env, "Invalid exception: code %d (%04x)\n", excp, excp);
1411         return;
1412     store_current:
1413         /* save current instruction location */
1414         *srr_0 = (env->nip - 4) & 0xFFFFFFFFULL;
1415         break;
1416     store_next:
1417         /* save next instruction location */
1418         *srr_0 = env->nip & 0xFFFFFFFFULL;
1419         break;
1420     }
1421     /* Save msr */
1422     *srr_1 = msr;
1423     /* If we disactivated any translation, flush TLBs */
1424     if (msr_ir || msr_dr) {
1425         tlb_flush(env, 1);
1426     }
1427     /* reload MSR with correct bits */
1428     msr_ee = 0;
1429     msr_pr = 0;
1430     msr_fp = 0;
1431     msr_fe0 = 0;
1432     msr_se = 0;
1433     msr_be = 0;
1434     msr_fe1 = 0;
1435     msr_ir = 0;
1436     msr_dr = 0;
1437     msr_ri = 0;
1438     msr_le = msr_ile;
1439     msr_sf = msr_isf;
1440     do_compute_hflags(env);
1441     /* Jump to handler */
1442     env->nip = excp;
1443     env->exception_index = EXCP_NONE;
1444 }
1445 #endif /* !CONFIG_USER_ONLY */