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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* basic types
*/
typedef signed char int8;
typedef unsigned char uint8;
typedef signed short int16;
typedef unsigned short uint16;
typedef signed int int32;
typedef unsigned int uint32;
typedef signed long long int int64;
typedef unsigned long long int uint64;
typedef float float32;
typedef double float64;
typedef uint64 uintptr;
/*
* get rid of C types
* the / / / forces a syntax error immediately,
* which will show "last name: XXunsigned".
*/
#define unsigned XXunsigned / / /
#define signed XXsigned / / /
#define char XXchar / / /
#define short XXshort / / /
#define int XXint / / /
#define long XXlong / / /
#define float XXfloat / / /
#define double XXdouble / / /
/*
* defined types
*/
typedef uint8 bool;
typedef uint8 byte;
typedef struct Alg Alg;
typedef struct Array Array;
typedef struct Func Func;
typedef struct G G;
typedef struct Gobuf Gobuf;
typedef struct Lock Lock;
typedef struct M M;
typedef struct Mem Mem;
typedef union Note Note;
typedef struct Stktop Stktop;
typedef struct String *string;
typedef struct Usema Usema;
/*
* per cpu declaration
*/
extern register G* g; // R15
extern register M* m; // R14
/*
* defined constants
*/
enum
{
// G status
Gidle,
Grunnable,
Grunning,
Gwaiting,
Gmoribund,
Gdead,
};
enum
{
true = 1,
false = 0,
};
enum
{
SmallFreeClasses = 168, // number of small free lists in malloc
};
/*
* structures
*/
struct Lock
{
uint32 key;
uint32 sema; // for OS X
};
struct Usema
{
uint32 u;
uint32 k;
};
union Note
{
struct { // Linux
Lock lock;
};
struct { // OS X
int32 wakeup;
Usema sema;
};
};
struct String
{
int32 len;
byte str[1];
};
struct Array
{ // must not move anything
byte* array; // actual data
uint32 nel; // number of elements
uint32 cap; // allocated number of elements
byte b[8]; // actual array - may not be contig
};
struct Gobuf
{
byte* SP;
byte* PC;
};
struct G
{
byte* stackguard; // must not move
byte* stackbase; // must not move
byte* stack0; // first stack segment
Gobuf sched;
G* alllink; // on allg
void* param; // passed parameter on wakeup
int16 status;
int32 goid;
int32 selgen; // valid sudog pointer
G* schedlink;
bool readyonstop;
M* m; // for debuggers
};
struct Mem
{
uint8* hunk;
uint32 nhunk;
uint64 nmmap;
uint64 nmal;
};
struct M
{
G* g0; // g0 w interrupt stack - must not move
uint64 morearg; // arg to morestack - must not move
uint64 cret; // return value from C - must not move
uint64 procid; // for debuggers - must not move
G* gsignal; // signal-handling G - must not move
G* curg; // current running goroutine
G* lastg; // last running goroutine - to emulate fifo
Gobuf sched;
Gobuf morestack;
byte* moresp;
int32 siz1;
int32 siz2;
Note havenextg;
G* nextg;
M* schedlink;
Mem mem;
uint32 machport; // Return address for Mach IPC (OS X)
void* freelist[SmallFreeClasses];
};
struct Stktop
{
uint8* oldbase;
uint8* oldsp;
uint64 magic;
uint8* oldguard;
};
struct Alg
{
uint64 (*hash)(uint32, void*);
uint32 (*equal)(uint32, void*, void*);
void (*print)(uint32, void*);
void (*copy)(uint32, void*, void*);
};
struct SigTab
{
int32 catch;
int8 *name;
};
// (will be) shared with go; edit ../cmd/6g/sys.go too.
// should move out of sys.go eventually.
// also eventually, the loaded symbol table should
// be closer to this form.
struct Func
{
string name;
string type;
uint64 entry;
int64 frame;
};
/*
* defined macros
* you need super-goru privilege
* to add this list.
*/
#define nelem(x) (sizeof(x)/sizeof((x)[0]))
#define nil ((void*)0)
/*
* external data
*/
extern Alg algarray[3];
extern string emptystring;
G* allg;
int32 goidgen;
extern int32 gomaxprocs;
extern int32 panicking;
extern int32 maxround;
/*
* common functions and data
*/
int32 strcmp(byte*, byte*);
int32 findnull(byte*);
void dump(byte*, int32);
int32 runetochar(byte*, int32);
int32 chartorune(uint32*, byte*);
/*
* very low level c-called
*/
int32 gogo(Gobuf*);
int32 gosave(Gobuf*);
int32 gogoret(Gobuf*, uint64);
void retfromnewstack(void);
void setspgoto(byte*, void(*)(void), void(*)(void));
void FLUSH(void*);
void* getu(void);
void throw(int8*);
uint32 rnd(uint32, uint32);
void prints(int8*);
byte* mchr(byte*, byte, byte*);
void mcpy(byte*, byte*, uint32);
void mmov(byte*, byte*, uint32);
void* mal(uint32);
uint32 cmpstring(string, string);
string gostring(byte*);
void initsig(void);
int32 gotraceback(void);
void traceback(uint8 *pc, uint8 *sp, G* gp);
void tracebackothers(G*);
int32 open(byte*, int32, ...);
int32 read(int32, void*, int32);
int32 write(int32, void*, int32);
void close(int32);
int32 fstat(int32, void*);
bool cas(uint32*, uint32, uint32);
void exit1(int32);
void ready(G*);
byte* getenv(int8*);
int32 atoi(byte*);
void newosproc(M *m, G *g, void *stk, void (*fn)(void));
void sigaltstack(void*, void*);
void signalstack(byte*, int32);
G* malg(int32);
void minit(void);
Func* findfunc(uint64);
/*
* mutual exclusion locks. in the uncontended case,
* as fast as spin locks (just a few user-level instructions),
* but on the contention path they sleep in the kernel.
* a zeroed Lock is unlocked (no need to initialize each lock).
*/
void lock(Lock*);
void unlock(Lock*);
/*
* sleep and wakeup on one-time events.
* before any calls to notesleep or notewakeup,
* must call noteclear to initialize the Note.
* then, any number of threads can call notesleep
* and exactly one thread can call notewakeup (once).
* once notewakeup has been called, all the notesleeps
* will return. future notesleeps will return immediately.
*/
void noteclear(Note*);
void notesleep(Note*);
void notewakeup(Note*);
/*
* low level go -called
*/
void sys·goexit(void);
void sys·gosched(void);
void sys·exit(int32);
void sys·write(int32, void*, int32);
void sys·breakpoint(void);
uint8* sys·mmap(byte*, uint32, int32, int32, int32, uint32);
void sys·memclr(byte*, uint32);
void sys·setcallerpc(void*, void*);
void* sys·getcallerpc(void*);
void sys·sigaction(int64, void*, void*);
void sys·rt_sigaction(int64, void*, void*, uint64);
/*
* runtime go-called
*/
void sys·printbool(bool);
void sys·printfloat(float64);
void sys·printint(int64);
void sys·printstring(string);
void sys·printpc(void*);
void sys·printpointer(void*);
void sys·catstring(string, string, string);
void sys·cmpstring(string, string, int32);
void sys·slicestring(string, int32, int32, string);
void sys·indexstring(string, int32, byte);
void sys·intstring(int64, string);
bool isInf(float64, int32);
bool isNaN(float64);
/*
* User go-called
*/
void sys·readfile(string, string, bool);
void sys·bytestorune(byte*, int32, int32, int32, int32);
void sys·stringtorune(string, int32, int32, int32);
|