preliminary patch to support more PowerPC CPUs (Jocelyn Mayer)
[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 "exec.h"
21
22 //#define DEBUG_MMU
23 //#define DEBUG_BATS
24 //#define DEBUG_EXCEPTIONS
25 /* accurate but slower TLB flush in exceptions */
26 //#define ACCURATE_TLB_FLUSH
27
28 /*****************************************************************************/
29 /* PowerPC MMU emulation */
30
31 /* Perform BAT hit & translation */
32 static int get_bat (CPUState *env, uint32_t *real, int *prot,
33                     uint32_t virtual, int rw, int type)
34 {
35     uint32_t *BATlt, *BATut, *BATu, *BATl;
36     uint32_t base, BEPIl, BEPIu, bl;
37     int i;
38     int ret = -1;
39
40 #if defined (DEBUG_BATS)
41     if (loglevel > 0) {
42         fprintf(logfile, "%s: %cBAT v 0x%08x\n", __func__,
43                type == ACCESS_CODE ? 'I' : 'D', virtual);
44     }
45 #endif
46     switch (type) {
47     case ACCESS_CODE:
48         BATlt = env->IBAT[1];
49         BATut = env->IBAT[0];
50         break;
51     default:
52         BATlt = env->DBAT[1];
53         BATut = env->DBAT[0];
54         break;
55     }
56 #if defined (DEBUG_BATS)
57     if (loglevel > 0) {
58         fprintf(logfile, "%s...: %cBAT v 0x%08x\n", __func__,
59                type == ACCESS_CODE ? 'I' : 'D', virtual);
60     }
61 #endif
62     base = virtual & 0xFFFC0000;
63     for (i = 0; i < 4; i++) {
64         BATu = &BATut[i];
65         BATl = &BATlt[i];
66         BEPIu = *BATu & 0xF0000000;
67         BEPIl = *BATu & 0x0FFE0000;
68         bl = (*BATu & 0x00001FFC) << 15;
69 #if defined (DEBUG_BATS)
70         if (loglevel > 0) {
71             fprintf(logfile, "%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x\n",
72                     __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
73                     *BATu, *BATl);
74         }
75 #endif
76         if ((virtual & 0xF0000000) == BEPIu &&
77             ((virtual & 0x0FFE0000) & ~bl) == BEPIl) {
78             /* BAT matches */
79             if ((msr_pr == 0 && (*BATu & 0x00000002)) ||
80                 (msr_pr == 1 && (*BATu & 0x00000001))) {
81                 /* Get physical address */
82                 *real = (*BATl & 0xF0000000) |
83                     ((virtual & 0x0FFE0000 & bl) | (*BATl & 0x0FFE0000)) |
84                     (virtual & 0x0001F000);
85                 if (*BATl & 0x00000001)
86                     *prot = PAGE_READ;
87                 if (*BATl & 0x00000002)
88                     *prot = PAGE_WRITE | PAGE_READ;
89 #if defined (DEBUG_BATS)
90                 if (loglevel > 0) {
91                     fprintf(logfile, "BAT %d match: r 0x%08x prot=%c%c\n",
92                             i, *real, *prot & PAGE_READ ? 'R' : '-',
93                             *prot & PAGE_WRITE ? 'W' : '-');
94                 }
95 #endif
96                 ret = 0;
97                 break;
98             }
99         }
100     }
101     if (ret < 0) {
102 #if defined (DEBUG_BATS)
103         printf("no BAT match for 0x%08x:\n", virtual);
104         for (i = 0; i < 4; i++) {
105             BATu = &BATut[i];
106             BATl = &BATlt[i];
107             BEPIu = *BATu & 0xF0000000;
108             BEPIl = *BATu & 0x0FFE0000;
109             bl = (*BATu & 0x00001FFC) << 15;
110             printf("%s: %cBAT%d v 0x%08x BATu 0x%08x BATl 0x%08x \n\t"
111                    "0x%08x 0x%08x 0x%08x\n",
112                    __func__, type == ACCESS_CODE ? 'I' : 'D', i, virtual,
113                    *BATu, *BATl, BEPIu, BEPIl, bl);
114         }
115 #endif
116     }
117     /* No hit */
118     return ret;
119 }
120
121 /* PTE table lookup */
122 static int find_pte (uint32_t *RPN, int *prot, uint32_t base, uint32_t va,
123                      int h, int key, int rw)
124 {
125     uint32_t pte0, pte1, keep = 0, access = 0;
126     int i, good = -1, store = 0;
127     int ret = -1; /* No entry found */
128
129     for (i = 0; i < 8; i++) {
130         pte0 = ldl_phys(base + (i * 8));
131         pte1 =  ldl_phys(base + (i * 8) + 4);
132 #if defined (DEBUG_MMU)
133         if (loglevel > 0) {
134             fprintf(logfile, "Load pte from 0x%08x => 0x%08x 0x%08x "
135                     "%d %d %d 0x%08x\n", base + (i * 8), pte0, pte1,
136                     pte0 >> 31, h, (pte0 >> 6) & 1, va);
137         }
138 #endif
139         /* Check validity and table match */
140         if (pte0 & 0x80000000 && (h == ((pte0 >> 6) & 1))) {
141             /* Check vsid & api */
142             if ((pte0 & 0x7FFFFFBF) == va) {
143                 if (good == -1) {
144                     good = i;
145                     keep = pte1;
146                 } else {
147                     /* All matches should have equal RPN, WIMG & PP */
148                     if ((keep & 0xFFFFF07B) != (pte1 & 0xFFFFF07B)) {
149                         if (loglevel > 0)
150                             fprintf(logfile, "Bad RPN/WIMG/PP\n");
151                         return -1;
152                     }
153                 }
154                 /* Check access rights */
155                 if (key == 0) {
156                     access = PAGE_READ;
157                     if ((pte1 & 0x00000003) != 0x3)
158                         access |= PAGE_WRITE;
159                 } else {
160                     switch (pte1 & 0x00000003) {
161                     case 0x0:
162                         access = 0;
163                         break;
164                     case 0x1:
165                     case 0x3:
166                         access = PAGE_READ;
167                         break;
168                     case 0x2:
169                         access = PAGE_READ | PAGE_WRITE;
170                         break;
171                     }
172                 }
173                 if (ret < 0) {
174                     if ((rw == 0 && (access & PAGE_READ)) ||
175                         (rw == 1 && (access & PAGE_WRITE))) {
176 #if defined (DEBUG_MMU)
177                         if (loglevel > 0)
178                             fprintf(logfile, "PTE access granted !\n");
179 #endif
180                         good = i;
181                         keep = pte1;
182                         ret = 0;
183                     } else {
184                         /* Access right violation */
185                         ret = -2;
186 #if defined (DEBUG_MMU)
187                         if (loglevel > 0)
188                             fprintf(logfile, "PTE access rejected\n");
189 #endif
190                     }
191                     *prot = access;
192                 }
193             }
194         }
195     }
196     if (good != -1) {
197         *RPN = keep & 0xFFFFF000;
198 #if defined (DEBUG_MMU)
199         if (loglevel > 0) {
200             fprintf(logfile, "found PTE at addr 0x%08x prot=0x%01x ret=%d\n",
201                *RPN, *prot, ret);
202         }
203 #endif
204         /* Update page flags */
205         if (!(keep & 0x00000100)) {
206             /* Access flag */
207             keep |= 0x00000100;
208             store = 1;
209         }
210         if (!(keep & 0x00000080)) {
211             if (rw && ret == 0) {
212                 /* Change flag */
213                 keep |= 0x00000080;
214                 store = 1;
215             } else {
216                 /* Force page fault for first write access */
217                 *prot &= ~PAGE_WRITE;
218             }
219         }
220         if (store) {
221             stl_phys_notdirty(base + (good * 8) + 4, keep);
222         }
223     }
224
225     return ret;
226 }
227
228 static inline uint32_t get_pgaddr (uint32_t sdr1, uint32_t hash, uint32_t mask)
229 {
230     return (sdr1 & 0xFFFF0000) | (hash & mask);
231 }
232
233 /* Perform segment based translation */
234 static int get_segment (CPUState *env, uint32_t *real, int *prot,
235                         uint32_t virtual, int rw, int type)
236 {
237     uint32_t pg_addr, sdr, ptem, vsid, pgidx;
238     uint32_t hash, mask;
239     uint32_t sr;
240     int key;
241     int ret = -1, ret2;
242
243     sr = env->sr[virtual >> 28];
244 #if defined (DEBUG_MMU)
245     if (loglevel > 0) {
246         fprintf(logfile, "Check segment v=0x%08x %d 0x%08x nip=0x%08x "
247                 "lr=0x%08x ir=%d dr=%d pr=%d %d t=%d\n",
248                 virtual, virtual >> 28, sr, env->nip,
249                 env->lr, msr_ir, msr_dr, msr_pr, rw, type);
250     }
251 #endif
252     key = (((sr & 0x20000000) && msr_pr == 1) ||
253         ((sr & 0x40000000) && msr_pr == 0)) ? 1 : 0;
254     if ((sr & 0x80000000) == 0) {
255 #if defined (DEBUG_MMU)
256     if (loglevel > 0) 
257             fprintf(logfile, "pte segment: key=%d n=0x%08x\n",
258                     key, sr & 0x10000000);
259 #endif
260         /* Check if instruction fetch is allowed, if needed */
261         if (type != ACCESS_CODE || (sr & 0x10000000) == 0) {
262             /* Page address translation */
263             vsid = sr & 0x00FFFFFF;
264             pgidx = (virtual >> 12) & 0xFFFF;
265             sdr = env->sdr1;
266             hash = ((vsid ^ pgidx) & 0x0007FFFF) << 6;
267             mask = ((sdr & 0x000001FF) << 16) | 0xFFC0;
268             pg_addr = get_pgaddr(sdr, hash, mask);
269             ptem = (vsid << 7) | (pgidx >> 10);
270 #if defined (DEBUG_MMU)
271             if (loglevel > 0) {
272                 fprintf(logfile, "0 sdr1=0x%08x vsid=0x%06x api=0x%04x "
273                         "hash=0x%07x pg_addr=0x%08x\n", sdr, vsid, pgidx, hash,
274                         pg_addr);
275             }
276 #endif
277             /* Primary table lookup */
278             ret = find_pte(real, prot, pg_addr, ptem, 0, key, rw);
279             if (ret < 0) {
280                 /* Secondary table lookup */
281                 hash = (~hash) & 0x01FFFFC0;
282                 pg_addr = get_pgaddr(sdr, hash, mask);
283 #if defined (DEBUG_MMU)
284                 if (virtual != 0xEFFFFFFF && loglevel > 0) {
285                     fprintf(logfile, "1 sdr1=0x%08x vsid=0x%06x api=0x%04x "
286                             "hash=0x%05x pg_addr=0x%08x\n", sdr, vsid, pgidx,
287                             hash, pg_addr);
288                 }
289 #endif
290                 ret2 = find_pte(real, prot, pg_addr, ptem, 1, key, rw);
291                 if (ret2 != -1)
292                     ret = ret2;
293             }
294         } else {
295 #if defined (DEBUG_MMU)
296             if (loglevel > 0)
297                 fprintf(logfile, "No access allowed\n");
298 #endif
299             ret = -3;
300         }
301     } else {
302 #if defined (DEBUG_MMU)
303         if (loglevel > 0)
304             fprintf(logfile, "direct store...\n");
305 #endif
306         /* Direct-store segment : absolutely *BUGGY* for now */
307         switch (type) {
308         case ACCESS_INT:
309             /* Integer load/store : only access allowed */
310             break;
311         case ACCESS_CODE:
312             /* No code fetch is allowed in direct-store areas */
313             return -4;
314         case ACCESS_FLOAT:
315             /* Floating point load/store */
316             return -4;
317         case ACCESS_RES:
318             /* lwarx, ldarx or srwcx. */
319             return -4;
320         case ACCESS_CACHE:
321             /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
322             /* Should make the instruction do no-op.
323              * As it already do no-op, it's quite easy :-)
324              */
325             *real = virtual;
326             return 0;
327         case ACCESS_EXT:
328             /* eciwx or ecowx */
329             return -4;
330         default:
331             if (logfile) {
332                 fprintf(logfile, "ERROR: instruction should not need "
333                         "address translation\n");
334             }
335             printf("ERROR: instruction should not need "
336                    "address translation\n");
337             return -4;
338         }
339         if ((rw == 1 || key != 1) && (rw == 0 || key != 0)) {
340             *real = virtual;
341             ret = 2;
342         } else {
343             ret = -2;
344         }
345     }
346
347     return ret;
348 }
349
350 int get_physical_address (CPUState *env, uint32_t *physical, int *prot,
351                           uint32_t address, int rw, int access_type)
352 {
353     int ret;
354 #if 0
355     if (loglevel > 0) {
356         fprintf(logfile, "%s\n", __func__);
357     }
358 #endif    
359     if ((access_type == ACCESS_CODE && msr_ir == 0) ||
360         (access_type != ACCESS_CODE && msr_dr == 0)) {
361         /* No address translation */
362         *physical = address & ~0xFFF;
363         *prot = PAGE_READ | PAGE_WRITE;
364         ret = 0;
365     } else {
366         /* Try to find a BAT */
367         ret = get_bat(env, physical, prot, address, rw, access_type);
368         if (ret < 0) {
369             /* We didn't match any BAT entry */
370             ret = get_segment(env, physical, prot, address, rw, access_type);
371         }
372     }
373 #if 0
374     if (loglevel > 0) {
375         fprintf(logfile, "%s address %08x => %08x\n",
376                 __func__, address, *physical);
377     }
378 #endif    
379     return ret;
380 }
381
382 #if defined(CONFIG_USER_ONLY) 
383 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
384 {
385     return addr;
386 }
387 #else
388 target_ulong cpu_get_phys_page_debug(CPUState *env, target_ulong addr)
389 {
390     uint32_t phys_addr;
391     int prot;
392
393     if (get_physical_address(env, &phys_addr, &prot, addr, 0, ACCESS_INT) != 0)
394         return -1;
395     return phys_addr;
396 }
397 #endif
398
399 #if !defined(CONFIG_USER_ONLY) 
400
401 #define MMUSUFFIX _mmu
402 #define GETPC() (__builtin_return_address(0))
403
404 #define SHIFT 0
405 #include "softmmu_template.h"
406
407 #define SHIFT 1
408 #include "softmmu_template.h"
409
410 #define SHIFT 2
411 #include "softmmu_template.h"
412
413 #define SHIFT 3
414 #include "softmmu_template.h"
415
416 /* try to fill the TLB and return an exception if error. If retaddr is
417    NULL, it means that the function was called in C code (i.e. not
418    from generated code or from helper.c) */
419 /* XXX: fix it to restore all registers */
420 void tlb_fill(target_ulong addr, int is_write, int is_user, void *retaddr)
421 {
422     TranslationBlock *tb;
423     CPUState *saved_env;
424     unsigned long pc;
425     int ret;
426
427     /* XXX: hack to restore env in all cases, even if not called from
428        generated code */
429     saved_env = env;
430     env = cpu_single_env;
431 #if 0
432     {
433         unsigned long tlb_addrr, tlb_addrw;
434         int index;
435         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
436         tlb_addrr = env->tlb_read[is_user][index].address;
437         tlb_addrw = env->tlb_write[is_user][index].address;
438         if (loglevel) {
439             fprintf(logfile,
440                     "%s 1 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
441                "(0x%08lx 0x%08lx)\n", __func__, env,
442                &env->tlb_read[is_user][index], index, addr,
443                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
444                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
445         }
446     }
447 #endif
448     ret = cpu_ppc_handle_mmu_fault(env, addr, is_write, is_user, 1);
449     if (ret) {
450         if (retaddr) {
451             /* now we have a real cpu fault */
452             pc = (unsigned long)retaddr;
453             tb = tb_find_pc(pc);
454             if (tb) {
455                 /* the PC is inside the translated code. It means that we have
456                    a virtual CPU fault */
457                 cpu_restore_state(tb, env, pc, NULL);
458             }
459         }
460         do_raise_exception_err(env->exception_index, env->error_code);
461     }
462 #if 0
463     {
464         unsigned long tlb_addrr, tlb_addrw;
465         int index;
466         index = (addr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
467         tlb_addrr = env->tlb_read[is_user][index].address;
468         tlb_addrw = env->tlb_write[is_user][index].address;
469         printf("%s 2 %p %p idx=%d addr=0x%08lx tbl_addr=0x%08lx 0x%08lx "
470                "(0x%08lx 0x%08lx)\n", __func__, env,
471                &env->tlb_read[is_user][index], index, addr,
472                tlb_addrr, tlb_addrw, addr & TARGET_PAGE_MASK,
473                tlb_addrr & (TARGET_PAGE_MASK | TLB_INVALID_MASK));
474     }
475 #endif
476     env = saved_env;
477 }
478
479 void cpu_ppc_init_mmu(CPUState *env)
480 {
481     /* Nothing to do: all translation are disabled */
482 }
483 #endif
484
485 /* Perform address translation */
486 int cpu_ppc_handle_mmu_fault (CPUState *env, uint32_t address, int rw,
487                               int is_user, int is_softmmu)
488 {
489     uint32_t physical;
490     int prot;
491     int exception = 0, error_code = 0;
492     int access_type;
493     int ret = 0;
494
495     if (rw == 2) {
496         /* code access */
497         rw = 0;
498         access_type = ACCESS_CODE;
499     } else {
500         /* data access */
501         /* XXX: put correct access by using cpu_restore_state()
502            correctly */
503         access_type = ACCESS_INT;
504         //        access_type = env->access_type;
505     }
506     if (env->user_mode_only) {
507         /* user mode only emulation */
508         ret = -2;
509         goto do_fault;
510     }
511     ret = get_physical_address(env, &physical, &prot,
512                                address, rw, access_type);
513     if (ret == 0) {
514         ret = tlb_set_page(env, address & ~0xFFF, physical, prot,
515                            is_user, is_softmmu);
516     } else if (ret < 0) {
517     do_fault:
518 #if defined (DEBUG_MMU)
519         if (loglevel > 0)
520             cpu_dump_state(env, logfile, fprintf, 0);
521 #endif
522         if (access_type == ACCESS_CODE) {
523             exception = EXCP_ISI;
524             switch (ret) {
525             case -1:
526                 /* No matches in page tables */
527                 error_code = EXCP_ISI_TRANSLATE;
528                 break;
529             case -2:
530                 /* Access rights violation */
531                 error_code = EXCP_ISI_PROT;
532                 break;
533             case -3:
534                 /* No execute protection violation */
535                 error_code = EXCP_ISI_NOEXEC;
536                 break;
537             case -4:
538                 /* Direct store exception */
539                 /* No code fetch is allowed in direct-store areas */
540                 error_code = EXCP_ISI_DIRECT;
541                 break;
542             }
543         } else {
544             exception = EXCP_DSI;
545             switch (ret) {
546             case -1:
547                 /* No matches in page tables */
548                 error_code = EXCP_DSI_TRANSLATE;
549                 break;
550             case -2:
551                 /* Access rights violation */
552                 error_code = EXCP_DSI_PROT;
553                 break;
554             case -4:
555                 /* Direct store exception */
556                 switch (access_type) {
557                 case ACCESS_FLOAT:
558                     /* Floating point load/store */
559                     exception = EXCP_ALIGN;
560                     error_code = EXCP_ALIGN_FP;
561                     break;
562                 case ACCESS_RES:
563                     /* lwarx, ldarx or srwcx. */
564                     exception = EXCP_DSI;
565                     error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT;
566                     break;
567                 case ACCESS_EXT:
568                     /* eciwx or ecowx */
569                     exception = EXCP_DSI;
570                     error_code = EXCP_DSI_NOTSUP | EXCP_DSI_DIRECT |
571                         EXCP_DSI_ECXW;
572                     break;
573                 default:
574                     printf("DSI: invalid exception (%d)\n", ret);
575                     exception = EXCP_PROGRAM;
576                     error_code = EXCP_INVAL | EXCP_INVAL_INVAL;
577                     break;
578                 }
579             }
580             if (rw)
581                 error_code |= EXCP_DSI_STORE;
582             /* Store fault address */
583             env->spr[SPR_DAR] = address;
584         }
585 #if 0
586         printf("%s: set exception to %d %02x\n",
587                __func__, exception, error_code);
588 #endif
589         env->exception_index = exception;
590         env->error_code = error_code;
591         ret = 1;
592     }
593     return ret;
594 }
595
596 /*****************************************************************************/
597 /* BATs management */
598 #if !defined(FLUSH_ALL_TLBS)
599 static inline void do_invalidate_BAT (CPUPPCState *env,
600                                       target_ulong BATu, target_ulong mask)
601 {
602     target_ulong base, end, page;
603     base = BATu & ~0x0001FFFF;
604     end = base + mask + 0x00020000;
605 #if defined (DEBUG_BATS)
606     if (loglevel != 0)
607         fprintf(logfile, "Flush BAT from %08x to %08x (%08x)\n", base, end, mask);
608 #endif
609     for (page = base; page != end; page += TARGET_PAGE_SIZE)
610         tlb_flush_page(env, page);
611 #if defined (DEBUG_BATS)
612     if (loglevel != 0)
613         fprintf(logfile, "Flush done\n");
614 #endif
615 }
616 #endif
617
618 static inline void dump_store_bat (CPUPPCState *env, char ID, int ul, int nr,
619                                    target_ulong value)
620 {
621 #if defined (DEBUG_BATS)
622     if (loglevel != 0) {
623         fprintf(logfile, "Set %cBAT%d%c to 0x%08lx (0x%08lx)\n",
624                 ID, nr, ul == 0 ? 'u' : 'l', (unsigned long)value,
625                 (unsigned long)env->nip);
626     }
627 #endif
628 }
629
630 target_ulong do_load_ibatu (CPUPPCState *env, int nr)
631 {
632     return env->IBAT[0][nr];
633 }
634
635 target_ulong do_load_ibatl (CPUPPCState *env, int nr)
636 {
637     return env->IBAT[1][nr];
638 }
639
640 void do_store_ibatu (CPUPPCState *env, int nr, target_ulong value)
641 {
642     target_ulong mask;
643
644     dump_store_bat(env, 'I', 0, nr, value);
645     if (env->IBAT[0][nr] != value) {
646         mask = (value << 15) & 0x0FFE0000UL;
647 #if !defined(FLUSH_ALL_TLBS)
648         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
649 #endif
650         /* When storing valid upper BAT, mask BEPI and BRPN
651          * and invalidate all TLBs covered by this BAT
652          */
653         mask = (value << 15) & 0x0FFE0000UL;
654         env->IBAT[0][nr] = (value & 0x00001FFFUL) |
655             (value & ~0x0001FFFFUL & ~mask);
656         env->IBAT[1][nr] = (env->IBAT[1][nr] & 0x0000007B) |
657             (env->IBAT[1][nr] & ~0x0001FFFF & ~mask);
658 #if !defined(FLUSH_ALL_TLBS)
659         do_invalidate_BAT(env, env->IBAT[0][nr], mask);
660 #endif
661 #if defined(FLUSH_ALL_TLBS)
662         tlb_flush(env, 1);
663 #endif
664     }
665 }
666
667 void do_store_ibatl (CPUPPCState *env, int nr, target_ulong value)
668 {
669     dump_store_bat(env, 'I', 1, nr, value);
670     env->IBAT[1][nr] = value;
671 }
672
673 target_ulong do_load_dbatu (CPUPPCState *env, int nr)
674 {
675     return env->DBAT[0][nr];
676 }
677
678 target_ulong do_load_dbatl (CPUPPCState *env, int nr)
679 {
680     return env->DBAT[1][nr];
681 }
682
683 void do_store_dbatu (CPUPPCState *env, int nr, target_ulong value)
684 {
685     target_ulong mask;
686
687     dump_store_bat(env, 'D', 0, nr, value);
688     if (env->DBAT[0][nr] != value) {
689         /* When storing valid upper BAT, mask BEPI and BRPN
690          * and invalidate all TLBs covered by this BAT
691          */
692         mask = (value << 15) & 0x0FFE0000UL;
693 #if !defined(FLUSH_ALL_TLBS)
694         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
695 #endif
696         mask = (value << 15) & 0x0FFE0000UL;
697         env->DBAT[0][nr] = (value & 0x00001FFFUL) |
698             (value & ~0x0001FFFFUL & ~mask);
699         env->DBAT[1][nr] = (env->DBAT[1][nr] & 0x0000007B) |
700             (env->DBAT[1][nr] & ~0x0001FFFF & ~mask);
701 #if !defined(FLUSH_ALL_TLBS)
702         do_invalidate_BAT(env, env->DBAT[0][nr], mask);
703 #else
704         tlb_flush(env, 1);
705 #endif
706     }
707 }
708
709 void do_store_dbatl (CPUPPCState *env, int nr, target_ulong value)
710 {
711     dump_store_bat(env, 'D', 1, nr, value);
712     env->DBAT[1][nr] = value;
713 }
714
715 static inline void invalidate_all_tlbs (CPUPPCState *env)
716 {
717     /* XXX: this needs to be completed for sotware driven TLB support */
718     tlb_flush(env, 1);
719 }
720
721 /*****************************************************************************/
722 /* Special registers manipulation */
723 target_ulong do_load_nip (CPUPPCState *env)
724 {
725     return env->nip;
726 }
727
728 void do_store_nip (CPUPPCState *env, target_ulong value)
729 {
730     env->nip = value;
731 }
732
733 target_ulong do_load_sdr1 (CPUPPCState *env)
734 {
735     return env->sdr1;
736 }
737
738 void do_store_sdr1 (CPUPPCState *env, target_ulong value)
739 {
740 #if defined (DEBUG_MMU)
741     if (loglevel != 0) {
742         fprintf(logfile, "%s: 0x%08lx\n", __func__, (unsigned long)value);
743     }
744 #endif
745     if (env->sdr1 != value) {
746         env->sdr1 = value;
747         invalidate_all_tlbs(env);
748     }
749 }
750
751 target_ulong do_load_sr (CPUPPCState *env, int srnum)
752 {
753     return env->sr[srnum];
754 }
755
756 void do_store_sr (CPUPPCState *env, int srnum, target_ulong value)
757 {
758 #if defined (DEBUG_MMU)
759     if (loglevel != 0) {
760         fprintf(logfile, "%s: reg=%d 0x%08lx %08lx\n",
761                 __func__, srnum, (unsigned long)value, env->sr[srnum]);
762     }
763 #endif
764     if (env->sr[srnum] != value) {
765         env->sr[srnum] = value;
766 #if !defined(FLUSH_ALL_TLBS) && 0
767         {
768             target_ulong page, end;
769             /* Invalidate 256 MB of virtual memory */
770             page = (16 << 20) * srnum;
771             end = page + (16 << 20);
772             for (; page != end; page += TARGET_PAGE_SIZE)
773                 tlb_flush_page(env, page);
774         }
775 #else
776         invalidate_all_tlbs(env);
777 #endif
778     }
779 }
780
781 uint32_t do_load_cr (CPUPPCState *env)
782 {
783     return (env->crf[0] << 28) |
784         (env->crf[1] << 24) |
785         (env->crf[2] << 20) |
786         (env->crf[3] << 16) |
787         (env->crf[4] << 12) |
788         (env->crf[5] << 8) |
789         (env->crf[6] << 4) |
790         (env->crf[7] << 0);
791 }
792
793 void do_store_cr (CPUPPCState *env, uint32_t value, uint32_t mask)
794 {
795     int i, sh;
796
797     for (i = 0, sh = 7; i < 8; i++, sh --) {
798         if (mask & (1 << sh))
799             env->crf[i] = (value >> (sh * 4)) & 0xFUL;
800     }
801 }
802
803 uint32_t do_load_xer (CPUPPCState *env)
804 {
805     return (xer_so << XER_SO) |
806         (xer_ov << XER_OV) |
807         (xer_ca << XER_CA) |
808         (xer_bc << XER_BC) |
809         (xer_cmp << XER_CMP);
810 }
811
812 void do_store_xer (CPUPPCState *env, uint32_t value)
813 {
814     xer_so = (value >> XER_SO) & 0x01;
815     xer_ov = (value >> XER_OV) & 0x01;
816     xer_ca = (value >> XER_CA) & 0x01;
817     xer_cmp = (value >> XER_CMP) & 0xFF;
818     xer_bc = (value >> XER_BC) & 0x3F;
819 }
820
821 target_ulong do_load_msr (CPUPPCState *env)
822 {
823     return (msr_vr << MSR_VR)  |
824         (msr_ap  << MSR_AP)  |
825         (msr_sa  << MSR_SA)  |
826         (msr_key << MSR_KEY) |
827         (msr_pow << MSR_POW) |
828         (msr_tlb << MSR_TLB) |
829         (msr_ile << MSR_ILE) |
830         (msr_ee << MSR_EE) |
831         (msr_pr << MSR_PR) |
832         (msr_fp << MSR_FP) |
833         (msr_me << MSR_ME) |
834         (msr_fe0 << MSR_FE0) |
835         (msr_se << MSR_SE) |
836         (msr_be << MSR_BE) |
837         (msr_fe1 << MSR_FE1) |
838         (msr_al  << MSR_AL)  |
839         (msr_ip << MSR_IP) |
840         (msr_ir << MSR_IR) |
841         (msr_dr << MSR_DR) |
842         (msr_pe  << MSR_PE)  |
843         (msr_px  << MSR_PX)  |
844         (msr_ri << MSR_RI) |
845         (msr_le << MSR_LE);
846 }
847
848 void do_compute_hflags (CPUPPCState *env)
849 {
850     /* Compute current hflags */
851     env->hflags = (msr_pr << MSR_PR) | (msr_le << MSR_LE) |
852         (msr_fp << MSR_FP) | (msr_fe0 << MSR_FE0) | (msr_fe1 << MSR_FE1) |
853         (msr_vr << MSR_VR) | (msr_ap << MSR_AP) | (msr_sa << MSR_SA) | 
854         (msr_se << MSR_SE) | (msr_be << MSR_BE);
855 }
856
857 void do_store_msr (CPUPPCState *env, target_ulong value)
858     {
859     value &= env->msr_mask;
860     if (((value >> MSR_IR) & 1) != msr_ir ||
861         ((value >> MSR_DR) & 1) != msr_dr) {
862         /* Flush all tlb when changing translation mode
863          * When using software driven TLB, we may also need to reload
864          * all defined TLBs
865          */
866         tlb_flush(env, 1);
867         env->interrupt_request |= CPU_INTERRUPT_EXITTB;
868     }
869 #if 0
870     if (loglevel != 0) {
871         fprintf(logfile, "%s: T0 %08lx\n", __func__, value);
872     }
873 #endif
874     msr_vr  = (value >> MSR_VR)  & 1;
875     msr_ap  = (value >> MSR_AP)  & 1;
876     msr_sa  = (value >> MSR_SA)  & 1;
877     msr_key = (value >> MSR_KEY) & 1;
878     msr_pow = (value >> MSR_POW) & 1;
879     msr_tlb = (value >> MSR_TLB)  & 1;
880     msr_ile = (value >> MSR_ILE) & 1;
881     msr_ee  = (value >> MSR_EE)  & 1;
882     msr_pr  = (value >> MSR_PR)  & 1;
883     msr_fp  = (value >> MSR_FP)  & 1;
884     msr_me  = (value >> MSR_ME)  & 1;
885     msr_fe0 = (value >> MSR_FE0) & 1;
886     msr_se  = (value >> MSR_SE)  & 1;
887     msr_be  = (value >> MSR_BE)  & 1;
888     msr_fe1 = (value >> MSR_FE1) & 1;
889     msr_al  = (value >> MSR_AL)  & 1;
890     msr_ip  = (value >> MSR_IP)  & 1;
891     msr_ir  = (value >> MSR_IR)  & 1;
892     msr_dr  = (value >> MSR_DR)  & 1;
893     msr_pe  = (value >> MSR_PE)  & 1;
894     msr_px  = (value >> MSR_PX)  & 1;
895     msr_ri  = (value >> MSR_RI)  & 1;
896     msr_le  = (value >> MSR_LE)  & 1;
897     do_compute_hflags(env);
898 }
899
900 float64 do_load_fpscr (CPUPPCState *env)
901 {
902     /* The 32 MSB of the target fpr are undefined.
903      * They'll be zero...
904      */
905     union {
906         float64 d;
907         struct {
908             uint32_t u[2];
909         } s;
910     } u;
911     int i;
912
913 #ifdef WORDS_BIGENDIAN
914 #define WORD0 0
915 #define WORD1 1
916 #else
917 #define WORD0 1
918 #define WORD1 0
919 #endif
920     u.s.u[WORD0] = 0;
921     u.s.u[WORD1] = 0;
922     for (i = 0; i < 8; i++)
923         u.s.u[WORD1] |= env->fpscr[i] << (4 * i);
924     return u.d;
925 }
926
927 void do_store_fpscr (CPUPPCState *env, float64 f, uint32_t mask)
928 {
929     /*
930      * We use only the 32 LSB of the incoming fpr
931      */
932     union {
933         double d;
934         struct {
935             uint32_t u[2];
936         } s;
937     } u;
938     int i, rnd_type;
939
940     u.d = f;
941     if (mask & 0x80)
942         env->fpscr[0] = (env->fpscr[0] & 0x9) | ((u.s.u[WORD1] >> 28) & ~0x9);
943     for (i = 1; i < 7; i++) {
944         if (mask & (1 << (7 - i)))
945             env->fpscr[i] = (u.s.u[WORD1] >> (4 * (7 - i))) & 0xF;
946     }
947     /* TODO: update FEX & VX */
948     /* Set rounding mode */
949     switch (env->fpscr[0] & 0x3) {
950     case 0:
951         /* Best approximation (round to nearest) */
952         rnd_type = float_round_nearest_even;
953         break;
954     case 1:
955         /* Smaller magnitude (round toward zero) */
956         rnd_type = float_round_to_zero;
957         break;
958     case 2:
959         /* Round toward +infinite */
960         rnd_type = float_round_up;
961         break;
962     default:
963     case 3:
964         /* Round toward -infinite */
965         rnd_type = float_round_down;
966         break;
967     }
968     set_float_rounding_mode(rnd_type, &env->fp_status);
969 }
970
971 /*****************************************************************************/
972 /* Exception processing */
973 #if defined (CONFIG_USER_ONLY)
974 void do_interrupt (CPUState *env)
975 {
976     env->exception_index = -1;
977 }
978 #else
979 static void dump_syscall(CPUState *env)
980 {
981     fprintf(logfile, "syscall r0=0x%08x r3=0x%08x r4=0x%08x r5=0x%08x r6=0x%08x nip=0x%08x\n",
982             env->gpr[0], env->gpr[3], env->gpr[4],
983             env->gpr[5], env->gpr[6], env->nip);
984 }
985
986 void do_interrupt (CPUState *env)
987 {
988     uint32_t msr;
989     int excp;
990
991     excp = env->exception_index;
992     msr = do_load_msr(env);
993 #if defined (DEBUG_EXCEPTIONS)
994     if ((excp == EXCP_PROGRAM || excp == EXCP_DSI) && msr_pr == 1) 
995     {
996         if (loglevel > 0) {
997             fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
998                     env->nip, excp << 8, env->error_code);
999         }
1000         if (loglevel > 0)
1001             cpu_dump_state(env, logfile, fprintf, 0);
1002     }
1003 #endif
1004     if (loglevel & CPU_LOG_INT) {
1005         fprintf(logfile, "Raise exception at 0x%08x => 0x%08x (%02x)\n",
1006                 env->nip, excp << 8, env->error_code);
1007     }
1008     /* Generate informations in save/restore registers */
1009     switch (excp) {
1010     case EXCP_NONE:
1011         /* Do nothing */
1012 #if defined (DEBUG_EXCEPTIONS)
1013         printf("%s: escape EXCP_NONE\n", __func__);
1014 #endif
1015         return;
1016     case EXCP_RESET:
1017         if (msr_ip)
1018             excp += 0xFFC00;
1019         goto store_next;
1020     case EXCP_MACHINE_CHECK:
1021         if (msr_me == 0) {
1022             cpu_abort(env, "Machine check exception while not allowed\n");
1023         }
1024         msr_me = 0;
1025         break;
1026     case EXCP_DSI:
1027         /* Store exception cause */
1028         /* data location address has been stored
1029          * when the fault has been detected
1030      */
1031         msr &= ~0xFFFF0000;
1032         env->spr[SPR_DSISR] = 0;
1033         if ((env->error_code & 0x0f) ==  EXCP_DSI_TRANSLATE)
1034             env->spr[SPR_DSISR] |= 0x40000000;
1035         else if ((env->error_code & 0x0f) ==  EXCP_DSI_PROT)
1036             env->spr[SPR_DSISR] |= 0x08000000;
1037         else if ((env->error_code & 0x0f) ==  EXCP_DSI_NOTSUP) {
1038             env->spr[SPR_DSISR] |= 0x80000000;
1039             if (env->error_code & EXCP_DSI_DIRECT)
1040                 env->spr[SPR_DSISR] |= 0x04000000;
1041         }
1042         if (env->error_code & EXCP_DSI_STORE)
1043             env->spr[SPR_DSISR] |= 0x02000000;
1044         if ((env->error_code & 0xF) == EXCP_DSI_DABR)
1045             env->spr[SPR_DSISR] |= 0x00400000;
1046         if (env->error_code & EXCP_DSI_ECXW)
1047             env->spr[SPR_DSISR] |= 0x00100000;
1048 #if defined (DEBUG_EXCEPTIONS)
1049         if (loglevel) {
1050             fprintf(logfile, "DSI exception: DSISR=0x%08x, DAR=0x%08x\n",
1051                     env->spr[SPR_DSISR], env->spr[SPR_DAR]);
1052         } else {
1053             printf("DSI exception: DSISR=0x%08x, DAR=0x%08x nip=0x%08x\n",
1054                    env->spr[SPR_DSISR], env->spr[SPR_DAR], env->nip);
1055         }
1056 #endif
1057         goto store_next;
1058     case EXCP_ISI:
1059         /* Store exception cause */
1060         msr &= ~0xFFFF0000;
1061         if (env->error_code == EXCP_ISI_TRANSLATE)
1062             msr |= 0x40000000;
1063         else if (env->error_code == EXCP_ISI_NOEXEC ||
1064                  env->error_code == EXCP_ISI_GUARD ||
1065                  env->error_code == EXCP_ISI_DIRECT)
1066             msr |= 0x10000000;
1067         else
1068             msr |= 0x08000000;
1069 #if defined (DEBUG_EXCEPTIONS)
1070         if (loglevel) {
1071             fprintf(logfile, "ISI exception: msr=0x%08x, nip=0x%08x\n",
1072                     msr, env->nip);
1073         } else {
1074             printf("ISI exception: msr=0x%08x, nip=0x%08x tbl:0x%08x\n",
1075                    msr, env->nip, env->spr[V_TBL]);
1076         }
1077 #endif
1078         goto store_next;
1079     case EXCP_EXTERNAL:
1080         if (msr_ee == 0) {
1081 #if defined (DEBUG_EXCEPTIONS)
1082             if (loglevel > 0) {
1083                 fprintf(logfile, "Skipping hardware interrupt\n");
1084     }
1085 #endif
1086             /* Requeue it */
1087             do_raise_exception(EXCP_EXTERNAL);
1088             return;
1089             }
1090         goto store_next;
1091     case EXCP_ALIGN:
1092         /* Store exception cause */
1093         /* Get rS/rD and rA from faulting opcode */
1094         env->spr[SPR_DSISR] |=
1095             (ldl_code((env->nip - 4)) & 0x03FF0000) >> 16;
1096         /* data location address has been stored
1097          * when the fault has been detected
1098          */
1099         goto store_current;
1100     case EXCP_PROGRAM:
1101         msr &= ~0xFFFF0000;
1102         switch (env->error_code & ~0xF) {
1103         case EXCP_FP:
1104             if (msr_fe0 == 0 && msr_fe1 == 0) {
1105 #if defined (DEBUG_EXCEPTIONS)
1106                 printf("Ignore floating point exception\n");
1107 #endif
1108                 return;
1109         }
1110             msr |= 0x00100000;
1111             /* Set FX */
1112             env->fpscr[7] |= 0x8;
1113             /* Finally, update FEX */
1114             if ((((env->fpscr[7] & 0x3) << 3) | (env->fpscr[6] >> 1)) &
1115                 ((env->fpscr[1] << 1) | (env->fpscr[0] >> 3)))
1116                 env->fpscr[7] |= 0x4;
1117         break;
1118         case EXCP_INVAL:
1119             //      printf("Invalid instruction at 0x%08x\n", env->nip);
1120             msr |= 0x00080000;
1121         break;
1122         case EXCP_PRIV:
1123             msr |= 0x00040000;
1124         break;
1125         case EXCP_TRAP:
1126             msr |= 0x00020000;
1127             break;
1128         default:
1129             /* Should never occur */
1130         break;
1131     }
1132         msr |= 0x00010000;
1133         goto store_current;
1134     case EXCP_NO_FP:
1135         msr &= ~0xFFFF0000;
1136         goto store_current;
1137     case EXCP_DECR:
1138         if (msr_ee == 0) {
1139             /* Requeue it */
1140             do_raise_exception(EXCP_DECR);
1141             return;
1142         }
1143         goto store_next;
1144     case EXCP_SYSCALL:
1145         /* NOTE: this is a temporary hack to support graphics OSI
1146            calls from the MOL driver */
1147         if (env->gpr[3] == 0x113724fa && env->gpr[4] == 0x77810f9b &&
1148             env->osi_call) {
1149             if (env->osi_call(env) != 0)
1150                 return;
1151         }
1152         if (loglevel & CPU_LOG_INT) {
1153             dump_syscall(env);
1154         }
1155         goto store_next;
1156     case EXCP_TRACE:
1157         goto store_next;
1158     case EXCP_FP_ASSIST:
1159         goto store_next;
1160     case EXCP_MTMSR:
1161         /* Nothing to do */
1162         return;
1163     case EXCP_BRANCH:
1164         /* Nothing to do */
1165         return;
1166     case EXCP_RFI:
1167         /* Restore user-mode state */
1168 #if defined (DEBUG_EXCEPTIONS)
1169         if (msr_pr == 1)
1170             printf("Return from exception => 0x%08x\n", (uint32_t)env->nip);
1171 #endif
1172         return;
1173     store_current:
1174         /* SRR0 is set to current instruction */
1175         env->spr[SPR_SRR0] = (uint32_t)env->nip - 4;
1176         break;
1177     store_next:
1178         /* SRR0 is set to next instruction */
1179         env->spr[SPR_SRR0] = (uint32_t)env->nip;
1180         break;
1181     }
1182     env->spr[SPR_SRR1] = msr;
1183     /* reload MSR with correct bits */
1184     msr_pow = 0;
1185     msr_ee = 0;
1186     msr_pr = 0;
1187     msr_fp = 0;
1188     msr_fe0 = 0;
1189     msr_se = 0;
1190     msr_be = 0;
1191     msr_fe1 = 0;
1192     msr_ir = 0;
1193     msr_dr = 0;
1194     msr_ri = 0;
1195     msr_le = msr_ile;
1196     do_compute_hflags(env);
1197     /* Jump to handler */
1198     env->nip = excp << 8;
1199     env->exception_index = EXCP_NONE;
1200     /* Invalidate all TLB as we may have changed translation mode */
1201 #ifdef ACCURATE_TLB_FLUSH
1202     tlb_flush(env, 1);
1203 #endif
1204     /* ensure that no TB jump will be modified as
1205        the program flow was changed */
1206 #ifdef __sparc__
1207     tmp_T0 = 0;
1208 #else
1209     T0 = 0;
1210 #endif
1211     env->exception_index = -1;
1212 }
1213 #endif /* !CONFIG_USER_ONLY */