]> Git Repo - qemu.git/blob - target-ppc/mmu-hash32.c
target-ppc: Don't share get_pteg_offset() between 32 and 64-bit
[qemu.git] / target-ppc / mmu-hash32.c
1 /*
2  *  PowerPC MMU, TLB and BAT emulation helpers for QEMU.
3  *
4  *  Copyright (c) 2003-2007 Jocelyn Mayer
5  *  Copyright (c) 2013 David Gibson, IBM Corporation
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "cpu.h"
22 #include "helper.h"
23 #include "sysemu/kvm.h"
24 #include "kvm_ppc.h"
25 #include "mmu-hash32.h"
26
27 //#define DEBUG_MMU
28
29 #ifdef DEBUG_MMU
30 #  define LOG_MMU(...) qemu_log(__VA_ARGS__)
31 #  define LOG_MMU_STATE(env) log_cpu_state((env), 0)
32 #else
33 #  define LOG_MMU(...) do { } while (0)
34 #  define LOG_MMU_STATE(...) do { } while (0)
35 #endif
36
37 #define PTE_PTEM_MASK 0x7FFFFFBF
38 #define PTE_CHECK_MASK (TARGET_PAGE_MASK | 0x7B)
39
40 static int ppc_hash32_pp_check(int key, int pp, int nx)
41 {
42     int access;
43
44     /* Compute access rights */
45     access = 0;
46     if (key == 0) {
47         switch (pp) {
48         case 0x0:
49         case 0x1:
50         case 0x2:
51             access |= PAGE_WRITE;
52             /* No break here */
53         case 0x3:
54             access |= PAGE_READ;
55             break;
56         }
57     } else {
58         switch (pp) {
59         case 0x0:
60             access = 0;
61             break;
62         case 0x1:
63         case 0x3:
64             access = PAGE_READ;
65             break;
66         case 0x2:
67             access = PAGE_READ | PAGE_WRITE;
68             break;
69         }
70     }
71     if (nx == 0) {
72         access |= PAGE_EXEC;
73     }
74
75     return access;
76 }
77
78 static int ppc_hash32_check_prot(int prot, int rw, int access_type)
79 {
80     int ret;
81
82     if (access_type == ACCESS_CODE) {
83         if (prot & PAGE_EXEC) {
84             ret = 0;
85         } else {
86             ret = -2;
87         }
88     } else if (rw) {
89         if (prot & PAGE_WRITE) {
90             ret = 0;
91         } else {
92             ret = -2;
93         }
94     } else {
95         if (prot & PAGE_READ) {
96             ret = 0;
97         } else {
98             ret = -2;
99         }
100     }
101
102     return ret;
103 }
104
105 static inline int pte_is_valid_hash32(target_ulong pte0)
106 {
107     return pte0 & 0x80000000 ? 1 : 0;
108 }
109
110 static int pte_check_hash32(mmu_ctx_t *ctx, target_ulong pte0,
111                             target_ulong pte1, int h, int rw, int type)
112 {
113     target_ulong ptem, mmask;
114     int access, ret, pteh, ptev, pp;
115
116     ret = -1;
117     /* Check validity and table match */
118     ptev = pte_is_valid_hash32(pte0);
119     pteh = (pte0 >> 6) & 1;
120     if (ptev && h == pteh) {
121         /* Check vsid & api */
122         ptem = pte0 & PTE_PTEM_MASK;
123         mmask = PTE_CHECK_MASK;
124         pp = pte1 & 0x00000003;
125         if (ptem == ctx->ptem) {
126             if (ctx->raddr != (hwaddr)-1ULL) {
127                 /* all matches should have equal RPN, WIMG & PP */
128                 if ((ctx->raddr & mmask) != (pte1 & mmask)) {
129                     qemu_log("Bad RPN/WIMG/PP\n");
130                     return -3;
131                 }
132             }
133             /* Compute access rights */
134             access = ppc_hash32_pp_check(ctx->key, pp, ctx->nx);
135             /* Keep the matching PTE informations */
136             ctx->raddr = pte1;
137             ctx->prot = access;
138             ret = ppc_hash32_check_prot(ctx->prot, rw, type);
139             if (ret == 0) {
140                 /* Access granted */
141                 LOG_MMU("PTE access granted !\n");
142             } else {
143                 /* Access right violation */
144                 LOG_MMU("PTE access rejected\n");
145             }
146         }
147     }
148
149     return ret;
150 }
151
152 static int ppc_hash32_pte_update_flags(mmu_ctx_t *ctx, target_ulong *pte1p,
153                                        int ret, int rw)
154 {
155     int store = 0;
156
157     /* Update page flags */
158     if (!(*pte1p & 0x00000100)) {
159         /* Update accessed flag */
160         *pte1p |= 0x00000100;
161         store = 1;
162     }
163     if (!(*pte1p & 0x00000080)) {
164         if (rw == 1 && ret == 0) {
165             /* Update changed flag */
166             *pte1p |= 0x00000080;
167             store = 1;
168         } else {
169             /* Force page fault for first write access */
170             ctx->prot &= ~PAGE_WRITE;
171         }
172     }
173
174     return store;
175 }
176
177 hwaddr get_pteg_offset32(CPUPPCState *env, hwaddr hash)
178 {
179     return (hash * HASH_PTE_SIZE_32 * 8) & env->htab_mask;
180 }
181
182 /* PTE table lookup */
183 static int find_pte32(CPUPPCState *env, mmu_ctx_t *ctx, int h,
184                       int rw, int type, int target_page_bits)
185 {
186     hwaddr pteg_off;
187     target_ulong pte0, pte1;
188     int i, good = -1;
189     int ret, r;
190
191     ret = -1; /* No entry found */
192     pteg_off = get_pteg_offset32(env, ctx->hash[h]);
193     for (i = 0; i < 8; i++) {
194         if (env->external_htab) {
195             pte0 = ldl_p(env->external_htab + pteg_off + (i * 8));
196             pte1 = ldl_p(env->external_htab + pteg_off + (i * 8) + 4);
197         } else {
198             pte0 = ldl_phys(env->htab_base + pteg_off + (i * 8));
199             pte1 = ldl_phys(env->htab_base + pteg_off + (i * 8) + 4);
200         }
201         r = pte_check_hash32(ctx, pte0, pte1, h, rw, type);
202         LOG_MMU("Load pte from %08" HWADDR_PRIx " => " TARGET_FMT_lx " "
203                 TARGET_FMT_lx " %d %d %d " TARGET_FMT_lx "\n",
204                 pteg_off + (i * 8), pte0, pte1, (int)(pte0 >> 31), h,
205                 (int)((pte0 >> 6) & 1), ctx->ptem);
206         switch (r) {
207         case -3:
208             /* PTE inconsistency */
209             return -1;
210         case -2:
211             /* Access violation */
212             ret = -2;
213             good = i;
214             break;
215         case -1:
216         default:
217             /* No PTE match */
218             break;
219         case 0:
220             /* access granted */
221             /* XXX: we should go on looping to check all PTEs consistency
222              *      but if we can speed-up the whole thing as the
223              *      result would be undefined if PTEs are not consistent.
224              */
225             ret = 0;
226             good = i;
227             goto done;
228         }
229     }
230     if (good != -1) {
231     done:
232         LOG_MMU("found PTE at addr %08" HWADDR_PRIx " prot=%01x ret=%d\n",
233                 ctx->raddr, ctx->prot, ret);
234         /* Update page flags */
235         pte1 = ctx->raddr;
236         if (ppc_hash32_pte_update_flags(ctx, &pte1, ret, rw) == 1) {
237             if (env->external_htab) {
238                 stl_p(env->external_htab + pteg_off + (good * 8) + 4,
239                       pte1);
240             } else {
241                 stl_phys_notdirty(env->htab_base + pteg_off +
242                                   (good * 8) + 4, pte1);
243             }
244         }
245     }
246
247     /* We have a TLB that saves 4K pages, so let's
248      * split a huge page to 4k chunks */
249     if (target_page_bits != TARGET_PAGE_BITS) {
250         ctx->raddr |= (ctx->eaddr & ((1 << target_page_bits) - 1))
251                       & TARGET_PAGE_MASK;
252     }
253     return ret;
254 }
255
256 static int get_segment32(CPUPPCState *env, mmu_ctx_t *ctx,
257                          target_ulong eaddr, int rw, int type)
258 {
259     hwaddr hash;
260     target_ulong vsid;
261     int ds, pr, target_page_bits;
262     int ret, ret2;
263     target_ulong sr, pgidx;
264
265     pr = msr_pr;
266     ctx->eaddr = eaddr;
267
268     sr = env->sr[eaddr >> 28];
269     ctx->key = (((sr & 0x20000000) && (pr != 0)) ||
270                 ((sr & 0x40000000) && (pr == 0))) ? 1 : 0;
271     ds = sr & 0x80000000 ? 1 : 0;
272     ctx->nx = sr & 0x10000000 ? 1 : 0;
273     vsid = sr & 0x00FFFFFF;
274     target_page_bits = TARGET_PAGE_BITS;
275     LOG_MMU("Check segment v=" TARGET_FMT_lx " %d " TARGET_FMT_lx " nip="
276             TARGET_FMT_lx " lr=" TARGET_FMT_lx
277             " ir=%d dr=%d pr=%d %d t=%d\n",
278             eaddr, (int)(eaddr >> 28), sr, env->nip, env->lr, (int)msr_ir,
279             (int)msr_dr, pr != 0 ? 1 : 0, rw, type);
280     pgidx = (eaddr & ~SEGMENT_MASK_256M) >> target_page_bits;
281     hash = vsid ^ pgidx;
282     ctx->ptem = (vsid << 7) | (pgidx >> 10);
283
284     LOG_MMU("pte segment: key=%d ds %d nx %d vsid " TARGET_FMT_lx "\n",
285             ctx->key, ds, ctx->nx, vsid);
286     ret = -1;
287     if (!ds) {
288         /* Check if instruction fetch is allowed, if needed */
289         if (type != ACCESS_CODE || ctx->nx == 0) {
290             /* Page address translation */
291             LOG_MMU("htab_base " TARGET_FMT_plx " htab_mask " TARGET_FMT_plx
292                     " hash " TARGET_FMT_plx "\n",
293                     env->htab_base, env->htab_mask, hash);
294             ctx->hash[0] = hash;
295             ctx->hash[1] = ~hash;
296
297             /* Initialize real address with an invalid value */
298             ctx->raddr = (hwaddr)-1ULL;
299             LOG_MMU("0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
300                     " vsid=" TARGET_FMT_lx " ptem=" TARGET_FMT_lx
301                     " hash=" TARGET_FMT_plx "\n",
302                     env->htab_base, env->htab_mask, vsid, ctx->ptem,
303                     ctx->hash[0]);
304             /* Primary table lookup */
305             ret = find_pte32(env, ctx, 0, rw, type, target_page_bits);
306             if (ret < 0) {
307                 /* Secondary table lookup */
308                 LOG_MMU("1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
309                         " vsid=" TARGET_FMT_lx " api=" TARGET_FMT_lx
310                         " hash=" TARGET_FMT_plx "\n", env->htab_base,
311                         env->htab_mask, vsid, ctx->ptem, ctx->hash[1]);
312                 ret2 = find_pte32(env, ctx, 1, rw, type,
313                                   target_page_bits);
314                 if (ret2 != -1) {
315                     ret = ret2;
316                 }
317             }
318 #if defined(DUMP_PAGE_TABLES)
319             if (qemu_log_enabled()) {
320                 hwaddr curaddr;
321                 uint32_t a0, a1, a2, a3;
322
323                 qemu_log("Page table: " TARGET_FMT_plx " len " TARGET_FMT_plx
324                          "\n", sdr, mask + 0x80);
325                 for (curaddr = sdr; curaddr < (sdr + mask + 0x80);
326                      curaddr += 16) {
327                     a0 = ldl_phys(curaddr);
328                     a1 = ldl_phys(curaddr + 4);
329                     a2 = ldl_phys(curaddr + 8);
330                     a3 = ldl_phys(curaddr + 12);
331                     if (a0 != 0 || a1 != 0 || a2 != 0 || a3 != 0) {
332                         qemu_log(TARGET_FMT_plx ": %08x %08x %08x %08x\n",
333                                  curaddr, a0, a1, a2, a3);
334                     }
335                 }
336             }
337 #endif
338         } else {
339             LOG_MMU("No access allowed\n");
340             ret = -3;
341         }
342     } else {
343         target_ulong sr;
344
345         LOG_MMU("direct store...\n");
346         /* Direct-store segment : absolutely *BUGGY* for now */
347
348         /* Direct-store implies a 32-bit MMU.
349          * Check the Segment Register's bus unit ID (BUID).
350          */
351         sr = env->sr[eaddr >> 28];
352         if ((sr & 0x1FF00000) >> 20 == 0x07f) {
353             /* Memory-forced I/O controller interface access */
354             /* If T=1 and BUID=x'07F', the 601 performs a memory access
355              * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
356              */
357             ctx->raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF);
358             ctx->prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
359             return 0;
360         }
361
362         switch (type) {
363         case ACCESS_INT:
364             /* Integer load/store : only access allowed */
365             break;
366         case ACCESS_CODE:
367             /* No code fetch is allowed in direct-store areas */
368             return -4;
369         case ACCESS_FLOAT:
370             /* Floating point load/store */
371             return -4;
372         case ACCESS_RES:
373             /* lwarx, ldarx or srwcx. */
374             return -4;
375         case ACCESS_CACHE:
376             /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
377             /* Should make the instruction do no-op.
378              * As it already do no-op, it's quite easy :-)
379              */
380             ctx->raddr = eaddr;
381             return 0;
382         case ACCESS_EXT:
383             /* eciwx or ecowx */
384             return -4;
385         default:
386             qemu_log("ERROR: instruction should not need "
387                         "address translation\n");
388             return -4;
389         }
390         if ((rw == 1 || ctx->key != 1) && (rw == 0 || ctx->key != 0)) {
391             ctx->raddr = eaddr;
392             ret = 2;
393         } else {
394             ret = -2;
395         }
396     }
397
398     return ret;
399 }
400
401
402 static int ppc_hash32_get_physical_address(CPUPPCState *env, mmu_ctx_t *ctx,
403                                            target_ulong eaddr, int rw,
404                                            int access_type)
405 {
406     bool real_mode = (access_type == ACCESS_CODE && msr_ir == 0)
407         || (access_type != ACCESS_CODE && msr_dr == 0);
408
409     if (real_mode) {
410         ctx->raddr = eaddr;
411         ctx->prot = PAGE_READ | PAGE_EXEC | PAGE_WRITE;
412         return 0;
413     } else {
414         int ret = -1;
415
416         /* Try to find a BAT */
417         if (env->nb_BATs != 0) {
418             ret = get_bat(env, ctx, eaddr, rw, access_type);
419         }
420         if (ret < 0) {
421             /* We didn't match any BAT entry or don't have BATs */
422             ret = get_segment32(env, ctx, eaddr, rw, access_type);
423         }
424         return ret;
425     }
426 }
427
428 hwaddr ppc_hash32_get_phys_page_debug(CPUPPCState *env, target_ulong addr)
429 {
430     mmu_ctx_t ctx;
431
432     if (unlikely(ppc_hash32_get_physical_address(env, &ctx, addr, 0, ACCESS_INT)
433                  != 0)) {
434         return -1;
435     }
436
437     return ctx.raddr & TARGET_PAGE_MASK;
438 }
439
440 int ppc_hash32_handle_mmu_fault(CPUPPCState *env, target_ulong address, int rw,
441                                 int mmu_idx)
442 {
443     mmu_ctx_t ctx;
444     int access_type;
445     int ret = 0;
446
447     if (rw == 2) {
448         /* code access */
449         rw = 0;
450         access_type = ACCESS_CODE;
451     } else {
452         /* data access */
453         access_type = env->access_type;
454     }
455     ret = ppc_hash32_get_physical_address(env, &ctx, address, rw, access_type);
456     if (ret == 0) {
457         tlb_set_page(env, address & TARGET_PAGE_MASK,
458                      ctx.raddr & TARGET_PAGE_MASK, ctx.prot,
459                      mmu_idx, TARGET_PAGE_SIZE);
460         ret = 0;
461     } else if (ret < 0) {
462         LOG_MMU_STATE(env);
463         if (access_type == ACCESS_CODE) {
464             switch (ret) {
465             case -1:
466                 /* No matches in page tables or TLB */
467                 env->exception_index = POWERPC_EXCP_ISI;
468                 env->error_code = 0x40000000;
469                 break;
470             case -2:
471                 /* Access rights violation */
472                 env->exception_index = POWERPC_EXCP_ISI;
473                 env->error_code = 0x08000000;
474                 break;
475             case -3:
476                 /* No execute protection violation */
477                 env->exception_index = POWERPC_EXCP_ISI;
478                 env->error_code = 0x10000000;
479                 break;
480             case -4:
481                 /* Direct store exception */
482                 /* No code fetch is allowed in direct-store areas */
483                 env->exception_index = POWERPC_EXCP_ISI;
484                 env->error_code = 0x10000000;
485                 break;
486             }
487         } else {
488             switch (ret) {
489             case -1:
490                 /* No matches in page tables or TLB */
491                 env->exception_index = POWERPC_EXCP_DSI;
492                 env->error_code = 0;
493                 env->spr[SPR_DAR] = address;
494                 if (rw == 1) {
495                     env->spr[SPR_DSISR] = 0x42000000;
496                 } else {
497                     env->spr[SPR_DSISR] = 0x40000000;
498                 }
499                 break;
500             case -2:
501                 /* Access rights violation */
502                 env->exception_index = POWERPC_EXCP_DSI;
503                 env->error_code = 0;
504                 env->spr[SPR_DAR] = address;
505                 if (rw == 1) {
506                     env->spr[SPR_DSISR] = 0x0A000000;
507                 } else {
508                     env->spr[SPR_DSISR] = 0x08000000;
509                 }
510                 break;
511             case -4:
512                 /* Direct store exception */
513                 switch (access_type) {
514                 case ACCESS_FLOAT:
515                     /* Floating point load/store */
516                     env->exception_index = POWERPC_EXCP_ALIGN;
517                     env->error_code = POWERPC_EXCP_ALIGN_FP;
518                     env->spr[SPR_DAR] = address;
519                     break;
520                 case ACCESS_RES:
521                     /* lwarx, ldarx or stwcx. */
522                     env->exception_index = POWERPC_EXCP_DSI;
523                     env->error_code = 0;
524                     env->spr[SPR_DAR] = address;
525                     if (rw == 1) {
526                         env->spr[SPR_DSISR] = 0x06000000;
527                     } else {
528                         env->spr[SPR_DSISR] = 0x04000000;
529                     }
530                     break;
531                 case ACCESS_EXT:
532                     /* eciwx or ecowx */
533                     env->exception_index = POWERPC_EXCP_DSI;
534                     env->error_code = 0;
535                     env->spr[SPR_DAR] = address;
536                     if (rw == 1) {
537                         env->spr[SPR_DSISR] = 0x06100000;
538                     } else {
539                         env->spr[SPR_DSISR] = 0x04100000;
540                     }
541                     break;
542                 default:
543                     printf("DSI: invalid exception (%d)\n", ret);
544                     env->exception_index = POWERPC_EXCP_PROGRAM;
545                     env->error_code =
546                         POWERPC_EXCP_INVAL | POWERPC_EXCP_INVAL_INVAL;
547                     env->spr[SPR_DAR] = address;
548                     break;
549                 }
550                 break;
551             }
552         }
553 #if 0
554         printf("%s: set exception to %d %02x\n", __func__,
555                env->exception, env->error_code);
556 #endif
557         ret = 1;
558     }
559
560     return ret;
561 }
This page took 0.055486 seconds and 4 git commands to generate.