1 // Copyright 2013 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 "zasm_GOOS_GOARCH.h"
7 #include "syscall_nacl.h"
9 #define NACL_SYSCALL(code) \
10 MOVL $(0x10000 + ((code)<<5)), AX; CALL AX
12 TEXT runtime·settls(SB),NOSPLIT,$0
13 MOVL DI, TLS // really BP
16 TEXT runtime·exit(SB),NOSPLIT,$0
18 NACL_SYSCALL(SYS_exit)
21 TEXT runtime·exit1(SB),NOSPLIT,$0
23 NACL_SYSCALL(SYS_thread_exit)
26 TEXT runtime·open(SB),NOSPLIT,$0
30 NACL_SYSCALL(SYS_open)
34 TEXT runtime·close(SB),NOSPLIT,$0
36 NACL_SYSCALL(SYS_close)
40 TEXT runtime·read(SB),NOSPLIT,$0
44 NACL_SYSCALL(SYS_read)
48 TEXT syscall·naclWrite(SB), NOSPLIT, $24-20
55 CALL runtime·write(SB)
60 TEXT runtime·write(SB),NOSPLIT,$16-20
61 // If using fake time and writing to stdout or stderr,
62 // emit playback header before actual data.
63 MOVQ runtime·faketime(SB), AX
77 NACL_SYSCALL(SYS_write)
81 // Write with playback header.
82 // First, lock to avoid interleaving writes.
85 XCHGL runtime·writelock(SB), BX
89 // Playback header: 0 0 P B <8-byte time> <4-byte data length>
90 MOVL $(('B'<<24) | ('P'<<16)), 0(SP)
96 MOVL $1, DI // standard output
99 NACL_SYSCALL(SYS_write)
101 // Write actual data.
102 MOVL $1, DI // standard output
105 NACL_SYSCALL(SYS_write)
108 MOVL $0, runtime·writelock(SB)
113 TEXT runtime·nacl_exception_stack(SB),NOSPLIT,$0
116 NACL_SYSCALL(SYS_exception_stack)
120 TEXT runtime·nacl_exception_handler(SB),NOSPLIT,$0
123 NACL_SYSCALL(SYS_exception_handler)
127 TEXT runtime·nacl_sem_create(SB),NOSPLIT,$0
129 NACL_SYSCALL(SYS_sem_create)
133 TEXT runtime·nacl_sem_wait(SB),NOSPLIT,$0
135 NACL_SYSCALL(SYS_sem_wait)
139 TEXT runtime·nacl_sem_post(SB),NOSPLIT,$0
141 NACL_SYSCALL(SYS_sem_post)
145 TEXT runtime·nacl_mutex_create(SB),NOSPLIT,$0
147 NACL_SYSCALL(SYS_mutex_create)
151 TEXT runtime·nacl_mutex_lock(SB),NOSPLIT,$0
153 NACL_SYSCALL(SYS_mutex_lock)
157 TEXT runtime·nacl_mutex_trylock(SB),NOSPLIT,$0
159 NACL_SYSCALL(SYS_mutex_trylock)
163 TEXT runtime·nacl_mutex_unlock(SB),NOSPLIT,$0
165 NACL_SYSCALL(SYS_mutex_unlock)
169 TEXT runtime·nacl_cond_create(SB),NOSPLIT,$0
171 NACL_SYSCALL(SYS_cond_create)
175 TEXT runtime·nacl_cond_wait(SB),NOSPLIT,$0
178 NACL_SYSCALL(SYS_cond_wait)
182 TEXT runtime·nacl_cond_signal(SB),NOSPLIT,$0
184 NACL_SYSCALL(SYS_cond_signal)
188 TEXT runtime·nacl_cond_broadcast(SB),NOSPLIT,$0
190 NACL_SYSCALL(SYS_cond_broadcast)
194 TEXT runtime·nacl_cond_timed_wait_abs(SB),NOSPLIT,$0
198 NACL_SYSCALL(SYS_cond_timed_wait_abs)
202 TEXT runtime·nacl_thread_create(SB),NOSPLIT,$0
207 NACL_SYSCALL(SYS_thread_create)
211 TEXT runtime·mstart_nacl(SB),NOSPLIT,$0
212 NACL_SYSCALL(SYS_tls_get)
215 JMP runtime·mstart(SB)
217 TEXT runtime·nacl_nanosleep(SB),NOSPLIT,$0
220 NACL_SYSCALL(SYS_nanosleep)
224 TEXT runtime·osyield(SB),NOSPLIT,$0
225 NACL_SYSCALL(SYS_sched_yield)
228 TEXT runtime·mmap(SB),NOSPLIT,$8
232 MOVL flags+12(FP), CX
237 NACL_SYSCALL(SYS_mmap)
244 TEXT time·now(SB),NOSPLIT,$16
245 MOVQ runtime·faketime(SB), AX
255 MOVL $0, DI // real time clock
257 MOVL AX, SI // timespec
258 NACL_SYSCALL(SYS_clock_gettime)
259 MOVL 0(SP), AX // low 32 sec
260 MOVL 4(SP), CX // high 32 sec
261 MOVL 8(SP), BX // nsec
263 // sec is in AX, nsec in BX
269 TEXT syscall·now(SB),NOSPLIT,$0
272 TEXT runtime·nacl_clock_gettime(SB),NOSPLIT,$0
275 NACL_SYSCALL(SYS_clock_gettime)
279 TEXT runtime·nanotime(SB),NOSPLIT,$16
280 MOVQ runtime·faketime(SB), AX
285 MOVL $0, DI // real time clock
287 MOVL AX, SI // timespec
288 NACL_SYSCALL(SYS_clock_gettime)
289 MOVQ 0(SP), AX // sec
290 MOVL 8(SP), DX // nsec
292 // sec is in AX, nsec in DX
294 IMULQ $1000000000, AX
299 TEXT runtime·sigtramp(SB),NOSPLIT,$80
300 // restore TLS register at time of execution,
301 // in case it's been smashed.
302 // the TLS register is really BP, but for consistency
303 // with non-NaCl systems it is referred to here as TLS.
304 // NOTE: Cannot use SYS_tls_get here (like we do in mstart_nacl),
305 // because the main thread never calls tls_set.
307 MOVL (16*4+5*8)(AX), AX
310 // check that g exists
322 MOVL m_gsignal(BX), BX
327 // copy arguments for sighandler
328 MOVL $11, 0(SP) // signal
329 MOVL $0, 4(SP) // siginfo
331 MOVL AX, 8(SP) // context
334 CALL runtime·sighandler(SB)
341 // Enable exceptions again.
342 NACL_SYSCALL(SYS_exception_clear_flag)
344 // Restore registers as best we can. Impossible to do perfectly.
345 // See comment in sys_nacl_386.s for extended rationale.
352 MOVL 32(SI), SP // MOVL for SP sandboxing
353 // 40(SI) is saved BP aka TLS, already restored above
354 // 48(SI) is saved SI, never to be seen again
363 // 120(SI) is R15, which is owned by Native Client and must not be modified
364 MOVQ 128(SI), SI // saved PC
365 // 136(SI) is saved EFLAGS, never to be seen again
369 // print basic information
371 MOVL $runtime·sigtrampf(SB), AX
373 MOVQ (16*4+16*8)(DI), BX // rip
375 MOVQ (16*4+0*8)(DI), BX // rax
377 MOVQ (16*4+1*8)(DI), BX // rcx
379 MOVQ (16*4+2*8)(DI), BX // rdx
381 MOVQ (16*4+3*8)(DI), BX // rbx
383 MOVQ (16*4+7*8)(DI), BX // rdi
385 MOVQ (16*4+15*8)(DI), BX // r15
387 MOVQ (16*4+4*8)(DI), BX // rsp
390 CALL runtime·printf(SB)
393 MOVQ (16*4+16*8)(DI), BX // rip
395 MOVQ (16*4+4*8)(DI), BX // rsp
400 MOVL BX, 12(SP) // gp
401 CALL runtime·traceback(SB)
411 // cannot do real signal handling yet, because gsignal has not been allocated.
412 MOVL $1, DI; NACL_SYSCALL(SYS_exit)
414 TEXT runtime·nacl_sysinfo(SB),NOSPLIT,$16
422 MOVL BX, runtime·nacl_irt_query(SB)
434 MOVL BX, runtime·nacl_irt_query(SB)
436 LEAL runtime·nacl_irt_basic_v0_1_str(SB), DI
437 LEAL runtime·nacl_irt_basic_v0_1(SB), SI
438 MOVL runtime·nacl_irt_basic_v0_1_size(SB), DX
439 MOVL runtime·nacl_irt_query(SB), BX
442 LEAL runtime·nacl_irt_memory_v0_3_str(SB), DI
443 LEAL runtime·nacl_irt_memory_v0_3(SB), SI
444 MOVL runtime·nacl_irt_memory_v0_3_size(SB), DX
445 MOVL runtime·nacl_irt_query(SB), BX
448 LEAL runtime·nacl_irt_thread_v0_1_str(SB), DI
449 LEAL runtime·nacl_irt_thread_v0_1(SB), SI
450 MOVL runtime·nacl_irt_thread_v0_1_size(SB), DX
451 MOVL runtime·nacl_irt_query(SB), BX
454 // TODO: Once we have a NaCl SDK with futex syscall support,
455 // try switching to futex syscalls and here load the
456 // nacl-irt-futex-0.1 table.