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 now() (sec int64, nsec int32, mono int64)
    14  TEXT time·now<ABIInternal>(SB),NOSPLIT,$16-24
    15  #ifdef GOEXPERIMENT_runtimesecret
    16  	// The kernel might spill our secrets onto g0
    17  	// erase our registers here.
    18  	CMPL	g_secret(R14), $0
    19  	JEQ	nosecret
    20  	CALL	·secretEraseRegisters(SB)
    21  
    22  nosecret:
    23  #endif
    24  
    25  	MOVQ	SP, R12 // Save old SP; R12 unchanged by C code.
    26  
    27  	MOVQ	g_m(R14), BX // BX unchanged by C code.
    28  
    29  	// Set vdsoPC and vdsoSP for SIGPROF traceback.
    30  	// Save the old values on stack and restore them on exit,
    31  	// so this function is reentrant.
    32  	MOVQ	m_vdsoPC(BX), CX
    33  	MOVQ	m_vdsoSP(BX), DX
    34  	MOVQ	CX, 0(SP)
    35  	MOVQ	DX, 8(SP)
    36  
    37  	LEAQ	sec+0(FP), DX
    38  	MOVQ	-8(DX), CX	// Sets CX to function return address.
    39  	MOVQ	CX, m_vdsoPC(BX)
    40  	MOVQ	DX, m_vdsoSP(BX)
    41  
    42  	CMPQ	R14, m_curg(BX)	// Only switch if on curg.
    43  	JNE	noswitch
    44  
    45  	MOVQ	m_g0(BX), DX
    46  	MOVQ	(g_sched+gobuf_sp)(DX), SP	// Set SP to g0 stack
    47  
    48  noswitch:
    49  	SUBQ	$32, SP		// Space for two time results
    50  	ANDQ	$~15, SP	// Align for C code
    51  
    52  	MOVL	$0, DI // CLOCK_REALTIME
    53  	LEAQ	16(SP), SI
    54  	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
    55  	CMPQ	AX, $0
    56  	JEQ	fallback
    57  	CALL	AX
    58  
    59  	MOVL	$1, DI // CLOCK_MONOTONIC
    60  	LEAQ	0(SP), SI
    61  	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
    62  	CALL	AX
    63  
    64  ret:
    65  	MOVQ	16(SP), AX	// realtime sec
    66  	MOVQ	24(SP), DI	// realtime nsec (moved to BX below)
    67  	MOVQ	0(SP), CX	// monotonic sec
    68  	IMULQ	$1000000000, CX
    69  	MOVQ	8(SP), DX	// monotonic nsec
    70  
    71  	MOVQ	R12, SP		// Restore real SP
    72  
    73  	// Restore vdsoPC, vdsoSP
    74  	// We don't worry about being signaled between the two stores.
    75  	// If we are not in a signal handler, we'll restore vdsoSP to 0,
    76  	// and no one will care about vdsoPC. If we are in a signal handler,
    77  	// we cannot receive another signal.
    78  	MOVQ	8(SP), SI
    79  	MOVQ	SI, m_vdsoSP(BX)
    80  	MOVQ	0(SP), SI
    81  	MOVQ	SI, m_vdsoPC(BX)
    82  
    83  	// set result registers; AX is already correct
    84  	MOVQ	DI, BX
    85  	ADDQ	DX, CX
    86  	RET
    87  
    88  fallback:
    89  	MOVQ	$SYS_clock_gettime, AX
    90  	SYSCALL
    91  
    92  	MOVL	$1, DI // CLOCK_MONOTONIC
    93  	LEAQ	0(SP), SI
    94  	MOVQ	$SYS_clock_gettime, AX
    95  	SYSCALL
    96  
    97  	JMP	ret
    98  

View as plain text