runtime: don't stat a NULL filename
[official-gcc.git] / libgo / runtime / runtime_c.c
blobc65a7e0a950b8dae55b861607828f81e3d373117
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 #if defined(__i386__) || defined(__x86_64__) || defined (__s390__) || defined (__s390x__)
38 // When cputicks is just asm instructions, skip the split stack
39 // prologue for speed.
41 int64 runtime_cputicks(void) __attribute__((no_split_stack));
43 #endif
45 // Whether the processor supports SSE2.
46 #if defined (__i386__)
47 static _Bool hasSSE2;
49 // Force appropriate CPU level so that we can call the lfence/mfence
50 // builtins.
52 #pragma GCC push_options
53 #pragma GCC target("sse2")
55 #elif defined(__x86_64__)
56 #define hasSSE2 true
57 #endif
59 #if defined(__i386__) || defined(__x86_64__)
60 // Whether to use lfence, as opposed to mfence.
61 // Set based on cpuid.
62 static _Bool lfenceBeforeRdtsc;
63 #endif // defined(__i386__) || defined(__x86_64__)
65 int64
66 runtime_cputicks(void)
68 #if defined(__i386__) || defined(__x86_64__)
69 if (hasSSE2) {
70 if (lfenceBeforeRdtsc) {
71 __builtin_ia32_lfence();
72 } else {
73 __builtin_ia32_mfence();
76 return __builtin_ia32_rdtsc();
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·fastrand().
87 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
88 // TODO: need more entropy to better seed fastrand.
89 return runtime_nanotime();
90 #endif
93 #if defined(__i386__)
94 #pragma GCC pop_options
95 #endif
97 void
98 runtime_signalstack(byte *p, uintptr n)
100 stack_t st;
102 st.ss_sp = p;
103 st.ss_size = n;
104 st.ss_flags = 0;
105 if(p == nil)
106 st.ss_flags = SS_DISABLE;
107 if(sigaltstack(&st, nil) < 0)
108 *(int *)0xf1 = 0xf1;
111 int32 go_open(char *, int32, int32)
112 __asm__ (GOSYM_PREFIX "runtime.open");
114 int32
115 go_open(char *name, int32 mode, int32 perm)
117 return runtime_open(name, mode, perm);
120 int32 go_read(int32, void *, int32)
121 __asm__ (GOSYM_PREFIX "runtime.read");
123 int32
124 go_read(int32 fd, void *p, int32 n)
126 return runtime_read(fd, p, n);
129 int32 go_write(uintptr, void *, int32)
130 __asm__ (GOSYM_PREFIX "runtime.write");
132 int32
133 go_write(uintptr fd, void *p, int32 n)
135 return runtime_write(fd, p, n);
138 int32 go_closefd(int32)
139 __asm__ (GOSYM_PREFIX "runtime.closefd");
141 int32
142 go_closefd(int32 fd)
144 return runtime_close(fd);
147 intgo go_errno(void)
148 __asm__ (GOSYM_PREFIX "runtime.errno");
150 intgo
151 go_errno()
153 return (intgo)errno;
156 uintptr getEnd(void)
157 __asm__ (GOSYM_PREFIX "runtime.getEnd");
159 uintptr
160 getEnd()
162 #ifdef _AIX
163 // mmap adresses range start at 0x30000000 on AIX for 32 bits processes
164 uintptr end = 0x30000000U;
165 #else
166 uintptr end = 0;
167 uintptr *pend;
169 pend = &__go_end;
170 if (pend != nil) {
171 end = *pend;
173 #endif
175 return end;
178 // CPU-specific initialization.
179 // Fetch CPUID info on x86.
181 void
182 runtime_cpuinit()
184 #if defined(__i386__) || defined(__x86_64__)
185 unsigned int eax, ebx, ecx, edx;
187 if (__get_cpuid(0, &eax, &ebx, &ecx, &edx)) {
188 if (eax != 0
189 && ebx == 0x756E6547 // "Genu"
190 && edx == 0x49656E69 // "ineI"
191 && ecx == 0x6C65746E) { // "ntel"
192 lfenceBeforeRdtsc = true;
195 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
196 setCpuidECX(ecx);
197 #if defined(__i386__)
198 if ((edx & bit_SSE2) != 0) {
199 hasSSE2 = true;
201 #endif
204 #if defined(HAVE_AS_X86_AES)
205 setSupportAES(true);
206 #endif
207 #endif
210 // A publication barrier: a store/store barrier.
212 void publicationBarrier(void)
213 __asm__ (GOSYM_PREFIX "runtime.publicationBarrier");
215 void
216 publicationBarrier()
218 __atomic_thread_fence(__ATOMIC_RELEASE);
221 #ifdef __linux__
223 /* Currently sbrk0 is only called on GNU/Linux. */
225 uintptr sbrk0(void)
226 __asm__ (GOSYM_PREFIX "runtime.sbrk0");
228 uintptr
229 sbrk0()
231 return syscall(SYS_brk, (uintptr)(0));
234 #endif /* __linux__ */