Text file
src/runtime/time_linux_amd64.s
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build !faketime
6
7 #include "go_asm.h"
8 #include "go_tls.h"
9 #include "textflag.h"
10
11 #define SYS_clock_gettime 228
12
13 // func time.now() (sec int64, nsec int32, mono int64)
14 TEXT time·now<ABIInternal>(SB),NOSPLIT,$16-24
15 MOVQ SP, R12 // Save old SP; R12 unchanged by C code.
16
17 MOVQ g_m(R14), BX // BX unchanged by C code.
18
19 // Set vdsoPC and vdsoSP for SIGPROF traceback.
20 // Save the old values on stack and restore them on exit,
21 // so this function is reentrant.
22 MOVQ m_vdsoPC(BX), CX
23 MOVQ m_vdsoSP(BX), DX
24 MOVQ CX, 0(SP)
25 MOVQ DX, 8(SP)
26
27 LEAQ sec+0(FP), DX
28 MOVQ -8(DX), CX // Sets CX to function return address.
29 MOVQ CX, m_vdsoPC(BX)
30 MOVQ DX, m_vdsoSP(BX)
31
32 CMPQ R14, m_curg(BX) // Only switch if on curg.
33 JNE noswitch
34
35 MOVQ m_g0(BX), DX
36 MOVQ (g_sched+gobuf_sp)(DX), SP // Set SP to g0 stack
37
38 noswitch:
39 SUBQ $32, SP // Space for two time results
40 ANDQ $~15, SP // Align for C code
41
42 MOVL $0, DI // CLOCK_REALTIME
43 LEAQ 16(SP), SI
44 MOVQ runtime·vdsoClockgettimeSym(SB), AX
45 CMPQ AX, $0
46 JEQ fallback
47 CALL AX
48
49 MOVL $1, DI // CLOCK_MONOTONIC
50 LEAQ 0(SP), SI
51 MOVQ runtime·vdsoClockgettimeSym(SB), AX
52 CALL AX
53
54 ret:
55 MOVQ 16(SP), AX // realtime sec
56 MOVQ 24(SP), DI // realtime nsec (moved to BX below)
57 MOVQ 0(SP), CX // monotonic sec
58 IMULQ $1000000000, CX
59 MOVQ 8(SP), DX // monotonic nsec
60
61 MOVQ R12, SP // Restore real SP
62
63 // Restore vdsoPC, vdsoSP
64 // We don't worry about being signaled between the two stores.
65 // If we are not in a signal handler, we'll restore vdsoSP to 0,
66 // and no one will care about vdsoPC. If we are in a signal handler,
67 // we cannot receive another signal.
68 MOVQ 8(SP), SI
69 MOVQ SI, m_vdsoSP(BX)
70 MOVQ 0(SP), SI
71 MOVQ SI, m_vdsoPC(BX)
72
73 // set result registers; AX is already correct
74 MOVQ DI, BX
75 ADDQ DX, CX
76 RET
77
78 fallback:
79 MOVQ $SYS_clock_gettime, AX
80 SYSCALL
81
82 MOVL $1, DI // CLOCK_MONOTONIC
83 LEAQ 0(SP), SI
84 MOVQ $SYS_clock_gettime, AX
85 SYSCALL
86
87 JMP ret
88
View as plain text