xref: /linux-6.15/include/linux/regset.h (revision 597d77d2)
12522fe45SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2bdf88217SRoland McGrath /*
3bdf88217SRoland McGrath  * User-mode machine state access
4bdf88217SRoland McGrath  *
5bdf88217SRoland McGrath  * Copyright (C) 2007 Red Hat, Inc.  All rights reserved.
6bdf88217SRoland McGrath  *
7bdf88217SRoland McGrath  * Red Hat Author: Roland McGrath.
8bdf88217SRoland McGrath  */
9bdf88217SRoland McGrath 
10bdf88217SRoland McGrath #ifndef _LINUX_REGSET_H
11bdf88217SRoland McGrath #define _LINUX_REGSET_H	1
12bdf88217SRoland McGrath 
13bdf88217SRoland McGrath #include <linux/compiler.h>
14bdf88217SRoland McGrath #include <linux/types.h>
15187f1882SPaul Gortmaker #include <linux/bug.h>
16bae3f7c3SRoland McGrath #include <linux/uaccess.h>
17bdf88217SRoland McGrath struct task_struct;
18bdf88217SRoland McGrath struct user_regset;
19bdf88217SRoland McGrath 
207717cb9bSAl Viro struct membuf {
217717cb9bSAl Viro 	void *p;
227717cb9bSAl Viro 	size_t left;
237717cb9bSAl Viro };
247717cb9bSAl Viro 
membuf_zero(struct membuf * s,size_t size)257717cb9bSAl Viro static inline int membuf_zero(struct membuf *s, size_t size)
267717cb9bSAl Viro {
277717cb9bSAl Viro 	if (s->left) {
287717cb9bSAl Viro 		if (size > s->left)
297717cb9bSAl Viro 			size = s->left;
307717cb9bSAl Viro 		memset(s->p, 0, size);
317717cb9bSAl Viro 		s->p += size;
327717cb9bSAl Viro 		s->left -= size;
337717cb9bSAl Viro 	}
347717cb9bSAl Viro 	return s->left;
357717cb9bSAl Viro }
367717cb9bSAl Viro 
membuf_write(struct membuf * s,const void * v,size_t size)377717cb9bSAl Viro static inline int membuf_write(struct membuf *s, const void *v, size_t size)
387717cb9bSAl Viro {
397717cb9bSAl Viro 	if (s->left) {
407717cb9bSAl Viro 		if (size > s->left)
417717cb9bSAl Viro 			size = s->left;
427717cb9bSAl Viro 		memcpy(s->p, v, size);
437717cb9bSAl Viro 		s->p += size;
447717cb9bSAl Viro 		s->left -= size;
457717cb9bSAl Viro 	}
467717cb9bSAl Viro 	return s->left;
477717cb9bSAl Viro }
487717cb9bSAl Viro 
membuf_at(const struct membuf * s,size_t offs)49640586f8SOleg Nesterov static inline struct membuf membuf_at(const struct membuf *s, size_t offs)
50640586f8SOleg Nesterov {
51640586f8SOleg Nesterov 	struct membuf n = *s;
52640586f8SOleg Nesterov 
53640586f8SOleg Nesterov 	if (offs > n.left)
54640586f8SOleg Nesterov 		offs = n.left;
55640586f8SOleg Nesterov 	n.p += offs;
56640586f8SOleg Nesterov 	n.left -= offs;
57640586f8SOleg Nesterov 
58640586f8SOleg Nesterov 	return n;
59640586f8SOleg Nesterov }
60640586f8SOleg Nesterov 
617717cb9bSAl Viro /* current s->p must be aligned for v; v must be a scalar */
627717cb9bSAl Viro #define membuf_store(s, v)				\
637717cb9bSAl Viro ({							\
647717cb9bSAl Viro 	struct membuf *__s = (s);			\
657717cb9bSAl Viro         if (__s->left) {				\
667717cb9bSAl Viro 		typeof(v) __v = (v);			\
677717cb9bSAl Viro 		size_t __size = sizeof(__v);		\
687717cb9bSAl Viro 		if (unlikely(__size > __s->left)) {	\
697717cb9bSAl Viro 			__size = __s->left;		\
707717cb9bSAl Viro 			memcpy(__s->p, &__v, __size);	\
717717cb9bSAl Viro 		} else {				\
727717cb9bSAl Viro 			*(typeof(__v + 0) *)__s->p = __v;	\
737717cb9bSAl Viro 		}					\
747717cb9bSAl Viro 		__s->p += __size;			\
757717cb9bSAl Viro 		__s->left -= __size;			\
767717cb9bSAl Viro 	}						\
777717cb9bSAl Viro 	__s->left;})
78bdf88217SRoland McGrath 
79bdf88217SRoland McGrath /**
80bdf88217SRoland McGrath  * user_regset_active_fn - type of @active function in &struct user_regset
81bdf88217SRoland McGrath  * @target:	thread being examined
82bdf88217SRoland McGrath  * @regset:	regset being examined
83bdf88217SRoland McGrath  *
84bdf88217SRoland McGrath  * Return -%ENODEV if not available on the hardware found.
85bdf88217SRoland McGrath  * Return %0 if no interesting state in this thread.
86bdf88217SRoland McGrath  * Return >%0 number of @size units of interesting state.
87bdf88217SRoland McGrath  * Any get call fetching state beyond that number will
88bdf88217SRoland McGrath  * see the default initialization state for this data,
89bdf88217SRoland McGrath  * so a caller that knows what the default state is need
90bdf88217SRoland McGrath  * not copy it all out.
91bdf88217SRoland McGrath  * This call is optional; the pointer is %NULL if there
92bdf88217SRoland McGrath  * is no inexpensive check to yield a value < @n.
93bdf88217SRoland McGrath  */
94bdf88217SRoland McGrath typedef int user_regset_active_fn(struct task_struct *target,
95bdf88217SRoland McGrath 				  const struct user_regset *regset);
96bdf88217SRoland McGrath 
977717cb9bSAl Viro typedef int user_regset_get2_fn(struct task_struct *target,
987717cb9bSAl Viro 			       const struct user_regset *regset,
997717cb9bSAl Viro 			       struct membuf to);
1007717cb9bSAl Viro 
101bdf88217SRoland McGrath /**
102bdf88217SRoland McGrath  * user_regset_set_fn - type of @set function in &struct user_regset
103bdf88217SRoland McGrath  * @target:	thread being examined
104bdf88217SRoland McGrath  * @regset:	regset being examined
105bdf88217SRoland McGrath  * @pos:	offset into the regset data to access, in bytes
106bdf88217SRoland McGrath  * @count:	amount of data to copy, in bytes
107bdf88217SRoland McGrath  * @kbuf:	if not %NULL, a kernel-space pointer to copy from
108bdf88217SRoland McGrath  * @ubuf:	if @kbuf is %NULL, a user-space pointer to copy from
109bdf88217SRoland McGrath  *
110bdf88217SRoland McGrath  * Store register values.  Return %0 on success; -%EIO or -%ENODEV
111bdf88217SRoland McGrath  * are usual failure returns.  The @pos and @count values are in
112bdf88217SRoland McGrath  * bytes, but must be properly aligned.  If @kbuf is non-null, that
113bdf88217SRoland McGrath  * buffer is used and @ubuf is ignored.  If @kbuf is %NULL, then
114bdf88217SRoland McGrath  * ubuf gives a userland pointer to access directly, and an -%EFAULT
115bdf88217SRoland McGrath  * return value is possible.
116bdf88217SRoland McGrath  */
117bdf88217SRoland McGrath typedef int user_regset_set_fn(struct task_struct *target,
118bdf88217SRoland McGrath 			       const struct user_regset *regset,
119bdf88217SRoland McGrath 			       unsigned int pos, unsigned int count,
120bdf88217SRoland McGrath 			       const void *kbuf, const void __user *ubuf);
121bdf88217SRoland McGrath 
122bdf88217SRoland McGrath /**
123bdf88217SRoland McGrath  * user_regset_writeback_fn - type of @writeback function in &struct user_regset
124bdf88217SRoland McGrath  * @target:	thread being examined
125bdf88217SRoland McGrath  * @regset:	regset being examined
126bdf88217SRoland McGrath  * @immediate:	zero if writeback at completion of next context switch is OK
127bdf88217SRoland McGrath  *
128bdf88217SRoland McGrath  * This call is optional; usually the pointer is %NULL.  When
129bdf88217SRoland McGrath  * provided, there is some user memory associated with this regset's
130bdf88217SRoland McGrath  * hardware, such as memory backing cached register data on register
131bdf88217SRoland McGrath  * window machines; the regset's data controls what user memory is
132bdf88217SRoland McGrath  * used (e.g. via the stack pointer value).
133bdf88217SRoland McGrath  *
134bdf88217SRoland McGrath  * Write register data back to user memory.  If the @immediate flag
135bdf88217SRoland McGrath  * is nonzero, it must be written to the user memory so uaccess or
136bdf88217SRoland McGrath  * access_process_vm() can see it when this call returns; if zero,
137bdf88217SRoland McGrath  * then it must be written back by the time the task completes a
138bdf88217SRoland McGrath  * context switch (as synchronized with wait_task_inactive()).
139bdf88217SRoland McGrath  * Return %0 on success or if there was nothing to do, -%EFAULT for
140bdf88217SRoland McGrath  * a memory problem (bad stack pointer or whatever), or -%EIO for a
141bdf88217SRoland McGrath  * hardware problem.
142bdf88217SRoland McGrath  */
143bdf88217SRoland McGrath typedef int user_regset_writeback_fn(struct task_struct *target,
144bdf88217SRoland McGrath 				     const struct user_regset *regset,
145bdf88217SRoland McGrath 				     int immediate);
146bdf88217SRoland McGrath 
147bdf88217SRoland McGrath /**
148bdf88217SRoland McGrath  * struct user_regset - accessible thread CPU state
149bdf88217SRoland McGrath  * @n:			Number of slots (registers).
150bdf88217SRoland McGrath  * @size:		Size in bytes of a slot (register).
151bdf88217SRoland McGrath  * @align:		Required alignment, in bytes.
152bdf88217SRoland McGrath  * @bias:		Bias from natural indexing.
153bdf88217SRoland McGrath  * @core_note_type:	ELF note @n_type value used in core dumps.
154bdf88217SRoland McGrath  * @get:		Function to fetch values.
155bdf88217SRoland McGrath  * @set:		Function to store values.
156bdf88217SRoland McGrath  * @active:		Function to report if regset is active, or %NULL.
157bdf88217SRoland McGrath  * @writeback:		Function to write data back to user memory, or %NULL.
158bdf88217SRoland McGrath  *
159bdf88217SRoland McGrath  * This data structure describes a machine resource we call a register set.
160bdf88217SRoland McGrath  * This is part of the state of an individual thread, not necessarily
161bdf88217SRoland McGrath  * actual CPU registers per se.  A register set consists of a number of
162bdf88217SRoland McGrath  * similar slots, given by @n.  Each slot is @size bytes, and aligned to
16327e64b4bSDave Martin  * @align bytes (which is at least @size).  For dynamically-sized
16427e64b4bSDave Martin  * regsets, @n must contain the maximum possible number of slots for the
165c522401eSAl Viro  * regset.
16627e64b4bSDave Martin  *
16727e64b4bSDave Martin  * For backward compatibility, the @get and @set methods must pad to, or
16827e64b4bSDave Martin  * accept, @n * @size bytes, even if the current regset size is smaller.
16927e64b4bSDave Martin  * The precise semantics of these operations depend on the regset being
17027e64b4bSDave Martin  * accessed.
17127e64b4bSDave Martin  *
17227e64b4bSDave Martin  * The functions to which &struct user_regset members point must be
17327e64b4bSDave Martin  * called only on the current thread or on a thread that is in
17427e64b4bSDave Martin  * %TASK_STOPPED or %TASK_TRACED state, that we are guaranteed will not
17527e64b4bSDave Martin  * be woken up and return to user mode, and that we have called
17627e64b4bSDave Martin  * wait_task_inactive() on.  (The target thread always might wake up for
17727e64b4bSDave Martin  * SIGKILL while these functions are working, in which case that
17827e64b4bSDave Martin  * thread's user_regset state might be scrambled.)
179bdf88217SRoland McGrath  *
180bdf88217SRoland McGrath  * The @pos argument must be aligned according to @align; the @count
181bdf88217SRoland McGrath  * argument must be a multiple of @size.  These functions are not
182bdf88217SRoland McGrath  * responsible for checking for invalid arguments.
183bdf88217SRoland McGrath  *
184bdf88217SRoland McGrath  * When there is a natural value to use as an index, @bias gives the
185bdf88217SRoland McGrath  * difference between the natural index and the slot index for the
186bdf88217SRoland McGrath  * register set.  For example, x86 GDT segment descriptors form a regset;
187bdf88217SRoland McGrath  * the segment selector produces a natural index, but only a subset of
188bdf88217SRoland McGrath  * that index space is available as a regset (the TLS slots); subtracting
189bdf88217SRoland McGrath  * @bias from a segment selector index value computes the regset slot.
190bdf88217SRoland McGrath  *
191bdf88217SRoland McGrath  * If nonzero, @core_note_type gives the n_type field (NT_* value)
192bdf88217SRoland McGrath  * of the core file note in which this regset's data appears.
193bdf88217SRoland McGrath  * NT_PRSTATUS is a special case in that the regset data starts at
194bdf88217SRoland McGrath  * offsetof(struct elf_prstatus, pr_reg) into the note data; that is
195bdf88217SRoland McGrath  * part of the per-machine ELF formats userland knows about.  In
196bdf88217SRoland McGrath  * other cases, the core file note contains exactly the whole regset
197bdf88217SRoland McGrath  * (@n * @size) and nothing else.  The core file note is normally
198bdf88217SRoland McGrath  * omitted when there is an @active function and it returns zero.
199bdf88217SRoland McGrath  */
200bdf88217SRoland McGrath struct user_regset {
2017717cb9bSAl Viro 	user_regset_get2_fn		*regset_get;
202bdf88217SRoland McGrath 	user_regset_set_fn		*set;
203bdf88217SRoland McGrath 	user_regset_active_fn		*active;
204bdf88217SRoland McGrath 	user_regset_writeback_fn	*writeback;
205bdf88217SRoland McGrath 	unsigned int			n;
206bdf88217SRoland McGrath 	unsigned int 			size;
207bdf88217SRoland McGrath 	unsigned int 			align;
208bdf88217SRoland McGrath 	unsigned int 			bias;
209bdf88217SRoland McGrath 	unsigned int 			core_note_type;
210bdf88217SRoland McGrath };
211bdf88217SRoland McGrath 
212bdf88217SRoland McGrath /**
213bdf88217SRoland McGrath  * struct user_regset_view - available regsets
214bdf88217SRoland McGrath  * @name:	Identifier, e.g. UTS_MACHINE string.
215bdf88217SRoland McGrath  * @regsets:	Array of @n regsets available in this view.
216bdf88217SRoland McGrath  * @n:		Number of elements in @regsets.
217bdf88217SRoland McGrath  * @e_machine:	ELF header @e_machine %EM_* value written in core dumps.
218bdf88217SRoland McGrath  * @e_flags:	ELF header @e_flags value written in core dumps.
219bdf88217SRoland McGrath  * @ei_osabi:	ELF header @e_ident[%EI_OSABI] value written in core dumps.
220bdf88217SRoland McGrath  *
221bdf88217SRoland McGrath  * A regset view is a collection of regsets (&struct user_regset,
222bdf88217SRoland McGrath  * above).  This describes all the state of a thread that can be seen
223bdf88217SRoland McGrath  * from a given architecture/ABI environment.  More than one view might
224bdf88217SRoland McGrath  * refer to the same &struct user_regset, or more than one regset
225bdf88217SRoland McGrath  * might refer to the same machine-specific state in the thread.  For
226bdf88217SRoland McGrath  * example, a 32-bit thread's state could be examined from the 32-bit
227bdf88217SRoland McGrath  * view or from the 64-bit view.  Either method reaches the same thread
228bdf88217SRoland McGrath  * register state, doing appropriate widening or truncation.
229bdf88217SRoland McGrath  */
230bdf88217SRoland McGrath struct user_regset_view {
231bdf88217SRoland McGrath 	const char *name;
232bdf88217SRoland McGrath 	const struct user_regset *regsets;
233bdf88217SRoland McGrath 	unsigned int n;
234bdf88217SRoland McGrath 	u32 e_flags;
235bdf88217SRoland McGrath 	u16 e_machine;
236bdf88217SRoland McGrath 	u8 ei_osabi;
237bdf88217SRoland McGrath };
238bdf88217SRoland McGrath 
239bdf88217SRoland McGrath /*
240bdf88217SRoland McGrath  * This is documented here rather than at the definition sites because its
241bdf88217SRoland McGrath  * implementation is machine-dependent but its interface is universal.
242bdf88217SRoland McGrath  */
243bdf88217SRoland McGrath /**
244bdf88217SRoland McGrath  * task_user_regset_view - Return the process's native regset view.
245bdf88217SRoland McGrath  * @tsk: a thread of the process in question
246bdf88217SRoland McGrath  *
247bdf88217SRoland McGrath  * Return the &struct user_regset_view that is native for the given process.
248bdf88217SRoland McGrath  * For example, what it would access when it called ptrace().
249bdf88217SRoland McGrath  * Throughout the life of the process, this only changes at exec.
250bdf88217SRoland McGrath  */
251bdf88217SRoland McGrath const struct user_regset_view *task_user_regset_view(struct task_struct *tsk);
252bdf88217SRoland McGrath 
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)253bae3f7c3SRoland McGrath static inline int user_regset_copyin(unsigned int *pos, unsigned int *count,
254bae3f7c3SRoland McGrath 				     const void **kbuf,
255bae3f7c3SRoland McGrath 				     const void __user **ubuf, void *data,
256bae3f7c3SRoland McGrath 				     const int start_pos, const int end_pos)
257bae3f7c3SRoland McGrath {
258bae3f7c3SRoland McGrath 	if (*count == 0)
259bae3f7c3SRoland McGrath 		return 0;
260bae3f7c3SRoland McGrath 	BUG_ON(*pos < start_pos);
261bae3f7c3SRoland McGrath 	if (end_pos < 0 || *pos < end_pos) {
262bae3f7c3SRoland McGrath 		unsigned int copy = (end_pos < 0 ? *count
263bae3f7c3SRoland McGrath 				     : min(*count, end_pos - *pos));
264bae3f7c3SRoland McGrath 		data += *pos - start_pos;
265bae3f7c3SRoland McGrath 		if (*kbuf) {
266bae3f7c3SRoland McGrath 			memcpy(data, *kbuf, copy);
267bae3f7c3SRoland McGrath 			*kbuf += copy;
268bae3f7c3SRoland McGrath 		} else if (__copy_from_user(data, *ubuf, copy))
269bae3f7c3SRoland McGrath 			return -EFAULT;
270bae3f7c3SRoland McGrath 		else
271bae3f7c3SRoland McGrath 			*ubuf += copy;
272bae3f7c3SRoland McGrath 		*pos += copy;
273bae3f7c3SRoland McGrath 		*count -= copy;
274bae3f7c3SRoland McGrath 	}
275bae3f7c3SRoland McGrath 	return 0;
276bae3f7c3SRoland McGrath }
277bae3f7c3SRoland McGrath 
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)278*597d77d2SSergey Shtylyov static inline void user_regset_copyin_ignore(unsigned int *pos,
279bae3f7c3SRoland McGrath 					     unsigned int *count,
280bae3f7c3SRoland McGrath 					     const void **kbuf,
281bae3f7c3SRoland McGrath 					     const void __user **ubuf,
282bae3f7c3SRoland McGrath 					     const int start_pos,
283bae3f7c3SRoland McGrath 					     const int end_pos)
284bae3f7c3SRoland McGrath {
285bae3f7c3SRoland McGrath 	if (*count == 0)
286*597d77d2SSergey Shtylyov 		return;
287bae3f7c3SRoland McGrath 	BUG_ON(*pos < start_pos);
288bae3f7c3SRoland McGrath 	if (end_pos < 0 || *pos < end_pos) {
289bae3f7c3SRoland McGrath 		unsigned int copy = (end_pos < 0 ? *count
290bae3f7c3SRoland McGrath 				     : min(*count, end_pos - *pos));
291bae3f7c3SRoland McGrath 		if (*kbuf)
292bae3f7c3SRoland McGrath 			*kbuf += copy;
293bae3f7c3SRoland McGrath 		else
294bae3f7c3SRoland McGrath 			*ubuf += copy;
295bae3f7c3SRoland McGrath 		*pos += copy;
296bae3f7c3SRoland McGrath 		*count -= copy;
297bae3f7c3SRoland McGrath 	}
298bae3f7c3SRoland McGrath }
299bae3f7c3SRoland McGrath 
300b4e9c954SAl Viro extern int regset_get(struct task_struct *target,
301b4e9c954SAl Viro 		      const struct user_regset *regset,
302b4e9c954SAl Viro 		      unsigned int size, void *data);
303b4e9c954SAl Viro 
304b4e9c954SAl Viro extern int regset_get_alloc(struct task_struct *target,
305b4e9c954SAl Viro 			    const struct user_regset *regset,
306b4e9c954SAl Viro 			    unsigned int size,
307b4e9c954SAl Viro 			    void **data);
308b4e9c954SAl Viro 
309dc12d796SAl Viro extern int copy_regset_to_user(struct task_struct *target,
3105bde4d18SRoland McGrath 			       const struct user_regset_view *view,
311dc12d796SAl Viro 			       unsigned int setno, unsigned int offset,
312dc12d796SAl Viro 			       unsigned int size, void __user *data);
3135bde4d18SRoland McGrath 
3145bde4d18SRoland McGrath /**
3155bde4d18SRoland McGrath  * copy_regset_from_user - store into thread's user_regset data from user memory
3165bde4d18SRoland McGrath  * @target:	thread to be examined
3175bde4d18SRoland McGrath  * @view:	&struct user_regset_view describing user thread machine state
3185bde4d18SRoland McGrath  * @setno:	index in @view->regsets
3195bde4d18SRoland McGrath  * @offset:	offset into the regset data, in bytes
3205bde4d18SRoland McGrath  * @size:	amount of data to copy, in bytes
3215bde4d18SRoland McGrath  * @data:	user-mode pointer to copy from
3225bde4d18SRoland McGrath  */
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)3235bde4d18SRoland McGrath static inline int copy_regset_from_user(struct task_struct *target,
3245bde4d18SRoland McGrath 					const struct user_regset_view *view,
3255bde4d18SRoland McGrath 					unsigned int setno,
3265bde4d18SRoland McGrath 					unsigned int offset, unsigned int size,
3275bde4d18SRoland McGrath 					const void __user *data)
3285bde4d18SRoland McGrath {
3295bde4d18SRoland McGrath 	const struct user_regset *regset = &view->regsets[setno];
3305bde4d18SRoland McGrath 
331c8e25258SH. Peter Anvin 	if (!regset->set)
332c8e25258SH. Peter Anvin 		return -EOPNOTSUPP;
333c8e25258SH. Peter Anvin 
33496d4f267SLinus Torvalds 	if (!access_ok(data, size))
3355189fa19SH. Peter Anvin 		return -EFAULT;
3365bde4d18SRoland McGrath 
3375bde4d18SRoland McGrath 	return regset->set(target, regset, offset, size, NULL, data);
3385bde4d18SRoland McGrath }
3395bde4d18SRoland McGrath 
340bdf88217SRoland McGrath #endif	/* <linux/regset.h> */
341