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