xref: /openbmc/linux/arch/sh/include/asm/ptrace.h (revision feac8c8b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 1999, 2000  Niibe Yutaka
4  */
5 #ifndef __ASM_SH_PTRACE_H
6 #define __ASM_SH_PTRACE_H
7 
8 
9 #include <linux/stringify.h>
10 #include <linux/stddef.h>
11 #include <linux/thread_info.h>
12 #include <asm/addrspace.h>
13 #include <asm/page.h>
14 #include <uapi/asm/ptrace.h>
15 
16 #define user_mode(regs)			(((regs)->sr & 0x40000000)==0)
17 #define kernel_stack_pointer(_regs)	((unsigned long)(_regs)->regs[15])
18 
19 #define GET_FP(regs)	((regs)->regs[14])
20 #define GET_USP(regs)	((regs)->regs[15])
21 
22 #define arch_has_single_step()	(1)
23 
24 /*
25  * kprobe-based event tracer support
26  */
27 struct pt_regs_offset {
28 	const char *name;
29 	int offset;
30 };
31 
32 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
33 #define REGS_OFFSET_NAME(num)	\
34 	{.name = __stringify(r##num), .offset = offsetof(struct pt_regs, regs[num])}
35 #define TREGS_OFFSET_NAME(num)	\
36 	{.name = __stringify(tr##num), .offset = offsetof(struct pt_regs, tregs[num])}
37 #define REG_OFFSET_END {.name = NULL, .offset = 0}
38 
39 /* Query offset/name of register from its name/offset */
40 extern int regs_query_register_offset(const char *name);
41 extern const char *regs_query_register_name(unsigned int offset);
42 
43 extern const struct pt_regs_offset regoffset_table[];
44 
45 /**
46  * regs_get_register() - get register value from its offset
47  * @regs:	pt_regs from which register value is gotten.
48  * @offset:	offset number of the register.
49  *
50  * regs_get_register returns the value of a register. The @offset is the
51  * offset of the register in struct pt_regs address which specified by @regs.
52  * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
53  */
54 static inline unsigned long regs_get_register(struct pt_regs *regs,
55 					      unsigned int offset)
56 {
57 	if (unlikely(offset > MAX_REG_OFFSET))
58 		return 0;
59 	return *(unsigned long *)((unsigned long)regs + offset);
60 }
61 
62 /**
63  * regs_within_kernel_stack() - check the address in the stack
64  * @regs:	pt_regs which contains kernel stack pointer.
65  * @addr:	address which is checked.
66  *
67  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
68  * If @addr is within the kernel stack, it returns true. If not, returns false.
69  */
70 static inline int regs_within_kernel_stack(struct pt_regs *regs,
71 					   unsigned long addr)
72 {
73 	return ((addr & ~(THREAD_SIZE - 1))  ==
74 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
75 }
76 
77 /**
78  * regs_get_kernel_stack_nth() - get Nth entry of the stack
79  * @regs:	pt_regs which contains kernel stack pointer.
80  * @n:		stack entry number.
81  *
82  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
83  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
84  * this returns 0.
85  */
86 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
87 						      unsigned int n)
88 {
89 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
90 	addr += n;
91 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
92 		return *addr;
93 	else
94 		return 0;
95 }
96 
97 struct perf_event;
98 struct perf_sample_data;
99 
100 extern void ptrace_triggered(struct perf_event *bp,
101 		      struct perf_sample_data *data, struct pt_regs *regs);
102 
103 #define task_pt_regs(task) \
104 	((struct pt_regs *) (task_stack_page(task) + THREAD_SIZE) - 1)
105 
106 static inline unsigned long profile_pc(struct pt_regs *regs)
107 {
108 	unsigned long pc = regs->pc;
109 
110 	if (virt_addr_uncached(pc))
111 		return CAC_ADDR(pc);
112 
113 	return pc;
114 }
115 #define profile_pc profile_pc
116 
117 #include <asm-generic/ptrace.h>
118 #endif /* __ASM_SH_PTRACE_H */
119