1 // SPDX-License-Identifier: GPL-2.0
4 * MMU-generic set_memory implementation for powerpc
6 * Copyright 2019-2021, IBM Corporation.
10 #include <linux/vmalloc.h>
11 #include <linux/set_memory.h>
15 #include <asm/pgtable.h>
18 static pte_basic_t pte_update_delta(pte_t *ptep, unsigned long addr,
19 unsigned long old, unsigned long new)
21 return pte_update(&init_mm, addr, ptep, old & ~new, new & ~old, 0);
25 * Updates the attributes of a page atomically.
27 * This sequence is safe against concurrent updates, and also allows updating the
28 * attributes of a page currently being executed or accessed.
30 static int change_page_attr(pte_t *ptep, unsigned long addr, void *data)
32 long action = (long)data;
34 /* modify the PTE bits as desired */
37 /* Don't clear DIRTY bit */
38 pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_RO);
41 pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_RW);
44 pte_update_delta(ptep, addr, _PAGE_KERNEL_ROX, _PAGE_KERNEL_RO);
47 pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_ROX);
50 pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0);
53 pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0);
60 /* See ptesync comment in radix__set_pte_at() */
62 asm volatile("ptesync": : :"memory");
64 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
69 int change_memory_attr(unsigned long addr, int numpages, long action)
71 unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
72 unsigned long size = numpages * PAGE_SIZE;
77 if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) &&
78 is_vm_area_hugepages((void *)addr)))
81 #ifdef CONFIG_PPC_BOOK3S_64
83 * On hash, the linear mapping is not in the Linux page table so
84 * apply_to_existing_page_range() will have no effect. If in the future
85 * the set_memory_* functions are used on the linear map this will need
88 if (!radix_enabled()) {
89 int region = get_region_id(addr);
91 if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID))
96 return apply_to_existing_page_range(&init_mm, start, size,
97 change_page_attr, (void *)action);