Text file
src/runtime/sys_linux_loong64.s
Documentation: runtime
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.
4
5//
6// System calls and other sys.stuff for loong64, Linux
7//
8
9#include "go_asm.h"
10#include "go_tls.h"
11#include "textflag.h"
12#include "cgo/abi_loong64.h"
13
14#define AT_FDCWD -100
15#define CLOCK_REALTIME 0
16#define CLOCK_MONOTONIC 1
17
18#define SYS_exit 93
19#define SYS_read 63
20#define SYS_write 64
21#define SYS_close 57
22#define SYS_getpid 172
23#define SYS_kill 129
24#define SYS_mmap 222
25#define SYS_munmap 215
26#define SYS_setitimer 103
27#define SYS_clone 220
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
37#define SYS_futex 98
38#define SYS_sched_getaffinity 123
39#define SYS_exit_group 94
40#define SYS_tgkill 131
41#define SYS_openat 56
42#define SYS_clock_gettime 113
43#define SYS_brk 214
44#define SYS_pipe2 59
45#define SYS_timer_create 107
46#define SYS_timer_settime 110
47#define SYS_timer_delete 111
48
49// func exit(code int32)
50TEXT runtime·exit(SB),NOSPLIT|NOFRAME,$0-4
51 MOVW code+0(FP), R4
52 MOVV $SYS_exit_group, R11
53 SYSCALL
54 RET
55
56// func exitThread(wait *atomic.Uint32)
57TEXT runtime·exitThread(SB),NOSPLIT|NOFRAME,$0-8
58 MOVV wait+0(FP), R19
59 // We're done using the stack.
60 MOVW $0, R11
61 DBAR
62 MOVW R11, (R19)
63 DBAR
64 MOVW $0, R4 // exit code
65 MOVV $SYS_exit, R11
66 SYSCALL
67 JMP 0(PC)
68
69// func open(name *byte, mode, perm int32) int32
70TEXT runtime·open(SB),NOSPLIT|NOFRAME,$0-20
71 MOVW $AT_FDCWD, R4 // AT_FDCWD, so this acts like open
72 MOVV name+0(FP), R5
73 MOVW mode+8(FP), R6
74 MOVW perm+12(FP), R7
75 MOVV $SYS_openat, R11
76 SYSCALL
77 MOVW $-4096, R5
78 BGEU R5, R4, 2(PC)
79 MOVW $-1, R4
80 MOVW R4, ret+16(FP)
81 RET
82
83// func closefd(fd int32) int32
84TEXT runtime·closefd(SB),NOSPLIT|NOFRAME,$0-12
85 MOVW fd+0(FP), R4
86 MOVV $SYS_close, R11
87 SYSCALL
88 MOVW $-4096, R5
89 BGEU R5, R4, 2(PC)
90 MOVW $-1, R4
91 MOVW R4, ret+8(FP)
92 RET
93
94// func write1(fd uintptr, p unsafe.Pointer, n int32) int32
95TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0-28
96 MOVV fd+0(FP), R4
97 MOVV p+8(FP), R5
98 MOVW n+16(FP), R6
99 MOVV $SYS_write, R11
100 SYSCALL
101 MOVW R4, ret+24(FP)
102 RET
103
104// func read(fd int32, p unsafe.Pointer, n int32) int32
105TEXT runtime·read(SB),NOSPLIT|NOFRAME,$0-28
106 MOVW fd+0(FP), R4
107 MOVV p+8(FP), R5
108 MOVW n+16(FP), R6
109 MOVV $SYS_read, R11
110 SYSCALL
111 MOVW R4, ret+24(FP)
112 RET
113
114// func pipe2(flags int32) (r, w int32, errno int32)
115TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
116 MOVV $r+8(FP), R4
117 MOVW flags+0(FP), R5
118 MOVV $SYS_pipe2, R11
119 SYSCALL
120 MOVW R4, errno+16(FP)
121 RET
122
123// func usleep(usec uint32)
124TEXT runtime·usleep(SB),NOSPLIT,$16-4
125 MOVWU usec+0(FP), R7
126 MOVV $1000, R6
127 MULVU R6, R7, R7
128 MOVV $1000000000, R6
129
130 DIVVU R6, R7, R5 // ts->tv_sec
131 REMVU R6, R7, R4 // ts->tv_nsec
132 MOVV R5, 8(R3)
133 MOVV R4, 16(R3)
134
135 // nanosleep(&ts, 0)
136 ADDV $8, R3, R4
137 MOVV R0, R5
138 MOVV $SYS_nanosleep, R11
139 SYSCALL
140 RET
141
142// func gettid() uint32
143TEXT runtime·gettid(SB),NOSPLIT,$0-4
144 MOVV $SYS_gettid, R11
145 SYSCALL
146 MOVW R4, ret+0(FP)
147 RET
148
149// func raise(sig uint32)
150TEXT runtime·raise(SB),NOSPLIT|NOFRAME,$0
151 MOVV $SYS_getpid, R11
152 SYSCALL
153 MOVW R4, R23
154 MOVV $SYS_gettid, R11
155 SYSCALL
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
160 SYSCALL
161 RET
162
163// func raiseproc(sig uint32)
164TEXT runtime·raiseproc(SB),NOSPLIT|NOFRAME,$0
165 MOVV $SYS_getpid, R11
166 SYSCALL
167 //MOVW R4, R4 // arg 1 pid
168 MOVW sig+0(FP), R5 // arg 2
169 MOVV $SYS_kill, R11
170 SYSCALL
171 RET
172
173// func getpid() int
174TEXT ·getpid(SB),NOSPLIT|NOFRAME,$0-8
175 MOVV $SYS_getpid, R11
176 SYSCALL
177 MOVV R4, ret+0(FP)
178 RET
179
180// func tgkill(tgid, tid, sig int)
181TEXT ·tgkill(SB),NOSPLIT|NOFRAME,$0-24
182 MOVV tgid+0(FP), R4
183 MOVV tid+8(FP), R5
184 MOVV sig+16(FP), R6
185 MOVV $SYS_tgkill, R11
186 SYSCALL
187 RET
188
189// func setitimer(mode int32, new, old *itimerval)
190TEXT runtime·setitimer(SB),NOSPLIT|NOFRAME,$0-24
191 MOVW mode+0(FP), R4
192 MOVV new+8(FP), R5
193 MOVV old+16(FP), R6
194 MOVV $SYS_setitimer, R11
195 SYSCALL
196 RET
197
198// func timer_create(clockid int32, sevp *sigevent, timerid *int32) int32
199TEXT runtime·timer_create(SB),NOSPLIT,$0-28
200 MOVW clockid+0(FP), R4
201 MOVV sevp+8(FP), R5
202 MOVV timerid+16(FP), R6
203 MOVV $SYS_timer_create, R11
204 SYSCALL
205 MOVW R4, ret+24(FP)
206 RET
207
208// func timer_settime(timerid int32, flags int32, new, old *itimerspec) int32
209TEXT runtime·timer_settime(SB),NOSPLIT,$0-28
210 MOVW timerid+0(FP), R4
211 MOVW flags+4(FP), R5
212 MOVV new+8(FP), R6
213 MOVV old+16(FP), R7
214 MOVV $SYS_timer_settime, R11
215 SYSCALL
216 MOVW R4, ret+24(FP)
217 RET
218
219// func timer_delete(timerid int32) int32
220TEXT runtime·timer_delete(SB),NOSPLIT,$0-12
221 MOVW timerid+0(FP), R4
222 MOVV $SYS_timer_delete, R11
223 SYSCALL
224 MOVW R4, ret+8(FP)
225 RET
226
227// func mincore(addr unsafe.Pointer, n uintptr, dst *byte) int32
228TEXT runtime·mincore(SB),NOSPLIT|NOFRAME,$0-28
229 MOVV addr+0(FP), R4
230 MOVV n+8(FP), R5
231 MOVV dst+16(FP), R6
232 MOVV $SYS_mincore, R11
233 SYSCALL
234 MOVW R4, ret+24(FP)
235 RET
236
237// func walltime() (sec int64, nsec int32)
238TEXT runtime·walltime(SB),NOSPLIT,$24-12
239 MOVV R3, R23 // R23 is unchanged by C code
240 MOVV R3, R25
241
242 MOVV g_m(g), R24 // R24 = m
243
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
249 MOVV R11, 8(R3)
250 MOVV R7, 16(R3)
251
252 MOVV $ret-8(FP), R11 // caller's SP
253 MOVV R1, m_vdsoPC(R24)
254 MOVV R11, m_vdsoSP(R24)
255
256 MOVV m_curg(R24), R4
257 MOVV g, R5
258 BNE R4, R5, noswitch
259
260 MOVV m_g0(R24), R4
261 MOVV (g_sched+gobuf_sp)(R4), R25 // Set SP to g0 stack
262
263noswitch:
264 SUBV $16, R25
265 AND $~15, R25 // Align for C code
266 MOVV R25, R3
267
268 MOVW $CLOCK_REALTIME, R4
269 MOVV $0(R3), R5
270
271 MOVV runtime·vdsoClockgettimeSym(SB), R20
272 BEQ R20, fallback
273
274 // Store g on gsignal's stack, see sys_linux_arm64.s for detail
275 MOVBU runtime·iscgo(SB), R25
276 BNE R25, nosaveg
277
278 MOVV m_gsignal(R24), R25 // g.m.gsignal
279 BEQ R25, nosaveg
280 BEQ g, R25, nosaveg
281
282 MOVV (g_stack+stack_lo)(R25), R25 // g.m.gsignal.stack.lo
283 MOVV g, (R25)
284
285 JAL (R20)
286
287 MOVV R0, (R25)
288 JMP finish
289
290nosaveg:
291 JAL (R20)
292
293finish:
294 MOVV 0(R3), R7 // sec
295 MOVV 8(R3), R5 // nsec
296
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.
303 MOVV 16(R3), R25
304 MOVV R25, m_vdsoSP(R24)
305 MOVV 8(R3), R25
306 MOVV R25, m_vdsoPC(R24)
307
308 MOVV R7, sec+0(FP)
309 MOVW R5, nsec+8(FP)
310 RET
311
312fallback:
313 MOVV $SYS_clock_gettime, R11
314 SYSCALL
315 JMP finish
316
317// func nanotime1() int64
318TEXT runtime·nanotime1(SB),NOSPLIT,$16-8
319 MOVV R3, R23 // R23 is unchanged by C code
320 MOVV R3, R25
321
322 MOVV g_m(g), R24 // R24 = m
323
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
329 MOVV R11, 8(R3)
330 MOVV R7, 16(R3)
331
332 MOVV $ret-8(FP), R11 // caller's SP
333 MOVV R1, m_vdsoPC(R24)
334 MOVV R11, m_vdsoSP(R24)
335
336 MOVV m_curg(R24), R4
337 MOVV g, R5
338 BNE R4, R5, noswitch
339
340 MOVV m_g0(R24), R4
341 MOVV (g_sched+gobuf_sp)(R4), R25 // Set SP to g0 stack
342
343noswitch:
344 SUBV $16, R25
345 AND $~15, R25 // Align for C code
346 MOVV R25, R3
347
348 MOVW $CLOCK_MONOTONIC, R4
349 MOVV $0(R3), R5
350
351 MOVV runtime·vdsoClockgettimeSym(SB), R20
352 BEQ R20, fallback
353
354 // Store g on gsignal's stack, see sys_linux_arm64.s for detail
355 MOVBU runtime·iscgo(SB), R25
356 BNE R25, nosaveg
357
358 MOVV m_gsignal(R24), R25 // g.m.gsignal
359 BEQ R25, nosaveg
360 BEQ g, R25, nosaveg
361
362 MOVV (g_stack+stack_lo)(R25), R25 // g.m.gsignal.stack.lo
363 MOVV g, (R25)
364
365 JAL (R20)
366
367 MOVV R0, (R25)
368 JMP finish
369
370nosaveg:
371 JAL (R20)
372
373finish:
374 MOVV 0(R3), R7 // sec
375 MOVV 8(R3), R5 // nsec
376
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.
383 MOVV 16(R3), R25
384 MOVV R25, m_vdsoSP(R24)
385 MOVV 8(R3), R25
386 MOVV R25, m_vdsoPC(R24)
387
388 // sec is in R7, nsec in R5
389 // return nsec in R7
390 MOVV $1000000000, R4
391 MULVU R4, R7, R7
392 ADDVU R5, R7
393 MOVV R7, ret+0(FP)
394 RET
395
396fallback:
397 MOVV $SYS_clock_gettime, R11
398 SYSCALL
399 JMP finish
400
401// func rtsigprocmask(how int32, new, old *sigset, size int32)
402TEXT runtime·rtsigprocmask(SB),NOSPLIT|NOFRAME,$0-28
403 MOVW how+0(FP), R4
404 MOVV new+8(FP), R5
405 MOVV old+16(FP), R6
406 MOVW size+24(FP), R7
407 MOVV $SYS_rt_sigprocmask, R11
408 SYSCALL
409 MOVW $-4096, R5
410 BGEU R5, R4, 2(PC)
411 MOVV R0, 0xf1(R0) // crash
412 RET
413
414// func rt_sigaction(sig uintptr, new, old *sigactiont, size uintptr) int32
415TEXT runtime·rt_sigaction(SB),NOSPLIT|NOFRAME,$0-36
416 MOVV sig+0(FP), R4
417 MOVV new+8(FP), R5
418 MOVV old+16(FP), R6
419 MOVV size+24(FP), R7
420 MOVV $SYS_rt_sigaction, R11
421 SYSCALL
422 MOVW R4, ret+32(FP)
423 RET
424
425// func sigfwd(fn uintptr, sig uint32, info *siginfo, ctx unsafe.Pointer)
426TEXT runtime·sigfwd(SB),NOSPLIT,$0-32
427 MOVW sig+8(FP), R4
428 MOVV info+16(FP), R5
429 MOVV ctx+24(FP), R6
430 MOVV fn+0(FP), R20
431 JAL (R20)
432 RET
433
434// func sigtramp(signo, ureg, ctxt unsafe.Pointer)
435TEXT runtime·sigtramp(SB),NOSPLIT|TOPFRAME,$168
436 MOVW R4, (1*8)(R3)
437 MOVV R5, (2*8)(R3)
438 MOVV R6, (3*8)(R3)
439
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))
444
445 // this might be called in external code context,
446 // where g is not set.
447 MOVB runtime·iscgo(SB), R4
448 BEQ R4, 2(PC)
449 JAL runtime·load_g(SB)
450
451 MOVV $runtime·sigtrampgo(SB), R4
452 JAL (R4)
453
454 // Restore callee-save registers.
455 RESTORE_R22_TO_R31((4*8))
456 RESTORE_F24_TO_F31((14*8))
457
458 RET
459
460// func cgoSigtramp()
461TEXT runtime·cgoSigtramp(SB),NOSPLIT,$0
462 JMP runtime·sigtramp(SB)
463
464// func sysMmap(addr unsafe.Pointer, n uintptr, prot, flags, fd int32, off uint32) (p unsafe.Pointer, err int)
465TEXT runtime·sysMmap(SB),NOSPLIT|NOFRAME,$0
466 MOVV addr+0(FP), R4
467 MOVV n+8(FP), R5
468 MOVW prot+16(FP), R6
469 MOVW flags+20(FP), R7
470 MOVW fd+24(FP), R8
471 MOVW off+28(FP), R9
472
473 MOVV $SYS_mmap, R11
474 SYSCALL
475 MOVW $-4096, R5
476 BGEU R5, R4, ok
477 MOVV $0, p+32(FP)
478 SUBVU R4, R0, R4
479 MOVV R4, err+40(FP)
480 RET
481ok:
482 MOVV R4, p+32(FP)
483 MOVV $0, err+40(FP)
484 RET
485
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
489TEXT runtime·callCgoMmap(SB),NOSPLIT,$0
490 MOVV addr+0(FP), R4
491 MOVV n+8(FP), R5
492 MOVW prot+16(FP), R6
493 MOVW flags+20(FP), R7
494 MOVW fd+24(FP), R8
495 MOVW off+28(FP), R9
496 MOVV _cgo_mmap(SB), R13
497 SUBV $16, R3 // reserve 16 bytes for sp-8 where fp may be saved.
498 JAL (R13)
499 ADDV $16, R3
500 MOVV R4, ret+32(FP)
501 RET
502
503// func sysMunmap(addr unsafe.Pointer, n uintptr)
504TEXT runtime·sysMunmap(SB),NOSPLIT|NOFRAME,$0
505 MOVV addr+0(FP), R4
506 MOVV n+8(FP), R5
507 MOVV $SYS_munmap, R11
508 SYSCALL
509 MOVW $-4096, R5
510 BGEU R5, R4, 2(PC)
511 MOVV R0, 0xf3(R0) // crash
512 RET
513
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)
517TEXT runtime·callCgoMunmap(SB),NOSPLIT,$0
518 MOVV addr+0(FP), R4
519 MOVV n+8(FP), R5
520 MOVV _cgo_munmap(SB), R13
521 SUBV $16, R3 // reserve 16 bytes for sp-8 where fp may be saved.
522 JAL (R13)
523 ADDV $16, R3
524 RET
525
526// func madvise(addr unsafe.Pointer, n uintptr, flags int32)
527TEXT runtime·madvise(SB),NOSPLIT|NOFRAME,$0
528 MOVV addr+0(FP), R4
529 MOVV n+8(FP), R5
530 MOVW flags+16(FP), R6
531 MOVV $SYS_madvise, R11
532 SYSCALL
533 MOVW R4, ret+24(FP)
534 RET
535
536// func futex(addr unsafe.Pointer, op int32, val uint32, ts, addr2 unsafe.Pointer, val3 uint32) int32
537TEXT runtime·futex(SB),NOSPLIT|NOFRAME,$0
538 MOVV addr+0(FP), R4
539 MOVW op+8(FP), R5
540 MOVW val+12(FP), R6
541 MOVV ts+16(FP), R7
542 MOVV addr2+24(FP), R8
543 MOVW val3+32(FP), R9
544 MOVV $SYS_futex, R11
545 SYSCALL
546 MOVW R4, ret+40(FP)
547 RET
548
549// int64 clone(int32 flags, void *stk, M *mp, G *gp, void (*fn)(void));
550TEXT runtime·clone(SB),NOSPLIT|NOFRAME,$0
551 MOVW flags+0(FP), R4
552 MOVV stk+8(FP), R5
553
554 // Copy mp, gp, fn off parent stack for use by child.
555 // Careful: Linux system call clobbers ???.
556 MOVV mp+16(FP), R23
557 MOVV gp+24(FP), R24
558 MOVV fn+32(FP), R25
559
560 MOVV R23, -8(R5)
561 MOVV R24, -16(R5)
562 MOVV R25, -24(R5)
563 MOVV $1234, R23
564 MOVV R23, -32(R5)
565
566 MOVV $SYS_clone, R11
567 SYSCALL
568
569 // In parent, return.
570 BEQ R4, 3(PC)
571 MOVW R4, ret+40(FP)
572 RET
573
574 // In child, on new stack.
575 MOVV -32(R3), R23
576 MOVV $1234, R19
577 BEQ R23, R19, 2(PC)
578 MOVV R0, 0(R0)
579
580 // Initialize m->procid to Linux tid
581 MOVV $SYS_gettid, R11
582 SYSCALL
583
584 MOVV -24(R3), R25 // fn
585 MOVV -16(R3), R24 // g
586 MOVV -8(R3), R23 // m
587
588 BEQ R23, nog
589 BEQ R24, nog
590
591 MOVV R4, m_procid(R23)
592
593 // TODO: setup TLS.
594
595 // In child, set up new stack
596 MOVV R23, g_m(R24)
597 MOVV R24, g
598 //CALL runtime·stackcheck(SB)
599
600nog:
601 // Call fn
602 JAL (R25)
603
604 // It shouldn't return. If it does, exit that thread.
605 MOVW $111, R4
606 MOVV $SYS_exit, R11
607 SYSCALL
608 JMP -3(PC) // keep exiting
609
610// func sigaltstack(new, old *stackt)
611TEXT runtime·sigaltstack(SB),NOSPLIT|NOFRAME,$0
612 MOVV new+0(FP), R4
613 MOVV old+8(FP), R5
614 MOVV $SYS_sigaltstack, R11
615 SYSCALL
616 MOVW $-4096, R5
617 BGEU R5, R4, 2(PC)
618 MOVV R0, 0xf1(R0) // crash
619 RET
620
621// func osyield()
622TEXT runtime·osyield(SB),NOSPLIT|NOFRAME,$0
623 MOVV $SYS_sched_yield, R11
624 SYSCALL
625 RET
626
627// func sched_getaffinity(pid, len uintptr, buf *uintptr) int32
628TEXT runtime·sched_getaffinity(SB),NOSPLIT|NOFRAME,$0
629 MOVV pid+0(FP), R4
630 MOVV len+8(FP), R5
631 MOVV buf+16(FP), R6
632 MOVV $SYS_sched_getaffinity, R11
633 SYSCALL
634 MOVW R4, ret+24(FP)
635 RET
636
637// func sbrk0() uintptr
638TEXT runtime·sbrk0(SB),NOSPLIT|NOFRAME,$0-8
639 // Implemented as brk(NULL).
640 MOVV $0, R4
641 MOVV $SYS_brk, R11
642 SYSCALL
643 MOVV R4, ret+0(FP)
644 RET
645
646TEXT 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
649 RET
650
651TEXT 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
654 RET
655
656TEXT 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
659 RET
View as plain text