Skip to contentSkip to conversation

csky: Fixup init_fpu compile warning with __init

PATCH New [email protected] 1 participant 11 messages Apr 3, 2020

Subsystems (4)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 1/11 csky: Fixup init_fpu compile warning with __init

From: Guo Ren <[email protected]>

WARNING: vmlinux.o(.text+0x2366): Section mismatch in reference from the function csky_start_secondary() to the function .init.text:init_fpu()

The function csky_start_secondary() references the function __init init_fpu().
This is often because csky_start_secondary lacks a __init annotation or the annotation of init_fpu is wrong.

---
 arch/csky/abiv2/fpu.c         | 5 -----
 arch/csky/abiv2/inc/abi/fpu.h | 3 ++-
 arch/csky/kernel/smp.c        | 3 +++
 3 files changed, 5 insertions(+), 6 deletions(-)
arch/csky/abiv2/fpu.c +0 -5
index 86d187d4e5af..5acc5c2e544e 100644
@@ -10,11 +10,6 @@
 #define MTCR_DIST	0xC0006420
 #define MFCR_DIST	0xC0006020
 
-void __init init_fpu(void)
-{
-	mtcr("cr<1, 2>", 0);
-}
-
 /*
  * fpu_libc_helper() is to help libc to excute:
  *  - mfcr %a, cr<1, 2>
arch/csky/abiv2/inc/abi/fpu.h +2 -1
index 22ca3cf2794a..09e2700a3693 100644
@@ -9,7 +9,8 @@
 
 int fpu_libc_helper(struct pt_regs *regs);
 void fpu_fpe(struct pt_regs *regs);
-void __init init_fpu(void);
+
+static inline void init_fpu(void) { mtcr("cr<1, 2>", 0); }
 
 void save_to_user_fp(struct user_fp *user_fp);
 void restore_from_user_fp(struct user_fp *user_fp);
arch/csky/kernel/smp.c +3 -0
index 0bb0954d5570..df2e2174dbd0 100644
@@ -22,6 +22,9 @@
 #include <asm/sections.h>
 #include <asm/mmu_context.h>
 #include <asm/pgalloc.h>
+#ifdef CONFIG_CPU_HAS_FPU
+#include <abi/fpu.h>
+#endif
 
 struct ipi_data_struct {
 	unsigned long bits ____cacheline_aligned;
Signature · 1 line
2.17.0
author maintainer
#1 lore raw

PATCH 2/11 csky: Implement ptrace regs and stack API

From: Guo Ren <[email protected]>

Needed for kprobes support. Copied and adapted from Patrick's patch, but it has been modified for csky's pt_regs.

ref:
https://lore.kernel.org/linux-riscv/[email protected]/raw

Cc Patrick Staehlin <[email protected]>
---
 arch/csky/Kconfig              |   1 +
 arch/csky/include/asm/ptrace.h |  41 +++++++++++++
 arch/csky/kernel/ptrace.c      | 103 +++++++++++++++++++++++++++++++++
 3 files changed, 145 insertions(+)
arch/csky/Kconfig +1 -0
index 047427f71d83..2dfd3fb07db9 100644
@@ -48,6 +48,7 @@ config CSKY
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_DMA_CONTIGUOUS
+	select HAVE_REGS_AND_STACK_ACCESS_API
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select MAY_HAVE_SPARSE_IRQ
arch/csky/include/asm/ptrace.h +41 -0

58 lines not on this page · raw message

arch/csky/kernel/ptrace.c +103 -0

109 lines not on this page · raw message

Signature · 1 line
2.17.0
author maintainer
#2 lore raw

PATCH 3/11 csky: Add support for restartable sequence

In reply to [email protected]

From: Guo Ren <[email protected]>

Copied and adapted from vincent's patch, but modified for csky.

ref:
https://lore.kernel.org/linux-riscv/[email protected]/raw

Add calls to rseq_signal_deliver(), rseq_handle_notify_resume() and rseq_syscall() to introduce RSEQ support.

1. Call the rseq_handle_notify_resume() function on return to userspace if TIF_NOTIFY_RESUME thread flag is set.

2. Call the rseq_signal_deliver() function to fixup on the pre-signal frame when a signal is delivered on top of a restartable sequence critical section.

3. Check that system calls are not invoked from within rseq critical sections by invoking rseq_signal() from ret_from_syscall(). With CONFIG_DEBUG_RSEQ, such behavior results in termination of the process with SIGSEGV.

---
 arch/csky/Kconfig         | 1 +
 arch/csky/kernel/entry.S  | 5 ++++-
 arch/csky/kernel/signal.c | 3 +++
 3 files changed, 8 insertions(+), 1 deletion(-)
arch/csky/Kconfig +1 -0
index 2dfd3fb07db9..0e9b14367fa8 100644
@@ -49,6 +49,7 @@ config CSKY
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_DMA_CONTIGUOUS
 	select HAVE_REGS_AND_STACK_ACCESS_API
+	select HAVE_RSEQ
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select MAY_HAVE_SPARSE_IRQ
arch/csky/kernel/entry.S +4 -1
index 007706328000..0bde1d9fd277 100644
@@ -128,7 +128,10 @@ tlbop_end 1
 ENTRY(csky_systemcall)
 	SAVE_ALL TRAP0_SIZE
 	zero_fp
-
+#ifdef CONFIG_RSEQ_DEBUG
+	mov	a0, sp
+	jbsr	rseq_syscall
+#endif
 	psrset  ee, ie
 
 	lrw     r11, __NR_syscalls
arch/csky/kernel/signal.c +3 -0
index 9b1b7c039ddf..3d6b728cbb72 100644
@@ -175,6 +175,8 @@ static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
 	sigset_t *oldset = sigmask_to_save();
 	int ret;
 
+	rseq_signal_deliver(ksig, regs);
+
 	/* Are we from a system call? */
 	if (in_syscall(regs)) {
 		/* Avoid additional syscall restarting via ret_from_exception */
@@ -258,5 +260,6 @@ asmlinkage void do_notify_resume(struct pt_regs *regs,
 	if (thread_info_flags & _TIF_NOTIFY_RESUME) {
 		clear_thread_flag(TIF_NOTIFY_RESUME);
 		tracehook_notify_resume(regs);
+		rseq_handle_notify_resume(NULL, regs);
 	}
 }
Signature · 1 line
2.17.0
author maintainer
#3 lore raw

PATCH 4/11 csky: Implement ftrace with regs

In reply to [email protected]

From: Guo Ren <[email protected]>

This patch implements FTRACE_WITH_REGS for csky, which allows a traced function's arguments (and some other registers) to be captured into a struct pt_regs, allowing these to be inspected and/or modified.

---
 arch/csky/Kconfig               |  1 +
 arch/csky/abiv2/inc/abi/entry.h | 60 +++++++++++++++++++++++++++++++++
 arch/csky/abiv2/mcount.S        | 48 ++++++++++++++++++++++++++
 arch/csky/include/asm/ftrace.h  |  2 ++
 arch/csky/kernel/asm-offsets.c  |  1 +
 arch/csky/kernel/ftrace.c       | 11 ++++++
 6 files changed, 123 insertions(+)
arch/csky/Kconfig +1 -0
index 0e9b14367fa8..6cf169a15794 100644
@@ -38,6 +38,7 @@ config CSKY
 	select HAVE_ARCH_AUDITSYSCALL
 	select HAVE_COPY_THREAD_TLS
 	select HAVE_DYNAMIC_FTRACE
+	select HAVE_DYNAMIC_FTRACE_WITH_REGS
 	select HAVE_FUNCTION_TRACER
 	select HAVE_FUNCTION_GRAPH_TRACER
 	select HAVE_FTRACE_MCOUNT_RECORD
arch/csky/abiv2/inc/abi/entry.h +60 -0

66 lines not on this page · raw message

arch/csky/abiv2/mcount.S +48 -0

69 lines not on this page · raw message

arch/csky/include/asm/ftrace.h +2 -0
index ba35d93ecda2..fae72b0b1374 100644
@@ -10,6 +10,8 @@
 
 #define HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
 
+#define ARCH_SUPPORTS_FTRACE_OPS 1
+
 #define MCOUNT_ADDR	((unsigned long)_mcount)
 
 #ifndef __ASSEMBLY__
arch/csky/kernel/asm-offsets.c +1 -0
index 9b48b1b1a61b..f8be348df9e4 100644
@@ -72,6 +72,7 @@ int main(void)
 	DEFINE(PT_RLO,            offsetof(struct pt_regs, rlo));
 #endif
 	DEFINE(PT_USP,            offsetof(struct pt_regs, usp));
+	DEFINE(PT_FRAME_SIZE,     sizeof(struct pt_regs));
 
 	/* offsets into the irq_cpustat_t struct */
 	DEFINE(CPUSTAT_SOFTIRQ_PENDING, offsetof(irq_cpustat_t,
arch/csky/kernel/ftrace.c +11 -0
index 44f4880179b7..b4502cd2eabe 100644
@@ -126,6 +126,9 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
 {
 	int ret = ftrace_modify_code((unsigned long)&ftrace_call,
 				(unsigned long)func, true, true);
+	if (!ret)
+		ret = ftrace_modify_code((unsigned long)&ftrace_regs_call,
+				(unsigned long)func, true, true);
 	return ret;
 }
 
@@ -135,6 +138,14 @@ int __init ftrace_dyn_arch_init(void)
 }
 #endif /* CONFIG_DYNAMIC_FTRACE */
 
+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
+int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
+		       unsigned long addr)
+{
+	return ftrace_modify_code(rec->ip, addr, true, true);
+}
+#endif
+
 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
 void prepare_ftrace_return(unsigned long *parent, unsigned long self_addr,
 			   unsigned long frame_pointer)
Signature · 1 line
2.17.0
author maintainer
#4 lore raw

PATCH 5/11 csky/ftrace: Fixup ftrace_modify_code deadlock without CPU_HAS_ICACHE_INS

In reply to [email protected]

From: Guo Ren <[email protected]>

If ICACHE_INS is not supported, we use IPI to sync icache on each core. But ftrace_modify_code is called from stop_machine from default implementation of arch_ftrace_update_code and stop_machine callback is irq_disabled. When you call ipi with irq_disabled, a deadlock will happen.

We couldn't use icache_flush with irq_disabled, but startup make_nop is specific case and it needn't ipi other cores.

---
 arch/csky/kernel/ftrace.c | 31 +++++++++++++++++++++++++++
 arch/csky/mm/cachev2.c    | 45 +++++++++++++++++++++++++++++++++------
 2 files changed, 70 insertions(+), 6 deletions(-)
arch/csky/kernel/ftrace.c +31 -0
index b4502cd2eabe..44628e3f7fa6 100644
@@ -3,6 +3,7 @@
 
 #include <linux/ftrace.h>
 #include <linux/uaccess.h>
+#include <linux/stop_machine.h>
 #include <asm/cacheflush.h>
 
 #ifdef CONFIG_DYNAMIC_FTRACE
@@ -201,5 +202,35 @@ int ftrace_disable_ftrace_graph_caller(void)
 #endif /* CONFIG_DYNAMIC_FTRACE */
 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
 
+#ifndef CONFIG_CPU_HAS_ICACHE_INS
+struct ftrace_modify_param {
+	int command;
+	atomic_t cpu_count;
+};
+
+static int __ftrace_modify_code(void *data)
+{
+	struct ftrace_modify_param *param = data;
+
+	if (atomic_inc_return(&param->cpu_count) == 1) {
+		ftrace_modify_all_code(param->command);
+		atomic_inc(&param->cpu_count);
+	} else {
+		while (atomic_read(&param->cpu_count) <= num_online_cpus())
+			cpu_relax();
+		local_icache_inv_all(NULL);
+	}
+
+	return 0;
+}
+
+void arch_ftrace_update_code(int command)
+{
+	struct ftrace_modify_param param = { command, ATOMIC_INIT(0) };
+
+	stop_machine(__ftrace_modify_code, &param, cpu_online_mask);
+}
+#endif
+
 /* _mcount is defined in abi's mcount.S */
 EXPORT_SYMBOL(_mcount);
arch/csky/mm/cachev2.c +39 -6

67 lines not on this page · raw message

Signature · 1 line
2.17.0
author maintainer
#7 lore raw

PATCH 8/11 csky: Enable LOCKDEP_SUPPORT

In reply to [email protected]

From: Guo Ren <[email protected]>

Lockdep is needed by proving the spinlocks and rwlocks. Currently, we only put trace_hardirqs_on/off with csky_irq and ret_from_exception.

---
 arch/csky/Kconfig        |  3 +++
 arch/csky/kernel/entry.S | 11 +++++++++++
 2 files changed, 14 insertions(+)
arch/csky/Kconfig +3 -0
index a75b8e1fef1d..998188f44fd7 100644
@@ -69,6 +69,9 @@ config CSKY
 	select PCI_SYSCALL if PCI
 	select PCI_MSI if PCI
 
+config LOCKDEP_SUPPORT
+	def_bool y
+
 config CPU_HAS_CACHEV2
 	bool
 
arch/csky/kernel/entry.S +11 -0
index 0bde1d9fd277..8b040a1b11d0 100644
@@ -225,6 +225,13 @@ ret_from_exception:
 	cmpnei	r12, 0
 	bt	exit_work
 1:
+#ifdef CONFIG_TRACE_IRQFLAGS
+	ld	r10, (sp, LSAVE_PSR)
+	btsti	r10, 6
+	bf	2f
+	jbsr	trace_hardirqs_on
+2:
+#endif
 	RESTORE_ALL
 
 exit_work:
@@ -280,6 +287,10 @@ ENTRY(csky_irq)
 	zero_fp
 	psrset	ee
 
+#ifdef CONFIG_TRACE_IRQFLAGS
+	jbsr	trace_hardirqs_off
+#endif
+
 #ifdef CONFIG_PREEMPTION
 	mov	r9, sp			/* Get current stack  pointer */
 	bmaski	r10, THREAD_SHIFT
Signature · 1 line
2.17.0
author maintainer
#6 lore raw

PATCH 7/11 csky: Enable the gcov function

In reply to [email protected]

From: Ma Jun <[email protected]>

Support the gcov function in csky architecture.

---
 arch/csky/Kconfig | 1 +
 1 file changed, 1 insertion(+)
arch/csky/Kconfig +1 -0
index 6cf169a15794..a75b8e1fef1d 100644
@@ -3,6 +3,7 @@ config CSKY
 	def_bool y
 	select ARCH_32BIT_OFF_T
 	select ARCH_HAS_DMA_PREP_COHERENT
+	select ARCH_HAS_GCOV_PROFILE_ALL
 	select ARCH_HAS_SYNC_DMA_FOR_CPU
 	select ARCH_HAS_SYNC_DMA_FOR_DEVICE
 	select ARCH_USE_BUILTIN_BSWAP
Signature · 1 line
2.17.0
author maintainer
#8 lore raw

PATCH 9/11 csky: Add kprobes supported

In reply to [email protected]

From: Guo Ren <[email protected]>

This patch enable kprobes, kretprobes, ftrace interface. It utilized software breakpoint and single step debug exceptions, instructions simulation on csky.

We use USR_BKPT replace origin instruction, and the kprobe handler prepares an excutable memory slot for out-of-line execution with a copy of the original instruction being probed. Most of instructions could be executed by single-step, but some instructions need origin pc value to execute and we need software simulate these instructions.

Cc Arnd Bergmann <[email protected]>
Cc Steven Rostedt (VMware) <[email protected]>
---
 arch/csky/Kconfig                            |   3 +
 arch/csky/include/asm/Kbuild                 |   1 -
 arch/csky/include/asm/kprobes.h              |  48 ++
 arch/csky/include/asm/probes.h               |  24 +
 arch/csky/include/asm/ptrace.h               |   2 +
 arch/csky/kernel/Makefile                    |   1 +
 arch/csky/kernel/probes/Makefile             |   6 +
 arch/csky/kernel/probes/decode-insn.c        |  49 ++
 arch/csky/kernel/probes/decode-insn.h        |  20 +
 arch/csky/kernel/probes/ftrace.c             |  66 +++
 arch/csky/kernel/probes/kprobes.c            | 499 +++++++++++++++++++
 arch/csky/kernel/probes/kprobes_trampoline.S |  19 +
 arch/csky/kernel/probes/simulate-insn.c      | 398 +++++++++++++++
 arch/csky/kernel/probes/simulate-insn.h      |  49 ++
 arch/csky/kernel/traps.c                     |  10 +-
 arch/csky/mm/fault.c                         |   4 +
 16 files changed, 1197 insertions(+), 2 deletions(-)
 create mode 100644 arch/csky/include/asm/kprobes.h
 create mode 100644 arch/csky/include/asm/probes.h
 create mode 100644 arch/csky/kernel/probes/Makefile
 create mode 100644 arch/csky/kernel/probes/decode-insn.c
 create mode 100644 arch/csky/kernel/probes/decode-insn.h
 create mode 100644 arch/csky/kernel/probes/ftrace.c
 create mode 100644 arch/csky/kernel/probes/kprobes.c
 create mode 100644 arch/csky/kernel/probes/kprobes_trampoline.S
 create mode 100644 arch/csky/kernel/probes/simulate-insn.c
 create mode 100644 arch/csky/kernel/probes/simulate-insn.h
arch/csky/Kconfig +3 -0
index 998188f44fd7..f76c333758ca 100644
@@ -46,6 +46,9 @@ config CSKY
 	select HAVE_KERNEL_GZIP
 	select HAVE_KERNEL_LZO
 	select HAVE_KERNEL_LZMA
+	select HAVE_KPROBES if !CPU_CK610
+	select HAVE_KPROBES_ON_FTRACE if !CPU_CK610
+	select HAVE_KRETPROBES if !CPU_CK610
 	select HAVE_PERF_EVENTS
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
arch/csky/include/asm/Kbuild +0 -1
index 4130e3eaa766..59d3186578fd 100644
@@ -20,7 +20,6 @@ generic-y += irq_regs.h
 generic-y += irq_work.h
 generic-y += kdebug.h
 generic-y += kmap_types.h
-generic-y += kprobes.h
 generic-y += kvm_para.h
 generic-y += linkage.h
 generic-y += local.h
arch/csky/include/asm/kprobes.h new file +48 -0

48 lines not on this page · raw message

arch/csky/include/asm/probes.h new file +24 -0
new file mode 100644
index 000000000000..5e526334e6f9
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef __ASM_CSKY_PROBES_H
+#define __ASM_CSKY_PROBES_H
+
+typedef u32 probe_opcode_t;
+typedef void (probes_handler_t) (u32 opcode, long addr, struct pt_regs *);
+
+/* architecture specific copy of original instruction */
+struct arch_probe_insn {
+	probe_opcode_t *insn;
+	probes_handler_t *handler;
+	/* restore address after simulation */
+	unsigned long restore;
+};
+
+#ifdef CONFIG_KPROBES
+typedef u32 kprobe_opcode_t;
+struct arch_specific_insn {
+	struct arch_probe_insn api;
+};
+#endif
+
+#endif /* __ASM_CSKY_PROBES_H */
arch/csky/include/asm/ptrace.h +2 -0
index f16ca256e007..aae5aa96cf54 100644
@@ -13,6 +13,8 @@
 
 #define PS_S	0x80000000 /* Supervisor Mode */
 
+#define USR_BKPT	0x1464
+
 #define arch_has_single_step() (1)
 #define current_pt_regs() \
 ({ (struct pt_regs *)((char *)current_thread_info() + THREAD_SIZE) - 1; })
arch/csky/kernel/Makefile +1 -0
index 071d659f37b7..fd6d9dc8b7f3 100644
@@ -4,6 +4,7 @@ extra-y := head.o vmlinux.lds
 obj-y += entry.o atomic.o signal.o traps.o irq.o time.o vdso.o
 obj-y += power.o syscall.o syscall_table.o setup.o
 obj-y += process.o cpu-probe.o ptrace.o dumpstack.o
+obj-y += probes/
 
 obj-$(CONFIG_MODULES)			+= module.o
 obj-$(CONFIG_SMP)			+= smp.o
arch/csky/kernel/probes/Makefile new file +6 -0
new file mode 100644
index 000000000000..3a857d081958
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_KPROBES)		+= kprobes.o decode-insn.o simulate-insn.o
+obj-$(CONFIG_KPROBES)		+= kprobes_trampoline.o
+obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
+
+CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
arch/csky/kernel/probes/decode-insn.c new file +49 -0

49 lines not on this page · raw message

arch/csky/kernel/probes/decode-insn.h new file +20 -0
new file mode 100644
index 000000000000..9c4ad48fee0d
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#ifndef __CSKY_KERNEL_KPROBES_DECODE_INSN_H
+#define __CSKY_KERNEL_KPROBES_DECODE_INSN_H
+
+#include <asm/sections.h>
+#include <asm/kprobes.h>
+
+enum probe_insn {
+	INSN_REJECTED,
+	INSN_GOOD_NO_SLOT,
+	INSN_GOOD,
+};
+
+#define is_insn32(insn) ((insn & 0xc000) == 0xc000)
+
+enum probe_insn __kprobes
+csky_probe_decode_insn(probe_opcode_t *addr, struct arch_probe_insn *asi);
+
+#endif /* __CSKY_KERNEL_KPROBES_DECODE_INSN_H */
arch/csky/kernel/probes/ftrace.c new file +66 -0

66 lines not on this page · raw message

arch/csky/kernel/probes/kprobes.c new file +499 -0

499 lines not on this page · raw message

arch/csky/kernel/probes/kprobes_trampoline.S new file +19 -0
new file mode 100644
index 000000000000..b1fe3af24f03
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+
+#include <linux/linkage.h>
+
+#include <abi/entry.h>
+
+ENTRY(kretprobe_trampoline)
+	SAVE_REGS_FTRACE
+
+	mov	a0, sp /* pt_regs */
+
+	jbsr	trampoline_probe_handler
+
+	/* use the result as the return-address */
+	mov	lr, a0
+
+	RESTORE_REGS_FTRACE
+	rts
+ENDPROC(kretprobe_trampoline)
arch/csky/kernel/probes/simulate-insn.c new file +398 -0

398 lines not on this page · raw message

arch/csky/kernel/probes/simulate-insn.h new file +49 -0

49 lines not on this page · raw message

arch/csky/kernel/traps.c +9 -1
index 63715cb90ee9..c6a40806977f 100644
@@ -14,6 +14,7 @@
 #include <linux/kallsyms.h>
 #include <linux/rtc.h>
 #include <linux/uaccess.h>
+#include <linux/kprobes.h>
 
 #include <asm/setup.h>
 #include <asm/traps.h>
@@ -109,7 +110,6 @@ void buserr(struct pt_regs *regs)
 	force_sig_fault(SIGSEGV, 0, (void __user *)regs->pc);
 }
 
-#define USR_BKPT 0x1464
 asmlinkage void trap_c(struct pt_regs *regs)
 {
 	int sig;
@@ -126,11 +126,19 @@ asmlinkage void trap_c(struct pt_regs *regs)
 		break;
 	/* ptrace */
 	case VEC_TRACE:
+#ifdef CONFIG_KPROBES
+		if (kprobe_single_step_handler(regs))
+			return;
+#endif
 		info.si_code = TRAP_TRACE;
 		sig = SIGTRAP;
 		break;
 	case VEC_ILLEGAL:
 		tsk->thread.trap_no = vector;
+#ifdef CONFIG_KPROBES
+		if (kprobe_breakpoint_handler(regs))
+			return;
+#endif
 		die_if_kernel("Kernel mode ILLEGAL", regs, vector);
 #ifndef CONFIG_CPU_NO_USER_BKPT
 		if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT)
arch/csky/mm/fault.c +4 -0
index 562c7f708749..d3c61b83e195 100644
@@ -18,6 +18,7 @@
 #include <linux/extable.h>
 #include <linux/uaccess.h>
 #include <linux/perf_event.h>
+#include <linux/kprobes.h>
 
 #include <asm/hardirq.h>
 #include <asm/mmu_context.h>
@@ -53,6 +54,9 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
 	int fault;
 	unsigned long address = mmu_meh & PAGE_MASK;
 
+	if (kprobe_page_fault(regs, tsk->thread.trap_no))
+		return;
+
 	si_code = SEGV_MAPERR;
 
 #ifndef CONFIG_CPU_HAS_TLBI
Signature · 1 line
2.17.0
author maintainer
#9 lore raw

PATCH 10/11 csky: Add uprobes support

In reply to [email protected]

From: Guo Ren <[email protected]>

This patch adds support for uprobes on csky architecture.

Just like kprobe, it support single-step and simulate instructions.

Cc Arnd Bergmann <[email protected]>
Cc Steven Rostedt (VMware) <[email protected]>
---
 arch/csky/Kconfig                   |   3 +
 arch/csky/include/asm/thread_info.h |   2 +
 arch/csky/include/asm/uprobes.h     |  33 ++++++
 arch/csky/kernel/entry.S            |   2 +-
 arch/csky/kernel/probes/Makefile    |   1 +
 arch/csky/kernel/probes/uprobes.c   | 150 ++++++++++++++++++++++++++++
 arch/csky/kernel/signal.c           |   3 +
 arch/csky/kernel/traps.c            |   8 ++
 8 files changed, 201 insertions(+), 1 deletion(-)
 create mode 100644 arch/csky/include/asm/uprobes.h
 create mode 100644 arch/csky/kernel/probes/uprobes.c
arch/csky/Kconfig +3 -0
index f76c333758ca..94545d50d40f 100644
@@ -75,6 +75,9 @@ config CSKY
 config LOCKDEP_SUPPORT
 	def_bool y
 
+config ARCH_SUPPORTS_UPROBES
+	def_bool y if !CPU_CK610
+
 config CPU_HAS_CACHEV2
 	bool
 
arch/csky/include/asm/thread_info.h +2 -0
index 0b546a55a8bf..442fedad0260 100644
@@ -57,6 +57,7 @@ static inline struct thread_info *current_thread_info(void)
 #define TIF_SYSCALL_TRACE	3	/* syscall trace active */
 #define TIF_SYSCALL_TRACEPOINT	4       /* syscall tracepoint instrumentation */
 #define TIF_SYSCALL_AUDIT	5	/* syscall auditing */
+#define TIF_UPROBE		6	/* uprobe breakpoint or singlestep */
 #define TIF_POLLING_NRFLAG	16	/* poll_idle() is TIF_NEED_RESCHED */
 #define TIF_MEMDIE		18      /* is terminating due to OOM killer */
 #define TIF_RESTORE_SIGMASK	20	/* restore signal mask in do_signal() */
@@ -68,6 +69,7 @@ static inline struct thread_info *current_thread_info(void)
 #define _TIF_SYSCALL_TRACE	(1 << TIF_SYSCALL_TRACE)
 #define _TIF_SYSCALL_TRACEPOINT	(1 << TIF_SYSCALL_TRACEPOINT)
 #define _TIF_SYSCALL_AUDIT	(1 << TIF_SYSCALL_AUDIT)
+#define _TIF_UPROBE		(1 << TIF_UPROBE)
 #define _TIF_POLLING_NRFLAG	(1 << TIF_POLLING_NRFLAG)
 #define _TIF_MEMDIE		(1 << TIF_MEMDIE)
 #define _TIF_RESTORE_SIGMASK	(1 << TIF_RESTORE_SIGMASK)
arch/csky/include/asm/uprobes.h new file +33 -0
new file mode 100644
index 000000000000..600388eb93c6
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef __ASM_CSKY_UPROBES_H
+#define __ASM_CSKY_UPROBES_H
+
+#include <asm/probes.h>
+
+#define MAX_UINSN_BYTES		4
+
+#define UPROBE_SWBP_INSN	USR_BKPT
+#define UPROBE_SWBP_INSN_SIZE	2
+#define UPROBE_XOL_SLOT_BYTES	MAX_UINSN_BYTES
+
+typedef u32 uprobe_opcode_t;
+
+struct arch_uprobe_task {
+	unsigned long   saved_trap_no;
+};
+
+struct arch_uprobe {
+	union {
+		u8 insn[MAX_UINSN_BYTES];
+		u8 ixol[MAX_UINSN_BYTES];
+	};
+	struct arch_probe_insn api;
+	unsigned long insn_size;
+	bool simulate;
+};
+
+int uprobe_breakpoint_handler(struct pt_regs *regs);
+int uprobe_single_step_handler(struct pt_regs *regs);
+
+#endif /* __ASM_CSKY_UPROBES_H */
arch/csky/kernel/entry.S +1 -1
index 8b040a1b11d0..364819536f2e 100644
@@ -221,7 +221,7 @@ ret_from_exception:
 	andn	r9, r10
 
 	ldw	r12, (r9, TINFO_FLAGS)
-	andi	r12, (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NEED_RESCHED)
+	andi	r12, (_TIF_SIGPENDING | _TIF_NOTIFY_RESUME | _TIF_NEED_RESCHED | _TIF_UPROBE)
 	cmpnei	r12, 0
 	bt	exit_work
 1:
arch/csky/kernel/probes/Makefile +1 -0
index 3a857d081958..1c7c6e6cb25b 100644
@@ -2,5 +2,6 @@
 obj-$(CONFIG_KPROBES)		+= kprobes.o decode-insn.o simulate-insn.o
 obj-$(CONFIG_KPROBES)		+= kprobes_trampoline.o
 obj-$(CONFIG_KPROBES_ON_FTRACE)	+= ftrace.o
+obj-$(CONFIG_UPROBES)		+= uprobes.o decode-insn.o simulate-insn.o
 
 CFLAGS_REMOVE_simulate-insn.o = $(CC_FLAGS_FTRACE)
arch/csky/kernel/probes/uprobes.c new file +150 -0

150 lines not on this page · raw message

arch/csky/kernel/signal.c +3 -0
index 3d6b728cbb72..9452d6570b7e 100644
@@ -253,6 +253,9 @@ static void do_signal(struct pt_regs *regs)
 asmlinkage void do_notify_resume(struct pt_regs *regs,
 	unsigned long thread_info_flags)
 {
+	if (thread_info_flags & _TIF_UPROBE)
+		uprobe_notify_resume(regs);
+
 	/* Handle pending signal delivery */
 	if (thread_info_flags & _TIF_SIGPENDING)
 		do_signal(regs);
arch/csky/kernel/traps.c +8 -0
index c6a40806977f..fcc3a69831ad 100644
@@ -129,6 +129,10 @@ asmlinkage void trap_c(struct pt_regs *regs)
 #ifdef CONFIG_KPROBES
 		if (kprobe_single_step_handler(regs))
 			return;
+#endif
+#ifdef CONFIG_UPROBES
+		if (uprobe_single_step_handler(regs))
+			return;
 #endif
 		info.si_code = TRAP_TRACE;
 		sig = SIGTRAP;
@@ -138,6 +142,10 @@ asmlinkage void trap_c(struct pt_regs *regs)
 #ifdef CONFIG_KPROBES
 		if (kprobe_breakpoint_handler(regs))
 			return;
+#endif
+#ifdef CONFIG_UPROBES
+		if (uprobe_breakpoint_handler(regs))
+			return;
 #endif
 		die_if_kernel("Kernel mode ILLEGAL", regs, vector);
 #ifndef CONFIG_CPU_NO_USER_BKPT
Signature · 1 line
2.17.0
author maintainer
#10 lore raw

PATCH 11/11 csky: Fixup cpu speculative execution to IO area

In reply to [email protected]

From: Guo Ren <[email protected]>

For the memory size ( > 512MB, < 1GB), the MSA setting is:

- SSEG0: PHY_START , PHY_START + 512MB
- SSEG1: PHY_START + 512MB, PHY_START + 1GB

But the real memory is no more than 1GB, there is a gap between the end size of memory and border of 1GB. CPU could speculatively execute to that gap and if the gap of the bus couldn't respond to the CPU request, then the crash will happen.

Now make the setting with:

- SSEG0: PHY_START , PHY_START + 512MB (no change)
- SSEG1: Disabled (We use highmem to use the memory of 512MB~1GB)

We also deprecated zhole_szie[] settings, it's only used by arm style CPUs. All memory gap should use Reserved setting of dts in csky system.

---
 arch/csky/abiv1/inc/abi/entry.h |  5 +--
 arch/csky/abiv2/inc/abi/entry.h |  7 ++--
 arch/csky/kernel/head.S         |  5 +++
 arch/csky/kernel/setup.c        | 63 ++++++++-------------------------
 arch/csky/kernel/smp.c          |  3 ++
 5 files changed, 25 insertions(+), 58 deletions(-)
arch/csky/abiv1/inc/abi/entry.h +1 -4
index f35a9f3315ee..5056ebb902d1 100644
@@ -172,10 +172,7 @@
 	addi	r6, 0xe
 	cpwcr	r6, cpcr30
 
-	lsri	r6, 28
-	addi	r6, 2
-	lsli	r6, 28
-	addi	r6, 0xe
+	movi	r6, 0
 	cpwcr	r6, cpcr31
 .endm
 
arch/csky/abiv2/inc/abi/entry.h +2 -5
index 6a404393d0f4..a99aff555a0a 100644
@@ -290,11 +290,8 @@
 	addi	r6, 0x1ce
 	mtcr	r6, cr<30, 15> /* Set MSA0 */
 
-	lsri	r6, 28
-	addi	r6, 2
-	lsli	r6, 28
-	addi	r6, 0x1ce
-	mtcr	r6, cr<31, 15> /* Set MSA1 */
+	movi    r6, 0
+	mtcr	r6, cr<31, 15> /* Clr MSA1 */
 
 	/* enable MMU */
 	mfcr    r6, cr18
arch/csky/kernel/head.S +5 -0
index 61989f9241c0..17ed9d250480 100644
@@ -21,6 +21,11 @@ END(_start)
 ENTRY(_start_smp_secondary)
 	SETUP_MMU
 
+	/* copy msa1 from CPU0 */
+	lrw     r6, secondary_msa1
+	ld.w	r6, (r6, 0)
+	mtcr	r6, cr<31, 15>
+
 	/* set stack point */
 	lrw     r6, secondary_stack
 	ld.w	r6, (r6, 0)
arch/csky/kernel/setup.c +14 -49

94 lines not on this page · raw message

arch/csky/kernel/smp.c +3 -0
index df2e2174dbd0..b5c5bc3afeb5 100644
@@ -159,6 +159,8 @@ volatile unsigned int secondary_hint;
 volatile unsigned int secondary_ccr;
 volatile unsigned int secondary_stack;
 
+unsigned long secondary_msa1;
+
 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 {
 	unsigned long mask = 1 << cpu;
@@ -167,6 +169,7 @@ int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 		(unsigned int) task_stack_page(tidle) + THREAD_SIZE - 8;
 	secondary_hint = mfcr("cr31");
 	secondary_ccr  = mfcr("cr18");
+	secondary_msa1 = read_mmu_msa1();
 
 	/*
 	 * Because other CPUs are in reset status, we must flush data
Signature · 1 line
2.17.0
author maintainer
#5 lore raw

PATCH 6/11 csky: Fixup get wrong psr value from phyical reg

In reply to [email protected]

From: Guo Ren <[email protected]>

We should get psr value from regs->psr in stack, not directly get it from phyiscal register then save the vector number in tsk->trap_no.

---
 arch/csky/include/asm/processor.h |  1 +
 arch/csky/kernel/traps.c          | 11 ++++++++++-
 arch/csky/mm/fault.c              |  7 +++++++
 3 files changed, 18 insertions(+), 1 deletion(-)
arch/csky/include/asm/processor.h +1 -0
index 21e0bd5293dd..c6bcd7f7c720 100644
@@ -43,6 +43,7 @@ extern struct cpuinfo_csky cpu_data[];
 struct thread_struct {
 	unsigned long  ksp;       /* kernel stack pointer */
 	unsigned long  sr;        /* saved status register */
+	unsigned long  trap_no;   /* saved status register */
 
 	/* FPU regs */
 	struct user_fp __aligned(16) user_fp;
arch/csky/kernel/traps.c +10 -1
index b057480e7463..63715cb90ee9 100644
@@ -115,8 +115,9 @@ asmlinkage void trap_c(struct pt_regs *regs)
 	int sig;
 	unsigned long vector;
 	siginfo_t info;
+	struct task_struct *tsk = current;
 
-	vector = (mfcr("psr") >> 16) & 0xff;
+	vector = (regs->sr >> 16) & 0xff;
 
 	switch (vector) {
 	case VEC_ZERODIV:
@@ -129,6 +130,7 @@ asmlinkage void trap_c(struct pt_regs *regs)
 		sig = SIGTRAP;
 		break;
 	case VEC_ILLEGAL:
+		tsk->thread.trap_no = vector;
 		die_if_kernel("Kernel mode ILLEGAL", regs, vector);
 #ifndef CONFIG_CPU_NO_USER_BKPT
 		if (*(uint16_t *)instruction_pointer(regs) != USR_BKPT)
@@ -146,16 +148,20 @@ asmlinkage void trap_c(struct pt_regs *regs)
 		sig = SIGTRAP;
 		break;
 	case VEC_ACCESS:
+		tsk->thread.trap_no = vector;
 		return buserr(regs);
 #ifdef CONFIG_CPU_NEED_SOFTALIGN
 	case VEC_ALIGN:
+		tsk->thread.trap_no = vector;
 		return csky_alignment(regs);
 #endif
 #ifdef CONFIG_CPU_HAS_FPU
 	case VEC_FPE:
+		tsk->thread.trap_no = vector;
 		die_if_kernel("Kernel mode FPE", regs, vector);
 		return fpu_fpe(regs);
 	case VEC_PRIV:
+		tsk->thread.trap_no = vector;
 		die_if_kernel("Kernel mode PRIV", regs, vector);
 		if (fpu_libc_helper(regs))
 			return;
@@ -164,5 +170,8 @@ asmlinkage void trap_c(struct pt_regs *regs)
 		sig = SIGSEGV;
 		break;
 	}
+
+	tsk->thread.trap_no = vector;
+
 	send_sig(sig, current, 0);
 }
arch/csky/mm/fault.c +7 -0
index f76618b630f9..562c7f708749 100644
@@ -179,11 +179,14 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
 bad_area_nosemaphore:
 	/* User mode accesses just cause a SIGSEGV */
 	if (user_mode(regs)) {
+		tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
 		force_sig_fault(SIGSEGV, si_code, (void __user *)address);
 		return;
 	}
 
 no_context:
+	tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
+
 	/* Are we prepared to handle this kernel fault? */
 	if (fixup_exception(regs))
 		return;
@@ -198,6 +201,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
 	die_if_kernel("Oops", regs, write);
 
 out_of_memory:
+	tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
+
 	/*
 	 * We ran out of memory, call the OOM killer, and return the userspace
 	 * (which will retry the fault, or kill us if we got oom-killed).
@@ -206,6 +211,8 @@ asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long write,
 	return;
 
 do_sigbus:
+	tsk->thread.trap_no = (regs->sr >> 16) & 0xff;
+
 	up_read(&mm->mmap_sem);
 
 	/* Kernel mode? Handle exceptions or die */
Signature · 1 line
2.17.0