Home | History | Annotate | Download | only in runtime
      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, err := mmap(nil, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
     19 	if err != 0 {
     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, err := mmap(v, n, _PROT_NONE, _MAP_ANON|_MAP_PRIVATE, -1, 0)
     55 	if err != 0 {
     56 		return nil
     57 	}
     58 	*reserved = true
     59 	return p
     60 }
     61 
     62 const _sunosEAGAIN = 11
     63 const _ENOMEM = 12
     64 
     65 func sysMap(v unsafe.Pointer, n uintptr, reserved bool, sysStat *uint64) {
     66 	mSysStatInc(sysStat, n)
     67 
     68 	// On 64-bit, we don't actually have v reserved, so tread carefully.
     69 	if !reserved {
     70 		flags := int32(_MAP_ANON | _MAP_PRIVATE)
     71 		if GOOS == "dragonfly" {
     72 			// TODO(jsing): For some reason DragonFly seems to return
     73 			// memory at a different address than we requested, even when
     74 			// there should be no reason for it to do so. This can be
     75 			// avoided by using MAP_FIXED, but I'm not sure we should need
     76 			// to do this - we do not on other platforms.
     77 			flags |= _MAP_FIXED
     78 		}
     79 		p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, flags, -1, 0)
     80 		if err == _ENOMEM || (GOOS == "solaris" && err == _sunosEAGAIN) {
     81 			throw("runtime: out of memory")
     82 		}
     83 		if p != v || err != 0 {
     84 			print("runtime: address space conflict: map(", v, ") = ", p, "(err ", err, ")\n")
     85 			throw("runtime: address space conflict")
     86 		}
     87 		return
     88 	}
     89 
     90 	p, err := mmap(v, n, _PROT_READ|_PROT_WRITE, _MAP_ANON|_MAP_FIXED|_MAP_PRIVATE, -1, 0)
     91 	if err == _ENOMEM || (GOOS == "solaris" && err == _sunosEAGAIN) {
     92 		throw("runtime: out of memory")
     93 	}
     94 	if p != v || err != 0 {
     95 		throw("runtime: cannot map pages in arena address space")
     96 	}
     97 }
     98