• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * User-mode machine state access
4  *
5  * Copyright (C) 2007 Red Hat, Inc.  All rights reserved.
6  *
7  * Red Hat Author: Roland McGrath.
8  */
9 
10 #ifndef _LINUX_REGSET_H
11 #define _LINUX_REGSET_H	1
12 
13 #include <linux/compiler.h>
14 #include <linux/types.h>
15 #include <linux/bug.h>
16 #include <linux/uaccess.h>
17 struct task_struct;
18 struct user_regset;
19 
20 struct membuf {
21 	void *p;
22 	size_t left;
23 };
24 
membuf_zero(struct membuf * s,size_t size)25 static inline int membuf_zero(struct membuf *s, size_t size)
26 {
27 	if (s->left) {
28 		if (size > s->left)
29 			size = s->left;
30 		memset(s->p, 0, size);
31 		s->p += size;
32 		s->left -= size;
33 	}
34 	return s->left;
35 }
36 
membuf_write(struct membuf * s,const void * v,size_t size)37 static inline int membuf_write(struct membuf *s, const void *v, size_t size)
38 {
39 	if (s->left) {
40 		if (size > s->left)
41 			size = s->left;
42 		memcpy(s->p, v, size);
43 		s->p += size;
44 		s->left -= size;
45 	}
46 	return s->left;
47 }
48 
49 /* current s->p must be aligned for v; v must be a scalar */
50 #define membuf_store(s, v)				\
51 ({							\
52 	struct membuf *__s = (s);			\
53         if (__s->left) {				\
54 		typeof(v) __v = (v);			\
55 		size_t __size = sizeof(__v);		\
56 		if (unlikely(__size > __s->left)) {	\
57 			__size = __s->left;		\
58 			memcpy(__s->p, &__v, __size);	\
59 		} else {				\
60 			*(typeof(__v + 0) *)__s->p = __v;	\
61 		}					\
62 		__s->p += __size;			\
63 		__s->left -= __size;			\
64 	}						\
65 	__s->left;})
66 
67 /**
68  * user_regset_active_fn - type of @active function in &struct user_regset
69  * @target:	thread being examined
70  * @regset:	regset being examined
71  *
72  * Return -%ENODEV if not available on the hardware found.
73  * Return %0 if no interesting state in this thread.
74  * Return >%0 number of @size units of interesting state.
75  * Any get call fetching state beyond that number will
76  * see the default initialization state for this data,
77  * so a caller that knows what the default state is need
78  * not copy it all out.
79  * This call is optional; the pointer is %NULL if there
80  * is no inexpensive check to yield a value < @n.
81  */
82 typedef int user_regset_active_fn(struct task_struct *target,
83 				  const struct user_regset *regset);
84 
85 typedef int user_regset_get2_fn(struct task_struct *target,
86 			       const struct user_regset *regset,
87 			       struct membuf to);
88 
89 /**
90  * user_regset_set_fn - type of @set function in &struct user_regset
91  * @target:	thread being examined
92  * @regset:	regset being examined
93  * @pos:	offset into the regset data to access, in bytes
94  * @count:	amount of data to copy, in bytes
95  * @kbuf:	if not %NULL, a kernel-space pointer to copy from
96  * @ubuf:	if @kbuf is %NULL, a user-space pointer to copy from
97  *
98  * Store register values.  Return %0 on success; -%EIO or -%ENODEV
99  * are usual failure returns.  The @pos and @count values are in
100  * bytes, but must be properly aligned.  If @kbuf is non-null, that
101  * buffer is used and @ubuf is ignored.  If @kbuf is %NULL, then
102  * ubuf gives a userland pointer to access directly, and an -%EFAULT
103  * return value is possible.
104  */
105 typedef int user_regset_set_fn(struct task_struct *target,
106 			       const struct user_regset *regset,
107 			       unsigned int pos, unsigned int count,
108 			       const void *kbuf, const void __user *ubuf);
109 
110 /**
111  * user_regset_writeback_fn - type of @writeback function in &struct user_regset
112  * @target:	thread being examined
113  * @regset:	regset being examined
114  * @immediate:	zero if writeback at completion of next context switch is OK
115  *
116  * This call is optional; usually the pointer is %NULL.  When
117  * provided, there is some user memory associated with this regset's
118  * hardware, such as memory backing cached register data on register
119  * window machines; the regset's data controls what user memory is
120  * used (e.g. via the stack pointer value).
121  *
122  * Write register data back to user memory.  If the @immediate flag
123  * is nonzero, it must be written to the user memory so uaccess or
124  * access_process_vm() can see it when this call returns; if zero,
125  * then it must be written back by the time the task completes a
126  * context switch (as synchronized with wait_task_inactive()).
127  * Return %0 on success or if there was nothing to do, -%EFAULT for
128  * a memory problem (bad stack pointer or whatever), or -%EIO for a
129  * hardware problem.
130  */
131 typedef int user_regset_writeback_fn(struct task_struct *target,
132 				     const struct user_regset *regset,
133 				     int immediate);
134 
135 /**
136  * struct user_regset - accessible thread CPU state
137  * @n:			Number of slots (registers).
138  * @size:		Size in bytes of a slot (register).
139  * @align:		Required alignment, in bytes.
140  * @bias:		Bias from natural indexing.
141  * @core_note_type:	ELF note @n_type value used in core dumps.
142  * @get:		Function to fetch values.
143  * @set:		Function to store values.
144  * @active:		Function to report if regset is active, or %NULL.
145  * @writeback:		Function to write data back to user memory, or %NULL.
146  *
147  * This data structure describes a machine resource we call a register set.
148  * This is part of the state of an individual thread, not necessarily
149  * actual CPU registers per se.  A register set consists of a number of
150  * similar slots, given by @n.  Each slot is @size bytes, and aligned to
151  * @align bytes (which is at least @size).  For dynamically-sized
152  * regsets, @n must contain the maximum possible number of slots for the
153  * regset.
154  *
155  * For backward compatibility, the @get and @set methods must pad to, or
156  * accept, @n * @size bytes, even if the current regset size is smaller.
157  * The precise semantics of these operations depend on the regset being
158  * accessed.
159  *
160  * The functions to which &struct user_regset members point must be
161  * called only on the current thread or on a thread that is in
162  * %TASK_STOPPED or %TASK_TRACED state, that we are guaranteed will not
163  * be woken up and return to user mode, and that we have called
164  * wait_task_inactive() on.  (The target thread always might wake up for
165  * SIGKILL while these functions are working, in which case that
166  * thread's user_regset state might be scrambled.)
167  *
168  * The @pos argument must be aligned according to @align; the @count
169  * argument must be a multiple of @size.  These functions are not
170  * responsible for checking for invalid arguments.
171  *
172  * When there is a natural value to use as an index, @bias gives the
173  * difference between the natural index and the slot index for the
174  * register set.  For example, x86 GDT segment descriptors form a regset;
175  * the segment selector produces a natural index, but only a subset of
176  * that index space is available as a regset (the TLS slots); subtracting
177  * @bias from a segment selector index value computes the regset slot.
178  *
179  * If nonzero, @core_note_type gives the n_type field (NT_* value)
180  * of the core file note in which this regset's data appears.
181  * NT_PRSTATUS is a special case in that the regset data starts at
182  * offsetof(struct elf_prstatus, pr_reg) into the note data; that is
183  * part of the per-machine ELF formats userland knows about.  In
184  * other cases, the core file note contains exactly the whole regset
185  * (@n * @size) and nothing else.  The core file note is normally
186  * omitted when there is an @active function and it returns zero.
187  */
188 struct user_regset {
189 	user_regset_get2_fn		*regset_get;
190 	user_regset_set_fn		*set;
191 	user_regset_active_fn		*active;
192 	user_regset_writeback_fn	*writeback;
193 	unsigned int			n;
194 	unsigned int 			size;
195 	unsigned int 			align;
196 	unsigned int 			bias;
197 	unsigned int 			core_note_type;
198 };
199 
200 /**
201  * struct user_regset_view - available regsets
202  * @name:	Identifier, e.g. UTS_MACHINE string.
203  * @regsets:	Array of @n regsets available in this view.
204  * @n:		Number of elements in @regsets.
205  * @e_machine:	ELF header @e_machine %EM_* value written in core dumps.
206  * @e_flags:	ELF header @e_flags value written in core dumps.
207  * @ei_osabi:	ELF header @e_ident[%EI_OSABI] value written in core dumps.
208  *
209  * A regset view is a collection of regsets (&struct user_regset,
210  * above).  This describes all the state of a thread that can be seen
211  * from a given architecture/ABI environment.  More than one view might
212  * refer to the same &struct user_regset, or more than one regset
213  * might refer to the same machine-specific state in the thread.  For
214  * example, a 32-bit thread's state could be examined from the 32-bit
215  * view or from the 64-bit view.  Either method reaches the same thread
216  * register state, doing appropriate widening or truncation.
217  */
218 struct user_regset_view {
219 	const char *name;
220 	const struct user_regset *regsets;
221 	unsigned int n;
222 	u32 e_flags;
223 	u16 e_machine;
224 	u8 ei_osabi;
225 };
226 
227 /*
228  * This is documented here rather than at the definition sites because its
229  * implementation is machine-dependent but its interface is universal.
230  */
231 /**
232  * task_user_regset_view - Return the process's native regset view.
233  * @tsk: a thread of the process in question
234  *
235  * Return the &struct user_regset_view that is native for the given process.
236  * For example, what it would access when it called ptrace().
237  * Throughout the life of the process, this only changes at exec.
238  */
239 const struct user_regset_view *task_user_regset_view(struct task_struct *tsk);
240 
user_regset_copyin(unsigned int * pos,unsigned int * count,const void ** kbuf,const void __user ** ubuf,void * data,const int start_pos,const int end_pos)241 static inline int user_regset_copyin(unsigned int *pos, unsigned int *count,
242 				     const void **kbuf,
243 				     const void __user **ubuf, void *data,
244 				     const int start_pos, const int end_pos)
245 {
246 	if (*count == 0)
247 		return 0;
248 	BUG_ON(*pos < start_pos);
249 	if (end_pos < 0 || *pos < end_pos) {
250 		unsigned int copy = (end_pos < 0 ? *count
251 				     : min(*count, end_pos - *pos));
252 		data += *pos - start_pos;
253 		if (*kbuf) {
254 			memcpy(data, *kbuf, copy);
255 			*kbuf += copy;
256 		} else if (__copy_from_user(data, *ubuf, copy))
257 			return -EFAULT;
258 		else
259 			*ubuf += copy;
260 		*pos += copy;
261 		*count -= copy;
262 	}
263 	return 0;
264 }
265 
user_regset_copyin_ignore(unsigned int * pos,unsigned int * count,const void ** kbuf,const void __user ** ubuf,const int start_pos,const int end_pos)266 static inline int user_regset_copyin_ignore(unsigned int *pos,
267 					    unsigned int *count,
268 					    const void **kbuf,
269 					    const void __user **ubuf,
270 					    const int start_pos,
271 					    const int end_pos)
272 {
273 	if (*count == 0)
274 		return 0;
275 	BUG_ON(*pos < start_pos);
276 	if (end_pos < 0 || *pos < end_pos) {
277 		unsigned int copy = (end_pos < 0 ? *count
278 				     : min(*count, end_pos - *pos));
279 		if (*kbuf)
280 			*kbuf += copy;
281 		else
282 			*ubuf += copy;
283 		*pos += copy;
284 		*count -= copy;
285 	}
286 	return 0;
287 }
288 
289 extern int regset_get(struct task_struct *target,
290 		      const struct user_regset *regset,
291 		      unsigned int size, void *data);
292 
293 extern int regset_get_alloc(struct task_struct *target,
294 			    const struct user_regset *regset,
295 			    unsigned int size,
296 			    void **data);
297 
298 extern int copy_regset_to_user(struct task_struct *target,
299 			       const struct user_regset_view *view,
300 			       unsigned int setno, unsigned int offset,
301 			       unsigned int size, void __user *data);
302 
303 /**
304  * copy_regset_from_user - store into thread's user_regset data from user memory
305  * @target:	thread to be examined
306  * @view:	&struct user_regset_view describing user thread machine state
307  * @setno:	index in @view->regsets
308  * @offset:	offset into the regset data, in bytes
309  * @size:	amount of data to copy, in bytes
310  * @data:	user-mode pointer to copy from
311  */
copy_regset_from_user(struct task_struct * target,const struct user_regset_view * view,unsigned int setno,unsigned int offset,unsigned int size,const void __user * data)312 static inline int copy_regset_from_user(struct task_struct *target,
313 					const struct user_regset_view *view,
314 					unsigned int setno,
315 					unsigned int offset, unsigned int size,
316 					const void __user *data)
317 {
318 	const struct user_regset *regset = &view->regsets[setno];
319 
320 	if (!regset->set)
321 		return -EOPNOTSUPP;
322 
323 	if (!access_ok(data, size))
324 		return -EFAULT;
325 
326 	return regset->set(target, regset, offset, size, NULL, data);
327 }
328 
329 #endif	/* <linux/regset.h> */
330