-
Notifications
You must be signed in to change notification settings - Fork 0
/
boot.h
201 lines (163 loc) · 4.28 KB
/
boot.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* boot.h - boot memory and overall init
This file is part of yalloc, yet another memory allocator providing affordable safety in a compact package.
SPDX-FileCopyrightText: © 2024 Joris van der Geer
SPDX-License-Identifier: GPL-3.0-or-later
Provide memory used to initiate heap structures, saving on syscalls.
Also one-time init. This is triggered by the first API call of the first thread
*/
#define Fln (Fboot << 16) | __LINE__
static Cold void diag_initrace(void); // diag.h
#if __STDC_VERSION__ < 201112L // c99
static void assert_fail(ub4 fln,cchar *msg)
{
minidiag(fln,Lnone,Assert,0,"assertion failed: %s",msg);
}
#endif
#define Bootcnt 4
struct bootmem {
ub1 inimem[Bootmem];
_Atomic size_t pos;
_Atomic size_t mem;
_Atomic ub4 lock;
_Atomic ub4 allocs,mmaps,nolocks; // stats
};
static struct bootmem bootmems[Bootcnt];
// bump allocator from canned, yet expanding pool. Used for heap base and global directory
static void *bootalloc(ub4 fln,ub4 id,enum Loc loc,ub4 ulen)
{
static_assert(Bootmem <= Hi24,"Bootmem < 16M");
static_assert(Bootmem < Pagesize,"Bootmem >= Page");
struct bootmem *bootp;
void *np;
size_t imp,inp,pos;
ub4 len;
ub4 zero;
ub4 iter = 8;
bool didcas;
// minidiag(fln,loc,Info,id,"boot alloc %u",ulen);
bootp = bootmems + (id & 3); // limit contention
if (ulen == 0) {
minidiag(fln,loc,Warn,id,"(file coords)");
minidiag(Fln,loc,Assert,id,"bootalloc(0)");
return nil;
}
len = doalign4(ulen,Stdalign);
if (len >= Bootmem) {
Atomad(bootp->mmaps,1,Monone);
return osmmap(len);
}
Atomad(bootp->allocs,1,Monone);
do {
zero = 0;
didcas = Cas(bootp->lock,zero,1);
} while (didcas == 0 && --iter);
if (didcas == 0) {
Atomad(bootp->nolocks,1,Monone);
return osmmap(len); // fallback
}
vg_drd_wlock_acq(bootp)
imp = Atomget(bootp->mem,Moacq);
if (imp == 0) {
imp = (size_t)bootp->inimem;
Atomset(bootp->mem,imp,Morel);
}
pos = Atomget(bootp->pos,Moacq);
if (pos + len <= Bootmem) { // enough space
Atomset(bootp->pos,pos + len,Morel);
Atomset(bootp->lock,0,Morel);
vg_drd_wlock_rel(bootp)
return (void *)(imp + pos);
}
Atomad(bootp->mmaps,1,Monone);
np = osmmap(Bootmem); // expand
inp = (size_t)np;
if (inp == 0) {
minidiag(fln,loc,Fatal,id,"out of memory allocating %u bytes from boot memory",len);
} else {
Atomset(bootp->mem,inp,Morel);
Atomset(bootp->pos,len,Morel);
}
Atomset(bootp->lock,0,Morel);
vg_drd_wlock_rel(bootp)
return np;
}
#ifdef Yal_stats_envvar
static void at_exit(void)
{
ub4 opt = global_stats_opt | Yal_stats_print | Yal_stats_totals;
yal_mstats(nil,opt,Fln,"atexit");
}
#endif
static void setsigs(void); // dbg.h
static ub4 init_stats(ub4 uval)
{
ub4 prv = global_stats_opt;
#if Yal_enable_stats
cchar *envs;
ub4 val = 0;
if (uval != Hi32) val = uval;
else {
if (prv) return prv; // set earlier
envs = getenv(Yal_stats_envvar);
if (envs) val = atou(envs);
}
if (val) {
minidiag(Fln,Lnone,Vrb,0,"stats %u,%u",val,uval);
setsigs(); // also an exit point
atexit(at_exit);
}
prv = global_stats_opt;
global_stats_opt = val;
#endif
return prv;
}
static void init_trace(void)
{
#if Yal_enable_trace
cchar *envs = nil;
ub4 val = global_trace;
if (val & 16) val &= 15;// from options
else {
envs = getenv(Yal_trace_envvar);
if (envs == nil) return;
val = atou(envs);
minidiag(Fln,Lnone,Vrb,0,"trace %u",val);
}
global_trace = val & 15;
if (val & 4) { diag_initrace(); }
#endif
}
static void init_check(void)
{
cchar *envs = nil;
ub4 val = Yal_check_default;
ub4 page;
#ifdef Yal_check_envvar
envs = getenv(Yal_check_envvar);
#endif
if (envs) val = atou(envs);
global_check = val;
#if Yal_enable_check
page = ospagesize();
if (page != Pagesize) minidiag(Fln,Lnone,Assert,0,"os page size %u, configured %u",page,Pagesize);
#endif
}
static void init_env(void)
{
unsigned long pid = ospid();
Atomset(global_pid,pid,Monone);
global_zeroblock = osmmap(Pagesize);
vg_mem_noaccess((void *)global_zeroblock,Pagesize);
#ifdef __linux__
int fd = osopen("/proc/self/cmdline",nil);
if (fd != -1) {
osread(fd,global_cmdline,255);
osclose(fd);
}
#endif
setsigs();
init_check();
init_trace();
init_stats(Hi32);
}
#undef Fln