thread.c 65 KB

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