Merge zizzer.eecs.umich.edu:/z/m5/Bitkeeper/newmem
[gem5.git] / src / mem / translating_port.cc
1 /*
2 * Copyright (c) 2001-2005 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: Ron Dreslinski
29 * Steve Reinhardt
30 */
31
32 #include <string>
33 #include "base/chunk_generator.hh"
34 #include "mem/port.hh"
35 #include "mem/translating_port.hh"
36 #include "mem/page_table.hh"
37
38 using namespace TheISA;
39
40 TranslatingPort::TranslatingPort(const std::string &_name,
41 PageTable *p_table, bool alloc)
42 : FunctionalPort(_name), pTable(p_table), allocating(alloc)
43 { }
44
45 TranslatingPort::~TranslatingPort()
46 { }
47
48 bool
49 TranslatingPort::tryReadBlob(Addr addr, uint8_t *p, int size)
50 {
51 Addr paddr;
52 int prevSize = 0;
53
54 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
55
56 if (!pTable->translate(gen.addr(),paddr))
57 return false;
58
59 Port::readBlob(paddr, p + prevSize, gen.size());
60 prevSize += gen.size();
61 }
62
63 return true;
64 }
65
66 void
67 TranslatingPort::readBlob(Addr addr, uint8_t *p, int size)
68 {
69 if (!tryReadBlob(addr, p, size))
70 fatal("readBlob(0x%x, ...) failed", addr);
71 }
72
73
74 bool
75 TranslatingPort::tryWriteBlob(Addr addr, uint8_t *p, int size)
76 {
77
78 Addr paddr;
79 int prevSize = 0;
80
81 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
82
83 if (!pTable->translate(gen.addr(), paddr)) {
84 if (allocating) {
85 pTable->allocate(roundDown(gen.addr(), VMPageSize),
86 VMPageSize);
87 pTable->translate(gen.addr(), paddr);
88 } else {
89 return false;
90 }
91 }
92
93 Port::writeBlob(paddr, p + prevSize, gen.size());
94 prevSize += gen.size();
95 }
96
97 return true;
98 }
99
100
101 void
102 TranslatingPort::writeBlob(Addr addr, uint8_t *p, int size)
103 {
104 if (!tryWriteBlob(addr, p, size))
105 fatal("writeBlob(0x%x, ...) failed", addr);
106 }
107
108 bool
109 TranslatingPort::tryMemsetBlob(Addr addr, uint8_t val, int size)
110 {
111 Addr paddr;
112
113 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
114
115 if (!pTable->translate(gen.addr(), paddr)) {
116 if (allocating) {
117 pTable->allocate(roundDown(gen.addr(), VMPageSize),
118 VMPageSize);
119 pTable->translate(gen.addr(), paddr);
120 } else {
121 return false;
122 }
123 }
124
125 Port::memsetBlob(paddr, val, gen.size());
126 }
127
128 return true;
129 }
130
131 void
132 TranslatingPort::memsetBlob(Addr addr, uint8_t val, int size)
133 {
134 if (!tryMemsetBlob(addr, val, size))
135 fatal("memsetBlob(0x%x, ...) failed", addr);
136 }
137
138
139 bool
140 TranslatingPort::tryWriteString(Addr addr, const char *str)
141 {
142 Addr paddr,vaddr;
143 uint8_t c;
144
145 vaddr = addr;
146
147 do {
148 c = *str++;
149 if (!pTable->translate(vaddr++,paddr))
150 return false;
151
152 Port::writeBlob(paddr, &c, 1);
153 } while (c);
154
155 return true;
156 }
157
158 void
159 TranslatingPort::writeString(Addr addr, const char *str)
160 {
161 if (!tryWriteString(addr, str))
162 fatal("writeString(0x%x, ...) failed", addr);
163 }
164
165 bool
166 TranslatingPort::tryReadString(std::string &str, Addr addr)
167 {
168 Addr paddr,vaddr;
169 uint8_t c;
170
171 vaddr = addr;
172
173 do {
174 if (!pTable->translate(vaddr++,paddr))
175 return false;
176
177 Port::readBlob(paddr, &c, 1);
178 str += c;
179 } while (c);
180
181 return true;
182 }
183
184 void
185 TranslatingPort::readString(std::string &str, Addr addr)
186 {
187 if (!tryReadString(str, addr))
188 fatal("readString(0x%x, ...) failed", addr);
189 }
190