thread.c 66 KB

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