| 1 |
// Copyright 2014 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 |
package runtime |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/abi" |
| 9 |
"internal/goarch" |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
type mOS struct{} |
| 14 |
|
| 15 |
//go:noescape |
| 16 |
//extern umtx_sleep |
| 17 |
func sys_umtx_sleep(addr *uint32, val, timeout int32) int32 |
| 18 |
|
| 19 |
//go:noescape |
| 20 |
//extern umtx_wakeup |
| 21 |
func sys_umtx_wakeup(addr *uint32, val int32) int32 |
| 22 |
|
| 23 |
//go:noescape |
| 24 |
//extern sysctl |
| 25 |
func sysctl(*uint32, uint32, *byte, *uintptr, *byte, uintptr) int32 |
| 26 |
|
| 27 |
func getncpu() int32 { |
| 28 |
mib := [2]uint32{_CTL_HW, _HW_NCPU} |
| 29 |
out := uint32(0) |
| 30 |
nout := uintptr(unsafe.Sizeof(out)) |
| 31 |
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) |
| 32 |
if ret >= 0 { |
| 33 |
return int32(out) |
| 34 |
} |
| 35 |
return 1 |
| 36 |
} |
| 37 |
|
| 38 |
func getPageSize() uintptr { |
| 39 |
mib := [2]uint32{_CTL_HW, _HW_PAGESIZE} |
| 40 |
out := uint32(0) |
| 41 |
nout := uintptr(unsafe.Sizeof(out)) |
| 42 |
ret := sysctl(&mib[0], 2, (*byte)(unsafe.Pointer(&out)), &nout, nil, 0) |
| 43 |
if ret >= 0 { |
| 44 |
return uintptr(out) |
| 45 |
} |
| 46 |
return 0 |
| 47 |
} |
| 48 |
|
| 49 |
//go:nosplit |
| 50 |
func futexsleep(addr *uint32, val uint32, ns int64) { |
| 51 |
systemstack(func() { |
| 52 |
futexsleep1(addr, val, ns) |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func futexsleep1(addr *uint32, val uint32, ns int64) { |
| 57 |
var timeout int32 |
| 58 |
if ns >= 0 { |
| 59 |
// The timeout is specified in microseconds - ensure that we |
| 60 |
// do not end up dividing to zero, which would put us to sleep |
| 61 |
// indefinitely... |
| 62 |
timeout = timediv(ns, 1000, nil) |
| 63 |
if timeout == 0 { |
| 64 |
timeout = 1 |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// sys_umtx_sleep will return EWOULDBLOCK (EAGAIN) when the timeout |
| 69 |
// expires or EBUSY if the mutex value does not match. |
| 70 |
ret := sys_umtx_sleep(addr, int32(val), timeout) |
| 71 |
if ret >= 0 || ret == -_EINTR || ret == -_EAGAIN || ret == -_EBUSY { |
| 72 |
return |
| 73 |
} |
| 74 |
|
| 75 |
print("umtx_sleep addr=", addr, " val=", val, " ret=", ret, "\n") |
| 76 |
*(*int32)(unsafe.Pointer(uintptr(0x1005))) = 0x1005 |
| 77 |
} |
| 78 |
|
| 79 |
//go:nosplit |
| 80 |
func futexwakeup(addr *uint32, cnt uint32) { |
| 81 |
ret := sys_umtx_wakeup(addr, int32(cnt)) |
| 82 |
if ret >= 0 { |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
systemstack(func() { |
| 87 |
print("umtx_wake_addr=", addr, " ret=", ret, "\n") |
| 88 |
*(*int32)(unsafe.Pointer(uintptr(0x1006))) = 0x1006 |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
func osinit() { |
| 93 |
ncpu = getncpu() |
| 94 |
if physPageSize == 0 { |
| 95 |
physPageSize = getPageSize() |
| 96 |
} |
| 97 |
} |