regset.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifndef _LINUX_REGSET_H
  10. #define _LINUX_REGSET_H 1
  11. #include <linux/compiler.h>
  12. #include <linux/types.h>
  13. #include <linux/bug.h>
  14. #include <linux/uaccess.h>
  15. struct task_struct;
  16. struct user_regset;
  17. struct membuf {
  18. void *p;
  19. size_t left;
  20. };
  21. static inline int membuf_zero(struct membuf *s, size_t size)
  22. {
  23. if (s->left) {
  24. if (size > s->left)
  25. size = s->left;
  26. memset(s->p, 0, size);
  27. s->p += size;
  28. s->left -= size;
  29. }
  30. return s->left;
  31. }
  32. static inline int membuf_write(struct membuf *s, const void *v, size_t size)
  33. {
  34. if (s->left) {
  35. if (size > s->left)
  36. size = s->left;
  37. memcpy(s->p, v, size);
  38. s->p += size;
  39. s->left -= size;
  40. }
  41. return s->left;
  42. }
  43. static inline struct membuf membuf_at(const struct membuf *s, size_t offs)
  44. {
  45. struct membuf n = *s;
  46. if (offs > n.left)
  47. offs = n.left;
  48. n.p += offs;
  49. n.left -= offs;
  50. return n;
  51. }
  52. /* current s->p must be aligned for v; v must be a scalar */
  53. #define membuf_store(s, v) \
  54. ({ \
  55. struct membuf *__s = (s); \
  56. if (__s->left) { \
  57. typeof(v) __v = (v); \
  58. size_t __size = sizeof(__v); \
  59. if (unlikely(__size > __s->left)) { \
  60. __size = __s->left; \
  61. memcpy(__s->p, &__v, __size); \
  62. } else { \
  63. *(typeof(__v + 0) *)__s->p = __v; \
  64. } \
  65. __s->p += __size; \
  66. __s->left -= __size; \
  67. } \
  68. __s->left;})
  69. /**
  70. * user_regset_active_fn - type of @active function in &struct user_regset
  71. * @target: thread being examined
  72. * @regset: regset being examined
  73. *
  74. * Return -%ENODEV if not available on the hardware found.
  75. * Return %0 if no interesting state in this thread.
  76. * Return >%0 number of @size units of interesting state.
  77. * Any get call fetching state beyond that number will
  78. * see the default initialization state for this data,
  79. * so a caller that knows what the default state is need
  80. * not copy it all out.
  81. * This call is optional; the pointer is %NULL if there
  82. * is no inexpensive check to yield a value < @n.
  83. */
  84. typedef int user_regset_active_fn(struct task_struct *target,
  85. const struct user_regset *regset);
  86. typedef int user_regset_get2_fn(struct task_struct *target,
  87. const struct user_regset *regset,
  88. struct membuf to);
  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. * user_regset_writeback_fn - type of @writeback function in &struct user_regset
  111. * @target: thread being examined
  112. * @regset: regset being examined
  113. * @immediate: zero if writeback at completion of next context switch is OK
  114. *
  115. * This call is optional; usually the pointer is %NULL. When
  116. * provided, there is some user memory associated with this regset's
  117. * hardware, such as memory backing cached register data on register
  118. * window machines; the regset's data controls what user memory is
  119. * used (e.g. via the stack pointer value).
  120. *
  121. * Write register data back to user memory. If the @immediate flag
  122. * is nonzero, it must be written to the user memory so uaccess or
  123. * access_process_vm() can see it when this call returns; if zero,
  124. * then it must be written back by the time the task completes a
  125. * context switch (as synchronized with wait_task_inactive()).
  126. * Return %0 on success or if there was nothing to do, -%EFAULT for
  127. * a memory problem (bad stack pointer or whatever), or -%EIO for a
  128. * hardware problem.
  129. */
  130. typedef int user_regset_writeback_fn(struct task_struct *target,
  131. const struct user_regset *regset,
  132. int immediate);
  133. /**
  134. * struct user_regset - accessible thread CPU state
  135. * @n: Number of slots (registers).
  136. * @size: Size in bytes of a slot (register).
  137. * @align: Required alignment, in bytes.
  138. * @bias: Bias from natural indexing.
  139. * @core_note_type: ELF note @n_type value used in core dumps.
  140. * @get: Function to fetch values.
  141. * @set: Function to store values.
  142. * @active: Function to report if regset is active, or %NULL.
  143. * @writeback: Function to write data back to user memory, or %NULL.
  144. *
  145. * This data structure describes a machine resource we call a register set.
  146. * This is part of the state of an individual thread, not necessarily
  147. * actual CPU registers per se. A register set consists of a number of
  148. * similar slots, given by @n. Each slot is @size bytes, and aligned to
  149. * @align bytes (which is at least @size). For dynamically-sized
  150. * regsets, @n must contain the maximum possible number of slots for the
  151. * regset.
  152. *
  153. * For backward compatibility, the @get and @set methods must pad to, or
  154. * accept, @n * @size bytes, even if the current regset size is smaller.
  155. * The precise semantics of these operations depend on the regset being
  156. * accessed.
  157. *
  158. * The functions to which &struct user_regset members point must be
  159. * called only on the current thread or on a thread that is in
  160. * %TASK_STOPPED or %TASK_TRACED state, that we are guaranteed will not
  161. * be woken up and return to user mode, and that we have called
  162. * wait_task_inactive() on. (The target thread always might wake up for
  163. * SIGKILL while these functions are working, in which case that
  164. * thread's user_regset state might be scrambled.)
  165. *
  166. * The @pos argument must be aligned according to @align; the @count
  167. * argument must be a multiple of @size. These functions are not
  168. * responsible for checking for invalid arguments.
  169. *
  170. * When there is a natural value to use as an index, @bias gives the
  171. * difference between the natural index and the slot index for the
  172. * register set. For example, x86 GDT segment descriptors form a regset;
  173. * the segment selector produces a natural index, but only a subset of
  174. * that index space is available as a regset (the TLS slots); subtracting
  175. * @bias from a segment selector index value computes the regset slot.
  176. *
  177. * If nonzero, @core_note_type gives the n_type field (NT_* value)
  178. * of the core file note in which this regset's data appears.
  179. * NT_PRSTATUS is a special case in that the regset data starts at
  180. * offsetof(struct elf_prstatus, pr_reg) into the note data; that is
  181. * part of the per-machine ELF formats userland knows about. In
  182. * other cases, the core file note contains exactly the whole regset
  183. * (@n * @size) and nothing else. The core file note is normally
  184. * omitted when there is an @active function and it returns zero.
  185. */
  186. struct user_regset {
  187. user_regset_get2_fn *regset_get;
  188. user_regset_set_fn *set;
  189. user_regset_active_fn *active;
  190. user_regset_writeback_fn *writeback;
  191. unsigned int n;
  192. unsigned int size;
  193. unsigned int align;
  194. unsigned int bias;
  195. unsigned int core_note_type;
  196. };
  197. /**
  198. * struct user_regset_view - available regsets
  199. * @name: Identifier, e.g. UTS_MACHINE string.
  200. * @regsets: Array of @n regsets available in this view.
  201. * @n: Number of elements in @regsets.
  202. * @e_machine: ELF header @e_machine %EM_* value written in core dumps.
  203. * @e_flags: ELF header @e_flags value written in core dumps.
  204. * @ei_osabi: ELF header @e_ident[%EI_OSABI] value written in core dumps.
  205. *
  206. * A regset view is a collection of regsets (&struct user_regset,
  207. * above). This describes all the state of a thread that can be seen
  208. * from a given architecture/ABI environment. More than one view might
  209. * refer to the same &struct user_regset, or more than one regset
  210. * might refer to the same machine-specific state in the thread. For
  211. * example, a 32-bit thread's state could be examined from the 32-bit
  212. * view or from the 64-bit view. Either method reaches the same thread
  213. * register state, doing appropriate widening or truncation.
  214. */
  215. struct user_regset_view {
  216. const char *name;
  217. const struct user_regset *regsets;
  218. unsigned int n;
  219. u32 e_flags;
  220. u16 e_machine;
  221. u8 ei_osabi;
  222. };
  223. /*
  224. * This is documented here rather than at the definition sites because its
  225. * implementation is machine-dependent but its interface is universal.
  226. */
  227. /**
  228. * task_user_regset_view - Return the process's native regset view.
  229. * @tsk: a thread of the process in question
  230. *
  231. * Return the &struct user_regset_view that is native for the given process.
  232. * For example, what it would access when it called ptrace().
  233. * Throughout the life of the process, this only changes at exec.
  234. */
  235. const struct user_regset_view *task_user_regset_view(struct task_struct *tsk);
  236. static inline int user_regset_copyin(unsigned int *pos, unsigned int *count,
  237. const void **kbuf,
  238. const void __user **ubuf, void *data,
  239. const int start_pos, const int end_pos)
  240. {
  241. if (*count == 0)
  242. return 0;
  243. BUG_ON(*pos < start_pos);
  244. if (end_pos < 0 || *pos < end_pos) {
  245. unsigned int copy = (end_pos < 0 ? *count
  246. : min(*count, end_pos - *pos));
  247. data += *pos - start_pos;
  248. if (*kbuf) {
  249. memcpy(data, *kbuf, copy);
  250. *kbuf += copy;
  251. } else if (__copy_from_user(data, *ubuf, copy))
  252. return -EFAULT;
  253. else
  254. *ubuf += copy;
  255. *pos += copy;
  256. *count -= copy;
  257. }
  258. return 0;
  259. }
  260. static inline int user_regset_copyin_ignore(unsigned int *pos,
  261. unsigned int *count,
  262. const void **kbuf,
  263. const void __user **ubuf,
  264. const int start_pos,
  265. const int end_pos)
  266. {
  267. if (*count == 0)
  268. return 0;
  269. BUG_ON(*pos < start_pos);
  270. if (end_pos < 0 || *pos < end_pos) {
  271. unsigned int copy = (end_pos < 0 ? *count
  272. : min(*count, end_pos - *pos));
  273. if (*kbuf)
  274. *kbuf += copy;
  275. else
  276. *ubuf += copy;
  277. *pos += copy;
  278. *count -= copy;
  279. }
  280. return 0;
  281. }
  282. extern int regset_get(struct task_struct *target,
  283. const struct user_regset *regset,
  284. unsigned int size, void *data);
  285. extern int regset_get_alloc(struct task_struct *target,
  286. const struct user_regset *regset,
  287. unsigned int size,
  288. void **data);
  289. extern int copy_regset_to_user(struct task_struct *target,
  290. const struct user_regset_view *view,
  291. unsigned int setno, unsigned int offset,
  292. unsigned int size, void __user *data);
  293. /**
  294. * copy_regset_from_user - store into thread's user_regset data from user memory
  295. * @target: thread to be examined
  296. * @view: &struct user_regset_view describing user thread machine state
  297. * @setno: index in @view->regsets
  298. * @offset: offset into the regset data, in bytes
  299. * @size: amount of data to copy, in bytes
  300. * @data: user-mode pointer to copy from
  301. */
  302. static inline int copy_regset_from_user(struct task_struct *target,
  303. const struct user_regset_view *view,
  304. unsigned int setno,
  305. unsigned int offset, unsigned int size,
  306. const void __user *data)
  307. {
  308. const struct user_regset *regset = &view->regsets[setno];
  309. if (!regset->set)
  310. return -EOPNOTSUPP;
  311. if (!access_ok(data, size))
  312. return -EFAULT;
  313. return regset->set(target, regset, offset, size, NULL, data);
  314. }
  315. #endif /* <linux/regset.h> */