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.
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
23 static uint32 traceback_cache
= ~(uint32
)0;
25 extern volatile intgo runtime_MemProfileRate
26 __asm__ (GOSYM_PREFIX
"runtime.MemProfileRate");
29 // The GOTRACEBACK environment variable controls the
30 // behavior of a Go program that is crashing and exiting.
31 // GOTRACEBACK=0 suppress all tracebacks
32 // GOTRACEBACK=1 default behavior - show tracebacks but exclude runtime frames
33 // GOTRACEBACK=2 show tracebacks including runtime frames
34 // GOTRACEBACK=crash show tracebacks including runtime frames, then crash (core dump etc)
36 runtime_gotraceback(bool *crash
)
43 if(runtime_m()->traceback
!= 0)
44 return runtime_m()->traceback
;
45 x
= runtime_atomicload(&traceback_cache
);
47 p
= runtime_getenv("GOTRACEBACK");
52 else if(runtime_strcmp((const char *)p
, "crash") == 0)
55 x
= runtime_atoi(p
)<<1;
56 runtime_atomicstore(&traceback_cache
, x
);
69 void (*runtime_sysargs
)(int32
, uint8
**);
72 runtime_args(int32 c
, byte
**v
)
76 if(runtime_sysargs
!= nil
)
77 runtime_sysargs(c
, v
);
83 return argc
== 0 ? nil
: argv
[0];
92 // for windows implementation see "os" package
96 s
= runtime_malloc(argc
*sizeof s
[0]);
98 s
[i
] = runtime_gostringnocopy((const byte
*)argv
[i
]);
99 args
.__values
= (void*)s
;
101 args
.__capacity
= argc
;
105 runtime_goenvs_unix(void)
110 for(n
=0; argv
[argc
+1+n
] != 0; n
++)
113 s
= runtime_malloc(n
*sizeof s
[0]);
115 s
[i
] = runtime_gostringnocopy(argv
[argc
+1+i
]);
116 envs
.__values
= (void*)s
;
121 // Called from the syscall package.
122 Slice
runtime_envs(void) __asm__ (GOSYM_PREFIX
"syscall.runtime_envs");
130 Slice
os_runtime_args(void) __asm__ (GOSYM_PREFIX
"os.runtime_args");
139 runtime_atoi(const byte
*p
)
144 while('0' <= *p
&& *p
<= '9')
145 n
= n
*10 + *p
++ - '0';
149 static struct root_list runtime_roots
=
151 { { &envs
, sizeof envs
},
152 { &args
, sizeof args
},
164 if(runtime_cas64(&z64
, x64
, 1))
165 runtime_throw("cas64 failed");
167 runtime_throw("cas64 failed");
169 if(!runtime_cas64(&z64
, x64
, 1))
170 runtime_throw("cas64 failed");
171 if(x64
!= 42 || z64
!= 1)
172 runtime_throw("cas64 failed");
173 if(runtime_atomicload64(&z64
) != 1)
174 runtime_throw("load64 failed");
175 runtime_atomicstore64(&z64
, (1ull<<40)+1);
176 if(runtime_atomicload64(&z64
) != (1ull<<40)+1)
177 runtime_throw("store64 failed");
178 if(runtime_xadd64(&z64
, (1ull<<40)+1) != (2ull<<40)+2)
179 runtime_throw("xadd64 failed");
180 if(runtime_atomicload64(&z64
) != (2ull<<40)+2)
181 runtime_throw("xadd64 failed");
182 if(runtime_xchg64(&z64
, (3ull<<40)+3) != (2ull<<40)+2)
183 runtime_throw("xchg64 failed");
184 if(runtime_atomicload64(&z64
) != (3ull<<40)+3)
185 runtime_throw("xchg64 failed");
191 __go_register_gc_roots(&runtime_roots
);
197 runtime_fastrand1(void)
212 runtime_cputicks(void)
214 #if defined(__386__) || defined(__x86_64__)
216 asm("rdtsc" : "=a" (low
), "=d" (high
));
217 return (int64
)(((uint64
)high
<< 32) | (uint64
)low
);
218 #elif defined (__s390__) || defined (__s390x__)
220 /* stckf may not write the return variable in case of a clock error, so make
221 it read-write to prevent that the initialisation is optimised out.
222 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
223 we don't support Go for machines older than that. */
224 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock
) : : "cc" );
227 // FIXME: implement for other processors.
233 runtime_showframe(String s
, bool current
)
235 static int32 traceback
= -1;
237 if(current
&& runtime_m()->throwing
> 0)
240 traceback
= runtime_gotraceback(nil
);
241 return traceback
> 1 || (__builtin_memchr(s
.str
, '.', s
.len
) != nil
&& __builtin_memcmp(s
.str
, "runtime.", 7) != 0);
244 static Lock ticksLock
;
248 runtime_tickspersecond(void)
250 int64 res
, t0
, t1
, c0
, c1
;
252 res
= (int64
)runtime_atomicload64((uint64
*)&ticks
);
255 runtime_lock(&ticksLock
);
258 t0
= runtime_nanotime();
259 c0
= runtime_cputicks();
260 runtime_usleep(100*1000);
261 t1
= runtime_nanotime();
262 c1
= runtime_cputicks();
265 res
= (c1
-c0
)*1000*1000*1000/(t1
-t0
);
268 runtime_atomicstore64((uint64
*)&ticks
, res
);
270 runtime_unlock(&ticksLock
);
274 // Called to initialize a new m (including the bootstrap m).
275 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
277 runtime_mpreinit(M
*mp
)
279 mp
->gsignal
= runtime_malg(32*1024, &mp
->gsignalstack
, &mp
->gsignalstacksize
); // OS X wants >=8K, Linux >=2K
282 // Called to initialize a new m (including the bootstrap m).
283 // Called on the new thread, can not allocate memory.
290 // Initialize signal handling.
292 runtime_signalstack(m
->gsignalstack
, m
->gsignalstacksize
);
293 if (sigemptyset(&sigs
) != 0)
294 runtime_throw("sigemptyset");
295 pthread_sigmask(SIG_SETMASK
, &sigs
, nil
);
298 // Called from dropm to undo the effect of an minit.
300 runtime_unminit(void)
302 runtime_signalstack(nil
, 0);
307 runtime_signalstack(byte
*p
, int32 n
)
315 st
.ss_flags
= SS_DISABLE
;
316 if(sigaltstack(&st
, nil
) < 0)
320 DebugVars runtime_debug
;
322 // Holds variables parsed from GODEBUG env var,
323 // except for "memprofilerate" since there is an
324 // existing var for that value which is int
325 // instead of in32 and might have an
331 {"allocfreetrace", &runtime_debug
.allocfreetrace
},
332 {"efence", &runtime_debug
.efence
},
333 {"gctrace", &runtime_debug
.gctrace
},
334 {"gcdead", &runtime_debug
.gcdead
},
335 {"scheddetail", &runtime_debug
.scheddetail
},
336 {"schedtrace", &runtime_debug
.schedtrace
},
340 runtime_parsedebugvars(void)
346 // gotraceback caches the GOTRACEBACK setting in traceback_cache.
347 // gotraceback can be called before the environment is available.
348 // traceback_cache must be reset after the environment is made
349 // available, in order for the environment variable to take effect.
350 // The code is fixed differently in Go 1.4.
351 // This is a limited fix for Go 1.3.3.
352 traceback_cache
= ~(uint32
)0;
353 runtime_gotraceback(&tmp
);
355 p
= runtime_getenv("GODEBUG");
359 for(i
=0; i
<(intgo
)nelem(dbgvar
); i
++) {
360 n
= runtime_findnull((const byte
*)dbgvar
[i
].name
);
361 if(runtime_mcmp(p
, "memprofilerate", n
) == 0 && p
[n
] == '=')
362 // Set the MemProfileRate directly since it
363 // is an int, not int32, and should only lbe
364 // set here if specified by GODEBUG
365 runtime_MemProfileRate
= runtime_atoi(p
+n
+1);
366 else if(runtime_mcmp(p
, dbgvar
[i
].name
, n
) == 0 && p
[n
] == '=')
367 *dbgvar
[i
].value
= runtime_atoi(p
+n
+1);
369 p
= (const byte
*)runtime_strstr((const char *)p
, ",");
376 // Poor mans 64-bit division.
377 // This is a very special function, do not use it if you are not sure what you are doing.
378 // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
379 // Handles overflow in a time-specific manner.
381 runtime_timediv(int64 v
, int32 div
, int32
*rem
)
385 if(v
>= (int64
)div
*0x7fffffffLL
) {
391 for(bit
= 30; bit
>= 0; bit
--) {
392 if(v
>= ((int64
)div
<<bit
)) {
393 v
= v
- ((int64
)div
<<bit
);
402 // Setting the max stack size doesn't really do anything for gccgo.
404 uintptr runtime_maxstacksize
= 1<<20; // enough until runtime.main sets it for real
406 void memclrBytes(Slice
)
407 __asm__ (GOSYM_PREFIX
"runtime.memclrBytes");
412 runtime_memclr(s
.__values
, s
.__count
);