1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 by Ralf Baechle
7  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
8  */
9 #ifndef _ASM_PTRACE_H
10 #define _ASM_PTRACE_H
11 
12 
13 #include <linux/compiler.h>
14 #include <linux/linkage.h>
15 #include <linux/types.h>
16 #include <asm/isadep.h>
17 #include <asm/page.h>
18 #include <asm/thread_info.h>
19 #include <uapi/asm/ptrace.h>
20 
21 /*
22  * This struct defines the way the registers are stored on the stack during a
23  * system call/exception. As usual the registers k0/k1 aren't being saved.
24  *
25  * If you add a register here, also add it to regoffset_table[] in
26  * arch/mips/kernel/ptrace.c.
27  */
28 struct pt_regs {
29 #ifdef CONFIG_32BIT
30 	/* Pad bytes for argument save space on the stack. */
31 	unsigned long pad0[8];
32 #endif
33 
34 	/* Saved main processor registers. */
35 	unsigned long regs[32];
36 
37 	/* Saved special registers. */
38 	unsigned long cp0_status;
39 	unsigned long hi;
40 	unsigned long lo;
41 #ifdef CONFIG_CPU_HAS_SMARTMIPS
42 	unsigned long acx;
43 #endif
44 	unsigned long cp0_badvaddr;
45 	unsigned long cp0_cause;
46 	unsigned long cp0_epc;
47 #ifdef CONFIG_CPU_CAVIUM_OCTEON
48 	unsigned long long mpl[6];        /* MTM{0-5} */
49 	unsigned long long mtp[6];        /* MTP{0-5} */
50 #endif
51 	unsigned long __last[0];
52 } __aligned(8);
53 
kernel_stack_pointer(struct pt_regs * regs)54 static inline unsigned long kernel_stack_pointer(struct pt_regs *regs)
55 {
56 	return regs->regs[29];
57 }
58 
instruction_pointer_set(struct pt_regs * regs,unsigned long val)59 static inline void instruction_pointer_set(struct pt_regs *regs,
60                                            unsigned long val)
61 {
62 	regs->cp0_epc = val;
63 }
64 
65 /* Query offset/name of register from its name/offset */
66 extern int regs_query_register_offset(const char *name);
67 #define MAX_REG_OFFSET (offsetof(struct pt_regs, __last))
68 
69 /**
70  * regs_get_register() - get register value from its offset
71  * @regs:       pt_regs from which register value is gotten.
72  * @offset:     offset number of the register.
73  *
74  * regs_get_register returns the value of a register. The @offset is the
75  * offset of the register in struct pt_regs address which specified by @regs.
76  * If @offset is bigger than MAX_REG_OFFSET, this returns 0.
77  */
regs_get_register(struct pt_regs * regs,unsigned int offset)78 static inline unsigned long regs_get_register(struct pt_regs *regs,
79                                               unsigned int offset)
80 {
81 	if (unlikely(offset > MAX_REG_OFFSET))
82 		return 0;
83 
84 	return *(unsigned long *)((unsigned long)regs + offset);
85 }
86 
87 /**
88  * regs_within_kernel_stack() - check the address in the stack
89  * @regs:       pt_regs which contains kernel stack pointer.
90  * @addr:       address which is checked.
91  *
92  * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
93  * If @addr is within the kernel stack, it returns true. If not, returns false.
94  */
regs_within_kernel_stack(struct pt_regs * regs,unsigned long addr)95 static inline int regs_within_kernel_stack(struct pt_regs *regs,
96                                            unsigned long addr)
97 {
98 	return ((addr & ~(THREAD_SIZE - 1))  ==
99 		(kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1)));
100 }
101 
102 /**
103  * regs_get_kernel_stack_nth() - get Nth entry of the stack
104  * @regs:       pt_regs which contains kernel stack pointer.
105  * @n:          stack entry number.
106  *
107  * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
108  * is specified by @regs. If the @n th entry is NOT in the kernel stack,
109  * this returns 0.
110  */
regs_get_kernel_stack_nth(struct pt_regs * regs,unsigned int n)111 static inline unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs,
112                                                       unsigned int n)
113 {
114 	unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
115 
116 	addr += n;
117 	if (regs_within_kernel_stack(regs, (unsigned long)addr))
118 		return *addr;
119 	else
120 		return 0;
121 }
122 
123 struct task_struct;
124 
125 extern int ptrace_getregs(struct task_struct *child,
126 	struct user_pt_regs __user *data);
127 extern int ptrace_setregs(struct task_struct *child,
128 	struct user_pt_regs __user *data);
129 
130 extern int ptrace_getfpregs(struct task_struct *child, __u32 __user *data);
131 extern int ptrace_setfpregs(struct task_struct *child, __u32 __user *data);
132 
133 extern int ptrace_get_watch_regs(struct task_struct *child,
134 	struct pt_watch_regs __user *addr);
135 extern int ptrace_set_watch_regs(struct task_struct *child,
136 	struct pt_watch_regs __user *addr);
137 
138 /*
139  * Does the process account for user or for system time?
140  */
141 #define user_mode(regs) (((regs)->cp0_status & KU_MASK) == KU_USER)
142 
is_syscall_success(struct pt_regs * regs)143 static inline int is_syscall_success(struct pt_regs *regs)
144 {
145 	return !regs->regs[7];
146 }
147 
regs_return_value(struct pt_regs * regs)148 static inline long regs_return_value(struct pt_regs *regs)
149 {
150 	if (is_syscall_success(regs) || !user_mode(regs))
151 		return regs->regs[2];
152 	else
153 		return -regs->regs[2];
154 }
155 
156 #define instruction_pointer(regs) ((regs)->cp0_epc)
157 #define profile_pc(regs) instruction_pointer(regs)
158 
159 extern asmlinkage long syscall_trace_enter(struct pt_regs *regs, long syscall);
160 extern asmlinkage void syscall_trace_leave(struct pt_regs *regs);
161 
162 extern void die(const char *, struct pt_regs *) __noreturn;
163 
die_if_kernel(const char * str,struct pt_regs * regs)164 static inline void die_if_kernel(const char *str, struct pt_regs *regs)
165 {
166 	if (unlikely(!user_mode(regs)))
167 		die(str, regs);
168 }
169 
170 #define current_pt_regs()						\
171 ({									\
172 	unsigned long sp = (unsigned long)__builtin_frame_address(0);	\
173 	(struct pt_regs *)((sp | (THREAD_SIZE - 1)) + 1 - 32) - 1;	\
174 })
175 
176 /* Helpers for working with the user stack pointer */
177 
user_stack_pointer(struct pt_regs * regs)178 static inline unsigned long user_stack_pointer(struct pt_regs *regs)
179 {
180 	return regs->regs[29];
181 }
182 
user_stack_pointer_set(struct pt_regs * regs,unsigned long val)183 static inline void user_stack_pointer_set(struct pt_regs *regs,
184 	unsigned long val)
185 {
186 	regs->regs[29] = val;
187 }
188 
189 #endif /* _ASM_PTRACE_H */
190