forked from golang/go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvgetrandom_linux.go
105 lines (91 loc) · 2.95 KB
/
vgetrandom_linux.go
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
// Copyright 2024 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 linux && (amd64 || arm64 || arm64be || ppc64 || ppc64le || loong64 || s390x)
package runtime
import (
"internal/cpu"
"unsafe"
)
//go:noescape
func vgetrandom1(buf *byte, length uintptr, flags uint32, state uintptr, stateSize uintptr) int
var vgetrandomAlloc struct {
states []uintptr
statesLock mutex
stateSize uintptr
mmapProt int32
mmapFlags int32
}
func vgetrandomInit() {
if vdsoGetrandomSym == 0 {
return
}
var params struct {
SizeOfOpaqueState uint32
MmapProt uint32
MmapFlags uint32
reserved [13]uint32
}
if vgetrandom1(nil, 0, 0, uintptr(unsafe.Pointer(¶ms)), ^uintptr(0)) != 0 {
return
}
vgetrandomAlloc.stateSize = uintptr(params.SizeOfOpaqueState)
vgetrandomAlloc.mmapProt = int32(params.MmapProt)
vgetrandomAlloc.mmapFlags = int32(params.MmapFlags)
lockInit(&vgetrandomAlloc.statesLock, lockRankLeafRank)
}
func vgetrandomGetState() uintptr {
lock(&vgetrandomAlloc.statesLock)
if len(vgetrandomAlloc.states) == 0 {
num := uintptr(ncpu) // Just a reasonable size hint to start.
stateSizeCacheAligned := (vgetrandomAlloc.stateSize + cpu.CacheLineSize - 1) &^ (cpu.CacheLineSize - 1)
allocSize := (num*stateSizeCacheAligned + physPageSize - 1) &^ (physPageSize - 1)
num = (physPageSize / stateSizeCacheAligned) * (allocSize / physPageSize)
p, err := mmap(nil, allocSize, vgetrandomAlloc.mmapProt, vgetrandomAlloc.mmapFlags, -1, 0)
if err != 0 {
unlock(&vgetrandomAlloc.statesLock)
return 0
}
newBlock := uintptr(p)
if vgetrandomAlloc.states == nil {
vgetrandomAlloc.states = make([]uintptr, 0, num)
}
for i := uintptr(0); i < num; i++ {
if (newBlock&(physPageSize-1))+vgetrandomAlloc.stateSize > physPageSize {
newBlock = (newBlock + physPageSize - 1) &^ (physPageSize - 1)
}
vgetrandomAlloc.states = append(vgetrandomAlloc.states, newBlock)
newBlock += stateSizeCacheAligned
}
}
state := vgetrandomAlloc.states[len(vgetrandomAlloc.states)-1]
vgetrandomAlloc.states = vgetrandomAlloc.states[:len(vgetrandomAlloc.states)-1]
unlock(&vgetrandomAlloc.statesLock)
return state
}
func vgetrandomPutState(state uintptr) {
lock(&vgetrandomAlloc.statesLock)
vgetrandomAlloc.states = append(vgetrandomAlloc.states, state)
unlock(&vgetrandomAlloc.statesLock)
}
// This is exported for use in internal/syscall/unix as well as x/sys/unix.
//
//go:linkname vgetrandom
func vgetrandom(p []byte, flags uint32) (ret int, supported bool) {
if vgetrandomAlloc.stateSize == 0 {
return -1, false
}
mp := acquirem()
if mp.vgetrandomState == 0 {
state := vgetrandomGetState()
if state == 0 {
releasem(mp)
return -1, false
}
mp.vgetrandomState = state
}
ret = vgetrandom1(unsafe.SliceData(p), uintptr(len(p)), flags, mp.vgetrandomState, vgetrandomAlloc.stateSize)
supported = true
releasem(mp)
return
}