/*---------------------------------------------------------------------------* Project: Horizon File: os_StackMemory.cpp Copyright (C)2009 Nintendo Co., Ltd. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Rev: 29304 $ *---------------------------------------------------------------------------*/ #include #if NN_PLATFORM_HAS_MMU #include #include #include #include #include #include #include #include #include #include #include #include "os_AddressSpaceManager.h" //--------------------------------------------------------------------------- using namespace nn; using namespace nn::svc; namespace nn{ namespace os{ namespace { nnosAddressSpaceManager s_SpaceManager; } namespace detail { void InitializeStackMemory() { nnosAddressSpaceManagerInitialize(&s_SpaceManager, NN_OS_ADDR_STACK_BEGIN, NN_OS_ADDR_STACK_SIZE); } } void StackMemory::Initialize(void* pMem, size_t size) { // 未初期化であることをチェックします。 NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( pMem, NN_OS_MEMORY_PAGE_SIZE ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); // 仮想アドレス空間からメモリを取得します。 uptr addr = nnosAddressSpaceManagerAllocate(&s_SpaceManager, detail::ConvertToC(this), size, NN_OS_MEMORY_PAGE_SIZE); if (addr == NULL) { NN_TPANIC_("failed to allocate address space."); } m_MemoryAddress = reinterpret_cast(pMem); Result result; uptr dummy; result = nn::svc::ControlMemory( &dummy, addr, m_MemoryAddress, size, nn::os::MEMORY_OPERATION_MAP, nn::os::MEMORY_PERMISSION_READ_WRITE ); NN_ERR_THROW_FATAL(result); result = nn::svc::ControlMemory( &dummy, m_MemoryAddress, NULL, size, nn::os::MEMORY_OPERATION_PROTECT, nn::os::MEMORY_PERMISSION_NONE ); NN_ERR_THROW_FATAL(result); } void* StackMemory::Finalize() { if ( GetAddress() != NULL ) { const uptr addr = GetAddress(); const size_t size = GetSize(); const uptr memAddr = m_MemoryAddress; nnosAddressSpaceManagerFree(&s_SpaceManager, detail::ConvertToC(this)); Result result; uptr dummy; result = nn::svc::ControlMemory( &dummy, addr, NULL, size, nn::os::MEMORY_OPERATION_FREE, nn::os::MEMORY_PERMISSION_READ_WRITE ); NN_ERR_THROW_FATAL(result); // これ以降 this にアクセスしてはいけない result = nn::svc::ControlMemory( &dummy, memAddr, NULL, size, nn::os::MEMORY_OPERATION_PROTECT, nn::os::MEMORY_PERMISSION_READ_WRITE ); NN_ERR_THROW_FATAL(result); return reinterpret_cast(memAddr); } return NULL; } void StackMemory::MoveFrom(StackMemory* pFrom) { reinterpret_cast(&s_SpaceManager)->Switch(this, pFrom); this->m_MemoryAddress = pFrom->m_MemoryAddress; pFrom->m_MemoryAddress = NULL; } }} // namespace nn::os #include using namespace nn::os; extern "C" { // StackMemory void nnosStackMemoryProtect(nnosStackMemory* p, void* pMem, size_t size) { new (p) StackMemory(pMem, size); } void nnosStackMemoryUnprotect(nnosStackMemory* p) { StackMemory* pStackMemory = reinterpret_cast(p); pStackMemory->~StackMemory(); } uptr nnosStackMemoryGetAddress(nnosStackMemory* p) { StackMemory* pStackMemory = reinterpret_cast(p); return pStackMemory->GetAddress(); } size_t nnosStackMemoryGetSize(nnosStackMemory* p) { StackMemory* pStackMemory = reinterpret_cast(p); return pStackMemory->GetSize(); } uptr nnosStackMemoryGetStackBottom(nnosStackMemory* p) { StackMemory* pStackMemory = reinterpret_cast(p); return pStackMemory->GetStackBottom(); } } #endif // if NN_PLATFORM_HAS_MMU