From e44f456f1d0e429e08afed64a161175ff493f3ac Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Wed, 27 Feb 2019 05:05:44 +0100
Subject: [PATCH] runtime: use CLOCK_BOOTTIME in nanotime on Linux

This makes timers account for having expired while a computer was
asleep, which is quite common on mobile devices. Note that BOOTTIME is
identical to MONOTONIC, except that it takes into account time spent
in suspend. In Linux 4.17, the kernel will actually make MONOTONIC act
like BOOTTIME anyway, so this switch will additionally unify the
timer behavior across kernels.

BOOTTIME was introduced into Linux 2.6.39-rc1 with 70a08cca1227d in
2011.

Fixes #24595

Change-Id: I7b2a6ca0c5bc5fce57ec0eeafe7b68270b429321
---
 src/runtime/sys_linux_386.s     | 4 ++--
 src/runtime/sys_linux_amd64.s   | 2 +-
 src/runtime/sys_linux_arm.s     | 4 ++--
 src/runtime/sys_linux_arm64.s   | 4 ++--
 src/runtime/sys_linux_mips64x.s | 2 +-
 src/runtime/sys_linux_mipsx.s   | 2 +-
 src/runtime/sys_linux_ppc64x.s  | 2 +-
 src/runtime/sys_linux_s390x.s   | 2 +-
 8 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/src/runtime/sys_linux_386.s b/src/runtime/sys_linux_386.s
index 1b28098ad9..46b7071ed8 100644
--- a/src/runtime/sys_linux_386.s
+++ b/src/runtime/sys_linux_386.s
@@ -317,13 +317,13 @@ noswitch:
 
 	LEAL	8(SP), BX	// &ts (struct timespec)
 	MOVL	BX, 4(SP)
-	MOVL	$1, 0(SP)	// CLOCK_MONOTONIC
+	MOVL	$7, 0(SP)	// CLOCK_BOOTTIME
 	CALL	AX
 	JMP finish
 
 fallback:
 	MOVL	$SYS_clock_gettime, AX
-	MOVL	$1, BX		// CLOCK_MONOTONIC
+	MOVL	$7, BX		// CLOCK_BOOTTIME
 	LEAL	8(SP), CX
 	INVOKE_SYSCALL
 
diff --git a/src/runtime/sys_linux_amd64.s b/src/runtime/sys_linux_amd64.s
index 58d3bc54b4..4bb9bde3d0 100644
--- a/src/runtime/sys_linux_amd64.s
+++ b/src/runtime/sys_linux_amd64.s
@@ -293,7 +293,7 @@ noswitch:
 	MOVQ	runtime·vdsoClockgettimeSym(SB), AX
 	CMPQ	AX, $0
 	JEQ	fallback
-	MOVL	$1, DI // CLOCK_MONOTONIC
+	MOVL	$7, DI // CLOCK_BOOTTIME
 	LEAQ	0(SP), SI
 	CALL	AX
 	MOVQ	0(SP), AX	// sec
diff --git a/src/runtime/sys_linux_arm.s b/src/runtime/sys_linux_arm.s
index e103da56dc..0b872b90a6 100644
--- a/src/runtime/sys_linux_arm.s
+++ b/src/runtime/sys_linux_arm.s
@@ -11,7 +11,7 @@
 #include "textflag.h"
 
 #define CLOCK_REALTIME	0
-#define CLOCK_MONOTONIC	1
+#define CLOCK_BOOTTIME	7
 
 // for EABI, as we don't support OABI
 #define SYS_BASE 0x0
@@ -345,7 +345,7 @@ noswitch:
 	SUB	$24, R13	// Space for results
 	BIC	$0x7, R13	// Align for C code
 
-	MOVW	$CLOCK_MONOTONIC, R0
+	MOVW	$CLOCK_BOOTTIME, R0
 	MOVW	$8(R13), R1	// timespec
 	MOVW	runtime·vdsoClockgettimeSym(SB), R2
 	CMP	$0, R2
diff --git a/src/runtime/sys_linux_arm64.s b/src/runtime/sys_linux_arm64.s
index b9588cec30..e444d50df4 100644
--- a/src/runtime/sys_linux_arm64.s
+++ b/src/runtime/sys_linux_arm64.s
@@ -13,7 +13,7 @@
 #define AT_FDCWD -100
 
 #define CLOCK_REALTIME 0
-#define CLOCK_MONOTONIC 1
+#define CLOCK_BOOTTIME 7
 
 #define SYS_exit		93
 #define SYS_read		63
@@ -297,7 +297,7 @@ noswitch:
 	BIC	$15, R1
 	MOVD	R1, RSP
 
-	MOVW	$CLOCK_MONOTONIC, R0
+	MOVW	$CLOCK_BOOTTIME, R0
 	MOVD	runtime·vdsoClockgettimeSym(SB), R2
 	CBZ	R2, fallback
 
diff --git a/src/runtime/sys_linux_mips64x.s b/src/runtime/sys_linux_mips64x.s
index 723cfe43d9..edd7a195eb 100644
--- a/src/runtime/sys_linux_mips64x.s
+++ b/src/runtime/sys_linux_mips64x.s
@@ -278,7 +278,7 @@ noswitch:
 	AND	$~15, R1	// Align for C code
 	MOVV	R1, R29
 
-	MOVW	$1, R4 // CLOCK_MONOTONIC
+	MOVW	$7, R4 // CLOCK_BOOTTIME
 	MOVV	$0(R29), R5
 
 	MOVV	runtime·vdsoClockgettimeSym(SB), R25
diff --git a/src/runtime/sys_linux_mipsx.s b/src/runtime/sys_linux_mipsx.s
index 15893a7a28..f3edf9a83a 100644
--- a/src/runtime/sys_linux_mipsx.s
+++ b/src/runtime/sys_linux_mipsx.s
@@ -235,7 +235,7 @@ TEXT runtime·walltime1(SB),NOSPLIT,$8-12
 	RET
 
 TEXT runtime·nanotime1(SB),NOSPLIT,$8-8
-	MOVW	$1, R4	// CLOCK_MONOTONIC
+	MOVW	$7, R4	// CLOCK_BOOTTIME
 	MOVW	$4(R29), R5
 	MOVW	$SYS_clock_gettime, R2
 	SYSCALL
diff --git a/src/runtime/sys_linux_ppc64x.s b/src/runtime/sys_linux_ppc64x.s
index 8629fe3233..2402e2623a 100644
--- a/src/runtime/sys_linux_ppc64x.s
+++ b/src/runtime/sys_linux_ppc64x.s
@@ -233,7 +233,7 @@ fallback:
 	JMP	finish
 
 TEXT runtime·nanotime1(SB),NOSPLIT,$16
-	MOVD	$1, R3		// CLOCK_MONOTONIC
+	MOVD	$7, R3		// CLOCK_BOOTTIME
 
 	MOVD	R1, R15		// R15 is unchanged by C code
 	MOVD	g_m(g), R21	// R21 = m
diff --git a/src/runtime/sys_linux_s390x.s b/src/runtime/sys_linux_s390x.s
index c15a1d5364..f52c4d5098 100644
--- a/src/runtime/sys_linux_s390x.s
+++ b/src/runtime/sys_linux_s390x.s
@@ -207,7 +207,7 @@ TEXT runtime·walltime1(SB),NOSPLIT,$16
 	RET
 
 TEXT runtime·nanotime1(SB),NOSPLIT,$16
-	MOVW	$1, R2 // CLOCK_MONOTONIC
+	MOVW	$7, R2 // CLOCK_BOOTTIME
 	MOVD	$tp-16(SP), R3
 	MOVW	$SYS_clock_gettime, R1
 	SYSCALL
-- 
2.25.1

