libroot/x86_64: rewrite system_time[_nsecs]() to C++

No functional change intended, just code deobfuscation.

Signed-off-by: Paweł Dziepak <[email protected]>
This commit is contained in:
Paweł Dziepak
2014-08-26 19:01:37 +02:00
parent 58ec759995
commit 6d70d75a8e
5 changed files with 47 additions and 83 deletions
+1 -2
View File
@@ -14,8 +14,7 @@ SEARCH_SOURCE += [ FDirName $(librootSources) os arch generic ] ;
KernelMergeObject kernel_os_arch_$(TARGET_ARCH).o :
byteorder.S
system_time_asm.S
system_time.c
system_time.cpp
generic_atomic.cpp
: $(TARGET_KERNEL_PIC_CCFLAGS)
+1 -1
View File
@@ -18,7 +18,7 @@ for architectureObject in [ MultiArchSubDirSetup x86_64 ] {
byteorder.S
get_stack_frame.S
system_info.cpp
system_time_asm.S
system_time.cpp
thread.cpp
time.cpp
tls.cpp
@@ -1,4 +0,0 @@
/*
Just a dummy to avoid a special case in the build system. system_time()
is implemented in system_time_asm.S.
*/
@@ -0,0 +1,45 @@
/*
* Copyright 2014, Paweł Dziepak, [email protected].
* Distributed under the terms of the MIT License.
*/
#include <stdint.h>
static uint64_t cv_factor;
static uint64_t cv_factor_nsec;
extern "C" void
__x86_setup_system_time(uint64_t cv, uint64_t cv_nsec)
{
cv_factor = cv;
cv_factor_nsec = cv_nsec;
}
static inline uint64_t
rdtsc()
{
uint64_t lo, hi;
__asm__("rdtsc" : "=a" (lo), "=d" (hi));
return lo | (hi << 32);
}
extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
system_time()
{
__uint128_t time = static_cast<__uint128_t>(rdtsc()) * cv_factor;
return time >> 64;
}
extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
system_time_nsecs()
{
__uint128_t t = static_cast<__uint128_t>(rdtsc()) * cv_factor_nsec;
return t >> 32;
}
@@ -1,76 +0,0 @@
/*
* Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Copyright 2012, Alex Smith, alex@alex-smith.me.uk.
* Distributed under the terms of the MIT License.
*
* Copyright 2001, Travis Geiselbrecht. All rights reserved.
* Distributed under the terms of the NewOS License.
*/
#include <asm_defs.h>
/* saves the conversion factor needed for system_time */
.lcomm cv_factor 8
.lcomm cv_factor_nsecs 8
.text
FUNCTION(__x86_setup_system_time):
movq %rdi, cv_factor(%rip)
movq %rsi, cv_factor_nsecs(%rip)
ret
FUNCTION_END(__x86_setup_system_time)
/* int64 system_time(); */
FUNCTION(system_time):
// (rdtsc * cv_factor) >> 32.
// Factor is pre-shifted left by 32 bits.
movq cv_factor(%rip), %rcx
// Load 64-bit TSC into %eax (low), %edx (high).
rdtsc
// Convert into a single 64-bit value.
shl $32, %rdx
orq %rdx, %rax
// Multiply by conversion factor, result in %rax (low), %rdx (high).
mulq %rcx
// Due to pre-shifting of the factor the whole result in high.
movq %rdx, %rax
ret
FUNCTION_END(system_time)
/* int64 system_time_nsecs(); */
FUNCTION(system_time_nsecs):
// Same algorithm as system_time(), but with a different factor.
// (rdtsc * cv_factor_nsecs) >> 32.
// Factor has not been pre-shifted here, otherwise we may lose the upper
// 32 bits.
movq cv_factor_nsecs(%rip), %rcx
// Load 64-bit TSC into %eax (low), %edx (high).
rdtsc
// Convert into a single 64-bit value.
shl $32, %rdx
orq %rdx, %rax
// Multiply by conversion factor, result in %rax (low), %rdx (high).
mulq %rcx
// Shift the result right by 32 bits.
shr $32, %rax
shl $32, %rdx
orq %rdx, %rax
ret
FUNCTION_END(system_time_nsecs)