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, NetBSD
      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 // int32 lwp_create(void *context, uintptr flags, void *lwpid)
     14 TEXT runtimelwp_create(SB),NOSPLIT,$0
     15 	MOVQ	ctxt+0(FP), DI
     16 	MOVQ	flags+8(FP), SI
     17 	MOVQ	lwpid+16(FP), DX
     18 	MOVL	$309, AX		// sys__lwp_create
     19 	SYSCALL
     20 	JCC	2(PC)
     21 	NEGQ	AX
     22 	MOVL	AX, ret+24(FP)
     23 	RET
     24 
     25 TEXT runtimelwp_tramp(SB),NOSPLIT,$0
     26 
     27 	// Set FS to point at m->tls.
     28 	LEAQ	m_tls(R8), DI
     29 	CALL	runtimesettls(SB)
     30 
     31 	// Set up new stack.
     32 	get_tls(CX)
     33 	MOVQ	R8, g_m(R9)
     34 	MOVQ	R9, g(CX)
     35 	CALL	runtimestackcheck(SB)
     36 
     37 	// Call fn
     38 	CALL	R12
     39 
     40 	// It shouldn't return.  If it does, exit.
     41 	MOVL	$310, AX		// sys__lwp_exit
     42 	SYSCALL
     43 	JMP	-3(PC)			// keep exiting
     44 
     45 TEXT runtimeosyield(SB),NOSPLIT,$0
     46 	MOVL	$350, AX		// sys_sched_yield
     47 	SYSCALL
     48 	RET
     49 
     50 TEXT runtimelwp_park(SB),NOSPLIT,$0
     51 	MOVQ	abstime+0(FP), DI		// arg 1 - abstime
     52 	MOVL	unpark+8(FP), SI		// arg 2 - unpark
     53 	MOVQ	hint+16(FP), DX		// arg 3 - hint
     54 	MOVQ	unparkhint+24(FP), R10		// arg 4 - unparkhint
     55 	MOVL	$434, AX		// sys__lwp_park
     56 	SYSCALL
     57 	MOVL	AX, ret+32(FP)
     58 	RET
     59 
     60 TEXT runtimelwp_unpark(SB),NOSPLIT,$0
     61 	MOVL	lwp+0(FP), DI		// arg 1 - lwp
     62 	MOVQ	hint+8(FP), SI		// arg 2 - hint
     63 	MOVL	$321, AX		// sys__lwp_unpark
     64 	SYSCALL
     65 	MOVL	AX, ret+16(FP)
     66 	RET
     67 
     68 TEXT runtimelwp_self(SB),NOSPLIT,$0
     69 	MOVL	$311, AX		// sys__lwp_self
     70 	SYSCALL
     71 	MOVL	AX, ret+0(FP)
     72 	RET
     73 
     74 // Exit the entire program (like C exit)
     75 TEXT runtimeexit(SB),NOSPLIT,$-8
     76 	MOVL	code+0(FP), DI		// arg 1 - exit status
     77 	MOVL	$1, AX			// sys_exit
     78 	SYSCALL
     79 	MOVL	$0xf1, 0xf1		// crash
     80 	RET
     81 
     82 TEXT runtimeexit1(SB),NOSPLIT,$-8
     83 	MOVL	$310, AX		// sys__lwp_exit
     84 	SYSCALL
     85 	MOVL	$0xf1, 0xf1		// crash
     86 	RET
     87 
     88 TEXT runtimeopen(SB),NOSPLIT,$-8
     89 	MOVQ	name+0(FP), DI		// arg 1 pathname
     90 	MOVL	mode+8(FP), SI		// arg 2 flags
     91 	MOVL	perm+12(FP), DX		// arg 3 mode
     92 	MOVL	$5, AX
     93 	SYSCALL
     94 	JCC	2(PC)
     95 	MOVL	$-1, AX
     96 	MOVL	AX, ret+16(FP)
     97 	RET
     98 
     99 TEXT runtimeclosefd(SB),NOSPLIT,$-8
    100 	MOVL	fd+0(FP), DI		// arg 1 fd
    101 	MOVL	$6, AX
    102 	SYSCALL
    103 	JCC	2(PC)
    104 	MOVL	$-1, AX
    105 	MOVL	AX, ret+8(FP)
    106 	RET
    107 
    108 TEXT runtimeread(SB),NOSPLIT,$-8
    109 	MOVL	fd+0(FP), DI		// arg 1 fd
    110 	MOVQ	p+8(FP), SI		// arg 2 buf
    111 	MOVL	n+16(FP), DX		// arg 3 count
    112 	MOVL	$3, AX
    113 	SYSCALL
    114 	JCC	2(PC)
    115 	MOVL	$-1, AX
    116 	MOVL	AX, ret+24(FP)
    117 	RET
    118 
    119 TEXT runtimewrite(SB),NOSPLIT,$-8
    120 	MOVQ	fd+0(FP), DI		// arg 1 - fd
    121 	MOVQ	p+8(FP), SI		// arg 2 - buf
    122 	MOVL	n+16(FP), DX		// arg 3 - nbyte
    123 	MOVL	$4, AX			// sys_write
    124 	SYSCALL
    125 	JCC	2(PC)
    126 	MOVL	$-1, AX
    127 	MOVL	AX, ret+24(FP)
    128 	RET
    129 
    130 TEXT runtimeusleep(SB),NOSPLIT,$16
    131 	MOVL	$0, DX
    132 	MOVL	usec+0(FP), AX
    133 	MOVL	$1000000, CX
    134 	DIVL	CX
    135 	MOVQ	AX, 0(SP)		// tv_sec
    136 	MOVL	$1000, AX
    137 	MULL	DX
    138 	MOVQ	AX, 8(SP)		// tv_nsec
    139 
    140 	MOVQ	SP, DI			// arg 1 - rqtp
    141 	MOVQ	$0, SI			// arg 2 - rmtp
    142 	MOVL	$430, AX		// sys_nanosleep
    143 	SYSCALL
    144 	RET
    145 
    146 TEXT runtimeraise(SB),NOSPLIT,$16
    147 	MOVL	$311, AX		// sys__lwp_self
    148 	SYSCALL
    149 	MOVQ	AX, DI			// arg 1 - target
    150 	MOVL	sig+0(FP), SI		// arg 2 - signo
    151 	MOVL	$318, AX		// sys__lwp_kill
    152 	SYSCALL
    153 	RET
    154 
    155 TEXT runtimeraiseproc(SB),NOSPLIT,$16
    156 	MOVL	$20, AX			// sys_getpid
    157 	SYSCALL
    158 	MOVQ	AX, DI			// arg 1 - pid
    159 	MOVL	sig+0(FP), SI		// arg 2 - signo
    160 	MOVL	$37, AX			// sys_kill
    161 	SYSCALL
    162 	RET
    163 
    164 TEXT runtimesetitimer(SB),NOSPLIT,$-8
    165 	MOVL	mode+0(FP), DI		// arg 1 - which
    166 	MOVQ	new+8(FP), SI		// arg 2 - itv
    167 	MOVQ	old+16(FP), DX		// arg 3 - oitv
    168 	MOVL	$425, AX		// sys_setitimer
    169 	SYSCALL
    170 	RET
    171 
    172 // func now() (sec int64, nsec int32)
    173 TEXT timenow(SB), NOSPLIT, $32
    174 	MOVQ	$0, DI			// arg 1 - clock_id
    175 	LEAQ	8(SP), SI		// arg 2 - tp
    176 	MOVL	$427, AX		// sys_clock_gettime
    177 	SYSCALL
    178 	MOVQ	8(SP), AX		// sec
    179 	MOVL	16(SP), DX		// nsec
    180 
    181 	// sec is in AX, nsec in DX
    182 	MOVQ	AX, sec+0(FP)
    183 	MOVL	DX, nsec+8(FP)
    184 	RET
    185 
    186 TEXT runtimenanotime(SB),NOSPLIT,$32
    187 	MOVQ	$0, DI			// arg 1 - clock_id
    188 	LEAQ	8(SP), SI		// arg 2 - tp
    189 	MOVL	$427, AX		// sys_clock_gettime
    190 	SYSCALL
    191 	MOVQ	8(SP), AX		// sec
    192 	MOVL	16(SP), DX		// nsec
    193 
    194 	// sec is in AX, nsec in DX
    195 	// return nsec in AX
    196 	IMULQ	$1000000000, AX
    197 	ADDQ	DX, AX
    198 	MOVQ	AX, ret+0(FP)
    199 	RET
    200 
    201 TEXT runtimegetcontext(SB),NOSPLIT,$-8
    202 	MOVQ	ctxt+0(FP), DI		// arg 1 - context
    203 	MOVL	$307, AX		// sys_getcontext
    204 	SYSCALL
    205 	JCC	2(PC)
    206 	MOVL	$0xf1, 0xf1		// crash
    207 	RET
    208 
    209 TEXT runtimesigprocmask(SB),NOSPLIT,$0
    210 	MOVL	mode+0(FP), DI		// arg 1 - how
    211 	MOVQ	new+8(FP), SI		// arg 2 - set
    212 	MOVQ	old+16(FP), DX		// arg 3 - oset
    213 	MOVL	$293, AX		// sys_sigprocmask
    214 	SYSCALL
    215 	JCC	2(PC)
    216 	MOVL	$0xf1, 0xf1		// crash
    217 	RET
    218 
    219 TEXT runtimesigreturn_tramp(SB),NOSPLIT,$-8
    220 	MOVQ	R15, DI			// Load address of ucontext
    221 	MOVQ	$308, AX		// sys_setcontext
    222 	SYSCALL
    223 	MOVQ	$-1, DI			// Something failed...
    224 	MOVL	$1, AX			// sys_exit
    225 	SYSCALL
    226 
    227 TEXT runtimesigaction(SB),NOSPLIT,$-8
    228 	MOVL	sig+0(FP), DI		// arg 1 - signum
    229 	MOVQ	new+8(FP), SI		// arg 2 - nsa
    230 	MOVQ	old+16(FP), DX		// arg 3 - osa
    231 					// arg 4 - tramp
    232 	LEAQ	runtimesigreturn_tramp(SB), R10
    233 	MOVQ	$2, R8			// arg 5 - vers
    234 	MOVL	$340, AX		// sys___sigaction_sigtramp
    235 	SYSCALL
    236 	JCC	2(PC)
    237 	MOVL	$0xf1, 0xf1		// crash
    238 	RET
    239 
    240 TEXT runtimesigtramp(SB),NOSPLIT,$64
    241 	get_tls(BX)
    242 
    243 	// check that g exists
    244 	MOVQ	g(BX), R10
    245 	CMPQ	R10, $0
    246 	JNE	5(PC)
    247 	MOVQ	DI, 0(SP)
    248 	MOVQ	$runtimebadsignal(SB), AX
    249 	CALL	AX
    250 	RET
    251 
    252 	// save g
    253 	MOVQ	R10, 40(SP)
    254 
    255 	// g = m->signal
    256 	MOVQ	g_m(R10), AX
    257 	MOVQ	m_gsignal(AX), AX
    258 	MOVQ	AX, g(BX)
    259 
    260 	MOVQ	DI, 0(SP)
    261 	MOVQ	SI, 8(SP)
    262 	MOVQ	DX, 16(SP)
    263 	MOVQ	R10, 24(SP)
    264 
    265 	CALL	runtimesighandler(SB)
    266 
    267 	// restore g
    268 	get_tls(BX)
    269 	MOVQ	40(SP), R10
    270 	MOVQ	R10, g(BX)
    271 	RET
    272 
    273 TEXT runtimemmap(SB),NOSPLIT,$0
    274 	MOVQ	addr+0(FP), DI		// arg 1 - addr
    275 	MOVQ	n+8(FP), SI		// arg 2 - len
    276 	MOVL	prot+16(FP), DX		// arg 3 - prot
    277 	MOVL	flags+20(FP), R10		// arg 4 - flags
    278 	MOVL	fd+24(FP), R8		// arg 5 - fd
    279 	MOVL	off+28(FP), R9
    280 	SUBQ	$16, SP
    281 	MOVQ	R9, 8(SP)		// arg 7 - offset (passed on stack)
    282 	MOVQ	$0, R9			// arg 6 - pad
    283 	MOVL	$197, AX		// sys_mmap
    284 	SYSCALL
    285 	ADDQ	$16, SP
    286 	MOVQ	AX, ret+32(FP)
    287 	RET
    288 
    289 TEXT runtimemunmap(SB),NOSPLIT,$0
    290 	MOVQ	addr+0(FP), DI		// arg 1 - addr
    291 	MOVQ	n+8(FP), SI		// arg 2 - len
    292 	MOVL	$73, AX			// sys_munmap
    293 	SYSCALL
    294 	JCC	2(PC)
    295 	MOVL	$0xf1, 0xf1		// crash
    296 	RET
    297 
    298 
    299 TEXT runtimemadvise(SB),NOSPLIT,$0
    300 	MOVQ	addr+0(FP), DI		// arg 1 - addr
    301 	MOVQ	n+8(FP), SI		// arg 2 - len
    302 	MOVL	flags+16(FP), DX	// arg 3 - behav
    303 	MOVQ	$75, AX			// sys_madvise
    304 	SYSCALL
    305 	// ignore failure - maybe pages are locked
    306 	RET
    307 
    308 TEXT runtimesigaltstack(SB),NOSPLIT,$-8
    309 	MOVQ	new+8(SP), DI		// arg 1 - nss
    310 	MOVQ	old+16(SP), SI		// arg 2 - oss
    311 	MOVQ	$281, AX		// sys___sigaltstack14
    312 	SYSCALL
    313 	JCC	2(PC)
    314 	MOVL	$0xf1, 0xf1		// crash
    315 	RET
    316 
    317 // set tls base to DI
    318 TEXT runtimesettls(SB),NOSPLIT,$8
    319 	// adjust for ELF: wants to use -8(FS) for g
    320 	ADDQ	$8, DI			// arg 1 - ptr
    321 	MOVQ	$317, AX		// sys__lwp_setprivate
    322 	SYSCALL
    323 	JCC	2(PC)
    324 	MOVL	$0xf1, 0xf1		// crash
    325 	RET
    326 
    327 TEXT runtimesysctl(SB),NOSPLIT,$0
    328 	MOVQ	mib+0(FP), DI		// arg 1 - name
    329 	MOVL	miblen+8(FP), SI		// arg 2 - namelen
    330 	MOVQ	out+16(FP), DX		// arg 3 - oldp
    331 	MOVQ	size+24(FP), R10		// arg 4 - oldlenp
    332 	MOVQ	dst+32(FP), R8		// arg 5 - newp
    333 	MOVQ	ndst+40(FP), R9		// arg 6 - newlen
    334 	MOVQ	$202, AX		// sys___sysctl
    335 	SYSCALL
    336 	JCC 4(PC)
    337 	NEGQ	AX
    338 	MOVL	AX, ret+48(FP)
    339 	RET
    340 	MOVL	$0, AX
    341 	MOVL	AX, ret+48(FP)
    342 	RET
    343 
    344 // int32 runtimekqueue(void)
    345 TEXT runtimekqueue(SB),NOSPLIT,$0
    346 	MOVQ	$0, DI
    347 	MOVL	$344, AX
    348 	SYSCALL
    349 	JCC	2(PC)
    350 	NEGQ	AX
    351 	MOVL	AX, ret+0(FP)
    352 	RET
    353 
    354 // int32 runtimekevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout)
    355 TEXT runtimekevent(SB),NOSPLIT,$0
    356 	MOVL	fd+0(FP), DI
    357 	MOVQ	ev1+8(FP), SI
    358 	MOVL	nev1+16(FP), DX
    359 	MOVQ	ev2+24(FP), R10
    360 	MOVL	nev2+32(FP), R8
    361 	MOVQ	ts+40(FP), R9
    362 	MOVL	$435, AX
    363 	SYSCALL
    364 	JCC	2(PC)
    365 	NEGQ	AX
    366 	MOVL	AX, ret+48(FP)
    367 	RET
    368 
    369 // void runtimecloseonexec(int32 fd)
    370 TEXT runtimecloseonexec(SB),NOSPLIT,$0
    371 	MOVL	fd+0(FP), DI	// fd
    372 	MOVQ	$2, SI		// F_SETFD
    373 	MOVQ	$1, DX		// FD_CLOEXEC
    374 	MOVL	$92, AX		// fcntl
    375 	SYSCALL
    376 	RET
    377