// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build linux && (ppc64 || ppc64le)
// +build linux
// +build ppc64 ppc64le
//
// System calls and other sys.stuff for ppc64, Linux
//
#include "go_asm.h"
#include "go_tls.h"
#include "textflag.h"
#include "asm_ppc64x.h"
#define SYS_exit 1
#define SYS_read 3
#define SYS_write 4
#define SYS_open 5
#define SYS_close 6
#define SYS_getpid 20
#define SYS_kill 37
#define SYS_pipe 42
#define SYS_brk 45
#define SYS_fcntl 55
#define SYS_mmap 90
#define SYS_munmap 91
#define SYS_setitimer 104
#define SYS_clone 120
#define SYS_sched_yield 158
#define SYS_nanosleep 162
#define SYS_rt_sigreturn 172
#define SYS_rt_sigaction 173
#define SYS_rt_sigprocmask 174
#define SYS_sigaltstack 185
#define SYS_madvise 205
#define SYS_mincore 206
#define SYS_gettid 207
#define SYS_futex 221
#define SYS_sched_getaffinity 223
#define SYS_exit_group 234
#define SYS_epoll_create 236
#define SYS_epoll_ctl 237
#define SYS_epoll_wait 238
#define SYS_clock_gettime 246
#define SYS_tgkill 250
#define SYS_epoll_create1 315
#define SYS_pipe2 317
TEXT runtime·exit(SB),NOSPLIT|NOFRAME,$0-4
MOVW code+0(FP), R3
SYSCALL $SYS_exit_group
RET
// func exitThread(wait *uint32)
TEXT runtime·exitThread(SB),NOSPLIT|NOFRAME,$0-8
MOVD wait+0(FP), R1
// We're done using the stack.
MOVW $0, R2
SYNC
MOVW R2, (R1)
MOVW $0, R3 // exit code
SYSCALL $SYS_exit
JMP 0(PC)
TEXT runtime·open(SB),NOSPLIT|NOFRAME,$0-20
MOVD name+0(FP), R3
MOVW mode+8(FP), R4
MOVW perm+12(FP), R5
SYSCALL $SYS_open
BVC 2(PC)
MOVW $-1, R3
MOVW R3, ret+16(FP)
RET
TEXT runtime·closefd(SB),NOSPLIT|NOFRAME,$0-12
MOVW fd+0(FP), R3
SYSCALL $SYS_close
BVC 2(PC)
MOVW $-1, R3
MOVW R3, ret+8(FP)
RET
TEXT runtime·write1(SB),NOSPLIT|NOFRAME,$0-28
MOVD fd+0(FP), R3
MOVD p+8(FP), R4
MOVW n+16(FP), R5
SYSCALL $SYS_write
BVC 2(PC)
NEG R3 // caller expects negative errno
MOVW R3, ret+24(FP)
RET
TEXT runtime·read(SB),NOSPLIT|NOFRAME,$0-28
MOVW fd+0(FP), R3
MOVD p+8(FP), R4
MOVW n+16(FP), R5
SYSCALL $SYS_read
BVC 2(PC)
NEG R3 // caller expects negative errno
MOVW R3, ret+24(FP)
RET
// func pipe() (r, w int32, errno int32)
TEXT runtime·pipe(SB),NOSPLIT|NOFRAME,$0-12
ADD $FIXED_FRAME, R1, R3
SYSCALL $SYS_pipe
MOVW R3, errno+8(FP)
RET
// func pipe2(flags int32) (r, w int32, errno int32)
TEXT runtime·pipe2(SB),NOSPLIT|NOFRAME,$0-20
ADD $FIXED_FRAME+8, R1, R3
MOVW flags+0(FP), R4
SYSCALL $SYS_pipe2
MOVW R3, errno+16(FP)
RET
TEXT runtime·usleep(SB),NOSPLIT,$16-4
MOVW usec+0(FP), R3
MOVD R3, R5
MOVW $1000000, R4
DIVD R4, R3
MOVD R3, 8(R1)
MOVW $1000, R4
MULLD R3, R4
SUB R4, R5
MOVD R5, 16(R1)
// nanosleep(&ts, 0)
ADD $8, R1, R3
MOVW $0, R4
SYSCALL $SYS_nanosleep
RET
TEXT runtime·gettid(SB),NOSPLIT,$0-4
SYSCALL $SYS_gettid
MOVW R3, ret+0(FP)
RET
TEXT runtime·raise(SB),NOSPLIT|NOFRAME,$0
SYSCALL $SYS_getpid
MOVW R3, R14
SYSCALL $SYS_gettid
MOVW R3, R4 // arg 2 tid
MOVW R14, R3 // arg 1 pid
MOVW sig+0(FP), R5 // arg 3
SYSCALL $SYS_tgkill
RET
TEXT runtime·raiseproc(SB),NOSPLIT|NOFRAME,$0
SYSCALL $SYS_getpid
MOVW R3, R3 // arg 1 pid
MOVW sig+0(FP), R4 // arg 2
SYSCALL $SYS_kill
RET
TEXT ·getpid(SB),NOSPLIT|NOFRAME,$0-8
SYSCALL $SYS_getpid
MOVD R3, ret+0(FP)
RET
TEXT ·tgkill(SB),NOSPLIT|NOFRAME,$0-24
MOVD tgid+0(FP), R3
MOVD tid+8(FP), R4
MOVD sig+16(FP), R5
SYSCALL $SYS_tgkill
RET
TEXT runt