Skip to content

Commit 00cb41d

Browse files
zongboxpalmer-dabbelt
authored andcommitted
riscv: add alignment for text, rodata and data sections
The kernel mapping will tried to optimize its mapping by using bigger size. In rv64, it tries to use PMD_SIZE, and tryies to use PGDIR_SIZE in rv32. To ensure that the start address of these sections could fit the mapping entry size, make them align to the biggest alignment. Define a macro SECTION_ALIGN because the HPAGE_SIZE or PMD_SIZE, etc., are invisible in linker script. This patch is prepared for STRICT_KERNEL_RWX support. Signed-off-by: Zong Li <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent bd3d914 commit 00cb41d

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

arch/riscv/include/asm/set_memory.h

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

9+
#ifndef __ASSEMBLY__
910
/*
1011
* Functions to change memory attributes.
1112
*/
@@ -24,4 +25,16 @@ static inline int set_memory_nx(unsigned long addr, int numpages) { return 0; }
2425
int set_direct_map_invalid_noflush(struct page *page);
2526
int set_direct_map_default_noflush(struct page *page);
2627

28+
#endif /* __ASSEMBLY__ */
29+
30+
#ifdef CONFIG_ARCH_HAS_STRICT_KERNEL_RWX
31+
#ifdef CONFIG_64BIT
32+
#define SECTION_ALIGN (1 << 21)
33+
#else
34+
#define SECTION_ALIGN (1 << 22)
35+
#endif
36+
#else /* !CONFIG_ARCH_HAS_STRICT_KERNEL_RWX */
37+
#define SECTION_ALIGN L1_CACHE_BYTES
38+
#endif /* CONFIG_ARCH_HAS_STRICT_KERNEL_RWX */
39+
2740
#endif /* _ASM_RISCV_SET_MEMORY_H */

arch/riscv/kernel/vmlinux.lds.S

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <asm/page.h>
1010
#include <asm/cache.h>
1111
#include <asm/thread_info.h>
12+
#include <asm/set_memory.h>
1213

1314
OUTPUT_ARCH(riscv)
1415
ENTRY(_start)
@@ -36,6 +37,7 @@ SECTIONS
3637
PERCPU_SECTION(L1_CACHE_BYTES)
3738
__init_end = .;
3839

40+
. = ALIGN(SECTION_ALIGN);
3941
.text : {
4042
_text = .;
4143
_stext = .;
@@ -53,13 +55,14 @@ SECTIONS
5355

5456
/* Start of data section */
5557
_sdata = .;
56-
RO_DATA(L1_CACHE_BYTES)
58+
RO_DATA(SECTION_ALIGN)
5759
.srodata : {
5860
*(.srodata*)
5961
}
6062

6163
EXCEPTION_TABLE(0x10)
6264

65+
. = ALIGN(SECTION_ALIGN);
6366
_data = .;
6467

6568
RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_SIZE)

0 commit comments

Comments
 (0)