1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
// Copyright 2013 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "zasm_GOOS_GOARCH.h"
#include "textflag.h"
#include "syscall_nacl.h"
#define NACL_SYSCALL(code) \
MOVL $(0x10000 + ((code)<<5)), AX; CALL AX
TEXT runtime·exit(SB),NOSPLIT,$4
MOVL code+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_exit)
JMP 0(PC)
TEXT runtime·exit1(SB),NOSPLIT,$4
MOVL code+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_thread_exit)
RET
TEXT runtime·open(SB),NOSPLIT,$12
MOVL name+0(FP), AX
MOVL AX, 0(SP)
MOVL mode+4(FP), AX
MOVL AX, 4(SP)
MOVL perm+8(FP), AX
MOVL AX, 8(SP)
NACL_SYSCALL(SYS_open)
MOVL AX, ret+12(FP)
RET
TEXT runtime·close(SB),NOSPLIT,$4
MOVL fd+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_close)
MOVL AX, ret+4(FP)
RET
TEXT runtime·read(SB),NOSPLIT,$12
MOVL fd+0(FP), AX
MOVL AX, 0(SP)
MOVL p+4(FP), AX
MOVL AX, 4(SP)
MOVL n+8(FP), AX
MOVL AX, 8(SP)
NACL_SYSCALL(SYS_read)
MOVL AX, ret+12(FP)
RET
TEXT syscall·naclWrite(SB), NOSPLIT, $16-16
MOVL arg1+0(FP), DI
MOVL arg2+4(FP), SI
MOVL arg3+8(FP), DX
MOVL DI, 0(SP)
MOVL SI, 4(SP)
MOVL DX, 8(SP)
CALL runtime·write(SB)
MOVL AX, ret+16(FP)
RET
TEXT runtime·write(SB),NOSPLIT,$12
MOVL fd+0(FP), AX
MOVL AX, 0(SP)
MOVL p+4(FP), AX
MOVL AX, 4(SP)
MOVL n+8(FP), AX
MOVL AX, 8(SP)
NACL_SYSCALL(SYS_write)
MOVL AX, ret+12(FP)
RET
TEXT runtime·nacl_exception_stack(SB),NOSPLIT,$8
MOVL p+0(FP), AX
MOVL AX, 0(SP)
MOVL size+4(FP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_exception_stack)
MOVL AX, ret+8(FP)
RET
TEXT runtime·nacl_exception_handler(SB),NOSPLIT,$8
MOVL fn+0(FP), AX
MOVL AX, 0(SP)
MOVL arg+4(FP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_exception_handler)
MOVL AX, ret+8(FP)
RET
TEXT runtime·nacl_sem_create(SB),NOSPLIT,$4
MOVL flag+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_sem_create)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_sem_wait(SB),NOSPLIT,$4
MOVL sem+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_sem_wait)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_sem_post(SB),NOSPLIT,$4
MOVL sem+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_sem_post)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_mutex_create(SB),NOSPLIT,$4
MOVL flag+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_mutex_create)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_mutex_lock(SB),NOSPLIT,$4
MOVL mutex+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_mutex_lock)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_mutex_trylock(SB),NOSPLIT,$4
MOVL mutex+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_mutex_trylock)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_mutex_unlock(SB),NOSPLIT,$4
MOVL mutex+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_mutex_unlock)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_cond_create(SB),NOSPLIT,$4
MOVL flag+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_cond_create)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_cond_wait(SB),NOSPLIT,$8
MOVL cond+0(FP), AX
MOVL AX, 0(SP)
MOVL n+4(FP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_cond_wait)
MOVL AX, ret+8(FP)
RET
TEXT runtime·nacl_cond_signal(SB),NOSPLIT,$4
MOVL cond+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_cond_signal)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_cond_broadcast(SB),NOSPLIT,$4
MOVL cond+0(FP), AX
MOVL AX, 0(SP)
NACL_SYSCALL(SYS_cond_broadcast)
MOVL AX, ret+4(FP)
RET
TEXT runtime·nacl_cond_timed_wait_abs(SB),NOSPLIT,$12
MOVL cond+0(FP), AX
MOVL AX, 0(SP)
MOVL lock+4(FP), AX
MOVL AX, 4(SP)
MOVL ts+8(FP), AX
MOVL AX, 8(SP)
NACL_SYSCALL(SYS_cond_timed_wait_abs)
MOVL AX, ret+12(FP)
RET
TEXT runtime·nacl_thread_create(SB),NOSPLIT,$16
MOVL fn+0(FP), AX
MOVL AX, 0(SP)
MOVL stk+4(FP), AX
MOVL AX, 4(SP)
MOVL tls+8(FP), AX
MOVL AX, 8(SP)
MOVL xx+12(FP), AX
MOVL AX, 12(SP)
NACL_SYSCALL(SYS_thread_create)
MOVL AX, ret+16(FP)
RET
TEXT runtime·mstart_nacl(SB),NOSPLIT,$0
JMP runtime·mstart(SB)
TEXT runtime·nacl_nanosleep(SB),NOSPLIT,$8
MOVL ts+0(FP), AX
MOVL AX, 0(SP)
MOVL extra+4(FP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_nanosleep)
MOVL AX, ret+8(FP)
RET
TEXT runtime·osyield(SB),NOSPLIT,$0
NACL_SYSCALL(SYS_sched_yield)
RET
TEXT runtime·mmap(SB),NOSPLIT,$32
MOVL addr+0(FP), AX
MOVL AX, 0(SP)
MOVL n+4(FP), AX
MOVL AX, 4(SP)
MOVL prot+8(FP), AX
MOVL AX, 8(SP)
MOVL flags+12(FP), AX
MOVL AX, 12(SP)
MOVL fd+16(FP), AX
MOVL AX, 16(SP)
MOVL off+20(FP), AX
MOVL AX, 24(SP)
MOVL $0, 28(SP)
LEAL 24(SP), AX
MOVL AX, 20(SP)
NACL_SYSCALL(SYS_mmap)
MOVL AX, ret+24(FP)
RET
TEXT time·now(SB),NOSPLIT,$20
MOVL $0, 0(SP) // real time clock
LEAL 8(SP), AX
MOVL AX, 4(SP) // timespec
NACL_SYSCALL(SYS_clock_gettime)
MOVL 8(SP), AX // low 32 sec
MOVL 12(SP), CX // high 32 sec
MOVL 16(SP), BX // nsec
// sec is in AX, nsec in BX
MOVL AX, sec+0(FP)
MOVL CX, sec+4(FP)
MOVL BX, nsec+8(FP)
RET
TEXT syscall·now(SB),NOSPLIT,$0
JMP time·now(SB)
TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$8
MOVL arg1+0(FP), AX
MOVL AX, 0(SP)
MOVL arg2+4(FP), AX
MOVL AX, 4(SP)
NACL_SYSCALL(SYS_clock_gettime)
MOVL AX, ret+8(FP)
RET
TEXT runtime·nanotime(SB),NOSPLIT,$20
MOVL $0, 0(SP) // real time clock
LEAL 8(SP), AX
MOVL AX, 4(SP) // timespec
NACL_SYSCALL(SYS_clock_gettime)
MOVL 8(SP), AX // low 32 sec
MOVL 16(SP), BX // nsec
// sec is in AX, nsec in BX
// convert to DX:AX nsec
MOVL $1000000000, CX
MULL CX
ADDL BX, AX
ADCL $0, DX
MOVL AX, ret_lo+0(FP)
MOVL DX, ret_hi+4(FP)
RET
TEXT runtime·setldt(SB),NOSPLIT,$8
MOVL addr+4(FP), BX // aka base
ADDL $0x8, BX
MOVL BX, 0(SP)
NACL_SYSCALL(SYS_tls_init)
RET
TEXT runtime·sigtramp(SB),NOSPLIT,$0
get_tls(CX)
// check that g exists
MOVL g(CX), DI
CMPL DI, $0
JNE 6(PC)
MOVL $11, BX
MOVL $0, 0(SP)
MOVL $runtime·badsignal(SB), AX
CALL AX
JMP sigtramp_ret
// save g
MOVL DI, 20(SP)
// g = m->gsignal
MOVL g_m(DI), BX
MOVL m_gsignal(BX), BX
MOVL BX, g(CX)
// copy arguments for sighandler
MOVL $11, 0(SP) // signal
MOVL $0, 4(SP) // siginfo
LEAL ctxt+4(FP), AX
MOVL AX, 8(SP) // context
MOVL DI, 12(SP) // g
CALL runtime·sighandler(SB)
// restore g
get_tls(CX)
MOVL 20(SP), BX
MOVL BX, g(CX)
sigtramp_ret:
// Enable exceptions again.
NACL_SYSCALL(SYS_exception_clear_flag)
// NaCl has abidcated its traditional operating system responsibility
// and declined to implement 'sigreturn'. Instead the only way to return
// to the execution of our program is to restore the registers ourselves.
// Unfortunately, that is impossible to do with strict fidelity, because
// there is no way to do the final update of PC that ends the sequence
// without either (1) jumping to a register, in which case the register ends
// holding the PC value instead of its intended value or (2) storing the PC
// on the stack and using RET, which imposes the requirement that SP is
// valid and that is okay to smash the word below it. The second would
// normally be the lesser of the two evils, except that on NaCl, the linker
// must rewrite RET into "POP reg; AND $~31, reg; JMP reg", so either way
// we are going to lose a register as a result of the incoming signal.
// Similarly, there is no way to restore EFLAGS; the usual way is to use
// POPFL, but NaCl rejects that instruction. We could inspect the bits and
// execute a sequence of instructions designed to recreate those flag
// settings, but that's a lot of work.
//
// Thankfully, Go's signal handlers never try to return directly to the
// executing code, so all the registers and EFLAGS are dead and can be
// smashed. The only registers that matter are the ones that are setting
// up for the simulated call that the signal handler has created.
// Today those registers are just PC and SP, but in case additional registers
// are relevant in the future (for example DX is the Go func context register)
// we restore as many registers as possible.
//
// We smash BP, because that's what the linker smashes during RET.
//
LEAL ctxt+4(FP), BP
ADDL $64, BP
MOVL 0(BP), AX
MOVL 4(BP), CX
MOVL 8(BP), DX
MOVL 12(BP), BX
MOVL 16(BP), SP
// 20(BP) is saved BP, never to be seen again
MOVL 24(BP), SI
MOVL 28(BP), DI
// 36(BP) is saved EFLAGS, never to be seen again
MOVL 32(BP), BP // saved PC
JMP BP
|