regset.h 13 KB

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