[Patch Doc] Update documentation for __fp16 type
[official-gcc.git] / libgo / runtime / runtime_c.c
blob3387401b427c3419a814ce1ddf0d88704677e75f
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 crash;
31 extern struct gotraceback_ret gotraceback(void)
32 __asm__ (GOSYM_PREFIX "runtime.gotraceback");
34 // runtime_gotraceback is the C interface to runtime.gotraceback.
35 int32
36 runtime_gotraceback(bool *crash)
38 struct gotraceback_ret r;
40 r = gotraceback();
41 if(crash != nil)
42 *crash = r.crash;
43 return r.level;
46 int32
47 runtime_atoi(const byte *p, intgo len)
49 int32 n;
51 n = 0;
52 while(len > 0 && '0' <= *p && *p <= '9') {
53 n = n*10 + *p++ - '0';
54 len--;
56 return n;
59 uint32
60 runtime_fastrand1(void)
62 M *m;
63 uint32 x;
65 m = runtime_m();
66 x = m->fastrand;
67 x += x;
68 if(x & 0x80000000L)
69 x ^= 0x88888eefUL;
70 m->fastrand = x;
71 return x;
74 int64
75 runtime_cputicks(void)
77 #if defined(__386__) || defined(__x86_64__)
78 uint32 low, high;
79 asm("rdtsc" : "=a" (low), "=d" (high));
80 return (int64)(((uint64)high << 32) | (uint64)low);
81 #elif defined (__s390__) || defined (__s390x__)
82 uint64 clock = 0;
83 /* stckf may not write the return variable in case of a clock error, so make
84 it read-write to prevent that the initialisation is optimised out.
85 Note: Targets below z9-109 will crash when executing store clock fast, i.e.
86 we don't support Go for machines older than that. */
87 asm volatile(".insn s,0xb27c0000,%0" /* stckf */ : "+Q" (clock) : : "cc" );
88 return (int64)clock;
89 #else
90 // Currently cputicks() is used in blocking profiler and to seed runtime·fastrand1().
91 // runtime·nanotime() is a poor approximation of CPU ticks that is enough for the profiler.
92 // TODO: need more entropy to better seed fastrand1.
93 return runtime_nanotime();
94 #endif
97 // Called to initialize a new m (including the bootstrap m).
98 // Called on the parent thread (main thread in case of bootstrap), can allocate memory.
99 void
100 runtime_mpreinit(M *mp)
102 int32 stacksize = 32 * 1024; // OS X wants >=8K, Linux >=2K
104 #ifdef SIGSTKSZ
105 if(stacksize < SIGSTKSZ)
106 stacksize = SIGSTKSZ;
107 #endif
109 mp->gsignal = runtime_malg(stacksize, (byte**)&mp->gsignalstack, &mp->gsignalstacksize);
110 mp->gsignal->m = mp;
113 // Called to initialize a new m (including the bootstrap m).
114 // Called on the new thread, can not allocate memory.
115 void
116 runtime_minit(void)
118 M* m;
119 sigset_t sigs;
121 // Initialize signal handling.
122 m = runtime_m();
123 runtime_signalstack(m->gsignalstack, m->gsignalstacksize);
124 if (sigemptyset(&sigs) != 0)
125 runtime_throw("sigemptyset");
126 pthread_sigmask(SIG_SETMASK, &sigs, nil);
129 // Called from dropm to undo the effect of an minit.
130 void
131 runtime_unminit(void)
133 runtime_signalstack(nil, 0);
137 void
138 runtime_signalstack(byte *p, int32 n)
140 stack_t st;
142 st.ss_sp = p;
143 st.ss_size = n;
144 st.ss_flags = 0;
145 if(p == nil)
146 st.ss_flags = SS_DISABLE;
147 if(sigaltstack(&st, nil) < 0)
148 *(int *)0xf1 = 0xf1;
151 struct debugVars runtime_debug;
153 void
154 runtime_setdebug(struct debugVars* d) {
155 runtime_debug = *d;
158 void memclrBytes(Slice)
159 __asm__ (GOSYM_PREFIX "runtime.memclrBytes");
161 void
162 memclrBytes(Slice s)
164 runtime_memclr(s.__values, s.__count);
167 int32 go_open(char *, int32, int32)
168 __asm__ (GOSYM_PREFIX "runtime.open");
170 int32
171 go_open(char *name, int32 mode, int32 perm)
173 return runtime_open(name, mode, perm);
176 int32 go_read(int32, void *, int32)
177 __asm__ (GOSYM_PREFIX "runtime.read");
179 int32
180 go_read(int32 fd, void *p, int32 n)
182 return runtime_read(fd, p, n);
185 int32 go_write(uintptr, void *, int32)
186 __asm__ (GOSYM_PREFIX "runtime.write");
188 int32
189 go_write(uintptr fd, void *p, int32 n)
191 return runtime_write(fd, p, n);
194 int32 go_closefd(int32)
195 __asm__ (GOSYM_PREFIX "runtime.closefd");
197 int32
198 go_closefd(int32 fd)
200 return runtime_close(fd);
203 intgo go_errno(void)
204 __asm__ (GOSYM_PREFIX "runtime.errno");
206 intgo
207 go_errno()
209 return (intgo)errno;
212 // CPU-specific initialization.
213 // Fetch CPUID info on x86.
215 void
216 runtime_cpuinit()
218 #if defined(__i386__) || defined(__x86_64__)
219 unsigned int eax, ebx, ecx, edx;
221 if (__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
222 setCpuidECX(ecx);
224 #endif