00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __mmu_h
00009 #define __mmu_h
00010
00011 #include <system/config.h>
00012
00013 __BEGIN_SYS
00014
00015 template <unsigned int DIRECTORY_BITS,
00016 unsigned int PAGE_BITS,
00017 unsigned int OFFSET_BITS>
00018 class MMU_Common
00019 {
00020 protected:
00021 MMU_Common() {}
00022
00023 protected:
00024
00025 typedef CPU::Log_Addr Log_Addr;
00026 typedef CPU::Phy_Addr Phy_Addr;
00027
00028
00029 static const unsigned int PAGE_SHIFT = OFFSET_BITS;
00030 static const unsigned int DIRECTORY_SHIFT = OFFSET_BITS + PAGE_BITS;
00031 static const unsigned int PAGE_SIZE = 1 << PAGE_SHIFT;
00032
00033 public:
00034
00035 typedef unsigned char Page[PAGE_SIZE];
00036 typedef Page Frame;
00037
00038
00039 typedef Phy_Addr PT_Entry;
00040 typedef Phy_Addr PD_Entry;
00041
00042
00043 class Flags
00044 {
00045 public:
00046 enum {
00047 PRE = 0x001,
00048 RW = 0x002,
00049 USR = 0x004,
00050 CWT = 0x008,
00051 CD = 0x010,
00052 CT = 0x020,
00053 IO = 0x040,
00054 SYS = (PRE | RW ),
00055 APP = (PRE | RW | USR)
00056 };
00057
00058 public:
00059 Flags() {}
00060 Flags(const Flags & f) : _flags(f._flags) {}
00061 Flags(unsigned int f) : _flags(f) {}
00062
00063 operator unsigned int() const { return _flags; }
00064
00065 friend Debug & operator << (Debug & db, Flags f)
00066 { db << (void *)f._flags; return db; }
00067
00068 private:
00069 unsigned int _flags;
00070 };
00071
00072
00073 static const unsigned int PT_ENTRIES = sizeof(Page) / sizeof(PT_Entry);
00074 static const unsigned int PD_ENTRIES = PT_ENTRIES;
00075
00076 public:
00077 static unsigned int pages(unsigned int bytes) {
00078 return (bytes + sizeof(Page) - 1) / sizeof(Page);
00079 }
00080 static unsigned int page_tables(unsigned int pages) {
00081 return (pages + PT_ENTRIES - 1) / PT_ENTRIES;
00082 }
00083
00084 static unsigned int offset(Log_Addr addr) {
00085 return addr & (sizeof(Page) - 1);
00086 }
00087 static unsigned int indexes(Log_Addr addr) {
00088 return addr & ~(sizeof(Page) - 1);
00089 }
00090 static unsigned int page(Log_Addr addr) {
00091 return (addr >> PAGE_SHIFT) & (PT_ENTRIES - 1);
00092 }
00093 static unsigned int directory(Log_Addr addr) {
00094 return addr >> DIRECTORY_SHIFT;
00095 }
00096
00097 static Log_Addr align_page(Log_Addr addr) {
00098 return (addr + sizeof(Page) - 1) & ~(sizeof(Page) - 1);
00099 }
00100 static Log_Addr align_directory(Log_Addr addr) {
00101 return (addr + sizeof(Page) * sizeof(Page) - 1) &
00102 ~(sizeof(Page) * sizeof(Page) - 1);
00103 }
00104
00105 };
00106
00107 __END_SYS
00108
00109 #include __HEADER_ARCH(mmu)
00110
00111 #endif