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;
35 /* modify the PTE bits as desired */
38 /* Don't clear DIRTY bit */
39 pte_update_delta(ptep, addr, _PAGE_KERNEL_RW & ~_PAGE_DIRTY, _PAGE_KERNEL_RO);
42 pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_RW);
45 pte_update_delta(ptep, addr, _PAGE_KERNEL_ROX, _PAGE_KERNEL_RO);
48 pte_update_delta(ptep, addr, _PAGE_KERNEL_RO, _PAGE_KERNEL_ROX);
51 pte_update(&init_mm, addr, ptep, _PAGE_PRESENT, 0, 0);
54 pte_update(&init_mm, addr, ptep, 0, _PAGE_PRESENT, 0);
61 /* See ptesync comment in radix__set_pte_at() */
63 asm volatile("ptesync": : :"memory");
65 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
70 int change_memory_attr(unsigned long addr, int numpages, long action)
72 unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
73 unsigned long size = numpages * PAGE_SIZE;
78 if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) &&
79 is_vm_area_hugepages((void *)addr)))
82 #ifdef CONFIG_PPC_BOOK3S_64
84 * On hash, the linear mapping is not in the Linux page table so
85 * apply_to_existing_page_range() will have no effect. If in the future
86 * the set_memory_* functions are used on the linear map this will need
89 if (!radix_enabled()) {
90 int region = get_region_id(addr);
92 if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID))
97 return apply_to_existing_page_range(&init_mm, start, size,
98 change_page_attr, (void *)action);