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" 13664b4973SAlexander Yarygin #include <asm/switch_to.h> 1422938978SHeiko Carstens 1522938978SHeiko Carstens union asce { 1622938978SHeiko Carstens unsigned long val; 1722938978SHeiko Carstens struct { 1822938978SHeiko Carstens unsigned long origin : 52; /* Region- or Segment-Table Origin */ 1922938978SHeiko Carstens unsigned long : 2; 2022938978SHeiko Carstens unsigned long g : 1; /* Subspace Group Control */ 2122938978SHeiko Carstens unsigned long p : 1; /* Private Space Control */ 2222938978SHeiko Carstens unsigned long s : 1; /* Storage-Alteration-Event Control */ 2322938978SHeiko Carstens unsigned long x : 1; /* Space-Switch-Event Control */ 2422938978SHeiko Carstens unsigned long r : 1; /* Real-Space Control */ 2522938978SHeiko Carstens unsigned long : 1; 2622938978SHeiko Carstens unsigned long dt : 2; /* Designation-Type Control */ 2722938978SHeiko Carstens unsigned long tl : 2; /* Region- or Segment-Table Length */ 2822938978SHeiko Carstens }; 2922938978SHeiko Carstens }; 3022938978SHeiko Carstens 3122938978SHeiko Carstens enum { 3222938978SHeiko Carstens ASCE_TYPE_SEGMENT = 0, 3322938978SHeiko Carstens ASCE_TYPE_REGION3 = 1, 3422938978SHeiko Carstens ASCE_TYPE_REGION2 = 2, 3522938978SHeiko Carstens ASCE_TYPE_REGION1 = 3 3622938978SHeiko Carstens }; 3722938978SHeiko Carstens 3822938978SHeiko Carstens union region1_table_entry { 3922938978SHeiko Carstens unsigned long val; 4022938978SHeiko Carstens struct { 4122938978SHeiko Carstens unsigned long rto: 52;/* Region-Table Origin */ 4222938978SHeiko Carstens unsigned long : 2; 4322938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 4422938978SHeiko Carstens unsigned long : 1; 4522938978SHeiko Carstens unsigned long tf : 2; /* Region-Second-Table Offset */ 4622938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 4722938978SHeiko Carstens unsigned long : 1; 4822938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 4922938978SHeiko Carstens unsigned long tl : 2; /* Region-Second-Table Length */ 5022938978SHeiko Carstens }; 5122938978SHeiko Carstens }; 5222938978SHeiko Carstens 5322938978SHeiko Carstens union region2_table_entry { 5422938978SHeiko Carstens unsigned long val; 5522938978SHeiko Carstens struct { 5622938978SHeiko Carstens unsigned long rto: 52;/* Region-Table Origin */ 5722938978SHeiko Carstens unsigned long : 2; 5822938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 5922938978SHeiko Carstens unsigned long : 1; 6022938978SHeiko Carstens unsigned long tf : 2; /* Region-Third-Table Offset */ 6122938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 6222938978SHeiko Carstens unsigned long : 1; 6322938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 6422938978SHeiko Carstens unsigned long tl : 2; /* Region-Third-Table Length */ 6522938978SHeiko Carstens }; 6622938978SHeiko Carstens }; 6722938978SHeiko Carstens 6822938978SHeiko Carstens struct region3_table_entry_fc0 { 6922938978SHeiko Carstens unsigned long sto: 52;/* Segment-Table Origin */ 7022938978SHeiko Carstens unsigned long : 1; 7122938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 7222938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 7322938978SHeiko Carstens unsigned long : 1; 7422938978SHeiko Carstens unsigned long tf : 2; /* Segment-Table Offset */ 7522938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 7622938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 7722938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 7822938978SHeiko Carstens unsigned long tl : 2; /* Segment-Table Length */ 7922938978SHeiko Carstens }; 8022938978SHeiko Carstens 8122938978SHeiko Carstens struct region3_table_entry_fc1 { 8222938978SHeiko Carstens unsigned long rfaa : 33; /* Region-Frame Absolute Address */ 8322938978SHeiko Carstens unsigned long : 14; 8422938978SHeiko Carstens unsigned long av : 1; /* ACCF-Validity Control */ 8522938978SHeiko Carstens unsigned long acc: 4; /* Access-Control Bits */ 8622938978SHeiko Carstens unsigned long f : 1; /* Fetch-Protection Bit */ 8722938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 8822938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 8922938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 9022938978SHeiko Carstens unsigned long : 2; 9122938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 9222938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 9322938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 9422938978SHeiko Carstens unsigned long : 2; 9522938978SHeiko Carstens }; 9622938978SHeiko Carstens 9722938978SHeiko Carstens union region3_table_entry { 9822938978SHeiko Carstens unsigned long val; 9922938978SHeiko Carstens struct region3_table_entry_fc0 fc0; 10022938978SHeiko Carstens struct region3_table_entry_fc1 fc1; 10122938978SHeiko Carstens struct { 10222938978SHeiko Carstens unsigned long : 53; 10322938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 10422938978SHeiko Carstens unsigned long : 4; 10522938978SHeiko Carstens unsigned long i : 1; /* Region-Invalid Bit */ 10622938978SHeiko Carstens unsigned long cr : 1; /* Common-Region Bit */ 10722938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 10822938978SHeiko Carstens unsigned long : 2; 10922938978SHeiko Carstens }; 11022938978SHeiko Carstens }; 11122938978SHeiko Carstens 11222938978SHeiko Carstens struct segment_entry_fc0 { 11322938978SHeiko Carstens unsigned long pto: 53;/* Page-Table Origin */ 11422938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 11522938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 11622938978SHeiko Carstens unsigned long : 3; 11722938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 11822938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 11922938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 12022938978SHeiko Carstens unsigned long : 2; 12122938978SHeiko Carstens }; 12222938978SHeiko Carstens 12322938978SHeiko Carstens struct segment_entry_fc1 { 12422938978SHeiko Carstens unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ 12522938978SHeiko Carstens unsigned long : 3; 12622938978SHeiko Carstens unsigned long av : 1; /* ACCF-Validity Control */ 12722938978SHeiko Carstens unsigned long acc: 4; /* Access-Control Bits */ 12822938978SHeiko Carstens unsigned long f : 1; /* Fetch-Protection Bit */ 12922938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 13022938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 13122938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 13222938978SHeiko Carstens unsigned long : 2; 13322938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 13422938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 13522938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 13622938978SHeiko Carstens unsigned long : 2; 13722938978SHeiko Carstens }; 13822938978SHeiko Carstens 13922938978SHeiko Carstens union segment_table_entry { 14022938978SHeiko Carstens unsigned long val; 14122938978SHeiko Carstens struct segment_entry_fc0 fc0; 14222938978SHeiko Carstens struct segment_entry_fc1 fc1; 14322938978SHeiko Carstens struct { 14422938978SHeiko Carstens unsigned long : 53; 14522938978SHeiko Carstens unsigned long fc : 1; /* Format-Control */ 14622938978SHeiko Carstens unsigned long : 4; 14722938978SHeiko Carstens unsigned long i : 1; /* Segment-Invalid Bit */ 14822938978SHeiko Carstens unsigned long cs : 1; /* Common-Segment Bit */ 14922938978SHeiko Carstens unsigned long tt : 2; /* Table-Type Bits */ 15022938978SHeiko Carstens unsigned long : 2; 15122938978SHeiko Carstens }; 15222938978SHeiko Carstens }; 15322938978SHeiko Carstens 15422938978SHeiko Carstens enum { 15522938978SHeiko Carstens TABLE_TYPE_SEGMENT = 0, 15622938978SHeiko Carstens TABLE_TYPE_REGION3 = 1, 15722938978SHeiko Carstens TABLE_TYPE_REGION2 = 2, 15822938978SHeiko Carstens TABLE_TYPE_REGION1 = 3 15922938978SHeiko Carstens }; 16022938978SHeiko Carstens 16122938978SHeiko Carstens union page_table_entry { 16222938978SHeiko Carstens unsigned long val; 16322938978SHeiko Carstens struct { 16422938978SHeiko Carstens unsigned long pfra : 52; /* Page-Frame Real Address */ 16522938978SHeiko Carstens unsigned long z : 1; /* Zero Bit */ 16622938978SHeiko Carstens unsigned long i : 1; /* Page-Invalid Bit */ 16722938978SHeiko Carstens unsigned long p : 1; /* DAT-Protection Bit */ 16822938978SHeiko Carstens unsigned long co : 1; /* Change-Recording Override */ 16922938978SHeiko Carstens unsigned long : 8; 17022938978SHeiko Carstens }; 17122938978SHeiko Carstens }; 17222938978SHeiko Carstens 17322938978SHeiko Carstens /* 17422938978SHeiko Carstens * vaddress union in order to easily decode a virtual address into its 17522938978SHeiko Carstens * region first index, region second index etc. parts. 17622938978SHeiko Carstens */ 17722938978SHeiko Carstens union vaddress { 17822938978SHeiko Carstens unsigned long addr; 17922938978SHeiko Carstens struct { 18022938978SHeiko Carstens unsigned long rfx : 11; 18122938978SHeiko Carstens unsigned long rsx : 11; 18222938978SHeiko Carstens unsigned long rtx : 11; 18322938978SHeiko Carstens unsigned long sx : 11; 18422938978SHeiko Carstens unsigned long px : 8; 18522938978SHeiko Carstens unsigned long bx : 12; 18622938978SHeiko Carstens }; 18722938978SHeiko Carstens struct { 18822938978SHeiko Carstens unsigned long rfx01 : 2; 18922938978SHeiko Carstens unsigned long : 9; 19022938978SHeiko Carstens unsigned long rsx01 : 2; 19122938978SHeiko Carstens unsigned long : 9; 19222938978SHeiko Carstens unsigned long rtx01 : 2; 19322938978SHeiko Carstens unsigned long : 9; 19422938978SHeiko Carstens unsigned long sx01 : 2; 19522938978SHeiko Carstens unsigned long : 29; 19622938978SHeiko Carstens }; 19722938978SHeiko Carstens }; 19822938978SHeiko Carstens 19922938978SHeiko Carstens /* 20022938978SHeiko Carstens * raddress union which will contain the result (real or absolute address) 20122938978SHeiko Carstens * after a page table walk. The rfaa, sfaa and pfra members are used to 20222938978SHeiko Carstens * simply assign them the value of a region, segment or page table entry. 20322938978SHeiko Carstens */ 20422938978SHeiko Carstens union raddress { 20522938978SHeiko Carstens unsigned long addr; 20622938978SHeiko Carstens unsigned long rfaa : 33; /* Region-Frame Absolute Address */ 20722938978SHeiko Carstens unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ 20822938978SHeiko Carstens unsigned long pfra : 52; /* Page-Frame Real Address */ 20922938978SHeiko Carstens }; 21022938978SHeiko Carstens 211664b4973SAlexander Yarygin union alet { 212664b4973SAlexander Yarygin u32 val; 213664b4973SAlexander Yarygin struct { 214664b4973SAlexander Yarygin u32 reserved : 7; 215664b4973SAlexander Yarygin u32 p : 1; 216664b4973SAlexander Yarygin u32 alesn : 8; 217664b4973SAlexander Yarygin u32 alen : 16; 218664b4973SAlexander Yarygin }; 219664b4973SAlexander Yarygin }; 220664b4973SAlexander Yarygin 221664b4973SAlexander Yarygin union ald { 222664b4973SAlexander Yarygin u32 val; 223664b4973SAlexander Yarygin struct { 224664b4973SAlexander Yarygin u32 : 1; 225664b4973SAlexander Yarygin u32 alo : 24; 226664b4973SAlexander Yarygin u32 all : 7; 227664b4973SAlexander Yarygin }; 228664b4973SAlexander Yarygin }; 229664b4973SAlexander Yarygin 230664b4973SAlexander Yarygin struct ale { 231664b4973SAlexander Yarygin unsigned long i : 1; /* ALEN-Invalid Bit */ 232664b4973SAlexander Yarygin unsigned long : 5; 233664b4973SAlexander Yarygin unsigned long fo : 1; /* Fetch-Only Bit */ 234664b4973SAlexander Yarygin unsigned long p : 1; /* Private Bit */ 235664b4973SAlexander Yarygin unsigned long alesn : 8; /* Access-List-Entry Sequence Number */ 236664b4973SAlexander Yarygin unsigned long aleax : 16; /* Access-List-Entry Authorization Index */ 237664b4973SAlexander Yarygin unsigned long : 32; 238664b4973SAlexander Yarygin unsigned long : 1; 239664b4973SAlexander Yarygin unsigned long asteo : 25; /* ASN-Second-Table-Entry Origin */ 240664b4973SAlexander Yarygin unsigned long : 6; 241664b4973SAlexander Yarygin unsigned long astesn : 32; /* ASTE Sequence Number */ 242664b4973SAlexander Yarygin } __packed; 243664b4973SAlexander Yarygin 244664b4973SAlexander Yarygin struct aste { 245664b4973SAlexander Yarygin unsigned long i : 1; /* ASX-Invalid Bit */ 246664b4973SAlexander Yarygin unsigned long ato : 29; /* Authority-Table Origin */ 247664b4973SAlexander Yarygin unsigned long : 1; 248664b4973SAlexander Yarygin unsigned long b : 1; /* Base-Space Bit */ 249664b4973SAlexander Yarygin unsigned long ax : 16; /* Authorization Index */ 250664b4973SAlexander Yarygin unsigned long atl : 12; /* Authority-Table Length */ 251664b4973SAlexander Yarygin unsigned long : 2; 252664b4973SAlexander Yarygin unsigned long ca : 1; /* Controlled-ASN Bit */ 253664b4973SAlexander Yarygin unsigned long ra : 1; /* Reusable-ASN Bit */ 254664b4973SAlexander Yarygin unsigned long asce : 64; /* Address-Space-Control Element */ 255664b4973SAlexander Yarygin unsigned long ald : 32; 256664b4973SAlexander Yarygin unsigned long astesn : 32; 257664b4973SAlexander Yarygin /* .. more fields there */ 258664b4973SAlexander Yarygin } __packed; 2598a242234SHeiko Carstens 2608a242234SHeiko Carstens int ipte_lock_held(struct kvm_vcpu *vcpu) 2618a242234SHeiko Carstens { 2625e044315SEugene (jno) Dvurechenski if (vcpu->arch.sie_block->eca & 1) { 2635e044315SEugene (jno) Dvurechenski int rc; 2648a242234SHeiko Carstens 2655e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 2665e044315SEugene (jno) Dvurechenski rc = kvm_s390_get_ipte_control(vcpu->kvm)->kh != 0; 2675e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 2685e044315SEugene (jno) Dvurechenski return rc; 2695e044315SEugene (jno) Dvurechenski } 270a6b7e459SThomas Huth return vcpu->kvm->arch.ipte_lock_count != 0; 2718a242234SHeiko Carstens } 2728a242234SHeiko Carstens 2738a242234SHeiko Carstens static void ipte_lock_simple(struct kvm_vcpu *vcpu) 2748a242234SHeiko Carstens { 2758a242234SHeiko Carstens union ipte_control old, new, *ic; 2768a242234SHeiko Carstens 277a6b7e459SThomas Huth mutex_lock(&vcpu->kvm->arch.ipte_mutex); 278a6b7e459SThomas Huth vcpu->kvm->arch.ipte_lock_count++; 279a6b7e459SThomas Huth if (vcpu->kvm->arch.ipte_lock_count > 1) 2808a242234SHeiko Carstens goto out; 2815e044315SEugene (jno) Dvurechenski retry: 2825e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 28360514510SEugene (jno) Dvurechenski ic = kvm_s390_get_ipte_control(vcpu->kvm); 2848a242234SHeiko Carstens do { 2855de72a22SChristian Borntraeger old = READ_ONCE(*ic); 2865e044315SEugene (jno) Dvurechenski if (old.k) { 2875e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 2888a242234SHeiko Carstens cond_resched(); 2895e044315SEugene (jno) Dvurechenski goto retry; 2908a242234SHeiko Carstens } 2918a242234SHeiko Carstens new = old; 2928a242234SHeiko Carstens new.k = 1; 2938a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 2945e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 2958a242234SHeiko Carstens out: 296a6b7e459SThomas Huth mutex_unlock(&vcpu->kvm->arch.ipte_mutex); 2978a242234SHeiko Carstens } 2988a242234SHeiko Carstens 2998a242234SHeiko Carstens static void ipte_unlock_simple(struct kvm_vcpu *vcpu) 3008a242234SHeiko Carstens { 3018a242234SHeiko Carstens union ipte_control old, new, *ic; 3028a242234SHeiko Carstens 303a6b7e459SThomas Huth mutex_lock(&vcpu->kvm->arch.ipte_mutex); 304a6b7e459SThomas Huth vcpu->kvm->arch.ipte_lock_count--; 305a6b7e459SThomas Huth if (vcpu->kvm->arch.ipte_lock_count) 3068a242234SHeiko Carstens goto out; 3075e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 30860514510SEugene (jno) Dvurechenski ic = kvm_s390_get_ipte_control(vcpu->kvm); 3098a242234SHeiko Carstens do { 3105de72a22SChristian Borntraeger old = READ_ONCE(*ic); 3111365039dSChristian Borntraeger new = old; 3128a242234SHeiko Carstens new.k = 0; 3138a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 3145e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 3158a242234SHeiko Carstens wake_up(&vcpu->kvm->arch.ipte_wq); 3168a242234SHeiko Carstens out: 317a6b7e459SThomas Huth mutex_unlock(&vcpu->kvm->arch.ipte_mutex); 3188a242234SHeiko Carstens } 3198a242234SHeiko Carstens 3208a242234SHeiko Carstens static void ipte_lock_siif(struct kvm_vcpu *vcpu) 3218a242234SHeiko Carstens { 3228a242234SHeiko Carstens union ipte_control old, new, *ic; 3238a242234SHeiko Carstens 3245e044315SEugene (jno) Dvurechenski retry: 3255e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 32660514510SEugene (jno) Dvurechenski ic = kvm_s390_get_ipte_control(vcpu->kvm); 3278a242234SHeiko Carstens do { 3285de72a22SChristian Borntraeger old = READ_ONCE(*ic); 3295e044315SEugene (jno) Dvurechenski if (old.kg) { 3305e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 3318a242234SHeiko Carstens cond_resched(); 3325e044315SEugene (jno) Dvurechenski goto retry; 3338a242234SHeiko Carstens } 3348a242234SHeiko Carstens new = old; 3358a242234SHeiko Carstens new.k = 1; 3368a242234SHeiko Carstens new.kh++; 3378a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 3385e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 3398a242234SHeiko Carstens } 3408a242234SHeiko Carstens 3418a242234SHeiko Carstens static void ipte_unlock_siif(struct kvm_vcpu *vcpu) 3428a242234SHeiko Carstens { 3438a242234SHeiko Carstens union ipte_control old, new, *ic; 3448a242234SHeiko Carstens 3455e044315SEugene (jno) Dvurechenski read_lock(&vcpu->kvm->arch.sca_lock); 34660514510SEugene (jno) Dvurechenski ic = kvm_s390_get_ipte_control(vcpu->kvm); 3478a242234SHeiko Carstens do { 3485de72a22SChristian Borntraeger old = READ_ONCE(*ic); 3491365039dSChristian Borntraeger new = old; 3508a242234SHeiko Carstens new.kh--; 3518a242234SHeiko Carstens if (!new.kh) 3528a242234SHeiko Carstens new.k = 0; 3538a242234SHeiko Carstens } while (cmpxchg(&ic->val, old.val, new.val) != old.val); 3545e044315SEugene (jno) Dvurechenski read_unlock(&vcpu->kvm->arch.sca_lock); 3558a242234SHeiko Carstens if (!new.kh) 3568a242234SHeiko Carstens wake_up(&vcpu->kvm->arch.ipte_wq); 3578a242234SHeiko Carstens } 3588a242234SHeiko Carstens 359a0465f9aSThomas Huth void ipte_lock(struct kvm_vcpu *vcpu) 3608a242234SHeiko Carstens { 3618a242234SHeiko Carstens if (vcpu->arch.sie_block->eca & 1) 3628a242234SHeiko Carstens ipte_lock_siif(vcpu); 3638a242234SHeiko Carstens else 3648a242234SHeiko Carstens ipte_lock_simple(vcpu); 3658a242234SHeiko Carstens } 3668a242234SHeiko Carstens 367a0465f9aSThomas Huth void ipte_unlock(struct kvm_vcpu *vcpu) 3688a242234SHeiko Carstens { 3698a242234SHeiko Carstens if (vcpu->arch.sie_block->eca & 1) 3708a242234SHeiko Carstens ipte_unlock_siif(vcpu); 3718a242234SHeiko Carstens else 3728a242234SHeiko Carstens ipte_unlock_simple(vcpu); 3738a242234SHeiko Carstens } 3748a242234SHeiko Carstens 375664b4973SAlexander Yarygin static int ar_translation(struct kvm_vcpu *vcpu, union asce *asce, ar_t ar, 37692c96321SDavid Hildenbrand enum gacc_mode mode) 37722938978SHeiko Carstens { 378664b4973SAlexander Yarygin union alet alet; 379664b4973SAlexander Yarygin struct ale ale; 380664b4973SAlexander Yarygin struct aste aste; 381664b4973SAlexander Yarygin unsigned long ald_addr, authority_table_addr; 382664b4973SAlexander Yarygin union ald ald; 383664b4973SAlexander Yarygin int eax, rc; 384664b4973SAlexander Yarygin u8 authority_table; 385664b4973SAlexander Yarygin 386664b4973SAlexander Yarygin if (ar >= NUM_ACRS) 387664b4973SAlexander Yarygin return -EINVAL; 388664b4973SAlexander Yarygin 389664b4973SAlexander Yarygin save_access_regs(vcpu->run->s.regs.acrs); 390664b4973SAlexander Yarygin alet.val = vcpu->run->s.regs.acrs[ar]; 391664b4973SAlexander Yarygin 392664b4973SAlexander Yarygin if (ar == 0 || alet.val == 0) { 393664b4973SAlexander Yarygin asce->val = vcpu->arch.sie_block->gcr[1]; 394664b4973SAlexander Yarygin return 0; 395664b4973SAlexander Yarygin } else if (alet.val == 1) { 396664b4973SAlexander Yarygin asce->val = vcpu->arch.sie_block->gcr[7]; 397664b4973SAlexander Yarygin return 0; 398664b4973SAlexander Yarygin } 399664b4973SAlexander Yarygin 400664b4973SAlexander Yarygin if (alet.reserved) 401664b4973SAlexander Yarygin return PGM_ALET_SPECIFICATION; 402664b4973SAlexander Yarygin 403664b4973SAlexander Yarygin if (alet.p) 404664b4973SAlexander Yarygin ald_addr = vcpu->arch.sie_block->gcr[5]; 405664b4973SAlexander Yarygin else 406664b4973SAlexander Yarygin ald_addr = vcpu->arch.sie_block->gcr[2]; 407664b4973SAlexander Yarygin ald_addr &= 0x7fffffc0; 408664b4973SAlexander Yarygin 409664b4973SAlexander Yarygin rc = read_guest_real(vcpu, ald_addr + 16, &ald.val, sizeof(union ald)); 410664b4973SAlexander Yarygin if (rc) 411664b4973SAlexander Yarygin return rc; 412664b4973SAlexander Yarygin 413664b4973SAlexander Yarygin if (alet.alen / 8 > ald.all) 414664b4973SAlexander Yarygin return PGM_ALEN_TRANSLATION; 415664b4973SAlexander Yarygin 416664b4973SAlexander Yarygin if (0x7fffffff - ald.alo * 128 < alet.alen * 16) 417664b4973SAlexander Yarygin return PGM_ADDRESSING; 418664b4973SAlexander Yarygin 419664b4973SAlexander Yarygin rc = read_guest_real(vcpu, ald.alo * 128 + alet.alen * 16, &ale, 420664b4973SAlexander Yarygin sizeof(struct ale)); 421664b4973SAlexander Yarygin if (rc) 422664b4973SAlexander Yarygin return rc; 423664b4973SAlexander Yarygin 424664b4973SAlexander Yarygin if (ale.i == 1) 425664b4973SAlexander Yarygin return PGM_ALEN_TRANSLATION; 426664b4973SAlexander Yarygin if (ale.alesn != alet.alesn) 427664b4973SAlexander Yarygin return PGM_ALE_SEQUENCE; 428664b4973SAlexander Yarygin 429664b4973SAlexander Yarygin rc = read_guest_real(vcpu, ale.asteo * 64, &aste, sizeof(struct aste)); 430664b4973SAlexander Yarygin if (rc) 431664b4973SAlexander Yarygin return rc; 432664b4973SAlexander Yarygin 433664b4973SAlexander Yarygin if (aste.i) 434664b4973SAlexander Yarygin return PGM_ASTE_VALIDITY; 435664b4973SAlexander Yarygin if (aste.astesn != ale.astesn) 436664b4973SAlexander Yarygin return PGM_ASTE_SEQUENCE; 437664b4973SAlexander Yarygin 438664b4973SAlexander Yarygin if (ale.p == 1) { 439664b4973SAlexander Yarygin eax = (vcpu->arch.sie_block->gcr[8] >> 16) & 0xffff; 440664b4973SAlexander Yarygin if (ale.aleax != eax) { 441664b4973SAlexander Yarygin if (eax / 16 > aste.atl) 442664b4973SAlexander Yarygin return PGM_EXTENDED_AUTHORITY; 443664b4973SAlexander Yarygin 444664b4973SAlexander Yarygin authority_table_addr = aste.ato * 4 + eax / 4; 445664b4973SAlexander Yarygin 446664b4973SAlexander Yarygin rc = read_guest_real(vcpu, authority_table_addr, 447664b4973SAlexander Yarygin &authority_table, 448664b4973SAlexander Yarygin sizeof(u8)); 449664b4973SAlexander Yarygin if (rc) 450664b4973SAlexander Yarygin return rc; 451664b4973SAlexander Yarygin 452664b4973SAlexander Yarygin if ((authority_table & (0x40 >> ((eax & 3) * 2))) == 0) 453664b4973SAlexander Yarygin return PGM_EXTENDED_AUTHORITY; 454664b4973SAlexander Yarygin } 455664b4973SAlexander Yarygin } 456664b4973SAlexander Yarygin 45792c96321SDavid Hildenbrand if (ale.fo == 1 && mode == GACC_STORE) 458664b4973SAlexander Yarygin return PGM_PROTECTION; 459664b4973SAlexander Yarygin 460664b4973SAlexander Yarygin asce->val = aste.asce; 461664b4973SAlexander Yarygin return 0; 462664b4973SAlexander Yarygin } 463664b4973SAlexander Yarygin 464664b4973SAlexander Yarygin struct trans_exc_code_bits { 465664b4973SAlexander Yarygin unsigned long addr : 52; /* Translation-exception Address */ 466664b4973SAlexander Yarygin unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */ 467664b4973SAlexander Yarygin unsigned long : 6; 468664b4973SAlexander Yarygin unsigned long b60 : 1; 469664b4973SAlexander Yarygin unsigned long b61 : 1; 470664b4973SAlexander Yarygin unsigned long as : 2; /* ASCE Identifier */ 471664b4973SAlexander Yarygin }; 472664b4973SAlexander Yarygin 473664b4973SAlexander Yarygin enum { 474664b4973SAlexander Yarygin FSI_UNKNOWN = 0, /* Unknown wether fetch or store */ 475664b4973SAlexander Yarygin FSI_STORE = 1, /* Exception was due to store operation */ 476664b4973SAlexander Yarygin FSI_FETCH = 2 /* Exception was due to fetch operation */ 477664b4973SAlexander Yarygin }; 478664b4973SAlexander Yarygin 479d03193deSDavid Hildenbrand enum prot_type { 480d03193deSDavid Hildenbrand PROT_TYPE_LA = 0, 481d03193deSDavid Hildenbrand PROT_TYPE_KEYC = 1, 482d03193deSDavid Hildenbrand PROT_TYPE_ALC = 2, 483d03193deSDavid Hildenbrand PROT_TYPE_DAT = 3, 484d03193deSDavid Hildenbrand }; 485d03193deSDavid Hildenbrand 486d03193deSDavid Hildenbrand static int trans_exc(struct kvm_vcpu *vcpu, int code, unsigned long gva, 487d03193deSDavid Hildenbrand ar_t ar, enum gacc_mode mode, enum prot_type prot) 488d03193deSDavid Hildenbrand { 489d03193deSDavid Hildenbrand struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 490d03193deSDavid Hildenbrand struct trans_exc_code_bits *tec; 491d03193deSDavid Hildenbrand 492d03193deSDavid Hildenbrand memset(pgm, 0, sizeof(*pgm)); 493d03193deSDavid Hildenbrand pgm->code = code; 494d03193deSDavid Hildenbrand tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 495d03193deSDavid Hildenbrand 496d03193deSDavid Hildenbrand switch (code) { 497d03193deSDavid Hildenbrand case PGM_ASCE_TYPE: 498d03193deSDavid Hildenbrand case PGM_PAGE_TRANSLATION: 499d03193deSDavid Hildenbrand case PGM_REGION_FIRST_TRANS: 500d03193deSDavid Hildenbrand case PGM_REGION_SECOND_TRANS: 501d03193deSDavid Hildenbrand case PGM_REGION_THIRD_TRANS: 502d03193deSDavid Hildenbrand case PGM_SEGMENT_TRANSLATION: 503d03193deSDavid Hildenbrand /* 504d03193deSDavid Hildenbrand * op_access_id only applies to MOVE_PAGE -> set bit 61 505d03193deSDavid Hildenbrand * exc_access_id has to be set to 0 for some instructions. Both 506d03193deSDavid Hildenbrand * cases have to be handled by the caller. We can always store 507d03193deSDavid Hildenbrand * exc_access_id, as it is undefined for non-ar cases. 508d03193deSDavid Hildenbrand */ 509d03193deSDavid Hildenbrand tec->addr = gva >> PAGE_SHIFT; 510d03193deSDavid Hildenbrand tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH; 511d03193deSDavid Hildenbrand tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as; 512d03193deSDavid Hildenbrand /* FALL THROUGH */ 513d03193deSDavid Hildenbrand case PGM_ALEN_TRANSLATION: 514d03193deSDavid Hildenbrand case PGM_ALE_SEQUENCE: 515d03193deSDavid Hildenbrand case PGM_ASTE_VALIDITY: 516d03193deSDavid Hildenbrand case PGM_ASTE_SEQUENCE: 517d03193deSDavid Hildenbrand case PGM_EXTENDED_AUTHORITY: 518d03193deSDavid Hildenbrand pgm->exc_access_id = ar; 519d03193deSDavid Hildenbrand break; 520d03193deSDavid Hildenbrand case PGM_PROTECTION: 521d03193deSDavid Hildenbrand switch (prot) { 522d03193deSDavid Hildenbrand case PROT_TYPE_ALC: 523d03193deSDavid Hildenbrand tec->b60 = 1; 524d03193deSDavid Hildenbrand /* FALL THROUGH */ 525d03193deSDavid Hildenbrand case PROT_TYPE_DAT: 526d03193deSDavid Hildenbrand tec->b61 = 1; 527d03193deSDavid Hildenbrand tec->addr = gva >> PAGE_SHIFT; 528d03193deSDavid Hildenbrand tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH; 529d03193deSDavid Hildenbrand tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as; 530d03193deSDavid Hildenbrand /* exc_access_id is undefined for most cases */ 531d03193deSDavid Hildenbrand pgm->exc_access_id = ar; 532d03193deSDavid Hildenbrand break; 533d03193deSDavid Hildenbrand default: /* LA and KEYC set b61 to 0, other params undefined */ 534d03193deSDavid Hildenbrand break; 535d03193deSDavid Hildenbrand } 536d03193deSDavid Hildenbrand break; 537d03193deSDavid Hildenbrand } 538d03193deSDavid Hildenbrand return code; 539d03193deSDavid Hildenbrand } 540d03193deSDavid Hildenbrand 541664b4973SAlexander Yarygin static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce, 5426167375bSDavid Hildenbrand unsigned long ga, ar_t ar, enum gacc_mode mode) 543664b4973SAlexander Yarygin { 544664b4973SAlexander Yarygin int rc; 54534346b9aSDavid Hildenbrand struct psw_bits psw = psw_bits(vcpu->arch.sie_block->gpsw); 546664b4973SAlexander Yarygin struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; 547664b4973SAlexander Yarygin struct trans_exc_code_bits *tec_bits; 548664b4973SAlexander Yarygin 549664b4973SAlexander Yarygin memset(pgm, 0, sizeof(*pgm)); 550664b4973SAlexander Yarygin tec_bits = (struct trans_exc_code_bits *)&pgm->trans_exc_code; 55192c96321SDavid Hildenbrand tec_bits->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH; 55234346b9aSDavid Hildenbrand tec_bits->as = psw.as; 553664b4973SAlexander Yarygin 55434346b9aSDavid Hildenbrand if (!psw.t) { 555664b4973SAlexander Yarygin asce->val = 0; 556664b4973SAlexander Yarygin asce->r = 1; 557664b4973SAlexander Yarygin return 0; 558664b4973SAlexander Yarygin } 559664b4973SAlexander Yarygin 56034346b9aSDavid Hildenbrand if (mode == GACC_IFETCH) 56134346b9aSDavid Hildenbrand psw.as = psw.as == PSW_AS_HOME ? PSW_AS_HOME : PSW_AS_PRIMARY; 56234346b9aSDavid Hildenbrand 56334346b9aSDavid Hildenbrand switch (psw.as) { 56422938978SHeiko Carstens case PSW_AS_PRIMARY: 565664b4973SAlexander Yarygin asce->val = vcpu->arch.sie_block->gcr[1]; 566664b4973SAlexander Yarygin return 0; 56722938978SHeiko Carstens case PSW_AS_SECONDARY: 568664b4973SAlexander Yarygin asce->val = vcpu->arch.sie_block->gcr[7]; 569664b4973SAlexander Yarygin return 0; 57022938978SHeiko Carstens case PSW_AS_HOME: 571664b4973SAlexander Yarygin asce->val = vcpu->arch.sie_block->gcr[13]; 572664b4973SAlexander Yarygin return 0; 573664b4973SAlexander Yarygin case PSW_AS_ACCREG: 57492c96321SDavid Hildenbrand rc = ar_translation(vcpu, asce, ar, mode); 575664b4973SAlexander Yarygin switch (rc) { 576664b4973SAlexander Yarygin case PGM_ALEN_TRANSLATION: 577664b4973SAlexander Yarygin case PGM_ALE_SEQUENCE: 578664b4973SAlexander Yarygin case PGM_ASTE_VALIDITY: 579664b4973SAlexander Yarygin case PGM_ASTE_SEQUENCE: 580664b4973SAlexander Yarygin case PGM_EXTENDED_AUTHORITY: 581664b4973SAlexander Yarygin vcpu->arch.pgm.exc_access_id = ar; 582664b4973SAlexander Yarygin break; 583664b4973SAlexander Yarygin case PGM_PROTECTION: 5846167375bSDavid Hildenbrand tec_bits->addr = ga >> PAGE_SHIFT; 585664b4973SAlexander Yarygin tec_bits->b60 = 1; 586664b4973SAlexander Yarygin tec_bits->b61 = 1; 587664b4973SAlexander Yarygin break; 588664b4973SAlexander Yarygin } 589664b4973SAlexander Yarygin if (rc > 0) 590664b4973SAlexander Yarygin pgm->code = rc; 591664b4973SAlexander Yarygin return rc; 59222938978SHeiko Carstens } 59322938978SHeiko Carstens return 0; 59422938978SHeiko Carstens } 59522938978SHeiko Carstens 59622938978SHeiko Carstens static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val) 59722938978SHeiko Carstens { 59822938978SHeiko Carstens return kvm_read_guest(kvm, gpa, val, sizeof(*val)); 59922938978SHeiko Carstens } 60022938978SHeiko Carstens 60122938978SHeiko Carstens /** 60222938978SHeiko Carstens * guest_translate - translate a guest virtual into a guest absolute address 60322938978SHeiko Carstens * @vcpu: virtual cpu 60422938978SHeiko Carstens * @gva: guest virtual address 60522938978SHeiko Carstens * @gpa: points to where guest physical (absolute) address should be stored 60675a18122SAlexander Yarygin * @asce: effective asce 60792c96321SDavid Hildenbrand * @mode: indicates the access mode to be used 60822938978SHeiko Carstens * 60922938978SHeiko Carstens * Translate a guest virtual address into a guest absolute address by means 61016b0fc13SYannick Guerrini * of dynamic address translation as specified by the architecture. 61122938978SHeiko Carstens * If the resulting absolute address is not available in the configuration 61222938978SHeiko Carstens * an addressing exception is indicated and @gpa will not be changed. 61322938978SHeiko Carstens * 61422938978SHeiko Carstens * Returns: - zero on success; @gpa contains the resulting absolute address 61522938978SHeiko Carstens * - a negative value if guest access failed due to e.g. broken 61622938978SHeiko Carstens * guest mapping 61722938978SHeiko Carstens * - a positve value if an access exception happened. In this case 61822938978SHeiko Carstens * the returned value is the program interruption code as defined 61922938978SHeiko Carstens * by the architecture 62022938978SHeiko Carstens */ 62122938978SHeiko Carstens static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva, 62275a18122SAlexander Yarygin unsigned long *gpa, const union asce asce, 62392c96321SDavid Hildenbrand enum gacc_mode mode) 62422938978SHeiko Carstens { 62522938978SHeiko Carstens union vaddress vaddr = {.addr = gva}; 62622938978SHeiko Carstens union raddress raddr = {.addr = gva}; 62722938978SHeiko Carstens union page_table_entry pte; 62822938978SHeiko Carstens int dat_protection = 0; 62922938978SHeiko Carstens union ctlreg0 ctlreg0; 63022938978SHeiko Carstens unsigned long ptr; 63122938978SHeiko Carstens int edat1, edat2; 63222938978SHeiko Carstens 63322938978SHeiko Carstens ctlreg0.val = vcpu->arch.sie_block->gcr[0]; 6349d8d5786SMichael Mueller edat1 = ctlreg0.edat && test_kvm_facility(vcpu->kvm, 8); 6359d8d5786SMichael Mueller edat2 = edat1 && test_kvm_facility(vcpu->kvm, 78); 63622938978SHeiko Carstens if (asce.r) 63722938978SHeiko Carstens goto real_address; 63822938978SHeiko Carstens ptr = asce.origin * 4096; 63922938978SHeiko Carstens switch (asce.dt) { 64022938978SHeiko Carstens case ASCE_TYPE_REGION1: 64122938978SHeiko Carstens if (vaddr.rfx01 > asce.tl) 64222938978SHeiko Carstens return PGM_REGION_FIRST_TRANS; 64322938978SHeiko Carstens ptr += vaddr.rfx * 8; 64422938978SHeiko Carstens break; 64522938978SHeiko Carstens case ASCE_TYPE_REGION2: 64622938978SHeiko Carstens if (vaddr.rfx) 64722938978SHeiko Carstens return PGM_ASCE_TYPE; 64822938978SHeiko Carstens if (vaddr.rsx01 > asce.tl) 64922938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 65022938978SHeiko Carstens ptr += vaddr.rsx * 8; 65122938978SHeiko Carstens break; 65222938978SHeiko Carstens case ASCE_TYPE_REGION3: 65322938978SHeiko Carstens if (vaddr.rfx || vaddr.rsx) 65422938978SHeiko Carstens return PGM_ASCE_TYPE; 65522938978SHeiko Carstens if (vaddr.rtx01 > asce.tl) 65622938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 65722938978SHeiko Carstens ptr += vaddr.rtx * 8; 65822938978SHeiko Carstens break; 65922938978SHeiko Carstens case ASCE_TYPE_SEGMENT: 66022938978SHeiko Carstens if (vaddr.rfx || vaddr.rsx || vaddr.rtx) 66122938978SHeiko Carstens return PGM_ASCE_TYPE; 66222938978SHeiko Carstens if (vaddr.sx01 > asce.tl) 66322938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 66422938978SHeiko Carstens ptr += vaddr.sx * 8; 66522938978SHeiko Carstens break; 66622938978SHeiko Carstens } 66722938978SHeiko Carstens switch (asce.dt) { 66822938978SHeiko Carstens case ASCE_TYPE_REGION1: { 66922938978SHeiko Carstens union region1_table_entry rfte; 67022938978SHeiko Carstens 67122938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 67222938978SHeiko Carstens return PGM_ADDRESSING; 67322938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rfte.val)) 67422938978SHeiko Carstens return -EFAULT; 67522938978SHeiko Carstens if (rfte.i) 67622938978SHeiko Carstens return PGM_REGION_FIRST_TRANS; 67722938978SHeiko Carstens if (rfte.tt != TABLE_TYPE_REGION1) 67822938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 67922938978SHeiko Carstens if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl) 68022938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 68122938978SHeiko Carstens if (edat1) 68222938978SHeiko Carstens dat_protection |= rfte.p; 68322938978SHeiko Carstens ptr = rfte.rto * 4096 + vaddr.rsx * 8; 68422938978SHeiko Carstens } 68522938978SHeiko Carstens /* fallthrough */ 68622938978SHeiko Carstens case ASCE_TYPE_REGION2: { 68722938978SHeiko Carstens union region2_table_entry rste; 68822938978SHeiko Carstens 68922938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 69022938978SHeiko Carstens return PGM_ADDRESSING; 69122938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rste.val)) 69222938978SHeiko Carstens return -EFAULT; 69322938978SHeiko Carstens if (rste.i) 69422938978SHeiko Carstens return PGM_REGION_SECOND_TRANS; 69522938978SHeiko Carstens if (rste.tt != TABLE_TYPE_REGION2) 69622938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 69722938978SHeiko Carstens if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl) 69822938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 69922938978SHeiko Carstens if (edat1) 70022938978SHeiko Carstens dat_protection |= rste.p; 70122938978SHeiko Carstens ptr = rste.rto * 4096 + vaddr.rtx * 8; 70222938978SHeiko Carstens } 70322938978SHeiko Carstens /* fallthrough */ 70422938978SHeiko Carstens case ASCE_TYPE_REGION3: { 70522938978SHeiko Carstens union region3_table_entry rtte; 70622938978SHeiko Carstens 70722938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 70822938978SHeiko Carstens return PGM_ADDRESSING; 70922938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &rtte.val)) 71022938978SHeiko Carstens return -EFAULT; 71122938978SHeiko Carstens if (rtte.i) 71222938978SHeiko Carstens return PGM_REGION_THIRD_TRANS; 71322938978SHeiko Carstens if (rtte.tt != TABLE_TYPE_REGION3) 71422938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 71522938978SHeiko Carstens if (rtte.cr && asce.p && edat2) 71622938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 71722938978SHeiko Carstens if (rtte.fc && edat2) { 71822938978SHeiko Carstens dat_protection |= rtte.fc1.p; 71922938978SHeiko Carstens raddr.rfaa = rtte.fc1.rfaa; 72022938978SHeiko Carstens goto absolute_address; 72122938978SHeiko Carstens } 72222938978SHeiko Carstens if (vaddr.sx01 < rtte.fc0.tf) 72322938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 72422938978SHeiko Carstens if (vaddr.sx01 > rtte.fc0.tl) 72522938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 72622938978SHeiko Carstens if (edat1) 72722938978SHeiko Carstens dat_protection |= rtte.fc0.p; 72822938978SHeiko Carstens ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8; 72922938978SHeiko Carstens } 73022938978SHeiko Carstens /* fallthrough */ 73122938978SHeiko Carstens case ASCE_TYPE_SEGMENT: { 73222938978SHeiko Carstens union segment_table_entry ste; 73322938978SHeiko Carstens 73422938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 73522938978SHeiko Carstens return PGM_ADDRESSING; 73622938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &ste.val)) 73722938978SHeiko Carstens return -EFAULT; 73822938978SHeiko Carstens if (ste.i) 73922938978SHeiko Carstens return PGM_SEGMENT_TRANSLATION; 74022938978SHeiko Carstens if (ste.tt != TABLE_TYPE_SEGMENT) 74122938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 74222938978SHeiko Carstens if (ste.cs && asce.p) 74322938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 74422938978SHeiko Carstens if (ste.fc && edat1) { 74522938978SHeiko Carstens dat_protection |= ste.fc1.p; 74622938978SHeiko Carstens raddr.sfaa = ste.fc1.sfaa; 74722938978SHeiko Carstens goto absolute_address; 74822938978SHeiko Carstens } 74922938978SHeiko Carstens dat_protection |= ste.fc0.p; 75022938978SHeiko Carstens ptr = ste.fc0.pto * 2048 + vaddr.px * 8; 75122938978SHeiko Carstens } 75222938978SHeiko Carstens } 75322938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, ptr)) 75422938978SHeiko Carstens return PGM_ADDRESSING; 75522938978SHeiko Carstens if (deref_table(vcpu->kvm, ptr, &pte.val)) 75622938978SHeiko Carstens return -EFAULT; 75722938978SHeiko Carstens if (pte.i) 75822938978SHeiko Carstens return PGM_PAGE_TRANSLATION; 75922938978SHeiko Carstens if (pte.z) 76022938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 76122938978SHeiko Carstens if (pte.co && !edat1) 76222938978SHeiko Carstens return PGM_TRANSLATION_SPEC; 76322938978SHeiko Carstens dat_protection |= pte.p; 76422938978SHeiko Carstens raddr.pfra = pte.pfra; 76522938978SHeiko Carstens real_address: 76622938978SHeiko Carstens raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr); 76722938978SHeiko Carstens absolute_address: 76892c96321SDavid Hildenbrand if (mode == GACC_STORE && dat_protection) 76922938978SHeiko Carstens return PGM_PROTECTION; 77022938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, raddr.addr)) 77122938978SHeiko Carstens return PGM_ADDRESSING; 77222938978SHeiko Carstens *gpa = raddr.addr; 77322938978SHeiko Carstens return 0; 77422938978SHeiko Carstens } 77522938978SHeiko Carstens 77622938978SHeiko Carstens static inline int is_low_address(unsigned long ga) 77722938978SHeiko Carstens { 77822938978SHeiko Carstens /* Check for address ranges 0..511 and 4096..4607 */ 77922938978SHeiko Carstens return (ga & ~0x11fful) == 0; 78022938978SHeiko Carstens } 78122938978SHeiko Carstens 78275a18122SAlexander Yarygin static int low_address_protection_enabled(struct kvm_vcpu *vcpu, 78375a18122SAlexander Yarygin const union asce asce) 78422938978SHeiko Carstens { 78522938978SHeiko Carstens union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; 78622938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 78722938978SHeiko Carstens 78822938978SHeiko Carstens if (!ctlreg0.lap) 78922938978SHeiko Carstens return 0; 79022938978SHeiko Carstens if (psw_bits(*psw).t && asce.p) 79122938978SHeiko Carstens return 0; 79222938978SHeiko Carstens return 1; 79322938978SHeiko Carstens } 79422938978SHeiko Carstens 795*cde0dcfbSDavid Hildenbrand static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar, 79622938978SHeiko Carstens unsigned long *pages, unsigned long nr_pages, 79792c96321SDavid Hildenbrand const union asce asce, enum gacc_mode mode) 79822938978SHeiko Carstens { 79922938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 800*cde0dcfbSDavid Hildenbrand int lap_enabled, rc = 0; 80122938978SHeiko Carstens 80275a18122SAlexander Yarygin lap_enabled = low_address_protection_enabled(vcpu, asce); 80322938978SHeiko Carstens while (nr_pages) { 80422938978SHeiko Carstens ga = kvm_s390_logical_to_effective(vcpu, ga); 805*cde0dcfbSDavid Hildenbrand if (mode == GACC_STORE && lap_enabled && is_low_address(ga)) 806*cde0dcfbSDavid Hildenbrand return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode, 807*cde0dcfbSDavid Hildenbrand PROT_TYPE_LA); 80822938978SHeiko Carstens ga &= PAGE_MASK; 80922938978SHeiko Carstens if (psw_bits(*psw).t) { 81092c96321SDavid Hildenbrand rc = guest_translate(vcpu, ga, pages, asce, mode); 81122938978SHeiko Carstens if (rc < 0) 81222938978SHeiko Carstens return rc; 81322938978SHeiko Carstens } else { 81422938978SHeiko Carstens *pages = kvm_s390_real_to_abs(vcpu, ga); 81522938978SHeiko Carstens if (kvm_is_error_gpa(vcpu->kvm, *pages)) 816*cde0dcfbSDavid Hildenbrand rc = PGM_ADDRESSING; 81722938978SHeiko Carstens } 818*cde0dcfbSDavid Hildenbrand if (rc) 819*cde0dcfbSDavid Hildenbrand return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_DAT); 82022938978SHeiko Carstens ga += PAGE_SIZE; 82122938978SHeiko Carstens pages++; 82222938978SHeiko Carstens nr_pages--; 82322938978SHeiko Carstens } 82422938978SHeiko Carstens return 0; 82522938978SHeiko Carstens } 82622938978SHeiko Carstens 8278ae04b8fSAlexander Yarygin int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, ar_t ar, void *data, 82892c96321SDavid Hildenbrand unsigned long len, enum gacc_mode mode) 82922938978SHeiko Carstens { 83022938978SHeiko Carstens psw_t *psw = &vcpu->arch.sie_block->gpsw; 83122938978SHeiko Carstens unsigned long _len, nr_pages, gpa, idx; 83222938978SHeiko Carstens unsigned long pages_array[2]; 83322938978SHeiko Carstens unsigned long *pages; 8348a242234SHeiko Carstens int need_ipte_lock; 8358a242234SHeiko Carstens union asce asce; 83622938978SHeiko Carstens int rc; 83722938978SHeiko Carstens 83822938978SHeiko Carstens if (!len) 83922938978SHeiko Carstens return 0; 8406167375bSDavid Hildenbrand ga = kvm_s390_logical_to_effective(vcpu, ga); 8416167375bSDavid Hildenbrand rc = get_vcpu_asce(vcpu, &asce, ga, ar, mode); 842664b4973SAlexander Yarygin if (rc) 843664b4973SAlexander Yarygin return rc; 84422938978SHeiko Carstens nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1; 84522938978SHeiko Carstens pages = pages_array; 84622938978SHeiko Carstens if (nr_pages > ARRAY_SIZE(pages_array)) 84722938978SHeiko Carstens pages = vmalloc(nr_pages * sizeof(unsigned long)); 84822938978SHeiko Carstens if (!pages) 84922938978SHeiko Carstens return -ENOMEM; 8508a242234SHeiko Carstens need_ipte_lock = psw_bits(*psw).t && !asce.r; 8518a242234SHeiko Carstens if (need_ipte_lock) 8528a242234SHeiko Carstens ipte_lock(vcpu); 853*cde0dcfbSDavid Hildenbrand rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode); 85422938978SHeiko Carstens for (idx = 0; idx < nr_pages && !rc; idx++) { 85522938978SHeiko Carstens gpa = *(pages + idx) + (ga & ~PAGE_MASK); 85622938978SHeiko Carstens _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); 85792c96321SDavid Hildenbrand if (mode == GACC_STORE) 85822938978SHeiko Carstens rc = kvm_write_guest(vcpu->kvm, gpa, data, _len); 85922938978SHeiko Carstens else 86022938978SHeiko Carstens rc = kvm_read_guest(vcpu->kvm, gpa, data, _len); 86122938978SHeiko Carstens len -= _len; 86222938978SHeiko Carstens ga += _len; 86322938978SHeiko Carstens data += _len; 86422938978SHeiko Carstens } 8658a242234SHeiko Carstens if (need_ipte_lock) 8668a242234SHeiko Carstens ipte_unlock(vcpu); 86722938978SHeiko Carstens if (nr_pages > ARRAY_SIZE(pages_array)) 86822938978SHeiko Carstens vfree(pages); 86922938978SHeiko Carstens return rc; 87022938978SHeiko Carstens } 87122938978SHeiko Carstens 87222938978SHeiko Carstens int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, 87392c96321SDavid Hildenbrand void *data, unsigned long len, enum gacc_mode mode) 87422938978SHeiko Carstens { 87522938978SHeiko Carstens unsigned long _len, gpa; 87622938978SHeiko Carstens int rc = 0; 87722938978SHeiko Carstens 87822938978SHeiko Carstens while (len && !rc) { 87922938978SHeiko Carstens gpa = kvm_s390_real_to_abs(vcpu, gra); 88022938978SHeiko Carstens _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); 88192c96321SDavid Hildenbrand if (mode) 88222938978SHeiko Carstens rc = write_guest_abs(vcpu, gpa, data, _len); 88322938978SHeiko Carstens else 88422938978SHeiko Carstens rc = read_guest_abs(vcpu, gpa, data, _len); 88522938978SHeiko Carstens len -= _len; 88622938978SHeiko Carstens gra += _len; 88722938978SHeiko Carstens data += _len; 88822938978SHeiko Carstens } 88922938978SHeiko Carstens return rc; 89022938978SHeiko Carstens } 891f8232c8cSThomas Huth 892f8232c8cSThomas Huth /** 8939fbc0276SThomas Huth * guest_translate_address - translate guest logical into guest absolute address 8949fbc0276SThomas Huth * 8959fbc0276SThomas Huth * Parameter semantics are the same as the ones from guest_translate. 8969fbc0276SThomas Huth * The memory contents at the guest address are not changed. 8979fbc0276SThomas Huth * 8989fbc0276SThomas Huth * Note: The IPTE lock is not taken during this function, so the caller 8999fbc0276SThomas Huth * has to take care of this. 9009fbc0276SThomas Huth */ 9018ae04b8fSAlexander Yarygin int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar, 90292c96321SDavid Hildenbrand unsigned long *gpa, enum gacc_mode mode) 9039fbc0276SThomas Huth { 9049fbc0276SThomas Huth psw_t *psw = &vcpu->arch.sie_block->gpsw; 9059fbc0276SThomas Huth union asce asce; 9069fbc0276SThomas Huth int rc; 9079fbc0276SThomas Huth 9089fbc0276SThomas Huth gva = kvm_s390_logical_to_effective(vcpu, gva); 9096167375bSDavid Hildenbrand rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode); 910664b4973SAlexander Yarygin if (rc) 911664b4973SAlexander Yarygin return rc; 91275a18122SAlexander Yarygin if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) { 913fbcb7d51SDavid Hildenbrand if (mode == GACC_STORE) 914fbcb7d51SDavid Hildenbrand return trans_exc(vcpu, PGM_PROTECTION, gva, 0, 915fbcb7d51SDavid Hildenbrand mode, PROT_TYPE_LA); 9169fbc0276SThomas Huth } 9179fbc0276SThomas Huth 9189fbc0276SThomas Huth if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */ 91992c96321SDavid Hildenbrand rc = guest_translate(vcpu, gva, gpa, asce, mode); 920fbcb7d51SDavid Hildenbrand if (rc > 0) 921fbcb7d51SDavid Hildenbrand return trans_exc(vcpu, rc, gva, 0, mode, PROT_TYPE_DAT); 9229fbc0276SThomas Huth } else { 9239fbc0276SThomas Huth *gpa = kvm_s390_real_to_abs(vcpu, gva); 9249fbc0276SThomas Huth if (kvm_is_error_gpa(vcpu->kvm, *gpa)) 925fbcb7d51SDavid Hildenbrand return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0); 9269fbc0276SThomas Huth } 9279fbc0276SThomas Huth 9289fbc0276SThomas Huth return rc; 9299fbc0276SThomas Huth } 9309fbc0276SThomas Huth 9319fbc0276SThomas Huth /** 93241408c28SThomas Huth * check_gva_range - test a range of guest virtual addresses for accessibility 93341408c28SThomas Huth */ 93441408c28SThomas Huth int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, ar_t ar, 93592c96321SDavid Hildenbrand unsigned long length, enum gacc_mode mode) 93641408c28SThomas Huth { 93741408c28SThomas Huth unsigned long gpa; 93841408c28SThomas Huth unsigned long currlen; 93941408c28SThomas Huth int rc = 0; 94041408c28SThomas Huth 94141408c28SThomas Huth ipte_lock(vcpu); 94241408c28SThomas Huth while (length > 0 && !rc) { 94341408c28SThomas Huth currlen = min(length, PAGE_SIZE - (gva % PAGE_SIZE)); 94492c96321SDavid Hildenbrand rc = guest_translate_address(vcpu, gva, ar, &gpa, mode); 94541408c28SThomas Huth gva += currlen; 94641408c28SThomas Huth length -= currlen; 94741408c28SThomas Huth } 94841408c28SThomas Huth ipte_unlock(vcpu); 94941408c28SThomas Huth 95041408c28SThomas Huth return rc; 95141408c28SThomas Huth } 95241408c28SThomas Huth 95341408c28SThomas Huth /** 954dd9e5b7bSAlexander Yarygin * kvm_s390_check_low_addr_prot_real - check for low-address protection 955dd9e5b7bSAlexander Yarygin * @gra: Guest real address 956f8232c8cSThomas Huth * 957f8232c8cSThomas Huth * Checks whether an address is subject to low-address protection and set 958f8232c8cSThomas Huth * up vcpu->arch.pgm accordingly if necessary. 959f8232c8cSThomas Huth * 960f8232c8cSThomas Huth * Return: 0 if no protection exception, or PGM_PROTECTION if protected. 961f8232c8cSThomas Huth */ 962dd9e5b7bSAlexander Yarygin int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra) 963f8232c8cSThomas Huth { 964dd9e5b7bSAlexander Yarygin union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; 965f8232c8cSThomas Huth 966dd9e5b7bSAlexander Yarygin if (!ctlreg0.lap || !is_low_address(gra)) 967f8232c8cSThomas Huth return 0; 9683e3c67f6SDavid Hildenbrand return trans_exc(vcpu, PGM_PROTECTION, gra, 0, GACC_STORE, PROT_TYPE_LA); 969f8232c8cSThomas Huth } 970