1 // Copyright 2022 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.
6 // System calls and other sys.stuff for loong64, Linux
12 #include "cgo/abi_loong64.h"
15 #define CLOCK_REALTIME 0
16 #define CLOCK_MONOTONIC 1
22 #define SYS_getpid 172
25 #define SYS_munmap 215
26 #define SYS_setitimer 103
28 #define SYS_nanosleep 101
29 #define SYS_sched_yield 124
30 #define SYS_rt_sigreturn 139
31 #define SYS_rt_sigaction 134
32 #define SYS_rt_sigprocmask 135
33 #define SYS_sigaltstack 132
34 #define SYS_madvise 233
35 #define SYS_mincore 232
36 #define SYS_gettid 178
38 #define SYS_sched_getaffinity 123
39 #define SYS_exit_group 94
40 #define SYS_tgkill 131
42 #define SYS_clock_gettime 113
45 #define SYS_timer_create 107
46 #define SYS_timer_settime 110
47 #define SYS_timer_delete 111
49 // func exit(code int32)
50 TEXT runtime·exit(SB),NOSPLIT|NOFRAME,$0-4
52 MOVV $SYS_exit_group, R11
56 // func exitThread(wait *atomic.Uint32)
57 TEXT runtime·exitThread(SB),NOSPLIT|NOFRAME,$0-8
59 // We're done using the stack.
64 MOVW $0, R4 // exit code
69 // func open(name *byte, mode, perm int32) int32
70 TEXT runtime·open(SB),NOSPLIT|NOFRAME,$0-20
71 MOVW $AT_FDCWD, R4 // AT_FDCWD, so this acts like open
83 // func closefd(fd int32) int32
84 TEXT runtime·closefd(SB),NOSPLIT|NOFRAME,$0-12
94 // func write1(fd uintptr, p unsafe.Pointer, n int32) int32
95 TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0-28
104 // func read(fd int32, p unsafe.Pointer, n int32) int32
105 TEXT runtime·read(SB),NOSPLIT|NOFRAME,$0-28
114 // func pipe2(flags int32) (r, w int32, errno int32)
115 TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
120 MOVW R4, errno+16(FP)
123 // func usleep(usec uint32)
124 TEXT runtime·usleep(SB),NOSPLIT,$16-4
130 DIVVU R6, R7, R5 // ts->tv_sec
131 REMVU R6, R7, R4 // ts->tv_nsec
138 MOVV $SYS_nanosleep, R11
142 // func gettid() uint32
143 TEXT runtime·gettid(SB),NOSPLIT,$0-4
144 MOVV $SYS_gettid, R11
149 // func raise(sig uint32)
150 TEXT runtime·raise(SB),NOSPLIT|NOFRAME,$0
151 MOVV $SYS_getpid, R11
154 MOVV $SYS_gettid, R11
156 MOVW R4, R5 // arg 2 tid
157 MOVW R23, R4 // arg 1 pid
158 MOVW sig+0(FP), R6 // arg 3
159 MOVV $SYS_tgkill, R11
163 // func raiseproc(sig uint32)
164 TEXT runtime·raiseproc(SB),NOSPLIT|NOFRAME,$0
165 MOVV $SYS_getpid, R11
167 //MOVW R4, R4 // arg 1 pid
168 MOVW sig+0(FP), R5 // arg 2
174 TEXT ·getpid(SB),NOSPLIT|NOFRAME,$0-8
175 MOVV $SYS_getpid, R11
180 // func tgkill(tgid, tid, sig int)
181 TEXT ·tgkill(SB),NOSPLIT|NOFRAME,$0-24
185 MOVV $SYS_tgkill, R11
189 // func setitimer(mode int32, new, old *itimerval)
190 TEXT runtime·setitimer(SB),NOSPLIT|NOFRAME,$0-24
194 MOVV $SYS_setitimer, R11
198 // func timer_create(clockid int32, sevp *sigevent, timerid *int32) int32
199 TEXT runtime·timer_create(SB),NOSPLIT,$0-28
200 MOVW clockid+0(FP), R4
202 MOVV timerid+16(FP), R6
203 MOVV $SYS_timer_create, R11
208 // func timer_settime(timerid int32, flags int32, new, old *itimerspec) int32
209 TEXT runtime·timer_settime(SB),NOSPLIT,$0-28
210 MOVW timerid+0(FP), R4
214 MOVV $SYS_timer_settime, R11
219 // func timer_delete(timerid int32) int32
220 TEXT runtime·timer_delete(SB),NOSPLIT,$0-12
221 MOVW timerid+0(FP), R4
222 MOVV $SYS_timer_delete, R11
227 // func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
228 TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
232 MOVV $SYS_mincore, R11
237 // func walltime() (sec int64, nsec int32)
238 TEXT runtime·walltime(SB),NOSPLIT,$24-12
239 MOVV R3, R23 // R23 is unchanged by C code
242 MOVV g_m(g), R24 // R24 = m
244 // Set vdsoPC and vdsoSP for SIGPROF traceback.
245 // Save the old values on stack and restore them on exit,
246 // so this function is reentrant.
247 MOVV m_vdsoPC(R24), R11
248 MOVV m_vdsoSP(R24), R7
252 MOVV $ret-8(FP), R11 // caller's SP
253 MOVV R1, m_vdsoPC(R24)
254 MOVV R11, m_vdsoSP(R24)
261 MOVV (g_sched+gobuf_sp)(R4), R25 // Set SP to g0 stack
265 AND $~15, R25 // Align for C code
268 MOVW $CLOCK_REALTIME, R4
271 MOVV runtime·vdsoClockgettimeSym(SB), R20
274 // Store g on gsignal's stack, see sys_linux_arm64.s for detail
275 MOVBU runtime·iscgo(SB), R25
278 MOVV m_gsignal(R24), R25 // g.m.gsignal
282 MOVV (g_stack+stack_lo)(R25), R25 // g.m.gsignal.stack.lo
294 MOVV 0(R3), R7 // sec
295 MOVV 8(R3), R5 // nsec
297 MOVV R23, R3 // restore SP
298 // Restore vdsoPC, vdsoSP
299 // We don't worry about being signaled between the two stores.
300 // If we are not in a signal handler, we'll restore vdsoSP to 0,
301 // and no one will care about vdsoPC. If we are in a signal handler,
302 // we cannot receive another signal.
304 MOVV R25, m_vdsoSP(R24)
306 MOVV R25, m_vdsoPC(R24)
313 MOVV $SYS_clock_gettime, R11
317 // func nanotime1() int64
318 TEXT runtime·nanotime1(SB),NOSPLIT,$16-8
319 MOVV R3, R23 // R23 is unchanged by C code
322 MOVV g_m(g), R24 // R24 = m
324 // Set vdsoPC and vdsoSP for SIGPROF traceback.
325 // Save the old values on stack and restore them on exit,
326 // so this function is reentrant.
327 MOVV m_vdsoPC(R24), R11
328 MOVV m_vdsoSP(R24), R7
332 MOVV $ret-8(FP), R11 // caller's SP
333 MOVV R1, m_vdsoPC(R24)
334 MOVV R11, m_vdsoSP(R24)
341 MOVV (g_sched+gobuf_sp)(R4), R25 // Set SP to g0 stack
345 AND $~15, R25 // Align for C code
348 MOVW $CLOCK_MONOTONIC, R4
351 MOVV runtime·vdsoClockgettimeSym(SB), R20
354 // Store g on gsignal's stack, see sys_linux_arm64.s for detail
355 MOVBU runtime·iscgo(SB), R25
358 MOVV m_gsignal(R24), R25 // g.m.gsignal
362 MOVV (g_stack+stack_lo)(R25), R25 // g.m.gsignal.stack.lo
374 MOVV 0(R3), R7 // sec
375 MOVV 8(R3), R5 // nsec
377 MOVV R23, R3 // restore SP
378 // Restore vdsoPC, vdsoSP
379 // We don't worry about being signaled between the two stores.
380 // If we are not in a signal handler, we'll restore vdsoSP to 0,
381 // and no one will care about vdsoPC. If we are in a signal handler,
382 // we cannot receive another signal.
384 MOVV R25, m_vdsoSP(R24)
386 MOVV R25, m_vdsoPC(R24)
388 // sec is in R7, nsec in R5
397 MOVV $SYS_clock_gettime, R11
401 // func rtsigprocmask(how int32, new, old *sigset, size int32)
402 TEXT runtime·rtsigprocmask(SB),NOSPLIT|NOFRAME,$0-28
407 MOVV $SYS_rt_sigprocmask, R11
411 MOVV R0, 0xf1(R0) // crash
414 // func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
415 TEXT runtime·rt_sigaction(SB),NOSPLIT|NOFRAME,$0-36
420 MOVV $SYS_rt_sigaction, R11
425 // func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
426 TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
434 // func sigtramp(signo, ureg, ctxt unsafe.Pointer)
435 TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$168
440 // Save callee-save registers in the case of signal forwarding.
441 // Please refer to https://golang.org/issue/31827 .
442 SAVE_R22_TO_R31((4*8))
443 SAVE_F24_TO_F31((14*8))
445 // this might be called in external code context,
446 // where g is not set.
447 MOVB runtime·iscgo(SB), R4
449 JAL runtime·load_g(SB)
451 MOVV $runtime·sigtrampgo(SB), R4
454 // Restore callee-save registers.
455 RESTORE_R22_TO_R31((4*8))
456 RESTORE_F24_TO_F31((14*8))
460 // func cgoSigtramp()
461 TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
462 JMP runtime·sigtramp(SB)
464 // func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
465 TEXT runtime·sysMmap(SB),NOSPLIT|NOFRAME,$0
469 MOVW flags+20(FP), R7
486 // Call the function stored in _cgo_mmap using the GCC calling convention.
487 // This must be called on the system stack.
488 // func callCgoMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) uintptr
489 TEXT runtime·callCgoMmap(SB),NOSPLIT,$0
493 MOVW flags+20(FP), R7
496 MOVV _cgo_mmap(SB), R13
497 SUBV $16, R3 // reserve 16 bytes for sp-8 where fp may be saved.
503 // func sysMunmap(addr unsafe.Pointer, n uintptr)
504 TEXT runtime·sysMunmap(SB),NOSPLIT|NOFRAME,$0
507 MOVV $SYS_munmap, R11
511 MOVV R0, 0xf3(R0) // crash
514 // Call the function stored in _cgo_munmap using the GCC calling convention.
515 // This must be called on the system stack.
516 // func callCgoMunmap(addr unsafe.Pointer, n uintptr)
517 TEXT runtime·callCgoMunmap(SB),NOSPLIT,$0
520 MOVV _cgo_munmap(SB), R13
521 SUBV $16, R3 // reserve 16 bytes for sp-8 where fp may be saved.
526 // func madvise(addr unsafe.Pointer, n uintptr, flags int32)
527 TEXT runtime·madvise(SB),NOSPLIT|NOFRAME,$0
530 MOVW flags+16(FP), R6
531 MOVV $SYS_madvise, R11
536 // func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
537 TEXT runtime·futex(SB),NOSPLIT|NOFRAME,$0
542 MOVV addr2+24(FP), R8
549 // int64 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
550 TEXT runtime·clone(SB),NOSPLIT|NOFRAME,$0
554 // Copy mp, gp, fn off parent stack for use by child.
555 // Careful: Linux system call clobbers ???.
569 // In parent, return.
574 // In child, on new stack.
580 // Initialize m->procid to Linux tid
581 MOVV $SYS_gettid, R11
584 MOVV -24(R3), R25 // fn
585 MOVV -16(R3), R24 // g
586 MOVV -8(R3), R23 // m
591 MOVV R4, m_procid(R23)
595 // In child, set up new stack
598 //CALL runtime·stackcheck(SB)
604 // It shouldn't return. If it does, exit that thread.
608 JMP -3(PC) // keep exiting
610 // func sigaltstack(new, old *stackt)
611 TEXT runtime·sigaltstack(SB),NOSPLIT|NOFRAME,$0
614 MOVV $SYS_sigaltstack, R11
618 MOVV R0, 0xf1(R0) // crash
622 TEXT runtime·osyield(SB),NOSPLIT|NOFRAME,$0
623 MOVV $SYS_sched_yield, R11
627 // func sched_getaffinity(pid, len uintptr, buf *uintptr) int32
628 TEXT runtime·sched_getaffinity(SB),NOSPLIT|NOFRAME,$0
632 MOVV $SYS_sched_getaffinity, R11
637 // func sbrk0() uintptr
638 TEXT runtime·sbrk0(SB),NOSPLIT|NOFRAME,$0-8
639 // Implemented as brk(NULL).
646 TEXT runtime·access(SB),$0-20
647 MOVV R0, 2(R0) // unimplemented, only needed for android; declared in stubs_linux.go
648 MOVW R0, ret+16(FP) // for vet
651 TEXT runtime·connect(SB),$0-28
652 MOVV R0, 2(R0) // unimplemented, only needed for android; declared in stubs_linux.go
653 MOVW R0, ret+24(FP) // for vet
656 TEXT runtime·socket(SB),$0-20
657 MOVV R0, 2(R0) // unimplemented, only needed for android; declared in stubs_linux.go
658 MOVW R0, ret+16(FP) // for vet