github.com/tcnksm/go@v0.0.0-20141208075154-439b32936367/src/runtime/sys_linux_amd64.s (about) 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 // 6 // System calls and other sys.stuff for AMD64, Linux 7 // 8 9 #include "go_asm.h" 10 #include "go_tls.h" 11 #include "textflag.h" 12 13 TEXT runtime·exit(SB),NOSPLIT,$0-4 14 MOVL code+0(FP), DI 15 MOVL $231, AX // exitgroup - force all os threads to exit 16 SYSCALL 17 RET 18 19 TEXT runtime·exit1(SB),NOSPLIT,$0-4 20 MOVL code+0(FP), DI 21 MOVL $60, AX // exit - exit the current os thread 22 SYSCALL 23 RET 24 25 TEXT runtime·open(SB),NOSPLIT,$0-20 26 MOVQ name+0(FP), DI 27 MOVL mode+8(FP), SI 28 MOVL perm+12(FP), DX 29 MOVL $2, AX // syscall entry 30 SYSCALL 31 MOVL AX, ret+16(FP) 32 RET 33 34 TEXT runtime·close(SB),NOSPLIT,$0-12 35 MOVL fd+0(FP), DI 36 MOVL $3, AX // syscall entry 37 SYSCALL 38 MOVL AX, ret+8(FP) 39 RET 40 41 TEXT runtime·write(SB),NOSPLIT,$0-28 42 MOVQ fd+0(FP), DI 43 MOVQ p+8(FP), SI 44 MOVL n+16(FP), DX 45 MOVL $1, AX // syscall entry 46 SYSCALL 47 MOVL AX, ret+24(FP) 48 RET 49 50 TEXT runtime·read(SB),NOSPLIT,$0-28 51 MOVL fd+0(FP), DI 52 MOVQ p+8(FP), SI 53 MOVL n+16(FP), DX 54 MOVL $0, AX // syscall entry 55 SYSCALL 56 MOVL AX, ret+24(FP) 57 RET 58 59 TEXT runtime·getrlimit(SB),NOSPLIT,$0-20 60 MOVL kind+0(FP), DI 61 MOVQ limit+8(FP), SI 62 MOVL $97, AX // syscall entry 63 SYSCALL 64 MOVL AX, ret+16(FP) 65 RET 66 67 TEXT runtime·usleep(SB),NOSPLIT,$16 68 MOVL $0, DX 69 MOVL usec+0(FP), AX 70 MOVL $1000000, CX 71 DIVL CX 72 MOVQ AX, 0(SP) 73 MOVQ DX, 8(SP) 74 75 // select(0, 0, 0, 0, &tv) 76 MOVL $0, DI 77 MOVL $0, SI 78 MOVL $0, DX 79 MOVL $0, R10 80 MOVQ SP, R8 81 MOVL $23, AX 82 SYSCALL 83 RET 84 85 TEXT runtime·raise(SB),NOSPLIT,$0 86 MOVL $186, AX // syscall - gettid 87 SYSCALL 88 MOVL AX, DI // arg 1 tid 89 MOVL sig+0(FP), SI // arg 2 90 MOVL $200, AX // syscall - tkill 91 SYSCALL 92 RET 93 94 TEXT runtime·setitimer(SB),NOSPLIT,$0-24 95 MOVL mode+0(FP), DI 96 MOVQ new+8(FP), SI 97 MOVQ old+16(FP), DX 98 MOVL $38, AX // syscall entry 99 SYSCALL 100 RET 101 102 TEXT runtime·mincore(SB),NOSPLIT,$0-28 103 MOVQ addr+0(FP), DI 104 MOVQ n+8(FP), SI 105 MOVQ dst+16(FP), DX 106 MOVL $27, AX // syscall entry 107 SYSCALL 108 MOVL AX, ret+24(FP) 109 RET 110 111 // func now() (sec int64, nsec int32) 112 TEXT time·now(SB),NOSPLIT,$16 113 // Be careful. We're calling a function with gcc calling convention here. 114 // We're guaranteed 128 bytes on entry, and we've taken 16, and the 115 // call uses another 8. 116 // That leaves 104 for the gettime code to use. Hope that's enough! 117 MOVQ runtime·__vdso_clock_gettime_sym(SB), AX 118 CMPQ AX, $0 119 JEQ fallback 120 MOVL $0, DI // CLOCK_REALTIME 121 LEAQ 0(SP), SI 122 CALL AX 123 MOVQ 0(SP), AX // sec 124 MOVQ 8(SP), DX // nsec 125 MOVQ AX, sec+0(FP) 126 MOVL DX, nsec+8(FP) 127 RET 128 fallback: 129 LEAQ 0(SP), DI 130 MOVQ $0, SI 131 MOVQ runtime·__vdso_gettimeofday_sym(SB), AX 132 CALL AX 133 MOVQ 0(SP), AX // sec 134 MOVL 8(SP), DX // usec 135 IMULQ $1000, DX 136 MOVQ AX, sec+0(FP) 137 MOVL DX, nsec+8(FP) 138 RET 139 140 TEXT runtime·nanotime(SB),NOSPLIT,$16 141 // Duplicate time.now here to avoid using up precious stack space. 142 // See comment above in time.now. 143 MOVQ runtime·__vdso_clock_gettime_sym(SB), AX 144 CMPQ AX, $0 145 JEQ fallback 146 MOVL $1, DI // CLOCK_MONOTONIC 147 LEAQ 0(SP), SI 148 CALL AX 149 MOVQ 0(SP), AX // sec 150 MOVQ 8(SP), DX // nsec 151 // sec is in AX, nsec in DX 152 // return nsec in AX 153 IMULQ $1000000000, AX 154 ADDQ DX, AX 155 MOVQ AX, ret+0(FP) 156 RET 157 fallback: 158 LEAQ 0(SP), DI 159 MOVQ $0, SI 160 MOVQ runtime·__vdso_gettimeofday_sym(SB), AX 161 CALL AX 162 MOVQ 0(SP), AX // sec 163 MOVL 8(SP), DX // usec 164 IMULQ $1000, DX 165 // sec is in AX, nsec in DX 166 // return nsec in AX 167 IMULQ $1000000000, AX 168 ADDQ DX, AX 169 MOVQ AX, ret+0(FP) 170 RET 171 172 TEXT runtime·rtsigprocmask(SB),NOSPLIT,$0-28 173 MOVL sig+0(FP), DI 174 MOVQ new+8(FP), SI 175 MOVQ old+16(FP), DX 176 MOVL size+24(FP), R10 177 MOVL $14, AX // syscall entry 178 SYSCALL 179 CMPQ AX, $0xfffffffffffff001 180 JLS 2(PC) 181 MOVL $0xf1, 0xf1 // crash 182 RET 183 184 TEXT runtime·rt_sigaction(SB),NOSPLIT,$0-36 185 MOVQ sig+0(FP), DI 186 MOVQ new+8(FP), SI 187 MOVQ old+16(FP), DX 188 MOVQ size+24(FP), R10 189 MOVL $13, AX // syscall entry 190 SYSCALL 191 MOVL AX, ret+32(FP) 192 RET 193 194 TEXT runtime·sigtramp(SB),NOSPLIT,$64 195 get_tls(BX) 196 197 // check that g exists 198 MOVQ g(BX), R10 199 CMPQ R10, $0 200 JNE 5(PC) 201 MOVQ DI, 0(SP) 202 MOVQ $runtime·badsignal(SB), AX 203 CALL AX 204 RET 205 206 // save g 207 MOVQ R10, 40(SP) 208 209 // g = m->gsignal 210 MOVQ g_m(R10), BP 211 MOVQ m_gsignal(BP), BP 212 MOVQ BP, g(BX) 213 214 MOVQ DI, 0(SP) 215 MOVQ SI, 8(SP) 216 MOVQ DX, 16(SP) 217 MOVQ R10, 24(SP) 218 219 CALL runtime·sighandler(SB) 220 221 // restore g 222 get_tls(BX) 223 MOVQ 40(SP), R10 224 MOVQ R10, g(BX) 225 RET 226 227 TEXT runtime·sigreturn(SB),NOSPLIT,$0 228 MOVL $15, AX // rt_sigreturn 229 SYSCALL 230 INT $3 // not reached 231 232 TEXT runtime·mmap(SB),NOSPLIT,$0 233 MOVQ addr+0(FP), DI 234 MOVQ n+8(FP), SI 235 MOVL prot+16(FP), DX 236 MOVL flags+20(FP), R10 237 MOVL fd+24(FP), R8 238 MOVL off+28(FP), R9 239 240 MOVL $9, AX // mmap 241 SYSCALL 242 CMPQ AX, $0xfffffffffffff001 243 JLS 3(PC) 244 NOTQ AX 245 INCQ AX 246 MOVQ AX, ret+32(FP) 247 RET 248 249 TEXT runtime·munmap(SB),NOSPLIT,$0 250 MOVQ addr+0(FP), DI 251 MOVQ n+8(FP), SI 252 MOVQ $11, AX // munmap 253 SYSCALL 254 CMPQ AX, $0xfffffffffffff001 255 JLS 2(PC) 256 MOVL $0xf1, 0xf1 // crash 257 RET 258 259 TEXT runtime·madvise(SB),NOSPLIT,$0 260 MOVQ addr+0(FP), DI 261 MOVQ n+8(FP), SI 262 MOVL flags+16(FP), DX 263 MOVQ $28, AX // madvise 264 SYSCALL 265 // ignore failure - maybe pages are locked 266 RET 267 268 // int64 futex(int32 *uaddr, int32 op, int32 val, 269 // struct timespec *timeout, int32 *uaddr2, int32 val2); 270 TEXT runtime·futex(SB),NOSPLIT,$0 271 MOVQ addr+0(FP), DI 272 MOVL op+8(FP), SI 273 MOVL val+12(FP), DX 274 MOVQ ts+16(FP), R10 275 MOVQ addr2+24(FP), R8 276 MOVL val3+32(FP), R9 277 MOVL $202, AX 278 SYSCALL 279 MOVL AX, ret+40(FP) 280 RET 281 282 // int32 clone(int32 flags, void *stack, M *mp, G *gp, void (*fn)(void)); 283 TEXT runtime·clone(SB),NOSPLIT,$0 284 MOVL flags+8(SP), DI 285 MOVQ stack+16(SP), SI 286 287 // Copy mp, gp, fn off parent stack for use by child. 288 // Careful: Linux system call clobbers CX and R11. 289 MOVQ mm+24(SP), R8 290 MOVQ gg+32(SP), R9 291 MOVQ fn+40(SP), R12 292 293 MOVL $56, AX 294 SYSCALL 295 296 // In parent, return. 297 CMPQ AX, $0 298 JEQ 3(PC) 299 MOVL AX, ret+40(FP) 300 RET 301 302 // In child, on new stack. 303 MOVQ SI, SP 304 305 // Initialize m->procid to Linux tid 306 MOVL $186, AX // gettid 307 SYSCALL 308 MOVQ AX, m_procid(R8) 309 310 // Set FS to point at m->tls. 311 LEAQ m_tls(R8), DI 312 CALL runtime·settls(SB) 313 314 // In child, set up new stack 315 get_tls(CX) 316 MOVQ R8, g_m(R9) 317 MOVQ R9, g(CX) 318 CALL runtime·stackcheck(SB) 319 320 // Call fn 321 CALL R12 322 323 // It shouldn't return. If it does, exit 324 MOVL $111, DI 325 MOVL $60, AX 326 SYSCALL 327 JMP -3(PC) // keep exiting 328 329 TEXT runtime·sigaltstack(SB),NOSPLIT,$-8 330 MOVQ new+8(SP), DI 331 MOVQ old+16(SP), SI 332 MOVQ $131, AX 333 SYSCALL 334 CMPQ AX, $0xfffffffffffff001 335 JLS 2(PC) 336 MOVL $0xf1, 0xf1 // crash 337 RET 338 339 // set tls base to DI 340 TEXT runtime·settls(SB),NOSPLIT,$32 341 ADDQ $16, DI // ELF wants to use -16(FS), -8(FS) 342 343 MOVQ DI, SI 344 MOVQ $0x1002, DI // ARCH_SET_FS 345 MOVQ $158, AX // arch_prctl 346 SYSCALL 347 CMPQ AX, $0xfffffffffffff001 348 JLS 2(PC) 349 MOVL $0xf1, 0xf1 // crash 350 RET 351 352 TEXT runtime·osyield(SB),NOSPLIT,$0 353 MOVL $24, AX 354 SYSCALL 355 RET 356 357 TEXT runtime·sched_getaffinity(SB),NOSPLIT,$0 358 MOVQ pid+0(FP), DI 359 MOVQ len+8(FP), SI 360 MOVQ buf+16(FP), DX 361 MOVL $204, AX // syscall entry 362 SYSCALL 363 MOVL AX, ret+24(FP) 364 RET 365 366 // int32 runtime·epollcreate(int32 size); 367 TEXT runtime·epollcreate(SB),NOSPLIT,$0 368 MOVL size+0(FP), DI 369 MOVL $213, AX // syscall entry 370 SYSCALL 371 MOVL AX, ret+8(FP) 372 RET 373 374 // int32 runtime·epollcreate1(int32 flags); 375 TEXT runtime·epollcreate1(SB),NOSPLIT,$0 376 MOVL flags+0(FP), DI 377 MOVL $291, AX // syscall entry 378 SYSCALL 379 MOVL AX, ret+8(FP) 380 RET 381 382 // func epollctl(epfd, op, fd int32, ev *epollEvent) int 383 TEXT runtime·epollctl(SB),NOSPLIT,$0 384 MOVL epfd+0(FP), DI 385 MOVL op+4(FP), SI 386 MOVL fd+8(FP), DX 387 MOVQ ev+16(FP), R10 388 MOVL $233, AX // syscall entry 389 SYSCALL 390 MOVL AX, ret+24(FP) 391 RET 392 393 // int32 runtime·epollwait(int32 epfd, EpollEvent *ev, int32 nev, int32 timeout); 394 TEXT runtime·epollwait(SB),NOSPLIT,$0 395 MOVL epfd+0(FP), DI 396 MOVQ ev+8(FP), SI 397 MOVL nev+16(FP), DX 398 MOVL timeout+20(FP), R10 399 MOVL $232, AX // syscall entry 400 SYSCALL 401 MOVL AX, ret+24(FP) 402 RET 403 404 // void runtime·closeonexec(int32 fd); 405 TEXT runtime·closeonexec(SB),NOSPLIT,$0 406 MOVL fd+0(FP), DI // fd 407 MOVQ $2, SI // F_SETFD 408 MOVQ $1, DX // FD_CLOEXEC 409 MOVL $72, AX // fcntl 410 SYSCALL 411 RET