github.com/riscv/riscv-go@v0.0.0-20200123204226-124ebd6fcc8e/src/runtime/mem_bsd.go (about)

     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 nacl netbsd openbsd solaris
     6  
     7  package runtime
     8  
     9  import (
    10  	"runtime/internal/sys"
    11  	"unsafe"
    12  )
    13  
    14  // Don't split the stack as this function may be invoked without a valid G,
    15  // which prevents us from allocating more stack.
    16  //go:nosplit
    17  func sysAlloc(n uintptr, sysStat *uint64) unsafe.Pointer {
    18  	v := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    19  	if uintptr(v) < 4096 {
    20  		return nil
    21  	}
    22  	mSysStatInc(sysStat, n)
    23  	return v
    24  }
    25  
    26  func sysUnused(v unsafe.Pointer, n uintptr) {
    27  	madvise(v, n, _MADV_FREE)
    28  }
    29  
    30  func sysUsed(v unsafe.Pointer, n uintptr) {
    31  }
    32  
    33  // Don't split the stack as this function may be invoked without a valid G,
    34  // which prevents us from allocating more stack.
    35  //go:nosplit
    36  func sysFree(v unsafe.Pointer, n uintptr, sysStat *uint64) {
    37  	mSysStatDec(sysStat, n)
    38  	munmap(v, n)
    39  }
    40  
    41  func sysFault(v unsafe.Pointer, n uintptr) {
    42  	mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE|_MAP_FIXED, -1, 0)
    43  }
    44  
    45  func sysReserve(v unsafe.Pointer, n uintptr, reserved *bool) unsafe.Pointer {
    46  	// On 64-bit, people with ulimit -v set complain if we reserve too
    47  	// much address space. Instead, assume that the reservation is okay
    48  	// and check the assumption in SysMap.
    49  	if sys.PtrSize == 8 && uint64(n) > 1<<32 || sys.GoosNacl != 0 {
    50  		*reserved = false
    51  		return v
    52  	}
    53  
    54  	p := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
    55  	if uintptr(p) < 4096 {
    56  		return nil
    57  	}
    58  	*reserved = true
    59  	return p
    60  }
    61  
    62  const _ENOMEM = 12
    63  
    64  func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
    65  	mSysStatInc(sysStat, n)
    66  
    67  	// On 64-bit, we don't actually have v reserved, so tread carefully.
    68  	if !reserved {
    69  		flags := int32(_MAP_ANON | _MAP_PRIVATE)
    70  		if GOOS == "dragonfly" {
    71  			// TODO(jsing): For some reason DragonFly seems to return
    72  			// memory at a different address than we requested, even when
    73  			// there should be no reason for it to do so. This can be
    74  			// avoided by using MAP_FIXED, but I'm not sure we should need
    75  			// to do this - we do not on other platforms.
    76  			flags |= _MAP_FIXED
    77  		}
    78  		p := mmap(v, n, _PROT_READ|_PROT_WRITE, flags, -1, 0)
    79  		if uintptr(p) == _ENOMEM {
    80  			throw("runtime: out of memory")
    81  		}
    82  		if p != v {
    83  			print("runtime: address space conflict: map(", v, ") = ", p, "\n")
    84  			throw("runtime: address space conflict")
    85  		}
    86  		return
    87  	}
    88  
    89  	p := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
    90  	if uintptr(p) == _ENOMEM {
    91  		throw("runtime: out of memory")
    92  	}
    93  	if p != v {
    94  		throw("runtime: cannot map pages in arena address space")
    95  	}
    96  }