Skip to content

Commit d5f17ee

Browse files
chleroympe
authored andcommitted
powerpc/8xx: don't disable large TLBs with CONFIG_STRICT_KERNEL_RWX
This patch implements handling of STRICT_KERNEL_RWX with large TLBs directly in the TLB miss handlers. To do so, etext and sinittext are aligned on 512kB boundaries and the miss handlers use 512kB pages instead of 8Mb pages for addresses close to the boundaries. It sets RO PP flags for addresses under sinittext. Signed-off-by: Christophe Leroy <christophe.leroy@c-s.fr> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent 0f4a904 commit d5f17ee

6 files changed

Lines changed: 78 additions & 16 deletions

File tree

‎arch/powerpc/Kconfig‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ config ETEXT_SHIFT
735735
int "_etext shift" if ETEXT_SHIFT_BOOL
736736
range 17 28 if STRICT_KERNEL_RWX && PPC_BOOK3S_32
737737
default 17 if STRICT_KERNEL_RWX && PPC_BOOK3S_32
738+
default 19 if STRICT_KERNEL_RWX && PPC_8xx
738739
default PPC_PAGE_SHIFT
739740
help
740741
On Book3S 32 (603+), IBATs are used to map kernel text.
@@ -755,6 +756,7 @@ config DATA_SHIFT
755756
default 24 if STRICT_KERNEL_RWX && PPC64
756757
range 17 28 if STRICT_KERNEL_RWX && PPC_BOOK3S_32
757758
default 22 if STRICT_KERNEL_RWX && PPC_BOOK3S_32
759+
default 19 if STRICT_KERNEL_RWX && PPC_8xx
758760
default PPC_PAGE_SHIFT
759761
help
760762
On Book3S 32 (603+), DBATs are used to map kernel text and rodata RO.

‎arch/powerpc/include/asm/nohash/32/mmu-8xx.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,10 @@ static inline unsigned int mmu_psize_to_shift(unsigned int mmu_psize)
231231
}
232232

233233
/* patch sites */
234-
extern s32 patch__itlbmiss_linmem_top;
234+
extern s32 patch__itlbmiss_linmem_top, patch__itlbmiss_linmem_top8;
235235
extern s32 patch__dtlbmiss_linmem_top, patch__dtlbmiss_immr_jmp;
236236
extern s32 patch__fixupdar_linmem_top;
237+
extern s32 patch__dtlbmiss_romem_top, patch__dtlbmiss_romem_top8;
237238

238239
extern s32 patch__itlbmiss_exit_1, patch__itlbmiss_exit_2;
239240
extern s32 patch__dtlbmiss_exit_1, patch__dtlbmiss_exit_2, patch__dtlbmiss_exit_3;

‎arch/powerpc/kernel/head_8xx.S‎

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ SystemCall:
292292
*/
293293
EXCEPTION(0x1000, SoftEmu, program_check_exception, EXC_XFER_STD)
294294

295+
/* Called from DataStoreTLBMiss when perf TLB misses events are activated */
296+
#ifdef CONFIG_PERF_EVENTS
297+
patch_site 0f, patch__dtlbmiss_perf
298+
0: lwz r10, (dtlb_miss_counter - PAGE_OFFSET)@l(0)
299+
addi r10, r10, 1
300+
stw r10, (dtlb_miss_counter - PAGE_OFFSET)@l(0)
301+
mfspr r10, SPRN_SPRG_SCRATCH0
302+
mfspr r11, SPRN_SPRG_SCRATCH1
303+
rfi
304+
#endif
305+
295306
. = 0x1100
296307
/*
297308
* For the MPC8xx, this is a software tablewalk to load the instruction
@@ -405,10 +416,20 @@ InstructionTLBMiss:
405416
#ifndef CONFIG_PIN_TLB_TEXT
406417
ITLBMissLinear:
407418
mtcr r11
419+
#ifdef CONFIG_STRICT_KERNEL_RWX
420+
patch_site 0f, patch__itlbmiss_linmem_top8
421+
422+
mfspr r10, SPRN_SRR0
423+
0: subis r11, r10, (PAGE_OFFSET - 0x80000000)@ha
424+
rlwinm r11, r11, 4, MI_PS8MEG ^ MI_PS512K
425+
ori r11, r11, MI_PS512K | MI_SVALID
426+
rlwinm r10, r10, 0, 0x0ff80000 /* 8xx supports max 256Mb RAM */
427+
#else
408428
/* Set 8M byte page and mark it valid */
409429
li r11, MI_PS8MEG | MI_SVALID
410-
mtspr SPRN_MI_TWC, r11
411430
rlwinm r10, r10, 20, 0x0f800000 /* 8xx supports max 256Mb RAM */
431+
#endif
432+
mtspr SPRN_MI_TWC, r11
412433
ori r10, r10, 0xf0 | MI_SPS16K | _PAGE_SH | _PAGE_DIRTY | \
413434
_PAGE_PRESENT
414435
mtspr SPRN_MI_RPN, r10 /* Update TLB entry */
@@ -494,16 +515,6 @@ DataStoreTLBMiss:
494515
rfi
495516
patch_site 0b, patch__dtlbmiss_exit_1
496517

497-
#ifdef CONFIG_PERF_EVENTS
498-
patch_site 0f, patch__dtlbmiss_perf
499-
0: lwz r10, (dtlb_miss_counter - PAGE_OFFSET)@l(0)
500-
addi r10, r10, 1
501-
stw r10, (dtlb_miss_counter - PAGE_OFFSET)@l(0)
502-
mfspr r10, SPRN_SPRG_SCRATCH0
503-
mfspr r11, SPRN_SPRG_SCRATCH1
504-
rfi
505-
#endif
506-
507518
DTLBMissIMMR:
508519
mtcr r11
509520
/* Set 512k byte guarded page and mark it valid */
@@ -525,10 +536,29 @@ DTLBMissIMMR:
525536

526537
DTLBMissLinear:
527538
mtcr r11
539+
rlwinm r10, r10, 20, 0x0f800000 /* 8xx supports max 256Mb RAM */
540+
#ifdef CONFIG_STRICT_KERNEL_RWX
541+
patch_site 0f, patch__dtlbmiss_romem_top8
542+
543+
0: subis r11, r10, (PAGE_OFFSET - 0x80000000)@ha
544+
rlwinm r11, r11, 0, 0xff800000
545+
neg r10, r11
546+
or r11, r11, r10
547+
rlwinm r11, r11, 4, MI_PS8MEG ^ MI_PS512K
548+
ori r11, r11, MI_PS512K | MI_SVALID
549+
mfspr r10, SPRN_MD_EPN
550+
rlwinm r10, r10, 0, 0x0ff80000 /* 8xx supports max 256Mb RAM */
551+
#else
528552
/* Set 8M byte page and mark it valid */
529553
li r11, MD_PS8MEG | MD_SVALID
554+
#endif
530555
mtspr SPRN_MD_TWC, r11
531-
rlwinm r10, r10, 20, 0x0f800000 /* 8xx supports max 256Mb RAM */
556+
#ifdef CONFIG_STRICT_KERNEL_RWX
557+
patch_site 0f, patch__dtlbmiss_romem_top
558+
559+
0: subis r11, r10, 0
560+
rlwimi r10, r11, 11, _PAGE_RO
561+
#endif
532562
ori r10, r10, 0xf0 | MD_SPS16K | _PAGE_SH | _PAGE_DIRTY | \
533563
_PAGE_PRESENT
534564
mtspr SPRN_MD_RPN, r10 /* Update TLB entry */

‎arch/powerpc/mm/8xx_mmu.c‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,20 @@ static void __init mmu_mapin_immr(void)
9494
map_kernel_page(v + offset, p + offset, PAGE_KERNEL_NCG);
9595
}
9696

97-
static void __init mmu_patch_cmp_limit(s32 *site, unsigned long mapped)
97+
static void mmu_patch_cmp_limit(s32 *site, unsigned long mapped)
9898
{
9999
modify_instruction_site(site, 0xffff, (unsigned long)__va(mapped) >> 16);
100100
}
101101

102+
static void mmu_patch_addis(s32 *site, long simm)
103+
{
104+
unsigned int instr = *(unsigned int *)patch_site_addr(site);
105+
106+
instr &= 0xffff0000;
107+
instr |= ((unsigned long)simm) >> 16;
108+
patch_instruction_site(site, instr);
109+
}
110+
102111
unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
103112
{
104113
unsigned long mapped;
@@ -135,6 +144,26 @@ unsigned long __init mmu_mapin_ram(unsigned long base, unsigned long top)
135144
return mapped;
136145
}
137146

147+
void mmu_mark_initmem_nx(void)
148+
{
149+
if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && CONFIG_ETEXT_SHIFT < 23)
150+
mmu_patch_addis(&patch__itlbmiss_linmem_top8,
151+
-((long)_etext & ~(LARGE_PAGE_SIZE_8M - 1)));
152+
if (!IS_ENABLED(CONFIG_PIN_TLB_TEXT))
153+
mmu_patch_cmp_limit(&patch__itlbmiss_linmem_top, __pa(_etext));
154+
}
155+
156+
#ifdef CONFIG_STRICT_KERNEL_RWX
157+
void mmu_mark_rodata_ro(void)
158+
{
159+
if (CONFIG_DATA_SHIFT < 23)
160+
mmu_patch_addis(&patch__dtlbmiss_romem_top8,
161+
-__pa(((unsigned long)_sinittext) &
162+
~(LARGE_PAGE_SIZE_8M - 1)));
163+
mmu_patch_addis(&patch__dtlbmiss_romem_top, -__pa(_sinittext));
164+
}
165+
#endif
166+
138167
void __init setup_initial_memory_limit(phys_addr_t first_memblock_base,
139168
phys_addr_t first_memblock_size)
140169
{

‎arch/powerpc/mm/init_32.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void __init MMU_setup(void)
108108
__map_without_bats = 1;
109109
__map_without_ltlbs = 1;
110110
}
111-
if (strict_kernel_rwx_enabled())
111+
if (strict_kernel_rwx_enabled() && !IS_ENABLED(CONFIG_PPC_8xx))
112112
__map_without_ltlbs = 1;
113113
}
114114

‎arch/powerpc/mm/mmu_decl.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ static inline phys_addr_t v_block_mapped(unsigned long va) { return 0; }
166166
static inline unsigned long p_block_mapped(phys_addr_t pa) { return 0; }
167167
#endif
168168

169-
#if defined(CONFIG_PPC_BOOK3S_32)
169+
#if defined(CONFIG_PPC_BOOK3S_32) || defined(CONFIG_PPC_8xx)
170170
void mmu_mark_initmem_nx(void);
171171
void mmu_mark_rodata_ro(void);
172172
#else

0 commit comments

Comments
 (0)