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>
19 * Updates the attributes of a page in three steps:
21 * 1. take the page_table_lock
22 * 2. install the new entry with the updated attributes
25 * This sequence is safe against concurrent updates, and also allows updating the
26 * attributes of a page currently being executed or accessed.
28 static int change_page_attr(pte_t *ptep, unsigned long addr, void *data)
30 long action = (long)data;
33 spin_lock(&init_mm.page_table_lock);
37 /* modify the PTE bits as desired, then apply */
40 pte = pte_wrprotect(pte);
43 pte = pte_mkwrite(pte_mkdirty(pte));
46 pte = pte_exprotect(pte);
49 pte = pte_mkexec(pte);
56 pte_update(&init_mm, addr, ptep, ~0UL, pte_val(pte), 0);
58 /* See ptesync comment in radix__set_pte_at() */
60 asm volatile("ptesync": : :"memory");
62 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
64 spin_unlock(&init_mm.page_table_lock);
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);
101 * Set the attributes of a page:
103 * This function is used by PPC32 at the end of init to set final kernel memory
104 * protection. It includes changing the maping of the page it is executing from
105 * and data pages it is using.
107 static int set_page_attr(pte_t *ptep, unsigned long addr, void *data)
109 pgprot_t prot = __pgprot((unsigned long)data);
111 spin_lock(&init_mm.page_table_lock);
113 set_pte_at(&init_mm, addr, ptep, pte_modify(*ptep, prot));
114 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
116 spin_unlock(&init_mm.page_table_lock);
121 int set_memory_attr(unsigned long addr, int numpages, pgprot_t prot)
123 unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
124 unsigned long sz = numpages * PAGE_SIZE;
129 return apply_to_existing_page_range(&init_mm, start, sz, set_page_attr,
130 (void *)pgprot_val(prot));