2 * guest access functions
4 * Copyright IBM Corp. 2014
8 #include <linux/vmalloc.h>
10 #include <asm/pgtable.h>
17 unsigned long origin : 52; /* Region- or Segment-Table Origin */
19 unsigned long g : 1; /* Subspace Group Control */
20 unsigned long p : 1; /* Private Space Control */
21 unsigned long s : 1; /* Storage-Alteration-Event Control */
22 unsigned long x : 1; /* Space-Switch-Event Control */
23 unsigned long r : 1; /* Real-Space Control */
25 unsigned long dt : 2; /* Designation-Type Control */
26 unsigned long tl : 2; /* Region- or Segment-Table Length */
31 ASCE_TYPE_SEGMENT = 0,
32 ASCE_TYPE_REGION3 = 1,
33 ASCE_TYPE_REGION2 = 2,
37 union region1_table_entry {
40 unsigned long rto: 52;/* Region-Table Origin */
42 unsigned long p : 1; /* DAT-Protection Bit */
44 unsigned long tf : 2; /* Region-Second-Table Offset */
45 unsigned long i : 1; /* Region-Invalid Bit */
47 unsigned long tt : 2; /* Table-Type Bits */
48 unsigned long tl : 2; /* Region-Second-Table Length */
52 union region2_table_entry {
55 unsigned long rto: 52;/* Region-Table Origin */
57 unsigned long p : 1; /* DAT-Protection Bit */
59 unsigned long tf : 2; /* Region-Third-Table Offset */
60 unsigned long i : 1; /* Region-Invalid Bit */
62 unsigned long tt : 2; /* Table-Type Bits */
63 unsigned long tl : 2; /* Region-Third-Table Length */
67 struct region3_table_entry_fc0 {
68 unsigned long sto: 52;/* Segment-Table Origin */
70 unsigned long fc : 1; /* Format-Control */
71 unsigned long p : 1; /* DAT-Protection Bit */
73 unsigned long tf : 2; /* Segment-Table Offset */
74 unsigned long i : 1; /* Region-Invalid Bit */
75 unsigned long cr : 1; /* Common-Region Bit */
76 unsigned long tt : 2; /* Table-Type Bits */
77 unsigned long tl : 2; /* Segment-Table Length */
80 struct region3_table_entry_fc1 {
81 unsigned long rfaa : 33; /* Region-Frame Absolute Address */
83 unsigned long av : 1; /* ACCF-Validity Control */
84 unsigned long acc: 4; /* Access-Control Bits */
85 unsigned long f : 1; /* Fetch-Protection Bit */
86 unsigned long fc : 1; /* Format-Control */
87 unsigned long p : 1; /* DAT-Protection Bit */
88 unsigned long co : 1; /* Change-Recording Override */
90 unsigned long i : 1; /* Region-Invalid Bit */
91 unsigned long cr : 1; /* Common-Region Bit */
92 unsigned long tt : 2; /* Table-Type Bits */
96 union region3_table_entry {
98 struct region3_table_entry_fc0 fc0;
99 struct region3_table_entry_fc1 fc1;
102 unsigned long fc : 1; /* Format-Control */
104 unsigned long i : 1; /* Region-Invalid Bit */
105 unsigned long cr : 1; /* Common-Region Bit */
106 unsigned long tt : 2; /* Table-Type Bits */
111 struct segment_entry_fc0 {
112 unsigned long pto: 53;/* Page-Table Origin */
113 unsigned long fc : 1; /* Format-Control */
114 unsigned long p : 1; /* DAT-Protection Bit */
116 unsigned long i : 1; /* Segment-Invalid Bit */
117 unsigned long cs : 1; /* Common-Segment Bit */
118 unsigned long tt : 2; /* Table-Type Bits */
122 struct segment_entry_fc1 {
123 unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
125 unsigned long av : 1; /* ACCF-Validity Control */
126 unsigned long acc: 4; /* Access-Control Bits */
127 unsigned long f : 1; /* Fetch-Protection Bit */
128 unsigned long fc : 1; /* Format-Control */
129 unsigned long p : 1; /* DAT-Protection Bit */
130 unsigned long co : 1; /* Change-Recording Override */
132 unsigned long i : 1; /* Segment-Invalid Bit */
133 unsigned long cs : 1; /* Common-Segment Bit */
134 unsigned long tt : 2; /* Table-Type Bits */
138 union segment_table_entry {
140 struct segment_entry_fc0 fc0;
141 struct segment_entry_fc1 fc1;
144 unsigned long fc : 1; /* Format-Control */
146 unsigned long i : 1; /* Segment-Invalid Bit */
147 unsigned long cs : 1; /* Common-Segment Bit */
148 unsigned long tt : 2; /* Table-Type Bits */
154 TABLE_TYPE_SEGMENT = 0,
155 TABLE_TYPE_REGION3 = 1,
156 TABLE_TYPE_REGION2 = 2,
157 TABLE_TYPE_REGION1 = 3
160 union page_table_entry {
163 unsigned long pfra : 52; /* Page-Frame Real Address */
164 unsigned long z : 1; /* Zero Bit */
165 unsigned long i : 1; /* Page-Invalid Bit */
166 unsigned long p : 1; /* DAT-Protection Bit */
167 unsigned long co : 1; /* Change-Recording Override */
173 * vaddress union in order to easily decode a virtual address into its
174 * region first index, region second index etc. parts.
179 unsigned long rfx : 11;
180 unsigned long rsx : 11;
181 unsigned long rtx : 11;
182 unsigned long sx : 11;
183 unsigned long px : 8;
184 unsigned long bx : 12;
187 unsigned long rfx01 : 2;
189 unsigned long rsx01 : 2;
191 unsigned long rtx01 : 2;
193 unsigned long sx01 : 2;
199 * raddress union which will contain the result (real or absolute address)
200 * after a page table walk. The rfaa, sfaa and pfra members are used to
201 * simply assign them the value of a region, segment or page table entry.
205 unsigned long rfaa : 33; /* Region-Frame Absolute Address */
206 unsigned long sfaa : 44; /* Segment-Frame Absolute Address */
207 unsigned long pfra : 52; /* Page-Frame Real Address */
211 int ipte_lock_held(struct kvm_vcpu *vcpu)
213 union ipte_control *ic = &vcpu->kvm->arch.sca->ipte_control;
215 if (vcpu->arch.sie_block->eca & 1)
217 return vcpu->kvm->arch.ipte_lock_count != 0;
220 static void ipte_lock_simple(struct kvm_vcpu *vcpu)
222 union ipte_control old, new, *ic;
224 mutex_lock(&vcpu->kvm->arch.ipte_mutex);
225 vcpu->kvm->arch.ipte_lock_count++;
226 if (vcpu->kvm->arch.ipte_lock_count > 1)
228 ic = &vcpu->kvm->arch.sca->ipte_control;
239 } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
241 mutex_unlock(&vcpu->kvm->arch.ipte_mutex);
244 static void ipte_unlock_simple(struct kvm_vcpu *vcpu)
246 union ipte_control old, new, *ic;
248 mutex_lock(&vcpu->kvm->arch.ipte_mutex);
249 vcpu->kvm->arch.ipte_lock_count--;
250 if (vcpu->kvm->arch.ipte_lock_count)
252 ic = &vcpu->kvm->arch.sca->ipte_control;
258 } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
259 wake_up(&vcpu->kvm->arch.ipte_wq);
261 mutex_unlock(&vcpu->kvm->arch.ipte_mutex);
264 static void ipte_lock_siif(struct kvm_vcpu *vcpu)
266 union ipte_control old, new, *ic;
268 ic = &vcpu->kvm->arch.sca->ipte_control;
280 } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
283 static void ipte_unlock_siif(struct kvm_vcpu *vcpu)
285 union ipte_control old, new, *ic;
287 ic = &vcpu->kvm->arch.sca->ipte_control;
295 } while (cmpxchg(&ic->val, old.val, new.val) != old.val);
297 wake_up(&vcpu->kvm->arch.ipte_wq);
300 void ipte_lock(struct kvm_vcpu *vcpu)
302 if (vcpu->arch.sie_block->eca & 1)
303 ipte_lock_siif(vcpu);
305 ipte_lock_simple(vcpu);
308 void ipte_unlock(struct kvm_vcpu *vcpu)
310 if (vcpu->arch.sie_block->eca & 1)
311 ipte_unlock_siif(vcpu);
313 ipte_unlock_simple(vcpu);
316 static unsigned long get_vcpu_asce(struct kvm_vcpu *vcpu)
318 switch (psw_bits(vcpu->arch.sie_block->gpsw).as) {
320 return vcpu->arch.sie_block->gcr[1];
321 case PSW_AS_SECONDARY:
322 return vcpu->arch.sie_block->gcr[7];
324 return vcpu->arch.sie_block->gcr[13];
329 static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val)
331 return kvm_read_guest(kvm, gpa, val, sizeof(*val));
335 * guest_translate - translate a guest virtual into a guest absolute address
337 * @gva: guest virtual address
338 * @gpa: points to where guest physical (absolute) address should be stored
339 * @write: indicates if access is a write access
341 * Translate a guest virtual address into a guest absolute address by means
342 * of dynamic address translation as specified by the architecuture.
343 * If the resulting absolute address is not available in the configuration
344 * an addressing exception is indicated and @gpa will not be changed.
346 * Returns: - zero on success; @gpa contains the resulting absolute address
347 * - a negative value if guest access failed due to e.g. broken
349 * - a positve value if an access exception happened. In this case
350 * the returned value is the program interruption code as defined
351 * by the architecture
353 static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva,
354 unsigned long *gpa, int write)
356 union vaddress vaddr = {.addr = gva};
357 union raddress raddr = {.addr = gva};
358 union page_table_entry pte;
359 int dat_protection = 0;
360 union ctlreg0 ctlreg0;
365 ctlreg0.val = vcpu->arch.sie_block->gcr[0];
366 edat1 = ctlreg0.edat && test_vfacility(8);
367 edat2 = edat1 && test_vfacility(78);
368 asce.val = get_vcpu_asce(vcpu);
371 ptr = asce.origin * 4096;
373 case ASCE_TYPE_REGION1:
374 if (vaddr.rfx01 > asce.tl)
375 return PGM_REGION_FIRST_TRANS;
376 ptr += vaddr.rfx * 8;
378 case ASCE_TYPE_REGION2:
380 return PGM_ASCE_TYPE;
381 if (vaddr.rsx01 > asce.tl)
382 return PGM_REGION_SECOND_TRANS;
383 ptr += vaddr.rsx * 8;
385 case ASCE_TYPE_REGION3:
386 if (vaddr.rfx || vaddr.rsx)
387 return PGM_ASCE_TYPE;
388 if (vaddr.rtx01 > asce.tl)
389 return PGM_REGION_THIRD_TRANS;
390 ptr += vaddr.rtx * 8;
392 case ASCE_TYPE_SEGMENT:
393 if (vaddr.rfx || vaddr.rsx || vaddr.rtx)
394 return PGM_ASCE_TYPE;
395 if (vaddr.sx01 > asce.tl)
396 return PGM_SEGMENT_TRANSLATION;
401 case ASCE_TYPE_REGION1: {
402 union region1_table_entry rfte;
404 if (kvm_is_error_gpa(vcpu->kvm, ptr))
405 return PGM_ADDRESSING;
406 if (deref_table(vcpu->kvm, ptr, &rfte.val))
409 return PGM_REGION_FIRST_TRANS;
410 if (rfte.tt != TABLE_TYPE_REGION1)
411 return PGM_TRANSLATION_SPEC;
412 if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl)
413 return PGM_REGION_SECOND_TRANS;
415 dat_protection |= rfte.p;
416 ptr = rfte.rto * 4096 + vaddr.rsx * 8;
419 case ASCE_TYPE_REGION2: {
420 union region2_table_entry rste;
422 if (kvm_is_error_gpa(vcpu->kvm, ptr))
423 return PGM_ADDRESSING;
424 if (deref_table(vcpu->kvm, ptr, &rste.val))
427 return PGM_REGION_SECOND_TRANS;
428 if (rste.tt != TABLE_TYPE_REGION2)
429 return PGM_TRANSLATION_SPEC;
430 if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl)
431 return PGM_REGION_THIRD_TRANS;
433 dat_protection |= rste.p;
434 ptr = rste.rto * 4096 + vaddr.rtx * 8;
437 case ASCE_TYPE_REGION3: {
438 union region3_table_entry rtte;
440 if (kvm_is_error_gpa(vcpu->kvm, ptr))
441 return PGM_ADDRESSING;
442 if (deref_table(vcpu->kvm, ptr, &rtte.val))
445 return PGM_REGION_THIRD_TRANS;
446 if (rtte.tt != TABLE_TYPE_REGION3)
447 return PGM_TRANSLATION_SPEC;
448 if (rtte.cr && asce.p && edat2)
449 return PGM_TRANSLATION_SPEC;
450 if (rtte.fc && edat2) {
451 dat_protection |= rtte.fc1.p;
452 raddr.rfaa = rtte.fc1.rfaa;
453 goto absolute_address;
455 if (vaddr.sx01 < rtte.fc0.tf)
456 return PGM_SEGMENT_TRANSLATION;
457 if (vaddr.sx01 > rtte.fc0.tl)
458 return PGM_SEGMENT_TRANSLATION;
460 dat_protection |= rtte.fc0.p;
461 ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8;
464 case ASCE_TYPE_SEGMENT: {
465 union segment_table_entry ste;
467 if (kvm_is_error_gpa(vcpu->kvm, ptr))
468 return PGM_ADDRESSING;
469 if (deref_table(vcpu->kvm, ptr, &ste.val))
472 return PGM_SEGMENT_TRANSLATION;
473 if (ste.tt != TABLE_TYPE_SEGMENT)
474 return PGM_TRANSLATION_SPEC;
475 if (ste.cs && asce.p)
476 return PGM_TRANSLATION_SPEC;
477 if (ste.fc && edat1) {
478 dat_protection |= ste.fc1.p;
479 raddr.sfaa = ste.fc1.sfaa;
480 goto absolute_address;
482 dat_protection |= ste.fc0.p;
483 ptr = ste.fc0.pto * 2048 + vaddr.px * 8;
486 if (kvm_is_error_gpa(vcpu->kvm, ptr))
487 return PGM_ADDRESSING;
488 if (deref_table(vcpu->kvm, ptr, &pte.val))
491 return PGM_PAGE_TRANSLATION;
493 return PGM_TRANSLATION_SPEC;
494 if (pte.co && !edat1)
495 return PGM_TRANSLATION_SPEC;
496 dat_protection |= pte.p;
497 raddr.pfra = pte.pfra;
499 raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr);
501 if (write && dat_protection)
502 return PGM_PROTECTION;
503 if (kvm_is_error_gpa(vcpu->kvm, raddr.addr))
504 return PGM_ADDRESSING;
509 static inline int is_low_address(unsigned long ga)
511 /* Check for address ranges 0..511 and 4096..4607 */
512 return (ga & ~0x11fful) == 0;
515 static int low_address_protection_enabled(struct kvm_vcpu *vcpu)
517 union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]};
518 psw_t *psw = &vcpu->arch.sie_block->gpsw;
523 asce.val = get_vcpu_asce(vcpu);
524 if (psw_bits(*psw).t && asce.p)
529 struct trans_exc_code_bits {
530 unsigned long addr : 52; /* Translation-exception Address */
531 unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */
533 unsigned long b61 : 1;
534 unsigned long as : 2; /* ASCE Identifier */
538 FSI_UNKNOWN = 0, /* Unknown wether fetch or store */
539 FSI_STORE = 1, /* Exception was due to store operation */
540 FSI_FETCH = 2 /* Exception was due to fetch operation */
543 static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga,
544 unsigned long *pages, unsigned long nr_pages,
547 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
548 psw_t *psw = &vcpu->arch.sie_block->gpsw;
549 struct trans_exc_code_bits *tec_bits;
552 memset(pgm, 0, sizeof(*pgm));
553 tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
554 tec_bits->fsi = write ? FSI_STORE : FSI_FETCH;
555 tec_bits->as = psw_bits(*psw).as;
556 lap_enabled = low_address_protection_enabled(vcpu);
558 ga = kvm_s390_logical_to_effective(vcpu, ga);
559 tec_bits->addr = ga >> PAGE_SHIFT;
560 if (write && lap_enabled && is_low_address(ga)) {
561 pgm->code = PGM_PROTECTION;
565 if (psw_bits(*psw).t) {
566 rc = guest_translate(vcpu, ga, pages, write);
569 if (rc == PGM_PROTECTION)
574 *pages = kvm_s390_real_to_abs(vcpu, ga);
575 if (kvm_is_error_gpa(vcpu->kvm, *pages))
576 pgm->code = PGM_ADDRESSING;
587 int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, void *data,
588 unsigned long len, int write)
590 psw_t *psw = &vcpu->arch.sie_block->gpsw;
591 unsigned long _len, nr_pages, gpa, idx;
592 unsigned long pages_array[2];
593 unsigned long *pages;
600 /* Access register mode is not supported yet. */
601 if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
603 nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1;
605 if (nr_pages > ARRAY_SIZE(pages_array))
606 pages = vmalloc(nr_pages * sizeof(unsigned long));
609 asce.val = get_vcpu_asce(vcpu);
610 need_ipte_lock = psw_bits(*psw).t && !asce.r;
613 rc = guest_page_range(vcpu, ga, pages, nr_pages, write);
614 for (idx = 0; idx < nr_pages && !rc; idx++) {
615 gpa = *(pages + idx) + (ga & ~PAGE_MASK);
616 _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
618 rc = kvm_write_guest(vcpu->kvm, gpa, data, _len);
620 rc = kvm_read_guest(vcpu->kvm, gpa, data, _len);
627 if (nr_pages > ARRAY_SIZE(pages_array))
632 int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra,
633 void *data, unsigned long len, int write)
635 unsigned long _len, gpa;
639 gpa = kvm_s390_real_to_abs(vcpu, gra);
640 _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len);
642 rc = write_guest_abs(vcpu, gpa, data, _len);
644 rc = read_guest_abs(vcpu, gpa, data, _len);
653 * guest_translate_address - translate guest logical into guest absolute address
655 * Parameter semantics are the same as the ones from guest_translate.
656 * The memory contents at the guest address are not changed.
658 * Note: The IPTE lock is not taken during this function, so the caller
659 * has to take care of this.
661 int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva,
662 unsigned long *gpa, int write)
664 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
665 psw_t *psw = &vcpu->arch.sie_block->gpsw;
666 struct trans_exc_code_bits *tec;
670 /* Access register mode is not supported yet. */
671 if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG)
674 gva = kvm_s390_logical_to_effective(vcpu, gva);
675 memset(pgm, 0, sizeof(*pgm));
676 tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
677 tec->as = psw_bits(*psw).as;
678 tec->fsi = write ? FSI_STORE : FSI_FETCH;
679 tec->addr = gva >> PAGE_SHIFT;
680 if (is_low_address(gva) && low_address_protection_enabled(vcpu)) {
682 rc = pgm->code = PGM_PROTECTION;
687 asce.val = get_vcpu_asce(vcpu);
688 if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */
689 rc = guest_translate(vcpu, gva, gpa, write);
691 if (rc == PGM_PROTECTION)
697 *gpa = kvm_s390_real_to_abs(vcpu, gva);
698 if (kvm_is_error_gpa(vcpu->kvm, *gpa))
699 rc = pgm->code = PGM_ADDRESSING;
706 * kvm_s390_check_low_addr_protection - check for low-address protection
709 * Checks whether an address is subject to low-address protection and set
710 * up vcpu->arch.pgm accordingly if necessary.
712 * Return: 0 if no protection exception, or PGM_PROTECTION if protected.
714 int kvm_s390_check_low_addr_protection(struct kvm_vcpu *vcpu, unsigned long ga)
716 struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm;
717 psw_t *psw = &vcpu->arch.sie_block->gpsw;
718 struct trans_exc_code_bits *tec_bits;
720 if (!is_low_address(ga) || !low_address_protection_enabled(vcpu))
723 memset(pgm, 0, sizeof(*pgm));
724 tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code;
725 tec_bits->fsi = FSI_STORE;
726 tec_bits->as = psw_bits(*psw).as;
727 tec_bits->addr = ga >> PAGE_SHIFT;
728 pgm->code = PGM_PROTECTION;