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. invalidate the page table entry
23 * 3. install the new entry with the updated attributes
25 * Invalidating the pte means there are situations where this will not work
26 * when in theory it should.
28 * - removing write from page whilst it is being executed
29 * - setting a page read-only whilst it is being read by another CPU
32 static int change_page_attr(pte_t *ptep, unsigned long addr, void *data)
34 long action = (long)data;
37 spin_lock(&init_mm.page_table_lock);
39 /* invalidate the PTE so it's safe to modify */
40 pte = ptep_get_and_clear(&init_mm, addr, ptep);
41 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
43 /* modify the PTE bits as desired, then apply */
46 pte = pte_wrprotect(pte);
49 pte = pte_mkwrite(pte_mkdirty(pte));
52 pte = pte_exprotect(pte);
55 pte = pte_mkexec(pte);
62 set_pte_at(&init_mm, addr, ptep, pte);
64 /* See ptesync comment in radix__set_pte_at() */
66 asm volatile("ptesync": : :"memory");
67 spin_unlock(&init_mm.page_table_lock);
72 int change_memory_attr(unsigned long addr, int numpages, long action)
74 unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
75 unsigned long size = numpages * PAGE_SIZE;
80 if (WARN_ON_ONCE(is_vmalloc_or_module_addr((void *)addr) &&
81 is_vm_area_hugepages((void *)addr)))
84 #ifdef CONFIG_PPC_BOOK3S_64
86 * On hash, the linear mapping is not in the Linux page table so
87 * apply_to_existing_page_range() will have no effect. If in the future
88 * the set_memory_* functions are used on the linear map this will need
91 if (!radix_enabled()) {
92 int region = get_region_id(addr);
94 if (WARN_ON_ONCE(region != VMALLOC_REGION_ID && region != IO_REGION_ID))
99 return apply_to_existing_page_range(&init_mm, start, size,
100 change_page_attr, (void *)action);
104 * Set the attributes of a page:
106 * This function is used by PPC32 at the end of init to set final kernel memory
107 * protection. It includes changing the maping of the page it is executing from
108 * and data pages it is using.
110 static int set_page_attr(pte_t *ptep, unsigned long addr, void *data)
112 pgprot_t prot = __pgprot((unsigned long)data);
114 spin_lock(&init_mm.page_table_lock);
116 set_pte_at(&init_mm, addr, ptep, pte_modify(*ptep, prot));
117 flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
119 spin_unlock(&init_mm.page_table_lock);
124 int set_memory_attr(unsigned long addr, int numpages, pgprot_t prot)
126 unsigned long start = ALIGN_DOWN(addr, PAGE_SIZE);
127 unsigned long sz = numpages * PAGE_SIZE;
132 return apply_to_existing_page_range(&init_mm, start, sz, set_page_attr,
133 (void *)pgprot_val(prot));