gmap.c 77.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4
/*
 *  KVM guest address space mapping code
 *
5
 *    Copyright IBM Corp. 2007, 2020
6
 *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 8
 *		 David Hildenbrand <david@redhat.com>
 *		 Janosch Frank <frankja@linux.vnet.ibm.com>
9 10 11
 */

#include <linux/kernel.h>
12
#include <linux/pagewalk.h>
13 14 15 16 17 18 19
#include <linux/swap.h>
#include <linux/smp.h>
#include <linux/spinlock.h>
#include <linux/slab.h>
#include <linux/swapops.h>
#include <linux/ksm.h>
#include <linux/mman.h>
20
#include <linux/pgtable.h>
21

22 23 24 25
#include <asm/pgalloc.h>
#include <asm/gmap.h>
#include <asm/tlb.h>

26 27
#define GMAP_SHADOW_FAKE_TABLE 1ULL

28
/**
29
 * gmap_alloc - allocate and initialize a guest address space
30
 * @limit: maximum address of the gmap address space
31 32 33
 *
 * Returns a guest address space structure.
 */
34
static struct gmap *gmap_alloc(unsigned long limit)
35 36 37 38 39 40
{
	struct gmap *gmap;
	struct page *page;
	unsigned long *table;
	unsigned long etype, atype;

41 42
	if (limit < _REGION3_SIZE) {
		limit = _REGION3_SIZE - 1;
43 44
		atype = _ASCE_TYPE_SEGMENT;
		etype = _SEGMENT_ENTRY_EMPTY;
45 46
	} else if (limit < _REGION2_SIZE) {
		limit = _REGION2_SIZE - 1;
47 48
		atype = _ASCE_TYPE_REGION3;
		etype = _REGION3_ENTRY_EMPTY;
49 50
	} else if (limit < _REGION1_SIZE) {
		limit = _REGION1_SIZE - 1;
51 52 53 54 55 56 57
		atype = _ASCE_TYPE_REGION2;
		etype = _REGION2_ENTRY_EMPTY;
	} else {
		limit = -1UL;
		atype = _ASCE_TYPE_REGION1;
		etype = _REGION1_ENTRY_EMPTY;
	}
58
	gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
59 60 61
	if (!gmap)
		goto out;
	INIT_LIST_HEAD(&gmap->crst_list);
62 63
	INIT_LIST_HEAD(&gmap->children);
	INIT_LIST_HEAD(&gmap->pt_list);
64 65 66
	INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
	INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
	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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	if (!page)
		goto out_free;
	page->index = 0;
	list_add(&page->lru, &gmap->crst_list);
	table = (unsigned long *) page_to_phys(page);
	crst_table_init(table, etype);
	gmap->table = table;
	gmap->asce = atype | _ASCE_TABLE_LENGTH |
		_ASCE_USER_BITS | __pa(table);
	gmap->asce_end = limit;
	return gmap;

out_free:
	kfree(gmap);
out:
	return NULL;
}
88 89 90 91 92 93 94 95 96 97 98

/**
 * gmap_create - create a guest address space
 * @mm: pointer to the parent mm_struct
 * @limit: maximum size of the gmap address space
 *
 * Returns a guest address space structure.
 */
struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
{
	struct gmap *gmap;
99
	unsigned long gmap_asce;
100 101 102 103 104

	gmap = gmap_alloc(limit);
	if (!gmap)
		return NULL;
	gmap->mm = mm;
105
	spin_lock(&mm->context.lock);
106
	list_add_rcu(&gmap->list, &mm->context.gmap_list);
107 108 109 110 111
	if (list_is_singular(&mm->context.gmap_list))
		gmap_asce = gmap->asce;
	else
		gmap_asce = -1UL;
	WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
112
	spin_unlock(&mm->context.lock);
113 114 115
	return gmap;
}
EXPORT_SYMBOL_GPL(gmap_create);
116 117 118 119

static void gmap_flush_tlb(struct gmap *gmap)
{
	if (MACHINE_HAS_IDTE)
120
		__tlb_flush_idte(gmap->asce);
121 122 123 124 125 126 127 128 129
	else
		__tlb_flush_global();
}

static void gmap_radix_tree_free(struct radix_tree_root *root)
{
	struct radix_tree_iter iter;
	unsigned long indices[16];
	unsigned long index;
130
	void __rcu **slot;
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	int i, nr;

	/* A radix tree is freed by deleting all of its entries */
	index = 0;
	do {
		nr = 0;
		radix_tree_for_each_slot(slot, root, &iter, index) {
			indices[nr] = iter.index;
			if (++nr == 16)
				break;
		}
		for (i = 0; i < nr; i++) {
			index = indices[i];
			radix_tree_delete(root, index);
		}
	} while (nr > 0);
}

149 150 151 152 153 154
static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
{
	struct gmap_rmap *rmap, *rnext, *head;
	struct radix_tree_iter iter;
	unsigned long indices[16];
	unsigned long index;
155
	void __rcu **slot;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	int i, nr;

	/* A radix tree is freed by deleting all of its entries */
	index = 0;
	do {
		nr = 0;
		radix_tree_for_each_slot(slot, root, &iter, index) {
			indices[nr] = iter.index;
			if (++nr == 16)
				break;
		}
		for (i = 0; i < nr; i++) {
			index = indices[i];
			head = radix_tree_delete(root, index);
			gmap_for_each_rmap_safe(rmap, rnext, head)
				kfree(rmap);
		}
	} while (nr > 0);
}

176 177 178
/**
 * gmap_free - free a guest address space
 * @gmap: pointer to the guest address space structure
179 180
 *
 * No locks required. There are no references to this gmap anymore.
181
 */
182
static void gmap_free(struct gmap *gmap)
183 184 185
{
	struct page *page, *next;

186 187 188
	/* Flush tlb of all gmaps (if not already done for shadows) */
	if (!(gmap_is_shadow(gmap) && gmap->removed))
		gmap_flush_tlb(gmap);
189 190
	/* Free all segment & region tables. */
	list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
191
		__free_pages(page, CRST_ALLOC_ORDER);
192 193
	gmap_radix_tree_free(&gmap->guest_to_host);
	gmap_radix_tree_free(&gmap->host_to_guest);
194 195 196 197 198 199 200 201 202 203 204

	/* Free additional data for a shadow gmap */
	if (gmap_is_shadow(gmap)) {
		/* Free all page tables. */
		list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
			page_table_free_pgste(page);
		gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
		/* Release reference to the parent */
		gmap_put(gmap->parent);
	}

205 206
	kfree(gmap);
}
207 208 209 210 211 212 213 214 215

/**
 * gmap_get - increase reference counter for guest address space
 * @gmap: pointer to the guest address space structure
 *
 * Returns the gmap pointer
 */
struct gmap *gmap_get(struct gmap *gmap)
{
216
	refcount_inc(&gmap->ref_count);
217 218 219 220 221 222 223 224 225 226 227 228
	return gmap;
}
EXPORT_SYMBOL_GPL(gmap_get);

/**
 * gmap_put - decrease reference counter for guest address space
 * @gmap: pointer to the guest address space structure
 *
 * If the reference counter reaches zero the guest address space is freed.
 */
void gmap_put(struct gmap *gmap)
{
229
	if (refcount_dec_and_test(&gmap->ref_count))
230 231 232 233 234 235 236 237 238 239
		gmap_free(gmap);
}
EXPORT_SYMBOL_GPL(gmap_put);

/**
 * gmap_remove - remove a guest address space but do not free it yet
 * @gmap: pointer to the guest address space structure
 */
void gmap_remove(struct gmap *gmap)
{
240
	struct gmap *sg, *next;
241
	unsigned long gmap_asce;
242 243 244 245 246 247 248 249 250 251

	/* Remove all shadow gmaps linked to this gmap */
	if (!list_empty(&gmap->children)) {
		spin_lock(&gmap->shadow_lock);
		list_for_each_entry_safe(sg, next, &gmap->children, list) {
			list_del(&sg->list);
			gmap_put(sg);
		}
		spin_unlock(&gmap->shadow_lock);
	}
252
	/* Remove gmap from the pre-mm list */
253
	spin_lock(&gmap->mm->context.lock);
254
	list_del_rcu(&gmap->list);
255 256 257 258 259 260 261 262
	if (list_empty(&gmap->mm->context.gmap_list))
		gmap_asce = 0;
	else if (list_is_singular(&gmap->mm->context.gmap_list))
		gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
					     struct gmap, list)->asce;
	else
		gmap_asce = -1UL;
	WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
263
	spin_unlock(&gmap->mm->context.lock);
264 265 266 267 268
	synchronize_rcu();
	/* Put reference */
	gmap_put(gmap);
}
EXPORT_SYMBOL_GPL(gmap_remove);
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

/**
 * gmap_enable - switch primary space to the guest address space
 * @gmap: pointer to the guest address space structure
 */
void gmap_enable(struct gmap *gmap)
{
	S390_lowcore.gmap = (unsigned long) gmap;
}
EXPORT_SYMBOL_GPL(gmap_enable);

/**
 * gmap_disable - switch back to the standard primary address space
 * @gmap: pointer to the guest address space structure
 */
void gmap_disable(struct gmap *gmap)
{
	S390_lowcore.gmap = 0UL;
}
EXPORT_SYMBOL_GPL(gmap_disable);

290 291 292 293 294 295 296 297 298 299 300
/**
 * gmap_get_enabled - get a pointer to the currently enabled gmap
 *
 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
 */
struct gmap *gmap_get_enabled(void)
{
	return (struct gmap *) S390_lowcore.gmap;
}
EXPORT_SYMBOL_GPL(gmap_get_enabled);

301
/*
302
 * gmap_alloc_table is assumed to be called with mmap_lock held
303 304 305 306 307 308 309 310
 */
static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
			    unsigned long init, unsigned long gaddr)
{
	struct page *page;
	unsigned long *new;

	/* since we dont free the gmap table until gmap_free we can unlock */
311
	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
312 313 314 315
	if (!page)
		return -ENOMEM;
	new = (unsigned long *) page_to_phys(page);
	crst_table_init(new, init);
316
	spin_lock(&gmap->guest_table_lock);
317 318 319 320 321 322 323
	if (*table & _REGION_ENTRY_INVALID) {
		list_add(&page->lru, &gmap->crst_list);
		*table = (unsigned long) new | _REGION_ENTRY_LENGTH |
			(*table & _REGION_ENTRY_TYPE_MASK);
		page->index = gaddr;
		page = NULL;
	}
324
	spin_unlock(&gmap->guest_table_lock);
325
	if (page)
326
		__free_pages(page, CRST_ALLOC_ORDER);
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	return 0;
}

/**
 * __gmap_segment_gaddr - find virtual address from segment pointer
 * @entry: pointer to a segment table entry in the guest address space
 *
 * Returns the virtual address in the guest address space for the segment
 */
static unsigned long __gmap_segment_gaddr(unsigned long *entry)
{
	struct page *page;
	unsigned long offset, mask;

	offset = (unsigned long) entry / sizeof(unsigned long);
	offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
	mask = ~(PTRS_PER_PMD * sizeof(pmd_t) - 1);
	page = virt_to_page((void *)((unsigned long) entry & mask));
	return page->index + offset;
}

/**
 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
 * @gmap: pointer to the guest address space structure
 * @vmaddr: address in the host process address space
 *
 * Returns 1 if a TLB flush is required
 */
static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
{
	unsigned long *entry;
	int flush = 0;

360
	BUG_ON(gmap_is_shadow(gmap));
361 362 363
	spin_lock(&gmap->guest_table_lock);
	entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
	if (entry) {
364 365
		flush = (*entry != _SEGMENT_ENTRY_EMPTY);
		*entry = _SEGMENT_ENTRY_EMPTY;
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	}
	spin_unlock(&gmap->guest_table_lock);
	return flush;
}

/**
 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
 * @gmap: pointer to the guest address space structure
 * @gaddr: address in the guest address space
 *
 * Returns 1 if a TLB flush is required
 */
static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
{
	unsigned long vmaddr;

	vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
						   gaddr >> PMD_SHIFT);
	return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
}

/**
 * gmap_unmap_segment - unmap segment from the guest address space
 * @gmap: pointer to the guest address space structure
 * @to: address in the guest address space
 * @len: length of the memory area to unmap
 *
 * Returns 0 if the unmap succeeded, -EINVAL if not.
 */
int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
{
	unsigned long off;
	int flush;

400
	BUG_ON(gmap_is_shadow(gmap));
401 402 403 404 405 406
	if ((to | len) & (PMD_SIZE - 1))
		return -EINVAL;
	if (len == 0 || to + len < to)
		return -EINVAL;

	flush = 0;
407
	mmap_write_lock(gmap->mm);
408 409
	for (off = 0; off < len; off += PMD_SIZE)
		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
410
	mmap_write_unlock(gmap->mm);
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
	if (flush)
		gmap_flush_tlb(gmap);
	return 0;
}
EXPORT_SYMBOL_GPL(gmap_unmap_segment);

/**
 * gmap_map_segment - map a segment to the guest address space
 * @gmap: pointer to the guest address space structure
 * @from: source address in the parent address space
 * @to: target address in the guest address space
 * @len: length of the memory area to map
 *
 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
 */
int gmap_map_segment(struct gmap *gmap, unsigned long from,
		     unsigned long to, unsigned long len)
{
	unsigned long off;
	int flush;

432
	BUG_ON(gmap_is_shadow(gmap));
433 434 435
	if ((from | to | len) & (PMD_SIZE - 1))
		return -EINVAL;
	if (len == 0 || from + len < from || to + len < to ||
436
	    from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
437 438 439
		return -EINVAL;

	flush = 0;
440
	mmap_write_lock(gmap->mm);
441 442 443 444 445 446 447 448 449
	for (off = 0; off < len; off += PMD_SIZE) {
		/* Remove old translation */
		flush |= __gmap_unmap_by_gaddr(gmap, to + off);
		/* Store new translation */
		if (radix_tree_insert(&gmap->guest_to_host,
				      (to + off) >> PMD_SHIFT,
				      (void *) from + off))
			break;
	}
450
	mmap_write_unlock(gmap->mm);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	if (flush)
		gmap_flush_tlb(gmap);
	if (off >= len)
		return 0;
	gmap_unmap_segment(gmap, to, len);
	return -ENOMEM;
}
EXPORT_SYMBOL_GPL(gmap_map_segment);

/**
 * __gmap_translate - translate a guest address to a user space address
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: guest address
 *
 * Returns user space address which corresponds to the guest address or
 * -EFAULT if no such mapping exists.
 * This function does not establish potentially missing page table entries.
468
 * The mmap_lock of the mm that belongs to the address space must be held
469
 * when this function gets called.
470 471
 *
 * Note: Can also be called for shadow gmaps.
472 473 474 475 476 477 478
 */
unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
{
	unsigned long vmaddr;

	vmaddr = (unsigned long)
		radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
479
	/* Note: guest_to_host is empty for a shadow gmap */
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
}
EXPORT_SYMBOL_GPL(__gmap_translate);

/**
 * gmap_translate - translate a guest address to a user space address
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: guest address
 *
 * Returns user space address which corresponds to the guest address or
 * -EFAULT if no such mapping exists.
 * This function does not establish potentially missing page table entries.
 */
unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
{
	unsigned long rc;

497
	mmap_read_lock(gmap->mm);
498
	rc = __gmap_translate(gmap, gaddr);
499
	mmap_read_unlock(gmap->mm);
500 501 502 503 504 505
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_translate);

/**
 * gmap_unlink - disconnect a page table from the gmap shadow tables
506
 * @mm: pointer to the parent mm_struct
507 508 509 510 511 512 513 514 515
 * @table: pointer to the host page table
 * @vmaddr: vm address associated with the host page table
 */
void gmap_unlink(struct mm_struct *mm, unsigned long *table,
		 unsigned long vmaddr)
{
	struct gmap *gmap;
	int flush;

516 517
	rcu_read_lock();
	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
518 519 520 521
		flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
		if (flush)
			gmap_flush_tlb(gmap);
	}
522
	rcu_read_unlock();
523 524
}

525 526 527
static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
			   unsigned long gaddr);

528
/**
529
 * __gmap_link - set up shadow page tables to connect a host to a guest address
530 531 532 533 534 535
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: guest address
 * @vmaddr: vm address
 *
 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 * if the vm address is already mapped to a different guest segment.
536
 * The mmap_lock of the mm that belongs to the address space must be held
537 538 539 540 541 542 543 544
 * when this function gets called.
 */
int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
{
	struct mm_struct *mm;
	unsigned long *table;
	spinlock_t *ptl;
	pgd_t *pgd;
545
	p4d_t *p4d;
546 547
	pud_t *pud;
	pmd_t *pmd;
548
	u64 unprot;
549 550
	int rc;

551
	BUG_ON(gmap_is_shadow(gmap));
552 553 554
	/* Create higher level tables in the gmap page table */
	table = gmap->table;
	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
555
		table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
556 557
		if ((*table & _REGION_ENTRY_INVALID) &&
		    gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
558
				     gaddr & _REGION1_MASK))
559 560 561 562
			return -ENOMEM;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
	}
	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
563
		table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
564 565
		if ((*table & _REGION_ENTRY_INVALID) &&
		    gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
566
				     gaddr & _REGION2_MASK))
567 568 569 570
			return -ENOMEM;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
	}
	if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
571
		table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
572 573
		if ((*table & _REGION_ENTRY_INVALID) &&
		    gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
574
				     gaddr & _REGION3_MASK))
575 576 577
			return -ENOMEM;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
	}
578
	table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
579 580 581 582
	/* Walk the parent mm page table */
	mm = gmap->mm;
	pgd = pgd_offset(mm, vmaddr);
	VM_BUG_ON(pgd_none(*pgd));
583 584 585
	p4d = p4d_offset(pgd, vmaddr);
	VM_BUG_ON(p4d_none(*p4d));
	pud = pud_offset(p4d, vmaddr);
586
	VM_BUG_ON(pud_none(*pud));
587 588 589
	/* large puds cannot yet be handled */
	if (pud_large(*pud))
		return -EFAULT;
590 591
	pmd = pmd_offset(pud, vmaddr);
	VM_BUG_ON(pmd_none(*pmd));
592 593
	/* Are we allowed to use huge pages? */
	if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
594 595
		return -EFAULT;
	/* Link gmap segment table entry location to page table. */
596
	rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
597 598 599 600
	if (rc)
		return rc;
	ptl = pmd_lock(mm, pmd);
	spin_lock(&gmap->guest_table_lock);
601
	if (*table == _SEGMENT_ENTRY_EMPTY) {
602 603
		rc = radix_tree_insert(&gmap->host_to_guest,
				       vmaddr >> PMD_SHIFT, table);
Janosch Frank's avatar
Janosch Frank committed
604 605
		if (!rc) {
			if (pmd_large(*pmd)) {
606 607 608
				*table = (pmd_val(*pmd) &
					  _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
					| _SEGMENT_ENTRY_GMAP_UC;
Janosch Frank's avatar
Janosch Frank committed
609 610 611 612
			} else
				*table = pmd_val(*pmd) &
					_SEGMENT_ENTRY_HARDWARE_BITS;
		}
613 614 615 616 617 618
	} else if (*table & _SEGMENT_ENTRY_PROTECT &&
		   !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
		unprot = (u64)*table;
		unprot &= ~_SEGMENT_ENTRY_PROTECT;
		unprot |= _SEGMENT_ENTRY_GMAP_UC;
		gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
Janosch Frank's avatar
Janosch Frank committed
619
	}
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
	spin_unlock(&gmap->guest_table_lock);
	spin_unlock(ptl);
	radix_tree_preload_end();
	return rc;
}

/**
 * gmap_fault - resolve a fault on a guest address
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: guest address
 * @fault_flags: flags to pass down to handle_mm_fault()
 *
 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
 * if the vm address is already mapped to a different guest segment.
 */
int gmap_fault(struct gmap *gmap, unsigned long gaddr,
	       unsigned int fault_flags)
{
	unsigned long vmaddr;
	int rc;
	bool unlocked;

642
	mmap_read_lock(gmap->mm);
643 644 645 646 647 648 649 650

retry:
	unlocked = false;
	vmaddr = __gmap_translate(gmap, gaddr);
	if (IS_ERR_VALUE(vmaddr)) {
		rc = vmaddr;
		goto out_up;
	}
651
	if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
652 653 654 655 656
			     &unlocked)) {
		rc = -EFAULT;
		goto out_up;
	}
	/*
657
	 * In the case that fixup_user_fault unlocked the mmap_lock during
658 659 660 661 662 663 664
	 * faultin redo __gmap_translate to not race with a map/unmap_segment.
	 */
	if (unlocked)
		goto retry;

	rc = __gmap_link(gmap, gaddr, vmaddr);
out_up:
665
	mmap_read_unlock(gmap->mm);
666 667 668 669 670
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_fault);

/*
671
 * this function is assumed to be called with mmap_lock held
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
 */
void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
{
	unsigned long vmaddr;
	spinlock_t *ptl;
	pte_t *ptep;

	/* Find the vm address for the guest address */
	vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
						   gaddr >> PMD_SHIFT);
	if (vmaddr) {
		vmaddr |= gaddr & ~PMD_MASK;
		/* Get pointer to the page table entry */
		ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
		if (likely(ptep))
			ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
		pte_unmap_unlock(ptep, ptl);
	}
}
EXPORT_SYMBOL_GPL(__gmap_zap);

void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
{
	unsigned long gaddr, vmaddr, size;
	struct vm_area_struct *vma;

698
	mmap_read_lock(gmap->mm);
699 700 701 702 703 704 705 706 707 708 709
	for (gaddr = from; gaddr < to;
	     gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
		/* Find the vm address for the guest address */
		vmaddr = (unsigned long)
			radix_tree_lookup(&gmap->guest_to_host,
					  gaddr >> PMD_SHIFT);
		if (!vmaddr)
			continue;
		vmaddr |= gaddr & ~PMD_MASK;
		/* Find vma in the parent mm */
		vma = find_vma(gmap->mm, vmaddr);
710 711
		if (!vma)
			continue;
712 713 714 715
		/*
		 * We do not discard pages that are backed by
		 * hugetlbfs, so we don't have to refault them.
		 */
716
		if (is_vm_hugetlb_page(vma))
717
			continue;
718
		size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
719
		zap_page_range(vma, vmaddr, size);
720
	}
721
	mmap_read_unlock(gmap->mm);
722 723 724 725 726 727 728
}
EXPORT_SYMBOL_GPL(gmap_discard);

static LIST_HEAD(gmap_notifier_list);
static DEFINE_SPINLOCK(gmap_notifier_lock);

/**
729
 * gmap_register_pte_notifier - register a pte invalidation callback
730 731
 * @nb: pointer to the gmap notifier block
 */
732
void gmap_register_pte_notifier(struct gmap_notifier *nb)
733 734
{
	spin_lock(&gmap_notifier_lock);
735
	list_add_rcu(&nb->list, &gmap_notifier_list);
736 737
	spin_unlock(&gmap_notifier_lock);
}
738
EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
739 740

/**
741
 * gmap_unregister_pte_notifier - remove a pte invalidation callback
742 743
 * @nb: pointer to the gmap notifier block
 */
744
void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
745 746
{
	spin_lock(&gmap_notifier_lock);
747
	list_del_rcu(&nb->list);
748
	spin_unlock(&gmap_notifier_lock);
749
	synchronize_rcu();
750
}
751
EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
752

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
/**
 * gmap_call_notifier - call all registered invalidation callbacks
 * @gmap: pointer to guest mapping meta data structure
 * @start: start virtual address in the guest address space
 * @end: end virtual address in the guest address space
 */
static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
			       unsigned long end)
{
	struct gmap_notifier *nb;

	list_for_each_entry(nb, &gmap_notifier_list, list)
		nb->notifier_call(gmap, start, end);
}

768
/**
769 770 771
 * gmap_table_walk - walk the gmap page tables
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
772 773 774 775 776 777 778 779 780 781 782
 * @level: page table level to stop at
 *
 * Returns a table entry pointer for the given guest address and @level
 * @level=0 : returns a pointer to a page table table entry (or NULL)
 * @level=1 : returns a pointer to a segment table entry (or NULL)
 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
 *
 * Returns NULL if the gmap page tables could not be walked to the
 * requested level.
783
 *
784
 * Note: Can also be called for shadow gmaps.
785 786
 */
static inline unsigned long *gmap_table_walk(struct gmap *gmap,
787
					     unsigned long gaddr, int level)
788
{
789
	const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
790
	unsigned long *table = gmap->table;
791

792 793
	if (gmap_is_shadow(gmap) && gmap->removed)
		return NULL;
794

795 796 797
	if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
		return NULL;

798 799
	if (asce_type != _ASCE_TYPE_REGION1 &&
	    gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
800
		return NULL;
801

802
	switch (asce_type) {
803
	case _ASCE_TYPE_REGION1:
804
		table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
805 806
		if (level == 4)
			break;
807 808 809
		if (*table & _REGION_ENTRY_INVALID)
			return NULL;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Joe Perches's avatar
Joe Perches committed
810
		fallthrough;
811
	case _ASCE_TYPE_REGION2:
812
		table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
813 814
		if (level == 3)
			break;
815 816 817
		if (*table & _REGION_ENTRY_INVALID)
			return NULL;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Joe Perches's avatar
Joe Perches committed
818
		fallthrough;
819
	case _ASCE_TYPE_REGION3:
820
		table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
821 822
		if (level == 2)
			break;
823 824 825
		if (*table & _REGION_ENTRY_INVALID)
			return NULL;
		table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Joe Perches's avatar
Joe Perches committed
826
		fallthrough;
827
	case _ASCE_TYPE_SEGMENT:
828
		table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
829 830 831 832 833
		if (level == 1)
			break;
		if (*table & _REGION_ENTRY_INVALID)
			return NULL;
		table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
834
		table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
	}
	return table;
}

/**
 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
 *		      and return the pte pointer
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @ptl: pointer to the spinlock pointer
 *
 * Returns a pointer to the locked pte for a guest address, or NULL
 */
static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
			       spinlock_t **ptl)
{
	unsigned long *table;

853
	BUG_ON(gmap_is_shadow(gmap));
854
	/* Walk the gmap page table, lock and get pte pointer */
855
	table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
856
	if (!table || *table & _SEGMENT_ENTRY_INVALID)
857 858 859 860 861 862 863 864 865
		return NULL;
	return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
}

/**
 * gmap_pte_op_fixup - force a page in and connect the gmap page table
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @vmaddr: address in the host process address space
866
 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
867 868 869 870 871 872
 *
 * Returns 0 if the caller can retry __gmap_translate (might fail again),
 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
 * up or connecting the gmap page table.
 */
static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
873
			     unsigned long vmaddr, int prot)
874 875
{
	struct mm_struct *mm = gmap->mm;
876
	unsigned int fault_flags;
877 878
	bool unlocked = false;

879
	BUG_ON(gmap_is_shadow(gmap));
880
	fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
881
	if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
882 883
		return -EFAULT;
	if (unlocked)
884
		/* lost mmap_lock, caller has to retry __gmap_translate */
885 886 887
		return 0;
	/* Connect the page tables */
	return __gmap_link(gmap, gaddr, vmaddr);
888 889 890
}

/**
891 892 893 894 895
 * gmap_pte_op_end - release the page table lock
 * @ptl: pointer to the spinlock pointer
 */
static void gmap_pte_op_end(spinlock_t *ptl)
{
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
	if (ptl)
		spin_unlock(ptl);
}

/**
 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
 *		      and return the pmd pointer
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 *
 * Returns a pointer to the pmd for a guest address, or NULL
 */
static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
{
	pmd_t *pmdp;

	BUG_ON(gmap_is_shadow(gmap));
	pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
914 915
	if (!pmdp)
		return NULL;
916

917 918 919 920 921 922
	/* without huge pages, there is no need to take the table lock */
	if (!gmap->mm->context.allow_gmap_hpage_1m)
		return pmd_none(*pmdp) ? NULL : pmdp;

	spin_lock(&gmap->guest_table_lock);
	if (pmd_none(*pmdp)) {
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
		spin_unlock(&gmap->guest_table_lock);
		return NULL;
	}

	/* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
	if (!pmd_large(*pmdp))
		spin_unlock(&gmap->guest_table_lock);
	return pmdp;
}

/**
 * gmap_pmd_op_end - release the guest_table_lock if needed
 * @gmap: pointer to the guest mapping meta data structure
 * @pmdp: pointer to the pmd
 */
static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
{
	if (pmd_large(*pmdp))
		spin_unlock(&gmap->guest_table_lock);
}

944 945 946 947 948 949 950 951 952 953 954
/*
 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
 * @pmdp: pointer to the pmd to be protected
 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 * @bits: notification bits to set
 *
 * Returns:
 * 0 if successfully protected
 * -EAGAIN if a fixup is needed
 * -EINVAL if unsupported notifier bits have been specified
 *
955
 * Expected to be called with sg->mm->mmap_lock in read and
956 957 958 959 960 961 962
 * guest_table_lock held.
 */
static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
			    pmd_t *pmdp, int prot, unsigned long bits)
{
	int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
	int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
963
	pmd_t new = *pmdp;
964 965 966 967 968

	/* Fixup needed */
	if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
		return -EAGAIN;

969 970 971 972 973 974 975 976 977 978 979
	if (prot == PROT_NONE && !pmd_i) {
		pmd_val(new) |= _SEGMENT_ENTRY_INVALID;
		gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
	}

	if (prot == PROT_READ && !pmd_p) {
		pmd_val(new) &= ~_SEGMENT_ENTRY_INVALID;
		pmd_val(new) |= _SEGMENT_ENTRY_PROTECT;
		gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
	}

980 981 982 983 984 985 986 987 988 989
	if (bits & GMAP_NOTIFY_MPROT)
		pmd_val(*pmdp) |= _SEGMENT_ENTRY_GMAP_IN;

	/* Shadow GMAP protection needs split PMDs */
	if (bits & GMAP_NOTIFY_SHADOW)
		return -EINVAL;

	return 0;
}

990 991 992 993 994 995
/*
 * gmap_protect_pte - remove access rights to memory and set pgste bits
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @pmdp: pointer to the pmd associated with the pte
 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
996
 * @bits: notification bits to set
997 998 999 1000
 *
 * Returns 0 if successfully protected, -ENOMEM if out of memory and
 * -EAGAIN if a fixup is needed.
 *
1001
 * Expected to be called with sg->mm->mmap_lock in read
1002 1003 1004 1005 1006 1007 1008
 */
static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
			    pmd_t *pmdp, int prot, unsigned long bits)
{
	int rc;
	pte_t *ptep;
	spinlock_t *ptl = NULL;
1009
	unsigned long pbits = 0;
1010 1011 1012 1013 1014 1015 1016 1017

	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
		return -EAGAIN;

	ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
	if (!ptep)
		return -ENOMEM;

1018 1019
	pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
	pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1020
	/* Protect and unlock. */
1021
	rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1022 1023
	gmap_pte_op_end(ptl);
	return rc;
1024 1025
}

1026 1027
/*
 * gmap_protect_range - remove access rights to memory and set pgste bits
1028 1029 1030
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @len: size of area
1031 1032 1033 1034 1035 1036
 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
 * @bits: pgste notification bits to set
 *
 * Returns 0 if successfully protected, -ENOMEM if out of memory and
 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
 *
1037
 * Called with sg->mm->mmap_lock in read.
1038
 */
1039 1040
static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
			      unsigned long len, int prot, unsigned long bits)
1041
{
1042
	unsigned long vmaddr, dist;
1043
	pmd_t *pmdp;
1044 1045
	int rc;

1046
	BUG_ON(gmap_is_shadow(gmap));
1047 1048
	while (len) {
		rc = -EAGAIN;
1049 1050
		pmdp = gmap_pmd_op_walk(gmap, gaddr);
		if (pmdp) {
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
			if (!pmd_large(*pmdp)) {
				rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
						      bits);
				if (!rc) {
					len -= PAGE_SIZE;
					gaddr += PAGE_SIZE;
				}
			} else {
				rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
						      bits);
				if (!rc) {
					dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
					len = len < dist ? 0 : len - dist;
					gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
				}
1066 1067
			}
			gmap_pmd_op_end(gmap, pmdp);
1068 1069
		}
		if (rc) {
1070 1071 1072 1073
			if (rc == -EINVAL)
				return rc;

			/* -EAGAIN, fixup of userspace mm and gmap */
1074 1075 1076
			vmaddr = __gmap_translate(gmap, gaddr);
			if (IS_ERR_VALUE(vmaddr))
				return vmaddr;
1077
			rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1078 1079 1080 1081 1082 1083
			if (rc)
				return rc;
		}
	}
	return 0;
}
1084

1085 1086 1087
/**
 * gmap_mprotect_notify - change access rights for a range of ptes and
 *                        call the notifier if any pte changes again
1088 1089 1090
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @len: size of area
1091
 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1092
 *
1093 1094 1095 1096 1097
 * Returns 0 if for each page in the given range a gmap mapping exists,
 * the new access rights could be set and the notifier could be armed.
 * If the gmap mapping is missing for one or more pages -EFAULT is
 * returned. If no memory could be allocated -ENOMEM is returned.
 * This function establishes missing page table entries.
1098
 */
1099 1100
int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
			 unsigned long len, int prot)
1101
{
1102
	int rc;
1103

1104
	if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1105
		return -EINVAL;
1106
	if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1107
		return -EINVAL;
1108
	mmap_read_lock(gmap->mm);
1109
	rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1110
	mmap_read_unlock(gmap->mm);
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_mprotect_notify);

/**
 * gmap_read_table - get an unsigned long value from a guest page table using
 *                   absolute addressing, without marking the page referenced.
 * @gmap: pointer to guest mapping meta data structure
 * @gaddr: virtual address in the guest address space
 * @val: pointer to the unsigned long value to return
 *
 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1123 1124
 * if reading using the virtual address failed. -EINVAL if called on a gmap
 * shadow.
1125
 *
1126
 * Called with gmap->mm->mmap_lock in read.
1127 1128 1129 1130 1131 1132 1133 1134
 */
int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
{
	unsigned long address, vmaddr;
	spinlock_t *ptl;
	pte_t *ptep, pte;
	int rc;

1135 1136 1137
	if (gmap_is_shadow(gmap))
		return -EINVAL;

1138
	while (1) {
1139 1140 1141
		rc = -EAGAIN;
		ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
		if (ptep) {
1142 1143 1144 1145 1146 1147 1148 1149 1150
			pte = *ptep;
			if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
				address = pte_val(pte) & PAGE_MASK;
				address += gaddr & ~PAGE_MASK;
				*val = *(unsigned long *) address;
				pte_val(*ptep) |= _PAGE_YOUNG;
				/* Do *NOT* clear the _PAGE_INVALID bit! */
				rc = 0;
			}
1151
			gmap_pte_op_end(ptl);
1152
		}
1153 1154 1155 1156 1157
		if (!rc)
			break;
		vmaddr = __gmap_translate(gmap, gaddr);
		if (IS_ERR_VALUE(vmaddr)) {
			rc = vmaddr;
1158 1159
			break;
		}
1160
		rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1161 1162 1163 1164 1165
		if (rc)
			break;
	}
	return rc;
}
1166
EXPORT_SYMBOL_GPL(gmap_read_table);
1167 1168

/**
1169 1170 1171 1172
 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
 * @sg: pointer to the shadow guest address space structure
 * @vmaddr: vm address associated with the rmap
 * @rmap: pointer to the rmap structure
1173
 *
1174
 * Called with the sg->guest_table_lock
1175
 */
1176 1177
static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
				    struct gmap_rmap *rmap)
1178
{
1179
	void __rcu **slot;
1180

1181 1182 1183 1184 1185
	BUG_ON(!gmap_is_shadow(sg));
	slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
	if (slot) {
		rmap->next = radix_tree_deref_slot_protected(slot,
							&sg->guest_table_lock);
1186
		radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1187 1188 1189 1190 1191 1192 1193 1194
	} else {
		rmap->next = NULL;
		radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
				  rmap);
	}
}

/**
1195
 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1196 1197 1198 1199 1200 1201 1202 1203 1204
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow gmap
 * @paddr: address in the parent guest address space
 * @len: length of the memory area to protect
 *
 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
 * if out of memory and -EFAULT if paddr is invalid.
 */
static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1205
			     unsigned long paddr, unsigned long len)
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
{
	struct gmap *parent;
	struct gmap_rmap *rmap;
	unsigned long vmaddr;
	spinlock_t *ptl;
	pte_t *ptep;
	int rc;

	BUG_ON(!gmap_is_shadow(sg));
	parent = sg->parent;
	while (len) {
		vmaddr = __gmap_translate(parent, paddr);
		if (IS_ERR_VALUE(vmaddr))
			return vmaddr;
1220
		rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1221 1222 1223
		if (!rmap)
			return -ENOMEM;
		rmap->raddr = raddr;
1224
		rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1225
		if (rc) {
1226 1227 1228 1229 1230 1231 1232
			kfree(rmap);
			return rc;
		}
		rc = -EAGAIN;
		ptep = gmap_pte_op_walk(parent, paddr, &ptl);
		if (ptep) {
			spin_lock(&sg->guest_table_lock);
1233
			rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1234 1235 1236 1237 1238 1239 1240 1241 1242
					     PGSTE_VSIE_BIT);
			if (!rc)
				gmap_insert_rmap(sg, vmaddr, rmap);
			spin_unlock(&sg->guest_table_lock);
			gmap_pte_op_end(ptl);
		}
		radix_tree_preload_end();
		if (rc) {
			kfree(rmap);
1243
			rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1244
			if (rc)
1245
				return rc;
1246 1247
			continue;
		}
1248
		paddr += PAGE_SIZE;
1249
		len -= PAGE_SIZE;
1250
	}
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
	return 0;
}

#define _SHADOW_RMAP_MASK	0x7
#define _SHADOW_RMAP_REGION1	0x5
#define _SHADOW_RMAP_REGION2	0x4
#define _SHADOW_RMAP_REGION3	0x3
#define _SHADOW_RMAP_SEGMENT	0x2
#define _SHADOW_RMAP_PGTABLE	0x1

/**
 * gmap_idte_one - invalidate a single region or segment table entry
 * @asce: region or segment table *origin* + table-type bits
 * @vaddr: virtual address to identify the table entry to flush
 *
 * The invalid bit of a single region or segment table entry is set
 * and the associated TLB entries depending on the entry are flushed.
 * The table-type of the @asce identifies the portion of the @vaddr
 * that is used as the invalidation index.
 */
static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
{
	asm volatile(
		"	.insn	rrf,0xb98e0000,%0,%1,0,0"
		: : "a" (asce), "a" (vaddr) : "cc", "memory");
}

/**
 * gmap_unshadow_page - remove a page from a shadow page table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 *
 * Called with the sg->guest_table_lock
 */
static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
{
	unsigned long *table;

	BUG_ON(!gmap_is_shadow(sg));
	table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
	if (!table || *table & _PAGE_INVALID)
		return;
1293
	gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
}

/**
 * __gmap_unshadow_pgt - remove all entries from a shadow page table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 * @pgt: pointer to the start of a shadow page table
 *
 * Called with the sg->guest_table_lock
 */
static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
				unsigned long *pgt)
{
	int i;

	BUG_ON(!gmap_is_shadow(sg));
1311
	for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
		pgt[i] = _PAGE_INVALID;
}

/**
 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
 * @sg: pointer to the shadow guest address space structure
 * @raddr: address in the shadow guest address space
 *
 * Called with the sg->guest_table_lock
 */
static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
{
	unsigned long sto, *ste, *pgt;
	struct page *page;

	BUG_ON(!gmap_is_shadow(sg));
	ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1329
	if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1330
		return;
1331 1332
	gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
	sto = (unsigned long) (ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
	pgt = (unsigned long *)(*ste & _SEGMENT_ENTRY_ORIGIN);
	*ste = _SEGMENT_ENTRY_EMPTY;
	__gmap_unshadow_pgt(sg, raddr, pgt);
	/* Free page table */
	page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
	list_del(&page->lru);
	page_table_free_pgste(page);
}

/**
 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 * @sgt: pointer to the start of a shadow segment table
 *
 * Called with the sg->guest_table_lock
 */
static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
				unsigned long *sgt)
{
Heiko Carstens's avatar
Heiko Carstens committed
1354
	unsigned long *pgt;
1355 1356 1357 1358
	struct page *page;
	int i;

	BUG_ON(!gmap_is_shadow(sg));
1359
	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1360
		if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
			continue;
		pgt = (unsigned long *)(sgt[i] & _REGION_ENTRY_ORIGIN);
		sgt[i] = _SEGMENT_ENTRY_EMPTY;
		__gmap_unshadow_pgt(sg, raddr, pgt);
		/* Free page table */
		page = pfn_to_page(__pa(pgt) >> PAGE_SHIFT);
		list_del(&page->lru);
		page_table_free_pgste(page);
	}
}

/**
 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 *
 * Called with the shadow->guest_table_lock
 */
static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
{
	unsigned long r3o, *r3e, *sgt;
	struct page *page;

	BUG_ON(!gmap_is_shadow(sg));
	r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1386
	if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1387
		return;
1388 1389
	gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
	r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1390 1391 1392 1393 1394 1395 1396
	gmap_idte_one(r3o | _ASCE_TYPE_REGION3, raddr);
	sgt = (unsigned long *)(*r3e & _REGION_ENTRY_ORIGIN);
	*r3e = _REGION3_ENTRY_EMPTY;
	__gmap_unshadow_sgt(sg, raddr, sgt);
	/* Free segment table */
	page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
	list_del(&page->lru);
1397
	__free_pages(page, CRST_ALLOC_ORDER);
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
}

/**
 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: address in the shadow guest address space
 * @r3t: pointer to the start of a shadow region-3 table
 *
 * Called with the sg->guest_table_lock
 */
static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
				unsigned long *r3t)
{
Heiko Carstens's avatar
Heiko Carstens committed
1411
	unsigned long *sgt;
1412 1413 1414 1415
	struct page *page;
	int i;

	BUG_ON(!gmap_is_shadow(sg));
1416
	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1417
		if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1418 1419 1420 1421 1422 1423 1424
			continue;
		sgt = (unsigned long *)(r3t[i] & _REGION_ENTRY_ORIGIN);
		r3t[i] = _REGION3_ENTRY_EMPTY;
		__gmap_unshadow_sgt(sg, raddr, sgt);
		/* Free segment table */
		page = pfn_to_page(__pa(sgt) >> PAGE_SHIFT);
		list_del(&page->lru);
1425
		__free_pages(page, CRST_ALLOC_ORDER);
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
	}
}

/**
 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 *
 * Called with the sg->guest_table_lock
 */
static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
{
	unsigned long r2o, *r2e, *r3t;
	struct page *page;

	BUG_ON(!gmap_is_shadow(sg));
	r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1443
	if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1444
		return;
1445 1446
	gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
	r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1447 1448 1449 1450 1451 1452 1453
	gmap_idte_one(r2o | _ASCE_TYPE_REGION2, raddr);
	r3t = (unsigned long *)(*r2e & _REGION_ENTRY_ORIGIN);
	*r2e = _REGION2_ENTRY_EMPTY;
	__gmap_unshadow_r3t(sg, raddr, r3t);
	/* Free region 3 table */
	page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
	list_del(&page->lru);
1454
	__free_pages(page, CRST_ALLOC_ORDER);
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
}

/**
 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 * @r2t: pointer to the start of a shadow region-2 table
 *
 * Called with the sg->guest_table_lock
 */
static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
				unsigned long *r2t)
{
Heiko Carstens's avatar
Heiko Carstens committed
1468
	unsigned long *r3t;
1469 1470 1471 1472
	struct page *page;
	int i;

	BUG_ON(!gmap_is_shadow(sg));
1473
	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1474
		if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1475 1476 1477 1478 1479 1480 1481
			continue;
		r3t = (unsigned long *)(r2t[i] & _REGION_ENTRY_ORIGIN);
		r2t[i] = _REGION2_ENTRY_EMPTY;
		__gmap_unshadow_r3t(sg, raddr, r3t);
		/* Free region 3 table */
		page = pfn_to_page(__pa(r3t) >> PAGE_SHIFT);
		list_del(&page->lru);
1482
		__free_pages(page, CRST_ALLOC_ORDER);
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499
	}
}

/**
 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 *
 * Called with the sg->guest_table_lock
 */
static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
{
	unsigned long r1o, *r1e, *r2t;
	struct page *page;

	BUG_ON(!gmap_is_shadow(sg));
	r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1500
	if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1501
		return;
1502 1503
	gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
	r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1504 1505 1506 1507 1508 1509 1510
	gmap_idte_one(r1o | _ASCE_TYPE_REGION1, raddr);
	r2t = (unsigned long *)(*r1e & _REGION_ENTRY_ORIGIN);
	*r1e = _REGION1_ENTRY_EMPTY;
	__gmap_unshadow_r2t(sg, raddr, r2t);
	/* Free region 2 table */
	page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
	list_del(&page->lru);
1511
	__free_pages(page, CRST_ALLOC_ORDER);
1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
}

/**
 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
 * @sg: pointer to the shadow guest address space structure
 * @raddr: rmap address in the shadow guest address space
 * @r1t: pointer to the start of a shadow region-1 table
 *
 * Called with the shadow->guest_table_lock
 */
static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
				unsigned long *r1t)
{
	unsigned long asce, *r2t;
	struct page *page;
	int i;

	BUG_ON(!gmap_is_shadow(sg));
	asce = (unsigned long) r1t | _ASCE_TYPE_REGION1;
1531
	for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1532
		if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1533 1534 1535 1536 1537 1538 1539 1540 1541
			continue;
		r2t = (unsigned long *)(r1t[i] & _REGION_ENTRY_ORIGIN);
		__gmap_unshadow_r2t(sg, raddr, r2t);
		/* Clear entry and flush translation r1t -> r2t */
		gmap_idte_one(asce, raddr);
		r1t[i] = _REGION1_ENTRY_EMPTY;
		/* Free region 2 table */
		page = pfn_to_page(__pa(r2t) >> PAGE_SHIFT);
		list_del(&page->lru);
1542
		__free_pages(page, CRST_ALLOC_ORDER);
1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
	}
}

/**
 * gmap_unshadow - remove a shadow page table completely
 * @sg: pointer to the shadow guest address space structure
 *
 * Called with sg->guest_table_lock
 */
static void gmap_unshadow(struct gmap *sg)
{
	unsigned long *table;

	BUG_ON(!gmap_is_shadow(sg));
	if (sg->removed)
		return;
	sg->removed = 1;
	gmap_call_notifier(sg, 0, -1UL);
1561
	gmap_flush_tlb(sg);
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
	table = (unsigned long *)(sg->asce & _ASCE_ORIGIN);
	switch (sg->asce & _ASCE_TYPE_MASK) {
	case _ASCE_TYPE_REGION1:
		__gmap_unshadow_r1t(sg, 0, table);
		break;
	case _ASCE_TYPE_REGION2:
		__gmap_unshadow_r2t(sg, 0, table);
		break;
	case _ASCE_TYPE_REGION3:
		__gmap_unshadow_r3t(sg, 0, table);
		break;
	case _ASCE_TYPE_SEGMENT:
		__gmap_unshadow_sgt(sg, 0, table);
		break;
	}
}

/**
 * gmap_find_shadow - find a specific asce in the list of shadow tables
 * @parent: pointer to the parent gmap
 * @asce: ASCE for which the shadow table is created
1583
 * @edat_level: edat level to be used for the shadow translation
1584 1585
 *
 * Returns the pointer to a gmap if a shadow table with the given asce is
1586 1587
 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
 * otherwise NULL
1588
 */
1589 1590
static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
				     int edat_level)
1591 1592 1593 1594
{
	struct gmap *sg;

	list_for_each_entry(sg, &parent->children, list) {
1595 1596
		if (sg->orig_asce != asce || sg->edat_level != edat_level ||
		    sg->removed)
1597
			continue;
1598 1599
		if (!sg->initialized)
			return ERR_PTR(-EAGAIN);
1600
		refcount_inc(&sg->ref_count);
1601 1602 1603 1604 1605
		return sg;
	}
	return NULL;
}

1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
/**
 * gmap_shadow_valid - check if a shadow guest address space matches the
 *                     given properties and is still valid
 * @sg: pointer to the shadow guest address space structure
 * @asce: ASCE for which the shadow table is requested
 * @edat_level: edat level to be used for the shadow translation
 *
 * Returns 1 if the gmap shadow is still valid and matches the given
 * properties, the caller can continue using it. Returns 0 otherwise, the
 * caller has to request a new shadow gmap in this case.
 *
 */
int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
{
	if (sg->removed)
		return 0;
	return sg->orig_asce == asce && sg->edat_level == edat_level;
}
EXPORT_SYMBOL_GPL(gmap_shadow_valid);

1626 1627 1628 1629
/**
 * gmap_shadow - create/find a shadow guest address space
 * @parent: pointer to the parent gmap
 * @asce: ASCE for which the shadow table is created
1630
 * @edat_level: edat level to be used for the shadow translation
1631 1632 1633 1634 1635 1636
 *
 * The pages of the top level page table referred by the asce parameter
 * will be set to read-only and marked in the PGSTEs of the kvm process.
 * The shadow table will be removed automatically on any change to the
 * PTE mapping for the source table.
 *
1637 1638 1639
 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
 * parent gmap table could not be protected.
1640
 */
1641 1642
struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
			 int edat_level)
1643 1644 1645 1646 1647
{
	struct gmap *sg, *new;
	unsigned long limit;
	int rc;

1648
	BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1649 1650
	BUG_ON(gmap_is_shadow(parent));
	spin_lock(&parent->shadow_lock);
1651
	sg = gmap_find_shadow(parent, asce, edat_level);
1652 1653 1654 1655 1656
	spin_unlock(&parent->shadow_lock);
	if (sg)
		return sg;
	/* Create a new shadow gmap */
	limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1657 1658
	if (asce & _ASCE_REAL_SPACE)
		limit = -1UL;
1659 1660
	new = gmap_alloc(limit);
	if (!new)
1661
		return ERR_PTR(-ENOMEM);
1662 1663 1664
	new->mm = parent->mm;
	new->parent = gmap_get(parent);
	new->orig_asce = asce;
1665
	new->edat_level = edat_level;
1666 1667 1668
	new->initialized = false;
	spin_lock(&parent->shadow_lock);
	/* Recheck if another CPU created the same shadow */
1669
	sg = gmap_find_shadow(parent, asce, edat_level);
1670 1671 1672 1673 1674
	if (sg) {
		spin_unlock(&parent->shadow_lock);
		gmap_free(new);
		return sg;
	}
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687
	if (asce & _ASCE_REAL_SPACE) {
		/* only allow one real-space gmap shadow */
		list_for_each_entry(sg, &parent->children, list) {
			if (sg->orig_asce & _ASCE_REAL_SPACE) {
				spin_lock(&sg->guest_table_lock);
				gmap_unshadow(sg);
				spin_unlock(&sg->guest_table_lock);
				list_del(&sg->list);
				gmap_put(sg);
				break;
			}
		}
	}
1688
	refcount_set(&new->ref_count, 2);
1689
	list_add(&new->list, &parent->children);
1690 1691 1692 1693 1694 1695
	if (asce & _ASCE_REAL_SPACE) {
		/* nothing to protect, return right away */
		new->initialized = true;
		spin_unlock(&parent->shadow_lock);
		return new;
	}
1696 1697
	spin_unlock(&parent->shadow_lock);
	/* protect after insertion, so it will get properly invalidated */
1698
	mmap_read_lock(parent->mm);
1699
	rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1700
				((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1701
				PROT_READ, GMAP_NOTIFY_SHADOW);
1702
	mmap_read_unlock(parent->mm);
1703 1704
	spin_lock(&parent->shadow_lock);
	new->initialized = true;
1705
	if (rc) {
1706
		list_del(&new->list);
1707
		gmap_free(new);
1708 1709 1710 1711
		new = ERR_PTR(rc);
	}
	spin_unlock(&parent->shadow_lock);
	return new;
1712 1713 1714 1715 1716 1717 1718 1719
}
EXPORT_SYMBOL_GPL(gmap_shadow);

/**
 * gmap_shadow_r2t - create an empty shadow region 2 table
 * @sg: pointer to the shadow guest address space structure
 * @saddr: faulting address in the shadow gmap
 * @r2t: parent gmap address of the region 2 table to get shadowed
1720
 * @fake: r2t references contiguous guest memory block, not a r2t
1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
 *
 * The r2t parameter specifies the address of the source table. The
 * four pages of the source table are made read-only in the parent gmap
 * address space. A write to the source table area @r2t will automatically
 * remove the shadow r2 table and all of its decendents.
 *
 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
 * shadow table structure is incomplete, -ENOMEM if out of memory and
 * -EFAULT if an address in the parent gmap could not be resolved.
 *
1731
 * Called with sg->mm->mmap_lock in read.
1732
 */
1733 1734
int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
		    int fake)
1735 1736 1737 1738 1739 1740 1741 1742
{
	unsigned long raddr, origin, offset, len;
	unsigned long *s_r2t, *table;
	struct page *page;
	int rc;

	BUG_ON(!gmap_is_shadow(sg));
	/* Allocate a shadow region second table */
1743
	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1744 1745 1746
	if (!page)
		return -ENOMEM;
	page->index = r2t & _REGION_ENTRY_ORIGIN;
1747 1748
	if (fake)
		page->index |= GMAP_SHADOW_FAKE_TABLE;
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759
	s_r2t = (unsigned long *) page_to_phys(page);
	/* Install shadow region second table */
	spin_lock(&sg->guest_table_lock);
	table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
	if (!table) {
		rc = -EAGAIN;		/* Race with unshadow */
		goto out_free;
	}
	if (!(*table & _REGION_ENTRY_INVALID)) {
		rc = 0;			/* Already established */
		goto out_free;
1760 1761 1762
	} else if (*table & _REGION_ENTRY_ORIGIN) {
		rc = -EAGAIN;		/* Race with shadow */
		goto out_free;
1763 1764
	}
	crst_table_init(s_r2t, _REGION2_ENTRY_EMPTY);
1765 1766 1767
	/* mark as invalid as long as the parent table is not protected */
	*table = (unsigned long) s_r2t | _REGION_ENTRY_LENGTH |
		 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1768 1769
	if (sg->edat_level >= 1)
		*table |= (r2t & _REGION_ENTRY_PROTECT);
1770
	list_add(&page->lru, &sg->crst_list);
1771 1772 1773 1774 1775 1776
	if (fake) {
		/* nothing to protect for fake tables */
		*table &= ~_REGION_ENTRY_INVALID;
		spin_unlock(&sg->guest_table_lock);
		return 0;
	}
1777 1778
	spin_unlock(&sg->guest_table_lock);
	/* Make r2t read-only in parent gmap page table */
1779
	raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1780
	origin = r2t & _REGION_ENTRY_ORIGIN;
1781 1782
	offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
	len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1783
	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1784 1785 1786 1787 1788 1789 1790 1791 1792
	spin_lock(&sg->guest_table_lock);
	if (!rc) {
		table = gmap_table_walk(sg, saddr, 4);
		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
			      (unsigned long) s_r2t)
			rc = -EAGAIN;		/* Race with unshadow */
		else
			*table &= ~_REGION_ENTRY_INVALID;
	} else {
1793 1794
		gmap_unshadow_r2t(sg, raddr);
	}
1795
	spin_unlock(&sg->guest_table_lock);
1796 1797 1798
	return rc;
out_free:
	spin_unlock(&sg->guest_table_lock);
1799
	__free_pages(page, CRST_ALLOC_ORDER);
1800 1801
	return rc;
}
1802 1803 1804 1805 1806 1807 1808
EXPORT_SYMBOL_GPL(gmap_shadow_r2t);

/**
 * gmap_shadow_r3t - create a shadow region 3 table
 * @sg: pointer to the shadow guest address space structure
 * @saddr: faulting address in the shadow gmap
 * @r3t: parent gmap address of the region 3 table to get shadowed
1809
 * @fake: r3t references contiguous guest memory block, not a r3t
1810 1811 1812 1813 1814
 *
 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
 * shadow table structure is incomplete, -ENOMEM if out of memory and
 * -EFAULT if an address in the parent gmap could not be resolved.
 *
1815
 * Called with sg->mm->mmap_lock in read.
1816
 */
1817 1818
int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
		    int fake)
1819 1820 1821 1822 1823 1824 1825 1826
{
	unsigned long raddr, origin, offset, len;
	unsigned long *s_r3t, *table;
	struct page *page;
	int rc;

	BUG_ON(!gmap_is_shadow(sg));
	/* Allocate a shadow region second table */
1827
	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1828 1829 1830
	if (!page)
		return -ENOMEM;
	page->index = r3t & _REGION_ENTRY_ORIGIN;
1831 1832
	if (fake)
		page->index |= GMAP_SHADOW_FAKE_TABLE;
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843
	s_r3t = (unsigned long *) page_to_phys(page);
	/* Install shadow region second table */
	spin_lock(&sg->guest_table_lock);
	table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
	if (!table) {
		rc = -EAGAIN;		/* Race with unshadow */
		goto out_free;
	}
	if (!(*table & _REGION_ENTRY_INVALID)) {
		rc = 0;			/* Already established */
		goto out_free;
1844 1845
	} else if (*table & _REGION_ENTRY_ORIGIN) {
		rc = -EAGAIN;		/* Race with shadow */
1846
		goto out_free;
1847 1848
	}
	crst_table_init(s_r3t, _REGION3_ENTRY_EMPTY);
1849 1850 1851
	/* mark as invalid as long as the parent table is not protected */
	*table = (unsigned long) s_r3t | _REGION_ENTRY_LENGTH |
		 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1852 1853
	if (sg->edat_level >= 1)
		*table |= (r3t & _REGION_ENTRY_PROTECT);
1854
	list_add(&page->lru, &sg->crst_list);
1855 1856 1857 1858 1859 1860
	if (fake) {
		/* nothing to protect for fake tables */
		*table &= ~_REGION_ENTRY_INVALID;
		spin_unlock(&sg->guest_table_lock);
		return 0;
	}
1861 1862
	spin_unlock(&sg->guest_table_lock);
	/* Make r3t read-only in parent gmap page table */
1863
	raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1864
	origin = r3t & _REGION_ENTRY_ORIGIN;
1865 1866
	offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
	len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1867
	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1868 1869 1870 1871 1872 1873 1874 1875 1876
	spin_lock(&sg->guest_table_lock);
	if (!rc) {
		table = gmap_table_walk(sg, saddr, 3);
		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
			      (unsigned long) s_r3t)
			rc = -EAGAIN;		/* Race with unshadow */
		else
			*table &= ~_REGION_ENTRY_INVALID;
	} else {
1877 1878
		gmap_unshadow_r3t(sg, raddr);
	}
1879
	spin_unlock(&sg->guest_table_lock);
1880 1881 1882
	return rc;
out_free:
	spin_unlock(&sg->guest_table_lock);
1883
	__free_pages(page, CRST_ALLOC_ORDER);
1884 1885 1886 1887 1888 1889 1890 1891 1892
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_shadow_r3t);

/**
 * gmap_shadow_sgt - create a shadow segment table
 * @sg: pointer to the shadow guest address space structure
 * @saddr: faulting address in the shadow gmap
 * @sgt: parent gmap address of the segment table to get shadowed
1893
 * @fake: sgt references contiguous guest memory block, not a sgt
1894 1895 1896 1897 1898
 *
 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
 * shadow table structure is incomplete, -ENOMEM if out of memory and
 * -EFAULT if an address in the parent gmap could not be resolved.
 *
1899
 * Called with sg->mm->mmap_lock in read.
1900
 */
1901 1902
int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
		    int fake)
1903 1904 1905 1906 1907 1908
{
	unsigned long raddr, origin, offset, len;
	unsigned long *s_sgt, *table;
	struct page *page;
	int rc;

1909
	BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1910
	/* Allocate a shadow segment table */
1911
	page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
1912 1913 1914
	if (!page)
		return -ENOMEM;
	page->index = sgt & _REGION_ENTRY_ORIGIN;
1915 1916
	if (fake)
		page->index |= GMAP_SHADOW_FAKE_TABLE;
1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
	s_sgt = (unsigned long *) page_to_phys(page);
	/* Install shadow region second table */
	spin_lock(&sg->guest_table_lock);
	table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
	if (!table) {
		rc = -EAGAIN;		/* Race with unshadow */
		goto out_free;
	}
	if (!(*table & _REGION_ENTRY_INVALID)) {
		rc = 0;			/* Already established */
		goto out_free;
1928 1929 1930
	} else if (*table & _REGION_ENTRY_ORIGIN) {
		rc = -EAGAIN;		/* Race with shadow */
		goto out_free;
1931 1932
	}
	crst_table_init(s_sgt, _SEGMENT_ENTRY_EMPTY);
1933 1934 1935
	/* mark as invalid as long as the parent table is not protected */
	*table = (unsigned long) s_sgt | _REGION_ENTRY_LENGTH |
		 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1936 1937
	if (sg->edat_level >= 1)
		*table |= sgt & _REGION_ENTRY_PROTECT;
1938
	list_add(&page->lru, &sg->crst_list);
1939 1940 1941 1942 1943 1944
	if (fake) {
		/* nothing to protect for fake tables */
		*table &= ~_REGION_ENTRY_INVALID;
		spin_unlock(&sg->guest_table_lock);
		return 0;
	}
1945 1946
	spin_unlock(&sg->guest_table_lock);
	/* Make sgt read-only in parent gmap page table */
1947
	raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1948
	origin = sgt & _REGION_ENTRY_ORIGIN;
1949 1950
	offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
	len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1951
	rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1952 1953 1954 1955 1956 1957 1958 1959 1960
	spin_lock(&sg->guest_table_lock);
	if (!rc) {
		table = gmap_table_walk(sg, saddr, 2);
		if (!table || (*table & _REGION_ENTRY_ORIGIN) !=
			      (unsigned long) s_sgt)
			rc = -EAGAIN;		/* Race with unshadow */
		else
			*table &= ~_REGION_ENTRY_INVALID;
	} else {
1961 1962
		gmap_unshadow_sgt(sg, raddr);
	}
1963
	spin_unlock(&sg->guest_table_lock);
1964 1965 1966
	return rc;
out_free:
	spin_unlock(&sg->guest_table_lock);
1967
	__free_pages(page, CRST_ALLOC_ORDER);
1968 1969 1970 1971 1972
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_shadow_sgt);

/**
1973
 * gmap_shadow_pgt_lookup - find a shadow page table
1974 1975 1976 1977
 * @sg: pointer to the shadow guest address space structure
 * @saddr: the address in the shadow aguest address space
 * @pgt: parent gmap address of the page table to get shadowed
 * @dat_protection: if the pgtable is marked as protected by dat
1978
 * @fake: pgt references contiguous guest memory block, not a pgtable
1979 1980 1981 1982
 *
 * Returns 0 if the shadow page table was found and -EAGAIN if the page
 * table was not found.
 *
1983
 * Called with sg->mm->mmap_lock in read.
1984 1985
 */
int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
1986 1987
			   unsigned long *pgt, int *dat_protection,
			   int *fake)
1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998
{
	unsigned long *table;
	struct page *page;
	int rc;

	BUG_ON(!gmap_is_shadow(sg));
	spin_lock(&sg->guest_table_lock);
	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
	if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
		/* Shadow page tables are full pages (pte+pgste) */
		page = pfn_to_page(*table >> PAGE_SHIFT);
1999
		*pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2000
		*dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2001
		*fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
		rc = 0;
	} else  {
		rc = -EAGAIN;
	}
	spin_unlock(&sg->guest_table_lock);
	return rc;

}
EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);

/**
 * gmap_shadow_pgt - instantiate a shadow page table
 * @sg: pointer to the shadow guest address space structure
 * @saddr: faulting address in the shadow gmap
 * @pgt: parent gmap address of the page table to get shadowed
2017
 * @fake: pgt references contiguous guest memory block, not a pgtable
2018 2019 2020 2021 2022
 *
 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
 * shadow table structure is incomplete, -ENOMEM if out of memory,
 * -EFAULT if an address in the parent gmap could not be resolved and
 *
2023
 * Called with gmap->mm->mmap_lock in read
2024
 */
2025 2026
int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
		    int fake)
2027 2028 2029 2030 2031 2032
{
	unsigned long raddr, origin;
	unsigned long *s_pgt, *table;
	struct page *page;
	int rc;

2033
	BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2034 2035 2036 2037 2038
	/* Allocate a shadow page table */
	page = page_table_alloc_pgste(sg->mm);
	if (!page)
		return -ENOMEM;
	page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
2039 2040
	if (fake)
		page->index |= GMAP_SHADOW_FAKE_TABLE;
2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051
	s_pgt = (unsigned long *) page_to_phys(page);
	/* Install shadow page table */
	spin_lock(&sg->guest_table_lock);
	table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
	if (!table) {
		rc = -EAGAIN;		/* Race with unshadow */
		goto out_free;
	}
	if (!(*table & _SEGMENT_ENTRY_INVALID)) {
		rc = 0;			/* Already established */
		goto out_free;
2052 2053 2054
	} else if (*table & _SEGMENT_ENTRY_ORIGIN) {
		rc = -EAGAIN;		/* Race with shadow */
		goto out_free;
2055
	}
2056
	/* mark as invalid as long as the parent table is not protected */
2057
	*table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2058
		 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2059
	list_add(&page->lru, &sg->pt_list);
2060 2061 2062 2063 2064 2065
	if (fake) {
		/* nothing to protect for fake tables */
		*table &= ~_SEGMENT_ENTRY_INVALID;
		spin_unlock(&sg->guest_table_lock);
		return 0;
	}
2066 2067
	spin_unlock(&sg->guest_table_lock);
	/* Make pgt read-only in parent gmap page table (not the pgste) */
2068
	raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2069
	origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2070
	rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2071 2072 2073 2074 2075 2076 2077 2078 2079
	spin_lock(&sg->guest_table_lock);
	if (!rc) {
		table = gmap_table_walk(sg, saddr, 1);
		if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) !=
			      (unsigned long) s_pgt)
			rc = -EAGAIN;		/* Race with unshadow */
		else
			*table &= ~_SEGMENT_ENTRY_INVALID;
	} else {
2080 2081
		gmap_unshadow_pgt(sg, raddr);
	}
2082
	spin_unlock(&sg->guest_table_lock);
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095
	return rc;
out_free:
	spin_unlock(&sg->guest_table_lock);
	page_table_free_pgste(page);
	return rc;

}
EXPORT_SYMBOL_GPL(gmap_shadow_pgt);

/**
 * gmap_shadow_page - create a shadow page mapping
 * @sg: pointer to the shadow guest address space structure
 * @saddr: faulting address in the shadow gmap
2096
 * @pte: pte in parent gmap address space to get shadowed
2097 2098 2099 2100 2101
 *
 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
 * shadow table structure is incomplete, -ENOMEM if out of memory and
 * -EFAULT if an address in the parent gmap could not be resolved.
 *
2102
 * Called with sg->mm->mmap_lock in read.
2103
 */
2104
int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2105 2106 2107
{
	struct gmap *parent;
	struct gmap_rmap *rmap;
2108
	unsigned long vmaddr, paddr;
2109 2110
	spinlock_t *ptl;
	pte_t *sptep, *tptep;
2111
	int prot;
2112 2113 2114 2115
	int rc;

	BUG_ON(!gmap_is_shadow(sg));
	parent = sg->parent;
2116
	prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2117

2118
	rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2119 2120 2121 2122 2123
	if (!rmap)
		return -ENOMEM;
	rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;

	while (1) {
2124
		paddr = pte_val(pte) & PAGE_MASK;
2125 2126 2127 2128 2129
		vmaddr = __gmap_translate(parent, paddr);
		if (IS_ERR_VALUE(vmaddr)) {
			rc = vmaddr;
			break;
		}
2130
		rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144
		if (rc)
			break;
		rc = -EAGAIN;
		sptep = gmap_pte_op_walk(parent, paddr, &ptl);
		if (sptep) {
			spin_lock(&sg->guest_table_lock);
			/* Get page table pointer */
			tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
			if (!tptep) {
				spin_unlock(&sg->guest_table_lock);
				gmap_pte_op_end(ptl);
				radix_tree_preload_end();
				break;
			}
2145
			rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
			if (rc > 0) {
				/* Success and a new mapping */
				gmap_insert_rmap(sg, vmaddr, rmap);
				rmap = NULL;
				rc = 0;
			}
			gmap_pte_op_end(ptl);
			spin_unlock(&sg->guest_table_lock);
		}
		radix_tree_preload_end();
		if (!rc)
			break;
2158
		rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2159 2160 2161 2162 2163 2164 2165 2166
		if (rc)
			break;
	}
	kfree(rmap);
	return rc;
}
EXPORT_SYMBOL_GPL(gmap_shadow_page);

2167
/*
2168 2169 2170 2171 2172
 * gmap_shadow_notify - handle notifications for shadow gmap
 *
 * Called with sg->parent->shadow_lock.
 */
static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2173
			       unsigned long gaddr)
2174 2175
{
	struct gmap_rmap *rmap, *rnext, *head;
2176
	unsigned long start, end, bits, raddr;
2177 2178 2179 2180 2181 2182 2183 2184 2185 2186

	BUG_ON(!gmap_is_shadow(sg));

	spin_lock(&sg->guest_table_lock);
	if (sg->removed) {
		spin_unlock(&sg->guest_table_lock);
		return;
	}
	/* Check for top level table */
	start = sg->orig_asce & _ASCE_ORIGIN;
2187
	end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2188 2189
	if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
	    gaddr < end) {
2190 2191 2192 2193 2194 2195 2196 2197
		/* The complete shadow table has to go */
		gmap_unshadow(sg);
		spin_unlock(&sg->guest_table_lock);
		list_del(&sg->list);
		gmap_put(sg);
		return;
	}
	/* Remove the page table tree from on specific entry */
2198
	head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222
	gmap_for_each_rmap_safe(rmap, rnext, head) {
		bits = rmap->raddr & _SHADOW_RMAP_MASK;
		raddr = rmap->raddr ^ bits;
		switch (bits) {
		case _SHADOW_RMAP_REGION1:
			gmap_unshadow_r2t(sg, raddr);
			break;
		case _SHADOW_RMAP_REGION2:
			gmap_unshadow_r3t(sg, raddr);
			break;
		case _SHADOW_RMAP_REGION3:
			gmap_unshadow_sgt(sg, raddr);
			break;
		case _SHADOW_RMAP_SEGMENT:
			gmap_unshadow_pgt(sg, raddr);
			break;
		case _SHADOW_RMAP_PGTABLE:
			gmap_unshadow_page(sg, raddr);
			break;
		}
		kfree(rmap);
	}
	spin_unlock(&sg->guest_table_lock);
}
2223 2224 2225 2226

/**
 * ptep_notify - call all invalidation callbacks for a specific pte.
 * @mm: pointer to the process mm_struct
2227
 * @vmaddr: virtual address in the process address space
2228
 * @pte: pointer to the page table entry
2229
 * @bits: bits from the pgste that caused the notify call
2230 2231 2232 2233
 *
 * This function is assumed to be called with the page table lock held
 * for the pte to notify.
 */
2234 2235
void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
		 pte_t *pte, unsigned long bits)
2236
{
2237
	unsigned long offset, gaddr = 0;
2238
	unsigned long *table;
2239
	struct gmap *gmap, *sg, *next;
2240 2241

	offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2242
	offset = offset * (PAGE_SIZE / sizeof(pte_t));
2243 2244 2245
	rcu_read_lock();
	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
		spin_lock(&gmap->guest_table_lock);
2246 2247
		table = radix_tree_lookup(&gmap->host_to_guest,
					  vmaddr >> PMD_SHIFT);
2248 2249 2250
		if (table)
			gaddr = __gmap_segment_gaddr(table) + offset;
		spin_unlock(&gmap->guest_table_lock);
2251 2252 2253 2254 2255 2256 2257
		if (!table)
			continue;

		if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
			spin_lock(&gmap->shadow_lock);
			list_for_each_entry_safe(sg, next,
						 &gmap->children, list)
2258
				gmap_shadow_notify(sg, vmaddr, gaddr);
2259 2260 2261
			spin_unlock(&gmap->shadow_lock);
		}
		if (bits & PGSTE_IN_BIT)
2262
			gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2263
	}
2264
	rcu_read_unlock();
2265 2266 2267
}
EXPORT_SYMBOL_GPL(ptep_notify);

2268 2269 2270 2271 2272 2273 2274
static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
			     unsigned long gaddr)
{
	pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_IN;
	gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
}

2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300
/**
 * gmap_pmdp_xchg - exchange a gmap pmd with another
 * @gmap: pointer to the guest address space structure
 * @pmdp: pointer to the pmd entry
 * @new: replacement entry
 * @gaddr: the affected guest address
 *
 * This function is assumed to be called with the guest_table_lock
 * held.
 */
static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
			   unsigned long gaddr)
{
	gaddr &= HPAGE_MASK;
	pmdp_notify_gmap(gmap, pmdp, gaddr);
	pmd_val(new) &= ~_SEGMENT_ENTRY_GMAP_IN;
	if (MACHINE_HAS_TLB_GUEST)
		__pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
			    IDTE_GLOBAL);
	else if (MACHINE_HAS_IDTE)
		__pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
	else
		__pmdp_csp(pmdp);
	*pmdp = new;
}

2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
			    int purge)
{
	pmd_t *pmdp;
	struct gmap *gmap;
	unsigned long gaddr;

	rcu_read_lock();
	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
		spin_lock(&gmap->guest_table_lock);
		pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
						  vmaddr >> PMD_SHIFT);
		if (pmdp) {
			gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
			pmdp_notify_gmap(gmap, pmdp, gaddr);
2316 2317
			WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
						   _SEGMENT_ENTRY_GMAP_UC));
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
			if (purge)
				__pmdp_csp(pmdp);
			pmd_val(*pmdp) = _SEGMENT_ENTRY_EMPTY;
		}
		spin_unlock(&gmap->guest_table_lock);
	}
	rcu_read_unlock();
}

/**
 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
 *                        flushing
 * @mm: pointer to the process mm_struct
 * @vmaddr: virtual address in the process address space
 */
void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
{
	gmap_pmdp_clear(mm, vmaddr, 0);
}
EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);

/**
 * gmap_pmdp_csp - csp all affected guest pmd entries
 * @mm: pointer to the process mm_struct
 * @vmaddr: virtual address in the process address space
 */
void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
{
	gmap_pmdp_clear(mm, vmaddr, 1);
}
EXPORT_SYMBOL_GPL(gmap_pmdp_csp);

/**
 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
 * @mm: pointer to the process mm_struct
 * @vmaddr: virtual address in the process address space
 */
void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
{
	unsigned long *entry, gaddr;
	struct gmap *gmap;
	pmd_t *pmdp;

	rcu_read_lock();
	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
		spin_lock(&gmap->guest_table_lock);
		entry = radix_tree_delete(&gmap->host_to_guest,
					  vmaddr >> PMD_SHIFT);
		if (entry) {
			pmdp = (pmd_t *)entry;
			gaddr = __gmap_segment_gaddr(entry);
			pmdp_notify_gmap(gmap, pmdp, gaddr);
2370 2371
			WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
					   _SEGMENT_ENTRY_GMAP_UC));
2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404
			if (MACHINE_HAS_TLB_GUEST)
				__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
					    gmap->asce, IDTE_LOCAL);
			else if (MACHINE_HAS_IDTE)
				__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
			*entry = _SEGMENT_ENTRY_EMPTY;
		}
		spin_unlock(&gmap->guest_table_lock);
	}
	rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);

/**
 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
 * @mm: pointer to the process mm_struct
 * @vmaddr: virtual address in the process address space
 */
void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
{
	unsigned long *entry, gaddr;
	struct gmap *gmap;
	pmd_t *pmdp;

	rcu_read_lock();
	list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
		spin_lock(&gmap->guest_table_lock);
		entry = radix_tree_delete(&gmap->host_to_guest,
					  vmaddr >> PMD_SHIFT);
		if (entry) {
			pmdp = (pmd_t *)entry;
			gaddr = __gmap_segment_gaddr(entry);
			pmdp_notify_gmap(gmap, pmdp, gaddr);
2405 2406
			WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
					   _SEGMENT_ENTRY_GMAP_UC));
2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421
			if (MACHINE_HAS_TLB_GUEST)
				__pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
					    gmap->asce, IDTE_GLOBAL);
			else if (MACHINE_HAS_IDTE)
				__pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
			else
				__pmdp_csp(pmdp);
			*entry = _SEGMENT_ENTRY_EMPTY;
		}
		spin_unlock(&gmap->guest_table_lock);
	}
	rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);

2422 2423 2424 2425 2426 2427 2428 2429 2430
/**
 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
 * @gmap: pointer to guest address space
 * @pmdp: pointer to the pmd to be tested
 * @gaddr: virtual address in the guest address space
 *
 * This function is assumed to be called with the guest_table_lock
 * held.
 */
2431 2432
static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
					  unsigned long gaddr)
2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486
{
	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
		return false;

	/* Already protected memory, which did not change is clean */
	if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
	    !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
		return false;

	/* Clear UC indication and reset protection */
	pmd_val(*pmdp) &= ~_SEGMENT_ENTRY_GMAP_UC;
	gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
	return true;
}

/**
 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
 * @gmap: pointer to guest address space
 * @bitmap: dirty bitmap for this pmd
 * @gaddr: virtual address in the guest address space
 * @vmaddr: virtual address in the host address space
 *
 * This function is assumed to be called with the guest_table_lock
 * held.
 */
void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
			     unsigned long gaddr, unsigned long vmaddr)
{
	int i;
	pmd_t *pmdp;
	pte_t *ptep;
	spinlock_t *ptl;

	pmdp = gmap_pmd_op_walk(gmap, gaddr);
	if (!pmdp)
		return;

	if (pmd_large(*pmdp)) {
		if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
			bitmap_fill(bitmap, _PAGE_ENTRIES);
	} else {
		for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
			ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
			if (!ptep)
				continue;
			if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
				set_bit(i, bitmap);
			spin_unlock(ptl);
		}
	}
	gmap_pmd_op_end(gmap, pmdp);
}
EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);

2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500
#ifdef CONFIG_TRANSPARENT_HUGEPAGE
static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
				    unsigned long end, struct mm_walk *walk)
{
	struct vm_area_struct *vma = walk->vma;

	split_huge_pmd(vma, pmd, addr);
	return 0;
}

static const struct mm_walk_ops thp_split_walk_ops = {
	.pmd_entry	= thp_split_walk_pmd_entry,
};

2501 2502 2503 2504 2505 2506 2507
static inline void thp_split_mm(struct mm_struct *mm)
{
	struct vm_area_struct *vma;

	for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
		vma->vm_flags &= ~VM_HUGEPAGE;
		vma->vm_flags |= VM_NOHUGEPAGE;
2508
		walk_page_vma(vma, &thp_split_walk_ops, NULL);
2509 2510 2511
	}
	mm->def_flags |= VM_NOHUGEPAGE;
}
2512 2513 2514 2515 2516
#else
static inline void thp_split_mm(struct mm_struct *mm)
{
}
#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2517

2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540
/*
 * Remove all empty zero pages from the mapping for lazy refaulting
 * - This must be called after mm->context.has_pgste is set, to avoid
 *   future creation of zero pages
 * - This must be called after THP was enabled
 */
static int __zap_zero_pages(pmd_t *pmd, unsigned long start,
			   unsigned long end, struct mm_walk *walk)
{
	unsigned long addr;

	for (addr = start; addr != end; addr += PAGE_SIZE) {
		pte_t *ptep;
		spinlock_t *ptl;

		ptep = pte_offset_map_lock(walk->mm, pmd, addr, &ptl);
		if (is_zero_pfn(pte_pfn(*ptep)))
			ptep_xchg_direct(walk->mm, addr, ptep, __pte(_PAGE_INVALID));
		pte_unmap_unlock(ptep, ptl);
	}
	return 0;
}

2541 2542 2543
static const struct mm_walk_ops zap_zero_walk_ops = {
	.pmd_entry	= __zap_zero_pages,
};
2544

2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557
/*
 * switch on pgstes for its userspace process (for kvm)
 */
int s390_enable_sie(void)
{
	struct mm_struct *mm = current->mm;

	/* Do we have pgstes? if yes, we are done */
	if (mm_has_pgste(mm))
		return 0;
	/* Fail if the page tables are 2K */
	if (!mm_alloc_pgste(mm))
		return -EINVAL;
2558
	mmap_write_lock(mm);
2559 2560 2561
	mm->context.has_pgste = 1;
	/* split thp mappings and disable thp for future mappings */
	thp_split_mm(mm);
2562
	walk_page_range(mm, 0, TASK_SIZE, &zap_zero_walk_ops, NULL);
2563
	mmap_write_unlock(mm);
2564 2565 2566 2567
	return 0;
}
EXPORT_SYMBOL_GPL(s390_enable_sie);

2568 2569 2570 2571
int gmap_mark_unmergeable(void)
{
	struct mm_struct *mm = current->mm;
	struct vm_area_struct *vma;
2572
	int ret;
2573 2574

	for (vma = mm->mmap; vma; vma = vma->vm_next) {
2575 2576 2577 2578
		ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
				  MADV_UNMERGEABLE, &vma->vm_flags);
		if (ret)
			return ret;
2579 2580 2581 2582 2583 2584
	}
	mm->def_flags &= ~VM_MERGEABLE;
	return 0;
}
EXPORT_SYMBOL_GPL(gmap_mark_unmergeable);

2585 2586 2587 2588
/*
 * Enable storage key handling from now on and initialize the storage
 * keys with the default key.
 */
2589 2590
static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
				  unsigned long next, struct mm_walk *walk)
2591 2592 2593 2594 2595 2596
{
	/* Clear storage key */
	ptep_zap_key(walk->mm, addr, pte);
	return 0;
}

2597 2598 2599 2600 2601 2602
static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
				      unsigned long hmask, unsigned long next,
				      struct mm_walk *walk)
{
	pmd_t *pmd = (pmd_t *)pte;
	unsigned long start, end;
2603
	struct page *page = pmd_page(*pmd);
2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617

	/*
	 * The write check makes sure we do not set a key on shared
	 * memory. This is needed as the walker does not differentiate
	 * between actual guest memory and the process executable or
	 * shared libraries.
	 */
	if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
	    !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
		return 0;

	start = pmd_val(*pmd) & HPAGE_MASK;
	end = start + HPAGE_SIZE - 1;
	__storage_key_init_range(start, end);
2618
	set_bit(PG_arch_1, &page->flags);
2619 2620 2621
	return 0;
}

2622 2623 2624 2625 2626
static const struct mm_walk_ops enable_skey_walk_ops = {
	.hugetlb_entry		= __s390_enable_skey_hugetlb,
	.pte_entry		= __s390_enable_skey_pte,
};

2627 2628 2629 2630 2631
int s390_enable_skey(void)
{
	struct mm_struct *mm = current->mm;
	int rc = 0;

2632
	mmap_write_lock(mm);
2633
	if (mm_uses_skeys(mm))
2634 2635
		goto out_up;

2636
	mm->context.uses_skeys = 1;
2637 2638 2639 2640
	rc = gmap_mark_unmergeable();
	if (rc) {
		mm->context.uses_skeys = 0;
		goto out_up;
2641
	}
2642
	walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2643 2644

out_up:
2645
	mmap_write_unlock(mm);
2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659
	return rc;
}
EXPORT_SYMBOL_GPL(s390_enable_skey);

/*
 * Reset CMMA state, make all pages stable again.
 */
static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
			     unsigned long next, struct mm_walk *walk)
{
	ptep_zap_unused(walk->mm, addr, pte, 1);
	return 0;
}

2660 2661 2662 2663
static const struct mm_walk_ops reset_cmma_walk_ops = {
	.pte_entry		= __s390_reset_cmma,
};

2664 2665
void s390_reset_cmma(struct mm_struct *mm)
{
2666
	mmap_write_lock(mm);
2667
	walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2668
	mmap_write_unlock(mm);
2669 2670
}
EXPORT_SYMBOL_GPL(s390_reset_cmma);
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680

/*
 * make inaccessible pages accessible again
 */
static int __s390_reset_acc(pte_t *ptep, unsigned long addr,
			    unsigned long next, struct mm_walk *walk)
{
	pte_t pte = READ_ONCE(*ptep);

	if (pte_present(pte))
2681
		WARN_ON_ONCE(uv_destroy_page(pte_val(pte) & PAGE_MASK));
2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
	return 0;
}

static const struct mm_walk_ops reset_acc_walk_ops = {
	.pte_entry		= __s390_reset_acc,
};

#include <linux/sched/mm.h>
void s390_reset_acc(struct mm_struct *mm)
{
2692 2693
	if (!mm_is_protected(mm))
		return;
2694 2695 2696 2697 2698 2699 2700 2701
	/*
	 * we might be called during
	 * reset:                             we walk the pages and clear
	 * close of all kvm file descriptors: we walk the pages and clear
	 * exit of process on fd closure:     vma already gone, do nothing
	 */
	if (!mmget_not_zero(mm))
		return;
2702
	mmap_read_lock(mm);
2703
	walk_page_range(mm, 0, TASK_SIZE, &reset_acc_walk_ops, NULL);
2704
	mmap_read_unlock(mm);
2705 2706 2707
	mmput(mm);
}
EXPORT_SYMBOL_GPL(s390_reset_acc);