OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [runtime/] [mem.c] - Blame information for rev 801

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
/* Defining _XOPEN_SOURCE hides the declaration of madvise() on Solaris <
2
   11 and the MADV_DONTNEED definition on IRIX 6.5.  */
3
#undef _XOPEN_SOURCE
4
 
5
#include <errno.h>
6
#include <unistd.h>
7
 
8
#include "runtime.h"
9
#include "arch.h"
10
#include "malloc.h"
11
 
12
#ifndef MAP_ANON
13
#ifdef MAP_ANONYMOUS
14
#define MAP_ANON MAP_ANONYMOUS
15
#else
16
#define USE_DEV_ZERO
17
#define MAP_ANON 0
18
#endif
19
#endif
20
 
21
#ifdef USE_DEV_ZERO
22
static int dev_zero = -1;
23
#endif
24
 
25
static _Bool
26
addrspace_free(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
27
{
28
#ifdef HAVE_MINCORE
29
        size_t page_size = getpagesize();
30
        size_t off;
31
        char one_byte;
32
 
33
        errno = 0;
34
        for(off = 0; off < n; off += page_size)
35
                if(mincore((char *)v + off, page_size, (void *)&one_byte) != -1
36
                   || errno != ENOMEM)
37
                        return 0;
38
#endif
39
        return 1;
40
}
41
 
42
void*
43
runtime_SysAlloc(uintptr n)
44
{
45
        void *p;
46
        int fd = -1;
47
 
48
        mstats.sys += n;
49
 
50
#ifdef USE_DEV_ZERO
51
        if (dev_zero == -1) {
52
                dev_zero = open("/dev/zero", O_RDONLY);
53
                if (dev_zero < 0) {
54
                        runtime_printf("open /dev/zero: errno=%d\n", errno);
55
                        exit(2);
56
                }
57
        }
58
        fd = dev_zero;
59
#endif
60
 
61
        p = runtime_mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
62
        if (p == MAP_FAILED) {
63
                if(errno == EACCES) {
64
                        runtime_printf("runtime: mmap: access denied\n");
65
                        runtime_printf("if you're running SELinux, enable execmem for this process.\n");
66
                        exit(2);
67
                }
68
                return nil;
69
        }
70
        return p;
71
}
72
 
73
void
74
runtime_SysUnused(void *v __attribute__ ((unused)), uintptr n __attribute__ ((unused)))
75
{
76
#ifdef MADV_DONTNEED
77
        runtime_madvise(v, n, MADV_DONTNEED);
78
#endif
79
}
80
 
81
void
82
runtime_SysFree(void *v, uintptr n)
83
{
84
        mstats.sys -= n;
85
        runtime_munmap(v, n);
86
}
87
 
88
void*
89
runtime_SysReserve(void *v, uintptr n)
90
{
91
        int fd = -1;
92
        void *p;
93
 
94
        // On 64-bit, people with ulimit -v set complain if we reserve too
95
        // much address space.  Instead, assume that the reservation is okay
96
        // and check the assumption in SysMap.
97
        if(sizeof(void*) == 8)
98
                return v;
99
 
100
#ifdef USE_DEV_ZERO
101
        if (dev_zero == -1) {
102
                dev_zero = open("/dev/zero", O_RDONLY);
103
                if (dev_zero < 0) {
104
                        runtime_printf("open /dev/zero: errno=%d\n", errno);
105
                        exit(2);
106
                }
107
        }
108
        fd = dev_zero;
109
#endif
110
 
111
        p = runtime_mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, fd, 0);
112
        if((uintptr)p < 4096 || -(uintptr)p < 4096) {
113
                return nil;
114
        }
115
        return p;
116
}
117
 
118
void
119
runtime_SysMap(void *v, uintptr n)
120
{
121
        void *p;
122
        int fd = -1;
123
 
124
        mstats.sys += n;
125
 
126
#ifdef USE_DEV_ZERO
127
        if (dev_zero == -1) {
128
                dev_zero = open("/dev/zero", O_RDONLY);
129
                if (dev_zero < 0) {
130
                        runtime_printf("open /dev/zero: errno=%d\n", errno);
131
                        exit(2);
132
                }
133
        }
134
        fd = dev_zero;
135
#endif
136
 
137
        // On 64-bit, we don't actually have v reserved, so tread carefully.
138
        if(sizeof(void*) == 8) {
139
                p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, fd, 0);
140
                if(p != v && addrspace_free(v, n)) {
141
                        // On some systems, mmap ignores v without
142
                        // MAP_FIXED, so retry if the address space is free.
143
                        p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
144
                }
145
                if(p != v) {
146
                        runtime_printf("runtime: address space conflict: map(%p) = %p\n", v, p);
147
                        runtime_throw("runtime: address space conflict");
148
                }
149
                return;
150
        }
151
 
152
        p = runtime_mmap(v, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_FIXED|MAP_PRIVATE, fd, 0);
153
        if(p != v)
154
                runtime_throw("runtime: cannot map pages in arena address space");
155
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.