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/error-report.h"
19 #include "exec/address-spaces.h"
21 #include "sysemu/kvm.h"
23 #include "hw/s390x/storage-keys.h"
25 /* #define DEBUG_S390 */
26 /* #define DEBUG_S390_PTE */
27 /* #define DEBUG_S390_STDOUT */
30 #ifdef DEBUG_S390_STDOUT
31 #define DPRINTF(fmt, ...) \
32 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
33 qemu_log(fmt, ##__VA_ARGS__); } while (0)
35 #define DPRINTF(fmt, ...) \
36 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
39 #define DPRINTF(fmt, ...) \
44 #define PTE_DPRINTF DPRINTF
46 #define PTE_DPRINTF(fmt, ...) \
50 /* Fetch/store bits in the translation exception code: */
52 #define FS_WRITE 0x400
54 static void trigger_access_exception(CPUS390XState
*env
, uint32_t type
,
55 uint32_t ilen
, uint64_t tec
)
57 S390CPU
*cpu
= s390_env_get_cpu(env
);
60 kvm_s390_access_exception(cpu
, type
, tec
);
62 CPUState
*cs
= CPU(cpu
);
63 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
64 trigger_pgm_exception(env
, type
, ilen
);
68 static void trigger_prot_fault(CPUS390XState
*env
, target_ulong vaddr
,
69 uint64_t asc
, int rw
, bool exc
)
73 tec
= vaddr
| (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
) | 4 | asc
>> 46;
75 DPRINTF("%s: trans_exc_code=%016" PRIx64
"\n", __func__
, tec
);
81 trigger_access_exception(env
, PGM_PROTECTION
, ILEN_LATER_INC
, tec
);
84 static void trigger_page_fault(CPUS390XState
*env
, target_ulong vaddr
,
85 uint32_t type
, uint64_t asc
, int rw
, bool exc
)
87 int ilen
= ILEN_LATER
;
90 tec
= vaddr
| (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
) | asc
>> 46;
92 DPRINTF("%s: vaddr=%016" PRIx64
" bits=%d\n", __func__
, vaddr
, bits
);
98 /* Code accesses have an undefined ilc. */
99 if (rw
== MMU_INST_FETCH
) {
103 trigger_access_exception(env
, type
, ilen
, tec
);
107 * Translate real address to absolute (= physical)
108 * address by taking care of the prefix mapping.
110 static target_ulong
mmu_real2abs(CPUS390XState
*env
, target_ulong raddr
)
112 if (raddr
< 0x2000) {
113 return raddr
+ env
->psa
; /* Map the lowcore. */
114 } else if (raddr
>= env
->psa
&& raddr
< env
->psa
+ 0x2000) {
115 return raddr
- env
->psa
; /* Map the 0 page. */
120 /* Decode page table entry (normal 4KB page) */
121 static int mmu_translate_pte(CPUS390XState
*env
, target_ulong vaddr
,
122 uint64_t asc
, uint64_t pt_entry
,
123 target_ulong
*raddr
, int *flags
, int rw
, bool exc
)
125 if (pt_entry
& _PAGE_INVALID
) {
126 DPRINTF("%s: PTE=0x%" PRIx64
" invalid\n", __func__
, pt_entry
);
127 trigger_page_fault(env
, vaddr
, PGM_PAGE_TRANS
, asc
, rw
, exc
);
130 if (pt_entry
& _PAGE_RES0
) {
131 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
134 if (pt_entry
& _PAGE_RO
) {
135 *flags
&= ~PAGE_WRITE
;
138 *raddr
= pt_entry
& _ASCE_ORIGIN
;
140 PTE_DPRINTF("%s: PTE=0x%" PRIx64
"\n", __func__
, pt_entry
);
145 #define VADDR_PX 0xff000 /* Page index bits */
147 /* Decode segment table entry */
148 static int mmu_translate_segment(CPUS390XState
*env
, target_ulong vaddr
,
149 uint64_t asc
, uint64_t st_entry
,
150 target_ulong
*raddr
, int *flags
, int rw
,
153 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
154 uint64_t origin
, offs
, pt_entry
;
156 if (st_entry
& _SEGMENT_ENTRY_RO
) {
157 *flags
&= ~PAGE_WRITE
;
160 if ((st_entry
& _SEGMENT_ENTRY_FC
) && (env
->cregs
[0] & CR0_EDAT
)) {
161 /* Decode EDAT1 segment frame absolute address (1MB page) */
162 *raddr
= (st_entry
& 0xfffffffffff00000ULL
) | (vaddr
& 0xfffff);
163 PTE_DPRINTF("%s: SEG=0x%" PRIx64
"\n", __func__
, st_entry
);
167 /* Look up 4KB page entry */
168 origin
= st_entry
& _SEGMENT_ENTRY_ORIGIN
;
169 offs
= (vaddr
& VADDR_PX
) >> 9;
170 pt_entry
= ldq_phys(cs
->as
, origin
+ offs
);
171 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
172 __func__
, origin
, offs
, pt_entry
);
173 return mmu_translate_pte(env
, vaddr
, asc
, pt_entry
, raddr
, flags
, rw
, exc
);
176 /* Decode region table entries */
177 static int mmu_translate_region(CPUS390XState
*env
, target_ulong vaddr
,
178 uint64_t asc
, uint64_t entry
, int level
,
179 target_ulong
*raddr
, int *flags
, int rw
,
182 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
183 uint64_t origin
, offs
, new_entry
;
184 const int pchks
[4] = {
185 PGM_SEGMENT_TRANS
, PGM_REG_THIRD_TRANS
,
186 PGM_REG_SEC_TRANS
, PGM_REG_FIRST_TRANS
189 PTE_DPRINTF("%s: 0x%" PRIx64
"\n", __func__
, entry
);
191 origin
= entry
& _REGION_ENTRY_ORIGIN
;
192 offs
= (vaddr
>> (17 + 11 * level
/ 4)) & 0x3ff8;
194 new_entry
= ldq_phys(cs
->as
, origin
+ offs
);
195 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
196 __func__
, origin
, offs
, new_entry
);
198 if ((new_entry
& _REGION_ENTRY_INV
) != 0) {
199 DPRINTF("%s: invalid region\n", __func__
);
200 trigger_page_fault(env
, vaddr
, pchks
[level
/ 4], asc
, rw
, exc
);
204 if ((new_entry
& _REGION_ENTRY_TYPE_MASK
) != level
) {
205 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
209 if (level
== _ASCE_TYPE_SEGMENT
) {
210 return mmu_translate_segment(env
, vaddr
, asc
, new_entry
, raddr
, flags
,
214 /* Check region table offset and length */
215 offs
= (vaddr
>> (28 + 11 * (level
- 4) / 4)) & 3;
216 if (offs
< ((new_entry
& _REGION_ENTRY_TF
) >> 6)
217 || offs
> (new_entry
& _REGION_ENTRY_LENGTH
)) {
218 DPRINTF("%s: invalid offset or len (%lx)\n", __func__
, new_entry
);
219 trigger_page_fault(env
, vaddr
, pchks
[level
/ 4 - 1], asc
, rw
, exc
);
223 if ((env
->cregs
[0] & CR0_EDAT
) && (new_entry
& _REGION_ENTRY_RO
)) {
224 *flags
&= ~PAGE_WRITE
;
227 /* yet another region */
228 return mmu_translate_region(env
, vaddr
, asc
, new_entry
, level
- 4,
229 raddr
, flags
, rw
, exc
);
232 static int mmu_translate_asce(CPUS390XState
*env
, target_ulong vaddr
,
233 uint64_t asc
, uint64_t asce
, target_ulong
*raddr
,
234 int *flags
, int rw
, bool exc
)
239 if (asce
& _ASCE_REAL_SPACE
) {
245 level
= asce
& _ASCE_TYPE_MASK
;
247 case _ASCE_TYPE_REGION1
:
248 if ((vaddr
>> 62) > (asce
& _ASCE_TABLE_LENGTH
)) {
249 trigger_page_fault(env
, vaddr
, PGM_REG_FIRST_TRANS
, asc
, rw
, exc
);
253 case _ASCE_TYPE_REGION2
:
254 if (vaddr
& 0xffe0000000000000ULL
) {
255 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
256 " 0xffe0000000000000ULL\n", __func__
, vaddr
);
257 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
260 if ((vaddr
>> 51 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
261 trigger_page_fault(env
, vaddr
, PGM_REG_SEC_TRANS
, asc
, rw
, exc
);
265 case _ASCE_TYPE_REGION3
:
266 if (vaddr
& 0xfffffc0000000000ULL
) {
267 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
268 " 0xfffffc0000000000ULL\n", __func__
, vaddr
);
269 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
272 if ((vaddr
>> 40 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
273 trigger_page_fault(env
, vaddr
, PGM_REG_THIRD_TRANS
, asc
, rw
, exc
);
277 case _ASCE_TYPE_SEGMENT
:
278 if (vaddr
& 0xffffffff80000000ULL
) {
279 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
280 " 0xffffffff80000000ULL\n", __func__
, vaddr
);
281 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
284 if ((vaddr
>> 29 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
285 trigger_page_fault(env
, vaddr
, PGM_SEGMENT_TRANS
, asc
, rw
, exc
);
291 r
= mmu_translate_region(env
, vaddr
, asc
, asce
, level
, raddr
, flags
, rw
,
293 if (rw
== MMU_DATA_STORE
&& !(*flags
& PAGE_WRITE
)) {
294 trigger_prot_fault(env
, vaddr
, asc
, rw
, exc
);
302 * Translate a virtual (logical) address into a physical (absolute) address.
303 * @param vaddr the virtual address
304 * @param rw 0 = read, 1 = write, 2 = code fetch
305 * @param asc address space control (one of the PSW_ASC_* modes)
306 * @param raddr the translated address is stored to this pointer
307 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
308 * @param exc true = inject a program check if a fault occurred
309 * @return 0 if the translation was successful, -1 if a fault occurred
311 int mmu_translate(CPUS390XState
*env
, target_ulong vaddr
, int rw
, uint64_t asc
,
312 target_ulong
*raddr
, int *flags
, bool exc
)
314 static S390SKeysState
*ss
;
315 static S390SKeysClass
*skeyclass
;
320 ss
= s390_get_skeys_device();
321 skeyclass
= S390_SKEYS_GET_CLASS(ss
);
324 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
325 vaddr
&= TARGET_PAGE_MASK
;
327 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
334 case PSW_ASC_PRIMARY
:
335 PTE_DPRINTF("%s: asc=primary\n", __func__
);
336 r
= mmu_translate_asce(env
, vaddr
, asc
, env
->cregs
[1], raddr
, flags
,
340 PTE_DPRINTF("%s: asc=home\n", __func__
);
341 r
= mmu_translate_asce(env
, vaddr
, asc
, env
->cregs
[13], raddr
, flags
,
344 case PSW_ASC_SECONDARY
:
345 PTE_DPRINTF("%s: asc=secondary\n", __func__
);
347 * Instruction: Primary
350 if (rw
== MMU_INST_FETCH
) {
351 r
= mmu_translate_asce(env
, vaddr
, PSW_ASC_PRIMARY
, env
->cregs
[1],
352 raddr
, flags
, rw
, exc
);
353 *flags
&= ~(PAGE_READ
| PAGE_WRITE
);
355 r
= mmu_translate_asce(env
, vaddr
, PSW_ASC_SECONDARY
, env
->cregs
[7],
356 raddr
, flags
, rw
, exc
);
357 *flags
&= ~(PAGE_EXEC
);
362 hw_error("guest switched to unknown asc mode\n");
367 /* Convert real address -> absolute address */
368 *raddr
= mmu_real2abs(env
, *raddr
);
370 if (r
== 0 && *raddr
< ram_size
) {
371 if (skeyclass
->get_skeys(ss
, *raddr
/ TARGET_PAGE_SIZE
, 1, &key
)) {
372 trace_get_skeys_nonzero(r
);
376 if (*flags
& PAGE_READ
) {
380 if (*flags
& PAGE_WRITE
) {
384 if (skeyclass
->set_skeys(ss
, *raddr
/ TARGET_PAGE_SIZE
, 1, &key
)) {
385 trace_set_skeys_nonzero(r
);
394 * lowprot_enabled: Check whether low-address protection is enabled
396 static bool lowprot_enabled(const CPUS390XState
*env
)
398 if (!(env
->cregs
[0] & CR0_LOWPROT
)) {
401 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
405 /* Check the private-space control bit */
406 switch (env
->psw
.mask
& PSW_MASK_ASC
) {
407 case PSW_ASC_PRIMARY
:
408 return !(env
->cregs
[1] & _ASCE_PRIVATE_SPACE
);
409 case PSW_ASC_SECONDARY
:
410 return !(env
->cregs
[7] & _ASCE_PRIVATE_SPACE
);
412 return !(env
->cregs
[13] & _ASCE_PRIVATE_SPACE
);
414 /* We don't support access register mode */
415 error_report("unsupported addressing mode");
421 * translate_pages: Translate a set of consecutive logical page addresses
422 * to absolute addresses
424 static int translate_pages(S390CPU
*cpu
, vaddr addr
, int nr_pages
,
425 target_ulong
*pages
, bool is_write
)
427 bool lowprot
= is_write
&& lowprot_enabled(&cpu
->env
);
428 uint64_t asc
= cpu
->env
.psw
.mask
& PSW_MASK_ASC
;
429 CPUS390XState
*env
= &cpu
->env
;
432 for (i
= 0; i
< nr_pages
; i
++) {
433 /* Low-address protection? */
434 if (lowprot
&& (addr
< 512 || (addr
>= 4096 && addr
< 4096 + 512))) {
435 trigger_access_exception(env
, PGM_PROTECTION
, ILEN_LATER_INC
, 0);
438 ret
= mmu_translate(env
, addr
, is_write
, asc
, &pages
[i
], &pflags
, true);
442 if (!address_space_access_valid(&address_space_memory
, pages
[i
],
443 TARGET_PAGE_SIZE
, is_write
)) {
444 program_interrupt(env
, PGM_ADDRESSING
, 0);
447 addr
+= TARGET_PAGE_SIZE
;
454 * s390_cpu_virt_mem_rw:
455 * @laddr: the logical start address
456 * @ar: the access register number
457 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
458 * @len: length that should be transferred
459 * @is_write: true = write, false = read
460 * Returns: 0 on success, non-zero if an exception occurred
462 * Copy from/to guest memory using logical addresses. Note that we inject a
463 * program interrupt in case there is an error while accessing the memory.
465 int s390_cpu_virt_mem_rw(S390CPU
*cpu
, vaddr laddr
, uint8_t ar
, void *hostbuf
,
466 int len
, bool is_write
)
468 int currlen
, nr_pages
, i
;
473 ret
= kvm_s390_mem_op(cpu
, laddr
, ar
, hostbuf
, len
, is_write
);
479 nr_pages
= (((laddr
& ~TARGET_PAGE_MASK
) + len
- 1) >> TARGET_PAGE_BITS
)
481 pages
= g_malloc(nr_pages
* sizeof(*pages
));
483 ret
= translate_pages(cpu
, laddr
, nr_pages
, pages
, is_write
);
484 if (ret
== 0 && hostbuf
!= NULL
) {
485 /* Copy data by stepping through the area page by page */
486 for (i
= 0; i
< nr_pages
; i
++) {
487 currlen
= MIN(len
, TARGET_PAGE_SIZE
- (laddr
% TARGET_PAGE_SIZE
));
488 cpu_physical_memory_rw(pages
[i
] | (laddr
& ~TARGET_PAGE_MASK
),
489 hostbuf
, currlen
, is_write
);