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"
23 #include "kvm_s390x.h"
24 #include "sysemu/kvm.h"
25 #include "exec/exec-all.h"
27 #include "hw/s390x/storage-keys.h"
29 /* #define DEBUG_S390 */
30 /* #define DEBUG_S390_PTE */
31 /* #define DEBUG_S390_STDOUT */
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)
39 #define DPRINTF(fmt, ...) \
40 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
43 #define DPRINTF(fmt, ...) \
48 #define PTE_DPRINTF DPRINTF
50 #define PTE_DPRINTF(fmt, ...) \
54 /* Fetch/store bits in the translation exception code: */
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
);
64 kvm_s390_access_exception(cpu
, type
, tec
);
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
)
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
);
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
)
96 tec
= vaddr
| (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
) | asc
>> 46;
98 DPRINTF("%s: trans_exc_code=%016" PRIx64
"\n", __func__
, tec
);
104 /* Code accesses have an undefined ilc. */
105 if (rw
== MMU_INST_FETCH
) {
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
)) {
124 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
128 /* Check the private-space control bit */
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
);
135 return !(env
->cregs
[13] & _ASCE_PRIVATE_SPACE
);
137 /* We don't support access register mode */
138 error_report("unsupported addressing mode");
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. */
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
);
167 if (pt_entry
& _PAGE_RES0
) {
168 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
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
);
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
,
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
);
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
,
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
);
239 if ((new_entry
& _REGION_ENTRY_TYPE_MASK
) != level
) {
240 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
244 if (level
== _ASCE_TYPE_SEGMENT
) {
245 return mmu_translate_segment(env
, vaddr
, asc
, new_entry
, raddr
, flags
,
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
);
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
)
274 if (asce
& _ASCE_REAL_SPACE
) {
280 level
= asce
& _ASCE_TYPE_MASK
;
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
);
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
);
295 if ((vaddr
>> 51 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
296 trigger_page_fault(env
, vaddr
, PGM_REG_SEC_TRANS
, asc
, rw
, exc
);
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
);
307 if ((vaddr
>> 40 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
308 trigger_page_fault(env
, vaddr
, PGM_REG_THIRD_TRANS
, asc
, rw
, exc
);
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
);
319 if ((vaddr
>> 29 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
320 trigger_page_fault(env
, vaddr
, PGM_SEGMENT_TRANS
, asc
, rw
, exc
);
326 r
= mmu_translate_region(env
, vaddr
, asc
, asce
, level
, raddr
, flags
, rw
,
328 if (rw
== MMU_DATA_STORE
&& !(*flags
& PAGE_WRITE
)) {
329 trigger_prot_fault(env
, vaddr
, asc
, rw
, exc
);
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
;
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
) {
372 trigger_access_exception(env
, PGM_PROTECTION
, ILEN_AUTO
, 0);
378 vaddr
&= TARGET_PAGE_MASK
;
380 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
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
,
393 PTE_DPRINTF("%s: asc=home\n", __func__
);
394 r
= mmu_translate_asce(env
, vaddr
, asc
, env
->cregs
[13], raddr
, flags
,
397 case PSW_ASC_SECONDARY
:
398 PTE_DPRINTF("%s: asc=secondary\n", __func__
);
400 * Instruction: Primary
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
);
408 r
= mmu_translate_asce(env
, vaddr
, PSW_ASC_SECONDARY
, env
->cregs
[7],
409 raddr
, flags
, rw
, exc
);
410 *flags
&= ~(PAGE_EXEC
);
415 hw_error("guest switched to unknown asc mode\n");
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
);
429 if (*flags
& PAGE_READ
) {
433 if (*flags
& PAGE_WRITE
) {
437 if (skeyclass
->set_skeys(ss
, *raddr
/ TARGET_PAGE_SIZE
, 1, &key
)) {
438 trace_set_skeys_nonzero(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
;
458 for (i
= 0; i
< nr_pages
; i
++) {
459 ret
= mmu_translate(env
, addr
, is_write
, asc
, &pages
[i
], &pflags
, true);
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);
468 addr
+= TARGET_PAGE_SIZE
;
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
;
497 ret
= kvm_s390_mem_op(cpu
, laddr
, ar
, hostbuf
, len
, is_write
);
503 nr_pages
= (((laddr
& ~TARGET_PAGE_MASK
) + len
- 1) >> TARGET_PAGE_BITS
)
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
);
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 */
529 cpu_loop_exit_restore(CPU(cpu
), ra
);
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);
557 *addr
= mmu_real2abs(env
, raddr
& TARGET_PAGE_MASK
);
559 /* TODO: storage key handling */