aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/go/_std_1.22/src/runtime/vdso_freebsd.go
blob: 0fe21cf647044689327ebcec71a80df8d8c16564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build freebsd

package runtime

import (
	"runtime/internal/atomic"
	"unsafe"
)

const _VDSO_TH_NUM = 4 // defined in <sys/vdso.h> #ifdef _KERNEL

var timekeepSharedPage *vdsoTimekeep

//go:nosplit
func (bt *bintime) Add(bt2 *bintime) {
	u := bt.frac
	bt.frac += bt2.frac
	if u > bt.frac {
		bt.sec++
	}
	bt.sec += bt2.sec
}

//go:nosplit
func (bt *bintime) AddX(x uint64) {
	u := bt.frac
	bt.frac += x
	if u > bt.frac {
		bt.sec++
	}
}

var (
	// binuptimeDummy is used in binuptime as the address of an atomic.Load, to simulate
	// an atomic_thread_fence_acq() call which behaves as an instruction reordering and
	// memory barrier.
	binuptimeDummy uint32

	zeroBintime bintime
)

// based on /usr/src/lib/libc/sys/__vdso_gettimeofday.c
//
//go:nosplit
func binuptime(abs bool) (bt bintime) {
	timehands := (*[_VDSO_TH_NUM]vdsoTimehands)(add(unsafe.Pointer(timekeepSharedPage), vdsoTimekeepSize))
	for {
		if timekeepSharedPage.enabled == 0 {
			return zeroBintime
		}

		curr := atomic.Load(&timekeepSharedPage.current) // atomic_load_acq_32
		th := &timehands[curr]
		gen := atomic.Load(&th.gen) // atomic_load_acq_32
		bt = th.offset

		if tc, ok := th.getTimecounter(); !ok {
			return zeroBintime
		} else {
			delta := (tc - th.offset_count) & th.counter_mask
			bt.AddX(th.scale * uint64(delta))
		}
		if abs {
			bt.Add(&th.boottime)
		}

		atomic.Load(&binuptimeDummy) // atomic_thread_fence_acq()
		if curr == timekeepSharedPage.current && gen != 0 && gen == th.gen {
			break
		}
	}
	return bt
}

//go:nosplit
func vdsoClockGettime(clockID int32) bintime {
	if timekeepSharedPage == nil || timekeepSharedPage.ver != _VDSO_TK_VER_CURR {
		return zeroBintime
	}
	abs := false
	switch clockID {
	case _CLOCK_MONOTONIC:
		/* ok */
	case _CLOCK_REALTIME:
		abs = true
	default:
		return zeroBintime
	}
	return binuptime(abs)
}

func fallback_nanotime() int64
func fallback_walltime() (sec int64, nsec int32)

//go:nosplit
func nanotime1() int64 {
	bt := vdsoClockGettime(_CLOCK_MONOTONIC)
	if bt == zeroBintime {
		return fallback_nanotime()
	}
	return int64((1e9 * uint64(bt.sec)) + ((1e9 * uint64(bt.frac>>32)) >> 32))
}

func walltime() (sec int64, nsec int32) {
	bt := vdsoClockGettime(_CLOCK_REALTIME)
	if bt == zeroBintime {
		return fallback_walltime()
	}
	return int64(bt.sec), int32((1e9 * uint64(bt.frac>>32)) >> 32)
}