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 // System calls and other sys.stuff for AMD64, FreeBSD
6 // /usr/src/sys/kern/syscalls.master for syscall numbers.
12 #include "cgo/abi_amd64.h"
14 #define CLOCK_REALTIME 0
15 #define CLOCK_MONOTONIC 4
16 #define AMD64_SET_FSBASE 129
25 #define SYS_sigaltstack 53
27 #define SYS_madvise 75
28 #define SYS_setitimer 83
30 #define SYS_sysarch 165
31 #define SYS___sysctl 202
32 #define SYS_clock_gettime 232
33 #define SYS_nanosleep 240
34 #define SYS_issetugid 253
35 #define SYS_sched_yield 331
36 #define SYS_sigprocmask 340
37 #define SYS_kqueue 362
38 #define SYS_sigaction 416
39 #define SYS_thr_exit 431
40 #define SYS_thr_self 432
41 #define SYS_thr_kill 433
42 #define SYS__umtx_op 454
43 #define SYS_thr_new 455
45 #define SYS_cpuset_getaffinity 487
47 #define SYS_kevent 560
49 TEXT runtime·sys_umtx_op(SB),NOSPLIT,$0
53 MOVQ uaddr1+16(FP), R10
55 MOVL $SYS__umtx_op, AX
62 TEXT runtime·thr_new(SB),NOSPLIT,$0
72 TEXT runtime·thr_start(SB),NOSPLIT,$0
75 // set up FS to point at m->tls
77 CALL runtime·settls(SB) // smashes DI
85 CALL runtime·stackcheck(SB)
86 CALL runtime·mstart(SB)
88 MOVQ 0, AX // crash (not reached)
90 // Exit the entire program (like C exit)
91 TEXT runtime·exit(SB),NOSPLIT,$-8
92 MOVL code+0(FP), DI // arg 1 exit status
95 MOVL $0xf1, 0xf1 // crash
98 // func exitThread(wait *atomic.uint32)
99 TEXT runtime·exitThread(SB),NOSPLIT,$0-8
101 // We're done using the stack.
103 MOVL $0, DI // arg 1 long *state
104 MOVL $SYS_thr_exit, AX
106 MOVL $0xf1, 0xf1 // crash
109 TEXT runtime·open(SB),NOSPLIT,$-8
110 MOVQ name+0(FP), DI // arg 1 pathname
111 MOVL mode+8(FP), SI // arg 2 flags
112 MOVL perm+12(FP), DX // arg 3 mode
120 TEXT runtime·closefd(SB),NOSPLIT,$-8
121 MOVL fd+0(FP), DI // arg 1 fd
129 TEXT runtime·read(SB),NOSPLIT,$-8
130 MOVL fd+0(FP), DI // arg 1 fd
131 MOVQ p+8(FP), SI // arg 2 buf
132 MOVL n+16(FP), DX // arg 3 count
136 NEGQ AX // caller expects negative errno
140 // func pipe2(flags int32) (r, w int32, errno int32)
141 TEXT runtime·pipe2(SB),NOSPLIT,$0-20
148 MOVL AX, errno+16(FP)
151 TEXT runtime·write1(SB),NOSPLIT,$-8
152 MOVQ fd+0(FP), DI // arg 1 fd
153 MOVQ p+8(FP), SI // arg 2 buf
154 MOVL n+16(FP), DX // arg 3 count
158 NEGQ AX // caller expects negative errno
162 TEXT runtime·thr_self(SB),NOSPLIT,$0-8
164 LEAQ ret+0(FP), DI // arg 1
165 MOVL $SYS_thr_self, AX
169 TEXT runtime·thr_kill(SB),NOSPLIT,$0-16
170 // thr_kill(tid, sig)
171 MOVQ tid+0(FP), DI // arg 1 id
172 MOVQ sig+8(FP), SI // arg 2 sig
173 MOVL $SYS_thr_kill, AX
177 TEXT runtime·raiseproc(SB),NOSPLIT,$0
182 MOVQ AX, DI // arg 1 pid
183 MOVL sig+0(FP), SI // arg 2 sig
188 TEXT runtime·setitimer(SB), NOSPLIT, $-8
192 MOVL $SYS_setitimer, AX
196 // func fallback_walltime() (sec int64, nsec int32)
197 TEXT runtime·fallback_walltime(SB), NOSPLIT, $32-12
198 MOVL $SYS_clock_gettime, AX
199 MOVQ $CLOCK_REALTIME, DI
202 MOVQ 8(SP), AX // sec
203 MOVQ 16(SP), DX // nsec
205 // sec is in AX, nsec in DX
210 TEXT runtime·fallback_nanotime(SB), NOSPLIT, $32-8
211 MOVL $SYS_clock_gettime, AX
212 MOVQ $CLOCK_MONOTONIC, DI
215 MOVQ 8(SP), AX // sec
216 MOVQ 16(SP), DX // nsec
218 // sec is in AX, nsec in DX
220 IMULQ $1000000000, AX
225 TEXT runtime·asmSigaction(SB),NOSPLIT,$0
226 MOVQ sig+0(FP), DI // arg 1 sig
227 MOVQ new+8(FP), SI // arg 2 act
228 MOVQ old+16(FP), DX // arg 3 oact
229 MOVL $SYS_sigaction, AX
236 TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
237 MOVQ sig+0(FP), DI // arg 1 sig
238 MOVQ new+8(FP), SI // arg 2 act
239 MOVQ old+16(FP), DX // arg 3 oact
240 MOVQ _cgo_sigaction(SB), AX
241 MOVQ SP, BX // callee-saved
242 ANDQ $~15, SP // alignment as per amd64 psABI
248 TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
253 MOVQ SP, BX // callee-saved
254 ANDQ $~15, SP // alignment for x86_64 ABI
259 // Called using C ABI.
260 TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME|NOFRAME,$0
261 // Transition from C ABI to Go ABI.
262 PUSH_REGS_HOST_TO_ABI0()
264 // Set up ABIInternal environment: g in R14, cleared X15.
269 // Reserve space for spill slots.
270 NOP SP // disable vet stack checking
273 // Call into the Go signal handler
277 CALL ·sigtrampgo<ABIInternal>(SB)
281 POP_REGS_HOST_TO_ABI0()
284 // Called using C ABI.
285 TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT|NOFRAME,$0
286 // Transition from C ABI to Go ABI.
287 PUSH_REGS_HOST_TO_ABI0()
289 // Set up ABIInternal environment: g in R14, cleared X15.
294 // Reserve space for spill slots.
295 NOP SP // disable vet stack checking
298 // Call into the Go signal handler
302 CALL ·sigprofNonGo<ABIInternal>(SB)
306 POP_REGS_HOST_TO_ABI0()
309 // Used instead of sigtramp in programs that use cgo.
310 // Arguments from kernel are in DI, SI, DX.
311 TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
312 // If no traceback function, do usual sigtramp.
313 MOVQ runtime·cgoTraceback(SB), AX
317 // If no traceback support function, which means that
318 // runtime/cgo was not linked in, do usual sigtramp.
319 MOVQ _cgo_callers(SB), AX
323 // Figure out if we are currently in a cgo call.
324 // If not, just do usual sigtramp.
328 JZ sigtrampnog // g == nil
331 JZ sigtramp // g.m == nil
334 JZ sigtramp // g.m.ncgo == 0
337 JZ sigtramp // g.m.curg == nil
338 MOVQ g_syscallsp(CX), CX
340 JZ sigtramp // g.m.curg.syscallsp == 0
341 MOVQ m_cgoCallers(AX), R8
343 JZ sigtramp // g.m.cgoCallers == nil
344 MOVL m_cgoCallersUse(AX), CX
346 JNZ sigtramp // g.m.cgoCallersUse != 0
348 // Jump to a function in runtime/cgo.
349 // That function, written in C, will call the user's traceback
350 // function with proper unwind info, and will then call back here.
351 // The first three arguments, and the fifth, are already in registers.
352 // Set the two remaining arguments now.
353 MOVQ runtime·cgoTraceback(SB), CX
354 MOVQ $runtime·sigtramp(SB), R9
355 MOVQ _cgo_callers(SB), AX
359 JMP runtime·sigtramp(SB)
362 // Signal arrived on a non-Go thread. If this is SIGPROF, get a
364 CMPL DI, $27 // 27 == SIGPROF
367 // Lock sigprofCallersUse.
370 MOVQ $runtime·sigprofCallersUse(SB), R11
373 JNZ sigtramp // Skip stack trace if already locked.
375 // Jump to the traceback function in runtime/cgo.
376 // It will call back to sigprofNonGo, via sigprofNonGoWrapper, to convert
377 // the arguments to the Go calling convention.
378 // First three arguments to traceback function are in registers already.
379 MOVQ runtime·cgoTraceback(SB), CX
380 MOVQ $runtime·sigprofCallers(SB), R8
381 MOVQ $runtime·sigprofNonGoWrapper<>(SB), R9
382 MOVQ _cgo_callers(SB), AX
385 TEXT runtime·sysMmap(SB),NOSPLIT,$0
386 MOVQ addr+0(FP), DI // arg 1 addr
387 MOVQ n+8(FP), SI // arg 2 len
388 MOVL prot+16(FP), DX // arg 3 prot
389 MOVL flags+20(FP), R10 // arg 4 flags
390 MOVL fd+24(FP), R8 // arg 5 fid
391 MOVL off+28(FP), R9 // arg 6 offset
403 // Call the function stored in _cgo_mmap using the GCC calling convention.
404 // This must be called on the system stack.
405 TEXT runtime·callCgoMmap(SB),NOSPLIT,$16
409 MOVL flags+20(FP), CX
412 MOVQ _cgo_mmap(SB), AX
414 ANDQ $~15, SP // alignment as per amd64 psABI
421 TEXT runtime·sysMunmap(SB),NOSPLIT,$0
422 MOVQ addr+0(FP), DI // arg 1 addr
423 MOVQ n+8(FP), SI // arg 2 len
427 MOVL $0xf1, 0xf1 // crash
430 // Call the function stored in _cgo_munmap using the GCC calling convention.
431 // This must be called on the system stack.
432 TEXT runtime·callCgoMunmap(SB),NOSPLIT,$16-16
435 MOVQ _cgo_munmap(SB), AX
437 ANDQ $~15, SP // alignment as per amd64 psABI
443 TEXT runtime·madvise(SB),NOSPLIT,$0
446 MOVL flags+16(FP), DX
447 MOVQ $SYS_madvise, AX
454 TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
457 MOVQ $SYS_sigaltstack, AX
460 MOVL $0xf1, 0xf1 // crash
463 TEXT runtime·usleep(SB),NOSPLIT,$16
468 MOVQ AX, 0(SP) // tv_sec
471 MOVQ AX, 8(SP) // tv_nsec
473 MOVQ SP, DI // arg 1 - rqtp
474 MOVQ $0, SI // arg 2 - rmtp
475 MOVL $SYS_nanosleep, AX
479 // set tls base to DI
480 TEXT runtime·settls(SB),NOSPLIT,$8
481 ADDQ $8, DI // adjust for ELF: wants to use -8(FS) for g and m
484 MOVQ $AMD64_SET_FSBASE, DI
485 MOVQ $SYS_sysarch, AX
488 MOVL $0xf1, 0xf1 // crash
491 TEXT runtime·sysctl(SB),NOSPLIT,$0
492 MOVQ mib+0(FP), DI // arg 1 - name
493 MOVL miblen+8(FP), SI // arg 2 - namelen
494 MOVQ out+16(FP), DX // arg 3 - oldp
495 MOVQ size+24(FP), R10 // arg 4 - oldlenp
496 MOVQ dst+32(FP), R8 // arg 5 - newp
497 MOVQ ndst+40(FP), R9 // arg 6 - newlen
498 MOVQ $SYS___sysctl, AX
508 TEXT runtime·osyield(SB),NOSPLIT,$-4
509 MOVL $SYS_sched_yield, AX
513 TEXT runtime·sigprocmask(SB),NOSPLIT,$0
514 MOVL how+0(FP), DI // arg 1 - how
515 MOVQ new+8(FP), SI // arg 2 - set
516 MOVQ old+16(FP), DX // arg 3 - oset
517 MOVL $SYS_sigprocmask, AX
520 MOVL $0xf1, 0xf1 // crash
523 // int32 runtime·kqueue(void);
524 TEXT runtime·kqueue(SB),NOSPLIT,$0
535 // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout);
536 TEXT runtime·kevent(SB),NOSPLIT,$0
550 // func fcntl(fd, cmd, arg int32) (int32, int32)
551 TEXT runtime·fcntl(SB),NOSPLIT,$0
552 MOVL fd+0(FP), DI // fd
553 MOVL cmd+4(FP), SI // cmd
554 MOVL arg+8(FP), DX // arg
559 MOVL AX, errno+20(FP)
563 MOVL $0, errno+20(FP)
566 // func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
567 TEXT runtime·cpuset_getaffinity(SB), NOSPLIT, $0-44
571 MOVQ size+24(FP), R10
573 MOVL $SYS_cpuset_getaffinity, AX
580 // func issetugid() int32
581 TEXT runtime·issetugid(SB),NOSPLIT,$0
585 MOVL $SYS_issetugid, AX