1// Copyright 2018 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
5package runtime
6
7import (
8	"unsafe"
9)
10
11// Don't split the stack as this function may be invoked without a valid G,
12// which prevents us from allocating more stack.
13//
14//go:nosplit
15func sysAllocOS(n uintptr) unsafe.Pointer {
16	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
17	if err != 0 {
18		return nil
19	}
20	return v
21}
22
23func sysUnusedOS(v unsafe.Pointer, n uintptr) {
24	// MADV_FREE_REUSABLE is like MADV_FREE except it also propagates
25	// accounting information about the process to task_info.
26	madvise(v, n, _MADV_FREE_REUSABLE)
27}
28
29func sysUsedOS(v unsafe.Pointer, n uintptr) {
30	// MADV_FREE_REUSE is necessary to keep the kernel's accounting
31	// accurate. If called on any memory region that hasn't been
32	// MADV_FREE_REUSABLE'd, it's a no-op.
33	madvise(v, n, _MADV_FREE_REUSE)
34}
35
36func sysHugePageOS(v unsafe.Pointer, n uintptr) {
37}
38
39func sysNoHugePageOS(v unsafe.Pointer, n uintptr) {
40}
41
42func sysHugePageCollapseOS(v unsafe.Pointer, n uintptr) {
43}
44
45// Don't split the stack as this function may be invoked without a valid G,
46// which prevents us from allocating more stack.
47//
48//go:nosplit
49func sysFreeOS(v unsafe.Pointer, n uintptr) {
50	munmap(v, n)
51}
52
53func sysFaultOS(v unsafe.Pointer, n uintptr) {
54	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
55}
56
57func sysReserveOS(v unsafe.Pointer, n uintptr) unsafe.Pointer {
58	p, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
59	if err != 0 {
60		return nil
61	}
62	return p
63}
64
65const _ENOMEM = 12
66
67func sysMapOS(v unsafe.Pointer, n uintptr) {
68	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
69	if err == _ENOMEM {
70		throw("runtime: out of memory")
71	}
72	if p != v || err != 0 {
73		print("runtime: mmap(", v, ", ", n, ") returned ", p, ", ", err, "\n")
74		throw("runtime: cannot map pages in arena address space")
75	}
76}
77