aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi/memory/mmio.tests.cpp
blob: e9aeacdb0e9a4a75e59916d02573c8d73d30cbfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <kernel/test_support/cpu.hpp>
#include <kernel/test_support/memory.hpp>

#include <kapi/memory.hpp>
#include <kapi/test_support/memory.hpp>

#include <kstd/result.hpp>
#include <kstd/system_error.hpp>
#include <kstd/units.hpp>

#include <catch2/catch_message.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>

#include <cstddef>
#include <cstdint>
#include <limits>
#include <utility>

// NOLINTBEGIN(readability-magic-numbers)

namespace
{
  using kapi::memory::mmio_window;
  using access = kapi::memory::mmio_window::access;
  using halt = kernel::tests::cpu::halt;

  //! A hardware range to map
  struct window_layout
  {
    std::uint64_t hw_base;
    std::uint64_t hw_size;
  };

  //! The first and last offsets (inclusive) at which an access of a given width is both in bounds and naturally aligned
  //! with respect to the *hardware* address.
  struct slots
  {
    std::uint64_t first;
    std::uint64_t last;
  };

  constexpr auto slots_for(window_layout layout, std::uint64_t width) -> slots
  {
    auto const first = (width - layout.hw_base % width) % width;
    auto const latest_start = layout.hw_size - width;
    auto const last = latest_start - (layout.hw_base + latest_start) % width;
    return {first, last};
  }

  //! Map a window that needs every page of the MMIO pool.
  auto map_pool_sized() -> kstd::result<mmio_window>
  {
    return mmio_window::map(kapi::memory::physical_address{}, kernel::tests::memory::mmio_pool_size, access::read_only);
  }
}  // namespace

TEMPLATE_TEST_CASE("Scenario: MMIO window accesses", "[memory][mmio]", std::uint8_t, std::uint16_t, std::uint32_t,
                   std::uint64_t)
{
  constexpr auto width = sizeof(TestType);

  auto const layout = GENERATE(values<window_layout>({
    {0x1000,  0x100}, // aligned hw base == true  && hw size == sub-page   && boundary crossed == false
    {0x1000, 0x1000}, // aligned hw base == true  && hw size == one page   && boundary crossed == false
    {0x1000, 0x2000}, // aligned hw base == true  && hw size == multi-page && boundary crossed == false
    {0x1000,  0x102}, // aligned hw base == true  && hw size == sub-page   && boundary crossed == true
    {0x1001,  0x100}, // aligned hw base == false && hw size == sub-page   && boundary crossed == false
    {0x1001, 0x1000}, // aligned hw base == false && hw size == one page   && boundary crossed == false
    {0x1ff8,   0x10}, // aligned hw base == false && hw size == sub-page   && boundary crossed == true
  }));

  INFO("hw_base = " << layout.hw_base << ", hw_size = " << layout.hw_size);

  auto const slot = slots_for(layout, width);
  auto const hw_base = kapi::memory::physical_address{layout.hw_base};
  auto const hw_size = kstd::bytes{layout.hw_size};

  GIVEN("a writable window")
  {
    auto window = mmio_window::map(hw_base, hw_size, access::writable);
    REQUIRE(window);

    WHEN("writing to the first and last valid slot")
    {
      // The last slot touches the last byte of the range, which for page crossing layouts lives on the second page.
      auto const first_value = static_cast<TestType>(0x1122'3344'5566'7788ULL);
      auto const last_value = static_cast<TestType>(0x99aa'bbcc'ddee'ff00ULL);

      window->write<TestType>(kstd::bytes{slot.first}, first_value);
      window->write<TestType>(kstd::bytes{slot.last}, last_value);

      THEN("the values can be read back correctly")
      {
        CHECK(window->read<TestType>(kstd::bytes{slot.first}) == first_value);
        CHECK(window->read<TestType>(kstd::bytes{slot.last}) == last_value);
      }
    }

    WHEN("writing out of bounds but otherwise aligned")
    {
      // slot.last + width is aligned, and either straddles or lies past the end, so only the bounds check can fire.
      CHECK_THROWS_AS(window->write<TestType>(kstd::bytes{slot.last + width}, TestType{}), halt);
    }

    WHEN("writing misaligned but otherwise in bounds")
    {
      if constexpr (width > 1)
      {
        CHECK_THROWS_AS(window->write<TestType>(kstd::bytes{slot.first + 1}, TestType{}), halt);
      }
    }
  }

  GIVEN("a read-only window")
  {
    auto window = mmio_window::map(hw_base, hw_size, access::read_only);
    REQUIRE(window);

    THEN("reads at the first and last valid slot are allowed")
    {
      CHECK_NOTHROW(window->read<TestType>(kstd::bytes{slot.first}));
      CHECK_NOTHROW(window->read<TestType>(kstd::bytes{slot.last}));
    }

    THEN("aligned, in bounds write is rejected")
    {
      CHECK_THROWS_AS(window->write<TestType>(kstd::bytes{slot.first}, TestType{}), halt);
    }

    THEN("reading an aligned but out of bounds offset is rejected")
    {
      CHECK_THROWS_AS(window->read<TestType>(kstd::bytes{slot.last + width}), halt);
    }

    THEN("in bounds but misaligned read is rejected")
    {
      if constexpr (width > 1)
      {
        CHECK_THROWS_AS(window->read<TestType>(kstd::bytes{slot.first + 1}), halt);
      }
    }

    THEN("reading at exactly the window size is rejected")
    {
      CHECK_THROWS_AS(window->read<TestType>(hw_size), halt);
    }

    THEN("reading offsets close to the largest representable value is rejected")
    {
      constexpr auto max_offset = std::numeric_limits<std::size_t>::max();
      CHECK_THROWS_AS(window->read<TestType>(kstd::bytes{max_offset}), halt);
      CHECK_THROWS_AS(window->read<TestType>(kstd::bytes{max_offset - width + 1}), halt);
    }
  }
}

SCENARIO("MMIO windows over overlapping hardware ranges", "[memory][mmio]")
{
  GIVEN("a writable window")
  {
    auto second = mmio_window::map(kapi::memory::physical_address{0x1800}, kstd::bytes{0x1000}, access::writable);
    REQUIRE(second);

    AND_GIVEN("another writable window whose hardware range overlaps the first")
    {
      auto first = mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{0x1000}, access::writable);
      REQUIRE(first);

      WHEN("a value is written through one window")
      {
        // Hardware address 0x1800 is offset 0x800 in the first window and offset 0 in the second.
        first->write<std::uint32_t>(kstd::bytes{0x800}, 0xcafe'f00d);

        THEN("the value is visible through the other window")
        {
          CHECK(second->read<std::uint32_t>(kstd::bytes{0}) == 0xcafe'f00d);
        }
      }
    }

    AND_GIVEN("another window that is destroyed again")
    {
      {
        auto first = mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{0x1000}, access::writable);
        REQUIRE(first);
        first->write<std::uint32_t>(kstd::bytes{0x800}, 0xcafe'f00d);
      }

      THEN("the remaining window is still usable and observes the value")
      {
        CHECK(second->read<std::uint32_t>(kstd::bytes{0}) == 0xcafe'f00d);
      }
    }
  }
}

SCENARIO("MMIO window creation errors", "[memory][mmio]")
{
  GIVEN("an initialized MMIO subsystem")
  {
    WHEN("mapping a window of size zero")
    {
      auto window = mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{0}, access::read_only);

      THEN("the mapping fails with invalid_argument")
      {
        REQUIRE(!window);
        CHECK(window.error() == kstd::errc::invalid_argument);
      }
    }

    WHEN("mapping a window that would wrap around the end of the physical address space")
    {
      auto window = mmio_window::map(kapi::memory::physical_address{std::numeric_limits<std::uint64_t>::max()},
                                     kstd::bytes{2}, access::read_only);

      THEN("the mapping fails with invalid_argument")
      {
        REQUIRE(!window);
        CHECK(window.error() == kstd::errc::invalid_argument);
      }
    }

    WHEN("mapping a window that needs the whole MMIO pool")
    {
      auto window = map_pool_sized();

      THEN("the mapping succeeds")
      {
        REQUIRE(window);
      }

      AND_WHEN("mapping another window while the pool is exhausted")
      {
        REQUIRE(window);
        auto other = mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{1}, access::read_only);

        THEN("the mapping fails with not_enough_memory")
        {
          REQUIRE(!other);
          CHECK(other.error() == kstd::errc::not_enough_memory);
        }
      }
    }

    WHEN("the page mapper rejects the first page of a window")
    {
      auto failed = mmio_window::map(kapi::memory::physical_address{kernel::tests::memory::mmio_size.value},
                                     kstd::bytes{0x100}, access::read_only);

      THEN("the mapping fails")
      {
        REQUIRE(!failed);
      }

      THEN("the reserved linear range has been given back")
      {
        auto window = map_pool_sized();
        CHECK(window);
      }
    }

    WHEN("the page mapper rejects the second page of a window")
    {
      auto const last_page = kernel::tests::memory::mmio_size - kapi::memory::page::size;
      auto failed =
          mmio_window::map(kapi::memory::physical_address{last_page.value}, kstd::bytes{0x2000}, access::read_only);

      THEN("the mapping fails")
      {
        REQUIRE(!failed);
      }

      THEN("everything acquired for it has been given back")
      {
        auto window = map_pool_sized();
        CHECK(window);
      }
    }
  }
}

SCENARIO("MMIO window lifetime", "[memory][mmio]")
{
  GIVEN("a window of the entire MMIO pool that is destroyed again")
  {
    {
      auto window = map_pool_sized();
      REQUIRE(window);
    }

    THEN("the entire pool can be mapped again")
    {
      auto window = map_pool_sized();
      CHECK(window);
    }
  }

  GIVEN("an unaligned window that spans two pages and is destroyed again")
  {
    {
      auto window = mmio_window::map(kapi::memory::physical_address{0x1001}, kstd::bytes{0x1000}, access::read_only);
      REQUIRE(window);
    }

    THEN("the entire pool can be mapped again")
    {
      auto window = map_pool_sized();
      CHECK(window);
    }
  }

  GIVEN("a writable window")
  {
    auto original = mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{0x100}, access::writable);
    REQUIRE(original);
    original->write<std::uint32_t>(kstd::bytes{0}, 0x1234'5678);

    WHEN("it is move-constructed into another window")
    {
      auto moved = std::move(original.value());

      THEN("the new window refers to the same hardware range")
      {
        CHECK(moved.read<std::uint32_t>(kstd::bytes{0}) == 0x1234'5678);
      }

      THEN("the moved-from window rejects all accesses")
      {
        CHECK_THROWS_AS(original->read<std::uint32_t>(kstd::bytes{0}), halt);
        CHECK_THROWS_AS(original->write<std::uint32_t>(kstd::bytes{0}, 0), halt);
      }
    }

    WHEN("it is move-assigned over another window")
    {
      auto target = mmio_window::map(kapi::memory::physical_address{0x2000}, kstd::bytes{0x100}, access::writable);
      REQUIRE(target);

      target.value() = std::move(original.value());

      THEN("the target refers to the hardware range of the source")
      {
        CHECK(target->read<std::uint32_t>(kstd::bytes{0}) == 0x1234'5678);
      }

      THEN("the moved-from window rejects all accesses")
      {
        CHECK_THROWS_AS(original->read<std::uint32_t>(kstd::bytes{0}), halt);
      }

      THEN("the resources previously held by the target have been released")
      {
        auto const almost_everything = kernel::tests::memory::mmio_pool_size - kapi::memory::page::size;
        auto big = mmio_window::map(kapi::memory::physical_address{}, almost_everything, access::read_only);
        CHECK(big);
      }
    }

    WHEN("it is move-assigned to itself")
    {
      auto & reference = original.value();
      reference = std::move(original.value());

      THEN("it remains usable")
      {
        CHECK(original->read<std::uint32_t>(kstd::bytes{0}) == 0x1234'5678);
      }
    }
  }
}

SCENARIO("MMIO windows without an initialized MMIO subsystem", "[memory][mmio]")
{
  GIVEN("an uninitialized MMIO subsystem")
  {
    kapi::test_support::memory::deinit_mmio();

    WHEN("mapping a window")
    {
      THEN("the system panics")
      {
        CHECK_THROWS_AS(mmio_window::map(kapi::memory::physical_address{0x1000}, kstd::bytes{0x100}, access::read_only),
                        halt);
      }
    }
  }
}

// NOLINTEND(readability-magic-numbers)