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 // func exitThread(wait *uint32) 68 TEXT runtimeexitThread(SB),NOSPLIT,$0-8 69 MOVQ wait+0(FP), AX 70 // We're done using the stack. 71 MOVL $0, (AX) 72 MOVL $0x10000, DI // arg 1 how - EXTEXIT_LWP 73 MOVL $0, SI // arg 2 status 74 MOVL $0, DX // arg 3 addr 75 MOVL $494, AX // extexit 76 SYSCALL 77 MOVL $0xf1, 0xf1 // crash 78 JMP 0(PC) 79 80 TEXT runtimeopen(SB),NOSPLIT,$-8 81 MOVQ name+0(FP), DI // arg 1 pathname 82 MOVL mode+8(FP), SI // arg 2 flags 83 MOVL perm+12(FP), DX // arg 3 mode 84 MOVL $5, AX 85 SYSCALL 86 JCC 2(PC) 87 MOVL $-1, AX 88 MOVL AX, ret+16(FP) 89 RET 90 91 TEXT runtimeclosefd(SB),NOSPLIT,$-8 92 MOVL fd+0(FP), DI // arg 1 fd 93 MOVL $6, AX 94 SYSCALL 95 JCC 2(PC) 96 MOVL $-1, AX 97 MOVL AX, ret+8(FP) 98 RET 99 100 TEXT runtimeread(SB),NOSPLIT,$-8 101 MOVL fd+0(FP), DI // arg 1 fd 102 MOVQ p+8(FP), SI // arg 2 buf 103 MOVL n+16(FP), DX // arg 3 count 104 MOVL $3, AX 105 SYSCALL 106 JCC 2(PC) 107 MOVL $-1, AX 108 MOVL AX, ret+24(FP) 109 RET 110 111 TEXT runtimewrite(SB),NOSPLIT,$-8 112 MOVQ fd+0(FP), DI // arg 1 fd 113 MOVQ p+8(FP), SI // arg 2 buf 114 MOVL n+16(FP), DX // arg 3 count 115 MOVL $4, AX 116 SYSCALL 117 JCC 2(PC) 118 MOVL $-1, AX 119 MOVL AX, ret+24(FP) 120 RET 121 122 TEXT runtimegetrlimit(SB),NOSPLIT,$-8 123 MOVL kind+0(FP), DI 124 MOVQ limit+8(FP), SI 125 MOVL $194, AX 126 SYSCALL 127 MOVL AX, ret+16(FP) 128 RET 129 130 TEXT runtimeraise(SB),NOSPLIT,$16 131 MOVL $496, AX // lwp_gettid 132 SYSCALL 133 MOVQ $-1, DI // arg 1 - pid 134 MOVQ AX, SI // arg 2 - tid 135 MOVL sig+0(FP), DX // arg 3 - signum 136 MOVL $497, AX // lwp_kill 137 SYSCALL 138 RET 139 140 TEXT runtimeraiseproc(SB),NOSPLIT,$0 141 MOVL $20, AX // getpid 142 SYSCALL 143 MOVQ AX, DI // arg 1 - pid 144 MOVL sig+0(FP), SI // arg 2 - signum 145 MOVL $37, AX // kill 146 SYSCALL 147 RET 148 149 TEXT runtimesetitimer(SB), NOSPLIT, $-8 150 MOVL mode+0(FP), DI 151 MOVQ new+8(FP), SI 152 MOVQ old+16(FP), DX 153 MOVL $83, AX 154 SYSCALL 155 RET 156 157 // func walltime() (sec int64, nsec int32) 158 TEXT runtimewalltime(SB), NOSPLIT, $32 159 MOVL $232, AX // clock_gettime 160 MOVQ $0, DI // CLOCK_REALTIME 161 LEAQ 8(SP), SI 162 SYSCALL 163 MOVQ 8(SP), AX // sec 164 MOVQ 16(SP), DX // nsec 165 166 // sec is in AX, nsec in DX 167 MOVQ AX, sec+0(FP) 168 MOVL DX, nsec+8(FP) 169 RET 170 171 TEXT runtimenanotime(SB), NOSPLIT, $32 172 MOVL $232, AX 173 MOVQ $4, DI // CLOCK_MONOTONIC 174 LEAQ 8(SP), SI 175 SYSCALL 176 MOVQ 8(SP), AX // sec 177 MOVQ 16(SP), DX // nsec 178 179 // sec is in AX, nsec in DX 180 // return nsec in AX 181 IMULQ $1000000000, AX 182 ADDQ DX, AX 183 MOVQ AX, ret+0(FP) 184 RET 185 186 TEXT runtimesigaction(SB),NOSPLIT,$-8 187 MOVL sig+0(FP), DI // arg 1 sig 188 MOVQ new+8(FP), SI // arg 2 act 189 MOVQ old+16(FP), DX // arg 3 oact 190 MOVL $342, AX 191 SYSCALL 192 JCC 2(PC) 193 MOVL $0xf1, 0xf1 // crash 194 RET 195 196 TEXT runtimesigfwd(SB),NOSPLIT,$0-32 197 MOVQ fn+0(FP), AX 198 MOVL sig+8(FP), DI 199 MOVQ info+16(FP), SI 200 MOVQ ctx+24(FP), DX 201 PUSHQ BP 202 MOVQ SP, BP 203 ANDQ $~15, SP // alignment for x86_64 ABI 204 CALL AX 205 MOVQ BP, SP 206 POPQ BP 207 RET 208 209 TEXT runtimesigtramp(SB),NOSPLIT,$72 210 // Save callee-saved C registers, since the caller may be a C signal handler. 211 MOVQ BX, bx-8(SP) 212 MOVQ BP, bp-16(SP) // save in case GOEXPERIMENT=noframepointer is set 213 MOVQ R12, r12-24(SP) 214 MOVQ R13, r13-32(SP) 215 MOVQ R14, r14-40(SP) 216 MOVQ R15, r15-48(SP) 217 // We don't save mxcsr or the x87 control word because sigtrampgo doesn't 218 // modify them. 219 220 MOVQ DX, ctx-56(SP) 221 MOVQ SI, info-64(SP) 222 MOVQ DI, signum-72(SP) 223 CALL runtimesigtrampgo(SB) 224 225 MOVQ r15-48(SP), R15 226 MOVQ r14-40(SP), R14 227 MOVQ r13-32(SP), R13 228 MOVQ r12-24(SP), R12 229 MOVQ bp-16(SP), BP 230 MOVQ bx-8(SP), BX 231 RET 232 233 TEXT runtimemmap(SB),NOSPLIT,$0 234 MOVQ addr+0(FP), DI // arg 1 - addr 235 MOVQ n+8(FP), SI // arg 2 - len 236 MOVL prot+16(FP), DX // arg 3 - prot 237 MOVL flags+20(FP), R10 // arg 4 - flags 238 MOVL fd+24(FP), R8 // arg 5 - fd 239 MOVL off+28(FP), R9 240 SUBQ $16, SP 241 MOVQ R9, 8(SP) // arg 7 - offset (passed on stack) 242 MOVQ $0, R9 // arg 6 - pad 243 MOVL $197, AX 244 SYSCALL 245 JCC ok 246 ADDQ $16, SP 247 MOVQ $0, p+32(FP) 248 MOVQ AX, err+40(FP) 249 RET 250 ok: 251 ADDQ $16, SP 252 MOVQ AX, p+32(FP) 253 MOVQ $0, err+40(FP) 254 RET 255 256 TEXT runtimemunmap(SB),NOSPLIT,$0 257 MOVQ addr+0(FP), DI // arg 1 addr 258 MOVQ n+8(FP), SI // arg 2 len 259 MOVL $73, AX 260 SYSCALL 261 JCC 2(PC) 262 MOVL $0xf1, 0xf1 // crash 263 RET 264 265 TEXT runtimemadvise(SB),NOSPLIT,$0 266 MOVQ addr+0(FP), DI 267 MOVQ n+8(FP), SI 268 MOVL flags+16(FP), DX 269 MOVQ $75, AX // madvise 270 SYSCALL 271 // ignore failure - maybe pages are locked 272 RET 273 274 TEXT runtimesigaltstack(SB),NOSPLIT,$-8 275 MOVQ new+0(FP), DI 276 MOVQ old+8(FP), SI 277 MOVQ $53, AX 278 SYSCALL 279 JCC 2(PC) 280 MOVL $0xf1, 0xf1 // crash 281 RET 282 283 TEXT runtimeusleep(SB),NOSPLIT,$16 284 MOVL $0, DX 285 MOVL usec+0(FP), AX 286 MOVL $1000000, CX 287 DIVL CX 288 MOVQ AX, 0(SP) // tv_sec 289 MOVL $1000, AX 290 MULL DX 291 MOVQ AX, 8(SP) // tv_nsec 292 293 MOVQ SP, DI // arg 1 - rqtp 294 MOVQ $0, SI // arg 2 - rmtp 295 MOVL $240, AX // sys_nanosleep 296 SYSCALL 297 RET 298 299 // set tls base to DI 300 TEXT runtimesettls(SB),NOSPLIT,$16 301 ADDQ $8, DI // adjust for ELF: wants to use -8(FS) for g 302 MOVQ DI, 0(SP) 303 MOVQ $16, 8(SP) 304 MOVQ $0, DI // arg 1 - which 305 MOVQ SP, SI // arg 2 - tls_info 306 MOVQ $16, DX // arg 3 - infosize 307 MOVQ $472, AX // set_tls_area 308 SYSCALL 309 JCC 2(PC) 310 MOVL $0xf1, 0xf1 // crash 311 RET 312 313 TEXT runtimesysctl(SB),NOSPLIT,$0 314 MOVQ mib+0(FP), DI // arg 1 - name 315 MOVL miblen+8(FP), SI // arg 2 - namelen 316 MOVQ out+16(FP), DX // arg 3 - oldp 317 MOVQ size+24(FP), R10 // arg 4 - oldlenp 318 MOVQ dst+32(FP), R8 // arg 5 - newp 319 MOVQ ndst+40(FP), R9 // arg 6 - newlen 320 MOVQ $202, AX // sys___sysctl 321 SYSCALL 322 JCC 4(PC) 323 NEGQ AX 324 MOVL AX, ret+48(FP) 325 RET 326 MOVL $0, AX 327 MOVL AX, ret+48(FP) 328 RET 329 330 TEXT runtimeosyield(SB),NOSPLIT,$-4 331 MOVL $331, AX // sys_sched_yield 332 SYSCALL 333 RET 334 335 TEXT runtimesigprocmask(SB),NOSPLIT,$0 336 MOVL how+0(FP), DI // arg 1 - how 337 MOVQ new+8(FP), SI // arg 2 - set 338 MOVQ old+16(FP), DX // arg 3 - oset 339 MOVL $340, AX // sys_sigprocmask 340 SYSCALL 341 JAE 2(PC) 342 MOVL $0xf1, 0xf1 // crash 343 RET 344 345 // int32 runtimekqueue(void); 346 TEXT runtimekqueue(SB),NOSPLIT,$0 347 MOVQ $0, DI 348 MOVQ $0, SI 349 MOVQ $0, DX 350 MOVL $362, AX 351 SYSCALL 352 JCC 2(PC) 353 NEGQ AX 354 MOVL AX, ret+0(FP) 355 RET 356 357 // int32 runtimekevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout); 358 TEXT runtimekevent(SB),NOSPLIT,$0 359 MOVL kq+0(FP), DI 360 MOVQ ch+8(FP), SI 361 MOVL nch+16(FP), DX 362 MOVQ ev+24(FP), R10 363 MOVL nev+32(FP), R8 364 MOVQ ts+40(FP), R9 365 MOVL $363, AX 366 SYSCALL 367 JCC 2(PC) 368 NEGQ AX 369 MOVL AX, ret+48(FP) 370 RET 371 372 // void runtimecloseonexec(int32 fd); 373 TEXT runtimecloseonexec(SB),NOSPLIT,$0 374 MOVL fd+0(FP), DI // fd 375 MOVQ $2, SI // F_SETFD 376 MOVQ $1, DX // FD_CLOEXEC 377 MOVL $92, AX // fcntl 378 SYSCALL 379 RET 380