Daily bump.
[official-gcc.git] / libgo / runtime / runtime_c.c
blob9883e0a6c9191c2fd1fa54e7704dcc0d2fa31e60
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 #if defined(__i386__) || defined(__x86_64__)
10 #include <cpuid.h>
11 #endif
13 #include "config.h"
15 #include "runtime.h"
16 #include "arch.h"
17 #include "array.h"
19 enum {
20 maxround = sizeof(uintptr),
23 extern volatile intgo runtime_MemProfileRate
24 __asm__ (GOSYM_PREFIX "runtime.MemProfileRate");
26 struct gotraceback_ret {
27 int32 level;
28 bool all;
29 bool crash;
32 extern struct gotraceback_ret gotraceback(void)
33 __asm__ (GOSYM_PREFIX "runtime.gotraceback");
35 // runtime_gotraceback is the C interface to runtime.gotraceback.
36 int32
37 runtime_gotraceback(bool *crash)
39 struct gotraceback_ret r;
41 r = gotraceback();
42 if(crash != nil)
43 *crash = r.crash;
44 return r.level;
47 int32
48 runtime_atoi(const byte *p, intgo len)
50 int32 n;
52 n = 0;
53 while(len > 0 && '0' <= *p && *p <= '9') {
54 n = n*10 + *p++ - '0';
55 len--;
57 return n;
60 uint32
61 runtime_fastrand(void)
63 M *m;
64 uint32 x;
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;
75 int64
76 runtime_cputicks(void)
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
98 void
99 runtime_signalstack(byte *p, uintptr n)
101 stack_t st;
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;
112 struct debugVars runtime_debug;
114 void
115 runtime_setdebug(struct debugVars* d) {
116 runtime_debug = *d;
119 void memclrBytes(Slice)
120 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
122 void
123 memclrBytes(Slice s)
125 runtime_memclr(s.__values, s.__count);
128 int32 go_open(char *, int32, int32)
129 __asm__ (GOSYM_PREFIX "runtime.open");
131 int32
132 go_open(char *name, int32 mode, int32 perm)
134 return runtime_open(name, mode, perm);
137 int32 go_read(int32, void *, int32)
138 __asm__ (GOSYM_PREFIX "runtime.read");
140 int32
141 go_read(int32 fd, void *p, int32 n)
143 return runtime_read(fd, p, n);
146 int32 go_write(uintptr, void *, int32)
147 __asm__ (GOSYM_PREFIX "runtime.write");
149 int32
150 go_write(uintptr fd, void *p, int32 n)
152 return runtime_write(fd, p, n);
155 int32 go_closefd(int32)
156 __asm__ (GOSYM_PREFIX "runtime.closefd");
158 int32
159 go_closefd(int32 fd)
161 return runtime_close(fd);
164 intgo go_errno(void)
165 __asm__ (GOSYM_PREFIX "runtime.errno");
167 intgo
168 go_errno()
170 return (intgo)errno;
173 // CPU-specific initialization.
174 // Fetch CPUID info on x86.
176 void
177 runtime_cpuinit()
179 #if defined(__i386__) || defined(__x86_64__)
180 unsigned int eax, ebx, ecx, edx;
182 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
183 setCpuidECX(ecx);
186 #if defined(HAVE_AS_X86_AES)
187 setSupportAES(true);
188 #endif
189 #endif