context_i386.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. * i386 register context support
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  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 __i386__
  22. #include <assert.h>
  23. #include <errno.h>
  24. #ifdef HAVE_SYS_REG_H
  25. #include <sys/reg.h>
  26. #endif
  27. #include <stdarg.h>
  28. #include <unistd.h>
  29. #ifdef HAVE_SYS_PTRACE_H
  30. # include <sys/ptrace.h>
  31. #endif
  32. #ifdef HAVE_SYS_PARAM_H
  33. # include <sys/param.h>
  34. #endif
  35. #include "windef.h"
  36. #include "winbase.h"
  37. #include "file.h"
  38. #include "thread.h"
  39. #include "request.h"
  40. #ifndef PTRACE_PEEKUSER
  41. # ifdef PTRACE_PEEKUSR /* libc5 uses USR not USER */
  42. # define PTRACE_PEEKUSER PTRACE_PEEKUSR
  43. # else
  44. # define PTRACE_PEEKUSER PT_READ_U
  45. # endif
  46. #endif
  47. #ifndef PTRACE_POKEUSER
  48. # ifdef PTRACE_POKEUSR /* libc5 uses USR not USER */
  49. # define PTRACE_POKEUSER PTRACE_POKEUSR
  50. # else
  51. # define PTRACE_POKEUSER PT_WRITE_U
  52. # endif
  53. #endif
  54. #ifndef PTRACE_GETREGS
  55. #define PTRACE_GETREGS PT_GETREGS
  56. #endif
  57. #ifndef PTRACE_GETFPREGS
  58. #define PTRACE_GETFPREGS PT_GETFPREGS
  59. #endif
  60. #ifndef PTRACE_SETREGS
  61. #define PTRACE_SETREGS PT_SETREGS
  62. #endif
  63. #ifndef PTRACE_SETFPREGS
  64. #define PTRACE_SETFPREGS PT_SETFPREGS
  65. #endif
  66. #ifdef PT_GETDBREGS
  67. #define PTRACE_GETDBREGS PT_GETDBREGS
  68. #endif
  69. #ifdef PT_SETDBREGS
  70. #define PTRACE_SETDBREGS PT_SETDBREGS
  71. #endif
  72. #ifdef linux
  73. #ifdef HAVE_SYS_USER_H
  74. # include <sys/user.h>
  75. #endif
  76. /* user_regs definitions from asm/user.h */
  77. struct kernel_user_regs_struct
  78. {
  79. long ebx, ecx, edx, esi, edi, ebp, eax;
  80. unsigned short ds, __ds, es, __es;
  81. unsigned short fs, __fs, gs, __gs;
  82. long orig_eax, eip;
  83. unsigned short cs, __cs;
  84. long eflags, esp;
  85. unsigned short ss, __ss;
  86. };
  87. /* debug register offset in struct user */
  88. #define DR_OFFSET(dr) ((int)((((struct user *)0)->u_debugreg) + (dr)))
  89. /* retrieve a debug register */
  90. static inline int get_debug_reg( int pid, int num, DWORD *data )
  91. {
  92. int res;
  93. errno = 0;
  94. res = ptrace( PTRACE_PEEKUSER, pid, DR_OFFSET(num), 0 );
  95. if ((res == -1) && errno)
  96. {
  97. file_set_error();
  98. return -1;
  99. }
  100. *data = res;
  101. return 0;
  102. }
  103. /* retrieve a thread context */
  104. static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
  105. {
  106. int pid = get_ptrace_pid(thread);
  107. if (flags & CONTEXT_FULL)
  108. {
  109. struct kernel_user_regs_struct regs;
  110. if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
  111. if (flags & CONTEXT_INTEGER)
  112. {
  113. context->Eax = regs.eax;
  114. context->Ebx = regs.ebx;
  115. context->Ecx = regs.ecx;
  116. context->Edx = regs.edx;
  117. context->Esi = regs.esi;
  118. context->Edi = regs.edi;
  119. }
  120. if (flags & CONTEXT_CONTROL)
  121. {
  122. context->Ebp = regs.ebp;
  123. context->Esp = regs.esp;
  124. context->Eip = regs.eip;
  125. context->SegCs = regs.cs;
  126. context->SegSs = regs.ss;
  127. context->EFlags = regs.eflags;
  128. }
  129. if (flags & CONTEXT_SEGMENTS)
  130. {
  131. context->SegDs = regs.ds;
  132. context->SegEs = regs.es;
  133. context->SegFs = regs.fs;
  134. context->SegGs = regs.gs;
  135. }
  136. }
  137. if (flags & CONTEXT_DEBUG_REGISTERS)
  138. {
  139. if (get_debug_reg( pid, 0, &context->Dr0 ) == -1) goto error;
  140. if (get_debug_reg( pid, 1, &context->Dr1 ) == -1) goto error;
  141. if (get_debug_reg( pid, 2, &context->Dr2 ) == -1) goto error;
  142. if (get_debug_reg( pid, 3, &context->Dr3 ) == -1) goto error;
  143. if (get_debug_reg( pid, 6, &context->Dr6 ) == -1) goto error;
  144. if (get_debug_reg( pid, 7, &context->Dr7 ) == -1) goto error;
  145. }
  146. if (flags & CONTEXT_FLOATING_POINT)
  147. {
  148. /* we can use context->FloatSave directly as it is using the */
  149. /* correct structure (the same as fsave/frstor) */
  150. if (ptrace( PTRACE_GETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
  151. context->FloatSave.Cr0NpxState = 0; /* FIXME */
  152. }
  153. return;
  154. error:
  155. file_set_error();
  156. }
  157. /* set a thread context */
  158. static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
  159. {
  160. int pid = get_ptrace_pid(thread);
  161. if (flags & CONTEXT_FULL)
  162. {
  163. struct kernel_user_regs_struct regs;
  164. /* need to preserve some registers (at a minimum orig_eax must always be preserved) */
  165. if (ptrace( PTRACE_GETREGS, pid, 0, &regs ) == -1) goto error;
  166. if (flags & CONTEXT_INTEGER)
  167. {
  168. regs.eax = context->Eax;
  169. regs.ebx = context->Ebx;
  170. regs.ecx = context->Ecx;
  171. regs.edx = context->Edx;
  172. regs.esi = context->Esi;
  173. regs.edi = context->Edi;
  174. }
  175. if (flags & CONTEXT_CONTROL)
  176. {
  177. regs.ebp = context->Ebp;
  178. regs.esp = context->Esp;
  179. regs.eip = context->Eip;
  180. regs.cs = context->SegCs;
  181. regs.ss = context->SegSs;
  182. regs.eflags = context->EFlags;
  183. }
  184. if (flags & CONTEXT_SEGMENTS)
  185. {
  186. regs.ds = context->SegDs;
  187. regs.es = context->SegEs;
  188. regs.fs = context->SegFs;
  189. regs.gs = context->SegGs;
  190. }
  191. if (ptrace( PTRACE_SETREGS, pid, 0, &regs ) == -1) goto error;
  192. }
  193. if (flags & CONTEXT_DEBUG_REGISTERS)
  194. {
  195. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(0), context->Dr0 ) == -1) goto error;
  196. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(1), context->Dr1 ) == -1) goto error;
  197. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(2), context->Dr2 ) == -1) goto error;
  198. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(3), context->Dr3 ) == -1) goto error;
  199. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(6), context->Dr6 ) == -1) goto error;
  200. if (ptrace( PTRACE_POKEUSER, pid, DR_OFFSET(7), context->Dr7 ) == -1) goto error;
  201. }
  202. if (flags & CONTEXT_FLOATING_POINT)
  203. {
  204. /* we can use context->FloatSave directly as it is using the */
  205. /* correct structure (the same as fsave/frstor) */
  206. if (ptrace( PTRACE_SETFPREGS, pid, 0, &context->FloatSave ) == -1) goto error;
  207. }
  208. return;
  209. error:
  210. file_set_error();
  211. }
  212. #elif defined(__sun) || defined(__sun__)
  213. /* retrieve a thread context */
  214. static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
  215. {
  216. int pid = get_ptrace_pid(thread);
  217. if (flags & CONTEXT_FULL)
  218. {
  219. struct regs regs;
  220. if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
  221. if (flags & CONTEXT_INTEGER)
  222. {
  223. context->Eax = regs.r_eax;
  224. context->Ebx = regs.r_ebx;
  225. context->Ecx = regs.r_ecx;
  226. context->Edx = regs.r_edx;
  227. context->Esi = regs.r_esi;
  228. context->Edi = regs.r_edi;
  229. }
  230. if (flags & CONTEXT_CONTROL)
  231. {
  232. context->Ebp = regs.r_ebp;
  233. context->Esp = regs.r_esp;
  234. context->Eip = regs.r_eip;
  235. context->SegCs = regs.r_cs & 0xffff;
  236. context->SegSs = regs.r_ss & 0xffff;
  237. context->EFlags = regs.r_efl;
  238. }
  239. if (flags & CONTEXT_SEGMENTS)
  240. {
  241. context->SegDs = regs.r_ds & 0xffff;
  242. context->SegEs = regs.r_es & 0xffff;
  243. context->SegFs = regs.r_fs & 0xffff;
  244. context->SegGs = regs.r_gs & 0xffff;
  245. }
  246. }
  247. if (flags & CONTEXT_DEBUG_REGISTERS)
  248. {
  249. /* FIXME: How is this done on Solaris? */
  250. }
  251. if (flags & CONTEXT_FLOATING_POINT)
  252. {
  253. /* we can use context->FloatSave directly as it is using the */
  254. /* correct structure (the same as fsave/frstor) */
  255. if (ptrace( PTRACE_GETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
  256. context->FloatSave.Cr0NpxState = 0; /* FIXME */
  257. }
  258. return;
  259. error:
  260. file_set_error();
  261. }
  262. /* set a thread context */
  263. static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
  264. {
  265. int pid = get_ptrace_pid(thread);
  266. if (flags & CONTEXT_FULL)
  267. {
  268. struct regs regs;
  269. if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
  270. {
  271. /* need to preserve some registers */
  272. if (ptrace( PTRACE_GETREGS, pid, (int) &regs, 0 ) == -1) goto error;
  273. }
  274. if (flags & CONTEXT_INTEGER)
  275. {
  276. regs.r_eax = context->Eax;
  277. regs.r_ebx = context->Ebx;
  278. regs.r_ecx = context->Ecx;
  279. regs.r_edx = context->Edx;
  280. regs.r_esi = context->Esi;
  281. regs.r_edi = context->Edi;
  282. }
  283. if (flags & CONTEXT_CONTROL)
  284. {
  285. regs.r_ebp = context->Ebp;
  286. regs.r_esp = context->Esp;
  287. regs.r_eip = context->Eip;
  288. regs.r_cs = context->SegCs;
  289. regs.r_ss = context->SegSs;
  290. regs.r_efl = context->EFlags;
  291. }
  292. if (flags & CONTEXT_SEGMENTS)
  293. {
  294. regs.r_ds = context->SegDs;
  295. regs.r_es = context->SegEs;
  296. regs.r_fs = context->SegFs;
  297. regs.r_gs = context->SegGs;
  298. }
  299. if (ptrace( PTRACE_SETREGS, pid, (int) &regs, 0 ) == -1) goto error;
  300. }
  301. if (flags & CONTEXT_DEBUG_REGISTERS)
  302. {
  303. /* FIXME: How is this done on Solaris? */
  304. }
  305. if (flags & CONTEXT_FLOATING_POINT)
  306. {
  307. /* we can use context->FloatSave directly as it is using the */
  308. /* correct structure (the same as fsave/frstor) */
  309. if (ptrace( PTRACE_SETFPREGS, pid, (int) &context->FloatSave, 0 ) == -1) goto error;
  310. }
  311. return;
  312. error:
  313. file_set_error();
  314. }
  315. #elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
  316. #include <machine/reg.h>
  317. /* retrieve a thread context */
  318. static void get_thread_context( struct thread *thread, unsigned int flags, CONTEXT *context )
  319. {
  320. int pid = get_ptrace_pid(thread);
  321. if (flags & CONTEXT_FULL)
  322. {
  323. struct reg regs;
  324. if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
  325. if (flags & CONTEXT_INTEGER)
  326. {
  327. context->Eax = regs.r_eax;
  328. context->Ebx = regs.r_ebx;
  329. context->Ecx = regs.r_ecx;
  330. context->Edx = regs.r_edx;
  331. context->Esi = regs.r_esi;
  332. context->Edi = regs.r_edi;
  333. }
  334. if (flags & CONTEXT_CONTROL)
  335. {
  336. context->Ebp = regs.r_ebp;
  337. context->Esp = regs.r_esp;
  338. context->Eip = regs.r_eip;
  339. context->SegCs = regs.r_cs & 0xffff;
  340. context->SegSs = regs.r_ss & 0xffff;
  341. context->EFlags = regs.r_eflags;
  342. }
  343. if (flags & CONTEXT_SEGMENTS)
  344. {
  345. context->SegDs = regs.r_ds & 0xffff;
  346. context->SegEs = regs.r_es & 0xffff;
  347. context->SegFs = regs.r_fs & 0xffff;
  348. context->SegGs = regs.r_gs & 0xffff;
  349. }
  350. }
  351. if (flags & CONTEXT_DEBUG_REGISTERS)
  352. {
  353. #ifdef PTRACE_GETDBREGS
  354. struct dbreg dbregs;
  355. if (ptrace( PTRACE_GETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
  356. goto error;
  357. #ifdef DBREG_DRX
  358. /* needed for FreeBSD, the structure fields have changed under 5.x */
  359. context->Dr0 = DBREG_DRX((&dbregs), 0);
  360. context->Dr1 = DBREG_DRX((&dbregs), 1);
  361. context->Dr2 = DBREG_DRX((&dbregs), 2);
  362. context->Dr3 = DBREG_DRX((&dbregs), 3);
  363. context->Dr6 = DBREG_DRX((&dbregs), 6);
  364. context->Dr7 = DBREG_DRX((&dbregs), 7);
  365. #else
  366. context->Dr0 = dbregs.dr0;
  367. context->Dr1 = dbregs.dr1;
  368. context->Dr2 = dbregs.dr2;
  369. context->Dr3 = dbregs.dr3;
  370. context->Dr6 = dbregs.dr6;
  371. context->Dr7 = dbregs.dr7;
  372. #endif
  373. #endif
  374. }
  375. if (flags & CONTEXT_FLOATING_POINT)
  376. {
  377. /* we can use context->FloatSave directly as it is using the */
  378. /* correct structure (the same as fsave/frstor) */
  379. if (ptrace( PTRACE_GETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
  380. context->FloatSave.Cr0NpxState = 0; /* FIXME */
  381. }
  382. return;
  383. error:
  384. file_set_error();
  385. }
  386. /* set a thread context */
  387. static void set_thread_context( struct thread *thread, unsigned int flags, const CONTEXT *context )
  388. {
  389. int pid = get_ptrace_pid(thread);
  390. if (flags & CONTEXT_FULL)
  391. {
  392. struct reg regs;
  393. if (((flags | CONTEXT_i386) & CONTEXT_FULL) != CONTEXT_FULL)
  394. {
  395. /* need to preserve some registers */
  396. if (ptrace( PTRACE_GETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
  397. }
  398. if (flags & CONTEXT_INTEGER)
  399. {
  400. regs.r_eax = context->Eax;
  401. regs.r_ebx = context->Ebx;
  402. regs.r_ecx = context->Ecx;
  403. regs.r_edx = context->Edx;
  404. regs.r_esi = context->Esi;
  405. regs.r_edi = context->Edi;
  406. }
  407. if (flags & CONTEXT_CONTROL)
  408. {
  409. regs.r_ebp = context->Ebp;
  410. regs.r_esp = context->Esp;
  411. regs.r_eip = context->Eip;
  412. regs.r_cs = context->SegCs;
  413. regs.r_ss = context->SegSs;
  414. regs.r_eflags = context->EFlags;
  415. }
  416. if (flags & CONTEXT_SEGMENTS)
  417. {
  418. regs.r_ds = context->SegDs;
  419. regs.r_es = context->SegEs;
  420. regs.r_fs = context->SegFs;
  421. regs.r_gs = context->SegGs;
  422. }
  423. if (ptrace( PTRACE_SETREGS, pid, (caddr_t) &regs, 0 ) == -1) goto error;
  424. }
  425. if (flags & CONTEXT_DEBUG_REGISTERS)
  426. {
  427. #ifdef PTRACE_SETDBREGS
  428. struct dbreg dbregs;
  429. #ifdef DBREG_DRX
  430. /* needed for FreeBSD, the structure fields have changed under 5.x */
  431. DBREG_DRX((&dbregs), 0) = context->Dr0;
  432. DBREG_DRX((&dbregs), 1) = context->Dr1;
  433. DBREG_DRX((&dbregs), 2) = context->Dr2;
  434. DBREG_DRX((&dbregs), 3) = context->Dr3;
  435. DBREG_DRX((&dbregs), 4) = 0;
  436. DBREG_DRX((&dbregs), 5) = 0;
  437. DBREG_DRX((&dbregs), 6) = context->Dr6;
  438. DBREG_DRX((&dbregs), 7) = context->Dr7;
  439. #else
  440. dbregs.dr0 = context->Dr0;
  441. dbregs.dr1 = context->Dr1;
  442. dbregs.dr2 = context->Dr2;
  443. dbregs.dr3 = context->Dr3;
  444. dbregs.dr4 = 0;
  445. dbregs.dr5 = 0;
  446. dbregs.dr6 = context->Dr6;
  447. dbregs.dr7 = context->Dr7;
  448. #endif
  449. if (ptrace( PTRACE_SETDBREGS, pid, (caddr_t) &dbregs, 0 ) == -1)
  450. goto error;
  451. #endif
  452. }
  453. if (flags & CONTEXT_FLOATING_POINT)
  454. {
  455. /* we can use context->FloatSave directly as it is using the */
  456. /* correct structure (the same as fsave/frstor) */
  457. if (ptrace( PTRACE_SETFPREGS, pid, (caddr_t) &context->FloatSave, 0 ) == -1) goto error;
  458. }
  459. return;
  460. error:
  461. file_set_error();
  462. }
  463. #else /* linux || __sun__ || __FreeBSD__ */
  464. #error You must implement get/set_thread_context for your platform
  465. #endif /* linux || __sun__ || __FreeBSD__ */
  466. /* copy a context structure according to the flags */
  467. static void copy_context( CONTEXT *to, const CONTEXT *from, int flags )
  468. {
  469. if (flags & CONTEXT_CONTROL)
  470. {
  471. to->Ebp = from->Ebp;
  472. to->Eip = from->Eip;
  473. to->Esp = from->Esp;
  474. to->SegCs = from->SegCs;
  475. to->SegSs = from->SegSs;
  476. to->EFlags = from->EFlags;
  477. }
  478. if (flags & CONTEXT_INTEGER)
  479. {
  480. to->Eax = from->Eax;
  481. to->Ebx = from->Ebx;
  482. to->Ecx = from->Ecx;
  483. to->Edx = from->Edx;
  484. to->Esi = from->Esi;
  485. to->Edi = from->Edi;
  486. }
  487. if (flags & CONTEXT_SEGMENTS)
  488. {
  489. to->SegDs = from->SegDs;
  490. to->SegEs = from->SegEs;
  491. to->SegFs = from->SegFs;
  492. to->SegGs = from->SegGs;
  493. }
  494. if (flags & CONTEXT_FLOATING_POINT)
  495. {
  496. to->FloatSave = from->FloatSave;
  497. }
  498. /* we don't bother copying the debug registers, since they */
  499. /* always need to be accessed by ptrace anyway */
  500. }
  501. /* retrieve the current instruction pointer of a thread */
  502. void *get_thread_ip( struct thread *thread )
  503. {
  504. CONTEXT context;
  505. context.Eip = 0;
  506. if (suspend_for_ptrace( thread ))
  507. {
  508. get_thread_context( thread, CONTEXT_CONTROL, &context );
  509. resume_after_ptrace( thread );
  510. }
  511. return (void *)context.Eip;
  512. }
  513. /* determine if we should continue the thread in single-step mode */
  514. int get_thread_single_step( struct thread *thread )
  515. {
  516. CONTEXT context;
  517. if (thread->context) return 0; /* don't single-step inside exception event */
  518. get_thread_context( thread, CONTEXT_CONTROL, &context );
  519. return (context.EFlags & 0x100) != 0;
  520. }
  521. /* send a signal to a specific thread */
  522. int tkill( int pid, int sig )
  523. {
  524. #ifdef __linux__
  525. int ret;
  526. __asm__( "pushl %%ebx\n\t"
  527. "movl %2,%%ebx\n\t"
  528. "int $0x80\n\t"
  529. "popl %%ebx\n\t"
  530. : "=a" (ret)
  531. : "0" (238) /*SYS_tkill*/, "r" (pid), "c" (sig) );
  532. if (ret >= 0) return ret;
  533. errno = -ret;
  534. return -1;
  535. #else
  536. errno = ENOSYS;
  537. return -1;
  538. #endif
  539. }
  540. /* retrieve the current context of a thread */
  541. DECL_HANDLER(get_thread_context)
  542. {
  543. struct thread *thread;
  544. void *data;
  545. int flags = req->flags & ~CONTEXT_i386; /* get rid of CPU id */
  546. if (get_reply_max_size() < sizeof(CONTEXT))
  547. {
  548. set_error( STATUS_INVALID_PARAMETER );
  549. return;
  550. }
  551. if (!(thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT ))) return;
  552. if ((data = set_reply_data_size( sizeof(CONTEXT) )))
  553. {
  554. /* copy incoming context into reply */
  555. memset( data, 0, sizeof(CONTEXT) );
  556. memcpy( data, get_req_data(), min( get_req_data_size(), sizeof(CONTEXT) ));
  557. if (thread->context) /* thread is inside an exception event */
  558. {
  559. copy_context( data, thread->context, flags );
  560. flags &= CONTEXT_DEBUG_REGISTERS;
  561. }
  562. if (flags && suspend_for_ptrace( thread ))
  563. {
  564. get_thread_context( thread, flags, data );
  565. resume_after_ptrace( thread );
  566. }
  567. }
  568. release_object( thread );
  569. }
  570. /* set the current context of a thread */
  571. DECL_HANDLER(set_thread_context)
  572. {
  573. struct thread *thread;
  574. int flags = req->flags & ~CONTEXT_i386; /* get rid of CPU id */
  575. if (get_req_data_size() < sizeof(CONTEXT))
  576. {
  577. set_error( STATUS_INVALID_PARAMETER );
  578. return;
  579. }
  580. if ((thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT )))
  581. {
  582. if (thread->context) /* thread is inside an exception event */
  583. {
  584. copy_context( thread->context, get_req_data(), flags );
  585. flags &= CONTEXT_DEBUG_REGISTERS;
  586. }
  587. if (flags && suspend_for_ptrace( thread ))
  588. {
  589. set_thread_context( thread, flags, get_req_data() );
  590. resume_after_ptrace( thread );
  591. }
  592. release_object( thread );
  593. }
  594. }
  595. #endif /* __i386__ */