diff --git a/content/post/202103-timestamp-syscall.md b/content/post/202103-timestamp-syscall.md index f5b20d1..747852b 100644 --- a/content/post/202103-timestamp-syscall.md +++ b/content/post/202103-timestamp-syscall.md @@ -1,6 +1,6 @@ +++ title = "系统调用:从获取系统时间戳开始" -date = "2020-05-31" +date = "2021-03-31" description = "想知道 Go 中执行 time.Now() 如何获得系统时间" +++ @@ -31,7 +31,9 @@ func main() { 那么运行时,究竟发生了什么? 本文档将一步一步,从 Go 源码中 Now() 函数开始剖析,到 Go 如何调用汇编,再到系统调用过程, -解释编程语言如何与操作系统互动。 +解释系统调用如何进行。 + +> 文档中用的 Go 版本为 1.15.2 Go 源码分析 -------------------------------------------------------------------------------- @@ -61,8 +63,6 @@ Go 中对函数定义的说明,见 https://golang.org/ref/spec#Function_declar > A function declaration may omit the body. Such a declaration provides the signature for a function implemented outside Go, such as an assembly routine. -> 文档中用的 Go 版本为 1.15.2 - 注释写了 now() 由 runtime 包提供,那到 runtime 包去找 没找到 `func now()`,但有 `func time_now()`,是这个吗? diff --git a/content/post/202104-linux-syscall.md b/content/post/202104-linux-syscall.md new file mode 100644 index 0000000..ba41853 --- /dev/null +++ b/content/post/202104-linux-syscall.md @@ -0,0 +1,52 @@ ++++ +title = "系统调用:Linux 源码如何实现" +date = "2021-04-01" +description = "想知道 Go 中执行 time.Now() 如何获得系统时间" +draft = true ++++ + +查看 Linux 源码实现 +-------------------------------------------------------------------------------- +Linux 最新代码,https://github.com/torvalds/linux,Commit 5e46d1b78a + +找到了 `clock_gettime` 的定义,是 `__vdso_clock_gettime` 的 alias + +arch/x86/entry/vdso/vclock_gettime.c +```cpp +int clock_gettime(clockid_t, struct old_timespec32 *) + __attribute__((weak, alias("__vdso_clock_gettime"))); + +int clock_gettime64(clockid_t, struct __kernel_timespec *) + __attribute__((weak, alias("__vdso_clock_gettime64"))); +``` + +arch/x86/um/vdso/um_vdso.c +```cpp +int __vdso_clock_gettime(clockid_t clock, struct __kernel_old_timespec *ts) +{ + long ret; + + asm("syscall" : "=a" (ret) : + "0" (__NR_clock_gettime), "D" (clock), "S" (ts) : "memory"); + + return ret; +} +int clock_gettime(clockid_t, struct __kernel_old_timespec *) + __attribute__((weak, alias("__vdso_clock_gettime"))); +``` + +如何编写一个 Linux 系统调用? +-------------------------------------------------------------------------------- +能改 Linux 源码,实现一个系统调用,叫 get_my_birthday(),返回我的出生年月时间戳? +```cpp + +``` + +硬件时间 +-------------------------------------------------------------------------------- +石英钟如何计时? + +BIOS 芯片如何存储、读取时间? + +参考文档 +1. 《时间到底是什么?1 秒究竟有多长?李永乐老师讲石英钟和原子钟》https://www.youtube.com/watch?v=cXX_f_pWLQI \ No newline at end of file