blob: 29bace622aa06f340fc0fc631da758f7207c375b [file] [log] [blame]
David Gibson9d7c3f42013-03-12 00:31:07 +00001/*
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
Peter Maydell0d755902016-01-26 18:16:58 +000021#include "qemu/osdep.h"
David Gibson9d7c3f42013-03-12 00:31:07 +000022#include "cpu.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010023#include "exec/exec-all.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070024#include "exec/helper-proto.h"
David Gibson9d7c3f42013-03-12 00:31:07 +000025#include "sysemu/kvm.h"
26#include "kvm_ppc.h"
27#include "mmu-hash32.h"
Paolo Bonzini508127e2016-01-07 16:55:28 +030028#include "exec/log.h"
David Gibson9d7c3f42013-03-12 00:31:07 +000029
David Gibson98132792013-03-12 00:31:16 +000030//#define DEBUG_BAT
David Gibson9d7c3f42013-03-12 00:31:07 +000031
David Gibson98132792013-03-12 00:31:16 +000032#ifdef DEBUG_BATS
Paolo Bonzini48880da2015-11-13 13:34:23 +010033# define LOG_BATS(...) qemu_log_mask(CPU_LOG_MMU, __VA_ARGS__)
David Gibson98132792013-03-12 00:31:16 +000034#else
35# define LOG_BATS(...) do { } while (0)
36#endif
37
David Gibson5dc68eb2013-03-12 00:31:17 +000038struct mmu_ctx_hash32 {
39 hwaddr raddr; /* Real address */
David Gibson5dc68eb2013-03-12 00:31:17 +000040 int prot; /* Protection bits */
David Gibson5dc68eb2013-03-12 00:31:17 +000041 int key; /* Access key */
David Gibson5dc68eb2013-03-12 00:31:17 +000042};
43
David Gibsone01b4442013-03-12 00:31:40 +000044static int ppc_hash32_pp_prot(int key, int pp, int nx)
David Gibson496272a2013-03-12 00:31:14 +000045{
David Gibsone01b4442013-03-12 00:31:40 +000046 int prot;
David Gibson496272a2013-03-12 00:31:14 +000047
David Gibson496272a2013-03-12 00:31:14 +000048 if (key == 0) {
49 switch (pp) {
50 case 0x0:
51 case 0x1:
52 case 0x2:
David Gibsone01b4442013-03-12 00:31:40 +000053 prot = PAGE_READ | PAGE_WRITE;
David Gibson496272a2013-03-12 00:31:14 +000054 break;
David Gibsone01b4442013-03-12 00:31:40 +000055
56 case 0x3:
57 prot = PAGE_READ;
58 break;
59
60 default:
61 abort();
David Gibson496272a2013-03-12 00:31:14 +000062 }
63 } else {
64 switch (pp) {
65 case 0x0:
David Gibsone01b4442013-03-12 00:31:40 +000066 prot = 0;
David Gibson496272a2013-03-12 00:31:14 +000067 break;
David Gibsone01b4442013-03-12 00:31:40 +000068
David Gibson496272a2013-03-12 00:31:14 +000069 case 0x1:
70 case 0x3:
David Gibsone01b4442013-03-12 00:31:40 +000071 prot = PAGE_READ;
David Gibson496272a2013-03-12 00:31:14 +000072 break;
David Gibsone01b4442013-03-12 00:31:40 +000073
David Gibson496272a2013-03-12 00:31:14 +000074 case 0x2:
David Gibsone01b4442013-03-12 00:31:40 +000075 prot = PAGE_READ | PAGE_WRITE;
David Gibson496272a2013-03-12 00:31:14 +000076 break;
David Gibsone01b4442013-03-12 00:31:40 +000077
78 default:
79 abort();
David Gibson496272a2013-03-12 00:31:14 +000080 }
81 }
82 if (nx == 0) {
David Gibsone01b4442013-03-12 00:31:40 +000083 prot |= PAGE_EXEC;
David Gibson496272a2013-03-12 00:31:14 +000084 }
85
David Gibsone01b4442013-03-12 00:31:40 +000086 return prot;
David Gibson496272a2013-03-12 00:31:14 +000087}
88
David Gibson7ef23062016-01-14 15:33:27 +110089static int ppc_hash32_pte_prot(PowerPCCPU *cpu,
David Gibsone01b4442013-03-12 00:31:40 +000090 target_ulong sr, ppc_hash_pte32_t pte)
David Gibson496272a2013-03-12 00:31:14 +000091{
David Gibson7ef23062016-01-14 15:33:27 +110092 CPUPPCState *env = &cpu->env;
David Gibsone01b4442013-03-12 00:31:40 +000093 unsigned pp, key;
David Gibson496272a2013-03-12 00:31:14 +000094
David Gibsone01b4442013-03-12 00:31:40 +000095 key = !!(msr_pr ? (sr & SR32_KP) : (sr & SR32_KS));
96 pp = pte.pte1 & HPTE32_R_PP;
David Gibson496272a2013-03-12 00:31:14 +000097
David Gibsone01b4442013-03-12 00:31:40 +000098 return ppc_hash32_pp_prot(key, pp, !!(sr & SR32_NX));
David Gibson496272a2013-03-12 00:31:14 +000099}
100
David Gibson7ef23062016-01-14 15:33:27 +1100101static target_ulong hash32_bat_size(PowerPCCPU *cpu,
David Gibson6fc76aa2013-03-12 00:31:35 +0000102 target_ulong batu, target_ulong batl)
David Gibson98132792013-03-12 00:31:16 +0000103{
David Gibson7ef23062016-01-14 15:33:27 +1100104 CPUPPCState *env = &cpu->env;
105
David Gibson6fc76aa2013-03-12 00:31:35 +0000106 if ((msr_pr && !(batu & BATU32_VP))
107 || (!msr_pr && !(batu & BATU32_VS))) {
108 return 0;
David Gibson98132792013-03-12 00:31:16 +0000109 }
David Gibson6fc76aa2013-03-12 00:31:35 +0000110
111 return BATU32_BEPI & ~((batu & BATU32_BL) << 15);
David Gibson98132792013-03-12 00:31:16 +0000112}
113
David Gibson7ef23062016-01-14 15:33:27 +1100114static int hash32_bat_prot(PowerPCCPU *cpu,
David Gibsone1d49512013-03-12 00:31:34 +0000115 target_ulong batu, target_ulong batl)
116{
117 int pp, prot;
118
119 prot = 0;
120 pp = batl & BATL32_PP;
121 if (pp != 0) {
122 prot = PAGE_READ | PAGE_EXEC;
123 if (pp == 0x2) {
124 prot |= PAGE_WRITE;
125 }
126 }
127 return prot;
128}
129
David Gibson7ef23062016-01-14 15:33:27 +1100130static target_ulong hash32_bat_601_size(PowerPCCPU *cpu,
David Gibsone1d49512013-03-12 00:31:34 +0000131 target_ulong batu, target_ulong batl)
David Gibson98132792013-03-12 00:31:16 +0000132{
David Gibson6fc76aa2013-03-12 00:31:35 +0000133 if (!(batl & BATL32_601_V)) {
134 return 0;
135 }
David Gibson98132792013-03-12 00:31:16 +0000136
David Gibson6fc76aa2013-03-12 00:31:35 +0000137 return BATU32_BEPI & ~((batl & BATL32_601_BL) << 17);
David Gibsone1d49512013-03-12 00:31:34 +0000138}
139
David Gibson7ef23062016-01-14 15:33:27 +1100140static int hash32_bat_601_prot(PowerPCCPU *cpu,
David Gibsone1d49512013-03-12 00:31:34 +0000141 target_ulong batu, target_ulong batl)
142{
David Gibson7ef23062016-01-14 15:33:27 +1100143 CPUPPCState *env = &cpu->env;
David Gibsone1d49512013-03-12 00:31:34 +0000144 int key, pp;
145
146 pp = batu & BATU32_601_PP;
147 if (msr_pr == 0) {
148 key = !!(batu & BATU32_601_KS);
149 } else {
150 key = !!(batu & BATU32_601_KP);
151 }
David Gibsone01b4442013-03-12 00:31:40 +0000152 return ppc_hash32_pp_prot(key, pp, 0);
David Gibson98132792013-03-12 00:31:16 +0000153}
154
David Gibson7ef23062016-01-14 15:33:27 +1100155static hwaddr ppc_hash32_bat_lookup(PowerPCCPU *cpu, target_ulong ea, int rwx,
David Gibson145e52f2013-03-12 00:31:36 +0000156 int *prot)
David Gibson98132792013-03-12 00:31:16 +0000157{
David Gibson7ef23062016-01-14 15:33:27 +1100158 CPUPPCState *env = &cpu->env;
David Gibson9986ed12013-03-12 00:31:33 +0000159 target_ulong *BATlt, *BATut;
David Gibson145e52f2013-03-12 00:31:36 +0000160 int i;
David Gibson98132792013-03-12 00:31:16 +0000161
162 LOG_BATS("%s: %cBAT v " TARGET_FMT_lx "\n", __func__,
David Gibson145e52f2013-03-12 00:31:36 +0000163 rwx == 2 ? 'I' : 'D', ea);
David Gibson91cda452013-03-12 00:31:20 +0000164 if (rwx == 2) {
David Gibson98132792013-03-12 00:31:16 +0000165 BATlt = env->IBAT[1];
166 BATut = env->IBAT[0];
David Gibson91cda452013-03-12 00:31:20 +0000167 } else {
David Gibson98132792013-03-12 00:31:16 +0000168 BATlt = env->DBAT[1];
169 BATut = env->DBAT[0];
David Gibson98132792013-03-12 00:31:16 +0000170 }
171 for (i = 0; i < env->nb_BATs; i++) {
David Gibson9986ed12013-03-12 00:31:33 +0000172 target_ulong batu = BATut[i];
173 target_ulong batl = BATlt[i];
David Gibson6fc76aa2013-03-12 00:31:35 +0000174 target_ulong mask;
David Gibson9986ed12013-03-12 00:31:33 +0000175
David Gibson98132792013-03-12 00:31:16 +0000176 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
David Gibson7ef23062016-01-14 15:33:27 +1100177 mask = hash32_bat_601_size(cpu, batu, batl);
David Gibson98132792013-03-12 00:31:16 +0000178 } else {
David Gibson7ef23062016-01-14 15:33:27 +1100179 mask = hash32_bat_size(cpu, batu, batl);
David Gibson98132792013-03-12 00:31:16 +0000180 }
181 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
182 " BATl " TARGET_FMT_lx "\n", __func__,
David Gibson145e52f2013-03-12 00:31:36 +0000183 type == ACCESS_CODE ? 'I' : 'D', i, ea, batu, batl);
David Gibson6fc76aa2013-03-12 00:31:35 +0000184
David Gibson145e52f2013-03-12 00:31:36 +0000185 if (mask && ((ea & mask) == (batu & BATU32_BEPI))) {
186 hwaddr raddr = (batl & mask) | (ea & ~mask);
187
188 if (unlikely(env->mmu_model == POWERPC_MMU_601)) {
David Gibson7ef23062016-01-14 15:33:27 +1100189 *prot = hash32_bat_601_prot(cpu, batu, batl);
David Gibson145e52f2013-03-12 00:31:36 +0000190 } else {
David Gibson7ef23062016-01-14 15:33:27 +1100191 *prot = hash32_bat_prot(cpu, batu, batl);
David Gibson98132792013-03-12 00:31:16 +0000192 }
David Gibson145e52f2013-03-12 00:31:36 +0000193
194 return raddr & TARGET_PAGE_MASK;
David Gibson98132792013-03-12 00:31:16 +0000195 }
196 }
David Gibson145e52f2013-03-12 00:31:36 +0000197
David Gibson98132792013-03-12 00:31:16 +0000198 /* No hit */
David Gibson145e52f2013-03-12 00:31:36 +0000199#if defined(DEBUG_BATS)
200 if (qemu_log_enabled()) {
201 LOG_BATS("no BAT match for " TARGET_FMT_lx ":\n", ea);
202 for (i = 0; i < 4; i++) {
203 BATu = &BATut[i];
204 BATl = &BATlt[i];
205 BEPIu = *BATu & BATU32_BEPIU;
206 BEPIl = *BATu & BATU32_BEPIL;
207 bl = (*BATu & 0x00001FFC) << 15;
208 LOG_BATS("%s: %cBAT%d v " TARGET_FMT_lx " BATu " TARGET_FMT_lx
209 " BATl " TARGET_FMT_lx "\n\t" TARGET_FMT_lx " "
210 TARGET_FMT_lx " " TARGET_FMT_lx "\n",
211 __func__, type == ACCESS_CODE ? 'I' : 'D', i, ea,
212 *BATu, *BATl, BEPIu, BEPIl, bl);
213 }
214 }
215#endif
216
217 return -1;
David Gibson98132792013-03-12 00:31:16 +0000218}
219
David Gibson7ef23062016-01-14 15:33:27 +1100220static int ppc_hash32_direct_store(PowerPCCPU *cpu, target_ulong sr,
David Gibson723ed732013-03-12 00:31:25 +0000221 target_ulong eaddr, int rwx,
222 hwaddr *raddr, int *prot)
223{
David Gibson7ef23062016-01-14 15:33:27 +1100224 CPUState *cs = CPU(cpu);
225 CPUPPCState *env = &cpu->env;
David Gibson723ed732013-03-12 00:31:25 +0000226 int key = !!(msr_pr ? (sr & SR32_KP) : (sr & SR32_KS));
227
Antony Pavlov339aaf52014-12-13 19:48:18 +0300228 qemu_log_mask(CPU_LOG_MMU, "direct store...\n");
David Gibson723ed732013-03-12 00:31:25 +0000229
230 if ((sr & 0x1FF00000) >> 20 == 0x07f) {
231 /* Memory-forced I/O controller interface access */
232 /* If T=1 and BUID=x'07F', the 601 performs a memory access
233 * to SR[28-31] LA[4-31], bypassing all protection mechanisms.
234 */
235 *raddr = ((sr & 0xF) << 28) | (eaddr & 0x0FFFFFFF);
236 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
237 return 0;
238 }
239
240 if (rwx == 2) {
241 /* No code fetch is allowed in direct-store areas */
Andreas Färber27103422013-08-26 08:31:06 +0200242 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000243 env->error_code = 0x10000000;
244 return 1;
David Gibson723ed732013-03-12 00:31:25 +0000245 }
246
247 switch (env->access_type) {
248 case ACCESS_INT:
249 /* Integer load/store : only access allowed */
250 break;
251 case ACCESS_FLOAT:
252 /* Floating point load/store */
Andreas Färber27103422013-08-26 08:31:06 +0200253 cs->exception_index = POWERPC_EXCP_ALIGN;
David Gibsoncaa597b2013-03-12 00:31:46 +0000254 env->error_code = POWERPC_EXCP_ALIGN_FP;
255 env->spr[SPR_DAR] = eaddr;
256 return 1;
David Gibson723ed732013-03-12 00:31:25 +0000257 case ACCESS_RES:
258 /* lwarx, ldarx or srwcx. */
David Gibsoncaa597b2013-03-12 00:31:46 +0000259 env->error_code = 0;
260 env->spr[SPR_DAR] = eaddr;
261 if (rwx == 1) {
262 env->spr[SPR_DSISR] = 0x06000000;
263 } else {
264 env->spr[SPR_DSISR] = 0x04000000;
265 }
266 return 1;
David Gibson723ed732013-03-12 00:31:25 +0000267 case ACCESS_CACHE:
268 /* dcba, dcbt, dcbtst, dcbf, dcbi, dcbst, dcbz, or icbi */
269 /* Should make the instruction do no-op.
270 * As it already do no-op, it's quite easy :-)
271 */
272 *raddr = eaddr;
273 return 0;
274 case ACCESS_EXT:
275 /* eciwx or ecowx */
Andreas Färber27103422013-08-26 08:31:06 +0200276 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000277 env->error_code = 0;
278 env->spr[SPR_DAR] = eaddr;
279 if (rwx == 1) {
280 env->spr[SPR_DSISR] = 0x06100000;
281 } else {
282 env->spr[SPR_DSISR] = 0x04100000;
283 }
284 return 1;
David Gibson723ed732013-03-12 00:31:25 +0000285 default:
Paolo Bonzini48880da2015-11-13 13:34:23 +0100286 cpu_abort(cs, "ERROR: instruction should not need "
David Gibson723ed732013-03-12 00:31:25 +0000287 "address translation\n");
David Gibson723ed732013-03-12 00:31:25 +0000288 }
289 if ((rwx == 1 || key != 1) && (rwx == 0 || key != 0)) {
290 *raddr = eaddr;
David Gibsoncaa597b2013-03-12 00:31:46 +0000291 return 0;
David Gibson723ed732013-03-12 00:31:25 +0000292 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200293 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000294 env->error_code = 0;
295 env->spr[SPR_DAR] = eaddr;
296 if (rwx == 1) {
297 env->spr[SPR_DSISR] = 0x0a000000;
298 } else {
299 env->spr[SPR_DSISR] = 0x08000000;
300 }
301 return 1;
David Gibson723ed732013-03-12 00:31:25 +0000302 }
303}
304
David Gibson7ef23062016-01-14 15:33:27 +1100305hwaddr get_pteg_offset32(PowerPCCPU *cpu, hwaddr hash)
David Gibson59191722013-03-12 00:31:15 +0000306{
David Gibson7ef23062016-01-14 15:33:27 +1100307 CPUPPCState *env = &cpu->env;
308
David Gibsond5aea6f2013-03-12 00:31:18 +0000309 return (hash * HASH_PTEG_SIZE_32) & env->htab_mask;
David Gibson59191722013-03-12 00:31:15 +0000310}
311
David Gibson7ef23062016-01-14 15:33:27 +1100312static hwaddr ppc_hash32_pteg_search(PowerPCCPU *cpu, hwaddr pteg_off,
David Gibsonaea390e2013-03-12 00:31:28 +0000313 bool secondary, target_ulong ptem,
314 ppc_hash_pte32_t *pte)
315{
316 hwaddr pte_offset = pteg_off;
317 target_ulong pte0, pte1;
318 int i;
319
320 for (i = 0; i < HPTES_PER_GROUP; i++) {
David Gibson7ef23062016-01-14 15:33:27 +1100321 pte0 = ppc_hash32_load_hpte0(cpu, pte_offset);
322 pte1 = ppc_hash32_load_hpte1(cpu, pte_offset);
David Gibsonaea390e2013-03-12 00:31:28 +0000323
324 if ((pte0 & HPTE32_V_VALID)
325 && (secondary == !!(pte0 & HPTE32_V_SECONDARY))
326 && HPTE32_V_COMPARE(pte0, ptem)) {
327 pte->pte0 = pte0;
328 pte->pte1 = pte1;
329 return pte_offset;
330 }
331
332 pte_offset += HASH_PTE_SIZE_32;
333 }
334
335 return -1;
336}
337
David Gibson7ef23062016-01-14 15:33:27 +1100338static hwaddr ppc_hash32_htab_lookup(PowerPCCPU *cpu,
David Gibson7f3bdc22013-03-12 00:31:30 +0000339 target_ulong sr, target_ulong eaddr,
340 ppc_hash_pte32_t *pte)
David Gibsonc69b6152013-03-12 00:31:08 +0000341{
David Gibson7ef23062016-01-14 15:33:27 +1100342 CPUPPCState *env = &cpu->env;
David Gibsonaea390e2013-03-12 00:31:28 +0000343 hwaddr pteg_off, pte_offset;
David Gibsona1ff7512013-03-12 00:31:29 +0000344 hwaddr hash;
345 uint32_t vsid, pgidx, ptem;
David Gibsonc69b6152013-03-12 00:31:08 +0000346
David Gibsona1ff7512013-03-12 00:31:29 +0000347 vsid = sr & SR32_VSID;
David Gibsona1ff7512013-03-12 00:31:29 +0000348 pgidx = (eaddr & ~SEGMENT_MASK_256M) >> TARGET_PAGE_BITS;
349 hash = vsid ^ pgidx;
350 ptem = (vsid << 7) | (pgidx >> 10);
351
352 /* Page address translation */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300353 qemu_log_mask(CPU_LOG_MMU, "htab_base " TARGET_FMT_plx
354 " htab_mask " TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000355 " hash " TARGET_FMT_plx "\n",
356 env->htab_base, env->htab_mask, hash);
357
358 /* Primary PTEG lookup */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300359 qemu_log_mask(CPU_LOG_MMU, "0 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000360 " vsid=%" PRIx32 " ptem=%" PRIx32
361 " hash=" TARGET_FMT_plx "\n",
362 env->htab_base, env->htab_mask, vsid, ptem, hash);
David Gibson7ef23062016-01-14 15:33:27 +1100363 pteg_off = get_pteg_offset32(cpu, hash);
364 pte_offset = ppc_hash32_pteg_search(cpu, pteg_off, 0, ptem, pte);
David Gibsona1ff7512013-03-12 00:31:29 +0000365 if (pte_offset == -1) {
366 /* Secondary PTEG lookup */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300367 qemu_log_mask(CPU_LOG_MMU, "1 htab=" TARGET_FMT_plx "/" TARGET_FMT_plx
David Gibsona1ff7512013-03-12 00:31:29 +0000368 " vsid=%" PRIx32 " api=%" PRIx32
369 " hash=" TARGET_FMT_plx "\n", env->htab_base,
370 env->htab_mask, vsid, ptem, ~hash);
David Gibson7ef23062016-01-14 15:33:27 +1100371 pteg_off = get_pteg_offset32(cpu, ~hash);
372 pte_offset = ppc_hash32_pteg_search(cpu, pteg_off, 1, ptem, pte);
David Gibsona1ff7512013-03-12 00:31:29 +0000373 }
374
David Gibson7f3bdc22013-03-12 00:31:30 +0000375 return pte_offset;
David Gibsonc69b6152013-03-12 00:31:08 +0000376}
David Gibson04808842013-03-12 00:31:09 +0000377
David Gibson6d11d992013-03-12 00:31:43 +0000378static hwaddr ppc_hash32_pte_raddr(target_ulong sr, ppc_hash_pte32_t pte,
379 target_ulong eaddr)
380{
David Gibson75d5ec82013-03-12 00:31:44 +0000381 hwaddr rpn = pte.pte1 & HPTE32_R_RPN;
David Gibson6d11d992013-03-12 00:31:43 +0000382 hwaddr mask = ~TARGET_PAGE_MASK;
383
384 return (rpn & ~mask) | (eaddr & mask);
385}
386
Paolo Bonzinib2305602016-03-15 15:12:16 +0100387int ppc_hash32_handle_mmu_fault(PowerPCCPU *cpu, vaddr eaddr, int rwx,
David Gibsoncaa597b2013-03-12 00:31:46 +0000388 int mmu_idx)
David Gibson04808842013-03-12 00:31:09 +0000389{
Andreas Färberd0e39c52013-09-02 14:14:24 +0200390 CPUState *cs = CPU(cpu);
391 CPUPPCState *env = &cpu->env;
David Gibsona1ff7512013-03-12 00:31:29 +0000392 target_ulong sr;
David Gibson7f3bdc22013-03-12 00:31:30 +0000393 hwaddr pte_offset;
394 ppc_hash_pte32_t pte;
David Gibsoncaa597b2013-03-12 00:31:46 +0000395 int prot;
David Gibsonb3440742013-03-12 00:31:42 +0000396 uint32_t new_pte1;
David Gibsone01b4442013-03-12 00:31:40 +0000397 const int need_prot[] = {PAGE_READ, PAGE_WRITE, PAGE_EXEC};
David Gibsoncaa597b2013-03-12 00:31:46 +0000398 hwaddr raddr;
David Gibson04808842013-03-12 00:31:09 +0000399
David Gibson6a980112013-03-12 00:31:32 +0000400 assert((rwx == 0) || (rwx == 1) || (rwx == 2));
401
David Gibson65d61642013-03-12 00:31:23 +0000402 /* 1. Handle real mode accesses */
403 if (((rwx == 2) && (msr_ir == 0)) || ((rwx != 2) && (msr_dr == 0))) {
404 /* Translation is off */
David Gibsoncaa597b2013-03-12 00:31:46 +0000405 raddr = eaddr;
Andreas Färber0c591eb2013-09-03 13:59:37 +0200406 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000407 PAGE_READ | PAGE_WRITE | PAGE_EXEC, mmu_idx,
408 TARGET_PAGE_SIZE);
David Gibson65d61642013-03-12 00:31:23 +0000409 return 0;
410 }
411
412 /* 2. Check Block Address Translation entries (BATs) */
413 if (env->nb_BATs != 0) {
David Gibson7ef23062016-01-14 15:33:27 +1100414 raddr = ppc_hash32_bat_lookup(cpu, eaddr, rwx, &prot);
David Gibsoncaa597b2013-03-12 00:31:46 +0000415 if (raddr != -1) {
416 if (need_prot[rwx] & ~prot) {
417 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200418 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000419 env->error_code = 0x08000000;
420 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200421 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000422 env->error_code = 0;
423 env->spr[SPR_DAR] = eaddr;
424 if (rwx == 1) {
425 env->spr[SPR_DSISR] = 0x0a000000;
426 } else {
427 env->spr[SPR_DSISR] = 0x08000000;
428 }
429 }
430 return 1;
David Gibsone01b4442013-03-12 00:31:40 +0000431 }
David Gibsoncaa597b2013-03-12 00:31:46 +0000432
Andreas Färber0c591eb2013-09-03 13:59:37 +0200433 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000434 raddr & TARGET_PAGE_MASK, prot, mmu_idx,
435 TARGET_PAGE_SIZE);
David Gibsone01b4442013-03-12 00:31:40 +0000436 return 0;
David Gibson65d61642013-03-12 00:31:23 +0000437 }
438 }
439
David Gibson4b9605a2013-03-12 00:31:24 +0000440 /* 3. Look up the Segment Register */
David Gibson04808842013-03-12 00:31:09 +0000441 sr = env->sr[eaddr >> 28];
David Gibson4b9605a2013-03-12 00:31:24 +0000442
David Gibson723ed732013-03-12 00:31:25 +0000443 /* 4. Handle direct store segments */
444 if (sr & SR32_T) {
David Gibson7ef23062016-01-14 15:33:27 +1100445 if (ppc_hash32_direct_store(cpu, sr, eaddr, rwx,
David Gibsoncaa597b2013-03-12 00:31:46 +0000446 &raddr, &prot) == 0) {
Andreas Färber0c591eb2013-09-03 13:59:37 +0200447 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000448 raddr & TARGET_PAGE_MASK, prot, mmu_idx,
449 TARGET_PAGE_SIZE);
450 return 0;
451 } else {
452 return 1;
453 }
David Gibson723ed732013-03-12 00:31:25 +0000454 }
455
David Gibsonbb218042013-03-12 00:31:26 +0000456 /* 5. Check for segment level no-execute violation */
David Gibsone01b4442013-03-12 00:31:40 +0000457 if ((rwx == 2) && (sr & SR32_NX)) {
Andreas Färber27103422013-08-26 08:31:06 +0200458 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000459 env->error_code = 0x10000000;
460 return 1;
David Gibsonbb218042013-03-12 00:31:26 +0000461 }
David Gibson7f3bdc22013-03-12 00:31:30 +0000462
463 /* 6. Locate the PTE in the hash table */
David Gibson7ef23062016-01-14 15:33:27 +1100464 pte_offset = ppc_hash32_htab_lookup(cpu, sr, eaddr, &pte);
David Gibson7f3bdc22013-03-12 00:31:30 +0000465 if (pte_offset == -1) {
David Gibsoncaa597b2013-03-12 00:31:46 +0000466 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200467 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000468 env->error_code = 0x40000000;
469 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200470 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000471 env->error_code = 0;
472 env->spr[SPR_DAR] = eaddr;
473 if (rwx == 1) {
474 env->spr[SPR_DSISR] = 0x42000000;
475 } else {
476 env->spr[SPR_DSISR] = 0x40000000;
477 }
478 }
479
480 return 1;
David Gibson7f3bdc22013-03-12 00:31:30 +0000481 }
Antony Pavlov339aaf52014-12-13 19:48:18 +0300482 qemu_log_mask(CPU_LOG_MMU,
483 "found PTE at offset %08" HWADDR_PRIx "\n", pte_offset);
David Gibson7f3bdc22013-03-12 00:31:30 +0000484
485 /* 7. Check access permissions */
David Gibson6a980112013-03-12 00:31:32 +0000486
David Gibson7ef23062016-01-14 15:33:27 +1100487 prot = ppc_hash32_pte_prot(cpu, sr, pte);
David Gibson6a980112013-03-12 00:31:32 +0000488
David Gibsoncaa597b2013-03-12 00:31:46 +0000489 if (need_prot[rwx] & ~prot) {
David Gibson6a980112013-03-12 00:31:32 +0000490 /* Access right violation */
Antony Pavlov339aaf52014-12-13 19:48:18 +0300491 qemu_log_mask(CPU_LOG_MMU, "PTE access rejected\n");
David Gibsoncaa597b2013-03-12 00:31:46 +0000492 if (rwx == 2) {
Andreas Färber27103422013-08-26 08:31:06 +0200493 cs->exception_index = POWERPC_EXCP_ISI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000494 env->error_code = 0x08000000;
495 } else {
Andreas Färber27103422013-08-26 08:31:06 +0200496 cs->exception_index = POWERPC_EXCP_DSI;
David Gibsoncaa597b2013-03-12 00:31:46 +0000497 env->error_code = 0;
498 env->spr[SPR_DAR] = eaddr;
499 if (rwx == 1) {
500 env->spr[SPR_DSISR] = 0x0a000000;
501 } else {
502 env->spr[SPR_DSISR] = 0x08000000;
503 }
504 }
505 return 1;
David Gibson6a980112013-03-12 00:31:32 +0000506 }
507
Antony Pavlov339aaf52014-12-13 19:48:18 +0300508 qemu_log_mask(CPU_LOG_MMU, "PTE access granted !\n");
David Gibson87dc3fd2013-03-12 00:31:38 +0000509
510 /* 8. Update PTE referenced and changed bits if necessary */
511
David Gibsonb3440742013-03-12 00:31:42 +0000512 new_pte1 = pte.pte1 | HPTE32_R_R; /* set referenced bit */
513 if (rwx == 1) {
514 new_pte1 |= HPTE32_R_C; /* set changed (dirty) bit */
515 } else {
516 /* Treat the page as read-only for now, so that a later write
517 * will pass through this function again to set the C bit */
David Gibsoncaa597b2013-03-12 00:31:46 +0000518 prot &= ~PAGE_WRITE;
David Gibsonb3440742013-03-12 00:31:42 +0000519 }
520
521 if (new_pte1 != pte.pte1) {
David Gibson7ef23062016-01-14 15:33:27 +1100522 ppc_hash32_store_hpte1(cpu, pte_offset, new_pte1);
David Gibson7f3bdc22013-03-12 00:31:30 +0000523 }
David Gibson4b9605a2013-03-12 00:31:24 +0000524
David Gibson6d11d992013-03-12 00:31:43 +0000525 /* 9. Determine the real address from the PTE */
526
David Gibsoncaa597b2013-03-12 00:31:46 +0000527 raddr = ppc_hash32_pte_raddr(sr, pte, eaddr);
528
Andreas Färber0c591eb2013-09-03 13:59:37 +0200529 tlb_set_page(cs, eaddr & TARGET_PAGE_MASK, raddr & TARGET_PAGE_MASK,
David Gibsoncaa597b2013-03-12 00:31:46 +0000530 prot, mmu_idx, TARGET_PAGE_SIZE);
David Gibsone01b4442013-03-12 00:31:40 +0000531
532 return 0;
David Gibson04808842013-03-12 00:31:09 +0000533}
David Gibson629bd512013-03-12 00:31:11 +0000534
David Gibson7ef23062016-01-14 15:33:27 +1100535hwaddr ppc_hash32_get_phys_page_debug(PowerPCCPU *cpu, target_ulong eaddr)
David Gibsonf2ad6be2013-03-12 00:31:13 +0000536{
David Gibson7ef23062016-01-14 15:33:27 +1100537 CPUPPCState *env = &cpu->env;
David Gibson5883d8b2013-03-12 00:31:45 +0000538 target_ulong sr;
539 hwaddr pte_offset;
540 ppc_hash_pte32_t pte;
541 int prot;
David Gibsonf2ad6be2013-03-12 00:31:13 +0000542
David Gibson5883d8b2013-03-12 00:31:45 +0000543 if (msr_dr == 0) {
544 /* Translation is off */
545 return eaddr;
546 }
547
548 if (env->nb_BATs != 0) {
David Gibson7ef23062016-01-14 15:33:27 +1100549 hwaddr raddr = ppc_hash32_bat_lookup(cpu, eaddr, 0, &prot);
David Gibson5883d8b2013-03-12 00:31:45 +0000550 if (raddr != -1) {
551 return raddr;
552 }
553 }
554
555 sr = env->sr[eaddr >> 28];
556
557 if (sr & SR32_T) {
558 /* FIXME: Add suitable debug support for Direct Store segments */
David Gibsonf2ad6be2013-03-12 00:31:13 +0000559 return -1;
560 }
561
David Gibson7ef23062016-01-14 15:33:27 +1100562 pte_offset = ppc_hash32_htab_lookup(cpu, sr, eaddr, &pte);
David Gibson5883d8b2013-03-12 00:31:45 +0000563 if (pte_offset == -1) {
564 return -1;
565 }
566
567 return ppc_hash32_pte_raddr(sr, pte, eaddr) & TARGET_PAGE_MASK;
David Gibsonf2ad6be2013-03-12 00:31:13 +0000568}