122938978SHeiko Carstens /* 222938978SHeiko Carstens * guest access functions 322938978SHeiko Carstens * 422938978SHeiko Carstens * Copyright IBM Corp. 2014 522938978SHeiko Carstens * 622938978SHeiko Carstens */ 722938978SHeiko Carstens 822938978SHeiko Carstens #include <linux/vmalloc.h> 922938978SHeiko Carstens #include <linux/err.h> 1022938978SHeiko Carstens #include <asm/pgtable.h> 1122938978SHeiko Carstens #include "kvm-s390.h" 1222938978SHeiko Carstens #include "gaccess.h" 1322938978SHeiko Carstens 1422938978SHeiko Carstens union asce { 1522938978SHeiko Carstens unsigned long val; 1622938978SHeiko Carstens struct { 1722938978SHeiko Carstens unsigned long origin : 52; /* Region- or Segment-Table Origin */ 1822938978SHeiko Carstens unsigned long : 2; 1922938978SHeiko Carstens unsigned long g : 1; /* Subspace Group Control */ 2022938978SHeiko Carstens unsigned long p : 1; /* Private Space Control */ 2122938978SHeiko Carstens unsigned long s : 1; /* Storage-Alteration-Event Control */ 2222938978SHeiko Carstens unsigned long x : 1; /* Space-Switch-Event Control */ 2322938978SHeiko Carstens unsigned long r : 1; /* Real-Space Control */ 2422938978SHeiko Carstens unsigned long : 1; 2522938978SHeiko Carstens unsigned long dt : 2; /* Designation-Type Control */ 2622938978SHeiko Carstens unsigned long tl : 2; /* Region- or Segment-Table Length */ 2722938978SHeiko Carstens }; 2822938978SHeiko Carstens }; 2922938978SHeiko Carstens 3022938978SHeiko Carstens enum { 3122938978SHeiko Carstens ASCE_TYPE_SEGMENT = 0, 3222938978SHeiko Carstens ASCE_TYPE_REGION3 = 1, 3322938978SHeiko Carstens ASCE_TYPE_REGION2 = 2, 3422938978SHeiko Carstens ASCE_TYPE_REGION1 = 3 3522938978SHeiko Carstens }; 3622938978SHeiko Carstens 3722938978SHeiko Carstens union region1_table_entry { 3822938978SHeiko Carstens unsigned long val; 3922938978SHeiko Carstens struct { 4022938978SHeiko Carstens unsigned long rto: 52;/* Region-Table Origin */ 4122938978SHeiko Carstens unsigned long : 2; 4222938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 4322938978SHeiko Carstens unsigned long : 1; 4422938978SHeiko Carstens unsigned long tf : 2; /* Region-Second-Table Offset */ 4522938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 4622938978SHeiko Carstens unsigned long : 1; 4722938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 4822938978SHeiko Carstens unsigned long tl : 2; /* Region-Second-Table Length */ 4922938978SHeiko Carstens }; 5022938978SHeiko Carstens }; 5122938978SHeiko Carstens 5222938978SHeiko Carstens union region2_table_entry { 5322938978SHeiko Carstens unsigned long val; 5422938978SHeiko Carstens struct { 5522938978SHeiko Carstens unsigned long rto: 52;/* Region-Table Origin */ 5622938978SHeiko Carstens unsigned long : 2; 5722938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 5822938978SHeiko Carstens unsigned long : 1; 5922938978SHeiko Carstens unsigned long tf : 2; /* Region-Third-Table Offset */ 6022938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 6122938978SHeiko Carstens unsigned long : 1; 6222938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 6322938978SHeiko Carstens unsigned long tl : 2; /* Region-Third-Table Length */ 6422938978SHeiko Carstens }; 6522938978SHeiko Carstens }; 6622938978SHeiko Carstens 6722938978SHeiko Carstens struct region3_table_entry_fc0 { 6822938978SHeiko Carstens unsigned long sto: 52;/* Segment-Table Origin */ 6922938978SHeiko Carstens unsigned long : 1; 7022938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 7122938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 7222938978SHeiko Carstens unsigned long : 1; 7322938978SHeiko Carstens unsigned long tf : 2; /* Segment-Table Offset */ 7422938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 7522938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 7622938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 7722938978SHeiko Carstens unsigned long tl : 2; /* Segment-Table Length */ 7822938978SHeiko Carstens }; 7922938978SHeiko Carstens 8022938978SHeiko Carstens struct region3_table_entry_fc1 { 8122938978SHeiko Carstens unsigned long rfaa : 33; /* Region-Frame Absolute Address */ 8222938978SHeiko Carstens unsigned long : 14; 8322938978SHeiko Carstens unsigned long av : 1; /* ACCF-Validity Control */ 8422938978SHeiko Carstens unsigned long acc: 4; /* Access-Control Bits */ 8522938978SHeiko Carstens unsigned long f : 1; /* Fetch-Protection Bit */ 8622938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 8722938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 8822938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 8922938978SHeiko Carstens unsigned long : 2; 9022938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 9122938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 9222938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 9322938978SHeiko Carstens unsigned long : 2; 9422938978SHeiko Carstens }; 9522938978SHeiko Carstens 9622938978SHeiko Carstens union region3_table_entry { 9722938978SHeiko Carstens unsigned long val; 9822938978SHeiko Carstens struct region3_table_entry_fc0 fc0; 9922938978SHeiko Carstens struct region3_table_entry_fc1 fc1; 10022938978SHeiko Carstens struct { 10122938978SHeiko Carstens unsigned long : 53; 10222938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 10322938978SHeiko Carstens unsigned long : 4; 10422938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 10522938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 10622938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 10722938978SHeiko Carstens unsigned long : 2; 10822938978SHeiko Carstens }; 10922938978SHeiko Carstens }; 11022938978SHeiko Carstens 11122938978SHeiko Carstens struct segment_entry_fc0 { 11222938978SHeiko Carstens unsigned long pto: 53;/* Page-Table Origin */ 11322938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 11422938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 11522938978SHeiko Carstens unsigned long : 3; 11622938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 11722938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 11822938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 11922938978SHeiko Carstens unsigned long : 2; 12022938978SHeiko Carstens }; 12122938978SHeiko Carstens 12222938978SHeiko Carstens struct segment_entry_fc1 { 12322938978SHeiko Carstens unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ 12422938978SHeiko Carstens unsigned long : 3; 12522938978SHeiko Carstens unsigned long av : 1; /* ACCF-Validity Control */ 12622938978SHeiko Carstens unsigned long acc: 4; /* Access-Control Bits */ 12722938978SHeiko Carstens unsigned long f : 1; /* Fetch-Protection Bit */ 12822938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 12922938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 13022938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 13122938978SHeiko Carstens unsigned long : 2; 13222938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 13322938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 13422938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 13522938978SHeiko Carstens unsigned long : 2; 13622938978SHeiko Carstens }; 13722938978SHeiko Carstens 13822938978SHeiko Carstens union segment_table_entry { 13922938978SHeiko Carstens unsigned long val; 14022938978SHeiko Carstens struct segment_entry_fc0 fc0; 14122938978SHeiko Carstens struct segment_entry_fc1 fc1; 14222938978SHeiko Carstens struct { 14322938978SHeiko Carstens unsigned long : 53; 14422938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 14522938978SHeiko Carstens unsigned long : 4; 14622938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 14722938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 14822938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 14922938978SHeiko Carstens unsigned long : 2; 15022938978SHeiko Carstens }; 15122938978SHeiko Carstens }; 15222938978SHeiko Carstens 15322938978SHeiko Carstens enum { 15422938978SHeiko Carstens TABLE_TYPE_SEGMENT = 0, 15522938978SHeiko Carstens TABLE_TYPE_REGION3 = 1, 15622938978SHeiko Carstens TABLE_TYPE_REGION2 = 2, 15722938978SHeiko Carstens TABLE_TYPE_REGION1 = 3 15822938978SHeiko Carstens }; 15922938978SHeiko Carstens 16022938978SHeiko Carstens union page_table_entry { 16122938978SHeiko Carstens unsigned long val; 16222938978SHeiko Carstens struct { 16322938978SHeiko Carstens unsigned long pfra : 52; /* Page-Frame Real Address */ 16422938978SHeiko Carstens unsigned long z : 1; /* Zero Bit */ 16522938978SHeiko Carstens unsigned long i : 1; /* Page-Invalid Bit */ 16622938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 16722938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 16822938978SHeiko Carstens unsigned long : 8; 16922938978SHeiko Carstens }; 17022938978SHeiko Carstens }; 17122938978SHeiko Carstens 17222938978SHeiko Carstens /* 17322938978SHeiko Carstens * vaddress union in order to easily decode a virtual address into its 17422938978SHeiko Carstens * region first index, region second index etc. parts. 17522938978SHeiko Carstens */ 17622938978SHeiko Carstens union vaddress { 17722938978SHeiko Carstens unsigned long addr; 17822938978SHeiko Carstens struct { 17922938978SHeiko Carstens unsigned long rfx : 11; 18022938978SHeiko Carstens unsigned long rsx : 11; 18122938978SHeiko Carstens unsigned long rtx : 11; 18222938978SHeiko Carstens unsigned long sx : 11; 18322938978SHeiko Carstens unsigned long px : 8; 18422938978SHeiko Carstens unsigned long bx : 12; 18522938978SHeiko Carstens }; 18622938978SHeiko Carstens struct { 18722938978SHeiko Carstens unsigned long rfx01 : 2; 18822938978SHeiko Carstens unsigned long : 9; 18922938978SHeiko Carstens unsigned long rsx01 : 2; 19022938978SHeiko Carstens unsigned long : 9; 19122938978SHeiko Carstens unsigned long rtx01 : 2; 19222938978SHeiko Carstens unsigned long : 9; 19322938978SHeiko Carstens unsigned long sx01 : 2; 19422938978SHeiko Carstens unsigned long : 29; 19522938978SHeiko Carstens }; 19622938978SHeiko Carstens }; 19722938978SHeiko Carstens 19822938978SHeiko Carstens /* 19922938978SHeiko Carstens * raddress union which will contain the result (real or absolute address) 20022938978SHeiko Carstens * after a page table walk. The rfaa, sfaa and pfra members are used to 20122938978SHeiko Carstens * simply assign them the value of a region, segment or page table entry. 20222938978SHeiko Carstens */ 20322938978SHeiko Carstens union raddress { 20422938978SHeiko Carstens unsigned long addr; 20522938978SHeiko Carstens unsigned long rfaa : 33; /* Region-Frame Absolute Address */ 20622938978SHeiko Carstens unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ 20722938978SHeiko Carstens unsigned long pfra : 52; /* Page-Frame Real Address */ 20822938978SHeiko Carstens }; 20922938978SHeiko Carstens 2108a242234SHeiko Carstens 2118a242234SHeiko Carstens int ipte_lock_held(struct kvm_vcpu *vcpu) 2128a242234SHeiko Carstens { 2138a242234SHeiko Carstens union ipte_control *ic = &vcpu->kvm->arch.sca->ipte_control; 2148a242234SHeiko Carstens 2158a242234SHeiko Carstens if (vcpu->arch.sie_block->eca & 1) 2168a242234SHeiko Carstens return ic->kh != 0; 217a6b7e459SThomas Huth return vcpu->kvm->arch.ipte_lock_count != 0; 2188a242234SHeiko Carstens } 2198a242234SHeiko Carstens 2208a242234SHeiko Carstens static void ipte_lock_simple(struct kvm_vcpu *vcpu) 2218a242234SHeiko Carstens { 2228a242234SHeiko Carstens union ipte_control old, new, *ic; 2238a242234SHeiko Carstens 224a6b7e459SThomas Huth mutex_lock(&vcpu->kvm->arch.ipte_mutex); 225a6b7e459SThomas Huth vcpu->kvm->arch.ipte_lock_count++; 226a6b7e459SThomas Huth if (vcpu->kvm->arch.ipte_lock_count > 1) 2278a242234SHeiko Carstens goto out; 2288a242234SHeiko Carstens ic = &vcpu->kvm->arch.sca->ipte_control; 2298a242234SHeiko Carstens do { 2305de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2318a242234SHeiko Carstens while (old.k) { 2328a242234SHeiko Carstens cond_resched(); 2335de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2348a242234SHeiko Carstens } 2358a242234SHeiko Carstens new = old; 2368a242234SHeiko Carstens new.k = 1; 2378a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 2388a242234SHeiko Carstens out: 239a6b7e459SThomas Huth mutex_unlock(&vcpu->kvm->arch.ipte_mutex); 2408a242234SHeiko Carstens } 2418a242234SHeiko Carstens 2428a242234SHeiko Carstens static void ipte_unlock_simple(struct kvm_vcpu *vcpu) 2438a242234SHeiko Carstens { 2448a242234SHeiko Carstens union ipte_control old, new, *ic; 2458a242234SHeiko Carstens 246a6b7e459SThomas Huth mutex_lock(&vcpu->kvm->arch.ipte_mutex); 247a6b7e459SThomas Huth vcpu->kvm->arch.ipte_lock_count--; 248a6b7e459SThomas Huth if (vcpu->kvm->arch.ipte_lock_count) 2498a242234SHeiko Carstens goto out; 2508a242234SHeiko Carstens ic = &vcpu->kvm->arch.sca->ipte_control; 2518a242234SHeiko Carstens do { 2525de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2531365039dSChristian Borntraeger new = old; 2548a242234SHeiko Carstens new.k = 0; 2558a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 2568a242234SHeiko Carstens wake_up(&vcpu->kvm->arch.ipte_wq); 2578a242234SHeiko Carstens out: 258a6b7e459SThomas Huth mutex_unlock(&vcpu->kvm->arch.ipte_mutex); 2598a242234SHeiko Carstens } 2608a242234SHeiko Carstens 2618a242234SHeiko Carstens static void ipte_lock_siif(struct kvm_vcpu *vcpu) 2628a242234SHeiko Carstens { 2638a242234SHeiko Carstens union ipte_control old, new, *ic; 2648a242234SHeiko Carstens 2658a242234SHeiko Carstens ic = &vcpu->kvm->arch.sca->ipte_control; 2668a242234SHeiko Carstens do { 2675de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2688a242234SHeiko Carstens while (old.kg) { 2698a242234SHeiko Carstens cond_resched(); 2705de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2718a242234SHeiko Carstens } 2728a242234SHeiko Carstens new = old; 2738a242234SHeiko Carstens new.k = 1; 2748a242234SHeiko Carstens new.kh++; 2758a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 2768a242234SHeiko Carstens } 2778a242234SHeiko Carstens 2788a242234SHeiko Carstens static void ipte_unlock_siif(struct kvm_vcpu *vcpu) 2798a242234SHeiko Carstens { 2808a242234SHeiko Carstens union ipte_control old, new, *ic; 2818a242234SHeiko Carstens 2828a242234SHeiko Carstens ic = &vcpu->kvm->arch.sca->ipte_control; 2838a242234SHeiko Carstens do { 2845de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2851365039dSChristian Borntraeger new = old; 2868a242234SHeiko Carstens new.kh--; 2878a242234SHeiko Carstens if (!new.kh) 2888a242234SHeiko Carstens new.k = 0; 2898a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 2908a242234SHeiko Carstens if (!new.kh) 2918a242234SHeiko Carstens wake_up(&vcpu->kvm->arch.ipte_wq); 2928a242234SHeiko Carstens } 2938a242234SHeiko Carstens 294a0465f9aSThomas Huth void ipte_lock(struct kvm_vcpu *vcpu) 2958a242234SHeiko Carstens { 2968a242234SHeiko Carstens if (vcpu->arch.sie_block->eca & 1) 2978a242234SHeiko Carstens ipte_lock_siif(vcpu); 2988a242234SHeiko Carstens else 2998a242234SHeiko Carstens ipte_lock_simple(vcpu); 3008a242234SHeiko Carstens } 3018a242234SHeiko Carstens 302a0465f9aSThomas Huth void ipte_unlock(struct kvm_vcpu *vcpu) 3038a242234SHeiko Carstens { 3048a242234SHeiko Carstens if (vcpu->arch.sie_block->eca & 1) 3058a242234SHeiko Carstens ipte_unlock_siif(vcpu); 3068a242234SHeiko Carstens else 3078a242234SHeiko Carstens ipte_unlock_simple(vcpu); 3088a242234SHeiko Carstens } 3098a242234SHeiko Carstens 31022938978SHeiko Carstens static unsigned long get_vcpu_asce(struct kvm_vcpu *vcpu) 31122938978SHeiko Carstens { 31222938978SHeiko Carstens switch (psw_bits(vcpu->arch.sie_block->gpsw).as) { 31322938978SHeiko Carstens case PSW_AS_PRIMARY: 31422938978SHeiko Carstens return vcpu->arch.sie_block->gcr[1]; 31522938978SHeiko Carstens case PSW_AS_SECONDARY: 31622938978SHeiko Carstens return vcpu->arch.sie_block->gcr[7]; 31722938978SHeiko Carstens case PSW_AS_HOME: 31822938978SHeiko Carstens return vcpu->arch.sie_block->gcr[13]; 31922938978SHeiko Carstens } 32022938978SHeiko Carstens return 0; 32122938978SHeiko Carstens } 32222938978SHeiko Carstens 32322938978SHeiko Carstens static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val) 32422938978SHeiko Carstens { 32522938978SHeiko Carstens return kvm_read_guest(kvm, gpa, val, sizeof(*val)); 32622938978SHeiko Carstens } 32722938978SHeiko Carstens 32822938978SHeiko Carstens /** 32922938978SHeiko Carstens * guest_translate - translate a guest virtual into a guest absolute address 33022938978SHeiko Carstens * @vcpu: virtual cpu 33122938978SHeiko Carstens * @gva: guest virtual address 33222938978SHeiko Carstens * @gpa: points to where guest physical (absolute) address should be stored 33322938978SHeiko Carstens * @write: indicates if access is a write access 33422938978SHeiko Carstens * 33522938978SHeiko Carstens * Translate a guest virtual address into a guest absolute address by means 33616b0fc13SYannick Guerrini * of dynamic address translation as specified by the architecture. 33722938978SHeiko Carstens * If the resulting absolute address is not available in the configuration 33822938978SHeiko Carstens * an addressing exception is indicated and @gpa will not be changed. 33922938978SHeiko Carstens * 34022938978SHeiko Carstens * Returns: - zero on success; @gpa contains the resulting absolute address 34122938978SHeiko Carstens * - a negative value if guest access failed due to e.g. broken 34222938978SHeiko Carstens * guest mapping 34322938978SHeiko Carstens * - a positve value if an access exception happened. In this case 34422938978SHeiko Carstens * the returned value is the program interruption code as defined 34522938978SHeiko Carstens * by the architecture 34622938978SHeiko Carstens */ 34722938978SHeiko Carstens static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva, 34822938978SHeiko Carstens unsigned long *gpa, int write) 34922938978SHeiko Carstens { 35022938978SHeiko Carstens union vaddress vaddr = {.addr = gva}; 35122938978SHeiko Carstens union raddress raddr = {.addr = gva}; 35222938978SHeiko Carstens union page_table_entry pte; 35322938978SHeiko Carstens int dat_protection = 0; 35422938978SHeiko Carstens union ctlreg0 ctlreg0; 35522938978SHeiko Carstens unsigned long ptr; 35622938978SHeiko Carstens int edat1, edat2; 35722938978SHeiko Carstens union asce asce; 35822938978SHeiko Carstens 35922938978SHeiko Carstens ctlreg0.val = vcpu->arch.sie_block->gcr[0]; 3609d8d5786SMichael Mueller edat1 = ctlreg0.edat && test_kvm_facility(vcpu->kvm, 8); 3619d8d5786SMichael Mueller edat2 = edat1 && test_kvm_facility(vcpu->kvm, 78); 36222938978SHeiko Carstens asce.val = get_vcpu_asce(vcpu); 36322938978SHeiko Carstens if (asce.r) 36422938978SHeiko Carstens goto real_address; 36522938978SHeiko Carstens ptr = asce.origin * 4096; 36622938978SHeiko Carstens switch (asce.dt) { 36722938978SHeiko Carstens case ASCE_TYPE_REGION1: 36822938978SHeiko Carstens if (vaddr.rfx01 > asce.tl) 36922938978SHeiko Carstens return PGM_REGION_FIRST_TRANS; 37022938978SHeiko Carstens ptr += vaddr.rfx * 8; 37122938978SHeiko Carstens break; 37222938978SHeiko Carstens case ASCE_TYPE_REGION2: 37322938978SHeiko Carstens if (vaddr.rfx) 37422938978SHeiko Carstens return PGM_ASCE_TYPE; 37522938978SHeiko Carstens if (vaddr.rsx01 > asce.tl) 37622938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 37722938978SHeiko Carstens ptr += vaddr.rsx * 8; 37822938978SHeiko Carstens break; 37922938978SHeiko Carstens case ASCE_TYPE_REGION3: 38022938978SHeiko Carstens if (vaddr.rfx || vaddr.rsx) 38122938978SHeiko Carstens return PGM_ASCE_TYPE; 38222938978SHeiko Carstens if (vaddr.rtx01 > asce.tl) 38322938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 38422938978SHeiko Carstens ptr += vaddr.rtx * 8; 38522938978SHeiko Carstens break; 38622938978SHeiko Carstens case ASCE_TYPE_SEGMENT: 38722938978SHeiko Carstens if (vaddr.rfx || vaddr.rsx || vaddr.rtx) 38822938978SHeiko Carstens return PGM_ASCE_TYPE; 38922938978SHeiko Carstens if (vaddr.sx01 > asce.tl) 39022938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 39122938978SHeiko Carstens ptr += vaddr.sx * 8; 39222938978SHeiko Carstens break; 39322938978SHeiko Carstens } 39422938978SHeiko Carstens switch (asce.dt) { 39522938978SHeiko Carstens case ASCE_TYPE_REGION1: { 39622938978SHeiko Carstens union region1_table_entry rfte; 39722938978SHeiko Carstens 39822938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 39922938978SHeiko Carstens return PGM_ADDRESSING; 40022938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rfte.val)) 40122938978SHeiko Carstens return -EFAULT; 40222938978SHeiko Carstens if (rfte.i) 40322938978SHeiko Carstens return PGM_REGION_FIRST_TRANS; 40422938978SHeiko Carstens if (rfte.tt != TABLE_TYPE_REGION1) 40522938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 40622938978SHeiko Carstens if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl) 40722938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 40822938978SHeiko Carstens if (edat1) 40922938978SHeiko Carstens dat_protection |= rfte.p; 41022938978SHeiko Carstens ptr = rfte.rto * 4096 + vaddr.rsx * 8; 41122938978SHeiko Carstens } 41222938978SHeiko Carstens /* fallthrough */ 41322938978SHeiko Carstens case ASCE_TYPE_REGION2: { 41422938978SHeiko Carstens union region2_table_entry rste; 41522938978SHeiko Carstens 41622938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 41722938978SHeiko Carstens return PGM_ADDRESSING; 41822938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rste.val)) 41922938978SHeiko Carstens return -EFAULT; 42022938978SHeiko Carstens if (rste.i) 42122938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 42222938978SHeiko Carstens if (rste.tt != TABLE_TYPE_REGION2) 42322938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 42422938978SHeiko Carstens if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl) 42522938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 42622938978SHeiko Carstens if (edat1) 42722938978SHeiko Carstens dat_protection |= rste.p; 42822938978SHeiko Carstens ptr = rste.rto * 4096 + vaddr.rtx * 8; 42922938978SHeiko Carstens } 43022938978SHeiko Carstens /* fallthrough */ 43122938978SHeiko Carstens case ASCE_TYPE_REGION3: { 43222938978SHeiko Carstens union region3_table_entry rtte; 43322938978SHeiko Carstens 43422938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 43522938978SHeiko Carstens return PGM_ADDRESSING; 43622938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rtte.val)) 43722938978SHeiko Carstens return -EFAULT; 43822938978SHeiko Carstens if (rtte.i) 43922938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 44022938978SHeiko Carstens if (rtte.tt != TABLE_TYPE_REGION3) 44122938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 44222938978SHeiko Carstens if (rtte.cr && asce.p && edat2) 44322938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 44422938978SHeiko Carstens if (rtte.fc && edat2) { 44522938978SHeiko Carstens dat_protection |= rtte.fc1.p; 44622938978SHeiko Carstens raddr.rfaa = rtte.fc1.rfaa; 44722938978SHeiko Carstens goto absolute_address; 44822938978SHeiko Carstens } 44922938978SHeiko Carstens if (vaddr.sx01 < rtte.fc0.tf) 45022938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 45122938978SHeiko Carstens if (vaddr.sx01 > rtte.fc0.tl) 45222938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 45322938978SHeiko Carstens if (edat1) 45422938978SHeiko Carstens dat_protection |= rtte.fc0.p; 45522938978SHeiko Carstens ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8; 45622938978SHeiko Carstens } 45722938978SHeiko Carstens /* fallthrough */ 45822938978SHeiko Carstens case ASCE_TYPE_SEGMENT: { 45922938978SHeiko Carstens union segment_table_entry ste; 46022938978SHeiko Carstens 46122938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 46222938978SHeiko Carstens return PGM_ADDRESSING; 46322938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &ste.val)) 46422938978SHeiko Carstens return -EFAULT; 46522938978SHeiko Carstens if (ste.i) 46622938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 46722938978SHeiko Carstens if (ste.tt != TABLE_TYPE_SEGMENT) 46822938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 46922938978SHeiko Carstens if (ste.cs && asce.p) 47022938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 47122938978SHeiko Carstens if (ste.fc && edat1) { 47222938978SHeiko Carstens dat_protection |= ste.fc1.p; 47322938978SHeiko Carstens raddr.sfaa = ste.fc1.sfaa; 47422938978SHeiko Carstens goto absolute_address; 47522938978SHeiko Carstens } 47622938978SHeiko Carstens dat_protection |= ste.fc0.p; 47722938978SHeiko Carstens ptr = ste.fc0.pto * 2048 + vaddr.px * 8; 47822938978SHeiko Carstens } 47922938978SHeiko Carstens } 48022938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 48122938978SHeiko Carstens return PGM_ADDRESSING; 48222938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &pte.val)) 48322938978SHeiko Carstens return -EFAULT; 48422938978SHeiko Carstens if (pte.i) 48522938978SHeiko Carstens return PGM_PAGE_TRANSLATION; 48622938978SHeiko Carstens if (pte.z) 48722938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 48822938978SHeiko Carstens if (pte.co && !edat1) 48922938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 49022938978SHeiko Carstens dat_protection |= pte.p; 49122938978SHeiko Carstens raddr.pfra = pte.pfra; 49222938978SHeiko Carstens real_address: 49322938978SHeiko Carstens raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr); 49422938978SHeiko Carstens absolute_address: 49522938978SHeiko Carstens if (write && dat_protection) 49622938978SHeiko Carstens return PGM_PROTECTION; 49722938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, raddr.addr)) 49822938978SHeiko Carstens return PGM_ADDRESSING; 49922938978SHeiko Carstens *gpa = raddr.addr; 50022938978SHeiko Carstens return 0; 50122938978SHeiko Carstens } 50222938978SHeiko Carstens 50322938978SHeiko Carstens static inline int is_low_address(unsigned long ga) 50422938978SHeiko Carstens { 50522938978SHeiko Carstens /* Check for address ranges 0..511 and 4096..4607 */ 50622938978SHeiko Carstens return (ga & ~0x11fful) == 0; 50722938978SHeiko Carstens } 50822938978SHeiko Carstens 50922938978SHeiko Carstens static int low_address_protection_enabled(struct kvm_vcpu *vcpu) 51022938978SHeiko Carstens { 51122938978SHeiko Carstens union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; 51222938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 51322938978SHeiko Carstens union asce asce; 51422938978SHeiko Carstens 51522938978SHeiko Carstens if (!ctlreg0.lap) 51622938978SHeiko Carstens return 0; 51722938978SHeiko Carstens asce.val = get_vcpu_asce(vcpu); 51822938978SHeiko Carstens if (psw_bits(*psw).t && asce.p) 51922938978SHeiko Carstens return 0; 52022938978SHeiko Carstens return 1; 52122938978SHeiko Carstens } 52222938978SHeiko Carstens 52322938978SHeiko Carstens struct trans_exc_code_bits { 52422938978SHeiko Carstens unsigned long addr : 52; /* Translation-exception Address */ 52522938978SHeiko Carstens unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */ 52622938978SHeiko Carstens unsigned long : 7; 52722938978SHeiko Carstens unsigned long b61 : 1; 52822938978SHeiko Carstens unsigned long as : 2; /* ASCE Identifier */ 52922938978SHeiko Carstens }; 53022938978SHeiko Carstens 53122938978SHeiko Carstens enum { 53222938978SHeiko Carstens FSI_UNKNOWN = 0, /* Unknown wether fetch or store */ 53322938978SHeiko Carstens FSI_STORE = 1, /* Exception was due to store operation */ 53422938978SHeiko Carstens FSI_FETCH = 2 /* Exception was due to fetch operation */ 53522938978SHeiko Carstens }; 53622938978SHeiko Carstens 53722938978SHeiko Carstens static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, 53822938978SHeiko Carstens unsigned long *pages, unsigned long nr_pages, 53922938978SHeiko Carstens int write) 54022938978SHeiko Carstens { 54122938978SHeiko Carstens struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 54222938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 54322938978SHeiko Carstens struct trans_exc_code_bits *tec_bits; 54422938978SHeiko Carstens int lap_enabled, rc; 54522938978SHeiko Carstens 54622938978SHeiko Carstens memset(pgm, 0, sizeof(*pgm)); 54722938978SHeiko Carstens tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 54822938978SHeiko Carstens tec_bits->fsi = write ? FSI_STORE : FSI_FETCH; 54922938978SHeiko Carstens tec_bits->as = psw_bits(*psw).as; 55022938978SHeiko Carstens lap_enabled = low_address_protection_enabled(vcpu); 55122938978SHeiko Carstens while (nr_pages) { 55222938978SHeiko Carstens ga = kvm_s390_logical_to_effective(vcpu, ga); 55322938978SHeiko Carstens tec_bits->addr = ga >> PAGE_SHIFT; 55422938978SHeiko Carstens if (write && lap_enabled && is_low_address(ga)) { 55522938978SHeiko Carstens pgm->code = PGM_PROTECTION; 55622938978SHeiko Carstens return pgm->code; 55722938978SHeiko Carstens } 55822938978SHeiko Carstens ga &= PAGE_MASK; 55922938978SHeiko Carstens if (psw_bits(*psw).t) { 56022938978SHeiko Carstens rc = guest_translate(vcpu, ga, pages, write); 56122938978SHeiko Carstens if (rc < 0) 56222938978SHeiko Carstens return rc; 56322938978SHeiko Carstens if (rc == PGM_PROTECTION) 56422938978SHeiko Carstens tec_bits->b61 = 1; 56522938978SHeiko Carstens if (rc) 56622938978SHeiko Carstens pgm->code = rc; 56722938978SHeiko Carstens } else { 56822938978SHeiko Carstens *pages = kvm_s390_real_to_abs(vcpu, ga); 56922938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, *pages)) 57022938978SHeiko Carstens pgm->code = PGM_ADDRESSING; 57122938978SHeiko Carstens } 57222938978SHeiko Carstens if (pgm->code) 57322938978SHeiko Carstens return pgm->code; 57422938978SHeiko Carstens ga += PAGE_SIZE; 57522938978SHeiko Carstens pages++; 57622938978SHeiko Carstens nr_pages--; 57722938978SHeiko Carstens } 57822938978SHeiko Carstens return 0; 57922938978SHeiko Carstens } 58022938978SHeiko Carstens 581*8ae04b8fSAlexander Yarygin int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar, void *data, 58222938978SHeiko Carstens unsigned long len, int write) 58322938978SHeiko Carstens { 58422938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 58522938978SHeiko Carstens unsigned long _len, nr_pages, gpa, idx; 58622938978SHeiko Carstens unsigned long pages_array[2]; 58722938978SHeiko Carstens unsigned long *pages; 5888a242234SHeiko Carstens int need_ipte_lock; 5898a242234SHeiko Carstens union asce asce; 59022938978SHeiko Carstens int rc; 59122938978SHeiko Carstens 59222938978SHeiko Carstens if (!len) 59322938978SHeiko Carstens return 0; 59422938978SHeiko Carstens /* Access register mode is not supported yet. */ 59522938978SHeiko Carstens if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG) 59622938978SHeiko Carstens return -EOPNOTSUPP; 59722938978SHeiko Carstens nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1; 59822938978SHeiko Carstens pages = pages_array; 59922938978SHeiko Carstens if (nr_pages > ARRAY_SIZE(pages_array)) 60022938978SHeiko Carstens pages = vmalloc(nr_pages * sizeof(unsigned long)); 60122938978SHeiko Carstens if (!pages) 60222938978SHeiko Carstens return -ENOMEM; 6038a242234SHeiko Carstens asce.val = get_vcpu_asce(vcpu); 6048a242234SHeiko Carstens need_ipte_lock = psw_bits(*psw).t && !asce.r; 6058a242234SHeiko Carstens if (need_ipte_lock) 6068a242234SHeiko Carstens ipte_lock(vcpu); 60722938978SHeiko Carstens rc = guest_page_range(vcpu, ga, pages, nr_pages, write); 60822938978SHeiko Carstens for (idx = 0; idx < nr_pages && !rc; idx++) { 60922938978SHeiko Carstens gpa = *(pages + idx) + (ga & ~PAGE_MASK); 61022938978SHeiko Carstens _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); 61122938978SHeiko Carstens if (write) 61222938978SHeiko Carstens rc = kvm_write_guest(vcpu->kvm, gpa, data, _len); 61322938978SHeiko Carstens else 61422938978SHeiko Carstens rc = kvm_read_guest(vcpu->kvm, gpa, data, _len); 61522938978SHeiko Carstens len -= _len; 61622938978SHeiko Carstens ga += _len; 61722938978SHeiko Carstens data += _len; 61822938978SHeiko Carstens } 6198a242234SHeiko Carstens if (need_ipte_lock) 6208a242234SHeiko Carstens ipte_unlock(vcpu); 62122938978SHeiko Carstens if (nr_pages > ARRAY_SIZE(pages_array)) 62222938978SHeiko Carstens vfree(pages); 62322938978SHeiko Carstens return rc; 62422938978SHeiko Carstens } 62522938978SHeiko Carstens 62622938978SHeiko Carstens int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, 62722938978SHeiko Carstens void *data, unsigned long len, int write) 62822938978SHeiko Carstens { 62922938978SHeiko Carstens unsigned long _len, gpa; 63022938978SHeiko Carstens int rc = 0; 63122938978SHeiko Carstens 63222938978SHeiko Carstens while (len && !rc) { 63322938978SHeiko Carstens gpa = kvm_s390_real_to_abs(vcpu, gra); 63422938978SHeiko Carstens _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); 63522938978SHeiko Carstens if (write) 63622938978SHeiko Carstens rc = write_guest_abs(vcpu, gpa, data, _len); 63722938978SHeiko Carstens else 63822938978SHeiko Carstens rc = read_guest_abs(vcpu, gpa, data, _len); 63922938978SHeiko Carstens len -= _len; 64022938978SHeiko Carstens gra += _len; 64122938978SHeiko Carstens data += _len; 64222938978SHeiko Carstens } 64322938978SHeiko Carstens return rc; 64422938978SHeiko Carstens } 645f8232c8cSThomas Huth 646f8232c8cSThomas Huth /** 6479fbc0276SThomas Huth * guest_translate_address - translate guest logical into guest absolute address 6489fbc0276SThomas Huth * 6499fbc0276SThomas Huth * Parameter semantics are the same as the ones from guest_translate. 6509fbc0276SThomas Huth * The memory contents at the guest address are not changed. 6519fbc0276SThomas Huth * 6529fbc0276SThomas Huth * Note: The IPTE lock is not taken during this function, so the caller 6539fbc0276SThomas Huth * has to take care of this. 6549fbc0276SThomas Huth */ 655*8ae04b8fSAlexander Yarygin int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar, 6569fbc0276SThomas Huth unsigned long *gpa, int write) 6579fbc0276SThomas Huth { 6589fbc0276SThomas Huth struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 6599fbc0276SThomas Huth psw_t *psw = &vcpu->arch.sie_block->gpsw; 6609fbc0276SThomas Huth struct trans_exc_code_bits *tec; 6619fbc0276SThomas Huth union asce asce; 6629fbc0276SThomas Huth int rc; 6639fbc0276SThomas Huth 6649fbc0276SThomas Huth /* Access register mode is not supported yet. */ 6659fbc0276SThomas Huth if (psw_bits(*psw).t && psw_bits(*psw).as == PSW_AS_ACCREG) 6669fbc0276SThomas Huth return -EOPNOTSUPP; 6679fbc0276SThomas Huth 6689fbc0276SThomas Huth gva = kvm_s390_logical_to_effective(vcpu, gva); 6699fbc0276SThomas Huth memset(pgm, 0, sizeof(*pgm)); 6709fbc0276SThomas Huth tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 6719fbc0276SThomas Huth tec->as = psw_bits(*psw).as; 6729fbc0276SThomas Huth tec->fsi = write ? FSI_STORE : FSI_FETCH; 6739fbc0276SThomas Huth tec->addr = gva >> PAGE_SHIFT; 6749fbc0276SThomas Huth if (is_low_address(gva) && low_address_protection_enabled(vcpu)) { 6759fbc0276SThomas Huth if (write) { 6769fbc0276SThomas Huth rc = pgm->code = PGM_PROTECTION; 6779fbc0276SThomas Huth return rc; 6789fbc0276SThomas Huth } 6799fbc0276SThomas Huth } 6809fbc0276SThomas Huth 6819fbc0276SThomas Huth asce.val = get_vcpu_asce(vcpu); 6829fbc0276SThomas Huth if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */ 6839fbc0276SThomas Huth rc = guest_translate(vcpu, gva, gpa, write); 6849fbc0276SThomas Huth if (rc > 0) { 6859fbc0276SThomas Huth if (rc == PGM_PROTECTION) 6869fbc0276SThomas Huth tec->b61 = 1; 6879fbc0276SThomas Huth pgm->code = rc; 6889fbc0276SThomas Huth } 6899fbc0276SThomas Huth } else { 6909fbc0276SThomas Huth rc = 0; 6919fbc0276SThomas Huth *gpa = kvm_s390_real_to_abs(vcpu, gva); 6929fbc0276SThomas Huth if (kvm_is_error_gpa(vcpu->kvm, *gpa)) 6939fbc0276SThomas Huth rc = pgm->code = PGM_ADDRESSING; 6949fbc0276SThomas Huth } 6959fbc0276SThomas Huth 6969fbc0276SThomas Huth return rc; 6979fbc0276SThomas Huth } 6989fbc0276SThomas Huth 6999fbc0276SThomas Huth /** 700dd9e5b7bSAlexander Yarygin * kvm_s390_check_low_addr_prot_real - check for low-address protection 701dd9e5b7bSAlexander Yarygin * @gra: Guest real address 702f8232c8cSThomas Huth * 703f8232c8cSThomas Huth * Checks whether an address is subject to low-address protection and set 704f8232c8cSThomas Huth * up vcpu->arch.pgm accordingly if necessary. 705f8232c8cSThomas Huth * 706f8232c8cSThomas Huth * Return: 0 if no protection exception, or PGM_PROTECTION if protected. 707f8232c8cSThomas Huth */ 708dd9e5b7bSAlexander Yarygin int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra) 709f8232c8cSThomas Huth { 710f8232c8cSThomas Huth struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 711f8232c8cSThomas Huth psw_t *psw = &vcpu->arch.sie_block->gpsw; 712f8232c8cSThomas Huth struct trans_exc_code_bits *tec_bits; 713dd9e5b7bSAlexander Yarygin union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; 714f8232c8cSThomas Huth 715dd9e5b7bSAlexander Yarygin if (!ctlreg0.lap || !is_low_address(gra)) 716f8232c8cSThomas Huth return 0; 717f8232c8cSThomas Huth 718f8232c8cSThomas Huth memset(pgm, 0, sizeof(*pgm)); 719f8232c8cSThomas Huth tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 720f8232c8cSThomas Huth tec_bits->fsi = FSI_STORE; 721f8232c8cSThomas Huth tec_bits->as = psw_bits(*psw).as; 722dd9e5b7bSAlexander Yarygin tec_bits->addr = gra >> PAGE_SHIFT; 723f8232c8cSThomas Huth pgm->code = PGM_PROTECTION; 724f8232c8cSThomas Huth 725f8232c8cSThomas Huth return pgm->code; 726f8232c8cSThomas Huth } 727