1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
6 #include <linux/module.h>
8 #include <asm/tlbflush.h>
9 #include <asm/set_memory.h>
11 struct page_change_data
{
16 static int change_page_range(pte_t
*ptep
, unsigned long addr
, void *data
)
18 struct page_change_data
*cdata
= data
;
21 pte
= clear_pte_bit(pte
, cdata
->clear_mask
);
22 pte
= set_pte_bit(pte
, cdata
->set_mask
);
24 set_pte_ext(ptep
, pte
, 0);
28 static bool range_in_range(unsigned long start
, unsigned long size
,
29 unsigned long range_start
, unsigned long range_end
)
31 return start
>= range_start
&& start
< range_end
&&
32 size
<= range_end
- start
;
36 * This function assumes that the range is mapped with PAGE_SIZE pages.
38 static int __change_memory_common(unsigned long start
, unsigned long size
,
39 pgprot_t set_mask
, pgprot_t clear_mask
)
41 struct page_change_data data
;
44 data
.set_mask
= set_mask
;
45 data
.clear_mask
= clear_mask
;
47 ret
= apply_to_page_range(&init_mm
, start
, size
, change_page_range
,
50 flush_tlb_kernel_range(start
, start
+ size
);
54 static int change_memory_common(unsigned long addr
, int numpages
,
55 pgprot_t set_mask
, pgprot_t clear_mask
)
57 unsigned long start
= addr
& PAGE_MASK
;
58 unsigned long end
= PAGE_ALIGN(addr
) + numpages
* PAGE_SIZE
;
59 unsigned long size
= end
- start
;
61 WARN_ON_ONCE(start
!= addr
);
66 if (!range_in_range(start
, size
, MODULES_VADDR
, MODULES_END
) &&
67 !range_in_range(start
, size
, VMALLOC_START
, VMALLOC_END
))
70 return __change_memory_common(start
, size
, set_mask
, clear_mask
);
73 int set_memory_ro(unsigned long addr
, int numpages
)
75 return change_memory_common(addr
, numpages
,
76 __pgprot(L_PTE_RDONLY
),
80 int set_memory_rw(unsigned long addr
, int numpages
)
82 return change_memory_common(addr
, numpages
,
84 __pgprot(L_PTE_RDONLY
));
87 int set_memory_nx(unsigned long addr
, int numpages
)
89 return change_memory_common(addr
, numpages
,
94 int set_memory_x(unsigned long addr
, int numpages
)
96 return change_memory_common(addr
, numpages
,
101 int set_memory_valid(unsigned long addr
, int numpages
, int enable
)
104 return __change_memory_common(addr
, PAGE_SIZE
* numpages
,
105 __pgprot(L_PTE_VALID
),
108 return __change_memory_common(addr
, PAGE_SIZE
* numpages
,
110 __pgprot(L_PTE_VALID
));