thread.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /*
  2. * Server-side thread management
  3. *
  4. * Copyright (C) 1998 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "wine/port.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <fcntl.h>
  25. #include <signal.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <unistd.h>
  32. #include <time.h>
  33. #ifdef HAVE_POLL_H
  34. #include <poll.h>
  35. #endif
  36. #ifdef HAVE_SCHED_H
  37. #include <sched.h>
  38. #endif
  39. #include "ntstatus.h"
  40. #define WIN32_NO_STATUS
  41. #include "windef.h"
  42. #include "winternl.h"
  43. #include "file.h"
  44. #include "handle.h"
  45. #include "process.h"
  46. #include "thread.h"
  47. #include "request.h"
  48. #include "user.h"
  49. #include "security.h"
  50. #ifdef __i386__
  51. static const unsigned int supported_cpus = CPU_FLAG(CPU_x86);
  52. #elif defined(__x86_64__)
  53. static const unsigned int supported_cpus = CPU_FLAG(CPU_x86_64) | CPU_FLAG(CPU_x86);
  54. #elif defined(__powerpc__)
  55. static const unsigned int supported_cpus = CPU_FLAG(CPU_POWERPC);
  56. #elif defined(__arm__)
  57. static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM);
  58. #elif defined(__aarch64__)
  59. static const unsigned int supported_cpus = CPU_FLAG(CPU_ARM64) | CPU_FLAG(CPU_ARM);
  60. #else
  61. #error Unsupported CPU
  62. #endif
  63. /* thread queues */
  64. struct thread_wait
  65. {
  66. struct thread_wait *next; /* next wait structure for this thread */
  67. struct thread *thread; /* owner thread */
  68. int count; /* count of objects */
  69. int flags;
  70. int abandoned;
  71. enum select_op select;
  72. client_ptr_t key; /* wait key for keyed events */
  73. client_ptr_t cookie; /* magic cookie to return to client */
  74. abstime_t when;
  75. struct timeout_user *user;
  76. int status; /* status to return (unless STATUS_PENDING) */
  77. struct wait_queue_entry queues[1];
  78. };
  79. /* asynchronous procedure calls */
  80. struct thread_apc
  81. {
  82. struct object obj; /* object header */
  83. struct list entry; /* queue linked list */
  84. struct thread *caller; /* thread that queued this apc */
  85. struct object *owner; /* object that queued this apc */
  86. int executed; /* has it been executed by the client? */
  87. apc_call_t call; /* call arguments */
  88. apc_result_t result; /* call results once executed */
  89. };
  90. static void dump_thread_apc( struct object *obj, int verbose );
  91. static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry );
  92. static void thread_apc_destroy( struct object *obj );
  93. static void clear_apc_queue( struct list *queue );
  94. static const struct object_ops thread_apc_ops =
  95. {
  96. sizeof(struct thread_apc), /* size */
  97. &no_type, /* type */
  98. dump_thread_apc, /* dump */
  99. add_queue, /* add_queue */
  100. remove_queue, /* remove_queue */
  101. thread_apc_signaled, /* signaled */
  102. no_satisfied, /* satisfied */
  103. no_signal, /* signal */
  104. no_get_fd, /* get_fd */
  105. default_map_access, /* map_access */
  106. default_get_sd, /* get_sd */
  107. default_set_sd, /* set_sd */
  108. no_get_full_name, /* get_full_name */
  109. no_lookup_name, /* lookup_name */
  110. no_link_name, /* link_name */
  111. NULL, /* unlink_name */
  112. no_open_file, /* open_file */
  113. no_kernel_obj_list, /* get_kernel_obj_list */
  114. no_close_handle, /* close_handle */
  115. thread_apc_destroy /* destroy */
  116. };
  117. /* thread CPU context */
  118. struct context
  119. {
  120. struct object obj; /* object header */
  121. unsigned int status; /* status of the context */
  122. context_t regs; /* context data */
  123. };
  124. static void dump_context( struct object *obj, int verbose );
  125. static int context_signaled( struct object *obj, struct wait_queue_entry *entry );
  126. static const struct object_ops context_ops =
  127. {
  128. sizeof(struct context), /* size */
  129. &no_type, /* type */
  130. dump_context, /* dump */
  131. add_queue, /* add_queue */
  132. remove_queue, /* remove_queue */
  133. context_signaled, /* signaled */
  134. no_satisfied, /* satisfied */
  135. no_signal, /* signal */
  136. no_get_fd, /* get_fd */
  137. default_map_access, /* map_access */
  138. default_get_sd, /* get_sd */
  139. default_set_sd, /* set_sd */
  140. no_get_full_name, /* get_full_name */
  141. no_lookup_name, /* lookup_name */
  142. no_link_name, /* link_name */
  143. NULL, /* unlink_name */
  144. no_open_file, /* open_file */
  145. no_kernel_obj_list, /* get_kernel_obj_list */
  146. no_close_handle, /* close_handle */
  147. no_destroy /* destroy */
  148. };
  149. /* thread operations */
  150. static const WCHAR thread_name[] = {'T','h','r','e','a','d'};
  151. struct type_descr thread_type =
  152. {
  153. { thread_name, sizeof(thread_name) }, /* name */
  154. THREAD_ALL_ACCESS, /* valid_access */
  155. { /* mapping */
  156. STANDARD_RIGHTS_READ | THREAD_QUERY_INFORMATION | THREAD_GET_CONTEXT,
  157. STANDARD_RIGHTS_WRITE | THREAD_SET_LIMITED_INFORMATION | THREAD_SET_INFORMATION
  158. | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME | THREAD_TERMINATE | 0x04,
  159. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE | THREAD_RESUME | THREAD_QUERY_LIMITED_INFORMATION,
  160. THREAD_ALL_ACCESS
  161. },
  162. };
  163. static void dump_thread( struct object *obj, int verbose );
  164. static int thread_signaled( struct object *obj, struct wait_queue_entry *entry );
  165. static unsigned int thread_map_access( struct object *obj, unsigned int access );
  166. static void thread_poll_event( struct fd *fd, int event );
  167. static struct list *thread_get_kernel_obj_list( struct object *obj );
  168. static void destroy_thread( struct object *obj );
  169. static const struct object_ops thread_ops =
  170. {
  171. sizeof(struct thread), /* size */
  172. &thread_type, /* type */
  173. dump_thread, /* dump */
  174. add_queue, /* add_queue */
  175. remove_queue, /* remove_queue */
  176. thread_signaled, /* signaled */
  177. no_satisfied, /* satisfied */
  178. no_signal, /* signal */
  179. no_get_fd, /* get_fd */
  180. thread_map_access, /* map_access */
  181. default_get_sd, /* get_sd */
  182. default_set_sd, /* set_sd */
  183. no_get_full_name, /* get_full_name */
  184. no_lookup_name, /* lookup_name */
  185. no_link_name, /* link_name */
  186. NULL, /* unlink_name */
  187. no_open_file, /* open_file */
  188. thread_get_kernel_obj_list, /* get_kernel_obj_list */
  189. no_close_handle, /* close_handle */
  190. destroy_thread /* destroy */
  191. };
  192. static const struct fd_ops thread_fd_ops =
  193. {
  194. NULL, /* get_poll_events */
  195. thread_poll_event, /* poll_event */
  196. NULL, /* flush */
  197. NULL, /* get_fd_type */
  198. NULL, /* ioctl */
  199. NULL, /* queue_async */
  200. NULL /* reselect_async */
  201. };
  202. static struct list thread_list = LIST_INIT(thread_list);
  203. /* initialize the structure for a newly allocated thread */
  204. static inline void init_thread_structure( struct thread *thread )
  205. {
  206. int i;
  207. thread->unix_pid = -1; /* not known yet */
  208. thread->unix_tid = -1; /* not known yet */
  209. thread->context = NULL;
  210. thread->teb = 0;
  211. thread->entry_point = 0;
  212. thread->system_regs = 0;
  213. thread->queue = NULL;
  214. thread->wait = NULL;
  215. thread->error = 0;
  216. thread->req_data = NULL;
  217. thread->req_toread = 0;
  218. thread->reply_data = NULL;
  219. thread->reply_towrite = 0;
  220. thread->request_fd = NULL;
  221. thread->reply_fd = NULL;
  222. thread->wait_fd = NULL;
  223. thread->state = RUNNING;
  224. thread->exit_code = 0;
  225. thread->priority = 0;
  226. thread->suspend = 0;
  227. thread->dbg_hidden = 0;
  228. thread->desktop_users = 0;
  229. thread->token = NULL;
  230. thread->desc = NULL;
  231. thread->desc_len = 0;
  232. thread->creation_time = current_time;
  233. thread->exit_time = 0;
  234. list_init( &thread->mutex_list );
  235. list_init( &thread->system_apc );
  236. list_init( &thread->user_apc );
  237. list_init( &thread->kernel_object );
  238. for (i = 0; i < MAX_INFLIGHT_FDS; i++)
  239. thread->inflight[i].server = thread->inflight[i].client = -1;
  240. }
  241. /* check if address looks valid for a client-side data structure (TEB etc.) */
  242. static inline int is_valid_address( client_ptr_t addr )
  243. {
  244. return addr && !(addr % sizeof(int));
  245. }
  246. /* dump a context on stdout for debugging purposes */
  247. static void dump_context( struct object *obj, int verbose )
  248. {
  249. struct context *context = (struct context *)obj;
  250. assert( obj->ops == &context_ops );
  251. fprintf( stderr, "context flags=%x\n", context->regs.flags );
  252. }
  253. static int context_signaled( struct object *obj, struct wait_queue_entry *entry )
  254. {
  255. struct context *context = (struct context *)obj;
  256. return context->status != STATUS_PENDING;
  257. }
  258. static struct context *create_thread_context( struct thread *thread )
  259. {
  260. struct context *context;
  261. if (!(context = alloc_object( &context_ops ))) return NULL;
  262. context->status = STATUS_PENDING;
  263. memset( &context->regs, 0, sizeof(context->regs) );
  264. context->regs.cpu = thread->process->cpu;
  265. return context;
  266. }
  267. /* create a new thread */
  268. struct thread *create_thread( int fd, struct process *process, const struct security_descriptor *sd )
  269. {
  270. struct thread *thread;
  271. int request_pipe[2];
  272. if (fd == -1)
  273. {
  274. if (pipe( request_pipe ) == -1)
  275. {
  276. file_set_error();
  277. return NULL;
  278. }
  279. if (send_client_fd( process, request_pipe[1], SERVER_PROTOCOL_VERSION ) == -1)
  280. {
  281. close( request_pipe[0] );
  282. close( request_pipe[1] );
  283. return NULL;
  284. }
  285. close( request_pipe[1] );
  286. fd = request_pipe[0];
  287. }
  288. if (process->is_terminating)
  289. {
  290. close( fd );
  291. set_error( STATUS_PROCESS_IS_TERMINATING );
  292. return NULL;
  293. }
  294. if (!(thread = alloc_object( &thread_ops )))
  295. {
  296. close( fd );
  297. return NULL;
  298. }
  299. init_thread_structure( thread );
  300. thread->process = (struct process *)grab_object( process );
  301. thread->desktop = process->desktop;
  302. thread->affinity = process->affinity;
  303. if (!current) current = thread;
  304. list_add_tail( &thread_list, &thread->entry );
  305. if (sd && !set_sd_defaults_from_token( &thread->obj, sd,
  306. OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
  307. DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION,
  308. process->token ))
  309. {
  310. close( fd );
  311. release_object( thread );
  312. return NULL;
  313. }
  314. if (!(thread->id = alloc_ptid( thread )))
  315. {
  316. close( fd );
  317. release_object( thread );
  318. return NULL;
  319. }
  320. if (!(thread->request_fd = create_anonymous_fd( &thread_fd_ops, fd, &thread->obj, 0 )))
  321. {
  322. release_object( thread );
  323. return NULL;
  324. }
  325. set_fd_events( thread->request_fd, POLLIN ); /* start listening to events */
  326. add_process_thread( thread->process, thread );
  327. return thread;
  328. }
  329. /* handle a client event */
  330. static void thread_poll_event( struct fd *fd, int event )
  331. {
  332. struct thread *thread = get_fd_user( fd );
  333. assert( thread->obj.ops == &thread_ops );
  334. grab_object( thread );
  335. if (event & (POLLERR | POLLHUP)) kill_thread( thread, 0 );
  336. else if (event & POLLIN) read_request( thread );
  337. else if (event & POLLOUT) write_reply( thread );
  338. release_object( thread );
  339. }
  340. static struct list *thread_get_kernel_obj_list( struct object *obj )
  341. {
  342. struct thread *thread = (struct thread *)obj;
  343. return &thread->kernel_object;
  344. }
  345. /* cleanup everything that is no longer needed by a dead thread */
  346. /* used by destroy_thread and kill_thread */
  347. static void cleanup_thread( struct thread *thread )
  348. {
  349. int i;
  350. if (thread->context)
  351. {
  352. thread->context->status = STATUS_ACCESS_DENIED;
  353. wake_up( &thread->context->obj, 0 );
  354. release_object( thread->context );
  355. thread->context = NULL;
  356. }
  357. clear_apc_queue( &thread->system_apc );
  358. clear_apc_queue( &thread->user_apc );
  359. free( thread->req_data );
  360. free( thread->reply_data );
  361. if (thread->request_fd) release_object( thread->request_fd );
  362. if (thread->reply_fd) release_object( thread->reply_fd );
  363. if (thread->wait_fd) release_object( thread->wait_fd );
  364. cleanup_clipboard_thread(thread);
  365. destroy_thread_windows( thread );
  366. free_msg_queue( thread );
  367. close_thread_desktop( thread );
  368. for (i = 0; i < MAX_INFLIGHT_FDS; i++)
  369. {
  370. if (thread->inflight[i].client != -1)
  371. {
  372. close( thread->inflight[i].server );
  373. thread->inflight[i].client = thread->inflight[i].server = -1;
  374. }
  375. }
  376. free( thread->desc );
  377. thread->req_data = NULL;
  378. thread->reply_data = NULL;
  379. thread->request_fd = NULL;
  380. thread->reply_fd = NULL;
  381. thread->wait_fd = NULL;
  382. thread->desktop = 0;
  383. thread->desc = NULL;
  384. thread->desc_len = 0;
  385. }
  386. /* destroy a thread when its refcount is 0 */
  387. static void destroy_thread( struct object *obj )
  388. {
  389. struct thread *thread = (struct thread *)obj;
  390. assert( obj->ops == &thread_ops );
  391. list_remove( &thread->entry );
  392. cleanup_thread( thread );
  393. release_object( thread->process );
  394. if (thread->id) free_ptid( thread->id );
  395. if (thread->token) release_object( thread->token );
  396. }
  397. /* dump a thread on stdout for debugging purposes */
  398. static void dump_thread( struct object *obj, int verbose )
  399. {
  400. struct thread *thread = (struct thread *)obj;
  401. assert( obj->ops == &thread_ops );
  402. fprintf( stderr, "Thread id=%04x unix pid=%d unix tid=%d state=%d\n",
  403. thread->id, thread->unix_pid, thread->unix_tid, thread->state );
  404. }
  405. static int thread_signaled( struct object *obj, struct wait_queue_entry *entry )
  406. {
  407. struct thread *mythread = (struct thread *)obj;
  408. return (mythread->state == TERMINATED);
  409. }
  410. static unsigned int thread_map_access( struct object *obj, unsigned int access )
  411. {
  412. access = default_map_access( obj, access );
  413. if (access & THREAD_QUERY_INFORMATION) access |= THREAD_QUERY_LIMITED_INFORMATION;
  414. if (access & THREAD_SET_INFORMATION) access |= THREAD_SET_LIMITED_INFORMATION;
  415. return access;
  416. }
  417. static void dump_thread_apc( struct object *obj, int verbose )
  418. {
  419. struct thread_apc *apc = (struct thread_apc *)obj;
  420. assert( obj->ops == &thread_apc_ops );
  421. fprintf( stderr, "APC owner=%p type=%u\n", apc->owner, apc->call.type );
  422. }
  423. static int thread_apc_signaled( struct object *obj, struct wait_queue_entry *entry )
  424. {
  425. struct thread_apc *apc = (struct thread_apc *)obj;
  426. return apc->executed;
  427. }
  428. static void thread_apc_destroy( struct object *obj )
  429. {
  430. struct thread_apc *apc = (struct thread_apc *)obj;
  431. if (apc->caller) release_object( apc->caller );
  432. if (apc->owner)
  433. {
  434. if (apc->result.type == APC_ASYNC_IO)
  435. async_set_result( apc->owner, apc->result.async_io.status, apc->result.async_io.total );
  436. else if (apc->call.type == APC_ASYNC_IO)
  437. async_set_result( apc->owner, apc->call.async_io.status, 0 );
  438. release_object( apc->owner );
  439. }
  440. }
  441. /* queue an async procedure call */
  442. static struct thread_apc *create_apc( struct object *owner, const apc_call_t *call_data )
  443. {
  444. struct thread_apc *apc;
  445. if ((apc = alloc_object( &thread_apc_ops )))
  446. {
  447. apc->call = *call_data;
  448. apc->caller = NULL;
  449. apc->owner = owner;
  450. apc->executed = 0;
  451. apc->result.type = APC_NONE;
  452. if (owner) grab_object( owner );
  453. }
  454. return apc;
  455. }
  456. /* get a thread pointer from a thread id (and increment the refcount) */
  457. struct thread *get_thread_from_id( thread_id_t id )
  458. {
  459. struct object *obj = get_ptid_entry( id );
  460. if (obj && obj->ops == &thread_ops) return (struct thread *)grab_object( obj );
  461. set_error( STATUS_INVALID_CID );
  462. return NULL;
  463. }
  464. /* get a thread from a handle (and increment the refcount) */
  465. struct thread *get_thread_from_handle( obj_handle_t handle, unsigned int access )
  466. {
  467. return (struct thread *)get_handle_obj( current->process, handle,
  468. access, &thread_ops );
  469. }
  470. /* find a thread from a Unix tid */
  471. struct thread *get_thread_from_tid( int tid )
  472. {
  473. struct thread *thread;
  474. LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
  475. {
  476. if (thread->unix_tid == tid) return thread;
  477. }
  478. return NULL;
  479. }
  480. /* find a thread from a Unix pid */
  481. struct thread *get_thread_from_pid( int pid )
  482. {
  483. struct thread *thread;
  484. LIST_FOR_EACH_ENTRY( thread, &thread_list, struct thread, entry )
  485. {
  486. if (thread->unix_pid == pid) return thread;
  487. }
  488. return NULL;
  489. }
  490. int set_thread_affinity( struct thread *thread, affinity_t affinity )
  491. {
  492. int ret = 0;
  493. #ifdef HAVE_SCHED_SETAFFINITY
  494. if (thread->unix_tid != -1)
  495. {
  496. cpu_set_t set;
  497. int i;
  498. affinity_t mask;
  499. CPU_ZERO( &set );
  500. for (i = 0, mask = 1; mask; i++, mask <<= 1)
  501. if (affinity & mask) CPU_SET( i, &set );
  502. ret = sched_setaffinity( thread->unix_tid, sizeof(set), &set );
  503. }
  504. #endif
  505. if (!ret) thread->affinity = affinity;
  506. return ret;
  507. }
  508. affinity_t get_thread_affinity( struct thread *thread )
  509. {
  510. affinity_t mask = 0;
  511. #ifdef HAVE_SCHED_SETAFFINITY
  512. if (thread->unix_tid != -1)
  513. {
  514. cpu_set_t set;
  515. unsigned int i;
  516. if (!sched_getaffinity( thread->unix_tid, sizeof(set), &set ))
  517. for (i = 0; i < 8 * sizeof(mask); i++)
  518. if (CPU_ISSET( i, &set )) mask |= (affinity_t)1 << i;
  519. }
  520. #endif
  521. if (!mask) mask = ~(affinity_t)0;
  522. return mask;
  523. }
  524. #define THREAD_PRIORITY_REALTIME_HIGHEST 6
  525. #define THREAD_PRIORITY_REALTIME_LOWEST -7
  526. /* set all information about a thread */
  527. static void set_thread_info( struct thread *thread,
  528. const struct set_thread_info_request *req )
  529. {
  530. if (req->mask & SET_THREAD_INFO_PRIORITY)
  531. {
  532. int max = THREAD_PRIORITY_HIGHEST;
  533. int min = THREAD_PRIORITY_LOWEST;
  534. if (thread->process->priority == PROCESS_PRIOCLASS_REALTIME)
  535. {
  536. max = THREAD_PRIORITY_REALTIME_HIGHEST;
  537. min = THREAD_PRIORITY_REALTIME_LOWEST;
  538. }
  539. if ((req->priority >= min && req->priority <= max) ||
  540. req->priority == THREAD_PRIORITY_IDLE ||
  541. req->priority == THREAD_PRIORITY_TIME_CRITICAL)
  542. thread->priority = req->priority;
  543. else
  544. set_error( STATUS_INVALID_PARAMETER );
  545. }
  546. if (req->mask & SET_THREAD_INFO_AFFINITY)
  547. {
  548. if ((req->affinity & thread->process->affinity) != req->affinity)
  549. set_error( STATUS_INVALID_PARAMETER );
  550. else if (thread->state == TERMINATED)
  551. set_error( STATUS_THREAD_IS_TERMINATING );
  552. else if (set_thread_affinity( thread, req->affinity ))
  553. file_set_error();
  554. }
  555. if (req->mask & SET_THREAD_INFO_TOKEN)
  556. security_set_thread_token( thread, req->token );
  557. if (req->mask & SET_THREAD_INFO_ENTRYPOINT)
  558. thread->entry_point = req->entry_point;
  559. if (req->mask & SET_THREAD_INFO_DBG_HIDDEN)
  560. thread->dbg_hidden = 1;
  561. if (req->mask & SET_THREAD_INFO_DESCRIPTION)
  562. {
  563. WCHAR *desc;
  564. data_size_t desc_len = get_req_data_size();
  565. if (desc_len)
  566. {
  567. if ((desc = mem_alloc( desc_len )))
  568. {
  569. memcpy( desc, get_req_data(), desc_len );
  570. free( thread->desc );
  571. thread->desc = desc;
  572. thread->desc_len = desc_len;
  573. }
  574. }
  575. else
  576. {
  577. free( thread->desc );
  578. thread->desc = NULL;
  579. thread->desc_len = 0;
  580. }
  581. }
  582. }
  583. /* stop a thread (at the Unix level) */
  584. void stop_thread( struct thread *thread )
  585. {
  586. if (thread->context) return; /* already suspended, no need for a signal */
  587. if (!(thread->context = create_thread_context( thread ))) return;
  588. /* can't stop a thread while initialisation is in progress */
  589. if (is_process_init_done(thread->process)) send_thread_signal( thread, SIGUSR1 );
  590. }
  591. /* suspend a thread */
  592. int suspend_thread( struct thread *thread )
  593. {
  594. int old_count = thread->suspend;
  595. if (thread->suspend < MAXIMUM_SUSPEND_COUNT)
  596. {
  597. if (!(thread->process->suspend + thread->suspend++)) stop_thread( thread );
  598. }
  599. else set_error( STATUS_SUSPEND_COUNT_EXCEEDED );
  600. return old_count;
  601. }
  602. /* resume a thread */
  603. int resume_thread( struct thread *thread )
  604. {
  605. int old_count = thread->suspend;
  606. if (thread->suspend > 0)
  607. {
  608. if (!(--thread->suspend)) resume_delayed_debug_events( thread );
  609. if (!(thread->suspend + thread->process->suspend)) wake_thread( thread );
  610. }
  611. return old_count;
  612. }
  613. /* add a thread to an object wait queue; return 1 if OK, 0 on error */
  614. int add_queue( struct object *obj, struct wait_queue_entry *entry )
  615. {
  616. grab_object( obj );
  617. entry->obj = obj;
  618. list_add_tail( &obj->wait_queue, &entry->entry );
  619. return 1;
  620. }
  621. /* remove a thread from an object wait queue */
  622. void remove_queue( struct object *obj, struct wait_queue_entry *entry )
  623. {
  624. list_remove( &entry->entry );
  625. release_object( obj );
  626. }
  627. struct thread *get_wait_queue_thread( struct wait_queue_entry *entry )
  628. {
  629. return entry->wait->thread;
  630. }
  631. enum select_op get_wait_queue_select_op( struct wait_queue_entry *entry )
  632. {
  633. return entry->wait->select;
  634. }
  635. client_ptr_t get_wait_queue_key( struct wait_queue_entry *entry )
  636. {
  637. return entry->wait->key;
  638. }
  639. void make_wait_abandoned( struct wait_queue_entry *entry )
  640. {
  641. entry->wait->abandoned = 1;
  642. }
  643. void set_wait_status( struct wait_queue_entry *entry, int status )
  644. {
  645. entry->wait->status = status;
  646. }
  647. /* finish waiting */
  648. static unsigned int end_wait( struct thread *thread, unsigned int status )
  649. {
  650. struct thread_wait *wait = thread->wait;
  651. struct wait_queue_entry *entry;
  652. int i;
  653. assert( wait );
  654. thread->wait = wait->next;
  655. if (status < wait->count) /* wait satisfied, tell it to the objects */
  656. {
  657. if (wait->select == SELECT_WAIT_ALL)
  658. {
  659. for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
  660. entry->obj->ops->satisfied( entry->obj, entry );
  661. }
  662. else
  663. {
  664. entry = wait->queues + status;
  665. entry->obj->ops->satisfied( entry->obj, entry );
  666. }
  667. if (wait->abandoned) status += STATUS_ABANDONED_WAIT_0;
  668. if (wait->status != STATUS_PENDING) status = wait->status;
  669. }
  670. for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
  671. entry->obj->ops->remove_queue( entry->obj, entry );
  672. if (wait->user) remove_timeout_user( wait->user );
  673. free( wait );
  674. return status;
  675. }
  676. /* build the thread wait structure */
  677. static int wait_on( const select_op_t *select_op, unsigned int count, struct object *objects[],
  678. int flags, abstime_t when )
  679. {
  680. struct thread_wait *wait;
  681. struct wait_queue_entry *entry;
  682. unsigned int i;
  683. if (!(wait = mem_alloc( FIELD_OFFSET(struct thread_wait, queues[count]) ))) return 0;
  684. wait->next = current->wait;
  685. wait->thread = current;
  686. wait->count = count;
  687. wait->flags = flags;
  688. wait->select = select_op->op;
  689. wait->cookie = 0;
  690. wait->user = NULL;
  691. wait->when = when;
  692. wait->abandoned = 0;
  693. wait->status = STATUS_PENDING;
  694. current->wait = wait;
  695. for (i = 0, entry = wait->queues; i < count; i++, entry++)
  696. {
  697. struct object *obj = objects[i];
  698. entry->wait = wait;
  699. if (!obj->ops->add_queue( obj, entry ))
  700. {
  701. wait->count = i;
  702. end_wait( current, get_error() );
  703. return 0;
  704. }
  705. }
  706. return 1;
  707. }
  708. static int wait_on_handles( const select_op_t *select_op, unsigned int count, const obj_handle_t *handles,
  709. int flags, abstime_t when )
  710. {
  711. struct object *objects[MAXIMUM_WAIT_OBJECTS];
  712. unsigned int i;
  713. int ret = 0;
  714. assert( count <= MAXIMUM_WAIT_OBJECTS );
  715. for (i = 0; i < count; i++)
  716. if (!(objects[i] = get_handle_obj( current->process, handles[i], SYNCHRONIZE, NULL )))
  717. break;
  718. if (i == count) ret = wait_on( select_op, count, objects, flags, when );
  719. while (i > 0) release_object( objects[--i] );
  720. return ret;
  721. }
  722. /* check if the thread waiting condition is satisfied */
  723. static int check_wait( struct thread *thread )
  724. {
  725. int i;
  726. struct thread_wait *wait = thread->wait;
  727. struct wait_queue_entry *entry;
  728. assert( wait );
  729. if ((wait->flags & SELECT_INTERRUPTIBLE) && !list_empty( &thread->system_apc ))
  730. return STATUS_KERNEL_APC;
  731. /* Suspended threads may not acquire locks, but they can run system APCs */
  732. if (thread->process->suspend + thread->suspend > 0) return -1;
  733. if (wait->select == SELECT_WAIT_ALL)
  734. {
  735. int not_ok = 0;
  736. /* Note: we must check them all anyway, as some objects may
  737. * want to do something when signaled, even if others are not */
  738. for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
  739. not_ok |= !entry->obj->ops->signaled( entry->obj, entry );
  740. if (!not_ok) return STATUS_WAIT_0;
  741. }
  742. else
  743. {
  744. for (i = 0, entry = wait->queues; i < wait->count; i++, entry++)
  745. if (entry->obj->ops->signaled( entry->obj, entry )) return i;
  746. }
  747. if ((wait->flags & SELECT_ALERTABLE) && !list_empty(&thread->user_apc)) return STATUS_USER_APC;
  748. if (wait->when >= 0 && wait->when <= current_time) return STATUS_TIMEOUT;
  749. if (wait->when < 0 && -wait->when <= monotonic_time) return STATUS_TIMEOUT;
  750. return -1;
  751. }
  752. /* send the wakeup signal to a thread */
  753. static int send_thread_wakeup( struct thread *thread, client_ptr_t cookie, int signaled )
  754. {
  755. struct wake_up_reply reply;
  756. int ret;
  757. /* check if we're waking current suspend wait */
  758. if (thread->context && thread->suspend_cookie == cookie
  759. && signaled != STATUS_KERNEL_APC && signaled != STATUS_USER_APC)
  760. {
  761. if (!thread->context->regs.flags)
  762. {
  763. release_object( thread->context );
  764. thread->context = NULL;
  765. }
  766. else signaled = STATUS_KERNEL_APC; /* signal a fake APC so that client calls select to get a new context */
  767. }
  768. memset( &reply, 0, sizeof(reply) );
  769. reply.cookie = cookie;
  770. reply.signaled = signaled;
  771. if ((ret = write( get_unix_fd( thread->wait_fd ), &reply, sizeof(reply) )) == sizeof(reply))
  772. return 0;
  773. if (ret >= 0)
  774. fatal_protocol_error( thread, "partial wakeup write %d\n", ret );
  775. else if (errno == EPIPE)
  776. kill_thread( thread, 0 ); /* normal death */
  777. else
  778. fatal_protocol_error( thread, "write: %s\n", strerror( errno ));
  779. return -1;
  780. }
  781. /* attempt to wake up a thread */
  782. /* return >0 if OK, 0 if the wait condition is still not satisfied and -1 on error */
  783. int wake_thread( struct thread *thread )
  784. {
  785. int signaled, count;
  786. client_ptr_t cookie;
  787. for (count = 0; thread->wait; count++)
  788. {
  789. if ((signaled = check_wait( thread )) == -1) break;
  790. cookie = thread->wait->cookie;
  791. signaled = end_wait( thread, signaled );
  792. if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
  793. if (cookie && send_thread_wakeup( thread, cookie, signaled ) == -1) /* error */
  794. {
  795. if (!count) count = -1;
  796. break;
  797. }
  798. }
  799. return count;
  800. }
  801. /* attempt to wake up a thread from a wait queue entry, assuming that it is signaled */
  802. int wake_thread_queue_entry( struct wait_queue_entry *entry )
  803. {
  804. struct thread_wait *wait = entry->wait;
  805. struct thread *thread = wait->thread;
  806. int signaled;
  807. client_ptr_t cookie;
  808. if (thread->wait != wait) return 0; /* not the current wait */
  809. if (thread->process->suspend + thread->suspend > 0) return 0; /* cannot acquire locks */
  810. assert( wait->select != SELECT_WAIT_ALL );
  811. cookie = wait->cookie;
  812. signaled = end_wait( thread, entry - wait->queues );
  813. if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=%d\n", thread->id, signaled );
  814. if (!cookie || send_thread_wakeup( thread, cookie, signaled ) != -1)
  815. wake_thread( thread ); /* check other waits too */
  816. return 1;
  817. }
  818. /* thread wait timeout */
  819. static void thread_timeout( void *ptr )
  820. {
  821. struct thread_wait *wait = ptr;
  822. struct thread *thread = wait->thread;
  823. client_ptr_t cookie = wait->cookie;
  824. wait->user = NULL;
  825. if (thread->wait != wait) return; /* not the top-level wait, ignore it */
  826. if (thread->suspend + thread->process->suspend > 0) return; /* suspended, ignore it */
  827. if (debug_level) fprintf( stderr, "%04x: *wakeup* signaled=TIMEOUT\n", thread->id );
  828. end_wait( thread, STATUS_TIMEOUT );
  829. assert( cookie );
  830. if (send_thread_wakeup( thread, cookie, STATUS_TIMEOUT ) == -1) return;
  831. /* check if other objects have become signaled in the meantime */
  832. wake_thread( thread );
  833. }
  834. /* try signaling an event flag, a semaphore or a mutex */
  835. static int signal_object( obj_handle_t handle )
  836. {
  837. struct object *obj;
  838. int ret = 0;
  839. obj = get_handle_obj( current->process, handle, 0, NULL );
  840. if (obj)
  841. {
  842. ret = obj->ops->signal( obj, get_handle_access( current->process, handle ));
  843. release_object( obj );
  844. }
  845. return ret;
  846. }
  847. /* select on a list of handles */
  848. static void select_on( const select_op_t *select_op, data_size_t op_size, client_ptr_t cookie,
  849. int flags, abstime_t when )
  850. {
  851. int ret;
  852. unsigned int count;
  853. struct object *object;
  854. switch (select_op->op)
  855. {
  856. case SELECT_NONE:
  857. if (!wait_on( select_op, 0, NULL, flags, when )) return;
  858. break;
  859. case SELECT_WAIT:
  860. case SELECT_WAIT_ALL:
  861. count = (op_size - offsetof( select_op_t, wait.handles )) / sizeof(select_op->wait.handles[0]);
  862. if (op_size < offsetof( select_op_t, wait.handles ) || count > MAXIMUM_WAIT_OBJECTS)
  863. {
  864. set_error( STATUS_INVALID_PARAMETER );
  865. return;
  866. }
  867. if (!wait_on_handles( select_op, count, select_op->wait.handles, flags, when ))
  868. return;
  869. break;
  870. case SELECT_SIGNAL_AND_WAIT:
  871. if (!wait_on_handles( select_op, 1, &select_op->signal_and_wait.wait, flags, when ))
  872. return;
  873. if (select_op->signal_and_wait.signal)
  874. {
  875. if (!signal_object( select_op->signal_and_wait.signal ))
  876. {
  877. end_wait( current, get_error() );
  878. return;
  879. }
  880. /* check if we woke ourselves up */
  881. if (!current->wait) return;
  882. }
  883. break;
  884. case SELECT_KEYED_EVENT_WAIT:
  885. case SELECT_KEYED_EVENT_RELEASE:
  886. object = (struct object *)get_keyed_event_obj( current->process, select_op->keyed_event.handle,
  887. select_op->op == SELECT_KEYED_EVENT_WAIT ? KEYEDEVENT_WAIT : KEYEDEVENT_WAKE );
  888. if (!object) return;
  889. ret = wait_on( select_op, 1, &object, flags, when );
  890. release_object( object );
  891. if (!ret) return;
  892. current->wait->key = select_op->keyed_event.key;
  893. break;
  894. default:
  895. set_error( STATUS_INVALID_PARAMETER );
  896. return;
  897. }
  898. if ((ret = check_wait( current )) != -1)
  899. {
  900. /* condition is already satisfied */
  901. set_error( end_wait( current, ret ));
  902. return;
  903. }
  904. /* now we need to wait */
  905. if (current->wait->when != TIMEOUT_INFINITE)
  906. {
  907. if (!(current->wait->user = add_timeout_user( abstime_to_timeout(current->wait->when),
  908. thread_timeout, current->wait )))
  909. {
  910. end_wait( current, get_error() );
  911. return;
  912. }
  913. }
  914. current->wait->cookie = cookie;
  915. set_error( STATUS_PENDING );
  916. return;
  917. }
  918. /* attempt to wake threads sleeping on the object wait queue */
  919. void wake_up( struct object *obj, int max )
  920. {
  921. struct list *ptr;
  922. int ret;
  923. LIST_FOR_EACH( ptr, &obj->wait_queue )
  924. {
  925. struct wait_queue_entry *entry = LIST_ENTRY( ptr, struct wait_queue_entry, entry );
  926. if (!(ret = wake_thread( get_wait_queue_thread( entry )))) continue;
  927. if (ret > 0 && max && !--max) break;
  928. /* restart at the head of the list since a wake up can change the object wait queue */
  929. ptr = &obj->wait_queue;
  930. }
  931. }
  932. /* return the apc queue to use for a given apc type */
  933. static inline struct list *get_apc_queue( struct thread *thread, enum apc_type type )
  934. {
  935. switch(type)
  936. {
  937. case APC_NONE:
  938. case APC_USER:
  939. case APC_TIMER:
  940. return &thread->user_apc;
  941. default:
  942. return &thread->system_apc;
  943. }
  944. }
  945. /* check if thread is currently waiting for a (system) apc */
  946. static inline int is_in_apc_wait( struct thread *thread )
  947. {
  948. return (thread->process->suspend || thread->suspend ||
  949. (thread->wait && (thread->wait->flags & SELECT_INTERRUPTIBLE)));
  950. }
  951. /* queue an existing APC to a given thread */
  952. static int queue_apc( struct process *process, struct thread *thread, struct thread_apc *apc )
  953. {
  954. struct list *queue;
  955. if (thread && thread->state == TERMINATED && process)
  956. thread = NULL;
  957. if (!thread) /* find a suitable thread inside the process */
  958. {
  959. struct thread *candidate;
  960. /* first try to find a waiting thread */
  961. LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
  962. {
  963. if (candidate->state == TERMINATED) continue;
  964. if (is_in_apc_wait( candidate ))
  965. {
  966. thread = candidate;
  967. break;
  968. }
  969. }
  970. if (!thread)
  971. {
  972. /* then use the first one that accepts a signal */
  973. LIST_FOR_EACH_ENTRY( candidate, &process->thread_list, struct thread, proc_entry )
  974. {
  975. if (send_thread_signal( candidate, SIGUSR1 ))
  976. {
  977. thread = candidate;
  978. break;
  979. }
  980. }
  981. }
  982. if (!thread) return 0; /* nothing found */
  983. queue = get_apc_queue( thread, apc->call.type );
  984. }
  985. else
  986. {
  987. if (thread->state == TERMINATED) return 0;
  988. queue = get_apc_queue( thread, apc->call.type );
  989. /* send signal for system APCs if needed */
  990. if (queue == &thread->system_apc && list_empty( queue ) && !is_in_apc_wait( thread ))
  991. {
  992. if (!send_thread_signal( thread, SIGUSR1 )) return 0;
  993. }
  994. /* cancel a possible previous APC with the same owner */
  995. if (apc->owner) thread_cancel_apc( thread, apc->owner, apc->call.type );
  996. }
  997. grab_object( apc );
  998. list_add_tail( queue, &apc->entry );
  999. if (!list_prev( queue, &apc->entry )) /* first one */
  1000. wake_thread( thread );
  1001. return 1;
  1002. }
  1003. /* queue an async procedure call */
  1004. int thread_queue_apc( struct process *process, struct thread *thread, struct object *owner, const apc_call_t *call_data )
  1005. {
  1006. struct thread_apc *apc;
  1007. int ret = 0;
  1008. if ((apc = create_apc( owner, call_data )))
  1009. {
  1010. ret = queue_apc( process, thread, apc );
  1011. release_object( apc );
  1012. }
  1013. return ret;
  1014. }
  1015. /* cancel the async procedure call owned by a specific object */
  1016. void thread_cancel_apc( struct thread *thread, struct object *owner, enum apc_type type )
  1017. {
  1018. struct thread_apc *apc;
  1019. struct list *queue = get_apc_queue( thread, type );
  1020. LIST_FOR_EACH_ENTRY( apc, queue, struct thread_apc, entry )
  1021. {
  1022. if (apc->owner != owner) continue;
  1023. list_remove( &apc->entry );
  1024. apc->executed = 1;
  1025. wake_up( &apc->obj, 0 );
  1026. release_object( apc );
  1027. return;
  1028. }
  1029. }
  1030. /* remove the head apc from the queue; the returned object must be released by the caller */
  1031. static struct thread_apc *thread_dequeue_apc( struct thread *thread, int system )
  1032. {
  1033. struct thread_apc *apc = NULL;
  1034. struct list *ptr = list_head( system ? &thread->system_apc : &thread->user_apc );
  1035. if (ptr)
  1036. {
  1037. apc = LIST_ENTRY( ptr, struct thread_apc, entry );
  1038. list_remove( ptr );
  1039. }
  1040. return apc;
  1041. }
  1042. /* clear an APC queue, cancelling all the APCs on it */
  1043. static void clear_apc_queue( struct list *queue )
  1044. {
  1045. struct list *ptr;
  1046. while ((ptr = list_head( queue )))
  1047. {
  1048. struct thread_apc *apc = LIST_ENTRY( ptr, struct thread_apc, entry );
  1049. list_remove( &apc->entry );
  1050. apc->executed = 1;
  1051. wake_up( &apc->obj, 0 );
  1052. release_object( apc );
  1053. }
  1054. }
  1055. /* add an fd to the inflight list */
  1056. /* return list index, or -1 on error */
  1057. int thread_add_inflight_fd( struct thread *thread, int client, int server )
  1058. {
  1059. int i;
  1060. if (server == -1) return -1;
  1061. if (client == -1)
  1062. {
  1063. close( server );
  1064. return -1;
  1065. }
  1066. /* first check if we already have an entry for this fd */
  1067. for (i = 0; i < MAX_INFLIGHT_FDS; i++)
  1068. if (thread->inflight[i].client == client)
  1069. {
  1070. close( thread->inflight[i].server );
  1071. thread->inflight[i].server = server;
  1072. return i;
  1073. }
  1074. /* now find a free spot to store it */
  1075. for (i = 0; i < MAX_INFLIGHT_FDS; i++)
  1076. if (thread->inflight[i].client == -1)
  1077. {
  1078. thread->inflight[i].client = client;
  1079. thread->inflight[i].server = server;
  1080. return i;
  1081. }
  1082. close( server );
  1083. return -1;
  1084. }
  1085. /* get an inflight fd and purge it from the list */
  1086. /* the fd must be closed when no longer used */
  1087. int thread_get_inflight_fd( struct thread *thread, int client )
  1088. {
  1089. int i, ret;
  1090. if (client == -1) return -1;
  1091. do
  1092. {
  1093. for (i = 0; i < MAX_INFLIGHT_FDS; i++)
  1094. {
  1095. if (thread->inflight[i].client == client)
  1096. {
  1097. ret = thread->inflight[i].server;
  1098. thread->inflight[i].server = thread->inflight[i].client = -1;
  1099. return ret;
  1100. }
  1101. }
  1102. } while (!receive_fd( thread->process )); /* in case it is still in the socket buffer */
  1103. return -1;
  1104. }
  1105. /* kill a thread on the spot */
  1106. void kill_thread( struct thread *thread, int violent_death )
  1107. {
  1108. if (thread->state == TERMINATED) return; /* already killed */
  1109. thread->state = TERMINATED;
  1110. thread->exit_time = current_time;
  1111. if (current == thread) current = NULL;
  1112. if (debug_level)
  1113. fprintf( stderr,"%04x: *killed* exit_code=%d\n",
  1114. thread->id, thread->exit_code );
  1115. if (thread->wait)
  1116. {
  1117. while (thread->wait) end_wait( thread, STATUS_THREAD_IS_TERMINATING );
  1118. send_thread_wakeup( thread, 0, thread->exit_code );
  1119. /* if it is waiting on the socket, we don't need to send a SIGQUIT */
  1120. violent_death = 0;
  1121. }
  1122. kill_console_processes( thread, 0 );
  1123. abandon_mutexes( thread );
  1124. wake_up( &thread->obj, 0 );
  1125. if (violent_death) send_thread_signal( thread, SIGQUIT );
  1126. cleanup_thread( thread );
  1127. remove_process_thread( thread->process, thread );
  1128. release_object( thread );
  1129. }
  1130. /* copy parts of a context structure */
  1131. static void copy_context( context_t *to, const context_t *from, unsigned int flags )
  1132. {
  1133. assert( to->cpu == from->cpu );
  1134. if (flags & SERVER_CTX_CONTROL) to->ctl = from->ctl;
  1135. if (flags & SERVER_CTX_INTEGER) to->integer = from->integer;
  1136. if (flags & SERVER_CTX_SEGMENTS) to->seg = from->seg;
  1137. if (flags & SERVER_CTX_FLOATING_POINT) to->fp = from->fp;
  1138. if (flags & SERVER_CTX_DEBUG_REGISTERS) to->debug = from->debug;
  1139. if (flags & SERVER_CTX_EXTENDED_REGISTERS) to->ext = from->ext;
  1140. if (flags & SERVER_CTX_YMM_REGISTERS) to->ymm = from->ymm;
  1141. }
  1142. /* return the context flags that correspond to system regs */
  1143. /* (system regs are the ones we can't access on the client side) */
  1144. static unsigned int get_context_system_regs( enum cpu_type cpu )
  1145. {
  1146. switch (cpu)
  1147. {
  1148. case CPU_x86: return SERVER_CTX_DEBUG_REGISTERS;
  1149. case CPU_x86_64: return SERVER_CTX_DEBUG_REGISTERS;
  1150. case CPU_POWERPC: return 0;
  1151. case CPU_ARM: return SERVER_CTX_DEBUG_REGISTERS;
  1152. case CPU_ARM64: return SERVER_CTX_DEBUG_REGISTERS;
  1153. }
  1154. return 0;
  1155. }
  1156. /* gets the current impersonation token */
  1157. struct token *thread_get_impersonation_token( struct thread *thread )
  1158. {
  1159. if (thread->token)
  1160. return thread->token;
  1161. else
  1162. return thread->process->token;
  1163. }
  1164. /* check if a cpu type can be supported on this server */
  1165. int is_cpu_supported( enum cpu_type cpu )
  1166. {
  1167. unsigned int prefix_cpu_mask = get_prefix_cpu_mask();
  1168. if (supported_cpus & prefix_cpu_mask & CPU_FLAG(cpu)) return 1;
  1169. if (!(supported_cpus & prefix_cpu_mask))
  1170. set_error( STATUS_NOT_SUPPORTED );
  1171. else if (supported_cpus & CPU_FLAG(cpu))
  1172. set_error( STATUS_INVALID_IMAGE_WIN_64 ); /* server supports it but not the prefix */
  1173. else
  1174. set_error( STATUS_INVALID_IMAGE_FORMAT );
  1175. return 0;
  1176. }
  1177. /* return the cpu mask for supported cpus */
  1178. unsigned int get_supported_cpu_mask(void)
  1179. {
  1180. return supported_cpus & get_prefix_cpu_mask();
  1181. }
  1182. /* create a new thread */
  1183. DECL_HANDLER(new_thread)
  1184. {
  1185. struct thread *thread;
  1186. struct process *process;
  1187. struct unicode_str name;
  1188. const struct security_descriptor *sd;
  1189. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, NULL );
  1190. int request_fd = thread_get_inflight_fd( current, req->request_fd );
  1191. if (!(process = get_process_from_handle( req->process, PROCESS_CREATE_THREAD )))
  1192. {
  1193. if (request_fd != -1) close( request_fd );
  1194. return;
  1195. }
  1196. if (process != current->process)
  1197. {
  1198. if (request_fd != -1) /* can't create a request fd in a different process */
  1199. {
  1200. close( request_fd );
  1201. set_error( STATUS_INVALID_PARAMETER );
  1202. goto done;
  1203. }
  1204. if (process->running_threads) /* only the initial thread can be created in another process */
  1205. {
  1206. set_error( STATUS_ACCESS_DENIED );
  1207. goto done;
  1208. }
  1209. }
  1210. else if (request_fd == -1 || fcntl( request_fd, F_SETFL, O_NONBLOCK ) == -1)
  1211. {
  1212. if (request_fd != -1) close( request_fd );
  1213. set_error( STATUS_INVALID_HANDLE );
  1214. goto done;
  1215. }
  1216. if ((thread = create_thread( request_fd, process, sd )))
  1217. {
  1218. thread->system_regs = current->system_regs;
  1219. if (req->suspend) thread->suspend++;
  1220. reply->tid = get_thread_id( thread );
  1221. if ((reply->handle = alloc_handle_no_access_check( current->process, thread,
  1222. req->access, objattr->attributes )))
  1223. {
  1224. /* thread object will be released when the thread gets killed */
  1225. goto done;
  1226. }
  1227. kill_thread( thread, 1 );
  1228. }
  1229. done:
  1230. release_object( process );
  1231. }
  1232. static int init_thread( struct thread *thread, int reply_fd, int wait_fd )
  1233. {
  1234. if ((reply_fd = thread_get_inflight_fd( thread, reply_fd )) == -1)
  1235. {
  1236. set_error( STATUS_TOO_MANY_OPENED_FILES );
  1237. return 0;
  1238. }
  1239. if ((wait_fd = thread_get_inflight_fd( thread, wait_fd )) == -1)
  1240. {
  1241. set_error( STATUS_TOO_MANY_OPENED_FILES );
  1242. goto error;
  1243. }
  1244. if (thread->reply_fd) /* already initialised */
  1245. {
  1246. set_error( STATUS_INVALID_PARAMETER );
  1247. goto error;
  1248. }
  1249. if (fcntl( reply_fd, F_SETFL, O_NONBLOCK ) == -1) goto error;
  1250. thread->reply_fd = create_anonymous_fd( &thread_fd_ops, reply_fd, &thread->obj, 0 );
  1251. thread->wait_fd = create_anonymous_fd( &thread_fd_ops, wait_fd, &thread->obj, 0 );
  1252. return thread->reply_fd && thread->wait_fd;
  1253. error:
  1254. if (reply_fd != -1) close( reply_fd );
  1255. if (wait_fd != -1) close( wait_fd );
  1256. return 0;
  1257. }
  1258. /* initialize the first thread of a new process */
  1259. DECL_HANDLER(init_first_thread)
  1260. {
  1261. struct process *process = current->process;
  1262. if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
  1263. if (!is_valid_address(req->teb) || !is_valid_address(req->peb))
  1264. {
  1265. set_error( STATUS_INVALID_PARAMETER );
  1266. return;
  1267. }
  1268. if (!is_cpu_supported( req->cpu )) return;
  1269. current->unix_pid = process->unix_pid = req->unix_pid;
  1270. current->unix_tid = req->unix_tid;
  1271. current->teb = req->teb;
  1272. process->peb = req->peb;
  1273. process->ldt_copy = req->ldt_copy;
  1274. process->cpu = req->cpu;
  1275. if (!process->parent_id)
  1276. process->affinity = current->affinity = get_thread_affinity( current );
  1277. else
  1278. set_thread_affinity( current, current->affinity );
  1279. debug_level = max( debug_level, req->debug_level );
  1280. reply->pid = get_process_id( process );
  1281. reply->tid = get_thread_id( current );
  1282. reply->info_size = get_process_startup_info_size( process );
  1283. reply->server_start = server_start_time;
  1284. reply->all_cpus = supported_cpus & get_prefix_cpu_mask();
  1285. }
  1286. /* initialize a new thread */
  1287. DECL_HANDLER(init_thread)
  1288. {
  1289. if (!init_thread( current, req->reply_fd, req->wait_fd )) return;
  1290. if (!is_valid_address(req->teb))
  1291. {
  1292. set_error( STATUS_INVALID_PARAMETER );
  1293. return;
  1294. }
  1295. current->unix_pid = current->process->unix_pid;
  1296. current->unix_tid = req->unix_tid;
  1297. current->teb = req->teb;
  1298. current->entry_point = req->entry;
  1299. init_thread_context( current );
  1300. generate_debug_event( current, DbgCreateThreadStateChange, &req->entry );
  1301. set_thread_affinity( current, current->affinity );
  1302. reply->pid = get_process_id( current->process );
  1303. reply->tid = get_thread_id( current );
  1304. reply->suspend = (current->suspend || current->process->suspend || current->context != NULL);
  1305. }
  1306. /* terminate a thread */
  1307. DECL_HANDLER(terminate_thread)
  1308. {
  1309. struct thread *thread;
  1310. if ((thread = get_thread_from_handle( req->handle, THREAD_TERMINATE )))
  1311. {
  1312. thread->exit_code = req->exit_code;
  1313. if (thread != current) kill_thread( thread, 1 );
  1314. else reply->self = 1;
  1315. release_object( thread );
  1316. }
  1317. }
  1318. /* open a handle to a thread */
  1319. DECL_HANDLER(open_thread)
  1320. {
  1321. struct thread *thread = get_thread_from_id( req->tid );
  1322. reply->handle = 0;
  1323. if (thread)
  1324. {
  1325. reply->handle = alloc_handle( current->process, thread, req->access, req->attributes );
  1326. release_object( thread );
  1327. }
  1328. }
  1329. /* fetch information about a thread */
  1330. DECL_HANDLER(get_thread_info)
  1331. {
  1332. struct thread *thread;
  1333. unsigned int access = req->access & (THREAD_QUERY_INFORMATION | THREAD_QUERY_LIMITED_INFORMATION);
  1334. if (!access) access = THREAD_QUERY_LIMITED_INFORMATION;
  1335. thread = get_thread_from_handle( req->handle, access );
  1336. if (thread)
  1337. {
  1338. reply->pid = get_process_id( thread->process );
  1339. reply->tid = get_thread_id( thread );
  1340. reply->teb = thread->teb;
  1341. reply->entry_point = thread->entry_point;
  1342. reply->exit_code = (thread->state == TERMINATED) ? thread->exit_code : STATUS_PENDING;
  1343. reply->priority = thread->priority;
  1344. reply->affinity = thread->affinity;
  1345. reply->last = thread->process->running_threads == 1;
  1346. reply->suspend_count = thread->suspend;
  1347. reply->dbg_hidden = thread->dbg_hidden;
  1348. reply->desc_len = thread->desc_len;
  1349. if (thread->desc && get_reply_max_size())
  1350. {
  1351. if (thread->desc_len <= get_reply_max_size())
  1352. set_reply_data( thread->desc, thread->desc_len );
  1353. else
  1354. set_error( STATUS_BUFFER_TOO_SMALL );
  1355. }
  1356. release_object( thread );
  1357. }
  1358. }
  1359. /* fetch information about thread times */
  1360. DECL_HANDLER(get_thread_times)
  1361. {
  1362. struct thread *thread;
  1363. if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_LIMITED_INFORMATION )))
  1364. {
  1365. reply->creation_time = thread->creation_time;
  1366. reply->exit_time = thread->exit_time;
  1367. reply->unix_pid = thread->unix_pid;
  1368. reply->unix_tid = thread->unix_tid;
  1369. release_object( thread );
  1370. }
  1371. }
  1372. /* set information about a thread */
  1373. DECL_HANDLER(set_thread_info)
  1374. {
  1375. struct thread *thread;
  1376. if ((thread = get_thread_from_handle( req->handle, THREAD_SET_INFORMATION )))
  1377. {
  1378. set_thread_info( thread, req );
  1379. release_object( thread );
  1380. }
  1381. }
  1382. /* suspend a thread */
  1383. DECL_HANDLER(suspend_thread)
  1384. {
  1385. struct thread *thread;
  1386. if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
  1387. {
  1388. if (thread->state == TERMINATED) set_error( STATUS_ACCESS_DENIED );
  1389. else reply->count = suspend_thread( thread );
  1390. release_object( thread );
  1391. }
  1392. }
  1393. /* resume a thread */
  1394. DECL_HANDLER(resume_thread)
  1395. {
  1396. struct thread *thread;
  1397. if ((thread = get_thread_from_handle( req->handle, THREAD_SUSPEND_RESUME )))
  1398. {
  1399. reply->count = resume_thread( thread );
  1400. release_object( thread );
  1401. }
  1402. }
  1403. /* select on a handle list */
  1404. DECL_HANDLER(select)
  1405. {
  1406. select_op_t select_op;
  1407. data_size_t op_size;
  1408. struct thread_apc *apc;
  1409. const apc_result_t *result = get_req_data();
  1410. if (get_req_data_size() < sizeof(*result) ||
  1411. get_req_data_size() - sizeof(*result) < req->size ||
  1412. req->size & 3)
  1413. {
  1414. set_error( STATUS_INVALID_PARAMETER );
  1415. return;
  1416. }
  1417. if (get_req_data_size() - sizeof(*result) - req->size == sizeof(context_t))
  1418. {
  1419. const context_t *context = (const context_t *)((const char *)(result + 1) + req->size);
  1420. if ((current->context && current->context->status != STATUS_PENDING) || context->cpu != current->process->cpu)
  1421. {
  1422. set_error( STATUS_INVALID_PARAMETER );
  1423. return;
  1424. }
  1425. if (!current->context && !(current->context = create_thread_context( current ))) return;
  1426. copy_context( &current->context->regs, context,
  1427. context->flags & ~(current->context->regs.flags | get_context_system_regs(current->process->cpu)) );
  1428. current->context->status = STATUS_SUCCESS;
  1429. current->suspend_cookie = req->cookie;
  1430. wake_up( &current->context->obj, 0 );
  1431. }
  1432. if (!req->cookie)
  1433. {
  1434. set_error( STATUS_INVALID_PARAMETER );
  1435. return;
  1436. }
  1437. op_size = min( req->size, sizeof(select_op) );
  1438. memset( &select_op, 0, sizeof(select_op) );
  1439. memcpy( &select_op, result + 1, op_size );
  1440. /* first store results of previous apc */
  1441. if (req->prev_apc)
  1442. {
  1443. if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->prev_apc,
  1444. 0, &thread_apc_ops ))) return;
  1445. apc->result = *result;
  1446. apc->executed = 1;
  1447. if (apc->result.type == APC_CREATE_THREAD) /* transfer the handle to the caller process */
  1448. {
  1449. obj_handle_t handle = duplicate_handle( current->process, apc->result.create_thread.handle,
  1450. apc->caller->process, 0, 0, DUPLICATE_SAME_ACCESS );
  1451. close_handle( current->process, apc->result.create_thread.handle );
  1452. apc->result.create_thread.handle = handle;
  1453. clear_error(); /* ignore errors from the above calls */
  1454. }
  1455. wake_up( &apc->obj, 0 );
  1456. close_handle( current->process, req->prev_apc );
  1457. release_object( apc );
  1458. }
  1459. select_on( &select_op, op_size, req->cookie, req->flags, req->timeout );
  1460. while (get_error() == STATUS_USER_APC)
  1461. {
  1462. if (!(apc = thread_dequeue_apc( current, 0 )))
  1463. break;
  1464. /* Optimization: ignore APC_NONE calls, they are only used to
  1465. * wake up a thread, but since we got here the thread woke up already.
  1466. */
  1467. if (apc->call.type != APC_NONE &&
  1468. (reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
  1469. {
  1470. reply->call = apc->call;
  1471. release_object( apc );
  1472. break;
  1473. }
  1474. apc->executed = 1;
  1475. wake_up( &apc->obj, 0 );
  1476. release_object( apc );
  1477. }
  1478. if (get_error() == STATUS_KERNEL_APC)
  1479. {
  1480. apc = thread_dequeue_apc( current, 1 );
  1481. if ((reply->apc_handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 )))
  1482. reply->call = apc->call;
  1483. else
  1484. {
  1485. apc->executed = 1;
  1486. wake_up( &apc->obj, 0 );
  1487. }
  1488. release_object( apc );
  1489. }
  1490. else if (get_error() != STATUS_PENDING && get_reply_max_size() == sizeof(context_t) &&
  1491. current->context && current->suspend_cookie == req->cookie)
  1492. {
  1493. if (current->context->regs.flags)
  1494. {
  1495. unsigned int system_flags = get_context_system_regs(current->process->cpu) &
  1496. current->context->regs.flags;
  1497. if (system_flags) set_thread_context( current, &current->context->regs, system_flags );
  1498. set_reply_data( &current->context->regs, sizeof(context_t) );
  1499. }
  1500. release_object( current->context );
  1501. current->context = NULL;
  1502. }
  1503. }
  1504. /* queue an APC for a thread or process */
  1505. DECL_HANDLER(queue_apc)
  1506. {
  1507. struct thread *thread = NULL;
  1508. struct process *process = NULL;
  1509. struct thread_apc *apc;
  1510. if (!(apc = create_apc( NULL, &req->call ))) return;
  1511. switch (apc->call.type)
  1512. {
  1513. case APC_NONE:
  1514. case APC_USER:
  1515. thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT );
  1516. break;
  1517. case APC_VIRTUAL_ALLOC:
  1518. case APC_VIRTUAL_FREE:
  1519. case APC_VIRTUAL_PROTECT:
  1520. case APC_VIRTUAL_FLUSH:
  1521. case APC_VIRTUAL_LOCK:
  1522. case APC_VIRTUAL_UNLOCK:
  1523. case APC_UNMAP_VIEW:
  1524. process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
  1525. break;
  1526. case APC_VIRTUAL_QUERY:
  1527. process = get_process_from_handle( req->handle, PROCESS_QUERY_INFORMATION );
  1528. break;
  1529. case APC_MAP_VIEW:
  1530. process = get_process_from_handle( req->handle, PROCESS_VM_OPERATION );
  1531. if (process && process != current->process)
  1532. {
  1533. /* duplicate the handle into the target process */
  1534. obj_handle_t handle = duplicate_handle( current->process, apc->call.map_view.handle,
  1535. process, 0, 0, DUPLICATE_SAME_ACCESS );
  1536. if (handle) apc->call.map_view.handle = handle;
  1537. else
  1538. {
  1539. release_object( process );
  1540. process = NULL;
  1541. }
  1542. }
  1543. break;
  1544. case APC_CREATE_THREAD:
  1545. case APC_BREAK_PROCESS:
  1546. process = get_process_from_handle( req->handle, PROCESS_CREATE_THREAD );
  1547. break;
  1548. case APC_DUP_HANDLE:
  1549. process = get_process_from_handle( req->handle, PROCESS_DUP_HANDLE );
  1550. if (process && process != current->process)
  1551. {
  1552. /* duplicate the destination process handle into the target process */
  1553. obj_handle_t handle = duplicate_handle( current->process, apc->call.dup_handle.dst_process,
  1554. process, 0, 0, DUPLICATE_SAME_ACCESS );
  1555. if (handle) apc->call.dup_handle.dst_process = handle;
  1556. else
  1557. {
  1558. release_object( process );
  1559. process = NULL;
  1560. }
  1561. }
  1562. break;
  1563. default:
  1564. set_error( STATUS_INVALID_PARAMETER );
  1565. break;
  1566. }
  1567. if (thread)
  1568. {
  1569. if (!queue_apc( NULL, thread, apc )) set_error( STATUS_UNSUCCESSFUL );
  1570. release_object( thread );
  1571. }
  1572. else if (process)
  1573. {
  1574. reply->self = (process == current->process);
  1575. if (!reply->self)
  1576. {
  1577. obj_handle_t handle = alloc_handle( current->process, apc, SYNCHRONIZE, 0 );
  1578. if (handle)
  1579. {
  1580. if (queue_apc( process, NULL, apc ))
  1581. {
  1582. apc->caller = (struct thread *)grab_object( current );
  1583. reply->handle = handle;
  1584. }
  1585. else
  1586. {
  1587. close_handle( current->process, handle );
  1588. set_error( STATUS_PROCESS_IS_TERMINATING );
  1589. }
  1590. }
  1591. }
  1592. release_object( process );
  1593. }
  1594. release_object( apc );
  1595. }
  1596. /* Get the result of an APC call */
  1597. DECL_HANDLER(get_apc_result)
  1598. {
  1599. struct thread_apc *apc;
  1600. if (!(apc = (struct thread_apc *)get_handle_obj( current->process, req->handle,
  1601. 0, &thread_apc_ops ))) return;
  1602. if (apc->executed) reply->result = apc->result;
  1603. else set_error( STATUS_PENDING );
  1604. /* close the handle directly to avoid an extra round-trip */
  1605. close_handle( current->process, req->handle );
  1606. release_object( apc );
  1607. }
  1608. /* retrieve the current context of a thread */
  1609. DECL_HANDLER(get_thread_context)
  1610. {
  1611. struct context *thread_context = NULL;
  1612. unsigned int system_flags;
  1613. struct thread *thread;
  1614. context_t *context;
  1615. if (get_reply_max_size() < sizeof(context_t))
  1616. {
  1617. set_error( STATUS_INVALID_PARAMETER );
  1618. return;
  1619. }
  1620. if ((thread_context = (struct context *)get_handle_obj( current->process, req->handle, 0, &context_ops )))
  1621. {
  1622. close_handle( current->process, req->handle ); /* avoid extra server call */
  1623. system_flags = get_context_system_regs( thread_context->regs.cpu );
  1624. }
  1625. else if ((thread = get_thread_from_handle( req->handle, THREAD_GET_CONTEXT )))
  1626. {
  1627. clear_error();
  1628. system_flags = get_context_system_regs( thread->process->cpu );
  1629. if (thread->state == RUNNING)
  1630. {
  1631. reply->self = (thread == current);
  1632. if (thread != current) stop_thread( thread );
  1633. if (thread->context)
  1634. {
  1635. /* make sure that system regs are valid in thread context */
  1636. if (thread->unix_tid != -1 && (req->flags & system_flags & ~thread->context->regs.flags))
  1637. get_thread_context( thread, &thread->context->regs, req->flags & system_flags );
  1638. if (!get_error()) thread_context = (struct context *)grab_object( thread->context );
  1639. }
  1640. else if (!get_error() && (context = set_reply_data_size( sizeof(context_t) )))
  1641. {
  1642. assert( reply->self );
  1643. memset( context, 0, sizeof(context_t) );
  1644. context->cpu = thread->process->cpu;
  1645. if (req->flags & system_flags)
  1646. {
  1647. get_thread_context( thread, context, req->flags & system_flags );
  1648. context->flags |= req->flags & system_flags;
  1649. }
  1650. }
  1651. }
  1652. else set_error( STATUS_UNSUCCESSFUL );
  1653. release_object( thread );
  1654. }
  1655. if (get_error() || !thread_context) return;
  1656. set_error( thread_context->status );
  1657. if (!thread_context->status && (context = set_reply_data_size( sizeof(context_t) )))
  1658. {
  1659. memset( context, 0, sizeof(context_t) );
  1660. context->cpu = thread_context->regs.cpu;
  1661. copy_context( context, &thread_context->regs, req->flags );
  1662. context->flags = req->flags;
  1663. }
  1664. else if (thread_context->status == STATUS_PENDING)
  1665. {
  1666. reply->handle = alloc_handle( current->process, thread_context, SYNCHRONIZE, 0 );
  1667. }
  1668. release_object( thread_context );
  1669. }
  1670. /* set the current context of a thread */
  1671. DECL_HANDLER(set_thread_context)
  1672. {
  1673. struct thread *thread;
  1674. const context_t *context = get_req_data();
  1675. if (get_req_data_size() < sizeof(context_t))
  1676. {
  1677. set_error( STATUS_INVALID_PARAMETER );
  1678. return;
  1679. }
  1680. if (!(thread = get_thread_from_handle( req->handle, THREAD_SET_CONTEXT ))) return;
  1681. reply->self = (thread == current);
  1682. if (thread->state == TERMINATED) set_error( STATUS_UNSUCCESSFUL );
  1683. else if (context->cpu == thread->process->cpu)
  1684. {
  1685. unsigned int system_flags = get_context_system_regs(context->cpu) & context->flags;
  1686. if (thread != current) stop_thread( thread );
  1687. else if (system_flags) set_thread_context( thread, context, system_flags );
  1688. if (thread->context && !get_error())
  1689. {
  1690. copy_context( &thread->context->regs, context, context->flags );
  1691. thread->context->regs.flags |= context->flags;
  1692. }
  1693. }
  1694. else if (context->cpu == CPU_x86_64 && thread->process->cpu == CPU_x86)
  1695. {
  1696. /* convert the WoW64 context */
  1697. unsigned int system_flags = get_context_system_regs( context->cpu ) & context->flags;
  1698. if (system_flags)
  1699. {
  1700. set_thread_context( thread, context, system_flags );
  1701. if (thread->context && !get_error())
  1702. {
  1703. thread->context->regs.debug.i386_regs.dr0 = context->debug.x86_64_regs.dr0;
  1704. thread->context->regs.debug.i386_regs.dr1 = context->debug.x86_64_regs.dr1;
  1705. thread->context->regs.debug.i386_regs.dr2 = context->debug.x86_64_regs.dr2;
  1706. thread->context->regs.debug.i386_regs.dr3 = context->debug.x86_64_regs.dr3;
  1707. thread->context->regs.debug.i386_regs.dr6 = context->debug.x86_64_regs.dr6;
  1708. thread->context->regs.debug.i386_regs.dr7 = context->debug.x86_64_regs.dr7;
  1709. }
  1710. }
  1711. }
  1712. else set_error( STATUS_INVALID_PARAMETER );
  1713. release_object( thread );
  1714. }
  1715. /* fetch a selector entry for a thread */
  1716. DECL_HANDLER(get_selector_entry)
  1717. {
  1718. struct thread *thread;
  1719. if ((thread = get_thread_from_handle( req->handle, THREAD_QUERY_INFORMATION )))
  1720. {
  1721. get_selector_entry( thread, req->entry, &reply->base, &reply->limit, &reply->flags );
  1722. release_object( thread );
  1723. }
  1724. }