Rebase.
[official-gcc.git] / libgo / runtime / runtime.c
blob33fe1e7ee600046b47996d6148c6ab302363de69
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.
5 #include <signal.h>
6 #include <unistd.h>
8 #include "config.h"
10 #include "runtime.h"
11 #include "arch.h"
12 #include "array.h"
14 enum {
15 maxround = sizeof(uintptr),
18 // Keep a cached value to make gotraceback fast,
19 // since we call it on every call to gentraceback.
20 // The cached value is a uint32 in which the low bit
21 // is the "crash" setting and the top 31 bits are the
22 // gotraceback value.
23 static uint32 traceback_cache = ~(uint32)0;
25 // The GOTRACEBACK environment variable controls the
26 // behavior of a Go program that is crashing and exiting.
27 // GOTRACEBACK=0 suppress all tracebacks
28 // GOTRACEBACK=1 default behavior - show tracebacks but exclude runtime frames
29 // GOTRACEBACK=2 show tracebacks including runtime frames
30 // GOTRACEBACK=crash show tracebacks including runtime frames, then crash (core dump etc)
31 int32
32 runtime_gotraceback(bool *crash)
34 const byte *p;
35 uint32 x;
37 if(crash != nil)
38 *crash = false;
39 if(runtime_m()->traceback != 0)
40 return runtime_m()->traceback;
41 x = runtime_atomicload(&traceback_cache);
42 if(x == ~(uint32)0) {
43 p = runtime_getenv("GOTRACEBACK");
44 if(p == nil)
45 p = (const byte*)"";
46 if(p[0] == '\0')
47 x = 1<<1;
48 else if(runtime_strcmp((const char *)p, "crash") == 0)
49 x = (2<<1) | 1;
50 else
51 x = runtime_atoi(p)<<1;
52 runtime_atomicstore(&traceback_cache, x);
54 if(crash != nil)
55 *crash = x&1;
56 return x>>1;
59 static int32 argc;
60 static byte** argv;
62 extern Slice os_Args __asm__ (GOSYM_PREFIX "os.Args");
63 extern Slice syscall_Envs __asm__ (GOSYM_PREFIX "syscall.Envs");
65 void (*runtime_sysargs)(int32, uint8**);
67 void
68 runtime_args(int32 c, byte **v)
70 argc = c;
71 argv = v;
72 if(runtime_sysargs != nil)
73 runtime_sysargs(c, v);
76 byte*
77 runtime_progname()
79 return argc == 0 ? nil : argv[0];
82 void
83 runtime_goargs(void)
85 String *s;
86 int32 i;
88 // for windows implementation see "os" package
89 if(Windows)
90 return;
92 s = runtime_malloc(argc*sizeof s[0]);
93 for(i=0; i<argc; i++)
94 s[i] = runtime_gostringnocopy((const byte*)argv[i]);
95 os_Args.__values = (void*)s;
96 os_Args.__count = argc;
97 os_Args.__capacity = argc;
100 void
101 runtime_goenvs_unix(void)
103 String *s;
104 int32 i, n;
106 for(n=0; argv[argc+1+n] != 0; n++)
109 s = runtime_malloc(n*sizeof s[0]);
110 for(i=0; i<n; i++)
111 s[i] = runtime_gostringnocopy(argv[argc+1+i]);
112 syscall_Envs.__values = (void*)s;
113 syscall_Envs.__count = n;
114 syscall_Envs.__capacity = n;
116 traceback_cache = ~(uint32)0;
119 int32
120 runtime_atoi(const byte *p)
122 int32 n;
124 n = 0;
125 while('0' <= *p && *p <= '9')
126 n = n*10 + *p++ - '0';
127 return n;
130 static struct root_list runtime_roots =
131 { nil,
132 { { &syscall_Envs, sizeof syscall_Envs },
133 { &os_Args, sizeof os_Args },
134 { nil, 0 } },
137 static void
138 TestAtomic64(void)
140 uint64 z64, x64;
142 z64 = 42;
143 x64 = 0;
144 PREFETCH(&z64);
145 if(runtime_cas64(&z64, x64, 1))
146 runtime_throw("cas64 failed");
147 if(x64 != 0)
148 runtime_throw("cas64 failed");
149 x64 = 42;
150 if(!runtime_cas64(&z64, x64, 1))
151 runtime_throw("cas64 failed");
152 if(x64 != 42 || z64 != 1)
153 runtime_throw("cas64 failed");
154 if(runtime_atomicload64(&z64) != 1)
155 runtime_throw("load64 failed");
156 runtime_atomicstore64(&z64, (1ull<<40)+1);
157 if(runtime_atomicload64(&z64) != (1ull<<40)+1)
158 runtime_throw("store64 failed");
159 if(runtime_xadd64(&z64, (1ull<<40)+1) != (2ull<<40)+2)
160 runtime_throw("xadd64 failed");
161 if(runtime_atomicload64(&z64) != (2ull<<40)+2)
162 runtime_throw("xadd64 failed");
163 if(runtime_xchg64(&z64, (3ull<<40)+3) != (2ull<<40)+2)
164 runtime_throw("xchg64 failed");
165 if(runtime_atomicload64(&z64) != (3ull<<40)+3)
166 runtime_throw("xchg64 failed");
169 void
170 runtime_check(void)
172 __go_register_gc_roots(&runtime_roots);
174 TestAtomic64();
177 uint32
178 runtime_fastrand1(void)
180 M *m;
181 uint32 x;
183 m = runtime_m();
184 x = m->fastrand;
185 x += x;
186 if(x & 0x80000000L)
187 x ^= 0x88888eefUL;
188 m->fastrand = x;
189 return x;
192 int64
193 runtime_cputicks(void)
195 #if defined(__386__) || defined(__x86_64__)
196 uint32 low, high;
197 asm("rdtsc" : "=a" (low), "=d" (high));
198 return (int64)(((uint64)high << 32) | (uint64)low);
199 #else
200 // FIXME: implement for other processors.
201 return 0;
202 #endif
205 bool
206 runtime_showframe(String s, bool current)
208 static int32 traceback = -1;
210 if(current && runtime_m()->throwing > 0)
211 return 1;
212 if(traceback < 0)
213 traceback = runtime_gotraceback(nil);
214 return traceback > 1 || (__builtin_memchr(s.str, '.', s.len) != nil && __builtin_memcmp(s.str, "runtime.", 7) != 0);
217 static Lock ticksLock;
218 static int64 ticks;
220 int64
221 runtime_tickspersecond(void)
223 int64 res, t0, t1, c0, c1;
225 res = (int64)runtime_atomicload64((uint64*)&ticks);
226 if(res != 0)
227 return ticks;
228 runtime_lock(&ticksLock);
229 res = ticks;
230 if(res == 0) {
231 t0 = runtime_nanotime();
232 c0 = runtime_cputicks();
233 runtime_usleep(100*1000);
234 t1 = runtime_nanotime();
235 c1 = runtime_cputicks();
236 if(t1 == t0)
237 t1++;
238 res = (c1-c0)*1000*1000*1000/(t1-t0);
239 if(res == 0)
240 res++;
241 runtime_atomicstore64((uint64*)&ticks, res);
243 runtime_unlock(&ticksLock);
244 return res;
247 // Called to initialize a new m (including the bootstrap m).
248 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
249 void
250 runtime_mpreinit(M *mp)
252 mp->gsignal = runtime_malg(32*1024, &mp->gsignalstack, &mp->gsignalstacksize); // OS X wants >=8K, Linux >=2K
255 // Called to initialize a new m (including the bootstrap m).
256 // Called on the new thread, can not allocate memory.
257 void
258 runtime_minit(void)
260 M* m;
261 sigset_t sigs;
263 // Initialize signal handling.
264 m = runtime_m();
265 runtime_signalstack(m->gsignalstack, m->gsignalstacksize);
266 if (sigemptyset(&sigs) != 0)
267 runtime_throw("sigemptyset");
268 pthread_sigmask(SIG_SETMASK, &sigs, nil);
271 // Called from dropm to undo the effect of an minit.
272 void
273 runtime_unminit(void)
275 runtime_signalstack(nil, 0);
279 void
280 runtime_signalstack(byte *p, int32 n)
282 stack_t st;
284 st.ss_sp = p;
285 st.ss_size = n;
286 st.ss_flags = 0;
287 if(p == nil)
288 st.ss_flags = SS_DISABLE;
289 if(sigaltstack(&st, nil) < 0)
290 *(int *)0xf1 = 0xf1;
293 DebugVars runtime_debug;
295 static struct {
296 const char* name;
297 int32* value;
298 } dbgvar[] = {
299 {"allocfreetrace", &runtime_debug.allocfreetrace},
300 {"efence", &runtime_debug.efence},
301 {"gctrace", &runtime_debug.gctrace},
302 {"gcdead", &runtime_debug.gcdead},
303 {"scheddetail", &runtime_debug.scheddetail},
304 {"schedtrace", &runtime_debug.schedtrace},
307 void
308 runtime_parsedebugvars(void)
310 const byte *p;
311 intgo i, n;
313 p = runtime_getenv("GODEBUG");
314 if(p == nil)
315 return;
316 for(;;) {
317 for(i=0; i<(intgo)nelem(dbgvar); i++) {
318 n = runtime_findnull((const byte*)dbgvar[i].name);
319 if(runtime_mcmp(p, dbgvar[i].name, n) == 0 && p[n] == '=')
320 *dbgvar[i].value = runtime_atoi(p+n+1);
322 p = (const byte *)runtime_strstr((const char *)p, ",");
323 if(p == nil)
324 break;
325 p++;
329 // Poor mans 64-bit division.
330 // This is a very special function, do not use it if you are not sure what you are doing.
331 // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
332 // Handles overflow in a time-specific manner.
333 int32
334 runtime_timediv(int64 v, int32 div, int32 *rem)
336 int32 res, bit;
338 if(v >= (int64)div*0x7fffffffLL) {
339 if(rem != nil)
340 *rem = 0;
341 return 0x7fffffff;
343 res = 0;
344 for(bit = 30; bit >= 0; bit--) {
345 if(v >= ((int64)div<<bit)) {
346 v = v - ((int64)div<<bit);
347 res += 1<<bit;
350 if(rem != nil)
351 *rem = v;
352 return res;
355 // Setting the max stack size doesn't really do anything for gccgo.
357 uintptr runtime_maxstacksize = 1<<20; // enough until runtime.main sets it for real
359 void memclrBytes(Slice)
360 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
362 void
363 memclrBytes(Slice s)
365 runtime_memclr(s.__values, s.__count);