blob: 94ff6d7ca077decd7d5ce9b7b734948ab3de2973 [file] [log] [blame]
Ken Thompsonbbb20732008-06-05 19:38:39 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Ken Thompsonbbb20732008-06-05 19:38:39 -07005/*
6 * basic types
7 */
8typedef signed char int8;
9typedef unsigned char uint8;
10typedef signed short int16;
11typedef unsigned short uint16;
12typedef signed int int32;
13typedef unsigned int uint32;
14typedef signed long long int int64;
15typedef unsigned long long int uint64;
16typedef float float32;
17typedef double float64;
Russ Cox75647d22008-11-17 12:32:35 -080018typedef uint64 uintptr;
Ken Thompsonbbb20732008-06-05 19:38:39 -070019
20/*
21 * get rid of C types
Russ Cox3aa063d2008-11-23 17:08:55 -080022 * the / / / forces a syntax error immediately,
23 * which will show "last name: XXunsigned".
Ken Thompsonbbb20732008-06-05 19:38:39 -070024 */
Russ Cox3aa063d2008-11-23 17:08:55 -080025#define unsigned XXunsigned / / /
26#define signed XXsigned / / /
27#define char XXchar / / /
28#define short XXshort / / /
29#define int XXint / / /
30#define long XXlong / / /
31#define float XXfloat / / /
32#define double XXdouble / / /
Ken Thompsonbbb20732008-06-05 19:38:39 -070033
34/*
35 * defined types
36 */
37typedef uint8 bool;
38typedef uint8 byte;
Ken Thompson594175d2008-07-13 14:29:46 -070039typedef struct Alg Alg;
Russ Cox3aa063d2008-11-23 17:08:55 -080040typedef struct Array Array;
41typedef struct Func Func;
42typedef struct G G;
43typedef struct Gobuf Gobuf;
Russ Coxd28acc42008-08-04 16:43:49 -070044typedef struct Lock Lock;
Russ Cox3aa063d2008-11-23 17:08:55 -080045typedef struct M M;
Russ Coxd28acc42008-08-04 16:43:49 -070046typedef struct Mem Mem;
Russ Cox3aa063d2008-11-23 17:08:55 -080047typedef union Note Note;
48typedef struct Stktop Stktop;
49typedef struct String *string;
50typedef struct Usema Usema;
Russ Coxdfa58932008-12-03 14:21:28 -080051typedef struct SigTab SigTab;
Ken Thompson594175d2008-07-13 14:29:46 -070052
53/*
54 * per cpu declaration
55 */
56extern register G* g; // R15
57extern register M* m; // R14
58
59/*
60 * defined constants
61 */
62enum
63{
64 // G status
65 Gidle,
66 Grunnable,
Russ Coxd28acc42008-08-04 16:43:49 -070067 Grunning,
Russ Cox3f8aa662008-12-05 15:24:18 -080068 Gsyscall,
Ken Thompson52620032008-07-14 14:33:39 -070069 Gwaiting,
Russ Cox96824002008-08-05 14:18:47 -070070 Gmoribund,
Ken Thompson594175d2008-07-13 14:29:46 -070071 Gdead,
72};
73enum
74{
75 true = 1,
76 false = 0,
77};
Russ Cox75647d22008-11-17 12:32:35 -080078enum
79{
80 SmallFreeClasses = 168, // number of small free lists in malloc
81};
Ken Thompson594175d2008-07-13 14:29:46 -070082
83/*
84 * structures
85 */
Russ Coxd28acc42008-08-04 16:43:49 -070086struct Lock
87{
88 uint32 key;
Russ Cox376898c2008-09-09 11:50:14 -070089 uint32 sema; // for OS X
Russ Coxd28acc42008-08-04 16:43:49 -070090};
Russ Cox5ff12f82008-09-24 10:25:28 -070091struct Usema
92{
93 uint32 u;
94 uint32 k;
95};
Russ Cox376898c2008-09-09 11:50:14 -070096union Note
Russ Coxd28acc42008-08-04 16:43:49 -070097{
Russ Cox376898c2008-09-09 11:50:14 -070098 struct { // Linux
99 Lock lock;
100 };
101 struct { // OS X
102 int32 wakeup;
Russ Cox5ff12f82008-09-24 10:25:28 -0700103 Usema sema;
Russ Cox376898c2008-09-09 11:50:14 -0700104 };
Russ Coxd28acc42008-08-04 16:43:49 -0700105};
Ken Thompson594175d2008-07-13 14:29:46 -0700106struct String
Ken Thompsonbbb20732008-06-05 19:38:39 -0700107{
108 int32 len;
109 byte str[1];
Ken Thompson594175d2008-07-13 14:29:46 -0700110};
Ken Thompson66a603c2008-08-27 17:28:30 -0700111
112struct Array
113{ // must not move anything
114 byte* array; // actual data
115 uint32 nel; // number of elements
Russ Cox75647d22008-11-17 12:32:35 -0800116 uint32 cap; // allocated number of elements
Ken Thompson66a603c2008-08-27 17:28:30 -0700117 byte b[8]; // actual array - may not be contig
118};
Ken Thompson751ce3a2008-07-11 19:16:39 -0700119struct Gobuf
120{
121 byte* SP;
122 byte* PC;
123};
Ken Thompson7b454bb2008-07-09 11:35:26 -0700124struct G
Ken Thompson45288542008-07-08 17:19:17 -0700125{
126 byte* stackguard; // must not move
127 byte* stackbase; // must not move
Ken Thompsone7d549f2008-07-16 13:50:23 -0700128 byte* stack0; // first stack segment
Ken Thompson751ce3a2008-07-11 19:16:39 -0700129 Gobuf sched;
Rob Pike3835e012008-07-28 11:29:41 -0700130 G* alllink; // on allg
Ken Thompsone963cba2008-07-25 15:55:12 -0700131 void* param; // passed parameter on wakeup
132 int16 status;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700133 int32 goid;
Ken Thompsone963cba2008-07-25 15:55:12 -0700134 int32 selgen; // valid sudog pointer
Russ Cox96824002008-08-05 14:18:47 -0700135 G* schedlink;
Russ Coxa61bb952008-09-24 14:13:07 -0700136 bool readyonstop;
Russ Coxd28acc42008-08-04 16:43:49 -0700137 M* m; // for debuggers
138};
139struct Mem
140{
141 uint8* hunk;
142 uint32 nhunk;
143 uint64 nmmap;
144 uint64 nmal;
Ken Thompson45288542008-07-08 17:19:17 -0700145};
Ken Thompson45288542008-07-08 17:19:17 -0700146struct M
147{
Ken Thompson751ce3a2008-07-11 19:16:39 -0700148 G* g0; // g0 w interrupt stack - must not move
149 uint64 morearg; // arg to morestack - must not move
Ken Thompsone7d549f2008-07-16 13:50:23 -0700150 uint64 cret; // return value from C - must not move
Russ Cox376898c2008-09-09 11:50:14 -0700151 uint64 procid; // for debuggers - must not move
Russ Coxa67258f2008-09-18 15:56:46 -0700152 G* gsignal; // signal-handling G - must not move
Ken Thompson751ce3a2008-07-11 19:16:39 -0700153 G* curg; // current running goroutine
Ken Thompsone7d549f2008-07-16 13:50:23 -0700154 G* lastg; // last running goroutine - to emulate fifo
Ken Thompson751ce3a2008-07-11 19:16:39 -0700155 Gobuf sched;
156 Gobuf morestack;
157 byte* moresp;
158 int32 siz1;
159 int32 siz2;
Russ Coxefc86a72008-11-25 16:48:10 -0800160 int32 id;
Russ Cox96824002008-08-05 14:18:47 -0700161 Note havenextg;
162 G* nextg;
163 M* schedlink;
Russ Coxd28acc42008-08-04 16:43:49 -0700164 Mem mem;
Russ Cox376898c2008-09-09 11:50:14 -0700165 uint32 machport; // Return address for Mach IPC (OS X)
Russ Cox75647d22008-11-17 12:32:35 -0800166 void* freelist[SmallFreeClasses];
Ken Thompson751ce3a2008-07-11 19:16:39 -0700167};
Ken Thompson52620032008-07-14 14:33:39 -0700168struct Stktop
Ken Thompson594175d2008-07-13 14:29:46 -0700169{
Ken Thompson751ce3a2008-07-11 19:16:39 -0700170 uint8* oldbase;
171 uint8* oldsp;
172 uint64 magic;
173 uint8* oldguard;
Ken Thompson45288542008-07-08 17:19:17 -0700174};
Ken Thompson594175d2008-07-13 14:29:46 -0700175struct Alg
Ken Thompson751ce3a2008-07-11 19:16:39 -0700176{
Ken Thompson594175d2008-07-13 14:29:46 -0700177 uint64 (*hash)(uint32, void*);
178 uint32 (*equal)(uint32, void*, void*);
179 void (*print)(uint32, void*);
180 void (*copy)(uint32, void*, void*);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700181};
Ken Thompson594175d2008-07-13 14:29:46 -0700182struct SigTab
Ken Thompsonbbb20732008-06-05 19:38:39 -0700183{
Russ Coxdfa58932008-12-03 14:21:28 -0800184 int32 flags;
Ken Thompson594175d2008-07-13 14:29:46 -0700185 int8 *name;
Ken Thompsonbbb20732008-06-05 19:38:39 -0700186};
Russ Coxdfa58932008-12-03 14:21:28 -0800187enum
188{
189 SigCatch = 1<<0,
190 SigIgnore = 1<<1,
191 SigRestart = 1<<2,
192};
Ken Thompsonbbb20732008-06-05 19:38:39 -0700193
Russ Cox3aa063d2008-11-23 17:08:55 -0800194// (will be) shared with go; edit ../cmd/6g/sys.go too.
195// should move out of sys.go eventually.
196// also eventually, the loaded symbol table should
197// be closer to this form.
198struct Func
199{
200 string name;
Russ Coxa5433362008-11-25 09:23:36 -0800201 string type; // go type string
202 string src; // src file name
203 uint64 entry; // entry pc
204 int64 frame; // stack frame size
205 Array pcln; // pc/ln tab for this func
206 int64 pc0; // starting pc, ln for table
207 int32 ln0;
Russ Coxefc86a72008-11-25 16:48:10 -0800208 int32 args; // number of 32-bit in/out args
209 int32 locals; // number of 32-bit locals
Russ Cox3aa063d2008-11-23 17:08:55 -0800210};
211
Ken Thompsonbbb20732008-06-05 19:38:39 -0700212/*
213 * defined macros
214 * you need super-goru privilege
215 * to add this list.
216 */
217#define nelem(x) (sizeof(x)/sizeof((x)[0]))
218#define nil ((void*)0)
219
220/*
Ken Thompson594175d2008-07-13 14:29:46 -0700221 * external data
222 */
Russ Cox39356102008-12-09 16:16:07 -0800223extern Alg algarray[4];
Ken Thompson594175d2008-07-13 14:29:46 -0700224extern string emptystring;
Ken Thompson594175d2008-07-13 14:29:46 -0700225G* allg;
226int32 goidgen;
Russ Coxd28acc42008-08-04 16:43:49 -0700227extern int32 gomaxprocs;
Rob Pike6e8dbc22008-09-12 09:44:41 -0700228extern int32 panicking;
Ken Thompson79fbbe32008-11-05 21:50:28 -0800229extern int32 maxround;
Ken Thompson594175d2008-07-13 14:29:46 -0700230
231/*
Rob Pike8e82a672008-06-30 11:50:36 -0700232 * common functions and data
233 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700234int32 strcmp(byte*, byte*);
Russ Cox3aa063d2008-11-23 17:08:55 -0800235int32 findnull(byte*);
Rob Pike8e82a672008-06-30 11:50:36 -0700236void dump(byte*, int32);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700237int32 runetochar(byte*, int32);
238int32 chartorune(uint32*, byte*);
Rob Pike8e82a672008-06-30 11:50:36 -0700239
Rob Pike8e82a672008-06-30 11:50:36 -0700240/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700241 * very low level c-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700242 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700243int32 gogo(Gobuf*);
244int32 gosave(Gobuf*);
Rob Pike2da97832008-07-12 11:30:53 -0700245int32 gogoret(Gobuf*, uint64);
246void retfromnewstack(void);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700247void setspgoto(byte*, void(*)(void), void(*)(void));
Ken Thompsonbbb20732008-06-05 19:38:39 -0700248void FLUSH(void*);
Rob Piked3204ef2008-06-30 14:39:47 -0700249void* getu(void);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700250void throw(int8*);
Ken Thompson594175d2008-07-13 14:29:46 -0700251uint32 rnd(uint32, uint32);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700252void prints(int8*);
Russ Coxefc86a72008-11-25 16:48:10 -0800253void printf(int8*, ...);
Russ Cox3aa063d2008-11-23 17:08:55 -0800254byte* mchr(byte*, byte, byte*);
Ken Thompsone1a06cc2008-06-15 20:24:30 -0700255void mcpy(byte*, byte*, uint32);
Ken Thompsonbc0b4f02008-11-13 10:35:44 -0800256void mmov(byte*, byte*, uint32);
Ken Thompsone1a06cc2008-06-15 20:24:30 -0700257void* mal(uint32);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700258uint32 cmpstring(string, string);
Russ Cox3aa063d2008-11-23 17:08:55 -0800259string gostring(byte*);
Rob Pikeaeb43982008-06-21 15:36:23 -0700260void initsig(void);
Russ Coxfb40f882008-09-22 13:47:53 -0700261int32 gotraceback(void);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700262void traceback(uint8 *pc, uint8 *sp, G* gp);
Rob Pike3835e012008-07-28 11:29:41 -0700263void tracebackothers(G*);
Rob Pikec870ac22008-07-14 20:54:55 -0700264int32 open(byte*, int32, ...);
Rob Pike3e4e83a2008-06-26 14:09:26 -0700265int32 read(int32, void*, int32);
Rob Pikec870ac22008-07-14 20:54:55 -0700266int32 write(int32, void*, int32);
Rob Pike3e4e83a2008-06-26 14:09:26 -0700267void close(int32);
268int32 fstat(int32, void*);
Russ Coxd28acc42008-08-04 16:43:49 -0700269bool cas(uint32*, uint32, uint32);
Russ Coxd28acc42008-08-04 16:43:49 -0700270void exit1(int32);
271void ready(G*);
272byte* getenv(int8*);
273int32 atoi(byte*);
Russ Cox96824002008-08-05 14:18:47 -0700274void newosproc(M *m, G *g, void *stk, void (*fn)(void));
Russ Coxa67258f2008-09-18 15:56:46 -0700275void sigaltstack(void*, void*);
276void signalstack(byte*, int32);
277G* malg(int32);
278void minit(void);
Russ Cox3aa063d2008-11-23 17:08:55 -0800279Func* findfunc(uint64);
Russ Coxa5433362008-11-25 09:23:36 -0800280int32 funcline(Func*, uint64);
Russ Cox79e1db22008-12-04 08:30:54 -0800281void* stackalloc(uint32);
282void stackfree(void*);
Russ Coxd28acc42008-08-04 16:43:49 -0700283
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800284#pragma varargck argpos printf 1
285
286#pragma varargck type "d" int32
287#pragma varargck type "d" uint32
288#pragma varargck type "D" int64
289#pragma varargck type "D" uint64
290#pragma varargck type "x" int32
291#pragma varargck type "x" uint32
292#pragma varargck type "X" int64
293#pragma varargck type "X" uint64
294#pragma varargck type "p" void*
295#pragma varargck type "p" uint64
296#pragma varargck type "s" int8*
297#pragma varargck type "s" uint8*
298#pragma varargck type "S" string
299
Russ Cox3f8aa662008-12-05 15:24:18 -0800300// TODO(rsc): Remove. These are only temporary,
301// for the mark and sweep collector.
302void stoptheworld(void);
303void starttheworld(void);
304
Russ Coxd28acc42008-08-04 16:43:49 -0700305/*
306 * mutual exclusion locks. in the uncontended case,
307 * as fast as spin locks (just a few user-level instructions),
308 * but on the contention path they sleep in the kernel.
Russ Cox96824002008-08-05 14:18:47 -0700309 * a zeroed Lock is unlocked (no need to initialize each lock).
Russ Coxd28acc42008-08-04 16:43:49 -0700310 */
311void lock(Lock*);
312void unlock(Lock*);
Russ Coxd28acc42008-08-04 16:43:49 -0700313
314/*
Russ Cox96824002008-08-05 14:18:47 -0700315 * sleep and wakeup on one-time events.
Russ Coxf7f63292008-08-05 14:21:42 -0700316 * before any calls to notesleep or notewakeup,
Russ Cox96824002008-08-05 14:18:47 -0700317 * must call noteclear to initialize the Note.
318 * then, any number of threads can call notesleep
319 * and exactly one thread can call notewakeup (once).
320 * once notewakeup has been called, all the notesleeps
321 * will return. future notesleeps will return immediately.
Russ Coxd28acc42008-08-04 16:43:49 -0700322 */
Russ Cox96824002008-08-05 14:18:47 -0700323void noteclear(Note*);
324void notesleep(Note*);
325void notewakeup(Note*);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700326
327/*
328 * low level go -called
329 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700330void sys·goexit(void);
331void sys·gosched(void);
Ken Thompson7d119242008-06-24 17:16:06 -0700332void sys·exit(int32);
333void sys·write(int32, void*, int32);
334void sys·breakpoint(void);
335uint8* sys·mmap(byte*, uint32, int32, int32, int32, uint32);
336void sys·memclr(byte*, uint32);
Ken Thompsoncb9b1032008-07-24 15:57:30 -0700337void sys·setcallerpc(void*, void*);
Ken Thompson7d119242008-06-24 17:16:06 -0700338void* sys·getcallerpc(void*);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700339
340/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700341 * runtime go-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700342 */
Ken Thompson7d119242008-06-24 17:16:06 -0700343void sys·printbool(bool);
344void sys·printfloat(float64);
345void sys·printint(int64);
346void sys·printstring(string);
Rob Pike8e82a672008-06-30 11:50:36 -0700347void sys·printpc(void*);
Ken Thompson7d119242008-06-24 17:16:06 -0700348void sys·printpointer(void*);
Russ Coxefc86a72008-11-25 16:48:10 -0800349void sys·printuint(uint64);
350void sys·printhex(uint64);
Ken Thompson7d119242008-06-24 17:16:06 -0700351void sys·catstring(string, string, string);
352void sys·cmpstring(string, string, int32);
353void sys·slicestring(string, int32, int32, string);
354void sys·indexstring(string, int32, byte);
355void sys·intstring(int64, string);
Russ Coxe8766352008-11-10 14:54:10 -0800356bool isInf(float64, int32);
357bool isNaN(float64);
Rob Pike6db99de2008-07-08 10:36:43 -0700358
359/*
360 * User go-called
361 */
Rob Pike3e4e83a2008-06-26 14:09:26 -0700362void sys·readfile(string, string, bool);
Rob Pike6db99de2008-07-08 10:36:43 -0700363void sys·bytestorune(byte*, int32, int32, int32, int32);
Rob Pike7ee60b12008-10-07 10:03:34 -0700364void sys·stringtorune(string, int32, int32, int32);
Russ Cox3f8aa662008-12-05 15:24:18 -0800365void sys·semacquire(uint32*);
366void sys·semrelease(uint32*);