blob: bfb405607eaf6b3105873fb7d86f34f85a2291db [file] [log] [blame]
Colin Cross7bb052a2015-02-03 12:59:37 -08001// 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#include "runtime.h"
6#include "arch_GOARCH.h"
7#include "defs_GOOS_GOARCH.h"
8#include "os_GOOS.h"
9#include "malloc.h"
10#include "textflag.h"
11
12enum
13{
14 _PAGE_SIZE = 4096,
15 EACCES = 13,
16};
17
18static int32
19addrspace_free(void *v, uintptr n)
20{
21 int32 errval;
22 uintptr chunk;
23 uintptr off;
24
25 // NOTE: vec must be just 1 byte long here.
26 // Mincore returns ENOMEM if any of the pages are unmapped,
27 // but we want to know that all of the pages are unmapped.
28 // To make these the same, we can only ask about one page
29 // at a time. See golang.org/issue/7476.
30 static byte vec[1];
31
32 for(off = 0; off < n; off += chunk) {
33 chunk = _PAGE_SIZE * sizeof vec;
34 if(chunk > (n - off))
35 chunk = n - off;
36 errval = runtime·mincore((int8*)v + off, chunk, vec);
37 // ENOMEM means unmapped, which is what we want.
38 // Anything else we assume means the pages are mapped.
39 if (errval != -ENOMEM)
40 return 0;
41 }
42 return 1;
43}
44
45static void *
46mmap_fixed(byte *v, uintptr n, int32 prot, int32 flags, int32 fd, uint32 offset)
47{
48 void *p;
49
50 p = runtime·mmap(v, n, prot, flags, fd, offset);
51 if(p != v && addrspace_free(v, n)) {
52 // On some systems, mmap ignores v without
53 // MAP_FIXED, so retry if the address space is free.
54 if(p > (void*)4096)
55 runtime·munmap(p, n);
56 p = runtime·mmap(v, n, prot, flags|MAP_FIXED, fd, offset);
57 }
58 return p;
59}
60
61#pragma textflag NOSPLIT
62void*
63runtime·sysAlloc(uintptr n, uint64 *stat)
64{
65 void *p;
66
67 p = runtime·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
68 if(p < (void*)4096) {
69 if(p == (void*)EACCES) {
70 runtime·printf("runtime: mmap: access denied\n");
71 runtime·printf("if you're running SELinux, enable execmem for this process.\n");
72 runtime·exit(2);
73 }
74 if(p == (void*)EAGAIN) {
75 runtime·printf("runtime: mmap: too much locked memory (check 'ulimit -l').\n");
76 runtime·exit(2);
77 }
78 return nil;
79 }
80 runtime·xadd64(stat, n);
81 return p;
82}
83
84void
85runtime·SysUnused(void *v, uintptr n)
86{
87 runtime·madvise(v, n, MADV_DONTNEED);
88}
89
90void
91runtime·SysUsed(void *v, uintptr n)
92{
93 USED(v);
94 USED(n);
95}
96
97void
98runtime·SysFree(void *v, uintptr n, uint64 *stat)
99{
100 runtime·xadd64(stat, -(uint64)n);
101 runtime·munmap(v, n);
102}
103
104void
105runtime·SysFault(void *v, uintptr n)
106{
107 runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
108}
109
110void*
111runtime·SysReserve(void *v, uintptr n, bool *reserved)
112{
113 void *p;
114
115 // On 64-bit, people with ulimit -v set complain if we reserve too
116 // much address space. Instead, assume that the reservation is okay
117 // if we can reserve at least 64K and check the assumption in SysMap.
118 // Only user-mode Linux (UML) rejects these requests.
119 if(sizeof(void*) == 8 && n > 1LL<<32) {
120 p = mmap_fixed(v, 64<<10, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
121 if (p != v) {
122 if(p >= (void*)4096)
123 runtime·munmap(p, 64<<10);
124 return nil;
125 }
126 runtime·munmap(p, 64<<10);
127 *reserved = false;
128 return v;
129 }
130
131 p = runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
132 if((uintptr)p < 4096)
133 return nil;
134 *reserved = true;
135 return p;
136}
137
138void
139runtime·SysMap(void *v, uintptr n, bool reserved, uint64 *stat)
140{
141 void *p;
142
143 runtime·xadd64(stat, n);
144
145 // On 64-bit, we don't actually have v reserved, so tread carefully.
146 if(!reserved) {
147 p = mmap_fixed(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
148 if(p == (void*)ENOMEM)
149 runtime·throw("runtime: out of memory");
150 if(p != v) {
151 runtime·printf("runtime: address space conflict: map(%p) = %p\n", v, p);
152 runtime·throw("runtime: address space conflict");
153 }
154 return;
155 }
156
157 p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0);
158 if(p == (void*)ENOMEM)
159 runtime·throw("runtime: out of memory");
160 if(p != v)
161 runtime·throw("runtime: cannot map pages in arena address space");
162}