page_table.cc (2632:1bb2f91485ea) page_table.cc (2665:a124942bacb8)
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.
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 * Authors: Steve Reinhardt
29 * Ron Dreslinski
27 */
28
29/**
30 * @file
31 * Definitions of page table.
32 */
33#include <string>
34#include <map>
35#include <fstream>
36
37#include "arch/faults.hh"
38#include "base/bitfield.hh"
39#include "base/intmath.hh"
40#include "base/trace.hh"
41#include "mem/page_table.hh"
42#include "sim/builder.hh"
43#include "sim/sim_object.hh"
44#include "sim/system.hh"
45
46using namespace std;
47using namespace TheISA;
48
49PageTable::PageTable(System *_system, Addr _pageSize)
50 : pageSize(_pageSize), offsetMask(mask(floorLog2(_pageSize))),
51 system(_system)
52{
53 assert(isPowerOf2(pageSize));
54}
55
56PageTable::~PageTable()
57{
58}
59
60Fault
61PageTable::page_check(Addr addr, int size) const
62{
63 if (size < sizeof(uint64_t)) {
64 if (!isPowerOf2(size)) {
65 panic("Invalid request size!\n");
66 return genMachineCheckFault();
67 }
68
69 if ((size - 1) & addr)
70 return genAlignmentFault();
71 }
72 else {
73 if ((addr & (VMPageSize - 1)) + size > VMPageSize) {
74 panic("Invalid request size!\n");
75 return genMachineCheckFault();
76 }
77
78 if ((sizeof(uint64_t) - 1) & addr)
79 return genAlignmentFault();
80 }
81
82 return NoFault;
83}
84
85
86
87
88void
89PageTable::allocate(Addr vaddr, int size)
90{
91 // starting address must be page aligned
92 assert(pageOffset(vaddr) == 0);
93
94 for (; size > 0; size -= pageSize, vaddr += pageSize) {
95 std::map<Addr,Addr>::iterator iter = pTable.find(vaddr);
96
97 if (iter != pTable.end()) {
98 // already mapped
99 fatal("PageTable::allocate: address 0x%x already mapped", vaddr);
100 }
101
102 pTable[vaddr] = system->new_page();
103 }
104}
105
106
107
108bool
109PageTable::translate(Addr vaddr, Addr &paddr)
110{
111 Addr page_addr = pageAlign(vaddr);
112 std::map<Addr,Addr>::iterator iter = pTable.find(page_addr);
113
114 if (iter == pTable.end()) {
115 return false;
116 }
117
118 paddr = iter->second + pageOffset(vaddr);
119 return true;
120}
121
122
123Fault
124PageTable::translate(RequestPtr &req)
125{
126 Addr paddr;
127 assert(pageAlign(req->getVaddr() + req->getSize() - 1)
128 == pageAlign(req->getVaddr()));
129 if (!translate(req->getVaddr(), paddr)) {
130 return genMachineCheckFault();
131 }
132 req->setPaddr(paddr);
133 return page_check(req->getPaddr(), req->getSize());
134}
30 */
31
32/**
33 * @file
34 * Definitions of page table.
35 */
36#include <string>
37#include <map>
38#include <fstream>
39
40#include "arch/faults.hh"
41#include "base/bitfield.hh"
42#include "base/intmath.hh"
43#include "base/trace.hh"
44#include "mem/page_table.hh"
45#include "sim/builder.hh"
46#include "sim/sim_object.hh"
47#include "sim/system.hh"
48
49using namespace std;
50using namespace TheISA;
51
52PageTable::PageTable(System *_system, Addr _pageSize)
53 : pageSize(_pageSize), offsetMask(mask(floorLog2(_pageSize))),
54 system(_system)
55{
56 assert(isPowerOf2(pageSize));
57}
58
59PageTable::~PageTable()
60{
61}
62
63Fault
64PageTable::page_check(Addr addr, int size) const
65{
66 if (size < sizeof(uint64_t)) {
67 if (!isPowerOf2(size)) {
68 panic("Invalid request size!\n");
69 return genMachineCheckFault();
70 }
71
72 if ((size - 1) & addr)
73 return genAlignmentFault();
74 }
75 else {
76 if ((addr & (VMPageSize - 1)) + size > VMPageSize) {
77 panic("Invalid request size!\n");
78 return genMachineCheckFault();
79 }
80
81 if ((sizeof(uint64_t) - 1) & addr)
82 return genAlignmentFault();
83 }
84
85 return NoFault;
86}
87
88
89
90
91void
92PageTable::allocate(Addr vaddr, int size)
93{
94 // starting address must be page aligned
95 assert(pageOffset(vaddr) == 0);
96
97 for (; size > 0; size -= pageSize, vaddr += pageSize) {
98 std::map<Addr,Addr>::iterator iter = pTable.find(vaddr);
99
100 if (iter != pTable.end()) {
101 // already mapped
102 fatal("PageTable::allocate: address 0x%x already mapped", vaddr);
103 }
104
105 pTable[vaddr] = system->new_page();
106 }
107}
108
109
110
111bool
112PageTable::translate(Addr vaddr, Addr &paddr)
113{
114 Addr page_addr = pageAlign(vaddr);
115 std::map<Addr,Addr>::iterator iter = pTable.find(page_addr);
116
117 if (iter == pTable.end()) {
118 return false;
119 }
120
121 paddr = iter->second + pageOffset(vaddr);
122 return true;
123}
124
125
126Fault
127PageTable::translate(RequestPtr &req)
128{
129 Addr paddr;
130 assert(pageAlign(req->getVaddr() + req->getSize() - 1)
131 == pageAlign(req->getVaddr()));
132 if (!translate(req->getVaddr(), paddr)) {
133 return genMachineCheckFault();
134 }
135 req->setPaddr(paddr);
136 return page_check(req->getPaddr(), req->getSize());
137}