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
camera, torch, haptic: no read from a freed ring, the flashlight can always be turned off, a failed waveform load does not start the motor (docs/driver-review/camera-haptic-torch.md)
shimoku180006943807
diff --git a/docs/driver-review/camera-haptic-torch.md b/docs/driver-review/camera-haptic-torch.mdindex 5280e4a..f7f44f7 100644--- a/docs/driver-review/camera-haptic-torch.md+++ b/docs/driver-review/camera-haptic-torch.md@@ -225,3 +225,20 @@ Camera, user-space side- Behaviour with two simultaneous camera readers beyond what is said above; behaviour when `psci_smp_stop_others()` leaves a stopped CPU holding `ios_lock` or `hakios_smmu_lock` (the quiesce paths skip those locks when `leaving`, torch/haptic/camera all do).- Interplay with the audio DSP driver's use of the RPMh TCS 0 and with hakios.speaker.c / buttons for shared pins and the SPMI arbiter beyond the spin lock.- userland/src/inject_media.c (writes the torch node) and the camera ISP's float handling.++## Fix status++Nothing was built or run; each edit was re-read for syntax and types only.++- CAM-1: FIXED (hakios.camera.c). New `ios_readers` count (under `ios_lock`); `camdev_read` counts itself in the same lock hold that marks the place READING and takes `at` and the frame size from `ring`, `ring_place_bytes` and `ring_frame_bytes` there; it leaves the count (and `wakeup`s) in its final lock section. New `ios_drain_readers()` (waits under `ios_lock` with `msleep` until the count is 0) is called before `ring_free()` in `ios_stop()` and in `fake_session()`. Lock order: the drain holds only `ios_lock` (power is already down); nothing else nests with it. The optional "refresh `ios_last_read` while a read is in progress" was not done: the drain already makes an idle stop safe.+- CAM-2: SKIPPED. Capping the mode's pixels or fps for user-chosen modes changes the documented ABI (`HAKIOS_CAMERA_SENSOR` takes any mode), and the real mode sizes live in tables that are not in the tree. Replacing `give_up` with a fall back to the previous sensor and mode is a design change. Node mode is policy (see DEV-1).+- TORCH-1: FIXED (hakios.torch.c). `dark()` sets a `forcing` flag while it runs and `put()` ignores the `broken` latch only then (all other checks, the allow-list, the arbiter check and the read-back stay). `dark()` also tries all four steps instead of stopping at the first failure; a clean run does the same writes in the same order. Not done: the repeated dark() retry every 100 ms in `serve()` (a design change).+- TORCH-2: FIXED (hakios.torch.c). `light()` sets `lit = true` before the module-enable write; `dark()` still clears it only on success. In the torch=2 path, a failed `light(1000)` now calls `dark()` if `lit` is set. Quiesce is unchanged: it already calls `dark()` when `lit` is set, which is now true in the failing case.+- HAPTIC-1: FIXED (hakios.haptic.c). `loaded = level >= 3 && configure() && load_waveforms()` is evaluated apart from `level == 4`; level 5 calls `serve()` only when `loaded`, otherwise `give_up("the waveforms are not loaded")` (chip into reset).+- CAM-3: FIXED (hakios.camera.c). `vaf()` records `vaf_voted = on` before the RPMh request, as `rails()` does, so `af_down()` sends the "off" even if RPMh did not report done in time. Worst case is one extra harmless "off" request. Whether a late completion really happens is hardware behaviour.+- CAM-4: FIXED (hakios.camera.c). `power_step()` passes `delay_us` capped at 100000 to `sensor_delay()`, the same cap the sensor-register path uses.+- CAM-5: FIXED (hakios.camera.c). `ios_select()` rejects modes with `frame_length <= EXPOSURE_MARGIN`, `frame_length > FRAME_LINES_MAX`, width or height over 16384; `capture_sensor()` rejects width/height over 16384 and `frame_length <= 40` (EXPOSURE_MARGIN is defined later in the file, so the literal 40 as in `exposure()`). 16384 x 16384 keeps the 32-bit sizes and the SMMU third-level count in range.+- CAM-6: FIXED (hakios.camera.c). New `keep_lock` mutex (own mutex, not `ios_lock`, so the capture thread never waits on a user copy) held across the header and data writes in `keep_picture()`; never nested with `ios_lock`.+- HAPTIC-2: SKIPPED. A duty-cycle limit needs the motor's thermal limits (hardware) and is a policy change.+- TORCH-3: SKIPPED. The cap is per request by design; a hard on-time limit is a policy change.+- DEV-1: SKIPPED. Node mode 0666 is policy and was not to be changed.diff --git a/drivers/camera/hakios.camera.c b/drivers/camera/hakios.camera.cindex 0e3c470..e258296 100644--- a/drivers/camera/hakios.camera.c+++ b/drivers/camera/hakios.camera.c@@ -1266,7 +1266,8 @@ power_step(const struct ctab_entry *e, bool up)give_up("a power sequence not understood");return false;}- sensor_delay(e->delay_us);+ /* (capped at 100 ms, as the sensor tables' delays are: a corrupt table must not keep the rails up or spin for minutes) */+ sensor_delay(e->delay_us > 100000 ? 100000 : e->delay_us);return true;}@@ -2985,8 +2986,9 @@ vaf(bool on)values[1] = VRM_MODE_HPM;addresses[2] = base + VRM_ENABLE;values[2] = on;+ /* the vote is recorded before the request, as rails() does: one RPMh finishes after our wait must still be given back */+ vaf_voted = on;done = on ? rpmh_write(3, addresses, values) : rpmh_write(1, &addresses[2], &values[2]);- vaf_voted = on && done;say("camera: RPMh %s L9C (%#x) %s\n", done ? "took" : "DID NOT SAY IT TOOK", base, on ? "on at 2800 mV" : "off");return done;}@@ -3146,7 +3148,9 @@ capture_sensor(void)if (!PE_parse_boot_argn("cam_settle", &settle, sizeof(settle)) || settle == 0) {settle = mode->settle; /* (the module file's, for this mode) */}- if (mode->lanes < 1 || mode->lanes > (mode->cphy ? 3u : 4u) || mode->data_type != RAW10_DT || width % 4 != 0) {+ /* (sides over 16384 would wrap the 32-bit frame size below; a frame_length of 40 or less would wrap exposure()'s `- 40') */+ if (mode->lanes < 1 || mode->lanes > (mode->cphy ? 3u : 4u) || mode->data_type != RAW10_DT || width % 4 != 0 ||+ width > 16384 || height > 16384 || mode->frame_length <= 40) {say("camera: mode %u: %u lanes, data type %#x: not what this is written for\n", mode_n, mode->lanes, mode->data_type);return;}@@ -3278,6 +3282,8 @@ static const struct ctab_mode *ios_mode;static uint32_t ios_mode_n, ios_line_ns;static volatile bool ios_open, ios_streaming, ios_wanted; /* open; frames coming; a read wants them */static uint64_t ios_last_read, ios_given, ios_frames_done, ios_writes_late;+/* reads copying from the ring now (under ios_lock): a session's end waits for them before it frees the ring */+static unsigned ios_readers;static struct { uint32_t lines, gain, frame_lines; bool pending; uint32_t focus; bool focus_pending; } ios_ask;static struct { uint32_t lines, gain, frame_lines; uint64_t from; } ios_now, ios_next;/*@@ -3396,6 +3402,20 @@ ring_alloc(uint32_t frame_bytes)return true;}+/*+ * Waits until no read is copying from the ring. Every place is FREE by now, so no new read can start+ * one; this stops ring_free() from freeing the memory under a read that is mid-copy.+ */+static void+ios_drain_readers(void)+{+ lck_mtx_lock(&ios_lock);+ while (ios_readers != 0) {+ (void)msleep(&ios_readers, &ios_lock, PZERO, "hakios-camera-drain", NULL);+ }+ lck_mtx_unlock(&ios_lock);+}+/* The session's end, whatever ended it: the sensor's "off", the blocks, the supplies, the memory. */static voidios_stop(const char *why)@@ -3419,6 +3439,7 @@ ios_stop(const char *why)csiphy_down();titan_down();power_down();+ ios_drain_readers();ring_free();say("camera: the frames for iOS stopped (%s): %llu done, %llu given, %llu written over\n", why, ios_frames_done, ios_given,ios_writes_late);@@ -3765,6 +3786,7 @@ fake_session(void)}lck_mtx_unlock(&ios_lock);wakeup(&ios_frames_done);+ ios_drain_readers();ring_free();say("camera: the frames for iOS stopped (%s): %llu done, %llu given\n", !ios_open ? "closed" : ios_switch_to != 0 ?"another sensor asked for" : "nobody reads", ios_frames_done, ios_given);@@ -3805,6 +3827,8 @@ camdev_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)struct hakios_camera_frame f;uint64_t since = ios_ms();int b = -1, error = 0;+ vm_offset_t at = 0; /* the place's address and the frame's size, taken under the lock with the place */+ uint32_t frame_bytes = 0;lck_mtx_lock(&ios_lock);ios_last_read = ios_ms();@@ -3843,6 +3867,13 @@ camdev_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)ios_last_read = ios_ms();}place[b].state = PLACE_READING;+ /*+ * Counted as a reader in the same lock hold that chose the place (the ring is live while a place is READY):+ * a session's end waits for this read, so the ring is not freed under the copy below (a use-after-free).+ */+ ios_readers++;+ at = ring + (uint64_t)b * ring_place_bytes;+ frame_bytes = ring_frame_bytes;ios_given = place[b].sequence;bzero(&f, sizeof(f));memcpy(f.magic, HAKIOS_CAMERA_MAGIC, 8);@@ -3870,8 +3901,7 @@ camdev_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)error = uiomove((caddr_t)&f, (int)MIN(sizeof(f), (size_t)uio_resid(uio)), uio);if (error == 0 && uio_resid(uio) > 0) {- vm_offset_t at = ring + (uint64_t)b * ring_place_bytes;- uint32_t n = (uint32_t)MIN((uint64_t)ring_frame_bytes, (uint64_t)uio_resid(uio));+ uint32_t n = (uint32_t)MIN((uint64_t)frame_bytes, (uint64_t)uio_resid(uio));uint64_t t0 = mach_absolute_time(), t1, t2;@@ -3897,10 +3927,15 @@ camdev_read(__unused dev_t dev, struct uio *uio, __unused int ioflag)place[b].state = PLACE_READY;}ios_last_read = ios_ms();+ ios_readers--; /* (the copy is over: a session's end waiting to free the ring may go on) */lck_mtx_unlock(&ios_lock);+ wakeup(&ios_readers);return error;}+/* serialises the writers of `keep' (never held together with ios_lock) */+static LCK_MTX_DECLARE(keep_lock, &ios_lck_grp);+/* HAKIOS_CAMERA_KEEP: a picture for the host, in one piece of memory the loader hands over. */static intkeep_picture(const struct hakios_camera_command *c, struct uio *uio)@@ -3914,6 +3949,11 @@ keep_picture(const struct hakios_camera_command *c, struct uio *uio)if (keep == 0 || keep_bytes < want) {return ENOSPC; /* (the piece is made when the device is, while there is memory in one piece) */}+ /*+ * One writer at a time (its own mutex: ios_lock is the capture thread's and must not wait on a user copy):+ * two writers would interleave one picture's header with the other's data in `keep'.+ */+ lck_mtx_lock(&keep_lock);h = (struct cam_header *)keep;bzero(h, CAM_HEADER_BYTES);memcpy(h->magic, "RWCAM001", 8);@@ -3934,6 +3974,7 @@ keep_picture(const struct hakios_camera_command *c, struct uio *uio)loader_recording[0] = kvtophys(keep);loader_recording[1] = want;loader_recording[2] = (uint64_t)keep;+ lck_mtx_unlock(&keep_lock);say("camera: a picture from iOS for the host: %ux%u, format %u, %u bytes\n", c->a, c->b, c->d, c->bytes);return error;}@@ -4033,7 +4074,11 @@ ios_select(unsigned index, uint32_t mode_n)ios_mode_n = mode_n;ios_mode = &ctab_modes[mode_n];if (ios_mode->lanes < 1 || ios_mode->lanes > (ios_mode->cphy ? 3u : 4u) || ios_mode->data_type != RAW10_DT ||- ios_mode->width % 4 != 0 || ios_mode->fps_milli == 0 || ios_mode->frame_length == 0) {+ ios_mode->width % 4 != 0 || ios_mode->fps_milli == 0 || ios_mode->frame_length == 0 ||+ /* (a frame_length not above the margin makes `frame_length - EXPOSURE_MARGIN' wrap; sides over 16384 make the+ * 32-bit frame size wrap, so the ring and the IFE's writes would disagree) */+ ios_mode->frame_length <= EXPOSURE_MARGIN || ios_mode->frame_length > FRAME_LINES_MAX || ios_mode->width > 16384 ||+ ios_mode->height > 16384) {say("camera: mode %u is not what this is written for\n", mode_n);return false;}diff --git a/drivers/camera/hakios.torch.c b/drivers/camera/hakios.torch.cindex 85ab236..b19f57a 100644--- a/drivers/camera/hakios.torch.c+++ b/drivers/camera/hakios.torch.c@@ -67,6 +67,8 @@ extern void loader_log_putc(char c);static uint32_t level;static bool broken, leaving;+/* set only while dark() runs: the "turn off" writes may pass the `broken' latch (a lit LED must always get its off writes) */+static volatile bool forcing;static int say(const char *format, ...) __printflike(1, 2);@@ -327,7 +329,8 @@ put(uint8_t reg, uint8_t value)for (size_t i = 0; i < sizeof(writable_regs); i++) {allowed |= writable_regs[i] == reg;}- if (broken || !allowed || !flash.writable || flash.write == 0 || flash.id != 0x2ee) {+ /* (the latch does not stop dark(): after a failed write the final "off" would otherwise do nothing and the LED stay on) */+ if ((broken && !forcing) || !allowed || !flash.writable || flash.write == 0 || flash.id != 0x2ee) {give_up(allowed ? "a write that may not be made" : "a register that is not to be written");return false;}@@ -492,10 +495,13 @@ light(uint32_t permille)if (!configure() || !put_bits(FLASH_IRESOLUTION, 0x01, 0x00) || !put_bits(FLASH_ITARGET0, 0x7f, (uint8_t)code)) {return false;}- if (!lit && (!put(FLASH_MODULE_EN, 0x80) || !put_bits(FLASH_EN_LED_CTRL, 0x01, 0x01))) {- return false;+ if (!lit) {+ /* lit is set before the enabling writes: one that lands but fails its read-back must still get a dark() later */+ lit = true;+ if (!put(FLASH_MODULE_EN, 0x80) || !put_bits(FLASH_EN_LED_CTRL, 0x01, 0x01)) {+ return false;+ }}- lit = true;return true;}@@ -503,9 +509,15 @@ light(uint32_t permille)static booldark(void){- bool ok = put_bits(FLASH_EN_LED_CTRL, 0x01, 0x00) && put(FLASH_MODULE_EN, 0x00) && put_bits(FLASH_ITARGET0, 0x7f, 0x00) &&- put_bits(FLASH_SAFETY_TIMER0, 0x80, 0x00);-+ bool ok;++ /* every step is tried even if an earlier one failed, and none is stopped by `broken': a half-done "off" leaves the LED on */+ forcing = true;+ ok = put_bits(FLASH_EN_LED_CTRL, 0x01, 0x00);+ ok = put(FLASH_MODULE_EN, 0x00) && ok;+ ok = put_bits(FLASH_ITARGET0, 0x7f, 0x00) && ok;+ ok = put_bits(FLASH_SAFETY_TIMER0, 0x80, 0x00) && ok;+ forcing = false;lit = !ok && lit;return ok;}@@ -714,6 +726,9 @@ torch_thread(__unused void *parameter, __unused wait_result_t result)note("off again after %u s", secs);}show_registers();+ } else if (lit) {+ /* light() failed after it may have enabled the channel: switch it off again */+ (void)dark();}} else {serve();diff --git a/drivers/haptic/hakios.haptic.c b/drivers/haptic/hakios.haptic.cindex 944a32b..8650ba5 100644--- a/drivers/haptic/hakios.haptic.c+++ b/drivers/haptic/hakios.haptic.c@@ -937,6 +937,8 @@ serve(void)static voidhaptic_thread(__unused void *parameter, __unused wait_result_t result){+ bool loaded = false;+tlmm = ml_io_map(TLMM_PHYS, TLMM_SIZE);say("haptic: level %u: %s\n", level, level == 1 ? "a look, reading only" : level == 2 ? "the reset released, registers read" :level == 3 ? "the chip configured and its waveforms loaded, nothing played" : level == 4 ? "one effect played" :@@ -958,7 +960,9 @@ haptic_thread(__unused void *parameter, __unused wait_result_t result)}} else {show_registers();- if (level >= 3 && configure() && load_waveforms() && level == 4) {+ /* (kept apart from `level == 4' so that level 5 can tell that the waveforms are really in the RAM) */+ loaded = level >= 3 && configure() && load_waveforms();+ if (loaded && level == 4) {uint32_t effect = 1;PE_parse_boot_argn("haptic_effect", &effect, sizeof(effect));@@ -966,8 +970,11 @@ haptic_thread(__unused void *parameter, __unused wait_result_t result)effect = 1; /* (the short ones only) */}(void)play_effect(effect);- } else if (level == 5 && !broken) {+ } else if (level == 5 && !broken && loaded) {+ /* (not started when the waveforms did not load: it would play whatever the RAM held) */serve();+ } else if (level == 5 && !broken) {+ give_up("the waveforms are not loaded");}}say("haptic: done\n");