# Typed runtime service registry.
#
# Data only. `cargo run -p xenora-import-gen --bin xenora-runtime-gen` validates
# it and writes crates/loader/src/runtime_table.rs (typed Win64 adapters and
# static metadata) and docs/runtime/services.md (coverage counts).
#
#   service <library> <symbol>(<name>: <type>, ...) -> <type>
#       handler <Rust function in crates/loader/src/services.rs or crt.rs>
#       status  implemented | partial | stub
#       alias   <documented API set that forwards this symbol>
#       fails   <failure contract>
#       ref     <public header or document>
#       test    <test function in crates/loader/tests/runtime_services.rs>
#
# Types: u32 i32 u64 bool32 handle ptr out<N> f32 f64, and void as a result.
# A bool32 argument is true when nonzero. out<N> is a guest pointer to N bytes
# the service writes. Arguments are assigned by position (Microsoft x64 calling
# convention): RCX, RDX, R8, R9 or XMM0-XMM3 for floats, then the stack.
#
# "status" describes behaviour against the documented contract. A service needs
# at least one test before it may be implemented or partial.

service kernel32.dll SetProcessPriorityBoost(process: handle, disable: bool32) -> bool32
    handler set_process_priority_boost
    status partial
    alias api-ms-win-core-processthreads-l1-1-2.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref processthreadsapi.h SetProcessPriorityBoost
    test priority_boost_round_trips_and_checks_its_handle

service kernel32.dll GetProcessPriorityBoost(process: handle, disabled: out<4>) -> bool32
    handler get_process_priority_boost
    status partial
    alias api-ms-win-core-processthreads-l1-1-2.dll
    fails 0 with ERROR_INVALID_HANDLE or ERROR_NOACCESS
    ref processthreadsapi.h GetProcessPriorityBoost
    test priority_boost_round_trips_and_checks_its_handle

service kernel32.dll GetCurrentProcess() -> handle
    handler get_current_process
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentProcess
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetProcessId(process: handle) -> u32
    handler get_process_id
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref processthreadsapi.h GetProcessId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentThread() -> handle
    handler get_current_thread
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentThread
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentProcessId() -> u32
    handler get_current_process_id
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentProcessId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll GetCurrentThreadId() -> u32
    handler get_current_thread_id
    status implemented
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails never
    ref processthreadsapi.h GetCurrentThreadId
    test identity_services_return_documented_pseudo_handles_and_ids

service kernel32.dll OpenThread(access: u32, inherit: bool32, thread: u32) -> handle
    handler open_thread
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED or ERROR_NOT_ENOUGH_MEMORY
    ref processthreadsapi.h OpenThread
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll GetThreadPriority(thread: handle) -> i32
    handler get_thread_priority
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails THREAD_PRIORITY_ERROR_RETURN with ERROR_INVALID_HANDLE or ERROR_ACCESS_DENIED
    ref processthreadsapi.h GetThreadPriority
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll SetThreadPriority(thread: handle, priority: i32) -> bool32
    handler set_thread_priority
    status partial
    alias api-ms-win-core-processthreads-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE, ERROR_ACCESS_DENIED, ERROR_NOT_SUPPORTED or ERROR_INVALID_PARAMETER
    ref processthreadsapi.h SetThreadPriority
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll CloseHandle(object: handle) -> bool32
    handler close_handle
    status partial
    alias api-ms-win-core-handle-l1-1-0.dll
    fails 0 with ERROR_INVALID_HANDLE
    ref handleapi.h CloseHandle
    test thread_handles_check_access_and_are_rejected_once_closed

service kernel32.dll GetSystemTimeAsFileTime(time: out<8>) -> void
    handler get_system_time_as_file_time
    status implemented
    alias api-ms-win-core-sysinfo-l1-1-0.dll
    fails guest fault on an invalid pointer
    ref sysinfoapi.h GetSystemTimeAsFileTime
    test system_time_comes_from_the_injected_clock

# Standard streams and formatted output for a hosted C program. Output to
# stdout and stderr is captured; any other stream, input, floating-point and
# wide conversions stop the guest with the runtime-limit fault.

service ucrtbase.dll __acrt_iob_func(index: u32) -> ptr
    handler standard_stream
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest for an index above 2 or when the streams cannot be allocated
    ref corecrt_wstdio.h __acrt_iob_func
    test standard_streams_are_three_stable_file_objects

service ucrtbase.dll __stdio_common_vfprintf(options: u64, stream: ptr, format: ptr, locale: ptr, arguments: ptr) -> i32
    handler vfprintf
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails -1 with errno EINVAL for a null stream or format or an invalid format; stops the guest for what is not modelled
    ref corecrt_stdio_config.h __stdio_common_vfprintf; Microsoft Learn, vfprintf and format specification syntax
    test formatted_output_reaches_the_captured_standard_streams

service ucrtbase.dll fwrite(buffer: ptr, size: u64, count: u64, stream: ptr) -> u64
    handler fwrite
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails 0 with errno EINVAL for a null buffer or stream; stops the guest for a stream other than stdout or stderr
    ref stdio.h fwrite
    test fwrite_and_fputs_write_to_the_captured_standard_streams

service ucrtbase.dll fputs(text: ptr, stream: ptr) -> i32
    handler fputs
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails EOF with errno EINVAL for a null string or stream; stops the guest for a stream other than stdout or stderr
    ref stdio.h fputs
    test fwrite_and_fputs_write_to_the_captured_standard_streams

service ucrtbase.dll fflush(stream: ptr) -> i32
    handler fflush
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails stops the guest for a stream that is not a standard stream
    ref stdio.h fflush
    test fflush_and_setvbuf_return_their_documented_results

service ucrtbase.dll setvbuf(stream: ptr, buffer: ptr, mode: i32, size: u64) -> i32
    handler setvbuf
    status partial
    alias api-ms-win-crt-stdio-l1-1-0.dll
    fails -1 with errno EINVAL for a null stream, an unknown mode or a size outside 2 to INT_MAX; stops the guest for a stream that is not a standard stream
    ref stdio.h setvbuf
    test fflush_and_setvbuf_return_their_documented_results

service kernel32.dll GetLastError() -> u32
    handler get_last_error
    status partial
    alias api-ms-win-core-errorhandling-l1-1-0.dll
    fails never
    ref errhandlingapi.h GetLastError
    test last_error_is_set_by_failures_and_by_the_guest

service kernel32.dll SetLastError(code: u32) -> void
    handler set_last_error
    status partial
    alias api-ms-win-core-errorhandling-l1-1-0.dll
    fails never
    ref errhandlingapi.h SetLastError
    test last_error_is_set_by_failures_and_by_the_guest

service kernel32.dll VirtualAlloc(address: ptr, size: u64, kind: u32, protect: u32) -> ptr
    handler virtual_alloc
    status partial
    alias api-ms-win-core-memory-l1-1-0.dll
    fails 0 with ERROR_INVALID_PARAMETER, ERROR_NOT_SUPPORTED, ERROR_INVALID_ADDRESS or ERROR_NOT_ENOUGH_MEMORY
    ref memoryapi.h VirtualAlloc
    test virtual_memory_is_mapped_released_and_bounded

service kernel32.dll VirtualFree(address: ptr, size: u64, kind: u32) -> bool32
    handler virtual_free
    status partial
    alias api-ms-win-core-memory-l1-1-0.dll
    fails 0 with ERROR_NOT_SUPPORTED, ERROR_INVALID_PARAMETER or ERROR_INVALID_ADDRESS
    ref memoryapi.h VirtualFree
    test virtual_memory_is_mapped_released_and_bounded

service ucrtbase.dll sqrt(x: f64) -> f64
    handler sqrt
    status partial
    alias api-ms-win-crt-math-l1-1-0.dll
    fails NaN for a negative argument; errno is not set
    ref math.h sqrt (ISO C, IEC 60559 squareRoot)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll sqrtf(x: f32) -> f32
    handler sqrtf
    status partial
    alias api-ms-win-crt-math-l1-1-0.dll
    fails NaN for a negative argument; errno is not set
    ref math.h sqrtf (ISO C, IEC 60559 squareRoot)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll fabs(x: f64) -> f64
    handler fabs
    status implemented
    alias api-ms-win-crt-math-l1-1-0.dll
    fails never
    ref math.h fabs (ISO C, IEC 60559 abs)
    test floating_arguments_and_results_use_xmm_registers

service ucrtbase.dll fabsf(x: f32) -> f32
    handler fabsf
    status implemented
    alias api-ms-win-crt-math-l1-1-0.dll
    fails never
    ref math.h fabsf (ISO C, IEC 60559 abs)
    test floating_arguments_and_results_use_xmm_registers
