LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2010 The Go Authors. All rights reserved. | 1 // Copyright 2010 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #include "runtime.h" | 5 #include "runtime.h" |
6 #include "os_GOOS.h" | 6 #include "os_GOOS.h" |
7 #include "arch_GOARCH.h" | 7 #include "arch_GOARCH.h" |
8 | 8 |
9 int8 *goos = "plan9"; | 9 int8 *goos = "plan9"; |
| 10 int8 *runtime·exitstatus; |
| 11 |
| 12 int32 runtime·postnote(int32, int8*); |
10 | 13 |
11 void | 14 void |
12 runtime·minit(void) | 15 runtime·minit(void) |
13 { | 16 { |
14 } | 17 } |
15 | 18 |
16 static int32 | 19 static int32 |
17 getproccount(void) | 20 getproccount(void) |
18 { | 21 { |
19 int32 fd, i, n, ncpu; | 22 int32 fd, i, n, ncpu; |
20 byte buf[2048]; | 23 byte buf[2048]; |
21 | 24 |
22 fd = runtime·open((byte*)"/dev/sysstat", OREAD); | 25 fd = runtime·open((byte*)"/dev/sysstat", OREAD); |
23 if(fd < 0) | 26 if(fd < 0) |
24 return 1; | 27 return 1; |
25 ncpu = 0; | 28 ncpu = 0; |
26 for(;;) { | 29 for(;;) { |
27 n = runtime·read(fd, buf, sizeof buf); | 30 n = runtime·read(fd, buf, sizeof buf); |
28 if(n <= 0) | 31 if(n <= 0) |
29 break; | 32 break; |
30 for(i = 0; i < n; i++) { | 33 for(i = 0; i < n; i++) { |
31 if(buf[i] == '\n') | 34 if(buf[i] == '\n') |
32 ncpu++; | 35 ncpu++; |
33 } | 36 } |
34 } | 37 } |
35 runtime·close(fd); | 38 runtime·close(fd); |
36 return ncpu > 0 ? ncpu : 1; | 39 return ncpu > 0 ? ncpu : 1; |
37 } | 40 } |
38 | 41 |
| 42 static int32 |
| 43 getpid(void) |
| 44 { |
| 45 byte b[20], *c; |
| 46 int32 fd, n; |
| 47 |
| 48 runtime·memclr(b, sizeof(b)); |
| 49 fd = runtime·open((byte*)"#c/pid", 0); |
| 50 if(fd >= 0) { |
| 51 runtime·read(fd, b, sizeof(b)); |
| 52 runtime·close(fd); |
| 53 } |
| 54 c = b; |
| 55 while(*c == ' ' || *c == '\t') |
| 56 c++; |
| 57 return runtime·atoi(c); |
| 58 } |
| 59 |
39 void | 60 void |
40 runtime·osinit(void) | 61 runtime·osinit(void) |
41 { | 62 { |
42 runtime·ncpu = getproccount(); | 63 runtime·ncpu = getproccount(); |
| 64 m->procid = getpid(); |
| 65 runtime·notify(runtime·gonote); |
43 } | 66 } |
44 | 67 |
45 void | 68 void |
46 runtime·goenvs(void) | 69 runtime·goenvs(void) |
47 { | 70 { |
48 } | 71 } |
49 | 72 |
50 void | 73 void |
51 runtime·initsig(void) | 74 runtime·initsig(void) |
52 { | 75 { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 { | 125 { |
103 int64 ns; | 126 int64 ns; |
104 | 127 |
105 ns = runtime·nanotime(); | 128 ns = runtime·nanotime(); |
106 sec = ns / 1000000000LL; | 129 sec = ns / 1000000000LL; |
107 nsec = ns - sec * 1000000000LL; | 130 nsec = ns - sec * 1000000000LL; |
108 FLUSH(&sec); | 131 FLUSH(&sec); |
109 FLUSH(&nsec); | 132 FLUSH(&nsec); |
110 } | 133 } |
111 | 134 |
112 extern Tos *_tos; | 135 void |
113 void | 136 runtime·itoa(int32 n, byte *p, uint32 len) |
114 runtime·exit(int32) | 137 { |
115 { | 138 » byte *q, c; |
116 » int32 fd; | 139 » uint32 i; |
| 140 |
| 141 » if(len <= 1) |
| 142 » » return; |
| 143 |
| 144 » runtime·memclr(p, len); |
| 145 » q = p; |
| 146 |
| 147 » if(n==0) { |
| 148 » » *q++ = '0'; |
| 149 » » USED(q); |
| 150 » » return; |
| 151 » } |
| 152 » if(n < 0) { |
| 153 » » *q++ = '-'; |
| 154 » » p++; |
| 155 » » n = -n; |
| 156 » } |
| 157 » for(i=0; n > 0 && i < len; i++) { |
| 158 » » *q++ = '0' + (n%10); |
| 159 » » n = n/10; |
| 160 » } |
| 161 » for(q--; q >= p; ) { |
| 162 » » c = *p; |
| 163 » » *p++ = *q; |
| 164 » » *q-- = c; |
| 165 » } |
| 166 } |
| 167 |
| 168 void |
| 169 goexitsall(void) |
| 170 { |
| 171 » M *m; |
| 172 » int32 pid; |
| 173 |
| 174 » pid = getpid(); |
| 175 » for(m=runtime·atomicloadp(&runtime·allm); m; m=m->alllink) |
| 176 » » if(m->procid != pid) |
| 177 » » » runtime·postnote(m->procid, "gointr"); |
| 178 } |
| 179 |
| 180 void |
| 181 runtime·gonote(void*, byte *s) |
| 182 { |
| 183 » uint8 buf[128]; |
| 184 » int32 l; |
| 185 |
| 186 » l = runtime·findnull(s); |
| 187 » if(l > 4 && runtime·mcmp(s, (byte*)"sys:", 4) == 0) { |
| 188 » » runtime·memclr(buf, sizeof buf); |
| 189 » » runtime·memmove((void*)buf, (void*)s, runtime·findnull(s)); |
| 190 » » runtime·exitstatus = (int8*)buf; |
| 191 » » goexitsall(); |
| 192 » » runtime·noted(NDFLT); |
| 193 » } |
| 194 |
| 195 » if(runtime·exitstatus) |
| 196 » » runtime·exits(runtime·exitstatus); |
| 197 |
| 198 » if(runtime·strcmp(s, (byte*)"gointr") == 0) |
| 199 » » runtime·noted(NCONT); |
| 200 |
| 201 » runtime·noted(NDFLT); |
| 202 } |
| 203 |
| 204 int32 |
| 205 runtime·postnote(int32 pid, int8* msg) |
| 206 { |
| 207 » int32 fd, len; |
117 uint8 buf[128]; | 208 uint8 buf[128]; |
118 uint8 tmp[16]; | 209 uint8 tmp[16]; |
119 uint8 *p, *q; | 210 uint8 *p, *q; |
120 int32 pid; | |
121 | 211 |
122 runtime·memclr(buf, sizeof buf); | 212 runtime·memclr(buf, sizeof buf); |
123 » runtime·memclr(tmp, sizeof tmp); | 213 |
124 » pid = _tos->pid; | 214 » /* build path string /proc/pid/note */ |
125 | 215 » q = tmp; |
126 » /* build path string /proc/pid/notepg */ | |
127 » for(q=tmp; pid > 0;) { | |
128 » » *q++ = '0' + (pid%10); | |
129 » » pid = pid/10; | |
130 » } | |
131 p = buf; | 216 p = buf; |
| 217 runtime·itoa(pid, tmp, sizeof tmp); |
132 runtime·memmove((void*)p, (void*)"/proc/", 6); | 218 runtime·memmove((void*)p, (void*)"/proc/", 6); |
133 » p += 6; | 219 » for(p += 6; *p++ = *q++; ); |
134 » for(q--; q >= tmp;) | 220 » p--; |
135 » » *p++ = *q--; | 221 » runtime·memmove((void*)p, (void*)"/note", 5); |
136 » runtime·memmove((void*)p, (void*)"/notepg", 7); | 222 |
137 | |
138 » /* post interrupt note */ | |
139 fd = runtime·open(buf, OWRITE); | 223 fd = runtime·open(buf, OWRITE); |
140 » runtime·write(fd, "interrupt", 9); | 224 » if(fd < 0) |
141 » runtime·exits(nil); | 225 » » return -1; |
| 226 |
| 227 » len = runtime·findnull((byte*)msg); |
| 228 » if(runtime·write(fd, msg, len) != len) { |
| 229 » » runtime·close(fd); |
| 230 » » return -1; |
| 231 » } |
| 232 » runtime·close(fd); |
| 233 » return 0; |
| 234 } |
| 235 |
| 236 void |
| 237 runtime·exit(int32 e) |
| 238 { |
| 239 » byte tmp[16]; |
| 240 |
| 241 » if(e == 0) |
| 242 » » runtime·exitstatus = ""; |
| 243 » else { |
| 244 » » /* build error string */ |
| 245 » » runtime·itoa(e, tmp, sizeof tmp); |
| 246 » » runtime·exitstatus = (int8*)tmp; |
| 247 » } |
| 248 |
| 249 » goexitsall(); |
| 250 » runtime·exits(runtime·exitstatus); |
142 } | 251 } |
143 | 252 |
144 void | 253 void |
145 runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) | 254 runtime·newosproc(M *m, G *g, void *stk, void (*fn)(void)) |
146 { | 255 { |
147 m->tls[0] = m->id; // so 386 asm can find it | 256 m->tls[0] = m->id; // so 386 asm can find it |
148 if(0){ | 257 if(0){ |
149 runtime·printf("newosproc stk=%p m=%p g=%p fn=%p rfork=%p id=%d/
%d ostk=%p\n", | 258 runtime·printf("newosproc stk=%p m=%p g=%p fn=%p rfork=%p id=%d/
%d ostk=%p\n", |
150 stk, m, g, fn, runtime·rfork, m->id, m->tls[0], &m); | 259 stk, m, g, fn, runtime·rfork, m->id, m->tls[0], &m); |
151 } | 260 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 | 369 |
261 static int8 badsignal[] = "runtime: signal received on thread not created by Go.
\n"; | 370 static int8 badsignal[] = "runtime: signal received on thread not created by Go.
\n"; |
262 | 371 |
263 // This runs on a foreign stack, without an m or a g. No stack split. | 372 // This runs on a foreign stack, without an m or a g. No stack split. |
264 #pragma textflag 7 | 373 #pragma textflag 7 |
265 void | 374 void |
266 runtime·badsignal(void) | 375 runtime·badsignal(void) |
267 { | 376 { |
268 runtime·pwrite(2, badsignal, sizeof badsignal - 1, -1LL); | 377 runtime·pwrite(2, badsignal, sizeof badsignal - 1, -1LL); |
269 } | 378 } |
LEFT | RIGHT |