/*---------------------------------------------------------------------------* 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: 31762 $ *--------------------------------------------------------------------------- */ #include #if NN_PLATFORM_HAS_MMU #include #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) { // Check that it is not initialized. NN_TASSERT_( GetAddress() == NULL ); NN_ALIGN_TASSERT_( size, NN_OS_MEMORY_PAGE_SIZE ); // Get memory from the virtual address space. 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_ERR_THROW_FATAL(result); m_SpaceAllocated = true; // TODO: Delete this commented out portion if nn::svc::QueryMemory operations are stable. //MemoryInfo memoryInfo; //PageInfo pageInfo; //result = nn::svc::QueryMemory(&memoryInfo, &pageInfo, addr); //if (result.IsFailure()) //{ // NN_TPANIC_("QueryMemory failed."); //} // //// Could also redo by mapping the obtained size? //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_ERR_THROW_FATAL(TryInitialize(size, readOnly, otherReadOnly, noMap)); } void SharedMemoryBlock::AttachAndMap(Handle handle, size_t size, bool readOnly) { // TODO: Insert check code for size and attributes if SVC is maintained. size = GetPageAlignedSize(size); this->SetHandle(handle); Map(size, readOnly); } void SharedMemoryBlock::Unmap() { if (GetAddress() != NULL) { if( m_SpaceAllocated ) { NN_ERR_THROW_FATAL(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