/*---------------------------------------------------------------------------* Project: Horizon File: os_SharedMemory.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: 25458 $ *---------------------------------------------------------------------------*/ #include #if NN_PLATFORM_HAS_MMU #include #include #include #include #include #include "os_AddressSpaceManager.h" namespace nn { namespace os { namespace { nnosAddressSpaceManager s_SpaceManager; size_t GetPageAlignedSize(size_t size) { return (size + NN_OS_MEMORY_PAGE_SIZE - 1) & ~(NN_OS_MEMORY_PAGE_SIZE - 1); } } namespace detail { void InitializeSharedMemory() { nnosAddressSpaceManagerInitialize(&s_SpaceManager, NN_OS_ADDR_SHARED_BEGIN, NN_OS_ADDR_SHARED_SIZE); } uptr AllocateFromSharedMemorySpace(MemoryBlockBase* p, size_t s) { AddressSpaceManager* pManager = reinterpret_cast(&s_SpaceManager); return pManager->Allocate(p, s, NN_OS_MEMORY_PAGE_SIZE); } void FreeToSharedMemorySpace(MemoryBlockBase* p) { AddressSpaceManager* pManager = reinterpret_cast(&s_SpaceManager); pManager->Free(p); } } // detail void SharedMemoryBlock::Map(size_t size, bool readOnly) { // 未初期化であることをチェックします。 NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); // 仮想アドレス空間からメモリを取得します。 uptr addr = detail::AllocateFromSharedMemorySpace(this, size); if (addr == NULL) { NN_TPANIC_("failed to allocate address space."); } this->MemoryBlockBase::SetReadOnly(readOnly); Result result; const bit32 myPermission = readOnly ? MEMORY_PERMISSION_READ : (MEMORY_PERMISSION_READ | MEMORY_PERMISSION_WRITE); result = nn::svc::MapMemoryBlock(GetHandle(), addr, myPermission, os::MEMORY_PERMISSION_DONT_CARE); NN_UTIL_PANIC_IF_FAILED(result); m_SpaceAllocated = true; // TODO: nn::svc::QueryMemory の動作が安定したらここのコメントアウトを消す //MemoryInfo memoryInfo; //PageInfo pageInfo; //result = nn::svc::QueryMemory(&memoryInfo, &pageInfo, addr); //if (result.IsFailure()) //{ // NN_TPANIC_("QueryMemory failed."); //} // //// 取得したサイズでマップし直してあげる手もあり? //if (memoryInfo.size != size) //{ // NN_TPANIC_("Mismatch size."); //} } Result SharedMemoryBlock::TryInitialize(size_t size, bool readOnly, bool otherReadOnly, bool noMap) { size = GetPageAlignedSize(size); bit32 myPermission = readOnly ? MEMORY_PERMISSION_READ : MEMORY_PERMISSION_READ | MEMORY_PERMISSION_WRITE; bit32 otherPermission = otherReadOnly ? MEMORY_PERMISSION_READ : MEMORY_PERMISSION_READ | MEMORY_PERMISSION_WRITE; Handle handle; Result result; result = nn::svc::CreateMemoryBlock(&handle, NULL, size, myPermission, otherPermission); NN_UTIL_RETURN_IF_FAILED(result); this->SetHandle(handle); if (!noMap) { Map(size, readOnly); } return result; } void SharedMemoryBlock::Initialize(size_t size, bool readOnly, bool otherReadOnly, bool noMap) { NN_UTIL_PANIC_IF_FAILED(TryInitialize(size, readOnly, otherReadOnly, noMap)); } void SharedMemoryBlock::AttachAndMap(Handle handle, size_t size, bool readOnly) { // TODO: SVC が整備されたら、サイズや属性のチェックコードを挿入。 size = GetPageAlignedSize(size); this->SetHandle(handle); Map(size, readOnly); } void SharedMemoryBlock::Unmap() { if (GetAddress() != NULL) { if( m_SpaceAllocated ) { NN_UTIL_PANIC_IF_FAILED(nn::svc::UnmapMemoryBlock(GetHandle(), GetAddress())); detail::FreeToSharedMemorySpace(this); } else { SetAddressAndSize(0, 0); } } } void SharedMemoryBlock::Finalize() { if (this->IsValid()) { Unmap(); this->HandleObject::Close(); } } }} #include using namespace nn::os; // SharedMemoryBlock void nnosSharedMemoryBlockAllocate(nnosSharedMemoryBlock* this_, size_t size, bool readOnly, bool otherReadOnly, bool noMap) { new (this_) SharedMemoryBlock(size, readOnly, otherReadOnly, noMap); } void nnosSharedMemoryBlockInitializeNoAllocate(nnosSharedMemoryBlock* this_) { new (this_) SharedMemoryBlock(); } void nnosSharedMemoryBlockFree(nnosSharedMemoryBlock* this_) { SharedMemoryBlock* pSharedMemoryBlock = reinterpret_cast(this_); pSharedMemoryBlock->~SharedMemoryBlock(); } uptr nnosSharedMemoryBlockGetAddress(nnosSharedMemoryBlock* this_) { SharedMemoryBlock* pSharedMemoryBlock = reinterpret_cast(this_); return pSharedMemoryBlock->GetAddress(); } size_t nnosSharedMemoryBlockGetSize(nnosSharedMemoryBlock* this_) { SharedMemoryBlock* pSharedMemoryBlock = reinterpret_cast(this_); return pSharedMemoryBlock->GetSize(); } bool nnosSharedMemoryBlockIsReadOnly(nnosSharedMemoryBlock* this_) { SharedMemoryBlock* pSharedMemoryBlock = reinterpret_cast(this_); return pSharedMemoryBlock->IsReadOnly(); } #endif // if NN_PLATFORM_HAS_MMU