Skip to content

Commit 3847dab

Browse files
committed
parisc: Add alternative coding infrastructure
This patch adds the necessary code to patch a running kernel at runtime to improve performance. The current implementation offers a few optimizations variants: - When running a SMP kernel on a single UP processor, unwanted assembler statements like locking functions are overwritten with NOPs. When multiple instructions shall be skipped, one branch instruction is used instead of multiple nop instructions. - In the UP case, some pdtlb and pitlb instructions are patched to become pdtlb,l and pitlb,l which only flushes the CPU-local tlb entries instead of broadcasting the flush to other CPUs in the system and thus may improve performance. - fic and fdc instructions are skipped if no I- or D-caches are installed. This should speed up qemu emulation and cacheless systems. - If no cache coherence is needed for IO operations, the relevant fdc and sync instructions in the sba and ccio drivers are replaced by nops. - On systems which share I- and D-TLBs and thus don't have a seperate instruction TLB, the pitlb instruction is replaced by a nop. Live-patching is done early in the boot process, just after having run the system inventory. No drivers are running and thus no external interrupts should arrive. So the hope is that no TLB exceptions will occur during the patching. If this turns out to be wrong we will probably need to do the patching in real-mode. Signed-off-by: Helge Deller <deller@gmx.de>
1 parent 34c201a commit 3847dab

14 files changed

Lines changed: 233 additions & 62 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ASM_PARISC_ALTERNATIVE_H
3+
#define __ASM_PARISC_ALTERNATIVE_H
4+
5+
#define ALT_COND_NO_SMP 0x01 /* when running UP instead of SMP */
6+
#define ALT_COND_NO_DCACHE 0x02 /* if system has no d-cache */
7+
#define ALT_COND_NO_ICACHE 0x04 /* if system has no i-cache */
8+
#define ALT_COND_NO_SPLIT_TLB 0x08 /* if split_tlb == 0 */
9+
#define ALT_COND_NO_IOC_FDC 0x10 /* if I/O cache does not need flushes */
10+
11+
#define INSN_PxTLB 0x02 /* modify pdtlb, pitlb */
12+
#define INSN_NOP 0x08000240 /* nop */
13+
14+
#ifndef __ASSEMBLY__
15+
16+
#include <linux/init.h>
17+
#include <linux/types.h>
18+
#include <linux/stddef.h>
19+
#include <linux/stringify.h>
20+
21+
struct alt_instr {
22+
s32 orig_offset; /* offset to original instructions */
23+
u32 len; /* end of original instructions */
24+
u32 cond; /* see ALT_COND_XXX */
25+
u32 replacement; /* replacement instruction or code */
26+
};
27+
28+
void set_kernel_text_rw(int enable_read_write);
29+
30+
/* Alternative SMP implementation. */
31+
#define ALTERNATIVE(cond, replacement) "!0:" \
32+
".section .altinstructions, \"aw\" !" \
33+
".word (0b-4-.), 1, " __stringify(cond) "," \
34+
__stringify(replacement) " !" \
35+
".previous"
36+
37+
#else
38+
39+
#define ALTERNATIVE(from, to, cond, replacement)\
40+
.section .altinstructions, "aw" ! \
41+
.word (from - .), (to - from)/4 ! \
42+
.word cond, replacement ! \
43+
.previous
44+
45+
#endif /* __ASSEMBLY__ */
46+
47+
#endif /* __ASM_PARISC_ALTERNATIVE_H */

‎arch/parisc/include/asm/cache.h‎

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

9+
#include <asm/alternative.h>
910

1011
/*
1112
* PA 2.0 processors have 64 and 128-byte L2 cachelines; PA 1.1 processors
@@ -41,9 +42,24 @@ extern int icache_stride;
4142
extern struct pdc_cache_info cache_info;
4243
void parisc_setup_cache_timing(void);
4344

44-
#define pdtlb(addr) asm volatile("pdtlb 0(%%sr1,%0)" : : "r" (addr));
45-
#define pitlb(addr) asm volatile("pitlb 0(%%sr1,%0)" : : "r" (addr));
46-
#define pdtlb_kernel(addr) asm volatile("pdtlb 0(%0)" : : "r" (addr));
45+
#define pdtlb(addr) asm volatile("pdtlb 0(%%sr1,%0)" \
46+
ALTERNATIVE(ALT_COND_NO_SMP, INSN_PxTLB) \
47+
: : "r" (addr))
48+
#define pitlb(addr) asm volatile("pitlb 0(%%sr1,%0)" \
49+
ALTERNATIVE(ALT_COND_NO_SMP, INSN_PxTLB) \
50+
ALTERNATIVE(ALT_COND_NO_SPLIT_TLB, INSN_NOP) \
51+
: : "r" (addr))
52+
#define pdtlb_kernel(addr) asm volatile("pdtlb 0(%0)" \
53+
ALTERNATIVE(ALT_COND_NO_SMP, INSN_PxTLB) \
54+
: : "r" (addr))
55+
56+
#define asm_io_fdc(addr) asm volatile("fdc %%r0(%0)" \
57+
ALTERNATIVE(ALT_COND_NO_DCACHE, INSN_NOP) \
58+
ALTERNATIVE(ALT_COND_NO_IOC_FDC, INSN_NOP) \
59+
: : "r" (addr))
60+
#define asm_io_sync() asm volatile("sync" \
61+
ALTERNATIVE(ALT_COND_NO_DCACHE, INSN_NOP) \
62+
ALTERNATIVE(ALT_COND_NO_IOC_FDC, INSN_NOP) :: )
4763

4864
#endif /* ! __ASSEMBLY__ */
4965

‎arch/parisc/include/asm/pgtable.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ static inline void purge_tlb_entries(struct mm_struct *mm, unsigned long addr)
4343
{
4444
mtsp(mm->context, 1);
4545
pdtlb(addr);
46-
if (unlikely(split_tlb))
47-
pitlb(addr);
46+
pitlb(addr);
4847
}
4948

5049
/* Certain architectures need to do special things when PTEs

‎arch/parisc/include/asm/sections.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
/* nothing to see, move along */
66
#include <asm-generic/sections.h>
77

8+
extern char __alt_instructions[], __alt_instructions_end[];
9+
810
#ifdef CONFIG_64BIT
911

1012
#define HAVE_DEREFERENCE_FUNCTION_DESCRIPTOR 1

‎arch/parisc/include/asm/tlbflush.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ static inline void flush_tlb_page(struct vm_area_struct *vma,
8585
purge_tlb_start(flags);
8686
mtsp(sid, 1);
8787
pdtlb(addr);
88-
if (unlikely(split_tlb))
89-
pitlb(addr);
88+
pitlb(addr);
9089
purge_tlb_end(flags);
9190
}
9291
#endif

‎arch/parisc/kernel/cache.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,18 +479,6 @@ int __flush_tlb_range(unsigned long sid, unsigned long start,
479479
/* Purge TLB entries for small ranges using the pdtlb and
480480
pitlb instructions. These instructions execute locally
481481
but cause a purge request to be broadcast to other TLBs. */
482-
if (likely(!split_tlb)) {
483-
while (start < end) {
484-
purge_tlb_start(flags);
485-
mtsp(sid, 1);
486-
pdtlb(start);
487-
purge_tlb_end(flags);
488-
start += PAGE_SIZE;
489-
}
490-
return 0;
491-
}
492-
493-
/* split TLB case */
494482
while (start < end) {
495483
purge_tlb_start(flags);
496484
mtsp(sid, 1);

‎arch/parisc/kernel/entry.S‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <asm/ldcw.h>
3939
#include <asm/traps.h>
4040
#include <asm/thread_info.h>
41+
#include <asm/alternative.h>
4142

4243
#include <linux/linkage.h>
4344

@@ -464,7 +465,7 @@
464465
/* Acquire pa_tlb_lock lock and check page is present. */
465466
.macro tlb_lock spc,ptp,pte,tmp,tmp1,fault
466467
#ifdef CONFIG_SMP
467-
cmpib,COND(=),n 0,\spc,2f
468+
98: cmpib,COND(=),n 0,\spc,2f
468469
load_pa_tlb_lock \tmp
469470
1: LDCW 0(\tmp),\tmp1
470471
cmpib,COND(=) 0,\tmp1,1b
@@ -473,6 +474,7 @@
473474
bb,<,n \pte,_PAGE_PRESENT_BIT,3f
474475
b \fault
475476
stw,ma \spc,0(\tmp)
477+
99: ALTERNATIVE(98b, 99b, ALT_COND_NO_SMP, INSN_NOP)
476478
#endif
477479
2: LDREG 0(\ptp),\pte
478480
bb,>=,n \pte,_PAGE_PRESENT_BIT,\fault
@@ -482,15 +484,17 @@
482484
/* Release pa_tlb_lock lock without reloading lock address. */
483485
.macro tlb_unlock0 spc,tmp
484486
#ifdef CONFIG_SMP
485-
or,COND(=) %r0,\spc,%r0
487+
98: or,COND(=) %r0,\spc,%r0
486488
stw,ma \spc,0(\tmp)
489+
99: ALTERNATIVE(98b, 99b, ALT_COND_NO_SMP, INSN_NOP)
487490
#endif
488491
.endm
489492

490493
/* Release pa_tlb_lock lock. */
491494
.macro tlb_unlock1 spc,tmp
492495
#ifdef CONFIG_SMP
493-
load_pa_tlb_lock \tmp
496+
98: load_pa_tlb_lock \tmp
497+
99: ALTERNATIVE(98b, 99b, ALT_COND_NO_SMP, INSN_NOP)
494498
tlb_unlock0 \spc,\tmp
495499
#endif
496500
.endm

0 commit comments

Comments
 (0)