s390x/flic: optimize CPU wakeup for TCG
[qemu/ar7.git] / target / s390x / mmu_helper.c
blobf477cc006a3fb556058a0b2025590c0b5bbcb871
1 /*
2 * S390x MMU related functions
4 * Copyright (c) 2011 Alexander Graf
5 * Copyright (c) 2015 Thomas Huth, IBM Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program 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
15 * GNU General Public License for more details.
18 #include "qemu/osdep.h"
19 #include "qemu/error-report.h"
20 #include "exec/address-spaces.h"
21 #include "cpu.h"
22 #include "internal.h"
23 #include "kvm_s390x.h"
24 #include "sysemu/kvm.h"
25 #include "exec/exec-all.h"
26 #include "trace.h"
27 #include "hw/s390x/storage-keys.h"
29 /* #define DEBUG_S390 */
30 /* #define DEBUG_S390_PTE */
31 /* #define DEBUG_S390_STDOUT */
33 #ifdef DEBUG_S390
34 #ifdef DEBUG_S390_STDOUT
35 #define DPRINTF(fmt, ...) \
36 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
37 if (qemu_log_separate()) qemu_log(fmt, ##__VA_ARGS__); } while (0)
38 #else
39 #define DPRINTF(fmt, ...) \
40 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
41 #endif
42 #else
43 #define DPRINTF(fmt, ...) \
44 do { } while (0)
45 #endif
47 #ifdef DEBUG_S390_PTE
48 #define PTE_DPRINTF DPRINTF
49 #else
50 #define PTE_DPRINTF(fmt, ...) \
51 do { } while (0)
52 #endif
54 /* Fetch/store bits in the translation exception code: */
55 #define FS_READ 0x800
56 #define FS_WRITE 0x400
58 static void trigger_access_exception(CPUS390XState *env, uint32_t type,
59 uint32_t ilen, uint64_t tec)
61 S390CPU *cpu = s390_env_get_cpu(env);
63 if (kvm_enabled()) {
64 kvm_s390_access_exception(cpu, type, tec);
65 } else {
66 CPUState *cs = CPU(cpu);
67 if (type != PGM_ADDRESSING) {
68 stq_phys(cs->as, env->psa + offsetof(LowCore, trans_exc_code), tec);
70 trigger_pgm_exception(env, type, ilen);
74 static void trigger_prot_fault(CPUS390XState *env, target_ulong vaddr,
75 uint64_t asc, int rw, bool exc)
77 uint64_t tec;
79 tec = vaddr | (rw == MMU_DATA_STORE ? FS_WRITE : FS_READ) | 4 | asc >> 46;
81 DPRINTF("%s: trans_exc_code=%016" PRIx64 "\n", __func__, tec);
83 if (!exc) {
84 return;
87 trigger_access_exception(env, PGM_PROTECTION, ILEN_AUTO, tec);
90 static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr,
91 uint32_t type, uint64_t asc, int rw, bool exc)
93 int ilen = ILEN_AUTO;
94 uint64_t tec;
96 tec = vaddr | (rw == MMU_DATA_STORE ? FS_WRITE : FS_READ) | asc >> 46;
98 DPRINTF("%s: trans_exc_code=%016" PRIx64 "\n", __func__, tec);
100 if (!exc) {
101 return;
104 /* Code accesses have an undefined ilc. */
105 if (rw == MMU_INST_FETCH) {
106 ilen = 2;
109 trigger_access_exception(env, type, ilen, tec);
112 /* check whether the address would be proteted by Low-Address Protection */
113 static bool is_low_address(uint64_t addr)
115 return addr <= 511 || (addr >= 4096 && addr <= 4607);
118 /* check whether Low-Address Protection is enabled for mmu_translate() */
119 static bool lowprot_enabled(const CPUS390XState *env, uint64_t asc)
121 if (!(env->cregs[0] & CR0_LOWPROT)) {
122 return false;
124 if (!(env->psw.mask & PSW_MASK_DAT)) {
125 return true;
128 /* Check the private-space control bit */
129 switch (asc) {
130 case PSW_ASC_PRIMARY:
131 return !(env->cregs[1] & _ASCE_PRIVATE_SPACE);
132 case PSW_ASC_SECONDARY:
133 return !(env->cregs[7] & _ASCE_PRIVATE_SPACE);
134 case PSW_ASC_HOME:
135 return !(env->cregs[13] & _ASCE_PRIVATE_SPACE);
136 default:
137 /* We don't support access register mode */
138 error_report("unsupported addressing mode");
139 exit(1);
144 * Translate real address to absolute (= physical)
145 * address by taking care of the prefix mapping.
147 target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr)
149 if (raddr < 0x2000) {
150 return raddr + env->psa; /* Map the lowcore. */
151 } else if (raddr >= env->psa && raddr < env->psa + 0x2000) {
152 return raddr - env->psa; /* Map the 0 page. */
154 return raddr;
157 /* Decode page table entry (normal 4KB page) */
158 static int mmu_translate_pte(CPUS390XState *env, target_ulong vaddr,
159 uint64_t asc, uint64_t pt_entry,
160 target_ulong *raddr, int *flags, int rw, bool exc)
162 if (pt_entry & _PAGE_INVALID) {
163 DPRINTF("%s: PTE=0x%" PRIx64 " invalid\n", __func__, pt_entry);
164 trigger_page_fault(env, vaddr, PGM_PAGE_TRANS, asc, rw, exc);
165 return -1;
167 if (pt_entry & _PAGE_RES0) {
168 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw, exc);
169 return -1;
171 if (pt_entry & _PAGE_RO) {
172 *flags &= ~PAGE_WRITE;
175 *raddr = pt_entry & _ASCE_ORIGIN;
177 PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, pt_entry);
179 return 0;
182 /* Decode segment table entry */
183 static int mmu_translate_segment(CPUS390XState *env, target_ulong vaddr,
184 uint64_t asc, uint64_t st_entry,
185 target_ulong *raddr, int *flags, int rw,
186 bool exc)
188 CPUState *cs = CPU(s390_env_get_cpu(env));
189 uint64_t origin, offs, pt_entry;
191 if (st_entry & _SEGMENT_ENTRY_RO) {
192 *flags &= ~PAGE_WRITE;
195 if ((st_entry & _SEGMENT_ENTRY_FC) && (env->cregs[0] & CR0_EDAT)) {
196 /* Decode EDAT1 segment frame absolute address (1MB page) */
197 *raddr = (st_entry & 0xfffffffffff00000ULL) | (vaddr & 0xfffff);
198 PTE_DPRINTF("%s: SEG=0x%" PRIx64 "\n", __func__, st_entry);
199 return 0;
202 /* Look up 4KB page entry */
203 origin = st_entry & _SEGMENT_ENTRY_ORIGIN;
204 offs = (vaddr & VADDR_PX) >> 9;
205 pt_entry = ldq_phys(cs->as, origin + offs);
206 PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
207 __func__, origin, offs, pt_entry);
208 return mmu_translate_pte(env, vaddr, asc, pt_entry, raddr, flags, rw, exc);
211 /* Decode region table entries */
212 static int mmu_translate_region(CPUS390XState *env, target_ulong vaddr,
213 uint64_t asc, uint64_t entry, int level,
214 target_ulong *raddr, int *flags, int rw,
215 bool exc)
217 CPUState *cs = CPU(s390_env_get_cpu(env));
218 uint64_t origin, offs, new_entry;
219 const int pchks[4] = {
220 PGM_SEGMENT_TRANS, PGM_REG_THIRD_TRANS,
221 PGM_REG_SEC_TRANS, PGM_REG_FIRST_TRANS
224 PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, entry);
226 origin = entry & _REGION_ENTRY_ORIGIN;
227 offs = (vaddr >> (17 + 11 * level / 4)) & 0x3ff8;
229 new_entry = ldq_phys(cs->as, origin + offs);
230 PTE_DPRINTF("%s: 0x%" PRIx64 " + 0x%" PRIx64 " => 0x%016" PRIx64 "\n",
231 __func__, origin, offs, new_entry);
233 if ((new_entry & _REGION_ENTRY_INV) != 0) {
234 DPRINTF("%s: invalid region\n", __func__);
235 trigger_page_fault(env, vaddr, pchks[level / 4], asc, rw, exc);
236 return -1;
239 if ((new_entry & _REGION_ENTRY_TYPE_MASK) != level) {
240 trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw, exc);
241 return -1;
244 if (level == _ASCE_TYPE_SEGMENT) {
245 return mmu_translate_segment(env, vaddr, asc, new_entry, raddr, flags,
246 rw, exc);
249 /* Check region table offset and length */
250 offs = (vaddr >> (28 + 11 * (level - 4) / 4)) & 3;
251 if (offs < ((new_entry & _REGION_ENTRY_TF) >> 6)
252 || offs > (new_entry & _REGION_ENTRY_LENGTH)) {
253 DPRINTF("%s: invalid offset or len (%lx)\n", __func__, new_entry);
254 trigger_page_fault(env, vaddr, pchks[level / 4 - 1], asc, rw, exc);
255 return -1;
258 if ((env->cregs[0] & CR0_EDAT) && (new_entry & _REGION_ENTRY_RO)) {
259 *flags &= ~PAGE_WRITE;
262 /* yet another region */
263 return mmu_translate_region(env, vaddr, asc, new_entry, level - 4,
264 raddr, flags, rw, exc);
267 static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
268 uint64_t asc, uint64_t asce, target_ulong *raddr,
269 int *flags, int rw, bool exc)
271 int level;
272 int r;
274 if (asce & _ASCE_REAL_SPACE) {
275 /* direct mapping */
276 *raddr = vaddr;
277 return 0;
280 level = asce & _ASCE_TYPE_MASK;
281 switch (level) {
282 case _ASCE_TYPE_REGION1:
283 if ((vaddr >> 62) > (asce & _ASCE_TABLE_LENGTH)) {
284 trigger_page_fault(env, vaddr, PGM_REG_FIRST_TRANS, asc, rw, exc);
285 return -1;
287 break;
288 case _ASCE_TYPE_REGION2:
289 if (vaddr & 0xffe0000000000000ULL) {
290 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
291 " 0xffe0000000000000ULL\n", __func__, vaddr);
292 trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc);
293 return -1;
295 if ((vaddr >> 51 & 3) > (asce & _ASCE_TABLE_LENGTH)) {
296 trigger_page_fault(env, vaddr, PGM_REG_SEC_TRANS, asc, rw, exc);
297 return -1;
299 break;
300 case _ASCE_TYPE_REGION3:
301 if (vaddr & 0xfffffc0000000000ULL) {
302 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
303 " 0xfffffc0000000000ULL\n", __func__, vaddr);
304 trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc);
305 return -1;
307 if ((vaddr >> 40 & 3) > (asce & _ASCE_TABLE_LENGTH)) {
308 trigger_page_fault(env, vaddr, PGM_REG_THIRD_TRANS, asc, rw, exc);
309 return -1;
311 break;
312 case _ASCE_TYPE_SEGMENT:
313 if (vaddr & 0xffffffff80000000ULL) {
314 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
315 " 0xffffffff80000000ULL\n", __func__, vaddr);
316 trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc);
317 return -1;
319 if ((vaddr >> 29 & 3) > (asce & _ASCE_TABLE_LENGTH)) {
320 trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw, exc);
321 return -1;
323 break;
326 r = mmu_translate_region(env, vaddr, asc, asce, level, raddr, flags, rw,
327 exc);
328 if (rw == MMU_DATA_STORE && !(*flags & PAGE_WRITE)) {
329 trigger_prot_fault(env, vaddr, asc, rw, exc);
330 return -1;
333 return r;
337 * Translate a virtual (logical) address into a physical (absolute) address.
338 * @param vaddr the virtual address
339 * @param rw 0 = read, 1 = write, 2 = code fetch
340 * @param asc address space control (one of the PSW_ASC_* modes)
341 * @param raddr the translated address is stored to this pointer
342 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
343 * @param exc true = inject a program check if a fault occurred
344 * @return 0 if the translation was successful, -1 if a fault occurred
346 int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
347 target_ulong *raddr, int *flags, bool exc)
349 static S390SKeysState *ss;
350 static S390SKeysClass *skeyclass;
351 int r = -1;
352 uint8_t key;
354 if (unlikely(!ss)) {
355 ss = s390_get_skeys_device();
356 skeyclass = S390_SKEYS_GET_CLASS(ss);
359 *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
360 if (is_low_address(vaddr & TARGET_PAGE_MASK) && lowprot_enabled(env, asc)) {
362 * If any part of this page is currently protected, make sure the
363 * TLB entry will not be reused.
365 * As the protected range is always the first 512 bytes of the
366 * two first pages, we are able to catch all writes to these areas
367 * just by looking at the start address (triggering the tlb miss).
369 *flags |= PAGE_WRITE_INV;
370 if (is_low_address(vaddr) && rw == MMU_DATA_STORE) {
371 if (exc) {
372 trigger_access_exception(env, PGM_PROTECTION, ILEN_AUTO, 0);
374 return -EACCES;
378 vaddr &= TARGET_PAGE_MASK;
380 if (!(env->psw.mask & PSW_MASK_DAT)) {
381 *raddr = vaddr;
382 r = 0;
383 goto out;
386 switch (asc) {
387 case PSW_ASC_PRIMARY:
388 PTE_DPRINTF("%s: asc=primary\n", __func__);
389 r = mmu_translate_asce(env, vaddr, asc, env->cregs[1], raddr, flags,
390 rw, exc);
391 break;
392 case PSW_ASC_HOME:
393 PTE_DPRINTF("%s: asc=home\n", __func__);
394 r = mmu_translate_asce(env, vaddr, asc, env->cregs[13], raddr, flags,
395 rw, exc);
396 break;
397 case PSW_ASC_SECONDARY:
398 PTE_DPRINTF("%s: asc=secondary\n", __func__);
400 * Instruction: Primary
401 * Data: Secondary
403 if (rw == MMU_INST_FETCH) {
404 r = mmu_translate_asce(env, vaddr, PSW_ASC_PRIMARY, env->cregs[1],
405 raddr, flags, rw, exc);
406 *flags &= ~(PAGE_READ | PAGE_WRITE);
407 } else {
408 r = mmu_translate_asce(env, vaddr, PSW_ASC_SECONDARY, env->cregs[7],
409 raddr, flags, rw, exc);
410 *flags &= ~(PAGE_EXEC);
412 break;
413 case PSW_ASC_ACCREG:
414 default:
415 hw_error("guest switched to unknown asc mode\n");
416 break;
419 out:
420 /* Convert real address -> absolute address */
421 *raddr = mmu_real2abs(env, *raddr);
423 if (r == 0 && *raddr < ram_size) {
424 if (skeyclass->get_skeys(ss, *raddr / TARGET_PAGE_SIZE, 1, &key)) {
425 trace_get_skeys_nonzero(r);
426 return 0;
429 if (*flags & PAGE_READ) {
430 key |= SK_R;
433 if (*flags & PAGE_WRITE) {
434 key |= SK_C;
437 if (skeyclass->set_skeys(ss, *raddr / TARGET_PAGE_SIZE, 1, &key)) {
438 trace_set_skeys_nonzero(r);
439 return 0;
443 return r;
447 * translate_pages: Translate a set of consecutive logical page addresses
448 * to absolute addresses. This function is used for TCG and old KVM without
449 * the MEMOP interface.
451 static int translate_pages(S390CPU *cpu, vaddr addr, int nr_pages,
452 target_ulong *pages, bool is_write)
454 uint64_t asc = cpu->env.psw.mask & PSW_MASK_ASC;
455 CPUS390XState *env = &cpu->env;
456 int ret, i, pflags;
458 for (i = 0; i < nr_pages; i++) {
459 ret = mmu_translate(env, addr, is_write, asc, &pages[i], &pflags, true);
460 if (ret) {
461 return ret;
463 if (!address_space_access_valid(&address_space_memory, pages[i],
464 TARGET_PAGE_SIZE, is_write)) {
465 trigger_access_exception(env, PGM_ADDRESSING, ILEN_AUTO, 0);
466 return -EFAULT;
468 addr += TARGET_PAGE_SIZE;
471 return 0;
475 * s390_cpu_virt_mem_rw:
476 * @laddr: the logical start address
477 * @ar: the access register number
478 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
479 * @len: length that should be transferred
480 * @is_write: true = write, false = read
481 * Returns: 0 on success, non-zero if an exception occurred
483 * Copy from/to guest memory using logical addresses. Note that we inject a
484 * program interrupt in case there is an error while accessing the memory.
486 * This function will always return (also for TCG), make sure to call
487 * s390_cpu_virt_mem_handle_exc() to properly exit the CPU loop.
489 int s390_cpu_virt_mem_rw(S390CPU *cpu, vaddr laddr, uint8_t ar, void *hostbuf,
490 int len, bool is_write)
492 int currlen, nr_pages, i;
493 target_ulong *pages;
494 int ret;
496 if (kvm_enabled()) {
497 ret = kvm_s390_mem_op(cpu, laddr, ar, hostbuf, len, is_write);
498 if (ret >= 0) {
499 return ret;
503 nr_pages = (((laddr & ~TARGET_PAGE_MASK) + len - 1) >> TARGET_PAGE_BITS)
504 + 1;
505 pages = g_malloc(nr_pages * sizeof(*pages));
507 ret = translate_pages(cpu, laddr, nr_pages, pages, is_write);
508 if (ret == 0 && hostbuf != NULL) {
509 /* Copy data by stepping through the area page by page */
510 for (i = 0; i < nr_pages; i++) {
511 currlen = MIN(len, TARGET_PAGE_SIZE - (laddr % TARGET_PAGE_SIZE));
512 cpu_physical_memory_rw(pages[i] | (laddr & ~TARGET_PAGE_MASK),
513 hostbuf, currlen, is_write);
514 laddr += currlen;
515 hostbuf += currlen;
516 len -= currlen;
520 g_free(pages);
521 return ret;
524 void s390_cpu_virt_mem_handle_exc(S390CPU *cpu, uintptr_t ra)
526 /* KVM will handle the interrupt automatically, TCG has to exit the TB */
527 #ifdef CONFIG_TCG
528 if (tcg_enabled()) {
529 cpu_loop_exit_restore(CPU(cpu), ra);
531 #endif
535 * Translate a real address into a physical (absolute) address.
536 * @param raddr the real address
537 * @param rw 0 = read, 1 = write, 2 = code fetch
538 * @param addr the translated address is stored to this pointer
539 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
540 * @return 0 if the translation was successful, < 0 if a fault occurred
542 int mmu_translate_real(CPUS390XState *env, target_ulong raddr, int rw,
543 target_ulong *addr, int *flags)
545 const bool lowprot_enabled = env->cregs[0] & CR0_LOWPROT;
547 *flags = PAGE_READ | PAGE_WRITE;
548 if (is_low_address(raddr & TARGET_PAGE_MASK) && lowprot_enabled) {
549 /* see comment in mmu_translate() how this works */
550 *flags |= PAGE_WRITE_INV;
551 if (is_low_address(raddr) && rw == MMU_DATA_STORE) {
552 trigger_access_exception(env, PGM_PROTECTION, ILEN_AUTO, 0);
553 return -EACCES;
557 *addr = mmu_real2abs(env, raddr & TARGET_PAGE_MASK);
559 /* TODO: storage key handling */
560 return 0;