1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * KVM guest address space mapping code 4 * 5 * Copyright IBM Corp. 2024, 2025 6 * Author(s): Claudio Imbrenda <imbrenda@linux.ibm.com> 7 */ 8 9 #ifndef ARCH_KVM_GMAP_DAT_H 10 #define ARCH_KVM_GMAP_DAT_H 11 12 #include <linux/kvm_host.h> 13 #include <linux/radix-tree.h> 14 #include <linux/refcount.h> 15 #include <linux/io.h> 16 #include <linux/kvm_types.h> 17 #include <linux/pgalloc.h> 18 #include <asm/tlbflush.h> 19 #include <asm/dat-bits.h> 20 21 /* 22 * Base address and length must be sent at the start of each block, therefore 23 * it's cheaper to send some clean data, as long as it's less than the size of 24 * two longs. 25 */ 26 #define KVM_S390_MAX_BIT_DISTANCE (2 * sizeof(void *)) 27 /* For consistency */ 28 #define KVM_S390_CMMA_SIZE_MAX ((u32)KVM_S390_SKEYS_MAX) 29 30 #define _ASCE(x) ((union asce) { .val = (x), }) 31 #define NULL_ASCE _ASCE(0) 32 33 enum { 34 _DAT_TOKEN_NONE = 0, 35 _DAT_TOKEN_PIC, 36 }; 37 38 #define _CRSTE_TOK(l, t, p) ((union crste) { \ 39 .tok.i = 1, \ 40 .tok.tt = (l), \ 41 .tok.type = (t), \ 42 .tok.par = (p) \ 43 }) 44 #define _CRSTE_PIC(l, p) _CRSTE_TOK(l, _DAT_TOKEN_PIC, p) 45 46 #define _CRSTE_HOLE(l) _CRSTE_PIC(l, PGM_ADDRESSING) 47 #define _CRSTE_EMPTY(l) _CRSTE_TOK(l, _DAT_TOKEN_NONE, 0) 48 49 #define _PMD_EMPTY _CRSTE_EMPTY(TABLE_TYPE_SEGMENT) 50 51 #define _PTE_TOK(t, p) ((union pte) { .tok.i = 1, .tok.type = (t), .tok.par = (p) }) 52 #define _PTE_EMPTY _PTE_TOK(_DAT_TOKEN_NONE, 0) 53 54 /* This fake table type is used for page table walks (both for normal page tables and vSIE) */ 55 #define TABLE_TYPE_PAGE_TABLE -1 56 57 enum dat_walk_flags { 58 DAT_WALK_USES_SKEYS = 0x40, 59 DAT_WALK_CONTINUE = 0x20, 60 DAT_WALK_IGN_HOLES = 0x10, 61 DAT_WALK_SPLIT = 0x08, 62 DAT_WALK_ALLOC = 0x04, 63 DAT_WALK_ANY = 0x02, 64 DAT_WALK_LEAF = 0x01, 65 DAT_WALK_DEFAULT = 0 66 }; 67 68 #define DAT_WALK_SPLIT_ALLOC (DAT_WALK_SPLIT | DAT_WALK_ALLOC) 69 #define DAT_WALK_ALLOC_CONTINUE (DAT_WALK_CONTINUE | DAT_WALK_ALLOC) 70 #define DAT_WALK_LEAF_ALLOC (DAT_WALK_LEAF | DAT_WALK_ALLOC) 71 72 union pte { 73 unsigned long val; 74 union page_table_entry h; 75 struct { 76 unsigned long :56; /* Hardware bits */ 77 unsigned long u : 1; /* Page unused */ 78 unsigned long s : 1; /* Special */ 79 unsigned long w : 1; /* Writable */ 80 unsigned long r : 1; /* Readable */ 81 unsigned long d : 1; /* Dirty */ 82 unsigned long y : 1; /* Young */ 83 unsigned long sd: 1; /* Soft dirty */ 84 unsigned long pr: 1; /* Present */ 85 } s; 86 struct { 87 unsigned char hwbytes[7]; 88 unsigned char swbyte; 89 }; 90 union { 91 struct { 92 unsigned long type :16; /* Token type */ 93 unsigned long par :16; /* Token parameter */ 94 unsigned long :20; 95 unsigned long : 1; /* Must be 0 */ 96 unsigned long i : 1; /* Must be 1 */ 97 unsigned long : 2; 98 unsigned long : 7; 99 unsigned long pr : 1; /* Must be 0 */ 100 }; 101 struct { 102 unsigned long token:32; /* Token and parameter */ 103 unsigned long :32; 104 }; 105 } tok; 106 }; 107 108 #define _SEGMENT_FR_MASK (_SEGMENT_MASK >> PAGE_SHIFT) 109 #define _REGION3_FR_MASK (_REGION3_MASK >> PAGE_SHIFT) 110 #define _PAGES_PER_SEGMENT _PAGE_ENTRIES 111 #define _PAGES_PER_REGION3 (_PAGES_PER_SEGMENT * _CRST_ENTRIES) 112 #define _UCAS_ENTRY_LENGTH 0 113 #define _UCAS_ENTRY_OFFSET 3 114 115 /* Soft dirty, needed as macro for atomic operations on ptes */ 116 #define _PAGE_SD 0x002 117 118 /* Needed as macro to perform atomic operations */ 119 #define PGSTE_PCL_BIT 0x0080000000000000UL /* PCL lock, HW bit */ 120 #define PGSTE_CMMA_D_BIT 0x0000000000008000UL /* CMMA dirty soft-bit */ 121 122 enum pgste_gps_usage { 123 PGSTE_GPS_USAGE_STABLE = 0, 124 PGSTE_GPS_USAGE_UNUSED, 125 PGSTE_GPS_USAGE_POT_VOLATILE, 126 PGSTE_GPS_USAGE_VOLATILE, 127 }; 128 129 union pgste { 130 unsigned long val; 131 struct { 132 unsigned long acc : 4; 133 unsigned long fp : 1; 134 unsigned long : 3; 135 unsigned long pcl : 1; 136 unsigned long hr : 1; 137 unsigned long hc : 1; 138 unsigned long : 2; 139 unsigned long gr : 1; 140 unsigned long gc : 1; 141 unsigned long : 1; 142 unsigned long :16; /* val16 */ 143 unsigned long zero : 1; 144 unsigned long nodat : 1; 145 unsigned long : 4; 146 unsigned long usage : 2; 147 unsigned long : 8; 148 unsigned long cmma_d : 1; /* Dirty flag for CMMA bits */ 149 unsigned long prefix_notif : 1; /* Guest prefix invalidation notification */ 150 unsigned long vsie_notif : 1; /* Referenced in a shadow table */ 151 unsigned long vsie_gmem : 1; /* Contains nested guest memory */ 152 unsigned long : 4; 153 unsigned long : 8; 154 }; 155 struct { 156 unsigned short hwbytes0; 157 unsigned short val16; /* Used to store chunked values, see dat_{s,g}et_ptval() */ 158 unsigned short hwbytes4; 159 unsigned char flags; /* Maps to the software bits */ 160 unsigned char hwbyte7; 161 } __packed; 162 }; 163 164 union pmd { 165 unsigned long val; 166 union segment_table_entry h; 167 struct { 168 struct { 169 unsigned long :44; /* HW */ 170 unsigned long : 3; /* Unused */ 171 unsigned long : 1; /* HW */ 172 unsigned long s : 1; /* Special */ 173 unsigned long w : 1; /* Writable soft-bit */ 174 unsigned long r : 1; /* Readable soft-bit */ 175 unsigned long d : 1; /* Dirty */ 176 unsigned long y : 1; /* Young */ 177 unsigned long : 3; /* HW */ 178 unsigned long prefix_notif : 1; /* Guest prefix invalidation notification */ 179 unsigned long vsie_notif : 1; /* Referenced in a shadow table */ 180 unsigned long : 4; /* HW */ 181 unsigned long sd : 1; /* Soft-Dirty */ 182 unsigned long pr : 1; /* Present */ 183 } fc1; 184 } s; 185 }; 186 187 union pud { 188 unsigned long val; 189 union region3_table_entry h; 190 struct { 191 struct { 192 unsigned long :33; /* HW */ 193 unsigned long :14; /* Unused */ 194 unsigned long : 1; /* HW */ 195 unsigned long s : 1; /* Special */ 196 unsigned long w : 1; /* Writable soft-bit */ 197 unsigned long r : 1; /* Readable soft-bit */ 198 unsigned long d : 1; /* Dirty */ 199 unsigned long y : 1; /* Young */ 200 unsigned long : 3; /* HW */ 201 unsigned long prefix_notif : 1; /* Guest prefix invalidation notification */ 202 unsigned long vsie_notif : 1; /* Referenced in a shadow table */ 203 unsigned long : 4; /* HW */ 204 unsigned long sd : 1; /* Soft-Dirty */ 205 unsigned long pr : 1; /* Present */ 206 } fc1; 207 } s; 208 }; 209 210 union p4d { 211 unsigned long val; 212 union region2_table_entry h; 213 }; 214 215 union pgd { 216 unsigned long val; 217 union region1_table_entry h; 218 }; 219 220 union crste { 221 unsigned long val; 222 union { 223 struct { 224 unsigned long :52; 225 unsigned long : 1; 226 unsigned long fc: 1; 227 unsigned long p : 1; 228 unsigned long : 1; 229 unsigned long : 2; 230 unsigned long i : 1; 231 unsigned long : 1; 232 unsigned long tt: 2; 233 unsigned long : 2; 234 }; 235 struct { 236 unsigned long to:52; 237 unsigned long : 1; 238 unsigned long fc: 1; 239 unsigned long p : 1; 240 unsigned long : 1; 241 unsigned long tf: 2; 242 unsigned long i : 1; 243 unsigned long : 1; 244 unsigned long tt: 2; 245 unsigned long tl: 2; 246 } fc0; 247 struct { 248 unsigned long :47; 249 unsigned long av : 1; /* ACCF-Validity Control */ 250 unsigned long acc: 4; /* Access-Control Bits */ 251 unsigned long f : 1; /* Fetch-Protection Bit */ 252 unsigned long fc : 1; /* Format-Control */ 253 unsigned long p : 1; /* DAT-Protection Bit */ 254 unsigned long iep: 1; /* Instruction-Execution-Protection */ 255 unsigned long : 2; 256 unsigned long i : 1; /* Segment-Invalid Bit */ 257 unsigned long cs : 1; /* Common-Segment Bit */ 258 unsigned long tt : 2; /* Table-Type Bits */ 259 unsigned long : 2; 260 } fc1; 261 } h; 262 struct { 263 struct { 264 unsigned long :47; 265 unsigned long : 1; /* HW (should be 0) */ 266 unsigned long s : 1; /* Special */ 267 unsigned long w : 1; /* Writable */ 268 unsigned long r : 1; /* Readable */ 269 unsigned long d : 1; /* Dirty */ 270 unsigned long y : 1; /* Young */ 271 unsigned long : 3; /* HW */ 272 unsigned long prefix_notif : 1; /* Guest prefix invalidation notification */ 273 unsigned long vsie_notif : 1; /* Referenced in a shadow table */ 274 unsigned long : 4; /* HW */ 275 unsigned long sd : 1; /* Soft-Dirty */ 276 unsigned long pr : 1; /* Present */ 277 } fc1; 278 } s; 279 union { 280 struct { 281 unsigned long type :16; /* Token type */ 282 unsigned long par :16; /* Token parameter */ 283 unsigned long :26; 284 unsigned long i : 1; /* Must be 1 */ 285 unsigned long : 1; 286 unsigned long tt : 2; 287 unsigned long : 1; 288 unsigned long pr : 1; /* Must be 0 */ 289 }; 290 struct { 291 unsigned long token:32; /* Token and parameter */ 292 unsigned long :32; 293 }; 294 } tok; 295 union pmd pmd; 296 union pud pud; 297 union p4d p4d; 298 union pgd pgd; 299 }; 300 301 union skey { 302 unsigned char skey; 303 struct { 304 unsigned char acc :4; 305 unsigned char fp :1; 306 unsigned char r :1; 307 unsigned char c :1; 308 unsigned char zero:1; 309 }; 310 }; 311 312 static_assert(sizeof(union pgste) == sizeof(unsigned long)); 313 static_assert(sizeof(union pte) == sizeof(unsigned long)); 314 static_assert(sizeof(union pmd) == sizeof(unsigned long)); 315 static_assert(sizeof(union pud) == sizeof(unsigned long)); 316 static_assert(sizeof(union p4d) == sizeof(unsigned long)); 317 static_assert(sizeof(union pgd) == sizeof(unsigned long)); 318 static_assert(sizeof(union crste) == sizeof(unsigned long)); 319 static_assert(sizeof(union skey) == sizeof(char)); 320 321 struct segment_table { 322 union pmd pmds[_CRST_ENTRIES]; 323 }; 324 325 struct region3_table { 326 union pud puds[_CRST_ENTRIES]; 327 }; 328 329 struct region2_table { 330 union p4d p4ds[_CRST_ENTRIES]; 331 }; 332 333 struct region1_table { 334 union pgd pgds[_CRST_ENTRIES]; 335 }; 336 337 struct crst_table { 338 union { 339 union crste crstes[_CRST_ENTRIES]; 340 struct segment_table segment; 341 struct region3_table region3; 342 struct region2_table region2; 343 struct region1_table region1; 344 }; 345 }; 346 347 struct page_table { 348 union pte ptes[_PAGE_ENTRIES]; 349 union pgste pgstes[_PAGE_ENTRIES]; 350 }; 351 352 static_assert(sizeof(struct crst_table) == _CRST_TABLE_SIZE); 353 static_assert(sizeof(struct page_table) == PAGE_SIZE); 354 355 struct dat_walk; 356 357 typedef long (*dat_walk_op)(union crste *crste, gfn_t gfn, gfn_t next, struct dat_walk *w); 358 359 struct dat_walk_ops { 360 union { 361 dat_walk_op crste_ops[4]; 362 struct { 363 dat_walk_op pmd_entry; 364 dat_walk_op pud_entry; 365 dat_walk_op p4d_entry; 366 dat_walk_op pgd_entry; 367 }; 368 }; 369 long (*pte_entry)(union pte *pte, gfn_t gfn, gfn_t next, struct dat_walk *w); 370 }; 371 372 struct dat_walk { 373 const struct dat_walk_ops *ops; 374 union crste *last; 375 union pte *last_pte; 376 union asce asce; 377 gfn_t start; 378 gfn_t end; 379 int flags; 380 void *priv; 381 }; 382 383 struct ptval_param { 384 unsigned char offset : 6; 385 unsigned char len : 2; 386 }; 387 388 /** 389 * _pte() - Useful constructor for union pte 390 * @pfn: the pfn this pte should point to. 391 * @writable: whether the pte should be writable. 392 * @dirty: whether the pte should be dirty. 393 * @special: whether the pte should be marked as special 394 * 395 * The pte is also marked as young and present. If the pte is marked as dirty, 396 * it gets marked as soft-dirty too. If the pte is not dirty, the hardware 397 * protect bit is set (independently of the write softbit); this way proper 398 * dirty tracking can be performed. 399 * 400 * Return: a union pte value. 401 */ 402 static inline union pte _pte(kvm_pfn_t pfn, bool writable, bool dirty, bool special) 403 { 404 union pte res = { .val = PFN_PHYS(pfn) }; 405 406 res.h.p = !dirty; 407 res.s.y = 1; 408 res.s.pr = 1; 409 res.s.w = writable; 410 res.s.d = dirty; 411 res.s.sd = dirty; 412 res.s.s = special; 413 return res; 414 } 415 416 static inline union crste _crste_fc0(kvm_pfn_t pfn, int tt) 417 { 418 union crste res = { .val = PFN_PHYS(pfn) }; 419 420 res.h.tt = tt; 421 res.h.fc0.tl = _REGION_ENTRY_LENGTH; 422 res.h.fc0.tf = 0; 423 return res; 424 } 425 426 static inline union crste _crste_ucas(kvm_pfn_t pfn) 427 { 428 union crste res = { .val = PFN_PHYS(pfn) }; 429 430 res.h.i = 1; 431 res.h.tt = TABLE_TYPE_SEGMENT; 432 /* 433 * Segment entries do not have a TF or TL field; reuse those bits to 434 * indicate that this is a special mappped-but-not-allocated UCAS 435 * entry. Setting TL to 0 ensures that the entry will always be 436 * considered not present. 437 */ 438 res.h.fc0.tl = _UCAS_ENTRY_LENGTH; 439 res.h.fc0.tf = _UCAS_ENTRY_OFFSET; 440 return res; 441 } 442 443 /** 444 * _crste() - Useful constructor for union crste with FC=1 445 * @pfn: the pfn this pte should point to. 446 * @tt: the table type 447 * @writable: whether the pte should be writable. 448 * @dirty: whether the pte should be dirty. 449 * 450 * The crste is also marked as young and present. If the crste is marked as 451 * dirty, it gets marked as soft-dirty too. If the crste is not dirty, the 452 * hardware protect bit is set (independently of the write softbit); this way 453 * proper dirty tracking can be performed. 454 * 455 * Return: a union crste value. 456 */ 457 static inline union crste _crste_fc1(kvm_pfn_t pfn, int tt, bool writable, bool dirty) 458 { 459 union crste res = { .val = PFN_PHYS(pfn) & _SEGMENT_MASK }; 460 461 res.h.tt = tt; 462 res.h.p = !dirty; 463 res.h.fc = 1; 464 res.s.fc1.y = 1; 465 res.s.fc1.pr = 1; 466 res.s.fc1.w = writable; 467 res.s.fc1.d = dirty; 468 res.s.fc1.sd = dirty; 469 return res; 470 } 471 472 union essa_state { 473 unsigned char val; 474 struct { 475 unsigned char : 2; 476 unsigned char nodat : 1; 477 unsigned char exception : 1; 478 unsigned char usage : 2; 479 unsigned char content : 2; 480 }; 481 }; 482 483 /** 484 * struct vsie_rmap - reverse mapping for shadow page table entries 485 * @next: pointer to next rmap in the list 486 * @r_gfn: virtual rmap address in the shadow guest address space 487 */ 488 struct vsie_rmap { 489 struct vsie_rmap *next; 490 union { 491 unsigned long val; 492 struct { 493 long level: 8; 494 unsigned long : 4; 495 unsigned long r_gfn:52; 496 }; 497 }; 498 }; 499 500 static_assert(sizeof(struct vsie_rmap) == 2 * sizeof(long)); 501 502 #define KVM_S390_MMU_CACHE_N_CRSTS 6 503 #define KVM_S390_MMU_CACHE_N_PTS 2 504 #define KVM_S390_MMU_CACHE_N_RMAPS 16 505 struct kvm_s390_mmu_cache { 506 void *crsts[KVM_S390_MMU_CACHE_N_CRSTS]; 507 void *pts[KVM_S390_MMU_CACHE_N_PTS]; 508 void *rmaps[KVM_S390_MMU_CACHE_N_RMAPS]; 509 short int n_crsts; 510 short int n_pts; 511 short int n_rmaps; 512 }; 513 514 struct guest_fault { 515 gfn_t gfn; /* Guest frame */ 516 kvm_pfn_t pfn; /* Host PFN */ 517 struct page *page; /* Host page */ 518 union pte *ptep; /* Used to resolve the fault, or NULL */ 519 union crste *crstep; /* Used to resolve the fault, or NULL */ 520 bool writable; /* Mapping is writable */ 521 bool write_attempt; /* Write access attempted */ 522 bool attempt_pfault; /* Attempt a pfault first */ 523 bool valid; /* This entry contains valid data */ 524 bool crste_region3; /* Whether crstep refers to a region3 entry */ 525 void (*callback)(struct guest_fault *f); 526 void *priv; 527 }; 528 529 /* 530 * 0 1 2 3 4 5 6 7 531 * +-------+-------+-------+-------+-------+-------+-------+-------+ 532 * 0 | | PGT_ADDR | 533 * 8 | VMADDR | | 534 * 16 | | 535 * 24 | | 536 */ 537 #define MKPTVAL(o, l) ((struct ptval_param) { .offset = (o), .len = ((l) + 1) / 2 - 1}) 538 #define PTVAL_PGT_ADDR MKPTVAL(4, 8) 539 #define PTVAL_VMADDR MKPTVAL(8, 6) 540 541 union pgste __must_check __dat_ptep_xchg(union pte *ptep, union pgste pgste, union pte new, 542 gfn_t gfn, union asce asce, bool uses_skeys); 543 bool dat_crstep_xchg_atomic(union crste *crstep, union crste old, union crste new, gfn_t gfn, 544 union asce asce); 545 void dat_crstep_xchg(union crste *crstep, union crste new, gfn_t gfn, union asce asce); 546 547 long _dat_walk_gfn_range(gfn_t start, gfn_t end, union asce asce, 548 const struct dat_walk_ops *ops, int flags, void *priv); 549 550 int dat_entry_walk(struct kvm_s390_mmu_cache *mc, gfn_t gfn, union asce asce, int flags, 551 int walk_level, union crste **last, union pte **ptepp); 552 void dat_free_level(struct crst_table *table, bool owns_ptes); 553 struct crst_table *dat_alloc_crst_sleepable(unsigned long init); 554 int dat_set_asce_limit(struct kvm_s390_mmu_cache *mc, union asce *asce, int newtype); 555 556 #if KVM_S390_MANAGES_S390_GUEST 557 int dat_get_storage_key(union asce asce, gfn_t gfn, union skey *skey); 558 int dat_set_storage_key(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn, 559 union skey skey, bool nq); 560 int dat_cond_set_storage_key(struct kvm_s390_mmu_cache *mmc, union asce asce, gfn_t gfn, 561 union skey skey, union skey *oldkey, bool nq, bool mr, bool mc); 562 int dat_reset_reference_bit(union asce asce, gfn_t gfn, union skey *skey); 563 long dat_reset_skeys(union asce asce, gfn_t start); 564 #endif /* KVM_S390_MANAGES_S390_GUEST */ 565 566 unsigned long dat_get_ptval(struct page_table *table, struct ptval_param param); 567 void dat_set_ptval(struct page_table *table, struct ptval_param param, unsigned long val); 568 569 int dat_set_slot(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t start, gfn_t end); 570 571 #if KVM_S390_MANAGES_S390_GUEST 572 int dat_set_prefix_notif_bit(union asce asce, gfn_t gfn); 573 #else 574 static inline int dat_set_prefix_notif_bit(union asce asce, gfn_t gfn) 575 { 576 return 0; 577 } 578 #endif /* KVM_S390_MANAGES_S390_GUEST */ 579 580 bool dat_test_age_gfn(union asce asce, gfn_t start, gfn_t end); 581 582 #if KVM_S390_MANAGES_S390_GUEST 583 int dat_perform_essa(union asce asce, gfn_t gfn, int orc, union essa_state *state, bool *dirty); 584 long dat_reset_cmma(union asce asce, gfn_t start_gfn); 585 int dat_peek_cmma(gfn_t start, union asce asce, unsigned int *count, u8 *values); 586 int dat_get_cmma(union asce asce, gfn_t *start, unsigned int *count, u8 *values, atomic64_t *rem); 587 int dat_set_cmma_bits(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t gfn, 588 unsigned long count, unsigned long mask, const uint8_t *bits); 589 #endif /* KVM_S390_MANAGES_S390_GUEST */ 590 591 int kvm_s390_mmu_cache_topup(struct kvm_s390_mmu_cache *mc); 592 593 #define GFP_KVM_S390_MMU_CACHE (GFP_ATOMIC | __GFP_ACCOUNT | __GFP_NOWARN) 594 595 static inline struct page_table *kvm_s390_mmu_cache_alloc_pt(struct kvm_s390_mmu_cache *mc) 596 { 597 if (mc->n_pts) 598 return mc->pts[--mc->n_pts]; 599 return (void *)__get_free_page(GFP_KVM_S390_MMU_CACHE); 600 } 601 602 static inline struct crst_table *kvm_s390_mmu_cache_alloc_crst(struct kvm_s390_mmu_cache *mc) 603 { 604 if (mc->n_crsts) 605 return mc->crsts[--mc->n_crsts]; 606 return (void *)__get_free_pages(GFP_KVM_S390_MMU_CACHE | __GFP_COMP, CRST_ALLOC_ORDER); 607 } 608 609 static inline struct vsie_rmap *kvm_s390_mmu_cache_alloc_rmap(struct kvm_s390_mmu_cache *mc) 610 { 611 if (mc->n_rmaps) 612 return mc->rmaps[--mc->n_rmaps]; 613 return kzalloc_obj(struct vsie_rmap, GFP_KVM_S390_MMU_CACHE); 614 } 615 616 static inline struct crst_table *crste_table_start(union crste *crstep) 617 { 618 return (struct crst_table *)ALIGN_DOWN((unsigned long)crstep, _CRST_TABLE_SIZE); 619 } 620 621 static inline struct page_table *pte_table_start(union pte *ptep) 622 { 623 return (struct page_table *)ALIGN_DOWN((unsigned long)ptep, _PAGE_TABLE_SIZE); 624 } 625 626 static inline bool crdte_crste(union crste *crstep, union crste old, union crste new, gfn_t gfn, 627 union asce asce) 628 { 629 unsigned long dtt = 0x10 | new.h.tt << 2; 630 void *table = crste_table_start(crstep); 631 632 return crdte(old.val, new.val, table, dtt, gfn_to_gpa(gfn), asce.val); 633 } 634 635 /** 636 * idte_crste() - invalidate a crste entry using idte 637 * @crstep: pointer to the crste to be invalidated 638 * @gfn: a gfn mapped by the crste 639 * @opt: options for the idte instruction 640 * @asce: the asce 641 * @local: whether the operation is cpu-local 642 */ 643 static __always_inline void idte_crste(union crste *crstep, gfn_t gfn, unsigned long opt, 644 union asce asce, int local) 645 { 646 unsigned long table_origin = __pa(crste_table_start(crstep)); 647 unsigned long gaddr = gfn_to_gpa(gfn) & HPAGE_MASK; 648 649 table_origin |= crstep->val & _REGION_ENTRY_TYPE_MASK; 650 651 if (__builtin_constant_p(opt) && opt == 0) { 652 /* flush without guest asce */ 653 asm volatile("idte %[table_origin],0,%[gaddr],%[local]" 654 : "+m" (*crstep) 655 : [table_origin] "a" (table_origin), [gaddr] "a" (gaddr), 656 [local] "i" (local) 657 : "cc", "memory"); 658 } else { 659 /* flush with guest asce */ 660 asm volatile("idte %[table_origin],%[asce],%[gaddr_opt],%[local]" 661 : "+m" (*crstep) 662 : [table_origin] "a" (table_origin), [gaddr_opt] "a" (gaddr | opt), 663 [asce] "a" (asce.val), [local] "i" (local) 664 : "cc", "memory"); 665 } 666 } 667 668 static inline void dat_init_pgstes(struct page_table *pt, unsigned long val) 669 { 670 memset64((void *)pt->pgstes, val, PTRS_PER_PTE); 671 } 672 673 static inline void dat_init_page_table(struct page_table *pt, unsigned long ptes, 674 unsigned long pgstes) 675 { 676 memset64((void *)pt->ptes, ptes, PTRS_PER_PTE); 677 dat_init_pgstes(pt, pgstes); 678 } 679 680 static inline gfn_t asce_end(union asce asce) 681 { 682 return 1ULL << ((asce.dt + 1) * 11 + _SEGMENT_SHIFT - PAGE_SHIFT); 683 } 684 685 #define _CRSTE(x) ((union crste) { .val = _Generic((x), \ 686 union pgd : (x).val, \ 687 union p4d : (x).val, \ 688 union pud : (x).val, \ 689 union pmd : (x).val, \ 690 union crste : (x).val)}) 691 692 #define _CRSTEP(x) ((union crste *)_Generic((*(x)), \ 693 union pgd : (x), \ 694 union p4d : (x), \ 695 union pud : (x), \ 696 union pmd : (x), \ 697 union crste : (x))) 698 699 #define _CRSTP(x) ((struct crst_table *)_Generic((*(x)), \ 700 struct crst_table : (x), \ 701 struct segment_table : (x), \ 702 struct region3_table : (x), \ 703 struct region2_table : (x), \ 704 struct region1_table : (x))) 705 706 static inline bool asce_contains_gfn(union asce asce, gfn_t gfn) 707 { 708 return gfn < asce_end(asce); 709 } 710 711 static inline bool is_pmd(union crste crste) 712 { 713 return crste.h.tt == TABLE_TYPE_SEGMENT; 714 } 715 716 static inline bool is_pud(union crste crste) 717 { 718 return crste.h.tt == TABLE_TYPE_REGION3; 719 } 720 721 static inline bool is_p4d(union crste crste) 722 { 723 return crste.h.tt == TABLE_TYPE_REGION2; 724 } 725 726 static inline bool is_pgd(union crste crste) 727 { 728 return crste.h.tt == TABLE_TYPE_REGION1; 729 } 730 731 static inline phys_addr_t pmd_origin_large(union pmd pmd) 732 { 733 return pmd.val & _SEGMENT_ENTRY_ORIGIN_LARGE; 734 } 735 736 static inline phys_addr_t pud_origin_large(union pud pud) 737 { 738 return pud.val & _REGION3_ENTRY_ORIGIN_LARGE; 739 } 740 741 /** 742 * crste_origin_large() - Return the large frame origin of a large crste 743 * @crste: The crste whose origin is to be returned. Should be either a 744 * region-3 table entry or a segment table entry, in both cases with 745 * FC set to 1 (large pages). 746 * 747 * Return: The origin of the large frame pointed to by @crste, or -1 if the 748 * crste was not large (wrong table type, or FC==0) 749 */ 750 static inline phys_addr_t crste_origin_large(union crste crste) 751 { 752 if (unlikely(!crste.h.fc || crste.h.tt > TABLE_TYPE_REGION3)) 753 return -1; 754 if (is_pmd(crste)) 755 return pmd_origin_large(crste.pmd); 756 return pud_origin_large(crste.pud); 757 } 758 759 #define crste_origin(x) (_Generic((x), \ 760 union pmd : (x).val & _SEGMENT_ENTRY_ORIGIN, \ 761 union pud : (x).val & _REGION_ENTRY_ORIGIN, \ 762 union p4d : (x).val & _REGION_ENTRY_ORIGIN, \ 763 union pgd : (x).val & _REGION_ENTRY_ORIGIN)) 764 765 static inline unsigned long pte_origin(union pte pte) 766 { 767 return pte.val & PAGE_MASK; 768 } 769 770 static inline bool pmd_prefix(union pmd pmd) 771 { 772 return pmd.h.fc && pmd.s.fc1.prefix_notif; 773 } 774 775 static inline bool pud_prefix(union pud pud) 776 { 777 return pud.h.fc && pud.s.fc1.prefix_notif; 778 } 779 780 static inline bool crste_leaf(union crste crste) 781 { 782 return (crste.h.tt <= TABLE_TYPE_REGION3) && crste.h.fc; 783 } 784 785 static inline bool crste_prefix(union crste crste) 786 { 787 return crste_leaf(crste) && crste.s.fc1.prefix_notif; 788 } 789 790 static inline bool crste_dirty(union crste crste) 791 { 792 return crste_leaf(crste) && crste.s.fc1.d; 793 } 794 795 static inline union pgste *pgste_of(union pte *pte) 796 { 797 return (union pgste *)(pte + _PAGE_ENTRIES); 798 } 799 800 static inline bool pte_hole(union pte pte) 801 { 802 return pte.h.i && !pte.tok.pr && pte.tok.type != _DAT_TOKEN_NONE; 803 } 804 805 static inline bool _crste_hole(union crste crste) 806 { 807 return crste.h.i && !crste.tok.pr && crste.tok.type != _DAT_TOKEN_NONE; 808 } 809 810 #define crste_hole(x) _crste_hole(_CRSTE(x)) 811 812 static inline bool _crste_none(union crste crste) 813 { 814 return crste.h.i && !crste.tok.pr && crste.tok.type == _DAT_TOKEN_NONE; 815 } 816 817 #define crste_none(x) _crste_none(_CRSTE(x)) 818 819 static inline phys_addr_t large_pud_to_phys(union pud pud, gfn_t gfn) 820 { 821 return pud_origin_large(pud) | (gfn_to_gpa(gfn) & ~_REGION3_MASK); 822 } 823 824 static inline phys_addr_t large_pmd_to_phys(union pmd pmd, gfn_t gfn) 825 { 826 return pmd_origin_large(pmd) | (gfn_to_gpa(gfn) & ~_SEGMENT_MASK); 827 } 828 829 static inline phys_addr_t large_crste_to_phys(union crste crste, gfn_t gfn) 830 { 831 if (unlikely(!crste.h.fc || crste.h.tt > TABLE_TYPE_REGION3)) 832 return -1; 833 if (is_pmd(crste)) 834 return large_pmd_to_phys(crste.pmd, gfn); 835 return large_pud_to_phys(crste.pud, gfn); 836 } 837 838 static inline bool cspg_crste(union crste *crstep, union crste old, union crste new) 839 { 840 return cspg(&crstep->val, old.val, new.val); 841 } 842 843 static inline struct page_table *dereference_pmd(union pmd pmd) 844 { 845 return phys_to_virt(crste_origin(pmd)); 846 } 847 848 static inline struct segment_table *dereference_pud(union pud pud) 849 { 850 return phys_to_virt(crste_origin(pud)); 851 } 852 853 static inline struct region3_table *dereference_p4d(union p4d p4d) 854 { 855 return phys_to_virt(crste_origin(p4d)); 856 } 857 858 static inline struct region2_table *dereference_pgd(union pgd pgd) 859 { 860 return phys_to_virt(crste_origin(pgd)); 861 } 862 863 static inline struct crst_table *_dereference_crste(union crste crste) 864 { 865 if (unlikely(is_pmd(crste))) 866 return NULL; 867 return phys_to_virt(crste_origin(crste.pud)); 868 } 869 870 #define dereference_crste(x) (_Generic((x), \ 871 union pud : _dereference_crste(_CRSTE(x)), \ 872 union p4d : _dereference_crste(_CRSTE(x)), \ 873 union pgd : _dereference_crste(_CRSTE(x)), \ 874 union crste : _dereference_crste(_CRSTE(x)))) 875 876 static inline struct crst_table *dereference_asce(union asce asce) 877 { 878 return phys_to_virt(asce.val & _ASCE_ORIGIN); 879 } 880 881 static inline void asce_flush_tlb(union asce asce) 882 { 883 __tlb_flush_idte(asce.val); 884 } 885 886 static inline bool pgste_get_trylock(union pte *ptep, union pgste *res) 887 { 888 union pgste *pgstep = pgste_of(ptep); 889 union pgste old_pgste; 890 891 if (READ_ONCE(pgstep->val) & PGSTE_PCL_BIT) 892 return false; 893 old_pgste.val = __atomic64_or_barrier(PGSTE_PCL_BIT, &pgstep->val); 894 if (old_pgste.pcl) 895 return false; 896 old_pgste.pcl = 1; 897 *res = old_pgste; 898 return true; 899 } 900 901 static inline union pgste pgste_get_lock(union pte *ptep) 902 { 903 union pgste res; 904 905 while (!pgste_get_trylock(ptep, &res)) 906 cpu_relax(); 907 return res; 908 } 909 910 static inline void pgste_set_unlock(union pte *ptep, union pgste pgste) 911 { 912 pgste.pcl = 0; 913 barrier(); 914 WRITE_ONCE(*pgste_of(ptep), pgste); 915 } 916 917 static inline void dat_ptep_xchg(union pte *ptep, union pte new, gfn_t gfn, union asce asce, 918 bool has_skeys) 919 { 920 union pgste pgste; 921 922 pgste = pgste_get_lock(ptep); 923 pgste = __dat_ptep_xchg(ptep, pgste, new, gfn, asce, has_skeys); 924 pgste_set_unlock(ptep, pgste); 925 } 926 927 static inline void dat_ptep_clear(union pte *ptep, gfn_t gfn, union asce asce, bool has_skeys) 928 { 929 dat_ptep_xchg(ptep, _PTE_EMPTY, gfn, asce, has_skeys); 930 } 931 932 static inline void dat_free_pt(struct page_table *pt) 933 { 934 free_page((unsigned long)pt); 935 } 936 937 static inline void _dat_free_crst(struct crst_table *table) 938 { 939 free_pages((unsigned long)table, CRST_ALLOC_ORDER); 940 } 941 942 #define dat_free_crst(x) _dat_free_crst(_CRSTP(x)) 943 944 static inline void kvm_s390_free_mmu_cache(struct kvm_s390_mmu_cache *mc) 945 { 946 if (!mc) 947 return; 948 while (mc->n_pts) 949 dat_free_pt(mc->pts[--mc->n_pts]); 950 while (mc->n_crsts) 951 _dat_free_crst(mc->crsts[--mc->n_crsts]); 952 while (mc->n_rmaps) 953 kfree(mc->rmaps[--mc->n_rmaps]); 954 kfree(mc); 955 } 956 957 DEFINE_FREE(kvm_s390_mmu_cache, struct kvm_s390_mmu_cache *, if (_T) kvm_s390_free_mmu_cache(_T)) 958 959 static inline struct kvm_s390_mmu_cache *kvm_s390_new_mmu_cache(void) 960 { 961 struct kvm_s390_mmu_cache *mc __free(kvm_s390_mmu_cache) = NULL; 962 963 mc = kzalloc_obj(*mc, GFP_KERNEL_ACCOUNT); 964 if (mc && !kvm_s390_mmu_cache_topup(mc)) 965 return_ptr(mc); 966 return NULL; 967 } 968 969 static inline bool dat_pmdp_xchg_atomic(union pmd *pmdp, union pmd old, union pmd new, 970 gfn_t gfn, union asce asce) 971 { 972 return dat_crstep_xchg_atomic(_CRSTEP(pmdp), _CRSTE(old), _CRSTE(new), gfn, asce); 973 } 974 975 static inline bool dat_pudp_xchg_atomic(union pud *pudp, union pud old, union pud new, 976 gfn_t gfn, union asce asce) 977 { 978 return dat_crstep_xchg_atomic(_CRSTEP(pudp), _CRSTE(old), _CRSTE(new), gfn, asce); 979 } 980 981 static inline union crste dat_crstep_clear_atomic(union crste *crstep, gfn_t gfn, union asce asce) 982 { 983 union crste oldcrste, empty = _CRSTE_EMPTY(crstep->h.tt); 984 985 do { 986 oldcrste = READ_ONCE(*crstep); 987 } while (!dat_crstep_xchg_atomic(crstep, oldcrste, empty, gfn, asce)); 988 return oldcrste; 989 } 990 991 static inline int get_level(union crste *crstep, union pte *ptep) 992 { 993 return ptep ? TABLE_TYPE_PAGE_TABLE : crstep->h.tt; 994 } 995 996 static inline int dat_delete_slot(union asce asce, gfn_t start, unsigned long npages) 997 { 998 return dat_set_slot(NULL, asce, start, start + npages); 999 } 1000 1001 static inline int dat_create_slot(struct kvm_s390_mmu_cache *mc, union asce asce, gfn_t start, 1002 unsigned long npages) 1003 { 1004 return dat_set_slot(mc, asce, start, start + npages); 1005 } 1006 1007 static inline bool crste_is_ucas(union crste crste) 1008 { 1009 return is_pmd(crste) && crste.h.i && !crste.h.fc && 1010 crste.h.fc0.tl == _UCAS_ENTRY_LENGTH && crste.h.fc0.tf == _UCAS_ENTRY_OFFSET; 1011 } 1012 1013 static inline void dat_set_storage_key_from_pgste(union pte pte, union pgste pgste) 1014 { 1015 union skey nkey = { .acc = pgste.acc, .fp = pgste.fp }; 1016 1017 page_set_storage_key(pte_origin(pte), nkey.skey, 0); 1018 } 1019 1020 static inline void dat_move_storage_key(union pte old, union pte new) 1021 { 1022 page_set_storage_key(pte_origin(new), page_get_storage_key(pte_origin(old)), 1); 1023 } 1024 1025 static inline union pgste dat_save_storage_key_into_pgste(union pte pte, union pgste pgste) 1026 { 1027 union skey skey; 1028 1029 skey.skey = page_get_storage_key(pte_origin(pte)); 1030 1031 pgste.acc = skey.acc; 1032 pgste.fp = skey.fp; 1033 pgste.gr |= skey.r; 1034 pgste.gc |= skey.c; 1035 1036 return pgste; 1037 } 1038 1039 #endif /* ARCH_KVM_GMAP_DAT_H */ 1040