Text file src/runtime/sys_freebsd_amd64.s

     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  // System calls and other sys.stuff for AMD64, FreeBSD
     6  // /usr/src/sys/kern/syscalls.master for syscall numbers.
     7  //
     8  
     9  #include "go_asm.h"
    10  #include "go_tls.h"
    11  #include "textflag.h"
    12  #include "cgo/abi_amd64.h"
    13  
    14  TEXT runtime·sys_umtx_op(SB),NOSPLIT,$0
    15  	MOVQ addr+0(FP), DI
    16  	MOVL mode+8(FP), SI
    17  	MOVL val+12(FP), DX
    18  	MOVQ uaddr1+16(FP), R10
    19  	MOVQ ut+24(FP), R8
    20  	MOVL $454, AX
    21  	SYSCALL
    22  	JCC	2(PC)
    23  	NEGQ	AX
    24  	MOVL	AX, ret+32(FP)
    25  	RET
    26  
    27  TEXT runtime·thr_new(SB),NOSPLIT,$0
    28  	MOVQ param+0(FP), DI
    29  	MOVL size+8(FP), SI
    30  	MOVL $455, AX
    31  	SYSCALL
    32  	JCC	2(PC)
    33  	NEGQ	AX
    34  	MOVL	AX, ret+16(FP)
    35  	RET
    36  
    37  TEXT runtime·thr_start(SB),NOSPLIT,$0
    38  	MOVQ	DI, R13 // m
    39  
    40  	// set up FS to point at m->tls
    41  	LEAQ	m_tls(R13), DI
    42  	CALL	runtime·settls(SB)	// smashes DI
    43  
    44  	// set up m, g
    45  	get_tls(CX)
    46  	MOVQ	m_g0(R13), DI
    47  	MOVQ	R13, g_m(DI)
    48  	MOVQ	DI, g(CX)
    49  
    50  	CALL	runtime·stackcheck(SB)
    51  	CALL	runtime·mstart(SB)
    52  
    53  	MOVQ 0, AX			// crash (not reached)
    54  
    55  // Exit the entire program (like C exit)
    56  TEXT runtime·exit(SB),NOSPLIT,$-8
    57  	MOVL	code+0(FP), DI		// arg 1 exit status
    58  	MOVL	$1, AX
    59  	SYSCALL
    60  	MOVL	$0xf1, 0xf1  // crash
    61  	RET
    62  
    63  // func exitThread(wait *uint32)
    64  TEXT runtime·exitThread(SB),NOSPLIT,$0-8
    65  	MOVQ	wait+0(FP), AX
    66  	// We're done using the stack.
    67  	MOVL	$0, (AX)
    68  	MOVL	$0, DI		// arg 1 long *state
    69  	MOVL	$431, AX	// thr_exit
    70  	SYSCALL
    71  	MOVL	$0xf1, 0xf1  // crash
    72  	JMP	0(PC)
    73  
    74  TEXT runtime·open(SB),NOSPLIT,$-8
    75  	MOVQ	name+0(FP), DI		// arg 1 pathname
    76  	MOVL	mode+8(FP), SI		// arg 2 flags
    77  	MOVL	perm+12(FP), DX		// arg 3 mode
    78  	MOVL	$5, AX
    79  	SYSCALL
    80  	JCC	2(PC)
    81  	MOVL	$-1, AX
    82  	MOVL	AX, ret+16(FP)
    83  	RET
    84  
    85  TEXT runtime·closefd(SB),NOSPLIT,$-8
    86  	MOVL	fd+0(FP), DI		// arg 1 fd
    87  	MOVL	$6, AX
    88  	SYSCALL
    89  	JCC	2(PC)
    90  	MOVL	$-1, AX
    91  	MOVL	AX, ret+8(FP)
    92  	RET
    93  
    94  TEXT runtime·read(SB),NOSPLIT,$-8
    95  	MOVL	fd+0(FP), DI		// arg 1 fd
    96  	MOVQ	p+8(FP), SI		// arg 2 buf
    97  	MOVL	n+16(FP), DX		// arg 3 count
    98  	MOVL	$3, AX
    99  	SYSCALL
   100  	JCC	2(PC)
   101  	NEGQ	AX			// caller expects negative errno
   102  	MOVL	AX, ret+24(FP)
   103  	RET
   104  
   105  // func pipe2(flags int32) (r, w int32, errno int32)
   106  TEXT runtime·pipe2(SB),NOSPLIT,$0-20
   107  	LEAQ	r+8(FP), DI
   108  	MOVL	flags+0(FP), SI
   109  	MOVL	$542, AX
   110  	SYSCALL
   111  	JCC	2(PC)
   112  	NEGQ	AX
   113  	MOVL	AX, errno+16(FP)
   114  	RET
   115  
   116  TEXT runtime·write1(SB),NOSPLIT,$-8
   117  	MOVQ	fd+0(FP), DI		// arg 1 fd
   118  	MOVQ	p+8(FP), SI		// arg 2 buf
   119  	MOVL	n+16(FP), DX		// arg 3 count
   120  	MOVL	$4, AX
   121  	SYSCALL
   122  	JCC	2(PC)
   123  	NEGQ	AX			// caller expects negative errno
   124  	MOVL	AX, ret+24(FP)
   125  	RET
   126  
   127  TEXT runtime·thr_self(SB),NOSPLIT,$0-8
   128  	// thr_self(&0(FP))
   129  	LEAQ	ret+0(FP), DI	// arg 1
   130  	MOVL	$432, AX
   131  	SYSCALL
   132  	RET
   133  
   134  TEXT runtime·thr_kill(SB),NOSPLIT,$0-16
   135  	// thr_kill(tid, sig)
   136  	MOVQ	tid+0(FP), DI	// arg 1 id
   137  	MOVQ	sig+8(FP), SI	// arg 2 sig
   138  	MOVL	$433, AX
   139  	SYSCALL
   140  	RET
   141  
   142  TEXT runtime·raiseproc(SB),NOSPLIT,$0
   143  	// getpid
   144  	MOVL	$20, AX
   145  	SYSCALL
   146  	// kill(self, sig)
   147  	MOVQ	AX, DI		// arg 1 pid
   148  	MOVL	sig+0(FP), SI	// arg 2 sig
   149  	MOVL	$37, AX
   150  	SYSCALL
   151  	RET
   152  
   153  TEXT runtime·setitimer(SB), NOSPLIT, $-8
   154  	MOVL	mode+0(FP), DI
   155  	MOVQ	new+8(FP), SI
   156  	MOVQ	old+16(FP), DX
   157  	MOVL	$83, AX
   158  	SYSCALL
   159  	RET
   160  
   161  // func fallback_walltime() (sec int64, nsec int32)
   162  TEXT runtime·fallback_walltime(SB), NOSPLIT, $32-12
   163  	MOVL	$232, AX	// clock_gettime
   164  	MOVQ	$0, DI		// CLOCK_REALTIME
   165  	LEAQ	8(SP), SI
   166  	SYSCALL
   167  	MOVQ	8(SP), AX	// sec
   168  	MOVQ	16(SP), DX	// nsec
   169  
   170  	// sec is in AX, nsec in DX
   171  	MOVQ	AX, sec+0(FP)
   172  	MOVL	DX, nsec+8(FP)
   173  	RET
   174  
   175  TEXT runtime·fallback_nanotime(SB), NOSPLIT, $32-8
   176  	MOVL	$232, AX
   177  	MOVQ	$4, DI		// CLOCK_MONOTONIC
   178  	LEAQ	8(SP), SI
   179  	SYSCALL
   180  	MOVQ	8(SP), AX	// sec
   181  	MOVQ	16(SP), DX	// nsec
   182  
   183  	// sec is in AX, nsec in DX
   184  	// return nsec in AX
   185  	IMULQ	$1000000000, AX
   186  	ADDQ	DX, AX
   187  	MOVQ	AX, ret+0(FP)
   188  	RET
   189  
   190  TEXT runtime·asmSigaction(SB),NOSPLIT,$0
   191  	MOVQ	sig+0(FP), DI		// arg 1 sig
   192  	MOVQ	new+8(FP), SI		// arg 2 act
   193  	MOVQ	old+16(FP), DX		// arg 3 oact
   194  	MOVL	$416, AX
   195  	SYSCALL
   196  	JCC	2(PC)
   197  	MOVL	$-1, AX
   198  	MOVL	AX, ret+24(FP)
   199  	RET
   200  
   201  TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
   202  	MOVQ	sig+0(FP), DI		// arg 1 sig
   203  	MOVQ	new+8(FP), SI		// arg 2 act
   204  	MOVQ	old+16(FP), DX		// arg 3 oact
   205  	MOVQ	_cgo_sigaction(SB), AX
   206  	MOVQ	SP, BX			// callee-saved
   207  	ANDQ	$~15, SP		// alignment as per amd64 psABI
   208  	CALL	AX
   209  	MOVQ	BX, SP
   210  	MOVL	AX, ret+24(FP)
   211  	RET
   212  
   213  TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
   214  	MOVQ	fn+0(FP),    AX
   215  	MOVL	sig+8(FP),   DI
   216  	MOVQ	info+16(FP), SI
   217  	MOVQ	ctx+24(FP),  DX
   218  	PUSHQ	BP
   219  	MOVQ	SP, BP
   220  	ANDQ	$~15, SP     // alignment for x86_64 ABI
   221  	CALL	AX
   222  	MOVQ	BP, SP
   223  	POPQ	BP
   224  	RET
   225  
   226  // Called using C ABI.
   227  TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$0
   228  	// Transition from C ABI to Go ABI.
   229  	PUSH_REGS_HOST_TO_ABI0()
   230  
   231  	// Set up ABIInternal environment: g in R14, cleared X15.
   232  	get_tls(R12)
   233  	MOVQ	g(R12), R14
   234  	PXOR	X15, X15
   235  
   236  	// Reserve space for spill slots.
   237  	NOP	SP		// disable vet stack checking
   238  	ADJSP   $24
   239  
   240  	// Call into the Go signal handler
   241  	MOVQ	DI, AX	// sig
   242  	MOVQ	SI, BX	// info
   243  	MOVQ	DX, CX	// ctx
   244  	CALL	·sigtrampgo<ABIInternal>(SB)
   245  
   246  	ADJSP	$-24
   247  
   248          POP_REGS_HOST_TO_ABI0()
   249  	RET
   250  
   251  // Called using C ABI.
   252  TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT,$0
   253  	// Transition from C ABI to Go ABI.
   254  	PUSH_REGS_HOST_TO_ABI0()
   255  
   256  	// Set up ABIInternal environment: g in R14, cleared X15.
   257  	get_tls(R12)
   258  	MOVQ	g(R12), R14
   259  	PXOR	X15, X15
   260  
   261  	// Reserve space for spill slots.
   262  	NOP	SP		// disable vet stack checking
   263  	ADJSP   $24
   264  
   265  	// Call into the Go signal handler
   266  	MOVQ	DI, AX	// sig
   267  	MOVQ	SI, BX	// info
   268  	MOVQ	DX, CX	// ctx
   269  	CALL	·sigprofNonGo<ABIInternal>(SB)
   270  
   271  	ADJSP	$-24
   272  
   273  	POP_REGS_HOST_TO_ABI0()
   274  	RET
   275  
   276  // Used instead of sigtramp in programs that use cgo.
   277  // Arguments from kernel are in DI, SI, DX.
   278  TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
   279  	// If no traceback function, do usual sigtramp.
   280  	MOVQ	runtime·cgoTraceback(SB), AX
   281  	TESTQ	AX, AX
   282  	JZ	sigtramp
   283  
   284  	// If no traceback support function, which means that
   285  	// runtime/cgo was not linked in, do usual sigtramp.
   286  	MOVQ	_cgo_callers(SB), AX
   287  	TESTQ	AX, AX
   288  	JZ	sigtramp
   289  
   290  	// Figure out if we are currently in a cgo call.
   291  	// If not, just do usual sigtramp.
   292  	get_tls(CX)
   293  	MOVQ	g(CX),AX
   294  	TESTQ	AX, AX
   295  	JZ	sigtrampnog     // g == nil
   296  	MOVQ	g_m(AX), AX
   297  	TESTQ	AX, AX
   298  	JZ	sigtramp        // g.m == nil
   299  	MOVL	m_ncgo(AX), CX
   300  	TESTL	CX, CX
   301  	JZ	sigtramp        // g.m.ncgo == 0
   302  	MOVQ	m_curg(AX), CX
   303  	TESTQ	CX, CX
   304  	JZ	sigtramp        // g.m.curg == nil
   305  	MOVQ	g_syscallsp(CX), CX
   306  	TESTQ	CX, CX
   307  	JZ	sigtramp        // g.m.curg.syscallsp == 0
   308  	MOVQ	m_cgoCallers(AX), R8
   309  	TESTQ	R8, R8
   310  	JZ	sigtramp        // g.m.cgoCallers == nil
   311  	MOVL	m_cgoCallersUse(AX), CX
   312  	TESTL	CX, CX
   313  	JNZ	sigtramp	// g.m.cgoCallersUse != 0
   314  
   315  	// Jump to a function in runtime/cgo.
   316  	// That function, written in C, will call the user's traceback
   317  	// function with proper unwind info, and will then call back here.
   318  	// The first three arguments, and the fifth, are already in registers.
   319  	// Set the two remaining arguments now.
   320  	MOVQ	runtime·cgoTraceback(SB), CX
   321  	MOVQ	$runtime·sigtramp(SB), R9
   322  	MOVQ	_cgo_callers(SB), AX
   323  	JMP	AX
   324  
   325  sigtramp:
   326  	JMP	runtime·sigtramp(SB)
   327  
   328  sigtrampnog:
   329  	// Signal arrived on a non-Go thread. If this is SIGPROF, get a
   330  	// stack trace.
   331  	CMPL	DI, $27 // 27 == SIGPROF
   332  	JNZ	sigtramp
   333  
   334  	// Lock sigprofCallersUse.
   335  	MOVL	$0, AX
   336  	MOVL	$1, CX
   337  	MOVQ	$runtime·sigprofCallersUse(SB), R11
   338  	LOCK
   339  	CMPXCHGL	CX, 0(R11)
   340  	JNZ	sigtramp  // Skip stack trace if already locked.
   341  
   342  	// Jump to the traceback function in runtime/cgo.
   343  	// It will call back to sigprofNonGo, via sigprofNonGoWrapper, to convert
   344  	// the arguments to the Go calling convention.
   345  	// First three arguments to traceback function are in registers already.
   346  	MOVQ	runtime·cgoTraceback(SB), CX
   347  	MOVQ	$runtime·sigprofCallers(SB), R8
   348  	MOVQ	$runtime·sigprofNonGoWrapper<>(SB), R9
   349  	MOVQ	_cgo_callers(SB), AX
   350  	JMP	AX
   351  
   352  TEXT runtime·mmap(SB),NOSPLIT,$0
   353  	MOVQ	addr+0(FP), DI		// arg 1 addr
   354  	MOVQ	n+8(FP), SI		// arg 2 len
   355  	MOVL	prot+16(FP), DX		// arg 3 prot
   356  	MOVL	flags+20(FP), R10		// arg 4 flags
   357  	MOVL	fd+24(FP), R8		// arg 5 fid
   358  	MOVL	off+28(FP), R9		// arg 6 offset
   359  	MOVL	$477, AX
   360  	SYSCALL
   361  	JCC	ok
   362  	MOVQ	$0, p+32(FP)
   363  	MOVQ	AX, err+40(FP)
   364  	RET
   365  ok:
   366  	MOVQ	AX, p+32(FP)
   367  	MOVQ	$0, err+40(FP)
   368  	RET
   369  
   370  TEXT runtime·munmap(SB),NOSPLIT,$0
   371  	MOVQ	addr+0(FP), DI		// arg 1 addr
   372  	MOVQ	n+8(FP), SI		// arg 2 len
   373  	MOVL	$73, AX
   374  	SYSCALL
   375  	JCC	2(PC)
   376  	MOVL	$0xf1, 0xf1  // crash
   377  	RET
   378  
   379  TEXT runtime·madvise(SB),NOSPLIT,$0
   380  	MOVQ	addr+0(FP), DI
   381  	MOVQ	n+8(FP), SI
   382  	MOVL	flags+16(FP), DX
   383  	MOVQ	$75, AX	// madvise
   384  	SYSCALL
   385  	JCC	2(PC)
   386  	MOVL	$-1, AX
   387  	MOVL	AX, ret+24(FP)
   388  	RET
   389  
   390  TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
   391  	MOVQ	new+0(FP), DI
   392  	MOVQ	old+8(FP), SI
   393  	MOVQ	$53, AX
   394  	SYSCALL
   395  	JCC	2(PC)
   396  	MOVL	$0xf1, 0xf1  // crash
   397  	RET
   398  
   399  TEXT runtime·usleep(SB),NOSPLIT,$16
   400  	MOVL	$0, DX
   401  	MOVL	usec+0(FP), AX
   402  	MOVL	$1000000, CX
   403  	DIVL	CX
   404  	MOVQ	AX, 0(SP)		// tv_sec
   405  	MOVL	$1000, AX
   406  	MULL	DX
   407  	MOVQ	AX, 8(SP)		// tv_nsec
   408  
   409  	MOVQ	SP, DI			// arg 1 - rqtp
   410  	MOVQ	$0, SI			// arg 2 - rmtp
   411  	MOVL	$240, AX		// sys_nanosleep
   412  	SYSCALL
   413  	RET
   414  
   415  // set tls base to DI
   416  TEXT runtime·settls(SB),NOSPLIT,$8
   417  	ADDQ	$8, DI	// adjust for ELF: wants to use -8(FS) for g and m
   418  	MOVQ	DI, 0(SP)
   419  	MOVQ	SP, SI
   420  	MOVQ	$129, DI	// AMD64_SET_FSBASE
   421  	MOVQ	$165, AX	// sysarch
   422  	SYSCALL
   423  	JCC	2(PC)
   424  	MOVL	$0xf1, 0xf1  // crash
   425  	RET
   426  
   427  TEXT runtime·sysctl(SB),NOSPLIT,$0
   428  	MOVQ	mib+0(FP), DI		// arg 1 - name
   429  	MOVL	miblen+8(FP), SI		// arg 2 - namelen
   430  	MOVQ	out+16(FP), DX		// arg 3 - oldp
   431  	MOVQ	size+24(FP), R10		// arg 4 - oldlenp
   432  	MOVQ	dst+32(FP), R8		// arg 5 - newp
   433  	MOVQ	ndst+40(FP), R9		// arg 6 - newlen
   434  	MOVQ	$202, AX		// sys___sysctl
   435  	SYSCALL
   436  	JCC 4(PC)
   437  	NEGQ	AX
   438  	MOVL	AX, ret+48(FP)
   439  	RET
   440  	MOVL	$0, AX
   441  	MOVL	AX, ret+48(FP)
   442  	RET
   443  
   444  TEXT runtime·osyield(SB),NOSPLIT,$-4
   445  	MOVL	$331, AX		// sys_sched_yield
   446  	SYSCALL
   447  	RET
   448  
   449  TEXT runtime·sigprocmask(SB),NOSPLIT,$0
   450  	MOVL	how+0(FP), DI		// arg 1 - how
   451  	MOVQ	new+8(FP), SI		// arg 2 - set
   452  	MOVQ	old+16(FP), DX		// arg 3 - oset
   453  	MOVL	$340, AX		// sys_sigprocmask
   454  	SYSCALL
   455  	JAE	2(PC)
   456  	MOVL	$0xf1, 0xf1  // crash
   457  	RET
   458  
   459  // int32 runtime·kqueue(void);
   460  TEXT runtime·kqueue(SB),NOSPLIT,$0
   461  	MOVQ	$0, DI
   462  	MOVQ	$0, SI
   463  	MOVQ	$0, DX
   464  	MOVL	$362, AX
   465  	SYSCALL
   466  	JCC	2(PC)
   467  	NEGQ	AX
   468  	MOVL	AX, ret+0(FP)
   469  	RET
   470  
   471  // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout);
   472  TEXT runtime·kevent(SB),NOSPLIT,$0
   473  	MOVL	kq+0(FP), DI
   474  	MOVQ	ch+8(FP), SI
   475  	MOVL	nch+16(FP), DX
   476  	MOVQ	ev+24(FP), R10
   477  	MOVL	nev+32(FP), R8
   478  	MOVQ	ts+40(FP), R9
   479  	MOVL	$363, AX
   480  	SYSCALL
   481  	JCC	2(PC)
   482  	NEGQ	AX
   483  	MOVL	AX, ret+48(FP)
   484  	RET
   485  
   486  // void runtime·closeonexec(int32 fd);
   487  TEXT runtime·closeonexec(SB),NOSPLIT,$0
   488  	MOVL	fd+0(FP), DI	// fd
   489  	MOVQ	$2, SI		// F_SETFD
   490  	MOVQ	$1, DX		// FD_CLOEXEC
   491  	MOVL	$92, AX		// fcntl
   492  	SYSCALL
   493  	RET
   494  
   495  // func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
   496  TEXT runtime·cpuset_getaffinity(SB), NOSPLIT, $0-44
   497  	MOVQ	level+0(FP), DI
   498  	MOVQ	which+8(FP), SI
   499  	MOVQ	id+16(FP), DX
   500  	MOVQ	size+24(FP), R10
   501  	MOVQ	mask+32(FP), R8
   502  	MOVL	$487, AX
   503  	SYSCALL
   504  	JCC	2(PC)
   505  	NEGQ	AX
   506  	MOVL	AX, ret+40(FP)
   507  	RET
   508  

View as plain text