Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <linux/audit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/regset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/tracehook.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <asm/thread_info.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <asm/asm-offsets.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <abi/regdef.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <trace/events/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /* sets the trace bits. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define TRACE_MODE_SI      (1 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define TRACE_MODE_RUN     0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define TRACE_MODE_MASK    ~(0x3 << 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * Make sure the single step bit is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static void singlestep_disable(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	regs = task_pt_regs(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_RUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	/* Enable irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	regs->sr |= BIT(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) static void singlestep_enable(struct task_struct *tsk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	struct pt_regs *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	regs = task_pt_regs(tsk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	regs->sr = (regs->sr & TRACE_MODE_MASK) | TRACE_MODE_SI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	/* Disable irq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	regs->sr &= ~BIT(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * Make sure the single step bit is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) void user_enable_single_step(struct task_struct *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	singlestep_enable(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) void user_disable_single_step(struct task_struct *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	singlestep_disable(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) enum csky_regset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	REGSET_GPR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	REGSET_FPR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static int gpr_get(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		   const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		   struct membuf to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct pt_regs *regs = task_pt_regs(target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	/* Abiv1 regs->tls is fake and we need sync here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	regs->tls = task_thread_info(target)->tp_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return membuf_write(&to, regs, sizeof(*regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int gpr_set(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		    const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		    unsigned int pos, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		    const void *kbuf, const void __user *ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct pt_regs regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &regs, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	/* BIT(0) of regs.sr is Condition Code/Carry bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	regs.sr = (regs.sr & BIT(0)) | (task_pt_regs(target)->sr & ~BIT(0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #ifdef CONFIG_CPU_HAS_HILO
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	regs.dcsr = task_pt_regs(target)->dcsr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	task_thread_info(target)->tp_value = regs.tls;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	*task_pt_regs(target) = regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) static int fpr_get(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 		   const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		   struct membuf to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	struct user_fp tmp = *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	for (i = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 		tmp.vr[i*4] = regs->vr[i*2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		tmp.vr[i*4 + 1] = regs->vr[i*2 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	for (i = 0; i < 32; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		tmp.vr[64 + i] = regs->vr[32 + i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	return membuf_write(&to, &tmp, sizeof(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	return membuf_write(&to, regs, sizeof(*regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int fpr_set(struct task_struct *target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		   const struct user_regset *regset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		   unsigned int pos, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		   const void *kbuf, const void __user *ubuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	struct user_fp *regs = (struct user_fp *)&target->thread.user_fp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #if defined(CONFIG_CPU_HAS_FPUV2) && !defined(CONFIG_CPU_HAS_VDSP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	struct user_fp tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &tmp, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	*regs = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	for (i = 0; i < 16; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		regs->vr[i*2] = tmp.vr[i*4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		regs->vr[i*2 + 1] = tmp.vr[i*4 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	for (i = 0; i < 32; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		regs->vr[32 + i] = tmp.vr[64 + i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) static const struct user_regset csky_regsets[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	[REGSET_GPR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		.core_note_type = NT_PRSTATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		.n = sizeof(struct pt_regs) / sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		.size = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		.align = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		.regset_get = gpr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		.set = gpr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	[REGSET_FPR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		.core_note_type = NT_PRFPREG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		.n = sizeof(struct user_fp) / sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 		.size = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		.align = sizeof(u32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		.regset_get = fpr_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		.set = fpr_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static const struct user_regset_view user_csky_view = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.name = "csky",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.e_machine = ELF_ARCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.regsets = csky_regsets,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	.n = ARRAY_SIZE(csky_regsets),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) const struct user_regset_view *task_user_regset_view(struct task_struct *task)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	return &user_csky_view;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) struct pt_regs_offset {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) #define REG_OFFSET_END {.name = NULL, .offset = 0}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static const struct pt_regs_offset regoffset_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	REG_OFFSET_NAME(tls),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	REG_OFFSET_NAME(lr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	REG_OFFSET_NAME(pc),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	REG_OFFSET_NAME(sr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	REG_OFFSET_NAME(usp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	REG_OFFSET_NAME(orig_a0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	REG_OFFSET_NAME(a0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	REG_OFFSET_NAME(a1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	REG_OFFSET_NAME(a2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	REG_OFFSET_NAME(a3),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	REG_OFFSET_NAME(regs[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	REG_OFFSET_NAME(regs[1]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	REG_OFFSET_NAME(regs[2]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	REG_OFFSET_NAME(regs[3]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	REG_OFFSET_NAME(regs[4]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	REG_OFFSET_NAME(regs[5]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	REG_OFFSET_NAME(regs[6]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	REG_OFFSET_NAME(regs[7]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	REG_OFFSET_NAME(regs[8]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	REG_OFFSET_NAME(regs[9]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) #if defined(__CSKYABIV2__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	REG_OFFSET_NAME(exregs[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	REG_OFFSET_NAME(exregs[1]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	REG_OFFSET_NAME(exregs[2]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	REG_OFFSET_NAME(exregs[3]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	REG_OFFSET_NAME(exregs[4]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	REG_OFFSET_NAME(exregs[5]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	REG_OFFSET_NAME(exregs[6]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	REG_OFFSET_NAME(exregs[7]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	REG_OFFSET_NAME(exregs[8]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	REG_OFFSET_NAME(exregs[9]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	REG_OFFSET_NAME(exregs[10]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	REG_OFFSET_NAME(exregs[11]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	REG_OFFSET_NAME(exregs[12]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	REG_OFFSET_NAME(exregs[13]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	REG_OFFSET_NAME(exregs[14]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	REG_OFFSET_NAME(rhi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	REG_OFFSET_NAME(rlo),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	REG_OFFSET_NAME(dcsr),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	REG_OFFSET_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)  * regs_query_register_offset() - query register offset from its name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)  * @name:	the name of a register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  * regs_query_register_offset() returns the offset of a register in struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  * pt_regs from its name. If the name is invalid, this returns -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int regs_query_register_offset(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	const struct pt_regs_offset *roff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	for (roff = regoffset_table; roff->name != NULL; roff++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		if (!strcmp(roff->name, name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			return roff->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * regs_within_kernel_stack() - check the address in the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  * @regs:      pt_regs which contains kernel stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)  * @addr:      address which is checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)  * If @addr is within the kernel stack, it returns true. If not, returns false.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	return (addr & ~(THREAD_SIZE - 1))  ==
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)  * regs_get_kernel_stack_nth() - get Nth entry of the stack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)  * @regs:	pt_regs which contains kernel stack pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)  * @n:		stack entry number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * this returns 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	addr += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return *addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) void ptrace_disable(struct task_struct *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	singlestep_disable(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) long arch_ptrace(struct task_struct *child, long request,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		 unsigned long addr, unsigned long data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	long ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	switch (request) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		ret = ptrace_request(child, request, addr, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) asmlinkage int syscall_trace_enter(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (test_thread_flag(TIF_SYSCALL_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		if (tracehook_report_syscall_entry(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (secure_computing() == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		trace_sys_enter(regs, syscall_get_nr(current, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	audit_syscall_entry(regs_syscallid(regs), regs->a0, regs->a1, regs->a2, regs->a3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) asmlinkage void syscall_trace_exit(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	audit_syscall_exit(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	if (test_thread_flag(TIF_SYSCALL_TRACE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		tracehook_report_syscall_exit(regs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		trace_sys_exit(regs, syscall_get_return_value(current, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) void show_regs(struct pt_regs *fp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	pr_info("\nCURRENT PROCESS:\n\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	pr_info("COMM=%s PID=%d\n", current->comm, current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	if (current->mm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 		pr_info("TEXT=%08x-%08x DATA=%08x-%08x BSS=%08x-%08x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		       (int) current->mm->start_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		       (int) current->mm->end_code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		       (int) current->mm->start_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		       (int) current->mm->end_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		       (int) current->mm->end_data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 		       (int) current->mm->brk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		pr_info("USER-STACK=%08x  KERNEL-STACK=%08x\n\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		       (int) current->mm->start_stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		       (int) (((unsigned long) current) + 2 * PAGE_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	pr_info("PC: 0x%08lx (%pS)\n", (long)fp->pc, (void *)fp->pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	pr_info("LR: 0x%08lx (%pS)\n", (long)fp->lr, (void *)fp->lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	pr_info("SP: 0x%08lx\n", (long)fp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	pr_info("orig_a0: 0x%08lx\n", fp->orig_a0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	pr_info("PSR: 0x%08lx\n", (long)fp->sr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	pr_info(" a0: 0x%08lx   a1: 0x%08lx   a2: 0x%08lx   a3: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		fp->a0, fp->a1, fp->a2, fp->a3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) #if defined(__CSKYABIV2__)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	pr_info(" r4: 0x%08lx   r5: 0x%08lx   r6: 0x%08lx   r7: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	pr_info(" r8: 0x%08lx   r9: 0x%08lx  r10: 0x%08lx  r11: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	pr_info("r12: 0x%08lx  r13: 0x%08lx  r15: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		fp->regs[8], fp->regs[9], fp->lr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	pr_info("r16: 0x%08lx  r17: 0x%08lx  r18: 0x%08lx  r19: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		fp->exregs[0], fp->exregs[1], fp->exregs[2], fp->exregs[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	pr_info("r20: 0x%08lx  r21: 0x%08lx  r22: 0x%08lx  r23: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		fp->exregs[4], fp->exregs[5], fp->exregs[6], fp->exregs[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	pr_info("r24: 0x%08lx  r25: 0x%08lx  r26: 0x%08lx  r27: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		fp->exregs[8], fp->exregs[9], fp->exregs[10], fp->exregs[11]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	pr_info("r28: 0x%08lx  r29: 0x%08lx  r30: 0x%08lx  tls: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		fp->exregs[12], fp->exregs[13], fp->exregs[14], fp->tls);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	pr_info(" hi: 0x%08lx   lo: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		fp->rhi, fp->rlo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	pr_info(" r6: 0x%08lx   r7: 0x%08lx   r8: 0x%08lx   r9: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		fp->regs[0], fp->regs[1], fp->regs[2], fp->regs[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	pr_info("r10: 0x%08lx  r11: 0x%08lx  r12: 0x%08lx  r13: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		fp->regs[4], fp->regs[5], fp->regs[6], fp->regs[7]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	pr_info("r14: 0x%08lx   r1: 0x%08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		fp->regs[8], fp->regs[9]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }