blob: 66e87d5c27c62caa6c230d95c6efd756c9dcdc32 [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 Cox10ea6512012-09-24 20:57:01 -040022typedef int64 intgo; // Go's int
23typedef uint64 uintgo; // Go's uint
Russ Cox0d3a0432009-03-30 00:01:07 -070024#else
25typedef uint32 uintptr;
Russ Cox0b08c942012-09-24 14:58:34 -040026typedef int32 intptr;
27typedef int32 intgo; // Go's int
28typedef uint32 uintgo; // Go's uint
Russ Cox0d3a0432009-03-30 00:01:07 -070029#endif
Ken Thompsonbbb20732008-06-05 19:38:39 -070030
31/*
32 * get rid of C types
Russ Cox3aa063d2008-11-23 17:08:55 -080033 * the / / / forces a syntax error immediately,
34 * which will show "last name: XXunsigned".
Ken Thompsonbbb20732008-06-05 19:38:39 -070035 */
Russ Cox3aa063d2008-11-23 17:08:55 -080036#define unsigned XXunsigned / / /
37#define signed XXsigned / / /
38#define char XXchar / / /
39#define short XXshort / / /
40#define int XXint / / /
41#define long XXlong / / /
42#define float XXfloat / / /
43#define double XXdouble / / /
Ken Thompsonbbb20732008-06-05 19:38:39 -070044
45/*
46 * defined types
47 */
48typedef uint8 bool;
49typedef uint8 byte;
Russ Cox3aa063d2008-11-23 17:08:55 -080050typedef struct Func Func;
51typedef struct G G;
52typedef struct Gobuf Gobuf;
Dmitriy Vyukovd6174542013-04-06 20:07:07 -070053typedef struct Lock Lock;
Russ Cox3aa063d2008-11-23 17:08:55 -080054typedef struct M M;
Russ Coxc5f694a2013-03-01 08:30:11 -050055typedef struct P P;
Dmitriy Vyukov58030c52013-04-06 20:09:02 -070056typedef struct Note Note;
Rob Pike87f22082009-08-25 15:54:25 -070057typedef struct Slice Slice;
Russ Cox3aa063d2008-11-23 17:08:55 -080058typedef struct Stktop Stktop;
Ken Thompson36570612009-04-09 18:16:21 -070059typedef struct String String;
Russ Cox1903ad72013-02-21 17:01:13 -050060typedef struct FuncVal FuncVal;
Russ Coxc3077f72008-12-19 17:11:54 -080061typedef struct SigTab SigTab;
62typedef struct MCache MCache;
Russ Coxd324f212011-09-30 09:40:01 -040063typedef struct FixAlloc FixAlloc;
Russ Coxc3077f72008-12-19 17:11:54 -080064typedef struct Iface Iface;
Russ Coxc7513ea2009-07-07 11:02:54 -070065typedef struct Itab Itab;
Jan Ziak5c1422a2012-11-01 13:13:20 -040066typedef struct InterfaceType InterfaceType;
Ken Thompsonf59cb492010-02-19 20:42:50 -080067typedef struct Eface Eface;
Russ Coxc7513ea2009-07-07 11:02:54 -070068typedef struct Type Type;
Russ Cox3770b0e2011-08-17 15:54:17 -040069typedef struct ChanType ChanType;
Russ Cox65bde082011-08-17 14:56:27 -040070typedef struct MapType MapType;
Ken Thompson1e1cc4e2009-01-27 12:03:53 -080071typedef struct Defer Defer;
Russ Cox0de71612012-12-22 14:54:39 -050072typedef struct DeferChunk DeferChunk;
Russ Cox9b1507b2010-03-31 11:46:01 -070073typedef struct Panic Panic;
Luuk van Dijk7400be82011-01-31 12:27:28 +010074typedef struct Hmap Hmap;
Russ Cox5ddaf9a2009-07-08 15:00:54 -070075typedef struct Hchan Hchan;
Ken Thompsonf59cb492010-02-19 20:42:50 -080076typedef struct Complex64 Complex64;
77typedef struct Complex128 Complex128;
Alex Brainman2a808822011-08-27 23:17:00 +100078typedef struct WinCall WinCall;
Alex Brainmanafe0e972012-05-30 15:10:54 +100079typedef struct SEH SEH;
Alex Brainman05a5de32013-06-24 17:17:45 +100080typedef struct WinCallbackContext WinCallbackContext;
Russ Cox3b860262011-11-09 15:17:05 -050081typedef struct Timers Timers;
82typedef struct Timer Timer;
Russ Cox1903ad72013-02-21 17:01:13 -050083typedef struct GCStats GCStats;
84typedef struct LFNode LFNode;
85typedef struct ParFor ParFor;
86typedef struct ParForThread ParForThread;
87typedef struct CgoMal CgoMal;
Dmitriy Vyukov0bee99a2013-03-14 10:38:37 +040088typedef struct PollDesc PollDesc;
Dmitriy Vyukov4b536a52013-06-28 18:37:06 +040089typedef struct DebugVars DebugVars;
Ken Thompson594175d2008-07-13 14:29:46 -070090
91/*
Nigel Tao90ad6a22012-10-19 11:02:32 +110092 * Per-CPU declaration.
93 *
Rob Piked08f0062009-08-11 13:30:35 -070094 * "extern register" is a special storage class implemented by 6c, 8c, etc.
Nigel Tao90ad6a22012-10-19 11:02:32 +110095 * On the ARM, it is an actual register; elsewhere it is a slot in thread-
96 * local storage indexed by a segment register. See zasmhdr in
97 * src/cmd/dist/buildruntime.c for details, and be aware that the linker may
98 * make further OS-specific changes to the compiler's output. For example,
99 * 6l/linux rewrites 0(GS) as -16(FS).
Rob Piked08f0062009-08-11 13:30:35 -0700100 *
Nigel Tao90ad6a22012-10-19 11:02:32 +1100101 * Every C file linked into a Go program must include runtime.h so that the
102 * C compiler (6c, 8c, etc.) knows to avoid other uses of these dedicated
103 * registers. The Go compiler (6g, 8g, etc.) knows to avoid them.
Ken Thompson594175d2008-07-13 14:29:46 -0700104 */
Rob Piked08f0062009-08-11 13:30:35 -0700105extern register G* g;
106extern register M* m;
Ken Thompson594175d2008-07-13 14:29:46 -0700107
108/*
109 * defined constants
110 */
111enum
112{
113 // G status
Russ Cox72157c32010-04-08 13:24:53 -0700114 //
115 // If you add to this list, add to the list
116 // of "okay during garbage collection" status
117 // in mgc0.c too.
Ken Thompson594175d2008-07-13 14:29:46 -0700118 Gidle,
119 Grunnable,
Russ Coxd28acc42008-08-04 16:43:49 -0700120 Grunning,
Russ Cox3f8aa662008-12-05 15:24:18 -0800121 Gsyscall,
Ken Thompson52620032008-07-14 14:33:39 -0700122 Gwaiting,
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200123 Gmoribund_unused, // currently unused, but hardcoded in gdb scripts
Ken Thompson594175d2008-07-13 14:29:46 -0700124 Gdead,
125};
126enum
127{
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200128 // P status
129 Pidle,
130 Prunning,
131 Psyscall,
132 Pgcstop,
133 Pdead,
134};
135enum
136{
Ken Thompson594175d2008-07-13 14:29:46 -0700137 true = 1,
138 false = 0,
139};
Jan Ziak54193682012-09-17 17:18:21 -0400140enum
141{
142 PtrSize = sizeof(void*),
143};
Dmitriy Vyukovf82db7d2013-01-10 09:57:06 +0400144enum
145{
146 // Per-M stack segment cache size.
147 StackCacheSize = 32,
148 // Global <-> per-M stack segment cache transfer batch size.
149 StackCacheBatch = 16,
150};
Ken Thompson594175d2008-07-13 14:29:46 -0700151/*
152 * structures
153 */
Dmitriy Vyukovd6174542013-04-06 20:07:07 -0700154struct Lock
Russ Coxd28acc42008-08-04 16:43:49 -0700155{
Dmitriy Vyukovd6174542013-04-06 20:07:07 -0700156 // Futex-based impl treats it as uint32 key,
157 // while sema-based impl as M* waitm.
158 // Used to be a union, but unions break precise GC.
159 uintptr key;
Russ Cox5ff12f82008-09-24 10:25:28 -0700160};
Dmitriy Vyukov58030c52013-04-06 20:09:02 -0700161struct Note
Russ Coxd28acc42008-08-04 16:43:49 -0700162{
Dmitriy Vyukov58030c52013-04-06 20:09:02 -0700163 // Futex-based impl treats it as uint32 key,
164 // while sema-based impl as M* waitm.
165 // Used to be a union, but unions break precise GC.
166 uintptr key;
Russ Coxd28acc42008-08-04 16:43:49 -0700167};
Ken Thompson594175d2008-07-13 14:29:46 -0700168struct String
Ken Thompsonbbb20732008-06-05 19:38:39 -0700169{
Ken Thompson36570612009-04-09 18:16:21 -0700170 byte* str;
Russ Cox0b08c942012-09-24 14:58:34 -0400171 intgo len;
Ken Thompson594175d2008-07-13 14:29:46 -0700172};
Russ Cox1903ad72013-02-21 17:01:13 -0500173struct FuncVal
174{
175 void (*fn)(void);
176 // variable-size, fn-specific data here
177};
Russ Coxc3077f72008-12-19 17:11:54 -0800178struct Iface
179{
Russ Coxc7513ea2009-07-07 11:02:54 -0700180 Itab* tab;
Ken Thompson36570612009-04-09 18:16:21 -0700181 void* data;
Russ Coxc3077f72008-12-19 17:11:54 -0800182};
Russ Cox2da50222009-05-20 14:57:55 -0700183struct Eface
184{
Russ Coxc7513ea2009-07-07 11:02:54 -0700185 Type* type;
Russ Cox2da50222009-05-20 14:57:55 -0700186 void* data;
187};
Ken Thompsonf59cb492010-02-19 20:42:50 -0800188struct Complex64
189{
190 float32 real;
191 float32 imag;
192};
193struct Complex128
194{
195 float64 real;
196 float64 imag;
197};
Ken Thompson66a603c2008-08-27 17:28:30 -0700198
Rob Pike87f22082009-08-25 15:54:25 -0700199struct Slice
Ken Thompson66a603c2008-08-27 17:28:30 -0700200{ // must not move anything
201 byte* array; // actual data
Russ Cox0b08c942012-09-24 14:58:34 -0400202 uintgo len; // number of elements
203 uintgo cap; // allocated number of elements
Ken Thompson66a603c2008-08-27 17:28:30 -0700204};
Ken Thompson751ce3a2008-07-11 19:16:39 -0700205struct Gobuf
206{
Russ Coxd67e7e32013-06-12 15:22:26 -0400207 // The offsets of sp, pc, and g are known to (hard-coded in) libmach.
Jan Ziak334bf952012-05-30 13:07:52 -0400208 uintptr sp;
Russ Coxe58f7982013-06-12 08:49:38 -0400209 uintptr pc;
Russ Cox7343e032009-06-17 15:12:16 -0700210 G* g;
Russ Coxd67e7e32013-06-12 15:22:26 -0400211 uintptr ret;
212 void* ctxt;
213 uintptr lr;
Ken Thompson751ce3a2008-07-11 19:16:39 -0700214};
Dmitriy Vyukovd839a802012-04-05 20:48:28 +0400215struct GCStats
216{
217 // the struct must consist of only uint64's,
218 // because it is casted to uint64[].
219 uint64 nhandoff;
220 uint64 nhandoffcnt;
221 uint64 nprocyield;
222 uint64 nosyield;
223 uint64 nsleep;
224};
Dmitriy Vyukove84d9e12013-07-29 22:22:34 +0400225
226struct WinCall
227{
228 void (*fn)(void*);
229 uintptr n; // number of parameters
230 void* args; // parameters
231 uintptr r1; // return values
232 uintptr r2;
233 uintptr err; // error number
234};
235struct SEH
236{
237 void* prev;
238 void* handler;
239};
240// describes how to handle callback
241struct WinCallbackContext
242{
243 void* gobody; // Go function to call
244 uintptr argsize; // callback arguments size (in bytes)
245 uintptr restorestack; // adjust stack on return by (in bytes) (386 only)
246};
247
Ken Thompson7b454bb2008-07-09 11:35:26 -0700248struct G
Ken Thompson45288542008-07-08 17:19:17 -0700249{
Dmitriy Vyukovf5becf42013-06-03 12:28:24 +0400250 // stackguard0 can be set to StackPreempt as opposed to stackguard
251 uintptr stackguard0; // cannot move - also known to linker, libmach, runtime/cgo
Jan Ziak334bf952012-05-30 13:07:52 -0400252 uintptr stackbase; // cannot move - also known to libmach, runtime/cgo
Russ Cox7276c022013-09-12 14:00:16 -0400253 uint32 panicwrap; // cannot move - also known to linker
254 uint32 selgen; // valid sudog pointer
Russ Cox7343e032009-06-17 15:12:16 -0700255 Defer* defer;
Russ Cox9b1507b2010-03-31 11:46:01 -0700256 Panic* panic;
257 Gobuf sched;
Keith Randall44870542013-09-10 09:02:22 -0700258 uintptr syscallstack; // if status==Gsyscall, syscallstack = stackbase to use during gc
259 uintptr syscallsp; // if status==Gsyscall, syscallsp = sched.sp to use during gc
260 uintptr syscallpc; // if status==Gsyscall, syscallpc = sched.pc to use during gc
261 uintptr syscallguard; // if status==Gsyscall, syscallguard = stackguard to use during gc
Dmitriy Vyukovf5becf42013-06-03 12:28:24 +0400262 uintptr stackguard; // same as stackguard0, but not set to StackPreempt
Jan Ziak334bf952012-05-30 13:07:52 -0400263 uintptr stack0;
Russ Cox757e0de2013-08-15 22:34:06 -0400264 uintptr stacksize;
Rob Pike3835e012008-07-28 11:29:41 -0700265 G* alllink; // on allg
Ken Thompsone963cba2008-07-25 15:55:12 -0700266 void* param; // passed parameter on wakeup
267 int16 status;
Dmitriy Vyukov320df442012-10-26 10:13:06 +0400268 int64 goid;
Russ Cox03e9ea52011-08-22 23:26:39 -0400269 int8* waitreason; // if status==Gwaiting
Russ Cox96824002008-08-05 14:18:47 -0700270 G* schedlink;
Russ Cox9b1507b2010-03-31 11:46:01 -0700271 bool ispanic;
Dmitriy Vyukov6ee739d2013-03-12 17:21:44 +0400272 bool issystem; // do not output in stack dump
273 bool isbackground; // ignore in deadlock detector
Dmitriy Vyukov5887f142013-07-17 12:52:37 -0400274 bool preempt; // preemption signal, duplicates stackguard0 = StackPreempt
Dmitriy Vyukov6ee739d2013-03-12 17:21:44 +0400275 int8 raceignore; // ignore race detection events
Russ Cox7343e032009-06-17 15:12:16 -0700276 M* m; // for debuggers, but offset not hard-coded
Russ Cox218c3932009-07-13 17:28:39 -0700277 M* lockedm;
Russ Cox5963dba2010-04-08 18:15:30 -0700278 int32 sig;
Russ Coxe4b02bf2012-02-22 21:45:01 -0500279 int32 writenbuf;
280 byte* writebuf;
Dmitriy Vyukov3d6bce42013-07-30 23:48:18 +0400281 DeferChunk* dchunk;
282 DeferChunk* dchunknext;
Russ Cox5963dba2010-04-08 18:15:30 -0700283 uintptr sigcode0;
284 uintptr sigcode1;
Russ Cox12307002011-01-18 14:15:11 -0500285 uintptr sigpc;
Keith Randall44870542013-09-10 09:02:22 -0700286 uintptr gopc; // pc of go statement that created this goroutine
Dmitriy Vyukov0a40cd22013-02-06 11:40:54 +0400287 uintptr racectx;
Hector Chu9fd26872011-09-17 17:57:59 +1000288 uintptr end[];
Russ Coxd28acc42008-08-04 16:43:49 -0700289};
Ken Thompson45288542008-07-08 17:19:17 -0700290struct M
291{
Russ Cox7343e032009-06-17 15:12:16 -0700292 G* g0; // goroutine with scheduling stack
Russ Cox141a4a12011-01-14 14:05:20 -0500293 void* moreargp; // argument pointer for more stack
Russ Cox7343e032009-06-17 15:12:16 -0700294 Gobuf morebuf; // gobuf arg to morestack
295
Russ Cox38020092009-06-17 16:31:02 -0700296 // Fields not known to debuggers.
Russ Cox141a4a12011-01-14 14:05:20 -0500297 uint32 moreframesize; // size arguments to morestack
298 uint32 moreargsize;
Russ Cox7343e032009-06-17 15:12:16 -0700299 uintptr cret; // return value from C
300 uint64 procid; // for debuggers, but offset not hard-coded
301 G* gsignal; // signal-handling G
Russ Coxd0d74162013-03-01 09:24:17 -0500302 uintptr tls[4]; // thread-local storage (for x86 extern register)
303 void (*mstartfn)(void);
Russ Cox7343e032009-06-17 15:12:16 -0700304 G* curg; // current running goroutine
Russ Cox6fa3c892013-06-27 11:32:01 -0400305 G* caughtsig; // goroutine running during fatal signal
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200306 P* p; // attached P for executing Go code (nil if not executing Go code)
307 P* nextp;
Russ Coxefc86a72008-11-25 16:48:10 -0800308 int32 id;
Russ Coxda0a7d72008-12-19 03:13:39 -0800309 int32 mallocing;
Dmitriy Vyukov81221f52013-01-29 14:57:11 +0400310 int32 throwing;
Russ Cox8c357ce2009-06-15 21:31:56 -0700311 int32 gcing;
Russ Cox1ce17912009-01-26 17:37:05 -0800312 int32 locks;
Russ Cox6eb251f2010-03-24 09:40:09 -0700313 int32 nomemprof;
Russ Cox67793502011-02-16 13:21:13 -0500314 int32 dying;
Russ Coxc19b3732011-03-23 11:43:37 -0400315 int32 profilehz;
Russ Coxd324f212011-09-30 09:40:01 -0400316 int32 helpgc;
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200317 bool spinning;
Dmitriy Vyukov909f3182011-07-12 01:23:58 -0400318 uint32 fastrand;
Ian Lance Taylore9a30872012-11-10 11:19:06 -0800319 uint64 ncgocall; // number of cgo calls in total
320 int32 ncgo; // number of cgo calls currently in progress
321 CgoMal* cgomal;
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200322 Note park;
Russ Cox93689d82009-10-09 15:35:33 -0700323 M* alllink; // on allm
Russ Cox96824002008-08-05 14:18:47 -0700324 M* schedlink;
Russ Cox376898c2008-09-09 11:50:14 -0700325 uint32 machport; // Return address for Mach IPC (OS X)
Dmitriy Vyukov3d6bce42013-07-30 23:48:18 +0400326 MCache* mcache;
Dmitriy Vyukovf82db7d2013-01-10 09:57:06 +0400327 int32 stackinuse;
328 uint32 stackcachepos;
329 uint32 stackcachecnt;
330 void* stackcache[StackCacheSize];
Russ Cox218c3932009-07-13 17:28:39 -0700331 G* lockedg;
Keith Randall44870542013-09-10 09:02:22 -0700332 uintptr createstack[32];// Stack that created this thread.
Ken Thompsonae605262010-12-09 14:45:27 -0800333 uint32 freglo[16]; // D[i] lsb and F[i]
334 uint32 freghi[16]; // D[i] msb and F[i+16]
335 uint32 fflag; // floating point compare flags
Keith Randall44870542013-09-10 09:02:22 -0700336 uint32 locked; // tracking for LockOSThread
Dmitriy Vyukovee24bfc2011-11-02 16:42:01 +0300337 M* nextwaitm; // next M waiting for lock
338 uintptr waitsema; // semaphore for parking on locks
339 uint32 waitsemacount;
340 uint32 waitsemalock;
Dmitriy Vyukovd839a802012-04-05 20:48:28 +0400341 GCStats gcstats;
Dmitriy Vyukov2f6cbc72012-10-07 22:05:32 +0400342 bool racecall;
Russ Cox6c976392013-02-20 17:48:23 -0500343 bool needextram;
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200344 void (*waitunlockf)(Lock*);
Dmitriy Vyukov54340bf2013-04-06 20:01:28 -0700345 void* waitlock;
Russ Cox3b860262011-11-09 15:17:05 -0500346
Jan Ziakf8c58372012-09-24 20:08:05 -0400347 uintptr settype_buf[1024];
348 uintptr settype_bufsize;
349
Russ Cox851f3012011-12-16 15:33:58 -0500350#ifdef GOOS_windows
Hector Chu9fd26872011-09-17 17:57:59 +1000351 void* thread; // thread handle
Dmitriy Vyukove84d9e12013-07-29 22:22:34 +0400352 WinCall wincall;
Hector Chu5c303252011-09-14 20:23:21 -0400353#endif
Akshat Kumarc74f3c42013-01-30 02:53:56 -0800354#ifdef GOOS_plan9
Dmitriy Vyukov3d6bce42013-07-30 23:48:18 +0400355 int8* notesig;
Akshat Kumara566dea2013-03-08 00:54:44 +0100356 byte* errstr;
Akshat Kumarc74f3c42013-01-30 02:53:56 -0800357#endif
Alex Brainmanafe0e972012-05-30 15:10:54 +1000358 SEH* seh;
Hector Chu9fd26872011-09-17 17:57:59 +1000359 uintptr end[];
Ken Thompson751ce3a2008-07-11 19:16:39 -0700360};
Russ Cox370276a2011-04-27 23:21:12 -0400361
Dmitriy Vyukov353ce602013-02-23 08:48:02 +0400362struct P
363{
364 Lock;
365
Dmitriy Vyukov4f2e3822013-08-14 00:30:55 +0400366 int32 id;
Keith Randall44870542013-09-10 09:02:22 -0700367 uint32 status; // one of Pidle/Prunning/...
Dmitriy Vyukov6cdfb002013-02-27 21:17:53 +0200368 P* link;
Dmitriy Vyukovf9066fe2013-08-13 22:14:04 +0400369 uint32 schedtick; // incremented on every scheduler call
370 uint32 syscalltick; // incremented on every system call
Keith Randall44870542013-09-10 09:02:22 -0700371 M* m; // back-link to associated M (nil if idle)
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200372 MCache* mcache;
Dmitriy Vyukov6cdfb002013-02-27 21:17:53 +0200373
Dmitriy Vyukov353ce602013-02-23 08:48:02 +0400374 // Queue of runnable goroutines.
375 G** runq;
376 int32 runqhead;
377 int32 runqtail;
378 int32 runqsize;
Dmitriy Vyukov6cdfb002013-02-27 21:17:53 +0200379
380 // Available G's (status == Gdead)
381 G* gfree;
382 int32 gfreecnt;
383
384 byte pad[64];
Dmitriy Vyukov353ce602013-02-23 08:48:02 +0400385};
386
Dmitriy Vyukov27134562013-07-22 16:37:31 +0400387// The m->locked word holds two pieces of state counting active calls to LockOSThread/lockOSThread.
388// The low bit (LockExternal) is a boolean reporting whether any LockOSThread call is active.
389// External locks are not recursive; a second lock is silently ignored.
390// The upper bits of m->lockedcount record the nesting depth of calls to lockOSThread
391// (counting up by LockInternal), popped by unlockOSThread (counting down by LockInternal).
392// Internal locks can be recursive. For instance, a lock for cgo can occur while the main
393// goroutine is holding the lock during the initialization phase.
Russ Coxb0a29f32013-02-01 08:34:41 -0800394enum
395{
396 LockExternal = 1,
397 LockInternal = 2,
398};
399
Ken Thompson52620032008-07-14 14:33:39 -0700400struct Stktop
Ken Thompson594175d2008-07-13 14:29:46 -0700401{
Russ Cox38020092009-06-17 16:31:02 -0700402 // The offsets of these fields are known to (hard-coded in) libmach.
Russ Coxe58f7982013-06-12 08:49:38 -0400403 uintptr stackguard;
404 uintptr stackbase;
Russ Cox7343e032009-06-17 15:12:16 -0700405 Gobuf gobuf;
Russ Cox141a4a12011-01-14 14:05:20 -0500406 uint32 argsize;
Russ Cox7276c022013-09-12 14:00:16 -0400407 uint32 panicwrap;
Russ Coxbba278a2009-07-08 18:16:09 -0700408
Russ Coxafc69282011-01-25 16:35:36 -0500409 uint8* argp; // pointer to arguments in old frame
410 uintptr free; // if free>0, call stackfree using free as size
Russ Cox9b1507b2010-03-31 11:46:01 -0700411 bool panic; // is this frame the top of a panic?
Ken Thompson45288542008-07-08 17:19:17 -0700412};
Ken Thompson594175d2008-07-13 14:29:46 -0700413struct SigTab
Ken Thompsonbbb20732008-06-05 19:38:39 -0700414{
Russ Coxdfa58932008-12-03 14:21:28 -0800415 int32 flags;
Ken Thompson594175d2008-07-13 14:29:46 -0700416 int8 *name;
Ken Thompsonbbb20732008-06-05 19:38:39 -0700417};
Russ Coxdfa58932008-12-03 14:21:28 -0800418enum
419{
Russ Cox35586f72012-02-13 13:52:37 -0500420 SigNotify = 1<<0, // let signal.Notify have signal, even if from kernel
David Symonds3d8ebef2012-02-17 14:36:40 +1100421 SigKill = 1<<1, // if signal.Notify doesn't take it, exit quietly
422 SigThrow = 1<<2, // if signal.Notify doesn't take it, exit loudly
423 SigPanic = 1<<3, // if the signal is from the kernel, panic
424 SigDefault = 1<<4, // if the signal isn't explicitly requested, don't monitor it
Russ Coxcb4428e2013-03-15 00:00:02 -0400425 SigHandling = 1<<5, // our signal handler is registered
426 SigIgnored = 1<<6, // the signal was ignored before we registered for it
Russ Coxdfa58932008-12-03 14:21:28 -0800427};
Ken Thompsonbbb20732008-06-05 19:38:39 -0700428
Russ Coxc3de91b2013-07-18 10:43:22 -0400429// Layout of in-memory per-function information prepared by linker
Russ Cox5d363c62013-07-16 09:41:38 -0400430// See http://golang.org/s/go12symtab.
Russ Coxc3de91b2013-07-18 10:43:22 -0400431// Keep in sync with linker and with ../../libmach/sym.c
432// and with package debug/gosym.
Russ Cox3aa063d2008-11-23 17:08:55 -0800433struct Func
434{
Russ Cox5d363c62013-07-16 09:41:38 -0400435 uintptr entry; // start pc
Keith Randall44870542013-09-10 09:02:22 -0700436 int32 nameoff;// function name
Russ Cox5d363c62013-07-16 09:41:38 -0400437
Carl Shapirof4666172013-02-21 12:52:26 -0800438 int32 args; // in/out args size
Russ Cox5d363c62013-07-16 09:41:38 -0400439 int32 frame; // legacy frame size; use pcsp if possible
Russ Cox5d363c62013-07-16 09:41:38 -0400440
441 int32 pcsp;
442 int32 pcfile;
443 int32 pcln;
444 int32 npcdata;
445 int32 nfuncdata;
Russ Cox3aa063d2008-11-23 17:08:55 -0800446};
447
Jan Ziak5c1422a2012-11-01 13:13:20 -0400448// layout of Itab known to compilers
Dmitriy Vyukovb36f2db2013-06-09 21:58:35 +0400449// allocated in non-garbage-collected memory
Jan Ziak5c1422a2012-11-01 13:13:20 -0400450struct Itab
451{
452 InterfaceType* inter;
453 Type* type;
454 Itab* link;
455 int32 bad;
456 int32 unused;
457 void (*fun[])(void);
458};
459
Russ Cox851f3012011-12-16 15:33:58 -0500460#ifdef GOOS_windows
Alex Brainmanf95a2f22010-09-12 11:45:16 +1000461enum {
462 Windows = 1
463};
464#else
465enum {
466 Windows = 0
467};
468#endif
469
Russ Cox3b860262011-11-09 15:17:05 -0500470struct Timers
471{
472 Lock;
473 G *timerproc;
474 bool sleeping;
475 bool rescheduling;
476 Note waitnote;
477 Timer **t;
478 int32 len;
479 int32 cap;
480};
481
482// Package time knows the layout of this structure.
483// If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
484struct Timer
485{
Keith Randall44870542013-09-10 09:02:22 -0700486 int32 i; // heap index
Russ Cox3b860262011-11-09 15:17:05 -0500487
488 // Timer wakes up at when, and then at when+period, ... (period > 0 only)
489 // each time calling f(now, arg) in the timer goroutine, so f must be
490 // a well-behaved function and not block.
491 int64 when;
492 int64 period;
Russ Cox1903ad72013-02-21 17:01:13 -0500493 FuncVal *fv;
Russ Cox3b860262011-11-09 15:17:05 -0500494 Eface arg;
495};
496
Dmitriy Vyukova5dc7792012-04-12 11:49:25 +0400497// Lock-free stack node.
498struct LFNode
499{
500 LFNode *next;
501 uintptr pushcnt;
502};
503
Dmitriy Vyukov95643642012-05-11 10:50:03 +0400504// Parallel for descriptor.
505struct ParFor
506{
507 void (*body)(ParFor*, uint32); // executed for each element
508 uint32 done; // number of idle threads
509 uint32 nthr; // total number of threads
510 uint32 nthrmax; // maximum number of threads
511 uint32 thrseq; // thread id sequencer
512 uint32 cnt; // iteration space [0, cnt)
513 void *ctx; // arbitrary user context
514 bool wait; // if true, wait while all threads finish processing,
515 // otherwise parfor may return while other threads are still working
516 ParForThread *thr; // array of thread descriptors
Dmitriy Vyukov433824d2013-03-10 20:46:11 +0400517 uint32 pad; // to align ParForThread.pos for 64-bit atomic operations
Dmitriy Vyukov95643642012-05-11 10:50:03 +0400518 // stats
519 uint64 nsteal;
520 uint64 nstealcnt;
521 uint64 nprocyield;
522 uint64 nosyield;
523 uint64 nsleep;
524};
525
Ian Lance Taylore9a30872012-11-10 11:19:06 -0800526// Track memory allocated by code not written in Go during a cgo call,
527// so that the garbage collector can see them.
528struct CgoMal
529{
530 CgoMal *next;
Ian Lance Taylor6732ad92013-04-06 20:18:15 -0700531 void *alloc;
Ian Lance Taylore9a30872012-11-10 11:19:06 -0800532};
533
Dmitriy Vyukov4b536a52013-06-28 18:37:06 +0400534// Holds variables parsed from GODEBUG env var.
535struct DebugVars
536{
537 int32 gctrace;
Dmitriy Vyukov4f2e3822013-08-14 00:30:55 +0400538 int32 schedtrace;
539 int32 scheddetail;
Dmitriy Vyukov4b536a52013-06-28 18:37:06 +0400540};
541
Russ Cox30ecb4c2013-09-16 20:26:10 -0400542extern bool runtime·precisestack;
543
Ken Thompsonbbb20732008-06-05 19:38:39 -0700544/*
545 * defined macros
Robert Hencke3fbd4782011-05-30 18:02:59 +1000546 * you need super-gopher-guru privilege
Ken Thompsonbbb20732008-06-05 19:38:39 -0700547 * to add this list.
548 */
549#define nelem(x) (sizeof(x)/sizeof((x)[0]))
550#define nil ((void*)0)
Russ Coxdc9a3b22010-12-13 16:22:19 -0500551#define offsetof(s,m) (uint32)(&(((s*)0)->m))
Russ Cox6dbaa202012-05-29 14:02:29 -0400552#define ROUND(x, n) (((x)+(n)-1)&~((n)-1)) /* all-caps to mark as macro: it evaluates n twice */
Ken Thompsonbbb20732008-06-05 19:38:39 -0700553
554/*
Russ Coxeee50ae2008-12-19 12:05:22 -0800555 * known to compiler
556 */
Russ Coxb9ccd072011-12-05 09:40:22 -0500557enum {
558 Structrnd = sizeof(uintptr)
559};
560
561/*
562 * type algorithms - known to compiler
563 */
Russ Coxeee50ae2008-12-19 12:05:22 -0800564enum
565{
Russ Coxa7f6d402009-01-26 09:56:42 -0800566 AMEM,
Dmitriy Vyukov1ff14052012-01-20 10:32:55 +0400567 AMEM0,
Dmitriy Vyukov54e94062011-08-08 09:35:32 -0400568 AMEM8,
569 AMEM16,
570 AMEM32,
571 AMEM64,
572 AMEM128,
Russ Cox196b6632011-12-12 22:22:09 -0500573 ANOEQ,
Dmitriy Vyukov1ff14052012-01-20 10:32:55 +0400574 ANOEQ0,
Dmitriy Vyukov54e94062011-08-08 09:35:32 -0400575 ANOEQ8,
576 ANOEQ16,
577 ANOEQ32,
578 ANOEQ64,
579 ANOEQ128,
Russ Cox196b6632011-12-12 22:22:09 -0500580 ASTRING,
581 AINTER,
582 ANILINTER,
583 ASLICE,
Russ Cox408f0b12012-01-26 16:25:07 -0500584 AFLOAT32,
585 AFLOAT64,
586 ACPLX64,
587 ACPLX128,
Russ Coxa7f6d402009-01-26 09:56:42 -0800588 Amax
Russ Coxeee50ae2008-12-19 12:05:22 -0800589};
Russ Coxb9ccd072011-12-05 09:40:22 -0500590typedef struct Alg Alg;
591struct Alg
592{
593 void (*hash)(uintptr*, uintptr, void*);
594 void (*equal)(bool*, uintptr, void*, void*);
595 void (*print)(uintptr, void*);
596 void (*copy)(uintptr, void*, void*);
Russ Cox29aa3ff2009-07-02 21:25:46 -0700597};
598
Russ Coxb9ccd072011-12-05 09:40:22 -0500599extern Alg runtime·algarray[Amax];
600
Keith Randalla5d40242013-03-12 10:47:44 -0700601byte* runtime·startup_random_data;
602uint32 runtime·startup_random_data_len;
603void runtime·get_random_data(byte**, int32*);
604
605enum {
606 // hashinit wants this many random bytes
607 HashRandomBytes = 32
608};
609void runtime·hashinit(void);
610
Russ Coxb9ccd072011-12-05 09:40:22 -0500611void runtime·memhash(uintptr*, uintptr, void*);
612void runtime·nohash(uintptr*, uintptr, void*);
613void runtime·strhash(uintptr*, uintptr, void*);
614void runtime·interhash(uintptr*, uintptr, void*);
615void runtime·nilinterhash(uintptr*, uintptr, void*);
Keith Randalla5d40242013-03-12 10:47:44 -0700616void runtime·aeshash(uintptr*, uintptr, void*);
617void runtime·aeshash32(uintptr*, uintptr, void*);
618void runtime·aeshash64(uintptr*, uintptr, void*);
619void runtime·aeshashstr(uintptr*, uintptr, void*);
Russ Coxb9ccd072011-12-05 09:40:22 -0500620
621void runtime·memequal(bool*, uintptr, void*, void*);
622void runtime·noequal(bool*, uintptr, void*, void*);
623void runtime·strequal(bool*, uintptr, void*, void*);
624void runtime·interequal(bool*, uintptr, void*, void*);
625void runtime·nilinterequal(bool*, uintptr, void*, void*);
626
Keith Randall3d5daa22013-04-02 16:26:15 -0700627bool runtime·memeq(void*, void*, uintptr);
628
Russ Coxb9ccd072011-12-05 09:40:22 -0500629void runtime·memprint(uintptr, void*);
630void runtime·strprint(uintptr, void*);
631void runtime·interprint(uintptr, void*);
632void runtime·nilinterprint(uintptr, void*);
633
634void runtime·memcopy(uintptr, void*, void*);
635void runtime·memcopy8(uintptr, void*, void*);
636void runtime·memcopy16(uintptr, void*, void*);
637void runtime·memcopy32(uintptr, void*, void*);
638void runtime·memcopy64(uintptr, void*, void*);
639void runtime·memcopy128(uintptr, void*, void*);
Russ Coxb9ccd072011-12-05 09:40:22 -0500640void runtime·strcopy(uintptr, void*, void*);
641void runtime·algslicecopy(uintptr, void*, void*);
642void runtime·intercopy(uintptr, void*, void*);
643void runtime·nilintercopy(uintptr, void*, void*);
644
Russ Coxeee50ae2008-12-19 12:05:22 -0800645/*
Ken Thompson47ab1c12009-01-27 13:23:28 -0800646 * deferred subroutine calls
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800647 */
648struct Defer
649{
650 int32 siz;
Keith Randall44870542013-09-10 09:02:22 -0700651 bool special; // not part of defer frame
652 bool free; // if special, free when done
653 byte* argp; // where args were copied from
Russ Cox9b1507b2010-03-31 11:46:01 -0700654 byte* pc;
Russ Cox1903ad72013-02-21 17:01:13 -0500655 FuncVal* fn;
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800656 Defer* link;
Jan Ziak334bf952012-05-30 13:07:52 -0400657 void* args[1]; // padded to actual size
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800658};
659
Russ Cox0de71612012-12-22 14:54:39 -0500660struct DeferChunk
661{
662 DeferChunk *prev;
663 uintptr off;
664};
665
Ken Thompson1e1cc4e2009-01-27 12:03:53 -0800666/*
Russ Cox9b1507b2010-03-31 11:46:01 -0700667 * panics
668 */
669struct Panic
670{
671 Eface arg; // argument to panic
Russ Coxe58f7982013-06-12 08:49:38 -0400672 uintptr stackbase; // g->stackbase in panic
Russ Cox9b1507b2010-03-31 11:46:01 -0700673 Panic* link; // link to earlier panic
674 bool recovered; // whether this panic is over
675};
676
677/*
Russ Coxe58f7982013-06-12 08:49:38 -0400678 * stack traces
679 */
680typedef struct Stkframe Stkframe;
681struct Stkframe
682{
683 Func* fn; // function being run
684 uintptr pc; // program counter within fn
685 uintptr lr; // program counter at caller aka link register
686 uintptr sp; // stack pointer at pc
687 uintptr fp; // stack pointer at caller aka frame pointer
Keith Randall44870542013-09-10 09:02:22 -0700688 byte* varp; // top of local variables
Russ Coxe58f7982013-06-12 08:49:38 -0400689 byte* argp; // pointer to function arguments
690 uintptr arglen; // number of bytes at argp
Russ Coxe58f7982013-06-12 08:49:38 -0400691};
692
Russ Cox6fa3c892013-06-27 11:32:01 -0400693int32 runtime·gentraceback(uintptr, uintptr, uintptr, G*, int32, uintptr*, int32, void(*)(Stkframe*, void*), void*, bool);
Russ Coxe58f7982013-06-12 08:49:38 -0400694void runtime·traceback(uintptr pc, uintptr sp, uintptr lr, G* gp);
695void runtime·tracebackothers(G*);
Russ Coxd67e7e32013-06-12 15:22:26 -0400696bool runtime·haszeroargs(uintptr pc);
Russ Coxa8374852013-07-17 12:47:18 -0400697bool runtime·topofstack(Func*);
Russ Coxe58f7982013-06-12 08:49:38 -0400698
699/*
Ken Thompson594175d2008-07-13 14:29:46 -0700700 * external data
701 */
Russ Cox68b42552010-11-04 14:00:19 -0400702extern String runtime·emptystring;
Jan Ziak4a191c22012-10-21 17:41:32 -0400703extern uintptr runtime·zerobase;
Shenghou Ma4019d0e2013-01-26 09:57:06 +0800704extern G* runtime·allg;
705extern G* runtime·lastg;
706extern M* runtime·allm;
Dmitriy Vyukov779c45a2013-03-01 13:49:16 +0200707extern P** runtime·allp;
Russ Cox68b42552010-11-04 14:00:19 -0400708extern int32 runtime·gomaxprocs;
Dmitriy Vyukov34c67eb2013-05-22 22:57:47 +0400709extern uint32 runtime·needextram;
Russ Cox67793502011-02-16 13:21:13 -0500710extern uint32 runtime·panicking;
Shenghou Ma4019d0e2013-01-26 09:57:06 +0800711extern int8* runtime·goos;
712extern int32 runtime·ncpu;
Russ Cox9042c2c2010-12-08 13:53:30 -0500713extern bool runtime·iscgo;
Russ Cox0b08c942012-09-24 14:58:34 -0400714extern void (*runtime·sysargs)(int32, uint8**);
Rob Pike98a80b92013-08-07 06:49:11 +1000715extern uintptr runtime·maxstring;
Jan Ziaka656f822013-02-25 15:58:23 -0500716extern uint32 runtime·Hchansize;
Keith Randalla5d40242013-03-12 10:47:44 -0700717extern uint32 runtime·cpuid_ecx;
718extern uint32 runtime·cpuid_edx;
Dmitriy Vyukov4b536a52013-06-28 18:37:06 +0400719extern DebugVars runtime·debug;
Russ Cox757e0de2013-08-15 22:34:06 -0400720extern uintptr runtime·maxstacksize;
Ken Thompson594175d2008-07-13 14:29:46 -0700721
722/*
Rob Pike8e82a672008-06-30 11:50:36 -0700723 * common functions and data
724 */
Russ Cox68b42552010-11-04 14:00:19 -0400725int32 runtime·strcmp(byte*, byte*);
Dmitriy Vyukov4e5086b2011-07-29 12:44:06 -0400726byte* runtime·strstr(byte*, byte*);
Rob Pike82f5ca12013-08-06 21:49:03 +1000727intgo runtime·findnull(byte*);
728intgo runtime·findnullw(uint16*);
Russ Cox68b42552010-11-04 14:00:19 -0400729void runtime·dump(byte*, int32);
730int32 runtime·runetochar(byte*, int32);
731int32 runtime·charntorune(int32*, uint8*, int32);
Rob Pike8e82a672008-06-30 11:50:36 -0700732
Rob Pike8e82a672008-06-30 11:50:36 -0700733/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700734 * very low level c-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700735 */
Russ Cox68b42552010-11-04 14:00:19 -0400736#define FLUSH(x) USED(x)
Russ Cox4e28cfe2010-03-26 14:15:30 -0700737
Russ Coxd67e7e32013-06-12 15:22:26 -0400738void runtime·gogo(Gobuf*);
739void runtime·gostartcall(Gobuf*, void(*)(void), void*);
740void runtime·gostartcallfn(Gobuf*, FuncVal*);
Russ Coxf9ca3b52011-03-07 10:37:42 -0500741void runtime·gosave(Gobuf*);
Russ Cox68b42552010-11-04 14:00:19 -0400742void runtime·lessstack(void);
743void runtime·goargs(void);
Alex Brainmana41d8542011-01-12 11:48:15 +1100744void runtime·goenvs(void);
745void runtime·goenvs_unix(void);
Russ Cox68b42552010-11-04 14:00:19 -0400746void* runtime·getu(void);
747void runtime·throw(int8*);
748void runtime·panicstring(int8*);
Russ Cox68b42552010-11-04 14:00:19 -0400749void runtime·prints(int8*);
750void runtime·printf(int8*, ...);
751byte* runtime·mchr(byte*, byte, byte*);
Keith Randall00224a32013-03-20 13:51:29 -0700752int32 runtime·mcmp(byte*, byte*, uintptr);
Rémy Oudompheng1b8f51c2013-03-09 00:41:03 +0100753void runtime·memmove(void*, void*, uintptr);
Russ Cox68b42552010-11-04 14:00:19 -0400754void* runtime·mal(uintptr);
755String runtime·catstring(String, String);
756String runtime·gostring(byte*);
Russ Cox0b08c942012-09-24 14:58:34 -0400757String runtime·gostringn(byte*, intgo);
758Slice runtime·gobytes(byte*, intgo);
Russ Cox68b42552010-11-04 14:00:19 -0400759String runtime·gostringnocopy(byte*);
Alex Brainmana41d8542011-01-12 11:48:15 +1100760String runtime·gostringw(uint16*);
Russ Cox35586f72012-02-13 13:52:37 -0500761void runtime·initsig(void);
David Symonds3d8ebef2012-02-17 14:36:40 +1100762void runtime·sigenable(uint32 sig);
Russ Coxcb4428e2013-03-15 00:00:02 -0400763void runtime·sigdisable(uint32 sig);
Russ Cox5146a932013-03-15 01:11:03 -0400764int32 runtime·gotraceback(bool *crash);
Russ Cox03e9ea52011-08-22 23:26:39 -0400765void runtime·goroutineheader(G*);
Keith Randalla5d40242013-03-12 10:47:44 -0700766int32 runtime·open(int8*, int32, int32);
767int32 runtime·read(int32, void*, int32);
Russ Cox68b42552010-11-04 14:00:19 -0400768int32 runtime·write(int32, void*, int32);
Keith Randalla5d40242013-03-12 10:47:44 -0700769int32 runtime·close(int32);
Jonathan Markddde52a2011-06-07 21:50:10 -0700770int32 runtime·mincore(void*, uintptr, byte*);
Russ Cox68b42552010-11-04 14:00:19 -0400771bool runtime·cas(uint32*, uint32, uint32);
Russ Coxfb63e4f2013-07-12 00:03:32 -0400772bool runtime·cas64(uint64*, uint64, uint64);
Russ Cox68b42552010-11-04 14:00:19 -0400773bool runtime·casp(void**, void*, void*);
Dmitriy Vyukov997c00f2011-06-28 15:09:53 -0400774// Don't confuse with XADD x86 instruction,
775// this one is actually 'addx', that is, add-and-fetch.
Russ Cox68b42552010-11-04 14:00:19 -0400776uint32 runtime·xadd(uint32 volatile*, int32);
Dmitriy Vyukov46675712012-04-05 18:47:43 +0400777uint64 runtime·xadd64(uint64 volatile*, int64);
Dmitriy Vyukov4e5086b2011-07-29 12:44:06 -0400778uint32 runtime·xchg(uint32 volatile*, uint32);
Dmitriy Vyukovadd33492013-03-05 09:46:52 +0200779uint64 runtime·xchg64(uint64 volatile*, uint64);
Dmitriy Vyukov86a659c2011-07-13 11:22:41 -0700780uint32 runtime·atomicload(uint32 volatile*);
Dmitriy Vyukov91f0f182011-07-29 13:47:24 -0400781void runtime·atomicstore(uint32 volatile*, uint32);
Dmitriy Vyukov46675712012-04-05 18:47:43 +0400782void runtime·atomicstore64(uint64 volatile*, uint64);
783uint64 runtime·atomicload64(uint64 volatile*);
Dmitriy Vyukov86a659c2011-07-13 11:22:41 -0700784void* runtime·atomicloadp(void* volatile*);
785void runtime·atomicstorep(void* volatile*, void*);
Russ Cox1903ad72013-02-21 17:01:13 -0500786void runtime·jmpdefer(FuncVal*, void*);
Russ Cox68b42552010-11-04 14:00:19 -0400787void runtime·exit1(int32);
788void runtime·ready(G*);
789byte* runtime·getenv(int8*);
790int32 runtime·atoi(byte*);
Russ Coxe6a3e222013-03-01 11:44:43 -0500791void runtime·newosproc(M *mp, void *stk);
792void runtime·mstart(void);
Russ Cox68b42552010-11-04 14:00:19 -0400793G* runtime·malg(int32);
Russ Cox1707a992012-02-14 01:23:15 -0500794void runtime·asminit(void);
Dmitriy Vyukova0955a22013-02-21 16:24:38 +0400795void runtime·mpreinit(M*);
Russ Cox68b42552010-11-04 14:00:19 -0400796void runtime·minit(void);
Russ Cox6c976392013-02-20 17:48:23 -0500797void runtime·unminit(void);
798void runtime·signalstack(byte*, int32);
Dmitriy Vyukov081129e2013-05-28 21:10:10 +0400799void runtime·symtabinit(void);
Russ Cox68b42552010-11-04 14:00:19 -0400800Func* runtime·findfunc(uintptr);
Russ Cox5d363c62013-07-16 09:41:38 -0400801int32 runtime·funcline(Func*, uintptr, String*);
802int32 runtime·funcarglen(Func*, uintptr);
803int32 runtime·funcspdelta(Func*, uintptr);
Russ Coxc3de91b2013-07-18 10:43:22 -0400804int8* runtime·funcname(Func*);
Carl Shapiro16d6b6c2013-09-16 19:03:19 -0700805int32 runtime·pcdatavalue(Func*, int32, uintptr);
Russ Cox68b42552010-11-04 14:00:19 -0400806void* runtime·stackalloc(uint32);
Russ Coxafc69282011-01-25 16:35:36 -0500807void runtime·stackfree(void*, uintptr);
Russ Cox68b42552010-11-04 14:00:19 -0400808MCache* runtime·allocmcache(void);
Dmitriy Vyukoved516df2012-07-01 13:10:01 +0400809void runtime·freemcache(MCache*);
Russ Cox68b42552010-11-04 14:00:19 -0400810void runtime·mallocinit(void);
Jan Ziak4da6b362013-01-30 09:01:31 -0800811void runtime·mprofinit(void);
Russ Cox68b42552010-11-04 14:00:19 -0400812bool runtime·ifaceeq_c(Iface, Iface);
813bool runtime·efaceeq_c(Eface, Eface);
Ian Lance Taylor63bee952013-01-04 07:53:42 -0800814uintptr runtime·ifacehash(Iface, uintptr);
815uintptr runtime·efacehash(Eface, uintptr);
Russ Cox68b42552010-11-04 14:00:19 -0400816void* runtime·malloc(uintptr size);
817void runtime·free(void *v);
Russ Cox68b42552010-11-04 14:00:19 -0400818void runtime·runpanic(Panic*);
Russ Coxe58f7982013-06-12 08:49:38 -0400819uintptr runtime·getcallersp(void*);
Russ Cox4608feb2011-01-28 15:03:26 -0500820int32 runtime·mcount(void);
Russ Coxe4b02bf2012-02-22 21:45:01 -0500821int32 runtime·gcount(void);
Russ Coxf9ca3b52011-03-07 10:37:42 -0500822void runtime·mcall(void(*)(G*));
Dmitriy Vyukov909f3182011-07-12 01:23:58 -0400823uint32 runtime·fastrand1(void);
Russ Cox6fa3c892013-06-27 11:32:01 -0400824void runtime·rewindmorestack(Gobuf*);
Dmitriy Vyukove84d9e12013-07-29 22:22:34 +0400825int32 runtime·timediv(int64, int32, int32*);
Russ Coxd28acc42008-08-04 16:43:49 -0700826
Russ Cox6c976392013-02-20 17:48:23 -0500827void runtime·setmg(M*, G*);
828void runtime·newextram(void);
Russ Cox68b42552010-11-04 14:00:19 -0400829void runtime·exit(int32);
830void runtime·breakpoint(void);
831void runtime·gosched(void);
Dmitriy Vyukov1e112cd2013-06-28 17:52:17 +0400832void runtime·gosched0(G*);
Dmitriy Vyukov4f2e3822013-08-14 00:30:55 +0400833void runtime·schedtrace(bool);
Dmitriy Vyukovf20fd872012-09-18 21:15:46 +0400834void runtime·park(void(*)(Lock*), Lock*, int8*);
835void runtime·tsleep(int64, int8*);
Russ Cox3b860262011-11-09 15:17:05 -0500836M* runtime·newm(void);
Russ Cox68b42552010-11-04 14:00:19 -0400837void runtime·goexit(void);
Russ Coxf9ca3b52011-03-07 10:37:42 -0500838void runtime·asmcgocall(void (*fn)(void*), void*);
Russ Cox68b42552010-11-04 14:00:19 -0400839void runtime·entersyscall(void);
Dmitriy Vyukove25f19a2013-02-20 20:21:45 +0400840void runtime·entersyscallblock(void);
Russ Cox68b42552010-11-04 14:00:19 -0400841void runtime·exitsyscall(void);
Russ Cox1903ad72013-02-21 17:01:13 -0500842G* runtime·newproc1(FuncVal*, byte*, int32, int32, void*);
Russ Cox68b42552010-11-04 14:00:19 -0400843bool runtime·sigsend(int32 sig);
Russ Cox68b42552010-11-04 14:00:19 -0400844int32 runtime·callers(int32, uintptr*, int32);
845int64 runtime·nanotime(void);
846void runtime·dopanic(int32);
Russ Cox67793502011-02-16 13:21:13 -0500847void runtime·startpanic(void);
Dmitriy Vyukov01f1e3d2013-08-09 12:53:35 +0400848void runtime·freezetheworld(void);
Dmitriy Vyukova54f920b2012-07-04 14:52:51 +0400849void runtime·unwindstack(G*, byte*);
Russ Coxc19b3732011-03-23 11:43:37 -0400850void runtime·sigprof(uint8 *pc, uint8 *sp, uint8 *lr, G *gp);
851void runtime·resetcpuprofiler(int32);
852void runtime·setcpuprofilerate(void(*)(uintptr*, int32), int32);
Russ Coxd324f212011-09-30 09:40:01 -0400853void runtime·usleep(uint32);
Damian Gryski8e765da2012-02-02 14:09:27 -0500854int64 runtime·cputicks(void);
Dmitriy Vyukov4cc7bf32012-10-06 12:56:04 +0400855int64 runtime·tickspersecond(void);
856void runtime·blockevent(int64, int32);
857extern int64 runtime·blockprofilerate;
Dmitriy Vyukovd0c11d22013-03-05 09:38:15 +0200858void runtime·addtimer(Timer*);
859bool runtime·deltimer(Timer*);
Dmitriy Vyukovc2118842013-03-12 21:14:26 +0400860G* runtime·netpoll(bool);
Dmitriy Vyukov0bee99a2013-03-14 10:38:37 +0400861void runtime·netpollinit(void);
Alex Brainman38abb092013-05-20 12:55:50 +1000862int32 runtime·netpollopen(uintptr, PollDesc*);
863int32 runtime·netpollclose(uintptr);
Dmitriy Vyukov0bee99a2013-03-14 10:38:37 +0400864void runtime·netpollready(G**, PollDesc*, int32);
Dmitriy Vyukov65834682013-08-08 17:41:57 +0400865uintptr runtime·netpollfd(PollDesc*);
Russ Cox5146a932013-03-15 01:11:03 -0400866void runtime·crash(void);
Dmitriy Vyukov4b536a52013-06-28 18:37:06 +0400867void runtime·parsedebugvars(void);
Russ Coxe58f7982013-06-12 08:49:38 -0400868void _rt0_go(void);
Russ Cox48769bf2013-07-19 16:04:09 -0400869void* runtime·funcdata(Func*, int32);
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800870
Russ Cox68b42552010-11-04 14:00:19 -0400871#pragma varargck argpos runtime·printf 1
Carl Shapiro4e0a51c2013-05-28 17:59:10 -0700872#pragma varargck type "c" int32
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800873#pragma varargck type "d" int32
874#pragma varargck type "d" uint32
875#pragma varargck type "D" int64
876#pragma varargck type "D" uint64
877#pragma varargck type "x" int32
878#pragma varargck type "x" uint32
879#pragma varargck type "X" int64
880#pragma varargck type "X" uint64
881#pragma varargck type "p" void*
Russ Cox0d3a0432009-03-30 00:01:07 -0700882#pragma varargck type "p" uintptr
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800883#pragma varargck type "s" int8*
884#pragma varargck type "s" uint8*
Ken Thompson36570612009-04-09 18:16:21 -0700885#pragma varargck type "S" String
Russ Cox5bb0c4f2008-12-15 10:50:33 -0800886
Russ Cox68b42552010-11-04 14:00:19 -0400887void runtime·stoptheworld(void);
Dmitriy Vyukov01826282012-05-15 19:10:16 +0400888void runtime·starttheworld(void);
Russ Coxe4b02bf2012-02-22 21:45:01 -0500889extern uint32 runtime·worldsema;
Russ Cox3f8aa662008-12-05 15:24:18 -0800890
Russ Coxd28acc42008-08-04 16:43:49 -0700891/*
892 * mutual exclusion locks. in the uncontended case,
893 * as fast as spin locks (just a few user-level instructions),
894 * but on the contention path they sleep in the kernel.
Russ Cox96824002008-08-05 14:18:47 -0700895 * a zeroed Lock is unlocked (no need to initialize each lock).
Russ Coxd28acc42008-08-04 16:43:49 -0700896 */
Russ Cox68b42552010-11-04 14:00:19 -0400897void runtime·lock(Lock*);
898void runtime·unlock(Lock*);
Russ Coxd28acc42008-08-04 16:43:49 -0700899
900/*
Russ Coxa68592a2009-10-14 13:02:05 -0700901 * sleep and wakeup on one-time events.
Russ Coxf7f63292008-08-05 14:21:42 -0700902 * before any calls to notesleep or notewakeup,
Russ Cox96824002008-08-05 14:18:47 -0700903 * must call noteclear to initialize the Note.
Dmitriy Vyukova496c9e2011-08-03 15:51:55 -0400904 * then, exactly one thread can call notesleep
Russ Cox96824002008-08-05 14:18:47 -0700905 * and exactly one thread can call notewakeup (once).
Dmitriy Vyukova496c9e2011-08-03 15:51:55 -0400906 * once notewakeup has been called, the notesleep
907 * will return. future notesleep will return immediately.
908 * subsequent noteclear must be called only after
909 * previous notesleep has returned, e.g. it's disallowed
910 * to call noteclear straight after notewakeup.
Russ Cox3b860262011-11-09 15:17:05 -0500911 *
912 * notetsleep is like notesleep but wakes up after
913 * a given number of nanoseconds even if the event
914 * has not yet happened. if a goroutine uses notetsleep to
915 * wake up early, it must wait to call noteclear until it
916 * can be sure that no other goroutine is calling
917 * notewakeup.
Dmitriy Vyukove97d6772013-07-22 23:02:27 +0400918 *
919 * notesleep/notetsleep are generally called on g0,
920 * notetsleepg is similar to notetsleep but is called on user g.
Russ Coxd28acc42008-08-04 16:43:49 -0700921 */
Russ Cox68b42552010-11-04 14:00:19 -0400922void runtime·noteclear(Note*);
923void runtime·notesleep(Note*);
924void runtime·notewakeup(Note*);
Dmitriy Vyukove932c202013-05-29 11:49:45 +0400925bool runtime·notetsleep(Note*, int64); // false - timeout
Dmitriy Vyukove97d6772013-07-22 23:02:27 +0400926bool runtime·notetsleepg(Note*, int64); // false - timeout
Russ Cox3b860262011-11-09 15:17:05 -0500927
928/*
929 * low-level synchronization for implementing the above
930 */
931uintptr runtime·semacreate(void);
932int32 runtime·semasleep(int64);
933void runtime·semawakeup(M*);
934// or
935void runtime·futexsleep(uint32*, uint32, int64);
936void runtime·futexwakeup(uint32*, uint32);
Ian Lance Taylor9b8da822009-01-13 09:55:24 -0800937
938/*
Dmitriy Vyukova5dc7792012-04-12 11:49:25 +0400939 * Lock-free stack.
940 * Initialize uint64 head to 0, compare with 0 to test for emptiness.
941 * The stack does not keep pointers to nodes,
942 * so they can be garbage collected if there are no other pointers to nodes.
943 */
944void runtime·lfstackpush(uint64 *head, LFNode *node);
945LFNode* runtime·lfstackpop(uint64 *head);
946
947/*
Dmitriy Vyukov95643642012-05-11 10:50:03 +0400948 * Parallel for over [0, n).
949 * body() is executed for each iteration.
950 * nthr - total number of worker threads.
951 * ctx - arbitrary user context.
952 * if wait=true, threads return from parfor() when all work is done;
953 * otherwise, threads can return while other threads are still finishing processing.
954 */
955ParFor* runtime·parforalloc(uint32 nthrmax);
956void runtime·parforsetup(ParFor *desc, uint32 nthr, uint32 n, void *ctx, bool wait, void (*body)(ParFor*, uint32));
957void runtime·parfordo(ParFor *desc);
958
959/*
Adam Langley3f7a3242009-11-13 10:08:51 -0800960 * This is consistent across Linux and BSD.
961 * If a new OS is added that is different, move this to
962 * $GOOS/$GOARCH/defs.h.
963 */
964#define EACCES 13
965
966/*
Russ Coxafc69282011-01-25 16:35:36 -0500967 * low level C-called
Ken Thompson4e8142c2008-06-16 22:34:50 -0700968 */
Shenghou Mab151af12012-09-21 13:50:02 +0800969// for mmap, we only pass the lower 32 bits of file offset to the
970// assembly routine; the higher bits (if required), should be provided
971// by the assembly routine as 0.
Russ Cox68b42552010-11-04 14:00:19 -0400972uint8* runtime·mmap(byte*, uintptr, int32, int32, int32, uint32);
Russ Coxe83cd7f2011-12-20 17:54:40 -0500973void runtime·munmap(byte*, uintptr);
974void runtime·madvise(byte*, uintptr, int32);
Russ Coxafc69282011-01-25 16:35:36 -0500975void runtime·memclr(byte*, uintptr);
Russ Cox68b42552010-11-04 14:00:19 -0400976void runtime·setcallerpc(void*, void*);
977void* runtime·getcallerpc(void*);
Ken Thompsonbbb20732008-06-05 19:38:39 -0700978
979/*
Ken Thompson4e8142c2008-06-16 22:34:50 -0700980 * runtime go-called
Ken Thompsonbbb20732008-06-05 19:38:39 -0700981 */
Russ Cox68b42552010-11-04 14:00:19 -0400982void runtime·printbool(bool);
Carl Shapiro7f9c02a2013-02-19 18:05:44 -0800983void runtime·printbyte(int8);
Russ Cox68b42552010-11-04 14:00:19 -0400984void runtime·printfloat(float64);
985void runtime·printint(int64);
986void runtime·printiface(Iface);
987void runtime·printeface(Eface);
988void runtime·printstring(String);
989void runtime·printpc(void*);
990void runtime·printpointer(void*);
991void runtime·printuint(uint64);
992void runtime·printhex(uint64);
993void runtime·printslice(Slice);
994void runtime·printcomplex(Complex128);
Keith Randall9cd57062013-08-02 13:03:14 -0700995void runtime·newstackcall(FuncVal*, byte*, uint32);
Russ Cox1903ad72013-02-21 17:01:13 -0500996void reflect·call(FuncVal*, byte*, uint32);
Russ Cox68b42552010-11-04 14:00:19 -0400997void runtime·panic(Eface);
998void runtime·panicindex(void);
999void runtime·panicslice(void);
Russ Cox7c2b1592010-10-25 17:55:50 -07001000
Russ Cox63e878a2010-03-31 15:55:10 -07001001/*
1002 * runtime c-called (but written in Go)
1003 */
Russ Cox68b42552010-11-04 14:00:19 -04001004void runtime·printany(Eface);
Russ Cox6a75ece2012-02-12 23:26:20 -05001005void runtime·newTypeAssertionError(String*, String*, String*, String*, Eface*);
Russ Cox68b42552010-11-04 14:00:19 -04001006void runtime·newErrorString(String, Eface*);
1007void runtime·fadd64c(uint64, uint64, uint64*);
1008void runtime·fsub64c(uint64, uint64, uint64*);
1009void runtime·fmul64c(uint64, uint64, uint64*);
1010void runtime·fdiv64c(uint64, uint64, uint64*);
1011void runtime·fneg64c(uint64, uint64*);
1012void runtime·f32to64c(uint32, uint64*);
1013void runtime·f64to32c(uint64, uint32*);
1014void runtime·fcmp64c(uint64, uint64, int32*, bool*);
1015void runtime·fintto64c(int64, uint64*);
1016void runtime·f64tointc(uint64, int64*, bool*);
Rob Pike6db99de2008-07-08 10:36:43 -07001017
1018/*
Russ Cox1f8a40d2009-01-22 16:23:44 -08001019 * wrapped for go users
Rob Pike6db99de2008-07-08 10:36:43 -07001020 */
Russ Cox68b42552010-11-04 14:00:19 -04001021float64 runtime·Inf(int32 sign);
1022float64 runtime·NaN(void);
1023float32 runtime·float32frombits(uint32 i);
1024uint32 runtime·float32tobits(float32 f);
1025float64 runtime·float64frombits(uint64 i);
1026uint64 runtime·float64tobits(float64 f);
1027float64 runtime·frexp(float64 d, int32 *ep);
1028bool runtime·isInf(float64 f, int32 sign);
1029bool runtime·isNaN(float64 f);
1030float64 runtime·ldexp(float64 d, int32 e);
1031float64 runtime·modf(float64 d, float64 *ip);
Dmitriy Vyukov23e15f72013-08-09 21:43:00 +04001032void runtime·semacquire(uint32*, bool);
Russ Cox68b42552010-11-04 14:00:19 -04001033void runtime·semrelease(uint32*);
Russ Cox68b42552010-11-04 14:00:19 -04001034int32 runtime·gomaxprocsfunc(int32 n);
Dmitriy Vyukov4e5086b2011-07-29 12:44:06 -04001035void runtime·procyield(uint32);
1036void runtime·osyield(void);
Russ Coxb0a29f32013-02-01 08:34:41 -08001037void runtime·lockOSThread(void);
1038void runtime·unlockOSThread(void);
David Symondsb5866492009-12-15 18:21:29 -08001039
Russ Cox65bde082011-08-17 14:56:27 -04001040void runtime·mapassign(MapType*, Hmap*, byte*, byte*);
1041void runtime·mapaccess(MapType*, Hmap*, byte*, byte*, bool*);
Russ Cox68b42552010-11-04 14:00:19 -04001042void runtime·mapiternext(struct hash_iter*);
1043bool runtime·mapiterkey(struct hash_iter*, void*);
Russ Cox65bde082011-08-17 14:56:27 -04001044Hmap* runtime·makemap_c(MapType*, int64);
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001045
Russ Cox3770b0e2011-08-17 15:54:17 -04001046Hchan* runtime·makechan_c(ChanType*, int64);
Dmitriy Vyukov2f6cbc72012-10-07 22:05:32 +04001047void runtime·chansend(ChanType*, Hchan*, byte*, bool*, void*);
Russ Cox33e9d242011-08-23 13:13:27 -04001048void runtime·chanrecv(ChanType*, Hchan*, byte*, bool*, bool*);
Russ Cox6fa3c892013-06-27 11:32:01 -04001049bool runtime·showframe(Func*, G*);
Dmitriy Vyukovc33d4902013-08-01 19:28:38 +04001050void runtime·printcreatedby(G*);
Russ Cox5ddaf9a2009-07-08 15:00:54 -07001051
Jan Ziak5c1422a2012-11-01 13:13:20 -04001052void runtime·ifaceE2I(InterfaceType*, Eface, Iface*);
Russ Cox5822e782013-08-14 14:54:31 -04001053bool runtime·ifaceE2I2(InterfaceType*, Eface, Iface*);
Russ Cox102274a2012-02-24 15:28:51 -05001054uintptr runtime·memlimit(void);
Russ Cox6e2ae0a2012-02-28 16:18:24 -05001055
Shenghou Ma0157c722012-08-07 23:45:50 +08001056// float.c
1057extern float64 runtime·nan;
1058extern float64 runtime·posinf;
1059extern float64 runtime·neginf;
1060extern uint64 ·nan;
1061extern uint64 ·posinf;
1062extern uint64 ·neginf;
1063#define ISNAN(f) ((f) != (f))
Jan Ziakf8c58372012-09-24 20:08:05 -04001064
1065enum
1066{
Dmitriy Vyukov5b79aa82013-03-14 13:48:19 +04001067 UseSpanType = 1,
Jan Ziakf8c58372012-09-24 20:08:05 -04001068};