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"
31 /* Fetch/store bits in the translation exception code: */
33 #define FS_WRITE 0x400
35 static void trigger_access_exception(CPUS390XState
*env
, uint32_t type
,
36 uint32_t ilen
, uint64_t tec
)
38 S390CPU
*cpu
= env_archcpu(env
);
41 kvm_s390_access_exception(cpu
, type
, tec
);
43 CPUState
*cs
= env_cpu(env
);
44 if (type
!= PGM_ADDRESSING
) {
45 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
47 trigger_pgm_exception(env
, type
, ilen
);
51 /* check whether the address would be proteted by Low-Address Protection */
52 static bool is_low_address(uint64_t addr
)
54 return addr
<= 511 || (addr
>= 4096 && addr
<= 4607);
57 /* check whether Low-Address Protection is enabled for mmu_translate() */
58 static bool lowprot_enabled(const CPUS390XState
*env
, uint64_t asc
)
60 if (!(env
->cregs
[0] & CR0_LOWPROT
)) {
63 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
67 /* Check the private-space control bit */
70 return !(env
->cregs
[1] & ASCE_PRIVATE_SPACE
);
71 case PSW_ASC_SECONDARY
:
72 return !(env
->cregs
[7] & ASCE_PRIVATE_SPACE
);
74 return !(env
->cregs
[13] & ASCE_PRIVATE_SPACE
);
76 /* We don't support access register mode */
77 error_report("unsupported addressing mode");
83 * Translate real address to absolute (= physical)
84 * address by taking care of the prefix mapping.
86 target_ulong
mmu_real2abs(CPUS390XState
*env
, target_ulong raddr
)
89 return raddr
+ env
->psa
; /* Map the lowcore. */
90 } else if (raddr
>= env
->psa
&& raddr
< env
->psa
+ 0x2000) {
91 return raddr
- env
->psa
; /* Map the 0 page. */
96 static inline bool read_table_entry(CPUS390XState
*env
, hwaddr gaddr
,
99 CPUState
*cs
= env_cpu(env
);
102 * According to the PoP, these table addresses are "unpredictably real
103 * or absolute". Also, "it is unpredictable whether the address wraps
104 * or an addressing exception is recognized".
106 * We treat them as absolute addresses and don't wrap them.
108 if (unlikely(address_space_read(cs
->as
, gaddr
, MEMTXATTRS_UNSPECIFIED
,
109 (uint8_t *)entry
, sizeof(*entry
)) !=
113 *entry
= be64_to_cpu(*entry
);
117 static int mmu_translate_asce(CPUS390XState
*env
, target_ulong vaddr
,
118 uint64_t asc
, uint64_t asce
, target_ulong
*raddr
,
121 const bool edat1
= (env
->cregs
[0] & CR0_EDAT
) &&
122 s390_has_feat(S390_FEAT_EDAT
);
123 const bool edat2
= edat1
&& s390_has_feat(S390_FEAT_EDAT_2
);
124 const bool iep
= (env
->cregs
[0] & CR0_IEP
) &&
125 s390_has_feat(S390_FEAT_INSTRUCTION_EXEC_PROT
);
126 const int asce_tl
= asce
& ASCE_TABLE_LENGTH
;
127 const int asce_p
= asce
& ASCE_PRIVATE_SPACE
;
128 hwaddr gaddr
= asce
& ASCE_ORIGIN
;
131 if (asce
& ASCE_REAL_SPACE
) {
137 switch (asce
& ASCE_TYPE_MASK
) {
138 case ASCE_TYPE_REGION1
:
139 if (VADDR_REGION1_TL(vaddr
) > asce_tl
) {
140 return PGM_REG_FIRST_TRANS
;
142 gaddr
+= VADDR_REGION1_TX(vaddr
) * 8;
144 case ASCE_TYPE_REGION2
:
145 if (VADDR_REGION1_TX(vaddr
)) {
146 return PGM_ASCE_TYPE
;
148 if (VADDR_REGION2_TL(vaddr
) > asce_tl
) {
149 return PGM_REG_SEC_TRANS
;
151 gaddr
+= VADDR_REGION2_TX(vaddr
) * 8;
153 case ASCE_TYPE_REGION3
:
154 if (VADDR_REGION1_TX(vaddr
) || VADDR_REGION2_TX(vaddr
)) {
155 return PGM_ASCE_TYPE
;
157 if (VADDR_REGION3_TL(vaddr
) > asce_tl
) {
158 return PGM_REG_THIRD_TRANS
;
160 gaddr
+= VADDR_REGION3_TX(vaddr
) * 8;
162 case ASCE_TYPE_SEGMENT
:
163 if (VADDR_REGION1_TX(vaddr
) || VADDR_REGION2_TX(vaddr
) ||
164 VADDR_REGION3_TX(vaddr
)) {
165 return PGM_ASCE_TYPE
;
167 if (VADDR_SEGMENT_TL(vaddr
) > asce_tl
) {
168 return PGM_SEGMENT_TRANS
;
170 gaddr
+= VADDR_SEGMENT_TX(vaddr
) * 8;
174 switch (asce
& ASCE_TYPE_MASK
) {
175 case ASCE_TYPE_REGION1
:
176 if (!read_table_entry(env
, gaddr
, &entry
)) {
177 return PGM_ADDRESSING
;
179 if (entry
& REGION_ENTRY_I
) {
180 return PGM_REG_FIRST_TRANS
;
182 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION1
) {
183 return PGM_TRANS_SPEC
;
185 if (VADDR_REGION2_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
186 VADDR_REGION2_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
187 return PGM_REG_SEC_TRANS
;
189 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
190 *flags
&= ~PAGE_WRITE
;
192 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_REGION2_TX(vaddr
) * 8;
194 case ASCE_TYPE_REGION2
:
195 if (!read_table_entry(env
, gaddr
, &entry
)) {
196 return PGM_ADDRESSING
;
198 if (entry
& REGION_ENTRY_I
) {
199 return PGM_REG_SEC_TRANS
;
201 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION2
) {
202 return PGM_TRANS_SPEC
;
204 if (VADDR_REGION3_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
205 VADDR_REGION3_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
206 return PGM_REG_THIRD_TRANS
;
208 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
209 *flags
&= ~PAGE_WRITE
;
211 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_REGION3_TX(vaddr
) * 8;
213 case ASCE_TYPE_REGION3
:
214 if (!read_table_entry(env
, gaddr
, &entry
)) {
215 return PGM_ADDRESSING
;
217 if (entry
& REGION_ENTRY_I
) {
218 return PGM_REG_THIRD_TRANS
;
220 if ((entry
& REGION_ENTRY_TT
) != REGION_ENTRY_TT_REGION3
) {
221 return PGM_TRANS_SPEC
;
223 if (edat2
&& (entry
& REGION3_ENTRY_CR
) && asce_p
) {
224 return PGM_TRANS_SPEC
;
226 if (edat1
&& (entry
& REGION_ENTRY_P
)) {
227 *flags
&= ~PAGE_WRITE
;
229 if (edat2
&& (entry
& REGION3_ENTRY_FC
)) {
230 if (iep
&& (entry
& REGION3_ENTRY_IEP
)) {
231 *flags
&= ~PAGE_EXEC
;
233 *raddr
= (entry
& REGION3_ENTRY_RFAA
) |
234 (vaddr
& ~REGION3_ENTRY_RFAA
);
237 if (VADDR_SEGMENT_TL(vaddr
) < (entry
& REGION_ENTRY_TF
) >> 6 ||
238 VADDR_SEGMENT_TL(vaddr
) > (entry
& REGION_ENTRY_TL
)) {
239 return PGM_SEGMENT_TRANS
;
241 gaddr
= (entry
& REGION_ENTRY_ORIGIN
) + VADDR_SEGMENT_TX(vaddr
) * 8;
243 case ASCE_TYPE_SEGMENT
:
244 if (!read_table_entry(env
, gaddr
, &entry
)) {
245 return PGM_ADDRESSING
;
247 if (entry
& SEGMENT_ENTRY_I
) {
248 return PGM_SEGMENT_TRANS
;
250 if ((entry
& SEGMENT_ENTRY_TT
) != SEGMENT_ENTRY_TT_SEGMENT
) {
251 return PGM_TRANS_SPEC
;
253 if ((entry
& SEGMENT_ENTRY_CS
) && asce_p
) {
254 return PGM_TRANS_SPEC
;
256 if (entry
& SEGMENT_ENTRY_P
) {
257 *flags
&= ~PAGE_WRITE
;
259 if (edat1
&& (entry
& SEGMENT_ENTRY_FC
)) {
260 if (iep
&& (entry
& SEGMENT_ENTRY_IEP
)) {
261 *flags
&= ~PAGE_EXEC
;
263 *raddr
= (entry
& SEGMENT_ENTRY_SFAA
) |
264 (vaddr
& ~SEGMENT_ENTRY_SFAA
);
267 gaddr
= (entry
& SEGMENT_ENTRY_ORIGIN
) + VADDR_PAGE_TX(vaddr
) * 8;
271 if (!read_table_entry(env
, gaddr
, &entry
)) {
272 return PGM_ADDRESSING
;
274 if (entry
& PAGE_ENTRY_I
) {
275 return PGM_PAGE_TRANS
;
277 if (entry
& PAGE_ENTRY_0
) {
278 return PGM_TRANS_SPEC
;
280 if (entry
& PAGE_ENTRY_P
) {
281 *flags
&= ~PAGE_WRITE
;
283 if (iep
&& (entry
& PAGE_ENTRY_IEP
)) {
284 *flags
&= ~PAGE_EXEC
;
287 *raddr
= entry
& TARGET_PAGE_MASK
;
291 static void mmu_handle_skey(target_ulong addr
, int rw
, int *flags
)
293 static S390SKeysClass
*skeyclass
;
294 static S390SKeysState
*ss
;
298 if (unlikely(addr
>= ram_size
)) {
303 ss
= s390_get_skeys_device();
304 skeyclass
= S390_SKEYS_GET_CLASS(ss
);
308 * Whenever we create a new TLB entry, we set the storage key reference
309 * bit. In case we allow write accesses, we set the storage key change
310 * bit. Whenever the guest changes the storage key, we have to flush the
311 * TLBs of all CPUs (the whole TLB or all affected entries), so that the
312 * next reference/change will result in an MMU fault and make us properly
313 * update the storage key here.
315 * Note 1: "record of references ... is not necessarily accurate",
316 * "change bit may be set in case no storing has occurred".
317 * -> We can set reference/change bits even on exceptions.
318 * Note 2: certain accesses seem to ignore storage keys. For example,
319 * DAT translation does not set reference bits for table accesses.
321 * TODO: key-controlled protection. Only CPU accesses make use of the
322 * PSW key. CSS accesses are different - we have to pass in the key.
324 * TODO: we have races between getting and setting the key.
326 rc
= skeyclass
->get_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
328 trace_get_skeys_nonzero(rc
);
336 * The TLB entry has to remain write-protected on read-faults if
337 * the storage key does not indicate a change already. Otherwise
338 * we might miss setting the change bit on write accesses.
341 *flags
&= ~PAGE_WRITE
;
348 g_assert_not_reached();
351 /* Any store/fetch sets the reference bit */
354 rc
= skeyclass
->set_skeys(ss
, addr
/ TARGET_PAGE_SIZE
, 1, &key
);
356 trace_set_skeys_nonzero(rc
);
361 * Translate a virtual (logical) address into a physical (absolute) address.
362 * @param vaddr the virtual address
363 * @param rw 0 = read, 1 = write, 2 = code fetch
364 * @param asc address space control (one of the PSW_ASC_* modes)
365 * @param raddr the translated address is stored to this pointer
366 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
367 * @param exc true = inject a program check if a fault occurred
368 * @return 0 if the translation was successful, -1 if a fault occurred
370 int mmu_translate(CPUS390XState
*env
, target_ulong vaddr
, int rw
, uint64_t asc
,
371 target_ulong
*raddr
, int *flags
, bool exc
)
373 /* Code accesses have an undefined ilc, let's use 2 bytes. */
374 const int ilen
= (rw
== MMU_INST_FETCH
) ? 2 : ILEN_AUTO
;
375 uint64_t tec
= (vaddr
& TARGET_PAGE_MASK
) | (asc
>> 46) |
376 (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
);
381 *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
) {
394 /* LAP sets bit 56 */
396 trigger_access_exception(env
, PGM_PROTECTION
, ilen
, tec
);
402 vaddr
&= TARGET_PAGE_MASK
;
404 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
410 case PSW_ASC_PRIMARY
:
411 asce
= env
->cregs
[1];
414 asce
= env
->cregs
[13];
416 case PSW_ASC_SECONDARY
:
417 asce
= env
->cregs
[7];
421 hw_error("guest switched to unknown asc mode\n");
425 /* perform the DAT translation */
426 r
= mmu_translate_asce(env
, vaddr
, asc
, asce
, raddr
, flags
, rw
);
429 trigger_access_exception(env
, r
, ilen
, tec
);
434 /* check for DAT protection */
435 if (unlikely(rw
== MMU_DATA_STORE
&& !(*flags
& PAGE_WRITE
))) {
437 /* DAT sets bit 61 only */
439 trigger_access_exception(env
, PGM_PROTECTION
, ilen
, tec
);
444 /* check for Instruction-Execution-Protection */
445 if (unlikely(rw
== MMU_INST_FETCH
&& !(*flags
& PAGE_EXEC
))) {
447 /* IEP sets bit 56 and 61 */
449 trigger_access_exception(env
, PGM_PROTECTION
, ilen
, tec
);
455 /* Convert real address -> absolute address */
456 *raddr
= mmu_real2abs(env
, *raddr
);
458 mmu_handle_skey(*raddr
, rw
, flags
);
463 * translate_pages: Translate a set of consecutive logical page addresses
464 * to absolute addresses. This function is used for TCG and old KVM without
465 * the MEMOP interface.
467 static int translate_pages(S390CPU
*cpu
, vaddr addr
, int nr_pages
,
468 target_ulong
*pages
, bool is_write
)
470 uint64_t asc
= cpu
->env
.psw
.mask
& PSW_MASK_ASC
;
471 CPUS390XState
*env
= &cpu
->env
;
474 for (i
= 0; i
< nr_pages
; i
++) {
475 ret
= mmu_translate(env
, addr
, is_write
, asc
, &pages
[i
], &pflags
, true);
479 if (!address_space_access_valid(&address_space_memory
, pages
[i
],
480 TARGET_PAGE_SIZE
, is_write
,
481 MEMTXATTRS_UNSPECIFIED
)) {
482 trigger_access_exception(env
, PGM_ADDRESSING
, ILEN_AUTO
, 0);
485 addr
+= TARGET_PAGE_SIZE
;
492 * s390_cpu_virt_mem_rw:
493 * @laddr: the logical start address
494 * @ar: the access register number
495 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
496 * @len: length that should be transferred
497 * @is_write: true = write, false = read
498 * Returns: 0 on success, non-zero if an exception occurred
500 * Copy from/to guest memory using logical addresses. Note that we inject a
501 * program interrupt in case there is an error while accessing the memory.
503 * This function will always return (also for TCG), make sure to call
504 * s390_cpu_virt_mem_handle_exc() to properly exit the CPU loop.
506 int s390_cpu_virt_mem_rw(S390CPU
*cpu
, vaddr laddr
, uint8_t ar
, void *hostbuf
,
507 int len
, bool is_write
)
509 int currlen
, nr_pages
, i
;
514 ret
= kvm_s390_mem_op(cpu
, laddr
, ar
, hostbuf
, len
, is_write
);
520 nr_pages
= (((laddr
& ~TARGET_PAGE_MASK
) + len
- 1) >> TARGET_PAGE_BITS
)
522 pages
= g_malloc(nr_pages
* sizeof(*pages
));
524 ret
= translate_pages(cpu
, laddr
, nr_pages
, pages
, is_write
);
525 if (ret
== 0 && hostbuf
!= NULL
) {
526 /* Copy data by stepping through the area page by page */
527 for (i
= 0; i
< nr_pages
; i
++) {
528 currlen
= MIN(len
, TARGET_PAGE_SIZE
- (laddr
% TARGET_PAGE_SIZE
));
529 cpu_physical_memory_rw(pages
[i
] | (laddr
& ~TARGET_PAGE_MASK
),
530 hostbuf
, currlen
, is_write
);
541 void s390_cpu_virt_mem_handle_exc(S390CPU
*cpu
, uintptr_t ra
)
543 /* KVM will handle the interrupt automatically, TCG has to exit the TB */
546 cpu_loop_exit_restore(CPU(cpu
), ra
);
552 * Translate a real address into a physical (absolute) address.
553 * @param raddr the real address
554 * @param rw 0 = read, 1 = write, 2 = code fetch
555 * @param addr the translated address is stored to this pointer
556 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
557 * @return 0 = success, != 0, the exception to raise
559 int mmu_translate_real(CPUS390XState
*env
, target_ulong raddr
, int rw
,
560 target_ulong
*addr
, int *flags
, uint64_t *tec
)
562 const bool lowprot_enabled
= env
->cregs
[0] & CR0_LOWPROT
;
564 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
565 if (is_low_address(raddr
& TARGET_PAGE_MASK
) && lowprot_enabled
) {
566 /* see comment in mmu_translate() how this works */
567 *flags
|= PAGE_WRITE_INV
;
568 if (is_low_address(raddr
) && rw
== MMU_DATA_STORE
) {
569 /* LAP sets bit 56 */
570 *tec
= (raddr
& TARGET_PAGE_MASK
)
571 | (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
)
573 return PGM_PROTECTION
;
577 *addr
= mmu_real2abs(env
, raddr
& TARGET_PAGE_MASK
);
579 mmu_handle_skey(*addr
, rw
, flags
);