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
Calendar icon: what the broken picture is, and a copy-on-write test that rules the kernel's copies out
vcvkke1a67cde2134
diff --git a/userland/build.sh b/userland/build.shindex 8df64e1..b97cb83 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -108,6 +108,7 @@ if [ -d $IOS/rootfs/usr/lib ]; then$DYN -o obj/rwtouch src/rwtouch.c$DYN -framework CoreFoundation -lMobileGestalt -o obj/mg_test src/mg_test.c$DYN -o obj/cpu_test src/cpu_test.c+ $DYN -framework CoreFoundation -framework IOSurface -o obj/cow_test src/cow_test.c$DYN -fobjc-arc -framework Foundation -framework SystemConfiguration -o obj/net_test src/net_test.m# (the keychain answers only a program that has a keychain access group: signed with one, if ldid is there)$DYN -framework CoreFoundation -framework Security -o obj/keychain_test src/keychain_test.c &&@@ -140,6 +141,7 @@ if [ -d $IOS/rootfs/usr/lib ]; thenficp obj/hello_dyn obj/svc_test obj/surf_test obj/rwtouch obj/mg_test root/bin/[ -f obj/cpu_test ] && cp obj/cpu_test root/bin/ # (a prebuilt/ from before this program has none)+ [ -f obj/cow_test ] && cp obj/cow_test root/bin/[ -f obj/net_test ] && cp obj/net_test root/bin/[ -f obj/keychain_test ] && cp obj/keychain_test root/bin/[ -f obj/wifipw_test ] && cp obj/wifipw_test root/bin/diff --git a/userland/rc.jobs.calendar b/userland/rc.jobs.calendarindex f8a1a18..ad55581 100644--- a/userland/rc.jobs.calendar+++ b/userland/rc.jobs.calendar@@ -4,9 +4,16 @@# 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. The window server's copy of the new picture is right in-# the 16K pages that lie wholly inside it and still the picture without its rounded mask in the two-# pages it shares with the memory around it (the icon's 180 x 180 at 720 bytes a row starts 4224 bytes-# into a page: the first page holds rows 0-16, the last rows 176-179), and it stays so.+# 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./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 3diff --git a/userland/rc.jobs.cow b/userland/rc.jobs.cownew file mode 100644index 0000000..a27ecf3--- /dev/null+++ b/userland/rc.jobs.cow@@ -0,0 +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+# it writes should end with no page wrong.+/redwood/bin/cow_testdiff --git a/userland/src/cow_test.c b/userland/src/cow_test.cnew file mode 100644index 0000000..75dc163--- /dev/null+++ b/userland/src/cow_test.c@@ -0,0 +1,290 @@+/*+ * 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.+ */+#include <mach/mach.h>+#include <CoreFoundation/CoreFoundation.h>+#include <IOSurface/IOSurfaceRef.h>+#include <sys/mman.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <unistd.h>++struct sent {+ mach_msg_header_t header;+ mach_msg_body_t body;+ mach_msg_ool_descriptor_t data;+};++struct got {+ struct sent message;+ mach_msg_trailer_t trailer;+};++static void+one(const char *what, unsigned char *buffer, size_t size, int touch_neighbours)+{+ mach_port_t port;+ struct sent m;+ struct got g;+ kern_return_t kr;+ size_t page = (size_t)getpagesize(), changed = 0, pages = 0;+ char line[512] = "";+ size_t used = 0;++ mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);+ mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);+ memset(buffer, 0x11, size);+ memset(&m, 0, sizeof(m));+ m.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;+ m.header.msgh_size = sizeof(m);+ m.header.msgh_remote_port = port;+ m.body.msgh_descriptor_count = 1;+ m.data.address = buffer;+ m.data.size = (mach_msg_size_t)size;+ m.data.deallocate = 0;+ m.data.copy = MACH_MSG_VIRTUAL_COPY;+ m.data.type = MACH_MSG_OOL_DESCRIPTOR;+ kr = mach_msg(&m.header, MACH_SEND_MSG, sizeof(m), 0, MACH_PORT_NULL, 0, MACH_PORT_NULL);+ if (kr != KERN_SUCCESS) {+ printf("cow_test: %s: send failed %#x\n", what, kr);+ return;+ }+ if (touch_neighbours) {+ /* what is before and after it in its first and last page, written first */+ uintptr_t first = (uintptr_t)buffer & ~(page - 1), end = ((uintptr_t)buffer + size + page - 1) & ~(page - 1);++ memset((void *)first, 0x33, (uintptr_t)buffer - first);+ memset(buffer + size, 0x33, end - ((uintptr_t)buffer + size));+ }+ memset(buffer, 0x22, size);+ memset(&g, 0, sizeof(g));+ kr = mach_msg(&g.message.header, MACH_RCV_MSG, 0, sizeof(g), port, 0, MACH_PORT_NULL);+ if (kr != KERN_SUCCESS) {+ printf("cow_test: %s: receive failed %#x\n", what, kr);+ return;+ }+ {+ const unsigned char *got = g.message.data.address;+ 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;+ }+ printf("cow_test: %s (%zu bytes, %zu into its page, received %zu into its page): %zu of %zu pages show what was "+ "written after the send%s%s\n", what, size, (size_t)((uintptr_t)buffer & (page - 1)), (size_t)(start & (page - 1)),+ changed, pages, changed ? ":" : "", line);+ vm_deallocate(mach_task_self(), (vm_address_t)got, size);+ }+ mach_port_destroy(mach_task_self(), port);+}++/* vm_copy() into memory that is there already (what a picture's copy-on-write snapshot may be made with) */+static void+two(const char *what, unsigned char *from, unsigned char *to, size_t size, int write_after)+{+ size_t page = (size_t)getpagesize(), pages = 0, bad_pages = 0;+ char line[512] = "";+ size_t used = 0;+ kern_return_t kr;++ memset(from, 0x11, size);+ memset(to, 0x44, size);+ kr = vm_copy(mach_task_self(), (vm_address_t)from, size, (vm_address_t)to);+ if (write_after == 1) {+ memset(from, 0x22, size); /* the source written after the copy */+ } else if (write_after == 2) {+ memset(to + size / 2, 0x55, 1); /* the copy written in one place */+ }+ for (size_t at = 0; at < size;) {+ size_t next = (((uintptr_t)to + at) & ~(page - 1)) + page - (uintptr_t)to, end = next < size ? next : size, bad = 0;++ for (size_t i = at; i < end; i++) {+ bad += to[i] != 0x11 && !(write_after == 2 && i == size / 2);+ }+ 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);+ }+ bad_pages += bad != 0;+ pages++;+ at = end;+ }+ if (write_after == 2) {+ size_t leaked = 0;++ for (size_t i = 0; i < size; i++) {+ leaked += from[i] != 0x11;+ }+ used += (size_t)snprintf(line + used, sizeof(line) - used, "; the source changed in %zu bytes", leaked);+ }+ printf("cow_test: vm_copy %s (%zu bytes, from %zu into its page to %zu into its): %#x; %zu of %zu pages of the copy wrong%s\n",+ what, size, (size_t)((uintptr_t)from & (page - 1)), (size_t)((uintptr_t)to & (page - 1)), kr, bad_pages, pages, line);+}++/* sent out of line, received, the copy wired (as a surface around it would be), and only then the source written */+static void+three(const char *what, unsigned char *buffer, size_t size, int surface_mode)+{+ IOSurfaceRef surface = NULL;+ mach_port_t port;+ struct sent m;+ struct got g;+ size_t page = (size_t)getpagesize(), changed = 0, pages = 0;+ char line[512] = "";+ size_t used = 0;+ int locked;++ mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);+ mach_port_insert_right(mach_task_self(), port, port, MACH_MSG_TYPE_MAKE_SEND);+ memset(buffer, 0x11, size);+ memset(&m, 0, sizeof(m));+ m.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0) | MACH_MSGH_BITS_COMPLEX;+ m.header.msgh_size = sizeof(m);+ m.header.msgh_remote_port = port;+ m.body.msgh_descriptor_count = 1;+ m.data.address = buffer;+ m.data.size = (mach_msg_size_t)size;+ m.data.copy = MACH_MSG_VIRTUAL_COPY;+ m.data.type = MACH_MSG_OOL_DESCRIPTOR;+ mach_msg(&m.header, MACH_SEND_MSG, sizeof(m), 0, MACH_PORT_NULL, 0, MACH_PORT_NULL);+ memset(&g, 0, sizeof(g));+ mach_msg(&g.message.header, MACH_RCV_MSG, 0, sizeof(g), port, 0, MACH_PORT_NULL);+ {+ 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);+ if (!surface_mode) {+ locked = mlock((const void *)((uintptr_t)got & ~(page - 1)), ((start + size + page - 1) & ~(page - 1)) - (start & ~(page - 1)));+ } else {+ /* a surface made around the received copy, as CoreAnimation's window server does with a picture */+ CFMutableDictionaryRef d = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);+ long long address = (long long)start, w = 180, h = 180, row = 720, bpe = 4, alloc = (long long)size, format = 0;+ struct { CFStringRef key; long long *value; } values[] = { { CFSTR("IOSurfaceAddress"), &address },+ { CFSTR("IOSurfaceWidth"), &w }, { CFSTR("IOSurfaceHeight"), &h }, { CFSTR("IOSurfaceBytesPerRow"), &row },+ { CFSTR("IOSurfaceBytesPerElement"), &bpe }, { CFSTR("IOSurfaceAllocSize"), &alloc },+ { CFSTR("IOSurfacePixelFormat"), &format } };++ for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); i++) {+ CFNumberRef n = CFNumberCreate(NULL, kCFNumberLongLongType, values[i].value);++ CFDictionarySetValue(d, values[i].key, n);+ CFRelease(n);+ }+ surface = IOSurfaceCreate(d);+ locked = surface != NULL ? 0 : -1;+ CFRelease(d);+ }+ memset(buffer, 0x22, size);+ 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;+ }+ {+ size_t source_bad = 0;++ for (size_t i = 0; i < size; i++) {+ source_bad += buffer[i] != 0x22;+ }+ printf("cow_test: %s (wired or surface made: %d, 0 is yes): %zu of %zu pages of the received copy show what was written after; the source "+ "itself has %zu bytes that are not what was written to it%s\n", what, locked, changed, pages, source_bad, line);+ }+ }+ if (surface != NULL) {+ CFRelease(surface);+ }+ mach_port_destroy(mach_task_self(), port);+}++int+main(void)+{+ size_t size = 180 * 720, page = (size_t)getpagesize();+ vm_address_t region = 0;++ printf("cow_test: pages of %zu bytes\n", page);+ vm_allocate(mach_task_self(), ®ion, 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);+ one("a region of our own, 4224 bytes in, neighbours written first", (unsigned char *)region + 4224, size, 1);+ {+ vm_address_t a = 0, b = 0;++ vm_allocate(mach_task_self(), &a, size + 4 * page, VM_FLAGS_ANYWHERE);+ vm_allocate(mach_task_self(), &b, size + 4 * page, VM_FLAGS_ANYWHERE);+ memset((void *)b, 0x66, size + 4 * page); /* (the destination's pages in use already) */+ two("aligned", (unsigned char *)a, (unsigned char *)b, size, 0);+ two("aligned, source written after", (unsigned char *)a, (unsigned char *)b, size, 1);+ two("aligned, copy written after", (unsigned char *)a, (unsigned char *)b, size, 2);+ two("4224 in both", (unsigned char *)a + 4224, (unsigned char *)b + 4224, size, 0);+ two("4224 in both, source written after", (unsigned char *)a + 4224, (unsigned char *)b + 4224, size, 1);+ two("4224 in both, copy written after", (unsigned char *)a + 4224, (unsigned char *)b + 4224, size, 2);+ two("4224 and 2816", (unsigned char *)a + 4224, (unsigned char *)b + 2816, size, 1);+ three("received, made read-only and wired, then the source written (aligned)", (unsigned char *)a, size, 0);+ three("received, made read-only and wired, then the source written (4224 in)", (unsigned char *)a + 4224, size, 0);+ three("received, made read-only, a surface made around it, then the source written (aligned)", (unsigned char *)a, size, 1);+ three("received, made read-only, a surface made around it, then the source written (4224 in)", (unsigned char *)a + 4224, size, 1);+ {+ unsigned char *heap = malloc(size);++ three("malloc, received, made read-only, a surface made around it, then the source written", heap, size, 1);+ }+ {+ vm_address_t p = 0, q = 0;+ kern_return_t kp = vm_allocate(mach_task_self(), &p, size + 4 * page, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);+ kern_return_t kq = vm_allocate(mach_task_self(), &q, size + 4 * page, VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE);++ printf("cow_test: purgeable regions: %#x %#x\n", kp, kq);+ two("purgeable source, aligned, source written after", (unsigned char *)p, (unsigned char *)b, size, 1);+ two("purgeable source, 4224 in both, source written after", (unsigned char *)p + 4224, (unsigned char *)b + 4224, size, 1);+ two("purgeable both, 4224 in both, source written after", (unsigned char *)p + 4224, (unsigned char *)q + 4224, size, 1);+ two("purgeable destination, 4224 in both, source written after", (unsigned char *)a + 4224, (unsigned char *)q + 4224, size, 1);+ one("OOL from a purgeable region, 4224 bytes in", (unsigned char *)p + 4224, size, 0);+ three("OOL from a purgeable region, received, a surface made around it, then the source written (4224 in)",+ (unsigned char *)p + 4224, size, 1);+ }+ 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);+ one("OOL from a wired region, 4224 bytes in", (unsigned char *)a + 4224, size, 0);+ munlock((void *)a, size + 4 * page);+ printf("cow_test: the destination locked in memory (wired): mlock %d\n", mlock((void *)b, size + 4 * page));+ two("wired destination, 4224 in both, source written after", (unsigned char *)a + 4224, (unsigned char *)b + 4224, size, 1);+ munlock((void *)b, size + 4 * page);+ }+ {+ unsigned char *heap = malloc(size);++ one("malloc", heap, size, 0);+ one("malloc, neighbours written first", heap, size, 1);+ }+ return 0;+}