]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/runtime.c
[dev.power64] all: merge default (dd5014ed9b01) into dev.power64
[gostls13.git] / src / runtime / runtime.c
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.
4
5 #include "runtime.h"
6 #include "stack.h"
7 #include "arch_GOARCH.h"
8 #include "textflag.h"
9 #include "malloc.h"
10
11 // Keep a cached value to make gotraceback fast,
12 // since we call it on every call to gentraceback.
13 // The cached value is a uint32 in which the low bit
14 // is the "crash" setting and the top 31 bits are the
15 // gotraceback value.
16 static uint32 traceback_cache = 2<<1;
17
18 // The GOTRACEBACK environment variable controls the
19 // behavior of a Go program that is crashing and exiting.
20 //      GOTRACEBACK=0   suppress all tracebacks
21 //      GOTRACEBACK=1   default behavior - show tracebacks but exclude runtime frames
22 //      GOTRACEBACK=2   show tracebacks including runtime frames
23 //      GOTRACEBACK=crash   show tracebacks including runtime frames, then crash (core dump etc)
24 #pragma textflag NOSPLIT
25 int32
26 runtime·gotraceback(bool *crash)
27 {
28         if(crash != nil)
29                 *crash = false;
30         if(g->m->traceback != 0)
31                 return g->m->traceback;
32         if(crash != nil)
33                 *crash = traceback_cache&1;
34         return traceback_cache>>1;
35 }
36
37 int32
38 runtime·mcmp(byte *s1, byte *s2, uintptr n)
39 {
40         uintptr i;
41         byte c1, c2;
42
43         for(i=0; i<n; i++) {
44                 c1 = s1[i];
45                 c2 = s2[i];
46                 if(c1 < c2)
47                         return -1;
48                 if(c1 > c2)
49                         return +1;
50         }
51         return 0;
52 }
53
54
55 byte*
56 runtime·mchr(byte *p, byte c, byte *ep)
57 {
58         for(; p < ep; p++)
59                 if(*p == c)
60                         return p;
61         return nil;
62 }
63
64 static int32    argc;
65
66 #pragma dataflag NOPTR /* argv not a heap pointer */
67 static uint8**  argv;
68
69 extern Slice runtime·argslice;
70 extern Slice runtime·envs;
71
72 void (*runtime·sysargs)(int32, uint8**);
73
74 void
75 runtime·args(int32 c, uint8 **v)
76 {
77         argc = c;
78         argv = v;
79         if(runtime·sysargs != nil)
80                 runtime·sysargs(c, v);
81 }
82
83 int32 runtime·isplan9;
84 int32 runtime·issolaris;
85 int32 runtime·iswindows;
86
87 // Information about what cpu features are available.
88 // Set on startup in asm_{x86/amd64}.s.
89 uint32 runtime·cpuid_ecx;
90 uint32 runtime·cpuid_edx;
91
92 void
93 runtime·goargs(void)
94 {
95         String *s;
96         int32 i;
97
98         // for windows implementation see "os" package
99         if(Windows)
100                 return;
101
102         runtime·argslice = runtime·makeStringSlice(argc);
103         s = (String*)runtime·argslice.array;
104         for(i=0; i<argc; i++)
105                 s[i] = runtime·gostringnocopy(argv[i]);
106 }
107
108 void
109 runtime·goenvs_unix(void)
110 {
111         String *s;
112         int32 i, n;
113
114         for(n=0; argv[argc+1+n] != 0; n++)
115                 ;
116
117         runtime·envs = runtime·makeStringSlice(n);
118         s = (String*)runtime·envs.array;
119         for(i=0; i<n; i++)
120                 s[i] = runtime·gostringnocopy(argv[argc+1+i]);
121 }
122
123 #pragma textflag NOSPLIT
124 Slice
125 runtime·environ()
126 {
127         return runtime·envs;
128 }
129
130 int32
131 runtime·atoi(byte *p)
132 {
133         int32 n;
134
135         n = 0;
136         while('0' <= *p && *p <= '9')
137                 n = n*10 + *p++ - '0';
138         return n;
139 }
140
141 static void
142 TestAtomic64(void)
143 {
144         uint64 z64, x64;
145
146         z64 = 42;
147         x64 = 0;
148         PREFETCH(&z64);
149         if(runtime·cas64(&z64, x64, 1))
150                 runtime·throw("cas64 failed");
151         if(x64 != 0)
152                 runtime·throw("cas64 failed");
153         x64 = 42;
154         if(!runtime·cas64(&z64, x64, 1))
155                 runtime·throw("cas64 failed");
156         if(x64 != 42 || z64 != 1)
157                 runtime·throw("cas64 failed");
158         if(runtime·atomicload64(&z64) != 1)
159                 runtime·throw("load64 failed");
160         runtime·atomicstore64(&z64, (1ull<<40)+1);
161         if(runtime·atomicload64(&z64) != (1ull<<40)+1)
162                 runtime·throw("store64 failed");
163         if(runtime·xadd64(&z64, (1ull<<40)+1) != (2ull<<40)+2)
164                 runtime·throw("xadd64 failed");
165         if(runtime·atomicload64(&z64) != (2ull<<40)+2)
166                 runtime·throw("xadd64 failed");
167         if(runtime·xchg64(&z64, (3ull<<40)+3) != (2ull<<40)+2)
168                 runtime·throw("xchg64 failed");
169         if(runtime·atomicload64(&z64) != (3ull<<40)+3)
170                 runtime·throw("xchg64 failed");
171 }
172
173 void
174 runtime·check(void)
175 {
176         int8 a;
177         uint8 b;
178         int16 c;
179         uint16 d;
180         int32 e;
181         uint32 f;
182         int64 g;
183         uint64 h;
184         float32 i, i1;
185         float64 j, j1;
186         byte *k, *k1;
187         uint16* l;
188         byte m[4];
189         struct x1 {
190                 byte x;
191         };
192         struct y1 {
193                 struct x1 x1;
194                 byte y;
195         };
196
197         if(sizeof(a) != 1) runtime·throw("bad a");
198         if(sizeof(b) != 1) runtime·throw("bad b");
199         if(sizeof(c) != 2) runtime·throw("bad c");
200         if(sizeof(d) != 2) runtime·throw("bad d");
201         if(sizeof(e) != 4) runtime·throw("bad e");
202         if(sizeof(f) != 4) runtime·throw("bad f");
203         if(sizeof(g) != 8) runtime·throw("bad g");
204         if(sizeof(h) != 8) runtime·throw("bad h");
205         if(sizeof(i) != 4) runtime·throw("bad i");
206         if(sizeof(j) != 8) runtime·throw("bad j");
207         if(sizeof(k) != sizeof(uintptr)) runtime·throw("bad k");
208         if(sizeof(l) != sizeof(uintptr)) runtime·throw("bad l");
209         if(sizeof(struct x1) != 1) runtime·throw("bad sizeof x1");
210         if(offsetof(struct y1, y) != 1) runtime·throw("bad offsetof y1.y");
211         if(sizeof(struct y1) != 2) runtime·throw("bad sizeof y1");
212
213         if(runtime·timediv(12345LL*1000000000+54321, 1000000000, &e) != 12345 || e != 54321)
214                 runtime·throw("bad timediv");
215
216         uint32 z;
217         z = 1;
218         if(!runtime·cas(&z, 1, 2))
219                 runtime·throw("cas1");
220         if(z != 2)
221                 runtime·throw("cas2");
222
223         z = 4;
224         if(runtime·cas(&z, 5, 6))
225                 runtime·throw("cas3");
226         if(z != 4)
227                 runtime·throw("cas4");
228
229         k = (byte*)0xfedcb123;
230         if(sizeof(void*) == 8)
231                 k = (byte*)((uintptr)k<<10);
232         if(runtime·casp((void**)&k, nil, nil))
233                 runtime·throw("casp1");
234         k1 = k+1;
235         if(!runtime·casp((void**)&k, k, k1))
236                 runtime·throw("casp2");
237         if(k != k1)
238                 runtime·throw("casp3");
239
240         m[0] = m[1] = m[2] = m[3] = 0x1;
241         runtime·atomicor8(&m[1], 0xf0);
242         if (m[0] != 0x1 || m[1] != 0xf1 || m[2] != 0x1 || m[3] != 0x1)
243                 runtime·throw("atomicor8");
244
245         *(uint64*)&j = ~0ULL;
246         if(j == j)
247                 runtime·throw("float64nan");
248         if(!(j != j))
249                 runtime·throw("float64nan1");
250
251         *(uint64*)&j1 = ~1ULL;
252         if(j == j1)
253                 runtime·throw("float64nan2");
254         if(!(j != j1))
255                 runtime·throw("float64nan3");
256
257         *(uint32*)&i = ~0UL;
258         if(i == i)
259                 runtime·throw("float32nan");
260         if(!(i != i))
261                 runtime·throw("float32nan1");
262
263         *(uint32*)&i1 = ~1UL;
264         if(i == i1)
265                 runtime·throw("float32nan2");
266         if(!(i != i1))
267                 runtime·throw("float32nan3");
268
269         TestAtomic64();
270
271         if(FixedStack != runtime·round2(FixedStack))
272                 runtime·throw("FixedStack is not power-of-2");
273 }
274
275 #pragma dataflag NOPTR
276 DebugVars       runtime·debug;
277
278 typedef struct DbgVar DbgVar;
279 struct DbgVar
280 {
281         int8*   name;
282         int32*  value;
283 };
284
285 // Do we report invalid pointers found during stack or heap scans?
286 int32 runtime·invalidptr = 1;
287
288 #pragma dataflag NOPTR /* dbgvar has no heap pointers */
289 static DbgVar dbgvar[] = {
290         {"allocfreetrace", &runtime·debug.allocfreetrace},
291         {"invalidptr", &runtime·invalidptr},
292         {"efence", &runtime·debug.efence},
293         {"gctrace", &runtime·debug.gctrace},
294         {"gcdead", &runtime·debug.gcdead},
295         {"scheddetail", &runtime·debug.scheddetail},
296         {"schedtrace", &runtime·debug.schedtrace},
297         {"scavenge", &runtime·debug.scavenge},
298 };
299
300 void
301 runtime·parsedebugvars(void)
302 {
303         byte *p;
304         intgo i, n;
305
306         p = runtime·getenv("GODEBUG");
307         if(p != nil){
308                 for(;;) {
309                         for(i=0; i<nelem(dbgvar); i++) {
310                                 n = runtime·findnull((byte*)dbgvar[i].name);
311                                 if(runtime·mcmp(p, (byte*)dbgvar[i].name, n) == 0 && p[n] == '=')
312                                         *dbgvar[i].value = runtime·atoi(p+n+1);
313                         }
314                         p = runtime·strstr(p, (byte*)",");
315                         if(p == nil)
316                                 break;
317                         p++;
318                 }
319         }
320
321         p = runtime·getenv("GOTRACEBACK");
322         if(p == nil)
323                 p = (byte*)"";
324         if(p[0] == '\0')
325                 traceback_cache = 1<<1;
326         else if(runtime·strcmp(p, (byte*)"crash") == 0)
327                 traceback_cache = (2<<1) | 1;
328         else
329                 traceback_cache = runtime·atoi(p)<<1;  
330 }
331
332 // Poor mans 64-bit division.
333 // This is a very special function, do not use it if you are not sure what you are doing.
334 // int64 division is lowered into _divv() call on 386, which does not fit into nosplit functions.
335 // Handles overflow in a time-specific manner.
336 #pragma textflag NOSPLIT
337 int32
338 runtime·timediv(int64 v, int32 div, int32 *rem)
339 {
340         int32 res, bit;
341
342         res = 0;
343         for(bit = 30; bit >= 0; bit--) {
344                 if(v >= ((int64)div<<bit)) {
345                         v = v - ((int64)div<<bit);
346                         res += 1<<bit;
347                 }
348         }
349         if(v >= (int64)div) {
350                 if(rem != nil)
351                         *rem = 0;
352                 return 0x7fffffff;
353         }
354         if(rem != nil)
355                 *rem = v;
356         return res;
357 }
358
359 // Helpers for Go. Must be NOSPLIT, must only call NOSPLIT functions, and must not block.
360
361 #pragma textflag NOSPLIT
362 G*
363 runtime·getg(void)
364 {
365         return g;
366 }
367
368 #pragma textflag NOSPLIT
369 M*
370 runtime·acquirem(void)
371 {
372         g->m->locks++;
373         return g->m;
374 }
375
376 #pragma textflag NOSPLIT
377 void
378 runtime·releasem(M *mp)
379 {
380         mp->locks--;
381         if(mp->locks == 0 && g->preempt) {
382                 // restore the preemption request in case we've cleared it in newstack
383                 g->stackguard0 = StackPreempt;
384         }
385 }
386
387 #pragma textflag NOSPLIT
388 MCache*
389 runtime·gomcache(void)
390 {
391         return g->m->mcache;
392 }
393
394 #pragma textflag NOSPLIT
395 Slice
396 reflect·typelinks(void)
397 {
398         extern Type *runtime·typelink[], *runtime·etypelink[];
399         Slice ret;
400
401         ret.array = (byte*)runtime·typelink;
402         ret.len = runtime·etypelink - runtime·typelink;
403         ret.cap = ret.len;
404         return ret;
405 }