Skeleton code for more kernel calls.

This commit is contained in:
Ben Vanik 2013-01-28 13:28:22 -08:00
parent 6c4af5aa70
commit e93908dd72
9 changed files with 248 additions and 28 deletions

View File

@ -5,5 +5,7 @@
'xboxkrnl_hal.cc',
'xboxkrnl_memory.cc',
'xboxkrnl_module.cc',
'xboxkrnl_rtl.cc',
'xboxkrnl_threading.cc',
],
}

View File

@ -0,0 +1,76 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_KERNEL_MODULES_XBOXKRNL_H_
#define XENIA_KERNEL_MODULES_XBOXKRNL_H_
#include <xenia/common.h>
#include <xenia/core.h>
namespace xe {
namespace kernel {
namespace xboxkrnl {
// NT_STATUS (STATUS_*)
// http://msdn.microsoft.com/en-us/library/cc704588.aspx
// Adding as needed.
#define X_STAUTS_SUCCESS ((uint32_t)0x00000000L)
#define X_STATUS_UNSUCCESSFUL ((uint32_t)0xC0000001L)
#define X_STATUS_NOT_IMPLEMENTED ((uint32_t)0xC0000002L)
#define X_STATUS_ACCESS_VIOLATION ((uint32_t)0xC0000005L)
#define X_STATUS_INVALID_HANDLE ((uint32_t)0xC0000008L)
#define X_STATUS_INVALID_PARAMETER ((uint32_t)0xC000000DL)
#define X_STATUS_NO_MEMORY ((uint32_t)0xC0000017L)
#define X_STATUS_ALREADY_COMMITTED ((uint32_t)0xC0000021L)
#define X_STATUS_ACCESS_DENIED ((uint32_t)0xC0000022L)
#define X_STATUS_BUFFER_TOO_SMALL ((uint32_t)0xC0000023L)
#define X_STATUS_OBJECT_TYPE_MISMATCH ((uint32_t)0xC0000024L)
#define X_STATUS_INVALID_PAGE_PROTECTION ((uint32_t)0xC0000045L)
// MEM_*, used by NtAllocateVirtualMemory
#define X_MEM_COMMIT 0x00001000
#define X_MEM_RESERVE 0x00002000
#define X_MEM_DECOMMIT 0x00004000
#define X_MEM_RELEASE 0x00008000
#define X_MEM_FREE 0x00010000
#define X_MEM_PRIVATE 0x00020000
#define X_MEM_RESET 0x00080000
#define X_MEM_TOP_DOWN 0x00100000
#define X_MEM_NOZERO 0x00800000
#define X_MEM_LARGE_PAGES 0x20000000
#define X_MEM_HEAP 0x40000000
#define X_MEM_16MB_PAGES 0x80000000 // from Valve SDK
// PAGE_*, used by NtAllocateVirtualMemory
#define X_PAGE_NOACCESS 0x00000001
#define X_PAGE_READONLY 0x00000002
#define X_PAGE_READWRITE 0x00000004
#define X_PAGE_WRITECOPY 0x00000008
// *_EXECUTE_* bits omitted, as user code can't mark pages as executable.
#define X_PAGE_GUARD 0x00000100
#define X_PAGE_NOCACHE 0x00000200
#define X_PAGE_WRITECOMBINE 0x00000400
// (?), used by KeGetCurrentProcessType
#define X_PROCTYPE_IDLE 0
#define X_PROCTYPE_USER 1
#define X_PROCTYPE_SYSTEM 2
} // namespace xboxkrnl
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_MODULES_XBOXKRNL_H_

View File

@ -10,6 +10,7 @@
#include "kernel/modules/xboxkrnl/xboxkrnl_hal.h"
#include "kernel/shim_utils.h"
#include "kernel/modules/xboxkrnl/xboxkrnl.h"
using namespace xe;

View File

@ -10,6 +10,7 @@
#include "kernel/modules/xboxkrnl/xboxkrnl_memory.h"
#include "kernel/shim_utils.h"
#include "kernel/modules/xboxkrnl/xboxkrnl.h"
using namespace xe;
@ -34,34 +35,29 @@ void NtAllocateVirtualMemory_shim(
uint32_t base_addr_value = SHIM_MEM_32(base_addr_ptr);
uint32_t region_size_ptr = SHIM_GET_ARG_32(1);
uint32_t region_size_value = SHIM_MEM_32(region_size_ptr);
// MEM_COMMIT | MEM_PHYSICAL | MEM_RESERVE | MEM_RESET | MEM_TOP_DOWN
// X_MEM_*
uint32_t allocation_type = SHIM_GET_ARG_32(2);
// PAGE_NOACCESS | PAGE_READONLY | PAGE_READWRITE | PAGE_EXECUTE |
// PAGE_EXECUTE_READ | PAGE_EXECUTE_READWRITE | PAGE_GUARD | PAGE_NOCACHE |
// PAGE_WRITECOMBINE
// X_PAGE_*
uint32_t protect_bits = SHIM_GET_ARG_32(3);
uint32_t unknown = SHIM_GET_ARG_32(4);
XELOGD(
XT("NtAllocateVirtualMemory(%.8X(%.8X), %.8X(%.8X), %.8X, %.8X, %.8X)"),
base_addr_ptr, base_addr_value,
region_size_ptr, region_size_value,
allocation_type, protect_bits, unknown);
XT("NtAllocateVirtualMemory(%.8X(%.8X), %.8X(%.8X), %.8X, %.8X, %.8X)"),
base_addr_ptr, base_addr_value,
region_size_ptr, region_size_value,
allocation_type, protect_bits, unknown);
// TODO(benvanik): alloc memory
// Possible return codes:
// STATUS_ACCESS_DENIED
// STATUS_ALREADY_COMMITTED
// STATUS_COMMITMENT_LIMIT
// STATUS_CONFLICTING_ADDRESSES
// STATUS_INSUFFICIENT_RESOURCES
// STATUS_INVALID_HANDLE
// STATUS_INVALID_PAGE_PROTECTION
// STATUS_NO_MEMORY
// STATUS_OBJECT_TYPE_MISMATCH
// STATUS_PROCESS_IS_TERMINATING
SHIM_SET_RETURN(0xC0000017);
// X_STATUS_UNSUCCESSFUL
// X_STATUS_INVALID_PAGE_PROTECTION
// X_STATUS_ACCESS_DENIED
// X_STATUS_ALREADY_COMMITTED
// X_STATUS_INVALID_HANDLE
// X_STATUS_INVALID_PAGE_PROTECTION
// X_STATUS_NO_MEMORY
SHIM_SET_RETURN(X_STATUS_UNSUCCESSFUL);
}
void NtFreeVirtualMemory_shim(
@ -76,23 +72,23 @@ void NtFreeVirtualMemory_shim(
uint32_t base_addr_value = SHIM_MEM_32(base_addr_ptr);
uint32_t region_size_ptr = SHIM_GET_ARG_32(1);
uint32_t region_size_value = SHIM_MEM_32(region_size_ptr);
// MEM_DECOMMIT | MEM_RELEASE
// X_MEM_DECOMMIT | X_MEM_RELEASE
uint32_t free_type = SHIM_GET_ARG_32(2);
uint32_t unknown = SHIM_GET_ARG_32(3);
XELOGD(
XT("NtFreeVirtualMemory(%.8X(%.8X), %.8X(%.8X), %.8X, %.8X)"),
base_addr_ptr, base_addr_value,
region_size_ptr, region_size_value,
free_type, unknown);
XT("NtFreeVirtualMemory(%.8X(%.8X), %.8X(%.8X), %.8X, %.8X)"),
base_addr_ptr, base_addr_value,
region_size_ptr, region_size_value,
free_type, unknown);
// TODO(benvanik): free memory
// Possible return codes:
// STATUS_ACCESS_DENIED
// STATUS_INVALID_HANDLE
// STATUS_OBJECT_TYPE_MISMATCH
SHIM_SET_RETURN(0xFFFFFFFF);
// X_STATUS_UNSUCCESSFUL
// X_STATUS_ACCESS_DENIED
// X_STATUS_INVALID_HANDLE
SHIM_SET_RETURN(X_STATUS_UNSUCCESSFUL);
}

View File

@ -12,6 +12,9 @@
#include "kernel/modules/xboxkrnl/kernel_state.h"
#include "kernel/modules/xboxkrnl/xboxkrnl_hal.h"
#include "kernel/modules/xboxkrnl/xboxkrnl_memory.h"
#include "kernel/modules/xboxkrnl/xboxkrnl_rtl.h"
#include "kernel/modules/xboxkrnl/xboxkrnl_threading.h"
#include "kernel/modules/xboxkrnl/xboxkrnl_table.h"
@ -58,6 +61,8 @@ XboxkrnlModule::XboxkrnlModule(xe_pal_ref pal, xe_memory_ref memory,
// Register all exported functions.
RegisterHalExports(resolver.get(), kernel_state.get());
RegisterMemoryExports(resolver.get(), kernel_state.get());
RegisterRtlExports(resolver.get(), kernel_state.get());
RegisterThreadingExports(resolver.get(), kernel_state.get());
// TODO(benvanik): alloc heap memory somewhere in user space
// TODO(benvanik): tools for reading/writing to heap memory

View File

@ -0,0 +1,34 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "kernel/modules/xboxkrnl/xboxkrnl_rtl.h"
#include "kernel/shim_utils.h"
#include "kernel/modules/xboxkrnl/xboxkrnl.h"
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::xboxkrnl;
namespace {
}
void xe::kernel::xboxkrnl::RegisterRtlExports(
ExportResolver* export_resolver, KernelState* state) {
#define SHIM_SET_MAPPING(ordinal, shim, impl) \
export_resolver->SetFunctionMapping("xboxkrnl.exe", ordinal, \
state, (xe_kernel_export_shim_fn)shim, (xe_kernel_export_impl_fn)impl)
#undef SET_MAPPING
}

View File

@ -0,0 +1,29 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_KERNEL_MODULES_XBOXKRNL_RTL_H_
#define XENIA_KERNEL_MODULES_XBOXKRNL_RTL_H_
#include "kernel/modules/xboxkrnl/kernel_state.h"
namespace xe {
namespace kernel {
namespace xboxkrnl {
void RegisterRtlExports(ExportResolver* export_resolver, KernelState* state);
} // namespace xboxkrnl
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_MODULES_XBOXKRNL_RTL_H_

View File

@ -0,0 +1,47 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "kernel/modules/xboxkrnl/xboxkrnl_threading.h"
#include "kernel/shim_utils.h"
#include "kernel/modules/xboxkrnl/xboxkrnl.h"
using namespace xe;
using namespace xe::kernel;
using namespace xe::kernel::xboxkrnl;
namespace {
void KeGetCurrentProcessType_shim(
xe_ppc_state_t* ppc_state, KernelState* state) {
// DWORD
XELOGD(
XT("KeGetCurrentProcessType()"));
SHIM_SET_RETURN(X_PROCTYPE_USER);
}
}
void xe::kernel::xboxkrnl::RegisterThreadingExports(
ExportResolver* export_resolver, KernelState* state) {
#define SHIM_SET_MAPPING(ordinal, shim, impl) \
export_resolver->SetFunctionMapping("xboxkrnl.exe", ordinal, \
state, (xe_kernel_export_shim_fn)shim, (xe_kernel_export_impl_fn)impl)
SHIM_SET_MAPPING(0x00000066, KeGetCurrentProcessType_shim, NULL);
#undef SET_MAPPING
}

View File

@ -0,0 +1,30 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2013 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_KERNEL_MODULES_XBOXKRNL_THREADING_H_
#define XENIA_KERNEL_MODULES_XBOXKRNL_THREADING_H_
#include "kernel/modules/xboxkrnl/kernel_state.h"
namespace xe {
namespace kernel {
namespace xboxkrnl {
void RegisterThreadingExports(ExportResolver* export_resolver,
KernelState* state);
} // namespace xboxkrnl
} // namespace kernel
} // namespace xe
#endif // XENIA_KERNEL_MODULES_XBOXKRNL_THREADING_H_