blob: b4011b7588adcaaefca29df8a07b5e8509ab6b4c [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 Cox0d3a0432009-03-30 00:01:07 -070018
19#ifdef _64BIT
Russ Cox75647d22008-11-17 12:32:35 -080020typedef uint64 uintptr;
Devon H. O'Dell659780b2009-11-18 09:11:39 -080021typedef int64 intptr;
Russ Cox0d3a0432009-03-30 00:01:07 -070022#else
23typedef uint32 uintptr;
Devon H. O'Dell659780b2009-11-18 09:11:39 -080024typedef int32 intptr;
Russ Cox0d3a0432009-03-30 00:01:07 -070025#endif
Ken Thompsonbbb20732008-06-05 19:38:39 -070026
27/*
28 * get rid of C types
Russ Cox3aa063d2008-11-23 17:08:55 -080029 * the / / / forces a syntax error immediately,
30 * which will show "last name: XXunsigned".
Ken Thompsonbbb20732008-06-05 19:38:39 -070031 */
Russ Cox3aa063d2008-11-23 17:08:55 -080032#define unsigned XXunsigned / / /
33#define signed XXsigned / / /
34#define char XXchar / / /
35#define short XXshort / / /
36#define int XXint / / /
37#define long XXlong / / /
38#define float XXfloat / / /
39#define double XXdouble / / /
Ken Thompsonbbb20732008-06-05 19:38:39 -070040
41/*
42 * defined types
43 */
44typedef uint8 bool;
45typedef uint8 byte;
Ken Thompson594175d2008-07-13 14:29:46 -070046typedef struct Alg Alg;
Russ Cox3aa063d2008-11-23 17:08:55 -080047typedef struct Func Func;
48typedef struct G G;
49typedef struct Gobuf Gobuf;
Russ Coxd28acc42008-08-04 16:43:49 -070050typedef struct Lock Lock;
Russ Cox3aa063d2008-11-23 17:08:55 -080051typedef struct M M;
Russ Coxd28acc42008-08-04 16:43:49 -070052typedef struct Mem Mem;
Russ Cox3aa063d2008-11-23 17:08:55 -080053typedef union Note Note;
Rob Pike87f22082009-08-25 15:54:25 -070054typedef struct Slice Slice;
Russ Cox3aa063d2008-11-23 17:08:55 -080055typedef struct Stktop Stktop;
Ken Thompson36570612009-04-09 18:16:21 -070056typedef struct String String;
Russ Cox3aa063d2008-11-23 17:08:55 -080057typedef struct Usema Usema;
Russ Coxc3077f72008-12-19 17:11:54 -080058typedef struct SigTab SigTab;
59typedef struct MCache MCache;
60typedef struct Iface Iface;
Russ Coxc7513ea2009-07-07 11:02:54 -070061typedef struct Itab Itab;
Ken Thompsonf59cb492010-02-19 20:42:50 -080062typedef struct Eface Eface;
Russ Coxc7513ea2009-07-07 11:02:54 -070063typedef struct Type Type;
Ken Thompson1e1cc4e2009-01-27 12:03:53 -080064typedef struct Defer Defer;
Russ Cox9b1507b2010-03-31 11:46:01 -070065typedef struct Panic Panic;
Russ Cox764b6ec2009-07-08 13:55:57 -070066typedef struct hash Hmap;
Russ Cox5ddaf9a2009-07-08 15:00:54 -070067typedef struct Hchan Hchan;
Ken Thompsonf59cb492010-02-19 20:42:50 -080068typedef struct Complex64 Complex64;
69typedef struct Complex128 Complex128;
Ken Thompson594175d2008-07-13 14:29:46 -070070
71/*
Rob Piked08f0062009-08-11 13:30:35 -070072 * per-cpu declaration.
73 * "extern register" is a special storage class implemented by 6c, 8c, etc.
74 * on machines with lots of registers, it allocates a register that will not be
75 * used in generated code. on the x86, it allocates a slot indexed by a
76 * segment register.
77 *
78 * amd64: allocated downwards from R15
79 * x86: allocated upwards from 0(FS)
Kai Backmanebf32c52010-02-06 21:59:46 -080080 * arm: allocated downwards from R10
Russ Cox653cef12009-08-26 10:47:18 -070081 *
Rob Piked08f0062009-08-11 13:30:35 -070082 * every C file linked into a Go program must include runtime.h
83 * so that the C compiler knows to avoid other uses of these registers.
84 * the Go compilers know to avoid them.
Ken Thompson594175d2008-07-13 14:29:46 -070085 */
Rob Piked08f0062009-08-11 13:30:35 -070086extern register G* g;
87extern register M* m;
Ken Thompson594175d2008-07-13 14:29:46 -070088
89/*
90 * defined constants
91 */
92enum
93{
94 // G status
95 Gidle,
96 Grunnable,
Russ Coxd28acc42008-08-04 16:43:49 -070097 Grunning,
Russ Cox3f8aa662008-12-05 15:24:18 -080098 Gsyscall,
Ken Thompson52620032008-07-14 14:33:39 -070099 Gwaiting,
Russ Cox96824002008-08-05 14:18:47 -0700100 Gmoribund,
Ken Thompson594175d2008-07-13 14:29:46 -0700101 Gdead,
Russ Cox9b1507b2010-03-31 11:46:01 -0700102 Grecovery,
Ken Thompson594175d2008-07-13 14:29:46 -0700103};
104enum
105{
106 true = 1,
107 false = 0,
108};
109
110/*
111 * structures
112 */
Russ Coxd28acc42008-08-04 16:43:49 -0700113struct Lock
114{
115 uint32 key;
Hector Chu6bfe5f52010-01-06 17:58:55 -0800116#ifdef __MINGW__
117 void* event;
118#else
Russ Cox376898c2008-09-09 11:50:14 -0700119 uint32 sema; // for OS X
Hector Chu6bfe5f52010-01-06 17:58:55 -0800120#endif
Russ Coxd28acc42008-08-04 16:43:49 -0700121};
Russ Cox5ff12f82008-09-24 10:25:28 -0700122struct Usema
123{
124 uint32 u;
125 uint32 k;
126};
Russ Cox376898c2008-09-09 11:50:14 -0700127union Note
Russ Coxd28acc42008-08-04 16:43:49 -0700128{
Russ Cox376898c2008-09-09 11:50:14 -0700129 struct { // Linux
130 Lock lock;
131 };
132 struct { // OS X
133 int32 wakeup;
Russ Cox5ff12f82008-09-24 10:25:28 -0700134 Usema sema;
Russ Cox376898c2008-09-09 11:50:14 -0700135 };
Russ Coxd28acc42008-08-04 16:43:49 -0700136};
Ken Thompson594175d2008-07-13 14:29:46 -0700137struct String
Ken Thompsonbbb20732008-06-05 19:38:39 -0700138{
Ken Thompson36570612009-04-09 18:16:21 -0700139 byte* str;
Ken Thompsonbbb20732008-06-05 19:38:39 -0700140 int32 len;
Ken Thompson594175d2008-07-13 14:29:46 -0700141};
Russ Coxc3077f72008-12-19 17:11:54 -0800142struct Iface
143{
Russ Coxc7513ea2009-07-07 11:02:54 -0700144 Itab* tab;
Ken Thompson36570612009-04-09 18:16:21 -0700145 void* data;
Russ Coxc3077f72008-12-19 17:11:54 -0800146};
Russ Cox2da50222009-05-20 14:57:55 -0700147struct Eface
148{
Russ Coxc7513ea2009-07-07 11:02:54 -0700149 Type* type;
Russ Cox2da50222009-05-20 14:57:55 -0700150 void* data;
151};
Ken Thompsonf59cb492010-02-19 20:42:50 -0800152struct Complex64
153{
154 float32 real;
155 float32 imag;
156};
157struct Complex128
158{
159 float64 real;
160 float64 imag;
161};
Ken Thompson66a603c2008-08-27 17:28:30 -0700162
Rob Pike87f22082009-08-25 15:54:25 -0700163struct Slice
Ken Thompson66a603c2008-08-27 17:28:30 -0700164{ // must not move anything
165 byte* array; // actual data
Rob Pike87f22082009-08-25 15:54:25 -0700166 uint32 len; // number of elements
Russ Cox75647d22008-11-17 12:32:35 -0800167 uint32 cap; // allocated number of elements
Ken Thompson66a603c2008-08-27 17:28:30 -0700168};
Ken Thompson751ce3a2008-07-11 19:16:39 -0700169struct Gobuf
170{
Russ Cox38020092009-06-17 16:31:02 -0700171 // The offsets of these fields are known to (hard-coded in) libmach.
Russ Cox7343e032009-06-17 15:12:16 -0700172 byte* sp;
173 byte* pc;
174 G* g;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700175};
Ken Thompson7b454bb2008-07-09 11:35:26 -0700176struct G
Ken Thompson45288542008-07-08 17:19:17 -0700177{
Russ Cox133a1582009-10-03 10:37:12 -0700178 byte* stackguard; // cannot move - also known to linker, libmach, libcgo
179 byte* stackbase; // cannot move - also known to libmach, libcgo
Russ Cox7343e032009-06-17 15:12:16 -0700180 Defer* defer;
Russ Cox9b1507b2010-03-31 11:46:01 -0700181 Panic* panic;
182 Gobuf sched;
Russ Cox133a1582009-10-03 10:37:12 -0700183 byte* stack0;
Russ Cox7343e032009-06-17 15:12:16 -0700184 byte* entry; // initial function
Rob Pike3835e012008-07-28 11:29:41 -0700185 G* alllink; // on allg
Ken Thompsone963cba2008-07-25 15:55:12 -0700186 void* param; // passed parameter on wakeup
187 int16 status;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700188 int32 goid;
Adam Langley50d6c812009-12-18 12:25:53 -0800189 uint32 selgen; // valid sudog pointer
Russ Cox96824002008-08-05 14:18:47 -0700190 G* schedlink;
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800191 bool readyonstop;
Russ Cox9b1507b2010-03-31 11:46:01 -0700192 bool ispanic;
Russ Cox7343e032009-06-17 15:12:16 -0700193 M* m; // for debuggers, but offset not hard-coded
Russ Cox218c3932009-07-13 17:28:39 -0700194 M* lockedm;
Russ Cox133a1582009-10-03 10:37:12 -0700195 void (*cgofn)(void*); // for cgo/ffi
196 void *cgoarg;
Russ Coxd28acc42008-08-04 16:43:49 -0700197};
Ken Thompson45288542008-07-08 17:19:17 -0700198struct M
199{
Russ Cox38020092009-06-17 16:31:02 -0700200 // The offsets of these fields are known to (hard-coded in) libmach.
Russ Cox7343e032009-06-17 15:12:16 -0700201 G* g0; // goroutine with scheduling stack
202 void (*morepc)(void);
Russ Coxbba278a2009-07-08 18:16:09 -0700203 void* morefp; // frame pointer for more stack
Russ Cox7343e032009-06-17 15:12:16 -0700204 Gobuf morebuf; // gobuf arg to morestack
205
Russ Cox38020092009-06-17 16:31:02 -0700206 // Fields not known to debuggers.
Russ Cox7343e032009-06-17 15:12:16 -0700207 uint32 moreframe; // size arguments to morestack
208 uint32 moreargs;
209 uintptr cret; // return value from C
210 uint64 procid; // for debuggers, but offset not hard-coded
211 G* gsignal; // signal-handling G
212 uint32 tls[8]; // thread-local storage (for 386 extern register)
213 Gobuf sched; // scheduling stack
214 G* curg; // current running goroutine
Russ Coxefc86a72008-11-25 16:48:10 -0800215 int32 id;
Russ Coxda0a7d72008-12-19 03:13:39 -0800216 int32 mallocing;
Russ Cox8c357ce2009-06-15 21:31:56 -0700217 int32 gcing;
Russ Cox1ce17912009-01-26 17:37:05 -0800218 int32 locks;
Russ Cox6eb251f2010-03-24 09:40:09 -0700219 int32 nomemprof;
Russ Cox218c3932009-07-13 17:28:39 -0700220 int32 waitnextg;
Russ Cox96824002008-08-05 14:18:47 -0700221 Note havenextg;
222 G* nextg;
Russ Cox93689d82009-10-09 15:35:33 -0700223 M* alllink; // on allm
Russ Cox96824002008-08-05 14:18:47 -0700224 M* schedlink;
Russ Cox376898c2008-09-09 11:50:14 -0700225 uint32 machport; // Return address for Mach IPC (OS X)
Russ Coxe29ce172008-12-18 15:42:28 -0800226 MCache *mcache;
Russ Cox218c3932009-07-13 17:28:39 -0700227 G* lockedg;
Hector Chu6bfe5f52010-01-06 17:58:55 -0800228#ifdef __MINGW__
229 void* return_address; // saved return address and stack
230 void* stack_pointer; // pointer for Windows stdcall
231 void* os_stack_pointer;
232#endif
Ken Thompson751ce3a2008-07-11 19:16:39 -0700233};
Ken Thompson52620032008-07-14 14:33:39 -0700234struct Stktop
Ken Thompson594175d2008-07-13 14:29:46 -0700235{
Russ Cox38020092009-06-17 16:31:02 -0700236 // The offsets of these fields are known to (hard-coded in) libmach.
Russ Cox7343e032009-06-17 15:12:16 -0700237 uint8* stackguard;
238 uint8* stackbase;
239 Gobuf gobuf;
240 uint32 args;
Russ Coxbba278a2009-07-08 18:16:09 -0700241
242 // Frame pointer: where args start in old frame.
243 // fp == gobuf.sp except in the case of a reflected
244 // function call, which uses an off-stack argument frame.
245 uint8* fp;
Russ Cox83727cc2010-03-29 21:48:22 -0700246 bool free; // call stackfree for this frame?
Russ Cox9b1507b2010-03-31 11:46:01 -0700247 bool panic; // is this frame the top of a panic?
Ken Thompson45288542008-07-08 17:19:17 -0700248};
Ken Thompson594175d2008-07-13 14:29:46 -0700249struct Alg
Ken Thompson751ce3a2008-07-11 19:16:39 -0700250{
Russ Coxa52fb812009-06-04 21:09:06 -0700251 uintptr (*hash)(uint32, void*);
Ken Thompson594175d2008-07-13 14:29:46 -0700252 uint32 (*equal)(uint32, void*, void*);
253 void (*print)(uint32, void*);
254 void (*copy)(uint32, void*, void*);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700255};
Ken Thompson594175d2008-07-13 14:29:46 -0700256struct SigTab
Ken Thompsonbbb20732008-06-05 19:38:39 -0700257{
Russ Coxdfa58932008-12-03 14:21:28 -0800258 int32 flags;
Ken Thompson594175d2008-07-13 14:29:46 -0700259 int8 *name;
Ken Thompsonbbb20732008-06-05 19:38:39 -0700260};
Russ Coxdfa58932008-12-03 14:21:28 -0800261enum
262{
263 SigCatch = 1<<0,
264 SigIgnore = 1<<1,
265 SigRestart = 1<<2,
David Symondsb5866492009-12-15 18:21:29 -0800266 SigQueue = 1<<3,
Russ Coxdfa58932008-12-03 14:21:28 -0800267};
Ken Thompsonbbb20732008-06-05 19:38:39 -0700268
Russ Cox6eb251f2010-03-24 09:40:09 -0700269// NOTE(rsc): keep in sync with extern.go:/type.Func.
270// Eventually, the loaded symbol table should be closer to this form.
Russ Cox3aa063d2008-11-23 17:08:55 -0800271struct Func
272{
Ken Thompson36570612009-04-09 18:16:21 -0700273 String name;
274 String type; // go type string
275 String src; // src file name
Rob Pike87f22082009-08-25 15:54:25 -0700276 Slice pcln; // pc/ln tab for this func
Russ Cox6eb251f2010-03-24 09:40:09 -0700277 uintptr entry; // entry pc
278 uintptr pc0; // starting pc, ln for table
Russ Coxa5433362008-11-25 09:23:36 -0800279 int32 ln0;
Russ Cox6eb251f2010-03-24 09:40:09 -0700280 int32 frame; // stack frame size
Russ Coxefc86a72008-11-25 16:48:10 -0800281 int32 args; // number of 32-bit in/out args
282 int32 locals; // number of 32-bit locals
Russ Cox3aa063d2008-11-23 17:08:55 -0800283};
284
Ken Thompsonbbb20732008-06-05 19:38:39 -0700285/*
286 * defined macros
287 * you need super-goru privilege
288 * to add this list.
289 */
290#define nelem(x) (sizeof(x)/sizeof((x)[0]))
291#define nil ((void*)0)
292
293/*
Russ Coxeee50ae2008-12-19 12:05:22 -0800294 * known to compiler
295 */
296enum
297{
Russ Coxa7f6d402009-01-26 09:56:42 -0800298 AMEM,
299 ANOEQ,
Russ Coxeee50ae2008-12-19 12:05:22 -0800300 ASTRING,
Russ Coxeee50ae2008-12-19 12:05:22 -0800301 AINTER,
Russ Cox2da50222009-05-20 14:57:55 -0700302 ANILINTER,
Russ Cox9b6d3852009-01-26 12:36:21 -0800303 AFAKE,
Russ Coxa7f6d402009-01-26 09:56:42 -0800304 Amax
Russ Coxeee50ae2008-12-19 12:05:22 -0800305};
306
Russ Cox29aa3ff2009-07-02 21:25:46 -0700307
308enum {
309 Structrnd = sizeof(uintptr)
310};
311
Russ Coxeee50ae2008-12-19 12:05:22 -0800312/*
Ken Thompson47ab1c12009-01-27 13:23:28 -0800313 * deferred subroutine calls
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800314 */
315struct Defer
316{
317 int32 siz;
318 byte* sp;
Russ Cox9b1507b2010-03-31 11:46:01 -0700319 byte* pc;
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800320 byte* fn;
321 Defer* link;
322 byte args[8]; // padded to actual size
323};
324
325/*
Russ Cox9b1507b2010-03-31 11:46:01 -0700326 * panics
327 */
328struct Panic
329{
330 Eface arg; // argument to panic
331 byte* stackbase; // g->stackbase in panic
332 Panic* link; // link to earlier panic
333 bool recovered; // whether this panic is over
334};
335
336/*
Ken Thompson594175d2008-07-13 14:29:46 -0700337 * external data
338 */
Russ Coxa7f6d402009-01-26 09:56:42 -0800339extern Alg algarray[Amax];
Ken Thompson36570612009-04-09 18:16:21 -0700340extern String emptystring;
Ken Thompson594175d2008-07-13 14:29:46 -0700341G* allg;
Russ Cox93689d82009-10-09 15:35:33 -0700342M* allm;
Ken Thompson594175d2008-07-13 14:29:46 -0700343int32 goidgen;
Russ Coxd28acc42008-08-04 16:43:49 -0700344extern int32 gomaxprocs;
Rob Pike6e8dbc22008-09-12 09:44:41 -0700345extern int32 panicking;
Ken Thompson79fbbe32008-11-05 21:50:28 -0800346extern int32 maxround;
Rob Pikef6d67c92009-10-13 22:48:03 -0700347extern int32 fd; // usually 1; set to 2 when panicking
Russ Cox5328df62010-01-09 09:47:45 -0800348extern int32 gcwaiting; // gc is waiting to run
Russ Cox1b14bdb2009-09-22 16:28:32 -0700349int8* goos;
Ken Thompson594175d2008-07-13 14:29:46 -0700350
351/*
Rob Pike8e82a672008-06-30 11:50:36 -0700352 * common functions and data
353 */
Ken Thompson751ce3a2008-07-11 19:16:39 -0700354int32 strcmp(byte*, byte*);
Russ Cox3aa063d2008-11-23 17:08:55 -0800355int32 findnull(byte*);
Hector Chu6bfe5f52010-01-06 17:58:55 -0800356int32 findnullw(uint16*);
Rob Pike8e82a672008-06-30 11:50:36 -0700357void dump(byte*, int32);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700358int32 runetochar(byte*, int32);
Rob Pike54ec7192009-04-12 17:01:17 -0700359int32 charntorune(int32*, uint8*, int32);
Rob Pike8e82a672008-06-30 11:50:36 -0700360
Rob Pike8e82a672008-06-30 11:50:36 -0700361/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700362 * very low level c-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700363 */
Russ Cox7343e032009-06-17 15:12:16 -0700364void gogo(Gobuf*, uintptr);
365void gogocall(Gobuf*, void(*)(void));
366uintptr gosave(Gobuf*);
Russ Cox718be322010-01-25 18:52:55 -0800367void ·lessstack(void);
Russ Cox36096242009-01-16 14:58:14 -0800368void goargs(void);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700369void FLUSH(void*);
Rob Piked3204ef2008-06-30 14:39:47 -0700370void* getu(void);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700371void throw(int8*);
Ken Thompson594175d2008-07-13 14:29:46 -0700372uint32 rnd(uint32, uint32);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700373void prints(int8*);
Russ Coxefc86a72008-11-25 16:48:10 -0800374void printf(int8*, ...);
Russ Cox3aa063d2008-11-23 17:08:55 -0800375byte* mchr(byte*, byte, byte*);
Ken Thompsone1a06cc2008-06-15 20:24:30 -0700376void mcpy(byte*, byte*, uint32);
Russ Cox9b6d3852009-01-26 12:36:21 -0800377int32 mcmp(byte*, byte*, uint32);
Russ Cox652f5562009-11-20 09:11:46 -0800378void memmove(void*, void*, uint32);
Russ Cox596c16e2010-03-23 20:48:23 -0700379void* mal(uintptr);
380void* malx(uintptr size, int32 skip_delta);
Ken Thompson36570612009-04-09 18:16:21 -0700381uint32 cmpstring(String, String);
382String gostring(byte*);
Hector Chu6bfe5f52010-01-06 17:58:55 -0800383String gostringw(uint16*);
Rob Pikeaeb43982008-06-21 15:36:23 -0700384void initsig(void);
Russ Coxfb40f882008-09-22 13:47:53 -0700385int32 gotraceback(void);
Ken Thompson751ce3a2008-07-11 19:16:39 -0700386void traceback(uint8 *pc, uint8 *sp, G* gp);
Rob Pike3835e012008-07-28 11:29:41 -0700387void tracebackothers(G*);
Rob Pikec870ac22008-07-14 20:54:55 -0700388int32 open(byte*, int32, ...);
Rob Pikec870ac22008-07-14 20:54:55 -0700389int32 write(int32, void*, int32);
Russ Coxd28acc42008-08-04 16:43:49 -0700390bool cas(uint32*, uint32, uint32);
Hector Chu6bfe5f52010-01-06 17:58:55 -0800391bool casp(void**, void*, void*);
392uint32 xadd(uint32 volatile*, int32);
Russ Coxaa3222d82009-06-02 23:02:12 -0700393void jmpdefer(byte*, void*);
Russ Coxd28acc42008-08-04 16:43:49 -0700394void exit1(int32);
395void ready(G*);
396byte* getenv(int8*);
397int32 atoi(byte*);
Russ Cox96824002008-08-05 14:18:47 -0700398void newosproc(M *m, G *g, void *stk, void (*fn)(void));
Russ Coxa67258f2008-09-18 15:56:46 -0700399void signalstack(byte*, int32);
400G* malg(int32);
401void minit(void);
Russ Cox0d3a0432009-03-30 00:01:07 -0700402Func* findfunc(uintptr);
Russ Coxa5433362008-11-25 09:23:36 -0800403int32 funcline(Func*, uint64);
Russ Cox79e1db22008-12-04 08:30:54 -0800404void* stackalloc(uint32);
405void stackfree(void*);
Russ Coxe29ce172008-12-18 15:42:28 -0800406MCache* allocmcache(void);
407void mallocinit(void);
Russ Coxa7f6d402009-01-26 09:56:42 -0800408bool ifaceeq(Iface, Iface);
Russ Cox2da50222009-05-20 14:57:55 -0700409bool efaceeq(Eface, Eface);
Russ Coxa52fb812009-06-04 21:09:06 -0700410uintptr ifacehash(Iface);
411uintptr efacehash(Eface);
412uintptr nohash(uint32, void*);
Russ Coxa7f6d402009-01-26 09:56:42 -0800413uint32 noequal(uint32, void*, void*);
Russ Cox1ce17912009-01-26 17:37:05 -0800414void* malloc(uintptr size);
Russ Cox1ce17912009-01-26 17:37:05 -0800415void free(void *v);
Russ Cox62d627f2010-02-08 21:41:54 -0800416void addfinalizer(void*, void(*fn)(void*), int32);
Russ Cox4e28cfe2010-03-26 14:15:30 -0700417void walkfintab(void (*fn)(void*));
Russ Cox9b1507b2010-03-31 11:46:01 -0700418void runpanic(Panic*);
Russ Cox4e28cfe2010-03-26 14:15:30 -0700419
Russ Cox918afd942009-05-08 15:21:41 -0700420void exit(int32);
421void breakpoint(void);
422void gosched(void);
423void goexit(void);
Russ Cox133a1582009-10-03 10:37:12 -0700424void runcgo(void (*fn)(void*), void*);
Russ Cox718be322010-01-25 18:52:55 -0800425void ·entersyscall(void);
426void ·exitsyscall(void);
Russ Cox4e28cfe2010-03-26 14:15:30 -0700427G* newproc1(byte*, byte*, int32, int32);
David Symondsb5866492009-12-15 18:21:29 -0800428void siginit(void);
Russ Cox08579c22009-12-16 20:20:50 -0800429bool sigsend(int32 sig);
Russ Coxe4f06812010-02-08 14:32:22 -0800430void gettime(int64*, int32*);
Russ Cox2b7d1472010-03-23 17:01:17 -0700431int32 callers(int32, uintptr*, int32);
Russ Coxe4f06812010-02-08 14:32:22 -0800432int64 nanotime(void);
Russ Cox63e878a2010-03-31 15:55:10 -0700433void panic(int32);
Russ Coxd28acc42008-08-04 16:43:49 -0700434
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800435#pragma varargck argpos printf 1
436
437#pragma varargck type "d" int32
438#pragma varargck type "d" uint32
439#pragma varargck type "D" int64
440#pragma varargck type "D" uint64
441#pragma varargck type "x" int32
442#pragma varargck type "x" uint32
443#pragma varargck type "X" int64
444#pragma varargck type "X" uint64
445#pragma varargck type "p" void*
Russ Cox0d3a0432009-03-30 00:01:07 -0700446#pragma varargck type "p" uintptr
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800447#pragma varargck type "s" int8*
448#pragma varargck type "s" uint8*
Ken Thompson36570612009-04-09 18:16:21 -0700449#pragma varargck type "S" String
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800450
Russ Cox3f8aa662008-12-05 15:24:18 -0800451// TODO(rsc): Remove. These are only temporary,
452// for the mark and sweep collector.
453void stoptheworld(void);
454void starttheworld(void);
455
Russ Coxd28acc42008-08-04 16:43:49 -0700456/*
457 * mutual exclusion locks. in the uncontended case,
458 * as fast as spin locks (just a few user-level instructions),
459 * but on the contention path they sleep in the kernel.
Russ Cox96824002008-08-05 14:18:47 -0700460 * a zeroed Lock is unlocked (no need to initialize each lock).
Russ Coxd28acc42008-08-04 16:43:49 -0700461 */
462void lock(Lock*);
463void unlock(Lock*);
Russ Cox62d627f2010-02-08 21:41:54 -0800464void destroylock(Lock*);
Russ Coxd28acc42008-08-04 16:43:49 -0700465
466/*
Russ Coxa68592a2009-10-14 13:02:05 -0700467 * sleep and wakeup on one-time events.
Russ Coxf7f63292008-08-05 14:21:42 -0700468 * before any calls to notesleep or notewakeup,
Russ Cox96824002008-08-05 14:18:47 -0700469 * must call noteclear to initialize the Note.
470 * then, any number of threads can call notesleep
471 * and exactly one thread can call notewakeup (once).
472 * once notewakeup has been called, all the notesleeps
473 * will return. future notesleeps will return immediately.
Russ Coxd28acc42008-08-04 16:43:49 -0700474 */
Russ Cox96824002008-08-05 14:18:47 -0700475void noteclear(Note*);
476void notesleep(Note*);
477void notewakeup(Note*);
Ken Thompson4e8142c2008-06-16 22:34:50 -0700478
479/*
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800480 * Redefine methods for the benefit of gcc, which does not support
481 * UTF-8 characters in identifiers.
482 */
483#ifndef __GNUC__
Russ Cox718be322010-01-25 18:52:55 -0800484#define runtime_memclr ·memclr
485#define runtime_getcallerpc ·getcallerpc
486#define runtime_mmap ·mmap
487#define runtime_printslice ·printslice
488#define runtime_printbool ·printbool
489#define runtime_printfloat ·printfloat
490#define runtime_printhex ·printhex
491#define runtime_printint ·printint
492#define runtime_printiface ·printiface
493#define runtime_printeface ·printeface
494#define runtime_printpc ·printpc
495#define runtime_printpointer ·printpointer
496#define runtime_printstring ·printstring
497#define runtime_printuint ·printuint
Ken Thompsonf59cb492010-02-19 20:42:50 -0800498#define runtime_printcomplex ·printcomplex
Russ Cox718be322010-01-25 18:52:55 -0800499#define runtime_setcallerpc ·setcallerpc
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800500#endif
501
502/*
Adam Langley3f7a3242009-11-13 10:08:51 -0800503 * This is consistent across Linux and BSD.
504 * If a new OS is added that is different, move this to
505 * $GOOS/$GOARCH/defs.h.
506 */
507#define EACCES 13
508
509/*
Russ Cox1f8a40d2009-01-22 16:23:44 -0800510 * low level go-called
Ken Thompson4e8142c2008-06-16 22:34:50 -0700511 */
Russ Cox22a5c782009-10-15 23:10:49 -0700512uint8* runtime_mmap(byte*, uint32, int32, int32, int32, uint32);
513void runtime_memclr(byte*, uint32);
514void runtime_setcallerpc(void*, void*);
515void* runtime_getcallerpc(void*);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700516
517/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700518 * runtime go-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700519 */
Russ Cox22a5c782009-10-15 23:10:49 -0700520void runtime_printbool(bool);
521void runtime_printfloat(float64);
522void runtime_printint(int64);
523void runtime_printiface(Iface);
524void runtime_printeface(Eface);
525void runtime_printstring(String);
526void runtime_printpc(void*);
527void runtime_printpointer(void*);
528void runtime_printuint(uint64);
529void runtime_printhex(uint64);
530void runtime_printslice(Slice);
Ken Thompsonf59cb492010-02-19 20:42:50 -0800531void runtime_printcomplex(Complex128);
Russ Cox4e28cfe2010-03-26 14:15:30 -0700532void reflect·call(byte*, byte*, uint32);
Russ Cox63e878a2010-03-31 15:55:10 -0700533void ·panic(Eface);
534
535/*
536 * runtime c-called (but written in Go)
537 */
538void ·newError(String, Eface*);
539void ·printany(Eface);
540void ·newTypeAssertionError(Type*, Type*, Type*, String*, String*, String*, String*, Eface*);
Rob Pike6db99de2008-07-08 10:36:43 -0700541
542/*
Russ Cox1f8a40d2009-01-22 16:23:44 -0800543 * wrapped for go users
Rob Pike6db99de2008-07-08 10:36:43 -0700544 */
Russ Cox1f8a40d2009-01-22 16:23:44 -0800545float64 Inf(int32 sign);
546float64 NaN(void);
547float32 float32frombits(uint32 i);
548uint32 float32tobits(float32 f);
549float64 float64frombits(uint64 i);
550uint64 float64tobits(float64 f);
551float64 frexp(float64 d, int32 *ep);
552bool isInf(float64 f, int32 sign);
553bool isNaN(float64 f);
554float64 ldexp(float64 d, int32 e);
555float64 modf(float64 d, float64 *ip);
556void semacquire(uint32*);
557void semrelease(uint32*);
David Symondsb5866492009-12-15 18:21:29 -0800558String signame(int32 sig);
559
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700560
Russ Cox764b6ec2009-07-08 13:55:57 -0700561void mapassign(Hmap*, byte*, byte*);
562void mapaccess(Hmap*, byte*, byte*, bool*);
563struct hash_iter* mapiterinit(Hmap*);
564void mapiternext(struct hash_iter*);
565bool mapiterkey(struct hash_iter*, void*);
566void mapiterkeyvalue(struct hash_iter*, void*, void*);
Russ Cox7a0f4ca2009-09-08 13:46:54 -0700567Hmap* makemap(Type*, Type*, uint32);
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700568
Russ Cox7a0f4ca2009-09-08 13:46:54 -0700569Hchan* makechan(Type*, uint32);
Russ Cox5ddaf9a2009-07-08 15:00:54 -0700570void chansend(Hchan*, void*, bool*);
571void chanrecv(Hchan*, void*, bool*);
Russ Cox653cef12009-08-26 10:47:18 -0700572void chanclose(Hchan*);
573bool chanclosed(Hchan*);
Russ Coxde7920e2009-08-26 12:42:22 -0700574int32 chanlen(Hchan*);
575int32 chancap(Hchan*);
Russ Cox92e92572009-07-10 16:32:26 -0700576
577void ifaceE2I(struct InterfaceType*, Eface, Iface*);