Text file
src/runtime/sys_freebsd_amd64.s
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 #include "cgo/abi_amd64.h"
13
14 TEXT runtime·sys_umtx_op(SB),NOSPLIT,$0
15 MOVQ addr+0(FP), DI
16 MOVL mode+8(FP), SI
17 MOVL val+12(FP), DX
18 MOVQ uaddr1+16(FP), R10
19 MOVQ ut+24(FP), R8
20 MOVL $454, AX
21 SYSCALL
22 JCC 2(PC)
23 NEGQ AX
24 MOVL AX, ret+32(FP)
25 RET
26
27 TEXT runtime·thr_new(SB),NOSPLIT,$0
28 MOVQ param+0(FP), DI
29 MOVL size+8(FP), SI
30 MOVL $455, AX
31 SYSCALL
32 JCC 2(PC)
33 NEGQ AX
34 MOVL AX, ret+16(FP)
35 RET
36
37 TEXT runtime·thr_start(SB),NOSPLIT,$0
38 MOVQ DI, R13 // m
39
40 // set up FS to point at m->tls
41 LEAQ m_tls(R13), DI
42 CALL runtime·settls(SB) // smashes DI
43
44 // set up m, g
45 get_tls(CX)
46 MOVQ m_g0(R13), DI
47 MOVQ R13, g_m(DI)
48 MOVQ DI, g(CX)
49
50 CALL runtime·stackcheck(SB)
51 CALL runtime·mstart(SB)
52
53 MOVQ 0, AX // crash (not reached)
54
55 // Exit the entire program (like C exit)
56 TEXT runtime·exit(SB),NOSPLIT,$-8
57 MOVL code+0(FP), DI // arg 1 exit status
58 MOVL $1, AX
59 SYSCALL
60 MOVL $0xf1, 0xf1 // crash
61 RET
62
63 // func exitThread(wait *uint32)
64 TEXT runtime·exitThread(SB),NOSPLIT,$0-8
65 MOVQ wait+0(FP), AX
66 // We're done using the stack.
67 MOVL $0, (AX)
68 MOVL $0, DI // arg 1 long *state
69 MOVL $431, AX // thr_exit
70 SYSCALL
71 MOVL $0xf1, 0xf1 // crash
72 JMP 0(PC)
73
74 TEXT runtime·open(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 runtime·closefd(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 runtime·read(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 NEGQ AX // caller expects negative errno
102 MOVL AX, ret+24(FP)
103 RET
104
105 // func pipe() (r, w int32, errno int32)
106 TEXT runtime·pipe(SB),NOSPLIT,$0-12
107 MOVL $42, AX
108 SYSCALL
109 JCC ok
110 MOVL $0, r+0(FP)
111 MOVL $0, w+4(FP)
112 MOVL AX, errno+8(FP)
113 RET
114 ok:
115 MOVL AX, r+0(FP)
116 MOVL DX, w+4(FP)
117 MOVL $0, errno+8(FP)
118 RET
119
120 // func pipe2(flags int32) (r, w int32, errno int32)
121 TEXT runtime·pipe2(SB),NOSPLIT,$0-20
122 LEAQ r+8(FP), DI
123 MOVL flags+0(FP), SI
124 MOVL $542, AX
125 SYSCALL
126 JCC 2(PC)
127 NEGQ AX
128 MOVL AX, errno+16(FP)
129 RET
130
131 TEXT runtime·write1(SB),NOSPLIT,$-8
132 MOVQ fd+0(FP), DI // arg 1 fd
133 MOVQ p+8(FP), SI // arg 2 buf
134 MOVL n+16(FP), DX // arg 3 count
135 MOVL $4, AX
136 SYSCALL
137 JCC 2(PC)
138 NEGQ AX // caller expects negative errno
139 MOVL AX, ret+24(FP)
140 RET
141
142 TEXT runtime·thr_self(SB),NOSPLIT,$0-8
143 // thr_self(&0(FP))
144 LEAQ ret+0(FP), DI // arg 1
145 MOVL $432, AX
146 SYSCALL
147 RET
148
149 TEXT runtime·thr_kill(SB),NOSPLIT,$0-16
150 // thr_kill(tid, sig)
151 MOVQ tid+0(FP), DI // arg 1 id
152 MOVQ sig+8(FP), SI // arg 2 sig
153 MOVL $433, AX
154 SYSCALL
155 RET
156
157 TEXT runtime·raiseproc(SB),NOSPLIT,$0
158 // getpid
159 MOVL $20, AX
160 SYSCALL
161 // kill(self, sig)
162 MOVQ AX, DI // arg 1 pid
163 MOVL sig+0(FP), SI // arg 2 sig
164 MOVL $37, AX
165 SYSCALL
166 RET
167
168 TEXT runtime·setitimer(SB), NOSPLIT, $-8
169 MOVL mode+0(FP), DI
170 MOVQ new+8(FP), SI
171 MOVQ old+16(FP), DX
172 MOVL $83, AX
173 SYSCALL
174 RET
175
176 // func fallback_walltime() (sec int64, nsec int32)
177 TEXT runtime·fallback_walltime(SB), NOSPLIT, $32-12
178 MOVL $232, AX // clock_gettime
179 MOVQ $0, DI // CLOCK_REALTIME
180 LEAQ 8(SP), SI
181 SYSCALL
182 MOVQ 8(SP), AX // sec
183 MOVQ 16(SP), DX // nsec
184
185 // sec is in AX, nsec in DX
186 MOVQ AX, sec+0(FP)
187 MOVL DX, nsec+8(FP)
188 RET
189
190 TEXT runtime·fallback_nanotime(SB), NOSPLIT, $32-8
191 MOVL $232, AX
192 MOVQ $4, DI // CLOCK_MONOTONIC
193 LEAQ 8(SP), SI
194 SYSCALL
195 MOVQ 8(SP), AX // sec
196 MOVQ 16(SP), DX // nsec
197
198 // sec is in AX, nsec in DX
199 // return nsec in AX
200 IMULQ $1000000000, AX
201 ADDQ DX, AX
202 MOVQ AX, ret+0(FP)
203 RET
204
205 TEXT runtime·asmSigaction(SB),NOSPLIT,$0
206 MOVQ sig+0(FP), DI // arg 1 sig
207 MOVQ new+8(FP), SI // arg 2 act
208 MOVQ old+16(FP), DX // arg 3 oact
209 MOVL $416, AX
210 SYSCALL
211 JCC 2(PC)
212 MOVL $-1, AX
213 MOVL AX, ret+24(FP)
214 RET
215
216 TEXT runtime·callCgoSigaction(SB),NOSPLIT,$16
217 MOVQ sig+0(FP), DI // arg 1 sig
218 MOVQ new+8(FP), SI // arg 2 act
219 MOVQ old+16(FP), DX // arg 3 oact
220 MOVQ _cgo_sigaction(SB), AX
221 MOVQ SP, BX // callee-saved
222 ANDQ $~15, SP // alignment as per amd64 psABI
223 CALL AX
224 MOVQ BX, SP
225 MOVL AX, ret+24(FP)
226 RET
227
228 TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
229 MOVQ fn+0(FP), AX
230 MOVL sig+8(FP), DI
231 MOVQ info+16(FP), SI
232 MOVQ ctx+24(FP), DX
233 PUSHQ BP
234 MOVQ SP, BP
235 ANDQ $~15, SP // alignment for x86_64 ABI
236 CALL AX
237 MOVQ BP, SP
238 POPQ BP
239 RET
240
241 // Called using C ABI.
242 TEXT runtime·sigtramp(SB),NOSPLIT,$0
243 // Transition from C ABI to Go ABI.
244 PUSH_REGS_HOST_TO_ABI0()
245
246 // Call into the Go signal handler
247 NOP SP // disable vet stack checking
248 ADJSP $24
249 MOVQ DI, 0(SP) // sig
250 MOVQ SI, 8(SP) // info
251 MOVQ DX, 16(SP) // ctx
252 CALL ·sigtrampgo(SB)
253 ADJSP $-24
254
255 POP_REGS_HOST_TO_ABI0()
256 RET
257
258 // Called using C ABI.
259 TEXT runtime·sigprofNonGoWrapper<>(SB),NOSPLIT,$0
260 // Transition from C ABI to Go ABI.
261 PUSH_REGS_HOST_TO_ABI0()
262
263 // Call into the Go signal handler
264 NOP SP // disable vet stack checking
265 ADJSP $24
266 MOVL DI, 0(SP) // sig
267 MOVQ SI, 8(SP) // info
268 MOVQ DX, 16(SP) // ctx
269 CALL ·sigprofNonGo(SB)
270 ADJSP $-24
271
272 POP_REGS_HOST_TO_ABI0()
273 RET
274
275 // Used instead of sigtramp in programs that use cgo.
276 // Arguments from kernel are in DI, SI, DX.
277 TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
278 // If no traceback function, do usual sigtramp.
279 MOVQ runtime·cgoTraceback(SB), AX
280 TESTQ AX, AX
281 JZ sigtramp
282
283 // If no traceback support function, which means that
284 // runtime/cgo was not linked in, do usual sigtramp.
285 MOVQ _cgo_callers(SB), AX
286 TESTQ AX, AX
287 JZ sigtramp
288
289 // Figure out if we are currently in a cgo call.
290 // If not, just do usual sigtramp.
291 get_tls(CX)
292 MOVQ g(CX),AX
293 TESTQ AX, AX
294 JZ sigtrampnog // g == nil
295 MOVQ g_m(AX), AX
296 TESTQ AX, AX
297 JZ sigtramp // g.m == nil
298 MOVL m_ncgo(AX), CX
299 TESTL CX, CX
300 JZ sigtramp // g.m.ncgo == 0
301 MOVQ m_curg(AX), CX
302 TESTQ CX, CX
303 JZ sigtramp // g.m.curg == nil
304 MOVQ g_syscallsp(CX), CX
305 TESTQ CX, CX
306 JZ sigtramp // g.m.curg.syscallsp == 0
307 MOVQ m_cgoCallers(AX), R8
308 TESTQ R8, R8
309 JZ sigtramp // g.m.cgoCallers == nil
310 MOVL m_cgoCallersUse(AX), CX
311 TESTL CX, CX
312 JNZ sigtramp // g.m.cgoCallersUse != 0
313
314 // Jump to a function in runtime/cgo.
315 // That function, written in C, will call the user's traceback
316 // function with proper unwind info, and will then call back here.
317 // The first three arguments, and the fifth, are already in registers.
318 // Set the two remaining arguments now.
319 MOVQ runtime·cgoTraceback(SB), CX
320 MOVQ $runtime·sigtramp(SB), R9
321 MOVQ _cgo_callers(SB), AX
322 JMP AX
323
324 sigtramp:
325 JMP runtime·sigtramp(SB)
326
327 sigtrampnog:
328 // Signal arrived on a non-Go thread. If this is SIGPROF, get a
329 // stack trace.
330 CMPL DI, $27 // 27 == SIGPROF
331 JNZ sigtramp
332
333 // Lock sigprofCallersUse.
334 MOVL $0, AX
335 MOVL $1, CX
336 MOVQ $runtime·sigprofCallersUse(SB), R11
337 LOCK
338 CMPXCHGL CX, 0(R11)
339 JNZ sigtramp // Skip stack trace if already locked.
340
341 // Jump to the traceback function in runtime/cgo.
342 // It will call back to sigprofNonGo, via sigprofNonGoWrapper, to convert
343 // the arguments to the Go calling convention.
344 // First three arguments to traceback function are in registers already.
345 MOVQ runtime·cgoTraceback(SB), CX
346 MOVQ $runtime·sigprofCallers(SB), R8
347 MOVQ $runtime·sigprofNonGoWrapper<>(SB), R9
348 MOVQ _cgo_callers(SB), AX
349 JMP AX
350
351 TEXT runtime·mmap(SB),NOSPLIT,$0
352 MOVQ addr+0(FP), DI // arg 1 addr
353 MOVQ n+8(FP), SI // arg 2 len
354 MOVL prot+16(FP), DX // arg 3 prot
355 MOVL flags+20(FP), R10 // arg 4 flags
356 MOVL fd+24(FP), R8 // arg 5 fid
357 MOVL off+28(FP), R9 // arg 6 offset
358 MOVL $477, AX
359 SYSCALL
360 JCC ok
361 MOVQ $0, p+32(FP)
362 MOVQ AX, err+40(FP)
363 RET
364 ok:
365 MOVQ AX, p+32(FP)
366 MOVQ $0, err+40(FP)
367 RET
368
369 TEXT runtime·munmap(SB),NOSPLIT,$0
370 MOVQ addr+0(FP), DI // arg 1 addr
371 MOVQ n+8(FP), SI // arg 2 len
372 MOVL $73, AX
373 SYSCALL
374 JCC 2(PC)
375 MOVL $0xf1, 0xf1 // crash
376 RET
377
378 TEXT runtime·madvise(SB),NOSPLIT,$0
379 MOVQ addr+0(FP), DI
380 MOVQ n+8(FP), SI
381 MOVL flags+16(FP), DX
382 MOVQ $75, AX // madvise
383 SYSCALL
384 JCC 2(PC)
385 MOVL $-1, AX
386 MOVL AX, ret+24(FP)
387 RET
388
389 TEXT runtime·sigaltstack(SB),NOSPLIT,$-8
390 MOVQ new+0(FP), DI
391 MOVQ old+8(FP), SI
392 MOVQ $53, AX
393 SYSCALL
394 JCC 2(PC)
395 MOVL $0xf1, 0xf1 // crash
396 RET
397
398 TEXT runtime·usleep(SB),NOSPLIT,$16
399 MOVL $0, DX
400 MOVL usec+0(FP), AX
401 MOVL $1000000, CX
402 DIVL CX
403 MOVQ AX, 0(SP) // tv_sec
404 MOVL $1000, AX
405 MULL DX
406 MOVQ AX, 8(SP) // tv_nsec
407
408 MOVQ SP, DI // arg 1 - rqtp
409 MOVQ $0, SI // arg 2 - rmtp
410 MOVL $240, AX // sys_nanosleep
411 SYSCALL
412 RET
413
414 // set tls base to DI
415 TEXT runtime·settls(SB),NOSPLIT,$8
416 ADDQ $8, DI // adjust for ELF: wants to use -8(FS) for g and m
417 MOVQ DI, 0(SP)
418 MOVQ SP, SI
419 MOVQ $129, DI // AMD64_SET_FSBASE
420 MOVQ $165, AX // sysarch
421 SYSCALL
422 JCC 2(PC)
423 MOVL $0xf1, 0xf1 // crash
424 RET
425
426 TEXT runtime·sysctl(SB),NOSPLIT,$0
427 MOVQ mib+0(FP), DI // arg 1 - name
428 MOVL miblen+8(FP), SI // arg 2 - namelen
429 MOVQ out+16(FP), DX // arg 3 - oldp
430 MOVQ size+24(FP), R10 // arg 4 - oldlenp
431 MOVQ dst+32(FP), R8 // arg 5 - newp
432 MOVQ ndst+40(FP), R9 // arg 6 - newlen
433 MOVQ $202, AX // sys___sysctl
434 SYSCALL
435 JCC 4(PC)
436 NEGQ AX
437 MOVL AX, ret+48(FP)
438 RET
439 MOVL $0, AX
440 MOVL AX, ret+48(FP)
441 RET
442
443 TEXT runtime·osyield(SB),NOSPLIT,$-4
444 MOVL $331, AX // sys_sched_yield
445 SYSCALL
446 RET
447
448 TEXT runtime·sigprocmask(SB),NOSPLIT,$0
449 MOVL how+0(FP), DI // arg 1 - how
450 MOVQ new+8(FP), SI // arg 2 - set
451 MOVQ old+16(FP), DX // arg 3 - oset
452 MOVL $340, AX // sys_sigprocmask
453 SYSCALL
454 JAE 2(PC)
455 MOVL $0xf1, 0xf1 // crash
456 RET
457
458 // int32 runtime·kqueue(void);
459 TEXT runtime·kqueue(SB),NOSPLIT,$0
460 MOVQ $0, DI
461 MOVQ $0, SI
462 MOVQ $0, DX
463 MOVL $362, AX
464 SYSCALL
465 JCC 2(PC)
466 NEGQ AX
467 MOVL AX, ret+0(FP)
468 RET
469
470 // int32 runtime·kevent(int kq, Kevent *changelist, int nchanges, Kevent *eventlist, int nevents, Timespec *timeout);
471 TEXT runtime·kevent(SB),NOSPLIT,$0
472 MOVL kq+0(FP), DI
473 MOVQ ch+8(FP), SI
474 MOVL nch+16(FP), DX
475 MOVQ ev+24(FP), R10
476 MOVL nev+32(FP), R8
477 MOVQ ts+40(FP), R9
478 MOVL $363, AX
479 SYSCALL
480 JCC 2(PC)
481 NEGQ AX
482 MOVL AX, ret+48(FP)
483 RET
484
485 // void runtime·closeonexec(int32 fd);
486 TEXT runtime·closeonexec(SB),NOSPLIT,$0
487 MOVL fd+0(FP), DI // fd
488 MOVQ $2, SI // F_SETFD
489 MOVQ $1, DX // FD_CLOEXEC
490 MOVL $92, AX // fcntl
491 SYSCALL
492 RET
493
494 // func runtime·setNonblock(int32 fd)
495 TEXT runtime·setNonblock(SB),NOSPLIT,$0-4
496 MOVL fd+0(FP), DI // fd
497 MOVQ $3, SI // F_GETFL
498 MOVQ $0, DX
499 MOVL $92, AX // fcntl
500 SYSCALL
501 MOVL fd+0(FP), DI // fd
502 MOVQ $4, SI // F_SETFL
503 MOVQ $4, DX // O_NONBLOCK
504 ORL AX, DX
505 MOVL $92, AX // fcntl
506 SYSCALL
507 RET
508
509 // func cpuset_getaffinity(level int, which int, id int64, size int, mask *byte) int32
510 TEXT runtime·cpuset_getaffinity(SB), NOSPLIT, $0-44
511 MOVQ level+0(FP), DI
512 MOVQ which+8(FP), SI
513 MOVQ id+16(FP), DX
514 MOVQ size+24(FP), R10
515 MOVQ mask+32(FP), R8
516 MOVL $487, AX
517 SYSCALL
518 JCC 2(PC)
519 NEGQ AX
520 MOVL AX, ret+40(FP)
521 RET
522
View as plain text