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