1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * User-mode machine state access
5 * Copyright (C) 2007 Red Hat, Inc. All rights reserved.
7 * Red Hat Author: Roland McGrath.
10 #ifndef _LINUX_REGSET_H
11 #define _LINUX_REGSET_H 1
13 #include <linux/compiler.h>
14 #include <linux/types.h>
15 #include <linux/bug.h>
16 #include <linux/uaccess.h>
22 * user_regset_active_fn - type of @active function in &struct user_regset
23 * @target: thread being examined
24 * @regset: regset being examined
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.
36 typedef int user_regset_active_fn(struct task_struct *target,
37 const struct user_regset *regset);
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
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.
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);
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
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.
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);
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
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).
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
102 typedef int user_regset_writeback_fn(struct task_struct *target,
103 const struct user_regset *regset,
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
111 * This call is optional; usually the pointer is %NULL.
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.
119 * This function is intended for dynamically sized regsets. A regset
120 * that is statically sized does not need to implement it.
122 * This function should not be called directly: instead, callers should
123 * call regset_size() to determine the current size of a regset.
125 typedef unsigned int user_regset_get_size_fn(struct task_struct *target,
126 const struct user_regset *regset);
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.
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.
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.
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
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.)
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.
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.
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.
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;
197 unsigned int core_note_type;
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.
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.
218 struct user_regset_view {
220 const struct user_regset *regsets;
228 * This is documented here rather than at the definition sites because its
229 * implementation is machine-dependent but its interface is universal.
232 * task_user_regset_view - Return the process's native regset view.
233 * @tsk: a thread of the process in question
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.
239 const struct user_regset_view *task_user_regset_view(struct task_struct *tsk);
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.
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.
254 static inline int user_regset_copyout(unsigned int *pos, unsigned int *count,
256 void __user **ubuf, const void *data,
257 const int start_pos, const int end_pos)
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;
267 memcpy(*kbuf, data, copy);
269 } else if (__copy_to_user(*ubuf, data, copy))
279 static inline int user_regset_copyin(unsigned int *pos, unsigned int *count,
281 const void __user **ubuf, void *data,
282 const int start_pos, const int end_pos)
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;
292 memcpy(data, *kbuf, copy);
294 } else if (__copy_from_user(data, *ubuf, copy))
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.
308 static inline int user_regset_copyout_zero(unsigned int *pos,
310 void **kbuf, void __user **ubuf,
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));
321 memset(*kbuf, 0, copy);
323 } else if (__clear_user(*ubuf, copy))
333 static inline int user_regset_copyin_ignore(unsigned int *pos,
336 const void __user **ubuf,
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));
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
365 static inline int copy_regset_to_user(struct task_struct *target,
366 const struct user_regset_view *view,
368 unsigned int offset, unsigned int size,
371 const struct user_regset *regset = &view->regsets[setno];
376 if (!access_ok(data, size))
379 return regset->get(target, regset, offset, size, NULL, data);
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
391 static inline int copy_regset_from_user(struct task_struct *target,
392 const struct user_regset_view *view,
394 unsigned int offset, unsigned int size,
395 const void __user *data)
397 const struct user_regset *regset = &view->regsets[setno];
402 if (!access_ok(data, size))
405 return regset->set(target, regset, offset, size, NULL, data);
409 * regset_size - determine the current size of a regset
410 * @target: thread to be examined
411 * @regset: regset to be examined
413 * Note that the returned size is valid only until the next time
414 * (if any) @regset is modified for @target.
416 static inline unsigned int regset_size(struct task_struct *target,
417 const struct user_regset *regset)
419 if (!regset->get_size)
420 return regset->n * regset->size;
422 return regset->get_size(target, regset);
425 #endif /* <linux/regset.h> */