Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions dm/gfx/nvn/nvn_dirty.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,25 @@ void nvn_dirty_remove_range(const void *mem) {
if (r) { prep_write((void *)r->base, r->end - r->base); atomic_store_explicit(&r->live, 0, memory_order_release); atomic_fetch_sub(&g_live, 1); }
pthread_mutex_unlock(&g_slot_mu);
}
// A cached slot can have retired or been reused. Check that it still
// covers the requested span before skipping the range table lookup.
static range_t *resolve_span(const void *span, uintptr_t addr, size_t len) {
range_t *r = (range_t *)span;
if (r && atomic_load_explicit(&r->live, memory_order_acquire) &&
addr >= r->base && addr + len <= r->end) return r;
return find_span(addr, len);
}
int nvn_dirty_tracked(const void *addr, size_t len) {
return find_span((uintptr_t)addr, len) != NULL;
}
// The tracked range holding [addr, addr+len), as a handle a caller keeps (packaged textures: the scan over every range ran twice per texture lookup). NULL = untracked
const void *nvn_dirty_span(const void *addr, size_t len) { return len ? find_span((uintptr_t)addr, len) : NULL; }
// Set read-only: mark the pages BUSY first and mark them PROT only after mprotect has landed (for the order and the invariant see the state notes at the top of the file)
void nvn_dirty_protect(const void *addr, size_t len) {
void nvn_dirty_protect_span(const void *span, const void *addr, size_t len) {
// Keep this descriptor live through the last mprotect. Retirement restores
// RW under this same mutex; it must not finish between lookup and PG_BUSY.
pthread_mutex_lock(&g_slot_mu);
range_t *r = len ? find_span((uintptr_t)addr, len) : NULL;
range_t *r = len ? resolve_span(span, (uintptr_t)addr, len) : NULL;
if (!r) { pthread_mutex_unlock(&g_slot_mu); return; }
atomic_fetch_add(&r->nprot, 1); // PEANUTS(upstream-candidate): in-flight guard: nprot stays non-zero until every transition in this call finishes
uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift;
Expand Down Expand Up @@ -341,6 +349,7 @@ void nvn_dirty_protect(const void *addr, size_t len) {
nstuck ? "; some could not be made writable again and stay protected (their first write faults and unprotects them)" : "");
}
}
void nvn_dirty_protect(const void *addr, size_t len) { nvn_dirty_protect_span(NULL, addr, len); }
// A swap of guest pages under the tracker (the Vulkan replay placing a pool whose textures it already tracks, vka_mem.c place): the swap reads the range's protections,
// copies the pages and puts the protections back on the new ones, so a page this module protected or unprotected meanwhile would come out with the wrong protection for
// its state (read-only and NONE: a writer spins forever; writable and PROT: its writes are missed). So the swap holds the tracker: every protected page of the range is
Expand Down Expand Up @@ -377,8 +386,7 @@ void nvn_dirty_release_all(void) {
uint64_t nvn_dirty_now(void) { return atomic_load(&g_now); }
int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_t stamp) {
if (!len) return 0;
range_t *r = (range_t *)span; uintptr_t a0 = (uintptr_t)addr; // the kept handle still holds the span (a slot reused by another pool over the same memory is as good): no scan
if (!r || !atomic_load_explicit(&r->live, memory_order_acquire) || a0 < r->base || a0 + len > r->end) r = find_span(a0, len);
range_t *r = resolve_span(span, (uintptr_t)addr, len);
if (!r) return 1; // When untracked, conservatively treat as dirty: better to upload more than to show stale content
if (stamp < atomic_load_explicit(&r->born, memory_order_relaxed)) return 1; // PEANUTS: checked before this pool was registered over the memory (born)
uintptr_t a = (uintptr_t)addr, p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift;
Expand All @@ -389,8 +397,8 @@ int nvn_dirty_since_span(const void *span, const void *addr, size_t len, uint64_
int nvn_dirty_bounds_since(const void *span, const void *addr, size_t len, uint64_t stamp, size_t *lo, size_t *hi) {
*lo = 0; *hi = len;
if (!len) return 0;
range_t *r = (range_t *)span; uintptr_t a = (uintptr_t)addr;
if (!r || !atomic_load_explicit(&r->live, memory_order_acquire) || a < r->base || a + len > r->end) r = find_span(a, len);
uintptr_t a = (uintptr_t)addr;
range_t *r = resolve_span(span, a, len);
if (!r) return 1;
if (stamp < atomic_load_explicit(&r->born, memory_order_relaxed)) return 1; // PEANUTS: every page counts (born); *lo, *hi are the whole of it already
uintptr_t p = (a - r->base) >> g_shift, q = (a + len - 1 - r->base) >> g_shift, f = UINTPTR_MAX, l = 0;
Expand Down
1 change: 1 addition & 0 deletions dm/gfx/nvn/nvn_dirty.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ void nvn_dirty_add_range(const void *mem, size_t size); // Register a
void nvn_dirty_stat(int *live, int *peak, int *cap); // number of registered memory pools, peak, tracking table capacity (for the memory readout line)
void nvn_dirty_remove_range(const void *mem); // Pool destruction: restore protected pages to writable and mark invalid
int nvn_dirty_tracked(const void *addr, size_t len);
void nvn_dirty_protect_span(const void *span, const void *addr, size_t len); // Reuse a span from nvn_dirty_span if it still covers the range
void nvn_dirty_protect(const void *addr, size_t len); // Set the covered pages read-only
void nvn_dirty_hold(const void *addr, size_t len); // Unprotect (and stamp) every protected page of [addr, addr+len) and protect nothing more until nvn_dirty_unhold
void nvn_dirty_unhold(void); // (a placement swapping those pages, vka_mem.c place: the tracker's protections must not change under it)
Expand Down
9 changes: 8 additions & 1 deletion dm/gfx/tests/t_dirty_slots.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,23 @@ int main(void) {
for (int i = 0; i < NMANY; i++) { many[i] = page_map(); nvn_dirty_add_range(many[i], 4096); }
void *last = many[NMANY - 1];
EXPECT("5000 live pools: the last one is tracked", 1, nvn_dirty_tracked(last, 4096));
nvn_dirty_protect(last, 4096);
const void *span = nvn_dirty_span(last, 4096);
nvn_dirty_protect_span(span, last, 4096);
stamp = nvn_dirty_now();
EXPECT("pool 5000 not written after protect: clean", 0, nvn_dirty_since(last, 4096, stamp));
((volatile uint8_t *)last)[0] = 5;
EXPECT("pool 5000 written by the game: dirty", 1, nvn_dirty_since(last, 4096, stamp));
nvn_dirty_remove_range(last);
nvn_dirty_protect_span(span, many[0], 4096);
stamp = nvn_dirty_now();
((volatile uint8_t *)many[0])[0] = 6;
EXPECT("a retired span falls back to the live range", 1, nvn_dirty_since(many[0], 4096, stamp));
for (int i = 0; i < NMANY; i++) { nvn_dirty_remove_range(many[i]); munmap(many[i], 4096); }
free(many);

// Adjacent pools sharing a host page (pools are 4 KB aligned, host pages 16 KB): the first page of SMBW's ASTC texture pool is shared with a small preceding pool, and was judged untracked and re-uploaded on every use
size_t pg = (size_t)getpagesize();
if (pg <= 4096) { puts("4 KiB-aligned adjacent pools cannot share a host page here"); return g_fail != 0; }
uint8_t *blk = mmap(NULL, 4 * pg, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
if (blk == MAP_FAILED) { perror("mmap"); exit(2); }
uint8_t *pa = blk, *pb = blk + pg - 0x1000; // pool A covers the start of page 0, pool B starts in the last 4 KB of page 0
Expand Down
2 changes: 1 addition & 1 deletion dm/gfx/vk_android/vka.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ typedef struct vka_img {
uint64_t wb_fence; // the fence of the submission that holds the last copy of its GPU content back into its storage (vka_tex_writeback; 0: none): vka_wbfollow.h
int wbf_waiting; // the prepare's last scan found it waiting for that copy-back; a scan that only runs for waiting images visits just these (wbf_visit)
int wt; // the game writes it through WriteTexels (vka_tex.c mtl_tex_write_region): never tracked again
uint64_t edge_h[2]; int edged;
uint64_t edge_h[2]; uintptr_t edge_block; int edged;
uint64_t hash; // DM_VK_NANSCAN: a hash of the storage at the last upload (vka_tex_id_check)
uint64_t gw_seen; int from_mem; uint64_t head, up_fence; uint8_t *snap; // head, up_fence: DM_VK_NANSCAN's storage watch // the newest GPU write into guest memory its last upload included (vka_tex.c gw_t); content from guest storage (ST_TRACK / ST_FILL)
// In a sparse virtual pool (hle_nvn.c nvn_sparse_*): the storage is read through the pool's 4 KB page table, never at mem (unmapped pages there are inaccessible);
Expand Down
13 changes: 9 additions & 4 deletions dm/gfx/vk_android/vka_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -1420,16 +1420,21 @@ static writes_t g_w;
// A texture or image read by a shader that declares it in another numeric class (float / uint / sint) than the view the game binds: logged once per shader, stage and binding
// (the first 64), with the texture. Such a binding is read through a view of the shader's class of the same texel size (vka_tex.c class_twin) unless its module is retyped
static void class_note(const vka_shader *sh, int st, const vka_res *r, int tid, int storage) {
static uint64_t seen[4096]; static int nlog;
static uint64_t seen[4096], logged[64]; static int nlog; // PEANUTS(upstream-candidate): bound the diagnostic lookup.
if (nlog >= 64) return;
int want = r->dim & DMSH_VK_DIM_SINT ? 2 : r->dim & DMSH_VK_DIM_UINT ? 1 : 0;
uint64_t h = (sh->key ^ (uint64_t)st << 56 ^ (uint64_t)r->binding << 40 ^ (uint64_t)want << 38) | 1;
uint32_t s = (uint32_t)(h * 0x9e3779b97f4a7c15ull >> 52);
for (uint32_t n = 0; n < 4096; n++, s = (s + 1) & 4095) { if (seen[s] == h) return; if (!seen[s]) break; }
if (nlog >= 64) return;
// A full diagnostic table must not scan thousands of slots per descriptor.
for (unsigned probe = 0; probe < 8; probe++, s = (s + 1) & 4095) {
if (seen[s] == h) return;
if (!seen[s]) break;
}
seen[s] = h;
int have = vka_tex_id_class_rec(tid);
if (have == want) return;
nlog++;
for (int i = 0; i < nlog; i++) if (logged[i] == h) return;
logged[nlog++] = h;
static const char *cn[3] = {"float", "uint", "sint"}; char why[320]; vka_tex_id_desc(tid, why, sizeof why);
VLOG("class: shader %016llx stage %d binding %u declares a %s %s, texture ID %d is %s (%s)", (unsigned long long)sh->key, st, r->binding, cn[want], storage ? "image" : "texture", tid, cn[have], why);
}
Expand Down
21 changes: 11 additions & 10 deletions dm/gfx/vk_android/vka_tex.c
Original file line number Diff line number Diff line change
Expand Up @@ -1100,32 +1100,33 @@ static void img_edges_take(vka_img *im) {
if (ie <= ia) { r[0][0] = a; r[0][1] = e; r[1][0] = r[1][1] = 0; }
else { r[0][0] = a; r[0][1] = ia; r[1][0] = ie; r[1][1] = e; }
for (int i = 0; i < 2; i++) im->edge_h[i] = r[i][1] > r[i][0] ? ft_hash(r[i][0], r[i][1]) : 0;
im->edge_block = pg;
im->edged = !nvn_dirty_since_span(im->span, im->mem, im->L.size, im->stamp);
}
static int img_edges_clean(vka_img *im) {
if (!im || !im->edged || im->vpool || !im->span || !im->mem) return 0;
if (dm_guest_unmapped((uintptr_t)im->mem, im->L.size)) return 0;
uintptr_t pg = nvn_dirty_block_bytes(); if (!pg) pg = (uintptr_t)getpagesize();
if (pg != im->edge_block || dm_guest_unmapped((uintptr_t)im->mem, im->L.size)) return 0;
uintptr_t r[2][2], ia, ie;
uintptr_t a = (uintptr_t)im->mem, e = a + im->L.size;
ia = (a + pg - 1) & ~(pg - 1); ie = e & ~(pg - 1);
if (ie <= ia) { r[0][0] = a; r[0][1] = e; r[1][0] = r[1][1] = 0; ia = ie = 0; }
else { r[0][0] = a; r[0][1] = ia; r[1][0] = ie; r[1][1] = e; }
uint64_t st = nvn_dirty_now();
nvn_dirty_protect(im->mem, im->L.size);
int in_dirty = (ie > ia && nvn_dirty_since_span(im->span, (const void *)ia, ie - ia, im->stamp));
int h0_bad = (r[0][1] > r[0][0] && ft_hash(r[0][0], r[0][1]) != im->edge_h[0]);
int h1_bad = (r[1][1] > r[1][0] && ft_hash(r[1][0], r[1][1]) != im->edge_h[1]);
if (in_dirty || h0_bad || h1_bad) return 0;
nvn_dirty_protect_span(im->span, im->mem, im->L.size);
if (ie > ia && nvn_dirty_since_span(im->span, (const void *)ia, ie - ia, im->stamp)) return 0;
for (int i = 0; i < 2; i++)
if (r[i][1] > r[i][0] && ft_hash(r[i][0], r[i][1]) != im->edge_h[i]) return 0;
// The hashes still describe the GPU copy. Writes during validation
// must remain newer than the stamp for the next prepare.
im->stamp = st;
img_edges_take(im);
static uint64_t g_edge_skips;
if (++g_edge_skips <= 8) VLOG("texture serial %llu %ux%u format %#x: only shared pages written beside it, re-upload skipped (%llu so far)",
(unsigned long long)im->serial, im->w, im->h, im->d.format, (unsigned long long)g_edge_skips);
return 1;
}
static void upload_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb) { // stamp, protect, then read (nvn_mtl.m pkg_upload): a write during the copy is seen at the next check
im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size);
im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size);
img_edges_take(im);
if (vka_nan_on()) im->hash = mem_hash(t->mem, t->L.size);
uint8_t *c = im->vpool ? NULL : gw_content(t->mem, t->L.size, &im->gw_seen); // (a sparse pool's storage is read through its page table: upload_)
Expand Down Expand Up @@ -1170,7 +1171,7 @@ static int upload_rows_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb,
if (st < pitch) return 0;
uint32_t r0 = (uint32_t)(lo / st), r1 = (uint32_t)((hi + st - 1) / st); if (r1 > by) r1 = by;
if (r0 >= r1) r0 = 0, r1 = by;
im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size); // stamp, protect, then read (upload_tracked)
im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size); // stamp, protect, then read (upload_tracked)
img_edges_take(im);
if (tbuf) { tbuf_rows_copy(im, t, cb, r0, r1, &g_rows_up); return 1; } // PEANUTS: tbuf (see above)
vka_span s; if (!vka_scratch(pitch * (r1 - r0), 16, &s)) return 1;
Expand All @@ -1193,7 +1194,7 @@ static int upload_bands_tracked(vka_img *im, const tex_t *t, VkCommandBuffer cb,
if (c) { free(c); return 0; }
if ((c = unmapped_content(t))) { free(c); return 0; }
im->gw_seen = seen;
im->stamp = nvn_dirty_now(); nvn_dirty_protect(t->mem, t->L.size); // stamp, protect, then read (upload_tracked)
im->stamp = nvn_dirty_now(); nvn_dirty_protect_span(im->span, t->mem, t->L.size); // stamp, protect, then read (upload_tracked)
img_edges_take(im);
uint64_t up = 0;
uint8_t *bst = NULL; uint32_t bst_cap = 0;
Expand Down
19 changes: 16 additions & 3 deletions dm/host/hle_svc.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,22 @@ static int guest_page_mapped(uintptr_t p) { // p: a guest address (page aligne
return 1;
}
uint64_t dm_guest_unmapped(uintptr_t a, size_t n) {
uint64_t c = 0; if (!n) return 0;
for (uintptr_t p = a & ~(uintptr_t)0xFFF; p < a + n; p += 0x1000) c += !guest_page_mapped(p);
return c;
if (!n) return 0;
uintptr_t first = a >> 12, last = ((a + n - 1) >> 12) + 1;
uintptr_t alias = (uintptr_t)g_alias >> 12, heap = (uintptr_t)g_heap >> 12;
uintptr_t lo = first > alias ? first : alias;
uintptr_t hi = alias + (g_alias_cap >> 12);
if (hi > last) hi = last;
uint64_t count = g_alias && hi > lo ? alias_unmapped((lo - alias) << 12, (hi - lo) << 12) : 0;

if (g_heap && last > heap && first < heap + (g_heap_cap >> 12)) {
uintptr_t opened = heap + ((atomic_load(&g_heap_cur) + 0xfff) >> 12);
lo = first > opened ? first : opened;
hi = heap + (g_heap_cap >> 12);
if (hi > last) hi = last;
if (hi > lo) count += hi - lo;
}
return count;
}
// Copy n bytes of our own memory that may become inaccessible meanwhile: the bytes copied before the first inaccessible one (a system call reads them, so an inaccessible page
// is an error, not a signal). If the system refuses the call itself (a seccomp filter: not seen on Android), a plain copy, with a red line once
Expand Down
21 changes: 19 additions & 2 deletions dm/host/tests/t_svc_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ static void alias_lifecycle(void) {
assert(pages(1, alias + g_alias_cap - P, 2 * P) == BAD);
}

static void unmapped_counts(void) {
// Include the edges of both regions and an alias bitmap word boundary.
assert(!pages(1, alias + 63 * P, 2 * P));
assert(!resize(2 * 1024 * 1024));
assert(dm_guest_unmapped(alias - 1, 2) == 1);
assert(dm_guest_unmapped(alias + 63 * P - 1, 2 * P + 2) == 2);
assert(dm_guest_unmapped(alias + g_alias_cap - 1, 2) == 1);
uintptr_t heap_end = (uintptr_t)g_heap + g_heap_cur;
assert(dm_guest_unmapped(heap_end - 1, 2) == 1);
assert(dm_guest_unmapped((uintptr_t)g_heap + g_heap_cap - 1, 2) == 1);
assert(dm_guest_unmapped(alias, 0) == 0);
assert(!pages(0, alias + 63 * P, 2 * P));
assert(!resize(0));
}

static void ownership(void) {
assert(!pages(1, alias, 16 * P));
memset((void *)alias, 0x34, 16 * P);
Expand All @@ -212,7 +227,9 @@ static void ownership(void) {
assert(!pages(0, alias, P) && !pages(1, alias, P));
assert(*(uint8_t *)(alias + P) == 0x34);
memset((void *)(stack + P), 0x76, 2 * P);
readonly_base = alias; readonly_size = memory_getpagesize();
// Protect the host page that contains the start of the copyback source.
readonly_size = memory_getpagesize();
readonly_base = (alias + P) & ~(uintptr_t)(readonly_size - 1);
assert(!mprotect((void *)readonly_base, readonly_size, PROT_READ));
assert(!unmap(stack + P, alias + P, 2 * P) && !readonly_size);
for (size_t i = P; i < 3 * P; ++i) assert(((uint8_t *)alias)[i] == 0x76);
Expand Down Expand Up @@ -549,7 +566,7 @@ int main(int argc, char **argv) {
for (unsigned i = 0; i < 8; ++i) assert(!pthread_create(&threads[i], NULL, init_worker, &addresses[i]));
for (unsigned i = 0; i < 8; ++i) { assert(!pthread_join(threads[i], NULL)); assert(addresses[i] == addresses[0]); }
alias = addresses[0]; stack = info(I_StackAddr); heap = info(I_HeapAddr);
lazy_commit(); alias_lifecycle(); ownership(); module_sources(); module_back_patch(); failures(); release_fallback(); concurrency(); fragmented_queries();
lazy_commit(); alias_lifecycle(); unmapped_counts(); ownership(); module_sources(); module_back_patch(); failures(); release_fallback(); concurrency(); fragmented_queries();
guest_helper(); read_mapped_race();
if (!DM_GAME_MEM_MAP_LIMIT) metadata_limit();
else reported_lower();
Expand Down
Loading