context_powerpc.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * PowerPC register context support
  3. *
  4. * Copyright (C) 2002 Marcus Meissner, SuSE Linux AG.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include "config.h"
  21. #ifdef __powerpc__
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #ifdef HAVE_SYS_REG_H
  26. # include <sys/reg.h>
  27. #endif
  28. #include <stdarg.h>
  29. #include <unistd.h>
  30. #ifdef HAVE_SYS_PTRACE_H
  31. # include <sys/ptrace.h>
  32. #endif
  33. #ifndef PTRACE_PEEKUSER
  34. # ifdef PT_READ_D
  35. # define PTRACE_PEEKUSER PT_READ_D
  36. # endif
  37. #endif /* PTRACE_PEEKUSER */
  38. #ifndef PTRACE_POKEUSER
  39. # ifdef PT_WRITE_D
  40. # define PTRACE_POKEUSER PT_WRITE_D
  41. # endif
  42. #endif /* PTRACE_POKEUSER */
  43. #include "windef.h"
  44. #include "winbase.h"
  45. #include "file.h"
  46. #include "thread.h"
  47. #include "request.h"
  48. /* retrieve a thread context */
  49. static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
  50. {
  51. int pid = get_ptrace_pid(thread);
  52. if (flags & CONTEXT_FULL)
  53. {
  54. if (flags & CONTEXT_INTEGER)
  55. {
  56. #define XREG(x,y) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
  57. #define IREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
  58. IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
  59. IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
  60. IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
  61. IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
  62. IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
  63. #undef IREG
  64. XREG(37,Xer);
  65. XREG(38,Cr);
  66. }
  67. if (flags & CONTEXT_CONTROL)
  68. {
  69. XREG(32,Iar);
  70. XREG(33,Msr);
  71. XREG(35,Ctr);
  72. XREG(36,Lr); /* 36 is LNK ... probably Lr ? */
  73. }
  74. }
  75. if (flags & CONTEXT_FLOATING_POINT)
  76. {
  77. #define FREG(x) if (ptrace( PTRACE_PEEKUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
  78. FREG(0);
  79. FREG(1);
  80. FREG(2);
  81. FREG(3);
  82. FREG(4);
  83. FREG(5);
  84. FREG(6);
  85. FREG(7);
  86. FREG(8);
  87. FREG(9);
  88. FREG(10);
  89. FREG(11);
  90. FREG(12);
  91. FREG(13);
  92. FREG(14);
  93. FREG(15);
  94. FREG(16);
  95. FREG(17);
  96. FREG(18);
  97. FREG(19);
  98. FREG(20);
  99. FREG(21);
  100. FREG(22);
  101. FREG(23);
  102. FREG(24);
  103. FREG(25);
  104. FREG(26);
  105. FREG(27);
  106. FREG(28);
  107. FREG(29);
  108. FREG(30);
  109. FREG(31);
  110. XREG((48+32*2),Fpscr);
  111. }
  112. return;
  113. error:
  114. file_set_error();
  115. }
  116. #undef XREG
  117. #undef IREG
  118. #undef FREG
  119. #define XREG(x,y) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->y) == -1) goto error;
  120. #define IREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)(x<<2), &context->Gpr##x) == -1) goto error;
  121. #define FREG(x) if (ptrace( PTRACE_POKEUSER, pid, (void*)((48+x*2)<<2), &context->Fpr##x) == -1) goto error;
  122. /* set a thread context */
  123. static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
  124. {
  125. int pid = get_ptrace_pid(thread);
  126. if (flags & CONTEXT_FULL)
  127. {
  128. if (flags & CONTEXT_INTEGER)
  129. {
  130. IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
  131. IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
  132. IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
  133. IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
  134. IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
  135. XREG(37,Xer);
  136. XREG(38,Cr);
  137. }
  138. if (flags & CONTEXT_CONTROL)
  139. {
  140. XREG(32,Iar);
  141. XREG(33,Msr);
  142. XREG(35,Ctr);
  143. XREG(36,Lr);
  144. }
  145. }
  146. if (flags & CONTEXT_FLOATING_POINT)
  147. {
  148. FREG(0);
  149. FREG(1);
  150. FREG(2);
  151. FREG(3);
  152. FREG(4);
  153. FREG(5);
  154. FREG(6);
  155. FREG(7);
  156. FREG(8);
  157. FREG(9);
  158. FREG(10);
  159. FREG(11);
  160. FREG(12);
  161. FREG(13);
  162. FREG(14);
  163. FREG(15);
  164. FREG(16);
  165. FREG(17);
  166. FREG(18);
  167. FREG(19);
  168. FREG(20);
  169. FREG(21);
  170. FREG(22);
  171. FREG(23);
  172. FREG(24);
  173. FREG(25);
  174. FREG(26);
  175. FREG(27);
  176. FREG(28);
  177. FREG(29);
  178. FREG(30);
  179. FREG(31);
  180. #undef FREG
  181. XREG((48+32*2),Fpscr);
  182. }
  183. return;
  184. error:
  185. file_set_error();
  186. }
  187. #undef XREG
  188. #undef IREG
  189. #undef FREG
  190. #define IREG(x) to->Gpr##x = from->Gpr##x;
  191. #define FREG(x) to->Fpr##x = from->Fpr##x;
  192. #define CREG(x) to->x = from->x;
  193. /* copy a context structure according to the flags */
  194. static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
  195. {
  196. if (flags & CONTEXT_CONTROL)
  197. {
  198. CREG(Msr);
  199. CREG(Ctr);
  200. CREG(Iar);
  201. }
  202. if (flags & CONTEXT_INTEGER)
  203. {
  204. IREG(0); IREG(1); IREG(2); IREG(3); IREG(4); IREG(5); IREG(6);
  205. IREG(7); IREG(8); IREG(9); IREG(10); IREG(11); IREG(12); IREG(13);
  206. IREG(14); IREG(15); IREG(16); IREG(17); IREG(18); IREG(19);
  207. IREG(20); IREG(21); IREG(22); IREG(23); IREG(24); IREG(25);
  208. IREG(26); IREG(27); IREG(28); IREG(29); IREG(30); IREG(31);
  209. CREG(Xer);
  210. CREG(Cr);
  211. }
  212. if (flags & CONTEXT_FLOATING_POINT)
  213. {
  214. FREG(0);
  215. FREG(1);
  216. FREG(2);
  217. FREG(3);
  218. FREG(4);
  219. FREG(5);
  220. FREG(6);
  221. FREG(7);
  222. FREG(8);
  223. FREG(9);
  224. FREG(10);
  225. FREG(11);
  226. FREG(12);
  227. FREG(13);
  228. FREG(14);
  229. FREG(15);
  230. FREG(16);
  231. FREG(17);
  232. FREG(18);
  233. FREG(19);
  234. FREG(20);
  235. FREG(21);
  236. FREG(22);
  237. FREG(23);
  238. FREG(24);
  239. FREG(25);
  240. FREG(26);
  241. FREG(27);
  242. FREG(28);
  243. FREG(29);
  244. FREG(30);
  245. FREG(31);
  246. CREG(Fpscr);
  247. }
  248. }
  249. /* retrieve the current instruction pointer of a thread */
  250. void *get_thread_ip( struct thread *thread )
  251. {
  252. CONTEXT context;
  253. context.Iar = 0;
  254. if (suspend_for_ptrace( thread ))
  255. {
  256. get_thread_context( thread, CONTEXT_CONTROL, &context );
  257. resume_after_ptrace( thread );
  258. }
  259. return (void *)context.Iar;
  260. }
  261. /* determine if we should continue the thread in single-step mode */
  262. int get_thread_single_step( struct thread *thread )
  263. {
  264. CONTEXT context;
  265. if (thread->context) return 0;
  266. get_thread_context( thread, CONTEXT_CONTROL, &context );
  267. #ifndef MSR_SE
  268. # define MSR_SE (1<<10)
  269. #endif
  270. return (context.Msr & MSR_SE) != 0;
  271. }
  272. /* send a signal to a specific thread */
  273. int tkill( int pid, int sig )
  274. {
  275. /* FIXME: should do something here */
  276. errno = ENOSYS;
  277. return -1;
  278. }
  279. /* retrieve the current context of a thread */
  280. DECL_HANDLER(get_thread_context)
  281. {
  282. struct thread *thread;
  283. void *data;
  284. int flags = req->flags;
  285. if (get_reply_max_size() < sizeof(CONTEXT))
  286. {
  287. set_error( STATUS_INVALID_PARAMETER );
  288. return;
  289. }
  290. if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
  291. if ((data = set_reply_data_size( sizeof(CONTEXT) )))
  292. {
  293. if (thread->context) /* thread is inside an exception event */
  294. {
  295. copy_context( data, thread->context, flags );
  296. flags = 0;
  297. }
  298. if (flags && suspend_for_ptrace( thread ))
  299. {
  300. get_thread_context( thread, flags, data );
  301. resume_after_ptrace( thread );
  302. }
  303. }
  304. release_object( thread );
  305. }
  306. /* set the current context of a thread */
  307. DECL_HANDLER(set_thread_context)
  308. {
  309. struct thread *thread;
  310. int flags = req->flags;
  311. if (get_req_data_size() < sizeof(CONTEXT))
  312. {
  313. set_error( STATUS_INVALID_PARAMETER );
  314. return;
  315. }
  316. if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
  317. {
  318. if (thread->context) /* thread is inside an exception event */
  319. {
  320. copy_context( thread->context, get_req_data(), flags );
  321. flags = 0;
  322. }
  323. if (flags && suspend_for_ptrace( thread ))
  324. {
  325. set_thread_context( thread, flags, get_req_data() );
  326. resume_after_ptrace( thread );
  327. }
  328. release_object( thread );
  329. }
  330. }
  331. #endif /* __powerpc__ */