Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
15 changes: 15 additions & 0 deletions src/nvme.c
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,20 @@ bool nvme_post_m4_passed_off(void)
return !nvme_initialized && nvme_sart_saved_cnt && nvme_post_m4();
}

/*
* A proxy kboot (P_KBOOT_PREPARE_DT, then P_KBOOT_BOOT) hands Linux this stage's ANS session, as
* payload.c's boot= does. Key on the same controller state nvme_shutdown() acts on for boot=
* (NVME_T8132 with non-secure registers), so the two Linux entry points cannot drift apart: the
* tree describes ANS as running, and nvme_shutdown() verifies the controller and leaves it and
* RTKit to Linux, or refuses an unhealthy one and the stage returns to the proxy.
*/
bool nvme_keep_for_kboot(bool keep)
{
nvme_keep_running_for_linux =
keep && nvme_initialized && nvme_type == NVME_T8132 && !nvme_secure_regs;
return nvme_keep_running_for_linux;
}

bool nvme_shutdown(void)
{
if (nvme_reboot_required) {
Expand Down Expand Up @@ -1304,6 +1318,7 @@ bool nvme_shutdown(void)
return false;
}
nvme_initialized = false;
nvme_keep_running_for_linux = false;
printf("nvme: leaving controller and RTKit running for Linux adoption\n");
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/nvme.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ int nvme_cold_handoff(bool idle, bool no_probe);
/* Post-M4: how a stage that powered ANS off tells the next one to start it again. */
bool nvme_post_m4(void);
bool nvme_post_m4_passed_off(void);
/* A proxy kboot: hand Linux this stage's live post-M4 ANS session, as boot= does. */
bool nvme_keep_for_kboot(bool keep);
int nvme_sart_entries(struct sart_entry *entries, int max, bool *off);
char *nvme_sart_var(void);
bool nvme_set_sart_var(const char *val);
Expand Down
10 changes: 10 additions & 0 deletions src/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply)
reply->status = S_ERROR;
break;
}
/* A chainload is not a Linux kboot; clear any stale prepare_dt request. */
nvme_keep_for_kboot(false);
// forcefully restore tps6598x IRQs
usb_hpm_restore_irqs(1);
iodev_console_flush();
Expand Down Expand Up @@ -444,6 +446,7 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply)
if (kboot_boot((void *)request->args[0]) == 0)
return 1;
reply->status = S_ERROR; /* refused: the caller sees it */
nvme_keep_for_kboot(false);
#endif
break;
case P_KBOOT_SET_CHOSEN:
Expand All @@ -456,7 +459,12 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply)
#ifdef SPTM_VIRT
panic("proxy: P_KBOOT_PREPARE_DT is gated out of SPTM_VIRT builds\n");
#else
/* Decided before the tree is built: it describes ANS as running or as powered off. */
if (nvme_keep_for_kboot(true))
printf("kboot: keeping this stage's ANS session running for Linux to adopt\n");
reply->retval = kboot_prepare_dt((void *)request->args[0]);
if (reply->retval)
nvme_keep_for_kboot(false);
#endif
break;
case P_KBOOT_SET_UBOOT:
Expand Down Expand Up @@ -656,9 +664,11 @@ int proxy_process(ProxyRequest *request, ProxyReply *reply)
break;

case P_NVME_INIT:
nvme_keep_for_kboot(false);
reply->retval = nvme_init();
break;
case P_NVME_SHUTDOWN:
nvme_keep_for_kboot(false);
if (!nvme_shutdown())
reply->status = S_ERROR;
break;
Expand Down
10 changes: 9 additions & 1 deletion tests/python/test_kboot_prepared_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ def test_boot_after_failed_preparation_is_refused(tmp_path):
static void *dt = (void *)0x1000;
static int dt_bufsize = 0x10000;
static bool dt_ready, dt_retained_live_dcp;
static int chip_id = T8132, hardware_calls, prepare_fails;
static int chip_id = T8132, hardware_calls, prepare_fails, nvme_clear_calls;
static bool nvme_keep_for_kboot(bool keep)
{
assert(!keep);
nvme_clear_calls++;
return false;
}
static bool smp_has_pending_calls(void) { return false; }
static bool ctxr_exec_os_may_start(void) { return true; }
static int kboot_move_to_exec_window(void **kernel) { return 0; }
Expand Down Expand Up @@ -121,12 +127,14 @@ def test_boot_after_failed_preparation_is_refused(tmp_path):
assert(kboot_boot((void *)0xface) == -1 && !next_stage.entry && !hardware_calls);
assert(proxy_boot(&req, &rep) == 0 && rep.status == S_ERROR && !next_stage.entry);
assert(!hardware_calls);
assert(nvme_clear_calls == 1);

/* A preparation that succeeds: it boots */
prepare_fails = 0;
assert(kboot_prepare_dt((void *)1) == 0);
assert(proxy_boot(&req, &rep) == 1 && next_stage.entry == (void *)0xface);
assert(next_stage.args[0] == (u64)dt);
assert(nvme_clear_calls == 1);

/* A failed preparation after it does not boot the earlier tree */
prepare_fails = 1;
Expand Down
38 changes: 36 additions & 2 deletions tests/python/test_nvme_adoption.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_adopted_queue_lifetime(tmp_path):
sleep_reset = source[source.index("static bool nvme_ans_sleep_and_reset(void)"):
source.index("static bool nvme_exec_command(")]
# The handoff and the SART entries it reports to the next stage.
shutdown = source[source.index("bool nvme_shutdown(void)"):
shutdown = source[source.index("bool nvme_keep_for_kboot(bool keep)"):
source.index("bool nvme_set_sart_var(")]
harness = r'''
#include <assert.h>
Expand Down Expand Up @@ -231,6 +231,40 @@ def test_adopted_queue_lifetime(tmp_path):
{
assert(argc == 2);
failure = atoi(argv[1]);
if (failure >= 37) {
/*
* Proxy kboot Linux handoff on T8132 (37: prepare_dt -> Linux branch -> flag cleared;
* 38: prepare_dt aborted before chainload -> stage-to-stage power-off branch).
*/
bool aborted = failure == 38;
chip_id = T8132;
model_power = true;
nvme_sart_saved_cnt = 1;
cpu_off = true;
failure = 0;
assert(nvme_init());
assert(nvme_keep_for_kboot(true));
struct sart_entry e[4];
bool off = true;
assert(nvme_sart_entries(e, 4, &off) > 0 && !off);
if (aborted) {
assert(!nvme_keep_for_kboot(false));
assert(nvme_sart_entries(e, 4, &off) > 0 && off);
assert(nvme_shutdown());
assert(ans_offs == 1 && cpu_off && queue_frees == 2);
} else {
assert(nvme_shutdown());
assert(!nvme_initialized && !nvme_keep_running_for_linux);
assert(ans_offs == 0 && !cpu_off && queue_frees == 0);
/* Next stage init/shutdown without keep_for_kboot takes the normal power-off path. */
assert(nvme_init());
assert(nvme_shutdown());
assert(ans_offs == 1 && cpu_off);
}
for (unsigned i = 0; i < allocated; i++)
free(allocations[i]);
return 0;
}
if (failure >= 33) {
/*
* Post-M4 handoffs, modelled with ANS's power.
Expand Down Expand Up @@ -461,5 +495,5 @@ def test_adopted_queue_lifetime(tmp_path):
"-Wno-unused-variable", "-Wno-format", "-x", "c", "-", "-o", str(binary)],
input=harness, text=True, check=True,
)
for failure in range(37):
for failure in range(39):
subprocess.run([str(binary), str(failure)], check=True)
Loading