thread.c 66 KB

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