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

sound: the volume a user sets is clamped; the ACF bounds, the amplifier stop and the guard level cannot wrap (docs/driver-review/sound.md)

shimoku13691bea0661
diff --git a/docs/driver-review/sound.md b/docs/driver-review/sound.md
index 59f9ff1..5c73fdc 100644
--- a/docs/driver-review/sound.md
+++ b/docs/driver-review/sound.md
@@ -253,3 +253,24 @@ userland/src/audio_plugin.c
- QEMU's virtio-sound device behaviour; the audio_plugin.c property/object plumbing outside I/O and volume.
- Timing, latency and audio quality (underflow behaviour of the 160 ms DSP queue, the 5 ms virtio poll).
- Nothing was built or executed; no sanitizer or fuzzing was run on the ACF parser.
+
+## Fix status
+
+Edits only in drivers/audio/hakios.{audio,sound,speaker,mic,virtio_sound}.c and drivers/storage/hakios.guard.c. Nothing was compiled or run.
+
+- S-01 FIXED. Three layers: audio.c audio_ioctl clamps the user's `below` to HAKIOS_AUDIO_BELOW_MAX (90 * 8 = speaker.c VOLUME_LOWEST, the documented "sound_below=720" floor); sound.c watch() now uses a saturating below_with(extra) instead of `below + extra` (three places), and guard_level() never passes a level equal to the HAKIOS_GUARD_SILENT sentinel; speaker.c hakios_speaker_volume() clamps `below` to VOLUME_LOWEST before `volume_at + below`, so the fade loop's target can no longer wrap under volume_at and the loop always ends. guard.c block(): `2 * attenuation` no longer wraps in 32 bits (the blind-guard range from item 5). Not done: an iteration limit on the fade loop (not needed once the target is in range) and a guard_level() clamp beyond the sentinel one.
+- S-02 FIXED with S-01 (clamp of `extra` in audio.c, saturating sum in sound.c).
+- S-03 FIXED. speaker.c acf_monitor(): both table-end checks are done in uint64; the `less` addition for the battery rows now saturates at 0xffff instead of wrapping.
+- S-04 FIXED (partly). speaker.c hakios_speaker_stop(): every failed step now goes through stop_failed(), which calls power_down_now() (both amplifiers, unless already broken), so a failed read in sysctrl() no longer leaves the amplifiers up. NOT done: take_down() in sound.c still ignores the result of the stop (it is a void function; changing it is a design change), but it now finds the amplifiers powered down or retained as "started" for the quiesce.
+- S-05 FIXED (partly). speaker.c power_down_now(): `started` is cleared only when the power-down write and read-back were acknowledged, so hakios_speaker_quiesce() still tries again. SKIPPED: the bus recovery (clocking out SCL pulses) in bus_take(); it needs knowledge of the I2C/amplifier hardware.
+- S-06 FIXED. mic.c: listen() now returns bool (false when the RUN is refused, a read cannot be sent, or no read comes back in 1.5 s); for_ios() counts `!path_up() || !listen()` as a failure, so the existing three-failure limit applies. Not done: counting "listen produced no samples" beyond those cases.
+- S-07 FIXED. sound.c space_on(): the three page-table pages are allocated once and re-zeroed on reuse, instead of a new kmem_alloc at every call.
+- S-08 SKIPPED. The fix is a change of the node mode (0666), which is policy and not to be changed here; the refcount of opens and the rate limit of the ioctl log are a design change.
+- S-09 SKIPPED. Changing the SoundWire read id range (and re-testing the FIFO) depends on how the hardware treats id 0 and an empty FIFO (NEEDS-HARDWARE).
+- S-10 SKIPPED. NEEDS-HARDWARE: what to do when the command FIFO stays full, and every caller would have to propagate a failure.
+- S-11 SKIPPED. NEEDS-HARDWARE: cache maintenance for the DSP's buffers depends on whether the stream is I/O-coherent.
+- S-12 FIXED. virtio_sound.c sound_pump(): a barrier after the idx load, and a used-ring id that is not one of the TX_BUFFERS buffers (or more entries than buffers) is dropped instead of used as an index.
+- S-13 SKIPPED. In hakios.adsp.c, which is not in this task's file list (someone else's).
+- S-14 SKIPPED. In userland/src/audio_plugin.c, outside the allowed files. Its fix (isnan) is still needed; the kernel clamp does not cover it.
+- S-15 SKIPPED. A pending-period retry mask in the feeder and mic loop is a design change.
+- S-16 FIXED. mic.c listen(): `stopping` now ends the loop for mic=1..3 too, so hakios_mic_dsp_down() is not held for mic_secs.
diff --git a/drivers/audio/hakios.audio.c b/drivers/audio/hakios.audio.c
index de97d92..2b5bbfc 100644
--- a/drivers/audio/hakios.audio.c
+++ b/drivers/audio/hakios.audio.c
@@ -253,6 +253,7 @@ audio_write(__unused dev_t dev, struct uio *uio, __unused int ioflag)
}
/* the speakers' volume, as iOS sets it (sys/hakios.audio.h): kept for the sink's driver to look at */
+#define HAKIOS_AUDIO_BELOW_MAX (90 * 8) /* eighths of a dB: the amplifiers' own lowest volume (speaker.c VOLUME_LOWEST) */
static uint32_t volume_below, volume_generation;
static bool volume_muted;
@@ -260,17 +261,24 @@ static int
audio_ioctl(__unused dev_t dev, u_long command, caddr_t data, __unused int flags, __unused struct proc *p)
{
const struct hakios_audio_volume *volume = (const struct hakios_audio_volume *)data;
+ uint32_t below;
if (command != HAKIOS_AUDIO_SET_VOLUME) {
return ENOTTY;
}
+ /*
+ * The amplifiers go no lower than -90 dB (hakios.speaker.c's VOLUME_LOWEST, the "mute volume"): a larger `below'
+ * means nothing there, and from user space (the node is open to all) it would make the sums made with it
+ * (sound_below + below) wrap round to a level far louder than the one asked for.
+ */
+ below = volume->below > HAKIOS_AUDIO_BELOW_MAX ? HAKIOS_AUDIO_BELOW_MAX : volume->below;
lck_mtx_lock(&audio_lock);
- volume_below = volume->below;
+ volume_below = below;
volume_muted = volume->muted != 0;
volume_generation++;
lck_mtx_unlock(&audio_lock);
- printf("hakios-audio: the volume: %s%u.%03u dB under the loudest\n", volume->muted ? "muted; " : "", volume->below / 8,
- volume->below % 8 * 125);
+ printf("hakios-audio: the volume: %s%u.%03u dB under the loudest\n", volume->muted ? "muted; " : "", below / 8,
+ below % 8 * 125);
return 0;
}
diff --git a/drivers/audio/hakios.mic.c b/drivers/audio/hakios.mic.c
index 5cdecde..88737f4 100644
--- a/drivers/audio/hakios.mic.c
+++ b/drivers/audio/hakios.mic.c
@@ -1546,9 +1546,10 @@ static uint32_t mic_secs = 10;
/*
* The stream run, read for mic_secs (for_ios: while iOS has the device open): how loud it is, every
* second (for iOS: the first three, then every 30th); into the recording with rec_sound_mic=1; for
- * iOS into the ring.
+ * iOS into the ring. False if it could not get going (the run refused, a read not sent, no read ever came
+ * back): not a failure that sets `broken', so for_ios() counts it from this.
*/
-static void
+static bool
listen(bool for_ios)
{
uint32_t run[3] = { 0, 0, 0 }, frames = 0, seconds = 0, peak = 0, periods = 0, empty = 0;
@@ -1565,13 +1566,13 @@ listen(bool for_ios)
if (!hakios_adsp_apr(APR_SVC_ASM, MIC_STREAM_PORT, 0, ASM_SESSION_CMD_RUN_V2, run, sizeof(run), APR_WAIT_MS, &answer) ||
answer.opcode != APR_BASIC_RSP_RESULT || answer.words[1] != 0) {
say("mic: the session, run: REFUSED OR NO ANSWER (%#x %#x)\n", answer.opcode, answer.words[1]);
- return;
+ return false;
}
say("mic: the session 2, run: done\n");
for (unsigned p = 0; p < MIC_PERIODS; p++) {
if (!mic_read(p)) {
say("mic: a read could not be sent\n");
- return;
+ return false;
}
}
if (for_ios) {
@@ -1594,13 +1595,14 @@ listen(bool for_ios)
hakios_haptic_pretend(0);
tap = 2;
}
- if ((for_ios ? (!ios_wants || stopping) : elapsed_ms >= mic_secs * 1000ull) || broken) {
+ /* (`stopping' ends every kind of run: hakios_mic_dsp_down() waits for mic_lock, which is held until this returns) */
+ if ((for_ios ? !ios_wants : elapsed_ms >= mic_secs * 1000ull) || stopping || broken) {
break;
}
if (!(read_ready & (1u << next))) {
if (elapsed_ms > 1500 && reads_done == 0) {
say("mic: NO READ HAS COME BACK in 1.5 s\n");
- break;
+ return false;
}
sleep_ms(2);
continue;
@@ -1674,6 +1676,7 @@ listen(bool for_ios)
squares = 0;
}
}
+ return true;
}
static void
@@ -1736,7 +1739,7 @@ for_ios(void)
say("mic: THE MICROPHONE IS IOS'S (/dev/hakios-mic) from here on, until the run ends: it listens only while iOS records\n");
while (!stopping) {
uint64_t began, ns;
- bool failed;
+ bool failed, up;
if (!ios_wants) {
sleep_ms(20);
@@ -1755,10 +1758,12 @@ for_ios(void)
began = mach_absolute_time();
say("mic: iOS records (%u): the path up%s\n", recordings, quiet ? " (the lines that say all went well are not said again)" :
"");
- if (path_up()) {
- listen(true);
- }
- failed = broken;
+ /*
+ * (a bring-up or a start that fails without setting `broken' counts as a failure too: otherwise the three-recordings
+ * limit below never applies and, while iOS keeps the device open, the path is brought up and undone for ever)
+ */
+ up = path_up() && listen(true);
+ failed = broken || !up;
absolutetime_to_nanoseconds(mach_absolute_time() - began, &ns);
say("mic: iOS's recording %u %s after %llu.%llu s; last: %s; %llu frames skipped as too late, %llu reads found too little\n",
recordings, failed ? "BROKE OFF" : "ended", ns / NSEC_PER_SEC, ns % NSEC_PER_SEC / (NSEC_PER_SEC / 10), last_write,
diff --git a/drivers/audio/hakios.sound.c b/drivers/audio/hakios.sound.c
index 9636fdf..add3dc8 100644
--- a/drivers/audio/hakios.sound.c
+++ b/drivers/audio/hakios.sound.c
@@ -632,7 +632,15 @@ space_on(void)
uint64_t *root, *second, *third, table_root;
uint32_t smr = SMMU_SMR(space.match), s2cr = SMMU_S2CR(space.match), sees = 1u << 31 | AUDIO_STREAM;
- if (kmem_alloc(kernel_map, &space.tables, 3 * 4096, KMA_KOBJECT | KMA_ZERO | KMA_DATA, VM_KERN_MEMORY_IOKIT) != KERN_SUCCESS) {
+ /*
+ * (the page tables are allocated once and reused: this runs at every first user of the DSP's buffers, and nothing frees
+ * the tables, so a new allocation each time would lose 12 KB of wired memory per recording or run)
+ */
+ if (space.tables != 0) {
+ bzero((void *)space.tables, 3 * 4096);
+ } else if (kmem_alloc(kernel_map, &space.tables, 3 * 4096, KMA_KOBJECT | KMA_ZERO | KMA_DATA, VM_KERN_MEMORY_IOKIT) !=
+ KERN_SUCCESS) {
+ space.tables = 0;
return false;
}
root = (uint64_t *)space.tables;
@@ -1213,7 +1221,22 @@ feeder_thread(__unused void *parameter, __unused wait_result_t result)
static void
guard_level(uint32_t level_below)
{
- hakios_guard_attenuation(level_below < guard_as ? level_below : guard_as);
+ uint32_t as = level_below < guard_as ? level_below : guard_as;
+
+ /* (HAKIOS_GUARD_SILENT means "the amplifiers are off": a level must never be taken for it, it would turn the power limits off) */
+ hakios_guard_attenuation(as >= HAKIOS_GUARD_SILENT ? HAKIOS_GUARD_SILENT - 1 : as);
+}
+
+/*
+ * sound_below and the volume iOS has set (`extra', eighths of a dB under it), added up: saturating, so that a
+ * huge sound_below or `extra' cannot wrap the sum round to a small number, that is to a loud level.
+ */
+static uint32_t
+below_with(uint32_t extra)
+{
+ uint64_t total = (uint64_t)below + extra;
+
+ return total > HAKIOS_GUARD_SILENT - 1 ? HAKIOS_GUARD_SILENT - 1 : (uint32_t)total;
}
/*
@@ -1264,19 +1287,19 @@ watch(void)
hakios_speaker_stop();
on = false;
}
- } else if (on && !hakios_speaker_volume(below + extra, 1)) {
+ } else if (on && !hakios_speaker_volume(below_with(extra), 1)) {
guard_off(true);
on = false;
given_up = true;
} else if (!given_up) {
- guard_level(below + extra);
+ guard_level(below_with(extra));
}
}
if (!on && !given_up && !muted && sounded != 0 && now - sounded < 100000) {
say_when("iOS plays: the amplifiers are started");
starts++;
ios_peak = 0;
- if (!hakios_speaker_start() || !hakios_speaker_volume(below + extra, 1)) {
+ if (!hakios_speaker_start() || !hakios_speaker_volume(below_with(extra), 1)) {
say("sound: THE AMPLIFIERS DID NOT START; iOS's sound goes on to the DSP, unheard\n");
guard_off(true);
given_up = true;
diff --git a/drivers/audio/hakios.speaker.c b/drivers/audio/hakios.speaker.c
index cf19161..86cf8c9 100644
--- a/drivers/audio/hakios.speaker.c
+++ b/drivers/audio/hakios.speaker.c
@@ -475,8 +475,11 @@ acf_monitor(uint16_t device, const uint8_t *block, uint32_t length)
vbat_count = le32(block + 0x3c);
vbat_size = le32(block + 0x40);
vbat_at = le32(block + 0x44);
+ /* (the end of each table is worked out in 64 bits: in 32 a crafted size or offset wraps and the check passes, and the
+ rows read below would then lie gigabytes outside the file's buffer) */
if (sum != le32(block) || temp_size < 12 || vbat_size < 12 || temp_count > MONITOR_ROWS || vbat_count > MONITOR_ROWS ||
- temp_at + temp_count * temp_size > length || vbat_at + vbat_count * vbat_size > length) {
+ (uint64_t)temp_at + (uint64_t)temp_count * temp_size > length ||
+ (uint64_t)vbat_at + (uint64_t)vbat_count * vbat_size > length) {
say("speaker: device %u's monitor table is not of the shape known: not used\n", device);
return;
}
@@ -499,7 +502,10 @@ acf_monitor(uint16_t device, const uint8_t *block, uint32_t length)
a->vbat_rows[i].max = (int16_t)(row[2] | row[3] << 8);
a->vbat_rows[i].less = (uint16_t)(row[6] | row[7] << 8);
if (vmax < 0 && per_eighth > 0) {
- a->vbat_rows[i].less += (uint16_t)((-(int64_t)vmax + per_eighth / 2) / per_eighth);
+ /* (saturating: a huge vmax in the file must not wrap the 16-bit reduction round to a small one) */
+ int64_t more = (-(int64_t)vmax + per_eighth / 2) / per_eighth;
+
+ a->vbat_rows[i].less = (uint16_t)(more > 0xffff - a->vbat_rows[i].less ? 0xffff : a->vbat_rows[i].less + more);
}
}
a->temp_count = temp_count;
@@ -638,9 +644,11 @@ power_down_now(const char *why)
value |= SYSCTRL_PWDN | SYSCTRL_AMPPD | SYSCTRL_HMUTE;
if (!register_write(a->address, AW_SYSCTRL, value) || !register_read(a->address, AW_SYSCTRL, &after)) {
say("speaker: the amplifier %s: 0x04 <- %#06x NOT ACKNOWLEDGED\n", a->where, value);
- } else {
- say("speaker: the amplifier %s: 0x04 <- %#06x, reads %#06x\n", a->where, value, after);
+ /* (`started' stays set: the amplifier may still be up, and hakios_speaker_quiesce() tries again only for an
+ amplifier that is not known to be off) */
+ continue;
}
+ say("speaker: the amplifier %s: 0x04 <- %#06x, reads %#06x\n", a->where, value, after);
a->started = false;
}
}
@@ -892,6 +900,12 @@ hakios_speaker_volume(uint32_t below, uint32_t step_ms)
{
uint16_t status;
+ /* Nothing is quieter than the driver's mute volume. Without this a huge `below' wraps volume_at + below to a level
+ under volume_at, which the fade loop below can never reach (set_volume() never goes louder than Android), so it
+ would spin forever with the amplifiers at their loudest. */
+ if (below > VOLUME_LOWEST) {
+ below = VOLUME_LOWEST;
+ }
for (unsigned i = 0; i < 2; i++) {
struct amplifier *a = &amplifiers[i];
uint32_t target = a->volume_at + below;
@@ -1029,6 +1043,19 @@ hakios_speaker_monitor(bool tell)
return wanted;
}
+/*
+ * A step of the stop sequence failed. A failed write has already powered both down (put()), but a failed read in
+ * sysctrl() has not: without this the stop gave up half way, with this amplifier (and the other, not yet reached)
+ * still up and the caller about to stop the clock they run on. Both powered down at once, as for any fault.
+ */
+static void
+stop_failed(void)
+{
+ if (!broken) {
+ power_down_now("a step of the stop failed");
+ }
+}
+
void
hakios_speaker_stop(void)
{
@@ -1043,20 +1070,24 @@ hakios_speaker_stop(void)
}
say("speaker: the amplifier %s is stopped as Android's driver stops it\n", a->where);
if (!sysctrl(a, SYSCTRL_ULS_HMUTE, true)) {
+ stop_failed();
return;
}
while (a->volume < VOLUME_LOWEST) {
if (!set_volume(a, (uint16_t)(a->volume + VOLUME_STEP > VOLUME_LOWEST ? VOLUME_LOWEST : a->volume +
VOLUME_STEP))) {
+ stop_failed();
return;
}
wait_ms(1);
}
if (!sysctrl(a, SYSCTRL_HMUTE, true) || !put(a, AW_I2SCTRL3, (uint16_t)(a->i2sctrl3 & ~I2SCTRL3_TXEN))) {
+ stop_failed();
return;
}
wait_ms(1);
if (!sysctrl(a, SYSCTRL_AMPPD, true) || !sysctrl(a, SYSCTRL_PWDN, true)) {
+ stop_failed();
return;
}
a->started = false;
diff --git a/drivers/audio/hakios.virtio_sound.c b/drivers/audio/hakios.virtio_sound.c
index 0614978..d80e48b 100644
--- a/drivers/audio/hakios.virtio_sound.c
+++ b/drivers/audio/hakios.virtio_sound.c
@@ -247,7 +247,17 @@ static void
sound_pump(void)
{
while (tx.seen != *(volatile uint16_t *)&tx.used->idx) {
- tx_free[tx_free_count++] = (uint16_t)(tx.used->ring[tx.seen++ % QUEUE_SIZE].id / 2);
+ uint32_t n;
+
+ /* (the device wrote the ring entry before it moved idx: read it only after idx, not ahead of it) */
+ __builtin_arm_dmb(0xb);
+ n = tx.used->ring[tx.seen++ % QUEUE_SIZE].id / 2;
+ /* (the id is the device's: one that is not one of our buffers, or more buffers than there are, would write outside
+ tx_free[] and the shared area below; such an entry is dropped) */
+ if (n >= TX_BUFFERS || tx_free_count >= TX_BUFFERS) {
+ continue;
+ }
+ tx_free[tx_free_count++] = (uint16_t)n;
}
while (tx_free_count != 0 && ring_tail - ring_head >= PERIOD_FRAMES) {
uint16_t n = tx_free[--tx_free_count];
diff --git a/drivers/storage/hakios.guard.c b/drivers/storage/hakios.guard.c
index 6b1a124..955be01 100644
--- a/drivers/storage/hakios.guard.c
+++ b/drivers/storage/hakios.guard.c
@@ -157,7 +157,10 @@ block(int16_t *frames, uint32_t count)
{
uint32_t attenuation = guard.attenuation;
struct hakios_guard_report *r = &guard.report;
- uint64_t to_speaker = attenuation == HAKIOS_GUARD_SILENT ? 0 : amplitude(2 * attenuation);
+ /* (2 * attenuation is kept from wrapping in 32 bits: a wrapped value says nothing about the level, and for some it is
+ a huge one, which makes the power limits see no power at all while the amplifiers are not turned down) */
+ uint64_t to_speaker = attenuation == HAKIOS_GUARD_SILENT ? 0 :
+ amplitude(attenuation > HAKIOS_GUARD_SILENT / 2 ? HAKIOS_GUARD_SILENT - 1 : 2 * attenuation);
uint64_t sum[2] = { 0, 0 }, g2 = ONE, g, start = guard.level, ceiling;
uint32_t binding = 0;