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
|
/********************************************************/
/* ntapi: Native API core library */
/* Copyright (C) 2013--2016 Z. Gilboa */
/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
/********************************************************/
#include <psxtypes/psxtypes.h>
#include <ntapi/nt_port.h>
#include <ntapi/nt_vmount.h>
#include <ntapi/ntapi.h>
#include "ntapi_impl.h"
typedef struct nt_vms_cache_interface {
nt_vms_system * vms_sys;
struct dalist_ex cache;
size_t alloc_size;
uintptr_t buffer[1];
} nt_vms_cache_context;
typedef struct _nt_vms_cache_record {
void * hfile;
uint32_t dev_name_hash;
nt_large_integer index_number;
intptr_t client_key;
intptr_t server_key;
} nt_vms_cache_record;
int32_t __stdcall __ntapi_vms_cache_free(
__in nt_vms_cache vms_cache)
{
int32_t status;
void * region_addr;
size_t region_size;
/* validation */
if (!vms_cache)
return NT_STATUS_INVALID_PARAMETER;
/* free memory */
region_addr = vms_cache;
region_size = vms_cache->alloc_size;
status = __ntapi->zw_free_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
®ion_addr,
®ion_size,
NT_MEM_RELEASE);
return status;
}
/* vms optional cache functions */
nt_vms_cache __stdcall __ntapi_vms_cache_alloc(
__in nt_vms_system * vms_sys,
__in uint32_t flags __reserved,
__in void * options __reserved,
__out int32_t * status __optional)
{
int32_t _status;
void * buffer;
size_t buffer_size;
nt_vms_cache_context * vms_cache;
/* unused params */
(void)flags;
(void)options;
/* status */
if (!status) status = &_status;
/* validation */
if (!vms_sys) {
*status = NT_STATUS_INVALID_PARAMETER;
return (nt_vms_cache)0;
}
/* calculate size */
buffer_size = sizeof(nt_vms_cache_context);
buffer_size += vms_sys->vms_points_cap * (sizeof(nt_vms_cache_record) - sizeof(uintptr_t));
/* allocate buffer */
*status = __ntapi->zw_allocate_virtual_memory(
NT_CURRENT_PROCESS_HANDLE,
&buffer,
0,
&buffer_size,
NT_MEM_COMMIT,
NT_PAGE_READWRITE);
if (*status) return (nt_vms_cache)0;
/* init vms cache */
vms_cache = (nt_vms_cache_context *)buffer;
vms_cache->vms_sys = vms_sys;
vms_cache->alloc_size = buffer_size;
/* init list */
*status = dalist_init_ex(
&vms_cache->cache,
sizeof(nt_vms_cache_record),
0x1000,
__ntapi->zw_allocate_virtual_memory,
DALIST_MEMFN_NT_ALLOCATE_VIRTUAL_MEMORY);
if (*status != DALIST_OK) {
*status = NT_STATUS_UNSUCCESSFUL;
__ntapi_vms_cache_free(vms_cache);
return (nt_vms_cache)0;
}
/* set list buffer */
buffer_size -= (size_t)&(((nt_vms_cache_context *)0)->buffer);
*status = dalist_deposit_memory_block(
&vms_cache->cache,
&vms_cache->buffer,
buffer_size);
return vms_cache;
}
int32_t __stdcall __ntapi_vms_cache_record_append(
__in nt_vms_cache cache,
__in void * hfile,
__in uint32_t dev_name_hash,
__in nt_large_integer index_number,
__in intptr_t client_key,
__in intptr_t server_key)
{
int32_t status;
struct dalist_node_ex * node;
nt_vms_cache_record * cache_record;
status = dalist_get_node_by_key(
&cache->cache,
&node,
(uintptr_t)hfile,
DALIST_NODE_TYPE_EXISTING,
(uintptr_t *)0);
if (status != DALIST_OK)
status = NT_STATUS_INTERNAL_ERROR;
else if (node)
status = NT_STATUS_OBJECTID_EXISTS;
else {
status = dalist_get_free_node(&cache->cache,(void **)&node);
if (status == DALIST_OK) {
cache_record = (nt_vms_cache_record *)&node->dblock;
__ntapi->tt_aligned_block_memset(
node,
0,
(uintptr_t)&((struct dalist_node_ex *)0)->dblock + sizeof(*cache_record));
node->key = (uintptr_t)hfile;
cache_record->hfile = hfile;
cache_record->dev_name_hash = dev_name_hash;
cache_record->index_number.quad = index_number.quad;
cache_record->client_key = client_key;
cache_record->server_key = server_key;
status = dalist_insert_node_by_key(
&cache->cache,
node);
if (status != DALIST_OK)
dalist_deposit_free_node(
&cache->cache,
node);
}
}
return status;
}
int32_t __stdcall __ntapi_vms_cache_record_remove(
__in nt_vms_cache cache,
__in void * hfile,
__in uint32_t dev_name_hash,
__in nt_large_integer index_number)
{
int32_t status;
struct dalist_node_ex * node;
(void)dev_name_hash;
(void)index_number;
status = dalist_get_node_by_key(
&cache->cache,
&node,
(uintptr_t)hfile,
DALIST_NODE_TYPE_EXISTING,
(uintptr_t *)0);
if (status != DALIST_OK)
status = NT_STATUS_INTERNAL_ERROR;
else if (node)
status = NT_STATUS_INVALID_PARAMETER;
else {
status = dalist_discard_node(
&cache->cache,
node);
if (status != DALIST_OK)
status = NT_STATUS_INTERNAL_ERROR;
}
return status;
}
|