summaryrefslogtreecommitdiffhomepage
path: root/src/thread/ntapi_tt_create_thread.c
blob: 2d469964d078cb8376a05c012fbc64c38de283a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>
#include <ntapi/nt_status.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_memory.h>
#include <ntapi/nt_thread.h>
#include <ntapi/nt_process.h>
#include <ntapi/nt_string.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"

/* (no planned support of alpha processors, use constant values) */
#define __PAGE_SIZE 		0x001000
#define __GRANULARITY		0x010000
#define __RESERVE_ROUND_UP	0x100000

static int32_t __create_thread_fail(
	void *	hprocess,
	void *	stack_bottom,
	size_t	stack_size,
	int32_t status)
{
	__ntapi->zw_free_virtual_memory(
		hprocess,
		&stack_bottom,
		&stack_size,
		NT_MEM_RELEASE);
	return status;
}


static int32_t __tt_create_thread_stack(
	nt_thread_params *	params,
	nt_user_stack *		stack)
{
	int32_t		status;
	uint32_t	protect_type_old;
	void *		stack_system_limit;
	char *		base;
	size_t		size;
	size_t		commit;

	/* caller-provided stack? */
	if (params->stack_info && params->stack_info->expandable_stack_base) {
		stack->fixed_stack_base        = params->stack_info->fixed_stack_base;
		stack->fixed_stack_limit       = params->stack_info->fixed_stack_limit;
		stack->expandable_stack_base   = params->stack_info->expandable_stack_base;
		stack->expandable_stack_limit  = params->stack_info->expandable_stack_limit;
		stack->expandable_stack_bottom = params->stack_info->expandable_stack_bottom;

		return NT_STATUS_SUCCESS;
	}

	/* init */
	params->stack_size_commit  = __NT_ROUND_UP_TO_POWER_OF_2(params->stack_size_commit+params->ext_ctx_size, __PAGE_SIZE);
	params->stack_size_reserve = __NT_ROUND_UP_TO_POWER_OF_2(params->stack_size_reserve,__GRANULARITY);

	/* compare, round-up as needed */
	if (params->stack_size_commit >= params->stack_size_reserve)
		params->stack_size_reserve = __NT_ROUND_UP_TO_POWER_OF_2(params->stack_size_commit,__RESERVE_ROUND_UP);

	/**
	  *
	  *  --------- BASE ----------
	  *
	  *  ---- (COMMITED AREA) ----
	  *
	  *  --------- LIMIT ---------
	  *
	  *  ------ GUARD PAGE -------
	  *
	  *  ------ ACTUAL LIMIT -----
	  *
	  *  ---- (RESERVED AREA) ----
	  *
	  *  -------- BOTTOM ---------
	  *
	**/

	/* stack structure: unused fields */
	stack->fixed_stack_base  = 0;
	stack->fixed_stack_limit = 0;

	/* first we reserve */
	stack->expandable_stack_bottom = 0;

	if ((status = __ntapi->zw_allocate_virtual_memory(
			params->hprocess,
			&stack->expandable_stack_bottom,
			params->stack_zero_bits,
			&params->stack_size_reserve,
			NT_MEM_RESERVE,
			NT_PAGE_READWRITE)))
		return status;

	/* calculate base and limit */
	base  = stack->expandable_stack_bottom;
	base += params->stack_size_reserve;

	stack->expandable_stack_base   = base;
	stack->expandable_stack_limit  = base - params->stack_size_commit;

	/* guard page */
	commit             = params->stack_size_commit + __PAGE_SIZE;
	stack_system_limit = base - commit;

	/* then we commit */
	if ((status = __ntapi->zw_allocate_virtual_memory(
			params->hprocess,
			&stack_system_limit,
			0,&commit,
			NT_MEM_COMMIT,
			NT_PAGE_READWRITE)))
		return __create_thread_fail(
			params->hprocess,
			stack->expandable_stack_bottom,
			params->stack_size_reserve,
			status);

	/* finally we protect the guard page */
	size = __PAGE_SIZE;

	if ((status = __ntapi->zw_protect_virtual_memory(
			params->hprocess,
			&stack_system_limit,
			&size,
			NT_PAGE_READWRITE | NT_MEM_PAGE_GUARD,
			&protect_type_old)))
		return __create_thread_fail(
			params->hprocess,
			stack->expandable_stack_bottom,
			params->stack_size_reserve,
			status);

	/* all done */
	if (params->stack_info) {
		params->stack_info->fixed_stack_base        = stack->fixed_stack_base;
		params->stack_info->fixed_stack_limit       = stack->fixed_stack_limit;
		params->stack_info->expandable_stack_base   = stack->expandable_stack_base;
		params->stack_info->expandable_stack_limit  = stack->expandable_stack_limit;
		params->stack_info->expandable_stack_bottom = stack->expandable_stack_bottom;

		return NT_STATUS_SUCCESS;
	}

	return NT_STATUS_SUCCESS;
}

int32_t __stdcall __ntapi_tt_create_thread(nt_thread_params * params)
{
	int32_t				status;
	ntapi_internals *		__internals;

	nt_client_id			cid;
	nt_port_message_csrss_process	csrss_msg;
	nt_port_message_csrss_process *	csrss_msg_1st;
	nt_port_message_csrss_thread *	csrss_msg_any;

	nt_thread_context 	__attr_aligned__(0x40) context;
	nt_user_stack		__attr_aligned__(0x10) stack;
	uintptr_t		fsuspended;
	uintptr_t *		parg;

	if (!(params->stack_size_commit))
		return NT_STATUS_INVALID_PARAMETER;
	else if (!(params->stack_size_reserve))
		return NT_STATUS_INVALID_PARAMETER;
	else if (params->ext_ctx_size > __NT_INTERNAL_PAGE_SIZE)
		return NT_STATUS_INVALID_PARAMETER;
	else if (params->ext_ctx_size % sizeof(intptr_t))
		return NT_STATUS_INVALID_PARAMETER;
	else if (params->arg && params->ext_ctx)
		return NT_STATUS_INVALID_PARAMETER_MIX;
	else if (params->ext_ctx && !params->ext_ctx_size)
		return NT_STATUS_INVALID_PARAMETER_MIX;

	/* init */
	__internals  = __ntapi_internals();

	/* stack */
	if ((status = __tt_create_thread_stack(params,&stack)))
		return status;

	/* context */
	if (params->reg_context) {
		__ntapi->tt_aligned_block_memcpy(
			(uintptr_t *)&context,
			(uintptr_t *)params->reg_context,
			sizeof(context));
	} else {
		__ntapi->tt_aligned_block_memset(
			&context,0,sizeof(context));

		__INIT_CONTEXT(context);
		context.INSTRUCTION_POINTER_REGISTER = (uintptr_t)params->start;
		context.STACK_POINTER_REGISTER       = (uintptr_t)(stack.expandable_stack_base);
	}







/*****************************************************************************/
/*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/
/*                                                                           */
/*                                                                           */
/*      INNOVATION IN THE FIELD OF MULTI-THREADED COMPUTER PROGRAMMING       */
/*                                                                           */
/*      A "RAPUNZEL" TOP-OF-STACK, VARIABLE-SIZE ENTRY-ROUTINE CONTEXT       */
/*                                                                           */
/*                   COPYRIGHT (C) 2013,2014,2015 ZVI GILBOA                 */
/*                                                                           */
/*                                                                           */
/*                                                                           */
/*      Laß mir dein Haar herunter.«                                         */
/**/	if (params->ext_ctx) {                                             /**/
/**/		context.STACK_POINTER_REGISTER -= params->ext_ctx_size;    /**/
/**/		params->arg = (void *)context.STACK_POINTER_REGISTER;      /**/
/**/                                                                       /**/
/**/		if (params->creation_flags & NT_CREATE_LOCAL_THREAD)       /**/
/**/			__ntapi->tt_aligned_block_memcpy(                  /**/
/**/				(uintptr_t *)params->arg,                  /**/
/**/				(uintptr_t *)params->ext_ctx,              /**/
/**/				params->ext_ctx_size);                     /**/
/**/		else {                                                     /**/
/**/			status = __ntapi->zw_write_virtual_memory(         /**/
/**/				params->hprocess,                          /**/
/**/				params->arg,                               /**/
/**/				(char *)params->ext_ctx,                   /**/
/**/				params->ext_ctx_size,                      /**/
/**/				0);                                        /**/
/**/                                                                       /**/
/**/			if (status) return __create_thread_fail(           /**/
/**/				params->hprocess,                          /**/
/**/				stack.expandable_stack_bottom,             /**/
/**/				params->stack_size_reserve,                /**/
/**/				status);                                   /**/
/**/		}                                                          /**/
/**/	}                                                                  /**/
/**/                                                                       /**/
/**/                                                                       /**/
/**/                                                                       /**/
/*      entry-routine argument address and stack pointer adjustment          */
/**/	if (sizeof(intptr_t) == 4) {                                       /**/
/**/		context.STACK_POINTER_REGISTER -= sizeof(intptr_t);        /**/
/**/		parg  = (uintptr_t *)context.STACK_POINTER_REGISTER;       /**/
/**/	} else                                                             /**/
/**/		parg  = &context.FAST_CALL_ARG0;                           /**/
/**/                                                                       /**/
/**/                                                                       /**/
/*      write entry-routine argument                                         */
/**/	if ((sizeof(size_t) == 8)                                          /**/
/**/			|| (params->creation_flags&NT_CREATE_LOCAL_THREAD))/**/
/**/		*parg = (uintptr_t)params->arg;                            /**/
/**/	else {                                                             /**/
/**/		status = __ntapi->zw_write_virtual_memory(                 /**/
/**/			params->hprocess,                                  /**/
/**/			parg,                                              /**/
/**/			(char *)&params->arg,                              /**/
/**/			sizeof(uintptr_t),                                 /**/
/**/			0);                                                /**/
/**/                                                                       /**/
/**/		if (status) return __create_thread_fail(                   /**/
/**/			params->hprocess,                                  /**/
/**/			stack.expandable_stack_bottom,                     /**/
/**/			params->stack_size_reserve,                        /**/
/**/			status);                                           /**/
/**/	}                                                                  /**/
/**/                                                                       /**/
/**/                                                                       /**/
/*-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*/
/*****************************************************************************/











	/* create thread */
	fsuspended = (params->creation_flags & NT_CREATE_SUSPENDED)
		? 1 : __ntapi->zw_create_user_process
			? 0 : 1;

	if ((status = __ntapi->zw_create_thread(
			&params->hthread,
			NT_THREAD_ALL_ACCESS,
			params->obj_attr,
			params->hprocess,
			&cid,&context,&stack,
			fsuspended)))
		return __create_thread_fail(
			params->hprocess,
			stack.expandable_stack_bottom,
			params->stack_size_reserve,
			status);

	/* for os versions prior to hasta la */
	if (!__ntapi->zw_create_user_process) {
		__ntapi->tt_aligned_block_memset(&csrss_msg,0,sizeof(csrss_msg));

		if (params->creation_flags & NT_CREATE_FIRST_THREAD_OF_PROCESS) {
			/* nt_port_message_csrss_process is the larger structure */
			csrss_msg_1st = &csrss_msg;

			csrss_msg_1st->header.data_size		= sizeof(nt_port_message_csrss_process) - sizeof(nt_port_message);
			csrss_msg_1st->header.msg_size		= sizeof(nt_port_message_csrss_process);
			csrss_msg_1st->opcode			= 0x10000;
			csrss_msg_1st->hprocess			= params->hprocess;
			csrss_msg_1st->hthread			= params->hthread;
			csrss_msg_1st->unique_process_id	= cid.process_id;
			csrss_msg_1st->unique_thread_id		= cid.thread_id;
		} else {
			/* nt_port_message_csrss_thread is the smaller structure */
			csrss_msg_any = (nt_port_message_csrss_thread *)&csrss_msg;

			csrss_msg_any->header.data_size		= sizeof(nt_port_message_csrss_thread) - sizeof(nt_port_message);
			csrss_msg_any->header.msg_size		= sizeof(nt_port_message_csrss_thread);
			csrss_msg_any->opcode			= 0x10001;
			csrss_msg_any->hthread			= params->hthread;
			csrss_msg_any->unique_process_id	= cid.process_id;
			csrss_msg_any->unique_thread_id		= cid.thread_id;
		}

		/* send csrss a new-thread notification */
		if (__internals->csr_port_handle_addr) {
			status = __ntapi->zw_request_wait_reply_port(
				*__internals->csr_port_handle_addr,
				&csrss_msg,&csrss_msg);
		}

		/* output csrss_status to caller */
		params->csrss_status = status
			? status
			: csrss_msg.status;
	}

	/* resume thread, close handle as needed */
	if (fsuspended && !(params->creation_flags & NT_CREATE_SUSPENDED))
		status = __ntapi->zw_resume_thread(params->hthread,0);

	if (params->creation_flags & NT_CLOSE_THREAD_HANDLE)
		__ntapi->zw_close(params->hthread);

	/* and finally */
	params->cid.process_id = cid.process_id;
	params->cid.thread_id  = cid.thread_id;

	return NT_STATUS_SUCCESS;
}


int32_t __stdcall __ntapi_tt_create_local_thread(nt_thread_params * params)
{
	nt_status			status;
	void *				image_base;
	struct pe_stack_heap_info	stack_heap_info;

	/* flags */
	params->creation_flags |= NT_CREATE_LOCAL_THREAD;

	/* hprocess */
	if (!params->hprocess)
		params->hprocess = __ntapi_internals()->hprocess;

	/* stack defaults */
	if (params->stack_info)
		(void)0;

	else if (params->stack_size_commit && params->stack_size_reserve)
		(void)0;

	else {
		/* image_base*/
		if (!(image_base = pe_get_first_module_handle()))
			return NT_STATUS_INVALID_IMPORT_OF_NON_DLL;

		if ((status = pe_get_image_stack_heap_info(
				image_base,
				&stack_heap_info)))
			return NT_STATUS_INVALID_IMAGE_FORMAT;

		/* stack_size_commit */
		if (!params->stack_size_commit)
			params->stack_size_commit = stack_heap_info.size_of_stack_commit;

		/* stack_size_reserve */
		if (!params->stack_size_reserve)
			params->stack_size_reserve = stack_heap_info.size_of_stack_reserve;

		if (!(params->stack_size_commit && params->stack_size_reserve))
			return NT_STATUS_INVALID_IMAGE_FORMAT;
	}

	return __ntapi_tt_create_thread(params);
}


int32_t __stdcall __ntapi_tt_create_remote_thread(nt_thread_params * params)
{
	return __ntapi_tt_create_thread(params);
}


void * __cdecl __ntapi_csr_port_handle(nt_status * pstatus)
{
	nt_status		status;
	ntapi_internals *	__internals;

	pstatus     = pstatus ? pstatus : &status;
	__internals = __ntapi_internals();

	if (__internals->csr_port_handle_addr) {
		*pstatus = NT_STATUS_SUCCESS;
		return *__internals->csr_port_handle_addr;
	} else {
		*pstatus = NT_STATUS_UNSUCCESSFUL;
		return 0;
	}
}