Linux Audio

Check our new training course

Loading...
v6.2
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 *  KVM guest address space mapping code
   4 *
   5 *    Copyright IBM Corp. 2007, 2020
   6 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
   7 *		 David Hildenbrand <david@redhat.com>
   8 *		 Janosch Frank <frankja@linux.vnet.ibm.com>
   9 */
  10
  11#include <linux/kernel.h>
  12#include <linux/pagewalk.h>
  13#include <linux/swap.h>
  14#include <linux/smp.h>
  15#include <linux/spinlock.h>
  16#include <linux/slab.h>
  17#include <linux/swapops.h>
  18#include <linux/ksm.h>
  19#include <linux/mman.h>
  20#include <linux/pgtable.h>
  21
 
  22#include <asm/pgalloc.h>
  23#include <asm/gmap.h>
  24#include <asm/tlb.h>
  25
  26#define GMAP_SHADOW_FAKE_TABLE 1ULL
  27
  28/**
  29 * gmap_alloc - allocate and initialize a guest address space
 
  30 * @limit: maximum address of the gmap address space
  31 *
  32 * Returns a guest address space structure.
  33 */
  34static struct gmap *gmap_alloc(unsigned long limit)
  35{
  36	struct gmap *gmap;
  37	struct page *page;
  38	unsigned long *table;
  39	unsigned long etype, atype;
  40
  41	if (limit < _REGION3_SIZE) {
  42		limit = _REGION3_SIZE - 1;
  43		atype = _ASCE_TYPE_SEGMENT;
  44		etype = _SEGMENT_ENTRY_EMPTY;
  45	} else if (limit < _REGION2_SIZE) {
  46		limit = _REGION2_SIZE - 1;
  47		atype = _ASCE_TYPE_REGION3;
  48		etype = _REGION3_ENTRY_EMPTY;
  49	} else if (limit < _REGION1_SIZE) {
  50		limit = _REGION1_SIZE - 1;
  51		atype = _ASCE_TYPE_REGION2;
  52		etype = _REGION2_ENTRY_EMPTY;
  53	} else {
  54		limit = -1UL;
  55		atype = _ASCE_TYPE_REGION1;
  56		etype = _REGION1_ENTRY_EMPTY;
  57	}
  58	gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
  59	if (!gmap)
  60		goto out;
  61	INIT_LIST_HEAD(&gmap->crst_list);
  62	INIT_LIST_HEAD(&gmap->children);
  63	INIT_LIST_HEAD(&gmap->pt_list);
  64	INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
  65	INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
  66	INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
  67	spin_lock_init(&gmap->guest_table_lock);
  68	spin_lock_init(&gmap->shadow_lock);
  69	refcount_set(&gmap->ref_count, 1);
  70	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
  71	if (!page)
  72		goto out_free;
  73	page->index = 0;
  74	list_add(&page->lru, &gmap->crst_list);
  75	table = page_to_virt(page);
  76	crst_table_init(table, etype);
  77	gmap->table = table;
  78	gmap->asce = atype | _ASCE_TABLE_LENGTH |
  79		_ASCE_USER_BITS | __pa(table);
  80	gmap->asce_end = limit;
  81	return gmap;
  82
  83out_free:
  84	kfree(gmap);
  85out:
  86	return NULL;
  87}
  88
  89/**
  90 * gmap_create - create a guest address space
  91 * @mm: pointer to the parent mm_struct
  92 * @limit: maximum size of the gmap address space
  93 *
  94 * Returns a guest address space structure.
  95 */
  96struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
  97{
  98	struct gmap *gmap;
  99	unsigned long gmap_asce;
 100
 101	gmap = gmap_alloc(limit);
 102	if (!gmap)
 103		return NULL;
 104	gmap->mm = mm;
 105	spin_lock(&mm->context.lock);
 106	list_add_rcu(&gmap->list, &mm->context.gmap_list);
 107	if (list_is_singular(&mm->context.gmap_list))
 108		gmap_asce = gmap->asce;
 109	else
 110		gmap_asce = -1UL;
 111	WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
 112	spin_unlock(&mm->context.lock);
 113	return gmap;
 114}
 115EXPORT_SYMBOL_GPL(gmap_create);
 116
 117static void gmap_flush_tlb(struct gmap *gmap)
 118{
 119	if (MACHINE_HAS_IDTE)
 120		__tlb_flush_idte(gmap->asce);
 121	else
 122		__tlb_flush_global();
 123}
 124
 125static void gmap_radix_tree_free(struct radix_tree_root *root)
 126{
 127	struct radix_tree_iter iter;
 128	unsigned long indices[16];
 129	unsigned long index;
 130	void __rcu **slot;
 131	int i, nr;
 132
 133	/* A radix tree is freed by deleting all of its entries */
 134	index = 0;
 135	do {
 136		nr = 0;
 137		radix_tree_for_each_slot(slot, root, &iter, index) {
 138			indices[nr] = iter.index;
 139			if (++nr == 16)
 140				break;
 141		}
 142		for (i = 0; i < nr; i++) {
 143			index = indices[i];
 144			radix_tree_delete(root, index);
 145		}
 146	} while (nr > 0);
 147}
 148
 149static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
 150{
 151	struct gmap_rmap *rmap, *rnext, *head;
 152	struct radix_tree_iter iter;
 153	unsigned long indices[16];
 154	unsigned long index;
 155	void __rcu **slot;
 156	int i, nr;
 157
 158	/* A radix tree is freed by deleting all of its entries */
 159	index = 0;
 160	do {
 161		nr = 0;
 162		radix_tree_for_each_slot(slot, root, &iter, index) {
 163			indices[nr] = iter.index;
 164			if (++nr == 16)
 165				break;
 166		}
 167		for (i = 0; i < nr; i++) {
 168			index = indices[i];
 169			head = radix_tree_delete(root, index);
 170			gmap_for_each_rmap_safe(rmap, rnext, head)
 171				kfree(rmap);
 172		}
 173	} while (nr > 0);
 174}
 175
 176/**
 177 * gmap_free - free a guest address space
 178 * @gmap: pointer to the guest address space structure
 179 *
 180 * No locks required. There are no references to this gmap anymore.
 181 */
 182static void gmap_free(struct gmap *gmap)
 183{
 184	struct page *page, *next;
 185
 186	/* Flush tlb of all gmaps (if not already done for shadows) */
 187	if (!(gmap_is_shadow(gmap) && gmap->removed))
 188		gmap_flush_tlb(gmap);
 189	/* Free all segment & region tables. */
 190	list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
 191		__free_pages(page, CRST_ALLOC_ORDER);
 192	gmap_radix_tree_free(&gmap->guest_to_host);
 193	gmap_radix_tree_free(&gmap->host_to_guest);
 194
 195	/* Free additional data for a shadow gmap */
 196	if (gmap_is_shadow(gmap)) {
 197		/* Free all page tables. */
 198		list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
 199			page_table_free_pgste(page);
 200		gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
 201		/* Release reference to the parent */
 202		gmap_put(gmap->parent);
 203	}
 204
 205	kfree(gmap);
 206}
 207
 208/**
 209 * gmap_get - increase reference counter for guest address space
 210 * @gmap: pointer to the guest address space structure
 211 *
 212 * Returns the gmap pointer
 213 */
 214struct gmap *gmap_get(struct gmap *gmap)
 215{
 216	refcount_inc(&gmap->ref_count);
 217	return gmap;
 218}
 219EXPORT_SYMBOL_GPL(gmap_get);
 220
 221/**
 222 * gmap_put - decrease reference counter for guest address space
 223 * @gmap: pointer to the guest address space structure
 224 *
 225 * If the reference counter reaches zero the guest address space is freed.
 226 */
 227void gmap_put(struct gmap *gmap)
 228{
 229	if (refcount_dec_and_test(&gmap->ref_count))
 230		gmap_free(gmap);
 231}
 232EXPORT_SYMBOL_GPL(gmap_put);
 233
 234/**
 235 * gmap_remove - remove a guest address space but do not free it yet
 236 * @gmap: pointer to the guest address space structure
 237 */
 238void gmap_remove(struct gmap *gmap)
 239{
 240	struct gmap *sg, *next;
 241	unsigned long gmap_asce;
 242
 243	/* Remove all shadow gmaps linked to this gmap */
 244	if (!list_empty(&gmap->children)) {
 245		spin_lock(&gmap->shadow_lock);
 246		list_for_each_entry_safe(sg, next, &gmap->children, list) {
 247			list_del(&sg->list);
 248			gmap_put(sg);
 249		}
 250		spin_unlock(&gmap->shadow_lock);
 251	}
 252	/* Remove gmap from the pre-mm list */
 253	spin_lock(&gmap->mm->context.lock);
 254	list_del_rcu(&gmap->list);
 255	if (list_empty(&gmap->mm->context.gmap_list))
 256		gmap_asce = 0;
 257	else if (list_is_singular(&gmap->mm->context.gmap_list))
 258		gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
 259					     struct gmap, list)->asce;
 260	else
 261		gmap_asce = -1UL;
 262	WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
 263	spin_unlock(&gmap->mm->context.lock);
 264	synchronize_rcu();
 265	/* Put reference */
 266	gmap_put(gmap);
 267}
 268EXPORT_SYMBOL_GPL(gmap_remove);
 269
 270/**
 271 * gmap_enable - switch primary space to the guest address space
 272 * @gmap: pointer to the guest address space structure
 273 */
 274void gmap_enable(struct gmap *gmap)
 275{
 276	S390_lowcore.gmap = (unsigned long) gmap;
 277}
 278EXPORT_SYMBOL_GPL(gmap_enable);
 279
 280/**
 281 * gmap_disable - switch back to the standard primary address space
 282 * @gmap: pointer to the guest address space structure
 283 */
 284void gmap_disable(struct gmap *gmap)
 285{
 286	S390_lowcore.gmap = 0UL;
 287}
 288EXPORT_SYMBOL_GPL(gmap_disable);
 289
 290/**
 291 * gmap_get_enabled - get a pointer to the currently enabled gmap
 292 *
 293 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
 294 */
 295struct gmap *gmap_get_enabled(void)
 296{
 297	return (struct gmap *) S390_lowcore.gmap;
 298}
 299EXPORT_SYMBOL_GPL(gmap_get_enabled);
 300
 301/*
 302 * gmap_alloc_table is assumed to be called with mmap_lock held
 303 */
 304static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
 305			    unsigned long init, unsigned long gaddr)
 306{
 307	struct page *page;
 308	unsigned long *new;
 309
 310	/* since we dont free the gmap table until gmap_free we can unlock */
 311	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
 312	if (!page)
 313		return -ENOMEM;
 314	new = page_to_virt(page);
 315	crst_table_init(new, init);
 316	spin_lock(&gmap->guest_table_lock);
 317	if (*table & _REGION_ENTRY_INVALID) {
 318		list_add(&page->lru, &gmap->crst_list);
 319		*table = __pa(new) | _REGION_ENTRY_LENGTH |
 320			(*table & _REGION_ENTRY_TYPE_MASK);
 321		page->index = gaddr;
 322		page = NULL;
 323	}
 324	spin_unlock(&gmap->guest_table_lock);
 325	if (page)
 326		__free_pages(page, CRST_ALLOC_ORDER);
 327	return 0;
 328}
 329
 330/**
 331 * __gmap_segment_gaddr - find virtual address from segment pointer
 332 * @entry: pointer to a segment table entry in the guest address space
 333 *
 334 * Returns the virtual address in the guest address space for the segment
 335 */
 336static unsigned long __gmap_segment_gaddr(unsigned long *entry)
 337{
 338	struct page *page;
 339	unsigned long offset;
 340
 341	offset = (unsigned long) entry / sizeof(unsigned long);
 342	offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
 343	page = pmd_pgtable_page((pmd_t *) entry);
 
 344	return page->index + offset;
 345}
 346
 347/**
 348 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
 349 * @gmap: pointer to the guest address space structure
 350 * @vmaddr: address in the host process address space
 351 *
 352 * Returns 1 if a TLB flush is required
 353 */
 354static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
 355{
 356	unsigned long *entry;
 357	int flush = 0;
 358
 359	BUG_ON(gmap_is_shadow(gmap));
 360	spin_lock(&gmap->guest_table_lock);
 361	entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
 362	if (entry) {
 363		flush = (*entry != _SEGMENT_ENTRY_EMPTY);
 364		*entry = _SEGMENT_ENTRY_EMPTY;
 365	}
 366	spin_unlock(&gmap->guest_table_lock);
 367	return flush;
 368}
 369
 370/**
 371 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
 372 * @gmap: pointer to the guest address space structure
 373 * @gaddr: address in the guest address space
 374 *
 375 * Returns 1 if a TLB flush is required
 376 */
 377static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
 378{
 379	unsigned long vmaddr;
 380
 381	vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
 382						   gaddr >> PMD_SHIFT);
 383	return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
 384}
 385
 386/**
 387 * gmap_unmap_segment - unmap segment from the guest address space
 388 * @gmap: pointer to the guest address space structure
 389 * @to: address in the guest address space
 390 * @len: length of the memory area to unmap
 391 *
 392 * Returns 0 if the unmap succeeded, -EINVAL if not.
 393 */
 394int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
 395{
 396	unsigned long off;
 397	int flush;
 398
 399	BUG_ON(gmap_is_shadow(gmap));
 400	if ((to | len) & (PMD_SIZE - 1))
 401		return -EINVAL;
 402	if (len == 0 || to + len < to)
 403		return -EINVAL;
 404
 405	flush = 0;
 406	mmap_write_lock(gmap->mm);
 407	for (off = 0; off < len; off += PMD_SIZE)
 408		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
 409	mmap_write_unlock(gmap->mm);
 410	if (flush)
 411		gmap_flush_tlb(gmap);
 412	return 0;
 413}
 414EXPORT_SYMBOL_GPL(gmap_unmap_segment);
 415
 416/**
 417 * gmap_map_segment - map a segment to the guest address space
 418 * @gmap: pointer to the guest address space structure
 419 * @from: source address in the parent address space
 420 * @to: target address in the guest address space
 421 * @len: length of the memory area to map
 422 *
 423 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
 424 */
 425int gmap_map_segment(struct gmap *gmap, unsigned long from,
 426		     unsigned long to, unsigned long len)
 427{
 428	unsigned long off;
 429	int flush;
 430
 431	BUG_ON(gmap_is_shadow(gmap));
 432	if ((from | to | len) & (PMD_SIZE - 1))
 433		return -EINVAL;
 434	if (len == 0 || from + len < from || to + len < to ||
 435	    from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
 436		return -EINVAL;
 437
 438	flush = 0;
 439	mmap_write_lock(gmap->mm);
 440	for (off = 0; off < len; off += PMD_SIZE) {
 441		/* Remove old translation */
 442		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
 443		/* Store new translation */
 444		if (radix_tree_insert(&gmap->guest_to_host,
 445				      (to + off) >> PMD_SHIFT,
 446				      (void *) from + off))
 447			break;
 448	}
 449	mmap_write_unlock(gmap->mm);
 450	if (flush)
 451		gmap_flush_tlb(gmap);
 452	if (off >= len)
 453		return 0;
 454	gmap_unmap_segment(gmap, to, len);
 455	return -ENOMEM;
 456}
 457EXPORT_SYMBOL_GPL(gmap_map_segment);
 458
 459/**
 460 * __gmap_translate - translate a guest address to a user space address
 461 * @gmap: pointer to guest mapping meta data structure
 462 * @gaddr: guest address
 463 *
 464 * Returns user space address which corresponds to the guest address or
 465 * -EFAULT if no such mapping exists.
 466 * This function does not establish potentially missing page table entries.
 467 * The mmap_lock of the mm that belongs to the address space must be held
 468 * when this function gets called.
 469 *
 470 * Note: Can also be called for shadow gmaps.
 471 */
 472unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
 473{
 474	unsigned long vmaddr;
 475
 476	vmaddr = (unsigned long)
 477		radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
 478	/* Note: guest_to_host is empty for a shadow gmap */
 479	return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
 480}
 481EXPORT_SYMBOL_GPL(__gmap_translate);
 482
 483/**
 484 * gmap_translate - translate a guest address to a user space address
 485 * @gmap: pointer to guest mapping meta data structure
 486 * @gaddr: guest address
 487 *
 488 * Returns user space address which corresponds to the guest address or
 489 * -EFAULT if no such mapping exists.
 490 * This function does not establish potentially missing page table entries.
 491 */
 492unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
 493{
 494	unsigned long rc;
 495
 496	mmap_read_lock(gmap->mm);
 497	rc = __gmap_translate(gmap, gaddr);
 498	mmap_read_unlock(gmap->mm);
 499	return rc;
 500}
 501EXPORT_SYMBOL_GPL(gmap_translate);
 502
 503/**
 504 * gmap_unlink - disconnect a page table from the gmap shadow tables
 505 * @mm: pointer to the parent mm_struct
 506 * @table: pointer to the host page table
 507 * @vmaddr: vm address associated with the host page table
 508 */
 509void gmap_unlink(struct mm_struct *mm, unsigned long *table,
 510		 unsigned long vmaddr)
 511{
 512	struct gmap *gmap;
 513	int flush;
 514
 515	rcu_read_lock();
 516	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
 517		flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
 518		if (flush)
 519			gmap_flush_tlb(gmap);
 520	}
 521	rcu_read_unlock();
 522}
 523
 524static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
 525			   unsigned long gaddr);
 526
 527/**
 528 * __gmap_link - set up shadow page tables to connect a host to a guest address
 529 * @gmap: pointer to guest mapping meta data structure
 530 * @gaddr: guest address
 531 * @vmaddr: vm address
 532 *
 533 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 534 * if the vm address is already mapped to a different guest segment.
 535 * The mmap_lock of the mm that belongs to the address space must be held
 536 * when this function gets called.
 537 */
 538int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
 539{
 540	struct mm_struct *mm;
 541	unsigned long *table;
 542	spinlock_t *ptl;
 543	pgd_t *pgd;
 544	p4d_t *p4d;
 545	pud_t *pud;
 546	pmd_t *pmd;
 547	u64 unprot;
 548	int rc;
 549
 550	BUG_ON(gmap_is_shadow(gmap));
 551	/* Create higher level tables in the gmap page table */
 552	table = gmap->table;
 553	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
 554		table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
 555		if ((*table & _REGION_ENTRY_INVALID) &&
 556		    gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
 557				     gaddr & _REGION1_MASK))
 558			return -ENOMEM;
 559		table = __va(*table & _REGION_ENTRY_ORIGIN);
 560	}
 561	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
 562		table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
 563		if ((*table & _REGION_ENTRY_INVALID) &&
 564		    gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
 565				     gaddr & _REGION2_MASK))
 566			return -ENOMEM;
 567		table = __va(*table & _REGION_ENTRY_ORIGIN);
 568	}
 569	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
 570		table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
 571		if ((*table & _REGION_ENTRY_INVALID) &&
 572		    gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
 573				     gaddr & _REGION3_MASK))
 574			return -ENOMEM;
 575		table = __va(*table & _REGION_ENTRY_ORIGIN);
 576	}
 577	table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
 578	/* Walk the parent mm page table */
 579	mm = gmap->mm;
 580	pgd = pgd_offset(mm, vmaddr);
 581	VM_BUG_ON(pgd_none(*pgd));
 582	p4d = p4d_offset(pgd, vmaddr);
 583	VM_BUG_ON(p4d_none(*p4d));
 584	pud = pud_offset(p4d, vmaddr);
 585	VM_BUG_ON(pud_none(*pud));
 586	/* large puds cannot yet be handled */
 587	if (pud_large(*pud))
 588		return -EFAULT;
 589	pmd = pmd_offset(pud, vmaddr);
 590	VM_BUG_ON(pmd_none(*pmd));
 591	/* Are we allowed to use huge pages? */
 592	if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
 593		return -EFAULT;
 594	/* Link gmap segment table entry location to page table. */
 595	rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
 596	if (rc)
 597		return rc;
 598	ptl = pmd_lock(mm, pmd);
 599	spin_lock(&gmap->guest_table_lock);
 600	if (*table == _SEGMENT_ENTRY_EMPTY) {
 601		rc = radix_tree_insert(&gmap->host_to_guest,
 602				       vmaddr >> PMD_SHIFT, table);
 603		if (!rc) {
 604			if (pmd_large(*pmd)) {
 605				*table = (pmd_val(*pmd) &
 606					  _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
 607					| _SEGMENT_ENTRY_GMAP_UC;
 608			} else
 609				*table = pmd_val(*pmd) &
 610					_SEGMENT_ENTRY_HARDWARE_BITS;
 611		}
 612	} else if (*table & _SEGMENT_ENTRY_PROTECT &&
 613		   !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
 614		unprot = (u64)*table;
 615		unprot &= ~_SEGMENT_ENTRY_PROTECT;
 616		unprot |= _SEGMENT_ENTRY_GMAP_UC;
 617		gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
 618	}
 619	spin_unlock(&gmap->guest_table_lock);
 620	spin_unlock(ptl);
 621	radix_tree_preload_end();
 622	return rc;
 623}
 624
 625/**
 626 * gmap_fault - resolve a fault on a guest address
 627 * @gmap: pointer to guest mapping meta data structure
 628 * @gaddr: guest address
 629 * @fault_flags: flags to pass down to handle_mm_fault()
 630 *
 631 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 632 * if the vm address is already mapped to a different guest segment.
 633 */
 634int gmap_fault(struct gmap *gmap, unsigned long gaddr,
 635	       unsigned int fault_flags)
 636{
 637	unsigned long vmaddr;
 638	int rc;
 639	bool unlocked;
 640
 641	mmap_read_lock(gmap->mm);
 642
 643retry:
 644	unlocked = false;
 645	vmaddr = __gmap_translate(gmap, gaddr);
 646	if (IS_ERR_VALUE(vmaddr)) {
 647		rc = vmaddr;
 648		goto out_up;
 649	}
 650	if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
 651			     &unlocked)) {
 652		rc = -EFAULT;
 653		goto out_up;
 654	}
 655	/*
 656	 * In the case that fixup_user_fault unlocked the mmap_lock during
 657	 * faultin redo __gmap_translate to not race with a map/unmap_segment.
 658	 */
 659	if (unlocked)
 660		goto retry;
 661
 662	rc = __gmap_link(gmap, gaddr, vmaddr);
 663out_up:
 664	mmap_read_unlock(gmap->mm);
 665	return rc;
 666}
 667EXPORT_SYMBOL_GPL(gmap_fault);
 668
 669/*
 670 * this function is assumed to be called with mmap_lock held
 671 */
 672void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
 673{
 674	struct vm_area_struct *vma;
 675	unsigned long vmaddr;
 676	spinlock_t *ptl;
 677	pte_t *ptep;
 678
 679	/* Find the vm address for the guest address */
 680	vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
 681						   gaddr >> PMD_SHIFT);
 682	if (vmaddr) {
 683		vmaddr |= gaddr & ~PMD_MASK;
 684
 685		vma = vma_lookup(gmap->mm, vmaddr);
 686		if (!vma || is_vm_hugetlb_page(vma))
 687			return;
 688
 689		/* Get pointer to the page table entry */
 690		ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
 691		if (likely(ptep)) {
 692			ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
 693			pte_unmap_unlock(ptep, ptl);
 694		}
 695	}
 696}
 697EXPORT_SYMBOL_GPL(__gmap_zap);
 698
 699void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
 700{
 701	unsigned long gaddr, vmaddr, size;
 702	struct vm_area_struct *vma;
 703
 704	mmap_read_lock(gmap->mm);
 705	for (gaddr = from; gaddr < to;
 706	     gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
 707		/* Find the vm address for the guest address */
 708		vmaddr = (unsigned long)
 709			radix_tree_lookup(&gmap->guest_to_host,
 710					  gaddr >> PMD_SHIFT);
 711		if (!vmaddr)
 712			continue;
 713		vmaddr |= gaddr & ~PMD_MASK;
 714		/* Find vma in the parent mm */
 715		vma = find_vma(gmap->mm, vmaddr);
 716		if (!vma)
 717			continue;
 718		/*
 719		 * We do not discard pages that are backed by
 720		 * hugetlbfs, so we don't have to refault them.
 721		 */
 722		if (is_vm_hugetlb_page(vma))
 723			continue;
 724		size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
 725		zap_page_range(vma, vmaddr, size);
 726	}
 727	mmap_read_unlock(gmap->mm);
 728}
 729EXPORT_SYMBOL_GPL(gmap_discard);
 730
 731static LIST_HEAD(gmap_notifier_list);
 732static DEFINE_SPINLOCK(gmap_notifier_lock);
 733
 734/**
 735 * gmap_register_pte_notifier - register a pte invalidation callback
 736 * @nb: pointer to the gmap notifier block
 737 */
 738void gmap_register_pte_notifier(struct gmap_notifier *nb)
 739{
 740	spin_lock(&gmap_notifier_lock);
 741	list_add_rcu(&nb->list, &gmap_notifier_list);
 742	spin_unlock(&gmap_notifier_lock);
 743}
 744EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
 745
 746/**
 747 * gmap_unregister_pte_notifier - remove a pte invalidation callback
 748 * @nb: pointer to the gmap notifier block
 749 */
 750void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
 751{
 752	spin_lock(&gmap_notifier_lock);
 753	list_del_rcu(&nb->list);
 754	spin_unlock(&gmap_notifier_lock);
 755	synchronize_rcu();
 756}
 757EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
 758
 759/**
 760 * gmap_call_notifier - call all registered invalidation callbacks
 761 * @gmap: pointer to guest mapping meta data structure
 762 * @start: start virtual address in the guest address space
 763 * @end: end virtual address in the guest address space
 764 */
 765static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
 766			       unsigned long end)
 767{
 768	struct gmap_notifier *nb;
 769
 770	list_for_each_entry(nb, &gmap_notifier_list, list)
 771		nb->notifier_call(gmap, start, end);
 772}
 773
 774/**
 775 * gmap_table_walk - walk the gmap page tables
 776 * @gmap: pointer to guest mapping meta data structure
 777 * @gaddr: virtual address in the guest address space
 778 * @level: page table level to stop at
 779 *
 780 * Returns a table entry pointer for the given guest address and @level
 781 * @level=0 : returns a pointer to a page table table entry (or NULL)
 782 * @level=1 : returns a pointer to a segment table entry (or NULL)
 783 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
 784 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
 785 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
 786 *
 787 * Returns NULL if the gmap page tables could not be walked to the
 788 * requested level.
 789 *
 790 * Note: Can also be called for shadow gmaps.
 791 */
 792static inline unsigned long *gmap_table_walk(struct gmap *gmap,
 793					     unsigned long gaddr, int level)
 794{
 795	const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
 796	unsigned long *table = gmap->table;
 797
 798	if (gmap_is_shadow(gmap) && gmap->removed)
 799		return NULL;
 800
 801	if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
 802		return NULL;
 803
 804	if (asce_type != _ASCE_TYPE_REGION1 &&
 805	    gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
 806		return NULL;
 807
 808	switch (asce_type) {
 809	case _ASCE_TYPE_REGION1:
 810		table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
 811		if (level == 4)
 812			break;
 813		if (*table & _REGION_ENTRY_INVALID)
 814			return NULL;
 815		table = __va(*table & _REGION_ENTRY_ORIGIN);
 816		fallthrough;
 817	case _ASCE_TYPE_REGION2:
 818		table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
 819		if (level == 3)
 820			break;
 821		if (*table & _REGION_ENTRY_INVALID)
 822			return NULL;
 823		table = __va(*table & _REGION_ENTRY_ORIGIN);
 824		fallthrough;
 825	case _ASCE_TYPE_REGION3:
 826		table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
 827		if (level == 2)
 828			break;
 829		if (*table & _REGION_ENTRY_INVALID)
 830			return NULL;
 831		table = __va(*table & _REGION_ENTRY_ORIGIN);
 832		fallthrough;
 833	case _ASCE_TYPE_SEGMENT:
 834		table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
 835		if (level == 1)
 836			break;
 837		if (*table & _REGION_ENTRY_INVALID)
 838			return NULL;
 839		table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
 840		table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
 841	}
 842	return table;
 843}
 844
 845/**
 846 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
 847 *		      and return the pte pointer
 848 * @gmap: pointer to guest mapping meta data structure
 849 * @gaddr: virtual address in the guest address space
 850 * @ptl: pointer to the spinlock pointer
 851 *
 852 * Returns a pointer to the locked pte for a guest address, or NULL
 
 
 853 */
 854static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
 855			       spinlock_t **ptl)
 856{
 857	unsigned long *table;
 858
 859	BUG_ON(gmap_is_shadow(gmap));
 
 860	/* Walk the gmap page table, lock and get pte pointer */
 861	table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
 862	if (!table || *table & _SEGMENT_ENTRY_INVALID)
 
 
 863		return NULL;
 
 
 
 
 
 864	return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
 865}
 866
 867/**
 868 * gmap_pte_op_fixup - force a page in and connect the gmap page table
 869 * @gmap: pointer to guest mapping meta data structure
 870 * @gaddr: virtual address in the guest address space
 871 * @vmaddr: address in the host process address space
 872 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 873 *
 874 * Returns 0 if the caller can retry __gmap_translate (might fail again),
 875 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
 876 * up or connecting the gmap page table.
 877 */
 878static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
 879			     unsigned long vmaddr, int prot)
 880{
 881	struct mm_struct *mm = gmap->mm;
 882	unsigned int fault_flags;
 883	bool unlocked = false;
 884
 885	BUG_ON(gmap_is_shadow(gmap));
 886	fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
 887	if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
 888		return -EFAULT;
 889	if (unlocked)
 890		/* lost mmap_lock, caller has to retry __gmap_translate */
 891		return 0;
 892	/* Connect the page tables */
 893	return __gmap_link(gmap, gaddr, vmaddr);
 894}
 895
 896/**
 897 * gmap_pte_op_end - release the page table lock
 898 * @ptl: pointer to the spinlock pointer
 899 */
 900static void gmap_pte_op_end(spinlock_t *ptl)
 901{
 902	if (ptl)
 903		spin_unlock(ptl);
 904}
 905
 906/**
 907 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
 908 *		      and return the pmd pointer
 909 * @gmap: pointer to guest mapping meta data structure
 910 * @gaddr: virtual address in the guest address space
 911 *
 912 * Returns a pointer to the pmd for a guest address, or NULL
 913 */
 914static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
 915{
 916	pmd_t *pmdp;
 917
 918	BUG_ON(gmap_is_shadow(gmap));
 919	pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
 920	if (!pmdp)
 921		return NULL;
 922
 923	/* without huge pages, there is no need to take the table lock */
 924	if (!gmap->mm->context.allow_gmap_hpage_1m)
 925		return pmd_none(*pmdp) ? NULL : pmdp;
 926
 927	spin_lock(&gmap->guest_table_lock);
 928	if (pmd_none(*pmdp)) {
 929		spin_unlock(&gmap->guest_table_lock);
 930		return NULL;
 931	}
 932
 933	/* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
 934	if (!pmd_large(*pmdp))
 935		spin_unlock(&gmap->guest_table_lock);
 936	return pmdp;
 937}
 938
 939/**
 940 * gmap_pmd_op_end - release the guest_table_lock if needed
 941 * @gmap: pointer to the guest mapping meta data structure
 942 * @pmdp: pointer to the pmd
 943 */
 944static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
 945{
 946	if (pmd_large(*pmdp))
 947		spin_unlock(&gmap->guest_table_lock);
 948}
 949
 950/*
 951 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
 952 * @pmdp: pointer to the pmd to be protected
 953 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 954 * @bits: notification bits to set
 955 *
 956 * Returns:
 957 * 0 if successfully protected
 958 * -EAGAIN if a fixup is needed
 959 * -EINVAL if unsupported notifier bits have been specified
 960 *
 961 * Expected to be called with sg->mm->mmap_lock in read and
 962 * guest_table_lock held.
 963 */
 964static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
 965			    pmd_t *pmdp, int prot, unsigned long bits)
 966{
 967	int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
 968	int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
 969	pmd_t new = *pmdp;
 970
 971	/* Fixup needed */
 972	if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
 973		return -EAGAIN;
 974
 975	if (prot == PROT_NONE && !pmd_i) {
 976		new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
 977		gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
 978	}
 979
 980	if (prot == PROT_READ && !pmd_p) {
 981		new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
 982		new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
 983		gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
 984	}
 985
 986	if (bits & GMAP_NOTIFY_MPROT)
 987		set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
 988
 989	/* Shadow GMAP protection needs split PMDs */
 990	if (bits & GMAP_NOTIFY_SHADOW)
 991		return -EINVAL;
 992
 993	return 0;
 994}
 995
 996/*
 997 * gmap_protect_pte - remove access rights to memory and set pgste bits
 998 * @gmap: pointer to guest mapping meta data structure
 999 * @gaddr: virtual address in the guest address space
1000 * @pmdp: pointer to the pmd associated with the pte
1001 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1002 * @bits: notification bits to set
1003 *
1004 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1005 * -EAGAIN if a fixup is needed.
1006 *
1007 * Expected to be called with sg->mm->mmap_lock in read
1008 */
1009static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1010			    pmd_t *pmdp, int prot, unsigned long bits)
1011{
1012	int rc;
1013	pte_t *ptep;
1014	spinlock_t *ptl = NULL;
1015	unsigned long pbits = 0;
1016
1017	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1018		return -EAGAIN;
1019
1020	ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1021	if (!ptep)
1022		return -ENOMEM;
1023
1024	pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1025	pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1026	/* Protect and unlock. */
1027	rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1028	gmap_pte_op_end(ptl);
1029	return rc;
1030}
1031
1032/*
1033 * gmap_protect_range - remove access rights to memory and set pgste bits
1034 * @gmap: pointer to guest mapping meta data structure
1035 * @gaddr: virtual address in the guest address space
1036 * @len: size of area
1037 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1038 * @bits: pgste notification bits to set
1039 *
1040 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1041 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1042 *
1043 * Called with sg->mm->mmap_lock in read.
 
 
1044 */
1045static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1046			      unsigned long len, int prot, unsigned long bits)
1047{
1048	unsigned long vmaddr, dist;
1049	pmd_t *pmdp;
 
1050	int rc;
1051
1052	BUG_ON(gmap_is_shadow(gmap));
1053	while (len) {
1054		rc = -EAGAIN;
1055		pmdp = gmap_pmd_op_walk(gmap, gaddr);
1056		if (pmdp) {
1057			if (!pmd_large(*pmdp)) {
1058				rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1059						      bits);
1060				if (!rc) {
1061					len -= PAGE_SIZE;
1062					gaddr += PAGE_SIZE;
1063				}
1064			} else {
1065				rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1066						      bits);
1067				if (!rc) {
1068					dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1069					len = len < dist ? 0 : len - dist;
1070					gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1071				}
1072			}
1073			gmap_pmd_op_end(gmap, pmdp);
1074		}
1075		if (rc) {
1076			if (rc == -EINVAL)
1077				return rc;
1078
1079			/* -EAGAIN, fixup of userspace mm and gmap */
1080			vmaddr = __gmap_translate(gmap, gaddr);
1081			if (IS_ERR_VALUE(vmaddr))
1082				return vmaddr;
1083			rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1084			if (rc)
1085				return rc;
 
1086		}
 
 
1087	}
1088	return 0;
1089}
1090
1091/**
1092 * gmap_mprotect_notify - change access rights for a range of ptes and
1093 *                        call the notifier if any pte changes again
1094 * @gmap: pointer to guest mapping meta data structure
1095 * @gaddr: virtual address in the guest address space
1096 * @len: size of area
1097 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1098 *
1099 * Returns 0 if for each page in the given range a gmap mapping exists,
1100 * the new access rights could be set and the notifier could be armed.
1101 * If the gmap mapping is missing for one or more pages -EFAULT is
1102 * returned. If no memory could be allocated -ENOMEM is returned.
1103 * This function establishes missing page table entries.
1104 */
1105int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1106			 unsigned long len, int prot)
1107{
1108	int rc;
1109
1110	if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1111		return -EINVAL;
1112	if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1113		return -EINVAL;
1114	mmap_read_lock(gmap->mm);
1115	rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1116	mmap_read_unlock(gmap->mm);
1117	return rc;
1118}
1119EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1120
1121/**
1122 * gmap_read_table - get an unsigned long value from a guest page table using
1123 *                   absolute addressing, without marking the page referenced.
1124 * @gmap: pointer to guest mapping meta data structure
1125 * @gaddr: virtual address in the guest address space
1126 * @val: pointer to the unsigned long value to return
1127 *
1128 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1129 * if reading using the virtual address failed. -EINVAL if called on a gmap
1130 * shadow.
1131 *
1132 * Called with gmap->mm->mmap_lock in read.
1133 */
1134int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1135{
1136	unsigned long address, vmaddr;
1137	spinlock_t *ptl;
1138	pte_t *ptep, pte;
1139	int rc;
1140
1141	if (gmap_is_shadow(gmap))
1142		return -EINVAL;
1143
1144	while (1) {
1145		rc = -EAGAIN;
1146		ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1147		if (ptep) {
1148			pte = *ptep;
1149			if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1150				address = pte_val(pte) & PAGE_MASK;
1151				address += gaddr & ~PAGE_MASK;
1152				*val = *(unsigned long *)__va(address);
1153				set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
1154				/* Do *NOT* clear the _PAGE_INVALID bit! */
1155				rc = 0;
1156			}
1157			gmap_pte_op_end(ptl);
1158		}
1159		if (!rc)
1160			break;
1161		vmaddr = __gmap_translate(gmap, gaddr);
1162		if (IS_ERR_VALUE(vmaddr)) {
1163			rc = vmaddr;
1164			break;
1165		}
1166		rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1167		if (rc)
1168			break;
1169	}
1170	return rc;
1171}
1172EXPORT_SYMBOL_GPL(gmap_read_table);
1173
1174/**
1175 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1176 * @sg: pointer to the shadow guest address space structure
1177 * @vmaddr: vm address associated with the rmap
1178 * @rmap: pointer to the rmap structure
1179 *
1180 * Called with the sg->guest_table_lock
1181 */
1182static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1183				    struct gmap_rmap *rmap)
1184{
1185	struct gmap_rmap *temp;
1186	void __rcu **slot;
1187
1188	BUG_ON(!gmap_is_shadow(sg));
1189	slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1190	if (slot) {
1191		rmap->next = radix_tree_deref_slot_protected(slot,
1192							&sg->guest_table_lock);
1193		for (temp = rmap->next; temp; temp = temp->next) {
1194			if (temp->raddr == rmap->raddr) {
1195				kfree(rmap);
1196				return;
1197			}
1198		}
1199		radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1200	} else {
1201		rmap->next = NULL;
1202		radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1203				  rmap);
1204	}
1205}
1206
1207/**
1208 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1209 * @sg: pointer to the shadow guest address space structure
1210 * @raddr: rmap address in the shadow gmap
1211 * @paddr: address in the parent guest address space
1212 * @len: length of the memory area to protect
 
1213 *
1214 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1215 * if out of memory and -EFAULT if paddr is invalid.
1216 */
1217static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1218			     unsigned long paddr, unsigned long len)
1219{
1220	struct gmap *parent;
1221	struct gmap_rmap *rmap;
1222	unsigned long vmaddr;
1223	spinlock_t *ptl;
1224	pte_t *ptep;
1225	int rc;
1226
1227	BUG_ON(!gmap_is_shadow(sg));
1228	parent = sg->parent;
1229	while (len) {
1230		vmaddr = __gmap_translate(parent, paddr);
1231		if (IS_ERR_VALUE(vmaddr))
1232			return vmaddr;
1233		rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1234		if (!rmap)
1235			return -ENOMEM;
1236		rmap->raddr = raddr;
1237		rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1238		if (rc) {
1239			kfree(rmap);
1240			return rc;
1241		}
1242		rc = -EAGAIN;
1243		ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1244		if (ptep) {
1245			spin_lock(&sg->guest_table_lock);
1246			rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1247					     PGSTE_VSIE_BIT);
1248			if (!rc)
1249				gmap_insert_rmap(sg, vmaddr, rmap);
1250			spin_unlock(&sg->guest_table_lock);
1251			gmap_pte_op_end(ptl);
1252		}
1253		radix_tree_preload_end();
1254		if (rc) {
1255			kfree(rmap);
1256			rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1257			if (rc)
1258				return rc;
1259			continue;
1260		}
1261		paddr += PAGE_SIZE;
1262		len -= PAGE_SIZE;
1263	}
1264	return 0;
1265}
1266
1267#define _SHADOW_RMAP_MASK	0x7
1268#define _SHADOW_RMAP_REGION1	0x5
1269#define _SHADOW_RMAP_REGION2	0x4
1270#define _SHADOW_RMAP_REGION3	0x3
1271#define _SHADOW_RMAP_SEGMENT	0x2
1272#define _SHADOW_RMAP_PGTABLE	0x1
1273
1274/**
1275 * gmap_idte_one - invalidate a single region or segment table entry
1276 * @asce: region or segment table *origin* + table-type bits
1277 * @vaddr: virtual address to identify the table entry to flush
1278 *
1279 * The invalid bit of a single region or segment table entry is set
1280 * and the associated TLB entries depending on the entry are flushed.
1281 * The table-type of the @asce identifies the portion of the @vaddr
1282 * that is used as the invalidation index.
1283 */
1284static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1285{
1286	asm volatile(
1287		"	idte	%0,0,%1"
1288		: : "a" (asce), "a" (vaddr) : "cc", "memory");
1289}
1290
1291/**
1292 * gmap_unshadow_page - remove a page from a shadow page table
1293 * @sg: pointer to the shadow guest address space structure
1294 * @raddr: rmap address in the shadow guest address space
1295 *
1296 * Called with the sg->guest_table_lock
1297 */
1298static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1299{
1300	unsigned long *table;
1301
1302	BUG_ON(!gmap_is_shadow(sg));
1303	table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1304	if (!table || *table & _PAGE_INVALID)
1305		return;
1306	gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1307	ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1308}
1309
1310/**
1311 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1312 * @sg: pointer to the shadow guest address space structure
1313 * @raddr: rmap address in the shadow guest address space
1314 * @pgt: pointer to the start of a shadow page table
1315 *
1316 * Called with the sg->guest_table_lock
1317 */
1318static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1319				unsigned long *pgt)
1320{
1321	int i;
1322
1323	BUG_ON(!gmap_is_shadow(sg));
1324	for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1325		pgt[i] = _PAGE_INVALID;
1326}
1327
1328/**
1329 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1330 * @sg: pointer to the shadow guest address space structure
1331 * @raddr: address in the shadow guest address space
1332 *
1333 * Called with the sg->guest_table_lock
1334 */
1335static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1336{
1337	unsigned long *ste;
1338	phys_addr_t sto, pgt;
1339	struct page *page;
1340
1341	BUG_ON(!gmap_is_shadow(sg));
1342	ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1343	if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1344		return;
1345	gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1346	sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1347	gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1348	pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
1349	*ste = _SEGMENT_ENTRY_EMPTY;
1350	__gmap_unshadow_pgt(sg, raddr, __va(pgt));
1351	/* Free page table */
1352	page = phys_to_page(pgt);
1353	list_del(&page->lru);
1354	page_table_free_pgste(page);
1355}
1356
1357/**
1358 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1359 * @sg: pointer to the shadow guest address space structure
1360 * @raddr: rmap address in the shadow guest address space
1361 * @sgt: pointer to the start of a shadow segment table
1362 *
1363 * Called with the sg->guest_table_lock
1364 */
1365static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1366				unsigned long *sgt)
1367{
 
1368	struct page *page;
1369	phys_addr_t pgt;
1370	int i;
1371
1372	BUG_ON(!gmap_is_shadow(sg));
1373	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
 
1374		if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1375			continue;
1376		pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
1377		sgt[i] = _SEGMENT_ENTRY_EMPTY;
1378		__gmap_unshadow_pgt(sg, raddr, __va(pgt));
1379		/* Free page table */
1380		page = phys_to_page(pgt);
1381		list_del(&page->lru);
1382		page_table_free_pgste(page);
1383	}
1384}
1385
1386/**
1387 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1388 * @sg: pointer to the shadow guest address space structure
1389 * @raddr: rmap address in the shadow guest address space
1390 *
1391 * Called with the shadow->guest_table_lock
1392 */
1393static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1394{
1395	unsigned long r3o, *r3e;
1396	phys_addr_t sgt;
1397	struct page *page;
1398
1399	BUG_ON(!gmap_is_shadow(sg));
1400	r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1401	if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1402		return;
1403	gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1404	r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1405	gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
1406	sgt = *r3e & _REGION_ENTRY_ORIGIN;
1407	*r3e = _REGION3_ENTRY_EMPTY;
1408	__gmap_unshadow_sgt(sg, raddr, __va(sgt));
1409	/* Free segment table */
1410	page = phys_to_page(sgt);
1411	list_del(&page->lru);
1412	__free_pages(page, CRST_ALLOC_ORDER);
1413}
1414
1415/**
1416 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1417 * @sg: pointer to the shadow guest address space structure
1418 * @raddr: address in the shadow guest address space
1419 * @r3t: pointer to the start of a shadow region-3 table
1420 *
1421 * Called with the sg->guest_table_lock
1422 */
1423static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1424				unsigned long *r3t)
1425{
 
1426	struct page *page;
1427	phys_addr_t sgt;
1428	int i;
1429
1430	BUG_ON(!gmap_is_shadow(sg));
1431	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
 
1432		if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1433			continue;
1434		sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
1435		r3t[i] = _REGION3_ENTRY_EMPTY;
1436		__gmap_unshadow_sgt(sg, raddr, __va(sgt));
1437		/* Free segment table */
1438		page = phys_to_page(sgt);
1439		list_del(&page->lru);
1440		__free_pages(page, CRST_ALLOC_ORDER);
1441	}
1442}
1443
1444/**
1445 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1446 * @sg: pointer to the shadow guest address space structure
1447 * @raddr: rmap address in the shadow guest address space
1448 *
1449 * Called with the sg->guest_table_lock
1450 */
1451static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1452{
1453	unsigned long r2o, *r2e;
1454	phys_addr_t r3t;
1455	struct page *page;
1456
1457	BUG_ON(!gmap_is_shadow(sg));
1458	r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1459	if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1460		return;
1461	gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1462	r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1463	gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
1464	r3t = *r2e & _REGION_ENTRY_ORIGIN;
1465	*r2e = _REGION2_ENTRY_EMPTY;
1466	__gmap_unshadow_r3t(sg, raddr, __va(r3t));
1467	/* Free region 3 table */
1468	page = phys_to_page(r3t);
1469	list_del(&page->lru);
1470	__free_pages(page, CRST_ALLOC_ORDER);
1471}
1472
1473/**
1474 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1475 * @sg: pointer to the shadow guest address space structure
1476 * @raddr: rmap address in the shadow guest address space
1477 * @r2t: pointer to the start of a shadow region-2 table
1478 *
1479 * Called with the sg->guest_table_lock
1480 */
1481static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1482				unsigned long *r2t)
1483{
1484	phys_addr_t r3t;
1485	struct page *page;
1486	int i;
1487
1488	BUG_ON(!gmap_is_shadow(sg));
1489	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
 
1490		if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1491			continue;
1492		r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
1493		r2t[i] = _REGION2_ENTRY_EMPTY;
1494		__gmap_unshadow_r3t(sg, raddr, __va(r3t));
1495		/* Free region 3 table */
1496		page = phys_to_page(r3t);
1497		list_del(&page->lru);
1498		__free_pages(page, CRST_ALLOC_ORDER);
1499	}
1500}
1501
1502/**
1503 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1504 * @sg: pointer to the shadow guest address space structure
1505 * @raddr: rmap address in the shadow guest address space
1506 *
1507 * Called with the sg->guest_table_lock
1508 */
1509static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1510{
1511	unsigned long r1o, *r1e;
1512	struct page *page;
1513	phys_addr_t r2t;
1514
1515	BUG_ON(!gmap_is_shadow(sg));
1516	r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1517	if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1518		return;
1519	gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1520	r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1521	gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
1522	r2t = *r1e & _REGION_ENTRY_ORIGIN;
1523	*r1e = _REGION1_ENTRY_EMPTY;
1524	__gmap_unshadow_r2t(sg, raddr, __va(r2t));
1525	/* Free region 2 table */
1526	page = phys_to_page(r2t);
1527	list_del(&page->lru);
1528	__free_pages(page, CRST_ALLOC_ORDER);
1529}
1530
1531/**
1532 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1533 * @sg: pointer to the shadow guest address space structure
1534 * @raddr: rmap address in the shadow guest address space
1535 * @r1t: pointer to the start of a shadow region-1 table
1536 *
1537 * Called with the shadow->guest_table_lock
1538 */
1539static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1540				unsigned long *r1t)
1541{
1542	unsigned long asce;
1543	struct page *page;
1544	phys_addr_t r2t;
1545	int i;
1546
1547	BUG_ON(!gmap_is_shadow(sg));
1548	asce = __pa(r1t) | _ASCE_TYPE_REGION1;
1549	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1550		if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1551			continue;
1552		r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
1553		__gmap_unshadow_r2t(sg, raddr, __va(r2t));
1554		/* Clear entry and flush translation r1t -> r2t */
1555		gmap_idte_one(asce, raddr);
1556		r1t[i] = _REGION1_ENTRY_EMPTY;
1557		/* Free region 2 table */
1558		page = phys_to_page(r2t);
1559		list_del(&page->lru);
1560		__free_pages(page, CRST_ALLOC_ORDER);
1561	}
1562}
1563
1564/**
1565 * gmap_unshadow - remove a shadow page table completely
1566 * @sg: pointer to the shadow guest address space structure
1567 *
1568 * Called with sg->guest_table_lock
1569 */
1570static void gmap_unshadow(struct gmap *sg)
1571{
1572	unsigned long *table;
1573
1574	BUG_ON(!gmap_is_shadow(sg));
1575	if (sg->removed)
1576		return;
1577	sg->removed = 1;
1578	gmap_call_notifier(sg, 0, -1UL);
1579	gmap_flush_tlb(sg);
1580	table = __va(sg->asce & _ASCE_ORIGIN);
1581	switch (sg->asce & _ASCE_TYPE_MASK) {
1582	case _ASCE_TYPE_REGION1:
1583		__gmap_unshadow_r1t(sg, 0, table);
1584		break;
1585	case _ASCE_TYPE_REGION2:
1586		__gmap_unshadow_r2t(sg, 0, table);
1587		break;
1588	case _ASCE_TYPE_REGION3:
1589		__gmap_unshadow_r3t(sg, 0, table);
1590		break;
1591	case _ASCE_TYPE_SEGMENT:
1592		__gmap_unshadow_sgt(sg, 0, table);
1593		break;
1594	}
1595}
1596
1597/**
1598 * gmap_find_shadow - find a specific asce in the list of shadow tables
1599 * @parent: pointer to the parent gmap
1600 * @asce: ASCE for which the shadow table is created
1601 * @edat_level: edat level to be used for the shadow translation
1602 *
1603 * Returns the pointer to a gmap if a shadow table with the given asce is
1604 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1605 * otherwise NULL
1606 */
1607static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1608				     int edat_level)
1609{
1610	struct gmap *sg;
1611
1612	list_for_each_entry(sg, &parent->children, list) {
1613		if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1614		    sg->removed)
1615			continue;
1616		if (!sg->initialized)
1617			return ERR_PTR(-EAGAIN);
1618		refcount_inc(&sg->ref_count);
1619		return sg;
1620	}
1621	return NULL;
1622}
1623
1624/**
1625 * gmap_shadow_valid - check if a shadow guest address space matches the
1626 *                     given properties and is still valid
1627 * @sg: pointer to the shadow guest address space structure
1628 * @asce: ASCE for which the shadow table is requested
1629 * @edat_level: edat level to be used for the shadow translation
1630 *
1631 * Returns 1 if the gmap shadow is still valid and matches the given
1632 * properties, the caller can continue using it. Returns 0 otherwise, the
1633 * caller has to request a new shadow gmap in this case.
1634 *
1635 */
1636int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1637{
1638	if (sg->removed)
1639		return 0;
1640	return sg->orig_asce == asce && sg->edat_level == edat_level;
1641}
1642EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1643
1644/**
1645 * gmap_shadow - create/find a shadow guest address space
1646 * @parent: pointer to the parent gmap
1647 * @asce: ASCE for which the shadow table is created
1648 * @edat_level: edat level to be used for the shadow translation
1649 *
1650 * The pages of the top level page table referred by the asce parameter
1651 * will be set to read-only and marked in the PGSTEs of the kvm process.
1652 * The shadow table will be removed automatically on any change to the
1653 * PTE mapping for the source table.
1654 *
1655 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1656 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1657 * parent gmap table could not be protected.
1658 */
1659struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1660			 int edat_level)
1661{
1662	struct gmap *sg, *new;
1663	unsigned long limit;
1664	int rc;
1665
1666	BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1667	BUG_ON(gmap_is_shadow(parent));
1668	spin_lock(&parent->shadow_lock);
1669	sg = gmap_find_shadow(parent, asce, edat_level);
1670	spin_unlock(&parent->shadow_lock);
1671	if (sg)
1672		return sg;
1673	/* Create a new shadow gmap */
1674	limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1675	if (asce & _ASCE_REAL_SPACE)
1676		limit = -1UL;
1677	new = gmap_alloc(limit);
1678	if (!new)
1679		return ERR_PTR(-ENOMEM);
1680	new->mm = parent->mm;
1681	new->parent = gmap_get(parent);
1682	new->orig_asce = asce;
1683	new->edat_level = edat_level;
1684	new->initialized = false;
1685	spin_lock(&parent->shadow_lock);
1686	/* Recheck if another CPU created the same shadow */
1687	sg = gmap_find_shadow(parent, asce, edat_level);
1688	if (sg) {
1689		spin_unlock(&parent->shadow_lock);
1690		gmap_free(new);
1691		return sg;
1692	}
1693	if (asce & _ASCE_REAL_SPACE) {
1694		/* only allow one real-space gmap shadow */
1695		list_for_each_entry(sg, &parent->children, list) {
1696			if (sg->orig_asce & _ASCE_REAL_SPACE) {
1697				spin_lock(&sg->guest_table_lock);
1698				gmap_unshadow(sg);
1699				spin_unlock(&sg->guest_table_lock);
1700				list_del(&sg->list);
1701				gmap_put(sg);
1702				break;
1703			}
1704		}
1705	}
1706	refcount_set(&new->ref_count, 2);
1707	list_add(&new->list, &parent->children);
1708	if (asce & _ASCE_REAL_SPACE) {
1709		/* nothing to protect, return right away */
1710		new->initialized = true;
1711		spin_unlock(&parent->shadow_lock);
1712		return new;
1713	}
1714	spin_unlock(&parent->shadow_lock);
1715	/* protect after insertion, so it will get properly invalidated */
1716	mmap_read_lock(parent->mm);
1717	rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1718				((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1719				PROT_READ, GMAP_NOTIFY_SHADOW);
1720	mmap_read_unlock(parent->mm);
1721	spin_lock(&parent->shadow_lock);
1722	new->initialized = true;
1723	if (rc) {
1724		list_del(&new->list);
1725		gmap_free(new);
1726		new = ERR_PTR(rc);
1727	}
1728	spin_unlock(&parent->shadow_lock);
1729	return new;
1730}
1731EXPORT_SYMBOL_GPL(gmap_shadow);
1732
1733/**
1734 * gmap_shadow_r2t - create an empty shadow region 2 table
1735 * @sg: pointer to the shadow guest address space structure
1736 * @saddr: faulting address in the shadow gmap
1737 * @r2t: parent gmap address of the region 2 table to get shadowed
1738 * @fake: r2t references contiguous guest memory block, not a r2t
1739 *
1740 * The r2t parameter specifies the address of the source table. The
1741 * four pages of the source table are made read-only in the parent gmap
1742 * address space. A write to the source table area @r2t will automatically
1743 * remove the shadow r2 table and all of its decendents.
1744 *
1745 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1746 * shadow table structure is incomplete, -ENOMEM if out of memory and
1747 * -EFAULT if an address in the parent gmap could not be resolved.
1748 *
1749 * Called with sg->mm->mmap_lock in read.
1750 */
1751int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1752		    int fake)
1753{
1754	unsigned long raddr, origin, offset, len;
1755	unsigned long *table;
1756	phys_addr_t s_r2t;
1757	struct page *page;
1758	int rc;
1759
1760	BUG_ON(!gmap_is_shadow(sg));
1761	/* Allocate a shadow region second table */
1762	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1763	if (!page)
1764		return -ENOMEM;
1765	page->index = r2t & _REGION_ENTRY_ORIGIN;
1766	if (fake)
1767		page->index |= GMAP_SHADOW_FAKE_TABLE;
1768	s_r2t = page_to_phys(page);
1769	/* Install shadow region second table */
1770	spin_lock(&sg->guest_table_lock);
1771	table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1772	if (!table) {
1773		rc = -EAGAIN;		/* Race with unshadow */
1774		goto out_free;
1775	}
1776	if (!(*table & _REGION_ENTRY_INVALID)) {
1777		rc = 0;			/* Already established */
1778		goto out_free;
1779	} else if (*table & _REGION_ENTRY_ORIGIN) {
1780		rc = -EAGAIN;		/* Race with shadow */
1781		goto out_free;
1782	}
1783	crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
1784	/* mark as invalid as long as the parent table is not protected */
1785	*table = s_r2t | _REGION_ENTRY_LENGTH |
1786		 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1787	if (sg->edat_level >= 1)
1788		*table |= (r2t & _REGION_ENTRY_PROTECT);
1789	list_add(&page->lru, &sg->crst_list);
1790	if (fake) {
1791		/* nothing to protect for fake tables */
1792		*table &= ~_REGION_ENTRY_INVALID;
1793		spin_unlock(&sg->guest_table_lock);
1794		return 0;
1795	}
1796	spin_unlock(&sg->guest_table_lock);
1797	/* Make r2t read-only in parent gmap page table */
1798	raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1799	origin = r2t & _REGION_ENTRY_ORIGIN;
1800	offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1801	len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1802	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1803	spin_lock(&sg->guest_table_lock);
1804	if (!rc) {
1805		table = gmap_table_walk(sg, saddr, 4);
1806		if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
 
1807			rc = -EAGAIN;		/* Race with unshadow */
1808		else
1809			*table &= ~_REGION_ENTRY_INVALID;
1810	} else {
1811		gmap_unshadow_r2t(sg, raddr);
1812	}
1813	spin_unlock(&sg->guest_table_lock);
1814	return rc;
1815out_free:
1816	spin_unlock(&sg->guest_table_lock);
1817	__free_pages(page, CRST_ALLOC_ORDER);
1818	return rc;
1819}
1820EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1821
1822/**
1823 * gmap_shadow_r3t - create a shadow region 3 table
1824 * @sg: pointer to the shadow guest address space structure
1825 * @saddr: faulting address in the shadow gmap
1826 * @r3t: parent gmap address of the region 3 table to get shadowed
1827 * @fake: r3t references contiguous guest memory block, not a r3t
1828 *
1829 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1830 * shadow table structure is incomplete, -ENOMEM if out of memory and
1831 * -EFAULT if an address in the parent gmap could not be resolved.
1832 *
1833 * Called with sg->mm->mmap_lock in read.
1834 */
1835int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1836		    int fake)
1837{
1838	unsigned long raddr, origin, offset, len;
1839	unsigned long *table;
1840	phys_addr_t s_r3t;
1841	struct page *page;
1842	int rc;
1843
1844	BUG_ON(!gmap_is_shadow(sg));
1845	/* Allocate a shadow region second table */
1846	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1847	if (!page)
1848		return -ENOMEM;
1849	page->index = r3t & _REGION_ENTRY_ORIGIN;
1850	if (fake)
1851		page->index |= GMAP_SHADOW_FAKE_TABLE;
1852	s_r3t = page_to_phys(page);
1853	/* Install shadow region second table */
1854	spin_lock(&sg->guest_table_lock);
1855	table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1856	if (!table) {
1857		rc = -EAGAIN;		/* Race with unshadow */
1858		goto out_free;
1859	}
1860	if (!(*table & _REGION_ENTRY_INVALID)) {
1861		rc = 0;			/* Already established */
1862		goto out_free;
1863	} else if (*table & _REGION_ENTRY_ORIGIN) {
1864		rc = -EAGAIN;		/* Race with shadow */
1865		goto out_free;
1866	}
1867	crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
1868	/* mark as invalid as long as the parent table is not protected */
1869	*table = s_r3t | _REGION_ENTRY_LENGTH |
1870		 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1871	if (sg->edat_level >= 1)
1872		*table |= (r3t & _REGION_ENTRY_PROTECT);
1873	list_add(&page->lru, &sg->crst_list);
1874	if (fake) {
1875		/* nothing to protect for fake tables */
1876		*table &= ~_REGION_ENTRY_INVALID;
1877		spin_unlock(&sg->guest_table_lock);
1878		return 0;
1879	}
1880	spin_unlock(&sg->guest_table_lock);
1881	/* Make r3t read-only in parent gmap page table */
1882	raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1883	origin = r3t & _REGION_ENTRY_ORIGIN;
1884	offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1885	len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1886	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1887	spin_lock(&sg->guest_table_lock);
1888	if (!rc) {
1889		table = gmap_table_walk(sg, saddr, 3);
1890		if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
 
1891			rc = -EAGAIN;		/* Race with unshadow */
1892		else
1893			*table &= ~_REGION_ENTRY_INVALID;
1894	} else {
1895		gmap_unshadow_r3t(sg, raddr);
1896	}
1897	spin_unlock(&sg->guest_table_lock);
1898	return rc;
1899out_free:
1900	spin_unlock(&sg->guest_table_lock);
1901	__free_pages(page, CRST_ALLOC_ORDER);
1902	return rc;
1903}
1904EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1905
1906/**
1907 * gmap_shadow_sgt - create a shadow segment table
1908 * @sg: pointer to the shadow guest address space structure
1909 * @saddr: faulting address in the shadow gmap
1910 * @sgt: parent gmap address of the segment table to get shadowed
1911 * @fake: sgt references contiguous guest memory block, not a sgt
1912 *
1913 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1914 * shadow table structure is incomplete, -ENOMEM if out of memory and
1915 * -EFAULT if an address in the parent gmap could not be resolved.
1916 *
1917 * Called with sg->mm->mmap_lock in read.
1918 */
1919int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1920		    int fake)
1921{
1922	unsigned long raddr, origin, offset, len;
1923	unsigned long *table;
1924	phys_addr_t s_sgt;
1925	struct page *page;
1926	int rc;
1927
1928	BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1929	/* Allocate a shadow segment table */
1930	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1931	if (!page)
1932		return -ENOMEM;
1933	page->index = sgt & _REGION_ENTRY_ORIGIN;
1934	if (fake)
1935		page->index |= GMAP_SHADOW_FAKE_TABLE;
1936	s_sgt = page_to_phys(page);
1937	/* Install shadow region second table */
1938	spin_lock(&sg->guest_table_lock);
1939	table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1940	if (!table) {
1941		rc = -EAGAIN;		/* Race with unshadow */
1942		goto out_free;
1943	}
1944	if (!(*table & _REGION_ENTRY_INVALID)) {
1945		rc = 0;			/* Already established */
1946		goto out_free;
1947	} else if (*table & _REGION_ENTRY_ORIGIN) {
1948		rc = -EAGAIN;		/* Race with shadow */
1949		goto out_free;
1950	}
1951	crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
1952	/* mark as invalid as long as the parent table is not protected */
1953	*table = s_sgt | _REGION_ENTRY_LENGTH |
1954		 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1955	if (sg->edat_level >= 1)
1956		*table |= sgt & _REGION_ENTRY_PROTECT;
1957	list_add(&page->lru, &sg->crst_list);
1958	if (fake) {
1959		/* nothing to protect for fake tables */
1960		*table &= ~_REGION_ENTRY_INVALID;
1961		spin_unlock(&sg->guest_table_lock);
1962		return 0;
1963	}
1964	spin_unlock(&sg->guest_table_lock);
1965	/* Make sgt read-only in parent gmap page table */
1966	raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1967	origin = sgt & _REGION_ENTRY_ORIGIN;
1968	offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1969	len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1970	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1971	spin_lock(&sg->guest_table_lock);
1972	if (!rc) {
1973		table = gmap_table_walk(sg, saddr, 2);
1974		if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
 
1975			rc = -EAGAIN;		/* Race with unshadow */
1976		else
1977			*table &= ~_REGION_ENTRY_INVALID;
1978	} else {
1979		gmap_unshadow_sgt(sg, raddr);
1980	}
1981	spin_unlock(&sg->guest_table_lock);
1982	return rc;
1983out_free:
1984	spin_unlock(&sg->guest_table_lock);
1985	__free_pages(page, CRST_ALLOC_ORDER);
1986	return rc;
1987}
1988EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1989
1990/**
1991 * gmap_shadow_pgt_lookup - find a shadow page table
1992 * @sg: pointer to the shadow guest address space structure
1993 * @saddr: the address in the shadow aguest address space
1994 * @pgt: parent gmap address of the page table to get shadowed
1995 * @dat_protection: if the pgtable is marked as protected by dat
1996 * @fake: pgt references contiguous guest memory block, not a pgtable
1997 *
1998 * Returns 0 if the shadow page table was found and -EAGAIN if the page
1999 * table was not found.
2000 *
2001 * Called with sg->mm->mmap_lock in read.
2002 */
2003int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
2004			   unsigned long *pgt, int *dat_protection,
2005			   int *fake)
2006{
2007	unsigned long *table;
2008	struct page *page;
2009	int rc;
2010
2011	BUG_ON(!gmap_is_shadow(sg));
2012	spin_lock(&sg->guest_table_lock);
2013	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2014	if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2015		/* Shadow page tables are full pages (pte+pgste) */
2016		page = pfn_to_page(*table >> PAGE_SHIFT);
2017		*pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2018		*dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2019		*fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2020		rc = 0;
2021	} else  {
2022		rc = -EAGAIN;
2023	}
2024	spin_unlock(&sg->guest_table_lock);
2025	return rc;
2026
2027}
2028EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2029
2030/**
2031 * gmap_shadow_pgt - instantiate a shadow page table
2032 * @sg: pointer to the shadow guest address space structure
2033 * @saddr: faulting address in the shadow gmap
2034 * @pgt: parent gmap address of the page table to get shadowed
2035 * @fake: pgt references contiguous guest memory block, not a pgtable
2036 *
2037 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2038 * shadow table structure is incomplete, -ENOMEM if out of memory,
2039 * -EFAULT if an address in the parent gmap could not be resolved and
2040 *
2041 * Called with gmap->mm->mmap_lock in read
2042 */
2043int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2044		    int fake)
2045{
2046	unsigned long raddr, origin;
2047	unsigned long *table;
2048	struct page *page;
2049	phys_addr_t s_pgt;
2050	int rc;
2051
2052	BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2053	/* Allocate a shadow page table */
2054	page = page_table_alloc_pgste(sg->mm);
2055	if (!page)
2056		return -ENOMEM;
2057	page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
2058	if (fake)
2059		page->index |= GMAP_SHADOW_FAKE_TABLE;
2060	s_pgt = page_to_phys(page);
2061	/* Install shadow page table */
2062	spin_lock(&sg->guest_table_lock);
2063	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2064	if (!table) {
2065		rc = -EAGAIN;		/* Race with unshadow */
2066		goto out_free;
2067	}
2068	if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2069		rc = 0;			/* Already established */
2070		goto out_free;
2071	} else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2072		rc = -EAGAIN;		/* Race with shadow */
2073		goto out_free;
2074	}
2075	/* mark as invalid as long as the parent table is not protected */
2076	*table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2077		 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2078	list_add(&page->lru, &sg->pt_list);
2079	if (fake) {
2080		/* nothing to protect for fake tables */
2081		*table &= ~_SEGMENT_ENTRY_INVALID;
2082		spin_unlock(&sg->guest_table_lock);
2083		return 0;
2084	}
2085	spin_unlock(&sg->guest_table_lock);
2086	/* Make pgt read-only in parent gmap page table (not the pgste) */
2087	raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2088	origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2089	rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2090	spin_lock(&sg->guest_table_lock);
2091	if (!rc) {
2092		table = gmap_table_walk(sg, saddr, 1);
2093		if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
 
2094			rc = -EAGAIN;		/* Race with unshadow */
2095		else
2096			*table &= ~_SEGMENT_ENTRY_INVALID;
2097	} else {
2098		gmap_unshadow_pgt(sg, raddr);
2099	}
2100	spin_unlock(&sg->guest_table_lock);
2101	return rc;
2102out_free:
2103	spin_unlock(&sg->guest_table_lock);
2104	page_table_free_pgste(page);
2105	return rc;
2106
2107}
2108EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2109
2110/**
2111 * gmap_shadow_page - create a shadow page mapping
2112 * @sg: pointer to the shadow guest address space structure
2113 * @saddr: faulting address in the shadow gmap
2114 * @pte: pte in parent gmap address space to get shadowed
2115 *
2116 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2117 * shadow table structure is incomplete, -ENOMEM if out of memory and
2118 * -EFAULT if an address in the parent gmap could not be resolved.
2119 *
2120 * Called with sg->mm->mmap_lock in read.
2121 */
2122int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2123{
2124	struct gmap *parent;
2125	struct gmap_rmap *rmap;
2126	unsigned long vmaddr, paddr;
2127	spinlock_t *ptl;
2128	pte_t *sptep, *tptep;
2129	int prot;
2130	int rc;
2131
2132	BUG_ON(!gmap_is_shadow(sg));
2133	parent = sg->parent;
2134	prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2135
2136	rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2137	if (!rmap)
2138		return -ENOMEM;
2139	rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2140
2141	while (1) {
2142		paddr = pte_val(pte) & PAGE_MASK;
2143		vmaddr = __gmap_translate(parent, paddr);
2144		if (IS_ERR_VALUE(vmaddr)) {
2145			rc = vmaddr;
2146			break;
2147		}
2148		rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2149		if (rc)
2150			break;
2151		rc = -EAGAIN;
2152		sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2153		if (sptep) {
2154			spin_lock(&sg->guest_table_lock);
2155			/* Get page table pointer */
2156			tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2157			if (!tptep) {
2158				spin_unlock(&sg->guest_table_lock);
2159				gmap_pte_op_end(ptl);
2160				radix_tree_preload_end();
2161				break;
2162			}
2163			rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2164			if (rc > 0) {
2165				/* Success and a new mapping */
2166				gmap_insert_rmap(sg, vmaddr, rmap);
2167				rmap = NULL;
2168				rc = 0;
2169			}
2170			gmap_pte_op_end(ptl);
2171			spin_unlock(&sg->guest_table_lock);
2172		}
2173		radix_tree_preload_end();
2174		if (!rc)
2175			break;
2176		rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2177		if (rc)
2178			break;
2179	}
2180	kfree(rmap);
2181	return rc;
2182}
2183EXPORT_SYMBOL_GPL(gmap_shadow_page);
2184
2185/*
2186 * gmap_shadow_notify - handle notifications for shadow gmap
2187 *
2188 * Called with sg->parent->shadow_lock.
2189 */
2190static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2191			       unsigned long gaddr)
2192{
2193	struct gmap_rmap *rmap, *rnext, *head;
2194	unsigned long start, end, bits, raddr;
 
2195
2196	BUG_ON(!gmap_is_shadow(sg));
 
 
 
 
 
 
 
2197
2198	spin_lock(&sg->guest_table_lock);
2199	if (sg->removed) {
2200		spin_unlock(&sg->guest_table_lock);
2201		return;
2202	}
2203	/* Check for top level table */
2204	start = sg->orig_asce & _ASCE_ORIGIN;
2205	end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2206	if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2207	    gaddr < end) {
2208		/* The complete shadow table has to go */
2209		gmap_unshadow(sg);
2210		spin_unlock(&sg->guest_table_lock);
2211		list_del(&sg->list);
2212		gmap_put(sg);
2213		return;
2214	}
2215	/* Remove the page table tree from on specific entry */
2216	head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2217	gmap_for_each_rmap_safe(rmap, rnext, head) {
2218		bits = rmap->raddr & _SHADOW_RMAP_MASK;
2219		raddr = rmap->raddr ^ bits;
2220		switch (bits) {
2221		case _SHADOW_RMAP_REGION1:
2222			gmap_unshadow_r2t(sg, raddr);
2223			break;
2224		case _SHADOW_RMAP_REGION2:
2225			gmap_unshadow_r3t(sg, raddr);
2226			break;
2227		case _SHADOW_RMAP_REGION3:
2228			gmap_unshadow_sgt(sg, raddr);
2229			break;
2230		case _SHADOW_RMAP_SEGMENT:
2231			gmap_unshadow_pgt(sg, raddr);
2232			break;
2233		case _SHADOW_RMAP_PGTABLE:
2234			gmap_unshadow_page(sg, raddr);
2235			break;
2236		}
2237		kfree(rmap);
2238	}
2239	spin_unlock(&sg->guest_table_lock);
2240}
2241
2242/**
2243 * ptep_notify - call all invalidation callbacks for a specific pte.
2244 * @mm: pointer to the process mm_struct
2245 * @vmaddr: virtual address in the process address space
2246 * @pte: pointer to the page table entry
2247 * @bits: bits from the pgste that caused the notify call
2248 *
2249 * This function is assumed to be called with the page table lock held
2250 * for the pte to notify.
2251 */
2252void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2253		 pte_t *pte, unsigned long bits)
2254{
2255	unsigned long offset, gaddr = 0;
2256	unsigned long *table;
2257	struct gmap *gmap, *sg, *next;
2258
2259	offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2260	offset = offset * (PAGE_SIZE / sizeof(pte_t));
2261	rcu_read_lock();
2262	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2263		spin_lock(&gmap->guest_table_lock);
2264		table = radix_tree_lookup(&gmap->host_to_guest,
2265					  vmaddr >> PMD_SHIFT);
2266		if (table)
2267			gaddr = __gmap_segment_gaddr(table) + offset;
2268		spin_unlock(&gmap->guest_table_lock);
2269		if (!table)
2270			continue;
2271
2272		if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2273			spin_lock(&gmap->shadow_lock);
2274			list_for_each_entry_safe(sg, next,
2275						 &gmap->children, list)
2276				gmap_shadow_notify(sg, vmaddr, gaddr);
2277			spin_unlock(&gmap->shadow_lock);
2278		}
2279		if (bits & PGSTE_IN_BIT)
2280			gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2281	}
2282	rcu_read_unlock();
2283}
2284EXPORT_SYMBOL_GPL(ptep_notify);
2285
2286static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2287			     unsigned long gaddr)
2288{
2289	set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
2290	gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2291}
2292
2293/**
2294 * gmap_pmdp_xchg - exchange a gmap pmd with another
2295 * @gmap: pointer to the guest address space structure
2296 * @pmdp: pointer to the pmd entry
2297 * @new: replacement entry
2298 * @gaddr: the affected guest address
2299 *
2300 * This function is assumed to be called with the guest_table_lock
2301 * held.
2302 */
2303static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2304			   unsigned long gaddr)
2305{
2306	gaddr &= HPAGE_MASK;
2307	pmdp_notify_gmap(gmap, pmdp, gaddr);
2308	new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
2309	if (MACHINE_HAS_TLB_GUEST)
2310		__pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2311			    IDTE_GLOBAL);
2312	else if (MACHINE_HAS_IDTE)
2313		__pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2314	else
2315		__pmdp_csp(pmdp);
2316	set_pmd(pmdp, new);
2317}
2318
2319static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2320			    int purge)
2321{
2322	pmd_t *pmdp;
2323	struct gmap *gmap;
2324	unsigned long gaddr;
2325
2326	rcu_read_lock();
2327	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2328		spin_lock(&gmap->guest_table_lock);
2329		pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2330						  vmaddr >> PMD_SHIFT);
2331		if (pmdp) {
2332			gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2333			pmdp_notify_gmap(gmap, pmdp, gaddr);
2334			WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2335						   _SEGMENT_ENTRY_GMAP_UC));
2336			if (purge)
2337				__pmdp_csp(pmdp);
2338			set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
2339		}
2340		spin_unlock(&gmap->guest_table_lock);
2341	}
2342	rcu_read_unlock();
2343}
2344
2345/**
2346 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2347 *                        flushing
2348 * @mm: pointer to the process mm_struct
2349 * @vmaddr: virtual address in the process address space
2350 */
2351void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2352{
2353	gmap_pmdp_clear(mm, vmaddr, 0);
2354}
2355EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2356
2357/**
2358 * gmap_pmdp_csp - csp all affected guest pmd entries
2359 * @mm: pointer to the process mm_struct
2360 * @vmaddr: virtual address in the process address space
2361 */
2362void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2363{
2364	gmap_pmdp_clear(mm, vmaddr, 1);
2365}
2366EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2367
2368/**
2369 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2370 * @mm: pointer to the process mm_struct
2371 * @vmaddr: virtual address in the process address space
2372 */
2373void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2374{
2375	unsigned long *entry, gaddr;
2376	struct gmap *gmap;
2377	pmd_t *pmdp;
2378
2379	rcu_read_lock();
2380	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2381		spin_lock(&gmap->guest_table_lock);
2382		entry = radix_tree_delete(&gmap->host_to_guest,
2383					  vmaddr >> PMD_SHIFT);
2384		if (entry) {
2385			pmdp = (pmd_t *)entry;
2386			gaddr = __gmap_segment_gaddr(entry);
2387			pmdp_notify_gmap(gmap, pmdp, gaddr);
2388			WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2389					   _SEGMENT_ENTRY_GMAP_UC));
2390			if (MACHINE_HAS_TLB_GUEST)
2391				__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2392					    gmap->asce, IDTE_LOCAL);
2393			else if (MACHINE_HAS_IDTE)
2394				__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2395			*entry = _SEGMENT_ENTRY_EMPTY;
2396		}
2397		spin_unlock(&gmap->guest_table_lock);
2398	}
2399	rcu_read_unlock();
2400}
2401EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2402
2403/**
2404 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2405 * @mm: pointer to the process mm_struct
2406 * @vmaddr: virtual address in the process address space
2407 */
2408void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2409{
2410	unsigned long *entry, gaddr;
2411	struct gmap *gmap;
2412	pmd_t *pmdp;
2413
2414	rcu_read_lock();
2415	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2416		spin_lock(&gmap->guest_table_lock);
2417		entry = radix_tree_delete(&gmap->host_to_guest,
2418					  vmaddr >> PMD_SHIFT);
2419		if (entry) {
2420			pmdp = (pmd_t *)entry;
2421			gaddr = __gmap_segment_gaddr(entry);
2422			pmdp_notify_gmap(gmap, pmdp, gaddr);
2423			WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2424					   _SEGMENT_ENTRY_GMAP_UC));
2425			if (MACHINE_HAS_TLB_GUEST)
2426				__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2427					    gmap->asce, IDTE_GLOBAL);
2428			else if (MACHINE_HAS_IDTE)
2429				__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2430			else
2431				__pmdp_csp(pmdp);
2432			*entry = _SEGMENT_ENTRY_EMPTY;
2433		}
2434		spin_unlock(&gmap->guest_table_lock);
 
 
2435	}
2436	rcu_read_unlock();
2437}
2438EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2439
2440/**
2441 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2442 * @gmap: pointer to guest address space
2443 * @pmdp: pointer to the pmd to be tested
2444 * @gaddr: virtual address in the guest address space
2445 *
2446 * This function is assumed to be called with the guest_table_lock
2447 * held.
2448 */
2449static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2450					  unsigned long gaddr)
2451{
2452	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2453		return false;
2454
2455	/* Already protected memory, which did not change is clean */
2456	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2457	    !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2458		return false;
2459
2460	/* Clear UC indication and reset protection */
2461	set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
2462	gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2463	return true;
2464}
2465
2466/**
2467 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2468 * @gmap: pointer to guest address space
2469 * @bitmap: dirty bitmap for this pmd
2470 * @gaddr: virtual address in the guest address space
2471 * @vmaddr: virtual address in the host address space
2472 *
2473 * This function is assumed to be called with the guest_table_lock
2474 * held.
2475 */
2476void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2477			     unsigned long gaddr, unsigned long vmaddr)
2478{
2479	int i;
2480	pmd_t *pmdp;
2481	pte_t *ptep;
2482	spinlock_t *ptl;
2483
2484	pmdp = gmap_pmd_op_walk(gmap, gaddr);
2485	if (!pmdp)
2486		return;
2487
2488	if (pmd_large(*pmdp)) {
2489		if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2490			bitmap_fill(bitmap, _PAGE_ENTRIES);
2491	} else {
2492		for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2493			ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2494			if (!ptep)
2495				continue;
2496			if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2497				set_bit(i, bitmap);
2498			spin_unlock(ptl);
2499		}
2500	}
2501	gmap_pmd_op_end(gmap, pmdp);
2502}
2503EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2504
2505#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2506static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2507				    unsigned long end, struct mm_walk *walk)
2508{
2509	struct vm_area_struct *vma = walk->vma;
2510
2511	split_huge_pmd(vma, pmd, addr);
2512	return 0;
2513}
2514
2515static const struct mm_walk_ops thp_split_walk_ops = {
2516	.pmd_entry	= thp_split_walk_pmd_entry,
2517};
2518
2519static inline void thp_split_mm(struct mm_struct *mm)
2520{
 
2521	struct vm_area_struct *vma;
2522	VMA_ITERATOR(vmi, mm, 0);
2523
2524	for_each_vma(vmi, vma) {
 
 
 
 
2525		vma->vm_flags &= ~VM_HUGEPAGE;
2526		vma->vm_flags |= VM_NOHUGEPAGE;
2527		walk_page_vma(vma, &thp_split_walk_ops, NULL);
2528	}
2529	mm->def_flags |= VM_NOHUGEPAGE;
 
2530}
2531#else
2532static inline void thp_split_mm(struct mm_struct *mm)
2533{
2534}
2535#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2536
2537/*
2538 * Remove all empty zero pages from the mapping for lazy refaulting
2539 * - This must be called after mm->context.has_pgste is set, to avoid
2540 *   future creation of zero pages
2541 * - This must be called after THP was enabled
2542 */
2543static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
2544			   unsigned long end, struct mm_walk *walk)
2545{
2546	unsigned long addr;
2547
2548	for (addr = start; addr != end; addr += PAGE_SIZE) {
2549		pte_t *ptep;
2550		spinlock_t *ptl;
2551
2552		ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
2553		if (is_zero_pfn(pte_pfn(*ptep)))
2554			ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
2555		pte_unmap_unlock(ptep, ptl);
2556	}
2557	return 0;
2558}
2559
2560static const struct mm_walk_ops zap_zero_walk_ops = {
2561	.pmd_entry	= __zap_zero_pages,
2562};
2563
2564/*
2565 * switch on pgstes for its userspace process (for kvm)
2566 */
2567int s390_enable_sie(void)
2568{
2569	struct mm_struct *mm = current->mm;
2570
2571	/* Do we have pgstes? if yes, we are done */
2572	if (mm_has_pgste(mm))
2573		return 0;
2574	/* Fail if the page tables are 2K */
2575	if (!mm_alloc_pgste(mm))
2576		return -EINVAL;
2577	mmap_write_lock(mm);
2578	mm->context.has_pgste = 1;
2579	/* split thp mappings and disable thp for future mappings */
2580	thp_split_mm(mm);
2581	walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
2582	mmap_write_unlock(mm);
2583	return 0;
2584}
2585EXPORT_SYMBOL_GPL(s390_enable_sie);
2586
2587int gmap_mark_unmergeable(void)
2588{
2589	struct mm_struct *mm = current->mm;
2590	struct vm_area_struct *vma;
2591	int ret;
2592	VMA_ITERATOR(vmi, mm, 0);
2593
2594	for_each_vma(vmi, vma) {
2595		ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
2596				  MADV_UNMERGEABLE, &vma->vm_flags);
2597		if (ret)
2598			return ret;
2599	}
2600	mm->def_flags &= ~VM_MERGEABLE;
2601	return 0;
2602}
2603EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);
2604
2605/*
2606 * Enable storage key handling from now on and initialize the storage
2607 * keys with the default key.
2608 */
2609static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2610				  unsigned long next, struct mm_walk *walk)
2611{
2612	/* Clear storage key */
2613	ptep_zap_key(walk->mm, addr, pte);
2614	return 0;
2615}
2616
2617/*
2618 * Give a chance to schedule after setting a key to 256 pages.
2619 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2620 * Both can sleep.
2621 */
2622static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2623				  unsigned long next, struct mm_walk *walk)
2624{
2625	cond_resched();
2626	return 0;
2627}
2628
2629static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2630				      unsigned long hmask, unsigned long next,
2631				      struct mm_walk *walk)
2632{
2633	pmd_t *pmd = (pmd_t *)pte;
2634	unsigned long start, end;
2635	struct page *page = pmd_page(*pmd);
2636
2637	/*
2638	 * The write check makes sure we do not set a key on shared
2639	 * memory. This is needed as the walker does not differentiate
2640	 * between actual guest memory and the process executable or
2641	 * shared libraries.
2642	 */
2643	if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2644	    !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2645		return 0;
2646
2647	start = pmd_val(*pmd) & HPAGE_MASK;
2648	end = start + HPAGE_SIZE - 1;
2649	__storage_key_init_range(start, end);
2650	set_bit(PG_arch_1, &page->flags);
2651	cond_resched();
2652	return 0;
2653}
2654
2655static const struct mm_walk_ops enable_skey_walk_ops = {
2656	.hugetlb_entry		= __s390_enable_skey_hugetlb,
2657	.pte_entry		= __s390_enable_skey_pte,
2658	.pmd_entry		= __s390_enable_skey_pmd,
2659};
2660
2661int s390_enable_skey(void)
2662{
 
2663	struct mm_struct *mm = current->mm;
 
2664	int rc = 0;
2665
2666	mmap_write_lock(mm);
2667	if (mm_uses_skeys(mm))
2668		goto out_up;
2669
2670	mm->context.uses_skeys = 1;
2671	rc = gmap_mark_unmergeable();
2672	if (rc) {
2673		mm->context.uses_skeys = 0;
2674		goto out_up;
 
 
 
2675	}
2676	walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
 
 
 
2677
2678out_up:
2679	mmap_write_unlock(mm);
2680	return rc;
2681}
2682EXPORT_SYMBOL_GPL(s390_enable_skey);
2683
2684/*
2685 * Reset CMMA state, make all pages stable again.
2686 */
2687static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2688			     unsigned long next, struct mm_walk *walk)
2689{
2690	ptep_zap_unused(walk->mm, addr, pte, 1);
2691	return 0;
2692}
2693
2694static const struct mm_walk_ops reset_cmma_walk_ops = {
2695	.pte_entry		= __s390_reset_cmma,
2696};
2697
2698void s390_reset_cmma(struct mm_struct *mm)
2699{
2700	mmap_write_lock(mm);
2701	walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2702	mmap_write_unlock(mm);
2703}
2704EXPORT_SYMBOL_GPL(s390_reset_cmma);
2705
2706#define GATHER_GET_PAGES 32
2707
2708struct reset_walk_state {
2709	unsigned long next;
2710	unsigned long count;
2711	unsigned long pfns[GATHER_GET_PAGES];
2712};
2713
2714static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2715			     unsigned long next, struct mm_walk *walk)
2716{
2717	struct reset_walk_state *p = walk->private;
2718	pte_t pte = READ_ONCE(*ptep);
2719
2720	if (pte_present(pte)) {
2721		/* we have a reference from the mapping, take an extra one */
2722		get_page(phys_to_page(pte_val(pte)));
2723		p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2724		p->next = next;
2725		p->count++;
2726	}
2727	return p->count >= GATHER_GET_PAGES;
2728}
2729
2730static const struct mm_walk_ops gather_pages_ops = {
2731	.pte_entry = s390_gather_pages,
2732};
2733
2734/*
2735 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2736 * Each page needs to have an extra reference, which will be released here.
2737 */
2738void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2739{
2740	unsigned long i;
2741
2742	for (i = 0; i < count; i++) {
2743		/* we always have an extra reference */
2744		uv_destroy_owned_page(pfn_to_phys(pfns[i]));
2745		/* get rid of the extra reference */
2746		put_page(pfn_to_page(pfns[i]));
2747		cond_resched();
2748	}
2749}
2750EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2751
2752/**
2753 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2754 * in the given range of the given address space.
2755 * @mm: the mm to operate on
2756 * @start: the start of the range
2757 * @end: the end of the range
2758 * @interruptible: if not 0, stop when a fatal signal is received
2759 *
2760 * Walk the given range of the given address space and call the destroy
2761 * secure page UVC on each page. Optionally exit early if a fatal signal is
2762 * pending.
2763 *
2764 * Return: 0 on success, -EINTR if the function stopped before completing
2765 */
2766int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2767			    unsigned long end, bool interruptible)
2768{
2769	struct reset_walk_state state = { .next = start };
2770	int r = 1;
2771
2772	while (r > 0) {
2773		state.count = 0;
2774		mmap_read_lock(mm);
2775		r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2776		mmap_read_unlock(mm);
2777		cond_resched();
2778		s390_uv_destroy_pfns(state.count, state.pfns);
2779		if (interruptible && fatal_signal_pending(current))
2780			return -EINTR;
2781	}
2782	return 0;
2783}
2784EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
2785
2786/**
2787 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2788 * list of page tables of the gmap.
2789 * @gmap: the gmap whose table is to be removed
2790 *
2791 * On s390x, KVM keeps a list of all pages containing the page tables of the
2792 * gmap (the CRST list). This list is used at tear down time to free all
2793 * pages that are now not needed anymore.
2794 *
2795 * This function removes the topmost page of the tree (the one pointed to by
2796 * the ASCE) from the CRST list.
2797 *
2798 * This means that it will not be freed when the VM is torn down, and needs
2799 * to be handled separately by the caller, unless a leak is actually
2800 * intended. Notice that this function will only remove the page from the
2801 * list, the page will still be used as a top level page table (and ASCE).
2802 */
2803void s390_unlist_old_asce(struct gmap *gmap)
2804{
2805	struct page *old;
2806
2807	old = virt_to_page(gmap->table);
2808	spin_lock(&gmap->guest_table_lock);
2809	list_del(&old->lru);
2810	/*
2811	 * Sometimes the topmost page might need to be "removed" multiple
2812	 * times, for example if the VM is rebooted into secure mode several
2813	 * times concurrently, or if s390_replace_asce fails after calling
2814	 * s390_remove_old_asce and is attempted again later. In that case
2815	 * the old asce has been removed from the list, and therefore it
2816	 * will not be freed when the VM terminates, but the ASCE is still
2817	 * in use and still pointed to.
2818	 * A subsequent call to replace_asce will follow the pointer and try
2819	 * to remove the same page from the list again.
2820	 * Therefore it's necessary that the page of the ASCE has valid
2821	 * pointers, so list_del can work (and do nothing) without
2822	 * dereferencing stale or invalid pointers.
2823	 */
2824	INIT_LIST_HEAD(&old->lru);
2825	spin_unlock(&gmap->guest_table_lock);
2826}
2827EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2828
2829/**
2830 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2831 * @gmap: the gmap whose ASCE needs to be replaced
2832 *
2833 * If the allocation of the new top level page table fails, the ASCE is not
2834 * replaced.
2835 * In any case, the old ASCE is always removed from the gmap CRST list.
2836 * Therefore the caller has to make sure to save a pointer to it
2837 * beforehand, unless a leak is actually intended.
2838 */
2839int s390_replace_asce(struct gmap *gmap)
2840{
2841	unsigned long asce;
2842	struct page *page;
2843	void *table;
2844
2845	s390_unlist_old_asce(gmap);
2846
2847	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
2848	if (!page)
2849		return -ENOMEM;
2850	table = page_to_virt(page);
2851	memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
2852
2853	/*
2854	 * The caller has to deal with the old ASCE, but here we make sure
2855	 * the new one is properly added to the CRST list, so that
2856	 * it will be freed when the VM is torn down.
2857	 */
2858	spin_lock(&gmap->guest_table_lock);
2859	list_add(&page->lru, &gmap->crst_list);
2860	spin_unlock(&gmap->guest_table_lock);
2861
2862	/* Set new table origin while preserving existing ASCE control bits */
2863	asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
2864	WRITE_ONCE(gmap->asce, asce);
2865	WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
2866	WRITE_ONCE(gmap->table, table);
2867
2868	return 0;
2869}
2870EXPORT_SYMBOL_GPL(s390_replace_asce);
v4.10.11
 
   1/*
   2 *  KVM guest address space mapping code
   3 *
   4 *    Copyright IBM Corp. 2007, 2016
   5 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
 
 
   6 */
   7
   8#include <linux/kernel.h>
   9#include <linux/mm.h>
  10#include <linux/swap.h>
  11#include <linux/smp.h>
  12#include <linux/spinlock.h>
  13#include <linux/slab.h>
  14#include <linux/swapops.h>
  15#include <linux/ksm.h>
  16#include <linux/mman.h>
 
  17
  18#include <asm/pgtable.h>
  19#include <asm/pgalloc.h>
  20#include <asm/gmap.h>
  21#include <asm/tlb.h>
  22
  23#define GMAP_SHADOW_FAKE_TABLE 1ULL
  24
  25/**
  26 * gmap_alloc - allocate and initialize a guest address space
  27 * @mm: pointer to the parent mm_struct
  28 * @limit: maximum address of the gmap address space
  29 *
  30 * Returns a guest address space structure.
  31 */
  32static struct gmap *gmap_alloc(unsigned long limit)
  33{
  34	struct gmap *gmap;
  35	struct page *page;
  36	unsigned long *table;
  37	unsigned long etype, atype;
  38
  39	if (limit < (1UL << 31)) {
  40		limit = (1UL << 31) - 1;
  41		atype = _ASCE_TYPE_SEGMENT;
  42		etype = _SEGMENT_ENTRY_EMPTY;
  43	} else if (limit < (1UL << 42)) {
  44		limit = (1UL << 42) - 1;
  45		atype = _ASCE_TYPE_REGION3;
  46		etype = _REGION3_ENTRY_EMPTY;
  47	} else if (limit < (1UL << 53)) {
  48		limit = (1UL << 53) - 1;
  49		atype = _ASCE_TYPE_REGION2;
  50		etype = _REGION2_ENTRY_EMPTY;
  51	} else {
  52		limit = -1UL;
  53		atype = _ASCE_TYPE_REGION1;
  54		etype = _REGION1_ENTRY_EMPTY;
  55	}
  56	gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
  57	if (!gmap)
  58		goto out;
  59	INIT_LIST_HEAD(&gmap->crst_list);
  60	INIT_LIST_HEAD(&gmap->children);
  61	INIT_LIST_HEAD(&gmap->pt_list);
  62	INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL);
  63	INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC);
  64	INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC);
  65	spin_lock_init(&gmap->guest_table_lock);
  66	spin_lock_init(&gmap->shadow_lock);
  67	atomic_set(&gmap->ref_count, 1);
  68	page = alloc_pages(GFP_KERNEL, 2);
  69	if (!page)
  70		goto out_free;
  71	page->index = 0;
  72	list_add(&page->lru, &gmap->crst_list);
  73	table = (unsigned long *) page_to_phys(page);
  74	crst_table_init(table, etype);
  75	gmap->table = table;
  76	gmap->asce = atype | _ASCE_TABLE_LENGTH |
  77		_ASCE_USER_BITS | __pa(table);
  78	gmap->asce_end = limit;
  79	return gmap;
  80
  81out_free:
  82	kfree(gmap);
  83out:
  84	return NULL;
  85}
  86
  87/**
  88 * gmap_create - create a guest address space
  89 * @mm: pointer to the parent mm_struct
  90 * @limit: maximum size of the gmap address space
  91 *
  92 * Returns a guest address space structure.
  93 */
  94struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
  95{
  96	struct gmap *gmap;
  97	unsigned long gmap_asce;
  98
  99	gmap = gmap_alloc(limit);
 100	if (!gmap)
 101		return NULL;
 102	gmap->mm = mm;
 103	spin_lock(&mm->context.gmap_lock);
 104	list_add_rcu(&gmap->list, &mm->context.gmap_list);
 105	if (list_is_singular(&mm->context.gmap_list))
 106		gmap_asce = gmap->asce;
 107	else
 108		gmap_asce = -1UL;
 109	WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
 110	spin_unlock(&mm->context.gmap_lock);
 111	return gmap;
 112}
 113EXPORT_SYMBOL_GPL(gmap_create);
 114
 115static void gmap_flush_tlb(struct gmap *gmap)
 116{
 117	if (MACHINE_HAS_IDTE)
 118		__tlb_flush_idte(gmap->asce);
 119	else
 120		__tlb_flush_global();
 121}
 122
 123static void gmap_radix_tree_free(struct radix_tree_root *root)
 124{
 125	struct radix_tree_iter iter;
 126	unsigned long indices[16];
 127	unsigned long index;
 128	void **slot;
 129	int i, nr;
 130
 131	/* A radix tree is freed by deleting all of its entries */
 132	index = 0;
 133	do {
 134		nr = 0;
 135		radix_tree_for_each_slot(slot, root, &iter, index) {
 136			indices[nr] = iter.index;
 137			if (++nr == 16)
 138				break;
 139		}
 140		for (i = 0; i < nr; i++) {
 141			index = indices[i];
 142			radix_tree_delete(root, index);
 143		}
 144	} while (nr > 0);
 145}
 146
 147static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
 148{
 149	struct gmap_rmap *rmap, *rnext, *head;
 150	struct radix_tree_iter iter;
 151	unsigned long indices[16];
 152	unsigned long index;
 153	void **slot;
 154	int i, nr;
 155
 156	/* A radix tree is freed by deleting all of its entries */
 157	index = 0;
 158	do {
 159		nr = 0;
 160		radix_tree_for_each_slot(slot, root, &iter, index) {
 161			indices[nr] = iter.index;
 162			if (++nr == 16)
 163				break;
 164		}
 165		for (i = 0; i < nr; i++) {
 166			index = indices[i];
 167			head = radix_tree_delete(root, index);
 168			gmap_for_each_rmap_safe(rmap, rnext, head)
 169				kfree(rmap);
 170		}
 171	} while (nr > 0);
 172}
 173
 174/**
 175 * gmap_free - free a guest address space
 176 * @gmap: pointer to the guest address space structure
 177 *
 178 * No locks required. There are no references to this gmap anymore.
 179 */
 180static void gmap_free(struct gmap *gmap)
 181{
 182	struct page *page, *next;
 183
 184	/* Flush tlb of all gmaps (if not already done for shadows) */
 185	if (!(gmap_is_shadow(gmap) && gmap->removed))
 186		gmap_flush_tlb(gmap);
 187	/* Free all segment & region tables. */
 188	list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
 189		__free_pages(page, 2);
 190	gmap_radix_tree_free(&gmap->guest_to_host);
 191	gmap_radix_tree_free(&gmap->host_to_guest);
 192
 193	/* Free additional data for a shadow gmap */
 194	if (gmap_is_shadow(gmap)) {
 195		/* Free all page tables. */
 196		list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
 197			page_table_free_pgste(page);
 198		gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
 199		/* Release reference to the parent */
 200		gmap_put(gmap->parent);
 201	}
 202
 203	kfree(gmap);
 204}
 205
 206/**
 207 * gmap_get - increase reference counter for guest address space
 208 * @gmap: pointer to the guest address space structure
 209 *
 210 * Returns the gmap pointer
 211 */
 212struct gmap *gmap_get(struct gmap *gmap)
 213{
 214	atomic_inc(&gmap->ref_count);
 215	return gmap;
 216}
 217EXPORT_SYMBOL_GPL(gmap_get);
 218
 219/**
 220 * gmap_put - decrease reference counter for guest address space
 221 * @gmap: pointer to the guest address space structure
 222 *
 223 * If the reference counter reaches zero the guest address space is freed.
 224 */
 225void gmap_put(struct gmap *gmap)
 226{
 227	if (atomic_dec_return(&gmap->ref_count) == 0)
 228		gmap_free(gmap);
 229}
 230EXPORT_SYMBOL_GPL(gmap_put);
 231
 232/**
 233 * gmap_remove - remove a guest address space but do not free it yet
 234 * @gmap: pointer to the guest address space structure
 235 */
 236void gmap_remove(struct gmap *gmap)
 237{
 238	struct gmap *sg, *next;
 239	unsigned long gmap_asce;
 240
 241	/* Remove all shadow gmaps linked to this gmap */
 242	if (!list_empty(&gmap->children)) {
 243		spin_lock(&gmap->shadow_lock);
 244		list_for_each_entry_safe(sg, next, &gmap->children, list) {
 245			list_del(&sg->list);
 246			gmap_put(sg);
 247		}
 248		spin_unlock(&gmap->shadow_lock);
 249	}
 250	/* Remove gmap from the pre-mm list */
 251	spin_lock(&gmap->mm->context.gmap_lock);
 252	list_del_rcu(&gmap->list);
 253	if (list_empty(&gmap->mm->context.gmap_list))
 254		gmap_asce = 0;
 255	else if (list_is_singular(&gmap->mm->context.gmap_list))
 256		gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
 257					     struct gmap, list)->asce;
 258	else
 259		gmap_asce = -1UL;
 260	WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
 261	spin_unlock(&gmap->mm->context.gmap_lock);
 262	synchronize_rcu();
 263	/* Put reference */
 264	gmap_put(gmap);
 265}
 266EXPORT_SYMBOL_GPL(gmap_remove);
 267
 268/**
 269 * gmap_enable - switch primary space to the guest address space
 270 * @gmap: pointer to the guest address space structure
 271 */
 272void gmap_enable(struct gmap *gmap)
 273{
 274	S390_lowcore.gmap = (unsigned long) gmap;
 275}
 276EXPORT_SYMBOL_GPL(gmap_enable);
 277
 278/**
 279 * gmap_disable - switch back to the standard primary address space
 280 * @gmap: pointer to the guest address space structure
 281 */
 282void gmap_disable(struct gmap *gmap)
 283{
 284	S390_lowcore.gmap = 0UL;
 285}
 286EXPORT_SYMBOL_GPL(gmap_disable);
 287
 288/**
 289 * gmap_get_enabled - get a pointer to the currently enabled gmap
 290 *
 291 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
 292 */
 293struct gmap *gmap_get_enabled(void)
 294{
 295	return (struct gmap *) S390_lowcore.gmap;
 296}
 297EXPORT_SYMBOL_GPL(gmap_get_enabled);
 298
 299/*
 300 * gmap_alloc_table is assumed to be called with mmap_sem held
 301 */
 302static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
 303			    unsigned long init, unsigned long gaddr)
 304{
 305	struct page *page;
 306	unsigned long *new;
 307
 308	/* since we dont free the gmap table until gmap_free we can unlock */
 309	page = alloc_pages(GFP_KERNEL, 2);
 310	if (!page)
 311		return -ENOMEM;
 312	new = (unsigned long *) page_to_phys(page);
 313	crst_table_init(new, init);
 314	spin_lock(&gmap->guest_table_lock);
 315	if (*table & _REGION_ENTRY_INVALID) {
 316		list_add(&page->lru, &gmap->crst_list);
 317		*table = (unsigned long) new | _REGION_ENTRY_LENGTH |
 318			(*table & _REGION_ENTRY_TYPE_MASK);
 319		page->index = gaddr;
 320		page = NULL;
 321	}
 322	spin_unlock(&gmap->guest_table_lock);
 323	if (page)
 324		__free_pages(page, 2);
 325	return 0;
 326}
 327
 328/**
 329 * __gmap_segment_gaddr - find virtual address from segment pointer
 330 * @entry: pointer to a segment table entry in the guest address space
 331 *
 332 * Returns the virtual address in the guest address space for the segment
 333 */
 334static unsigned long __gmap_segment_gaddr(unsigned long *entry)
 335{
 336	struct page *page;
 337	unsigned long offset, mask;
 338
 339	offset = (unsigned long) entry / sizeof(unsigned long);
 340	offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
 341	mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
 342	page = virt_to_page((void *)((unsigned long) entry & mask));
 343	return page->index + offset;
 344}
 345
 346/**
 347 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
 348 * @gmap: pointer to the guest address space structure
 349 * @vmaddr: address in the host process address space
 350 *
 351 * Returns 1 if a TLB flush is required
 352 */
 353static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
 354{
 355	unsigned long *entry;
 356	int flush = 0;
 357
 358	BUG_ON(gmap_is_shadow(gmap));
 359	spin_lock(&gmap->guest_table_lock);
 360	entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
 361	if (entry) {
 362		flush = (*entry != _SEGMENT_ENTRY_INVALID);
 363		*entry = _SEGMENT_ENTRY_INVALID;
 364	}
 365	spin_unlock(&gmap->guest_table_lock);
 366	return flush;
 367}
 368
 369/**
 370 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
 371 * @gmap: pointer to the guest address space structure
 372 * @gaddr: address in the guest address space
 373 *
 374 * Returns 1 if a TLB flush is required
 375 */
 376static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
 377{
 378	unsigned long vmaddr;
 379
 380	vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
 381						   gaddr >> PMD_SHIFT);
 382	return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
 383}
 384
 385/**
 386 * gmap_unmap_segment - unmap segment from the guest address space
 387 * @gmap: pointer to the guest address space structure
 388 * @to: address in the guest address space
 389 * @len: length of the memory area to unmap
 390 *
 391 * Returns 0 if the unmap succeeded, -EINVAL if not.
 392 */
 393int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
 394{
 395	unsigned long off;
 396	int flush;
 397
 398	BUG_ON(gmap_is_shadow(gmap));
 399	if ((to | len) & (PMD_SIZE - 1))
 400		return -EINVAL;
 401	if (len == 0 || to + len < to)
 402		return -EINVAL;
 403
 404	flush = 0;
 405	down_write(&gmap->mm->mmap_sem);
 406	for (off = 0; off < len; off += PMD_SIZE)
 407		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
 408	up_write(&gmap->mm->mmap_sem);
 409	if (flush)
 410		gmap_flush_tlb(gmap);
 411	return 0;
 412}
 413EXPORT_SYMBOL_GPL(gmap_unmap_segment);
 414
 415/**
 416 * gmap_map_segment - map a segment to the guest address space
 417 * @gmap: pointer to the guest address space structure
 418 * @from: source address in the parent address space
 419 * @to: target address in the guest address space
 420 * @len: length of the memory area to map
 421 *
 422 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
 423 */
 424int gmap_map_segment(struct gmap *gmap, unsigned long from,
 425		     unsigned long to, unsigned long len)
 426{
 427	unsigned long off;
 428	int flush;
 429
 430	BUG_ON(gmap_is_shadow(gmap));
 431	if ((from | to | len) & (PMD_SIZE - 1))
 432		return -EINVAL;
 433	if (len == 0 || from + len < from || to + len < to ||
 434	    from + len - 1 > TASK_MAX_SIZE || to + len - 1 > gmap->asce_end)
 435		return -EINVAL;
 436
 437	flush = 0;
 438	down_write(&gmap->mm->mmap_sem);
 439	for (off = 0; off < len; off += PMD_SIZE) {
 440		/* Remove old translation */
 441		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
 442		/* Store new translation */
 443		if (radix_tree_insert(&gmap->guest_to_host,
 444				      (to + off) >> PMD_SHIFT,
 445				      (void *) from + off))
 446			break;
 447	}
 448	up_write(&gmap->mm->mmap_sem);
 449	if (flush)
 450		gmap_flush_tlb(gmap);
 451	if (off >= len)
 452		return 0;
 453	gmap_unmap_segment(gmap, to, len);
 454	return -ENOMEM;
 455}
 456EXPORT_SYMBOL_GPL(gmap_map_segment);
 457
 458/**
 459 * __gmap_translate - translate a guest address to a user space address
 460 * @gmap: pointer to guest mapping meta data structure
 461 * @gaddr: guest address
 462 *
 463 * Returns user space address which corresponds to the guest address or
 464 * -EFAULT if no such mapping exists.
 465 * This function does not establish potentially missing page table entries.
 466 * The mmap_sem of the mm that belongs to the address space must be held
 467 * when this function gets called.
 468 *
 469 * Note: Can also be called for shadow gmaps.
 470 */
 471unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
 472{
 473	unsigned long vmaddr;
 474
 475	vmaddr = (unsigned long)
 476		radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
 477	/* Note: guest_to_host is empty for a shadow gmap */
 478	return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
 479}
 480EXPORT_SYMBOL_GPL(__gmap_translate);
 481
 482/**
 483 * gmap_translate - translate a guest address to a user space address
 484 * @gmap: pointer to guest mapping meta data structure
 485 * @gaddr: guest address
 486 *
 487 * Returns user space address which corresponds to the guest address or
 488 * -EFAULT if no such mapping exists.
 489 * This function does not establish potentially missing page table entries.
 490 */
 491unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
 492{
 493	unsigned long rc;
 494
 495	down_read(&gmap->mm->mmap_sem);
 496	rc = __gmap_translate(gmap, gaddr);
 497	up_read(&gmap->mm->mmap_sem);
 498	return rc;
 499}
 500EXPORT_SYMBOL_GPL(gmap_translate);
 501
 502/**
 503 * gmap_unlink - disconnect a page table from the gmap shadow tables
 504 * @gmap: pointer to guest mapping meta data structure
 505 * @table: pointer to the host page table
 506 * @vmaddr: vm address associated with the host page table
 507 */
 508void gmap_unlink(struct mm_struct *mm, unsigned long *table,
 509		 unsigned long vmaddr)
 510{
 511	struct gmap *gmap;
 512	int flush;
 513
 514	rcu_read_lock();
 515	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
 516		flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
 517		if (flush)
 518			gmap_flush_tlb(gmap);
 519	}
 520	rcu_read_unlock();
 521}
 522
 
 
 
 523/**
 524 * gmap_link - set up shadow page tables to connect a host to a guest address
 525 * @gmap: pointer to guest mapping meta data structure
 526 * @gaddr: guest address
 527 * @vmaddr: vm address
 528 *
 529 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 530 * if the vm address is already mapped to a different guest segment.
 531 * The mmap_sem of the mm that belongs to the address space must be held
 532 * when this function gets called.
 533 */
 534int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
 535{
 536	struct mm_struct *mm;
 537	unsigned long *table;
 538	spinlock_t *ptl;
 539	pgd_t *pgd;
 
 540	pud_t *pud;
 541	pmd_t *pmd;
 
 542	int rc;
 543
 544	BUG_ON(gmap_is_shadow(gmap));
 545	/* Create higher level tables in the gmap page table */
 546	table = gmap->table;
 547	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
 548		table += (gaddr >> 53) & 0x7ff;
 549		if ((*table & _REGION_ENTRY_INVALID) &&
 550		    gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
 551				     gaddr & 0xffe0000000000000UL))
 552			return -ENOMEM;
 553		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 554	}
 555	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
 556		table += (gaddr >> 42) & 0x7ff;
 557		if ((*table & _REGION_ENTRY_INVALID) &&
 558		    gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
 559				     gaddr & 0xfffffc0000000000UL))
 560			return -ENOMEM;
 561		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 562	}
 563	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
 564		table += (gaddr >> 31) & 0x7ff;
 565		if ((*table & _REGION_ENTRY_INVALID) &&
 566		    gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
 567				     gaddr & 0xffffffff80000000UL))
 568			return -ENOMEM;
 569		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 570	}
 571	table += (gaddr >> 20) & 0x7ff;
 572	/* Walk the parent mm page table */
 573	mm = gmap->mm;
 574	pgd = pgd_offset(mm, vmaddr);
 575	VM_BUG_ON(pgd_none(*pgd));
 576	pud = pud_offset(pgd, vmaddr);
 
 
 577	VM_BUG_ON(pud_none(*pud));
 578	/* large puds cannot yet be handled */
 579	if (pud_large(*pud))
 580		return -EFAULT;
 581	pmd = pmd_offset(pud, vmaddr);
 582	VM_BUG_ON(pmd_none(*pmd));
 583	/* large pmds cannot yet be handled */
 584	if (pmd_large(*pmd))
 585		return -EFAULT;
 586	/* Link gmap segment table entry location to page table. */
 587	rc = radix_tree_preload(GFP_KERNEL);
 588	if (rc)
 589		return rc;
 590	ptl = pmd_lock(mm, pmd);
 591	spin_lock(&gmap->guest_table_lock);
 592	if (*table == _SEGMENT_ENTRY_INVALID) {
 593		rc = radix_tree_insert(&gmap->host_to_guest,
 594				       vmaddr >> PMD_SHIFT, table);
 595		if (!rc)
 596			*table = pmd_val(*pmd);
 597	} else
 598		rc = 0;
 
 
 
 
 
 
 
 
 
 
 
 
 599	spin_unlock(&gmap->guest_table_lock);
 600	spin_unlock(ptl);
 601	radix_tree_preload_end();
 602	return rc;
 603}
 604
 605/**
 606 * gmap_fault - resolve a fault on a guest address
 607 * @gmap: pointer to guest mapping meta data structure
 608 * @gaddr: guest address
 609 * @fault_flags: flags to pass down to handle_mm_fault()
 610 *
 611 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 612 * if the vm address is already mapped to a different guest segment.
 613 */
 614int gmap_fault(struct gmap *gmap, unsigned long gaddr,
 615	       unsigned int fault_flags)
 616{
 617	unsigned long vmaddr;
 618	int rc;
 619	bool unlocked;
 620
 621	down_read(&gmap->mm->mmap_sem);
 622
 623retry:
 624	unlocked = false;
 625	vmaddr = __gmap_translate(gmap, gaddr);
 626	if (IS_ERR_VALUE(vmaddr)) {
 627		rc = vmaddr;
 628		goto out_up;
 629	}
 630	if (fixup_user_fault(current, gmap->mm, vmaddr, fault_flags,
 631			     &unlocked)) {
 632		rc = -EFAULT;
 633		goto out_up;
 634	}
 635	/*
 636	 * In the case that fixup_user_fault unlocked the mmap_sem during
 637	 * faultin redo __gmap_translate to not race with a map/unmap_segment.
 638	 */
 639	if (unlocked)
 640		goto retry;
 641
 642	rc = __gmap_link(gmap, gaddr, vmaddr);
 643out_up:
 644	up_read(&gmap->mm->mmap_sem);
 645	return rc;
 646}
 647EXPORT_SYMBOL_GPL(gmap_fault);
 648
 649/*
 650 * this function is assumed to be called with mmap_sem held
 651 */
 652void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
 653{
 
 654	unsigned long vmaddr;
 655	spinlock_t *ptl;
 656	pte_t *ptep;
 657
 658	/* Find the vm address for the guest address */
 659	vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
 660						   gaddr >> PMD_SHIFT);
 661	if (vmaddr) {
 662		vmaddr |= gaddr & ~PMD_MASK;
 
 
 
 
 
 663		/* Get pointer to the page table entry */
 664		ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
 665		if (likely(ptep))
 666			ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
 667		pte_unmap_unlock(ptep, ptl);
 
 668	}
 669}
 670EXPORT_SYMBOL_GPL(__gmap_zap);
 671
 672void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
 673{
 674	unsigned long gaddr, vmaddr, size;
 675	struct vm_area_struct *vma;
 676
 677	down_read(&gmap->mm->mmap_sem);
 678	for (gaddr = from; gaddr < to;
 679	     gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
 680		/* Find the vm address for the guest address */
 681		vmaddr = (unsigned long)
 682			radix_tree_lookup(&gmap->guest_to_host,
 683					  gaddr >> PMD_SHIFT);
 684		if (!vmaddr)
 685			continue;
 686		vmaddr |= gaddr & ~PMD_MASK;
 687		/* Find vma in the parent mm */
 688		vma = find_vma(gmap->mm, vmaddr);
 
 
 
 
 
 
 
 
 689		size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
 690		zap_page_range(vma, vmaddr, size, NULL);
 691	}
 692	up_read(&gmap->mm->mmap_sem);
 693}
 694EXPORT_SYMBOL_GPL(gmap_discard);
 695
 696static LIST_HEAD(gmap_notifier_list);
 697static DEFINE_SPINLOCK(gmap_notifier_lock);
 698
 699/**
 700 * gmap_register_pte_notifier - register a pte invalidation callback
 701 * @nb: pointer to the gmap notifier block
 702 */
 703void gmap_register_pte_notifier(struct gmap_notifier *nb)
 704{
 705	spin_lock(&gmap_notifier_lock);
 706	list_add_rcu(&nb->list, &gmap_notifier_list);
 707	spin_unlock(&gmap_notifier_lock);
 708}
 709EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
 710
 711/**
 712 * gmap_unregister_pte_notifier - remove a pte invalidation callback
 713 * @nb: pointer to the gmap notifier block
 714 */
 715void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
 716{
 717	spin_lock(&gmap_notifier_lock);
 718	list_del_rcu(&nb->list);
 719	spin_unlock(&gmap_notifier_lock);
 720	synchronize_rcu();
 721}
 722EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
 723
 724/**
 725 * gmap_call_notifier - call all registered invalidation callbacks
 726 * @gmap: pointer to guest mapping meta data structure
 727 * @start: start virtual address in the guest address space
 728 * @end: end virtual address in the guest address space
 729 */
 730static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
 731			       unsigned long end)
 732{
 733	struct gmap_notifier *nb;
 734
 735	list_for_each_entry(nb, &gmap_notifier_list, list)
 736		nb->notifier_call(gmap, start, end);
 737}
 738
 739/**
 740 * gmap_table_walk - walk the gmap page tables
 741 * @gmap: pointer to guest mapping meta data structure
 742 * @gaddr: virtual address in the guest address space
 743 * @level: page table level to stop at
 744 *
 745 * Returns a table entry pointer for the given guest address and @level
 746 * @level=0 : returns a pointer to a page table table entry (or NULL)
 747 * @level=1 : returns a pointer to a segment table entry (or NULL)
 748 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
 749 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
 750 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
 751 *
 752 * Returns NULL if the gmap page tables could not be walked to the
 753 * requested level.
 754 *
 755 * Note: Can also be called for shadow gmaps.
 756 */
 757static inline unsigned long *gmap_table_walk(struct gmap *gmap,
 758					     unsigned long gaddr, int level)
 759{
 760	unsigned long *table;
 
 761
 762	if ((gmap->asce & _ASCE_TYPE_MASK) + 4 < (level * 4))
 763		return NULL;
 764	if (gmap_is_shadow(gmap) && gmap->removed)
 
 765		return NULL;
 766	if (gaddr & (-1UL << (31 + ((gmap->asce & _ASCE_TYPE_MASK) >> 2)*11)))
 
 
 767		return NULL;
 768	table = gmap->table;
 769	switch (gmap->asce & _ASCE_TYPE_MASK) {
 770	case _ASCE_TYPE_REGION1:
 771		table += (gaddr >> 53) & 0x7ff;
 772		if (level == 4)
 773			break;
 774		if (*table & _REGION_ENTRY_INVALID)
 775			return NULL;
 776		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 777		/* Fallthrough */
 778	case _ASCE_TYPE_REGION2:
 779		table += (gaddr >> 42) & 0x7ff;
 780		if (level == 3)
 781			break;
 782		if (*table & _REGION_ENTRY_INVALID)
 783			return NULL;
 784		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 785		/* Fallthrough */
 786	case _ASCE_TYPE_REGION3:
 787		table += (gaddr >> 31) & 0x7ff;
 788		if (level == 2)
 789			break;
 790		if (*table & _REGION_ENTRY_INVALID)
 791			return NULL;
 792		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
 793		/* Fallthrough */
 794	case _ASCE_TYPE_SEGMENT:
 795		table += (gaddr >> 20) & 0x7ff;
 796		if (level == 1)
 797			break;
 798		if (*table & _REGION_ENTRY_INVALID)
 799			return NULL;
 800		table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
 801		table += (gaddr >> 12) & 0xff;
 802	}
 803	return table;
 804}
 805
 806/**
 807 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
 808 *		      and return the pte pointer
 809 * @gmap: pointer to guest mapping meta data structure
 810 * @gaddr: virtual address in the guest address space
 811 * @ptl: pointer to the spinlock pointer
 812 *
 813 * Returns a pointer to the locked pte for a guest address, or NULL
 814 *
 815 * Note: Can also be called for shadow gmaps.
 816 */
 817static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
 818			       spinlock_t **ptl)
 819{
 820	unsigned long *table;
 821
 822	if (gmap_is_shadow(gmap))
 823		spin_lock(&gmap->guest_table_lock);
 824	/* Walk the gmap page table, lock and get pte pointer */
 825	table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
 826	if (!table || *table & _SEGMENT_ENTRY_INVALID) {
 827		if (gmap_is_shadow(gmap))
 828			spin_unlock(&gmap->guest_table_lock);
 829		return NULL;
 830	}
 831	if (gmap_is_shadow(gmap)) {
 832		*ptl = &gmap->guest_table_lock;
 833		return pte_offset_map((pmd_t *) table, gaddr);
 834	}
 835	return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
 836}
 837
 838/**
 839 * gmap_pte_op_fixup - force a page in and connect the gmap page table
 840 * @gmap: pointer to guest mapping meta data structure
 841 * @gaddr: virtual address in the guest address space
 842 * @vmaddr: address in the host process address space
 843 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 844 *
 845 * Returns 0 if the caller can retry __gmap_translate (might fail again),
 846 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
 847 * up or connecting the gmap page table.
 848 */
 849static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
 850			     unsigned long vmaddr, int prot)
 851{
 852	struct mm_struct *mm = gmap->mm;
 853	unsigned int fault_flags;
 854	bool unlocked = false;
 855
 856	BUG_ON(gmap_is_shadow(gmap));
 857	fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
 858	if (fixup_user_fault(current, mm, vmaddr, fault_flags, &unlocked))
 859		return -EFAULT;
 860	if (unlocked)
 861		/* lost mmap_sem, caller has to retry __gmap_translate */
 862		return 0;
 863	/* Connect the page tables */
 864	return __gmap_link(gmap, gaddr, vmaddr);
 865}
 866
 867/**
 868 * gmap_pte_op_end - release the page table lock
 869 * @ptl: pointer to the spinlock pointer
 870 */
 871static void gmap_pte_op_end(spinlock_t *ptl)
 872{
 873	spin_unlock(ptl);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 874}
 875
 876/*
 877 * gmap_protect_range - remove access rights to memory and set pgste bits
 878 * @gmap: pointer to guest mapping meta data structure
 879 * @gaddr: virtual address in the guest address space
 880 * @len: size of area
 881 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 882 * @bits: pgste notification bits to set
 883 *
 884 * Returns 0 if successfully protected, -ENOMEM if out of memory and
 885 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
 886 *
 887 * Called with sg->mm->mmap_sem in read.
 888 *
 889 * Note: Can also be called for shadow gmaps.
 890 */
 891static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
 892			      unsigned long len, int prot, unsigned long bits)
 893{
 894	unsigned long vmaddr;
 895	spinlock_t *ptl;
 896	pte_t *ptep;
 897	int rc;
 898
 
 899	while (len) {
 900		rc = -EAGAIN;
 901		ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
 902		if (ptep) {
 903			rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, bits);
 904			gmap_pte_op_end(ptl);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 905		}
 906		if (rc) {
 
 
 
 
 907			vmaddr = __gmap_translate(gmap, gaddr);
 908			if (IS_ERR_VALUE(vmaddr))
 909				return vmaddr;
 910			rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
 911			if (rc)
 912				return rc;
 913			continue;
 914		}
 915		gaddr += PAGE_SIZE;
 916		len -= PAGE_SIZE;
 917	}
 918	return 0;
 919}
 920
 921/**
 922 * gmap_mprotect_notify - change access rights for a range of ptes and
 923 *                        call the notifier if any pte changes again
 924 * @gmap: pointer to guest mapping meta data structure
 925 * @gaddr: virtual address in the guest address space
 926 * @len: size of area
 927 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 928 *
 929 * Returns 0 if for each page in the given range a gmap mapping exists,
 930 * the new access rights could be set and the notifier could be armed.
 931 * If the gmap mapping is missing for one or more pages -EFAULT is
 932 * returned. If no memory could be allocated -ENOMEM is returned.
 933 * This function establishes missing page table entries.
 934 */
 935int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
 936			 unsigned long len, int prot)
 937{
 938	int rc;
 939
 940	if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
 941		return -EINVAL;
 942	if (!MACHINE_HAS_ESOP && prot == PROT_READ)
 943		return -EINVAL;
 944	down_read(&gmap->mm->mmap_sem);
 945	rc = gmap_protect_range(gmap, gaddr, len, prot, PGSTE_IN_BIT);
 946	up_read(&gmap->mm->mmap_sem);
 947	return rc;
 948}
 949EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
 950
 951/**
 952 * gmap_read_table - get an unsigned long value from a guest page table using
 953 *                   absolute addressing, without marking the page referenced.
 954 * @gmap: pointer to guest mapping meta data structure
 955 * @gaddr: virtual address in the guest address space
 956 * @val: pointer to the unsigned long value to return
 957 *
 958 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
 959 * if reading using the virtual address failed.
 
 960 *
 961 * Called with gmap->mm->mmap_sem in read.
 962 */
 963int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
 964{
 965	unsigned long address, vmaddr;
 966	spinlock_t *ptl;
 967	pte_t *ptep, pte;
 968	int rc;
 969
 
 
 
 970	while (1) {
 971		rc = -EAGAIN;
 972		ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
 973		if (ptep) {
 974			pte = *ptep;
 975			if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
 976				address = pte_val(pte) & PAGE_MASK;
 977				address += gaddr & ~PAGE_MASK;
 978				*val = *(unsigned long *) address;
 979				pte_val(*ptep) |= _PAGE_YOUNG;
 980				/* Do *NOT* clear the _PAGE_INVALID bit! */
 981				rc = 0;
 982			}
 983			gmap_pte_op_end(ptl);
 984		}
 985		if (!rc)
 986			break;
 987		vmaddr = __gmap_translate(gmap, gaddr);
 988		if (IS_ERR_VALUE(vmaddr)) {
 989			rc = vmaddr;
 990			break;
 991		}
 992		rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
 993		if (rc)
 994			break;
 995	}
 996	return rc;
 997}
 998EXPORT_SYMBOL_GPL(gmap_read_table);
 999
1000/**
1001 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1002 * @sg: pointer to the shadow guest address space structure
1003 * @vmaddr: vm address associated with the rmap
1004 * @rmap: pointer to the rmap structure
1005 *
1006 * Called with the sg->guest_table_lock
1007 */
1008static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1009				    struct gmap_rmap *rmap)
1010{
1011	void **slot;
 
1012
1013	BUG_ON(!gmap_is_shadow(sg));
1014	slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1015	if (slot) {
1016		rmap->next = radix_tree_deref_slot_protected(slot,
1017							&sg->guest_table_lock);
 
 
 
 
 
 
1018		radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1019	} else {
1020		rmap->next = NULL;
1021		radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1022				  rmap);
1023	}
1024}
1025
1026/**
1027 * gmap_protect_rmap - modify access rights to memory and create an rmap
1028 * @sg: pointer to the shadow guest address space structure
1029 * @raddr: rmap address in the shadow gmap
1030 * @paddr: address in the parent guest address space
1031 * @len: length of the memory area to protect
1032 * @prot: indicates access rights: none, read-only or read-write
1033 *
1034 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1035 * if out of memory and -EFAULT if paddr is invalid.
1036 */
1037static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1038			     unsigned long paddr, unsigned long len, int prot)
1039{
1040	struct gmap *parent;
1041	struct gmap_rmap *rmap;
1042	unsigned long vmaddr;
1043	spinlock_t *ptl;
1044	pte_t *ptep;
1045	int rc;
1046
1047	BUG_ON(!gmap_is_shadow(sg));
1048	parent = sg->parent;
1049	while (len) {
1050		vmaddr = __gmap_translate(parent, paddr);
1051		if (IS_ERR_VALUE(vmaddr))
1052			return vmaddr;
1053		rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1054		if (!rmap)
1055			return -ENOMEM;
1056		rmap->raddr = raddr;
1057		rc = radix_tree_preload(GFP_KERNEL);
1058		if (rc) {
1059			kfree(rmap);
1060			return rc;
1061		}
1062		rc = -EAGAIN;
1063		ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1064		if (ptep) {
1065			spin_lock(&sg->guest_table_lock);
1066			rc = ptep_force_prot(parent->mm, paddr, ptep, prot,
1067					     PGSTE_VSIE_BIT);
1068			if (!rc)
1069				gmap_insert_rmap(sg, vmaddr, rmap);
1070			spin_unlock(&sg->guest_table_lock);
1071			gmap_pte_op_end(ptl);
1072		}
1073		radix_tree_preload_end();
1074		if (rc) {
1075			kfree(rmap);
1076			rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
1077			if (rc)
1078				return rc;
1079			continue;
1080		}
1081		paddr += PAGE_SIZE;
1082		len -= PAGE_SIZE;
1083	}
1084	return 0;
1085}
1086
1087#define _SHADOW_RMAP_MASK	0x7
1088#define _SHADOW_RMAP_REGION1	0x5
1089#define _SHADOW_RMAP_REGION2	0x4
1090#define _SHADOW_RMAP_REGION3	0x3
1091#define _SHADOW_RMAP_SEGMENT	0x2
1092#define _SHADOW_RMAP_PGTABLE	0x1
1093
1094/**
1095 * gmap_idte_one - invalidate a single region or segment table entry
1096 * @asce: region or segment table *origin* + table-type bits
1097 * @vaddr: virtual address to identify the table entry to flush
1098 *
1099 * The invalid bit of a single region or segment table entry is set
1100 * and the associated TLB entries depending on the entry are flushed.
1101 * The table-type of the @asce identifies the portion of the @vaddr
1102 * that is used as the invalidation index.
1103 */
1104static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1105{
1106	asm volatile(
1107		"	.insn	rrf,0xb98e0000,%0,%1,0,0"
1108		: : "a" (asce), "a" (vaddr) : "cc", "memory");
1109}
1110
1111/**
1112 * gmap_unshadow_page - remove a page from a shadow page table
1113 * @sg: pointer to the shadow guest address space structure
1114 * @raddr: rmap address in the shadow guest address space
1115 *
1116 * Called with the sg->guest_table_lock
1117 */
1118static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1119{
1120	unsigned long *table;
1121
1122	BUG_ON(!gmap_is_shadow(sg));
1123	table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1124	if (!table || *table & _PAGE_INVALID)
1125		return;
1126	gmap_call_notifier(sg, raddr, raddr + (1UL << 12) - 1);
1127	ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1128}
1129
1130/**
1131 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1132 * @sg: pointer to the shadow guest address space structure
1133 * @raddr: rmap address in the shadow guest address space
1134 * @pgt: pointer to the start of a shadow page table
1135 *
1136 * Called with the sg->guest_table_lock
1137 */
1138static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1139				unsigned long *pgt)
1140{
1141	int i;
1142
1143	BUG_ON(!gmap_is_shadow(sg));
1144	for (i = 0; i < 256; i++, raddr += 1UL << 12)
1145		pgt[i] = _PAGE_INVALID;
1146}
1147
1148/**
1149 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1150 * @sg: pointer to the shadow guest address space structure
1151 * @raddr: address in the shadow guest address space
1152 *
1153 * Called with the sg->guest_table_lock
1154 */
1155static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1156{
1157	unsigned long sto, *ste, *pgt;
 
1158	struct page *page;
1159
1160	BUG_ON(!gmap_is_shadow(sg));
1161	ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1162	if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1163		return;
1164	gmap_call_notifier(sg, raddr, raddr + (1UL << 20) - 1);
1165	sto = (unsigned long) (ste - ((raddr >> 20) & 0x7ff));
1166	gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1167	pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
1168	*ste = _SEGMENT_ENTRY_EMPTY;
1169	__gmap_unshadow_pgt(sg, raddr, pgt);
1170	/* Free page table */
1171	page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1172	list_del(&page->lru);
1173	page_table_free_pgste(page);
1174}
1175
1176/**
1177 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1178 * @sg: pointer to the shadow guest address space structure
1179 * @raddr: rmap address in the shadow guest address space
1180 * @sgt: pointer to the start of a shadow segment table
1181 *
1182 * Called with the sg->guest_table_lock
1183 */
1184static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1185				unsigned long *sgt)
1186{
1187	unsigned long asce, *pgt;
1188	struct page *page;
 
1189	int i;
1190
1191	BUG_ON(!gmap_is_shadow(sg));
1192	asce = (unsigned long) sgt | _ASCE_TYPE_SEGMENT;
1193	for (i = 0; i < 2048; i++, raddr += 1UL << 20) {
1194		if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1195			continue;
1196		pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
1197		sgt[i] = _SEGMENT_ENTRY_EMPTY;
1198		__gmap_unshadow_pgt(sg, raddr, pgt);
1199		/* Free page table */
1200		page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
1201		list_del(&page->lru);
1202		page_table_free_pgste(page);
1203	}
1204}
1205
1206/**
1207 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1208 * @sg: pointer to the shadow guest address space structure
1209 * @raddr: rmap address in the shadow guest address space
1210 *
1211 * Called with the shadow->guest_table_lock
1212 */
1213static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1214{
1215	unsigned long r3o, *r3e, *sgt;
 
1216	struct page *page;
1217
1218	BUG_ON(!gmap_is_shadow(sg));
1219	r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1220	if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1221		return;
1222	gmap_call_notifier(sg, raddr, raddr + (1UL << 31) - 1);
1223	r3o = (unsigned long) (r3e - ((raddr >> 31) & 0x7ff));
1224	gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
1225	sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
1226	*r3e = _REGION3_ENTRY_EMPTY;
1227	__gmap_unshadow_sgt(sg, raddr, sgt);
1228	/* Free segment table */
1229	page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1230	list_del(&page->lru);
1231	__free_pages(page, 2);
1232}
1233
1234/**
1235 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1236 * @sg: pointer to the shadow guest address space structure
1237 * @raddr: address in the shadow guest address space
1238 * @r3t: pointer to the start of a shadow region-3 table
1239 *
1240 * Called with the sg->guest_table_lock
1241 */
1242static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1243				unsigned long *r3t)
1244{
1245	unsigned long asce, *sgt;
1246	struct page *page;
 
1247	int i;
1248
1249	BUG_ON(!gmap_is_shadow(sg));
1250	asce = (unsigned long) r3t | _ASCE_TYPE_REGION3;
1251	for (i = 0; i < 2048; i++, raddr += 1UL << 31) {
1252		if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1253			continue;
1254		sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
1255		r3t[i] = _REGION3_ENTRY_EMPTY;
1256		__gmap_unshadow_sgt(sg, raddr, sgt);
1257		/* Free segment table */
1258		page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
1259		list_del(&page->lru);
1260		__free_pages(page, 2);
1261	}
1262}
1263
1264/**
1265 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1266 * @sg: pointer to the shadow guest address space structure
1267 * @raddr: rmap address in the shadow guest address space
1268 *
1269 * Called with the sg->guest_table_lock
1270 */
1271static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1272{
1273	unsigned long r2o, *r2e, *r3t;
 
1274	struct page *page;
1275
1276	BUG_ON(!gmap_is_shadow(sg));
1277	r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1278	if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1279		return;
1280	gmap_call_notifier(sg, raddr, raddr + (1UL << 42) - 1);
1281	r2o = (unsigned long) (r2e - ((raddr >> 42) & 0x7ff));
1282	gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
1283	r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
1284	*r2e = _REGION2_ENTRY_EMPTY;
1285	__gmap_unshadow_r3t(sg, raddr, r3t);
1286	/* Free region 3 table */
1287	page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1288	list_del(&page->lru);
1289	__free_pages(page, 2);
1290}
1291
1292/**
1293 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1294 * @sg: pointer to the shadow guest address space structure
1295 * @raddr: rmap address in the shadow guest address space
1296 * @r2t: pointer to the start of a shadow region-2 table
1297 *
1298 * Called with the sg->guest_table_lock
1299 */
1300static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1301				unsigned long *r2t)
1302{
1303	unsigned long asce, *r3t;
1304	struct page *page;
1305	int i;
1306
1307	BUG_ON(!gmap_is_shadow(sg));
1308	asce = (unsigned long) r2t | _ASCE_TYPE_REGION2;
1309	for (i = 0; i < 2048; i++, raddr += 1UL << 42) {
1310		if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1311			continue;
1312		r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
1313		r2t[i] = _REGION2_ENTRY_EMPTY;
1314		__gmap_unshadow_r3t(sg, raddr, r3t);
1315		/* Free region 3 table */
1316		page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
1317		list_del(&page->lru);
1318		__free_pages(page, 2);
1319	}
1320}
1321
1322/**
1323 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1324 * @sg: pointer to the shadow guest address space structure
1325 * @raddr: rmap address in the shadow guest address space
1326 *
1327 * Called with the sg->guest_table_lock
1328 */
1329static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1330{
1331	unsigned long r1o, *r1e, *r2t;
1332	struct page *page;
 
1333
1334	BUG_ON(!gmap_is_shadow(sg));
1335	r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1336	if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1337		return;
1338	gmap_call_notifier(sg, raddr, raddr + (1UL << 53) - 1);
1339	r1o = (unsigned long) (r1e - ((raddr >> 53) & 0x7ff));
1340	gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
1341	r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
1342	*r1e = _REGION1_ENTRY_EMPTY;
1343	__gmap_unshadow_r2t(sg, raddr, r2t);
1344	/* Free region 2 table */
1345	page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1346	list_del(&page->lru);
1347	__free_pages(page, 2);
1348}
1349
1350/**
1351 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1352 * @sg: pointer to the shadow guest address space structure
1353 * @raddr: rmap address in the shadow guest address space
1354 * @r1t: pointer to the start of a shadow region-1 table
1355 *
1356 * Called with the shadow->guest_table_lock
1357 */
1358static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1359				unsigned long *r1t)
1360{
1361	unsigned long asce, *r2t;
1362	struct page *page;
 
1363	int i;
1364
1365	BUG_ON(!gmap_is_shadow(sg));
1366	asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
1367	for (i = 0; i < 2048; i++, raddr += 1UL << 53) {
1368		if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1369			continue;
1370		r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
1371		__gmap_unshadow_r2t(sg, raddr, r2t);
1372		/* Clear entry and flush translation r1t -> r2t */
1373		gmap_idte_one(asce, raddr);
1374		r1t[i] = _REGION1_ENTRY_EMPTY;
1375		/* Free region 2 table */
1376		page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
1377		list_del(&page->lru);
1378		__free_pages(page, 2);
1379	}
1380}
1381
1382/**
1383 * gmap_unshadow - remove a shadow page table completely
1384 * @sg: pointer to the shadow guest address space structure
1385 *
1386 * Called with sg->guest_table_lock
1387 */
1388static void gmap_unshadow(struct gmap *sg)
1389{
1390	unsigned long *table;
1391
1392	BUG_ON(!gmap_is_shadow(sg));
1393	if (sg->removed)
1394		return;
1395	sg->removed = 1;
1396	gmap_call_notifier(sg, 0, -1UL);
1397	gmap_flush_tlb(sg);
1398	table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
1399	switch (sg->asce & _ASCE_TYPE_MASK) {
1400	case _ASCE_TYPE_REGION1:
1401		__gmap_unshadow_r1t(sg, 0, table);
1402		break;
1403	case _ASCE_TYPE_REGION2:
1404		__gmap_unshadow_r2t(sg, 0, table);
1405		break;
1406	case _ASCE_TYPE_REGION3:
1407		__gmap_unshadow_r3t(sg, 0, table);
1408		break;
1409	case _ASCE_TYPE_SEGMENT:
1410		__gmap_unshadow_sgt(sg, 0, table);
1411		break;
1412	}
1413}
1414
1415/**
1416 * gmap_find_shadow - find a specific asce in the list of shadow tables
1417 * @parent: pointer to the parent gmap
1418 * @asce: ASCE for which the shadow table is created
1419 * @edat_level: edat level to be used for the shadow translation
1420 *
1421 * Returns the pointer to a gmap if a shadow table with the given asce is
1422 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1423 * otherwise NULL
1424 */
1425static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1426				     int edat_level)
1427{
1428	struct gmap *sg;
1429
1430	list_for_each_entry(sg, &parent->children, list) {
1431		if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1432		    sg->removed)
1433			continue;
1434		if (!sg->initialized)
1435			return ERR_PTR(-EAGAIN);
1436		atomic_inc(&sg->ref_count);
1437		return sg;
1438	}
1439	return NULL;
1440}
1441
1442/**
1443 * gmap_shadow_valid - check if a shadow guest address space matches the
1444 *                     given properties and is still valid
1445 * @sg: pointer to the shadow guest address space structure
1446 * @asce: ASCE for which the shadow table is requested
1447 * @edat_level: edat level to be used for the shadow translation
1448 *
1449 * Returns 1 if the gmap shadow is still valid and matches the given
1450 * properties, the caller can continue using it. Returns 0 otherwise, the
1451 * caller has to request a new shadow gmap in this case.
1452 *
1453 */
1454int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1455{
1456	if (sg->removed)
1457		return 0;
1458	return sg->orig_asce == asce && sg->edat_level == edat_level;
1459}
1460EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1461
1462/**
1463 * gmap_shadow - create/find a shadow guest address space
1464 * @parent: pointer to the parent gmap
1465 * @asce: ASCE for which the shadow table is created
1466 * @edat_level: edat level to be used for the shadow translation
1467 *
1468 * The pages of the top level page table referred by the asce parameter
1469 * will be set to read-only and marked in the PGSTEs of the kvm process.
1470 * The shadow table will be removed automatically on any change to the
1471 * PTE mapping for the source table.
1472 *
1473 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1474 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1475 * parent gmap table could not be protected.
1476 */
1477struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1478			 int edat_level)
1479{
1480	struct gmap *sg, *new;
1481	unsigned long limit;
1482	int rc;
1483
 
1484	BUG_ON(gmap_is_shadow(parent));
1485	spin_lock(&parent->shadow_lock);
1486	sg = gmap_find_shadow(parent, asce, edat_level);
1487	spin_unlock(&parent->shadow_lock);
1488	if (sg)
1489		return sg;
1490	/* Create a new shadow gmap */
1491	limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1492	if (asce & _ASCE_REAL_SPACE)
1493		limit = -1UL;
1494	new = gmap_alloc(limit);
1495	if (!new)
1496		return ERR_PTR(-ENOMEM);
1497	new->mm = parent->mm;
1498	new->parent = gmap_get(parent);
1499	new->orig_asce = asce;
1500	new->edat_level = edat_level;
1501	new->initialized = false;
1502	spin_lock(&parent->shadow_lock);
1503	/* Recheck if another CPU created the same shadow */
1504	sg = gmap_find_shadow(parent, asce, edat_level);
1505	if (sg) {
1506		spin_unlock(&parent->shadow_lock);
1507		gmap_free(new);
1508		return sg;
1509	}
1510	if (asce & _ASCE_REAL_SPACE) {
1511		/* only allow one real-space gmap shadow */
1512		list_for_each_entry(sg, &parent->children, list) {
1513			if (sg->orig_asce & _ASCE_REAL_SPACE) {
1514				spin_lock(&sg->guest_table_lock);
1515				gmap_unshadow(sg);
1516				spin_unlock(&sg->guest_table_lock);
1517				list_del(&sg->list);
1518				gmap_put(sg);
1519				break;
1520			}
1521		}
1522	}
1523	atomic_set(&new->ref_count, 2);
1524	list_add(&new->list, &parent->children);
1525	if (asce & _ASCE_REAL_SPACE) {
1526		/* nothing to protect, return right away */
1527		new->initialized = true;
1528		spin_unlock(&parent->shadow_lock);
1529		return new;
1530	}
1531	spin_unlock(&parent->shadow_lock);
1532	/* protect after insertion, so it will get properly invalidated */
1533	down_read(&parent->mm->mmap_sem);
1534	rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1535				((asce & _ASCE_TABLE_LENGTH) + 1) * 4096,
1536				PROT_READ, PGSTE_VSIE_BIT);
1537	up_read(&parent->mm->mmap_sem);
1538	spin_lock(&parent->shadow_lock);
1539	new->initialized = true;
1540	if (rc) {
1541		list_del(&new->list);
1542		gmap_free(new);
1543		new = ERR_PTR(rc);
1544	}
1545	spin_unlock(&parent->shadow_lock);
1546	return new;
1547}
1548EXPORT_SYMBOL_GPL(gmap_shadow);
1549
1550/**
1551 * gmap_shadow_r2t - create an empty shadow region 2 table
1552 * @sg: pointer to the shadow guest address space structure
1553 * @saddr: faulting address in the shadow gmap
1554 * @r2t: parent gmap address of the region 2 table to get shadowed
1555 * @fake: r2t references contiguous guest memory block, not a r2t
1556 *
1557 * The r2t parameter specifies the address of the source table. The
1558 * four pages of the source table are made read-only in the parent gmap
1559 * address space. A write to the source table area @r2t will automatically
1560 * remove the shadow r2 table and all of its decendents.
1561 *
1562 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1563 * shadow table structure is incomplete, -ENOMEM if out of memory and
1564 * -EFAULT if an address in the parent gmap could not be resolved.
1565 *
1566 * Called with sg->mm->mmap_sem in read.
1567 */
1568int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1569		    int fake)
1570{
1571	unsigned long raddr, origin, offset, len;
1572	unsigned long *s_r2t, *table;
 
1573	struct page *page;
1574	int rc;
1575
1576	BUG_ON(!gmap_is_shadow(sg));
1577	/* Allocate a shadow region second table */
1578	page = alloc_pages(GFP_KERNEL, 2);
1579	if (!page)
1580		return -ENOMEM;
1581	page->index = r2t & _REGION_ENTRY_ORIGIN;
1582	if (fake)
1583		page->index |= GMAP_SHADOW_FAKE_TABLE;
1584	s_r2t = (unsigned long *) page_to_phys(page);
1585	/* Install shadow region second table */
1586	spin_lock(&sg->guest_table_lock);
1587	table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1588	if (!table) {
1589		rc = -EAGAIN;		/* Race with unshadow */
1590		goto out_free;
1591	}
1592	if (!(*table & _REGION_ENTRY_INVALID)) {
1593		rc = 0;			/* Already established */
1594		goto out_free;
1595	} else if (*table & _REGION_ENTRY_ORIGIN) {
1596		rc = -EAGAIN;		/* Race with shadow */
1597		goto out_free;
1598	}
1599	crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
1600	/* mark as invalid as long as the parent table is not protected */
1601	*table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
1602		 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1603	if (sg->edat_level >= 1)
1604		*table |= (r2t & _REGION_ENTRY_PROTECT);
1605	list_add(&page->lru, &sg->crst_list);
1606	if (fake) {
1607		/* nothing to protect for fake tables */
1608		*table &= ~_REGION_ENTRY_INVALID;
1609		spin_unlock(&sg->guest_table_lock);
1610		return 0;
1611	}
1612	spin_unlock(&sg->guest_table_lock);
1613	/* Make r2t read-only in parent gmap page table */
1614	raddr = (saddr & 0xffe0000000000000UL) | _SHADOW_RMAP_REGION1;
1615	origin = r2t & _REGION_ENTRY_ORIGIN;
1616	offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * 4096;
1617	len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * 4096 - offset;
1618	rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1619	spin_lock(&sg->guest_table_lock);
1620	if (!rc) {
1621		table = gmap_table_walk(sg, saddr, 4);
1622		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1623			      (unsigned long) s_r2t)
1624			rc = -EAGAIN;		/* Race with unshadow */
1625		else
1626			*table &= ~_REGION_ENTRY_INVALID;
1627	} else {
1628		gmap_unshadow_r2t(sg, raddr);
1629	}
1630	spin_unlock(&sg->guest_table_lock);
1631	return rc;
1632out_free:
1633	spin_unlock(&sg->guest_table_lock);
1634	__free_pages(page, 2);
1635	return rc;
1636}
1637EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1638
1639/**
1640 * gmap_shadow_r3t - create a shadow region 3 table
1641 * @sg: pointer to the shadow guest address space structure
1642 * @saddr: faulting address in the shadow gmap
1643 * @r3t: parent gmap address of the region 3 table to get shadowed
1644 * @fake: r3t references contiguous guest memory block, not a r3t
1645 *
1646 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1647 * shadow table structure is incomplete, -ENOMEM if out of memory and
1648 * -EFAULT if an address in the parent gmap could not be resolved.
1649 *
1650 * Called with sg->mm->mmap_sem in read.
1651 */
1652int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1653		    int fake)
1654{
1655	unsigned long raddr, origin, offset, len;
1656	unsigned long *s_r3t, *table;
 
1657	struct page *page;
1658	int rc;
1659
1660	BUG_ON(!gmap_is_shadow(sg));
1661	/* Allocate a shadow region second table */
1662	page = alloc_pages(GFP_KERNEL, 2);
1663	if (!page)
1664		return -ENOMEM;
1665	page->index = r3t & _REGION_ENTRY_ORIGIN;
1666	if (fake)
1667		page->index |= GMAP_SHADOW_FAKE_TABLE;
1668	s_r3t = (unsigned long *) page_to_phys(page);
1669	/* Install shadow region second table */
1670	spin_lock(&sg->guest_table_lock);
1671	table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1672	if (!table) {
1673		rc = -EAGAIN;		/* Race with unshadow */
1674		goto out_free;
1675	}
1676	if (!(*table & _REGION_ENTRY_INVALID)) {
1677		rc = 0;			/* Already established */
1678		goto out_free;
1679	} else if (*table & _REGION_ENTRY_ORIGIN) {
1680		rc = -EAGAIN;		/* Race with shadow */
 
1681	}
1682	crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
1683	/* mark as invalid as long as the parent table is not protected */
1684	*table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
1685		 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1686	if (sg->edat_level >= 1)
1687		*table |= (r3t & _REGION_ENTRY_PROTECT);
1688	list_add(&page->lru, &sg->crst_list);
1689	if (fake) {
1690		/* nothing to protect for fake tables */
1691		*table &= ~_REGION_ENTRY_INVALID;
1692		spin_unlock(&sg->guest_table_lock);
1693		return 0;
1694	}
1695	spin_unlock(&sg->guest_table_lock);
1696	/* Make r3t read-only in parent gmap page table */
1697	raddr = (saddr & 0xfffffc0000000000UL) | _SHADOW_RMAP_REGION2;
1698	origin = r3t & _REGION_ENTRY_ORIGIN;
1699	offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * 4096;
1700	len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * 4096 - offset;
1701	rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1702	spin_lock(&sg->guest_table_lock);
1703	if (!rc) {
1704		table = gmap_table_walk(sg, saddr, 3);
1705		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1706			      (unsigned long) s_r3t)
1707			rc = -EAGAIN;		/* Race with unshadow */
1708		else
1709			*table &= ~_REGION_ENTRY_INVALID;
1710	} else {
1711		gmap_unshadow_r3t(sg, raddr);
1712	}
1713	spin_unlock(&sg->guest_table_lock);
1714	return rc;
1715out_free:
1716	spin_unlock(&sg->guest_table_lock);
1717	__free_pages(page, 2);
1718	return rc;
1719}
1720EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1721
1722/**
1723 * gmap_shadow_sgt - create a shadow segment table
1724 * @sg: pointer to the shadow guest address space structure
1725 * @saddr: faulting address in the shadow gmap
1726 * @sgt: parent gmap address of the segment table to get shadowed
1727 * @fake: sgt references contiguous guest memory block, not a sgt
1728 *
1729 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1730 * shadow table structure is incomplete, -ENOMEM if out of memory and
1731 * -EFAULT if an address in the parent gmap could not be resolved.
1732 *
1733 * Called with sg->mm->mmap_sem in read.
1734 */
1735int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1736		    int fake)
1737{
1738	unsigned long raddr, origin, offset, len;
1739	unsigned long *s_sgt, *table;
 
1740	struct page *page;
1741	int rc;
1742
1743	BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1744	/* Allocate a shadow segment table */
1745	page = alloc_pages(GFP_KERNEL, 2);
1746	if (!page)
1747		return -ENOMEM;
1748	page->index = sgt & _REGION_ENTRY_ORIGIN;
1749	if (fake)
1750		page->index |= GMAP_SHADOW_FAKE_TABLE;
1751	s_sgt = (unsigned long *) page_to_phys(page);
1752	/* Install shadow region second table */
1753	spin_lock(&sg->guest_table_lock);
1754	table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1755	if (!table) {
1756		rc = -EAGAIN;		/* Race with unshadow */
1757		goto out_free;
1758	}
1759	if (!(*table & _REGION_ENTRY_INVALID)) {
1760		rc = 0;			/* Already established */
1761		goto out_free;
1762	} else if (*table & _REGION_ENTRY_ORIGIN) {
1763		rc = -EAGAIN;		/* Race with shadow */
1764		goto out_free;
1765	}
1766	crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
1767	/* mark as invalid as long as the parent table is not protected */
1768	*table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
1769		 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1770	if (sg->edat_level >= 1)
1771		*table |= sgt & _REGION_ENTRY_PROTECT;
1772	list_add(&page->lru, &sg->crst_list);
1773	if (fake) {
1774		/* nothing to protect for fake tables */
1775		*table &= ~_REGION_ENTRY_INVALID;
1776		spin_unlock(&sg->guest_table_lock);
1777		return 0;
1778	}
1779	spin_unlock(&sg->guest_table_lock);
1780	/* Make sgt read-only in parent gmap page table */
1781	raddr = (saddr & 0xffffffff80000000UL) | _SHADOW_RMAP_REGION3;
1782	origin = sgt & _REGION_ENTRY_ORIGIN;
1783	offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * 4096;
1784	len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * 4096 - offset;
1785	rc = gmap_protect_rmap(sg, raddr, origin + offset, len, PROT_READ);
1786	spin_lock(&sg->guest_table_lock);
1787	if (!rc) {
1788		table = gmap_table_walk(sg, saddr, 2);
1789		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
1790			      (unsigned long) s_sgt)
1791			rc = -EAGAIN;		/* Race with unshadow */
1792		else
1793			*table &= ~_REGION_ENTRY_INVALID;
1794	} else {
1795		gmap_unshadow_sgt(sg, raddr);
1796	}
1797	spin_unlock(&sg->guest_table_lock);
1798	return rc;
1799out_free:
1800	spin_unlock(&sg->guest_table_lock);
1801	__free_pages(page, 2);
1802	return rc;
1803}
1804EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
1805
1806/**
1807 * gmap_shadow_lookup_pgtable - find a shadow page table
1808 * @sg: pointer to the shadow guest address space structure
1809 * @saddr: the address in the shadow aguest address space
1810 * @pgt: parent gmap address of the page table to get shadowed
1811 * @dat_protection: if the pgtable is marked as protected by dat
1812 * @fake: pgt references contiguous guest memory block, not a pgtable
1813 *
1814 * Returns 0 if the shadow page table was found and -EAGAIN if the page
1815 * table was not found.
1816 *
1817 * Called with sg->mm->mmap_sem in read.
1818 */
1819int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
1820			   unsigned long *pgt, int *dat_protection,
1821			   int *fake)
1822{
1823	unsigned long *table;
1824	struct page *page;
1825	int rc;
1826
1827	BUG_ON(!gmap_is_shadow(sg));
1828	spin_lock(&sg->guest_table_lock);
1829	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1830	if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
1831		/* Shadow page tables are full pages (pte+pgste) */
1832		page = pfn_to_page(*table >> PAGE_SHIFT);
1833		*pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
1834		*dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
1835		*fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
1836		rc = 0;
1837	} else  {
1838		rc = -EAGAIN;
1839	}
1840	spin_unlock(&sg->guest_table_lock);
1841	return rc;
1842
1843}
1844EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
1845
1846/**
1847 * gmap_shadow_pgt - instantiate a shadow page table
1848 * @sg: pointer to the shadow guest address space structure
1849 * @saddr: faulting address in the shadow gmap
1850 * @pgt: parent gmap address of the page table to get shadowed
1851 * @fake: pgt references contiguous guest memory block, not a pgtable
1852 *
1853 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1854 * shadow table structure is incomplete, -ENOMEM if out of memory,
1855 * -EFAULT if an address in the parent gmap could not be resolved and
1856 *
1857 * Called with gmap->mm->mmap_sem in read
1858 */
1859int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
1860		    int fake)
1861{
1862	unsigned long raddr, origin;
1863	unsigned long *s_pgt, *table;
1864	struct page *page;
 
1865	int rc;
1866
1867	BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
1868	/* Allocate a shadow page table */
1869	page = page_table_alloc_pgste(sg->mm);
1870	if (!page)
1871		return -ENOMEM;
1872	page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
1873	if (fake)
1874		page->index |= GMAP_SHADOW_FAKE_TABLE;
1875	s_pgt = (unsigned long *) page_to_phys(page);
1876	/* Install shadow page table */
1877	spin_lock(&sg->guest_table_lock);
1878	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
1879	if (!table) {
1880		rc = -EAGAIN;		/* Race with unshadow */
1881		goto out_free;
1882	}
1883	if (!(*table & _SEGMENT_ENTRY_INVALID)) {
1884		rc = 0;			/* Already established */
1885		goto out_free;
1886	} else if (*table & _SEGMENT_ENTRY_ORIGIN) {
1887		rc = -EAGAIN;		/* Race with shadow */
1888		goto out_free;
1889	}
1890	/* mark as invalid as long as the parent table is not protected */
1891	*table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
1892		 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
1893	list_add(&page->lru, &sg->pt_list);
1894	if (fake) {
1895		/* nothing to protect for fake tables */
1896		*table &= ~_SEGMENT_ENTRY_INVALID;
1897		spin_unlock(&sg->guest_table_lock);
1898		return 0;
1899	}
1900	spin_unlock(&sg->guest_table_lock);
1901	/* Make pgt read-only in parent gmap page table (not the pgste) */
1902	raddr = (saddr & 0xfffffffffff00000UL) | _SHADOW_RMAP_SEGMENT;
1903	origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
1904	rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE, PROT_READ);
1905	spin_lock(&sg->guest_table_lock);
1906	if (!rc) {
1907		table = gmap_table_walk(sg, saddr, 1);
1908		if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
1909			      (unsigned long) s_pgt)
1910			rc = -EAGAIN;		/* Race with unshadow */
1911		else
1912			*table &= ~_SEGMENT_ENTRY_INVALID;
1913	} else {
1914		gmap_unshadow_pgt(sg, raddr);
1915	}
1916	spin_unlock(&sg->guest_table_lock);
1917	return rc;
1918out_free:
1919	spin_unlock(&sg->guest_table_lock);
1920	page_table_free_pgste(page);
1921	return rc;
1922
1923}
1924EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
1925
1926/**
1927 * gmap_shadow_page - create a shadow page mapping
1928 * @sg: pointer to the shadow guest address space structure
1929 * @saddr: faulting address in the shadow gmap
1930 * @pte: pte in parent gmap address space to get shadowed
1931 *
1932 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1933 * shadow table structure is incomplete, -ENOMEM if out of memory and
1934 * -EFAULT if an address in the parent gmap could not be resolved.
1935 *
1936 * Called with sg->mm->mmap_sem in read.
1937 */
1938int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
1939{
1940	struct gmap *parent;
1941	struct gmap_rmap *rmap;
1942	unsigned long vmaddr, paddr;
1943	spinlock_t *ptl;
1944	pte_t *sptep, *tptep;
1945	int prot;
1946	int rc;
1947
1948	BUG_ON(!gmap_is_shadow(sg));
1949	parent = sg->parent;
1950	prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
1951
1952	rmap = kzalloc(sizeof(*rmap), GFP_KERNEL);
1953	if (!rmap)
1954		return -ENOMEM;
1955	rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
1956
1957	while (1) {
1958		paddr = pte_val(pte) & PAGE_MASK;
1959		vmaddr = __gmap_translate(parent, paddr);
1960		if (IS_ERR_VALUE(vmaddr)) {
1961			rc = vmaddr;
1962			break;
1963		}
1964		rc = radix_tree_preload(GFP_KERNEL);
1965		if (rc)
1966			break;
1967		rc = -EAGAIN;
1968		sptep = gmap_pte_op_walk(parent, paddr, &ptl);
1969		if (sptep) {
1970			spin_lock(&sg->guest_table_lock);
1971			/* Get page table pointer */
1972			tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
1973			if (!tptep) {
1974				spin_unlock(&sg->guest_table_lock);
1975				gmap_pte_op_end(ptl);
1976				radix_tree_preload_end();
1977				break;
1978			}
1979			rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
1980			if (rc > 0) {
1981				/* Success and a new mapping */
1982				gmap_insert_rmap(sg, vmaddr, rmap);
1983				rmap = NULL;
1984				rc = 0;
1985			}
1986			gmap_pte_op_end(ptl);
1987			spin_unlock(&sg->guest_table_lock);
1988		}
1989		radix_tree_preload_end();
1990		if (!rc)
1991			break;
1992		rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
1993		if (rc)
1994			break;
1995	}
1996	kfree(rmap);
1997	return rc;
1998}
1999EXPORT_SYMBOL_GPL(gmap_shadow_page);
2000
2001/**
2002 * gmap_shadow_notify - handle notifications for shadow gmap
2003 *
2004 * Called with sg->parent->shadow_lock.
2005 */
2006static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2007			       unsigned long offset, pte_t *pte)
2008{
2009	struct gmap_rmap *rmap, *rnext, *head;
2010	unsigned long gaddr, start, end, bits, raddr;
2011	unsigned long *table;
2012
2013	BUG_ON(!gmap_is_shadow(sg));
2014	spin_lock(&sg->parent->guest_table_lock);
2015	table = radix_tree_lookup(&sg->parent->host_to_guest,
2016				  vmaddr >> PMD_SHIFT);
2017	gaddr = table ? __gmap_segment_gaddr(table) + offset : 0;
2018	spin_unlock(&sg->parent->guest_table_lock);
2019	if (!table)
2020		return;
2021
2022	spin_lock(&sg->guest_table_lock);
2023	if (sg->removed) {
2024		spin_unlock(&sg->guest_table_lock);
2025		return;
2026	}
2027	/* Check for top level table */
2028	start = sg->orig_asce & _ASCE_ORIGIN;
2029	end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * 4096;
2030	if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2031	    gaddr < end) {
2032		/* The complete shadow table has to go */
2033		gmap_unshadow(sg);
2034		spin_unlock(&sg->guest_table_lock);
2035		list_del(&sg->list);
2036		gmap_put(sg);
2037		return;
2038	}
2039	/* Remove the page table tree from on specific entry */
2040	head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> 12);
2041	gmap_for_each_rmap_safe(rmap, rnext, head) {
2042		bits = rmap->raddr & _SHADOW_RMAP_MASK;
2043		raddr = rmap->raddr ^ bits;
2044		switch (bits) {
2045		case _SHADOW_RMAP_REGION1:
2046			gmap_unshadow_r2t(sg, raddr);
2047			break;
2048		case _SHADOW_RMAP_REGION2:
2049			gmap_unshadow_r3t(sg, raddr);
2050			break;
2051		case _SHADOW_RMAP_REGION3:
2052			gmap_unshadow_sgt(sg, raddr);
2053			break;
2054		case _SHADOW_RMAP_SEGMENT:
2055			gmap_unshadow_pgt(sg, raddr);
2056			break;
2057		case _SHADOW_RMAP_PGTABLE:
2058			gmap_unshadow_page(sg, raddr);
2059			break;
2060		}
2061		kfree(rmap);
2062	}
2063	spin_unlock(&sg->guest_table_lock);
2064}
2065
2066/**
2067 * ptep_notify - call all invalidation callbacks for a specific pte.
2068 * @mm: pointer to the process mm_struct
2069 * @addr: virtual address in the process address space
2070 * @pte: pointer to the page table entry
2071 * @bits: bits from the pgste that caused the notify call
2072 *
2073 * This function is assumed to be called with the page table lock held
2074 * for the pte to notify.
2075 */
2076void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2077		 pte_t *pte, unsigned long bits)
2078{
2079	unsigned long offset, gaddr;
2080	unsigned long *table;
2081	struct gmap *gmap, *sg, *next;
2082
2083	offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2084	offset = offset * (4096 / sizeof(pte_t));
2085	rcu_read_lock();
2086	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
 
 
 
 
 
 
 
 
 
2087		if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2088			spin_lock(&gmap->shadow_lock);
2089			list_for_each_entry_safe(sg, next,
2090						 &gmap->children, list)
2091				gmap_shadow_notify(sg, vmaddr, offset, pte);
2092			spin_unlock(&gmap->shadow_lock);
2093		}
2094		if (!(bits & PGSTE_IN_BIT))
2095			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2096		spin_lock(&gmap->guest_table_lock);
2097		table = radix_tree_lookup(&gmap->host_to_guest,
2098					  vmaddr >> PMD_SHIFT);
2099		if (table)
2100			gaddr = __gmap_segment_gaddr(table) + offset;
 
 
 
 
 
 
 
 
 
 
 
 
 
2101		spin_unlock(&gmap->guest_table_lock);
2102		if (table)
2103			gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2104	}
2105	rcu_read_unlock();
2106}
2107EXPORT_SYMBOL_GPL(ptep_notify);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2108
2109static inline void thp_split_mm(struct mm_struct *mm)
2110{
2111#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2112	struct vm_area_struct *vma;
2113	unsigned long addr;
2114
2115	for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
2116		for (addr = vma->vm_start;
2117		     addr < vma->vm_end;
2118		     addr += PAGE_SIZE)
2119			follow_page(vma, addr, FOLL_SPLIT);
2120		vma->vm_flags &= ~VM_HUGEPAGE;
2121		vma->vm_flags |= VM_NOHUGEPAGE;
 
2122	}
2123	mm->def_flags |= VM_NOHUGEPAGE;
2124#endif
2125}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2126
2127/*
2128 * switch on pgstes for its userspace process (for kvm)
2129 */
2130int s390_enable_sie(void)
2131{
2132	struct mm_struct *mm = current->mm;
2133
2134	/* Do we have pgstes? if yes, we are done */
2135	if (mm_has_pgste(mm))
2136		return 0;
2137	/* Fail if the page tables are 2K */
2138	if (!mm_alloc_pgste(mm))
2139		return -EINVAL;
2140	down_write(&mm->mmap_sem);
2141	mm->context.has_pgste = 1;
2142	/* split thp mappings and disable thp for future mappings */
2143	thp_split_mm(mm);
2144	up_write(&mm->mmap_sem);
 
2145	return 0;
2146}
2147EXPORT_SYMBOL_GPL(s390_enable_sie);
2148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2149/*
2150 * Enable storage key handling from now on and initialize the storage
2151 * keys with the default key.
2152 */
2153static int __s390_enable_skey(pte_t *pte, unsigned long addr,
2154			      unsigned long next, struct mm_walk *walk)
 
 
 
 
 
 
 
 
 
 
 
 
 
2155{
 
 
 
 
 
 
 
 
 
 
 
 
2156	/*
2157	 * Remove all zero page mappings,
2158	 * after establishing a policy to forbid zero page mappings
2159	 * following faults for that page will get fresh anonymous pages
 
2160	 */
2161	if (is_zero_pfn(pte_pfn(*pte)))
2162		ptep_xchg_direct(walk->mm, addr, pte, __pte(_PAGE_INVALID));
2163	/* Clear storage key */
2164	ptep_zap_key(walk->mm, addr, pte);
 
 
 
 
 
2165	return 0;
2166}
2167
 
 
 
 
 
 
2168int s390_enable_skey(void)
2169{
2170	struct mm_walk walk = { .pte_entry = __s390_enable_skey };
2171	struct mm_struct *mm = current->mm;
2172	struct vm_area_struct *vma;
2173	int rc = 0;
2174
2175	down_write(&mm->mmap_sem);
2176	if (mm_use_skey(mm))
2177		goto out_up;
2178
2179	mm->context.use_skey = 1;
2180	for (vma = mm->mmap; vma; vma = vma->vm_next) {
2181		if (ksm_madvise(vma, vma->vm_start, vma->vm_end,
2182				MADV_UNMERGEABLE, &vma->vm_flags)) {
2183			mm->context.use_skey = 0;
2184			rc = -ENOMEM;
2185			goto out_up;
2186		}
2187	}
2188	mm->def_flags &= ~VM_MERGEABLE;
2189
2190	walk.mm = mm;
2191	walk_page_range(0, TASK_SIZE, &walk);
2192
2193out_up:
2194	up_write(&mm->mmap_sem);
2195	return rc;
2196}
2197EXPORT_SYMBOL_GPL(s390_enable_skey);
2198
2199/*
2200 * Reset CMMA state, make all pages stable again.
2201 */
2202static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2203			     unsigned long next, struct mm_walk *walk)
2204{
2205	ptep_zap_unused(walk->mm, addr, pte, 1);
2206	return 0;
2207}
2208
 
 
 
 
2209void s390_reset_cmma(struct mm_struct *mm)
2210{
2211	struct mm_walk walk = { .pte_entry = __s390_reset_cmma };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2212
2213	down_write(&mm->mmap_sem);
2214	walk.mm = mm;
2215	walk_page_range(0, TASK_SIZE, &walk);
2216	up_write(&mm->mmap_sem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2217}
2218EXPORT_SYMBOL_GPL(s390_reset_cmma);