1 // 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.
7 #include "arch_GOARCH.h"
11 // Keep a cached value to make gotraceback fast,
12 // since we call it on every call to gentraceback.
13 // The cached value is a uint32 in which the low bit
14 // is the "crash" setting and the top 31 bits are the
16 static uint32 traceback_cache = 2<<1;
18 // The GOTRACEBACK environment variable controls the
19 // behavior of a Go program that is crashing and exiting.
20 // GOTRACEBACK=0 suppress all tracebacks
21 // GOTRACEBACK=1 default behavior - show tracebacks but exclude runtime frames
22 // GOTRACEBACK=2 show tracebacks including runtime frames
23 // GOTRACEBACK=crash show tracebacks including runtime frames, then crash (core dump etc)
24 #pragma textflag NOSPLIT
26 runtime·gotraceback(bool *crash)
30 if(g->m->traceback != 0)
31 return g->m->traceback;
33 *crash = traceback_cache&1;
34 return traceback_cache>>1;
38 runtime·mcmp(byte *s1, byte *s2, uintptr n)
56 runtime·mchr(byte *p, byte c, byte *ep)
66 #pragma dataflag NOPTR /* argv not a heap pointer */
69 extern Slice runtime·argslice;
70 extern Slice runtime·envs;
72 void (*runtime·sysargs)(int32, uint8**);
75 runtime·args(int32 c, uint8 **v)
79 if(runtime·sysargs != nil)
80 runtime·sysargs(c, v);
83 int32 runtime·isplan9;
84 int32 runtime·issolaris;
85 int32 runtime·iswindows;
87 // Information about what cpu features are available.
88 // Set on startup in asm_{x86/amd64}.s.
89 uint32 runtime·cpuid_ecx;
90 uint32 runtime·cpuid_edx;
98 // for windows implementation see "os" package
102 runtime·argslice = runtime·makeStringSlice(argc);
103 s = (String*)runtime·argslice.array;
104 for(i=0; i<argc; i++)
105 s[i] = runtime·gostringnocopy(argv[i]);
109 runtime·goenvs_unix(void)
114 for(n=0; argv[argc+1+n] != 0; n++)
117 runtime·envs = runtime·makeStringSlice(n);
118 s = (String*)runtime·envs.array;
120 s[i] = runtime·gostringnocopy(argv[argc+1+i]);
123 #pragma textflag NOSPLIT
131 runtime·atoi(byte *p)
136 while('0' <= *p && *p <= '9')
137 n = n*10 + *p++ - '0';
149 if(runtime·cas64(&z64, x64, 1))
150 runtime·throw("cas64 failed");
152 runtime·throw("cas64 failed");
154 if(!runtime·cas64(&z64, x64, 1))
155 runtime·throw("cas64 failed");
156 if(x64 != 42 || z64 != 1)
157 runtime·throw("cas64 failed");
158 if(runtime·atomicload64(&z64) != 1)
159 runtime·throw("load64 failed");
160 runtime·atomicstore64(&z64, (1ull<<40)+1);
161 if(runtime·atomicload64(&z64) != (1ull<<40)+1)
162 runtime·throw("store64 failed");
163 if(runtime·xadd64(&z64, (1ull<<40)+1) != (2ull<<40)+2)
164 runtime·throw("xadd64 failed");
165 if(runtime·atomicload64(&z64) != (2ull<<40)+2)
166 runtime·throw("xadd64 failed");
167 if(runtime·xchg64(&z64, (3ull<<40)+3) != (2ull<<40)+2)
168 runtime·throw("xchg64 failed");
169 if(runtime·atomicload64(&z64) != (3ull<<40)+3)
170 runtime·throw("xchg64 failed");
197 if(sizeof(a) != 1) runtime·throw("bad a");
198 if(sizeof(b) != 1) runtime·throw("bad b");
199 if(sizeof(c) != 2) runtime·throw("bad c");
200 if(sizeof(d) != 2) runtime·throw("bad d");
201 if(sizeof(e) != 4) runtime·throw("bad e");
202 if(sizeof(f) != 4) runtime·throw("bad f");
203 if(sizeof(g) != 8) runtime·throw("bad g");
204 if(sizeof(h) != 8) runtime·throw("bad h");
205 if(sizeof(i) != 4) runtime·throw("bad i");
206 if(sizeof(j) != 8) runtime·throw("bad j");
207 if(sizeof(k) != sizeof(uintptr)) runtime·throw("bad k");
208 if(sizeof(l) != sizeof(uintptr)) runtime·throw("bad l");
209 if(sizeof(struct x1) != 1) runtime·throw("bad sizeof x1");
210 if(offsetof(struct y1, y) != 1) runtime·throw("bad offsetof y1.y");
211 if(sizeof(struct y1) != 2) runtime·throw("bad sizeof y1");
213 if(runtime·timediv(12345LL*1000000000+54321, 1000000000, &e) != 12345 || e != 54321)
214 runtime·throw("bad timediv");
218 if(!runtime·cas(&z, 1, 2))
219 runtime·throw("cas1");
221 runtime·throw("cas2");
224 if(runtime·cas(&z, 5, 6))
225 runtime·throw("cas3");
227 runtime·throw("cas4");
229 k = (byte*)0xfedcb123;
230 if(sizeof(void*) == 8)
231 k = (byte*)((uintptr)k<<10);
232 if(runtime·casp((void**)&k, nil, nil))
233 runtime·throw("casp1");
235 if(!runtime·casp((void**)&k, k, k1))
236 runtime·throw("casp2");
238 runtime·throw("casp3");
240 m[0] = m[1] = m[2] = m[3] = 0x1;
241 runtime·atomicor8(&m[1], 0xf0);
242 if (m[0] != 0x1 || m[1] != 0xf1 || m[2] != 0x1 || m[3] != 0x1)
243 runtime·throw("atomicor8");
245 *(uint64*)&j = ~0ULL;
247 runtime·throw("float64nan");
249 runtime·throw("float64nan1");
251 *(uint64*)&j1 = ~1ULL;
253 runtime·throw("float64nan2");
255 runtime·throw("float64nan3");
259 runtime·throw("float32nan");
261 runtime·throw("float32nan1");
263 *(uint32*)&i1 = ~1UL;
265 runtime·throw("float32nan2");
267 runtime·throw("float32nan3");
271 if(FixedStack != runtime·round2(FixedStack))
272 runtime·throw("FixedStack is not power-of-2");
275 #pragma dataflag NOPTR
276 DebugVars runtime·debug;
278 typedef struct DbgVar DbgVar;
285 // Do we report invalid pointers found during stack or heap scans?
286 int32 runtime·invalidptr = 1;
288 #pragma dataflag NOPTR /* dbgvar has no heap pointers */
289 static DbgVar dbgvar[] = {
290 {"allocfreetrace", &runtime·debug.allocfreetrace},
291 {"invalidptr", &runtime·invalidptr},
292 {"efence", &runtime·debug.efence},
293 {"gctrace", &runtime·debug.gctrace},
294 {"gcdead", &runtime·debug.gcdead},
295 {"scheddetail", &runtime·debug.scheddetail},
296 {"schedtrace", &runtime·debug.schedtrace},
297 {"scavenge", &runtime·debug.scavenge},
301 runtime·parsedebugvars(void)
306 p = runtime·getenv("GODEBUG");
309 for(i=0; i<nelem(dbgvar); i++) {
310 n = runtime·findnull((byte*)dbgvar[i].name);
311 if(runtime·mcmp(p, (byte*)dbgvar[i].name, n) == 0 && p[n] == '=')
312 *dbgvar[i].value = runtime·atoi(p+n+1);
314 p = runtime·strstr(p, (byte*)",");
321 p = runtime·getenv("GOTRACEBACK");
325 traceback_cache = 1<<1;
326 else if(runtime·strcmp(p, (byte*)"crash") == 0)
327 traceback_cache = (2<<1) | 1;
329 traceback_cache = runtime·atoi(p)<<1;
332 // Poor mans 64-bit division.
333 // This is a very special function, do not use it if you are not sure what you are doing.
334 // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
335 // Handles overflow in a time-specific manner.
336 #pragma textflag NOSPLIT
338 runtime·timediv(int64 v, int32 div, int32 *rem)
343 for(bit = 30; bit >= 0; bit--) {
344 if(v >= ((int64)div<<bit)) {
345 v = v - ((int64)div<<bit);
349 if(v >= (int64)div) {
359 // Helpers for Go. Must be NOSPLIT, must only call NOSPLIT functions, and must not block.
361 #pragma textflag NOSPLIT
368 #pragma textflag NOSPLIT
370 runtime·acquirem(void)
376 #pragma textflag NOSPLIT
378 runtime·releasem(M *mp)
381 if(mp->locks == 0 && g->preempt) {
382 // restore the preemption request in case we've cleared it in newstack
383 g->stackguard0 = StackPreempt;
387 #pragma textflag NOSPLIT
389 runtime·gomcache(void)
394 #pragma textflag NOSPLIT
396 reflect·typelinks(void)
398 extern Type *runtime·typelink[], *runtime·etypelink[];
401 ret.array = (byte*)runtime·typelink;
402 ret.len = runtime·etypelink - runtime·typelink;