xref: /openbmc/qemu/target/s390x/mmu_helper.c (revision 7ffc4894)
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 
18 #include "qemu/osdep.h"
19 #include "qemu/error-report.h"
20 #include "exec/address-spaces.h"
21 #include "cpu.h"
22 #include "s390x-internal.h"
23 #include "kvm/kvm_s390x.h"
24 #include "sysemu/kvm.h"
25 #include "sysemu/tcg.h"
26 #include "exec/exec-all.h"
27 #include "exec/page-protection.h"
28 #include "trace.h"
29 #include "hw/hw.h"
30 #include "hw/s390x/storage-keys.h"
31 #include "hw/boards.h"
32 
33 /* Fetch/store bits in the translation exception code: */
34 #define FS_READ  0x800
35 #define FS_WRITE 0x400
36 
37 static void trigger_access_exception(CPUS390XState *env, uint32_t type,
38                                      uint64_t tec)
39 {
40     S390CPU *cpu = env_archcpu(env);
41 
42     if (kvm_enabled()) {
43         kvm_s390_access_exception(cpu, type, tec);
44     } else {
45         CPUState *cs = env_cpu(env);
46         if (type != PGM_ADDRESSING) {
47             stq_phys(cs->as, env->psa + offsetof(LowCore, trans_exc_code), tec);
48         }
49         trigger_pgm_exception(env, type);
50     }
51 }
52 
53 /* check whether the address would be proteted by Low-Address Protection */
54 static bool is_low_address(uint64_t addr)
55 {
56     return addr <= 511 || (addr >= 4096 && addr <= 4607);
57 }
58 
59 /* check whether Low-Address Protection is enabled for mmu_translate() */
60 static bool lowprot_enabled(const CPUS390XState *env, uint64_t asc)
61 {
62     if (!(env->cregs[0] & CR0_LOWPROT)) {
63         return false;
64     }
65     if (!(env->psw.mask & PSW_MASK_DAT)) {
66         return true;
67     }
68 
69     /* Check the private-space control bit */
70     switch (asc) {
71     case PSW_ASC_PRIMARY:
72         return !(env->cregs[1] & ASCE_PRIVATE_SPACE);
73     case PSW_ASC_SECONDARY:
74         return !(env->cregs[7] & ASCE_PRIVATE_SPACE);
75     case PSW_ASC_HOME:
76         return !(env->cregs[13] & ASCE_PRIVATE_SPACE);
77     default:
78         /* We don't support access register mode */
79         error_report("unsupported addressing mode");
80         exit(1);
81     }
82 }
83 
84 /**
85  * Translate real address to absolute (= physical)
86  * address by taking care of the prefix mapping.
87  */
88 target_ulong mmu_real2abs(CPUS390XState *env, target_ulong raddr)
89 {
90     if (raddr < 0x2000) {
91         return raddr + env->psa;    /* Map the lowcore. */
92     } else if (raddr >= env->psa && raddr < env->psa + 0x2000) {
93         return raddr - env->psa;    /* Map the 0 page. */
94     }
95     return raddr;
96 }
97 
98 bool mmu_absolute_addr_valid(target_ulong addr, bool is_write)
99 {
100     return address_space_access_valid(&address_space_memory,
101                                       addr & TARGET_PAGE_MASK,
102                                       TARGET_PAGE_SIZE, is_write,
103                                       MEMTXATTRS_UNSPECIFIED);
104 }
105 
106 static inline bool read_table_entry(CPUS390XState *env, hwaddr gaddr,
107                                     uint64_t *entry)
108 {
109     CPUState *cs = env_cpu(env);
110 
111     /*
112      * According to the PoP, these table addresses are "unpredictably real
113      * or absolute". Also, "it is unpredictable whether the address wraps
114      * or an addressing exception is recognized".
115      *
116      * We treat them as absolute addresses and don't wrap them.
117      */
118     if (unlikely(address_space_read(cs->as, gaddr, MEMTXATTRS_UNSPECIFIED,
119                                     entry, sizeof(*entry)) !=
120                  MEMTX_OK)) {
121         return false;
122     }
123     *entry = be64_to_cpu(*entry);
124     return true;
125 }
126 
127 static int mmu_translate_asce(CPUS390XState *env, target_ulong vaddr,
128                               uint64_t asc, uint64_t asce, target_ulong *raddr,
129                               int *flags)
130 {
131     const bool edat1 = (env->cregs[0] & CR0_EDAT) &&
132                        s390_has_feat(S390_FEAT_EDAT);
133     const bool edat2 = edat1 && s390_has_feat(S390_FEAT_EDAT_2);
134     const bool iep = (env->cregs[0] & CR0_IEP) &&
135                      s390_has_feat(S390_FEAT_INSTRUCTION_EXEC_PROT);
136     const int asce_tl = asce & ASCE_TABLE_LENGTH;
137     const int asce_p = asce & ASCE_PRIVATE_SPACE;
138     hwaddr gaddr = asce & ASCE_ORIGIN;
139     uint64_t entry;
140 
141     if (asce & ASCE_REAL_SPACE) {
142         /* direct mapping */
143         *raddr = vaddr;
144         return 0;
145     }
146 
147     switch (asce & ASCE_TYPE_MASK) {
148     case ASCE_TYPE_REGION1:
149         if (VADDR_REGION1_TL(vaddr) > asce_tl) {
150             return PGM_REG_FIRST_TRANS;
151         }
152         gaddr += VADDR_REGION1_TX(vaddr) * 8;
153         break;
154     case ASCE_TYPE_REGION2:
155         if (VADDR_REGION1_TX(vaddr)) {
156             return PGM_ASCE_TYPE;
157         }
158         if (VADDR_REGION2_TL(vaddr) > asce_tl) {
159             return PGM_REG_SEC_TRANS;
160         }
161         gaddr += VADDR_REGION2_TX(vaddr) * 8;
162         break;
163     case ASCE_TYPE_REGION3:
164         if (VADDR_REGION1_TX(vaddr) || VADDR_REGION2_TX(vaddr)) {
165             return PGM_ASCE_TYPE;
166         }
167         if (VADDR_REGION3_TL(vaddr) > asce_tl) {
168             return PGM_REG_THIRD_TRANS;
169         }
170         gaddr += VADDR_REGION3_TX(vaddr) * 8;
171         break;
172     case ASCE_TYPE_SEGMENT:
173         if (VADDR_REGION1_TX(vaddr) || VADDR_REGION2_TX(vaddr) ||
174             VADDR_REGION3_TX(vaddr)) {
175             return PGM_ASCE_TYPE;
176         }
177         if (VADDR_SEGMENT_TL(vaddr) > asce_tl) {
178             return PGM_SEGMENT_TRANS;
179         }
180         gaddr += VADDR_SEGMENT_TX(vaddr) * 8;
181         break;
182     }
183 
184     switch (asce & ASCE_TYPE_MASK) {
185     case ASCE_TYPE_REGION1:
186         if (!read_table_entry(env, gaddr, &entry)) {
187             return PGM_ADDRESSING;
188         }
189         if (entry & REGION_ENTRY_I) {
190             return PGM_REG_FIRST_TRANS;
191         }
192         if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION1) {
193             return PGM_TRANS_SPEC;
194         }
195         if (VADDR_REGION2_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
196             VADDR_REGION2_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
197             return PGM_REG_SEC_TRANS;
198         }
199         if (edat1 && (entry & REGION_ENTRY_P)) {
200             *flags &= ~PAGE_WRITE;
201         }
202         gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_REGION2_TX(vaddr) * 8;
203         /* fall through */
204     case ASCE_TYPE_REGION2:
205         if (!read_table_entry(env, gaddr, &entry)) {
206             return PGM_ADDRESSING;
207         }
208         if (entry & REGION_ENTRY_I) {
209             return PGM_REG_SEC_TRANS;
210         }
211         if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION2) {
212             return PGM_TRANS_SPEC;
213         }
214         if (VADDR_REGION3_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
215             VADDR_REGION3_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
216             return PGM_REG_THIRD_TRANS;
217         }
218         if (edat1 && (entry & REGION_ENTRY_P)) {
219             *flags &= ~PAGE_WRITE;
220         }
221         gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_REGION3_TX(vaddr) * 8;
222         /* fall through */
223     case ASCE_TYPE_REGION3:
224         if (!read_table_entry(env, gaddr, &entry)) {
225             return PGM_ADDRESSING;
226         }
227         if (entry & REGION_ENTRY_I) {
228             return PGM_REG_THIRD_TRANS;
229         }
230         if ((entry & REGION_ENTRY_TT) != REGION_ENTRY_TT_REGION3) {
231             return PGM_TRANS_SPEC;
232         }
233         if (edat2 && (entry & REGION3_ENTRY_CR) && asce_p) {
234             return PGM_TRANS_SPEC;
235         }
236         if (edat1 && (entry & REGION_ENTRY_P)) {
237             *flags &= ~PAGE_WRITE;
238         }
239         if (edat2 && (entry & REGION3_ENTRY_FC)) {
240             if (iep && (entry & REGION3_ENTRY_IEP)) {
241                 *flags &= ~PAGE_EXEC;
242             }
243             *raddr = (entry & REGION3_ENTRY_RFAA) |
244                      (vaddr & ~REGION3_ENTRY_RFAA);
245             return 0;
246         }
247         if (VADDR_SEGMENT_TL(vaddr) < (entry & REGION_ENTRY_TF) >> 6 ||
248             VADDR_SEGMENT_TL(vaddr) > (entry & REGION_ENTRY_TL)) {
249             return PGM_SEGMENT_TRANS;
250         }
251         gaddr = (entry & REGION_ENTRY_ORIGIN) + VADDR_SEGMENT_TX(vaddr) * 8;
252         /* fall through */
253     case ASCE_TYPE_SEGMENT:
254         if (!read_table_entry(env, gaddr, &entry)) {
255             return PGM_ADDRESSING;
256         }
257         if (entry & SEGMENT_ENTRY_I) {
258             return PGM_SEGMENT_TRANS;
259         }
260         if ((entry & SEGMENT_ENTRY_TT) != SEGMENT_ENTRY_TT_SEGMENT) {
261             return PGM_TRANS_SPEC;
262         }
263         if ((entry & SEGMENT_ENTRY_CS) && asce_p) {
264             return PGM_TRANS_SPEC;
265         }
266         if (entry & SEGMENT_ENTRY_P) {
267             *flags &= ~PAGE_WRITE;
268         }
269         if (edat1 && (entry & SEGMENT_ENTRY_FC)) {
270             if (iep && (entry & SEGMENT_ENTRY_IEP)) {
271                 *flags &= ~PAGE_EXEC;
272             }
273             *raddr = (entry & SEGMENT_ENTRY_SFAA) |
274                      (vaddr & ~SEGMENT_ENTRY_SFAA);
275             return 0;
276         }
277         gaddr = (entry & SEGMENT_ENTRY_ORIGIN) + VADDR_PAGE_TX(vaddr) * 8;
278         break;
279     }
280 
281     if (!read_table_entry(env, gaddr, &entry)) {
282         return PGM_ADDRESSING;
283     }
284     if (entry & PAGE_ENTRY_I) {
285         return PGM_PAGE_TRANS;
286     }
287     if (entry & PAGE_ENTRY_0) {
288         return PGM_TRANS_SPEC;
289     }
290     if (entry & PAGE_ENTRY_P) {
291         *flags &= ~PAGE_WRITE;
292     }
293     if (iep && (entry & PAGE_ENTRY_IEP)) {
294         *flags &= ~PAGE_EXEC;
295     }
296 
297     *raddr = entry & TARGET_PAGE_MASK;
298     return 0;
299 }
300 
301 static void mmu_handle_skey(target_ulong addr, int rw, int *flags)
302 {
303     static S390SKeysClass *skeyclass;
304     static S390SKeysState *ss;
305     uint8_t key, old_key;
306     int rc;
307 
308     /*
309      * We expect to be called with an absolute address that has already been
310      * validated, such that we can reliably use it to lookup the storage key.
311      */
312     if (unlikely(!ss)) {
313         ss = s390_get_skeys_device();
314         skeyclass = S390_SKEYS_GET_CLASS(ss);
315     }
316 
317     /*
318      * Don't enable storage keys if they are still disabled, i.e., no actual
319      * storage key instruction was issued yet.
320      */
321     if (!skeyclass->skeys_are_enabled(ss)) {
322         return;
323     }
324 
325     /*
326      * Whenever we create a new TLB entry, we set the storage key reference
327      * bit. In case we allow write accesses, we set the storage key change
328      * bit. Whenever the guest changes the storage key, we have to flush the
329      * TLBs of all CPUs (the whole TLB or all affected entries), so that the
330      * next reference/change will result in an MMU fault and make us properly
331      * update the storage key here.
332      *
333      * Note 1: "record of references ... is not necessarily accurate",
334      *         "change bit may be set in case no storing has occurred".
335      *         -> We can set reference/change bits even on exceptions.
336      * Note 2: certain accesses seem to ignore storage keys. For example,
337      *         DAT translation does not set reference bits for table accesses.
338      *
339      * TODO: key-controlled protection. Only CPU accesses make use of the
340      *       PSW key. CSS accesses are different - we have to pass in the key.
341      *
342      * TODO: we have races between getting and setting the key.
343      */
344     rc = skeyclass->get_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
345     if (rc) {
346         trace_get_skeys_nonzero(rc);
347         return;
348     }
349     old_key = key;
350 
351     switch (rw) {
352     case MMU_DATA_LOAD:
353     case MMU_INST_FETCH:
354         /*
355          * The TLB entry has to remain write-protected on read-faults if
356          * the storage key does not indicate a change already. Otherwise
357          * we might miss setting the change bit on write accesses.
358          */
359         if (!(key & SK_C)) {
360             *flags &= ~PAGE_WRITE;
361         }
362         break;
363     case MMU_DATA_STORE:
364         key |= SK_C;
365         break;
366     default:
367         g_assert_not_reached();
368     }
369 
370     /* Any store/fetch sets the reference bit */
371     key |= SK_R;
372 
373     if (key != old_key) {
374         rc = skeyclass->set_skeys(ss, addr / TARGET_PAGE_SIZE, 1, &key);
375         if (rc) {
376             trace_set_skeys_nonzero(rc);
377         }
378     }
379 }
380 
381 /**
382  * Translate a virtual (logical) address into a physical (absolute) address.
383  * @param vaddr  the virtual address
384  * @param rw     0 = read, 1 = write, 2 = code fetch, < 0 = load real address
385  * @param asc    address space control (one of the PSW_ASC_* modes)
386  * @param raddr  the translated address is stored to this pointer
387  * @param flags  the PAGE_READ/WRITE/EXEC flags are stored to this pointer
388  * @param tec    the translation exception code if stored to this pointer if
389  *               there is an exception to raise
390  * @return       0 = success, != 0, the exception to raise
391  */
392 int mmu_translate(CPUS390XState *env, target_ulong vaddr, int rw, uint64_t asc,
393                   target_ulong *raddr, int *flags, uint64_t *tec)
394 {
395     uint64_t asce;
396     int r;
397 
398     *tec = (vaddr & TARGET_PAGE_MASK) | (asc >> 46) |
399             (rw == MMU_DATA_STORE ? FS_WRITE : FS_READ);
400     *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
401 
402     if (is_low_address(vaddr & TARGET_PAGE_MASK) && lowprot_enabled(env, asc)) {
403         /*
404          * If any part of this page is currently protected, make sure the
405          * TLB entry will not be reused.
406          *
407          * As the protected range is always the first 512 bytes of the
408          * two first pages, we are able to catch all writes to these areas
409          * just by looking at the start address (triggering the tlb miss).
410          */
411         *flags |= PAGE_WRITE_INV;
412         if (is_low_address(vaddr) && rw == MMU_DATA_STORE) {
413             /* LAP sets bit 56 */
414             *tec |= 0x80;
415             return PGM_PROTECTION;
416         }
417     }
418 
419     vaddr &= TARGET_PAGE_MASK;
420 
421     if (rw != MMU_S390_LRA && !(env->psw.mask & PSW_MASK_DAT)) {
422         *raddr = vaddr;
423         goto nodat;
424     }
425 
426     switch (asc) {
427     case PSW_ASC_PRIMARY:
428         asce = env->cregs[1];
429         break;
430     case PSW_ASC_HOME:
431         asce = env->cregs[13];
432         break;
433     case PSW_ASC_SECONDARY:
434         asce = env->cregs[7];
435         break;
436     case PSW_ASC_ACCREG:
437     default:
438         hw_error("guest switched to unknown asc mode\n");
439         break;
440     }
441 
442     /* perform the DAT translation */
443     r = mmu_translate_asce(env, vaddr, asc, asce, raddr, flags);
444     if (unlikely(r)) {
445         return r;
446     }
447 
448     /* check for DAT protection */
449     if (unlikely(rw == MMU_DATA_STORE && !(*flags & PAGE_WRITE))) {
450         /* DAT sets bit 61 only */
451         *tec |= 0x4;
452         return PGM_PROTECTION;
453     }
454 
455     /* check for Instruction-Execution-Protection */
456     if (unlikely(rw == MMU_INST_FETCH && !(*flags & PAGE_EXEC))) {
457         /* IEP sets bit 56 and 61 */
458         *tec |= 0x84;
459         return PGM_PROTECTION;
460     }
461 
462 nodat:
463     if (rw >= 0) {
464         /* Convert real address -> absolute address */
465         *raddr = mmu_real2abs(env, *raddr);
466 
467         if (!mmu_absolute_addr_valid(*raddr, rw == MMU_DATA_STORE)) {
468             *tec = 0; /* unused */
469             return PGM_ADDRESSING;
470         }
471 
472         mmu_handle_skey(*raddr, rw, flags);
473     }
474     return 0;
475 }
476 
477 /**
478  * translate_pages: Translate a set of consecutive logical page addresses
479  * to absolute addresses. This function is used for TCG and old KVM without
480  * the MEMOP interface.
481  */
482 static int translate_pages(S390CPU *cpu, vaddr addr, int nr_pages,
483                            target_ulong *pages, bool is_write, uint64_t *tec)
484 {
485     uint64_t asc = cpu->env.psw.mask & PSW_MASK_ASC;
486     CPUS390XState *env = &cpu->env;
487     int ret, i, pflags;
488 
489     for (i = 0; i < nr_pages; i++) {
490         ret = mmu_translate(env, addr, is_write, asc, &pages[i], &pflags, tec);
491         if (ret) {
492             return ret;
493         }
494         addr += TARGET_PAGE_SIZE;
495     }
496 
497     return 0;
498 }
499 
500 int s390_cpu_pv_mem_rw(S390CPU *cpu, unsigned int offset, void *hostbuf,
501                        int len, bool is_write)
502 {
503     int ret;
504 
505     if (kvm_enabled()) {
506         ret = kvm_s390_mem_op_pv(cpu, offset, hostbuf, len, is_write);
507     } else {
508         /* Protected Virtualization is a KVM/Hardware only feature */
509         g_assert_not_reached();
510     }
511     return ret;
512 }
513 
514 /**
515  * s390_cpu_virt_mem_rw:
516  * @laddr:     the logical start address
517  * @ar:        the access register number
518  * @hostbuf:   buffer in host memory. NULL = do only checks w/o copying
519  * @len:       length that should be transferred
520  * @is_write:  true = write, false = read
521  * Returns:    0 on success, non-zero if an exception occurred
522  *
523  * Copy from/to guest memory using logical addresses. Note that we inject a
524  * program interrupt in case there is an error while accessing the memory.
525  *
526  * This function will always return (also for TCG), make sure to call
527  * s390_cpu_virt_mem_handle_exc() to properly exit the CPU loop.
528  */
529 int s390_cpu_virt_mem_rw(S390CPU *cpu, vaddr laddr, uint8_t ar, void *hostbuf,
530                          int len, bool is_write)
531 {
532     int currlen, nr_pages, i;
533     target_ulong *pages;
534     uint64_t tec;
535     int ret;
536 
537     if (kvm_enabled()) {
538         ret = kvm_s390_mem_op(cpu, laddr, ar, hostbuf, len, is_write);
539         if (ret >= 0) {
540             return ret;
541         }
542     }
543 
544     nr_pages = (((laddr & ~TARGET_PAGE_MASK) + len - 1) >> TARGET_PAGE_BITS)
545                + 1;
546     pages = g_malloc(nr_pages * sizeof(*pages));
547 
548     ret = translate_pages(cpu, laddr, nr_pages, pages, is_write, &tec);
549     if (ret) {
550         trigger_access_exception(&cpu->env, ret, tec);
551     } else if (hostbuf != NULL) {
552         /* Copy data by stepping through the area page by page */
553         for (i = 0; i < nr_pages; i++) {
554             currlen = MIN(len, TARGET_PAGE_SIZE - (laddr % TARGET_PAGE_SIZE));
555             cpu_physical_memory_rw(pages[i] | (laddr & ~TARGET_PAGE_MASK),
556                                    hostbuf, currlen, is_write);
557             laddr += currlen;
558             hostbuf += currlen;
559             len -= currlen;
560         }
561     }
562 
563     g_free(pages);
564     return ret;
565 }
566 
567 void s390_cpu_virt_mem_handle_exc(S390CPU *cpu, uintptr_t ra)
568 {
569     /* KVM will handle the interrupt automatically, TCG has to exit the TB */
570 #ifdef CONFIG_TCG
571     if (tcg_enabled()) {
572         cpu_loop_exit_restore(CPU(cpu), ra);
573     }
574 #endif
575 }
576 
577 /**
578  * Translate a real address into a physical (absolute) address.
579  * @param raddr  the real address
580  * @param rw     0 = read, 1 = write, 2 = code fetch
581  * @param addr   the translated address is stored to this pointer
582  * @param flags  the PAGE_READ/WRITE/EXEC flags are stored to this pointer
583  * @return       0 = success, != 0, the exception to raise
584  */
585 int mmu_translate_real(CPUS390XState *env, target_ulong raddr, int rw,
586                        target_ulong *addr, int *flags, uint64_t *tec)
587 {
588     const bool lowprot_enabled = env->cregs[0] & CR0_LOWPROT;
589 
590     *flags = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
591     if (is_low_address(raddr & TARGET_PAGE_MASK) && lowprot_enabled) {
592         /* see comment in mmu_translate() how this works */
593         *flags |= PAGE_WRITE_INV;
594         if (is_low_address(raddr) && rw == MMU_DATA_STORE) {
595             /* LAP sets bit 56 */
596             *tec = (raddr & TARGET_PAGE_MASK) | FS_WRITE | 0x80;
597             return PGM_PROTECTION;
598         }
599     }
600 
601     *addr = mmu_real2abs(env, raddr & TARGET_PAGE_MASK);
602 
603     if (!mmu_absolute_addr_valid(*addr, rw == MMU_DATA_STORE)) {
604         /* unused */
605         *tec = 0;
606         return PGM_ADDRESSING;
607     }
608 
609     mmu_handle_skey(*addr, rw, flags);
610     return 0;
611 }
612