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