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
|
#pragma once
// Where a title-window mapping sits and how much of a stream pool one mapping serves, without a device
// (docs/d3d12_vulkan.md section 3.3 "Title-chosen addresses").
//
// The device's resolve_title_memory makes the mapping; these decide its range first, so a test can count mappings
// without Vulkan. Two rules matter here:
// - a window is page-aligned, capped at one request's maximum and charged to LEONIDA_TITLE_WINDOWS_MB; one that
// does not fit is not made, and the address is backed another way or written as a zero-size view, never raw;
// - a stream pool (the title's CPU view paired with a GPU-only alias, gfxgeom section 3) is one reservation of
// 16-24 GiB, so it cannot be one allocation: one mapping serves a page-aligned readable run of it, and every
// request inside that run reuses it instead of asking for a 128 KiB window each.
#include <algorithm>
#include <cstdint>
namespace leonida {
// A page-aligned mapping of the title's memory: [start, start + size).
struct TitleWindowRange {
std::uint64_t start{};
std::uint64_t size{};
};
// `size` bytes more fit in the title-window budget [used, cap).
inline bool title_window_fits(std::uint64_t cap, std::uint64_t used, std::uint64_t size) {
return used <= cap && size <= cap - used;
}
// The page-aligned range a window made for [va, va + bytes) covers: inside the free span [lo, hi), aligned to
// `align`, at most `most` bytes of the request, and charged against [used, cap). False when the span holds no page
// for it or the budget has no room.
inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi,
std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used,
TitleWindowRange& out) {
if (!align || (align & (align - 1)) != 0 || !most || va < lo || hi <= va) return false;
const std::uint64_t want = (std::min)(bytes ? bytes : std::uint64_t(1), most);
const std::uint64_t start = (std::max)(va & ~(align - 1), lo);
const std::uint64_t raw_end = want > UINT64_MAX - va ? UINT64_MAX : va + want;
std::uint64_t end = raw_end > UINT64_MAX - (align - 1) ? UINT64_MAX : (raw_end + align - 1) & ~(align - 1);
end = (std::min)(end, hi);
if (end <= va || end <= start) return false;
const TitleWindowRange range{start, end - start};
if (!title_window_fits(cap, used, range.size)) return false;
out = range;
return true;
}
// The extent of a stream pool one mapping serves for the request at `va`: from its page to the end of the readable
// run (`readable_end`, exclusive), at most `most` bytes, inside the pool [pool_lo, pool_hi). The same extent comes
// back for every request inside it, so each extent costs one mapping, not one per request.
inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va,
std::uint64_t readable_end, std::uint64_t align, std::uint64_t most,
TitleWindowRange& out) {
if (!align || (align & (align - 1)) != 0 || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi ||
readable_end <= va)
return false;
const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo);
std::uint64_t end = (std::min)(readable_end, pool_hi);
end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1);
if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most);
end = (std::min)(end, pool_hi);
if (end <= va || end <= start) return false;
out = {start, end - start};
return true;
}
} // namespace leonida
|