diff --git a/include/ntapi/nt_sync.h b/include/ntapi/nt_sync.h index 2057632..700be38 100644 --- a/include/ntapi/nt_sync.h +++ b/include/ntapi/nt_sync.h @@ -54,9 +54,6 @@ typedef enum _nt_io_completion_info_class { } nt_io_completion_info_class; -/* cache block size */ -#define NT_SYNC_BLOCK_SIZE 64 - /* timer access bits */ #define NT_TIMER_QUERY_STATE 0x00000001U #define NT_TIMER_MODIFY_STATE 0x00000002U @@ -89,9 +86,7 @@ typedef enum _nt_io_completion_info_class { #define NT_SYNC_NON_ALERTABLE 0x00000000U #define NT_SYNC_ALERTABLE 0x00000001U -/* sync block flag bits */ -#define NT_SYNC_BLOCK_YIELD_TO_SERVER 0x00000001U - +/* sync structures */ typedef struct _nt_timer_basic_information { nt_large_integer timer_remaining; int32_t signal_state; @@ -122,25 +117,6 @@ typedef struct _nt_io_completion_basic_information { } nt_io_completion_basic_information; -typedef union __attr_aligned__(NT_SYNC_BLOCK_SIZE) _nt_sync_block { - intptr_t cache_line[NT_SYNC_BLOCK_SIZE/sizeof(intptr_t)]; - struct { - int32_t tid; - int32_t pid; - uint32_t flags; - uint32_t srvtid; - int32_t lock_tries; - uint32_t ref_cnt; - uint32_t busy; - int32_t invalid; - nt_timeout lock_wait; - void * hwait; - void * hsignal; - void * hserver; - }; -} nt_sync_block; - - typedef void __stdcall nt_timer_apc_routine( void * timer_context, uint32_t timer_low_value, @@ -385,37 +361,6 @@ typedef int32_t __stdcall ntapi_tt_create_private_event( __in int32_t initial_state); -typedef void __stdcall ntapi_tt_sync_block_init( - __in nt_sync_block * sync_block, - __in uint32_t flags __optional, - __in int32_t srvtid __optional, - __in int32_t default_lock_tries __optional, - __in int64_t default_lock_wait __optional, - __in void * hsignal __optional); - - -typedef int32_t __stdcall ntapi_tt_sync_block_discard( - __in nt_sync_block * sync_block); - - -typedef int32_t __stdcall ntapi_tt_sync_block_lock( - __in nt_sync_block * sync_block, - __in int32_t lock_tries __optional, - __in int64_t lock_wait __optional, - __in uint32_t * sig_flag __optional); - - -typedef int32_t __stdcall ntapi_tt_sync_block_unlock( - __in nt_sync_block * sync_block); - - -typedef int32_t __stdcall ntapi_tt_sync_block_server_lock( - __in nt_sync_block * sync_block, - __in int32_t lock_tries __optional, - __in int64_t lock_wait __optional, - __in uint32_t * sig_flag __optional); - - typedef int32_t __stdcall ntapi_tt_wait_for_dummy_event(void); #endif diff --git a/include/ntapi/ntapi.h b/include/ntapi/ntapi.h index ab82ac7..a5e91e3 100644 --- a/include/ntapi/ntapi.h +++ b/include/ntapi/ntapi.h @@ -536,11 +536,6 @@ typedef struct _ntapi_vtbl { /* nt_sync.h */ ntapi_tt_create_inheritable_event * tt_create_inheritable_event; ntapi_tt_create_private_event * tt_create_private_event; - ntapi_tt_sync_block_init * tt_sync_block_init; - ntapi_tt_sync_block_discard * tt_sync_block_discard; - ntapi_tt_sync_block_lock * tt_sync_block_lock; - ntapi_tt_sync_block_unlock * tt_sync_block_unlock; - ntapi_tt_sync_block_server_lock * tt_sync_block_server_lock; ntapi_tt_wait_for_dummy_event * tt_wait_for_dummy_event; /* nt_port.h */ diff --git a/project/common.mk b/project/common.mk index 5350b61..84749be 100644 --- a/project/common.mk +++ b/project/common.mk @@ -117,7 +117,6 @@ COMMON_SRCS = \ src/string/ntapi_tt_uintptr_to_hex_utf16.c \ src/string/ntapi_tt_uintptr_to_hex_utf8.c \ src/sync/ntapi_tt_create_event.c \ - src/sync/ntapi_tt_sync_block.c \ src/sync/ntapi_tt_wait_for_dummy_event.c \ src/system/ntapi_tt_get_csr_port_handle_addr_by_logic.c \ src/system/ntapi_tt_get_system_directory.c \ diff --git a/src/internal/ntapi.c b/src/internal/ntapi.c index 7fa5ceb..abddd1e 100644 --- a/src/internal/ntapi.c +++ b/src/internal/ntapi.c @@ -320,11 +320,6 @@ static int32_t __fastcall __ntapi_init_once(ntapi_vtbl ** pvtbl) __ntapi->tt_create_inheritable_event = __ntapi_tt_create_inheritable_event; __ntapi->tt_create_private_event = __ntapi_tt_create_private_event; __ntapi->tt_wait_for_dummy_event = __ntapi_tt_wait_for_dummy_event; - __ntapi->tt_sync_block_init = __ntapi_tt_sync_block_init; - __ntapi->tt_sync_block_discard = __ntapi_tt_sync_block_discard; - __ntapi->tt_sync_block_lock = __ntapi_tt_sync_block_lock; - __ntapi->tt_sync_block_unlock = __ntapi_tt_sync_block_unlock; - __ntapi->tt_sync_block_server_lock = __ntapi_tt_sync_block_server_lock; /* nt_port.h */ __ntapi->csr_port_handle = __ntapi_csr_port_handle; diff --git a/src/internal/ntapi_fnapi.h b/src/internal/ntapi_fnapi.h index 767e1f3..6bade8c 100644 --- a/src/internal/ntapi_fnapi.h +++ b/src/internal/ntapi_fnapi.h @@ -190,11 +190,6 @@ NTAPI_UFN(tt_get_section_name); NTAPI_UFN(tt_create_inheritable_event); NTAPI_UFN(tt_create_private_event); NTAPI_UFN(tt_wait_for_dummy_event); -NTAPI_UFN(tt_sync_block_init); -NTAPI_UFN(tt_sync_block_discard); -NTAPI_UFN(tt_sync_block_lock); -NTAPI_UFN(tt_sync_block_unlock); -NTAPI_UFN(tt_sync_block_server_lock); /* nt_port.h */ NTAPI_UFN(tt_port_guid_from_type); diff --git a/src/string/ntapi_tt_aligned_block_memlock.c b/src/string/ntapi_tt_aligned_block_memlock.c index 17fb078..3177759 100644 --- a/src/string/ntapi_tt_aligned_block_memlock.c +++ b/src/string/ntapi_tt_aligned_block_memlock.c @@ -9,15 +9,17 @@ #include #include +#define __CPU_CACHE_SIZE 64 + __attr_protected__ uintptr_t * __cdecl __ntapi_tt_aligned_block_memlock( __in uintptr_t * dst, __in size_t bytes) { - size_t addr = (size_t)dst & ~(size_t)(NT_SYNC_BLOCK_SIZE); + size_t addr = (size_t)dst & ~(size_t)(__CPU_CACHE_SIZE); size_t cap = addr + bytes; - for (; addr -#include -#include -#include -#include -#include "ntapi_impl.h" - -static void __sync_block_memset( - __in nt_sync_block * sync_block, - __in intptr_t value) -{ - intptr_t * sptr = sync_block->cache_line; - - at_store(&sptr[0x0],value); - at_store(&sptr[0x1],value); - at_store(&sptr[0x2],value); - at_store(&sptr[0x3],value); - at_store(&sptr[0x4],value); - at_store(&sptr[0x5],value); - at_store(&sptr[0x6],value); - at_store(&sptr[0x7],value); - - if (sizeof(intptr_t) == 4) { - at_store(&sptr[0x8],value); - at_store(&sptr[0x9],value); - at_store(&sptr[0xa],value); - at_store(&sptr[0xb],value); - at_store(&sptr[0xc],value); - at_store(&sptr[0xd],value); - at_store(&sptr[0xe],value); - at_store(&sptr[0xf],value); - } -} - -void __stdcall __ntapi_tt_sync_block_init( - __in nt_sync_block * sync_block, - __in uint32_t flags __optional, - __in int32_t srvtid __optional, - __in int32_t default_lock_tries __optional, - __in int64_t default_lock_wait __optional, - __in void * hsignal __optional) -{ - __sync_block_memset( - sync_block,0); - - at_store_32( - &sync_block->lock_tries, - default_lock_tries - ? default_lock_tries - : __NT_SYNC_BLOCK_LOCK_TRIES); - - at_store_64( - &sync_block->lock_wait.quad, - default_lock_wait - ? default_lock_wait - : (-1)); - - at_store_32( - (int32_t *)&sync_block->flags, - flags); - - at_store_32( - (int32_t *)&sync_block->srvtid, - srvtid); - - at_store( - (intptr_t *)&sync_block->hsignal, - (intptr_t)hsignal); -} - - -int32_t __stdcall __ntapi_tt_sync_block_lock( - __in nt_sync_block * sync_block, - __in int32_t lock_tries __optional, - __in int64_t lock_wait __optional, - __in uint32_t * sig_flag __optional) -{ - int32_t status; - int32_t tid; - intptr_t lock; - void * hwait[2]; - nt_timeout timeout; - - /* validation */ - if (sync_block->invalid) - return NT_STATUS_INVALID_HANDLE; - - /* already owned? */ - if ((tid = pe_get_current_thread_id()) == sync_block->tid) - return NT_STATUS_SUCCESS; - - /* yield to server? */ - if ((sync_block->flags & NT_SYNC_BLOCK_YIELD_TO_SERVER) - && ((uint32_t)tid != sync_block->srvtid)) { - hwait[0] = sync_block->hserver; - hwait[1] = sync_block->hsignal; - - /* signal support */ - if (sig_flag && *sig_flag) - return NT_STATUS_ALERTED; - - /* wait */ - status = __ntapi->zw_wait_for_multiple_objects( - 2,hwait, - NT_WAIT_ANY, - NT_SYNC_NON_ALERTABLE, - 0); - - /* signal support */ - if (sig_flag && *sig_flag) - return NT_STATUS_ALERTED; - } - - /* first try */ - lock = at_locked_cas_32( - &sync_block->tid, - 0,tid); - - if (lock && (lock_tries == 1)) - return NT_STATUS_NOT_LOCKED; - - /* first-time contended case? */ - if (lock && !sync_block->hwait) { - if ((status = __ntapi->tt_create_inheritable_event( - &hwait[0], - NT_NOTIFICATION_EVENT, - NT_EVENT_NOT_SIGNALED))) - return status; - - lock = at_locked_cas( - (intptr_t *)&sync_block->hwait, - 0,(intptr_t)hwait[0]); - - if (lock) - __ntapi->zw_close(hwait[0]); - - /* try again without a wait */ - lock = at_locked_cas_32( - &sync_block->tid, - 0,tid); - } - - /* contended case? */ - if (lock) { - hwait[0] = sync_block->hwait; - hwait[1] = sync_block->hsignal; - - lock_tries = lock_tries - ? lock_tries - : sync_block->lock_tries; - - timeout.quad = lock_wait - ? lock_wait - : sync_block->lock_wait.quad; - - for (; lock && lock_tries; lock_tries--) { - /* signal support */ - if (sig_flag && *sig_flag) - return NT_STATUS_ALERTED; - - /* wait */ - status = __ntapi->zw_wait_for_multiple_objects( - 2,hwait, - NT_WAIT_ANY, - NT_SYNC_NON_ALERTABLE, - &timeout); - - /* check status */ - if ((status >= NT_STATUS_WAIT_0) && (status < NT_STATUS_WAIT_CAP)) - (void)0; - else if (status == NT_STATUS_TIMEOUT) - (void)0; - else - return status; - - /* signal support */ - if (sig_flag && *sig_flag) - return NT_STATUS_ALERTED; - - /* try again */ - lock = at_locked_cas_32( - &sync_block->tid, - 0,tid); - }; - } - - if (lock) - return NT_STATUS_NOT_LOCKED; - - /* shared section support */ - at_store_32( - &sync_block->pid, - pe_get_current_process_id()); - - return NT_STATUS_SUCCESS; -} - - -int32_t __stdcall __ntapi_tt_sync_block_server_lock( - __in nt_sync_block * sync_block, - __in int32_t lock_tries __optional, - __in int64_t lock_wait __optional, - __in uint32_t * sig_flag __optional) -{ - int32_t status; - - /* validation */ - if (sync_block->invalid) - return NT_STATUS_INVALID_HANDLE; - - else if (sync_block->srvtid != pe_get_current_thread_id()) - return NT_STATUS_RESOURCE_NOT_OWNED; - - /* try once without yield request */ - status = __ntapi_tt_sync_block_lock( - sync_block, - 1,lock_wait, - sig_flag); - - if (status == NT_STATUS_SUCCESS) - return status; - - /* hserver */ - if (!sync_block->hserver) { - if ((status = __ntapi->tt_create_inheritable_event( - &sync_block->hserver, - NT_NOTIFICATION_EVENT, - NT_EVENT_NOT_SIGNALED))) - return status; - } else { - if ((status = __ntapi->zw_reset_event( - sync_block->hserver,0))) - return status; - } - - /* yield request: set */ - at_locked_or_32( - (int32_t *)&sync_block->flags, - NT_SYNC_BLOCK_YIELD_TO_SERVER); - - /* try again */ - status = __ntapi_tt_sync_block_lock( - sync_block, - lock_tries, - lock_wait, - sig_flag); - - /* yield request: unset */ - at_locked_xor_32( - (int32_t *)&sync_block->flags, - NT_SYNC_BLOCK_YIELD_TO_SERVER); - - __ntapi->zw_set_event( - sync_block->hserver, - 0); - - /* (locking not guaranteed) */ - return status; -} - - -int32_t __stdcall __ntapi_tt_sync_block_unlock( - __in nt_sync_block * sync_block) -{ - union { - int64_t i64; - nt_large_integer nti64; - } cmp; - - if (sync_block->invalid) - return NT_STATUS_INVALID_HANDLE; - - cmp.nti64.ihigh = pe_get_current_process_id(); - cmp.nti64.ulow = pe_get_current_thread_id(); - - if (cmp.i64 != at_locked_cas_64( - (int64_t *)&sync_block->tid, - cmp.i64,0)) - return NT_STATUS_RESOURCE_NOT_OWNED; - - return NT_STATUS_SUCCESS; -} - - -int32_t __stdcall __ntapi_tt_sync_block_discard( - __in nt_sync_block * sync_block) -{ - if (sync_block->hwait) - __ntapi->zw_close( - sync_block->hwait); - - if (sync_block->hserver) - __ntapi->zw_close( - sync_block->hserver); - - __sync_block_memset( - sync_block,-1); - - return NT_STATUS_SUCCESS; -}