From c8127e4ac9811517f6147cf019ba6a948cdaa4a5 Mon Sep 17 00:00:00 2001 From: Julius Werner Date: Fri, 6 Jun 2014 16:10:56 -0700 Subject: [PATCH] Add and consistently use wrapper macro for romstage static variables x86 systems run their romstage as execute-in-place from flash, which prevents them from having writable data segments. In several code pieces that get linked into both romstage and ramstage, this has been worked around by using a local variable and having the 'static' storage class guarded by #ifndef __PRE_RAM__. However, x86 is the only architecture using execute-in-place (for now), so it does not make sense to impose the restriction globally. Rather than fixing the #ifdef at every occurrence, this should really be wrapped in a way that makes it easier to modify in a single place. The chromeos/cros_vpd.c file already had a nice approach for a wrapper macro, but unfortunately restricted it to one file... this patch moves it to stddef.h and employs it consistently throughout coreboot. BRANCH=nyan BUG=None TEST=Measured boot time on Nyan_Big before and after, confirmed that it gained 6ms from caching the FMAP in vboot_loader.c. Change-Id: Ia53b94ab9c6a303b979db7ff20b79e14bc51f9f8 Signed-off-by: Julius Werner Reviewed-on: https://chromium-review.googlesource.com/203033 Reviewed-by: Aaron Durbin Reviewed-by: Stefan Reinauer --- src/include/stddef.h | 7 +++++++ src/lib/lzma.c | 7 +------ src/lib/timestamp.c | 11 +++-------- src/vendorcode/google/chromeos/cros_vpd.c | 18 ++++-------------- src/vendorcode/google/chromeos/fmap.c | 5 +---- 5 files changed, 16 insertions(+), 32 deletions(-) diff --git a/src/include/stddef.h b/src/include/stddef.h index 137de04405..bd6c3babf3 100644 --- a/src/include/stddef.h +++ b/src/include/stddef.h @@ -38,4 +38,11 @@ typedef unsigned int wint_t; #define ROMSTAGE_CONST #endif +/* Work around non-writable data segment in execute-in-place romstage on x86. */ +#if defined(__PRE_RAM__) && CONFIG_ARCH_X86 +#define MAYBE_STATIC +#else +#define MAYBE_STATIC static +#endif + #endif /* STDDEF_H */ diff --git a/src/lib/lzma.c b/src/lib/lzma.c index cd60b3f3b2..82dd9370e7 100644 --- a/src/lib/lzma.c +++ b/src/lib/lzma.c @@ -23,12 +23,7 @@ unsigned long ulzma(unsigned char * src, unsigned char * dst) int res; CLzmaDecoderState state; SizeT mallocneeds; -#if !defined(__PRE_RAM__) - /* in ramstage, this can go in BSS */ - static -#endif - /* in pre-ram, it must go on the stack */ - unsigned char scratchpad[15980]; + MAYBE_STATIC unsigned char scratchpad[15980]; unsigned char *cp; memcpy(properties, src, LZMA_PROPERTIES_SIZE); diff --git a/src/lib/timestamp.c b/src/lib/timestamp.c index 4643bdd084..58d25e3971 100644 --- a/src/lib/timestamp.c +++ b/src/lib/timestamp.c @@ -17,16 +17,13 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA */ +#include #include #include #include #include -#ifndef __PRE_RAM__ #include -static struct timestamp_table* ts_table; -#endif - #define MAX_TIMESTAMPS 30 void timestamp_init(uint64_t base) @@ -50,12 +47,10 @@ void timestamp_init(uint64_t base) void timestamp_add(enum timestamp_id id, uint64_t ts_time) { struct timestamp_entry *tse; -#ifdef __PRE_RAM__ - struct timestamp_table *ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); -#else + MAYBE_STATIC struct timestamp_table *ts_table = NULL; + if (!ts_table) ts_table = cbmem_find(CBMEM_ID_TIMESTAMP); -#endif if (!ts_table || (ts_table->num_entries == ts_table->max_entries)) return; diff --git a/src/vendorcode/google/chromeos/cros_vpd.c b/src/vendorcode/google/chromeos/cros_vpd.c index 26b01ee7a3..df2b5bf667 100644 --- a/src/vendorcode/google/chromeos/cros_vpd.c +++ b/src/vendorcode/google/chromeos/cros_vpd.c @@ -15,16 +15,6 @@ #include "lib_vpd.h" #include "vpd_tables.h" -/* - * Static variables are available in ramstage (all platforms), and romstage for - * some platforms (ex, ARM, which uses SRAM). - */ -#if defined(__PRE_RAM__) && CONFIG_ARCH_X86 -#define STATIC_VAR -#else -#define STATIC_VAR static -#endif - /* Currently we only support Google VPD 2.0, which has a fixed offset. */ enum { GOOGLE_VPD_2_0_OFFSET = 0x600, @@ -39,10 +29,10 @@ struct vpd_gets_arg { static int cros_vpd_load(uint8_t **vpd_address, int32_t *vpd_size) { - STATIC_VAR int cached = 0; - STATIC_VAR uint8_t *cached_address = NULL; - STATIC_VAR int32_t cached_size = 0; - STATIC_VAR int result = -1; + MAYBE_STATIC int cached = 0; + MAYBE_STATIC uint8_t *cached_address = NULL; + MAYBE_STATIC int32_t cached_size = 0; + MAYBE_STATIC int result = -1; struct google_vpd_info info; int32_t base; diff --git a/src/vendorcode/google/chromeos/fmap.c b/src/vendorcode/google/chromeos/fmap.c index bc56a19066..8bea8f73c9 100644 --- a/src/vendorcode/google/chromeos/fmap.c +++ b/src/vendorcode/google/chromeos/fmap.c @@ -92,10 +92,7 @@ const struct fmap_area *find_fmap_area(const struct fmap *fmap, int find_fmap_entry(const char name[], void **pointer) { -#ifndef __PRE_RAM__ - static -#endif - const struct fmap *fmap = NULL; + MAYBE_STATIC const struct fmap *fmap = NULL; const struct fmap_area *area; void *base = NULL;