2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2014-11-11 21:00:29 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-11-21 08:22:18 -07:00
|
|
|
// +build dragonfly freebsd nacl netbsd openbsd solaris
|
2014-11-11 21:00:29 -07:00
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
2015-11-11 10:39:30 -07:00
|
|
|
import (
|
|
|
|
"runtime/internal/sys"
|
|
|
|
"unsafe"
|
|
|
|
)
|
2014-11-11 21:00:29 -07:00
|
|
|
|
2015-04-16 15:32:18 -06:00
|
|
|
// Don't split the stack as this function may be invoked without a valid G,
|
|
|
|
// which prevents us from allocating more stack.
|
2014-11-11 21:00:29 -07:00
|
|
|
//go:nosplit
|
2015-04-16 15:32:18 -06:00
|
|
|
func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
|
2017-10-16 18:28:29 -06:00
|
|
|
v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
|
|
|
|
if err != 0 {
|
2014-11-11 21:00:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
2015-04-16 15:32:18 -06:00
|
|
|
mSysStatInc(sysStat, n)
|
2014-11-11 21:00:29 -07:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysUnused(v unsafe.Pointer, n uintptr) {
|
|
|
|
madvise(v, n, _MADV_FREE)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysUsed(v unsafe.Pointer, n uintptr) {
|
|
|
|
}
|
|
|
|
|
2015-04-16 15:32:18 -06:00
|
|
|
// Don't split the stack as this function may be invoked without a valid G,
|
|
|
|
// which prevents us from allocating more stack.
|
|
|
|
//go:nosplit
|
|
|
|
func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
|
|
|
|
mSysStatDec(sysStat, n)
|
2014-11-11 21:00:29 -07:00
|
|
|
munmap(v, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysFault(v unsafe.Pointer, n uintptr) {
|
|
|
|
mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
|
|
|
|
// On 64-bit, people with ulimit -v set complain if we reserve too
|
2016-03-01 16:21:55 -07:00
|
|
|
// much address space. Instead, assume that the reservation is okay
|
2014-11-11 21:00:29 -07:00
|
|
|
// and check the assumption in SysMap.
|
2015-11-11 10:39:30 -07:00
|
|
|
if sys.PtrSize == 8 && uint64(n) > 1<<32 || sys.GoosNacl != 0 {
|
2014-11-11 21:00:29 -07:00
|
|
|
*reserved = false
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2017-10-16 18:28:29 -06:00
|
|
|
p, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
|
|
|
|
if err != 0 {
|
2014-11-11 21:00:29 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
*reserved = true
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2017-05-14 10:42:35 -06:00
|
|
|
const _sunosEAGAIN = 11
|
2016-02-10 22:46:51 -07:00
|
|
|
const _ENOMEM = 12
|
2014-11-11 21:00:29 -07:00
|
|
|
|
2016-02-10 22:46:51 -07:00
|
|
|
func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
|
2015-04-16 15:32:18 -06:00
|
|
|
mSysStatInc(sysStat, n)
|
2014-11-11 21:00:29 -07:00
|
|
|
|
|
|
|
// On 64-bit, we don't actually have v reserved, so tread carefully.
|
|
|
|
if !reserved {
|
|
|
|
flags := int32(_MAP_ANON | _MAP_PRIVATE)
|
|
|
|
if GOOS == "dragonfly" {
|
|
|
|
// TODO(jsing): For some reason DragonFly seems to return
|
|
|
|
// memory at a different address than we requested, even when
|
|
|
|
// there should be no reason for it to do so. This can be
|
|
|
|
// avoided by using MAP_FIXED, but I'm not sure we should need
|
|
|
|
// to do this - we do not on other platforms.
|
|
|
|
flags |= _MAP_FIXED
|
|
|
|
}
|
2017-10-16 18:28:29 -06:00
|
|
|
p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, flags, -1, 0)
|
|
|
|
if err == _ENOMEM || (GOOS == "solaris" && err == _sunosEAGAIN) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: out of memory")
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2017-10-16 18:28:29 -06:00
|
|
|
if p != v || err != 0 {
|
|
|
|
print("runtime: address space conflict: map(", v, ") = ", p, "(err ", err, ")\n")
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: address space conflict")
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-16 18:28:29 -06:00
|
|
|
p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
|
|
|
|
if err == _ENOMEM || (GOOS == "solaris" && err == _sunosEAGAIN) {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: out of memory")
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
2017-10-16 18:28:29 -06:00
|
|
|
if p != v || err != 0 {
|
2014-12-27 21:58:00 -07:00
|
|
|
throw("runtime: cannot map pages in arena address space")
|
2014-11-11 21:00:29 -07:00
|
|
|
}
|
|
|
|
}
|