summaryrefslogtreecommitdiffhomepage
path: root/src/vmount
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2015-07-27 04:01:18 -0400
committermidipix <writeonce@midipix.org>2015-07-27 04:01:18 -0400
commitdd89bb8ad4fe184a34b5dbdda237e640fc82121b (patch)
tree5e80d2da35f5892f92be29f57982b2708e6bd99b /src/vmount
parentdcdadc2702712fa750ed255ed1dfa354522797a0 (diff)
downloadntapi-dd89bb8ad4fe184a34b5dbdda237e640fc82121b.tar.bz2
ntapi-dd89bb8ad4fe184a34b5dbdda237e640fc82121b.tar.xz
entered advanced internal development stage.
Diffstat (limited to 'src/vmount')
-rw-r--r--src/vmount/ntapi_vms_cache.c209
-rw-r--r--src/vmount/ntapi_vms_client_connect.c86
-rw-r--r--src/vmount/ntapi_vms_client_disconnect.c37
-rw-r--r--src/vmount/ntapi_vms_helper.c118
-rw-r--r--src/vmount/ntapi_vms_point_attach.c52
-rw-r--r--src/vmount/ntapi_vms_ref_count.c96
-rw-r--r--src/vmount/ntapi_vms_table_query.c45
7 files changed, 643 insertions, 0 deletions
diff --git a/src/vmount/ntapi_vms_cache.c b/src/vmount/ntapi_vms_cache.c
new file mode 100644
index 0000000..97fe32f
--- /dev/null
+++ b/src/vmount/ntapi_vms_cache.c
@@ -0,0 +1,209 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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,
+ &region_addr,
+ &region_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;
+
+ /* 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;
+
+ 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;
+}
diff --git a/src/vmount/ntapi_vms_client_connect.c b/src/vmount/ntapi_vms_client_connect.c
new file mode 100644
index 0000000..364d4d1
--- /dev/null
+++ b/src/vmount/ntapi_vms_client_connect.c
@@ -0,0 +1,86 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.NTAPI. */
+/********************************************************/
+
+#include <psxtypes/psxtypes.h>
+#include <ntapi/nt_port.h>
+#include <ntapi/nt_tty.h>
+#include <ntapi/nt_vmount.h>
+#include <ntapi/ntapi.h>
+#include "ntapi_impl.h"
+
+
+static void __vms_port_name_from_server_info(
+ __out nt_port_name * vms_port_name,
+ __in nt_tty_vms_info * vmsinfo)
+{
+ nt_port_attr port_attr;
+
+ port_attr.type = NT_PORT_TYPE_VMOUNT;
+ port_attr.subtype = NT_PORT_SUBTYPE_DEFAULT;
+
+ __ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)&port_attr.keys,
+ (uintptr_t *)&vmsinfo->vms_keys,
+ sizeof(nt_port_keys));
+
+ __ntapi->tt_port_guid_from_type(
+ &port_attr.guid,
+ port_attr.type,
+ port_attr.subtype);
+
+ __ntapi->tt_port_name_from_attributes(
+ vms_port_name,
+ &port_attr);
+}
+
+
+int32_t __stdcall __ntapi_vms_client_connect(
+ __out void ** hvms,
+ __in nt_tty_vms_info * vmsinfo)
+{
+ int32_t status;
+ nt_port_name vms_port_name;
+
+ nt_unicode_string name;
+ nt_sqos sqos;
+ nt_oa oa;
+
+ /* vmount daemon port name */
+ __vms_port_name_from_server_info(
+ &vms_port_name,
+ vmsinfo);
+
+ /* port name init */
+ name.buffer = (wchar16_t *)&vms_port_name;
+ name.maxlen = 0;
+ name.strlen = (uint16_t)(size_t)(&((nt_port_name *)0)->null_termination);
+
+ /* init security structure */
+ sqos.length = sizeof(sqos);
+ sqos.impersonation_level = NT_SECURITY_IMPERSONATION;
+ sqos.context_tracking_mode = NT_SECURITY_TRACKING_DYNAMIC;
+ sqos.effective_only = 1;
+
+ /* init the port's object attributes */
+ oa.len = sizeof(oa);
+ oa.root_dir = (void *)0;
+ oa.obj_name = &name;
+ oa.obj_attr = 0;
+ oa.sec_desc = (nt_security_descriptor *)0;
+ oa.sec_qos = &sqos;
+
+ status = __ntapi->zw_connect_port(
+ hvms,
+ &name,
+ &sqos,
+ (nt_port_section_write *)0,
+ (nt_port_section_read *)0,
+ (uint32_t *)0,
+ (void *)0,
+ (uint32_t *)0);
+
+ return status;
+}
diff --git a/src/vmount/ntapi_vms_client_disconnect.c b/src/vmount/ntapi_vms_client_disconnect.c
new file mode 100644
index 0000000..b7d528c
--- /dev/null
+++ b/src/vmount/ntapi_vms_client_disconnect.c
@@ -0,0 +1,37 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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"
+
+
+int32_t __stdcall __ntapi_vms_client_disconnect(
+ __in void * hvms)
+{
+ nt_vms_daemon_msg msg;
+
+ if (!hvms) return NT_STATUS_INVALID_HANDLE;
+
+ /* msg */
+ __ntapi->tt_aligned_block_memset(&msg,0,sizeof(msg));
+
+ msg.header.msg_type = NT_LPC_NEW_MESSAGE;
+ msg.header.data_size = sizeof(msg.data);
+ msg.header.msg_size = sizeof(msg);
+ msg.data.msginfo.opcode = NT_VMS_CLIENT_DISCONNECT;
+
+ /* zw_request_wait_reply_port */
+ __ntapi->zw_request_wait_reply_port(
+ hvms,
+ &msg,
+ &msg);
+
+ /* close client handle */
+ return __ntapi->zw_close(hvms);
+}
diff --git a/src/vmount/ntapi_vms_helper.c b/src/vmount/ntapi_vms_helper.c
new file mode 100644
index 0000000..4134112
--- /dev/null
+++ b/src/vmount/ntapi_vms_helper.c
@@ -0,0 +1,118 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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"
+
+
+nt_vms_node * __stdcall __ntapi_vms_get_end_component_first_node(
+ __in nt_vms_system * pvms_sys,
+ __in uint32_t end_component_hash)
+{
+ nt_vms_node * node;
+
+ /* verify non-empty list and valid input */
+ if (!pvms_sys->dev_name_head_node || !end_component_hash)
+ return (nt_vms_node *)0;
+
+ /* find first node by end component hash */
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + pvms_sys->end_component_head_node);
+
+ while (node->next && (node->end_component_hash < end_component_hash))
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + node->next);
+
+ if (node->end_component_hash == end_component_hash)
+ return node;
+ else
+ return (nt_vms_node *)0;
+}
+
+
+static nt_vms_node * __stdcall __ntapi_vms_get_node(
+ __in nt_vms_system * pvms_sys,
+ __in uint32_t end_component_hash,
+ __in uint32_t dev_name_hash,
+ __in nt_large_integer index_number)
+{
+ nt_vms_node * node;
+
+ /* verify non-empty list */
+ if (!pvms_sys->dev_name_head_node)
+ return (nt_vms_node *)0;
+
+ /* end_component_hash */
+ if (end_component_hash) {
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + pvms_sys->end_component_head_node);
+
+ while (node->next && (node->end_component_hash < end_component_hash))
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + node->next);
+
+ if (node->end_component_hash != end_component_hash)
+ return (nt_vms_node *)0;
+ } else
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + pvms_sys->dev_name_head_node);
+
+ /* find device nodes */
+ while (node->next && (node->dev_name_hash < dev_name_hash))
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + node->next);
+
+ if (node->dev_name_hash != dev_name_hash)
+ return (nt_vms_node *)0;
+
+ /* find mount-point nodes */
+ while (node->next && (node->index_number.quad < index_number.quad))
+ node = (nt_vms_node *)((uintptr_t)pvms_sys + node->next);
+
+ if (node->index_number.quad != index_number.quad)
+ return (nt_vms_node *)0;
+
+ return node;
+}
+
+
+nt_vms_node * __stdcall __ntapi_vms_get_node_by_dev_name(
+ __in nt_vms_system * pvms_sys,
+ __in uint32_t dev_name_hash,
+ __in nt_large_integer index_number)
+{
+ return __ntapi_vms_get_node(
+ pvms_sys,
+ 0,
+ dev_name_hash,
+ index_number);
+}
+
+
+nt_vms_node * __stdcall __ntapi_vms_get_node_by_end_component(
+ __in nt_vms_system * pvms_sys,
+ __in uint32_t end_component_hash,
+ __in uint32_t dev_name_hash,
+ __in nt_large_integer index_number)
+{
+ return __ntapi_vms_get_node(
+ pvms_sys,
+ end_component_hash,
+ dev_name_hash,
+ index_number);
+}
+
+
+nt_vms_point * __stdcall __ntapi_vms_get_top_of_stack_mount_point(
+ __in nt_vms_system * pvms_sys,
+ __in nt_vms_node * node)
+{
+ nt_vms_point * point;
+
+ point = (nt_vms_point *)((uintptr_t)pvms_sys + node->stack);
+
+ while (point->next)
+ point = (nt_vms_point *)((uintptr_t)pvms_sys + point->next);
+
+ return point;
+}
diff --git a/src/vmount/ntapi_vms_point_attach.c b/src/vmount/ntapi_vms_point_attach.c
new file mode 100644
index 0000000..a4c5c7e
--- /dev/null
+++ b/src/vmount/ntapi_vms_point_attach.c
@@ -0,0 +1,52 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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"
+
+
+static int32_t __stdcall __ntapi_vms_point_attach_detach(
+ __in void * hvms,
+ __in nt_vms_point_info * point_info,
+ __in int32_t vms_opcode)
+{
+ int32_t status;
+ nt_vms_daemon_msg msg;
+
+ /* msg */
+ __ntapi->tt_aligned_block_memset(&msg,0,sizeof(msg));
+
+ msg.header.msg_type = NT_LPC_NEW_MESSAGE;
+ msg.header.data_size = sizeof(msg.data);
+ msg.header.msg_size = sizeof(msg);
+ msg.data.msginfo.opcode = vms_opcode;
+
+ /* copy point to msg */
+ __ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)&(msg.data.pointinfo),
+ (uintptr_t *)point_info,
+ sizeof(*point_info));
+
+ /* zw_request_wait_reply_port */
+ status = __ntapi->zw_request_wait_reply_port(hvms,&msg,&msg);
+
+ /* return vms status */
+ return status ? status : msg.data.msginfo.status;
+}
+
+
+int32_t __stdcall __ntapi_vms_point_attach(
+ __in void * hvms,
+ __in nt_vms_point_info * point_info)
+{
+ return __ntapi_vms_point_attach_detach(
+ hvms,
+ point_info,
+ NT_VMS_POINT_ATTACH);
+}
diff --git a/src/vmount/ntapi_vms_ref_count.c b/src/vmount/ntapi_vms_ref_count.c
new file mode 100644
index 0000000..3be149f
--- /dev/null
+++ b/src/vmount/ntapi_vms_ref_count.c
@@ -0,0 +1,96 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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"
+
+
+static int32_t __stdcall __ntapi_vms_ref_count_inc_dec(
+ __in void * hvms,
+ __in nt_vms_ref_count_info * ref_cnt_info,
+ __in int32_t vms_opcode)
+{
+ int32_t status;
+ nt_vms_daemon_msg msg;
+
+ /* msg */
+ __ntapi->tt_aligned_block_memset(&msg,0,sizeof(msg));
+
+ msg.header.msg_type = NT_LPC_NEW_MESSAGE;
+ msg.header.data_size = sizeof(msg.data);
+ msg.header.msg_size = sizeof(msg);
+ msg.data.msginfo.opcode = vms_opcode;
+
+ /* copy ref count info to msg */
+ __ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)&(msg.data.refcntinfo),
+ (uintptr_t *)ref_cnt_info,
+ sizeof(*ref_cnt_info));
+
+ /* zw_request_wait_reply_port */
+ status = __ntapi->zw_request_wait_reply_port(
+ hvms,
+ &msg,
+ &msg);
+
+ if (status) return status;
+
+ /* return info */
+ __ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)ref_cnt_info,
+ (uintptr_t *)&(msg.data.refcntinfo),
+ sizeof(*ref_cnt_info));
+
+ /* return vms status */
+ return status ? status : msg.data.msginfo.status;
+}
+
+
+int32_t __stdcall __ntapi_vms_ref_count_inc(
+ __in void * hvms,
+ __in nt_vms_ref_count_info * ref_cnt_info)
+{
+ return __ntapi_vms_ref_count_inc_dec(
+ hvms,
+ ref_cnt_info,
+ NT_VMS_REF_COUNT_INC);
+}
+
+
+int32_t __stdcall __ntapi_vms_ref_count_dec(
+ __in void * hvms,
+ __in nt_vms_ref_count_info * ref_cnt_info)
+{
+ return __ntapi_vms_ref_count_inc_dec(
+ hvms,
+ ref_cnt_info,
+ NT_VMS_REF_COUNT_DEC);
+}
+
+
+int32_t __stdcall __ntapi_vms_point_detach(
+ __in void * hvms,
+ __in nt_vms_ref_count_info * ref_cnt_info)
+{
+ return __ntapi_vms_ref_count_inc_dec(
+ hvms,
+ ref_cnt_info,
+ NT_VMS_POINT_DETACH);
+}
+
+
+int32_t __stdcall __ntapi_vms_point_get_handles(
+ __in void * hvms,
+ __in nt_vms_ref_count_info * ref_cnt_info)
+{
+ return __ntapi_vms_ref_count_inc_dec(
+ hvms,
+ ref_cnt_info,
+ NT_VMS_POINT_GET_HANDLES);
+}
diff --git a/src/vmount/ntapi_vms_table_query.c b/src/vmount/ntapi_vms_table_query.c
new file mode 100644
index 0000000..847a58f
--- /dev/null
+++ b/src/vmount/ntapi_vms_table_query.c
@@ -0,0 +1,45 @@
+/********************************************************/
+/* ntapi: Native API core library */
+/* Copyright (C) 2013,2014,2015 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"
+
+
+int32_t __stdcall __ntapi_vms_table_query(
+ __in void * hvms,
+ __in nt_vms_daemon_info * vms_info)
+{
+ int32_t status;
+ nt_vms_daemon_msg msg;
+
+ /* msg */
+ __ntapi->tt_aligned_block_memset(&msg,0,sizeof(msg));
+
+ msg.header.msg_type = NT_LPC_NEW_MESSAGE;
+ msg.header.data_size = sizeof(msg.data);
+ msg.header.msg_size = sizeof(msg);
+ msg.data.msginfo.opcode = NT_VMS_TABLE_QUERY;
+
+ /* zw_request_wait_reply_port */
+ status = __ntapi->zw_request_wait_reply_port(
+ hvms,
+ &msg,
+ &msg);
+
+ if (status) return status;
+
+ /* return info */
+ __ntapi->tt_aligned_block_memcpy(
+ (uintptr_t *)vms_info,
+ (uintptr_t *)&(msg.data.vmsinfo),
+ sizeof(*vms_info));
+
+ /* return vms status */
+ return status ? status : msg.data.msginfo.status;
+}