Skip to content

Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.

C

ramfs: a write brings the cached pages up to date in place, so the Calendar icon comes out whole

vcvkk0acee4d8521a
diff --git a/userland/rc.jobs.calendar b/userland/rc.jobs.calendar
index ad55581..9d8a1c2 100644
--- a/userland/rc.jobs.calendar
+++ b/userland/rc.jobs.calendar
@@ -1,19 +1,12 @@
# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
-# The Calendar icon drawn wrong (RC_JOBS=rc.jobs.calendar, gpu=1, a run of about two and a half minutes):
-# the home screen, and SignificantTimeChangeNotification posted every three seconds, 26 times. Each makes
+# The Calendar icon (RC_JOBS=rc.jobs.calendar, gpu=1, a run of about two and a half minutes): the home
+# screen, and SignificantTimeChangeNotification posted every three seconds, 26 times. Each makes
# SpringBoard draw the Calendar's icon anew, for a date that may have changed (with wifi=1 the network
-# setting the clock does the same, now and then, which is how it was first seen). With gpu=1 about half
-# of the screenshots taken meanwhile show the icon with square corners in its top 16 rows and a line
-# under it; the software renderer never does.
-# What is known: each refresh gives two 180 x 180 pictures (720 bytes a row); the one whose bytes start
-# 4224 into a 16K page is wrong in exactly its first and last pages, the ones it shares with what is
-# around it (rows 0-16 and 176-179 still without the rounded mask, the rest masked), and the screen
-# shows it. It is so already in SpringBoard's own memory (the CGImage's bytes, looked at from inside
-# SpringBoard), with gpu=1 only: with the software renderer the same picture is right there too. The
-# window server only shows what it is sent: the pages it holds for its surface around the picture and
-# what it reads at that address agree, at creation and at every lock, and holding them for reading
-# only changes nothing; one processor (no cpus=4) changes nothing. Copy-on-write in the kernel passes
-# every case of src/cow_test.c (rc.jobs.cow), the window server's own way of holding a picture among
-# them. Why SpringBoard's picture comes out so when the window server draws with the GPU is not known.
+# setting the clock does the same, now and then, which is how it was first seen), and write it into
+# IconServices' cache, a file in /private/var that SpringBoard and the window server have mapped.
+# The icon used to come out with square corners in its top 16 rows and a line under it in about half
+# of the screenshots taken meanwhile (with gpu=1 only): the ramfs left the cache's pages that the
+# window server held wired, for its surfaces around the icons next to it, as they were before the
+# write. Now none (bsd/miscfs/ramfs/ramfs.c, ramfs_refresh_cache()).
/redwood/bin/rwtouch wait 40 swipe 0.5 0.985 0.5 0.3 200 wait 10 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3
/redwood/bin/rwtouch notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3 notify SignificantTimeChangeNotification wait 3
diff --git a/userland/rc.jobs.cow b/userland/rc.jobs.cow
index a27ecf3..179b656 100644
--- a/userland/rc.jobs.cow
+++ b/userland/rc.jobs.cow
@@ -1,4 +1,4 @@
# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
-# Copy-on-write in the kernel (RC_JOBS=rc.jobs.cow, a run of a minute): see src/cow_test.c. Every line
+# Copies and file writes in the kernel (RC_JOBS=rc.jobs.cow, a run of a minute): see src/cow_test.c. Every line
# it writes should end with no page wrong.
/redwood/bin/cow_test
diff --git a/userland/src/cow_test.c b/userland/src/cow_test.c
index 75dc163..c8062a4 100644
--- a/userland/src/cow_test.c
+++ b/userland/src/cow_test.c
@@ -1,19 +1,25 @@
/*
- * cow_test: is memory that is copied really a copy? For each of the ways a picture's bytes can go
- * from one place to another -- sent out of line with a virtual copy, vm_copy() into memory that is
- * there already -- a buffer of an icon's size (180 x 180 x 4) is filled, copied, and the source
- * written over afterwards; the copy must still be what the source was, in every 16K page of it.
- * Page-aligned and 4224 bytes into a page (where the broken Calendar icon's bytes lie: its first
- * and last pages shared with what is around it), from memory of our own, from malloc, from
- * purgeable memory, with the source or the destination wired (mlock), and with the received copy
- * made read-only and wired, or with a surface made around it the way CoreAnimation's window server
- * makes one around a picture (src/... IOSurfaceAddress; kernel: MiniIOSurface.cpp).
- * Run by RC_JOBS=rc.jobs.cow; every line should end with 0 pages wrong.
+ * cow_test: is what is copied really a copy, and what is written really seen? For each of the ways
+ * a picture's bytes go from one place to another -- sent out of line with a virtual copy, vm_copy()
+ * into memory that is there already, a memory entry made as a copy (how XPC hands large data over),
+ * a file written with write() while it is mapped -- a buffer of an icon's size (180 x 180 x 4) is
+ * filled, copied or written, and the source written over afterwards; every 16K page of the result
+ * must be what it should be. Page-aligned and 4224 bytes into a page (where the broken Calendar
+ * icon's bytes lay: its first and last pages shared with what is around it), from memory of our
+ * own, from malloc, from purgeable memory, with the source or the destination wired (mlock), with
+ * the received copy made read-only and then wired or with a surface made around it the way
+ * CoreAnimation's window server makes one around a picture (MiniIOSurface.cpp), and with a file's
+ * mapping wired before it is written: that last one is the Calendar icon's case, which the ramfs
+ * got wrong (it threw cached pages away after a write and could not throw away wired ones; see
+ * ramfs_refresh_cache()).
+ * Run by RC_JOBS=rc.jobs.cow ("cow_test files": only the file cases). Every line should end with
+ * no page wrong.
*/
#include <mach/mach.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOSurface/IOSurfaceRef.h>
#include <sys/mman.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -172,7 +178,7 @@ three(const char *what, unsigned char *buffer, size_t size, int surface_mode)
const unsigned char *got = g.message.data.address;
uintptr_t start = (uintptr_t)got;
- vm_protect(mach_task_self(), (vm_address_t)got & ~(page - 1), size + page, 0, VM_PROT_READ);
+ vm_protect(mach_task_self(), (vm_address_t)got & ~(page - 1), ((start + size + page - 1) & ~(page - 1)) - (start & ~(page - 1)), 0, VM_PROT_READ);
if (!surface_mode) {
locked = mlock((const void *)((uintptr_t)got & ~(page - 1)), ((start + size + page - 1) & ~(page - 1)) - (start & ~(page - 1)));
} else {
@@ -224,13 +230,147 @@ three(const char *what, unsigned char *buffer, size_t size, int surface_mode)
mach_port_destroy(mach_task_self(), port);
}
+/* a memory entry made as a copy of memory (what XPC hands large data over with), mapped, and the source written after */
+static void
+four(const char *what, unsigned char *buffer, size_t size, int flags)
+{
+ size_t page = (size_t)getpagesize(), changed = 0, pages = 0;
+ memory_object_size_t entry_size = size;
+ mach_port_t entry = MACH_PORT_NULL;
+ vm_address_t mapped = 0;
+ kern_return_t kr, km = KERN_FAILURE;
+ char line[512] = "";
+ size_t used = 0, offset = (uintptr_t)buffer & (page - 1);
+
+ memset(buffer, 0x11, size);
+ kr = mach_make_memory_entry_64(mach_task_self(), &entry_size, (memory_object_offset_t)(uintptr_t)buffer,
+ VM_PROT_READ | flags, &entry, MACH_PORT_NULL);
+ if (kr == KERN_SUCCESS) {
+ km = vm_map(mach_task_self(), &mapped, entry_size, 0, VM_FLAGS_ANYWHERE, entry, 0, 0, VM_PROT_READ, VM_PROT_READ,
+ VM_INHERIT_NONE);
+ }
+ memset(buffer, 0x22, size);
+ if (km == KERN_SUCCESS) {
+ /* where the bytes are in the mapping: at the entry's own offset if the entry starts on a page, else at 0 */
+ const unsigned char *got = (const unsigned char *)mapped + (entry_size >= size + offset ? offset : 0);
+ uintptr_t start = (uintptr_t)got;
+
+ for (size_t at = 0; at < size;) {
+ size_t next = ((start + at) & ~(page - 1)) + page - start, end = next < size ? next : size, bad = 0;
+
+ for (size_t i = at; i < end; i++) {
+ bad += got[i] != 0x11;
+ }
+ if (bad != 0 && used < sizeof(line) - 40) {
+ used += (size_t)snprintf(line + used, sizeof(line) - used, " page %zu (bytes %zu-%zu): %zu", pages, at, end, bad);
+ }
+ changed += bad != 0;
+ pages++;
+ at = end;
+ }
+ vm_deallocate(mach_task_self(), mapped, entry_size);
+ }
+ printf("cow_test: memory entry, %s (%zu bytes, %zu into its page): entry %#x, %llu bytes, mapped %#x; %zu of %zu pages show "
+ "what was written after%s\n", what, size, offset, kr, (unsigned long long)entry_size, km, changed, pages, line);
+ if (entry != MACH_PORT_NULL) {
+ mach_port_deallocate(mach_task_self(), entry);
+ }
+}
+
+/*
+ * A file that one program has mapped while another writes it (an icon cache read by SpringBoard and
+ * written by iconservicesagent): written with write() at a place that does not start on a page, the
+ * mapping must show what was written, in every page of it. `touch`: which pages of the mapping were
+ * read before the write (0 none, 1 the first and the last, 2 all).
+ */
+static void
+five(const char *what, const char *path, size_t offset, size_t size, int touch, int twice, int wire)
+{
+ size_t page = (size_t)getpagesize(), file_size = 16 * page, changed = 0, pages = 0;
+ unsigned char *data = malloc(size), *zero = calloc(1, file_size);
+ const unsigned char *map;
+ char line[512] = "";
+ size_t used = 0;
+ int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
+
+ if (fd < 0 || data == NULL || zero == NULL) {
+ printf("cow_test: file, %s: cannot open %s\n", what, path);
+ return;
+ }
+ write(fd, zero, file_size);
+ map = mmap(NULL, file_size, PROT_READ, MAP_SHARED, fd, 0);
+ if (map == MAP_FAILED) {
+ printf("cow_test: file, %s: cannot map\n", what);
+ close(fd);
+ return;
+ }
+ if (twice) {
+ memset(data, 0x33, size); /* a first version, read through the mapping, then the second */
+ pwrite(fd, data, size, (off_t)offset);
+ for (size_t at = 0; at < size; at += page / 2) {
+ (void)*(volatile const unsigned char *)(map + offset + at);
+ }
+ }
+ for (size_t at = 0; touch && at < file_size; at += page) {
+ if (touch == 2 || at == (offset & ~(page - 1)) || at == ((offset + size - 1) & ~(page - 1))) {
+ (void)*(volatile const unsigned char *)(map + at);
+ }
+ }
+ if (wire) {
+ /* the mapping's pages held in memory, as a surface made around them holds them */
+ mlock(map, file_size);
+ }
+ memset(data, 0x11, size);
+ pwrite(fd, data, size, (off_t)offset);
+ for (size_t at = 0; at < size;) {
+ size_t next = ((offset + at) & ~(page - 1)) + page - offset, end = next < size ? next : size, bad = 0;
+
+ for (size_t i = at; i < end; i++) {
+ bad += map[offset + i] != 0x11;
+ }
+ if (bad != 0 && used < sizeof(line) - 40) {
+ used += (size_t)snprintf(line + used, sizeof(line) - used, " page %zu (bytes %zu-%zu): %zu", pages, at, end, bad);
+ }
+ changed += bad != 0;
+ pages++;
+ at = end;
+ }
+ printf("cow_test: file, %s (%zu bytes at %zu in %s): %zu of %zu pages of the mapping do not show what was written%s\n",
+ what, size, offset, path, changed, pages, line);
+ if (wire) {
+ munlock(map, file_size);
+ }
+ munmap((void *)map, file_size);
+ close(fd);
+ unlink(path);
+ free(data);
+ free(zero);
+}
+
+static void
+files(size_t size)
+{
+ five("mapped, nothing read, then written", "/private/var/tmp/cow_test.data", 4224, size, 0, 0, 0);
+ five("mapped, its first and last page read, then written", "/private/var/tmp/cow_test.data", 4224, size, 1, 0, 0);
+ five("mapped, all read, then written", "/private/var/tmp/cow_test.data", 4224, size, 2, 0, 0);
+ five("written, read through the mapping, written again", "/private/var/tmp/cow_test.data", 4224, size, 0, 1, 0);
+ five("written, read through the mapping, written again (aligned)", "/private/var/tmp/cow_test.data", 0, size, 0, 1, 0);
+ five("mapped, wired, then written", "/private/var/tmp/cow_test.data", 4224, size, 2, 0, 1);
+ five("written, read, wired, written again", "/private/var/tmp/cow_test.data", 4224, size, 0, 1, 1);
+}
+
int
-main(void)
+main(int argc, char **argv)
{
size_t size = 180 * 720, page = (size_t)getpagesize();
vm_address_t region = 0;
+ setvbuf(stdout, NULL, _IONBF, 0);
printf("cow_test: pages of %zu bytes\n", page);
+ files(size); /* (first: the rest makes surfaces, which a kernel that breaks may not survive) */
+ if (argc > 1 && strcmp(argv[1], "files") == 0) {
+ return 0;
+ }
vm_allocate(mach_task_self(), &region, size + 4 * page, VM_FLAGS_ANYWHERE);
one("a region of our own, page-aligned", (unsigned char *)region, size, 0);
one("a region of our own, 4224 bytes in", (unsigned char *)region + 4224, size, 0);
@@ -271,6 +411,9 @@ main(void)
three("OOL from a purgeable region, received, a surface made around it, then the source written (4224 in)",
(unsigned char *)p + 4224, size, 1);
}
+ four("a copy (MAP_MEM_VM_COPY), aligned", (unsigned char *)a, size, MAP_MEM_VM_COPY);
+ four("a copy (MAP_MEM_VM_COPY), 4224 in", (unsigned char *)a + 4224, size, MAP_MEM_VM_COPY);
+ four("a copy (MAP_MEM_VM_COPY), 2816 in", (unsigned char *)a + 2816, size, MAP_MEM_VM_COPY);
printf("cow_test: the source locked in memory (wired): mlock %d\n", mlock((void *)a, size + 4 * page));
two("wired source, aligned, source written after", (unsigned char *)a, (unsigned char *)b, size, 1);
two("wired source, 4224 in both, source written after", (unsigned char *)a + 4224, (unsigned char *)b + 4224, size, 1);
diff --git a/xnu/files/bsd/miscfs/ramfs/ramfs.c b/xnu/files/bsd/miscfs/ramfs/ramfs.c
index b167fb5..6510077 100644
--- a/xnu/files/bsd/miscfs/ramfs/ramfs.c
+++ b/xnu/files/bsd/miscfs/ramfs/ramfs.c
@@ -10,7 +10,9 @@
* are just two entries naming one node. File contents are kept in
* individually allocated pages hanging off the node. That copy is the truth:
* read() and write() go straight to it, and the page cache is used only for
- * mmap, with page-in and page-out copying between the two.
+ * mmap, with page-in and page-out copying between the two. After a write(),
+ * the pages of the page cache that hold what was written are brought up to
+ * date in place (ramfs_refresh_cache()).
*
* One mutex per mount guards all of it. It is never held while user memory
* is touched (a fault there could come back to this filesystem as a
@@ -443,6 +445,56 @@ ramfs_resize(struct ramfs_node *node, uint64_t size)
ramfs_touch(node, true);
}
+/*
+ * What a write() has changed, into the pages of the page cache that hold it, where there are such
+ * pages: each is filled anew from the file, in place. Throwing them away instead, for the next
+ * page-in to fetch again, is not enough: the page cache cannot throw away a page that somebody
+ * holds wired, and that page goes on showing the file as it was. A surface made around memory that
+ * maps a file wires the pages under it (MiniIOSurface.cpp; CoreAnimation's window server makes one
+ * around every picture it shows, and the pictures of the home screen's icons are memory that maps
+ * IconServices' cache, a file here). When SpringBoard wrote the Calendar's new icon into that
+ * cache, the pages of the icon that it shares with the icons next to it were still held by those
+ * icons' surfaces: they kept the old icon, in the top 16 rows and the bottom 4 of the new one, and
+ * SpringBoard's own mapping of the cache, being of those very pages, showed the same.
+ */
+static void
+ramfs_refresh_cache(vnode_t vp, struct ramfs_node *node, uint64_t start, uint64_t end)
+{
+ struct ramfs_mount *rm = node->rn_mount;
+
+ for (uint64_t page = trunc_page_64(start); page < end; page += PAGE_SIZE) {
+ upl_t upl = NULL;
+ upl_page_info_t *pl = NULL;
+ vm_offset_t addr;
+ int flags = 0;
+
+ if (ubc_page_op(vp, (off_t)page, 0, NULL, &flags) != KERN_SUCCESS) {
+ continue; /* not in the page cache: the next page-in fetches it */
+ }
+ if (ubc_create_upl_kernel(vp, (off_t)page, PAGE_SIZE, &upl, &pl, UPL_SET_LITE | UPL_WILL_MODIFY,
+ VM_KERN_MEMORY_FILE) != KERN_SUCCESS || upl == NULL) {
+ continue;
+ }
+ if (!upl_page_present(pl, 0) || ubc_upl_map(upl, &addr) != KERN_SUCCESS) {
+ /* (gone meanwhile: the page the request made is thrown away again) */
+ ubc_upl_abort_range(upl, 0, PAGE_SIZE, UPL_ABORT_FREE_ON_EMPTY);
+ continue;
+ }
+ lck_mtx_lock(&rm->rm_lock);
+ if (page < node->rn_size) {
+ size_t len = (size_t)MIN((uint64_t)PAGE_SIZE, node->rn_size - page);
+
+ ramfs_fetch(node, page, (void *)addr, len);
+ bzero((void *)(addr + len), PAGE_SIZE - len);
+ } else {
+ bzero((void *)addr, PAGE_SIZE);
+ }
+ lck_mtx_unlock(&rm->rm_lock);
+ ubc_upl_unmap(upl);
+ ubc_upl_commit_range(upl, 0, PAGE_SIZE, UPL_COMMIT_CLEAR_DIRTY | UPL_COMMIT_FREE_ON_EMPTY);
+ }
+}
+
/* A mapping may hold changes that have not been paged out to us yet: get them. */
static void
ramfs_sync_mapping(vnode_t vp, struct ramfs_node *node)
@@ -1000,10 +1052,9 @@ ramfs_write(struct vnop_write_args *ap)
if (new_size != old_size) {
(void)ubc_setsize(vp, (off_t)new_size);
}
- /* cached pages of the range are stale now; they will be paged in again when wanted */
+ /* cached pages of the range are stale now: brought up to date in place */
if (ubc_pages_resident(vp)) {
- (void)ubc_msync(vp, (off_t)trunc_page_64(start), (off_t)round_page_64((uint64_t)uio_offset(uio)), NULL,
- UBC_INVALIDATE);
+ ramfs_refresh_cache(vp, node, start, (uint64_t)uio_offset(uio));
}
/* a partial write is a success */
if (error == ENOSPC) {