| .. | .. |
|---|
| 1 | +/* SPDX-License-Identifier: GPL-2.0-only */ |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * User-mode machine state access |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2007 Red Hat, Inc. All rights reserved. |
|---|
| 5 | | - * |
|---|
| 6 | | - * This copyrighted material is made available to anyone wishing to use, |
|---|
| 7 | | - * modify, copy, or redistribute it subject to the terms and conditions |
|---|
| 8 | | - * of the GNU General Public License v.2. |
|---|
| 9 | 6 | * |
|---|
| 10 | 7 | * Red Hat Author: Roland McGrath. |
|---|
| 11 | 8 | */ |
|---|
| .. | .. |
|---|
| 20 | 17 | struct task_struct; |
|---|
| 21 | 18 | struct user_regset; |
|---|
| 22 | 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;}) |
|---|
| 23 | 66 | |
|---|
| 24 | 67 | /** |
|---|
| 25 | 68 | * user_regset_active_fn - type of @active function in &struct user_regset |
|---|
| .. | .. |
|---|
| 39 | 82 | typedef int user_regset_active_fn(struct task_struct *target, |
|---|
| 40 | 83 | const struct user_regset *regset); |
|---|
| 41 | 84 | |
|---|
| 42 | | -/** |
|---|
| 43 | | - * user_regset_get_fn - type of @get function in &struct user_regset |
|---|
| 44 | | - * @target: thread being examined |
|---|
| 45 | | - * @regset: regset being examined |
|---|
| 46 | | - * @pos: offset into the regset data to access, in bytes |
|---|
| 47 | | - * @count: amount of data to copy, in bytes |
|---|
| 48 | | - * @kbuf: if not %NULL, a kernel-space pointer to copy into |
|---|
| 49 | | - * @ubuf: if @kbuf is %NULL, a user-space pointer to copy into |
|---|
| 50 | | - * |
|---|
| 51 | | - * Fetch register values. Return %0 on success; -%EIO or -%ENODEV |
|---|
| 52 | | - * are usual failure returns. The @pos and @count values are in |
|---|
| 53 | | - * bytes, but must be properly aligned. If @kbuf is non-null, that |
|---|
| 54 | | - * buffer is used and @ubuf is ignored. If @kbuf is %NULL, then |
|---|
| 55 | | - * ubuf gives a userland pointer to access directly, and an -%EFAULT |
|---|
| 56 | | - * return value is possible. |
|---|
| 57 | | - */ |
|---|
| 58 | | -typedef int user_regset_get_fn(struct task_struct *target, |
|---|
| 85 | +typedef int user_regset_get2_fn(struct task_struct *target, |
|---|
| 59 | 86 | const struct user_regset *regset, |
|---|
| 60 | | - unsigned int pos, unsigned int count, |
|---|
| 61 | | - void *kbuf, void __user *ubuf); |
|---|
| 87 | + struct membuf to); |
|---|
| 62 | 88 | |
|---|
| 63 | 89 | /** |
|---|
| 64 | 90 | * user_regset_set_fn - type of @set function in &struct user_regset |
|---|
| .. | .. |
|---|
| 107 | 133 | int immediate); |
|---|
| 108 | 134 | |
|---|
| 109 | 135 | /** |
|---|
| 110 | | - * user_regset_get_size_fn - type of @get_size function in &struct user_regset |
|---|
| 111 | | - * @target: thread being examined |
|---|
| 112 | | - * @regset: regset being examined |
|---|
| 113 | | - * |
|---|
| 114 | | - * This call is optional; usually the pointer is %NULL. |
|---|
| 115 | | - * |
|---|
| 116 | | - * When provided, this function must return the current size of regset |
|---|
| 117 | | - * data, as observed by the @get function in &struct user_regset. The |
|---|
| 118 | | - * value returned must be a multiple of @size. The returned size is |
|---|
| 119 | | - * required to be valid only until the next time (if any) @regset is |
|---|
| 120 | | - * modified for @target. |
|---|
| 121 | | - * |
|---|
| 122 | | - * This function is intended for dynamically sized regsets. A regset |
|---|
| 123 | | - * that is statically sized does not need to implement it. |
|---|
| 124 | | - * |
|---|
| 125 | | - * This function should not be called directly: instead, callers should |
|---|
| 126 | | - * call regset_size() to determine the current size of a regset. |
|---|
| 127 | | - */ |
|---|
| 128 | | -typedef unsigned int user_regset_get_size_fn(struct task_struct *target, |
|---|
| 129 | | - const struct user_regset *regset); |
|---|
| 130 | | - |
|---|
| 131 | | -/** |
|---|
| 132 | 136 | * struct user_regset - accessible thread CPU state |
|---|
| 133 | 137 | * @n: Number of slots (registers). |
|---|
| 134 | 138 | * @size: Size in bytes of a slot (register). |
|---|
| .. | .. |
|---|
| 139 | 143 | * @set: Function to store values. |
|---|
| 140 | 144 | * @active: Function to report if regset is active, or %NULL. |
|---|
| 141 | 145 | * @writeback: Function to write data back to user memory, or %NULL. |
|---|
| 142 | | - * @get_size: Function to return the regset's size, or %NULL. |
|---|
| 143 | 146 | * |
|---|
| 144 | 147 | * This data structure describes a machine resource we call a register set. |
|---|
| 145 | 148 | * This is part of the state of an individual thread, not necessarily |
|---|
| .. | .. |
|---|
| 147 | 150 | * similar slots, given by @n. Each slot is @size bytes, and aligned to |
|---|
| 148 | 151 | * @align bytes (which is at least @size). For dynamically-sized |
|---|
| 149 | 152 | * regsets, @n must contain the maximum possible number of slots for the |
|---|
| 150 | | - * regset, and @get_size must point to a function that returns the |
|---|
| 151 | | - * current regset size. |
|---|
| 152 | | - * |
|---|
| 153 | | - * Callers that need to know only the current size of the regset and do |
|---|
| 154 | | - * not care about its internal structure should call regset_size() |
|---|
| 155 | | - * instead of inspecting @n or calling @get_size. |
|---|
| 153 | + * regset. |
|---|
| 156 | 154 | * |
|---|
| 157 | 155 | * For backward compatibility, the @get and @set methods must pad to, or |
|---|
| 158 | 156 | * accept, @n * @size bytes, even if the current regset size is smaller. |
|---|
| .. | .. |
|---|
| 188 | 186 | * omitted when there is an @active function and it returns zero. |
|---|
| 189 | 187 | */ |
|---|
| 190 | 188 | struct user_regset { |
|---|
| 191 | | - user_regset_get_fn *get; |
|---|
| 189 | + user_regset_get2_fn *regset_get; |
|---|
| 192 | 190 | user_regset_set_fn *set; |
|---|
| 193 | 191 | user_regset_active_fn *active; |
|---|
| 194 | 192 | user_regset_writeback_fn *writeback; |
|---|
| 195 | | - user_regset_get_size_fn *get_size; |
|---|
| 196 | 193 | unsigned int n; |
|---|
| 197 | 194 | unsigned int size; |
|---|
| 198 | 195 | unsigned int align; |
|---|
| .. | .. |
|---|
| 241 | 238 | */ |
|---|
| 242 | 239 | const struct user_regset_view *task_user_regset_view(struct task_struct *tsk); |
|---|
| 243 | 240 | |
|---|
| 244 | | - |
|---|
| 245 | | -/* |
|---|
| 246 | | - * These are helpers for writing regset get/set functions in arch code. |
|---|
| 247 | | - * Because @start_pos and @end_pos are always compile-time constants, |
|---|
| 248 | | - * these are inlined into very little code though they look large. |
|---|
| 249 | | - * |
|---|
| 250 | | - * Use one or more calls sequentially for each chunk of regset data stored |
|---|
| 251 | | - * contiguously in memory. Call with constants for @start_pos and @end_pos, |
|---|
| 252 | | - * giving the range of byte positions in the regset that data corresponds |
|---|
| 253 | | - * to; @end_pos can be -1 if this chunk is at the end of the regset layout. |
|---|
| 254 | | - * Each call updates the arguments to point past its chunk. |
|---|
| 255 | | - */ |
|---|
| 256 | | - |
|---|
| 257 | | -static inline int user_regset_copyout(unsigned int *pos, unsigned int *count, |
|---|
| 258 | | - void **kbuf, |
|---|
| 259 | | - void __user **ubuf, const void *data, |
|---|
| 260 | | - const int start_pos, const int end_pos) |
|---|
| 261 | | -{ |
|---|
| 262 | | - if (*count == 0) |
|---|
| 263 | | - return 0; |
|---|
| 264 | | - BUG_ON(*pos < start_pos); |
|---|
| 265 | | - if (end_pos < 0 || *pos < end_pos) { |
|---|
| 266 | | - unsigned int copy = (end_pos < 0 ? *count |
|---|
| 267 | | - : min(*count, end_pos - *pos)); |
|---|
| 268 | | - data += *pos - start_pos; |
|---|
| 269 | | - if (*kbuf) { |
|---|
| 270 | | - memcpy(*kbuf, data, copy); |
|---|
| 271 | | - *kbuf += copy; |
|---|
| 272 | | - } else if (__copy_to_user(*ubuf, data, copy)) |
|---|
| 273 | | - return -EFAULT; |
|---|
| 274 | | - else |
|---|
| 275 | | - *ubuf += copy; |
|---|
| 276 | | - *pos += copy; |
|---|
| 277 | | - *count -= copy; |
|---|
| 278 | | - } |
|---|
| 279 | | - return 0; |
|---|
| 280 | | -} |
|---|
| 281 | | - |
|---|
| 282 | 241 | static inline int user_regset_copyin(unsigned int *pos, unsigned int *count, |
|---|
| 283 | 242 | const void **kbuf, |
|---|
| 284 | 243 | const void __user **ubuf, void *data, |
|---|
| .. | .. |
|---|
| 295 | 254 | memcpy(data, *kbuf, copy); |
|---|
| 296 | 255 | *kbuf += copy; |
|---|
| 297 | 256 | } else if (__copy_from_user(data, *ubuf, copy)) |
|---|
| 298 | | - return -EFAULT; |
|---|
| 299 | | - else |
|---|
| 300 | | - *ubuf += copy; |
|---|
| 301 | | - *pos += copy; |
|---|
| 302 | | - *count -= copy; |
|---|
| 303 | | - } |
|---|
| 304 | | - return 0; |
|---|
| 305 | | -} |
|---|
| 306 | | - |
|---|
| 307 | | -/* |
|---|
| 308 | | - * These two parallel the two above, but for portions of a regset layout |
|---|
| 309 | | - * that always read as all-zero or for which writes are ignored. |
|---|
| 310 | | - */ |
|---|
| 311 | | -static inline int user_regset_copyout_zero(unsigned int *pos, |
|---|
| 312 | | - unsigned int *count, |
|---|
| 313 | | - void **kbuf, void __user **ubuf, |
|---|
| 314 | | - const int start_pos, |
|---|
| 315 | | - const int end_pos) |
|---|
| 316 | | -{ |
|---|
| 317 | | - if (*count == 0) |
|---|
| 318 | | - return 0; |
|---|
| 319 | | - BUG_ON(*pos < start_pos); |
|---|
| 320 | | - if (end_pos < 0 || *pos < end_pos) { |
|---|
| 321 | | - unsigned int copy = (end_pos < 0 ? *count |
|---|
| 322 | | - : min(*count, end_pos - *pos)); |
|---|
| 323 | | - if (*kbuf) { |
|---|
| 324 | | - memset(*kbuf, 0, copy); |
|---|
| 325 | | - *kbuf += copy; |
|---|
| 326 | | - } else if (__clear_user(*ubuf, copy)) |
|---|
| 327 | 257 | return -EFAULT; |
|---|
| 328 | 258 | else |
|---|
| 329 | 259 | *ubuf += copy; |
|---|
| .. | .. |
|---|
| 356 | 286 | return 0; |
|---|
| 357 | 287 | } |
|---|
| 358 | 288 | |
|---|
| 359 | | -/** |
|---|
| 360 | | - * copy_regset_to_user - fetch a thread's user_regset data into user memory |
|---|
| 361 | | - * @target: thread to be examined |
|---|
| 362 | | - * @view: &struct user_regset_view describing user thread machine state |
|---|
| 363 | | - * @setno: index in @view->regsets |
|---|
| 364 | | - * @offset: offset into the regset data, in bytes |
|---|
| 365 | | - * @size: amount of data to copy, in bytes |
|---|
| 366 | | - * @data: user-mode pointer to copy into |
|---|
| 367 | | - */ |
|---|
| 368 | | -static inline int copy_regset_to_user(struct task_struct *target, |
|---|
| 369 | | - const struct user_regset_view *view, |
|---|
| 370 | | - unsigned int setno, |
|---|
| 371 | | - unsigned int offset, unsigned int size, |
|---|
| 372 | | - void __user *data) |
|---|
| 373 | | -{ |
|---|
| 374 | | - const struct user_regset *regset = &view->regsets[setno]; |
|---|
| 289 | +extern int regset_get(struct task_struct *target, |
|---|
| 290 | + const struct user_regset *regset, |
|---|
| 291 | + unsigned int size, void *data); |
|---|
| 375 | 292 | |
|---|
| 376 | | - if (!regset->get) |
|---|
| 377 | | - return -EOPNOTSUPP; |
|---|
| 293 | +extern int regset_get_alloc(struct task_struct *target, |
|---|
| 294 | + const struct user_regset *regset, |
|---|
| 295 | + unsigned int size, |
|---|
| 296 | + void **data); |
|---|
| 378 | 297 | |
|---|
| 379 | | - if (!access_ok(VERIFY_WRITE, data, size)) |
|---|
| 380 | | - return -EFAULT; |
|---|
| 381 | | - |
|---|
| 382 | | - return regset->get(target, regset, offset, size, NULL, data); |
|---|
| 383 | | -} |
|---|
| 298 | +extern int copy_regset_to_user(struct task_struct *target, |
|---|
| 299 | + const struct user_regset_view *view, |
|---|
| 300 | + unsigned int setno, unsigned int offset, |
|---|
| 301 | + unsigned int size, void __user *data); |
|---|
| 384 | 302 | |
|---|
| 385 | 303 | /** |
|---|
| 386 | 304 | * copy_regset_from_user - store into thread's user_regset data from user memory |
|---|
| .. | .. |
|---|
| 402 | 320 | if (!regset->set) |
|---|
| 403 | 321 | return -EOPNOTSUPP; |
|---|
| 404 | 322 | |
|---|
| 405 | | - if (!access_ok(VERIFY_READ, data, size)) |
|---|
| 323 | + if (!access_ok(data, size)) |
|---|
| 406 | 324 | return -EFAULT; |
|---|
| 407 | 325 | |
|---|
| 408 | 326 | return regset->set(target, regset, offset, size, NULL, data); |
|---|
| 409 | | -} |
|---|
| 410 | | - |
|---|
| 411 | | -/** |
|---|
| 412 | | - * regset_size - determine the current size of a regset |
|---|
| 413 | | - * @target: thread to be examined |
|---|
| 414 | | - * @regset: regset to be examined |
|---|
| 415 | | - * |
|---|
| 416 | | - * Note that the returned size is valid only until the next time |
|---|
| 417 | | - * (if any) @regset is modified for @target. |
|---|
| 418 | | - */ |
|---|
| 419 | | -static inline unsigned int regset_size(struct task_struct *target, |
|---|
| 420 | | - const struct user_regset *regset) |
|---|
| 421 | | -{ |
|---|
| 422 | | - if (!regset->get_size) |
|---|
| 423 | | - return regset->n * regset->size; |
|---|
| 424 | | - else |
|---|
| 425 | | - return regset->get_size(target, regset); |
|---|
| 426 | 327 | } |
|---|
| 427 | 328 | |
|---|
| 428 | 329 | #endif /* <linux/regset.h> */ |
|---|