blob: 5552c9e94d29fb117931a00184e853b745a9bcda [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 Coxc3077f72008-12-19 17:11:54 -080051typedef struct SigTab SigTab;
52typedef struct MCache MCache;
53typedef struct Iface Iface;
54typedef struct Itype Itype;
Ken Thompson594175d2008-07-13 14:29:46 -070055
56/*
57 * per cpu declaration
58 */
59extern register G* g; // R15
60extern register M* m; // R14
61
62/*
63 * defined constants
64 */
65enum
66{
67 // G status
68 Gidle,
69 Grunnable,
Russ Coxd28acc42008-08-04 16:43:49 -070070 Grunning,
Russ Cox3f8aa662008-12-05 15:24:18 -080071 Gsyscall,
Ken Thompson52620032008-07-14 14:33:39 -070072 Gwaiting,
Russ Cox96824002008-08-05 14:18:47 -070073 Gmoribund,
Ken Thompson594175d2008-07-13 14:29:46 -070074 Gdead,
75};
76enum
77{
78 true = 1,
79 false = 0,
80};
81
82/*
83 * structures
84 */
Russ Coxd28acc42008-08-04 16:43:49 -070085struct Lock
86{
87 uint32 key;
Russ Cox376898c2008-09-09 11:50:14 -070088 uint32 sema; // for OS X
Russ Coxd28acc42008-08-04 16:43:49 -070089};
Russ Cox5ff12f82008-09-24 10:25:28 -070090struct Usema
91{
92 uint32 u;
93 uint32 k;
94};
Russ Cox376898c2008-09-09 11:50:14 -070095union Note
Russ Coxd28acc42008-08-04 16:43:49 -070096{
Russ Cox376898c2008-09-09 11:50:14 -070097 struct { // Linux
98 Lock lock;
99 };
100 struct { // OS X
101 int32 wakeup;
Russ Cox5ff12f82008-09-24 10:25:28 -0700102 Usema sema;
Russ Cox376898c2008-09-09 11:50:14 -0700103 };
Russ Coxd28acc42008-08-04 16:43:49 -0700104};
Ken Thompson594175d2008-07-13 14:29:46 -0700105struct String
Ken Thompsonbbb20732008-06-05 19:38:39 -0700106{
107 int32 len;
108 byte str[1];
Ken Thompson594175d2008-07-13 14:29:46 -0700109};
Russ Coxc3077f72008-12-19 17:11:54 -0800110struct Iface
111{
112 Itype *type;
Russ Cox484ba932009-01-09 00:17:46 -0800113 void *data;
Russ Coxc3077f72008-12-19 17:11:54 -0800114};
Ken Thompson66a603c2008-08-27 17:28:30 -0700115
116struct Array
117{ // must not move anything
118 byte* array; // actual data
119 uint32 nel; // number of elements
Russ Cox75647d22008-11-17 12:32:35 -0800120 uint32 cap; // allocated number of elements
Ken Thompson66a603c2008-08-27 17:28:30 -0700121};
Ken Thompson751ce3a2008-07-11 19:16:39 -0700122struct Gobuf
123{
124 byte* SP;
125 byte* PC;
126};
Ken Thompson7b454bb2008-07-09 11:35:26 -0700127struct G
Ken Thompson45288542008-07-08 17:19:17 -0700128{
129 byte* stackguard; // must not move
130 byte* stackbase; // must not move
Ken Thompsone7d549f2008-07-16 13:50:23 -0700131 byte* stack0; // first stack segment
Ken Thompson751ce3a2008-07-11 19:16:39 -0700132 Gobuf sched;
Rob Pike3835e012008-07-28 11:29:41 -0700133 G* alllink; // on allg
Ken Thompsone963cba2008-07-25 15:55:12 -0700134 void* param; // passed parameter on wakeup
135 int16 status;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700136 int32 goid;
Ken Thompsone963cba2008-07-25 15:55:12 -0700137 int32 selgen; // valid sudog pointer
Russ Cox96824002008-08-05 14:18:47 -0700138 G* schedlink;
Russ Coxa61bb952008-09-24 14:13:07 -0700139 bool readyonstop;
Russ Coxd28acc42008-08-04 16:43:49 -0700140 M* m; // for debuggers
141};
142struct Mem
143{
144 uint8* hunk;
145 uint32 nhunk;
146 uint64 nmmap;
147 uint64 nmal;
Ken Thompson45288542008-07-08 17:19:17 -0700148};
Ken Thompson45288542008-07-08 17:19:17 -0700149struct M
150{
Ken Thompson751ce3a2008-07-11 19:16:39 -0700151 G* g0; // g0 w interrupt stack - must not move
152 uint64 morearg; // arg to morestack - must not move
Ken Thompsone7d549f2008-07-16 13:50:23 -0700153 uint64 cret; // return value from C - must not move
Russ Cox376898c2008-09-09 11:50:14 -0700154 uint64 procid; // for debuggers - must not move
Russ Coxa67258f2008-09-18 15:56:46 -0700155 G* gsignal; // signal-handling G - must not move
Ken Thompson751ce3a2008-07-11 19:16:39 -0700156 G* curg; // current running goroutine
Ken Thompsone7d549f2008-07-16 13:50:23 -0700157 G* lastg; // last running goroutine - to emulate fifo
Ken Thompson751ce3a2008-07-11 19:16:39 -0700158 Gobuf sched;
159 Gobuf morestack;
160 byte* moresp;
161 int32 siz1;
162 int32 siz2;
Russ Coxefc86a72008-11-25 16:48:10 -0800163 int32 id;
Russ Coxda0a7d72008-12-19 03:13:39 -0800164 int32 mallocing;
Russ Cox1ce17912009-01-26 17:37:05 -0800165 int32 locks;
Russ Cox96824002008-08-05 14:18:47 -0700166 Note havenextg;
167 G* nextg;
168 M* schedlink;
Russ Coxd28acc42008-08-04 16:43:49 -0700169 Mem mem;
Russ Cox376898c2008-09-09 11:50:14 -0700170 uint32 machport; // Return address for Mach IPC (OS X)
Russ Coxe29ce172008-12-18 15:42:28 -0800171 MCache *mcache;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700172};
Ken Thompson52620032008-07-14 14:33:39 -0700173struct Stktop
Ken Thompson594175d2008-07-13 14:29:46 -0700174{
Ken Thompson751ce3a2008-07-11 19:16:39 -0700175 uint8* oldbase;
176 uint8* oldsp;
177 uint64 magic;
178 uint8* oldguard;
Ken Thompson45288542008-07-08 17:19:17 -0700179};
Ken Thompson594175d2008-07-13 14:29:46 -0700180struct Alg
Ken Thompson751ce3a2008-07-11 19:16:39 -0700181{
Ken Thompson594175d2008-07-13 14:29:46 -0700182 uint64 (*hash)(uint32, void*);
183 uint32 (*equal)(uint32, void*, void*);
184 void (*print)(uint32, void*);
185 void (*copy)(uint32, void*, void*);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700186};
Ken Thompson594175d2008-07-13 14:29:46 -0700187struct SigTab
Ken Thompsonbbb20732008-06-05 19:38:39 -0700188{
Russ Coxdfa58932008-12-03 14:21:28 -0800189 int32 flags;
Ken Thompson594175d2008-07-13 14:29:46 -0700190 int8 *name;
Ken Thompsonbbb20732008-06-05 19:38:39 -0700191};
Russ Coxdfa58932008-12-03 14:21:28 -0800192enum
193{
194 SigCatch = 1<<0,
195 SigIgnore = 1<<1,
196 SigRestart = 1<<2,
197};
Ken Thompsonbbb20732008-06-05 19:38:39 -0700198
Russ Cox3aa063d2008-11-23 17:08:55 -0800199// (will be) shared with go; edit ../cmd/6g/sys.go too.
200// should move out of sys.go eventually.
201// also eventually, the loaded symbol table should
202// be closer to this form.
203struct Func
204{
205 string name;
Russ Coxa5433362008-11-25 09:23:36 -0800206 string type; // go type string
207 string src; // src file name
208 uint64 entry; // entry pc
209 int64 frame; // stack frame size
210 Array pcln; // pc/ln tab for this func
211 int64 pc0; // starting pc, ln for table
212 int32 ln0;
Russ Coxefc86a72008-11-25 16:48:10 -0800213 int32 args; // number of 32-bit in/out args
214 int32 locals; // number of 32-bit locals
Russ Cox3aa063d2008-11-23 17:08:55 -0800215};
216
Ken Thompsonbbb20732008-06-05 19:38:39 -0700217/*
218 * defined macros
219 * you need super-goru privilege
220 * to add this list.
221 */
222#define nelem(x) (sizeof(x)/sizeof((x)[0]))
223#define nil ((void*)0)
224
225/*
Russ Coxeee50ae2008-12-19 12:05:22 -0800226 * known to compiler
227 */
228enum
229{
Russ Coxa7f6d402009-01-26 09:56:42 -0800230 AMEM,
231 ANOEQ,
Russ Coxeee50ae2008-12-19 12:05:22 -0800232 ASTRING,
Russ Coxeee50ae2008-12-19 12:05:22 -0800233 AINTER,
Russ Cox9b6d3852009-01-26 12:36:21 -0800234 AFAKE,
Russ Coxa7f6d402009-01-26 09:56:42 -0800235 Amax
Russ Coxeee50ae2008-12-19 12:05:22 -0800236};
237
238/*
Ken Thompson594175d2008-07-13 14:29:46 -0700239 * external data
240 */
Russ Coxa7f6d402009-01-26 09:56:42 -0800241extern Alg algarray[Amax];
Ken Thompson594175d2008-07-13 14:29:46 -0700242extern string emptystring;
Ken Thompson594175d2008-07-13 14:29:46 -0700243G* allg;
244int32 goidgen;
Russ Coxd28acc42008-08-04 16:43:49 -0700245extern int32 gomaxprocs;
Rob Pike6e8dbc22008-09-12 09:44:41 -0700246extern int32 panicking;
Ken Thompson79fbbe32008-11-05 21:50:28 -0800247extern int32 maxround;
Ken Thompson594175d2008-07-13 14:29:46 -0700248
249/*
Rob Pike8e82a672008-06-30 11:50:36 -0700250 * common functions and data
251 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700252int32 strcmp(byte*, byte*);
Russ Cox3aa063d2008-11-23 17:08:55 -0800253int32 findnull(byte*);
Rob Pike8e82a672008-06-30 11:50:36 -0700254void dump(byte*, int32);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700255int32 runetochar(byte*, int32);
Rob Pike8e82a672008-06-30 11:50:36 -0700256
Rob Pike8e82a672008-06-30 11:50:36 -0700257/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700258 * very low level c-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700259 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700260int32 gogo(Gobuf*);
261int32 gosave(Gobuf*);
Rob Pike2da97832008-07-12 11:30:53 -0700262int32 gogoret(Gobuf*, uint64);
263void retfromnewstack(void);
Russ Cox36096242009-01-16 14:58:14 -0800264void goargs(void);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700265void setspgoto(byte*, void(*)(void), void(*)(void));
Ken Thompsonbbb20732008-06-05 19:38:39 -0700266void FLUSH(void*);
Rob Piked3204ef2008-06-30 14:39:47 -0700267void* getu(void);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700268void throw(int8*);
Ken Thompson594175d2008-07-13 14:29:46 -0700269uint32 rnd(uint32, uint32);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700270void prints(int8*);
Russ Coxefc86a72008-11-25 16:48:10 -0800271void printf(int8*, ...);
Russ Cox3aa063d2008-11-23 17:08:55 -0800272byte* mchr(byte*, byte, byte*);
Ken Thompsone1a06cc2008-06-15 20:24:30 -0700273void mcpy(byte*, byte*, uint32);
Russ Cox9b6d3852009-01-26 12:36:21 -0800274int32 mcmp(byte*, byte*, uint32);
Ken Thompsonbc0b4f02008-11-13 10:35:44 -0800275void mmov(byte*, byte*, uint32);
Ken Thompsone1a06cc2008-06-15 20:24:30 -0700276void* mal(uint32);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700277uint32 cmpstring(string, string);
Russ Cox3aa063d2008-11-23 17:08:55 -0800278string gostring(byte*);
Rob Pikeaeb43982008-06-21 15:36:23 -0700279void initsig(void);
Russ Coxfb40f882008-09-22 13:47:53 -0700280int32 gotraceback(void);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700281void traceback(uint8 *pc, uint8 *sp, G* gp);
Rob Pike3835e012008-07-28 11:29:41 -0700282void tracebackothers(G*);
Rob Pikec870ac22008-07-14 20:54:55 -0700283int32 open(byte*, int32, ...);
Rob Pike3e4e83a2008-06-26 14:09:26 -0700284int32 read(int32, void*, int32);
Rob Pikec870ac22008-07-14 20:54:55 -0700285int32 write(int32, void*, int32);
Rob Pike3e4e83a2008-06-26 14:09:26 -0700286void close(int32);
287int32 fstat(int32, void*);
Russ Coxd28acc42008-08-04 16:43:49 -0700288bool cas(uint32*, uint32, uint32);
Russ Coxd28acc42008-08-04 16:43:49 -0700289void exit1(int32);
290void ready(G*);
291byte* getenv(int8*);
292int32 atoi(byte*);
Russ Cox96824002008-08-05 14:18:47 -0700293void newosproc(M *m, G *g, void *stk, void (*fn)(void));
Russ Coxa67258f2008-09-18 15:56:46 -0700294void sigaltstack(void*, void*);
295void signalstack(byte*, int32);
296G* malg(int32);
297void minit(void);
Russ Cox3aa063d2008-11-23 17:08:55 -0800298Func* findfunc(uint64);
Russ Coxa5433362008-11-25 09:23:36 -0800299int32 funcline(Func*, uint64);
Russ Cox79e1db22008-12-04 08:30:54 -0800300void* stackalloc(uint32);
301void stackfree(void*);
Russ Coxe29ce172008-12-18 15:42:28 -0800302MCache* allocmcache(void);
303void mallocinit(void);
Russ Coxa7f6d402009-01-26 09:56:42 -0800304bool ifaceeq(Iface, Iface);
305uint64 ifacehash(Iface);
306uint64 nohash(uint32, void*);
307uint32 noequal(uint32, void*, void*);
Russ Cox1ce17912009-01-26 17:37:05 -0800308void* malloc(uintptr size);
309void* mallocgc(uintptr size);
310void free(void *v);
Russ Coxd28acc42008-08-04 16:43:49 -0700311
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800312#pragma varargck argpos printf 1
313
314#pragma varargck type "d" int32
315#pragma varargck type "d" uint32
316#pragma varargck type "D" int64
317#pragma varargck type "D" uint64
318#pragma varargck type "x" int32
319#pragma varargck type "x" uint32
320#pragma varargck type "X" int64
321#pragma varargck type "X" uint64
322#pragma varargck type "p" void*
323#pragma varargck type "p" uint64
324#pragma varargck type "s" int8*
325#pragma varargck type "s" uint8*
326#pragma varargck type "S" string
327
Russ Cox3f8aa662008-12-05 15:24:18 -0800328// TODO(rsc): Remove. These are only temporary,
329// for the mark and sweep collector.
330void stoptheworld(void);
331void starttheworld(void);
332
Russ Coxd28acc42008-08-04 16:43:49 -0700333/*
334 * mutual exclusion locks. in the uncontended case,
335 * as fast as spin locks (just a few user-level instructions),
336 * but on the contention path they sleep in the kernel.
Russ Cox96824002008-08-05 14:18:47 -0700337 * a zeroed Lock is unlocked (no need to initialize each lock).
Russ Coxd28acc42008-08-04 16:43:49 -0700338 */
339void lock(Lock*);
340void unlock(Lock*);
Russ Coxd28acc42008-08-04 16:43:49 -0700341
342/*
Russ Cox96824002008-08-05 14:18:47 -0700343 * sleep and wakeup on one-time events.
Russ Coxf7f63292008-08-05 14:21:42 -0700344 * before any calls to notesleep or notewakeup,
Russ Cox96824002008-08-05 14:18:47 -0700345 * must call noteclear to initialize the Note.
346 * then, any number of threads can call notesleep
347 * and exactly one thread can call notewakeup (once).
348 * once notewakeup has been called, all the notesleeps
349 * will return. future notesleeps will return immediately.
Russ Coxd28acc42008-08-04 16:43:49 -0700350 */
Russ Cox96824002008-08-05 14:18:47 -0700351void noteclear(Note*);
352void notesleep(Note*);
353void notewakeup(Note*);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700354
355/*
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800356 * Redefine methods for the benefit of gcc, which does not support
357 * UTF-8 characters in identifiers.
358 */
359#ifndef __GNUC__
Russ Cox36096242009-01-16 14:58:14 -0800360#define sys_Exit sys·Exit
361#define sys_Gosched sys·Gosched
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800362#define sys_memclr sys·memclr
363#define sys_write sys·write
Russ Cox36096242009-01-16 14:58:14 -0800364#define sys_Breakpoint sys·Breakpoint
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800365#define sys_catstring sys·catstring
366#define sys_cmpstring sys·cmpstring
367#define sys_getcallerpc sys·getcallerpc
Russ Cox36096242009-01-16 14:58:14 -0800368#define sys_Goexit sys·Goexit
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800369#define sys_indexstring sys·indexstring
370#define sys_intstring sys·intstring
371#define sys_mal sys·mal
372#define sys_mmap sys·mmap
373#define sys_printarray sys·printarray
374#define sys_printbool sys·printbool
375#define sys_printfloat sys·printfloat
376#define sys_printhex sys·printhex
377#define sys_printint sys·printint
Russ Coxa7f6d402009-01-26 09:56:42 -0800378#define sys_printinter sys·printinter
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800379#define sys_printpc sys·printpc
380#define sys_printpointer sys·printpointer
381#define sys_printstring sys·printstring
382#define sys_printuint sys·printuint
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800383#define sys_setcallerpc sys·setcallerpc
384#define sys_slicestring sys·slicestring
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800385#endif
386
387/*
Russ Cox1f8a40d2009-01-22 16:23:44 -0800388 * low level go-called
Ken Thompson4e8142c2008-06-16 22:34:50 -0700389 */
Russ Cox36096242009-01-16 14:58:14 -0800390void sys_Goexit(void);
391void sys_Gosched(void);
392void sys_Exit(int32);
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800393void sys_write(int32, void*, int32);
Russ Cox36096242009-01-16 14:58:14 -0800394void sys_Breakpoint(void);
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800395uint8* sys_mmap(byte*, uint32, int32, int32, int32, uint32);
396void sys_memclr(byte*, uint32);
397void sys_setcallerpc(void*, void*);
398void* sys_getcallerpc(void*);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700399
400/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700401 * runtime go-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700402 */
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800403void sys_printbool(bool);
404void sys_printfloat(float64);
405void sys_printint(int64);
Russ Coxa7f6d402009-01-26 09:56:42 -0800406void sys_printinter(Iface);
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800407void sys_printstring(string);
408void sys_printpc(void*);
409void sys_printpointer(void*);
410void sys_printuint(uint64);
411void sys_printhex(uint64);
412void sys_printarray(Array);
413void sys_catstring(string, string, string);
414void sys_cmpstring(string, string, int32);
415void sys_slicestring(string, int32, int32, string);
416void sys_indexstring(string, int32, byte);
417void sys_intstring(int64, string);
Rob Pike6db99de2008-07-08 10:36:43 -0700418
419/*
Russ Cox1f8a40d2009-01-22 16:23:44 -0800420 * wrapped for go users
Rob Pike6db99de2008-07-08 10:36:43 -0700421 */
Russ Cox1f8a40d2009-01-22 16:23:44 -0800422float64 Inf(int32 sign);
423float64 NaN(void);
424float32 float32frombits(uint32 i);
425uint32 float32tobits(float32 f);
426float64 float64frombits(uint64 i);
427uint64 float64tobits(float64 f);
428float64 frexp(float64 d, int32 *ep);
429bool isInf(float64 f, int32 sign);
430bool isNaN(float64 f);
431float64 ldexp(float64 d, int32 e);
432float64 modf(float64 d, float64 *ip);
433void semacquire(uint32*);
434void semrelease(uint32*);