2018-01-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / runtime / runtime_c.c
blob88f1adfb91bdf22834b1a96f2f1c999de2ec962d
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 int64
37 runtime_cputicks(void)
39 #if defined(__386__) || defined(__x86_64__)
40 uint32 low, high;
41 asm("rdtsc" : "=a" (low), "=d" (high));
42 return (int64)(((uint64)high << 32) | (uint64)low);
43 #elif defined (__s390__) || defined (__s390x__)
44 uint64 clock = 0;
45 /* stckf may not write the return variable in case of a clock error, so make
46 it read-write to prevent that the initialisation is optimised out.
47 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
48 we don't support Go for machines older than that. */
49 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
50 return (int64)clock;
51 #else
52 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
53 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
54 // TODO: need more entropy to better seed fastrand.
55 return runtime_nanotime();
56 #endif
59 void
60 runtime_signalstack(byte *p, uintptr n)
62 stack_t st;
64 st.ss_sp = p;
65 st.ss_size = n;
66 st.ss_flags = 0;
67 if(p == nil)
68 st.ss_flags = SS_DISABLE;
69 if(sigaltstack(&st, nil) < 0)
70 *(int *)0xf1 = 0xf1;
73 int32 go_open(char *, int32, int32)
74 __asm__ (GOSYM_PREFIX "runtime.open");
76 int32
77 go_open(char *name, int32 mode, int32 perm)
79 return runtime_open(name, mode, perm);
82 int32 go_read(int32, void *, int32)
83 __asm__ (GOSYM_PREFIX "runtime.read");
85 int32
86 go_read(int32 fd, void *p, int32 n)
88 return runtime_read(fd, p, n);
91 int32 go_write(uintptr, void *, int32)
92 __asm__ (GOSYM_PREFIX "runtime.write");
94 int32
95 go_write(uintptr fd, void *p, int32 n)
97 return runtime_write(fd, p, n);
100 int32 go_closefd(int32)
101 __asm__ (GOSYM_PREFIX "runtime.closefd");
103 int32
104 go_closefd(int32 fd)
106 return runtime_close(fd);
109 intgo go_errno(void)
110 __asm__ (GOSYM_PREFIX "runtime.errno");
112 intgo
113 go_errno()
115 return (intgo)errno;
118 uintptr getEnd(void)
119 __asm__ (GOSYM_PREFIX "runtime.getEnd");
121 uintptr
122 getEnd()
124 #ifdef _AIX
125 // mmap adresses range start at 0x30000000 on AIX for 32 bits processes
126 uintptr end = 0x30000000U;
127 #else
128 uintptr end = 0;
129 uintptr *pend;
131 pend = &__go_end;
132 if (pend != nil) {
133 end = *pend;
135 #endif
137 return end;
140 // CPU-specific initialization.
141 // Fetch CPUID info on x86.
143 void
144 runtime_cpuinit()
146 #if defined(__i386__) || defined(__x86_64__)
147 unsigned int eax, ebx, ecx, edx;
149 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
150 setCpuidECX(ecx);
153 #if defined(HAVE_AS_X86_AES)
154 setSupportAES(true);
155 #endif
156 #endif
159 // A publication barrier: a store/store barrier.
161 void publicationBarrier(void)
162 __asm__ (GOSYM_PREFIX "runtime.publicationBarrier");
164 void
165 publicationBarrier()
167 __atomic_thread_fence(__ATOMIC_RELEASE);
170 #ifdef __linux__
172 /* Currently sbrk0 is only called on GNU/Linux. */
174 uintptr sbrk0(void)
175 __asm__ (GOSYM_PREFIX "runtime.sbrk0");
177 uintptr
178 sbrk0()
180 return syscall(SYS_brk, (uintptr)(0));
183 #endif /* __linux__ */