Skip to content

Commit 100631b

Browse files
mmindpalmer-dabbelt
authored andcommitted
riscv: Fix accessing pfn bits in PTEs for non-32bit variants
On rv32 the PFN part of PTEs is defined to use bits [xlen-1:10] while on rv64 it is defined to use bits [53:10], leaving [63:54] as reserved. With upcoming optional extensions like svpbmt these previously reserved bits will get used so simply right-shifting the PTE to get the PFN won't be enough. So introduce a _PAGE_PFN_MASK constant to mask the correct bits for both rv32 and rv64 before shifting. Signed-off-by: Heiko Stuebner <heiko@sntech.de> Reviewed-by: Philipp Tomsich <philipp.tomsich@vrull.eu> Link: https://lore.kernel.org/r/20220511192921.2223629-9-heiko@sntech.de Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent ffb0b0a commit 100631b

File tree

4 files changed

+24
-12
lines changed

4 files changed

+24
-12
lines changed

arch/riscv/include/asm/pgtable-32.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define _ASM_RISCV_PGTABLE_32_H
88

99
#include <asm-generic/pgtable-nopmd.h>
10+
#include <linux/bits.h>
1011
#include <linux/const.h>
1112

1213
/* Size of region mapped by a page global directory */
@@ -16,4 +17,11 @@
1617

1718
#define MAX_POSSIBLE_PHYSMEM_BITS 34
1819

20+
/*
21+
* rv32 PTE format:
22+
* | XLEN-1 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
23+
* PFN reserved for SW D A G U X W R V
24+
*/
25+
#define _PAGE_PFN_MASK GENMASK(31, 10)
26+
1927
#endif /* _ASM_RISCV_PGTABLE_32_H */

arch/riscv/include/asm/pgtable-64.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef _ASM_RISCV_PGTABLE_64_H
77
#define _ASM_RISCV_PGTABLE_64_H
88

9+
#include <linux/bits.h>
910
#include <linux/const.h>
1011

1112
extern bool pgtable_l4_enabled;
@@ -65,6 +66,13 @@ typedef struct {
6566

6667
#define PTRS_PER_PMD (PAGE_SIZE / sizeof(pmd_t))
6768

69+
/*
70+
* rv64 PTE format:
71+
* | 63 | 62 61 | 60 54 | 53 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
72+
* N MT RSV PFN reserved for SW D A G U X W R V
73+
*/
74+
#define _PAGE_PFN_MASK GENMASK(53, 10)
75+
6876
static inline int pud_present(pud_t pud)
6977
{
7078
return (pud_val(pud) & _PAGE_PRESENT);
@@ -108,12 +116,12 @@ static inline unsigned long _pud_pfn(pud_t pud)
108116

109117
static inline pmd_t *pud_pgtable(pud_t pud)
110118
{
111-
return (pmd_t *)pfn_to_virt(pud_val(pud) >> _PAGE_PFN_SHIFT);
119+
return (pmd_t *)pfn_to_virt(__page_val_to_pfn(pud_val(pud)));
112120
}
113121

114122
static inline struct page *pud_page(pud_t pud)
115123
{
116-
return pfn_to_page(pud_val(pud) >> _PAGE_PFN_SHIFT);
124+
return pfn_to_page(__page_val_to_pfn(pud_val(pud)));
117125
}
118126

119127
#define mm_p4d_folded mm_p4d_folded
@@ -143,7 +151,7 @@ static inline pmd_t pfn_pmd(unsigned long pfn, pgprot_t prot)
143151

144152
static inline unsigned long _pmd_pfn(pmd_t pmd)
145153
{
146-
return pmd_val(pmd) >> _PAGE_PFN_SHIFT;
154+
return __page_val_to_pfn(pmd_val(pmd));
147155
}
148156

149157
#define mk_pmd(page, prot) pfn_pmd(page_to_pfn(page), prot)

arch/riscv/include/asm/pgtable-bits.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
#ifndef _ASM_RISCV_PGTABLE_BITS_H
77
#define _ASM_RISCV_PGTABLE_BITS_H
88

9-
/*
10-
* PTE format:
11-
* | XLEN-1 10 | 9 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0
12-
* PFN reserved for SW D A G U X W R V
13-
*/
14-
159
#define _PAGE_ACCESSED_OFFSET 6
1610

1711
#define _PAGE_PRESENT (1 << 0)

arch/riscv/include/asm/pgtable.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@
108108
#include <asm/tlbflush.h>
109109
#include <linux/mm_types.h>
110110

111+
#define __page_val_to_pfn(_val) (((_val) & _PAGE_PFN_MASK) >> _PAGE_PFN_SHIFT)
112+
111113
#ifdef CONFIG_64BIT
112114
#include <asm/pgtable-64.h>
113115
#else
@@ -261,12 +263,12 @@ static inline unsigned long _pgd_pfn(pgd_t pgd)
261263

262264
static inline struct page *pmd_page(pmd_t pmd)
263265
{
264-
return pfn_to_page(pmd_val(pmd) >> _PAGE_PFN_SHIFT);
266+
return pfn_to_page(__page_val_to_pfn(pmd_val(pmd)));
265267
}
266268

267269
static inline unsigned long pmd_page_vaddr(pmd_t pmd)
268270
{
269-
return (unsigned long)pfn_to_virt(pmd_val(pmd) >> _PAGE_PFN_SHIFT);
271+
return (unsigned long)pfn_to_virt(__page_val_to_pfn(pmd_val(pmd)));
270272
}
271273

272274
static inline pte_t pmd_pte(pmd_t pmd)
@@ -282,7 +284,7 @@ static inline pte_t pud_pte(pud_t pud)
282284
/* Yields the page frame number (PFN) of a page table entry */
283285
static inline unsigned long pte_pfn(pte_t pte)
284286
{
285-
return (pte_val(pte) >> _PAGE_PFN_SHIFT);
287+
return __page_val_to_pfn(pte_val(pte));
286288
}
287289

288290
#define pte_page(x) pfn_to_page(pte_pfn(x))

0 commit comments

Comments
 (0)