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.
20 /* #define DEBUG_S390 */
21 /* #define DEBUG_S390_PTE */
22 /* #define DEBUG_S390_STDOUT */
25 #ifdef DEBUG_S390_STDOUT
26 #define DPRINTF(fmt, ...) \
27 do { fprintf(stderr, fmt, ## __VA_ARGS__); \
28 qemu_log(fmt, ##__VA_ARGS__); } while (0)
30 #define DPRINTF(fmt, ...) \
31 do { qemu_log(fmt, ## __VA_ARGS__); } while (0)
34 #define DPRINTF(fmt, ...) \
39 #define PTE_DPRINTF DPRINTF
41 #define PTE_DPRINTF(fmt, ...) \
45 /* Fetch/store bits in the translation exception code: */
47 #define FS_WRITE 0x400
49 static void trigger_prot_fault(CPUS390XState
*env
, target_ulong vaddr
,
50 uint64_t asc
, int rw
, bool exc
)
52 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
55 tec
= vaddr
| (rw
== 1 ? FS_WRITE
: FS_READ
) | 4 | asc
>> 46;
57 DPRINTF("%s: trans_exc_code=%016" PRIx64
"\n", __func__
, tec
);
63 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
64 trigger_pgm_exception(env
, PGM_PROTECTION
, ILEN_LATER_INC
);
67 static void trigger_page_fault(CPUS390XState
*env
, target_ulong vaddr
,
68 uint32_t type
, uint64_t asc
, int rw
, bool exc
)
70 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
71 int ilen
= ILEN_LATER
;
74 tec
= vaddr
| (rw
== 1 ? FS_WRITE
: FS_READ
) | asc
>> 46;
76 DPRINTF("%s: vaddr=%016" PRIx64
" bits=%d\n", __func__
, vaddr
, bits
);
82 /* Code accesses have an undefined ilc. */
87 stq_phys(cs
->as
, env
->psa
+ offsetof(LowCore
, trans_exc_code
), tec
);
88 trigger_pgm_exception(env
, type
, ilen
);
92 * Translate real address to absolute (= physical)
93 * address by taking care of the prefix mapping.
95 static target_ulong
mmu_real2abs(CPUS390XState
*env
, target_ulong raddr
)
98 return raddr
+ env
->psa
; /* Map the lowcore. */
99 } else if (raddr
>= env
->psa
&& raddr
< env
->psa
+ 0x2000) {
100 return raddr
- env
->psa
; /* Map the 0 page. */
105 /* Decode page table entry (normal 4KB page) */
106 static int mmu_translate_pte(CPUS390XState
*env
, target_ulong vaddr
,
107 uint64_t asc
, uint64_t asce
,
108 target_ulong
*raddr
, int *flags
, int rw
, bool exc
)
110 if (asce
& _PAGE_INVALID
) {
111 DPRINTF("%s: PTE=0x%" PRIx64
" invalid\n", __func__
, asce
);
112 trigger_page_fault(env
, vaddr
, PGM_PAGE_TRANS
, asc
, rw
, exc
);
116 if (asce
& _PAGE_RO
) {
117 *flags
&= ~PAGE_WRITE
;
120 *raddr
= asce
& _ASCE_ORIGIN
;
122 PTE_DPRINTF("%s: PTE=0x%" PRIx64
"\n", __func__
, asce
);
127 #define VADDR_PX 0xff000 /* Page index bits */
129 /* Decode segment table entry */
130 static int mmu_translate_segment(CPUS390XState
*env
, target_ulong vaddr
,
131 uint64_t asc
, uint64_t st_entry
,
132 target_ulong
*raddr
, int *flags
, int rw
,
135 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
136 uint64_t origin
, offs
, pt_entry
;
138 if (st_entry
& _SEGMENT_ENTRY_RO
) {
139 *flags
&= ~PAGE_WRITE
;
142 if ((st_entry
& _SEGMENT_ENTRY_FC
) && (env
->cregs
[0] & CR0_EDAT
)) {
143 /* Decode EDAT1 segment frame absolute address (1MB page) */
144 *raddr
= (st_entry
& 0xfffffffffff00000ULL
) | (vaddr
& 0xfffff);
145 PTE_DPRINTF("%s: SEG=0x%" PRIx64
"\n", __func__
, st_entry
);
149 /* Look up 4KB page entry */
150 origin
= st_entry
& _SEGMENT_ENTRY_ORIGIN
;
151 offs
= (vaddr
& VADDR_PX
) >> 9;
152 pt_entry
= ldq_phys(cs
->as
, origin
+ offs
);
153 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
154 __func__
, origin
, offs
, pt_entry
);
155 return mmu_translate_pte(env
, vaddr
, asc
, pt_entry
, raddr
, flags
, rw
, exc
);
158 /* Decode region table entries */
159 static int mmu_translate_region(CPUS390XState
*env
, target_ulong vaddr
,
160 uint64_t asc
, uint64_t entry
, int level
,
161 target_ulong
*raddr
, int *flags
, int rw
,
164 CPUState
*cs
= CPU(s390_env_get_cpu(env
));
165 uint64_t origin
, offs
, new_entry
;
166 const int pchks
[4] = {
167 PGM_SEGMENT_TRANS
, PGM_REG_THIRD_TRANS
,
168 PGM_REG_SEC_TRANS
, PGM_REG_FIRST_TRANS
171 PTE_DPRINTF("%s: 0x%" PRIx64
"\n", __func__
, entry
);
173 origin
= entry
& _REGION_ENTRY_ORIGIN
;
174 offs
= (vaddr
>> (17 + 11 * level
/ 4)) & 0x3ff8;
176 new_entry
= ldq_phys(cs
->as
, origin
+ offs
);
177 PTE_DPRINTF("%s: 0x%" PRIx64
" + 0x%" PRIx64
" => 0x%016" PRIx64
"\n",
178 __func__
, origin
, offs
, new_entry
);
180 if ((new_entry
& _REGION_ENTRY_INV
) != 0) {
181 /* XXX different regions have different faults */
182 DPRINTF("%s: invalid region\n", __func__
);
183 trigger_page_fault(env
, vaddr
, PGM_SEGMENT_TRANS
, asc
, rw
, exc
);
187 if ((new_entry
& _REGION_ENTRY_TYPE_MASK
) != level
) {
188 trigger_page_fault(env
, vaddr
, PGM_TRANS_SPEC
, asc
, rw
, exc
);
192 /* XXX region protection flags */
193 /* *flags &= ~PAGE_WRITE */
195 if (level
== _ASCE_TYPE_SEGMENT
) {
196 return mmu_translate_segment(env
, vaddr
, asc
, new_entry
, raddr
, flags
,
200 /* Check region table offset and length */
201 offs
= (vaddr
>> (28 + 11 * (level
- 4) / 4)) & 3;
202 if (offs
< ((new_entry
& _REGION_ENTRY_TF
) >> 6)
203 || offs
> (new_entry
& _REGION_ENTRY_LENGTH
)) {
204 DPRINTF("%s: invalid offset or len (%lx)\n", __func__
, new_entry
);
205 trigger_page_fault(env
, vaddr
, pchks
[level
/ 4 - 1], asc
, rw
, exc
);
209 /* yet another region */
210 return mmu_translate_region(env
, vaddr
, asc
, new_entry
, level
- 4,
211 raddr
, flags
, rw
, exc
);
214 static int mmu_translate_asc(CPUS390XState
*env
, target_ulong vaddr
,
215 uint64_t asc
, target_ulong
*raddr
, int *flags
,
223 case PSW_ASC_PRIMARY
:
224 PTE_DPRINTF("%s: asc=primary\n", __func__
);
225 asce
= env
->cregs
[1];
227 case PSW_ASC_SECONDARY
:
228 PTE_DPRINTF("%s: asc=secondary\n", __func__
);
229 asce
= env
->cregs
[7];
232 PTE_DPRINTF("%s: asc=home\n", __func__
);
233 asce
= env
->cregs
[13];
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
== 1) && !(*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 occured
307 * @return 0 if the translation was successfull, -1 if a fault occured
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
:
327 r
= mmu_translate_asc(env
, vaddr
, asc
, raddr
, flags
, rw
, exc
);
329 case PSW_ASC_SECONDARY
:
331 * Instruction: Primary
335 r
= mmu_translate_asc(env
, vaddr
, PSW_ASC_PRIMARY
, raddr
, flags
,
337 *flags
&= ~(PAGE_READ
| PAGE_WRITE
);
339 r
= mmu_translate_asc(env
, vaddr
, PSW_ASC_SECONDARY
, raddr
, flags
,
341 *flags
&= ~(PAGE_EXEC
);
346 hw_error("guest switched to unknown asc mode\n");
351 /* Convert real address -> absolute address */
352 *raddr
= mmu_real2abs(env
, *raddr
);
354 if (*raddr
<= ram_size
) {
355 sk
= &env
->storage_keys
[*raddr
/ TARGET_PAGE_SIZE
];
356 if (*flags
& PAGE_READ
) {
360 if (*flags
& PAGE_WRITE
) {