I've been trying to figure out how the linux kernel reads the current time. I've looked into clock_gettime, which brought me to gettimeofday and glibc code, which brought me back to time() in the linux kernel and now I'm stuck in the timekeeping code.
Where and how does the code (of clock_gettime) get the actual time used as a base to then compute the current time by converting the elapsed (from last clock read, I assume) TSC ticks into nanoseconds and summing them to the "base" time read from the clock.
Also, how can the code fetch clock AND TSC "at the same time", so that there is ~nanosecond error when computing a time with the approach discussed before?
Thanks
UPDATE: From what I gathered, the time is stored in the struct vsyscall_gtod_data (http://elixir.free-electrons.com/linux/v4.11.8/source/arch/x86/include/asm/vgtod.h#L16) which is updated via "timer interrupt" according to https://github.com/0xAX/linux-insides/blob/master/Timers/timers-7.md. Where do I find the code of said interrupt?