Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * guest access functions |
| 3 | * |
| 4 | * Copyright IBM Corp. 2014 |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <linux/vmalloc.h> |
Ingo Molnar | 589ee62 | 2017-02-04 00:16:44 +0100 | [diff] [blame] | 9 | #include <linux/mm_types.h> |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 10 | #include <linux/err.h> |
Ingo Molnar | 589ee62 | 2017-02-04 00:16:44 +0100 | [diff] [blame] | 11 | |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 12 | #include <asm/pgtable.h> |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 13 | #include <asm/gmap.h> |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 14 | #include "kvm-s390.h" |
| 15 | #include "gaccess.h" |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 16 | #include <asm/switch_to.h> |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 17 | |
| 18 | union asce { |
| 19 | unsigned long val; |
| 20 | struct { |
| 21 | unsigned long origin : 52; /* Region- or Segment-Table Origin */ |
| 22 | unsigned long : 2; |
| 23 | unsigned long g : 1; /* Subspace Group Control */ |
| 24 | unsigned long p : 1; /* Private Space Control */ |
| 25 | unsigned long s : 1; /* Storage-Alteration-Event Control */ |
| 26 | unsigned long x : 1; /* Space-Switch-Event Control */ |
| 27 | unsigned long r : 1; /* Real-Space Control */ |
| 28 | unsigned long : 1; |
| 29 | unsigned long dt : 2; /* Designation-Type Control */ |
| 30 | unsigned long tl : 2; /* Region- or Segment-Table Length */ |
| 31 | }; |
| 32 | }; |
| 33 | |
| 34 | enum { |
| 35 | ASCE_TYPE_SEGMENT = 0, |
| 36 | ASCE_TYPE_REGION3 = 1, |
| 37 | ASCE_TYPE_REGION2 = 2, |
| 38 | ASCE_TYPE_REGION1 = 3 |
| 39 | }; |
| 40 | |
| 41 | union region1_table_entry { |
| 42 | unsigned long val; |
| 43 | struct { |
| 44 | unsigned long rto: 52;/* Region-Table Origin */ |
| 45 | unsigned long : 2; |
| 46 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 47 | unsigned long : 1; |
| 48 | unsigned long tf : 2; /* Region-Second-Table Offset */ |
| 49 | unsigned long i : 1; /* Region-Invalid Bit */ |
| 50 | unsigned long : 1; |
| 51 | unsigned long tt : 2; /* Table-Type Bits */ |
| 52 | unsigned long tl : 2; /* Region-Second-Table Length */ |
| 53 | }; |
| 54 | }; |
| 55 | |
| 56 | union region2_table_entry { |
| 57 | unsigned long val; |
| 58 | struct { |
| 59 | unsigned long rto: 52;/* Region-Table Origin */ |
| 60 | unsigned long : 2; |
| 61 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 62 | unsigned long : 1; |
| 63 | unsigned long tf : 2; /* Region-Third-Table Offset */ |
| 64 | unsigned long i : 1; /* Region-Invalid Bit */ |
| 65 | unsigned long : 1; |
| 66 | unsigned long tt : 2; /* Table-Type Bits */ |
| 67 | unsigned long tl : 2; /* Region-Third-Table Length */ |
| 68 | }; |
| 69 | }; |
| 70 | |
| 71 | struct region3_table_entry_fc0 { |
| 72 | unsigned long sto: 52;/* Segment-Table Origin */ |
| 73 | unsigned long : 1; |
| 74 | unsigned long fc : 1; /* Format-Control */ |
| 75 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 76 | unsigned long : 1; |
| 77 | unsigned long tf : 2; /* Segment-Table Offset */ |
| 78 | unsigned long i : 1; /* Region-Invalid Bit */ |
| 79 | unsigned long cr : 1; /* Common-Region Bit */ |
| 80 | unsigned long tt : 2; /* Table-Type Bits */ |
| 81 | unsigned long tl : 2; /* Segment-Table Length */ |
| 82 | }; |
| 83 | |
| 84 | struct region3_table_entry_fc1 { |
| 85 | unsigned long rfaa : 33; /* Region-Frame Absolute Address */ |
| 86 | unsigned long : 14; |
| 87 | unsigned long av : 1; /* ACCF-Validity Control */ |
| 88 | unsigned long acc: 4; /* Access-Control Bits */ |
| 89 | unsigned long f : 1; /* Fetch-Protection Bit */ |
| 90 | unsigned long fc : 1; /* Format-Control */ |
| 91 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 92 | unsigned long co : 1; /* Change-Recording Override */ |
| 93 | unsigned long : 2; |
| 94 | unsigned long i : 1; /* Region-Invalid Bit */ |
| 95 | unsigned long cr : 1; /* Common-Region Bit */ |
| 96 | unsigned long tt : 2; /* Table-Type Bits */ |
| 97 | unsigned long : 2; |
| 98 | }; |
| 99 | |
| 100 | union region3_table_entry { |
| 101 | unsigned long val; |
| 102 | struct region3_table_entry_fc0 fc0; |
| 103 | struct region3_table_entry_fc1 fc1; |
| 104 | struct { |
| 105 | unsigned long : 53; |
| 106 | unsigned long fc : 1; /* Format-Control */ |
| 107 | unsigned long : 4; |
| 108 | unsigned long i : 1; /* Region-Invalid Bit */ |
| 109 | unsigned long cr : 1; /* Common-Region Bit */ |
| 110 | unsigned long tt : 2; /* Table-Type Bits */ |
| 111 | unsigned long : 2; |
| 112 | }; |
| 113 | }; |
| 114 | |
| 115 | struct segment_entry_fc0 { |
| 116 | unsigned long pto: 53;/* Page-Table Origin */ |
| 117 | unsigned long fc : 1; /* Format-Control */ |
| 118 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 119 | unsigned long : 3; |
| 120 | unsigned long i : 1; /* Segment-Invalid Bit */ |
| 121 | unsigned long cs : 1; /* Common-Segment Bit */ |
| 122 | unsigned long tt : 2; /* Table-Type Bits */ |
| 123 | unsigned long : 2; |
| 124 | }; |
| 125 | |
| 126 | struct segment_entry_fc1 { |
| 127 | unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ |
| 128 | unsigned long : 3; |
| 129 | unsigned long av : 1; /* ACCF-Validity Control */ |
| 130 | unsigned long acc: 4; /* Access-Control Bits */ |
| 131 | unsigned long f : 1; /* Fetch-Protection Bit */ |
| 132 | unsigned long fc : 1; /* Format-Control */ |
| 133 | unsigned long p : 1; /* DAT-Protection Bit */ |
| 134 | unsigned long co : 1; /* Change-Recording Override */ |
| 135 | unsigned long : 2; |
| 136 | unsigned long i : 1; /* Segment-Invalid Bit */ |
| 137 | unsigned long cs : 1; /* Common-Segment Bit */ |
| 138 | unsigned long tt : 2; /* Table-Type Bits */ |
| 139 | unsigned long : 2; |
| 140 | }; |
| 141 | |
| 142 | union segment_table_entry { |
| 143 | unsigned long val; |
| 144 | struct segment_entry_fc0 fc0; |
| 145 | struct segment_entry_fc1 fc1; |
| 146 | struct { |
| 147 | unsigned long : 53; |
| 148 | unsigned long fc : 1; /* Format-Control */ |
| 149 | unsigned long : 4; |
| 150 | unsigned long i : 1; /* Segment-Invalid Bit */ |
| 151 | unsigned long cs : 1; /* Common-Segment Bit */ |
| 152 | unsigned long tt : 2; /* Table-Type Bits */ |
| 153 | unsigned long : 2; |
| 154 | }; |
| 155 | }; |
| 156 | |
| 157 | enum { |
| 158 | TABLE_TYPE_SEGMENT = 0, |
| 159 | TABLE_TYPE_REGION3 = 1, |
| 160 | TABLE_TYPE_REGION2 = 2, |
| 161 | TABLE_TYPE_REGION1 = 3 |
| 162 | }; |
| 163 | |
| 164 | union page_table_entry { |
| 165 | unsigned long val; |
| 166 | struct { |
| 167 | unsigned long pfra : 52; /* Page-Frame Real Address */ |
| 168 | unsigned long z : 1; /* Zero Bit */ |
| 169 | unsigned long i : 1; /* Page-Invalid Bit */ |
| 170 | unsigned long p : 1; /* DAT-Protection Bit */ |
Heiko Carstens | 232b8e3 | 2017-03-31 12:50:48 +0200 | [diff] [blame] | 171 | unsigned long : 9; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 172 | }; |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * vaddress union in order to easily decode a virtual address into its |
| 177 | * region first index, region second index etc. parts. |
| 178 | */ |
| 179 | union vaddress { |
| 180 | unsigned long addr; |
| 181 | struct { |
| 182 | unsigned long rfx : 11; |
| 183 | unsigned long rsx : 11; |
| 184 | unsigned long rtx : 11; |
| 185 | unsigned long sx : 11; |
| 186 | unsigned long px : 8; |
| 187 | unsigned long bx : 12; |
| 188 | }; |
| 189 | struct { |
| 190 | unsigned long rfx01 : 2; |
| 191 | unsigned long : 9; |
| 192 | unsigned long rsx01 : 2; |
| 193 | unsigned long : 9; |
| 194 | unsigned long rtx01 : 2; |
| 195 | unsigned long : 9; |
| 196 | unsigned long sx01 : 2; |
| 197 | unsigned long : 29; |
| 198 | }; |
| 199 | }; |
| 200 | |
| 201 | /* |
| 202 | * raddress union which will contain the result (real or absolute address) |
| 203 | * after a page table walk. The rfaa, sfaa and pfra members are used to |
| 204 | * simply assign them the value of a region, segment or page table entry. |
| 205 | */ |
| 206 | union raddress { |
| 207 | unsigned long addr; |
| 208 | unsigned long rfaa : 33; /* Region-Frame Absolute Address */ |
| 209 | unsigned long sfaa : 44; /* Segment-Frame Absolute Address */ |
| 210 | unsigned long pfra : 52; /* Page-Frame Real Address */ |
| 211 | }; |
| 212 | |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 213 | union alet { |
| 214 | u32 val; |
| 215 | struct { |
| 216 | u32 reserved : 7; |
| 217 | u32 p : 1; |
| 218 | u32 alesn : 8; |
| 219 | u32 alen : 16; |
| 220 | }; |
| 221 | }; |
| 222 | |
| 223 | union ald { |
| 224 | u32 val; |
| 225 | struct { |
| 226 | u32 : 1; |
| 227 | u32 alo : 24; |
| 228 | u32 all : 7; |
| 229 | }; |
| 230 | }; |
| 231 | |
| 232 | struct ale { |
| 233 | unsigned long i : 1; /* ALEN-Invalid Bit */ |
| 234 | unsigned long : 5; |
| 235 | unsigned long fo : 1; /* Fetch-Only Bit */ |
| 236 | unsigned long p : 1; /* Private Bit */ |
| 237 | unsigned long alesn : 8; /* Access-List-Entry Sequence Number */ |
| 238 | unsigned long aleax : 16; /* Access-List-Entry Authorization Index */ |
| 239 | unsigned long : 32; |
| 240 | unsigned long : 1; |
| 241 | unsigned long asteo : 25; /* ASN-Second-Table-Entry Origin */ |
| 242 | unsigned long : 6; |
| 243 | unsigned long astesn : 32; /* ASTE Sequence Number */ |
| 244 | } __packed; |
| 245 | |
| 246 | struct aste { |
| 247 | unsigned long i : 1; /* ASX-Invalid Bit */ |
| 248 | unsigned long ato : 29; /* Authority-Table Origin */ |
| 249 | unsigned long : 1; |
| 250 | unsigned long b : 1; /* Base-Space Bit */ |
| 251 | unsigned long ax : 16; /* Authorization Index */ |
| 252 | unsigned long atl : 12; /* Authority-Table Length */ |
| 253 | unsigned long : 2; |
| 254 | unsigned long ca : 1; /* Controlled-ASN Bit */ |
| 255 | unsigned long ra : 1; /* Reusable-ASN Bit */ |
| 256 | unsigned long asce : 64; /* Address-Space-Control Element */ |
| 257 | unsigned long ald : 32; |
| 258 | unsigned long astesn : 32; |
| 259 | /* .. more fields there */ |
| 260 | } __packed; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 261 | |
| 262 | int ipte_lock_held(struct kvm_vcpu *vcpu) |
| 263 | { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 264 | if (vcpu->arch.sie_block->eca & ECA_SII) { |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 265 | int rc; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 266 | |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 267 | read_lock(&vcpu->kvm->arch.sca_lock); |
| 268 | rc = kvm_s390_get_ipte_control(vcpu->kvm)->kh != 0; |
| 269 | read_unlock(&vcpu->kvm->arch.sca_lock); |
| 270 | return rc; |
| 271 | } |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 272 | return vcpu->kvm->arch.ipte_lock_count != 0; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void ipte_lock_simple(struct kvm_vcpu *vcpu) |
| 276 | { |
| 277 | union ipte_control old, new, *ic; |
| 278 | |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 279 | mutex_lock(&vcpu->kvm->arch.ipte_mutex); |
| 280 | vcpu->kvm->arch.ipte_lock_count++; |
| 281 | if (vcpu->kvm->arch.ipte_lock_count > 1) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 282 | goto out; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 283 | retry: |
| 284 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 6051451 | 2015-04-21 14:44:54 +0200 | [diff] [blame] | 285 | ic = kvm_s390_get_ipte_control(vcpu->kvm); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 286 | do { |
Christian Borntraeger | 5de72a2 | 2014-11-25 13:17:34 +0100 | [diff] [blame] | 287 | old = READ_ONCE(*ic); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 288 | if (old.k) { |
| 289 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 290 | cond_resched(); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 291 | goto retry; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 292 | } |
| 293 | new = old; |
| 294 | new.k = 1; |
| 295 | } while (cmpxchg(&ic->val, old.val, new.val) != old.val); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 296 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 297 | out: |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 298 | mutex_unlock(&vcpu->kvm->arch.ipte_mutex); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static void ipte_unlock_simple(struct kvm_vcpu *vcpu) |
| 302 | { |
| 303 | union ipte_control old, new, *ic; |
| 304 | |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 305 | mutex_lock(&vcpu->kvm->arch.ipte_mutex); |
| 306 | vcpu->kvm->arch.ipte_lock_count--; |
| 307 | if (vcpu->kvm->arch.ipte_lock_count) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 308 | goto out; |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 309 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 6051451 | 2015-04-21 14:44:54 +0200 | [diff] [blame] | 310 | ic = kvm_s390_get_ipte_control(vcpu->kvm); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 311 | do { |
Christian Borntraeger | 5de72a2 | 2014-11-25 13:17:34 +0100 | [diff] [blame] | 312 | old = READ_ONCE(*ic); |
Christian Borntraeger | 1365039 | 2014-11-04 08:31:16 +0100 | [diff] [blame] | 313 | new = old; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 314 | new.k = 0; |
| 315 | } while (cmpxchg(&ic->val, old.val, new.val) != old.val); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 316 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Christian Borntraeger | 6b33195 | 2014-09-03 21:17:03 +0200 | [diff] [blame] | 317 | wake_up(&vcpu->kvm->arch.ipte_wq); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 318 | out: |
Thomas Huth | a6b7e45 | 2014-10-01 14:48:42 +0200 | [diff] [blame] | 319 | mutex_unlock(&vcpu->kvm->arch.ipte_mutex); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void ipte_lock_siif(struct kvm_vcpu *vcpu) |
| 323 | { |
| 324 | union ipte_control old, new, *ic; |
| 325 | |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 326 | retry: |
| 327 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 6051451 | 2015-04-21 14:44:54 +0200 | [diff] [blame] | 328 | ic = kvm_s390_get_ipte_control(vcpu->kvm); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 329 | do { |
Christian Borntraeger | 5de72a2 | 2014-11-25 13:17:34 +0100 | [diff] [blame] | 330 | old = READ_ONCE(*ic); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 331 | if (old.kg) { |
| 332 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 333 | cond_resched(); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 334 | goto retry; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 335 | } |
| 336 | new = old; |
| 337 | new.k = 1; |
| 338 | new.kh++; |
| 339 | } while (cmpxchg(&ic->val, old.val, new.val) != old.val); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 340 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void ipte_unlock_siif(struct kvm_vcpu *vcpu) |
| 344 | { |
| 345 | union ipte_control old, new, *ic; |
| 346 | |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 347 | read_lock(&vcpu->kvm->arch.sca_lock); |
Eugene (jno) Dvurechenski | 6051451 | 2015-04-21 14:44:54 +0200 | [diff] [blame] | 348 | ic = kvm_s390_get_ipte_control(vcpu->kvm); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 349 | do { |
Christian Borntraeger | 5de72a2 | 2014-11-25 13:17:34 +0100 | [diff] [blame] | 350 | old = READ_ONCE(*ic); |
Christian Borntraeger | 1365039 | 2014-11-04 08:31:16 +0100 | [diff] [blame] | 351 | new = old; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 352 | new.kh--; |
| 353 | if (!new.kh) |
| 354 | new.k = 0; |
| 355 | } while (cmpxchg(&ic->val, old.val, new.val) != old.val); |
Eugene (jno) Dvurechenski | 5e04431 | 2015-04-22 18:08:39 +0200 | [diff] [blame] | 356 | read_unlock(&vcpu->kvm->arch.sca_lock); |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 357 | if (!new.kh) |
| 358 | wake_up(&vcpu->kvm->arch.ipte_wq); |
| 359 | } |
| 360 | |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 361 | void ipte_lock(struct kvm_vcpu *vcpu) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 362 | { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 363 | if (vcpu->arch.sie_block->eca & ECA_SII) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 364 | ipte_lock_siif(vcpu); |
| 365 | else |
| 366 | ipte_lock_simple(vcpu); |
| 367 | } |
| 368 | |
Thomas Huth | a0465f9 | 2014-02-04 14:48:07 +0100 | [diff] [blame] | 369 | void ipte_unlock(struct kvm_vcpu *vcpu) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 370 | { |
David Hildenbrand | 0c9d868 | 2017-03-13 11:48:28 +0100 | [diff] [blame] | 371 | if (vcpu->arch.sie_block->eca & ECA_SII) |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 372 | ipte_unlock_siif(vcpu); |
| 373 | else |
| 374 | ipte_unlock_simple(vcpu); |
| 375 | } |
| 376 | |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 377 | static int ar_translation(struct kvm_vcpu *vcpu, union asce *asce, u8 ar, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 378 | enum gacc_mode mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 379 | { |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 380 | union alet alet; |
| 381 | struct ale ale; |
| 382 | struct aste aste; |
| 383 | unsigned long ald_addr, authority_table_addr; |
| 384 | union ald ald; |
| 385 | int eax, rc; |
| 386 | u8 authority_table; |
| 387 | |
| 388 | if (ar >= NUM_ACRS) |
| 389 | return -EINVAL; |
| 390 | |
| 391 | save_access_regs(vcpu->run->s.regs.acrs); |
| 392 | alet.val = vcpu->run->s.regs.acrs[ar]; |
| 393 | |
| 394 | if (ar == 0 || alet.val == 0) { |
| 395 | asce->val = vcpu->arch.sie_block->gcr[1]; |
| 396 | return 0; |
| 397 | } else if (alet.val == 1) { |
| 398 | asce->val = vcpu->arch.sie_block->gcr[7]; |
| 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | if (alet.reserved) |
| 403 | return PGM_ALET_SPECIFICATION; |
| 404 | |
| 405 | if (alet.p) |
| 406 | ald_addr = vcpu->arch.sie_block->gcr[5]; |
| 407 | else |
| 408 | ald_addr = vcpu->arch.sie_block->gcr[2]; |
| 409 | ald_addr &= 0x7fffffc0; |
| 410 | |
| 411 | rc = read_guest_real(vcpu, ald_addr + 16, &ald.val, sizeof(union ald)); |
| 412 | if (rc) |
| 413 | return rc; |
| 414 | |
| 415 | if (alet.alen / 8 > ald.all) |
| 416 | return PGM_ALEN_TRANSLATION; |
| 417 | |
| 418 | if (0x7fffffff - ald.alo * 128 < alet.alen * 16) |
| 419 | return PGM_ADDRESSING; |
| 420 | |
| 421 | rc = read_guest_real(vcpu, ald.alo * 128 + alet.alen * 16, &ale, |
| 422 | sizeof(struct ale)); |
| 423 | if (rc) |
| 424 | return rc; |
| 425 | |
| 426 | if (ale.i == 1) |
| 427 | return PGM_ALEN_TRANSLATION; |
| 428 | if (ale.alesn != alet.alesn) |
| 429 | return PGM_ALE_SEQUENCE; |
| 430 | |
| 431 | rc = read_guest_real(vcpu, ale.asteo * 64, &aste, sizeof(struct aste)); |
| 432 | if (rc) |
| 433 | return rc; |
| 434 | |
| 435 | if (aste.i) |
| 436 | return PGM_ASTE_VALIDITY; |
| 437 | if (aste.astesn != ale.astesn) |
| 438 | return PGM_ASTE_SEQUENCE; |
| 439 | |
| 440 | if (ale.p == 1) { |
| 441 | eax = (vcpu->arch.sie_block->gcr[8] >> 16) & 0xffff; |
| 442 | if (ale.aleax != eax) { |
| 443 | if (eax / 16 > aste.atl) |
| 444 | return PGM_EXTENDED_AUTHORITY; |
| 445 | |
| 446 | authority_table_addr = aste.ato * 4 + eax / 4; |
| 447 | |
| 448 | rc = read_guest_real(vcpu, authority_table_addr, |
| 449 | &authority_table, |
| 450 | sizeof(u8)); |
| 451 | if (rc) |
| 452 | return rc; |
| 453 | |
| 454 | if ((authority_table & (0x40 >> ((eax & 3) * 2))) == 0) |
| 455 | return PGM_EXTENDED_AUTHORITY; |
| 456 | } |
| 457 | } |
| 458 | |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 459 | if (ale.fo == 1 && mode == GACC_STORE) |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 460 | return PGM_PROTECTION; |
| 461 | |
| 462 | asce->val = aste.asce; |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | struct trans_exc_code_bits { |
| 467 | unsigned long addr : 52; /* Translation-exception Address */ |
| 468 | unsigned long fsi : 2; /* Access Exception Fetch/Store Indication */ |
Christian Borntraeger | a679c54 | 2016-12-15 15:58:14 +0100 | [diff] [blame] | 469 | unsigned long : 2; |
| 470 | unsigned long b56 : 1; |
| 471 | unsigned long : 3; |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 472 | unsigned long b60 : 1; |
| 473 | unsigned long b61 : 1; |
| 474 | unsigned long as : 2; /* ASCE Identifier */ |
| 475 | }; |
| 476 | |
| 477 | enum { |
| 478 | FSI_UNKNOWN = 0, /* Unknown wether fetch or store */ |
| 479 | FSI_STORE = 1, /* Exception was due to store operation */ |
| 480 | FSI_FETCH = 2 /* Exception was due to fetch operation */ |
| 481 | }; |
| 482 | |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 483 | enum prot_type { |
| 484 | PROT_TYPE_LA = 0, |
| 485 | PROT_TYPE_KEYC = 1, |
| 486 | PROT_TYPE_ALC = 2, |
| 487 | PROT_TYPE_DAT = 3, |
| 488 | }; |
| 489 | |
| 490 | static int trans_exc(struct kvm_vcpu *vcpu, int code, unsigned long gva, |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 491 | u8 ar, enum gacc_mode mode, enum prot_type prot) |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 492 | { |
| 493 | struct kvm_s390_pgm_info *pgm = &vcpu->arch.pgm; |
| 494 | struct trans_exc_code_bits *tec; |
| 495 | |
| 496 | memset(pgm, 0, sizeof(*pgm)); |
| 497 | pgm->code = code; |
| 498 | tec = (struct trans_exc_code_bits *)&pgm->trans_exc_code; |
| 499 | |
| 500 | switch (code) { |
Janosch Frank | c14b88d | 2016-07-29 11:36:04 +0200 | [diff] [blame] | 501 | case PGM_PROTECTION: |
| 502 | switch (prot) { |
Christian Borntraeger | a679c54 | 2016-12-15 15:58:14 +0100 | [diff] [blame] | 503 | case PROT_TYPE_LA: |
| 504 | tec->b56 = 1; |
| 505 | break; |
| 506 | case PROT_TYPE_KEYC: |
| 507 | tec->b60 = 1; |
| 508 | break; |
Janosch Frank | c14b88d | 2016-07-29 11:36:04 +0200 | [diff] [blame] | 509 | case PROT_TYPE_ALC: |
| 510 | tec->b60 = 1; |
| 511 | /* FALL THROUGH */ |
| 512 | case PROT_TYPE_DAT: |
| 513 | tec->b61 = 1; |
| 514 | break; |
Janosch Frank | c14b88d | 2016-07-29 11:36:04 +0200 | [diff] [blame] | 515 | } |
| 516 | /* FALL THROUGH */ |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 517 | case PGM_ASCE_TYPE: |
| 518 | case PGM_PAGE_TRANSLATION: |
| 519 | case PGM_REGION_FIRST_TRANS: |
| 520 | case PGM_REGION_SECOND_TRANS: |
| 521 | case PGM_REGION_THIRD_TRANS: |
| 522 | case PGM_SEGMENT_TRANSLATION: |
| 523 | /* |
| 524 | * op_access_id only applies to MOVE_PAGE -> set bit 61 |
| 525 | * exc_access_id has to be set to 0 for some instructions. Both |
Janosch Frank | c14b88d | 2016-07-29 11:36:04 +0200 | [diff] [blame] | 526 | * cases have to be handled by the caller. |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 527 | */ |
| 528 | tec->addr = gva >> PAGE_SHIFT; |
| 529 | tec->fsi = mode == GACC_STORE ? FSI_STORE : FSI_FETCH; |
| 530 | tec->as = psw_bits(vcpu->arch.sie_block->gpsw).as; |
| 531 | /* FALL THROUGH */ |
| 532 | case PGM_ALEN_TRANSLATION: |
| 533 | case PGM_ALE_SEQUENCE: |
| 534 | case PGM_ASTE_VALIDITY: |
| 535 | case PGM_ASTE_SEQUENCE: |
| 536 | case PGM_EXTENDED_AUTHORITY: |
Janosch Frank | c14b88d | 2016-07-29 11:36:04 +0200 | [diff] [blame] | 537 | /* |
| 538 | * We can always store exc_access_id, as it is |
| 539 | * undefined for non-ar cases. It is undefined for |
| 540 | * most DAT protection exceptions. |
| 541 | */ |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 542 | pgm->exc_access_id = ar; |
| 543 | break; |
David Hildenbrand | d03193d | 2016-05-31 19:56:46 +0200 | [diff] [blame] | 544 | } |
| 545 | return code; |
| 546 | } |
| 547 | |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 548 | static int get_vcpu_asce(struct kvm_vcpu *vcpu, union asce *asce, |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 549 | unsigned long ga, u8 ar, enum gacc_mode mode) |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 550 | { |
| 551 | int rc; |
David Hildenbrand | 34346b9 | 2015-11-16 15:48:59 +0100 | [diff] [blame] | 552 | struct psw_bits psw = psw_bits(vcpu->arch.sie_block->gpsw); |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 553 | |
David Hildenbrand | 34346b9 | 2015-11-16 15:48:59 +0100 | [diff] [blame] | 554 | if (!psw.t) { |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 555 | asce->val = 0; |
| 556 | asce->r = 1; |
| 557 | return 0; |
| 558 | } |
| 559 | |
David Hildenbrand | 34346b9 | 2015-11-16 15:48:59 +0100 | [diff] [blame] | 560 | if (mode == GACC_IFETCH) |
| 561 | psw.as = psw.as == PSW_AS_HOME ? PSW_AS_HOME : PSW_AS_PRIMARY; |
| 562 | |
| 563 | switch (psw.as) { |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 564 | case PSW_AS_PRIMARY: |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 565 | asce->val = vcpu->arch.sie_block->gcr[1]; |
| 566 | return 0; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 567 | case PSW_AS_SECONDARY: |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 568 | asce->val = vcpu->arch.sie_block->gcr[7]; |
| 569 | return 0; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 570 | case PSW_AS_HOME: |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 571 | asce->val = vcpu->arch.sie_block->gcr[13]; |
| 572 | return 0; |
| 573 | case PSW_AS_ACCREG: |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 574 | rc = ar_translation(vcpu, asce, ar, mode); |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 575 | if (rc > 0) |
David Hildenbrand | bcfa01d7 | 2016-05-31 20:21:03 +0200 | [diff] [blame] | 576 | return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_ALC); |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 577 | return rc; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 578 | } |
| 579 | return 0; |
| 580 | } |
| 581 | |
| 582 | static int deref_table(struct kvm *kvm, unsigned long gpa, unsigned long *val) |
| 583 | { |
| 584 | return kvm_read_guest(kvm, gpa, val, sizeof(*val)); |
| 585 | } |
| 586 | |
| 587 | /** |
| 588 | * guest_translate - translate a guest virtual into a guest absolute address |
| 589 | * @vcpu: virtual cpu |
| 590 | * @gva: guest virtual address |
| 591 | * @gpa: points to where guest physical (absolute) address should be stored |
Alexander Yarygin | 75a1812 | 2015-01-22 12:44:11 +0300 | [diff] [blame] | 592 | * @asce: effective asce |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 593 | * @mode: indicates the access mode to be used |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 594 | * |
| 595 | * Translate a guest virtual address into a guest absolute address by means |
Yannick Guerrini | 16b0fc1 | 2015-02-26 23:16:44 +0100 | [diff] [blame] | 596 | * of dynamic address translation as specified by the architecture. |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 597 | * If the resulting absolute address is not available in the configuration |
| 598 | * an addressing exception is indicated and @gpa will not be changed. |
| 599 | * |
| 600 | * Returns: - zero on success; @gpa contains the resulting absolute address |
| 601 | * - a negative value if guest access failed due to e.g. broken |
| 602 | * guest mapping |
| 603 | * - a positve value if an access exception happened. In this case |
| 604 | * the returned value is the program interruption code as defined |
| 605 | * by the architecture |
| 606 | */ |
| 607 | static unsigned long guest_translate(struct kvm_vcpu *vcpu, unsigned long gva, |
Alexander Yarygin | 75a1812 | 2015-01-22 12:44:11 +0300 | [diff] [blame] | 608 | unsigned long *gpa, const union asce asce, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 609 | enum gacc_mode mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 610 | { |
| 611 | union vaddress vaddr = {.addr = gva}; |
| 612 | union raddress raddr = {.addr = gva}; |
| 613 | union page_table_entry pte; |
| 614 | int dat_protection = 0; |
| 615 | union ctlreg0 ctlreg0; |
| 616 | unsigned long ptr; |
| 617 | int edat1, edat2; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 618 | |
| 619 | ctlreg0.val = vcpu->arch.sie_block->gcr[0]; |
Michael Mueller | 9d8d578 | 2015-02-02 15:42:51 +0100 | [diff] [blame] | 620 | edat1 = ctlreg0.edat && test_kvm_facility(vcpu->kvm, 8); |
| 621 | edat2 = edat1 && test_kvm_facility(vcpu->kvm, 78); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 622 | if (asce.r) |
| 623 | goto real_address; |
| 624 | ptr = asce.origin * 4096; |
| 625 | switch (asce.dt) { |
| 626 | case ASCE_TYPE_REGION1: |
| 627 | if (vaddr.rfx01 > asce.tl) |
| 628 | return PGM_REGION_FIRST_TRANS; |
| 629 | ptr += vaddr.rfx * 8; |
| 630 | break; |
| 631 | case ASCE_TYPE_REGION2: |
| 632 | if (vaddr.rfx) |
| 633 | return PGM_ASCE_TYPE; |
| 634 | if (vaddr.rsx01 > asce.tl) |
| 635 | return PGM_REGION_SECOND_TRANS; |
| 636 | ptr += vaddr.rsx * 8; |
| 637 | break; |
| 638 | case ASCE_TYPE_REGION3: |
| 639 | if (vaddr.rfx || vaddr.rsx) |
| 640 | return PGM_ASCE_TYPE; |
| 641 | if (vaddr.rtx01 > asce.tl) |
| 642 | return PGM_REGION_THIRD_TRANS; |
| 643 | ptr += vaddr.rtx * 8; |
| 644 | break; |
| 645 | case ASCE_TYPE_SEGMENT: |
| 646 | if (vaddr.rfx || vaddr.rsx || vaddr.rtx) |
| 647 | return PGM_ASCE_TYPE; |
| 648 | if (vaddr.sx01 > asce.tl) |
| 649 | return PGM_SEGMENT_TRANSLATION; |
| 650 | ptr += vaddr.sx * 8; |
| 651 | break; |
| 652 | } |
| 653 | switch (asce.dt) { |
| 654 | case ASCE_TYPE_REGION1: { |
| 655 | union region1_table_entry rfte; |
| 656 | |
| 657 | if (kvm_is_error_gpa(vcpu->kvm, ptr)) |
| 658 | return PGM_ADDRESSING; |
| 659 | if (deref_table(vcpu->kvm, ptr, &rfte.val)) |
| 660 | return -EFAULT; |
| 661 | if (rfte.i) |
| 662 | return PGM_REGION_FIRST_TRANS; |
| 663 | if (rfte.tt != TABLE_TYPE_REGION1) |
| 664 | return PGM_TRANSLATION_SPEC; |
| 665 | if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl) |
| 666 | return PGM_REGION_SECOND_TRANS; |
| 667 | if (edat1) |
| 668 | dat_protection |= rfte.p; |
| 669 | ptr = rfte.rto * 4096 + vaddr.rsx * 8; |
| 670 | } |
| 671 | /* fallthrough */ |
| 672 | case ASCE_TYPE_REGION2: { |
| 673 | union region2_table_entry rste; |
| 674 | |
| 675 | if (kvm_is_error_gpa(vcpu->kvm, ptr)) |
| 676 | return PGM_ADDRESSING; |
| 677 | if (deref_table(vcpu->kvm, ptr, &rste.val)) |
| 678 | return -EFAULT; |
| 679 | if (rste.i) |
| 680 | return PGM_REGION_SECOND_TRANS; |
| 681 | if (rste.tt != TABLE_TYPE_REGION2) |
| 682 | return PGM_TRANSLATION_SPEC; |
| 683 | if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl) |
| 684 | return PGM_REGION_THIRD_TRANS; |
| 685 | if (edat1) |
| 686 | dat_protection |= rste.p; |
| 687 | ptr = rste.rto * 4096 + vaddr.rtx * 8; |
| 688 | } |
| 689 | /* fallthrough */ |
| 690 | case ASCE_TYPE_REGION3: { |
| 691 | union region3_table_entry rtte; |
| 692 | |
| 693 | if (kvm_is_error_gpa(vcpu->kvm, ptr)) |
| 694 | return PGM_ADDRESSING; |
| 695 | if (deref_table(vcpu->kvm, ptr, &rtte.val)) |
| 696 | return -EFAULT; |
| 697 | if (rtte.i) |
| 698 | return PGM_REGION_THIRD_TRANS; |
| 699 | if (rtte.tt != TABLE_TYPE_REGION3) |
| 700 | return PGM_TRANSLATION_SPEC; |
| 701 | if (rtte.cr && asce.p && edat2) |
| 702 | return PGM_TRANSLATION_SPEC; |
| 703 | if (rtte.fc && edat2) { |
| 704 | dat_protection |= rtte.fc1.p; |
| 705 | raddr.rfaa = rtte.fc1.rfaa; |
| 706 | goto absolute_address; |
| 707 | } |
| 708 | if (vaddr.sx01 < rtte.fc0.tf) |
| 709 | return PGM_SEGMENT_TRANSLATION; |
| 710 | if (vaddr.sx01 > rtte.fc0.tl) |
| 711 | return PGM_SEGMENT_TRANSLATION; |
| 712 | if (edat1) |
| 713 | dat_protection |= rtte.fc0.p; |
| 714 | ptr = rtte.fc0.sto * 4096 + vaddr.sx * 8; |
| 715 | } |
| 716 | /* fallthrough */ |
| 717 | case ASCE_TYPE_SEGMENT: { |
| 718 | union segment_table_entry ste; |
| 719 | |
| 720 | if (kvm_is_error_gpa(vcpu->kvm, ptr)) |
| 721 | return PGM_ADDRESSING; |
| 722 | if (deref_table(vcpu->kvm, ptr, &ste.val)) |
| 723 | return -EFAULT; |
| 724 | if (ste.i) |
| 725 | return PGM_SEGMENT_TRANSLATION; |
| 726 | if (ste.tt != TABLE_TYPE_SEGMENT) |
| 727 | return PGM_TRANSLATION_SPEC; |
| 728 | if (ste.cs && asce.p) |
| 729 | return PGM_TRANSLATION_SPEC; |
| 730 | if (ste.fc && edat1) { |
| 731 | dat_protection |= ste.fc1.p; |
| 732 | raddr.sfaa = ste.fc1.sfaa; |
| 733 | goto absolute_address; |
| 734 | } |
| 735 | dat_protection |= ste.fc0.p; |
| 736 | ptr = ste.fc0.pto * 2048 + vaddr.px * 8; |
| 737 | } |
| 738 | } |
| 739 | if (kvm_is_error_gpa(vcpu->kvm, ptr)) |
| 740 | return PGM_ADDRESSING; |
| 741 | if (deref_table(vcpu->kvm, ptr, &pte.val)) |
| 742 | return -EFAULT; |
| 743 | if (pte.i) |
| 744 | return PGM_PAGE_TRANSLATION; |
| 745 | if (pte.z) |
| 746 | return PGM_TRANSLATION_SPEC; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 747 | dat_protection |= pte.p; |
| 748 | raddr.pfra = pte.pfra; |
| 749 | real_address: |
| 750 | raddr.addr = kvm_s390_real_to_abs(vcpu, raddr.addr); |
| 751 | absolute_address: |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 752 | if (mode == GACC_STORE && dat_protection) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 753 | return PGM_PROTECTION; |
| 754 | if (kvm_is_error_gpa(vcpu->kvm, raddr.addr)) |
| 755 | return PGM_ADDRESSING; |
| 756 | *gpa = raddr.addr; |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | static inline int is_low_address(unsigned long ga) |
| 761 | { |
| 762 | /* Check for address ranges 0..511 and 4096..4607 */ |
| 763 | return (ga & ~0x11fful) == 0; |
| 764 | } |
| 765 | |
Alexander Yarygin | 75a1812 | 2015-01-22 12:44:11 +0300 | [diff] [blame] | 766 | static int low_address_protection_enabled(struct kvm_vcpu *vcpu, |
| 767 | const union asce asce) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 768 | { |
| 769 | union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; |
| 770 | psw_t *psw = &vcpu->arch.sie_block->gpsw; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 771 | |
| 772 | if (!ctlreg0.lap) |
| 773 | return 0; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 774 | if (psw_bits(*psw).t && asce.p) |
| 775 | return 0; |
| 776 | return 1; |
| 777 | } |
| 778 | |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 779 | static int guest_page_range(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 780 | unsigned long *pages, unsigned long nr_pages, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 781 | const union asce asce, enum gacc_mode mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 782 | { |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 783 | psw_t *psw = &vcpu->arch.sie_block->gpsw; |
David Hildenbrand | cde0dcf | 2016-05-31 20:13:35 +0200 | [diff] [blame] | 784 | int lap_enabled, rc = 0; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 785 | |
Alexander Yarygin | 75a1812 | 2015-01-22 12:44:11 +0300 | [diff] [blame] | 786 | lap_enabled = low_address_protection_enabled(vcpu, asce); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 787 | while (nr_pages) { |
| 788 | ga = kvm_s390_logical_to_effective(vcpu, ga); |
David Hildenbrand | cde0dcf | 2016-05-31 20:13:35 +0200 | [diff] [blame] | 789 | if (mode == GACC_STORE && lap_enabled && is_low_address(ga)) |
| 790 | return trans_exc(vcpu, PGM_PROTECTION, ga, ar, mode, |
| 791 | PROT_TYPE_LA); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 792 | ga &= PAGE_MASK; |
| 793 | if (psw_bits(*psw).t) { |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 794 | rc = guest_translate(vcpu, ga, pages, asce, mode); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 795 | if (rc < 0) |
| 796 | return rc; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 797 | } else { |
| 798 | *pages = kvm_s390_real_to_abs(vcpu, ga); |
| 799 | if (kvm_is_error_gpa(vcpu->kvm, *pages)) |
David Hildenbrand | cde0dcf | 2016-05-31 20:13:35 +0200 | [diff] [blame] | 800 | rc = PGM_ADDRESSING; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 801 | } |
David Hildenbrand | cde0dcf | 2016-05-31 20:13:35 +0200 | [diff] [blame] | 802 | if (rc) |
| 803 | return trans_exc(vcpu, rc, ga, ar, mode, PROT_TYPE_DAT); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 804 | ga += PAGE_SIZE; |
| 805 | pages++; |
| 806 | nr_pages--; |
| 807 | } |
| 808 | return 0; |
| 809 | } |
| 810 | |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 811 | int access_guest(struct kvm_vcpu *vcpu, unsigned long ga, u8 ar, void *data, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 812 | unsigned long len, enum gacc_mode mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 813 | { |
| 814 | psw_t *psw = &vcpu->arch.sie_block->gpsw; |
| 815 | unsigned long _len, nr_pages, gpa, idx; |
| 816 | unsigned long pages_array[2]; |
| 817 | unsigned long *pages; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 818 | int need_ipte_lock; |
| 819 | union asce asce; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 820 | int rc; |
| 821 | |
| 822 | if (!len) |
| 823 | return 0; |
David Hildenbrand | 6167375 | 2016-05-31 19:44:10 +0200 | [diff] [blame] | 824 | ga = kvm_s390_logical_to_effective(vcpu, ga); |
| 825 | rc = get_vcpu_asce(vcpu, &asce, ga, ar, mode); |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 826 | if (rc) |
| 827 | return rc; |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 828 | nr_pages = (((ga & ~PAGE_MASK) + len - 1) >> PAGE_SHIFT) + 1; |
| 829 | pages = pages_array; |
| 830 | if (nr_pages > ARRAY_SIZE(pages_array)) |
| 831 | pages = vmalloc(nr_pages * sizeof(unsigned long)); |
| 832 | if (!pages) |
| 833 | return -ENOMEM; |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 834 | need_ipte_lock = psw_bits(*psw).t && !asce.r; |
| 835 | if (need_ipte_lock) |
| 836 | ipte_lock(vcpu); |
David Hildenbrand | cde0dcf | 2016-05-31 20:13:35 +0200 | [diff] [blame] | 837 | rc = guest_page_range(vcpu, ga, ar, pages, nr_pages, asce, mode); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 838 | for (idx = 0; idx < nr_pages && !rc; idx++) { |
| 839 | gpa = *(pages + idx) + (ga & ~PAGE_MASK); |
| 840 | _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 841 | if (mode == GACC_STORE) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 842 | rc = kvm_write_guest(vcpu->kvm, gpa, data, _len); |
| 843 | else |
| 844 | rc = kvm_read_guest(vcpu->kvm, gpa, data, _len); |
| 845 | len -= _len; |
| 846 | ga += _len; |
| 847 | data += _len; |
| 848 | } |
Heiko Carstens | 8a242234 | 2014-01-10 14:33:28 +0100 | [diff] [blame] | 849 | if (need_ipte_lock) |
| 850 | ipte_unlock(vcpu); |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 851 | if (nr_pages > ARRAY_SIZE(pages_array)) |
| 852 | vfree(pages); |
| 853 | return rc; |
| 854 | } |
| 855 | |
| 856 | int access_guest_real(struct kvm_vcpu *vcpu, unsigned long gra, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 857 | void *data, unsigned long len, enum gacc_mode mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 858 | { |
| 859 | unsigned long _len, gpa; |
| 860 | int rc = 0; |
| 861 | |
| 862 | while (len && !rc) { |
| 863 | gpa = kvm_s390_real_to_abs(vcpu, gra); |
| 864 | _len = min(PAGE_SIZE - (gpa & ~PAGE_MASK), len); |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 865 | if (mode) |
Heiko Carstens | 2293897 | 2014-01-01 16:26:52 +0100 | [diff] [blame] | 866 | rc = write_guest_abs(vcpu, gpa, data, _len); |
| 867 | else |
| 868 | rc = read_guest_abs(vcpu, gpa, data, _len); |
| 869 | len -= _len; |
| 870 | gra += _len; |
| 871 | data += _len; |
| 872 | } |
| 873 | return rc; |
| 874 | } |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 875 | |
| 876 | /** |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 877 | * guest_translate_address - translate guest logical into guest absolute address |
| 878 | * |
| 879 | * Parameter semantics are the same as the ones from guest_translate. |
| 880 | * The memory contents at the guest address are not changed. |
| 881 | * |
| 882 | * Note: The IPTE lock is not taken during this function, so the caller |
| 883 | * has to take care of this. |
| 884 | */ |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 885 | int guest_translate_address(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 886 | unsigned long *gpa, enum gacc_mode mode) |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 887 | { |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 888 | psw_t *psw = &vcpu->arch.sie_block->gpsw; |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 889 | union asce asce; |
| 890 | int rc; |
| 891 | |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 892 | gva = kvm_s390_logical_to_effective(vcpu, gva); |
David Hildenbrand | 6167375 | 2016-05-31 19:44:10 +0200 | [diff] [blame] | 893 | rc = get_vcpu_asce(vcpu, &asce, gva, ar, mode); |
Alexander Yarygin | 664b497 | 2015-03-09 14:17:25 +0300 | [diff] [blame] | 894 | if (rc) |
| 895 | return rc; |
Alexander Yarygin | 75a1812 | 2015-01-22 12:44:11 +0300 | [diff] [blame] | 896 | if (is_low_address(gva) && low_address_protection_enabled(vcpu, asce)) { |
David Hildenbrand | fbcb7d5 | 2016-05-31 20:06:55 +0200 | [diff] [blame] | 897 | if (mode == GACC_STORE) |
| 898 | return trans_exc(vcpu, PGM_PROTECTION, gva, 0, |
| 899 | mode, PROT_TYPE_LA); |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 900 | } |
| 901 | |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 902 | if (psw_bits(*psw).t && !asce.r) { /* Use DAT? */ |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 903 | rc = guest_translate(vcpu, gva, gpa, asce, mode); |
David Hildenbrand | fbcb7d5 | 2016-05-31 20:06:55 +0200 | [diff] [blame] | 904 | if (rc > 0) |
| 905 | return trans_exc(vcpu, rc, gva, 0, mode, PROT_TYPE_DAT); |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 906 | } else { |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 907 | *gpa = kvm_s390_real_to_abs(vcpu, gva); |
| 908 | if (kvm_is_error_gpa(vcpu->kvm, *gpa)) |
David Hildenbrand | fbcb7d5 | 2016-05-31 20:06:55 +0200 | [diff] [blame] | 909 | return trans_exc(vcpu, rc, gva, PGM_ADDRESSING, mode, 0); |
Thomas Huth | 9fbc027 | 2014-02-04 14:43:25 +0100 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | return rc; |
| 913 | } |
| 914 | |
| 915 | /** |
Thomas Huth | 41408c2 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 916 | * check_gva_range - test a range of guest virtual addresses for accessibility |
| 917 | */ |
Christian Borntraeger | 27f67f8 | 2016-12-09 12:44:40 +0100 | [diff] [blame] | 918 | int check_gva_range(struct kvm_vcpu *vcpu, unsigned long gva, u8 ar, |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 919 | unsigned long length, enum gacc_mode mode) |
Thomas Huth | 41408c2 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 920 | { |
| 921 | unsigned long gpa; |
| 922 | unsigned long currlen; |
| 923 | int rc = 0; |
| 924 | |
| 925 | ipte_lock(vcpu); |
| 926 | while (length > 0 && !rc) { |
| 927 | currlen = min(length, PAGE_SIZE - (gva % PAGE_SIZE)); |
David Hildenbrand | 92c9632 | 2015-11-16 15:42:11 +0100 | [diff] [blame] | 928 | rc = guest_translate_address(vcpu, gva, ar, &gpa, mode); |
Thomas Huth | 41408c2 | 2015-02-06 15:01:21 +0100 | [diff] [blame] | 929 | gva += currlen; |
| 930 | length -= currlen; |
| 931 | } |
| 932 | ipte_unlock(vcpu); |
| 933 | |
| 934 | return rc; |
| 935 | } |
| 936 | |
| 937 | /** |
Alexander Yarygin | dd9e5b7 | 2015-03-03 14:26:14 +0300 | [diff] [blame] | 938 | * kvm_s390_check_low_addr_prot_real - check for low-address protection |
| 939 | * @gra: Guest real address |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 940 | * |
| 941 | * Checks whether an address is subject to low-address protection and set |
| 942 | * up vcpu->arch.pgm accordingly if necessary. |
| 943 | * |
| 944 | * Return: 0 if no protection exception, or PGM_PROTECTION if protected. |
| 945 | */ |
Alexander Yarygin | dd9e5b7 | 2015-03-03 14:26:14 +0300 | [diff] [blame] | 946 | int kvm_s390_check_low_addr_prot_real(struct kvm_vcpu *vcpu, unsigned long gra) |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 947 | { |
Alexander Yarygin | dd9e5b7 | 2015-03-03 14:26:14 +0300 | [diff] [blame] | 948 | union ctlreg0 ctlreg0 = {.val = vcpu->arch.sie_block->gcr[0]}; |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 949 | |
Alexander Yarygin | dd9e5b7 | 2015-03-03 14:26:14 +0300 | [diff] [blame] | 950 | if (!ctlreg0.lap || !is_low_address(gra)) |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 951 | return 0; |
David Hildenbrand | 3e3c67f | 2016-05-31 20:00:33 +0200 | [diff] [blame] | 952 | return trans_exc(vcpu, PGM_PROTECTION, gra, 0, GACC_STORE, PROT_TYPE_LA); |
Thomas Huth | f8232c8 | 2014-03-03 23:34:42 +0100 | [diff] [blame] | 953 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 954 | |
| 955 | /** |
| 956 | * kvm_s390_shadow_tables - walk the guest page table and create shadow tables |
| 957 | * @sg: pointer to the shadow guest address space structure |
| 958 | * @saddr: faulting address in the shadow gmap |
| 959 | * @pgt: pointer to the page table address result |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 960 | * @fake: pgt references contiguous guest memory block, not a pgtable |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 961 | */ |
| 962 | static int kvm_s390_shadow_tables(struct gmap *sg, unsigned long saddr, |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 963 | unsigned long *pgt, int *dat_protection, |
| 964 | int *fake) |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 965 | { |
| 966 | struct gmap *parent; |
| 967 | union asce asce; |
| 968 | union vaddress vaddr; |
| 969 | unsigned long ptr; |
| 970 | int rc; |
| 971 | |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 972 | *fake = 0; |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 973 | *dat_protection = 0; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 974 | parent = sg->parent; |
| 975 | vaddr.addr = saddr; |
| 976 | asce.val = sg->orig_asce; |
| 977 | ptr = asce.origin * 4096; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 978 | if (asce.r) { |
| 979 | *fake = 1; |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 980 | ptr = 0; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 981 | asce.dt = ASCE_TYPE_REGION1; |
| 982 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 983 | switch (asce.dt) { |
| 984 | case ASCE_TYPE_REGION1: |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 985 | if (vaddr.rfx01 > asce.tl && !*fake) |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 986 | return PGM_REGION_FIRST_TRANS; |
| 987 | break; |
| 988 | case ASCE_TYPE_REGION2: |
| 989 | if (vaddr.rfx) |
| 990 | return PGM_ASCE_TYPE; |
| 991 | if (vaddr.rsx01 > asce.tl) |
| 992 | return PGM_REGION_SECOND_TRANS; |
| 993 | break; |
| 994 | case ASCE_TYPE_REGION3: |
| 995 | if (vaddr.rfx || vaddr.rsx) |
| 996 | return PGM_ASCE_TYPE; |
| 997 | if (vaddr.rtx01 > asce.tl) |
| 998 | return PGM_REGION_THIRD_TRANS; |
| 999 | break; |
| 1000 | case ASCE_TYPE_SEGMENT: |
| 1001 | if (vaddr.rfx || vaddr.rsx || vaddr.rtx) |
| 1002 | return PGM_ASCE_TYPE; |
| 1003 | if (vaddr.sx01 > asce.tl) |
| 1004 | return PGM_SEGMENT_TRANSLATION; |
| 1005 | break; |
| 1006 | } |
| 1007 | |
| 1008 | switch (asce.dt) { |
| 1009 | case ASCE_TYPE_REGION1: { |
| 1010 | union region1_table_entry rfte; |
| 1011 | |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1012 | if (*fake) { |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 1013 | ptr += (unsigned long) vaddr.rfx << 53; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1014 | rfte.val = ptr; |
| 1015 | goto shadow_r2t; |
| 1016 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1017 | rc = gmap_read_table(parent, ptr + vaddr.rfx * 8, &rfte.val); |
| 1018 | if (rc) |
| 1019 | return rc; |
| 1020 | if (rfte.i) |
| 1021 | return PGM_REGION_FIRST_TRANS; |
| 1022 | if (rfte.tt != TABLE_TYPE_REGION1) |
| 1023 | return PGM_TRANSLATION_SPEC; |
| 1024 | if (vaddr.rsx01 < rfte.tf || vaddr.rsx01 > rfte.tl) |
| 1025 | return PGM_REGION_SECOND_TRANS; |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1026 | if (sg->edat_level >= 1) |
| 1027 | *dat_protection |= rfte.p; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1028 | ptr = rfte.rto << 12UL; |
| 1029 | shadow_r2t: |
| 1030 | rc = gmap_shadow_r2t(sg, saddr, rfte.val, *fake); |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1031 | if (rc) |
| 1032 | return rc; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1033 | /* fallthrough */ |
| 1034 | } |
| 1035 | case ASCE_TYPE_REGION2: { |
| 1036 | union region2_table_entry rste; |
| 1037 | |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1038 | if (*fake) { |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 1039 | ptr += (unsigned long) vaddr.rsx << 42; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1040 | rste.val = ptr; |
| 1041 | goto shadow_r3t; |
| 1042 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1043 | rc = gmap_read_table(parent, ptr + vaddr.rsx * 8, &rste.val); |
| 1044 | if (rc) |
| 1045 | return rc; |
| 1046 | if (rste.i) |
| 1047 | return PGM_REGION_SECOND_TRANS; |
| 1048 | if (rste.tt != TABLE_TYPE_REGION2) |
| 1049 | return PGM_TRANSLATION_SPEC; |
| 1050 | if (vaddr.rtx01 < rste.tf || vaddr.rtx01 > rste.tl) |
| 1051 | return PGM_REGION_THIRD_TRANS; |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1052 | if (sg->edat_level >= 1) |
| 1053 | *dat_protection |= rste.p; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1054 | ptr = rste.rto << 12UL; |
| 1055 | shadow_r3t: |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1056 | rste.p |= *dat_protection; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1057 | rc = gmap_shadow_r3t(sg, saddr, rste.val, *fake); |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1058 | if (rc) |
| 1059 | return rc; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1060 | /* fallthrough */ |
| 1061 | } |
| 1062 | case ASCE_TYPE_REGION3: { |
| 1063 | union region3_table_entry rtte; |
| 1064 | |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1065 | if (*fake) { |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 1066 | ptr += (unsigned long) vaddr.rtx << 31; |
David Hildenbrand | 3218f70 | 2016-04-18 16:22:24 +0200 | [diff] [blame] | 1067 | rtte.val = ptr; |
| 1068 | goto shadow_sgt; |
| 1069 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1070 | rc = gmap_read_table(parent, ptr + vaddr.rtx * 8, &rtte.val); |
| 1071 | if (rc) |
| 1072 | return rc; |
| 1073 | if (rtte.i) |
| 1074 | return PGM_REGION_THIRD_TRANS; |
| 1075 | if (rtte.tt != TABLE_TYPE_REGION3) |
| 1076 | return PGM_TRANSLATION_SPEC; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1077 | if (rtte.cr && asce.p && sg->edat_level >= 2) |
| 1078 | return PGM_TRANSLATION_SPEC; |
| 1079 | if (rtte.fc && sg->edat_level >= 2) { |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1080 | *dat_protection |= rtte.fc0.p; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1081 | *fake = 1; |
| 1082 | ptr = rtte.fc1.rfaa << 31UL; |
| 1083 | rtte.val = ptr; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1084 | goto shadow_sgt; |
| 1085 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1086 | if (vaddr.sx01 < rtte.fc0.tf || vaddr.sx01 > rtte.fc0.tl) |
| 1087 | return PGM_SEGMENT_TRANSLATION; |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1088 | if (sg->edat_level >= 1) |
| 1089 | *dat_protection |= rtte.fc0.p; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1090 | ptr = rtte.fc0.sto << 12UL; |
| 1091 | shadow_sgt: |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1092 | rtte.fc0.p |= *dat_protection; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1093 | rc = gmap_shadow_sgt(sg, saddr, rtte.val, *fake); |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1094 | if (rc) |
| 1095 | return rc; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1096 | /* fallthrough */ |
| 1097 | } |
| 1098 | case ASCE_TYPE_SEGMENT: { |
| 1099 | union segment_table_entry ste; |
| 1100 | |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1101 | if (*fake) { |
Heiko Carstens | addb63c | 2017-06-19 08:02:28 +0200 | [diff] [blame] | 1102 | ptr += (unsigned long) vaddr.sx << 20; |
David Hildenbrand | 18b8980 | 2016-04-18 13:42:05 +0200 | [diff] [blame] | 1103 | ste.val = ptr; |
| 1104 | goto shadow_pgt; |
| 1105 | } |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1106 | rc = gmap_read_table(parent, ptr + vaddr.sx * 8, &ste.val); |
| 1107 | if (rc) |
| 1108 | return rc; |
| 1109 | if (ste.i) |
| 1110 | return PGM_SEGMENT_TRANSLATION; |
| 1111 | if (ste.tt != TABLE_TYPE_SEGMENT) |
| 1112 | return PGM_TRANSLATION_SPEC; |
| 1113 | if (ste.cs && asce.p) |
| 1114 | return PGM_TRANSLATION_SPEC; |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1115 | *dat_protection |= ste.fc0.p; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1116 | if (ste.fc && sg->edat_level >= 1) { |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1117 | *fake = 1; |
| 1118 | ptr = ste.fc1.sfaa << 20UL; |
| 1119 | ste.val = ptr; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1120 | goto shadow_pgt; |
| 1121 | } |
| 1122 | ptr = ste.fc0.pto << 11UL; |
| 1123 | shadow_pgt: |
David Hildenbrand | 1c65781 | 2016-04-18 17:46:21 +0200 | [diff] [blame] | 1124 | ste.fc0.p |= *dat_protection; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1125 | rc = gmap_shadow_pgt(sg, saddr, ste.val, *fake); |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1126 | if (rc) |
| 1127 | return rc; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | /* Return the parent address of the page table */ |
| 1131 | *pgt = ptr; |
| 1132 | return 0; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * kvm_s390_shadow_fault - handle fault on a shadow page table |
David Hildenbrand | f4debb4 | 2016-01-27 17:24:03 +0100 | [diff] [blame] | 1137 | * @vcpu: virtual cpu |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1138 | * @sg: pointer to the shadow guest address space structure |
| 1139 | * @saddr: faulting address in the shadow gmap |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1140 | * |
| 1141 | * Returns: - 0 if the shadow fault was successfully resolved |
| 1142 | * - > 0 (pgm exception code) on exceptions while faulting |
| 1143 | * - -EAGAIN if the caller can retry immediately |
| 1144 | * - -EFAULT when accessing invalid guest addresses |
| 1145 | * - -ENOMEM if out of memory |
| 1146 | */ |
David Hildenbrand | f4debb4 | 2016-01-27 17:24:03 +0100 | [diff] [blame] | 1147 | int kvm_s390_shadow_fault(struct kvm_vcpu *vcpu, struct gmap *sg, |
| 1148 | unsigned long saddr) |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1149 | { |
| 1150 | union vaddress vaddr; |
| 1151 | union page_table_entry pte; |
| 1152 | unsigned long pgt; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1153 | int dat_protection, fake; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1154 | int rc; |
| 1155 | |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1156 | down_read(&sg->mm->mmap_sem); |
David Hildenbrand | f4debb4 | 2016-01-27 17:24:03 +0100 | [diff] [blame] | 1157 | /* |
| 1158 | * We don't want any guest-2 tables to change - so the parent |
| 1159 | * tables/pointers we read stay valid - unshadowing is however |
| 1160 | * always possible - only guest_table_lock protects us. |
| 1161 | */ |
| 1162 | ipte_lock(vcpu); |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1163 | |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1164 | rc = gmap_shadow_pgt_lookup(sg, saddr, &pgt, &dat_protection, &fake); |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1165 | if (rc) |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1166 | rc = kvm_s390_shadow_tables(sg, saddr, &pgt, &dat_protection, |
| 1167 | &fake); |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1168 | |
| 1169 | vaddr.addr = saddr; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1170 | if (fake) { |
| 1171 | /* offset in 1MB guest memory block */ |
| 1172 | pte.val = pgt + ((unsigned long) vaddr.px << 12UL); |
| 1173 | goto shadow_page; |
| 1174 | } |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1175 | if (!rc) |
| 1176 | rc = gmap_read_table(sg->parent, pgt + vaddr.px * 8, &pte.val); |
| 1177 | if (!rc && pte.i) |
| 1178 | rc = PGM_PAGE_TRANSLATION; |
Heiko Carstens | 232b8e3 | 2017-03-31 12:50:48 +0200 | [diff] [blame] | 1179 | if (!rc && pte.z) |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1180 | rc = PGM_TRANSLATION_SPEC; |
David Hildenbrand | fd8d4e3 | 2016-04-18 13:24:52 +0200 | [diff] [blame] | 1181 | shadow_page: |
David Hildenbrand | 00fc062 | 2016-04-18 17:19:59 +0200 | [diff] [blame] | 1182 | pte.p |= dat_protection; |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1183 | if (!rc) |
| 1184 | rc = gmap_shadow_page(sg, saddr, __pte(pte.val)); |
David Hildenbrand | f4debb4 | 2016-01-27 17:24:03 +0100 | [diff] [blame] | 1185 | ipte_unlock(vcpu); |
David Hildenbrand | e52f8b6 | 2016-02-02 12:26:00 +0100 | [diff] [blame] | 1186 | up_read(&sg->mm->mmap_sem); |
| 1187 | return rc; |
Martin Schwidefsky | aa17aa5 | 2016-03-08 12:16:35 +0100 | [diff] [blame] | 1188 | } |