thread.c 62 KB

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