]>
Commit | Line | Data |
---|---|---|
dfebd7a7 TH |
1 | /* |
2 | * S390x MMU related functions | |
3 | * | |
4 | * Copyright (c) 2011 Alexander Graf | |
5 | * Copyright (c) 2015 Thomas Huth, IBM Corporation | |
6 | * | |
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. | |
11 | * | |
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. | |
16 | */ | |
17 | ||
9615495a | 18 | #include "qemu/osdep.h" |
c3edd628 TH |
19 | #include "qemu/error-report.h" |
20 | #include "exec/address-spaces.h" | |
dfebd7a7 | 21 | #include "cpu.h" |
4e58b838 | 22 | #include "internal.h" |
fba0a593 | 23 | #include "sysemu/kvm.h" |
0f5f6691 JH |
24 | #include "trace.h" |
25 | #include "hw/s390x/storage-keys.h" | |
dfebd7a7 TH |
26 | |
27 | /* #define DEBUG_S390 */ | |
28 | /* #define DEBUG_S390_PTE */ | |
29 | /* #define DEBUG_S390_STDOUT */ | |
30 | ||
31 | #ifdef DEBUG_S390 | |
32 | #ifdef DEBUG_S390_STDOUT | |
33 | #define DPRINTF(fmt, ...) \ | |
34 | do { fprintf(stderr, fmt, ## __VA_ARGS__); \ | |
013a2942 | 35 | if (qemu_log_separate()) qemu_log(fmt, ##__VA_ARGS__); } while (0) |
dfebd7a7 TH |
36 | #else |
37 | #define DPRINTF(fmt, ...) \ | |
38 | do { qemu_log(fmt, ## __VA_ARGS__); } while (0) | |
39 | #endif | |
40 | #else | |
41 | #define DPRINTF(fmt, ...) \ | |
42 | do { } while (0) | |
43 | #endif | |
44 | ||
45 | #ifdef DEBUG_S390_PTE | |
46 | #define PTE_DPRINTF DPRINTF | |
47 | #else | |
48 | #define PTE_DPRINTF(fmt, ...) \ | |
49 | do { } while (0) | |
50 | #endif | |
51 | ||
bab58bf0 TH |
52 | /* Fetch/store bits in the translation exception code: */ |
53 | #define FS_READ 0x800 | |
54 | #define FS_WRITE 0x400 | |
dfebd7a7 | 55 | |
801cdd35 TH |
56 | static void trigger_access_exception(CPUS390XState *env, uint32_t type, |
57 | uint32_t ilen, uint64_t tec) | |
58 | { | |
59 | S390CPU *cpu = s390_env_get_cpu(env); | |
60 | ||
61 | if (kvm_enabled()) { | |
62 | kvm_s390_access_exception(cpu, type, tec); | |
63 | } else { | |
64 | CPUState *cs = CPU(cpu); | |
65 | stq_phys(cs->as, env->psa + offsetof(LowCore, trans_exc_code), tec); | |
66 | trigger_pgm_exception(env, type, ilen); | |
67 | } | |
68 | } | |
69 | ||
dfebd7a7 | 70 | static void trigger_prot_fault(CPUS390XState *env, target_ulong vaddr, |
bab58bf0 | 71 | uint64_t asc, int rw, bool exc) |
dfebd7a7 | 72 | { |
bab58bf0 | 73 | uint64_t tec; |
dfebd7a7 | 74 | |
217a4acb | 75 | tec = vaddr | (rw == MMU_DATA_STORE ? FS_WRITE : FS_READ) | 4 | asc >> 46; |
bab58bf0 TH |
76 | |
77 | DPRINTF("%s: trans_exc_code=%016" PRIx64 "\n", __func__, tec); | |
dfebd7a7 | 78 | |
e3e09d87 TH |
79 | if (!exc) { |
80 | return; | |
81 | } | |
82 | ||
becf8217 | 83 | trigger_access_exception(env, PGM_PROTECTION, ILEN_AUTO, tec); |
dfebd7a7 TH |
84 | } |
85 | ||
86 | static void trigger_page_fault(CPUS390XState *env, target_ulong vaddr, | |
e3e09d87 | 87 | uint32_t type, uint64_t asc, int rw, bool exc) |
dfebd7a7 | 88 | { |
becf8217 | 89 | int ilen = ILEN_AUTO; |
bab58bf0 TH |
90 | uint64_t tec; |
91 | ||
217a4acb | 92 | tec = vaddr | (rw == MMU_DATA_STORE ? FS_WRITE : FS_READ) | asc >> 46; |
dfebd7a7 | 93 | |
c5b2ee4c | 94 | DPRINTF("%s: trans_exc_code=%016" PRIx64 "\n", __func__, tec); |
e3e09d87 TH |
95 | |
96 | if (!exc) { | |
97 | return; | |
98 | } | |
99 | ||
dfebd7a7 | 100 | /* Code accesses have an undefined ilc. */ |
217a4acb | 101 | if (rw == MMU_INST_FETCH) { |
dfebd7a7 TH |
102 | ilen = 2; |
103 | } | |
104 | ||
801cdd35 | 105 | trigger_access_exception(env, type, ilen, tec); |
dfebd7a7 TH |
106 | } |
107 | ||
108 | /** | |
109 | * Translate real address to absolute (= physical) | |
110 | * address by taking care of the prefix mapping. | |
111 | */ | |
f79f1ca4 | 112 | target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr) |
dfebd7a7 TH |
113 | { |
114 | if (raddr < 0x2000) { | |
115 | return raddr + env->psa; /* Map the lowcore. */ | |
116 | } else if (raddr >= env->psa && raddr < env->psa + 0x2000) { | |
117 | return raddr - env->psa; /* Map the 0 page. */ | |
118 | } | |
119 | return raddr; | |
120 | } | |
121 | ||
122 | /* Decode page table entry (normal 4KB page) */ | |
123 | static int mmu_translate_pte(CPUS390XState *env, target_ulong vaddr, | |
ede59855 | 124 | uint64_t asc, uint64_t pt_entry, |
e3e09d87 | 125 | target_ulong *raddr, int *flags, int rw, bool exc) |
dfebd7a7 | 126 | { |
ede59855 TH |
127 | if (pt_entry & _PAGE_INVALID) { |
128 | DPRINTF("%s: PTE=0x%" PRIx64 " invalid\n", __func__, pt_entry); | |
e3e09d87 | 129 | trigger_page_fault(env, vaddr, PGM_PAGE_TRANS, asc, rw, exc); |
dfebd7a7 TH |
130 | return -1; |
131 | } | |
b4ecbf80 TH |
132 | if (pt_entry & _PAGE_RES0) { |
133 | trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw, exc); | |
134 | return -1; | |
135 | } | |
ede59855 | 136 | if (pt_entry & _PAGE_RO) { |
dfebd7a7 TH |
137 | *flags &= ~PAGE_WRITE; |
138 | } | |
139 | ||
ede59855 | 140 | *raddr = pt_entry & _ASCE_ORIGIN; |
dfebd7a7 | 141 | |
ede59855 | 142 | PTE_DPRINTF("%s: PTE=0x%" PRIx64 "\n", __func__, pt_entry); |
dfebd7a7 TH |
143 | |
144 | return 0; | |
145 | } | |
146 | ||
f8f84e93 TH |
147 | /* Decode segment table entry */ |
148 | static int mmu_translate_segment(CPUS390XState *env, target_ulong vaddr, | |
149 | uint64_t asc, uint64_t st_entry, | |
e3e09d87 TH |
150 | target_ulong *raddr, int *flags, int rw, |
151 | bool exc) | |
dfebd7a7 | 152 | { |
f8f84e93 TH |
153 | CPUState *cs = CPU(s390_env_get_cpu(env)); |
154 | uint64_t origin, offs, pt_entry; | |
dfebd7a7 | 155 | |
f8f84e93 | 156 | if (st_entry & _SEGMENT_ENTRY_RO) { |
dfebd7a7 TH |
157 | *flags &= ~PAGE_WRITE; |
158 | } | |
159 | ||
f8f84e93 TH |
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); | |
164 | return 0; | |
165 | } | |
dfebd7a7 | 166 | |
f8f84e93 TH |
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); | |
e3e09d87 | 173 | return mmu_translate_pte(env, vaddr, asc, pt_entry, raddr, flags, rw, exc); |
dfebd7a7 TH |
174 | } |
175 | ||
f8f84e93 TH |
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, | |
e3e09d87 TH |
179 | target_ulong *raddr, int *flags, int rw, |
180 | bool exc) | |
dfebd7a7 TH |
181 | { |
182 | CPUState *cs = CPU(s390_env_get_cpu(env)); | |
f8f84e93 | 183 | uint64_t origin, offs, new_entry; |
5d180439 TH |
184 | const int pchks[4] = { |
185 | PGM_SEGMENT_TRANS, PGM_REG_THIRD_TRANS, | |
186 | PGM_REG_SEC_TRANS, PGM_REG_FIRST_TRANS | |
187 | }; | |
f8f84e93 TH |
188 | |
189 | PTE_DPRINTF("%s: 0x%" PRIx64 "\n", __func__, entry); | |
dfebd7a7 | 190 | |
f8f84e93 TH |
191 | origin = entry & _REGION_ENTRY_ORIGIN; |
192 | offs = (vaddr >> (17 + 11 * level / 4)) & 0x3ff8; | |
193 | ||
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); | |
dfebd7a7 | 197 | |
f8f84e93 | 198 | if ((new_entry & _REGION_ENTRY_INV) != 0) { |
dfebd7a7 | 199 | DPRINTF("%s: invalid region\n", __func__); |
5a123b3c | 200 | trigger_page_fault(env, vaddr, pchks[level / 4], asc, rw, exc); |
dfebd7a7 TH |
201 | return -1; |
202 | } | |
203 | ||
f8f84e93 | 204 | if ((new_entry & _REGION_ENTRY_TYPE_MASK) != level) { |
e3e09d87 | 205 | trigger_page_fault(env, vaddr, PGM_TRANS_SPEC, asc, rw, exc); |
dfebd7a7 TH |
206 | return -1; |
207 | } | |
208 | ||
dfebd7a7 | 209 | if (level == _ASCE_TYPE_SEGMENT) { |
f8f84e93 | 210 | return mmu_translate_segment(env, vaddr, asc, new_entry, raddr, flags, |
e3e09d87 | 211 | rw, exc); |
dfebd7a7 | 212 | } |
f8f84e93 | 213 | |
5d180439 TH |
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); | |
e3e09d87 | 219 | trigger_page_fault(env, vaddr, pchks[level / 4 - 1], asc, rw, exc); |
5d180439 TH |
220 | return -1; |
221 | } | |
222 | ||
43d49b01 TH |
223 | if ((env->cregs[0] & CR0_EDAT) && (new_entry & _REGION_ENTRY_RO)) { |
224 | *flags &= ~PAGE_WRITE; | |
225 | } | |
226 | ||
f8f84e93 TH |
227 | /* yet another region */ |
228 | return mmu_translate_region(env, vaddr, asc, new_entry, level - 4, | |
e3e09d87 | 229 | raddr, flags, rw, exc); |
dfebd7a7 TH |
230 | } |
231 | ||
9d77309c TH |
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) | |
dfebd7a7 | 235 | { |
f8f84e93 | 236 | int level; |
dfebd7a7 TH |
237 | int r; |
238 | ||
89a41e0a TH |
239 | if (asce & _ASCE_REAL_SPACE) { |
240 | /* direct mapping */ | |
241 | *raddr = vaddr; | |
242 | return 0; | |
243 | } | |
244 | ||
f8f84e93 TH |
245 | level = asce & _ASCE_TYPE_MASK; |
246 | switch (level) { | |
dfebd7a7 | 247 | case _ASCE_TYPE_REGION1: |
5d180439 | 248 | if ((vaddr >> 62) > (asce & _ASCE_TABLE_LENGTH)) { |
e3e09d87 | 249 | trigger_page_fault(env, vaddr, PGM_REG_FIRST_TRANS, asc, rw, exc); |
5d180439 TH |
250 | return -1; |
251 | } | |
dfebd7a7 TH |
252 | break; |
253 | case _ASCE_TYPE_REGION2: | |
254 | if (vaddr & 0xffe0000000000000ULL) { | |
255 | DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64 | |
256 | " 0xffe0000000000000ULL\n", __func__, vaddr); | |
d267571b | 257 | trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc); |
dfebd7a7 TH |
258 | return -1; |
259 | } | |
5d180439 | 260 | if ((vaddr >> 51 & 3) > (asce & _ASCE_TABLE_LENGTH)) { |
e3e09d87 | 261 | trigger_page_fault(env, vaddr, PGM_REG_SEC_TRANS, asc, rw, exc); |
5d180439 TH |
262 | return -1; |
263 | } | |
dfebd7a7 TH |
264 | break; |
265 | case _ASCE_TYPE_REGION3: | |
266 | if (vaddr & 0xfffffc0000000000ULL) { | |
267 | DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64 | |
268 | " 0xfffffc0000000000ULL\n", __func__, vaddr); | |
d267571b | 269 | trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc); |
dfebd7a7 TH |
270 | return -1; |
271 | } | |
5d180439 | 272 | if ((vaddr >> 40 & 3) > (asce & _ASCE_TABLE_LENGTH)) { |
e3e09d87 | 273 | trigger_page_fault(env, vaddr, PGM_REG_THIRD_TRANS, asc, rw, exc); |
5d180439 TH |
274 | return -1; |
275 | } | |
dfebd7a7 TH |
276 | break; |
277 | case _ASCE_TYPE_SEGMENT: | |
278 | if (vaddr & 0xffffffff80000000ULL) { | |
279 | DPRINTF("%s: vaddr doesn't fit 0x%16" PRIx64 | |
280 | " 0xffffffff80000000ULL\n", __func__, vaddr); | |
d267571b | 281 | trigger_page_fault(env, vaddr, PGM_ASCE_TYPE, asc, rw, exc); |
dfebd7a7 TH |
282 | return -1; |
283 | } | |
5d180439 | 284 | if ((vaddr >> 29 & 3) > (asce & _ASCE_TABLE_LENGTH)) { |
e3e09d87 | 285 | trigger_page_fault(env, vaddr, PGM_SEGMENT_TRANS, asc, rw, exc); |
5d180439 TH |
286 | return -1; |
287 | } | |
dfebd7a7 TH |
288 | break; |
289 | } | |
290 | ||
e3e09d87 TH |
291 | r = mmu_translate_region(env, vaddr, asc, asce, level, raddr, flags, rw, |
292 | exc); | |
217a4acb | 293 | if (rw == MMU_DATA_STORE && !(*flags & PAGE_WRITE)) { |
bab58bf0 | 294 | trigger_prot_fault(env, vaddr, asc, rw, exc); |
dfebd7a7 TH |
295 | return -1; |
296 | } | |
297 | ||
298 | return r; | |
299 | } | |
300 | ||
e3e09d87 TH |
301 | /** |
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 | |
631b22ea SW |
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 | |
e3e09d87 | 310 | */ |
dfebd7a7 | 311 | int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc, |
e3e09d87 | 312 | target_ulong *raddr, int *flags, bool exc) |
dfebd7a7 | 313 | { |
0f5f6691 JH |
314 | static S390SKeysState *ss; |
315 | static S390SKeysClass *skeyclass; | |
dfebd7a7 | 316 | int r = -1; |
0f5f6691 JH |
317 | uint8_t key; |
318 | ||
319 | if (unlikely(!ss)) { | |
320 | ss = s390_get_skeys_device(); | |
321 | skeyclass = S390_SKEYS_GET_CLASS(ss); | |
322 | } | |
dfebd7a7 TH |
323 | |
324 | *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
325 | vaddr &= TARGET_PAGE_MASK; | |
326 | ||
327 | if (!(env->psw.mask & PSW_MASK_DAT)) { | |
328 | *raddr = vaddr; | |
329 | r = 0; | |
330 | goto out; | |
331 | } | |
332 | ||
333 | switch (asc) { | |
334 | case PSW_ASC_PRIMARY: | |
9d77309c TH |
335 | PTE_DPRINTF("%s: asc=primary\n", __func__); |
336 | r = mmu_translate_asce(env, vaddr, asc, env->cregs[1], raddr, flags, | |
337 | rw, exc); | |
338 | break; | |
dfebd7a7 | 339 | case PSW_ASC_HOME: |
9d77309c TH |
340 | PTE_DPRINTF("%s: asc=home\n", __func__); |
341 | r = mmu_translate_asce(env, vaddr, asc, env->cregs[13], raddr, flags, | |
342 | rw, exc); | |
dfebd7a7 TH |
343 | break; |
344 | case PSW_ASC_SECONDARY: | |
9d77309c | 345 | PTE_DPRINTF("%s: asc=secondary\n", __func__); |
dfebd7a7 TH |
346 | /* |
347 | * Instruction: Primary | |
348 | * Data: Secondary | |
349 | */ | |
217a4acb | 350 | if (rw == MMU_INST_FETCH) { |
9d77309c TH |
351 | r = mmu_translate_asce(env, vaddr, PSW_ASC_PRIMARY, env->cregs[1], |
352 | raddr, flags, rw, exc); | |
dfebd7a7 TH |
353 | *flags &= ~(PAGE_READ | PAGE_WRITE); |
354 | } else { | |
9d77309c TH |
355 | r = mmu_translate_asce(env, vaddr, PSW_ASC_SECONDARY, env->cregs[7], |
356 | raddr, flags, rw, exc); | |
dfebd7a7 TH |
357 | *flags &= ~(PAGE_EXEC); |
358 | } | |
359 | break; | |
360 | case PSW_ASC_ACCREG: | |
361 | default: | |
362 | hw_error("guest switched to unknown asc mode\n"); | |
363 | break; | |
364 | } | |
365 | ||
366 | out: | |
367 | /* Convert real address -> absolute address */ | |
368 | *raddr = mmu_real2abs(env, *raddr); | |
369 | ||
0f5f6691 JH |
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); | |
373 | return 0; | |
374 | } | |
375 | ||
dfebd7a7 | 376 | if (*flags & PAGE_READ) { |
0f5f6691 | 377 | key |= SK_R; |
dfebd7a7 TH |
378 | } |
379 | ||
380 | if (*flags & PAGE_WRITE) { | |
0f5f6691 JH |
381 | key |= SK_C; |
382 | } | |
383 | ||
384 | if (skeyclass->set_skeys(ss, *raddr / TARGET_PAGE_SIZE, 1, &key)) { | |
385 | trace_set_skeys_nonzero(r); | |
386 | return 0; | |
dfebd7a7 TH |
387 | } |
388 | } | |
389 | ||
390 | return r; | |
391 | } | |
c3edd628 TH |
392 | |
393 | /** | |
394 | * lowprot_enabled: Check whether low-address protection is enabled | |
395 | */ | |
396 | static bool lowprot_enabled(const CPUS390XState *env) | |
397 | { | |
398 | if (!(env->cregs[0] & CR0_LOWPROT)) { | |
399 | return false; | |
400 | } | |
401 | if (!(env->psw.mask & PSW_MASK_DAT)) { | |
402 | return true; | |
403 | } | |
404 | ||
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); | |
411 | case PSW_ASC_HOME: | |
412 | return !(env->cregs[13] & _ASCE_PRIVATE_SPACE); | |
413 | default: | |
414 | /* We don't support access register mode */ | |
415 | error_report("unsupported addressing mode"); | |
416 | exit(1); | |
417 | } | |
418 | } | |
419 | ||
420 | /** | |
421 | * translate_pages: Translate a set of consecutive logical page addresses | |
422 | * to absolute addresses | |
423 | */ | |
424 | static int translate_pages(S390CPU *cpu, vaddr addr, int nr_pages, | |
425 | target_ulong *pages, bool is_write) | |
426 | { | |
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; | |
430 | int ret, i, pflags; | |
431 | ||
432 | for (i = 0; i < nr_pages; i++) { | |
433 | /* Low-address protection? */ | |
434 | if (lowprot && (addr < 512 || (addr >= 4096 && addr < 4096 + 512))) { | |
becf8217 | 435 | trigger_access_exception(env, PGM_PROTECTION, ILEN_AUTO, 0); |
c3edd628 TH |
436 | return -EACCES; |
437 | } | |
438 | ret = mmu_translate(env, addr, is_write, asc, &pages[i], &pflags, true); | |
439 | if (ret) { | |
440 | return ret; | |
441 | } | |
442 | if (!address_space_access_valid(&address_space_memory, pages[i], | |
443 | TARGET_PAGE_SIZE, is_write)) { | |
98987d30 | 444 | program_interrupt(env, PGM_ADDRESSING, ILEN_AUTO); |
c3edd628 TH |
445 | return -EFAULT; |
446 | } | |
447 | addr += TARGET_PAGE_SIZE; | |
448 | } | |
449 | ||
450 | return 0; | |
451 | } | |
452 | ||
453 | /** | |
454 | * s390_cpu_virt_mem_rw: | |
455 | * @laddr: the logical start address | |
6cb1e49d | 456 | * @ar: the access register number |
c3edd628 | 457 | * @hostbuf: buffer in host memory. NULL = do only checks w/o copying |
631b22ea | 458 | * @len: length that should be transferred |
c3edd628 | 459 | * @is_write: true = write, false = read |
631b22ea | 460 | * Returns: 0 on success, non-zero if an exception occurred |
c3edd628 TH |
461 | * |
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. | |
464 | */ | |
6cb1e49d | 465 | int s390_cpu_virt_mem_rw(S390CPU *cpu, vaddr laddr, uint8_t ar, void *hostbuf, |
c3edd628 TH |
466 | int len, bool is_write) |
467 | { | |
468 | int currlen, nr_pages, i; | |
469 | target_ulong *pages; | |
470 | int ret; | |
471 | ||
a9bcd1b8 | 472 | if (kvm_enabled()) { |
6cb1e49d | 473 | ret = kvm_s390_mem_op(cpu, laddr, ar, hostbuf, len, is_write); |
a9bcd1b8 TH |
474 | if (ret >= 0) { |
475 | return ret; | |
476 | } | |
477 | } | |
478 | ||
c3edd628 TH |
479 | nr_pages = (((laddr & ~TARGET_PAGE_MASK) + len - 1) >> TARGET_PAGE_BITS) |
480 | + 1; | |
481 | pages = g_malloc(nr_pages * sizeof(*pages)); | |
482 | ||
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); | |
490 | laddr += currlen; | |
491 | hostbuf += currlen; | |
492 | len -= currlen; | |
493 | } | |
494 | } | |
495 | ||
496 | g_free(pages); | |
497 | return ret; | |
498 | } |