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

Safari draws pages: the screen is sRGB, not a wide-gamut one

vcvkk388da70a6c6e
diff --git a/README.md b/README.md
index 9ce31f4..41af7b6 100644
--- a/README.md
+++ b/README.md
@@ -150,8 +150,15 @@ AUDIO=pipewire qemu/run-ios.sh 1200 -display gtk > logs/qemu.log # или AUD
настроить его просит configd задание `com.redwood.wifi`, которое есть в каждом образе и без `wifi=1` ничего не
делает. Без прошивки ядро говорит об этом одной строкой и идёт дальше; без файла сети радио поднимается, но ни к
чему не подключается. В Safari при наборе в
- строке поиска появляются подсказки поисковика — они приходят уже по этой сети; сами страницы Safari открывает
- (замок и имя сайта в адресной строке), но не рисует: поле страницы остаётся чёрным, с GPU и без него.
+ строке поиска появляются подсказки поисковика — они приходят уже по этой сети; страницы Safari открывает и рисует
+ (yandex.ru с поиском, погодой и новостями дзена, example.com). Чёрными они были, пока экран считался экраном
+ широкой гаммы: у iPhone X в дереве устройств «artwork-display-gamut» = P3, MobileGestalt по нему отвечает
+ HasExtendedColorDisplay = да, и WebKit рисует плитки страницы в поверхности по десять бит на цвет ('w30r',
+ 'b3a8'). Таких наших поверхностей нет (ядро даёт вместо них BGRA, оконный сервер только её и показывает), и
+ CoreGraphics в них не рисовал — плитки оставались нулями. Теперь загрузчик пишет в дерево sRGB, как и есть у
+ нашего кадрового буфера, и WebKit рисует в BGRA (`xnuloader/loader.c`, `dt_identity()`; проверка —
+ `RC_JOBS=rc.jobs.safari`). Знаки из шрифтов, которых нет в образе системы (цветные эмодзи, китайский:
+ `ios165/select-system.py` оставляет их снаружи), выходят квадратиками с вопросом.
Настройки: если собрать малый образ с `WIFID=1` (`WIFID=1 GL=1 REST=1 userland/build.sh`), в системе работает и
wifid, служба Wi-Fi самой iOS, а ядро отвечает на её запросы (`bsd/dev/redwood_apple80211.c`). Тогда в строке
состояния есть значок Wi-Fi, в Настройках Wi-Fi включён, подключённая сеть названа, под ней список сетей,
@@ -236,7 +243,8 @@ XL_SECS=900 XL_CMDLINE="touchscreen=1 cpufreq=1 bootcpu=7 cpus=8" ./xl run #
| `applog=1` / `applog=2` | stderr приложений в журнал / плюс os_log всех процессов |
| `applog_name=ИМЯ[,ИМЯ]` | вместе с `applog=1`: os_log только тех программ, чьи имена файлов начинаются с одного из ИМЁН (так читают расширения приложений — виджеты, обои: их запускает launchd по просьбе других процессов, описания задания у нас для них нет) |
| `launchd_disable_all_timeouts=1` | параметр самого launchd: не убивать расширение приложения, которое не отметилось за 5 секунд после запуска («Extension is hanging on launch. Killing.»). Нужен только в QEMU, где при загрузке полсотни расширений стартуют одновременно на эмулируемом процессоре; на телефоне таких убийств нет |
-| `ustacks=ИМЯ ustacks_at=С` | через С секунд после запуска напечатать, где стоит каждый поток процесса ИМЯ (`ustacks_ms=МС` — повторять каждые МС, `ustacks_count=N` — N раз, `ustacks_threads=N` — только первые N потоков, `ustacks_tname=ТЕКСТ` — только потоки, в имени которых есть ТЕКСТ: тогда строки идут и в журнал загрузчика, с именем потока и временем); имена функций — `qemu/symstack.py ЖУРНАЛ ИМЯ`. `ustacks=top` — кто сколько занимал процессор |
+| `ustacks=ИМЯ ustacks_at=С` | через С секунд после запуска напечатать, где стоит каждый поток процесса ИМЯ (`ustacks_ms=МС` — повторять каждые МС, `ustacks_count=N` — N раз, `ustacks_threads=N` — только первые N потоков, `ustacks_tname=ТЕКСТ` — только потоки, в имени которых есть ТЕКСТ: тогда строки идут и в журнал загрузчика, с именем потока и временем; `ustacks_tname=*` — все потоки, и тоже в журнал загрузчика); имена функций — `qemu/symstack.py ЖУРНАЛ ИМЯ`. `ustacks=top` — кто сколько занимал процессор |
+| `surftrace=N surftrace_of=ИМЯ` | первые N поверхностей IOSurface (по умолчанию 24 при запуске) в журнал; с `surftrace_of` — только тех программ, чьи имена начинаются с ИМЕНИ (для WebKit — `com.apple.WebKit`), и строки идут в журнал загрузчика: что программа просила (ключи), какой порт сделала, кто нашёл поверхность по порту (оконный сервер — для слоя) и что в ней было в ту минуту и когда её не стало (сколько пикселей не нули) |
| `systrace=1` | печатать неудачные системные вызовы и аварии процессов (`systrace_name=ИМЯ` — только процессов с этим именем) |
| `fbscale=2` | рисовать в 2 пикселя на точку вместо 3 и растягивать на панель: в 2,25 раза меньше работы программному рендеру (на телефоне пик 28 кадров/с вместо 19), картинка мягче, сверху появляется красная служебная полоса SpringBoard |
| `fb_filter=0` | масштабировать картинку на панель «ближайшим соседом», как раньше. По умолчанию каждый пиксель панели смешивается из четырёх соседних пикселей картинки (билинейно): 1125×2436 на 1080×2400 «ближайший сосед» выбрасывает каждый 25-й столбец и каждую 67-ю строку, и буквы и наклонные линии там, где они попадают на выброшенные, выходят чуть кривыми. Цена — около 9 мс на кадр в потоке драйвера экрана вместо 4 (отдельный поток, частота кадров та же), если считает одно ядро |
diff --git a/userland/build.sh b/userland/build.sh
index be95c23..3375138 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -114,6 +114,8 @@ if [ -d $IOS/rootfs/usr/lib ]; then
$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 -framework CoreFoundation -framework CoreGraphics -framework IOSurface -o obj/cg_test src/cg_test.c
+ $DYN -o obj/shm_test src/shm_test.c
$DYN -framework CoreFoundation -framework CoreGraphics -framework ImageIO -o obj/snap_test src/snap_test.c
$DYN -fobjc-arc -framework Foundation -framework SystemConfiguration -o obj/net_test src/net_test.m
# (with an entitlement for "battery_test open URL", if ldid is there: src/battery_test.c)
@@ -154,6 +156,8 @@ if [ -d $IOS/rootfs/usr/lib ]; then
cp 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/cg_test ] && cp obj/cg_test root/bin/
+ [ -f obj/shm_test ] && cp obj/shm_test root/bin/
[ -f obj/snap_test ] && cp obj/snap_test root/bin/
[ -f obj/net_test ] && cp obj/net_test root/bin/
[ -f obj/battery_test ] && cp obj/battery_test root/bin/
diff --git a/userland/rc.jobs.safari b/userland/rc.jobs.safari
new file mode 100644
index 0000000..21edd2a
--- /dev/null
+++ b/userland/rc.jobs.safari
@@ -0,0 +1,15 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# Safari (RC_JOBS=rc.jobs.safari, with wifi=1): whether iOS takes the screen for one of extended colour (it must
+# not: WebKit then draws into surfaces of ten bits a colour that come out black), then yandex.ru and example.com
+# opened the way a link opens them (battery_test open URL: SpringBoard is asked to open it), for screenshots of what
+# Safari shows; Safari's layers, application state and web views are said in between (com.redwood.layers, when
+# libredwood-sb.dylib is in it: INJECT with MobileSafari on its line).
+/redwood/bin/mg_test HasExtendedColorDisplay
+/redwood/bin/sound_test springboard
+/redwood/bin/rwtouch wait 10
+/redwood/bin/battery_test open https://yandex.ru
+/redwood/bin/rwtouch wait 8 notify com.redwood.layers wait 15 notify com.redwood.layers wait 7
+/bin/ps -A
+/redwood/bin/battery_test open https://example.com
+/redwood/bin/rwtouch wait 25
+/bin/ps -A
diff --git a/userland/src/cg_test.c b/userland/src/cg_test.c
new file mode 100644
index 0000000..ac1c3d9
--- /dev/null
+++ b/userland/src/cg_test.c
@@ -0,0 +1,135 @@
+/*
+ * cg_test: CoreGraphics drawing into an IOSurface, the way WebKit's GPU process draws a web page's
+ * tiles (WebCore::IOSurface::createPlatformContext: CGIOSurfaceContextCreate), and the plain way
+ * for comparison (a bitmap context on the surface's locked memory). Says, for each, whether a red
+ * rectangle drawn and flushed ends up in the surface's memory.
+ */
+#include <CoreFoundation/CoreFoundation.h>
+#include <CoreGraphics/CoreGraphics.h>
+#include <IOSurface/IOSurfaceRef.h>
+#include <dlfcn.h>
+#include <spawn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/wait.h>
+
+extern char **environ;
+
+#define SIZE 256
+
+static CFNumberRef
+num(int value)
+{
+ return CFNumberCreate(NULL, kCFNumberIntType, &value);
+}
+
+static IOSurfaceRef
+make_surface(void)
+{
+ CFMutableDictionaryRef props = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,
+ &kCFTypeDictionaryValueCallBacks);
+ CFNumberRef n;
+ IOSurfaceRef surface;
+
+ n = num(SIZE); CFDictionarySetValue(props, kIOSurfaceWidth, n); CFRelease(n);
+ n = num(SIZE); CFDictionarySetValue(props, kIOSurfaceHeight, n); CFRelease(n);
+ n = num(4); CFDictionarySetValue(props, kIOSurfaceBytesPerElement, n); CFRelease(n);
+ n = num(SIZE * 4); CFDictionarySetValue(props, kIOSurfaceBytesPerRow, n); CFRelease(n);
+ n = num('BGRA'); CFDictionarySetValue(props, kIOSurfacePixelFormat, n); CFRelease(n);
+ n = num(SIZE * SIZE * 4); CFDictionarySetValue(props, kIOSurfaceAllocSize, n); CFRelease(n);
+ surface = IOSurfaceCreate(props);
+ CFRelease(props);
+ return surface;
+}
+
+/* how many pixels are red, and what the one at 10,10 is */
+static void
+look(const char *what, IOSurfaceRef surface)
+{
+ uint32_t seed = 0, red = 0, zero = 0, first;
+ const uint32_t *pixels;
+
+ IOSurfaceLock(surface, kIOSurfaceLockReadOnly, &seed);
+ pixels = IOSurfaceGetBaseAddress(surface);
+ for (int i = 0; i < SIZE * SIZE; i++) {
+ red += pixels[i] == 0xffff0000;
+ zero += pixels[i] == 0;
+ }
+ first = pixels[10 * SIZE + 10];
+ IOSurfaceUnlock(surface, kIOSurfaceLockReadOnly, &seed);
+ printf("cg_test: %s: %u of %u pixels red, %u zero, the one at 10,10 %#010x (seed %u)\n", what, red, SIZE * SIZE, zero, first,
+ IOSurfaceGetSeed(surface));
+}
+
+/* a second process looks the surface up by its number and says what it sees there */
+static void
+seen_elsewhere(const char *self, IOSurfaceRef surface)
+{
+ char number[16];
+ char *args[] = { (char *)self, "child", number, NULL };
+ pid_t pid;
+ int status;
+
+ snprintf(number, sizeof(number), "%u", IOSurfaceGetID(surface));
+ if (posix_spawn(&pid, self, NULL, NULL, args, environ) == 0) {
+ waitpid(pid, &status, 0);
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ if (argc == 3 && strcmp(argv[1], "child") == 0) {
+ IOSurfaceRef surface = IOSurfaceLookup((IOSurfaceID)atoi(argv[2]));
+
+ if (surface == NULL) {
+ printf("cg_test: (another process) surface %s not found\n", argv[2]);
+ return 1;
+ }
+ look("(another process)", surface);
+ return 0;
+ }
+ CGContextRef (*create)(IOSurfaceRef, size_t, size_t, size_t, size_t, CGColorSpaceRef, CGBitmapInfo) =
+ (CGContextRef (*)(IOSurfaceRef, size_t, size_t, size_t, size_t, CGColorSpaceRef, CGBitmapInfo))dlsym(RTLD_DEFAULT,
+ "CGIOSurfaceContextCreate");
+ CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
+ CGBitmapInfo info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little;
+ IOSurfaceRef surface = make_surface(), plain = make_surface();
+ CGContextRef context;
+ uint32_t seed = 0;
+
+ if (surface == NULL || plain == NULL) {
+ printf("cg_test: IOSurfaceCreate failed\n");
+ return 1;
+ }
+ printf("cg_test: surfaces %u and %u, %zu bytes a row; CGIOSurfaceContextCreate is %s\n", IOSurfaceGetID(surface),
+ IOSurfaceGetID(plain), IOSurfaceGetBytesPerRow(surface), create ? "there" : "NOT THERE");
+ look("before", surface);
+
+ /* WebKit's way */
+ if (create != NULL && (context = create(surface, SIZE, SIZE, 8, 32, space, info)) != NULL) {
+ CGContextSetRGBFillColor(context, 1, 0, 0, 1);
+ CGContextFillRect(context, CGRectMake(0, 0, SIZE, SIZE));
+ CGContextFlush(context);
+ look("CGIOSurfaceContext, filled and flushed", surface);
+ CGContextRelease(context);
+ look("CGIOSurfaceContext, released", surface);
+ seen_elsewhere(argv[0], surface);
+ } else {
+ printf("cg_test: CGIOSurfaceContextCreate gave no context\n");
+ }
+
+ /* the plain way */
+ IOSurfaceLock(plain, 0, &seed);
+ context = CGBitmapContextCreate(IOSurfaceGetBaseAddress(plain), SIZE, SIZE, 8, IOSurfaceGetBytesPerRow(plain), space, info);
+ if (context != NULL) {
+ CGContextSetRGBFillColor(context, 1, 0, 0, 1);
+ CGContextFillRect(context, CGRectMake(0, 0, SIZE, SIZE));
+ CGContextRelease(context);
+ }
+ IOSurfaceUnlock(plain, 0, &seed);
+ look("a bitmap context on the locked memory", plain);
+ seen_elsewhere(argv[0], plain);
+ return 0;
+}
diff --git a/userland/src/inject_sb.c b/userland/src/inject_sb.c
index 48d8866..3dce897 100644
--- a/userland/src/inject_sb.c
+++ b/userland/src/inject_sb.c
@@ -33,6 +33,7 @@
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <dispatch/dispatch.h>
+#include <fcntl.h>
#include <mach/mach_time.h>
#include <notify.h>
#include <objc/message.h>
@@ -45,6 +46,9 @@
#include <sys/sysctl.h>
#include <unistd.h>
+/* where what is said goes: the standard error, which is the console's for SpringBoard (an application's is nowhere) */
+static int say_fd = 2;
+
static void
say(const char *format, ...)
{
@@ -58,7 +62,7 @@ say(const char *format, ...)
if (n > (int)sizeof(line) - 1) {
n = sizeof(line) - 1;
}
- write(2, line, (size_t)n);
+ write(say_fd, line, (size_t)n);
}
/*
@@ -666,6 +670,38 @@ layer_line(id layer, const char *role, int depth)
}
}
+/* Every WKWebView under a view: whether WebKit takes it for one in the background, its web process and whether that answers. */
+static void
+web_views(id view, int depth)
+{
+ Class web = objc_getClass("WKWebView");
+ id subviews;
+ bool is_web = false;
+
+ if (view == NULL || depth > 40) {
+ return;
+ }
+
+ for (Class c = object_getClass(view); web != NULL && c != NULL && !is_web; c = class_getSuperclass(c)) {
+ is_web = c == web;
+ }
+ if (is_web) {
+ SEL background = sel_registerName("_isBackground"), pid = sel_registerName("_webProcessIdentifier");
+ SEL responsive = sel_registerName("_webProcessIsResponsive");
+
+ say("redwood: layers%s: a WKWebView %p: in the background %s, web process %d, responsive %s, in a window %s, hidden %s\n",
+ layers_of, (void *)view, class_respondsToSelector(object_getClass(view), background) ?
+ (SEND(BOOL, view, "_isBackground") ? "YES" : "no") : "?", class_respondsToSelector(object_getClass(view), pid) ?
+ SEND(int, view, "_webProcessIdentifier") : -1, class_respondsToSelector(object_getClass(view), responsive) ?
+ (SEND(BOOL, view, "_webProcessIsResponsive") ? "yes" : "NO") : "?", SEND(id, view, "window") ? "yes" : "NO",
+ SEND(BOOL, view, "isHidden") ? "YES" : "no");
+ }
+ subviews = SEND(id, view, "subviews");
+ for (long i = 0; subviews != NULL && i < SEND(long, subviews, "count"); i++) {
+ web_views(SEND1(id, subviews, "objectAtIndex:", long, i), depth + 1);
+ }
+}
+
static void
layers(void)
{
@@ -693,6 +729,13 @@ layers(void)
scene ? text_of(SEND(id, scene, "_persistenceIdentifier")) : "none", scene ? SEND(long, scene, "activationState") : -1L);
layer_line(SEND(id, w, "layer"), "", 1);
}
+ if (application != NULL) {
+ say("redwood: layers%s: the application's state %ld (0 active, 1 inactive, 2 in the background)\n", layers_of,
+ SEND(long, application, "applicationState"));
+ }
+ for (long i = 0; windows != NULL && i < SEND(long, windows, "count"); i++) {
+ web_views(SEND1(id, windows, "objectAtIndex:", long, i), 0);
+ }
say("redwood: layers%s: end\n", layers_of);
}
@@ -713,6 +756,10 @@ redwood_springboard_init(void)
snprintf(who, sizeof(who), " of %s", name);
layers_of = who;
+ if ((say_fd = open("/dev/console", O_WRONLY | O_NOCTTY)) < 0) {
+ say_fd = 2;
+ }
+ say("redwood: in %s[%d]: its layers are said on the notification com.redwood.layers\n", name, getpid());
notify_register_dispatch("com.redwood.layers", &token, dispatch_get_main_queue(), ^(__unused int t) {
layers();
});
diff --git a/userland/src/shm_test.c b/userland/src/shm_test.c
new file mode 100644
index 0000000..9e1b5be
--- /dev/null
+++ b/userland/src/shm_test.c
@@ -0,0 +1,117 @@
+/*
+ * shm_test: the two things WebKit's processes talk through besides messages, between two processes of ours
+ * (WebKit's IPC::StreamConnection: a shared buffer the web content process writes drawing commands into,
+ * and a mach semaphore it signals to wake the GPU process up):
+ * - memory shared through a named entry (mach_make_memory_entry_64, mapped on the other side with
+ * mach_vm_map, as WebKit's SharedMemory does): is what one side writes seen by the other?
+ * - a mach semaphore handed over as a port: does a signal on one side wake a wait on the other?
+ *
+ * shm_test [share] the test (share: the memory entry made as WebKit's SharedMemory makes it, with
+ * MAP_MEM_VM_SHARE); starts itself again as the second process
+ * shm_test child (internal) the second process: its ports come through mach_ports_register()
+ */
+#include <mach/mach.h>
+#include <spawn.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+extern char **environ;
+/* (the SDK's mach/mach_vm.h refuses to be included on iOS; the calls are there all the same) */
+extern kern_return_t mach_vm_allocate(vm_map_t, mach_vm_address_t *, mach_vm_size_t, int);
+extern kern_return_t mach_vm_map(vm_map_t, mach_vm_address_t *, mach_vm_size_t, mach_vm_offset_t, int, mem_entry_name_port_t,
+ memory_object_offset_t, boolean_t, vm_prot_t, vm_prot_t, vm_inherit_t);
+
+#define SIZE (2 << 20) /* WebKit's stream buffer is about this big */
+
+static int
+child(void)
+{
+ mach_port_array_t ports;
+ mach_msg_type_number_t count = 0;
+ mach_vm_address_t address = 0;
+ kern_return_t kr;
+ volatile uint32_t *words;
+
+ kr = mach_ports_lookup(mach_task_self(), &ports, &count);
+ if (kr != KERN_SUCCESS || count < 3) {
+ printf("shm_test child: mach_ports_lookup: %#x, %u ports\n", kr, count);
+ return 1;
+ }
+ kr = mach_vm_map(mach_task_self(), &address, SIZE, 0, VM_FLAGS_ANYWHERE, ports[0], 0, FALSE, VM_PROT_READ | VM_PROT_WRITE,
+ VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_NONE);
+ if (kr != KERN_SUCCESS) {
+ printf("shm_test child: mach_vm_map: %#x\n", kr);
+ return 1;
+ }
+ words = (volatile uint32_t *)address;
+ printf("shm_test child: the shared memory at %#llx says %#x %#x (the parent wrote 0x1234 and 0x5678)\n", address, words[0],
+ words[SIZE / 4 - 1]);
+ fflush(stdout);
+ words[1] = 0xc0ffee;
+ words[SIZE / 4 - 2] = 0xbeef;
+ kr = semaphore_signal(ports[1]);
+ printf("shm_test child: wrote 0xc0ffee and 0xbeef, signalled the semaphore: %#x; now waiting on the other one\n", kr);
+ fflush(stdout);
+ {
+ mach_timespec_t wait = { 3, 0 };
+
+ kr = semaphore_timedwait(ports[2], wait);
+ printf("shm_test child: the parent's signal %s (%#x)\n", kr == KERN_SUCCESS ? "came" : "DID NOT COME", kr);
+ }
+ return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+ mach_vm_address_t address = 0;
+ memory_object_size_t size = SIZE;
+ mach_port_t entry = MACH_PORT_NULL, wake = MACH_PORT_NULL, back = MACH_PORT_NULL, ports[3];
+ volatile uint32_t *words;
+ kern_return_t kr;
+ mach_timespec_t wait = { 3, 0 };
+ pid_t pid;
+ int status = 0;
+ char *args[] = { argv[0], "child", NULL };
+ bool share = argc == 2 && strcmp(argv[1], "share") == 0; /* "shm_test share": the entry made as WebKit makes it */
+
+ if (argc == 2 && strcmp(argv[1], "child") == 0) {
+ return child();
+ }
+ printf("shm_test: the memory entry made %s\n", share ? "with MAP_MEM_VM_SHARE | VM_PROT_IS_MASK, as WebKit's SharedMemory "
+ "makes it" : "plainly");
+ if ((kr = mach_vm_allocate(mach_task_self(), &address, SIZE, VM_FLAGS_ANYWHERE)) != KERN_SUCCESS ||
+ (kr = mach_make_memory_entry_64(mach_task_self(), &size, address, VM_PROT_READ | VM_PROT_WRITE | (share ? VM_PROT_IS_MASK |
+ MAP_MEM_VM_SHARE : 0), &entry, MACH_PORT_NULL))
+ != KERN_SUCCESS || (kr = semaphore_create(mach_task_self(), &wake, SYNC_POLICY_FIFO, 0)) != KERN_SUCCESS ||
+ (kr = semaphore_create(mach_task_self(), &back, SYNC_POLICY_FIFO, 0)) != KERN_SUCCESS) {
+ printf("shm_test: setting up: %#x\n", kr);
+ return 1;
+ }
+ words = (volatile uint32_t *)address;
+ words[0] = 0x1234;
+ words[SIZE / 4 - 1] = 0x5678;
+ ports[0] = entry;
+ ports[1] = wake;
+ ports[2] = back;
+ if ((kr = mach_ports_register(mach_task_self(), ports, 3)) != KERN_SUCCESS) {
+ printf("shm_test: mach_ports_register: %#x\n", kr);
+ return 1;
+ }
+ if (posix_spawn(&pid, argv[0], NULL, NULL, args, environ) != 0) {
+ printf("shm_test: cannot start the second process\n");
+ return 1;
+ }
+ kr = semaphore_timedwait(wake, wait);
+ printf("shm_test: the child's signal %s (%#x); the shared memory now says %#x %#x (the child writes 0xc0ffee and 0xbeef)\n",
+ kr == KERN_SUCCESS ? "came" : "DID NOT COME", kr, words[1], words[SIZE / 4 - 2]);
+ fflush(stdout);
+ semaphore_signal(back);
+ waitpid(pid, &status, 0);
+ printf("shm_test: %s\n", kr == KERN_SUCCESS && words[1] == 0xc0ffee && words[SIZE / 4 - 2] == 0xbeef ? "both work" :
+ "SOMETHING DOES NOT WORK");
+ return 0;
+}
diff --git a/xnu/files/iokit/Kernel/MiniIOSurface.cpp b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
index ab0e35a..5523e85 100644
--- a/xnu/files/iokit/Kernel/MiniIOSurface.cpp
+++ b/xnu/files/iokit/Kernel/MiniIOSurface.cpp
@@ -10,12 +10,15 @@
*/
#include <IOKit/IOLib.h>
+#include <pexpert/pexpert.h>
#include <IOKit/IOLocks.h>
#include <IOKit/IOKitKeys.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <libkern/c++/OSSerialize.h>
#include <libkern/c++/OSUnserialize.h>
#include <libkern/c++/OSDictionary.h>
+#include <libkern/c++/OSCollectionIterator.h>
+#include <libkern/c++/OSSymbol.h>
#include <libkern/c++/OSArray.h>
#include <libkern/c++/OSNumber.h>
#include <libkern/c++/OSBoolean.h>
@@ -91,6 +94,30 @@ static uint64_t gSurfaceLogged[2]; /* selectors alr
/* Boot-arg surftrace=N: log the first N surfaces created (default 24). */
static TUNABLE(uint32_t, gSurfaceTrace, "surftrace", 24);
static uint32_t gSurfaceTraced; /* (a tunable is read-only after boot) */
+/*
+ * Boot-arg surftrace_of=NAME: only the surfaces made by programs whose name begins with NAME count for
+ * surftrace (then up to N of theirs), and what becomes of them is said as well: the ports made for them
+ * and who looks them up from a port (another program, which shows it: the window server for a layer).
+ */
+static char gSurfaceTraceOf[32];
+
+extern "C" void loader_log_line(const char *line);
+
+/* A line about a traced surface: to the loader's log too, where it outlives the kernel's own, short log. */
+static void
+traceLine(const char * format, ...)
+{
+ char line[256];
+ va_list list;
+
+ va_start(list, format);
+ vsnprintf(line, sizeof(line), format, list);
+ va_end(list);
+ if (gSurfaceTraceOf[0] != 0) {
+ loader_log_line(line);
+ }
+ IOLog("%s", line);
+}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -245,6 +272,17 @@ MiniIOSurface::free(void)
{
uint64_t began = mach_absolute_time();
+ if (traced && kpixels != NULL) {
+ uint32_t nonzero = 0, middle = 0;
+
+ for (uint32_t at = 0; at + 4 <= info.allocSize; at += 64) {
+ nonzero += kpixels[at] != 0 || kpixels[at + 1] != 0 || kpixels[at + 2] != 0 || kpixels[at + 3] != 0;
+ }
+ memcpy(&middle, kpixels + (info.allocSize / 2 & ~3u), 4);
+ traceLine("MiniIOSurface: surface %u (%ux%u) goes: of every 64 bytes %u of %u begin with a pixel that is not 0, one in "
+ "the middle %#010x\n", sid, info.width, info.height, nonzero, info.allocSize / 64, middle);
+ }
+
OSSafeReleaseNULL(values);
OSSafeReleaseNULL(clientMap);
if (client != NULL) {
@@ -804,6 +842,9 @@ MiniIOSurfaceStart(IOService * provider)
gSurfaceLock = IOLockAlloc();
gSurfaceKeptLock = IOLockAlloc();
+ if (!PE_parse_boot_arg_str("surftrace_of", gSurfaceTraceOf, sizeof(gSurfaceTraceOf))) {
+ gSurfaceTraceOf[0] = 0;
+ }
gSurfaceClearing = thread_call_allocate(surfaceMemoryClear, NULL);
root = OSTypeAlloc(IOCoreSurfaceRoot);
if (root == NULL || !root->init() || !root->attach(provider)) {
@@ -1109,13 +1150,47 @@ IOSurfaceRootUserClient::methodCreate(IOExternalMethodArguments * args)
props->release();
return ret;
}
+ {
+ char procname[32] = "?";
+ static unsigned keysSaid;
+
+ proc_selfname(procname, sizeof(procname));
+ /* what a traced program asks for, key by key (the first few surfaces) */
+ if (gSurfaceTraceOf[0] != 0 && strncmp(procname, gSurfaceTraceOf, strlen(gSurfaceTraceOf)) == 0 && keysSaid++ < 6) {
+ OSCollectionIterator * keys = OSCollectionIterator::withCollection(props);
+ char line[256];
+ size_t used = 0;
+ OSSymbol * key;
+
+ line[0] = 0;
+ while (keys != NULL && (key = OSDynamicCast(OSSymbol, keys->getNextObject())) != NULL && used < sizeof(line)) {
+ OSNumber * n = OSDynamicCast(OSNumber, props->getObject(key));
+
+ used += (size_t) snprintf(line + used, sizeof(line) - used, " %s=%lld", key->getCStringNoCopy(),
+ n ? (long long) n->unsigned64BitValue() : -1LL);
+ }
+ OSSafeReleaseNULL(keys);
+ traceLine("MiniIOSurface: %s asks for a surface with%s\n", procname, line);
+ }
+ }
props->release();
- if (gSurfaceTraced < gSurfaceTrace) {
+ {
+ char procname[32] = "?";
+
+ proc_selfname(procname, sizeof(procname));
+ if (gSurfaceTraceOf[0] != 0 && strncmp(procname, gSurfaceTraceOf, strlen(gSurfaceTraceOf)) != 0) {
+ procname[0] = 0; /* (not one of those asked about: not counted) */
+ }
+ if (procname[0] != 0 && gSurfaceTraced < gSurfaceTrace) {
+ s->traced = gSurfaceTraceOf[0] != 0;
+ }
+ }
+ if (s->traced || (gSurfaceTraceOf[0] == 0 && gSurfaceTraced < gSurfaceTrace)) {
char procname[32] = "?";
gSurfaceTraced++;
proc_selfname(procname, sizeof(procname));
- IOLog("MiniIOSurface: %s[%d] created surface %u: %ux%u '%c%c%c%c', %u bytes per row, %u planes, %u bytes\n",
+ traceLine("MiniIOSurface: %s[%d] created surface %u: %ux%u '%c%c%c%c', %u bytes per row, %u planes, %u bytes\n",
procname, proc_selfpid(), s->sid, s->info.width, s->info.height, (int) (s->info.pixelFormat >> 24 & 0xff),
(int) (s->info.pixelFormat >> 16 & 0xff), (int) (s->info.pixelFormat >> 8 & 0xff),
(int) (s->info.pixelFormat & 0xff), s->info.bytesPerRow, s->info.planeCount, s->info.allocSize);
@@ -1205,6 +1280,16 @@ IOSurfaceRootUserClient::methodLookupFromPort(IOExternalMethodArguments * args)
ret = copyObjectForPortNameInTask(fTask, (mach_port_name_t) args->scalarInput[0], &obj);
right = OSDynamicCast(IOSurfaceSendRight, obj);
if (ret != kIOReturnSuccess || right == NULL || right->surface == NULL) {
+ static unsigned said;
+
+ if (said++ < 16) {
+ char procname[32] = "?";
+
+ proc_selfname(procname, sizeof(procname));
+ traceLine("MiniIOSurface: %s[%d] looked up a surface from port %#llx: %s\n", procname, proc_selfpid(),
+ args->scalarInput[0], ret != kIOReturnSuccess ? "no object behind the port" : obj ? "not a surface's port" :
+ "no surface");
+ }
OSSafeReleaseNULL(obj);
return kIOReturnBadArgument;
}
@@ -1216,6 +1301,26 @@ IOSurfaceRootUserClient::methodLookupFromPort(IOExternalMethodArguments * args)
}
ret = ref ? returnSurface(ref, known, args) : kIOReturnNoMemory;
IOLockUnlock(gSurfaceLock);
+ if (right->surface->traced) {
+ char procname[32] = "?";
+ uint32_t format = right->surface->info.pixelFormat, nonzero = 0, first = 0, middle = 0;
+ const uint8_t * pixels = right->surface->kpixels;
+ uint32_t size = right->surface->info.allocSize;
+
+ /* what is in it by now: how many of its bytes are not zero, its first pixel and one in the middle */
+ for (uint32_t at = 0; pixels != NULL && at < size; at += 64) {
+ nonzero += pixels[at] != 0 || pixels[at + 1] != 0 || pixels[at + 2] != 0 || pixels[at + 3] != 0;
+ }
+ if (pixels != NULL && size >= 8) {
+ memcpy(&first, pixels, 4);
+ memcpy(&middle, pixels + (size / 2 & ~3u), 4);
+ }
+ proc_selfname(procname, sizeof(procname));
+ traceLine("MiniIOSurface: %s[%d] looked up surface %u (%ux%u '%c%c%c%c') from a port: %#x; of every 64 bytes %u of %u "
+ "begin with a pixel that is not 0, the first %#010x, one in the middle %#010x\n", procname, proc_selfpid(),
+ right->surface->sid, right->surface->info.width, right->surface->info.height, (int) (format >> 24 & 0xff),
+ (int) (format >> 16 & 0xff), (int) (format >> 8 & 0xff), (int) (format & 0xff), ret, nonzero, size / 64, first, middle);
+ }
obj->release();
return ret;
}
@@ -1260,6 +1365,12 @@ IOSurfaceRootUserClient::methodCreatePort(IOExternalMethodArguments * args)
return kIOReturnNotFound;
}
name = makeSendRight(fTask, s);
+ if (s->traced) {
+ char procname[32] = "?";
+
+ proc_selfname(procname, sizeof(procname));
+ traceLine("MiniIOSurface: %s[%d] made a port for surface %u: %#x\n", procname, proc_selfpid(), s->sid, name);
+ }
MiniIOSurfaceDrop(s);
args->scalarOutput[0] = name;
args->scalarOutputCount = 1;
diff --git a/xnu/files/iokit/Kernel/MiniIOSurface.h b/xnu/files/iokit/Kernel/MiniIOSurface.h
index 21f1f0f..431423b 100644
--- a/xnu/files/iokit/Kernel/MiniIOSurface.h
+++ b/xnu/files/iokit/Kernel/MiniIOSurface.h
@@ -123,6 +123,7 @@ public:
OSDictionary * values;
uint32_t useCount[2];
uint32_t purgeable;
+ bool traced; /* made by a program of boot-arg surftrace_of: its ports are said too */
virtual bool
init(void) APPLE_KEXT_OVERRIDE
diff --git a/xnu/files/osfmk/arm64/user_exception_trace.c b/xnu/files/osfmk/arm64/user_exception_trace.c
index d97224c..2701fb2 100644
--- a/xnu/files/osfmk/arm64/user_exception_trace.c
+++ b/xnu/files/osfmk/arm64/user_exception_trace.c
@@ -172,6 +172,7 @@ static uint32_t ustacks_threads = USTACKS_MAX_THREADS; /* boot-arg ustacks_thre
* the window server's render-server and its other threads), each line with the thread's name and, in
* the first, the time in microseconds of mach_absolute_time (as the "tl:" lines of tools/latency.py);
* the lines are few enough then to go to the loader's log as well, where all of a long run is kept.
+ * ustacks_tname=* takes every thread, named or not, and sends them to the loader's log too.
*/
static char ustacks_tname[32];
static uint32_t ustacks_count; /* boot-arg ustacks_count=N: so many times, then no more (0: for ever) */
@@ -217,7 +218,8 @@ ustacks_task(task_t task, struct proc *proc)
bsd_getthreadname(get_bsdthread_info(thread), snap[count].name);
snap[count].name[63] = 0;
}
- if (ustacks_tname[0] != 0 && strnstr(snap[count].name, ustacks_tname, sizeof(snap[count].name)) == NULL) {
+ if (ustacks_tname[0] != 0 && strcmp(ustacks_tname, "*") != 0 &&
+ strnstr(snap[count].name, ustacks_tname, sizeof(snap[count].name)) == NULL) {
continue;
}
snap[count].tid = thread_tid(thread);
diff --git a/xnuloader/loader.c b/xnuloader/loader.c
index 67e6a22..d27a3b9 100644
--- a/xnuloader/loader.c
+++ b/xnuloader/loader.c
@@ -412,11 +412,22 @@ dt_prop_padded(struct dt_writer *w, const char *name, const char *value, uint32_
dt_prop(w, name, field, size);
}
-/* Properties copied from the iPhone's device tree (identity.h). */
+/*
+ * Properties copied from the iPhone's device tree (identity.h), but one: the iPhone X's display is a
+ * wide-gamut one ("artwork-display-gamut" P3), and with it iOS takes the screen for one that shows
+ * extended colour (MobileGestalt's HasExtendedColorDisplay): WebKit then draws a page's tiles into
+ * surfaces of ten bits a colour ('w30r', 'b3a8'), which neither the kernel's surfaces nor the window
+ * server's renderer here do (eight bits, BGRA), and every page comes out black. Here the screen is
+ * what the framebuffer is: sRGB.
+ */
static void
dt_identity(struct dt_writer *w, const struct identity_prop *props, uint32_t count, const unsigned char *data)
{
for (uint32_t i = 0; i < count; i++) {
+ if (str_eq(props[i].name, "artwork-display-gamut")) {
+ dt_prop(w, props[i].name, "sRGB", 5);
+ continue;
+ }
dt_prop(w, props[i].name, data + props[i].offset, props[i].length);
}
}