I am trying to understand the implementation of time() in glibc: https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/x86/time.c.html#time
If you expand the macros (hovering over them), you get:
time_t time (time_t *t)
{
unsigned long int resultvar;
long int __arg1 = (long int) (t);
register long int _a1 asm (""rdi"") = __arg1;
asm volatile ( ""syscall\n\t"" : ""=a"" (resultvar) : ""0"" (201) , ""r"" (_a1) : ""memory"", ""cc"", ""r11"", ""cx"");
(long int) resultvar;
}
The assembly command clearly must return the current time, but I can not understand how. Checking on Intel manual syscall I see it clobbers r11 and cx, the other clobbers are gcc stuff, up to there, I'm fine.
From what I read (https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html) "0"
means something is used as both input and output, although I do not get what (201)
means (I was expecting a variable name, not a number). Any ideas?
I am not sure if _a1 is required to have info, I would assume it can be NULL, so the only real input of the assembly command is that (201)
, but I can't tell how syscall
together with 201 gives me access to the internal clock.
P.S. Bonus question: from what I read on the Intel manual I am under the impression that the only clock available is expected to be on the MoBo rather than be part of the CPU hardware. Have I misunderstood?