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
UFS: one poller for the doorbell, the completion bits put back, LUN 1-5 as well, a real hang tried
vcvkk17ba88e5542d
diff --git a/tools/flash-fingerprint.sh b/tools/flash-fingerprint.shindex 2bdd23a..cc0e783 100755--- a/tools/flash-fingerprint.sh+++ b/tools/flash-fingerprint.sh@@ -32,9 +32,12 @@ doneecho "userdata ($(readlink /dev/block/by-name/userdata)) first MB: $(dd if=/dev/block/by-name/userdata bs=1048576 count=1 2>/dev/null | sha256sum | cut -d' ' -f1)"echo "darwin ($(readlink /dev/block/by-name/darwin)) first 5628755968 bytes: $(dd if=/dev/block/by-name/darwin bs=4194304 count=1342 2>/dev/null | sha256sum | cut -d' ' -f1)"echo "files under /sdcard: $(find /sdcard/ -type f 2>/dev/null | wc -l)"+find /sdcard/ -type f 2>/dev/null | sort | sed 's/^/sdcard file: /'echo "android: boot_completed=$(getprop sys.boot_completed), /data: $(mount | grep ' /data ' | cut -d' ' -f1,5)"PHONE- mv "$2/android.txt.new" "$2/android.txt"+ grep '^sdcard file: ' "$2/android.txt.new" > "$2/sdcard-files.txt" || true+ grep -v '^sdcard file: ' "$2/android.txt.new" > "$2/android.txt"+ rm -f "$2/android.txt.new"grep -E '^(sd[a-f]:|userdata|darwin|files|android)' "$2/android.txt";;survey)diff --git a/xnu/files/bsd/dev/redwood_ufs.c b/xnu/files/bsd/dev/redwood_ufs.cindex 6a5403e..3d4d8be 100644--- a/xnu/files/bsd/dev/redwood_ufs.c+++ b/xnu/files/bsd/dev/redwood_ufs.c@@ -29,6 +29,7 @@#include <kern/clock.h>#include <kern/locks.h>#include <kern/thread.h>+#include <os/atomic_private.h>#include <libkern/libkern.h>#include <mach/mach_types.h>#include <mach/vm_param.h>@@ -39,6 +40,7 @@#include <sys/disk.h>#include <sys/errno.h>#include <sys/systm.h>+#include <sys/proc.h>#include <sys/sysctl.h>#include <sys/redwood_input.h>#include <libkern/crc.h>@@ -97,7 +99,8 @@ struct loader_block {} loader_native __attribute__((aligned(128)));struct {volatile uint32_t list_in; /* this kernel's: its list is in */- uint32_t switched_in, switched_out, reserved;+ uint32_t switched_in, switched_out;+ volatile uint32_t cnr_at_switch; /* UTRLCNR when its list went in (for the loader's take-back) */} kernel_native __attribute__((aligned(128)));};@@ -125,6 +128,7 @@ static uint64_t ufs_last_end;static uint8_t ufs_lun[BLOCK_MAX_PARTS]; /* 0xff: not known, read through the loader */static uint64_t ufs_lun_offset[BLOCK_MAX_PARTS];static bool ufs_native_mapped;+static uint32_t ufs_native_luns; /* how many units are mapped (LUN 0..n-1) */static volatile bool ufs_native_on;static uint64_t ufs_native_fallbacks, ufs_mailbox_refused;@@ -537,17 +541,21 @@ ufs_is_reference(const char *name, const char *ref)}static void-ufs_check(__unused void *arg, __unused wait_result_t wr)+ufs_check(void *arg, __unused wait_result_t wr){uint8_t *chunk = kalloc_data(1 << 20, Z_WAITOK);/** Not at once: what the kernel prints in its first minute or so does not come back with the* log on the phone (the first line that does is from long after this driver has started), so- * the check waits until it will be read.+ * the check waits until it will be read -- unless it runs right at the start (ufs_check=2, then+ * back to the loader: the log is short).*/- IOSleep(40000);- printf("ufs: check: %u partitions of the phone's storage, read through the loader's page\n", ufs_parts);+ if (arg == NULL) {+ IOSleep(40000);+ }+ printf("ufs: check: %u partitions of the phone's storage, read through %s\n", ufs_parts,+ ufs_native_on ? "this kernel's own UFS driver" : "the loader's page");for (uint32_t part = 0; chunk != NULL && part < ufs_parts; part++) {for (unsigned r = 0; r < sizeof(ufs_references) / sizeof(ufs_references[0]); r++) {uint64_t h = 0xcbf29ce484222325ULL, done = 0, started = mach_absolute_time(), ns;@@ -654,8 +662,8 @@ ufs_entry(const char *name)/** Which logical unit each unit the loader lists is (their device paths do not say): unit k is- * taken to be LUN k -- with ufs_native=1 only the unit darwin is on, which must be LUN 0 -- and it- * is so only if block 1 (the GPT header, with the unit's own GUID in it) read through the loader+ * taken to be LUN k (ordinary logical units only: never a well-known one), and it is so only if+ * block 1 (the GPT header, with the unit's own GUID in it) read through the loader* is the same as block 1 of that LUN read here; no other LUN is read. The loader's reads are made* first, with UEFI's list in; then this kernel's list goes in (with its proof) and stays in.*/@@ -684,6 +692,7 @@ ufs_native_map(bool all_units)}units[n++] = i;mask |= 1u << e->unit;+ ufs_native_luns = n;}if (good && !all_units && (n != 1 || ufs_page->part[units[0]].unit != 0)) {printf("ufs: native: darwin is not on the first unit the loader lists: not LUN 0, not read here\n");@@ -916,6 +925,89 @@ ufs_native_check(void)ufshc_report("after the check");}+/*+ * ufs_native_bench=1 (with ufs_native=1): what the flash gives with N reads in flight -- N+ * threads each reading at random places of darwin, a size at a time -- MB/s and microseconds a+ * read, for 1, 2, 4, 8, 16 and 31 threads. Then back to the loader.+ */+struct ufs_nb {+ uint32_t part, size, per_thread;+ uint64_t span, seed;+ volatile uint32_t left;+ int error;+};++static void+ufs_nb_thread(void *arg, __unused wait_result_t wr)+{+ struct ufs_nb *nb = arg;+ uint8_t *buf = kalloc_data(nb->size, Z_WAITOK);+ uint64_t seed = os_atomic_add(&nb->seed, 0x9e3779b97f4a7c15ULL, relaxed);++ for (uint32_t i = 0; buf != NULL && i < nb->per_thread; i++) {+ int e;++ seed = seed * 6364136223846793005ULL + 1442695040888963407ULL;+ e = ufs_read_native(nb->part, (seed >> 20) % (nb->span / nb->size) * nb->size, buf, nb->size);+ if (e != 0) {+ nb->error = e;+ }+ }+ if (buf != NULL) {+ kfree_data(buf, nb->size);+ }+ lck_mtx_lock(&ufs_lock);+ nb->left--;+ wakeup(nb);+ lck_mtx_unlock(&ufs_lock);+}++static void+ufs_native_bench(void)+{+ static const uint32_t sizes[] = { 4096, 32768, 131072, 524288 };+ static const uint32_t threads[] = { 1, 2, 4, 8, 16, 31 };+ uint32_t part = ufs_entry("darwin");++ if (part >= BLOCK_MAX_PARTS || ufs_lun[part] == 0xff) {+ printf("ufs: native bench: no darwin\n");+ return;+ }+ for (unsigned z = 0; z < sizeof(sizes) / sizeof(sizes[0]); z++) {+ for (unsigned t = 0; t < sizeof(threads) / sizeof(threads[0]); t++) {+ struct ufs_nb nb = {+ .part = part, .size = sizes[z], .span = ufs_part_bytes(part),+ .per_thread = (sizes[z] <= 32768 ? 8192 : 2048) / threads[t] + 1, .seed = 12345 + z * 100 + t,+ };+ uint64_t started = mach_absolute_time(), ns, bytes;++ nb.left = threads[t];+ for (uint32_t i = 0; i < threads[t]; i++) {+ thread_t th;++ if (kernel_thread_start(ufs_nb_thread, &nb, &th) == KERN_SUCCESS) {+ thread_deallocate(th);+ } else {+ lck_mtx_lock(&ufs_lock);+ nb.left--;+ lck_mtx_unlock(&ufs_lock);+ }+ }+ lck_mtx_lock(&ufs_lock);+ while (nb.left > 0) {+ msleep(&nb, &ufs_lock, PRIBIO, "ufsbench", NULL);+ }+ lck_mtx_unlock(&ufs_lock);+ absolutetime_to_nanoseconds(mach_absolute_time() - started, &ns);+ bytes = (uint64_t)nb.per_thread * threads[t] * sizes[z];+ printf("ufs: native bench: %6u bytes, %2u in flight: %4llu MB/s, %6llu reads/s, %5llu us a read%s\n",+ sizes[z], threads[t], ns ? bytes * 1000 / ns : 0, ns ? (bytes / sizes[z]) * 1000000000ULL / ns : 0,+ ns / 1000 * threads[t] / (bytes / sizes[z]), nb.error ? " (ERRORS)" : "");+ }+ }+ ufshc_report("after the bench");+}+/* kern.redwood.ufs_native: 1 while reads go through this kernel's own driver; 0 or 1 may be set */static intufs_sysctl_native(__unused struct sysctl_oid *oidp, __unused void *arg1, __unused int arg2, struct sysctl_req *req)@@ -1013,6 +1105,7 @@ redwood_ufs_init(void).take_back = &ufs_page->loader_native.take_back, .list_in = &ufs_page->kernel_native.list_in,.switched_in = (uint32_t *)&ufs_page->kernel_native.switched_in,.switched_out = (uint32_t *)&ufs_page->kernel_native.switched_out,+ .cnr_at_switch = &ufs_page->kernel_native.cnr_at_switch,};uint32_t mode = check, compare = 0;@@ -1030,7 +1123,7 @@ redwood_ufs_init(void)ufs_native_in(false);printf("ufs: native: dry run: the list stays in for the end of the run\n");}- } else if (ufs_native_map(compare != 0)) {+ } else if (ufs_native_map(true)) {ufs_native_mapped = true;if (compare != 0) {/* right here, in bsd_init's thread, and then back to the loader */@@ -1039,8 +1132,14 @@ redwood_ufs_init(void)loader_return();}ufs_native_on = true;- printf("ufs: the partitions on LUN 0 are read by this kernel's own UFS driver, its list in the controller "- "(kern.redwood.ufs_native=0: through the loader)\n");+ compare = 0;+ if (PE_parse_boot_argn("ufs_native_bench", &compare, sizeof(compare)) && compare != 0) {+ ufs_native_bench();+ redwood_ufs_report("after the bench");+ loader_return();+ }+ printf("ufs: the partitions (LUN 0-%u) are read by this kernel's own UFS driver, its list in the "+ "controller (kern.redwood.ufs_native=0: through the loader)\n", ufs_native_luns - 1);} else {printf("ufs: this kernel's own UFS driver is not used: through the loader\n");}@@ -1052,7 +1151,13 @@ redwood_ufs_init(void)loader_return();}check = 0;- if (PE_parse_boot_argn("ufs_check", &check, sizeof(check)) && check != 0) {+ if (PE_parse_boot_argn("ufs_check", &check, sizeof(check)) && check == 2) {+ /* right here, whichever way the partitions are read (ufs_native=), and back to the loader */+ ufs_check((void *)1, THREAD_AWAKENED);+ redwood_ufs_report("after the check");+ loader_return();+ }+ if (check == 1) {thread_t thread;if (kernel_thread_start(ufs_check, NULL, &thread) == KERN_SUCCESS) {diff --git a/xnu/files/bsd/dev/redwood_ufshc.c b/xnu/files/bsd/dev/redwood_ufshc.cindex 4b5d46e..c68595b 100644--- a/xnu/files/bsd/dev/redwood_ufshc.c+++ b/xnu/files/bsd/dev/redwood_ufshc.c@@ -50,6 +50,7 @@#include <kern/clock.h>#include <kern/locks.h>#include <kern/thread.h>+#include <os/atomic_private.h>#include <libkern/libkern.h>#include <stdarg.h>#include <mach/mach_types.h>@@ -127,7 +128,6 @@ extern boolean_t ml_set_interrupts_enabled(boolean_t enable);#define SLOT_BUFFER_DEFAULT (256u << 10)#define BLOCK 4096u /* the logical block size of every unit here */-#define SPIN_US 5000 /* look without sleeping this long, then a millisecond at a time */#define TIMEOUT_MS 10000struct hc_cmd {@@ -162,6 +162,8 @@ static uint32_t hc_is_in, hc_cnr_in; /* IS and UTRLCNR when this kernstatic uint64_t hc_requests, hc_errors, hc_bytes, hc_wait_us, hc_refused, hc_most_in_flight, hc_proofs;static uint64_t hc_switch_in_count, hc_switch_out_count;static uint32_t hc_in_flight;+/* where the time goes: doorbell reads while waiting, waits for a free slot, taking hc_lock, how many in flight */+static uint64_t hc_polls, hc_slot_waits, hc_slot_wait_ticks, hc_lock_ticks, hc_flight_hist[6];static LCK_GRP_DECLARE(hc_lck_grp, "redwood-ufshc");static LCK_MTX_DECLARE(hc_lock, &hc_lck_grp);@@ -535,6 +537,37 @@ hc_ring(struct hc_slot *s)return ok ? 0 : ENXIO;}+/*+ * ufs_native_hang=N (a test of the loader's side): once N reads are done, the thread that has just+ * rung a doorbell stops the kernel -- every processor spinning with interrupts off, this one+ * included -- with reads in flight and this kernel's list in. The heartbeat stops, and the+ * loader (or the watchdog) must take it from there.+ */+extern unsigned int cpu_broadcast_xcall(uint32_t *synch, boolean_t self_xcall, void (*func)(void *), void *parm);+static uint32_t hc_hang_after;+static volatile uint32_t hc_hanging;++static void+hc_spin_forever(__unused void *parm)+{+ (void)ml_set_interrupts_enabled(FALSE);+ for (;;) {+ __builtin_arm_yield();+ }+}++static void+hc_hang(uint32_t slot)+{+ static int parm;++ /* (into the loader's log directly as well: a hung kernel's own log never comes back to it) */+ hc_say_late("ufs: native: ufs_native_hang=%u: the kernel stops HERE, every processor spinning with interrupts "+ "off; slot %u just rung, doorbell %08x, %u reads in flight, this kernel's list in\n", hc_hang_after, slot,+ hc_read(UFSHC_UTRLDBR), hc_in_flight);+ cpu_broadcast_xcall(NULL, TRUE, hc_spin_forever, &parm);+}+/* Nothing more is sent from here on; whoever waits for a slot is told. */static voidhc_off(void)@@ -545,32 +578,95 @@ hc_off(void)lck_mtx_unlock(&hc_lock);}+/*+ * Waiting for the controller. Its interrupt stays off (UEFI's setting), so completions are seen+ * by reading UTRLDBR. A waiter looks at its own bit for a short while (hc_spin_us: a small read+ * is done by then), then sleeps; one poller thread looks for all who sleep and wakes each whose+ * slot is done. So the processors are not all taken by threads spinning on the doorbell, which+ * kept others -- one woken because the volume's lock came free, say -- from running.+ */+static volatile uint32_t hc_waiting; /* slots whose waiters sleep (hc_lock) */+static uint32_t hc_done; /* slots the poller has seen done, not yet collected (hc_lock) */+static bool hc_poller_asleep;+static uint32_t hc_spin_us = 40; /* boot-arg ufs_native_spin */+static uint64_t hc_sleeps, hc_poller_polls, hc_poller_rounds;++static void+hc_poller(__unused void *arg, __unused wait_result_t wr)+{+ for (;;) {+ uint32_t waiting, db, done;++ lck_mtx_lock(&hc_lock);+ while (hc_waiting == 0) {+ hc_poller_asleep = true;+ msleep((void *)&hc_waiting, &hc_lock, PRIBIO, "ufshc-poll", NULL);+ }+ hc_poller_asleep = false;+ waiting = hc_waiting;+ lck_mtx_unlock(&hc_lock);+ hc_poller_rounds++;+ /* until one of them is done, or another waiter comes */+ while (((db = hc_read(UFSHC_UTRLDBR)) & waiting) == waiting && hc_waiting == waiting) {+ hc_poller_polls++;+ __builtin_arm_yield();+ }+ done = waiting & ~db;+ if (done != 0) {+ lck_mtx_lock(&hc_lock);+ hc_done |= done;+ hc_waiting &= ~done;+ for (uint32_t m = done; m != 0; m &= m - 1) {+ wakeup(&hc_slots[__builtin_ctz(m)]);+ }+ lck_mtx_unlock(&hc_lock);+ }+ }+}+/* Until the controller is done with the slot's command; then whether it went well. */static inthc_complete(struct hc_slot *s, const char *what, bool tur){uint64_t started = mach_absolute_time(), us;volatile const uint8_t *r = (volatile const uint8_t *)s->ucd + UCD_RESPONSE;- uint32_t ocs;+ uint32_t ocs, bit = 1u << s->number;- while (hc_read(UFSHC_UTRLDBR) & (1u << s->number)) {- us = hc_us_since(started);- if (us < SPIN_US) {- __builtin_arm_yield();- continue;+ uint64_t polls = 1;++ while ((hc_read(UFSHC_UTRLDBR) & bit) && hc_us_since(started) < hc_spin_us) {+ polls++;+ __builtin_arm_yield();+ }+ if (hc_read(UFSHC_UTRLDBR) & bit) {+ struct timespec second = { 1, 0 };++ lck_mtx_lock(&hc_lock);+ hc_sleeps++;+ hc_waiting |= bit;+ if (hc_poller_asleep) {+ wakeup((void *)&hc_waiting);}- if (us > TIMEOUT_MS * 1000ULL) {- hc_say("ufs: native: slot %u (%s) not done in %u s (doorbell %08x, IS %08x, HCS %08x): "- "this path is off from now on\n", s->number, what, TIMEOUT_MS / 1000, hc_read(UFSHC_UTRLDBR),- hc_read(UFSHC_IS), hc_read(UFSHC_HCS));- hc_off();- return ETIMEDOUT;+ while ((hc_done & bit) == 0) {+ (void)msleep(s, &hc_lock, PRIBIO, "ufshc-io", &second);+ us = hc_us_since(started);+ if ((hc_done & bit) == 0 && us > TIMEOUT_MS * 1000ULL) {+ hc_waiting &= ~bit;+ lck_mtx_unlock(&hc_lock);+ hc_say("ufs: native: slot %u (%s) not done in %u s (doorbell %08x, IS %08x, HCS %08x): "+ "this path is off from now on\n", s->number, what, TIMEOUT_MS / 1000, hc_read(UFSHC_UTRLDBR),+ hc_read(UFSHC_IS), hc_read(UFSHC_HCS));+ hc_off();+ return ETIMEDOUT;+ }}- IOSleep(1);+ hc_done &= ~bit;+ lck_mtx_unlock(&hc_lock);}__asm__ volatile ("dsb sy" ::: "memory");+ os_atomic_add(&hc_polls, polls, relaxed);if (!tur) {- hc_wait_us += hc_us_since(started);+ os_atomic_add(&hc_wait_us, hc_us_since(started), relaxed);}ocs = s->utrd[2] & 0xff;if (ocs != OCS_SUCCESS || (r[0] & 0x3f) != UPIU_RESPONSE || r[3] != s->number || r[6] != 0 ||@@ -580,7 +676,7 @@ hc_complete(struct hc_slot *s, const char *what, bool tur)"sense key %x asc %02x ascq %02x\n", s->number, what, ocs, r[0], r[3], r[6], r[7],r[34 + 2] & 0x0f, r[34 + 12], r[34 + 13]);if (!tur) {- hc_errors++;+ os_atomic_inc(&hc_errors, relaxed);}return EIO;}@@ -591,19 +687,29 @@ static struct hc_slot *hc_take(bool wait){struct hc_slot *s = NULL;+ uint64_t t0 = mach_absolute_time();lck_mtx_lock(&hc_lock);- while (hc_free == 0 && wait && !hc_dead && hc_in) {- msleep(&hc_free, &hc_lock, PRIBIO, "ufshc", NULL);+ hc_lock_ticks += mach_absolute_time() - t0;+ if (hc_free == 0 && wait && !hc_dead && hc_in) {+ uint64_t w0 = mach_absolute_time();++ hc_slot_waits++;+ while (hc_free == 0 && wait && !hc_dead && hc_in) {+ msleep(&hc_free, &hc_lock, PRIBIO, "ufshc", NULL);+ }+ hc_slot_wait_ticks += mach_absolute_time() - w0;}if (hc_free != 0 && hc_in && !hc_dead) {- uint32_t i = (uint32_t)__builtin_ctz(hc_free);+ uint32_t i = (uint32_t)__builtin_ctz(hc_free), f;hc_free &= ~(1u << i);s = &hc_slots[i];- if (++hc_in_flight > hc_most_in_flight) {- hc_most_in_flight = hc_in_flight;+ f = ++hc_in_flight;+ if (f > hc_most_in_flight) {+ hc_most_in_flight = f;}+ hc_flight_hist[f <= 1 ? 0 : f <= 2 ? 1 : f <= 4 ? 2 : f <= 8 ? 3 : f <= 16 ? 4 : 5]++;}lck_mtx_unlock(&hc_lock);return s;@@ -672,6 +778,10 @@ ufshc_read(uint8_t lun, uint64_t offset, uint8_t *dst, uint64_t len)if (e == 0) {e = hc_ring(s);}+ if (e == 0 && hc_hang_after != 0 && hc_requests >= hc_hang_after &&+ os_atomic_cmpxchg(&hc_hanging, 0, 1, relaxed)) {+ hc_hang(s->number);+ }if (e != 0) {hc_give(s);error = e;@@ -698,8 +808,8 @@ ufshc_read(uint8_t lun, uint64_t offset, uint8_t *dst, uint64_t len)r = hc_complete(e->slot, what, false);if (r == 0) {FlushPoC_DcacheRegion((vm_offset_t)e->slot->data, (size_t)e->blocks * BLOCK);- hc_requests++;- hc_bytes += (uint64_t)e->blocks * BLOCK;+ os_atomic_inc(&hc_requests, relaxed);+ os_atomic_add(&hc_bytes, (uint64_t)e->blocks * BLOCK, relaxed);if (error == 0) {memcpy(e->to, e->slot->data + e->skip, e->take);}@@ -765,7 +875,7 @@ hc_prove(void)}struct hc_put_back_result {- uint32_t db_before, db_after_clear, lo, hi, rsr, db, is, cnr;+ uint32_t db_before, db_after_clear, lo, hi, rsr, db, is, cnr, cnr_ours;bool cleared, ok;};@@ -790,6 +900,11 @@ hc_put_back(struct hc_put_back_result *r)__builtin_arm_yield();}}+ /* the completion notifications this kernel's requests left: exactly those bits, written with ones */+ r->cnr_ours = hc_read(UFSHC_UTRLCNR) & ~hc_cnr_in;+ if (r->cnr_ours != 0) {+ hc_write(UFSHC_UTRLCNR, r->cnr_ours);+ }hc_write(UFSHC_UTRLRSR, 0);for (t = mach_absolute_time(); (hc_read(UFSHC_UTRLRSR) & 1) && hc_us_since(t) < 5000;) {__builtin_arm_yield();@@ -821,8 +936,9 @@ hc_put_back_print(const char *why, const struct hc_put_back_result *r, bool late"then %08x\n", why, r->db_before, r->db_after_clear);}say("ufs: native: %s: transfer list %s UEFI's %08x:%08x (run %u, doorbell %08x; IS %08x, at the switch %08x; "- "completion %08x, at the switch %08x)\n", why, r->ok ? "back to" : "NOT BACK TO", r->hi, r->lo, r->rsr, r->db,- r->is, hc_is_in, r->cnr, hc_cnr_in);+ "completion %08x after clearing this kernel's %08x, at the switch %08x%s)\n", why, r->ok ? "back to" :+ "NOT BACK TO", r->hi, r->lo, r->rsr, r->db, r->is, hc_is_in, r->cnr, r->cnr_ours, hc_cnr_in,+ r->cnr == hc_cnr_in ? ", as it was" : ", NOT AS IT WAS");}/*@@ -892,6 +1008,7 @@ ufshc_switch_in(bool prove)undone = true;} else {hc_in = true;+ *hc_loader.cnr_at_switch = hc_cnr_in;*hc_loader.list_in = 1;(*hc_loader.switched_in)++;hc_switch_in_count++;@@ -981,6 +1098,19 @@ hc_report(const char *when, bool late)"in flight, %llu refused, list in %llu times and out %llu times, %llu proofs%s\n", when, hc_requests, hc_errors,hc_bytes, hc_wait_us / 1000, hc_most_in_flight, hc_refused, hc_switch_in_count, hc_switch_out_count, hc_proofs,hc_dead ? "; THE PATH IS OFF" : "");+ {+ uint64_t wait_ns, lock_ns;++ absolutetime_to_nanoseconds(hc_slot_wait_ticks, &wait_ns);+ absolutetime_to_nanoseconds(hc_lock_ticks, &lock_ns);+ (late ? hc_say_late : hc_say)("ufs: native: %s: %llu doorbell reads while waiting (%llu a read), %llu waits for a "+ "free slot (%llu ms), %llu ms taking the slots' lock; reads put in with 1 in flight %llu, 2 %llu, 3-4 %llu, "+ "5-8 %llu, 9-16 %llu, more %llu\n", when, hc_polls, hc_requests ? hc_polls / hc_requests : 0, hc_slot_waits,+ wait_ns / 1000000, lock_ns / 1000000, hc_flight_hist[0], hc_flight_hist[1], hc_flight_hist[2],+ hc_flight_hist[3], hc_flight_hist[4], hc_flight_hist[5]);+ (late ? hc_say_late : hc_say)("ufs: native: %s: waiters spin %u us, then sleep: %llu slept; the poller %llu "+ "rounds, %llu doorbell reads\n", when, hc_spin_us, hc_sleeps, hc_poller_rounds, hc_poller_polls);+ }}void@@ -1020,6 +1150,7 @@ ufshc_probe(const struct ufshc_loader *loader, bool only)slots = SLOTS - 1;}PE_parse_boot_argn("ufs_native_leave", &hc_leave, sizeof(hc_leave));+ PE_parse_boot_argn("ufs_native_hang", &hc_hang_after, sizeof(hc_hang_after));if (loader->hc_pa != UFSHC_PHYS || loader->area_pa == 0 || loader->slots != SLOTS || (loader->area_pa & 0x3ff) ||loader->ucd_size < UCD_PRDT + 16 * (hc_slot_buffer / PRD_MAX + 1) || (loader->ucd_offset & 0x7f) ||(loader->ucd_size & 0x7f) || loader->ucd_offset < SLOTS * 32 ||@@ -1136,10 +1267,23 @@ ufshc_probe(const struct ufshc_loader *loader, bool only)return false;}}+ PE_parse_boot_argn("ufs_native_spin", &hc_spin_us, sizeof(hc_spin_us));+ if (hc_spin_us > 5000) {+ hc_spin_us = 5000;+ }+ {+ thread_t th;++ if (kernel_thread_start(hc_poller, NULL, &th) != KERN_SUCCESS) {+ hc_say("ufs: native: not used: no poller thread\n");+ return false;+ }+ thread_deallocate(th);+ }hc_nslots = slots;hc_free = (uint32_t)(((1ULL << (slots + 1)) - 1) & ~1ULL); /* slots 1..n */hc_usable = true;- hc_say("ufs: native: usable: a list of %u slots at %llx (the loader's), reads in %u of them, %u KB each, LUN 0 only%s\n",+ hc_say("ufs: native: usable: a list of %u slots at %llx (the loader's), reads in %u of them, %u KB each (the logical units reads may go to are set by redwood_ufs.c)%s\n",SLOTS, loader->area_pa, slots, hc_slot_buffer >> 10, hc_leave ? "; ufs_native_leave=1: the list is left in ""at the end, for the loader to take back" : "");return true;diff --git a/xnu/files/bsd/dev/redwood_ufshc.h b/xnu/files/bsd/dev/redwood_ufshc.hindex 6703ac9..404448f 100644--- a/xnu/files/bsd/dev/redwood_ufshc.h+++ b/xnu/files/bsd/dev/redwood_ufshc.h@@ -16,6 +16,7 @@ struct ufshc_loader {volatile uint32_t *take_back; /* the loader's: it takes the controller back, send nothing more */volatile uint32_t *list_in; /* this kernel's: its list is in */uint32_t *switched_in, *switched_out;+ volatile uint32_t *cnr_at_switch; /* this kernel's: UTRLCNR when its list went in */};bool ufshc_probe(const struct ufshc_loader *loader, bool only);diff --git a/xnuloader/loader.c b/xnuloader/loader.cindex 22602a3..a0448dc 100644--- a/xnuloader/loader.c+++ b/xnuloader/loader.c@@ -1518,7 +1518,8 @@ struct loader_block {/* written by the kernel: its list is in the controller (for the loader's log) */struct {volatile uint32_t list_in;- uint32_t switched_in, switched_out, reserved;+ uint32_t switched_in, switched_out;+ volatile uint32_t cnr_at_switch; /* UTRLCNR when the kernel's list went in */} kernel_native __attribute__((aligned(128)));};@@ -1692,6 +1693,18 @@ hc_take_back(const char *when)print("ufs: %s: doorbell %x after the clear\n", when, (uint64_t)db);}}+ /* the completion notifications the kernel's requests left (it says what there was when its list went in) */+ flush_dcache((uint64_t)(uintptr_t)&block_shared.kernel_native,+ (uint64_t)(uintptr_t)&block_shared.kernel_native + sizeof(block_shared.kernel_native));+ if (block_shared.kernel_native.switched_in != 0) {+ uint32_t cnr = hc_rd(HC_UTRLCNR), ours = cnr & ~block_shared.kernel_native.cnr_at_switch;++ if (ours != 0) {+ hc_wr(HC_UTRLCNR, ours);+ }+ print("ufs: %s: completion %x, at the kernel's switch %x: cleared %x, now %x\n", when, (uint64_t)cnr,+ (uint64_t)block_shared.kernel_native.cnr_at_switch, (uint64_t)ours, (uint64_t)hc_rd(HC_UTRLCNR));+ }hc_wr(HC_UTRLRSR, 0);for (t = READ_SYSREG(CNTVCT_EL0); (hc_rd(HC_UTRLRSR) & 1) && hc_ms_since(t) < 100;) {}