re PR fortran/52564 (Accepts invalid: Missing I/O list after comma)
[official-gcc.git] / libgo / runtime / runtime_c.c
blob6d77af4b067f44249c7ae216ffdc98b0d8a5d0ce
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 #ifdef __linux__
14 #include <syscall.h>
15 #endif
17 #include "config.h"
19 #include "runtime.h"
20 #include "arch.h"
21 #include "array.h"
23 int32
24 runtime_atoi(const byte *p, intgo len)
26 int32 n;
28 n = 0;
29 while(len > 0 && '0' <= *p && *p <= '9') {
30 n = n*10 + *p++ - '0';
31 len--;
33 return n;
36 // A random number from the GNU/Linux auxv array.
37 static uint32 randomNumber;
39 // Set the random number from Go code.
41 void
42 setRandomNumber(uint32 r)
44 randomNumber = r;
47 #if defined(__i386__) || defined(__x86_64__) || defined (__s390__) || defined (__s390x__)
49 // When cputicks is just asm instructions, skip the split stack
50 // prologue for speed.
52 int64 runtime_cputicks(void) __attribute__((no_split_stack));
54 #endif
56 // Whether the processor supports SSE2.
57 #if defined (__i386__)
58 static _Bool hasSSE2;
60 // Force appropriate CPU level so that we can call the lfence/mfence
61 // builtins.
63 #pragma GCC push_options
64 #pragma GCC target("sse2")
66 #elif defined(__x86_64__)
67 #define hasSSE2 true
68 #endif
70 #if defined(__i386__) || defined(__x86_64__)
71 // Whether to use lfence, as opposed to mfence.
72 // Set based on cpuid.
73 static _Bool lfenceBeforeRdtsc;
74 #endif // defined(__i386__) || defined(__x86_64__)
76 int64
77 runtime_cputicks(void)
79 #if defined(__i386__) || defined(__x86_64__)
80 if (hasSSE2) {
81 if (lfenceBeforeRdtsc) {
82 __builtin_ia32_lfence();
83 } else {
84 __builtin_ia32_mfence();
87 return __builtin_ia32_rdtsc();
88 #elif defined (__s390__) || defined (__s390x__)
89 uint64 clock = 0;
90 /* stckf may not write the return variable in case of a clock error, so make
91 it read-write to prevent that the initialisation is optimised out.
92 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
93 we don't support Go for machines older than that. */
94 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
95 return (int64)clock;
96 #else
97 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
98 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
99 // randomNumber provides better seeding of fastrand.
100 return runtime_nanotime() + randomNumber;
101 #endif
104 #if defined(__i386__)
105 #pragma GCC pop_options
106 #endif
108 void
109 runtime_signalstack(byte *p, uintptr n)
111 stack_t st;
113 st.ss_sp = p;
114 st.ss_size = n;
115 st.ss_flags = 0;
116 if(p == nil)
117 st.ss_flags = SS_DISABLE;
118 if(sigaltstack(&st, nil) < 0)
119 *(int *)0xf1 = 0xf1;
122 int32 go_open(char *, int32, int32)
123 __asm__ (GOSYM_PREFIX "runtime.open");
125 int32
126 go_open(char *name, int32 mode, int32 perm)
128 return runtime_open(name, mode, perm);
131 int32 go_read(int32, void *, int32)
132 __asm__ (GOSYM_PREFIX "runtime.read");
134 int32
135 go_read(int32 fd, void *p, int32 n)
137 return runtime_read(fd, p, n);
140 int32 go_write(uintptr, void *, int32)
141 __asm__ (GOSYM_PREFIX "runtime.write");
143 int32
144 go_write(uintptr fd, void *p, int32 n)
146 return runtime_write(fd, p, n);
149 int32 go_closefd(int32)
150 __asm__ (GOSYM_PREFIX "runtime.closefd");
152 int32
153 go_closefd(int32 fd)
155 return runtime_close(fd);
158 intgo go_errno(void)
159 __asm__ (GOSYM_PREFIX "runtime.errno");
161 intgo
162 go_errno()
164 return (intgo)errno;
167 uintptr getEnd(void)
168 __asm__ (GOSYM_PREFIX "runtime.getEnd");
170 uintptr
171 getEnd()
173 #ifdef _AIX
174 // mmap adresses range start at 0x30000000 on AIX for 32 bits processes
175 uintptr end = 0x30000000U;
176 #else
177 uintptr end = 0;
178 uintptr *pend;
180 pend = &__go_end;
181 if (pend != nil) {
182 end = *pend;
184 #endif
186 return end;
189 // CPU-specific initialization.
190 // Fetch CPUID info on x86.
192 void
193 runtime_cpuinit()
195 #if defined(__i386__) || defined(__x86_64__)
196 unsigned int eax, ebx, ecx, edx;
198 if (__get_cpuid(0, &eax, &ebx, &ecx, &edx)) {
199 if (eax != 0
200 && ebx == 0x756E6547 // "Genu"
201 && edx == 0x49656E69 // "ineI"
202 && ecx == 0x6C65746E) { // "ntel"
203 lfenceBeforeRdtsc = true;
206 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
207 #if defined(__i386__)
208 if ((edx & bit_SSE2) != 0) {
209 hasSSE2 = true;
211 #endif
214 #if defined(HAVE_AS_X86_AES)
215 setSupportAES(true);
216 #endif
217 #endif
220 // A publication barrier: a store/store barrier.
222 void publicationBarrier(void)
223 __asm__ (GOSYM_PREFIX "runtime.publicationBarrier");
225 void
226 publicationBarrier()
228 __atomic_thread_fence(__ATOMIC_RELEASE);
231 #ifdef __linux__
233 /* Currently sbrk0 is only called on GNU/Linux. */
235 uintptr sbrk0(void)
236 __asm__ (GOSYM_PREFIX "runtime.sbrk0");
238 uintptr
239 sbrk0()
241 return syscall(SYS_brk, (uintptr)(0));
244 #endif /* __linux__ */