thread.c 67 KB

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