page_table.cc revision 2399
1/* 2 * Copyright (c) 2003 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/** 30 * @file 31 * Definitions of page table. 32 */ 33#include <string> 34#include <map> 35#include <fstream> 36 37#include "base/bitfield.hh" 38#include "base/intmath.hh" 39#include "base/trace.hh" 40#include "mem/page_table.hh" 41#include "sim/builder.hh" 42#include "sim/sim_object.hh" 43#include "sim/system.hh" 44 45using namespace std; 46 47PageTable::PageTable(System *_system, Addr _pageSize) 48 : pageSize(_pageSize), offsetMask(mask(floorLog2(_pageSize))), 49 system(_system) 50{ 51 assert(isPowerOf2(pageSize)); 52} 53 54PageTable::~PageTable() 55{ 56} 57 58Fault 59PageTable::page_check(Addr addr, int size) const 60{ 61 if (size < sizeof(uint64_t)) { 62 if (!isPowerOf2(size)) { 63 panic("Invalid request size!\n"); 64 return Machine_Check_Fault; 65 } 66 67 if ((size - 1) & addr) 68 return Alignment_Fault; 69 } 70 else { 71 if ((addr & (VMPageSize - 1)) + size > VMPageSize) { 72 panic("Invalid request size!\n"); 73 return Machine_Check_Fault; 74 } 75 76 if ((sizeof(uint64_t) - 1) & addr) 77 return Alignment_Fault; 78 } 79 80 return No_Fault; 81} 82 83 84 85 86void 87PageTable::allocate(Addr vaddr, int size) 88{ 89 // starting address must be page aligned 90 assert(pageOffset(vaddr) == 0); 91 92 for (; size > 0; size -= pageSize, vaddr += pageSize) { 93 std::map<Addr,Addr>::iterator iter = pTable.find(vaddr); 94 95 if (iter != pTable.end()) { 96 // already mapped 97 fatal("PageTable::allocate: address 0x%x already mapped", vaddr); 98 } 99 100 pTable[vaddr] = system->new_page(); 101 } 102} 103 104 105 106bool 107PageTable::translate(Addr vaddr, Addr &paddr) 108{ 109 Addr page_addr = pageAlign(vaddr); 110 std::map<Addr,Addr>::iterator iter = pTable.find(page_addr); 111 112 if (iter == pTable.end()) { 113 return false; 114 } 115 116 paddr = iter->second + pageOffset(vaddr); 117 return true; 118} 119 120 121Fault 122PageTable::translate(CpuRequestPtr &req) 123{ 124 assert(pageAlign(req->vaddr + req->size - 1) == pageAlign(req->vaddr)); 125 if (!translate(req->vaddr, req->paddr)) { 126 return Machine_Check_Fault; 127 } 128 return page_check(req->paddr, req->size); 129} 130