regset.h 11 KB

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