runtime: fix context used by getTraceback
[gcc.git] / libgo / runtime / runtime_c.c
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.
4
5 #include <errno.h>
6 #include <signal.h>
7 #include <unistd.h>
8
9 #if defined(__i386__) || defined(__x86_64__)
10 #include <cpuid.h>
11 #endif
12
13 #include "config.h"
14
15 #include "runtime.h"
16 #include "arch.h"
17 #include "array.h"
18
19 enum {
20 maxround = sizeof(uintptr),
21 };
22
23 extern volatile intgo runtime_MemProfileRate
24 __asm__ (GOSYM_PREFIX "runtime.MemProfileRate");
25
26 struct gotraceback_ret {
27 int32 level;
28 bool all;
29 bool crash;
30 };
31
32 extern struct gotraceback_ret gotraceback(void)
33 __asm__ (GOSYM_PREFIX "runtime.gotraceback");
34
35 // runtime_gotraceback is the C interface to runtime.gotraceback.
36 int32
37 runtime_gotraceback(bool *crash)
38 {
39 struct gotraceback_ret r;
40
41 r = gotraceback();
42 if(crash != nil)
43 *crash = r.crash;
44 return r.level;
45 }
46
47 int32
48 runtime_atoi(const byte *p, intgo len)
49 {
50 int32 n;
51
52 n = 0;
53 while(len > 0 && '0' <= *p && *p <= '9') {
54 n = n*10 + *p++ - '0';
55 len--;
56 }
57 return n;
58 }
59
60 uint32
61 runtime_fastrand(void)
62 {
63 M *m;
64 uint32 x;
65
66 m = runtime_m();
67 x = m->fastrand;
68 x += x;
69 if(x & 0x80000000L)
70 x ^= 0x88888eefUL;
71 m->fastrand = x;
72 return x;
73 }
74
75 int64
76 runtime_cputicks(void)
77 {
78 #if defined(__386__) || defined(__x86_64__)
79 uint32 low, high;
80 asm("rdtsc" : "=a" (low), "=d" (high));
81 return (int64)(((uint64)high << 32) | (uint64)low);
82 #elif defined (__s390__) || defined (__s390x__)
83 uint64 clock = 0;
84 /* stckf may not write the return variable in case of a clock error, so make
85 it read-write to prevent that the initialisation is optimised out.
86 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
87 we don't support Go for machines older than that. */
88 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
89 return (int64)clock;
90 #else
91 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
92 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
93 // TODO: need more entropy to better seed fastrand.
94 return runtime_nanotime();
95 #endif
96 }
97
98 void
99 runtime_signalstack(byte *p, uintptr n)
100 {
101 stack_t st;
102
103 st.ss_sp = p;
104 st.ss_size = n;
105 st.ss_flags = 0;
106 if(p == nil)
107 st.ss_flags = SS_DISABLE;
108 if(sigaltstack(&st, nil) < 0)
109 *(int *)0xf1 = 0xf1;
110 }
111
112 struct debugVars runtime_debug;
113
114 void
115 runtime_setdebug(struct debugVars* d) {
116 runtime_debug = *d;
117 }
118
119 void memclrBytes(Slice)
120 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
121
122 void
123 memclrBytes(Slice s)
124 {
125 runtime_memclr(s.__values, s.__count);
126 }
127
128 int32 go_open(char *, int32, int32)
129 __asm__ (GOSYM_PREFIX "runtime.open");
130
131 int32
132 go_open(char *name, int32 mode, int32 perm)
133 {
134 return runtime_open(name, mode, perm);
135 }
136
137 int32 go_read(int32, void *, int32)
138 __asm__ (GOSYM_PREFIX "runtime.read");
139
140 int32
141 go_read(int32 fd, void *p, int32 n)
142 {
143 return runtime_read(fd, p, n);
144 }
145
146 int32 go_write(uintptr, void *, int32)
147 __asm__ (GOSYM_PREFIX "runtime.write");
148
149 int32
150 go_write(uintptr fd, void *p, int32 n)
151 {
152 return runtime_write(fd, p, n);
153 }
154
155 int32 go_closefd(int32)
156 __asm__ (GOSYM_PREFIX "runtime.closefd");
157
158 int32
159 go_closefd(int32 fd)
160 {
161 return runtime_close(fd);
162 }
163
164 intgo go_errno(void)
165 __asm__ (GOSYM_PREFIX "runtime.errno");
166
167 intgo
168 go_errno()
169 {
170 return (intgo)errno;
171 }
172
173 // CPU-specific initialization.
174 // Fetch CPUID info on x86.
175
176 void
177 runtime_cpuinit()
178 {
179 #if defined(__i386__) || defined(__x86_64__)
180 unsigned int eax, ebx, ecx, edx;
181
182 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
183 setCpuidECX(ecx);
184 }
185
186 #if defined(HAVE_AS_X86_AES)
187 setSupportAES(true);
188 #endif
189 #endif
190 }