OSDN Git Service

PR go/47515
[pf3gnuchains/gcc-fork.git] / libgo / runtime / mem.c
1 #include <errno.h>
2
3 #include "runtime.h"
4 #include "malloc.h"
5
6 #ifndef MAP_ANON
7 #ifdef MAP_ANONYMOUS
8 #define MAP_ANON MAP_ANONYMOUS
9 #else
10 #define USE_DEV_ZERO
11 #define MAP_ANON 0
12 #endif
13 #endif
14
15 #ifdef USE_DEV_ZERO
16 static int dev_zero = -1;
17 #endif
18
19 void*
20 runtime_SysAlloc(uintptr n)
21 {
22         void *p;
23         int fd = -1;
24
25         mstats.sys += n;
26
27 #ifdef USE_DEV_ZERO
28         if (dev_zero == -1) {
29                 dev_zero = open("/dev/zero", O_RDONLY);
30                 if (dev_zero < 0) {
31                         printf("open /dev/zero: errno=%d\n", errno);
32                         exit(2);
33                 }
34         }
35         fd = dev_zero;
36 #endif
37
38         p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
39         if (p == MAP_FAILED) {
40                 if(errno == EACCES) {
41                         printf("mmap: access denied\n");
42                         printf("If you're running SELinux, enable execmem for this process.\n");
43                 } else {
44                         printf("mmap: errno=%d\n", errno);
45                 }
46                 exit(2);
47         }
48         return p;
49 }
50
51 void
52 runtime_SysUnused(void *v, uintptr n)
53 {
54         USED(v);
55         USED(n);
56         // TODO(rsc): call madvise MADV_DONTNEED
57 }
58
59 void
60 runtime_SysFree(void *v, uintptr n)
61 {
62         mstats.sys -= n;
63         runtime_munmap(v, n);
64 }
65
66 void
67 runtime_SysMemInit(void)
68 {
69         // Code generators assume that references to addresses
70         // on the first page will fault.  Map the page explicitly with
71         // no permissions, to head off possible bugs like the system
72         // allocating that page as the virtual address space fills.
73         // Ignore any error, since other systems might be smart
74         // enough to never allow anything there.
75         runtime_mmap(nil, 4096, PROT_NONE, MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
76 }