Converting some exports to the new way.

This commit is contained in:
Ben Vanik
2015-06-01 20:56:57 -07:00
parent f634edb9eb
commit a91a754b24
17 changed files with 857 additions and 1325 deletions

View File

@@ -17,210 +17,15 @@
namespace xe {
namespace kernel {
#define SHIM_GPR_32(n) (uint32_t)(ppc_context->r[n])
// TODO: clean me up!
SHIM_CALL DbgPrint_shim(PPCContext* ppc_context, KernelState* kernel_state) {
uint32_t format_ptr = SHIM_GET_ARG_32(0);
if (format_ptr == 0) {
SHIM_SET_RETURN_32(-1);
return;
}
const char* format = (const char*)SHIM_MEM_ADDR(format_ptr);
int arg_index = 0;
char buffer[512]; // TODO: ensure it never writes past the end of the
// buffer...
char* b = buffer;
for (; *format != '\0'; ++format) {
const char* start = format;
if (*format != '%') {
*b++ = *format;
continue;
}
++format;
if (*format == '\0') {
break;
}
if (*format == '%') {
*b++ = *format;
continue;
}
const char* end;
end = format;
// skip flags
while (*end == '-' || *end == '+' || *end == ' ' || *end == '#' ||
*end == '0') {
++end;
}
if (*end == '\0') {
break;
}
int arg_extras = 0;
// skip width
if (*end == '*') {
++end;
arg_extras++;
} else {
while (*end >= '0' && *end <= '9') {
++end;
}
}
if (*end == '\0') {
break;
}
// skip precision
if (*end == '.') {
++end;
if (*end == '*') {
++end;
++arg_extras;
} else {
while (*end >= '0' && *end <= '9') {
++end;
}
}
}
if (*end == '\0') {
break;
}
// get length
int arg_size = 4;
if (*end == 'h') {
++end;
arg_size = 4;
if (*end == 'h') {
++end;
}
} else if (*end == 'l') {
++end;
arg_size = 4;
if (*end == 'l') {
++end;
arg_size = 8;
}
} else if (*end == 'j') {
arg_size = 8;
++end;
} else if (*end == 'z') {
arg_size = 4;
++end;
} else if (*end == 't') {
arg_size = 8;
++end;
} else if (*end == 'L') {
arg_size = 8;
++end;
}
if (*end == '\0') {
break;
}
if (*end == 'd' || *end == 'i' || *end == 'u' || *end == 'o' ||
*end == 'x' || *end == 'X' || *end == 'f' || *end == 'F' ||
*end == 'e' || *end == 'E' || *end == 'g' || *end == 'G' ||
*end == 'a' || *end == 'A' || *end == 'c') {
char local[512];
local[0] = '\0';
strncat(local, start, end + 1 - start);
assert_true(arg_size == 8 || arg_size == 4);
if (arg_size == 8) {
if (arg_extras == 0) {
uint64_t value =
arg_index < 7
? SHIM_GET_ARG_64(1 + arg_index)
: SHIM_MEM_32(SHIM_GPR_32(1) + 16 + ((1 + arg_index) * 8));
int result = sprintf(b, local, value);
b += result;
arg_index++;
} else {
assert_true(false);
}
} else if (arg_size == 4) {
if (arg_extras == 0) {
uint64_t value =
arg_index < 7
? SHIM_GET_ARG_64(1 + arg_index)
: SHIM_MEM_32(SHIM_GPR_32(1) + 16 + ((1 + arg_index) * 8));
int result = sprintf(b, local, (uint32_t)value);
b += result;
arg_index++;
} else {
assert_true(false);
}
}
} else if (*end == 'n') {
assert_true(arg_size == 4);
if (arg_extras == 0) {
uint32_t value = arg_index < 7
? SHIM_GET_ARG_32(1 + arg_index)
: (uint32_t)SHIM_MEM_64(SHIM_GPR_32(1) + 16 +
((1 + arg_index) * 8));
SHIM_SET_MEM_32(value, (uint32_t)((b - buffer) / sizeof(char)));
arg_index++;
} else {
assert_true(false);
}
} else if (*end == 's' || *end == 'p') {
char local[512];
local[0] = '\0';
strncat(local, start, end + 1 - start);
assert_true(arg_size == 4);
if (arg_extras == 0) {
uint32_t value = arg_index < 7
? SHIM_GET_ARG_32(1 + arg_index)
: (uint32_t)SHIM_MEM_64(SHIM_GPR_32(1) + 16 +
((1 + arg_index) * 8));
const void* pointer = (const void*)SHIM_MEM_ADDR(value);
int result = sprintf(b, local, pointer);
b += result;
arg_index++;
} else {
assert_true(false);
}
} else {
assert_true(false);
break;
}
format = end;
}
*b++ = '\0';
XELOGD("(DbgPrint) %s", buffer);
}
SHIM_CALL DbgBreakPoint_shim(PPCContext* ppc_context,
KernelState* kernel_state) {
XELOGD("DbgBreakPoint()");
DebugBreak();
}
void DbgBreakPoint() { DebugBreak(); }
DECLARE_XBOXKRNL_EXPORT(DbgBreakPoint, ExportTag::kImportant);
// https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
typedef struct {
xe::be<uint32_t> type;
xe::be<uint32_t> name_ptr;
xe::be<uint32_t> thread_id;
xe::be<uint32_t> flags;
xe::be<uint32_t> type;
xe::be<uint32_t> name_ptr;
xe::be<uint32_t> thread_id;
xe::be<uint32_t> flags;
} X_THREADNAME_INFO;
static_assert_size(X_THREADNAME_INFO, 0x10);
@@ -234,24 +39,24 @@ typedef struct {
xe::be<uint32_t> exception_information[15];
} X_EXCEPTION_RECORD;
static_assert_size(X_EXCEPTION_RECORD, 0x50);
void AppendParam(StringBuffer& string_buffer,
pointer_t<X_EXCEPTION_RECORD> record) {
string_buffer.AppendFormat("%.8X(%.8X)", record.guest_address(),
record->exception_code);
}
SHIM_CALL RtlRaiseException_shim(PPCContext* ppc_context,
KernelState* kernel_state) {
uint32_t record_ptr = SHIM_GET_ARG_32(0);
auto record = reinterpret_cast<X_EXCEPTION_RECORD*>(SHIM_MEM_ADDR(record_ptr));
XELOGD("RtlRaiseException(%.8X(%.8X))", record_ptr, record->exception_code);
void RtlRaiseException(pointer_t<X_EXCEPTION_RECORD> record) {
if (record->exception_code == 0x406D1388) {
// SetThreadName. FFS.
// https://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
// TODO: check record->number_parameters to make sure it's a correct size
auto thread_info = reinterpret_cast<X_THREADNAME_INFO*>(&record->exception_information[0]);
auto thread_info =
reinterpret_cast<X_THREADNAME_INFO*>(&record->exception_information[0]);
assert_true(thread_info->type == 0x1000);
const char* name = (const char*)SHIM_MEM_ADDR(thread_info->name_ptr);
auto name =
kernel_memory()->TranslateVirtual<const char*>(thread_info->name_ptr);
object_ref<XThread> thread;
if (thread_info->thread_id == -1) {
@@ -259,7 +64,7 @@ SHIM_CALL RtlRaiseException_shim(PPCContext* ppc_context,
thread = retain_object(XThread::GetCurrentThread());
} else {
// Lookup thread by ID.
thread = kernel_state->GetThreadByID(thread_info->thread_id);
thread = kernel_state()->GetThreadByID(thread_info->thread_id);
}
if (thread) {
@@ -282,39 +87,20 @@ SHIM_CALL RtlRaiseException_shim(PPCContext* ppc_context,
// This is going to suck.
DebugBreak();
}
DECLARE_XBOXKRNL_EXPORT(RtlRaiseException, ExportTag::kImportant);
void xeKeBugCheckEx(uint32_t code, uint32_t param1, uint32_t param2,
uint32_t param3, uint32_t param4) {
void KeBugCheckEx(dword_t code, dword_t param1, dword_t param2, dword_t param3,
dword_t param4) {
XELOGD("*** STOP: 0x%.8X (0x%.8X, 0x%.8X, 0x%.8X, 0x%.8X)", code, param1,
param2, param3, param4);
fflush(stdout);
DebugBreak();
assert_always();
}
DECLARE_XBOXKRNL_EXPORT(KeBugCheckEx, ExportTag::kImportant);
SHIM_CALL KeBugCheck_shim(PPCContext* ppc_context, KernelState* kernel_state) {
uint32_t code = SHIM_GET_ARG_32(0);
xeKeBugCheckEx(code, 0, 0, 0, 0);
}
SHIM_CALL KeBugCheckEx_shim(PPCContext* ppc_context,
KernelState* kernel_state) {
uint32_t code = SHIM_GET_ARG_32(0);
uint32_t param1 = SHIM_GET_ARG_32(1);
uint32_t param2 = SHIM_GET_ARG_32(2);
uint32_t param3 = SHIM_GET_ARG_32(3);
uint32_t param4 = SHIM_GET_ARG_32(4);
xeKeBugCheckEx(code, param1, param2, param3, param4);
}
void KeBugCheck(dword_t code) { KeBugCheckEx(code, 0, 0, 0, 0); }
DECLARE_XBOXKRNL_EXPORT(KeBugCheck, ExportTag::kImportant);
} // namespace kernel
} // namespace xe
void xe::kernel::xboxkrnl::RegisterDebugExports(
xe::cpu::ExportResolver* export_resolver, KernelState* kernel_state) {
SHIM_SET_MAPPING("xboxkrnl.exe", DbgPrint, state);
SHIM_SET_MAPPING("xboxkrnl.exe", DbgBreakPoint, state);
SHIM_SET_MAPPING("xboxkrnl.exe", RtlRaiseException, state);
SHIM_SET_MAPPING("xboxkrnl.exe", KeBugCheck, state);
SHIM_SET_MAPPING("xboxkrnl.exe", KeBugCheckEx, state);
}