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 
21 /**
22  * user_regset_active_fn - type of @active function in &struct user_regset
23  * @target:	thread being examined
24  * @regset:	regset being examined
25  *
26  * Return -%ENODEV if not available on the hardware found.
27  * Return %0 if no interesting state in this thread.
28  * Return >%0 number of @size units of interesting state.
29  * Any get call fetching state beyond that number will
30  * see the default initialization state for this data,
31  * so a caller that knows what the default state is need
32  * not copy it all out.
33  * This call is optional; the pointer is %NULL if there
34  * is no inexpensive check to yield a value < @n.
35  */
36 typedef int user_regset_active_fn(struct task_struct *target,
37 				  const struct user_regset *regset);
38 
39 /**
40  * user_regset_get_fn - type of @get function in &struct user_regset
41  * @target:	thread being examined
42  * @regset:	regset being examined
43  * @pos:	offset into the regset data to access, in bytes
44  * @count:	amount of data to copy, in bytes
45  * @kbuf:	if not %NULL, a kernel-space pointer to copy into
46  * @ubuf:	if @kbuf is %NULL, a user-space pointer to copy into
47  *
48  * Fetch register values.  Return %0 on success; -%EIO or -%ENODEV
49  * are usual failure returns.  The @pos and @count values are in
50  * bytes, but must be properly aligned.  If @kbuf is non-null, that
51  * buffer is used and @ubuf is ignored.  If @kbuf is %NULL, then
52  * ubuf gives a userland pointer to access directly, and an -%EFAULT
53  * return value is possible.
54  */
55 typedef int user_regset_get_fn(struct task_struct *target,
56 			       const struct user_regset *regset,
57 			       unsigned int pos, unsigned int count,
58 			       void *kbuf, void __user *ubuf);
59 
60 /**
61  * user_regset_set_fn - type of @set function in &struct user_regset
62  * @target:	thread being examined
63  * @regset:	regset being examined
64  * @pos:	offset into the regset data to access, in bytes
65  * @count:	amount of data to copy, in bytes
66  * @kbuf:	if not %NULL, a kernel-space pointer to copy from
67  * @ubuf:	if @kbuf is %NULL, a user-space pointer to copy from
68  *
69  * Store register values.  Return %0 on success; -%EIO or -%ENODEV
70  * are usual failure returns.  The @pos and @count values are in
71  * bytes, but must be properly aligned.  If @kbuf is non-null, that
72  * buffer is used and @ubuf is ignored.  If @kbuf is %NULL, then
73  * ubuf gives a userland pointer to access directly, and an -%EFAULT
74  * return value is possible.
75  */
76 typedef int user_regset_set_fn(struct task_struct *target,
77 			       const struct user_regset *regset,
78 			       unsigned int pos, unsigned int count,
79 			       const void *kbuf, const void __user *ubuf);
80 
81 /**
82  * user_regset_writeback_fn - type of @writeback function in &struct user_regset
83  * @target:	thread being examined
84  * @regset:	regset being examined
85  * @immediate:	zero if writeback at completion of next context switch is OK
86  *
87  * This call is optional; usually the pointer is %NULL.  When
88  * provided, there is some user memory associated with this regset's
89  * hardware, such as memory backing cached register data on register
90  * window machines; the regset's data controls what user memory is
91  * used (e.g. via the stack pointer value).
92  *
93  * Write register data back to user memory.  If the @immediate flag
94  * is nonzero, it must be written to the user memory so uaccess or
95  * access_process_vm() can see it when this call returns; if zero,
96  * then it must be written back by the time the task completes a
97  * context switch (as synchronized with wait_task_inactive()).
98  * Return %0 on success or if there was nothing to do, -%EFAULT for
99  * a memory problem (bad stack pointer or whatever), or -%EIO for a
100  * hardware problem.
101  */
102 typedef int user_regset_writeback_fn(struct task_struct *target,
103 				     const struct user_regset *regset,
104 				     int immediate);
105 
106 /**
107  * user_regset_get_size_fn - type of @get_size function in &struct user_regset
108  * @target:	thread being examined
109  * @regset:	regset being examined
110  *
111  * This call is optional; usually the pointer is %NULL.
112  *
113  * When provided, this function must return the current size of regset
114  * data, as observed by the @get function in &struct user_regset.  The
115  * value returned must be a multiple of @size.  The returned size is
116  * required to be valid only until the next time (if any) @regset is
117  * modified for @target.
118  *
119  * This function is intended for dynamically sized regsets.  A regset
120  * that is statically sized does not need to implement it.
121  *
122  * This function should not be called directly: instead, callers should
123  * call regset_size() to determine the current size of a regset.
124  */
125 typedef unsigned int user_regset_get_size_fn(struct task_struct *target,
126 					     const struct user_regset *regset);
127 
128 /**
129  * struct user_regset - accessible thread CPU state
130  * @n:			Number of slots (registers).
131  * @size:		Size in bytes of a slot (register).
132  * @align:		Required alignment, in bytes.
133  * @bias:		Bias from natural indexing.
134  * @core_note_type:	ELF note @n_type value used in core dumps.
135  * @get:		Function to fetch values.
136  * @set:		Function to store values.
137  * @active:		Function to report if regset is active, or %NULL.
138  * @writeback:		Function to write data back to user memory, or %NULL.
139  * @get_size:		Function to return the regset's size, or %NULL.
140  *
141  * This data structure describes a machine resource we call a register set.
142  * This is part of the state of an individual thread, not necessarily
143  * actual CPU registers per se.  A register set consists of a number of
144  * similar slots, given by @n.  Each slot is @size bytes, and aligned to
145  * @align bytes (which is at least @size).  For dynamically-sized
146  * regsets, @n must contain the maximum possible number of slots for the
147  * regset, and @get_size must point to a function that returns the
148  * current regset size.
149  *
150  * Callers that need to know only the current size of the regset and do
151  * not care about its internal structure should call regset_size()
152  * instead of inspecting @n or calling @get_size.
153  *
154  * For backward compatibility, the @get and @set methods must pad to, or
155  * accept, @n * @size bytes, even if the current regset size is smaller.
156  * The precise semantics of these operations depend on the regset being
157  * accessed.
158  *
159  * The functions to which &struct user_regset members point must be
160  * called only on the current thread or on a thread that is in
161  * %TASK_STOPPED or %TASK_TRACED state, that we are guaranteed will not
162  * be woken up and return to user mode, and that we have called
163  * wait_task_inactive() on.  (The target thread always might wake up for
164  * SIGKILL while these functions are working, in which case that
165  * thread's user_regset state might be scrambled.)
166  *
167  * The @pos argument must be aligned according to @align; the @count
168  * argument must be a multiple of @size.  These functions are not
169  * responsible for checking for invalid arguments.
170  *
171  * When there is a natural value to use as an index, @bias gives the
172  * difference between the natural index and the slot index for the
173  * register set.  For example, x86 GDT segment descriptors form a regset;
174  * the segment selector produces a natural index, but only a subset of
175  * that index space is available as a regset (the TLS slots); subtracting
176  * @bias from a segment selector index value computes the regset slot.
177  *
178  * If nonzero, @core_note_type gives the n_type field (NT_* value)
179  * of the core file note in which this regset's data appears.
180  * NT_PRSTATUS is a special case in that the regset data starts at
181  * offsetof(struct elf_prstatus, pr_reg) into the note data; that is
182  * part of the per-machine ELF formats userland knows about.  In
183  * other cases, the core file note contains exactly the whole regset
184  * (@n * @size) and nothing else.  The core file note is normally
185  * omitted when there is an @active function and it returns zero.
186  */
187 struct user_regset {
188 	user_regset_get_fn		*get;
189 	user_regset_set_fn		*set;
190 	user_regset_active_fn		*active;
191 	user_regset_writeback_fn	*writeback;
192 	user_regset_get_size_fn		*get_size;
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 
241 
242 /*
243  * These are helpers for writing regset get/set functions in arch code.
244  * Because @start_pos and @end_pos are always compile-time constants,
245  * these are inlined into very little code though they look large.
246  *
247  * Use one or more calls sequentially for each chunk of regset data stored
248  * contiguously in memory.  Call with constants for @start_pos and @end_pos,
249  * giving the range of byte positions in the regset that data corresponds
250  * to; @end_pos can be -1 if this chunk is at the end of the regset layout.
251  * Each call updates the arguments to point past its chunk.
252  */
253 
user_regset_copyout(unsigned int * pos,unsigned int * count,void ** kbuf,void __user ** ubuf,const void * data,const int start_pos,const int end_pos)254 static inline int user_regset_copyout(unsigned int *pos, unsigned int *count,
255 				      void **kbuf,
256 				      void __user **ubuf, const void *data,
257 				      const int start_pos, const int end_pos)
258 {
259 	if (*count == 0)
260 		return 0;
261 	BUG_ON(*pos < start_pos);
262 	if (end_pos < 0 || *pos < end_pos) {
263 		unsigned int copy = (end_pos < 0 ? *count
264 				     : min(*count, end_pos - *pos));
265 		data += *pos - start_pos;
266 		if (*kbuf) {
267 			memcpy(*kbuf, data, copy);
268 			*kbuf += copy;
269 		} else if (__copy_to_user(*ubuf, data, copy))
270 			return -EFAULT;
271 		else
272 			*ubuf += copy;
273 		*pos += copy;
274 		*count -= copy;
275 	}
276 	return 0;
277 }
278 
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)279 static inline int user_regset_copyin(unsigned int *pos, unsigned int *count,
280 				     const void **kbuf,
281 				     const void __user **ubuf, void *data,
282 				     const int start_pos, const int end_pos)
283 {
284 	if (*count == 0)
285 		return 0;
286 	BUG_ON(*pos < start_pos);
287 	if (end_pos < 0 || *pos < end_pos) {
288 		unsigned int copy = (end_pos < 0 ? *count
289 				     : min(*count, end_pos - *pos));
290 		data += *pos - start_pos;
291 		if (*kbuf) {
292 			memcpy(data, *kbuf, copy);
293 			*kbuf += copy;
294 		} else if (__copy_from_user(data, *ubuf, copy))
295 			return -EFAULT;
296 		else
297 			*ubuf += copy;
298 		*pos += copy;
299 		*count -= copy;
300 	}
301 	return 0;
302 }
303 
304 /*
305  * These two parallel the two above, but for portions of a regset layout
306  * that always read as all-zero or for which writes are ignored.
307  */
user_regset_copyout_zero(unsigned int * pos,unsigned int * count,void ** kbuf,void __user ** ubuf,const int start_pos,const int end_pos)308 static inline int user_regset_copyout_zero(unsigned int *pos,
309 					   unsigned int *count,
310 					   void **kbuf, void __user **ubuf,
311 					   const int start_pos,
312 					   const int end_pos)
313 {
314 	if (*count == 0)
315 		return 0;
316 	BUG_ON(*pos < start_pos);
317 	if (end_pos < 0 || *pos < end_pos) {
318 		unsigned int copy = (end_pos < 0 ? *count
319 				     : min(*count, end_pos - *pos));
320 		if (*kbuf) {
321 			memset(*kbuf, 0, copy);
322 			*kbuf += copy;
323 		} else if (__clear_user(*ubuf, copy))
324 			return -EFAULT;
325 		else
326 			*ubuf += copy;
327 		*pos += copy;
328 		*count -= copy;
329 	}
330 	return 0;
331 }
332 
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)333 static inline int user_regset_copyin_ignore(unsigned int *pos,
334 					    unsigned int *count,
335 					    const void **kbuf,
336 					    const void __user **ubuf,
337 					    const int start_pos,
338 					    const int end_pos)
339 {
340 	if (*count == 0)
341 		return 0;
342 	BUG_ON(*pos < start_pos);
343 	if (end_pos < 0 || *pos < end_pos) {
344 		unsigned int copy = (end_pos < 0 ? *count
345 				     : min(*count, end_pos - *pos));
346 		if (*kbuf)
347 			*kbuf += copy;
348 		else
349 			*ubuf += copy;
350 		*pos += copy;
351 		*count -= copy;
352 	}
353 	return 0;
354 }
355 
356 /**
357  * copy_regset_to_user - fetch a thread's user_regset data into user memory
358  * @target:	thread to be examined
359  * @view:	&struct user_regset_view describing user thread machine state
360  * @setno:	index in @view->regsets
361  * @offset:	offset into the regset data, in bytes
362  * @size:	amount of data to copy, in bytes
363  * @data:	user-mode pointer to copy into
364  */
copy_regset_to_user(struct task_struct * target,const struct user_regset_view * view,unsigned int setno,unsigned int offset,unsigned int size,void __user * data)365 static inline int copy_regset_to_user(struct task_struct *target,
366 				      const struct user_regset_view *view,
367 				      unsigned int setno,
368 				      unsigned int offset, unsigned int size,
369 				      void __user *data)
370 {
371 	const struct user_regset *regset = &view->regsets[setno];
372 
373 	if (!regset->get)
374 		return -EOPNOTSUPP;
375 
376 	if (!access_ok(data, size))
377 		return -EFAULT;
378 
379 	return regset->get(target, regset, offset, size, NULL, data);
380 }
381 
382 /**
383  * copy_regset_from_user - store into thread's user_regset data from user memory
384  * @target:	thread to be examined
385  * @view:	&struct user_regset_view describing user thread machine state
386  * @setno:	index in @view->regsets
387  * @offset:	offset into the regset data, in bytes
388  * @size:	amount of data to copy, in bytes
389  * @data:	user-mode pointer to copy from
390  */
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)391 static inline int copy_regset_from_user(struct task_struct *target,
392 					const struct user_regset_view *view,
393 					unsigned int setno,
394 					unsigned int offset, unsigned int size,
395 					const void __user *data)
396 {
397 	const struct user_regset *regset = &view->regsets[setno];
398 
399 	if (!regset->set)
400 		return -EOPNOTSUPP;
401 
402 	if (!access_ok(data, size))
403 		return -EFAULT;
404 
405 	return regset->set(target, regset, offset, size, NULL, data);
406 }
407 
408 /**
409  * regset_size - determine the current size of a regset
410  * @target:	thread to be examined
411  * @regset:	regset to be examined
412  *
413  * Note that the returned size is valid only until the next time
414  * (if any) @regset is modified for @target.
415  */
regset_size(struct task_struct * target,const struct user_regset * regset)416 static inline unsigned int regset_size(struct task_struct *target,
417 				       const struct user_regset *regset)
418 {
419 	if (!regset->get_size)
420 		return regset->n * regset->size;
421 	else
422 		return regset->get_size(target, regset);
423 }
424 
425 #endif	/* <linux/regset.h> */
426