Skip to content

Commit d3ab332

Browse files
zongboxpalmer-dabbelt
authored andcommitted
riscv: add ARCH_HAS_SET_MEMORY support
Add set_memory_ro/rw/x/nx architecture hooks to change the page attribution. Use own set_memory.h rather than generic set_memory.h (i.e. include/asm-generic/set_memory.h), because we want to add other function prototypes here. Signed-off-by: Zong Li <zong.li@sifive.com> Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
1 parent 9f40b6e commit d3ab332

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ config RISCV
5959
select HAVE_EBPF_JIT if 64BIT
6060
select EDAC_SUPPORT
6161
select ARCH_HAS_GIGANTIC_PAGE
62+
select ARCH_HAS_SET_MEMORY
6263
select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
6364
select SPARSEMEM_STATIC if 32BIT
6465
select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU

arch/riscv/include/asm/set_memory.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
/*
3+
* Copyright (C) 2019 SiFive
4+
*/
5+
6+
#ifndef _ASM_RISCV_SET_MEMORY_H
7+
#define _ASM_RISCV_SET_MEMORY_H
8+
9+
/*
10+
* Functions to change memory attributes.
11+
*/
12+
#ifdef CONFIG_MMU
13+
int set_memory_ro(unsigned long addr, int numpages);
14+
int set_memory_rw(unsigned long addr, int numpages);
15+
int set_memory_x(unsigned long addr, int numpages);
16+
int set_memory_nx(unsigned long addr, int numpages);
17+
#else
18+
static inline int set_memory_ro(unsigned long addr, int numpages) { return 0; }
19+
static inline int set_memory_rw(unsigned long addr, int numpages) { return 0; }
20+
static inline int set_memory_x(unsigned long addr, int numpages) { return 0; }
21+
static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
22+
#endif
23+
24+
#endif /* _ASM_RISCV_SET_MEMORY_H */

arch/riscv/mm/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ endif
77

88
obj-y += init.o
99
obj-y += extable.o
10-
obj-$(CONFIG_MMU) += fault.o
10+
obj-$(CONFIG_MMU) += fault.o pageattr.o
1111
obj-y += cacheflush.o
1212
obj-y += context.o
1313

arch/riscv/mm/pageattr.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* Copyright (C) 2019 SiFive
4+
*/
5+
6+
#include <linux/pagewalk.h>
7+
#include <asm/pgtable.h>
8+
#include <asm/tlbflush.h>
9+
#include <asm/bitops.h>
10+
11+
struct pageattr_masks {
12+
pgprot_t set_mask;
13+
pgprot_t clear_mask;
14+
};
15+
16+
static unsigned long set_pageattr_masks(unsigned long val, struct mm_walk *walk)
17+
{
18+
struct pageattr_masks *masks = walk->private;
19+
unsigned long new_val = val;
20+
21+
new_val &= ~(pgprot_val(masks->clear_mask));
22+
new_val |= (pgprot_val(masks->set_mask));
23+
24+
return new_val;
25+
}
26+
27+
static int pageattr_pgd_entry(pgd_t *pgd, unsigned long addr,
28+
unsigned long next, struct mm_walk *walk)
29+
{
30+
pgd_t val = READ_ONCE(*pgd);
31+
32+
if (pgd_leaf(val)) {
33+
val = __pgd(set_pageattr_masks(pgd_val(val), walk));
34+
set_pgd(pgd, val);
35+
}
36+
37+
return 0;
38+
}
39+
40+
static int pageattr_p4d_entry(p4d_t *p4d, unsigned long addr,
41+
unsigned long next, struct mm_walk *walk)
42+
{
43+
p4d_t val = READ_ONCE(*p4d);
44+
45+
if (p4d_leaf(val)) {
46+
val = __p4d(set_pageattr_masks(p4d_val(val), walk));
47+
set_p4d(p4d, val);
48+
}
49+
50+
return 0;
51+
}
52+
53+
static int pageattr_pud_entry(pud_t *pud, unsigned long addr,
54+
unsigned long next, struct mm_walk *walk)
55+
{
56+
pud_t val = READ_ONCE(*pud);
57+
58+
if (pud_leaf(val)) {
59+
val = __pud(set_pageattr_masks(pud_val(val), walk));
60+
set_pud(pud, val);
61+
}
62+
63+
return 0;
64+
}
65+
66+
static int pageattr_pmd_entry(pmd_t *pmd, unsigned long addr,
67+
unsigned long next, struct mm_walk *walk)
68+
{
69+
pmd_t val = READ_ONCE(*pmd);
70+
71+
if (pmd_leaf(val)) {
72+
val = __pmd(set_pageattr_masks(pmd_val(val), walk));
73+
set_pmd(pmd, val);
74+
}
75+
76+
return 0;
77+
}
78+
79+
static int pageattr_pte_entry(pte_t *pte, unsigned long addr,
80+
unsigned long next, struct mm_walk *walk)
81+
{
82+
pte_t val = READ_ONCE(*pte);
83+
84+
val = __pte(set_pageattr_masks(pte_val(val), walk));
85+
set_pte(pte, val);
86+
87+
return 0;
88+
}
89+
90+
static int pageattr_pte_hole(unsigned long addr, unsigned long next,
91+
int depth, struct mm_walk *walk)
92+
{
93+
/* Nothing to do here */
94+
return 0;
95+
}
96+
97+
const static struct mm_walk_ops pageattr_ops = {
98+
.pgd_entry = pageattr_pgd_entry,
99+
.p4d_entry = pageattr_p4d_entry,
100+
.pud_entry = pageattr_pud_entry,
101+
.pmd_entry = pageattr_pmd_entry,
102+
.pte_entry = pageattr_pte_entry,
103+
.pte_hole = pageattr_pte_hole,
104+
};
105+
106+
static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask,
107+
pgprot_t clear_mask)
108+
{
109+
int ret;
110+
unsigned long start = addr;
111+
unsigned long end = start + PAGE_SIZE * numpages;
112+
struct pageattr_masks masks = {
113+
.set_mask = set_mask,
114+
.clear_mask = clear_mask
115+
};
116+
117+
if (!numpages)
118+
return 0;
119+
120+
down_read(&init_mm.mmap_sem);
121+
ret = walk_page_range_novma(&init_mm, start, end, &pageattr_ops, NULL,
122+
&masks);
123+
up_read(&init_mm.mmap_sem);
124+
125+
flush_tlb_kernel_range(start, end);
126+
127+
return ret;
128+
}
129+
130+
int set_memory_ro(unsigned long addr, int numpages)
131+
{
132+
return __set_memory(addr, numpages, __pgprot(_PAGE_READ),
133+
__pgprot(_PAGE_WRITE));
134+
}
135+
136+
int set_memory_rw(unsigned long addr, int numpages)
137+
{
138+
return __set_memory(addr, numpages, __pgprot(_PAGE_READ | _PAGE_WRITE),
139+
__pgprot(0));
140+
}
141+
142+
int set_memory_x(unsigned long addr, int numpages)
143+
{
144+
return __set_memory(addr, numpages, __pgprot(_PAGE_EXEC), __pgprot(0));
145+
}
146+
147+
int set_memory_nx(unsigned long addr, int numpages)
148+
{
149+
return __set_memory(addr, numpages, __pgprot(0), __pgprot(_PAGE_EXEC));
150+
}

0 commit comments

Comments
 (0)