![]() |
|
|||
0001 /* SPDX-License-Identifier: GPL-2.0-only */ 0002 /* 0003 * User-mode machine state access 0004 * 0005 * Copyright (C) 2007 Red Hat, Inc. All rights reserved. 0006 * 0007 * Red Hat Author: Roland McGrath. 0008 */ 0009 0010 #ifndef _LINUX_REGSET_H 0011 #define _LINUX_REGSET_H 1 0012 0013 #include <linux/compiler.h> 0014 #include <linux/types.h> 0015 #include <linux/bug.h> 0016 #include <linux/uaccess.h> 0017 struct task_struct; 0018 struct user_regset; 0019 0020 struct membuf { 0021 void *p; 0022 size_t left; 0023 }; 0024 0025 static inline int membuf_zero(struct membuf *s, size_t size) 0026 { 0027 if (s->left) { 0028 if (size > s->left) 0029 size = s->left; 0030 memset(s->p, 0, size); 0031 s->p += size; 0032 s->left -= size; 0033 } 0034 return s->left; 0035 } 0036 0037 static inline int membuf_write(struct membuf *s, const void *v, size_t size) 0038 { 0039 if (s->left) { 0040 if (size > s->left) 0041 size = s->left; 0042 memcpy(s->p, v, size); 0043 s->p += size; 0044 s->left -= size; 0045 } 0046 return s->left; 0047 } 0048 0049 static inline struct membuf membuf_at(const struct membuf *s, size_t offs) 0050 { 0051 struct membuf n = *s; 0052 0053 if (offs > n.left) 0054 offs = n.left; 0055 n.p += offs; 0056 n.left -= offs; 0057 0058 return n; 0059 } 0060 0061 /* current s->p must be aligned for v; v must be a scalar */ 0062 #define membuf_store(s, v) \ 0063 ({ \ 0064 struct membuf *__s = (s); \ 0065 if (__s->left) { \ 0066 typeof(v) __v = (v); \ 0067 size_t __size = sizeof(__v); \ 0068 if (unlikely(__size > __s->left)) { \ 0069 __size = __s->left; \ 0070 memcpy(__s->p, &__v, __size); \ 0071 } else { \ 0072 *(typeof(__v + 0) *)__s->p = __v; \ 0073 } \ 0074 __s->p += __size; \ 0075 __s->left -= __size; \ 0076 } \ 0077 __s->left;}) 0078 0079 /** 0080 * user_regset_active_fn - type of @active function in &struct user_regset 0081 * @target: thread being examined 0082 * @regset: regset being examined 0083 * 0084 * Return -%ENODEV if not available on the hardware found. 0085 * Return %0 if no interesting state in this thread. 0086 * Return >%0 number of @size units of interesting state. 0087 * Any get call fetching state beyond that number will 0088 * see the default initialization state for this data, 0089 * so a caller that knows what the default state is need 0090 * not copy it all out. 0091 * This call is optional; the pointer is %NULL if there 0092 * is no inexpensive check to yield a value < @n. 0093 */ 0094 typedef int user_regset_active_fn(struct task_struct *target, 0095 const struct user_regset *regset); 0096 0097 typedef int user_regset_get2_fn(struct task_struct *target, 0098 const struct user_regset *regset, 0099 struct membuf to); 0100 0101 /** 0102 * user_regset_set_fn - type of @set function in &struct user_regset 0103 * @target: thread being examined 0104 * @regset: regset being examined 0105 * @pos: offset into the regset data to access, in bytes 0106 * @count: amount of data to copy, in bytes 0107 * @kbuf: if not %NULL, a kernel-space pointer to copy from 0108 * @ubuf: if @kbuf is %NULL, a user-space pointer to copy from 0109 * 0110 * Store register values. Return %0 on success; -%EIO or -%ENODEV 0111 * are usual failure returns. The @pos and @count values are in 0112 * bytes, but must be properly aligned. If @kbuf is non-null, that 0113 * buffer is used and @ubuf is ignored. If @kbuf is %NULL, then 0114 * ubuf gives a userland pointer to access directly, and an -%EFAULT 0115 * return value is possible. 0116 */ 0117 typedef int user_regset_set_fn(struct task_struct *target, 0118 const struct user_regset *regset, 0119 unsigned int pos, unsigned int count, 0120 const void *kbuf, const void __user *ubuf); 0121 0122 /** 0123 * user_regset_writeback_fn - type of @writeback function in &struct user_regset 0124 * @target: thread being examined 0125 * @regset: regset being examined 0126 * @immediate: zero if writeback at completion of next context switch is OK 0127 * 0128 * This call is optional; usually the pointer is %NULL. When 0129 * provided, there is some user memory associated with this regset's 0130 * hardware, such as memory backing cached register data on register 0131 * window machines; the regset's data controls what user memory is 0132 * used (e.g. via the stack pointer value). 0133 * 0134 * Write register data back to user memory. If the @immediate flag 0135 * is nonzero, it must be written to the user memory so uaccess or 0136 * access_process_vm() can see it when this call returns; if zero, 0137 * then it must be written back by the time the task completes a 0138 * context switch (as synchronized with wait_task_inactive()). 0139 * Return %0 on success or if there was nothing to do, -%EFAULT for 0140 * a memory problem (bad stack pointer or whatever), or -%EIO for a 0141 * hardware problem. 0142 */ 0143 typedef int user_regset_writeback_fn(struct task_struct *target, 0144 const struct user_regset *regset, 0145 int immediate); 0146 0147 /** 0148 * struct user_regset - accessible thread CPU state 0149 * @n: Number of slots (registers). 0150 * @size: Size in bytes of a slot (register). 0151 * @align: Required alignment, in bytes. 0152 * @bias: Bias from natural indexing. 0153 * @core_note_type: ELF note @n_type value used in core dumps. 0154 * @get: Function to fetch values. 0155 * @set: Function to store values. 0156 * @active: Function to report if regset is active, or %NULL. 0157 * @writeback: Function to write data back to user memory, or %NULL. 0158 * 0159 * This data structure describes a machine resource we call a register set. 0160 * This is part of the state of an individual thread, not necessarily 0161 * actual CPU registers per se. A register set consists of a number of 0162 * similar slots, given by @n. Each slot is @size bytes, and aligned to 0163 * @align bytes (which is at least @size). For dynamically-sized 0164 * regsets, @n must contain the maximum possible number of slots for the 0165 * regset. 0166 * 0167 * For backward compatibility, the @get and @set methods must pad to, or 0168 * accept, @n * @size bytes, even if the current regset size is smaller. 0169 * The precise semantics of these operations depend on the regset being 0170 * accessed. 0171 * 0172 * The functions to which &struct user_regset members point must be 0173 * called only on the current thread or on a thread that is in 0174 * %TASK_STOPPED or %TASK_TRACED state, that we are guaranteed will not 0175 * be woken up and return to user mode, and that we have called 0176 * wait_task_inactive() on. (The target thread always might wake up for 0177 * SIGKILL while these functions are working, in which case that 0178 * thread's user_regset state might be scrambled.) 0179 * 0180 * The @pos argument must be aligned according to @align; the @count 0181 * argument must be a multiple of @size. These functions are not 0182 * responsible for checking for invalid arguments. 0183 * 0184 * When there is a natural value to use as an index, @bias gives the 0185 * difference between the natural index and the slot index for the 0186 * register set. For example, x86 GDT segment descriptors form a regset; 0187 * the segment selector produces a natural index, but only a subset of 0188 * that index space is available as a regset (the TLS slots); subtracting 0189 * @bias from a segment selector index value computes the regset slot. 0190 * 0191 * If nonzero, @core_note_type gives the n_type field (NT_* value) 0192 * of the core file note in which this regset's data appears. 0193 * NT_PRSTATUS is a special case in that the regset data starts at 0194 * offsetof(struct elf_prstatus, pr_reg) into the note data; that is 0195 * part of the per-machine ELF formats userland knows about. In 0196 * other cases, the core file note contains exactly the whole regset 0197 * (@n * @size) and nothing else. The core file note is normally 0198 * omitted when there is an @active function and it returns zero. 0199 */ 0200 struct user_regset { 0201 user_regset_get2_fn *regset_get; 0202 user_regset_set_fn *set; 0203 user_regset_active_fn *active; 0204 user_regset_writeback_fn *writeback; 0205 unsigned int n; 0206 unsigned int size; 0207 unsigned int align; 0208 unsigned int bias; 0209 unsigned int core_note_type; 0210 }; 0211 0212 /** 0213 * struct user_regset_view - available regsets 0214 * @name: Identifier, e.g. UTS_MACHINE string. 0215 * @regsets: Array of @n regsets available in this view. 0216 * @n: Number of elements in @regsets. 0217 * @e_machine: ELF header @e_machine %EM_* value written in core dumps. 0218 * @e_flags: ELF header @e_flags value written in core dumps. 0219 * @ei_osabi: ELF header @e_ident[%EI_OSABI] value written in core dumps. 0220 * 0221 * A regset view is a collection of regsets (&struct user_regset, 0222 * above). This describes all the state of a thread that can be seen 0223 * from a given architecture/ABI environment. More than one view might 0224 * refer to the same &struct user_regset, or more than one regset 0225 * might refer to the same machine-specific state in the thread. For 0226 * example, a 32-bit thread's state could be examined from the 32-bit 0227 * view or from the 64-bit view. Either method reaches the same thread 0228 * register state, doing appropriate widening or truncation. 0229 */ 0230 struct user_regset_view { 0231 const char *name; 0232 const struct user_regset *regsets; 0233 unsigned int n; 0234 u32 e_flags; 0235 u16 e_machine; 0236 u8 ei_osabi; 0237 }; 0238 0239 /* 0240 * This is documented here rather than at the definition sites because its 0241 * implementation is machine-dependent but its interface is universal. 0242 */ 0243 /** 0244 * task_user_regset_view - Return the process's native regset view. 0245 * @tsk: a thread of the process in question 0246 * 0247 * Return the &struct user_regset_view that is native for the given process. 0248 * For example, what it would access when it called ptrace(). 0249 * Throughout the life of the process, this only changes at exec. 0250 */ 0251 const struct user_regset_view *task_user_regset_view(struct task_struct *tsk); 0252 0253 static inline int user_regset_copyin(unsigned int *pos, unsigned int *count, 0254 const void **kbuf, 0255 const void __user **ubuf, void *data, 0256 const int start_pos, const int end_pos) 0257 { 0258 if (*count == 0) 0259 return 0; 0260 BUG_ON(*pos < start_pos); 0261 if (end_pos < 0 || *pos < end_pos) { 0262 unsigned int copy = (end_pos < 0 ? *count 0263 : min(*count, end_pos - *pos)); 0264 data += *pos - start_pos; 0265 if (*kbuf) { 0266 memcpy(data, *kbuf, copy); 0267 *kbuf += copy; 0268 } else if (__copy_from_user(data, *ubuf, copy)) 0269 return -EFAULT; 0270 else 0271 *ubuf += copy; 0272 *pos += copy; 0273 *count -= copy; 0274 } 0275 return 0; 0276 } 0277 0278 static inline int user_regset_copyin_ignore(unsigned int *pos, 0279 unsigned int *count, 0280 const void **kbuf, 0281 const void __user **ubuf, 0282 const int start_pos, 0283 const int end_pos) 0284 { 0285 if (*count == 0) 0286 return 0; 0287 BUG_ON(*pos < start_pos); 0288 if (end_pos < 0 || *pos < end_pos) { 0289 unsigned int copy = (end_pos < 0 ? *count 0290 : min(*count, end_pos - *pos)); 0291 if (*kbuf) 0292 *kbuf += copy; 0293 else 0294 *ubuf += copy; 0295 *pos += copy; 0296 *count -= copy; 0297 } 0298 return 0; 0299 } 0300 0301 extern int regset_get(struct task_struct *target, 0302 const struct user_regset *regset, 0303 unsigned int size, void *data); 0304 0305 extern int regset_get_alloc(struct task_struct *target, 0306 const struct user_regset *regset, 0307 unsigned int size, 0308 void **data); 0309 0310 extern int copy_regset_to_user(struct task_struct *target, 0311 const struct user_regset_view *view, 0312 unsigned int setno, unsigned int offset, 0313 unsigned int size, void __user *data); 0314 0315 /** 0316 * copy_regset_from_user - store into thread's user_regset data from user memory 0317 * @target: thread to be examined 0318 * @view: &struct user_regset_view describing user thread machine state 0319 * @setno: index in @view->regsets 0320 * @offset: offset into the regset data, in bytes 0321 * @size: amount of data to copy, in bytes 0322 * @data: user-mode pointer to copy from 0323 */ 0324 static inline int copy_regset_from_user(struct task_struct *target, 0325 const struct user_regset_view *view, 0326 unsigned int setno, 0327 unsigned int offset, unsigned int size, 0328 const void __user *data) 0329 { 0330 const struct user_regset *regset = &view->regsets[setno]; 0331 0332 if (!regset->set) 0333 return -EOPNOTSUPP; 0334 0335 if (!access_ok(data, size)) 0336 return -EFAULT; 0337 0338 return regset->set(target, regset, offset, size, NULL, data); 0339 } 0340 0341 #endif /* <linux/regset.h> */
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.1.0 LXR engine. The LXR team |
![]() ![]() |