From: Lennart Poettering Date: Tue, 8 Feb 2022 10:18:44 +0000 (+0100) Subject: sd-boot: split out TSC/time API X-Git-Tag: v251-rc1~326^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e66a233734a34a9eedf4b1c02cb7246f86bbd48;p=thirdparty%2Fsystemd.git sd-boot: split out TSC/time API These are a whole family of functions, let's give them their own .c/.h file. No code changes, just splitting things out. --- diff --git a/src/boot/efi/boot.c b/src/boot/efi/boot.c index c431c38e9f8..b2f4f873c3d 100644 --- a/src/boot/efi/boot.c +++ b/src/boot/efi/boot.c @@ -18,6 +18,7 @@ #include "random-seed.h" #include "secure-boot.h" #include "shim.h" +#include "ticks.h" #include "util.h" #include "xbootldr.h" diff --git a/src/boot/efi/meson.build b/src/boot/efi/meson.build index d23352d48e5..f6004e47ecc 100644 --- a/src/boot/efi/meson.build +++ b/src/boot/efi/meson.build @@ -322,6 +322,7 @@ efi_headers = files( 'secure-boot.h', 'shim.h', 'splash.h', + 'ticks.h', 'util.h', 'xbootldr.h', ) @@ -334,6 +335,7 @@ common_sources = files( 'measure.c', 'pe.c', 'secure-boot.c', + 'ticks.c', 'util.c', ) diff --git a/src/boot/efi/ticks.c b/src/boot/efi/ticks.c new file mode 100644 index 00000000000..68b2d5806ce --- /dev/null +++ b/src/boot/efi/ticks.c @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include +#include + +#include "ticks.h" + +#ifdef __x86_64__ +UINT64 ticks_read(void) { + UINT64 a, d; + __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); + return (d << 32) | a; +} +#elif defined(__i386__) +UINT64 ticks_read(void) { + UINT64 val; + __asm__ volatile ("rdtsc" : "=A" (val)); + return val; +} +#elif defined(__aarch64__) +UINT64 ticks_read(void) { + UINT64 val; + __asm__ volatile ("mrs %0, cntpct_el0" : "=r" (val)); + return val; +} +#else +UINT64 ticks_read(void) { + UINT64 val = 1; + return val; +} +#endif + +#if defined(__aarch64__) +UINT64 ticks_freq(void) { + UINT64 freq; + __asm__ volatile ("mrs %0, cntfrq_el0": "=r" (freq)); + return freq; +} +#else +/* count TSC ticks during a millisecond delay */ +UINT64 ticks_freq(void) { + UINT64 ticks_start, ticks_end; + + ticks_start = ticks_read(); + BS->Stall(1000); + ticks_end = ticks_read(); + + return (ticks_end - ticks_start) * 1000UL; +} +#endif + +UINT64 time_usec(void) { + UINT64 ticks; + static UINT64 freq; + + ticks = ticks_read(); + if (ticks == 0) + return 0; + + if (freq == 0) { + freq = ticks_freq(); + if (freq == 0) + return 0; + } + + return 1000UL * 1000UL * ticks / freq; +} diff --git a/src/boot/efi/ticks.h b/src/boot/efi/ticks.h new file mode 100644 index 00000000000..389c7051b1f --- /dev/null +++ b/src/boot/efi/ticks.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +#pragma once + +#include +#include + +UINT64 ticks_read(void); +UINT64 ticks_freq(void); +UINT64 time_usec(void); diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 257646551c5..e6250207f68 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -3,69 +3,9 @@ #include #include +#include "ticks.h" #include "util.h" -#ifdef __x86_64__ -UINT64 ticks_read(void) { - UINT64 a, d; - __asm__ volatile ("rdtsc" : "=a" (a), "=d" (d)); - return (d << 32) | a; -} -#elif defined(__i386__) -UINT64 ticks_read(void) { - UINT64 val; - __asm__ volatile ("rdtsc" : "=A" (val)); - return val; -} -#elif defined(__aarch64__) -UINT64 ticks_read(void) { - UINT64 val; - __asm__ volatile ("mrs %0, cntpct_el0" : "=r" (val)); - return val; -} -#else -UINT64 ticks_read(void) { - UINT64 val = 1; - return val; -} -#endif - -#if defined(__aarch64__) -UINT64 ticks_freq(void) { - UINT64 freq; - __asm__ volatile ("mrs %0, cntfrq_el0": "=r" (freq)); - return freq; -} -#else -/* count TSC ticks during a millisecond delay */ -UINT64 ticks_freq(void) { - UINT64 ticks_start, ticks_end; - - ticks_start = ticks_read(); - BS->Stall(1000); - ticks_end = ticks_read(); - - return (ticks_end - ticks_start) * 1000UL; -} -#endif - -UINT64 time_usec(void) { - UINT64 ticks; - static UINT64 freq; - - ticks = ticks_read(); - if (ticks == 0) - return 0; - - if (freq == 0) { - freq = ticks_freq(); - if (freq == 0) - return 0; - } - - return 1000UL * 1000UL * ticks / freq; -} - EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b) { assert(b); diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index 3d659f7043f..33b17d04fbb 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -41,10 +41,6 @@ EFI_STATUS parse_boolean(const CHAR8 *v, BOOLEAN *b); -UINT64 ticks_read(void); -UINT64 ticks_freq(void); -UINT64 time_usec(void); - EFI_STATUS efivar_set(const EFI_GUID *vendor, const CHAR16 *name, const CHAR16 *value, UINT32 flags); EFI_STATUS efivar_set_raw(const EFI_GUID *vendor, const CHAR16 *name, const void *buf, UINTN size, UINT32 flags); EFI_STATUS efivar_set_uint_string(const EFI_GUID *vendor, const CHAR16 *name, UINTN i, UINT32 flags);