summaryrefslogtreecommitdiffhomepage
path: root/src/internal/ntapi_impl.h
blob: eb24deec3e55ee106c08cd7a55f44ca6642c4484 (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#ifndef ___NTAPI_IMPL_H_
#define ___NTAPI_IMPL_H_

#include <psxtypes/psxtypes.h>
#include <ntapi/nt_object.h>
#include <ntapi/nt_sysinfo.h>
#include <ntapi/nt_thread.h>
#include <ntapi/nt_process.h>
#include <ntapi/ntapi.h>
#include "ntapi_hash_table.h"
#include "ntapi_context.h"
#include "ntapi_fnapi.h"

#define __NT_BASED_NAMED_OBJECTS	{'\\','B','a','s','e', \
					'N','a','m','e','d', \
					'O','b','j','e','c','t','s'}

/* helper macros */
#define __NT_ROUND_UP_TO_POWER_OF_2(x,y)(x + (y-1)) & ~(y-1)
#define __NT_IS_MISALIGNED_BUFFER(x)	((!(uintptr_t)x) || ((uintptr_t)x % sizeof(size_t)))
#define __NT_IS_MISALIGNED_LENGTH(x)	(x % sizeof(size_t))
#define __NT_FILE_SYNC_IO		(NT_FILE_SYNCHRONOUS_IO_ALERT|NT_FILE_SYNCHRONOUS_IO_NONALERT)

/* user-defined options: head */
#ifndef __NT_TTY_MONITORS
#define __NT_TTY_MONITORS		0x10
#endif

#ifndef __NT_FORK_CHILD_WAIT_MILLISEC
#define __NT_FORK_CHILD_WAIT_MILLISEC	60000
#endif

#ifndef __NT_SYNC_BLOCK_LOCK_TRIES
#define __NT_SYNC_BLOCK_LOCK_TRIES	1024
#endif

#ifndef __NT_IPC_PAGES
#define __NT_IPC_PAGES			64
#endif
/* user-defined options: tail */

/* internal page size */
#ifndef __NT_INTERNAL_PAGE_SIZE
#define __NT_INTERNAL_PAGE_SIZE		4096
#endif

/* .bss section */
#ifndef __NT_BSS_RESERVED_PAGES
#define __NT_BSS_RESERVED_PAGES		10
#endif

/* runtime buffers */
#define __NT_BSS_ARGV_BUFFER_SIZE	__NT_INTERNAL_PAGE_SIZE * 2

#define __NT_BSS_IPC_BUFFER_SIZE	__NT_INTERNAL_PAGE_SIZE * 2

#define __NT_BSS_ARGV_MAX_IDX		__NT_BSS_ARGV_BUFFER_SIZE \
					/ sizeof(uintptr_t)

#define __NT_BSS_ARGS_BUFFER_SIZE	__NT_INTERNAL_PAGE_SIZE \
					* __NT_BSS_RESERVED_PAGES \
					- __NT_BSS_ARGV_BUFFER_SIZE \
					- __NT_BSS_IPC_BUFFER_SIZE

/* ntapi .bss section structure */
typedef struct ___ntapi_img_sec_bss {
	wchar16_t *	argv_envp_array[__NT_BSS_ARGV_MAX_IDX];
	char		args_envs_buffer[__NT_BSS_ARGS_BUFFER_SIZE];
	size_t		ipc_buffer[__NT_BSS_IPC_BUFFER_SIZE / sizeof(size_t)];
} __ntapi_img_sec_bss;


/* ntapi library internals */
typedef struct __attr_ptr_size_aligned__ _ntapi_internals {
	nt_runtime_data *				rtdata;
	nt_port_name *					subsystem;
	nt_security_descriptor				seq_desc;
	nt_security_quality_of_service			seq_qos;
	nt_sid_any					sid_buffer[2];
	nt_sid *					user;
	nt_sid *					admin;
	void *						hprocess;
	void *						htoken;
	void *						hport_tty_session;
	void *						hport_tty_daemon;
	void *						hport_tty_debug;
	void *						hport_tty_monitor[__NT_TTY_MONITORS];
	size_t						nt_mem_page_size;
	size_t						nt_mem_allocation_granularity;
	size_t						ntapi_internals_alloc_size;
	void **						csr_port_handle_addr;
	void *						hdev_mount_point_mgr;
	void *						hany[8];
	struct dalist_ex				ipc_conns;
	void *						ipc_pages[__NT_IPC_PAGES];
	int32_t						ipc_page;
	intptr_t					hlock;
	uintptr_t					v1_pipe_counter;
	ntapi_tt_get_csr_port_handle_addr_by_logic *	tt_get_csr_port_handle_addr_by_logic;
	__ntapi_img_sec_bss *				ntapi_img_sec_bss;
} ntapi_internals;


/* ipc internals */
typedef struct _nt_ipc_conn {
	nt_port_attr	attr;
	void *		hsection;
	void *		secaddr;
	size_t		secsize;
} nt_ipc_conn;


/* __ntapi_img_sec_data */
typedef struct __attr_ptr_size_aligned__ ___ntapi_img_sec_rdata {
	ntapi_hashed_symbol	__ntapi_import_table[__NT_IMPORTED_SYMBOLS_ARRAY_SIZE];
	ntapi_vtbl * 		__ntapi;
	nt_port_name		__session_name;
	ntapi_internals *	__internals;
} __ntapi_img_sec_rdata;

union __ntapi_img_rdata {
	__ntapi_img_sec_rdata	img_sec_data;
	char			buffer[__NT_INTERNAL_PAGE_SIZE];
};


/* accessor table */
extern	ntapi_vtbl	___ntapi;
extern	ntapi_vtbl	___ntapi_shadow;
#define	__ntapi		(&___ntapi)


/* access to library internals */
__attr_protected__ ntapi_internals * __cdecl __ntapi_internals(void);


/* helper functions */
__attr_protected__ int32_t __ntapi_tt_open_file_utf8(void ** hfile, void * hat, const char * arg, int fprivate, wchar16_t *, uint32_t);
__attr_protected__ int32_t __ntapi_tt_open_dir_utf8(void ** hfile, void * hat, const char * arg, int fprivate, wchar16_t *, uint32_t);

/* daemon internals */
__attr_protected__ int32_t __ntapi_dsr_start(nt_daemon_params *);
__attr_protected__ int32_t __ntapi_dsr_connect_internal_client(nt_daemon_params *);
__attr_protected__ int32_t __ntapi_dsr_internal_client_connect(nt_daemon_params *);

/* ipc internals */
__attr_protected__ int __ntapi_ipc_page_alloc(struct dalist_ex * dlist, void ** addr, size_t * alloc_size);

/* debug */
#define __ntidx(x) __offsetof(ntapi_vtbl,x) / sizeof(size_t)


#endif