Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ jobs:
run: sh tools/modem/shannon-test.sh
- name: The Galaxy S9 Mali's GPU addresses for panfrost's buffers (hakios.mali_va.h) hold together (sanitizers)
run: sh tools/mali-va-test.sh
- name: The framebuffer's sweetfb_nowait present state machine (hakios.sweetfb_present.h) shows only finished frames, releases each swap once and never strands one, over random schedules (sanitizers)
run: sh tools/sweetfb-present-test.sh
- name: The Galaxy S9 secure-OS probe's DER reader (hakios.exynos_tee_der.h) keeps inside what the keymaster TA sends, mutated or cut short (sanitizers)
run: sh tools/exynos-tee-der-test.sh
- name: The rule that holds the Galaxy S9's M3s at 1794 MHz unless the over-current guard's stage two allows more (hakios.exynos_cpufreq_ceiling.h) (sanitizers)
Expand Down
55 changes: 55 additions & 0 deletions docs/ios27/PRESENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,58 @@ is today's path exactly.
- The vsync timer still free-runs; under Stage A it could follow the DECON's FRAME_COUNT (a tick per refresh) so that a
frame never waits for a flip it just missed. Not built: the counters will say if the waits matter.
- A panic under Stage A draws into the boot framebuffer, which is not shown: the panic is in the log, not on the screen.

## 6. The tick that does not sleep (`sweetfb_nowait=1`, built; NOT run on a phone in this form)

**Why.** While swiping the home screen (phone 1, 17 Nov, `sweetfb_prof=1`) the vsync tick of the framebuffer stand-in
(fork `iokit/Kernel/arm/SweetFramebuffer.cpp`, `vsyncFired`) took the newest frame and, its GPU chains still running, slept
until they were done before presenting it, holding the timer meanwhile: "tick slept for the GPU avg 11-12 ms", "ticks avg
20.0-20.3 ms" instead of 16.7. The tick is the display's cadence; it must not wait for the GPU.

**The states of a frame** (one SwapEnd). The state machine is `drivers/display/sweetfb/hakios.sweetfb_present.h` (plain C,
copied into the kernel tree by `profiles/ios-27.copy`), host-tested by `tools/sweetfb-present-test.sh`.

| state | entered by | who | under |
|---|---|---|---|
| queued | the window server queues the frame's chains (`starlte_gpu_user_submit2`, async) | backboardd's render thread | `sgu_q_lock` |
| recorded | `swap()` (SwapEnd): the record and the number of the last chain queued (`starlte_gpu_user_seq`) | the same thread, after the hold below | `fLock` (`sfp_swap`) |
| done | the queue's worker retires the frame's last chain and stores `sgu_q_done` | `sgu_q_worker`, woken by the job interrupt with `sgu_irq=1`, else by its naps | `sgu_q_lock` (an atomic store; read without a lock by `starlte_gpu_user_done`) |
| shown | the first vsync tick that finds `done >= seq` takes it (`sfp_tick` = SHOW), then presents it outside the lock | the timer (the framebuffer's work loop) | `fLock` to take, none to present |
| released | after the present (relbuf, when on), or at once when a newer swap replaces it while recorded | the tick, or `swap()` | none |

- A tick sends the vsync callback first, on every tick, whatever is recorded (unchanged), and never sleeps when the frame is
not done: it leaves it recorded (`deferred`). Only a finished frame is shown.
- At most one frame is recorded: a newer swap replaces the older (`replaced`), which is released then; so a frame is released
exactly once, by the swap that replaces it or after the tick that shows it.
- The SwapEnd hold (at most two frames in flight: `swap()` returns once the chains of the swap before it are done) depends on
GPU completion only, not on the present, so a present that is later or never cannot keep it: nothing to leak.
- Display off: the tick fades and takes nothing; the recorded frame stays. Display on: `sfp_redraw` records the newest frame
again (already released: nothing to release), as `fDirty = true` did before.
- **Self-check.** A frame that has waited `SFP_IDLE_TICKS` (4) ticks while nothing is queued or running on the GPU cannot be
unfinished: that is a logic error (a done number not seen). The tick then takes it for the old path (sleeps for its chains,
bounded at 3 s, then presents), counts it and logs `A5XELTE FB nowait: SELF-CHECK: ...` once. A frame that has waited
`SFP_MAX_TICKS` (30, half a second) with the GPU busy is taken the same way ("GPU slow"). A wrong state machine is a log
line and a late frame, never a black screen.
- Without `sweetfb_nowait=1` the tick is the old sleeping path, unchanged.

**What the host test proves** (random schedules of swaps with 1-12 chains, the two-in-flight hold, in-order GPU completions
at random rates, GPU stalls, ticks, display off/on; 300 seeds x 3 modes under ASan/UBSan): SHOW only ever takes a frame whose
chains are done; frames are shown in order; every swap is released exactly once; a recorded frame is taken within 31 lit
ticks; with the GPU drained nothing is left recorded (no lost wake-up); the self-check never fires when the done number is
honest and does fire, with the display going on, when it is stuck. Mutations (no tick counting; showing without the done
check) make it fail.

**The first attempt** (xnu PR 43, 17 Nov) cleared `fDirty`, copied the record, looked at the GPU outside the lock and put
the frame back. Its log on phone 1 (`nwt1`) shows the mechanism working: in both five-second windows with a frame left for a
later tick ("ticks that left a frame ... 1"), that frame was presented by a later tick (`frames=2`), nothing on the GPU
afterwards. The boot then stopped producing swaps at the Apple logo after swap 6 (backboardd not blocked in the
framebuffer: no thread in `swap()` or the tick), the same picture as the `a-g455` boot of the same evening WITHOUT
`sweetfb_nowait` (GPU at 455 MHz: last swap 3, two frames). So the black screen is not shown to be the nowait logic; it may
be a boot-timing stall that a different GPU or present timing exposes. The rewrite removes the window in which a frame was
neither recorded nor being presented, and adds the self-check, so if the stall comes back with this build the console says
whether the framebuffer is holding a frame (the self-check line, the nowait counters) or simply never given one.

**Phone test** (phone 1, the current owner line plus `sweetspeed` with 16384, `sweetfb_prof=1`, `sweetfb_nowait=1`), look
for, while swiping: `A5XELTE FB prof2: ticks avg ~16667 us`; `tick slept for the GPU avg` near 0; the `nowait:` line with
`frames shown done` = the frames, `SELF-CHECK 0`, `GPU slow 0`. Black screen or no swaps after the logo: note whether a
`SELF-CHECK` line came; the same boot line without `sweetfb_nowait=1` is the fallback.
124 changes: 124 additions & 0 deletions drivers/display/sweetfb/hakios.sweetfb_present.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* SPDX-License-Identifier: MIT-0 */
/*
* The framebuffer stand-in's present state machine for sweetfb_nowait=1 (docs/ios27/PRESENT.md, "The tick that does not
* sleep"): which frame a vsync tick shows, without the tick ever sleeping for the GPU. Plain C over integers, no kernel: the
* kernel's iokit/Kernel/arm/SweetFramebuffer.cpp (the xnu fork, STARLTE) calls it under its fLock, and
* tools/sweetfb_present_test.c drives it on a host through every order of swaps, GPU completions, ticks and display off/on.
*
* A frame (one SwapEnd) is recorded with the number of the last GPU chain queued when it came: it is finished once the GPU
* queue's "done" number (written by the queue's worker when it retires a chain, bsd/dev/starlte_gpu.c) has reached it.
*
* recorded --(a newer swap before it is shown)--> replaced: released at once
* recorded --(a tick: done >= its seq)----------> shown: taken by that tick, presented, released
* recorded --(a tick: not done)-----------------> stays recorded (the tick only counts it; the vsync callback still goes out)
* recorded --(waited SFP_IDLE_TICKS ticks with the GPU queue empty, or SFP_MAX_TICKS ticks)--> taken by that tick for the
* old path: the tick sleeps for its chains as without sweetfb_nowait, then presents it (a "fallback", counted;
* with the GPU idle it is a logic error and logged once)
*
* At most one frame is recorded at a time, so a frame is released exactly once: by the swap that replaces it, or after the
* tick that takes it. NOT run on a phone when written (18 Nov 2026).
*/
#ifndef HAKIOS_SWEETFB_PRESENT_H
#define HAKIOS_SWEETFB_PRESENT_H

#include <stdbool.h>
#include <stdint.h>

#define SFP_IDLE_TICKS 4 /* ticks a recorded frame may wait with nothing on the GPU before the self-check acts */
#define SFP_MAX_TICKS 30 /* ticks (half a second) a recorded frame may wait at all before the old path takes it */

enum sfp_action {
SFP_NOTHING = 0, /* nothing recorded: the tick only sends the vsync callback */
SFP_WAIT, /* a frame recorded, its chains not done: shown by a later tick */
SFP_SHOW, /* take the recorded frame: its chains are done */
SFP_FALLBACK, /* take the recorded frame and sleep for its chains (the old path) */
};

struct sfp {
bool recorded; /* a frame is waiting to be shown (the framebuffer's fDirty) */
uint32_t id; /* its swap id (0: a redraw of a frame already shown and released) */
uint64_t seq; /* the last GPU chain queued when it came (0: none to wait for) */
uint64_t last_seq; /* the newest frame's seq, for a redraw */
uint32_t waited; /* ticks it has waited */
/* counters (the caller prints and clears them) */
uint32_t deferred; /* ticks that left a frame for a later one */
uint32_t replaced; /* frames a newer swap replaced before they were shown */
uint32_t shown; /* frames ticks took as done */
uint32_t fallbacks; /* frames taken for the old path after SFP_MAX_TICKS (the GPU slow) */
uint32_t idle_fallbacks;/* ... after SFP_IDLE_TICKS with the GPU queue empty: a logic error */
bool warned; /* the logic error has been logged once */
};

/* A swap: the new frame is recorded. Returns the swap id of the frame it replaced, to be released (0: none). */
static inline uint32_t
sfp_swap(struct sfp *s, uint32_t id, uint64_t seq)
{
uint32_t replaced = s->recorded ? s->id : 0;
if (s->recorded) {
s->replaced++;
}
s->recorded = true;
s->id = id;
s->seq = seq;
s->last_seq = seq;
s->waited = 0;
return replaced;
}

/* The display back on: the newest frame drawn again (it was shown and released already: nothing to release for it). */
static inline void
sfp_redraw(struct sfp *s)
{
if (s->recorded) {
return; /* (a frame not yet shown is newer: it is the one to draw) */
}
s->recorded = true;
s->id = 0;
s->seq = s->last_seq;
s->waited = 0;
}

static inline bool
sfp_done(uint64_t seq, uint64_t done)
{
return seq == 0 || done >= seq;
}

/*
* A tick that may show a frame. `done`: the GPU queue's done number now; `gpu_idle`: nothing queued or running. On SFP_SHOW
* and SFP_FALLBACK the frame is no longer recorded: *id and *seq say which it was (the caller presents it, then releases *id
* when not 0).
*/
static inline enum sfp_action
sfp_tick(struct sfp *s, uint64_t done, bool gpu_idle, uint32_t *id, uint64_t *seq)
{
*id = 0;
*seq = 0;
if (!s->recorded) {
return SFP_NOTHING;
}
enum sfp_action act;
if (sfp_done(s->seq, done)) {
act = SFP_SHOW;
s->shown++;
} else if (gpu_idle && s->waited >= SFP_IDLE_TICKS) {
act = SFP_FALLBACK;
s->idle_fallbacks++;
} else if (s->waited >= SFP_MAX_TICKS) {
act = SFP_FALLBACK;
s->fallbacks++;
} else {
s->waited++;
s->deferred++;
return SFP_WAIT;
}
*id = s->id;
*seq = s->seq;
s->recorded = false;
s->id = 0;
s->seq = 0;
s->waited = 0;
return act;
}

#endif /* HAKIOS_SWEETFB_PRESENT_H */
1 change: 1 addition & 0 deletions drivers/map
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ media/mfc bsd/dev/hakios.exynos_mfc_test.h
media/mfc bsd/dev/hakios.mfc_check.h
display/panel bsd/dev/hakios.s6e3ha8.c
display/panel bsd/dev/hakios.s6e3ha8_gamma.h
display/sweetfb iokit/Kernel/arm/hakios.sweetfb_present.h
display/panel bsd/dev/hakios.s6e3ha8_star2.h
display/panel iokit/Kernel/hakios.backlight.cpp
gpu iokit/Kernel/hakios.gpu.cpp
Expand Down
1 change: 1 addition & 0 deletions profiles/ios-27.copy
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ drivers/input/ssp/hakios.bcm_shub.h bsd/dev/hakios.bcm_shub.h
drivers/core/hakios.physmap.cpp iokit/Kernel/hakios.physmap.cpp
drivers/display/panel/hakios.s6e3ha8_gamma.h bsd/dev/hakios.s6e3ha8_gamma.h
drivers/display/panel/hakios.s6e3ha8_star2.h bsd/dev/hakios.s6e3ha8_star2.h
drivers/display/sweetfb/hakios.sweetfb_present.h iokit/Kernel/arm/hakios.sweetfb_present.h
drivers/usb/hakios.exynos_usb.c bsd/dev/hakios.exynos_usb.c
drivers/usb/hakios.usb_acm.h bsd/dev/hakios.usb_acm.h
drivers/usb/hakios.usb_glue.h bsd/dev/hakios.usb_glue.h
Expand Down
1 change: 1 addition & 0 deletions tools/arch-layers.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ codec drivers/input/ssp/hakios.bcm_shub.h
codec drivers/display/panel/hakios.s6e3ha8_gamma.h
codec drivers/display/panel/hakios.s6e3ha8_star2.h
codec drivers/gpu/mali/hakios.mali_va.h
codec drivers/display/sweetfb/hakios.sweetfb_present.h
codec drivers/storage/ufs/hakios.ufshc_upiu.h
codec drivers/storage/ufs/hakios.ufshc.h # the driver's interface, plain C: the codec above includes it
13 changes: 13 additions & 0 deletions tools/sweetfb-present-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh
# SPDX-License-Identifier: MIT-0
# The framebuffer stand-in's sweetfb_nowait present state machine (drivers/display/sweetfb/hakios.sweetfb_present.h, which the
# ios-27 kernel's iokit/Kernel/arm/SweetFramebuffer.cpp takes through profiles/ios-27.copy) on this machine:
# tools/sweetfb_present_test.c drives it through random interleavings of swaps, GPU completions, stalls, vsync ticks and the
# display going off and on, under the address and undefined-behaviour sanitizers. The header is plain C.
# usage: tools/sweetfb-present-test.sh [seeds]
set -e
cd "$(dirname "$0")/.."
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
cc -O1 -g -Wall -Wextra -fsanitize=address,undefined -fno-sanitize-recover=all tools/sweetfb_present_test.c -o "$tmp/sweetfb_present_test"
"$tmp/sweetfb_present_test" "${1:-300}"
Loading
Loading