mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-02-18 17:57:27 +03:00
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.
This commit is contained in:
parent
9b176fbdc0
commit
1e66a23373
@ -18,6 +18,7 @@
|
||||
#include "random-seed.h"
|
||||
#include "secure-boot.h"
|
||||
#include "shim.h"
|
||||
#include "ticks.h"
|
||||
#include "util.h"
|
||||
#include "xbootldr.h"
|
||||
|
||||
|
@ -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',
|
||||
)
|
||||
|
||||
|
67
src/boot/efi/ticks.c
Normal file
67
src/boot/efi/ticks.c
Normal file
@ -0,0 +1,67 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
|
||||
#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;
|
||||
}
|
9
src/boot/efi/ticks.h
Normal file
9
src/boot/efi/ticks.h
Normal file
@ -0,0 +1,9 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#pragma once
|
||||
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
|
||||
UINT64 ticks_read(void);
|
||||
UINT64 ticks_freq(void);
|
||||
UINT64 time_usec(void);
|
@ -3,69 +3,9 @@
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
|
||||
#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);
|
||||
|
||||
|
@ -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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user