gem5 v24.0.0.0
Loading...
Searching...
No Matches
translation_gen.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google, Inc.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
30
31#include <initializer_list>
32#include <list>
33#include <memory>
34#include <ostream>
35#include <vector>
36
37#include "base/cprintf.hh"
38#include "base/gtest/logging.hh"
40
41using testing::HasSubstr;
42using testing::Pointwise;
43using namespace gem5;
44
45namespace gem5
46{
47
48// A dummy fault class so we have something to return from failed translations.
49class FaultBase {};
50
51Fault dummyFault1 = std::make_shared<gem5::FaultBase>();
52Fault dummyFault2 = std::make_shared<gem5::FaultBase>();
53
54std::ostream &
55operator<<(std::ostream &os, const TranslationGen::Range &range)
56{
57 if (range.fault == dummyFault1)
58 ccprintf(os, "%#x=>fault1", range.vaddr);
59 else if (range.fault == dummyFault2)
60 ccprintf(os, "%#x=>fault2", range.vaddr);
61 else
62 ccprintf(os, "%#x=>%#x [%#x]", range.vaddr, range.paddr, range.size);
63 return os;
64}
65
66} // namespace gem5
67
69
70// Compare ranges which are returned by the generator.
71MATCHER(GenRangeEq, "")
72{
73 const auto &[actual, expected] = arg;
74 // vaddr and fault should match no matter what.
75 if (actual.vaddr != expected.vaddr || actual.fault != expected.fault)
76 return false;
77 if (expected.fault) {
78 // If there was a fault, paddr and size don't matter.
79 return true;
80 } else {
81 // If there was no fault, check paddr and size.
82 return actual.paddr == expected.paddr && actual.size == expected.size;
83 }
84}
85
86// Compare ranges which are sent to the translate function.
87MATCHER(TransRangeEq, "")
88{
89 const auto &[actual, expected] = arg;
90 // Only the vaddr and size fields are inputs to the translate function.
91 return actual.vaddr == expected.vaddr && actual.size == expected.size;
92}
93
95{
96 public:
97 // Results to return from the translate function, one by one.
99 // All the Ranges which were passed into translate from the generator.
101
102 private:
103 // Where we are in the results vector.
105
106 public:
107 TestTranslationGen(Addr new_start, Addr new_size,
108 std::initializer_list<Range> ranges={}) :
109 TranslationGen(new_start, new_size), results(ranges),
111 {}
112
113 void
114 translate(Range &range) const override
115 {
116 // First, record what range we were asked to translate.
117 args.emplace_back(range);
118 // Then, if we're not out of results to return...
119 if (resultPos != results.end()) {
120 // Record the fault we're supposed to return, if any.
121 range.fault = resultPos->fault;
122 if (!range.fault) {
123 // If there wasn't a fault, size and paddr are meaningful.
124 range.size = resultPos->size;
125 range.paddr = resultPos->paddr;
126 range.flags = resultPos->flags;
127 }
128 // Advance to the next result.
129 resultPos++;
130 }
131 }
132};
133
135{
136 TestTranslationGen gen1(0x10000, 0x20000);
137
138 EXPECT_EQ(gen1.start(), 0x10000);
139 EXPECT_EQ(gen1.size(), 0x20000);
140
141 TestTranslationGen gen2(0x3456, 0x6543);
142
143 EXPECT_EQ(gen2.start(), 0x3456);
144 EXPECT_EQ(gen2.size(), 0x6543);
145}
146
147TEST(TranslationGen, BeginAndEnd)
148{
149 TestTranslationGen gen1(0x10000, 0x20000);
150
151 EXPECT_NE(gen1.begin(), gen1.end());
152 EXPECT_EQ(gen1.begin(), gen1.begin());
153 EXPECT_EQ(gen1.end(), gen1.end());
154
155 TestTranslationGen gen2(0x30000, 0x40000);
156
157 EXPECT_NE(gen1.begin(), gen2.begin());
158 EXPECT_EQ(gen1.end(), gen2.end());
159}
160
161TEST(TranslationGen, SuccessfulTwoStep)
162{
163 TestTranslationGen gen(0x10000, 0x10000, {
164 // Results for translate.
165 {0x0, 0x8000, 0x30000, {}, NoFault},
166 {0x0, 0x8000, 0x40000, {}, NoFault}
167 });
168
169 RangeList range_list;
170 for (const auto &range: gen)
171 range_list.emplace_back(range);
172
173 // What the generator should return.
174 const RangeList expected_gen{
175 {0x10000, 0x8000, 0x30000, {}, NoFault},
176 {0x18000, 0x8000, 0x40000, {}, NoFault}
177 };
178 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
179
180 // What the generator should have been asked to translate.
181 const RangeList expected_trans{
182 {0x10000, 0x10000, 0x0, {}, NoFault},
183 {0x18000, 0x8000, 0x0, {}, NoFault}
184 };
185 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
186}
187
188TEST(TranslationGen, RetryOnFault)
189{
190 TestTranslationGen gen(0x10000, 0x10000, {
191 // Results for translate.
192 {0x0, 0x8000, 0x30000, {}, NoFault},
193 {0x0, 0x0, 0x0, {}, dummyFault1},
194 {0x0, 0x8000, 0x40000, {}, NoFault}
195 });
196
197 RangeList range_list;
198 for (const auto &range: gen)
199 range_list.emplace_back(range);
200
201 // What the generator should return.
202 const RangeList expected_gen{
203 {0x10000, 0x8000, 0x30000, {}, NoFault},
204 {0x18000, 0x0, 0x0, {}, dummyFault1},
205 {0x18000, 0x8000, 0x40000, {}, NoFault}
206 };
207 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
208
209 // What the generator should have been asked to translate.
210 const RangeList expected_trans{
211 {0x10000, 0x10000, 0x0, {}, NoFault},
212 {0x18000, 0x8000, 0x0, {}, NoFault},
213 {0x18000, 0x8000, 0x0, {}, NoFault}
214 };
215 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
216}
217
218TEST(TranslationGen, RetryTwiceOnFault)
219{
220 TestTranslationGen gen(0x10000, 0x10000, {
221 // Results for translate.
222 {0x0, 0x8000, 0x30000, {}, NoFault},
223 {0x0, 0x0, 0x0, {}, dummyFault1},
224 {0x0, 0x0, 0x0, {}, dummyFault2},
225 {0x0, 0x8000, 0x40000, {}, NoFault}
226 });
227
228 RangeList range_list;
229 for (const auto &range: gen)
230 range_list.emplace_back(range);
231
232 // What the generator should return.
233 const RangeList expected_gen{
234 {0x10000, 0x8000, 0x30000, {}, NoFault},
235 {0x18000, 0x0, 0x0, {}, dummyFault1},
236 {0x18000, 0x0, 0x0, {}, dummyFault2},
237 {0x18000, 0x8000, 0x40000, {}, NoFault}
238 };
239 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
240
241 // What the generator should have been asked to translate.
242 const RangeList expected_trans{
243 {0x10000, 0x10000, 0x0, {}, NoFault},
244 {0x18000, 0x8000, 0x0, {}, NoFault},
245 {0x18000, 0x8000, 0x0, {}, NoFault},
246 {0x18000, 0x8000, 0x0, {}, NoFault}
247 };
248 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
249}
250
251TEST(TranslationGen, FaultAtStart)
252{
253 TestTranslationGen gen(0x10000, 0x10000, {
254 // Results for translate.
255 {0x0, 0x0, 0x0, {}, dummyFault1},
256 {0x0, 0x8000, 0x30000, {}, NoFault},
257 {0x0, 0x8000, 0x40000, {}, NoFault}
258 });
259
260 RangeList range_list;
261 for (const auto &range: gen)
262 range_list.emplace_back(range);
263
264 // What the generator should return.
265 const RangeList expected_gen{
266 {0x10000, 0x0, 0x0, {}, dummyFault1},
267 {0x10000, 0x8000, 0x30000, {}, NoFault},
268 {0x18000, 0x8000, 0x40000, {}, NoFault}
269 };
270 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
271
272 // What the generator should have been asked to translate.
273 const RangeList expected_trans{
274 {0x10000, 0x10000, 0x0, {}, NoFault},
275 {0x10000, 0x10000, 0x0, {}, NoFault},
276 {0x18000, 0x8000, 0x0, {}, NoFault}
277 };
278 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
279}
280
281TEST(TranslationGen, FaultInMiddle)
282{
283 TestTranslationGen gen(0x10000, 0x18000, {
284 // Results for translate.
285 {0x0, 0x8000, 0x30000, {}, NoFault},
286 {0x0, 0x0, 0x0, {}, dummyFault1},
287 {0x0, 0x8000, 0x40000, {}, NoFault},
288 {0x0, 0x8000, 0x50000, {}, NoFault}
289 });
290
291 RangeList range_list;
292 for (const auto &range: gen)
293 range_list.emplace_back(range);
294
295 // What the generator should return.
296 const RangeList expected_gen{
297 {0x10000, 0x8000, 0x30000, {}, NoFault},
298 {0x18000, 0x0, 0x0, {}, dummyFault1},
299 {0x18000, 0x8000, 0x40000, {}, NoFault},
300 {0x20000, 0x8000, 0x50000, {}, NoFault}
301 };
302 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
303
304 // What the generator should have been asked to translate.
305 const RangeList expected_trans{
306 {0x10000, 0x18000, 0x0, {}, NoFault},
307 {0x18000, 0x10000, 0x0, {}, NoFault},
308 {0x18000, 0x10000, 0x0, {}, NoFault},
309 {0x20000, 0x8000, 0x0, {}, NoFault}
310 };
311 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
312}
313
314TEST(TranslationGen, VariablePageSize)
315{
316 TestTranslationGen gen(0x10000, 0x20000, {
317 // Results for translate.
318 {0x0, 0x8000, 0x30000, {}, NoFault},
319 {0x0, 0x10000, 0x40000, {}, NoFault},
320 {0x0, 0x8000, 0x50000, {}, NoFault}
321 });
322
323 RangeList range_list;
324 for (const auto &range: gen)
325 range_list.emplace_back(range);
326
327 // What the generator should return.
328 const RangeList expected_gen{
329 {0x10000, 0x8000, 0x30000, {}, NoFault},
330 {0x18000, 0x10000, 0x40000, {}, NoFault},
331 {0x28000, 0x8000, 0x50000, {}, NoFault}
332 };
333 EXPECT_THAT(range_list, Pointwise(GenRangeEq(), expected_gen));
334
335 // What the generator should have been asked to translate.
336 const RangeList expected_trans{
337 {0x10000, 0x20000, 0x0, {}, NoFault},
338 {0x18000, 0x18000, 0x0, {}, NoFault},
339 {0x28000, 0x8000, 0x0, {}, NoFault}
340 };
341 EXPECT_THAT(gen.args, Pointwise(TransRangeEq(), expected_trans));
342}
343
344TEST(TranslationGenDeathTest, IncrementEndIterator)
345{
346 TestTranslationGen gen(0x10000, 0x20000);
347 gtestLogOutput.str("");
348 ASSERT_ANY_THROW(gen.end()++);
349 EXPECT_THAT(gtestLogOutput.str(),
350 HasSubstr("Can't increment end iterator."));
351}
const std::vector< Range > results
TestTranslationGen(Addr new_start, Addr new_size, std::initializer_list< Range > ranges={})
void translate(Range &range) const override
Subclasses implement this function to complete TranslationGen.
std::vector< Range >::const_iterator resultPos
TranslationGen is a base class for a generator object which returns information about address transla...
const_iterator begin() const
const_iterator end() const
STL vector class.
Definition stl.hh:37
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
Bitfield< 17 > os
Definition misc.hh:838
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
std::shared_ptr< FaultBase > Fault
Definition types.hh:249
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
thread_local GTestLogOutput gtestLogOutput
Definition logging.cc:33
static std::ostream & operator<<(std::ostream &os, const DummyMatRegContainer &d)
Definition matrix.hh:564
constexpr decltype(nullptr) NoFault
Definition types.hh:253
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
This structure represents a single, contiguous translation, or carries information about whatever fau...
TEST(TranslationGen, Accessors)
MATCHER(GenRangeEq, "")

Generated on Tue Jun 18 2024 16:24:05 for gem5 by doxygen 1.11.0