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