Automated merge with ssh://daystrom.m5sim.org//repo/m5
[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 #include "sim/process.hh"
38
39 using namespace TheISA;
40
41 TranslatingPort::TranslatingPort(const std::string &_name,
42 Process *p, AllocType alloc)
43 : FunctionalPort(_name), pTable(p->pTable), process(p),
44 allocating(alloc)
45 { }
46
47 TranslatingPort::~TranslatingPort()
48 { }
49
50 bool
51 TranslatingPort::tryReadBlob(Addr addr, uint8_t *p, int size)
52 {
53 Addr paddr;
54 int prevSize = 0;
55
56 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
57
58 if (!pTable->translate(gen.addr(),paddr))
59 return false;
60
61 Port::readBlob(paddr, p + prevSize, gen.size());
62 prevSize += gen.size();
63 }
64
65 return true;
66 }
67
68 void
69 TranslatingPort::readBlob(Addr addr, uint8_t *p, int size)
70 {
71 if (!tryReadBlob(addr, p, size))
72 fatal("readBlob(0x%x, ...) failed", addr);
73 }
74
75
76 bool
77 TranslatingPort::tryWriteBlob(Addr addr, uint8_t *p, int size)
78 {
79
80 Addr paddr;
81 int prevSize = 0;
82
83 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
84
85 if (!pTable->translate(gen.addr(), paddr)) {
86 if (allocating == Always) {
87 pTable->allocate(roundDown(gen.addr(), VMPageSize),
88 VMPageSize);
89 } else if (allocating == NextPage) {
90 // check if we've accessed the next page on the stack
91 if (!process->checkAndAllocNextPage(gen.addr()))
92 panic("Page table fault when accessing virtual address %#x "
93 "during functional write\n", gen.addr());
94 } else {
95 return false;
96 }
97 pTable->translate(gen.addr(), paddr);
98 }
99
100 Port::writeBlob(paddr, p + prevSize, gen.size());
101 prevSize += gen.size();
102 }
103
104 return true;
105 }
106
107
108 void
109 TranslatingPort::writeBlob(Addr addr, uint8_t *p, int size)
110 {
111 if (!tryWriteBlob(addr, p, size))
112 fatal("writeBlob(0x%x, ...) failed", addr);
113 }
114
115 bool
116 TranslatingPort::tryMemsetBlob(Addr addr, uint8_t val, int size)
117 {
118 Addr paddr;
119
120 for (ChunkGenerator gen(addr, size, VMPageSize); !gen.done(); gen.next()) {
121
122 if (!pTable->translate(gen.addr(), paddr)) {
123 if (allocating == Always) {
124 pTable->allocate(roundDown(gen.addr(), VMPageSize),
125 VMPageSize);
126 pTable->translate(gen.addr(), paddr);
127 } else {
128 return false;
129 }
130 }
131
132 Port::memsetBlob(paddr, val, gen.size());
133 }
134
135 return true;
136 }
137
138 void
139 TranslatingPort::memsetBlob(Addr addr, uint8_t val, int size)
140 {
141 if (!tryMemsetBlob(addr, val, size))
142 fatal("memsetBlob(0x%x, ...) failed", addr);
143 }
144
145
146 bool
147 TranslatingPort::tryWriteString(Addr addr, const char *str)
148 {
149 Addr paddr,vaddr;
150 uint8_t c;
151
152 vaddr = addr;
153
154 do {
155 c = *str++;
156 if (!pTable->translate(vaddr++,paddr))
157 return false;
158
159 Port::writeBlob(paddr, &c, 1);
160 } while (c);
161
162 return true;
163 }
164
165 void
166 TranslatingPort::writeString(Addr addr, const char *str)
167 {
168 if (!tryWriteString(addr, str))
169 fatal("writeString(0x%x, ...) failed", addr);
170 }
171
172 bool
173 TranslatingPort::tryReadString(std::string &str, Addr addr)
174 {
175 Addr paddr,vaddr;
176 uint8_t c;
177
178 vaddr = addr;
179
180 do {
181 if (!pTable->translate(vaddr++,paddr))
182 return false;
183
184 Port::readBlob(paddr, &c, 1);
185 str += c;
186 } while (c);
187
188 return true;
189 }
190
191 void
192 TranslatingPort::readString(std::string &str, Addr addr)
193 {
194 if (!tryReadString(str, addr))
195 fatal("readString(0x%x, ...) failed", addr);
196 }
197