-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathsysdeps_solo5.c
175 lines (151 loc) · 3.85 KB
/
sysdeps_solo5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/times.h>
#include <unistd.h>
#include <solo5.h>
/*
* Global errno lives in this module.
*/
int errno;
/*
* Standard output and error "streams".
*/
static size_t console_write(FILE *f __attribute__((unused)), const char *s,
size_t l)
{
solo5_console_write(s, l);
return l;
}
static FILE console = { .write = console_write };
FILE *stderr = &console;
FILE *stdout = &console;
ssize_t write(int fd, const void *buf, size_t count)
{
if (fd == 1 || fd == 2) {
solo5_console_write(buf, count);
return count;
}
errno = ENOSYS;
return -1;
}
void exit(int status)
{
solo5_exit(status);
}
void abort(void)
{
solo5_console_write("Aborted\n", 8);
solo5_abort();
}
/*
* System time.
*/
#define NSEC_PER_SEC 1000000000ULL
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
if (tv != NULL) {
solo5_time_t now = solo5_clock_wall();
tv->tv_sec = now / NSEC_PER_SEC;
tv->tv_usec = (now % NSEC_PER_SEC) / 1000ULL;
}
if (tz != NULL) {
memset(tz, 0, sizeof(*tz));
}
return 0;
}
clock_t times(struct tms *buf)
{
memset(buf, 0, sizeof(*buf));
return (clock_t)solo5_clock_monotonic();
}
static uintptr_t sbrk_start;
static uintptr_t sbrk_end;
static uintptr_t sbrk_cur;
static uintptr_t sbrk_guard_size;
/*
* To be called by Mirage/Solo5 before calling caml_startup().
*
* XXX: There is intentionally no public prototype for this function. There
* should really be a caml_solo5_startup(), but I'm lazy and don't have
* a proper place to put it in the build system right now.
*/
void _nolibc_init(uintptr_t heap_start, size_t heap_size)
{
/*
* If we have <1MB of heap available at init time then don't let the heap
* grow to within (heap_size / 2) of the stack, otherwise don't let it
* grow to within 1MB of the stack.
*/
sbrk_guard_size = (heap_size >= 0x100000) ?
0x100000 : (heap_size / 2);
sbrk_start = sbrk_cur = heap_start;
sbrk_end = heap_start + heap_size;
}
/*
* Called by dlmalloc to allocate or free memory.
*/
void *sbrk(intptr_t increment)
{
uintptr_t prev, brk;
uintptr_t max = (uintptr_t)&prev - sbrk_guard_size;
prev = brk = sbrk_cur;
/*
* dlmalloc guarantees increment values less than half of size_t, so this
* is safe from overflow.
*/
brk += increment;
if (brk >= max || brk >= sbrk_end || brk < sbrk_start)
return (void *)-1;
sbrk_cur = brk;
return (void *)prev;
}
/*
* On ARM64, Linux's libgcc requires the presence of a __getauxval function in
* the libc. We stub it out.
*/
#if defined(__aarch64__)
int __getauxval(int unused) {
errno = ENOENT;
return 0;
}
#endif
/*
* dlmalloc configuration:
*/
/*
* DEBUG not defined and assertions enabled corresponds to the recommended
* configuration as our assert() does not call malloc(). (see documentation in
* dlmalloc.i). If you need to debug dlmalloc on Solo5 then define DEBUG to `1'
* here.
*/
#include <assert.h>
#define ABORT_ON_ASSERT_FAILURE 0
#undef WIN32
#define HAVE_MMAP 0
#define HAVE_MREMAP 0
#define MMAP_CLEARS 0
#define NO_MALLOC_STATS 1
#define LACKS_FCNTL_H
#define LACKS_SYS_PARAM_H
#define LACKS_SYS_MMAN_H
#define LACKS_STRINGS_H
#define LACKS_SYS_TYPES_H
#define LACKS_SCHED_H
#define LACKS_TIME_H
#define MALLOC_FAILURE_ACTION
#define USE_LOCKS 0
#define STRUCT_MALLINFO_DECLARED 1
#define FOOTERS 1
/* disable null-pointer-arithmetic warning on clang */
#if defined(__clang__) && __clang_major__ >= 6
#pragma clang diagnostic ignored "-Wnull-pointer-arithmetic"
#endif
/* inline the dlmalloc implementation into this module */
#include "dlmalloc.i"
/*
* When adding new functions to this module, add them BEFORE the "dlmalloc
* configuration" comment above, not here.
*/