2016-10-07 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / runtime.c
blob8e6f1f5052468bd5eb54a74249d49f9d8128157e
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 <errno.h>
6 #include <signal.h>
7 #include <unistd.h>
9 #include "config.h"
11 #include "runtime.h"
12 #include "arch.h"
13 #include "array.h"
15 enum {
16 maxround = sizeof(uintptr),
19 extern volatile intgo runtime_MemProfileRate
20 __asm__ (GOSYM_PREFIX "runtime.MemProfileRate");
22 struct gotraceback_ret {
23 int32 level;
24 bool crash;
27 extern struct gotraceback_ret gotraceback(void)
28 __asm__ (GOSYM_PREFIX "runtime.gotraceback");
30 // runtime_gotraceback is the C interface to runtime.gotraceback.
31 int32
32 runtime_gotraceback(bool *crash)
34 struct gotraceback_ret r;
36 r = gotraceback();
37 if(crash != nil)
38 *crash = r.crash;
39 return r.level;
42 int32
43 runtime_atoi(const byte *p, intgo len)
45 int32 n;
47 n = 0;
48 while(len > 0 && '0' <= *p && *p <= '9') {
49 n = n*10 + *p++ - '0';
50 len--;
52 return n;
55 uint32
56 runtime_fastrand1(void)
58 M *m;
59 uint32 x;
61 m = runtime_m();
62 x = m->fastrand;
63 x += x;
64 if(x & 0x80000000L)
65 x ^= 0x88888eefUL;
66 m->fastrand = x;
67 return x;
70 int64
71 runtime_cputicks(void)
73 #if defined(__386__) || defined(__x86_64__)
74 uint32 low, high;
75 asm("rdtsc" : "=a" (low), "=d" (high));
76 return (int64)(((uint64)high << 32) | (uint64)low);
77 #elif defined (__s390__) || defined (__s390x__)
78 uint64 clock = 0;
79 /* stckf may not write the return variable in case of a clock error, so make
80 it read-write to prevent that the initialisation is optimised out.
81 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
82 we don't support Go for machines older than that. */
83 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
84 return (int64)clock;
85 #else
86 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand1().
87 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
88 // TODO: need more entropy to better seed fastrand1.
89 return runtime_nanotime();
90 #endif
93 bool
94 runtime_showframe(String s, bool current)
96 static int32 traceback = -1;
98 if(current && runtime_m()->throwing > 0)
99 return 1;
100 if(traceback < 0)
101 traceback = runtime_gotraceback(nil);
102 return traceback > 1 || (__builtin_memchr(s.str, '.', s.len) != nil && __builtin_memcmp(s.str, "runtime.", 7) != 0);
105 // Called to initialize a new m (including the bootstrap m).
106 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
107 void
108 runtime_mpreinit(M *mp)
110 int32 stacksize = 32 * 1024; // OS X wants >=8K, Linux >=2K
112 #ifdef SIGSTKSZ
113 if(stacksize < SIGSTKSZ)
114 stacksize = SIGSTKSZ;
115 #endif
117 mp->gsignal = runtime_malg(stacksize, (byte**)&mp->gsignalstack, &mp->gsignalstacksize);
118 mp->gsignal->m = mp;
121 // Called to initialize a new m (including the bootstrap m).
122 // Called on the new thread, can not allocate memory.
123 void
124 runtime_minit(void)
126 M* m;
127 sigset_t sigs;
129 // Initialize signal handling.
130 m = runtime_m();
131 runtime_signalstack(m->gsignalstack, m->gsignalstacksize);
132 if (sigemptyset(&sigs) != 0)
133 runtime_throw("sigemptyset");
134 pthread_sigmask(SIG_SETMASK, &sigs, nil);
137 // Called from dropm to undo the effect of an minit.
138 void
139 runtime_unminit(void)
141 runtime_signalstack(nil, 0);
145 void
146 runtime_signalstack(byte *p, int32 n)
148 stack_t st;
150 st.ss_sp = p;
151 st.ss_size = n;
152 st.ss_flags = 0;
153 if(p == nil)
154 st.ss_flags = SS_DISABLE;
155 if(sigaltstack(&st, nil) < 0)
156 *(int *)0xf1 = 0xf1;
159 struct debugVars runtime_debug;
161 void
162 runtime_setdebug(struct debugVars* d) {
163 runtime_debug = *d;
166 // Setting the max stack size doesn't really do anything for gccgo.
168 uintptr runtime_maxstacksize = 1<<20; // enough until runtime.main sets it for real
170 void memclrBytes(Slice)
171 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
173 void
174 memclrBytes(Slice s)
176 runtime_memclr(s.__values, s.__count);
179 int32 go_open(char *, int32, int32)
180 __asm__ (GOSYM_PREFIX "runtime.open");
182 int32
183 go_open(char *name, int32 mode, int32 perm)
185 return runtime_open(name, mode, perm);
188 int32 go_read(int32, void *, int32)
189 __asm__ (GOSYM_PREFIX "runtime.read");
191 int32
192 go_read(int32 fd, void *p, int32 n)
194 return runtime_read(fd, p, n);
197 int32 go_write(uintptr, void *, int32)
198 __asm__ (GOSYM_PREFIX "runtime.write");
200 int32
201 go_write(uintptr fd, void *p, int32 n)
203 return runtime_write(fd, p, n);
206 int32 go_closefd(int32)
207 __asm__ (GOSYM_PREFIX "runtime.closefd");
209 int32
210 go_closefd(int32 fd)
212 return runtime_close(fd);
215 intgo go_errno(void)
216 __asm__ (GOSYM_PREFIX "runtime.errno");
218 intgo
219 go_errno()
221 return (intgo)errno;