[Patch 2/7 s390] Deprecate *_BY_PIECES_P, move to hookized version
[official-gcc.git] / libgo / runtime / runtime.c
blobbae462120923a085b023c2ac2f96ce8ddf410fdf
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;
117 int32
118 runtime_atoi(const byte *p)
120 int32 n;
122 n = 0;
123 while('0' <= *p && *p <= '9')
124 n = n*10 + *p++ - '0';
125 return n;
128 static struct root_list runtime_roots =
129 { nil,
130 { { &syscall_Envs, sizeof syscall_Envs },
131 { &os_Args, sizeof os_Args },
132 { nil, 0 } },
135 static void
136 TestAtomic64(void)
138 uint64 z64, x64;
140 z64 = 42;
141 x64 = 0;
142 PREFETCH(&z64);
143 if(runtime_cas64(&z64, x64, 1))
144 runtime_throw("cas64 failed");
145 if(x64 != 0)
146 runtime_throw("cas64 failed");
147 x64 = 42;
148 if(!runtime_cas64(&z64, x64, 1))
149 runtime_throw("cas64 failed");
150 if(x64 != 42 || z64 != 1)
151 runtime_throw("cas64 failed");
152 if(runtime_atomicload64(&z64) != 1)
153 runtime_throw("load64 failed");
154 runtime_atomicstore64(&z64, (1ull<<40)+1);
155 if(runtime_atomicload64(&z64) != (1ull<<40)+1)
156 runtime_throw("store64 failed");
157 if(runtime_xadd64(&z64, (1ull<<40)+1) != (2ull<<40)+2)
158 runtime_throw("xadd64 failed");
159 if(runtime_atomicload64(&z64) != (2ull<<40)+2)
160 runtime_throw("xadd64 failed");
161 if(runtime_xchg64(&z64, (3ull<<40)+3) != (2ull<<40)+2)
162 runtime_throw("xchg64 failed");
163 if(runtime_atomicload64(&z64) != (3ull<<40)+3)
164 runtime_throw("xchg64 failed");
167 void
168 runtime_check(void)
170 __go_register_gc_roots(&runtime_roots);
172 TestAtomic64();
175 uint32
176 runtime_fastrand1(void)
178 M *m;
179 uint32 x;
181 m = runtime_m();
182 x = m->fastrand;
183 x += x;
184 if(x & 0x80000000L)
185 x ^= 0x88888eefUL;
186 m->fastrand = x;
187 return x;
190 int64
191 runtime_cputicks(void)
193 #if defined(__386__) || defined(__x86_64__)
194 uint32 low, high;
195 asm("rdtsc" : "=a" (low), "=d" (high));
196 return (int64)(((uint64)high << 32) | (uint64)low);
197 #else
198 // FIXME: implement for other processors.
199 return 0;
200 #endif
203 bool
204 runtime_showframe(String s, bool current)
206 static int32 traceback = -1;
208 if(current && runtime_m()->throwing > 0)
209 return 1;
210 if(traceback < 0)
211 traceback = runtime_gotraceback(nil);
212 return traceback > 1 || (__builtin_memchr(s.str, '.', s.len) != nil && __builtin_memcmp(s.str, "runtime.", 7) != 0);
215 static Lock ticksLock;
216 static int64 ticks;
218 int64
219 runtime_tickspersecond(void)
221 int64 res, t0, t1, c0, c1;
223 res = (int64)runtime_atomicload64((uint64*)&ticks);
224 if(res != 0)
225 return ticks;
226 runtime_lock(&ticksLock);
227 res = ticks;
228 if(res == 0) {
229 t0 = runtime_nanotime();
230 c0 = runtime_cputicks();
231 runtime_usleep(100*1000);
232 t1 = runtime_nanotime();
233 c1 = runtime_cputicks();
234 if(t1 == t0)
235 t1++;
236 res = (c1-c0)*1000*1000*1000/(t1-t0);
237 if(res == 0)
238 res++;
239 runtime_atomicstore64((uint64*)&ticks, res);
241 runtime_unlock(&ticksLock);
242 return res;
245 // Called to initialize a new m (including the bootstrap m).
246 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
247 void
248 runtime_mpreinit(M *mp)
250 mp->gsignal = runtime_malg(32*1024, &mp->gsignalstack, &mp->gsignalstacksize); // OS X wants >=8K, Linux >=2K
253 // Called to initialize a new m (including the bootstrap m).
254 // Called on the new thread, can not allocate memory.
255 void
256 runtime_minit(void)
258 M* m;
259 sigset_t sigs;
261 // Initialize signal handling.
262 m = runtime_m();
263 runtime_signalstack(m->gsignalstack, m->gsignalstacksize);
264 if (sigemptyset(&sigs) != 0)
265 runtime_throw("sigemptyset");
266 pthread_sigmask(SIG_SETMASK, &sigs, nil);
269 // Called from dropm to undo the effect of an minit.
270 void
271 runtime_unminit(void)
273 runtime_signalstack(nil, 0);
277 void
278 runtime_signalstack(byte *p, int32 n)
280 stack_t st;
282 st.ss_sp = p;
283 st.ss_size = n;
284 st.ss_flags = 0;
285 if(p == nil)
286 st.ss_flags = SS_DISABLE;
287 if(sigaltstack(&st, nil) < 0)
288 *(int *)0xf1 = 0xf1;
291 DebugVars runtime_debug;
293 static struct {
294 const char* name;
295 int32* value;
296 } dbgvar[] = {
297 {"allocfreetrace", &runtime_debug.allocfreetrace},
298 {"efence", &runtime_debug.efence},
299 {"gctrace", &runtime_debug.gctrace},
300 {"gcdead", &runtime_debug.gcdead},
301 {"scheddetail", &runtime_debug.scheddetail},
302 {"schedtrace", &runtime_debug.schedtrace},
305 void
306 runtime_parsedebugvars(void)
308 const byte *p;
309 intgo i, n;
310 bool tmp;
312 // gotraceback caches the GOTRACEBACK setting in traceback_cache.
313 // gotraceback can be called before the environment is available.
314 // traceback_cache must be reset after the environment is made
315 // available, in order for the environment variable to take effect.
316 // The code is fixed differently in Go 1.4.
317 // This is a limited fix for Go 1.3.3.
318 traceback_cache = ~(uint32)0;
319 runtime_gotraceback(&tmp);
321 p = runtime_getenv("GODEBUG");
322 if(p == nil)
323 return;
324 for(;;) {
325 for(i=0; i<(intgo)nelem(dbgvar); i++) {
326 n = runtime_findnull((const byte*)dbgvar[i].name);
327 if(runtime_mcmp(p, dbgvar[i].name, n) == 0 && p[n] == '=')
328 *dbgvar[i].value = runtime_atoi(p+n+1);
330 p = (const byte *)runtime_strstr((const char *)p, ",");
331 if(p == nil)
332 break;
333 p++;
337 // Poor mans 64-bit division.
338 // This is a very special function, do not use it if you are not sure what you are doing.
339 // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
340 // Handles overflow in a time-specific manner.
341 int32
342 runtime_timediv(int64 v, int32 div, int32 *rem)
344 int32 res, bit;
346 if(v >= (int64)div*0x7fffffffLL) {
347 if(rem != nil)
348 *rem = 0;
349 return 0x7fffffff;
351 res = 0;
352 for(bit = 30; bit >= 0; bit--) {
353 if(v >= ((int64)div<<bit)) {
354 v = v - ((int64)div<<bit);
355 res += 1<<bit;
358 if(rem != nil)
359 *rem = v;
360 return res;
363 // Setting the max stack size doesn't really do anything for gccgo.
365 uintptr runtime_maxstacksize = 1<<20; // enough until runtime.main sets it for real
367 void memclrBytes(Slice)
368 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
370 void
371 memclrBytes(Slice s)
373 runtime_memclr(s.__values, s.__count);