2016-03-01 22:57:46 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
2014-11-11 23:00:29 -05:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2018-09-12 14:18:45 +02:00
|
|
|
// +build darwin dragonfly freebsd nacl netbsd openbsd solaris
|
2014-11-11 23:00:29 -05:00
|
|
|
|
|
|
|
|
package runtime
|
|
|
|
|
|
2015-11-11 12:39:30 -05:00
|
|
|
import (
|
|
|
|
|
"unsafe"
|
|
|
|
|
)
|
2014-11-11 23:00:29 -05:00
|
|
|
|
2015-04-16 14:32:18 -07: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 23:00:29 -05:00
|
|
|
//go:nosplit
|
2015-04-16 14:32:18 -07:00
|
|
|
func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
|
2017-10-16 20:28:29 -04:00
|
|
|
v, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
|
|
|
|
|
if err != 0 {
|
2014-11-11 23:00:29 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
2015-04-16 14:32:18 -07:00
|
|
|
mSysStatInc(sysStat, n)
|
2014-11-11 23:00:29 -05: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 14:32:18 -07: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 23:00:29 -05:00
|
|
|
munmap(v, n)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func sysFault(v unsafe.Pointer, n uintptr) {
|
|
|
|
|
mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
|
|
|
|
|
}
|
|
|
|
|
|
runtime: remove non-reserved heap logic
Currently large sysReserve calls on some OSes don't actually reserve
the memory, but just check that it can be reserved. This was important
when we called sysReserve to "reserve" many gigabytes for the heap up
front, but now that we map memory in small increments as we need it,
this complication is no longer necessary.
This has one curious side benefit: currently, on Linux, allocations
that are large enough to be rejected by mmap wind up freezing the
application for a long time before it panics. This happens because
sysReserve doesn't reserve the memory, so sysMap calls mmap_fixed,
which calls mmap, which fails because the mapping is too large.
However, mmap_fixed doesn't inspect *why* mmap fails, so it falls back
to probing every page in the desired region individually with mincore
before performing an (otherwise dangerous) MAP_FIXED mapping, which
will also fail. This takes a long time for a large region. Now this
logic is gone, so the mmap failure leads to an immediate panic.
Updates #10460.
Change-Id: I8efe88c611871cdb14f99fadd09db83e0161ca2e
Reviewed-on: https://go-review.googlesource.com/85888
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-12-30 19:35:46 -05:00
|
|
|
func sysReserve(v unsafe.Pointer, n uintptr) unsafe.Pointer {
|
2018-12-11 22:03:04 +00:00
|
|
|
flags := int32(_MAP_ANON | _MAP_PRIVATE)
|
2018-12-19 17:32:22 +00:00
|
|
|
if raceenabled && GOOS == "darwin" {
|
2018-12-11 22:03:04 +00:00
|
|
|
// Currently the race detector expects memory to live within a certain
|
|
|
|
|
// range, and on Darwin 10.10 mmap is prone to ignoring hints, moreso
|
|
|
|
|
// than later versions and other BSDs (#26475). So, even though it's
|
|
|
|
|
// potentially dangerous to MAP_FIXED, we do it in the race detection
|
|
|
|
|
// case because it'll help maintain the race detector's invariants.
|
|
|
|
|
//
|
|
|
|
|
// TODO(mknyszek): Drop this once support for Darwin 10.10 is dropped,
|
|
|
|
|
// and reconsider this when #24133 is addressed.
|
|
|
|
|
flags |= _MAP_FIXED
|
|
|
|
|
}
|
|
|
|
|
p, err := mmap(v, n, _PROT_NONE, flags, -1, 0)
|
2017-10-16 20:28:29 -04:00
|
|
|
if err != 0 {
|
2014-11-11 23:00:29 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return p
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-14 09:42:35 -07:00
|
|
|
const _sunosEAGAIN = 11
|
2016-02-10 21:46:51 -08:00
|
|
|
const _ENOMEM = 12
|
2014-11-11 23:00:29 -05:00
|
|
|
|
runtime: remove non-reserved heap logic
Currently large sysReserve calls on some OSes don't actually reserve
the memory, but just check that it can be reserved. This was important
when we called sysReserve to "reserve" many gigabytes for the heap up
front, but now that we map memory in small increments as we need it,
this complication is no longer necessary.
This has one curious side benefit: currently, on Linux, allocations
that are large enough to be rejected by mmap wind up freezing the
application for a long time before it panics. This happens because
sysReserve doesn't reserve the memory, so sysMap calls mmap_fixed,
which calls mmap, which fails because the mapping is too large.
However, mmap_fixed doesn't inspect *why* mmap fails, so it falls back
to probing every page in the desired region individually with mincore
before performing an (otherwise dangerous) MAP_FIXED mapping, which
will also fail. This takes a long time for a large region. Now this
logic is gone, so the mmap failure leads to an immediate panic.
Updates #10460.
Change-Id: I8efe88c611871cdb14f99fadd09db83e0161ca2e
Reviewed-on: https://go-review.googlesource.com/85888
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rick Hudson <rlh@golang.org>
2017-12-30 19:35:46 -05:00
|
|
|
func sysMap(v unsafe.Pointer, n uintptr, sysStat *uint64) {
|
2015-04-16 14:32:18 -07:00
|
|
|
mSysStatInc(sysStat, n)
|
2014-11-11 23:00:29 -05:00
|
|
|
|
2017-10-16 20:28:29 -04: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 20:58:00 -08:00
|
|
|
throw("runtime: out of memory")
|
2014-11-11 23:00:29 -05:00
|
|
|
}
|
2017-10-16 20:28:29 -04:00
|
|
|
if p != v || err != 0 {
|
2014-12-27 20:58:00 -08:00
|
|
|
throw("runtime: cannot map pages in arena address space")
|
2014-11-11 23:00:29 -05:00
|
|
|
}
|
|
|
|
|
}
|