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 /* #define DEBUG_S390 */
24 /* #define DEBUG_S390_PTE */
25 /* #define DEBUG_S390_STDOUT */
28 #ifdef DEBUG_S390_STDOUT
29 #define DPRINTF(fmt, ...) \
30 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
31 qemu_log(fmt, ##__VA_ARGS__); } while (0)
33 #define DPRINTF(fmt, ...) \
34 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
37 #define DPRINTF(fmt, ...) \
42 #define PTE_DPRINTF DPRINTF
44 #define PTE_DPRINTF(fmt, ...) \
48 /* Fetch/store bits in the translation exception code: */
50 #define FS_WRITE 0x400
52 static void trigger_access_exception(CPUS390XState
*env
, uint32_t type
,
53 uint32_t ilen
, uint64_t tec
)
55 S390CPU
*cpu
= s390_env_get_cpu(env
);
58 kvm_s390_access_exception(cpu
, type
, tec
);
60 CPUState
*cs
= CPU(cpu
);
61 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
62 trigger_pgm_exception(env
, type
, ilen
);
66 static void trigger_prot_fault(CPUS390XState
*env
, target_ulong vaddr
,
67 uint64_t asc
, int rw
, bool exc
)
71 tec
= vaddr
| (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
) | 4 | asc
>> 46;
73 DPRINTF("%s: trans_exc_code=%016" PRIx64
"\n", __func__
, tec
);
79 trigger_access_exception(env
, PGM_PROTECTION
, ILEN_LATER_INC
, tec
);
82 static void trigger_page_fault(CPUS390XState
*env
, target_ulong vaddr
,
83 uint32_t type
, uint64_t asc
, int rw
, bool exc
)
85 int ilen
= ILEN_LATER
;
88 tec
= vaddr
| (rw
== MMU_DATA_STORE
? FS_WRITE
: FS_READ
) | asc
>> 46;
90 DPRINTF("%s: vaddr=%016" PRIx64
" bits=%d\n", __func__
, vaddr
, bits
);
96 /* Code accesses have an undefined ilc. */
97 if (rw
== MMU_INST_FETCH
) {
101 trigger_access_exception(env
, type
, ilen
, tec
);
105 * Translate real address to absolute (= physical)
106 * address by taking care of the prefix mapping.
108 static target_ulong
mmu_real2abs(CPUS390XState
*env
, target_ulong raddr
)
110 if (raddr
< 0x2000) {
111 return raddr
+ env
->psa
; /* Map the lowcore. */
112 } else if (raddr
>= env
->psa
&& raddr
< env
->psa
+ 0x2000) {
113 return raddr
- env
->psa
; /* Map the 0 page. */
118 /* Decode page table entry (normal 4KB page) */
119 static int mmu_translate_pte(CPUS390XState
*env
, target_ulong vaddr
,
120 uint64_t asc
, uint64_t pt_entry
,
121 target_ulong
*raddr
, int *flags
, int rw
, bool exc
)
123 if (pt_entry
& _PAGE_INVALID
) {
124 DPRINTF("%s: PTE=0x%" PRIx64
" invalid\n", __func__
, pt_entry
);
125 trigger_page_fault(env
, vaddr
, PGM_PAGE_TRANS
, asc
, rw
, exc
);
128 if (pt_entry
& _PAGE_RES0
) {
129 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
132 if (pt_entry
& _PAGE_RO
) {
133 *flags
&= ~PAGE_WRITE
;
136 *raddr
= pt_entry
& _ASCE_ORIGIN
;
138 PTE_DPRINTF("%s: PTE=0x%" PRIx64
"\n", __func__
, pt_entry
);
143 #define VADDR_PX 0xff000 /* Page index bits */
145 /* Decode segment table entry */
146 static int mmu_translate_segment(CPUS390XState
*env
, target_ulong vaddr
,
147 uint64_t asc
, uint64_t st_entry
,
148 target_ulong
*raddr
, int *flags
, int rw
,
151 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
152 uint64_t origin
, offs
, pt_entry
;
154 if (st_entry
& _SEGMENT_ENTRY_RO
) {
155 *flags
&= ~PAGE_WRITE
;
158 if ((st_entry
& _SEGMENT_ENTRY_FC
) && (env
->cregs
[0] & CR0_EDAT
)) {
159 /* Decode EDAT1 segment frame absolute address (1MB page) */
160 *raddr
= (st_entry
& 0xfffffffffff00000ULL
) | (vaddr
& 0xfffff);
161 PTE_DPRINTF("%s: SEG=0x%" PRIx64
"\n", __func__
, st_entry
);
165 /* Look up 4KB page entry */
166 origin
= st_entry
& _SEGMENT_ENTRY_ORIGIN
;
167 offs
= (vaddr
& VADDR_PX
) >> 9;
168 pt_entry
= ldq_phys(cs
->as
, origin
+ offs
);
169 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
170 __func__
, origin
, offs
, pt_entry
);
171 return mmu_translate_pte(env
, vaddr
, asc
, pt_entry
, raddr
, flags
, rw
, exc
);
174 /* Decode region table entries */
175 static int mmu_translate_region(CPUS390XState
*env
, target_ulong vaddr
,
176 uint64_t asc
, uint64_t entry
, int level
,
177 target_ulong
*raddr
, int *flags
, int rw
,
180 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
181 uint64_t origin
, offs
, new_entry
;
182 const int pchks
[4] = {
183 PGM_SEGMENT_TRANS
, PGM_REG_THIRD_TRANS
,
184 PGM_REG_SEC_TRANS
, PGM_REG_FIRST_TRANS
187 PTE_DPRINTF("%s: 0x%" PRIx64
"\n", __func__
, entry
);
189 origin
= entry
& _REGION_ENTRY_ORIGIN
;
190 offs
= (vaddr
>> (17 + 11 * level
/ 4)) & 0x3ff8;
192 new_entry
= ldq_phys(cs
->as
, origin
+ offs
);
193 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
194 __func__
, origin
, offs
, new_entry
);
196 if ((new_entry
& _REGION_ENTRY_INV
) != 0) {
197 DPRINTF("%s: invalid region\n", __func__
);
198 trigger_page_fault(env
, vaddr
, pchks
[level
/ 4], asc
, rw
, exc
);
202 if ((new_entry
& _REGION_ENTRY_TYPE_MASK
) != level
) {
203 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
207 if (level
== _ASCE_TYPE_SEGMENT
) {
208 return mmu_translate_segment(env
, vaddr
, asc
, new_entry
, raddr
, flags
,
212 /* Check region table offset and length */
213 offs
= (vaddr
>> (28 + 11 * (level
- 4) / 4)) & 3;
214 if (offs
< ((new_entry
& _REGION_ENTRY_TF
) >> 6)
215 || offs
> (new_entry
& _REGION_ENTRY_LENGTH
)) {
216 DPRINTF("%s: invalid offset or len (%lx)\n", __func__
, new_entry
);
217 trigger_page_fault(env
, vaddr
, pchks
[level
/ 4 - 1], asc
, rw
, exc
);
221 if ((env
->cregs
[0] & CR0_EDAT
) && (new_entry
& _REGION_ENTRY_RO
)) {
222 *flags
&= ~PAGE_WRITE
;
225 /* yet another region */
226 return mmu_translate_region(env
, vaddr
, asc
, new_entry
, level
- 4,
227 raddr
, flags
, rw
, exc
);
230 static int mmu_translate_asce(CPUS390XState
*env
, target_ulong vaddr
,
231 uint64_t asc
, uint64_t asce
, target_ulong
*raddr
,
232 int *flags
, int rw
, bool exc
)
237 if (asce
& _ASCE_REAL_SPACE
) {
243 level
= asce
& _ASCE_TYPE_MASK
;
245 case _ASCE_TYPE_REGION1
:
246 if ((vaddr
>> 62) > (asce
& _ASCE_TABLE_LENGTH
)) {
247 trigger_page_fault(env
, vaddr
, PGM_REG_FIRST_TRANS
, asc
, rw
, exc
);
251 case _ASCE_TYPE_REGION2
:
252 if (vaddr
& 0xffe0000000000000ULL
) {
253 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
254 " 0xffe0000000000000ULL\n", __func__
, vaddr
);
255 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
258 if ((vaddr
>> 51 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
259 trigger_page_fault(env
, vaddr
, PGM_REG_SEC_TRANS
, asc
, rw
, exc
);
263 case _ASCE_TYPE_REGION3
:
264 if (vaddr
& 0xfffffc0000000000ULL
) {
265 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
266 " 0xfffffc0000000000ULL\n", __func__
, vaddr
);
267 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
270 if ((vaddr
>> 40 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
271 trigger_page_fault(env
, vaddr
, PGM_REG_THIRD_TRANS
, asc
, rw
, exc
);
275 case _ASCE_TYPE_SEGMENT
:
276 if (vaddr
& 0xffffffff80000000ULL
) {
277 DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64
278 " 0xffffffff80000000ULL\n", __func__
, vaddr
);
279 trigger_page_fault(env
, vaddr
, PGM_ASCE_TYPE
, asc
, rw
, exc
);
282 if ((vaddr
>> 29 & 3) > (asce
& _ASCE_TABLE_LENGTH
)) {
283 trigger_page_fault(env
, vaddr
, PGM_SEGMENT_TRANS
, asc
, rw
, exc
);
289 r
= mmu_translate_region(env
, vaddr
, asc
, asce
, level
, raddr
, flags
, rw
,
291 if (rw
== MMU_DATA_STORE
&& !(*flags
& PAGE_WRITE
)) {
292 trigger_prot_fault(env
, vaddr
, asc
, rw
, exc
);
300 * Translate a virtual (logical) address into a physical (absolute) address.
301 * @param vaddr the virtual address
302 * @param rw 0 = read, 1 = write, 2 = code fetch
303 * @param asc address space control (one of the PSW_ASC_* modes)
304 * @param raddr the translated address is stored to this pointer
305 * @param flags the PAGE_READ/WRITE/EXEC flags are stored to this pointer
306 * @param exc true = inject a program check if a fault occurred
307 * @return 0 if the translation was successful, -1 if a fault occurred
309 int mmu_translate(CPUS390XState
*env
, target_ulong vaddr
, int rw
, uint64_t asc
,
310 target_ulong
*raddr
, int *flags
, bool exc
)
315 *flags
= PAGE_READ
| PAGE_WRITE
| PAGE_EXEC
;
316 vaddr
&= TARGET_PAGE_MASK
;
318 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
325 case PSW_ASC_PRIMARY
:
326 PTE_DPRINTF("%s: asc=primary\n", __func__
);
327 r
= mmu_translate_asce(env
, vaddr
, asc
, env
->cregs
[1], raddr
, flags
,
331 PTE_DPRINTF("%s: asc=home\n", __func__
);
332 r
= mmu_translate_asce(env
, vaddr
, asc
, env
->cregs
[13], raddr
, flags
,
335 case PSW_ASC_SECONDARY
:
336 PTE_DPRINTF("%s: asc=secondary\n", __func__
);
338 * Instruction: Primary
341 if (rw
== MMU_INST_FETCH
) {
342 r
= mmu_translate_asce(env
, vaddr
, PSW_ASC_PRIMARY
, env
->cregs
[1],
343 raddr
, flags
, rw
, exc
);
344 *flags
&= ~(PAGE_READ
| PAGE_WRITE
);
346 r
= mmu_translate_asce(env
, vaddr
, PSW_ASC_SECONDARY
, env
->cregs
[7],
347 raddr
, flags
, rw
, exc
);
348 *flags
&= ~(PAGE_EXEC
);
353 hw_error("guest switched to unknown asc mode\n");
358 /* Convert real address -> absolute address */
359 *raddr
= mmu_real2abs(env
, *raddr
);
361 if (*raddr
< ram_size
) {
362 sk
= &env
->storage_keys
[*raddr
/ TARGET_PAGE_SIZE
];
363 if (*flags
& PAGE_READ
) {
367 if (*flags
& PAGE_WRITE
) {
376 * lowprot_enabled: Check whether low-address protection is enabled
378 static bool lowprot_enabled(const CPUS390XState
*env
)
380 if (!(env
->cregs
[0] & CR0_LOWPROT
)) {
383 if (!(env
->psw
.mask
& PSW_MASK_DAT
)) {
387 /* Check the private-space control bit */
388 switch (env
->psw
.mask
& PSW_MASK_ASC
) {
389 case PSW_ASC_PRIMARY
:
390 return !(env
->cregs
[1] & _ASCE_PRIVATE_SPACE
);
391 case PSW_ASC_SECONDARY
:
392 return !(env
->cregs
[7] & _ASCE_PRIVATE_SPACE
);
394 return !(env
->cregs
[13] & _ASCE_PRIVATE_SPACE
);
396 /* We don't support access register mode */
397 error_report("unsupported addressing mode");
403 * translate_pages: Translate a set of consecutive logical page addresses
404 * to absolute addresses
406 static int translate_pages(S390CPU
*cpu
, vaddr addr
, int nr_pages
,
407 target_ulong
*pages
, bool is_write
)
409 bool lowprot
= is_write
&& lowprot_enabled(&cpu
->env
);
410 uint64_t asc
= cpu
->env
.psw
.mask
& PSW_MASK_ASC
;
411 CPUS390XState
*env
= &cpu
->env
;
414 for (i
= 0; i
< nr_pages
; i
++) {
415 /* Low-address protection? */
416 if (lowprot
&& (addr
< 512 || (addr
>= 4096 && addr
< 4096 + 512))) {
417 trigger_access_exception(env
, PGM_PROTECTION
, ILEN_LATER_INC
, 0);
420 ret
= mmu_translate(env
, addr
, is_write
, asc
, &pages
[i
], &pflags
, true);
424 if (!address_space_access_valid(&address_space_memory
, pages
[i
],
425 TARGET_PAGE_SIZE
, is_write
)) {
426 program_interrupt(env
, PGM_ADDRESSING
, 0);
429 addr
+= TARGET_PAGE_SIZE
;
436 * s390_cpu_virt_mem_rw:
437 * @laddr: the logical start address
438 * @ar: the access register number
439 * @hostbuf: buffer in host memory. NULL = do only checks w/o copying
440 * @len: length that should be transferred
441 * @is_write: true = write, false = read
442 * Returns: 0 on success, non-zero if an exception occurred
444 * Copy from/to guest memory using logical addresses. Note that we inject a
445 * program interrupt in case there is an error while accessing the memory.
447 int s390_cpu_virt_mem_rw(S390CPU
*cpu
, vaddr laddr
, uint8_t ar
, void *hostbuf
,
448 int len
, bool is_write
)
450 int currlen
, nr_pages
, i
;
455 ret
= kvm_s390_mem_op(cpu
, laddr
, ar
, hostbuf
, len
, is_write
);
461 nr_pages
= (((laddr
& ~TARGET_PAGE_MASK
) + len
- 1) >> TARGET_PAGE_BITS
)
463 pages
= g_malloc(nr_pages
* sizeof(*pages
));
465 ret
= translate_pages(cpu
, laddr
, nr_pages
, pages
, is_write
);
466 if (ret
== 0 && hostbuf
!= NULL
) {
467 /* Copy data by stepping through the area page by page */
468 for (i
= 0; i
< nr_pages
; i
++) {
469 currlen
= MIN(len
, TARGET_PAGE_SIZE
- (laddr
% TARGET_PAGE_SIZE
));
470 cpu_physical_memory_rw(pages
[i
] | (laddr
& ~TARGET_PAGE_MASK
),
471 hostbuf
, currlen
, is_write
);