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
5package runtime
6
7import (
8	"unsafe"
9)
10
11const (
12	_MEM_COMMIT   = 0x1000
13	_MEM_RESERVE  = 0x2000
14	_MEM_DECOMMIT = 0x4000
15	_MEM_RELEASE  = 0x8000
16
17	_PAGE_READWRITE = 0x0004
18	_PAGE_NOACCESS  = 0x0001
19
20	_ERROR_NOT_ENOUGH_MEMORY = 8
21	_ERROR_COMMITMENT_LIMIT  = 1455
22)
23
24// Don't split the stack as this function may be invoked without a valid G,
25// which prevents us from allocating more stack.
26//go:nosplit
27func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
28	mSysStatInc(sysStat, n)
29	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_COMMIT|_MEM_RESERVE, _PAGE_READWRITE))
30}
31
32func sysUnused(v unsafe.Pointer, n uintptr) {
33	r := stdcall3(_VirtualFree, uintptr(v), n, _MEM_DECOMMIT)
34	if r != 0 {
35		return
36	}
37
38	// Decommit failed. Usual reason is that we've merged memory from two different
39	// VirtualAlloc calls, and Windows will only let each VirtualFree handle pages from
40	// a single VirtualAlloc. It is okay to specify a subset of the pages from a single alloc,
41	// just not pages from multiple allocs. This is a rare case, arising only when we're
42	// trying to give memory back to the operating system, which happens on a time
43	// scale of minutes. It doesn't have to be terribly fast. Instead of extra bookkeeping
44	// on all our VirtualAlloc calls, try freeing successively smaller pieces until
45	// we manage to free something, and then repeat. This ends up being O(n log n)
46	// in the worst case, but that's fast enough.
47	for n > 0 {
48		small := n
49		for small >= 4096 && stdcall3(_VirtualFree, uintptr(v), small, _MEM_DECOMMIT) == 0 {
50			small /= 2
51			small &^= 4096 - 1
52		}
53		if small < 4096 {
54			print("runtime: VirtualFree of ", small, " bytes failed with errno=", getlasterror(), "\n")
55			throw("runtime: failed to decommit pages")
56		}
57		v = add(v, small)
58		n -= small
59	}
60}
61
62func sysUsed(v unsafe.Pointer, n uintptr) {
63	p := stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_COMMIT, _PAGE_READWRITE)
64	if p == uintptr(v) {
65		return
66	}
67
68	// Commit failed. See SysUnused.
69	// Hold on to n here so we can give back a better error message
70	// for certain cases.
71	k := n
72	for k > 0 {
73		small := k
74		for small >= 4096 && stdcall4(_VirtualAlloc, uintptr(v), small, _MEM_COMMIT, _PAGE_READWRITE) == 0 {
75			small /= 2
76			small &^= 4096 - 1
77		}
78		if small < 4096 {
79			errno := getlasterror()
80			switch errno {
81			case _ERROR_NOT_ENOUGH_MEMORY, _ERROR_COMMITMENT_LIMIT:
82				print("runtime: VirtualAlloc of ", n, " bytes failed with errno=", errno, "\n")
83				throw("out of memory")
84			default:
85				print("runtime: VirtualAlloc of ", small, " bytes failed with errno=", errno, "\n")
86				throw("runtime: failed to commit pages")
87			}
88		}
89		v = add(v, small)
90		k -= small
91	}
92}
93
94func sysHugePage(v unsafe.Pointer, n uintptr) {
95}
96
97// Don't split the stack as this function may be invoked without a valid G,
98// which prevents us from allocating more stack.
99//go:nosplit
100func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
101	mSysStatDec(sysStat, n)
102	r := stdcall3(_VirtualFree, uintptr(v), 0, _MEM_RELEASE)
103	if r == 0 {
104		print("runtime: VirtualFree of ", n, " bytes failed with errno=", getlasterror(), "\n")
105		throw("runtime: failed to release pages")
106	}
107}
108
109func sysFault(v unsafe.Pointer, n uintptr) {
110	// SysUnused makes the memory inaccessible and prevents its reuse
111	sysUnused(v, n)
112}
113
114func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
115	// v is just a hint.
116	// First try at v.
117	// This will fail if any of [v, v+n) is already reserved.
118	v = unsafe.Pointer(stdcall4(_VirtualAlloc, uintptr(v), n, _MEM_RESERVE, _PAGE_READWRITE))
119	if v != nil {
120		return v
121	}
122
123	// Next let the kernel choose the address.
124	return unsafe.Pointer(stdcall4(_VirtualAlloc, 0, n, _MEM_RESERVE, _PAGE_READWRITE))
125}
126
127func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
128	mSysStatInc(sysStat, n)
129}
130