diff options
author | Julius Werner <jwerner@chromium.org> | 2021-05-20 17:00:46 -0700 |
---|---|---|
committer | Julius Werner <jwerner@chromium.org> | 2021-05-27 23:30:42 +0000 |
commit | 913a47a32236cac6eae8c707f643616686dc8598 (patch) | |
tree | 5493a5fd594d438d6a90120c073eed5ab1905b40 /src | |
parent | 8ad93797d6e1eb2d4be4010e29152636551567fa (diff) |
cbmem: Introduce "early" init hooks for console
Over the last couple of years we have continuously added more and more
CBMEM init hooks related to different independent components. One
disadvantage of the API is that it can not model any dependencies
between the different hooks, and their order is essentially undefined
(based on link order). For most hooks this is not a problem, and in fact
it's probably not a bad thing to discourage implicit dependencies
between unrelated components like this... but one resource the
components obviously all share is CBMEM, and since many CBMEM init hooks
are used to create new CBMEM areas, the arbitrary order means that the
order of these areas becomes unpredictable.
Generally code using CBMEM should not care where exactly an area is
allocated, but one exception is the persistent CBMEM console which
relies (on a best effort basis) on always getting allocated at the same
address on every boot. This is, technically, a hack, but it's a pretty
harmless hack that has served us reasonably well so far and would be
difficult to realize in a more robust way (without adding a lot of new
infrastructure). Most of the time, coreboot will allocate the same CBMEM
areas in the same order with the same sizes on every boot, and this all
kinda works out (and since it's only a debug console, we don't need to
be afraid of the odd one-in-a-million edge case breaking it).
But one reproducible difference we can have between boots is the vboot
boot mode (e.g. normal vs. recovery boot), and we had just kinda gotten
lucky in the past that we didn't have differences in CBMEM allocations
in different boot modes. With the recent addition of the RW_MCACHE
(which does not get allocated in recovery mode), this is no longer true,
and as a result CBMEM consoles can no longer persist between normal and
recovery modes.
The somewhat kludgy but simple solution is to just create a new class of
specifically "early" CBMEM init hooks that will always run before all
the others. While arbitrarily partitioning hooks into "early" and "not
early" without any precise definition of what these things mean may seem
a bit haphazard, I think it will be good enough in practice for the very
few cases where this matters and beats building anything much more
complicated (FWIW Linux has been doing something similar for years with
device suspend/resume ordering). Since the current use case only relates
to CBMEM allocation ordering and you can only really be "first" if you
allocate in romstage, the "early" hook is only available in romstage for
now (could be expanded later if we find a use case for it).
Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: If2c849a89f07a87d448ec1edbad4ce404afb0746
Reviewed-on: https://review.coreboot.org/c/coreboot/+/54737
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/include/cbmem.h | 29 | ||||
-rw-r--r-- | src/lib/cbmem_console.c | 6 | ||||
-rw-r--r-- | src/lib/program.ld | 1 |
3 files changed, 26 insertions, 10 deletions
diff --git a/src/include/cbmem.h b/src/include/cbmem.h index 9e12afe4a3..ec2b928ff8 100644 --- a/src/include/cbmem.h +++ b/src/include/cbmem.h @@ -108,37 +108,48 @@ void cbmem_add_records_to_cbtable(struct lb_header *header); #if ENV_RAMSTAGE #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_rom_ = init_fn_; #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) \ static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ section(".rodata.cbmem_init_hooks"))) = init_fn_; #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_pc_ = init_fn_; #elif ENV_ROMSTAGE #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) \ static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ section(".rodata.cbmem_init_hooks"))) = init_fn_; #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_ram_ = init_fn_; #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_pc_ = init_fn_; #elif ENV_POSTCAR #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_rom_ = init_fn_; #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_ram_ = init_fn_; #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) \ static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ section(".rodata.cbmem_init_hooks"))) = init_fn_; #else #define ROMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_rom_ = init_fn_; #define RAMSTAGE_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused2_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_ram_ = init_fn_; #define POSTCAR_CBMEM_INIT_HOOK(init_fn_) __attribute__((unused)) \ - static cbmem_init_hook_t init_fn_ ## _unused3_ = init_fn_; + static cbmem_init_hook_t init_fn_ ## _unused_pc_ = init_fn_; #endif /* ENV_RAMSTAGE */ +/* Early hooks get executed before other hooks. Use sparingly for hooks that create + CBMEM regions which need to remain in a constant location across boot modes. */ +#if ENV_ROMSTAGE +#define ROMSTAGE_CBMEM_INIT_HOOK_EARLY(init_fn_) \ + static cbmem_init_hook_t init_fn_ ## _ptr_ __attribute__((used, \ + section(".rodata.cbmem_init_hooks_early"))) = init_fn_; +#else +#define ROMSTAGE_CBMEM_INIT_HOOK_EARLY(init_fn_) __attribute__((unused)) \ + static cbmem_init_hook_t init_fn_ ## _unused_rom_ = init_fn_; +#endif /* ENV_ROMSTAGE */ + /* * Returns 0 for the stages where we know that cbmem does not come online. * Even if this function returns 1 for romstage, depending upon the point in diff --git a/src/lib/cbmem_console.c b/src/lib/cbmem_console.c index 641824f942..a7d67a3981 100644 --- a/src/lib/cbmem_console.c +++ b/src/lib/cbmem_console.c @@ -145,7 +145,11 @@ static void cbmemc_reinit(int is_recovery) init_console_ptr(cbmem_cons_p, size); copy_console_buffer(previous_cons_p); } -ROMSTAGE_CBMEM_INIT_HOOK(cbmemc_reinit) + +/* Run the romstage hook early so that the console region is one of the earliest created, and + therefore more likely to stay in the same place even across different boot modes where some + other regions may sometimes not get created (e.g. RW_MCACHE in vboot recovery mode). */ +ROMSTAGE_CBMEM_INIT_HOOK_EARLY(cbmemc_reinit) RAMSTAGE_CBMEM_INIT_HOOK(cbmemc_reinit) POSTCAR_CBMEM_INIT_HOOK(cbmemc_reinit) diff --git a/src/lib/program.ld b/src/lib/program.ld index 8180d9f1dc..1c5cda40e1 100644 --- a/src/lib/program.ld +++ b/src/lib/program.ld @@ -27,6 +27,7 @@ #if ENV_RAMSTAGE || ENV_ROMSTAGE || ENV_POSTCAR . = ALIGN(ARCH_POINTER_ALIGN_SIZE); _cbmem_init_hooks = .; + KEEP(*(.rodata.cbmem_init_hooks_early)); KEEP(*(.rodata.cbmem_init_hooks)); _ecbmem_init_hooks = .; RECORD_SIZE(cbmem_init_hooks) |