1// Copyright 2010 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// +build dragonfly freebsd netbsd openbsd solaris
6
7package runtime
8
9import (
10	"unsafe"
11)
12
13// Don't split the stack as this function may be invoked without a valid G,
14// which prevents us from allocating more stack.
15//go:nosplit
16func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
17	v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
18	if err != 0 {
19		return nil
20	}
21	mSysStatInc(sysStat, n)
22	return v
23}
24
25func sysUnused(v unsafe.Pointer, n uintptr) {
26	madvise(v, n, _MADV_FREE)
27}
28
29func sysUsed(v unsafe.Pointer, n uintptr) {
30}
31
32func sysHugePage(v unsafe.Pointer, n uintptr) {
33}
34
35// Don't split the stack as this function may be invoked without a valid G,
36// which prevents us from allocating more stack.
37//go:nosplit
38func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
39	mSysStatDec(sysStat, n)
40	munmap(v, n)
41}
42
43func sysFault(v unsafe.Pointer, n uintptr) {
44	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
45}
46
47// Indicates not to reserve swap space for the mapping.
48const _sunosMAP_NORESERVE = 0x40
49
50func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
51	flags := int32(_MAP_ANON | _MAP_PRIVATE)
52	if GOOS == "solaris" || GOOS == "illumos" {
53		// Be explicit that we don't want to reserve swap space
54		// for PROT_NONE anonymous mappings. This avoids an issue
55		// wherein large mappings can cause fork to fail.
56		flags |= _sunosMAP_NORESERVE
57	}
58	p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
59	if err != 0 {
60		return nil
61	}
62	return p
63}
64
65const _sunosEAGAIN = 11
66const _ENOMEM = 12
67
68func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
69	mSysStatInc(sysStat, n)
70
71	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
72	if err == _ENOMEM || ((GOOS == "solaris" || GOOS == "illumos") && err == _sunosEAGAIN) {
73		throw("runtime: out of memory")
74	}
75	if p != v || err != 0 {
76		throw("runtime: cannot map pages in arena address space")
77	}
78}
79