Home | History | Annotate | Download | only in runtime
      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 
     13 TEXT runtimesys_umtx_sleep(SB),NOSPLIT,$0
     14 	MOVQ addr+0(FP), DI		// arg 1 - ptr
     15 	MOVL val+8(FP), SI		// arg 2 - value
     16 	MOVL timeout+12(FP), DX		// arg 3 - timeout
     17 	MOVL $469, AX		// umtx_sleep
     18 	SYSCALL
     19 	JCC	2(PC)
     20 	NEGQ	AX
     21 	MOVL	AX, ret+16(FP)
     22 	RET
     23 
     24 TEXT runtimesys_umtx_wakeup(SB),NOSPLIT,$0
     25 	MOVQ addr+0(FP), DI		// arg 1 - ptr
     26 	MOVL val+8(FP), SI		// arg 2 - count
     27 	MOVL $470, AX		// umtx_wakeup
     28 	SYSCALL
     29 	JCC	2(PC)
     30 	NEGQ	AX
     31 	MOVL	AX, ret+16(FP)
     32 	RET
     33 
     34 TEXT runtimelwp_create(SB),NOSPLIT,$0
     35 	MOVQ param+0(FP), DI		// arg 1 - params
     36 	MOVL $495, AX		// lwp_create
     37 	SYSCALL
     38 	MOVL	AX, ret+8(FP)
     39 	RET
     40 
     41 TEXT runtimelwp_start(SB),NOSPLIT,$0
     42 	MOVQ	DI, R13 // m
     43 
     44 	// set up FS to point at m->tls
     45 	LEAQ	m_tls(R13), DI
     46 	CALL	runtimesettls(SB)	// smashes DI
     47 
     48 	// set up m, g
     49 	get_tls(CX)
     50 	MOVQ	m_g0(R13), DI
     51 	MOVQ	R13, g_m(DI)
     52 	MOVQ	DI, g(CX)
     53 
     54 	CALL	runtimestackcheck(SB)
     55 	CALL	runtimemstart(SB)
     56 
     57 	MOVQ 0, AX			// crash (not reached)
     58 
     59 // Exit the entire program (like C exit)
     60 TEXT runtimeexit(SB),NOSPLIT,$-8
     61 	MOVL	code+0(FP), DI		// arg 1 exit status
     62 	MOVL	$1, AX
     63 	SYSCALL
     64 	MOVL	$0xf1, 0xf1  // crash
     65 	RET
     66 
     67 TEXT runtimeexit1(SB),NOSPLIT,$-8
     68 	MOVL	code+0(FP), DI		// arg 1 exit status
     69 	MOVL	$431, AX
     70 	SYSCALL
     71 	MOVL	$0xf1, 0xf1  // crash
     72 	RET
     73 
     74 TEXT runtimeopen(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 runtimeclosefd(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 runtimeread(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 	MOVL	$-1, AX
    102 	MOVL	AX, ret+24(FP)
    103 	RET
    104 
    105 TEXT runtimewrite(SB),NOSPLIT,$-8
    106 	MOVQ	fd+0(FP), DI		// arg 1 fd
    107 	MOVQ	p+8(FP), SI		// arg 2 buf
    108 	MOVL	n+16(FP), DX		// arg 3 count
    109 	MOVL	$4, AX
    110 	SYSCALL
    111 	JCC	2(PC)
    112 	MOVL	$-1, AX
    113 	MOVL	AX, ret+24(FP)
    114 	RET
    115 
    116 TEXT runtimegetrlimit(SB),NOSPLIT,$-8
    117 	MOVL	kind+0(FP), DI
    118 	MOVQ	limit+8(FP), SI
    119 	MOVL	$194, AX
    120 	SYSCALL
    121 	MOVL	AX, ret+16(FP)
    122 	RET
    123 
    124 TEXT runtimeraise(SB),NOSPLIT,$16
    125 	MOVL	$496, AX	// lwp_gettid
    126 	SYSCALL
    127 	MOVQ	$-1, DI		// arg 1 - pid
    128 	MOVQ	AX, SI		// arg 2 - tid
    129 	MOVL	sig+0(FP), DX	// arg 3 - signum
    130 	MOVL	$497, AX	// lwp_kill
    131 	SYSCALL
    132 	RET
    133 
    134 TEXT runtimeraiseproc(SB),NOSPLIT,$0
    135 	MOVL	$20, AX		// getpid
    136 	SYSCALL
    137 	MOVQ	AX, DI		// arg 1 - pid
    138 	MOVL	sig+0(FP), SI	// arg 2 - signum
    139 	MOVL	$37, AX		// kill
    140 	SYSCALL
    141 	RET
    142 
    143 TEXT runtimesetitimer(SB), NOSPLIT, $-8
    144 	MOVL	mode+0(FP), DI
    145 	MOVQ	new+8(FP), SI
    146 	MOVQ	old+16(FP), DX
    147 	MOVL	$83, AX
    148 	SYSCALL
    149 	RET
    150 
    151 // func now() (sec int64, nsec int32)
    152 TEXT timenow(SB), NOSPLIT, $32
    153 	MOVL	$232, AX // clock_gettime
    154 	MOVQ	$0, DI  	// CLOCK_REALTIME
    155 	LEAQ	8(SP), SI
    156 	SYSCALL
    157 	MOVQ	8(SP), AX	// sec
    158 	MOVQ	16(SP), DX	// nsec
    159 
    160 	// sec is in AX, nsec in DX
    161 	MOVQ	AX, sec+0(FP)
    162 	MOVL	DX, nsec+8(FP)
    163 	RET
    164 
    165 TEXT runtimenanotime(SB), NOSPLIT, $32
    166 	MOVL	$232, AX
    167 	MOVQ	$4, DI  	// CLOCK_MONOTONIC
    168 	LEAQ	8(SP), SI
    169 	SYSCALL
    170 	MOVQ	8(SP), AX	// sec
    171 	MOVQ	16(SP), DX	// nsec
    172 
    173 	// sec is in AX, nsec in DX
    174 	// return nsec in AX
    175 	IMULQ	$1000000000, AX
    176 	ADDQ	DX, AX
    177 	MOVQ	AX, ret+0(FP)
    178 	RET
    179 
    180 TEXT runtimesigaction(SB),NOSPLIT,$-8
    181 	MOVL	sig+0(FP), DI		// arg 1 sig
    182 	MOVQ	new+8(FP), SI		// arg 2 act
    183 	MOVQ	old+16(FP), DX		// arg 3 oact
    184 	MOVL	$342, AX
    185 	SYSCALL
    186 	JCC	2(PC)
    187 	MOVL	$0xf1, 0xf1  // crash
    188 	RET
    189 
    190 TEXT runtimesigfwd(SB),NOSPLIT,$0-32
    191 	MOVQ	fn+0(FP),    AX
    192 	MOVL	sig+8(FP),   DI
    193 	MOVQ	info+16(FP), SI
    194 	MOVQ	ctx+24(FP),  DX
    195 	PUSHQ	BP
    196 	MOVQ	SP, BP
    197 	ANDQ	$~15, SP     // alignment for x86_64 ABI
    198 	CALL	AX
    199 	MOVQ	BP, SP
    200 	POPQ	BP
    201 	RET
    202 
    203 TEXT runtimesigtramp(SB),NOSPLIT,$72
    204 	// Save callee-saved C registers, since the caller may be a C signal handler.
    205 	MOVQ	BX,  bx-8(SP)
    206 	MOVQ	BP,  bp-16(SP)  // save in case GOEXPERIMENT=noframepointer is set
    207 	MOVQ	R12, r12-24(SP)
    208 	MOVQ	R13, r13-32(SP)
    209 	MOVQ	R14, r14-40(SP)
    210 	MOVQ	R15, r15-48(SP)
    211 	// We don't save mxcsr or the x87 control word because sigtrampgo doesn't
    212 	// modify them.
    213 
    214 	MOVQ	DX, ctx-56(SP)
    215 	MOVQ	SI, info-64(SP)
    216 	MOVQ	DI, signum-72(SP)
    217 	CALL	runtimesigtrampgo(SB)
    218 
    219 	MOVQ	r15-48(SP), R15
    220 	MOVQ	r14-40(SP), R14
    221 	MOVQ	r13-32(SP), R13
    222 	MOVQ	r12-24(SP), R12
    223 	MOVQ	bp-16(SP),  BP
    224 	MOVQ	bx-8(SP),   BX
    225 	RET
    226 
    227 TEXT runtimemmap(SB),NOSPLIT,$0
    228 	MOVQ	addr+0(FP), DI		// arg 1 - addr
    229 	MOVQ	n+8(FP), SI		// arg 2 - len
    230 	MOVL	prot+16(FP), DX		// arg 3 - prot
    231 	MOVL	flags+20(FP), R10		// arg 4 - flags
    232 	MOVL	fd+24(FP), R8		// arg 5 - fd
    233 	MOVL	off+28(FP), R9
    234 	SUBQ	$16, SP
    235 	MOVQ	R9, 8(SP)		// arg 7 - offset (passed on stack)
    236 	MOVQ	$0, R9			// arg 6 - pad
    237 	MOVL	$197, AX
    238 	SYSCALL
    239 	ADDQ	$16, SP
    240 	MOVQ	AX, ret+32(FP)
    241 	RET
    242 
    243 TEXT runtimemunmap(SB),NOSPLIT,$0
    244 	MOVQ	addr+0(FP), DI		// arg 1 addr
    245 	MOVQ	n+8(FP), SI		// arg 2 len
    246 	MOVL	$73, AX
    247 	SYSCALL
    248 	JCC	2(PC)
    249 	MOVL	$0xf1, 0xf1  // crash
    250 	RET
    251 
    252 TEXT runtimemadvise(SB),NOSPLIT,$0
    253 	MOVQ	addr+0(FP), DI
    254 	MOVQ	n+8(FP), SI
    255 	MOVL	flags+16(FP), DX
    256 	MOVQ	$75, AX	// madvise
    257 	SYSCALL
    258 	// ignore failure - maybe pages are locked
    259 	RET
    260 
    261 TEXT runtimesigaltstack(SB),NOSPLIT,$-8
    262 	MOVQ	new+0(FP), DI
    263 	MOVQ	old+8(FP), SI
    264 	MOVQ	$53, AX
    265 	SYSCALL
    266 	JCC	2(PC)
    267 	MOVL	$0xf1, 0xf1  // crash
    268 	RET
    269 
    270 TEXT runtimeusleep(SB),NOSPLIT,$16
    271 	MOVL	$0, DX
    272 	MOVL	usec+0(FP), AX
    273 	MOVL	$1000000, CX
    274 	DIVL	CX
    275 	MOVQ	AX, 0(SP)		// tv_sec
    276 	MOVL	$1000, AX
    277 	MULL	DX
    278 	MOVQ	AX, 8(SP)		// tv_nsec
    279 
    280 	MOVQ	SP, DI			// arg 1 - rqtp
    281 	MOVQ	$0, SI			// arg 2 - rmtp
    282 	MOVL	$240, AX		// sys_nanosleep
    283 	SYSCALL
    284 	RET
    285 
    286 // set tls base to DI
    287 TEXT runtimesettls(SB),NOSPLIT,$16
    288 	ADDQ	$8, DI	// adjust for ELF: wants to use -8(FS) for g
    289 	MOVQ	DI, 0(SP)
    290 	MOVQ	$16, 8(SP)
    291 	MOVQ	$0, DI			// arg 1 - which
    292 	MOVQ	SP, SI			// arg 2 - tls_info
    293 	MOVQ	$16, DX			// arg 3 - infosize
    294 	MOVQ	$472, AX		// set_tls_area
    295 	SYSCALL
    296 	JCC	2(PC)
    297 	MOVL	$0xf1, 0xf1  // crash
    298 	RET
    299 
    300 TEXT runtimesysctl(SB),NOSPLIT,$0
    301 	MOVQ	mib+0(FP), DI		// arg 1 - name
    302 	MOVL	miblen+8(FP), SI		// arg 2 - namelen
    303 	MOVQ	out+16(FP), DX		// arg 3 - oldp
    304 	MOVQ	size+24(FP), R10		// arg 4 - oldlenp
    305 	MOVQ	dst+32(FP), R8		// arg 5 - newp
    306 	MOVQ	ndst+40(FP), R9		// arg 6 - newlen
    307 	MOVQ	$202, AX		// sys___sysctl
    308 	SYSCALL
    309 	JCC 4(PC)
    310 	NEGQ	AX
    311 	MOVL	AX, ret+48(FP)
    312 	RET
    313 	MOVL	$0, AX
    314 	MOVL	AX, ret+48(FP)
    315 	RET
    316 
    317 TEXT runtimeosyield(SB),NOSPLIT,$-4
    318 	MOVL	$331, AX		// sys_sched_yield
    319 	SYSCALL
    320 	RET
    321 
    322 TEXT runtimesigprocmask(SB),NOSPLIT,$0
    323 	MOVL	how+0(FP), DI		// arg 1 - how
    324 	MOVQ	new+8(FP), SI		// arg 2 - set
    325 	MOVQ	old+16(FP), DX		// arg 3 - oset
    326 	MOVL	$340, AX		// sys_sigprocmask
    327 	SYSCALL
    328 	JAE	2(PC)
    329 	MOVL	$0xf1, 0xf1  // crash
    330 	RET
    331 
    332 // int32 runtimekqueue(void);
    333 TEXT runtimekqueue(SB),NOSPLIT,$0
    334 	MOVQ	$0, DI
    335 	MOVQ	$0, SI
    336 	MOVQ	$0, DX
    337 	MOVL	$362, AX
    338 	SYSCALL
    339 	JCC	2(PC)
    340 	NEGQ	AX
    341 	MOVL	AX, ret+0(FP)
    342 	RET
    343 
    344 // int32 runtimekevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout);
    345 TEXT runtimekevent(SB),NOSPLIT,$0
    346 	MOVL	kq+0(FP), DI
    347 	MOVQ	ch+8(FP), SI
    348 	MOVL	nch+16(FP), DX
    349 	MOVQ	ev+24(FP), R10
    350 	MOVL	nev+32(FP), R8
    351 	MOVQ	ts+40(FP), R9
    352 	MOVL	$363, AX
    353 	SYSCALL
    354 	JCC	2(PC)
    355 	NEGQ	AX
    356 	MOVL	AX, ret+48(FP)
    357 	RET
    358 
    359 // void runtimecloseonexec(int32 fd);
    360 TEXT runtimecloseonexec(SB),NOSPLIT,$0
    361 	MOVL	fd+0(FP), DI	// fd
    362 	MOVQ	$2, SI		// F_SETFD
    363 	MOVQ	$1, DX		// FD_CLOEXEC
    364 	MOVL	$92, AX		// fcntl
    365 	SYSCALL
    366 	RET
    367