Skip to content

Commit c5f8689

Browse files
committed
Merge tag 'riscv-for-linux-5.6-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux
Pull RISC-V fixes from Palmer Dabbelt: "This contains a handful of RISC-V related fixes that I've collected and would like to target for 5.6-rc4: - A fix to set up the PMPs on boot, which allows the kernel to access memory on systems that don't set up permissive PMPs before getting to Linux. This only effects machine-mode kernels, which currently means only NOMMU kernels. - A fix to avoid enabling supervisor-mode interrupts when running in machine-mode, also only for NOMMU kernels. - A pair of fixes to our KASAN support to avoid corrupting memory. - A gitignore fix. This boots on QEMU's virt board for me" * tag 'riscv-for-linux-5.6-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/riscv/linux: riscv: adjust the indent riscv: allocate a complete page size for each page table riscv: Fix gitignore RISC-V: Don't enable all interrupts in trap_init() riscv: set pmp configuration if kernel is running in M-mode
2 parents d67f250 + 8458ca1 commit c5f8689

File tree

5 files changed

+53
-24
lines changed

5 files changed

+53
-24
lines changed

arch/riscv/boot/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Image
22
Image.gz
3+
loader
4+
loader.lds

arch/riscv/include/asm/csr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@
7272
#define EXC_LOAD_PAGE_FAULT 13
7373
#define EXC_STORE_PAGE_FAULT 15
7474

75+
/* PMP configuration */
76+
#define PMP_R 0x01
77+
#define PMP_W 0x02
78+
#define PMP_X 0x04
79+
#define PMP_A 0x18
80+
#define PMP_A_TOR 0x08
81+
#define PMP_A_NA4 0x10
82+
#define PMP_A_NAPOT 0x18
83+
#define PMP_L 0x80
84+
7585
/* symbolic CSR names: */
7686
#define CSR_CYCLE 0xc00
7787
#define CSR_TIME 0xc01
@@ -100,6 +110,8 @@
100110
#define CSR_MCAUSE 0x342
101111
#define CSR_MTVAL 0x343
102112
#define CSR_MIP 0x344
113+
#define CSR_PMPCFG0 0x3a0
114+
#define CSR_PMPADDR0 0x3b0
103115
#define CSR_MHARTID 0xf14
104116

105117
#ifdef CONFIG_RISCV_M_MODE

arch/riscv/kernel/head.S

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ _start_kernel:
5858
/* Reset all registers except ra, a0, a1 */
5959
call reset_regs
6060

61+
/* Setup a PMP to permit access to all of memory. */
62+
li a0, -1
63+
csrw CSR_PMPADDR0, a0
64+
li a0, (PMP_A_NAPOT | PMP_R | PMP_W | PMP_X)
65+
csrw CSR_PMPCFG0, a0
66+
6167
/*
6268
* The hartid in a0 is expected later on, and we have no firmware
6369
* to hand it to us.

arch/riscv/kernel/traps.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,6 @@ void __init trap_init(void)
156156
csr_write(CSR_SCRATCH, 0);
157157
/* Set the exception vector address */
158158
csr_write(CSR_TVEC, &handle_exception);
159-
/* Enable all interrupts */
160-
csr_write(CSR_IE, -1);
159+
/* Enable interrupts */
160+
csr_write(CSR_IE, IE_SIE | IE_EIE);
161161
}

arch/riscv/mm/kasan_init.c

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,56 +19,64 @@ asmlinkage void __init kasan_early_init(void)
1919
for (i = 0; i < PTRS_PER_PTE; ++i)
2020
set_pte(kasan_early_shadow_pte + i,
2121
mk_pte(virt_to_page(kasan_early_shadow_page),
22-
PAGE_KERNEL));
22+
PAGE_KERNEL));
2323

2424
for (i = 0; i < PTRS_PER_PMD; ++i)
2525
set_pmd(kasan_early_shadow_pmd + i,
26-
pfn_pmd(PFN_DOWN(__pa((uintptr_t)kasan_early_shadow_pte)),
27-
__pgprot(_PAGE_TABLE)));
26+
pfn_pmd(PFN_DOWN
27+
(__pa((uintptr_t) kasan_early_shadow_pte)),
28+
__pgprot(_PAGE_TABLE)));
2829

2930
for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
3031
i += PGDIR_SIZE, ++pgd)
3132
set_pgd(pgd,
32-
pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
33-
__pgprot(_PAGE_TABLE)));
33+
pfn_pgd(PFN_DOWN
34+
(__pa(((uintptr_t) kasan_early_shadow_pmd))),
35+
__pgprot(_PAGE_TABLE)));
3436

3537
/* init for swapper_pg_dir */
3638
pgd = pgd_offset_k(KASAN_SHADOW_START);
3739

3840
for (i = KASAN_SHADOW_START; i < KASAN_SHADOW_END;
3941
i += PGDIR_SIZE, ++pgd)
4042
set_pgd(pgd,
41-
pfn_pgd(PFN_DOWN(__pa(((uintptr_t)kasan_early_shadow_pmd))),
42-
__pgprot(_PAGE_TABLE)));
43+
pfn_pgd(PFN_DOWN
44+
(__pa(((uintptr_t) kasan_early_shadow_pmd))),
45+
__pgprot(_PAGE_TABLE)));
4346

4447
flush_tlb_all();
4548
}
4649

4750
static void __init populate(void *start, void *end)
4851
{
49-
unsigned long i;
52+
unsigned long i, offset;
5053
unsigned long vaddr = (unsigned long)start & PAGE_MASK;
5154
unsigned long vend = PAGE_ALIGN((unsigned long)end);
5255
unsigned long n_pages = (vend - vaddr) / PAGE_SIZE;
56+
unsigned long n_ptes =
57+
((n_pages + PTRS_PER_PTE) & -PTRS_PER_PTE) / PTRS_PER_PTE;
5358
unsigned long n_pmds =
54-
(n_pages % PTRS_PER_PTE) ? n_pages / PTRS_PER_PTE + 1 :
55-
n_pages / PTRS_PER_PTE;
59+
((n_ptes + PTRS_PER_PMD) & -PTRS_PER_PMD) / PTRS_PER_PMD;
60+
61+
pte_t *pte =
62+
memblock_alloc(n_ptes * PTRS_PER_PTE * sizeof(pte_t), PAGE_SIZE);
63+
pmd_t *pmd =
64+
memblock_alloc(n_pmds * PTRS_PER_PMD * sizeof(pmd_t), PAGE_SIZE);
5665
pgd_t *pgd = pgd_offset_k(vaddr);
57-
pmd_t *pmd = memblock_alloc(n_pmds * sizeof(pmd_t), PAGE_SIZE);
58-
pte_t *pte = memblock_alloc(n_pages * sizeof(pte_t), PAGE_SIZE);
5966

6067
for (i = 0; i < n_pages; i++) {
6168
phys_addr_t phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
62-
63-
set_pte(pte + i, pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
69+
set_pte(&pte[i], pfn_pte(PHYS_PFN(phys), PAGE_KERNEL));
6470
}
6571

66-
for (i = 0; i < n_pmds; ++pgd, i += PTRS_PER_PMD)
67-
set_pgd(pgd, pfn_pgd(PFN_DOWN(__pa(((uintptr_t)(pmd + i)))),
72+
for (i = 0, offset = 0; i < n_ptes; i++, offset += PTRS_PER_PTE)
73+
set_pmd(&pmd[i],
74+
pfn_pmd(PFN_DOWN(__pa(&pte[offset])),
6875
__pgprot(_PAGE_TABLE)));
6976

70-
for (i = 0; i < n_pages; ++pmd, i += PTRS_PER_PTE)
71-
set_pmd(pmd, pfn_pmd(PFN_DOWN(__pa((uintptr_t)(pte + i))),
77+
for (i = 0, offset = 0; i < n_pmds; i++, offset += PTRS_PER_PMD)
78+
set_pgd(&pgd[i],
79+
pfn_pgd(PFN_DOWN(__pa(&pmd[offset])),
7280
__pgprot(_PAGE_TABLE)));
7381

7482
flush_tlb_all();
@@ -81,7 +89,8 @@ void __init kasan_init(void)
8189
unsigned long i;
8290

8391
kasan_populate_early_shadow((void *)KASAN_SHADOW_START,
84-
(void *)kasan_mem_to_shadow((void *)VMALLOC_END));
92+
(void *)kasan_mem_to_shadow((void *)
93+
VMALLOC_END));
8594

8695
for_each_memblock(memory, reg) {
8796
void *start = (void *)__va(reg->base);
@@ -90,14 +99,14 @@ void __init kasan_init(void)
9099
if (start >= end)
91100
break;
92101

93-
populate(kasan_mem_to_shadow(start),
94-
kasan_mem_to_shadow(end));
102+
populate(kasan_mem_to_shadow(start), kasan_mem_to_shadow(end));
95103
};
96104

97105
for (i = 0; i < PTRS_PER_PTE; i++)
98106
set_pte(&kasan_early_shadow_pte[i],
99107
mk_pte(virt_to_page(kasan_early_shadow_page),
100-
__pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_ACCESSED)));
108+
__pgprot(_PAGE_PRESENT | _PAGE_READ |
109+
_PAGE_ACCESSED)));
101110

102111
memset(kasan_early_shadow_page, 0, PAGE_SIZE);
103112
init_task.kasan_depth = 0;

0 commit comments

Comments
 (0)