ppc fixes (Jocelyn Mayer)
[qemu] / target-ppc / helper.c
1 /*
2  *  PPC emulation helpers for qemu.
3  * 
4  *  Copyright (c) 2003 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 <sys/mman.h>
21
22 #include "exec.h"
23 #if defined (USE_OPEN_FIRMWARE)
24 #include <time.h>
25 #include "of.h"
26 #endif
27
28 //#define DEBUG_MMU
29 //#define DEBUG_BATS
30 //#define DEBUG_EXCEPTIONS
31
32 extern FILE *logfile, *stdout, *stderr;
33 void exit (int);
34 void abort (void);
35
36 void cpu_loop_exit(void)
37 {
38     longjmp(env->jmp_env, 1);
39 }
40
41 void do_process_exceptions (void)
42 {
43     cpu_loop_exit();
44 }
45
46 int check_exception_state (CPUState *env)
47 {
48     int i;
49
50     /* Process PPC exceptions */
51     for (i = 1; i  < EXCP_PPC_MAX; i++) {
52         if (env->exceptions & (1 << i)) {
53             switch (i) {
54             case EXCP_EXTERNAL:
55             case EXCP_DECR:
56                 if (msr_ee == 0)
57                     return 0;
58                 break;
59             case EXCP_PROGRAM:
60                 if (env->errors[EXCP_PROGRAM] == EXCP_FP &&
61                     msr_fe0 == 0 && msr_fe1 == 0)
62                     return 0;
63                 break;
64             default:
65                 break;
66             }
67             env->exception_index = i;
68             env->error_code = env->errors[i];
69             return 1;
70         }
71     }
72
73     return 0;
74 }
75
76 /*****************************************************************************/
77 /* PPC MMU emulation */
78 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
79                               int is_user, int is_softmmu);
80
81 /* Perform BAT hit & translation */
82 static int get_bat (CPUState *env, uint32_t *real, int *prot,
83                     uint32_t virtual, int rw, int type)
84 {
85     uint32_t *BATlt, *BATut, *BATu, *BATl;
86     uint32_t base, BEPIl, BEPIu, bl;
87     int i;
88     int ret = -1;
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     switch (type) {
97     case ACCESS_CODE:
98         BATlt = env->IBAT[1];
99         BATut = env->IBAT[0];
100         break;
101     default:
102         BATlt = env->DBAT[1];
103         BATut = env->DBAT[0];
104         break;
105     }
106 #if defined (DEBUG_BATS)
107     if (loglevel > 0) {
108         fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
109                type == ACCESS_CODE ? 'I' : 'D', virtual);
110     }
111 #endif
112     base = virtual & 0xFFFC0000;
113     for (i = 0; i < 4; i++) {
114         BATu = &BATut[i];
115         BATl = &BATlt[i];
116         BEPIu = *BATu & 0xF0000000;
117         BEPIl = *BATu & 0x0FFE0000;
118         bl = (*BATu & 0x00001FFC) << 15;
119 #if defined (DEBUG_BATS)
120         if (loglevel > 0) {
121             fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
122                     __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
123                     *BATu, *BATl);
124         }
125 #endif
126         if ((virtual & 0xF0000000) == BEPIu &&
127             ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
128             /* BAT matches */
129             if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
130                 (msr_pr == 1 && (*BATu & 0x00000001))) {
131                 /* Get physical address */
132                 *real = (*BATl & 0xF0000000) |
133                     ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
134                     (virtual & 0x0001F000);
135                 if (*BATl & 0x00000001)
136                     *prot = PROT_READ;
137                 if (*BATl & 0x00000002)
138                     *prot = PROT_WRITE | PROT_READ;
139 #if defined (DEBUG_BATS)
140                 if (loglevel > 0) {
141                     fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
142                             i, *real, *prot & PROT_READ ? 'R' : '-',
143                             *prot & PROT_WRITE ? 'W' : '-');
144                 }
145 #endif
146                 ret = 0;
147                 break;
148             }
149         }
150     }
151     if (ret < 0) {
152 #if defined (DEBUG_BATS)
153         printf("no BAT match for 0x%08x:\n", virtual);
154         for (i = 0; i < 4; i++) {
155             BATu = &BATut[i];
156             BATl = &BATlt[i];
157             BEPIu = *BATu & 0xF0000000;
158             BEPIl = *BATu & 0x0FFE0000;
159             bl = (*BATu & 0x00001FFC) << 15;
160             printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
161                    "0x%08x 0x%08x 0x%08x\n",
162                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
163                    *BATu, *BATl, BEPIu, BEPIl, bl);
164         }
165 #endif
166     }
167     /* No hit */
168     return ret;
169 }
170
171 /* PTE table lookup */
172 static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
173                      int h, int key, int rw)
174 {
175     uint32_t pte0, pte1, keep = 0, access = 0;
176     int i, good = -1, store = 0;
177     int ret = -1; /* No entry found */
178
179     for (i = 0; i < 8; i++) {
180         pte0 = ldl_raw(phys_ram_base + base + (i * 8));
181         pte1 =  ldl_raw(phys_ram_base + base + (i * 8) + 4);
182 #if defined (DEBUG_MMU)
183         if (loglevel > 0) {
184             fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
185                     "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
186                     pte0 >> 31, h, (pte0 >> 6) & 1, va);
187         }
188 #endif
189         /* Check validity and table match */
190         if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
191             /* Check vsid & api */
192             if ((pte0 & 0x7FFFFFBF) == va) {
193                 if (good == -1) {
194                     good = i;
195                     keep = pte1;
196                 } else {
197                     /* All matches should have equal RPN, WIMG & PP */
198                     if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
199                         if (loglevel > 0)
200                             fprintf(logfile, "Bad RPN/WIMG/PP\n");
201                         return -1;
202                     }
203                 }
204                 /* Check access rights */
205                 if (key == 0) {
206                     access = PROT_READ;
207                     if ((pte1 & 0x00000003) != 0x3)
208                         access |= PROT_WRITE;
209                 } else {
210                     switch (pte1 & 0x00000003) {
211                     case 0x0:
212                         access = 0;
213                         break;
214                     case 0x1:
215                     case 0x3:
216                         access = PROT_READ;
217                         break;
218                     case 0x2:
219                         access = PROT_READ | PROT_WRITE;
220                         break;
221                     }
222                 }
223                 if (ret < 0) {
224                     if ((rw == 0 && (access & PROT_READ)) ||
225                         (rw == 1 && (access & PROT_WRITE))) {
226 #if defined (DEBUG_MMU)
227                         if (loglevel > 0)
228                             fprintf(logfile, "PTE access granted !\n");
229 #endif
230                     good = i;
231                     keep = pte1;
232                     ret = 0;
233                     } else {
234                         /* Access right violation */
235                         ret = -2;
236 #if defined (DEBUG_MMU)
237                         if (loglevel > 0)
238                             fprintf(logfile, "PTE access rejected\n");
239 #endif
240                 }
241                     *prot = access;
242                 }
243             }
244         }
245     }
246     if (good != -1) {
247         *RPN = keep & 0xFFFFF000;
248 #if defined (DEBUG_MMU)
249         if (loglevel > 0) {
250             fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
251                *RPN, *prot, ret);
252         }
253 #endif
254         /* Update page flags */
255         if (!(keep & 0x00000100)) {
256             /* Access flag */
257             keep |= 0x00000100;
258             store = 1;
259         }
260             if (!(keep & 0x00000080)) {
261             if (rw && ret == 0) {
262                 /* Change flag */
263                 keep |= 0x00000080;
264                 store = 1;
265             } else {
266                 /* Force page fault for first write access */
267                 *prot &= ~PROT_WRITE;
268             }
269         }
270         if (store) {
271             stl_raw(phys_ram_base + base + (good * 8) + 4, keep);
272         }
273     }
274
275     return ret;
276 }
277
278 static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
279 {
280     return (sdr1 & 0xFFFF0000) | (hash & mask);
281 }
282
283 /* Perform segment based translation */
284 static int get_segment (CPUState *env, uint32_t *real, int *prot,
285                         uint32_t virtual, int rw, int type)
286 {
287     uint32_t pg_addr, sdr, ptem, vsid, pgidx;
288     uint32_t hash, mask;
289     uint32_t sr;
290     int key;
291     int ret = -1, ret2;
292
293     sr = env->sr[virtual >> 28];
294 #if defined (DEBUG_MMU)
295     if (loglevel > 0) {
296         fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
297                 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
298                 virtual, virtual >> 28, sr, env->nip,
299                 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
300     }
301 #endif
302     key = (((sr & 0x20000000) && msr_pr == 1) ||
303         ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
304     if ((sr & 0x80000000) == 0) {
305 #if defined (DEBUG_MMU)
306         if (loglevel > 0)
307             fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
308                     key, sr & 0x10000000);
309 #endif
310         /* Check if instruction fetch is allowed, if needed */
311         if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
312             /* Page address translation */
313             vsid = sr & 0x00FFFFFF;
314             pgidx = (virtual >> 12) & 0xFFFF;
315             sdr = env->sdr1;
316             hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
317             mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
318             pg_addr = get_pgaddr(sdr, hash, mask);
319             ptem = (vsid << 7) | (pgidx >> 10);
320 #if defined (DEBUG_MMU)
321             if (loglevel > 0) {
322                 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
323                         "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
324                         pg_addr);
325             }
326 #endif
327             /* Primary table lookup */
328             ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
329             if (ret < 0) {
330                 /* Secondary table lookup */
331                 hash = (~hash) & 0x01FFFFC0;
332                 pg_addr = get_pgaddr(sdr, hash, mask);
333 #if defined (DEBUG_MMU)
334                 if (virtual != 0xEFFFFFFF && loglevel > 0) {
335                     fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
336                             "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
337                             hash, pg_addr);
338                 }
339 #endif
340                 ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
341                 if (ret2 != -1)
342                     ret = ret2;
343             }
344         } else {
345 #if defined (DEBUG_MMU)
346             if (loglevel > 0)
347                 fprintf(logfile, "No access allowed\n");
348 #endif
349             ret = -3;
350         }
351     } else {
352 #if defined (DEBUG_MMU)
353         if (loglevel > 0)
354             fprintf(logfile, "direct store...\n");
355 #endif
356         /* Direct-store segment : absolutely *BUGGY* for now */
357         switch (type) {
358         case ACCESS_INT:
359             /* Integer load/store : only access allowed */
360             break;
361         case ACCESS_CODE:
362             /* No code fetch is allowed in direct-store areas */
363             return -4;
364         case ACCESS_FLOAT:
365             /* Floating point load/store */
366             return -4;
367         case ACCESS_RES:
368             /* lwarx, ldarx or srwcx. */
369             return -4;
370         case ACCESS_CACHE:
371             /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
372             /* Should make the instruction do no-op.
373              * As it already do no-op, it's quite easy :-)
374              */
375             *real = virtual;
376             return 0;
377         case ACCESS_EXT:
378             /* eciwx or ecowx */
379             return -4;
380         default:
381             if (logfile) {
382                 fprintf(logfile, "ERROR: instruction should not need "
383                         "address translation\n");
384             }
385             printf("ERROR: instruction should not need "
386                    "address translation\n");
387             return -4;
388         }
389         if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
390             *real = virtual;
391             ret = 2;
392         } else {
393             ret = -2;
394         }
395     }
396
397     return ret;
398 }
399
400 int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
401                           uint32_t address, int rw, int access_type)
402 {
403     int ret;
404
405     if (loglevel > 0) {
406         fprintf(logfile, "%s\n", __func__);
407     }
408     
409     if ((access_type == ACCESS_CODE && msr_ir == 0) || msr_dr == 0) {
410         /* No address translation */
411         *physical = address & ~0xFFF;
412         *prot = PROT_READ | PROT_WRITE;
413         ret = 0;
414     } else {
415         /* Try to find a BAT */
416         ret = get_bat(env, physical, prot, address, rw, access_type);
417         if (ret < 0) {
418             /* We didn't match any BAT entry */
419             ret = get_segment(env, physical, prot, address, rw, access_type);
420         }
421     }
422     if (loglevel > 0) {
423         fprintf(logfile, "%s address %08x => %08x\n",
424                 __func__, address, *physical);
425     }
426     
427     return ret;
428 }
429
430 #if defined(CONFIG_USER_ONLY) 
431 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
432 {
433     return addr;
434 }
435 #else
436 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
437 {
438     uint32_t phys_addr;
439     int prot;
440
441     if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
442         return -1;
443     return phys_addr;
444 }
445 #endif
446
447 #if !defined(CONFIG_USER_ONLY) 
448
449 #define MMUSUFFIX _mmu
450 #define GETPC() (__builtin_return_address(0))
451
452 #define SHIFT 0
453 #include "softmmu_template.h"
454
455 #define SHIFT 1
456 #include "softmmu_template.h"
457
458 #define SHIFT 2
459 #include "softmmu_template.h"
460
461 #define SHIFT 3
462 #include "softmmu_template.h"
463
464 /* try to fill the TLB and return an exception if error. If retaddr is
465    NULL, it means that the function was called in C code (i.e. not
466    from generated code or from helper.c) */
467 /* XXX: fix it to restore all registers */
468 void tlb_fill(unsigned long addr, int is_write, int is_user, void *retaddr)
469 {
470     TranslationBlock *tb;
471     CPUState *saved_env;
472     unsigned long pc;
473     int ret;
474
475     /* XXX: hack to restore env in all cases, even if not called from
476        generated code */
477     saved_env = env;
478     env = cpu_single_env;
479     {
480         unsigned long tlb_addrr, tlb_addrw;
481         int index;
482         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
483         tlb_addrr = env->tlb_read[is_user][index].address;
484         tlb_addrw = env->tlb_write[is_user][index].address;
485 #if 0
486         printf("%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
487                "(0x%08lx 0x%08lx)\n", __func__, env,
488                &env->tlb_read[is_user][index], index, addr,
489                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
490                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
491 #endif
492     }
493     ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
494     if (ret) {
495         if (retaddr) {
496             /* now we have a real cpu fault */
497             pc = (unsigned long)retaddr;
498             tb = tb_find_pc(pc);
499             if (tb) {
500                 /* the PC is inside the translated code. It means that we have
501                    a virtual CPU fault */
502                 cpu_restore_state(tb, env, pc, NULL);
503             }
504         }
505         do_queue_exception_err(env->exception_index, env->error_code);
506         do_process_exceptions();
507     }
508     {
509         unsigned long tlb_addrr, tlb_addrw;
510         int index;
511         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
512         tlb_addrr = env->tlb_read[is_user][index].address;
513         tlb_addrw = env->tlb_write[is_user][index].address;
514 #if 0
515         printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
516                "(0x%08lx 0x%08lx)\n", __func__, env,
517                &env->tlb_read[is_user][index], index, addr,
518                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
519                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
520 #endif
521     }
522     env = saved_env;
523 }
524
525 void cpu_ppc_init_mmu(CPUState *env)
526 {
527     /* Nothing to do: all translation are disabled */
528 }
529 #endif
530
531 /* Perform address translation */
532 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
533                               int is_user, int is_softmmu)
534 {
535     uint32_t physical;
536     int prot;
537     int exception = 0, error_code = 0;
538     int access_type;
539     int ret = 0;
540
541 //    printf("%s 0\n", __func__);
542     access_type = env->access_type;
543     if (env->user_mode_only) {
544         /* user mode only emulation */
545         ret = -2;
546         goto do_fault;
547     }
548     /* NASTY BUG workaround */
549     if (access_type == ACCESS_CODE && rw) {
550         printf("%s: ERROR WRITE CODE ACCESS\n", __func__);
551         access_type = ACCESS_INT;
552     }
553     ret = get_physical_address(env, &physical, &prot,
554                                address, rw, access_type);
555     if (ret == 0) {
556         ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
557                            is_user, is_softmmu);
558     } else if (ret < 0) {
559     do_fault:
560 #if defined (DEBUG_MMU)
561         if (loglevel > 0)
562             cpu_ppc_dump_state(env, logfile, 0);
563 #endif
564         if (access_type == ACCESS_CODE) {
565             exception = EXCP_ISI;
566             switch (ret) {
567             case -1:
568                 /* No matches in page tables */
569                 error_code = EXCP_ISI_TRANSLATE;
570                 break;
571             case -2:
572                 /* Access rights violation */
573                 error_code = EXCP_ISI_PROT;
574                 break;
575             case -3:
576                 /* No execute protection violation */
577                 error_code = EXCP_ISI_NOEXEC;
578                 break;
579             case -4:
580                 /* Direct store exception */
581                 /* No code fetch is allowed in direct-store areas */
582                 error_code = EXCP_ISI_DIRECT;
583                 break;
584             }
585         } else {
586             exception = EXCP_DSI;
587             switch (ret) {
588             case -1:
589                 /* No matches in page tables */
590                 error_code = EXCP_DSI_TRANSLATE;
591                 break;
592             case -2:
593                 /* Access rights violation */
594                 error_code = EXCP_DSI_PROT;
595                 break;
596             case -4:
597                 /* Direct store exception */
598                 switch (access_type) {
599                 case ACCESS_FLOAT:
600                     /* Floating point load/store */
601                     exception = EXCP_ALIGN;
602                     error_code = EXCP_ALIGN_FP;
603                     break;
604                 case ACCESS_RES:
605                     /* lwarx, ldarx or srwcx. */
606                     exception = EXCP_DSI;
607                     error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT;
608                     break;
609                 case ACCESS_EXT:
610                     /* eciwx or ecowx */
611                     exception = EXCP_DSI;
612                     error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT |
613                         EXCP_DSI_ECXW;
614                     break;
615                 default:
616                     printf("DSI: invalid exception (%d)\n", ret);
617                     exception = EXCP_PROGRAM;
618                     error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
619                     break;
620                 }
621             }
622             if (rw)
623                 error_code |= EXCP_DSI_STORE;
624             /* Store fault address */
625             env->spr[DAR] = address;
626         }
627 #if 0
628         printf("%s: set exception to %d %02x\n",
629                __func__, exception, error_code);
630 #endif
631         env->exception_index = exception;
632         env->error_code = error_code;
633         ret = 1;
634     }
635
636     return ret;
637 }
638
639 uint32_t _load_xer (CPUState *env)
640 {
641     return (xer_so << XER_SO) |
642         (xer_ov << XER_OV) |
643         (xer_ca << XER_CA) |
644         (xer_bc << XER_BC);
645 }
646
647 void _store_xer (CPUState *env, uint32_t value)
648 {
649     xer_so = (value >> XER_SO) & 0x01;
650     xer_ov = (value >> XER_OV) & 0x01;
651     xer_ca = (value >> XER_CA) & 0x01;
652     xer_bc = (value >> XER_BC) & 0x1f;
653 }
654
655 uint32_t _load_msr (CPUState *env)
656 {
657     return (msr_pow << MSR_POW) |
658         (msr_ile << MSR_ILE) |
659         (msr_ee << MSR_EE) |
660         (msr_pr << MSR_PR) |
661         (msr_fp << MSR_FP) |
662         (msr_me << MSR_ME) |
663         (msr_fe0 << MSR_FE0) |
664         (msr_se << MSR_SE) |
665         (msr_be << MSR_BE) |
666         (msr_fe1 << MSR_FE1) |
667         (msr_ip << MSR_IP) |
668         (msr_ir << MSR_IR) |
669         (msr_dr << MSR_DR) |
670         (msr_ri << MSR_RI) |
671         (msr_le << MSR_LE);
672 }
673
674 void _store_msr (CPUState *env, uint32_t value)
675 {
676     if (((value >> MSR_IR) & 0x01) != msr_ir ||
677         ((value >> MSR_DR) & 0x01) != msr_dr) {
678         /* Flush all tlb when changing translation mode or privilege level */
679         tlb_flush(env, 1);
680     }
681     msr_pow = (value >> MSR_POW) & 0x03;
682     msr_ile = (value >> MSR_ILE) & 0x01;
683     msr_ee = (value >> MSR_EE) & 0x01;
684     msr_pr = (value >> MSR_PR) & 0x01;
685     msr_fp = (value >> MSR_FP) & 0x01;
686     msr_me = (value >> MSR_ME) & 0x01;
687     msr_fe0 = (value >> MSR_FE0) & 0x01;
688     msr_se = (value >> MSR_SE) & 0x01;
689     msr_be = (value >> MSR_BE) & 0x01;
690     msr_fe1 = (value >> MSR_FE1) & 0x01;
691     msr_ip = (value >> MSR_IP) & 0x01;
692     msr_ir = (value >> MSR_IR) & 0x01;
693     msr_dr = (value >> MSR_DR) & 0x01;
694     msr_ri = (value >> MSR_RI) & 0x01;
695     msr_le = (value >> MSR_LE) & 0x01;
696 }
697
698 void do_interrupt (CPUState *env)
699 {
700 #if defined (CONFIG_USER_ONLY)
701     env->exception_index |= 0x100;
702 #else
703     uint32_t msr;
704     int excp = env->exception_index;
705
706     /* Dequeue PPC exceptions */
707     if (excp < EXCP_PPC_MAX)
708         env->exceptions &= ~(1 << excp);
709     msr = _load_msr(env);
710 #if defined (DEBUG_EXCEPTIONS)
711     if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) 
712     {
713         if (loglevel > 0) {
714             fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
715                     env->nip, excp << 8, env->error_code);
716     }
717         if (loglevel > 0)
718             cpu_ppc_dump_state(env, logfile, 0);
719     }
720 #endif
721     /* Generate informations in save/restore registers */
722     switch (excp) {
723     case EXCP_OFCALL:
724 #if defined (USE_OPEN_FIRMWARE)
725         env->gpr[3] = OF_client_entry((void *)env->gpr[3]);
726 #endif
727         return;
728     case EXCP_RTASCALL:
729 #if defined (USE_OPEN_FIRMWARE)
730         printf("RTAS call !\n");
731         env->gpr[3] = RTAS_entry((void *)env->gpr[3]);
732         printf("RTAS call done\n");
733 #endif
734         return;
735     case EXCP_NONE:
736         /* Do nothing */
737 #if defined (DEBUG_EXCEPTIONS)
738         printf("%s: escape EXCP_NONE\n", __func__);
739 #endif
740         return;
741     case EXCP_RESET:
742         if (msr_ip)
743             excp += 0xFFC00;
744         goto store_next;
745     case EXCP_MACHINE_CHECK:
746         if (msr_me == 0) {
747             printf("Machine check exception while not allowed !\n");
748             if (loglevel) {
749                 fprintf(logfile,
750                         "Machine check exception while not allowed !\n");
751         }
752             abort();
753     }
754         msr_me = 0;
755         break;
756     case EXCP_DSI:
757         /* Store exception cause */
758         /* data location address has been stored
759          * when the fault has been detected
760      */
761         msr &= ~0xFFFF0000;
762         env->spr[DSISR] = 0;
763         if (env->error_code &  EXCP_DSI_TRANSLATE)
764             env->spr[DSISR] |= 0x40000000;
765         else if (env->error_code & EXCP_DSI_PROT)
766             env->spr[DSISR] |= 0x08000000;
767         else if (env->error_code & EXCP_DSI_NOTSUP) {
768             env->spr[DSISR] |= 0x80000000;
769             if (env->error_code & EXCP_DSI_DIRECT)
770                 env->spr[DSISR] |= 0x04000000;
771         }
772         if (env->error_code & EXCP_DSI_STORE)
773             env->spr[DSISR] |= 0x02000000;
774         if ((env->error_code & 0xF) == EXCP_DSI_DABR)
775             env->spr[DSISR] |= 0x00400000;
776         if (env->error_code & EXCP_DSI_ECXW)
777             env->spr[DSISR] |= 0x00100000;
778 #if defined (DEBUG_EXCEPTIONS)
779         if (loglevel) {
780             fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
781                     env->spr[DSISR], env->spr[DAR]);
782         } else {
783             printf("DSI exception: DSISR=0x%08x, DAR=0x%08x nip=0x%08x\n",
784                    env->spr[DSISR], env->spr[DAR], env->nip);
785         }
786 #endif
787         goto store_next;
788     case EXCP_ISI:
789         /* Store exception cause */
790         msr &= ~0xFFFF0000;
791         if (env->error_code == EXCP_ISI_TRANSLATE)
792             msr |= 0x40000000;
793         else if (env->error_code == EXCP_ISI_NOEXEC ||
794                  env->error_code == EXCP_ISI_GUARD ||
795                  env->error_code == EXCP_ISI_DIRECT)
796             msr |= 0x10000000;
797         else
798             msr |= 0x08000000;
799 #if defined (DEBUG_EXCEPTIONS)
800         if (loglevel) {
801             fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
802                     msr, env->nip);
803         } else {
804             printf("ISI exception: msr=0x%08x, nip=0x%08x tbl:0x%08x\n",
805                    msr, env->nip, env->spr[V_TBL]);
806         }
807 #endif
808         goto store_next;
809     case EXCP_EXTERNAL:
810         if (msr_ee == 0) {
811 #if defined (DEBUG_EXCEPTIONS)
812             if (loglevel > 0) {
813                 fprintf(logfile, "Skipping hardware interrupt\n");
814     }
815 #endif
816             /* Requeue it */
817             do_queue_exception(EXCP_EXTERNAL);
818             return;
819             }
820         goto store_next;
821     case EXCP_ALIGN:
822         /* Store exception cause */
823         /* Get rS/rD and rA from faulting opcode */
824         env->spr[DSISR] |=
825             (ldl_code((void *)(env->nip - 4)) & 0x03FF0000) >> 16;
826         /* data location address has been stored
827          * when the fault has been detected
828          */
829         goto store_current;
830     case EXCP_PROGRAM:
831         msr &= ~0xFFFF0000;
832         switch (env->error_code & ~0xF) {
833         case EXCP_FP:
834             if (msr_fe0 == 0 && msr_fe1 == 0) {
835 #if defined (DEBUG_EXCEPTIONS)
836                 printf("Ignore floating point exception\n");
837 #endif
838                 return;
839         }
840             msr |= 0x00100000;
841             /* Set FX */
842             env->fpscr[7] |= 0x8;
843             /* Finally, update FEX */
844             if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
845                 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
846                 env->fpscr[7] |= 0x4;
847         break;
848         case EXCP_INVAL:
849             printf("Invalid instruction at 0x%08x\n", env->nip);
850             msr |= 0x00080000;
851         break;
852         case EXCP_PRIV:
853             msr |= 0x00040000;
854         break;
855         case EXCP_TRAP:
856             msr |= 0x00020000;
857             break;
858         default:
859             /* Should never occur */
860         break;
861     }
862         msr |= 0x00010000;
863         goto store_current;
864     case EXCP_NO_FP:
865         goto store_current;
866     case EXCP_DECR:
867         if (msr_ee == 0) {
868             /* Requeue it */
869             do_queue_exception(EXCP_DECR);
870             return;
871         }
872         goto store_next;
873     case EXCP_SYSCALL:
874 #if defined (DEBUG_EXCEPTIONS)
875         if (msr_pr) {
876             if (loglevel) {
877                 fprintf(logfile, "syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n",
878                         env->gpr[0], env->gpr[3], env->gpr[4],
879                         env->gpr[5], env->gpr[6]);
880             } else {
881                 printf("syscall %d from 0x%08x 0x%08x 0x%08x 0x%08x 0x%08x\n",
882                        env->gpr[0], env->nip, env->gpr[3], env->gpr[4],
883                        env->gpr[5], env->gpr[6]);
884             }
885         }
886 #endif
887         goto store_next;
888     case EXCP_TRACE:
889         goto store_next;
890     case EXCP_FP_ASSIST:
891         goto store_next;
892     case EXCP_MTMSR:
893         /* Nothing to do */
894         return;
895     case EXCP_BRANCH:
896         /* Nothing to do */
897         return;
898     case EXCP_RFI:
899         /* Restore user-mode state */
900         tb_flush(env);
901 #if defined (DEBUG_EXCEPTIONS)
902         if (msr_pr == 1)
903             printf("Return from exception => 0x%08x\n", (uint32_t)env->nip);
904 #endif
905         return;
906     store_current:
907         /* SRR0 is set to current instruction */
908         env->spr[SRR0] = (uint32_t)env->nip - 4;
909         break;
910     store_next:
911         /* SRR0 is set to next instruction */
912         env->spr[SRR0] = (uint32_t)env->nip;
913         break;
914     }
915     env->spr[SRR1] = msr;
916     /* reload MSR with correct bits */
917     msr_pow = 0;
918     msr_ee = 0;
919     msr_pr = 0;
920     msr_fp = 0;
921     msr_fe0 = 0;
922     msr_se = 0;
923     msr_be = 0;
924     msr_fe1 = 0;
925     msr_ir = 0;
926     msr_dr = 0;
927     msr_ri = 0;
928     msr_le = msr_ile;
929     /* Jump to handler */
930     env->nip = excp << 8;
931     env->exception_index = EXCP_NONE;
932     /* Invalidate all TLB as we may have changed translation mode */
933     tlb_flush(env, 1);
934     /* ensure that no TB jump will be modified as
935        the program flow was changed */
936 #ifdef __sparc__
937     tmp_T0 = 0;
938 #else
939     T0 = 0;
940 #endif
941 #endif
942 }