* gcc-interface/decl.c (promote_object_alignment): New function taken
[official-gcc.git] / libgo / runtime / runtime_c.c
blob6da35210440501849265ac74b92cc9dd9564a3f8
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 int32
20 runtime_atoi(const byte *p, intgo len)
22 int32 n;
24 n = 0;
25 while(len > 0 && '0' <= *p && *p <= '9') {
26 n = n*10 + *p++ - '0';
27 len--;
29 return n;
32 uint32
33 runtime_fastrand(void)
35 M *m;
36 uint32 x;
38 m = runtime_m();
39 x = m->fastrand;
40 x += x;
41 if(x & 0x80000000L)
42 x ^= 0x88888eefUL;
43 m->fastrand = x;
44 return x;
47 int64
48 runtime_cputicks(void)
50 #if defined(__386__) || defined(__x86_64__)
51 uint32 low, high;
52 asm("rdtsc" : "=a" (low), "=d" (high));
53 return (int64)(((uint64)high << 32) | (uint64)low);
54 #elif defined (__s390__) || defined (__s390x__)
55 uint64 clock = 0;
56 /* stckf may not write the return variable in case of a clock error, so make
57 it read-write to prevent that the initialisation is optimised out.
58 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
59 we don't support Go for machines older than that. */
60 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
61 return (int64)clock;
62 #else
63 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand().
64 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
65 // TODO: need more entropy to better seed fastrand.
66 return runtime_nanotime();
67 #endif
70 void
71 runtime_signalstack(byte *p, uintptr n)
73 stack_t st;
75 st.ss_sp = p;
76 st.ss_size = n;
77 st.ss_flags = 0;
78 if(p == nil)
79 st.ss_flags = SS_DISABLE;
80 if(sigaltstack(&st, nil) < 0)
81 *(int *)0xf1 = 0xf1;
84 struct debugVars runtime_debug;
86 void
87 runtime_setdebug(struct debugVars* d) {
88 runtime_debug = *d;
91 int32 go_open(char *, int32, int32)
92 __asm__ (GOSYM_PREFIX "runtime.open");
94 int32
95 go_open(char *name, int32 mode, int32 perm)
97 return runtime_open(name, mode, perm);
100 int32 go_read(int32, void *, int32)
101 __asm__ (GOSYM_PREFIX "runtime.read");
103 int32
104 go_read(int32 fd, void *p, int32 n)
106 return runtime_read(fd, p, n);
109 int32 go_write(uintptr, void *, int32)
110 __asm__ (GOSYM_PREFIX "runtime.write");
112 int32
113 go_write(uintptr fd, void *p, int32 n)
115 return runtime_write(fd, p, n);
118 int32 go_closefd(int32)
119 __asm__ (GOSYM_PREFIX "runtime.closefd");
121 int32
122 go_closefd(int32 fd)
124 return runtime_close(fd);
127 intgo go_errno(void)
128 __asm__ (GOSYM_PREFIX "runtime.errno");
130 intgo
131 go_errno()
133 return (intgo)errno;
136 uintptr getEnd(void)
137 __asm__ (GOSYM_PREFIX "runtime.getEnd");
139 uintptr
140 getEnd()
142 #ifdef _AIX
143 // mmap adresses range start at 0x30000000 on AIX for 32 bits processes
144 uintptr end = 0x30000000U;
145 #else
146 uintptr end = 0;
147 uintptr *pend;
149 pend = &__go_end;
150 if (pend != nil) {
151 end = *pend;
153 #endif
155 return end;
158 // CPU-specific initialization.
159 // Fetch CPUID info on x86.
161 void
162 runtime_cpuinit()
164 #if defined(__i386__) || defined(__x86_64__)
165 unsigned int eax, ebx, ecx, edx;
167 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
168 setCpuidECX(ecx);
171 #if defined(HAVE_AS_X86_AES)
172 setSupportAES(true);
173 #endif
174 #endif
177 // A publication barrier: a store/store barrier.
179 void publicationBarrier(void)
180 __asm__ (GOSYM_PREFIX "runtime.publicationBarrier");
182 void
183 publicationBarrier()
185 __atomic_thread_fence(__ATOMIC_RELEASE);