From d9a55a5557bb55022d9da2dcaa3bddfdd2a23faa Mon Sep 17 00:00:00 2001 From: Ben Vanik Date: Tue, 22 Oct 2013 21:50:10 -0700 Subject: [PATCH] Zero memory on alloc. --- src/xenia/core/memory.cc | 17 +++++++++++++-- src/xenia/core/memory.h | 1 + src/xenia/cpu/processor.cc | 4 ++-- src/xenia/cpu/thread_state.cc | 3 ++- .../modules/xboxkrnl/objects/xthread.cc | 6 ++++-- src/xenia/kernel/xex2.cc | 21 ++++++++----------- 6 files changed, 33 insertions(+), 19 deletions(-) diff --git a/src/xenia/core/memory.cc b/src/xenia/core/memory.cc index a4aa9d32e..ed6782ba6 100644 --- a/src/xenia/core/memory.cc +++ b/src/xenia/core/memory.cc @@ -293,11 +293,20 @@ uint32_t xe_memory_heap_alloc( // will place wherever asked (so long as it doesn't overlap the heap). if (!base_address) { // Normal allocation from the managed heap. + uint32_t result; if (flags & XE_MEMORY_FLAG_PHYSICAL) { - return memory->physical_heap.Alloc(base_address, size, flags, alignment); + result = memory->physical_heap.Alloc( + base_address, size, flags, alignment); } else { - return memory->virtual_heap.Alloc(base_address, size, flags, alignment); + result = memory->virtual_heap.Alloc( + base_address, size, flags, alignment); } + if (result) { + if (flags & XE_MEMORY_FLAG_ZERO) { + xe_zero_struct(memory->mapping_base + result, size); + } + } + return result; } else { if (base_address >= XE_MEMORY_VIRTUAL_HEAP_LOW && base_address < XE_MEMORY_VIRTUAL_HEAP_HIGH) { @@ -322,6 +331,10 @@ uint32_t xe_memory_heap_alloc( return 0; } + if (flags & XE_MEMORY_FLAG_ZERO) { + xe_zero_struct(pv, size); + } + return base_address; } } diff --git a/src/xenia/core/memory.h b/src/xenia/core/memory.h index 683f665c1..b6b13e2eb 100644 --- a/src/xenia/core/memory.h +++ b/src/xenia/core/memory.h @@ -43,6 +43,7 @@ uint32_t xe_memory_search_aligned(xe_memory_ref memory, size_t start, enum { XE_MEMORY_FLAG_64KB_PAGES = (1 << 1), XE_MEMORY_FLAG_PHYSICAL = (1 << 2), + XE_MEMORY_FLAG_ZERO = (1 << 3), }; enum { diff --git a/src/xenia/cpu/processor.cc b/src/xenia/cpu/processor.cc index e9d71e986..92c07b181 100644 --- a/src/xenia/cpu/processor.cc +++ b/src/xenia/cpu/processor.cc @@ -110,7 +110,7 @@ int Processor::Setup() { interrupt_thread_lock_ = xe_mutex_alloc(10000); interrupt_thread_state_ = AllocThread(16 * 1024, 0, 0); interrupt_thread_block_ = xe_memory_heap_alloc( - memory_, 0, 2048, 0); + memory_, 0, 2048, XE_MEMORY_FLAG_ZERO); interrupt_thread_state_->ppc_state()->r[13] = interrupt_thread_block_; sym_table_ = new SymbolTable(); @@ -146,7 +146,7 @@ int Processor::LoadRawBinary(const xechar_t* path, uint32_t start_address) { // Place the data into memory at the desired address. XEEXPECTNOTZERO(xe_memory_heap_alloc( - memory_, start_address, (uint32_t)length, 0)); + memory_, start_address, (uint32_t)length, XE_MEMORY_FLAG_ZERO)); XEEXPECTZERO(xe_copy_memory( xe_memory_addr(memory_, start_address), length, addr, length)); diff --git a/src/xenia/cpu/thread_state.cc b/src/xenia/cpu/thread_state.cc index 9151ddf74..923c49da1 100644 --- a/src/xenia/cpu/thread_state.cc +++ b/src/xenia/cpu/thread_state.cc @@ -24,7 +24,8 @@ ThreadState::ThreadState( thread_id_(thread_id) { memory_ = processor->memory(); - stack_address_ = xe_memory_heap_alloc(memory_, 0, stack_size, 0); + stack_address_ = xe_memory_heap_alloc( + memory_, 0, stack_size, XE_MEMORY_FLAG_ZERO); // Allocate with 64b alignment. ppc_state_ = (xe_ppc_state_t*)xe_malloc_aligned(sizeof(xe_ppc_state_t)); diff --git a/src/xenia/kernel/modules/xboxkrnl/objects/xthread.cc b/src/xenia/kernel/modules/xboxkrnl/objects/xthread.cc index 56a0b7b12..d6b1ef077 100644 --- a/src/xenia/kernel/modules/xboxkrnl/objects/xthread.cc +++ b/src/xenia/kernel/modules/xboxkrnl/objects/xthread.cc @@ -133,7 +133,8 @@ X_STATUS XThread::Create() { // 0x160: last error // So, at offset 0x100 we have a 4b pointer to offset 200, then have the // structure. - thread_state_address_ = xe_memory_heap_alloc(memory(), 0, 2048, 0); + thread_state_address_ = xe_memory_heap_alloc( + memory(), 0, 2048, XE_MEMORY_FLAG_ZERO); if (!thread_state_address_) { XELOGW("Unable to allocate thread state block"); return X_STATUS_NO_MEMORY; @@ -147,7 +148,8 @@ X_STATUS XThread::Create() { // Allocate TLS block. const xe_xex2_header_t* header = module->xex_header(); uint32_t tls_size = header->tls_info.slot_count * header->tls_info.data_size; - tls_address_ = xe_memory_heap_alloc(memory(), 0, tls_size, 0); + tls_address_ = xe_memory_heap_alloc( + memory(), 0, tls_size, XE_MEMORY_FLAG_ZERO); if (!tls_address_) { XELOGW("Unable to allocate thread local storage block"); module->Release(); diff --git a/src/xenia/kernel/xex2.cc b/src/xenia/kernel/xex2.cc index 1f36d1328..a221c9c7e 100644 --- a/src/xenia/kernel/xex2.cc +++ b/src/xenia/kernel/xex2.cc @@ -533,10 +533,9 @@ int xe_xex2_read_image_uncompressed(const xe_xex2_header_t *header, // Allocate in-place the XEX memory. const size_t exe_length = xex_length - header->exe_offset; size_t uncompressed_size = exe_length; - uint32_t alloc_result = - xe_memory_heap_alloc(memory, - header->exe_address, (uint32_t)uncompressed_size, - 0); + uint32_t alloc_result = xe_memory_heap_alloc( + memory, header->exe_address, (uint32_t)uncompressed_size, + XE_MEMORY_FLAG_ZERO); if (!alloc_result) { XELOGE("Unable to allocate XEX memory at %.8X-%.8X.", header->exe_address, uncompressed_size); @@ -580,10 +579,9 @@ int xe_xex2_read_image_basic_compressed(const xe_xex2_header_t *header, } // Allocate in-place the XEX memory. - uint32_t alloc_result = - xe_memory_heap_alloc(memory, - header->exe_address, (uint32_t)uncompressed_size, - 0); + uint32_t alloc_result = xe_memory_heap_alloc( + memory, header->exe_address, (uint32_t)uncompressed_size, + XE_MEMORY_FLAG_ZERO); if (!alloc_result) { XELOGE("Unable to allocate XEX memory at %.8X-%.8X.", header->exe_address, uncompressed_size); @@ -719,10 +717,9 @@ int xe_xex2_read_image_compressed(const xe_xex2_header_t *header, } // Allocate in-place the XEX memory. - uint32_t alloc_result = - xe_memory_heap_alloc(memory, - header->exe_address, (uint32_t)uncompressed_size, - 0); + uint32_t alloc_result = xe_memory_heap_alloc( + memory, header->exe_address, (uint32_t)uncompressed_size, + XE_MEMORY_FLAG_ZERO); if (!alloc_result) { XELOGE("Unable to allocate XEX memory at %.8X-%.8X.", header->exe_address, uncompressed_size);