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 "sysemu/tcg.h"
26 #include "exec/exec-all.h"
29 #include "hw/s390x/storage-keys.h"
30 #include "hw/boards.h"
32 /* Fetch/store bits in the translation exception code: */
34 #define FS_WRITE 0x400
36 static void trigger_access_exception(CPUS390XState
*env
, uint32_t type
,
39 S390CPU
*cpu
= env_archcpu(env
);
42 kvm_s390_access_exception(cpu
, type
, tec
);
44 CPUState
*cs
= env_cpu(env
);
45 if (type
!= PGM_ADDRESSING
) {
46 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
48 trigger_pgm_exception(env
, type
);
52 /* check whether the address would be proteted by Low-Address Protection */
53 static bool is_low_address(uint64_t addr
)
55 return addr
<= 511 || (addr
>= 4096 && addr
<= 4607);
58 /* check whether Low-Address Protection is enabled for mmu_translate() */
59 static bool lowprot_enabled(const CPUS390XState
*env
, uint64_t asc
)
61 if (!(env
->cregs
[0] & CR0_LOWPROT
)) {
64 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
68 /* Check the private-space control bit */
71 return !(env
->cregs
[1] & ASCE_PRIVATE_SPACE
);
72 case PSW_ASC_SECONDARY
:
73 return !(env
->cregs
[7] & ASCE_PRIVATE_SPACE
);
75 return !(env
->cregs
[13] & ASCE_PRIVATE_SPACE
);
77 /* We don't support access register mode */
78 error_report("unsupported addressing mode");
84 * Translate real address to absolute (= physical)
85 * address by taking care of the prefix mapping.
87 target_ulong
mmu_real2abs(CPUS390XState
*env
, target_ulong raddr
)
90 return raddr
+ env
->psa
; /* Map the lowcore. */
91 } else if (raddr
>= env
->psa
&& raddr
< env
->psa
+ 0x2000) {
92 return raddr
- env
->psa
; /* Map the 0 page. */
97 static inline bool read_table_entry(CPUS390XState
*env
, hwaddr gaddr
,
100 CPUState
*cs
= env_cpu(env
);
103 * According to the PoP, these table addresses are "unpredictably real
104 * or absolute". Also, "it is unpredictable whether the address wraps
105 * or an addressing exception is recognized".
107 * We treat them as absolute addresses and don't wrap them.
109 if (unlikely(address_space_read(cs
->as
, gaddr
, MEMTXATTRS_UNSPECIFIED
,
110 entry
, sizeof(*entry
)) !=
114 *entry
= be64_to_cpu(*entry
);
118 static int mmu_translate_asce(CPUS390XState
*env
, target_ulong vaddr
,
119 uint64_t asc
, uint64_t asce
, target_ulong
*raddr
,
122 const bool edat1
= (env
->cregs
[0] & CR0_EDAT
) &&
123 s390_has_feat(S390_FEAT_EDAT
);
124 const bool edat2
= edat1
&& s390_has_feat(S390_FEAT_EDAT_2
);
125 const bool iep
= (env
->cregs
[0] & CR0_IEP
) &&
126 s390_has_feat(S390_FEAT_INSTRUCTION_EXEC_PROT
);
127 const int asce_tl
= asce
& ASCE_TABLE_LENGTH
;
128 const int asce_p
= asce
& ASCE_PRIVATE_SPACE
;
129 hwaddr gaddr
= asce
& ASCE_ORIGIN
;
132 if (asce
& ASCE_REAL_SPACE
) {
138 switch (asce
& ASCE_TYPE_MASK
) {
139 case ASCE_TYPE_REGION1
:
140 if (VADDR_REGION1_TL(vaddr
) > asce_tl
) {
141 return PGM_REG_FIRST_TRANS
;
143 gaddr
+= VADDR_REGION1_TX(vaddr
) * 8;
145 case ASCE_TYPE_REGION2
:
146 if (VADDR_REGION1_TX(vaddr
)) {
147 return PGM_ASCE_TYPE
;
149 if (VADDR_REGION2_TL(vaddr
) > asce_tl
) {
150 return PGM_REG_SEC_TRANS
;
152 gaddr
+= VADDR_REGION2_TX(vaddr
) * 8;
154 case ASCE_TYPE_REGION3
:
155 if (VADDR_REGION1_TX(vaddr
) || VADDR_REGION2_TX(vaddr
)) {
156 return PGM_ASCE_TYPE
;
158 if (VADDR_REGION3_TL(vaddr
) > asce_tl
) {
159 return PGM_REG_THIRD_TRANS
;
161 gaddr
+= VADDR_REGION3_TX(vaddr
) * 8;
163 case ASCE_TYPE_SEGMENT
:
164 if (VADDR_REGION1_TX(vaddr
) || VADDR_REGION2_TX(vaddr
) ||
165 VADDR_REGION3_TX(vaddr
)) {
166 return PGM_ASCE_TYPE
;
168 if (VADDR_SEGMENT_TL(vaddr
) > asce_tl
) {
169 return PGM_SEGMENT_TRANS
;
171 gaddr
+= VADDR_SEGMENT_TX(vaddr
) * 8;
175 switch (asce
& ASCE_TYPE_MASK
) {
176 case ASCE_TYPE_REGION1
:
177 if (!read_table_entry(env
, gaddr
, &entry
)) {
178 return PGM_ADDRESSING
;
180 if (entry
& REGION_ENTRY_I
) {
181 return PGM_REG_FIRST_TRANS
;
183 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION1
) {
184 return PGM_TRANS_SPEC
;
186 if (VADDR_REGION2_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
187 VADDR_REGION2_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
188 return PGM_REG_SEC_TRANS
;
190 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
191 *flags
&= ~PAGE_WRITE
;
193 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_REGION2_TX(vaddr
) * 8;
195 case ASCE_TYPE_REGION2
:
196 if (!read_table_entry(env
, gaddr
, &entry
)) {
197 return PGM_ADDRESSING
;
199 if (entry
& REGION_ENTRY_I
) {
200 return PGM_REG_SEC_TRANS
;
202 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION2
) {
203 return PGM_TRANS_SPEC
;
205 if (VADDR_REGION3_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
206 VADDR_REGION3_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
207 return PGM_REG_THIRD_TRANS
;
209 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
210 *flags
&= ~PAGE_WRITE
;
212 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_REGION3_TX(vaddr
) * 8;
214 case ASCE_TYPE_REGION3
:
215 if (!read_table_entry(env
, gaddr
, &entry
)) {
216 return PGM_ADDRESSING
;
218 if (entry
& REGION_ENTRY_I
) {
219 return PGM_REG_THIRD_TRANS
;
221 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION3
) {
222 return PGM_TRANS_SPEC
;
224 if (edat2
&& (entry
& REGION3_ENTRY_CR
) && asce_p
) {
225 return PGM_TRANS_SPEC
;
227 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
228 *flags
&= ~PAGE_WRITE
;
230 if (edat2
&& (entry
& REGION3_ENTRY_FC
)) {
231 if (iep
&& (entry
& REGION3_ENTRY_IEP
)) {
232 *flags
&= ~PAGE_EXEC
;
234 *raddr
= (entry
& REGION3_ENTRY_RFAA
) |
235 (vaddr
& ~REGION3_ENTRY_RFAA
);
238 if (VADDR_SEGMENT_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
239 VADDR_SEGMENT_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
240 return PGM_SEGMENT_TRANS
;
242 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_SEGMENT_TX(vaddr
) * 8;
244 case ASCE_TYPE_SEGMENT
:
245 if (!read_table_entry(env
, gaddr
, &entry
)) {
246 return PGM_ADDRESSING
;
248 if (entry
& SEGMENT_ENTRY_I
) {
249 return PGM_SEGMENT_TRANS
;
251 if ((entry
& SEGMENT_ENTRY_TT
) != SEGMENT_ENTRY_TT_SEGMENT
) {
252 return PGM_TRANS_SPEC
;
254 if ((entry
& SEGMENT_ENTRY_CS
) && asce_p
) {
255 return PGM_TRANS_SPEC
;
257 if (entry
& SEGMENT_ENTRY_P
) {
258 *flags
&= ~PAGE_WRITE
;
260 if (edat1
&& (entry
& SEGMENT_ENTRY_FC
)) {
261 if (iep
&& (entry
& SEGMENT_ENTRY_IEP
)) {
262 *flags
&= ~PAGE_EXEC
;
264 *raddr
= (entry
& SEGMENT_ENTRY_SFAA
) |
265 (vaddr
& ~SEGMENT_ENTRY_SFAA
);
268 gaddr
= (entry
& SEGMENT_ENTRY_ORIGIN
) + VADDR_PAGE_TX(vaddr
) * 8;
272 if (!read_table_entry(env
, gaddr
, &entry
)) {
273 return PGM_ADDRESSING
;
275 if (entry
& PAGE_ENTRY_I
) {
276 return PGM_PAGE_TRANS
;
278 if (entry
& PAGE_ENTRY_0
) {
279 return PGM_TRANS_SPEC
;
281 if (entry
& PAGE_ENTRY_P
) {
282 *flags
&= ~PAGE_WRITE
;
284 if (iep
&& (entry
& PAGE_ENTRY_IEP
)) {
285 *flags
&= ~PAGE_EXEC
;
288 *raddr
= entry
& TARGET_PAGE_MASK
;
292 static void mmu_handle_skey(target_ulong addr
, int rw
, int *flags
)
294 static S390SKeysClass
*skeyclass
;
295 static S390SKeysState
*ss
;
296 MachineState
*ms
= MACHINE(qdev_get_machine());
300 if (unlikely(addr
>= ms
->ram_size
)) {
305 ss
= s390_get_skeys_device();
306 skeyclass
= S390_SKEYS_GET_CLASS(ss
);
310 * Whenever we create a new TLB entry, we set the storage key reference
311 * bit. In case we allow write accesses, we set the storage key change
312 * bit. Whenever the guest changes the storage key, we have to flush the
313 * TLBs of all CPUs (the whole TLB or all affected entries), so that the
314 * next reference/change will result in an MMU fault and make us properly
315 * update the storage key here.
317 * Note 1: "record of references ... is not necessarily accurate",
318 * "change bit may be set in case no storing has occurred".
319 * -> We can set reference/change bits even on exceptions.
320 * Note 2: certain accesses seem to ignore storage keys. For example,
321 * DAT translation does not set reference bits for table accesses.
323 * TODO: key-controlled protection. Only CPU accesses make use of the
324 * PSW key. CSS accesses are different - we have to pass in the key.
326 * TODO: we have races between getting and setting the key.
328 rc
= skeyclass
->get_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
330 trace_get_skeys_nonzero(rc
);
338 * The TLB entry has to remain write-protected on read-faults if
339 * the storage key does not indicate a change already. Otherwise
340 * we might miss setting the change bit on write accesses.
343 *flags
&= ~PAGE_WRITE
;
350 g_assert_not_reached();
353 /* Any store/fetch sets the reference bit */
356 rc
= skeyclass
->set_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
358 trace_set_skeys_nonzero(rc
);
363 * Translate a virtual (logical) address into a physical (absolute) address.
364 * @param vaddr the virtual address
365 * @param rw 0 = read, 1 = write, 2 = code fetch
366 * @param asc address space control (one of the PSW_ASC_* modes)
367 * @param raddr the translated address is stored to this pointer
368 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
369 * @param exc true = inject a program check if a fault occurred
370 * @return 0 = success, != 0, the exception to raise
372 int mmu_translate(CPUS390XState
*env
, target_ulong vaddr
, int rw
, uint64_t asc
,
373 target_ulong
*raddr
, int *flags
, uint64_t *tec
)
378 *tec
= (vaddr
& TARGET_PAGE_MASK
) | (asc
>> 46) |
379 (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
);
380 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
382 if (is_low_address(vaddr
& TARGET_PAGE_MASK
) && lowprot_enabled(env
, asc
)) {
384 * If any part of this page is currently protected, make sure the
385 * TLB entry will not be reused.
387 * As the protected range is always the first 512 bytes of the
388 * two first pages, we are able to catch all writes to these areas
389 * just by looking at the start address (triggering the tlb miss).
391 *flags
|= PAGE_WRITE_INV
;
392 if (is_low_address(vaddr
) && rw
== MMU_DATA_STORE
) {
393 /* LAP sets bit 56 */
395 return PGM_PROTECTION
;
399 vaddr
&= TARGET_PAGE_MASK
;
401 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
407 case PSW_ASC_PRIMARY
:
408 asce
= env
->cregs
[1];
411 asce
= env
->cregs
[13];
413 case PSW_ASC_SECONDARY
:
414 asce
= env
->cregs
[7];
418 hw_error("guest switched to unknown asc mode\n");
422 /* perform the DAT translation */
423 r
= mmu_translate_asce(env
, vaddr
, asc
, asce
, raddr
, flags
, rw
);
428 /* check for DAT protection */
429 if (unlikely(rw
== MMU_DATA_STORE
&& !(*flags
& PAGE_WRITE
))) {
430 /* DAT sets bit 61 only */
432 return PGM_PROTECTION
;
435 /* check for Instruction-Execution-Protection */
436 if (unlikely(rw
== MMU_INST_FETCH
&& !(*flags
& PAGE_EXEC
))) {
437 /* IEP sets bit 56 and 61 */
439 return PGM_PROTECTION
;
443 /* Convert real address -> absolute address */
444 *raddr
= mmu_real2abs(env
, *raddr
);
446 mmu_handle_skey(*raddr
, rw
, flags
);
451 * translate_pages: Translate a set of consecutive logical page addresses
452 * to absolute addresses. This function is used for TCG and old KVM without
453 * the MEMOP interface.
455 static int translate_pages(S390CPU
*cpu
, vaddr addr
, int nr_pages
,
456 target_ulong
*pages
, bool is_write
, uint64_t *tec
)
458 uint64_t asc
= cpu
->env
.psw
.mask
& PSW_MASK_ASC
;
459 CPUS390XState
*env
= &cpu
->env
;
462 for (i
= 0; i
< nr_pages
; i
++) {
463 ret
= mmu_translate(env
, addr
, is_write
, asc
, &pages
[i
], &pflags
, tec
);
467 if (!address_space_access_valid(&address_space_memory
, pages
[i
],
468 TARGET_PAGE_SIZE
, is_write
,
469 MEMTXATTRS_UNSPECIFIED
)) {
470 *tec
= 0; /* unused */
471 return PGM_ADDRESSING
;
473 addr
+= TARGET_PAGE_SIZE
;
479 int s390_cpu_pv_mem_rw(S390CPU
*cpu
, unsigned int offset
, void *hostbuf
,
480 int len
, bool is_write
)
485 ret
= kvm_s390_mem_op_pv(cpu
, offset
, hostbuf
, len
, is_write
);
487 /* Protected Virtualization is a KVM/Hardware only feature */
488 g_assert_not_reached();
494 * s390_cpu_virt_mem_rw:
495 * @laddr: the logical start address
496 * @ar: the access register number
497 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
498 * @len: length that should be transferred
499 * @is_write: true = write, false = read
500 * Returns: 0 on success, non-zero if an exception occurred
502 * Copy from/to guest memory using logical addresses. Note that we inject a
503 * program interrupt in case there is an error while accessing the memory.
505 * This function will always return (also for TCG), make sure to call
506 * s390_cpu_virt_mem_handle_exc() to properly exit the CPU loop.
508 int s390_cpu_virt_mem_rw(S390CPU
*cpu
, vaddr laddr
, uint8_t ar
, void *hostbuf
,
509 int len
, bool is_write
)
511 int currlen
, nr_pages
, i
;
517 ret
= kvm_s390_mem_op(cpu
, laddr
, ar
, hostbuf
, len
, is_write
);
523 nr_pages
= (((laddr
& ~TARGET_PAGE_MASK
) + len
- 1) >> TARGET_PAGE_BITS
)
525 pages
= g_malloc(nr_pages
* sizeof(*pages
));
527 ret
= translate_pages(cpu
, laddr
, nr_pages
, pages
, is_write
, &tec
);
529 trigger_access_exception(&cpu
->env
, ret
, tec
);
530 } else if (hostbuf
!= NULL
) {
531 /* Copy data by stepping through the area page by page */
532 for (i
= 0; i
< nr_pages
; i
++) {
533 currlen
= MIN(len
, TARGET_PAGE_SIZE
- (laddr
% TARGET_PAGE_SIZE
));
534 cpu_physical_memory_rw(pages
[i
] | (laddr
& ~TARGET_PAGE_MASK
),
535 hostbuf
, currlen
, is_write
);
546 void s390_cpu_virt_mem_handle_exc(S390CPU
*cpu
, uintptr_t ra
)
548 /* KVM will handle the interrupt automatically, TCG has to exit the TB */
551 cpu_loop_exit_restore(CPU(cpu
), ra
);
557 * Translate a real address into a physical (absolute) address.
558 * @param raddr the real address
559 * @param rw 0 = read, 1 = write, 2 = code fetch
560 * @param addr the translated address is stored to this pointer
561 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
562 * @return 0 = success, != 0, the exception to raise
564 int mmu_translate_real(CPUS390XState
*env
, target_ulong raddr
, int rw
,
565 target_ulong
*addr
, int *flags
, uint64_t *tec
)
567 const bool lowprot_enabled
= env
->cregs
[0] & CR0_LOWPROT
;
569 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
570 if (is_low_address(raddr
& TARGET_PAGE_MASK
) && lowprot_enabled
) {
571 /* see comment in mmu_translate() how this works */
572 *flags
|= PAGE_WRITE_INV
;
573 if (is_low_address(raddr
) && rw
== MMU_DATA_STORE
) {
574 /* LAP sets bit 56 */
575 *tec
= (raddr
& TARGET_PAGE_MASK
) | FS_WRITE
| 0x80;
576 return PGM_PROTECTION
;
580 *addr
= mmu_real2abs(env
, raddr
& TARGET_PAGE_MASK
);
582 mmu_handle_skey(*addr
, rw
, flags
);