src/Emu/Utility/System/Memory/VirtualMemory.h

説明を見る。
00001 // 
00002 // Copyright (c) 2005-2008 Kenichi Watanabe.
00003 // Copyright (c) 2005-2008 Yasuhiro Watari.
00004 // Copyright (c) 2005-2008 Hironori Ichibayashi.
00005 // Copyright (c) 2008-2009 Kazuo Horio.
00006 // Copyright (c) 2009-2013 Naruki Kurata.
00007 // Copyright (c) 2005-2013 Ryota Shioya.
00008 // Copyright (c) 2005-2013 Masahiro Goshima.
00009 // 
00010 // This software is provided 'as-is', without any express or implied
00011 // warranty. In no event will the authors be held liable for any damages
00012 // arising from the use of this software.
00013 // 
00014 // Permission is granted to anyone to use this software for any purpose,
00015 // including commercial applications, and to alter it and redistribute it
00016 // freely, subject to the following restrictions:
00017 // 
00018 // 1. The origin of this software must not be misrepresented; you must not
00019 // claim that you wrote the original software. If you use this software
00020 // in a product, an acknowledgment in the product documentation would be
00021 // appreciated but is not required.
00022 // 
00023 // 2. Altered source versions must be plainly marked as such, and must not be
00024 // misrepresented as being the original software.
00025 // 
00026 // 3. This notice may not be removed or altered from any source
00027 // distribution.
00028 // 
00029 // 
00030 
00031 
00032 #ifndef __EMULATORUTILITY_VIRTUAL_MEMORY_H__
00033 #define __EMULATORUTILITY_VIRTUAL_MEMORY_H__
00034 
00035 #include "Emu/Utility/System/Memory/HeapAllocator.h"
00036 
00037 
00038 namespace Onikiri {
00039     namespace EmulatorUtility {
00040 
00041         // Page size
00042         static const int VIRTUAL_MEMORY_PAGE_SIZE_BITS = 12;
00043         
00044         // Page attribute
00045         typedef u32 VIRTUAL_MEMORY_ATTR_TYPE;
00046         static const u32 VIRTUAL_MEMORY_ATTR_READ  = 1 << 0;    // readable
00047         static const u32 VIRTUAL_MEMORY_ATTR_WRITE = 1 << 1;    // writable
00048         static const u32 VIRTUAL_MEMORY_ATTR_EXEC  = 1 << 2;    // executable
00049 
00050         // Information about a physical memory page.
00051         // Each instance of this structure corresponds to each 'physical' page.
00052         struct PhysicalMemoryPage
00053         {
00054             u8* ptr;        // Physical memory address
00055             int refCount;   // A reference count for a copy-on-write function.
00056         };
00057 
00058         // Page table entry
00059         // Each instance of this structure corresponds to each 'logical' page.
00060         struct PageTableEntry
00061         {
00062             PhysicalMemoryPage*      phyPage;   // Physical page
00063             VIRTUAL_MEMORY_ATTR_TYPE attr;      // Page attribute
00064         };
00065 
00066         // 1-entry TLB
00067         class TLB
00068         {
00069         protected:
00070             u64   m_addr;
00071             bool  m_valid;
00072             PageTableEntry m_body;
00073             u64 m_offsetBits;
00074             u64 m_addrMask;
00075 
00076         public:
00077             TLB( int offsetBits );
00078             ~TLB();
00079             bool Lookup( u64 addr, PageTableEntry* entry ) const;
00080             void Write( u64 addr, const PageTableEntry& entry ); 
00081             void Flush();
00082         };
00083 
00084         // target AhX host AhXsNX
00085         class PageTable
00086         {
00087         public:
00088             // }bvPItZbgw PageTable \z (PageSize = 1 << offsetBits)
00089             explicit PageTable(int offsetBits);
00090             ~PageTable();
00091 
00092             // \zy[WTCY
00093             size_t GetPageSize() const;
00094 
00095             // \zItZbgrbg
00096             int GetOffsetBits() const;
00097 
00098             // AhXy[WO}XN
00099             u64 GetOffsetMask() const;
00100 
00101             // target AhX addr  host AhX
00102             void *TargetToHost(u64 addr);
00103 
00104             // target AhXtargetAddr}bvPChostAddr  (PageSize oCg)
00105             // Set an attribute ('attr') to target page.
00106             void AddMap(u64 targetAddr, u8* hostAddr, VIRTUAL_MEMORY_ATTR_TYPE attr);
00107             bool IsMapped(u64 targetAddr) const;
00108 
00109             // Copy address mapping for copy-on-write.
00110             // Set an attribute ('dstAttr') to target page.
00111             void CopyMap( u64 dstTargetAddr, u64 srcTargetAddr, VIRTUAL_MEMORY_ATTR_TYPE dstAttr );
00112 
00113             // targetAddr}bvPGg/
00114             bool GetMap( u64 targetAddr, PageTableEntry* page );
00115             bool SetMap( u64 targetAddr, const PageTableEntry& page );
00116 
00117             // Get a reference count of a page including targetAddr
00118             int GetPageReferenceCount( u64 targetAddr ); 
00119 
00120             // targetAddr}bvP
00121             // lt@XJEg
00122             int RemoveMap(u64 targetAddr);
00123 
00124         private:
00125             class AddrHash
00126             {
00127             private:
00128                 int m_offsetBits;
00129             public:
00130                 explicit AddrHash(int offsetBits) : m_offsetBits(offsetBits) {}
00131 
00132                 size_t operator()(const u64 value) const
00133                 {
00134                     return (size_t)(value >> m_offsetBits);
00135                 }
00136             };
00137             typedef unordered_map<u64, PageTableEntry, AddrHash > map_type;
00138             map_type m_map;
00139             int m_offsetBits;
00140             u64 m_offsetMask;
00141             TLB m_tlb;
00142             boost::pool<> m_phyPagePool;
00143         };
00144 
00145 
00146         class VirtualMemory
00147         {
00148         public:
00149             VirtualMemory( int pid, bool bigEndian, SystemIF* simSystem );
00150             ~VirtualMemory();
00151 
00152             // 
00153             void ReadMemory( MemAccess* access );
00154             void WriteMemory( MemAccess* access );
00155 
00156             //
00157             // Helper methods for memory access.
00158             // These methods are implemented in this class because
00159             // implementation using ReadMemory/WriteMemory is too slow.
00160             //
00161             // Note: These functions ignore page attribute.
00162             //
00163 
00164             // targetAddrsizeoCgvaluel
00165             void TargetMemset(u64 targetAddr, int value, u64 size);
00166             // target AhX src Chost AhX dst  size oCgRs[
00167             void MemCopyToHost(void* dst, u64 src, u64 size);
00168             // host AhX src Ctarget AhX dst  size oCgRs[
00169             void MemCopyToTarget(u64 dst, const void* src, u64 size);
00170 
00171             // 
00172             u64 GetPageSize() const;
00173 
00174             // addr y[WDAddHeapBlockd
00175             void AssignPhysicalMemory(u64 addr, VIRTUAL_MEMORY_ATTR_TYPE attr);
00176             // [addr, addr+size) y[WD
00177             void AssignPhysicalMemory(u64 addr, u64 size, VIRTUAL_MEMORY_ATTR_TYPE attr);
00178             // addr y[W
00179             void FreePhysicalMemory(u64 addr);
00180             // [addr, addr+size) y[W
00181             void FreePhysicalMemory(u64 addr, u64 size);
00182             
00183             // dstAddr y[W srcAddr y[WD
00184             void SetPhysicalMemoryMapping( u64 dstAddr, u64 srcAddr, VIRTUAL_MEMORY_ATTR_TYPE attr );
00185             // [dstAddr, dstAddr+size) y[W}bsOZbgD
00186             void SetPhysicalMemoryMapping( u64 dstAddr, u64 srcAddr, u64 size, VIRTUAL_MEMORY_ATTR_TYPE attr );
00187 
00188             // addr y[WCaddr y[Wf[^Rs[
00189             void AssignAndCopyPhysicalMemory( u64 addr, VIRTUAL_MEMORY_ATTR_TYPE attr  );
00190             // [dstAddr, dstAddr+size) y[W
00191             void AssignAndCopyPhysicalMemory( u64 addr, u64 size, VIRTUAL_MEMORY_ATTR_TYPE attr );
00192 
00193             // Write a page attribute to a page including 'addr'.
00194             void SetPageAttribute( u64 addr, VIRTUAL_MEMORY_ATTR_TYPE attr );
00195 
00196             // Copy-on-Write for a memory page including addr.
00197             // Return whether copy-on-write is done or not.
00198             bool CopyPageOnWrite( u64 addr );
00199 
00200             // rbOGfBA
00201             bool IsBigEndian() const {
00202                 return m_bigEndian;
00203             }
00204 
00205         private:
00206             // addr  size oCgC}bvPE
00207             // CMemoryBlockReiCe[^ Iter i[
00208             // l
00209             //  IterCT^IMemoryBlockReiinserter
00210             template <typename Iter>
00211             int SplitAtMapUnitBoundary(u64 addr, u64 size, Iter e) const;
00212 
00213             struct MemoryBlock {
00214                 MemoryBlock(u64 addr, u64 size) { this->addr = addr; this->size = size; }
00215                 u64 addr;
00216                 u64 size;
00217             };
00218             typedef std::vector<MemoryBlock> BlockArray;
00219 
00220             // PageTable & FreeList
00221             PageTable m_pageTbl;
00222             boost::pool<> m_pool;
00223 
00224             // mCV~[^R[obNC^[tF[X
00225             SystemIF* m_simSystem;
00226 
00227             // PID
00228             int m_pid;
00229 
00230             // ^[QbgrbOGfBA
00231             bool m_bigEndian;
00232 
00233         };
00234 
00235 
00236     } // namespace EmulatorUtility
00237 } // namespace Onikiri
00238 
00239 #endif

Onikiri2に対してTue Jun 18 14:34:20 2013に生成されました。  doxygen 1.4.7