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