/*
 * RISC-V linux replacement vdso.
 *
 * Copyright 2021 Linaro, Ltd.
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include <asm/unistd.h>
#include <asm/errno.h>

#if __riscv_xlen == 32
# define TARGET_ABI32
#endif
#include "vdso-asmoffset.h"

	.text

.macro endf name
	.globl	\name
	.type	\name, @function
	.size	\name, . - \name
.endm

.macro raw_syscall nr
	li	a7, \nr
	ecall
.endm

.macro vdso_syscall name, nr
\name:
	raw_syscall \nr
	ret
endf	\name
.endm

__vdso_gettimeofday:
	.cfi_startproc
#ifdef __NR_gettimeofday
	raw_syscall __NR_gettimeofday
	ret
#else
	/* No gettimeofday, fall back to clock_gettime64. */
	beq	a1, zero, 1f
	sw	zero, 0(a1)	/* tz->tz_minuteswest = 0 */
	sw	zero, 4(a1)	/* tz->tz_dsttime = 0 */
1:	addi	sp, sp, -32
	.cfi_adjust_cfa_offset 32
	sw	a0, 16(sp)	/* save tv */
	mv	a0, sp
	raw_syscall __NR_clock_gettime64
	lw	t0, 0(sp)	/* timespec.tv_sec.low */
	lw	t1, 4(sp)	/* timespec.tv_sec.high */
	lw	t2, 8(sp)	/* timespec.tv_nsec.low */
	lw	a1, 16(sp)	/* restore tv */
	addi	sp, sp, 32
	.cfi_adjust_cfa_offset -32
	bne	a0, zero, 9f	/* syscall error? */
	li	a0, -EOVERFLOW
	bne	t1, zero, 9f	/* y2038? */
	li	a0, 0
	li	t3, 1000
	divu	t2, t2, t3	/* nsec -> usec */
	sw	t0, 0(a1)	/* tz->tv_sec */
	sw	t2, 4(a1)	/* tz->tv_usec */
9:	ret
#endif
	.cfi_endproc
endf __vdso_gettimeofday

	.cfi_startproc

#ifdef __NR_clock_gettime
vdso_syscall __vdso_clock_gettime, __NR_clock_gettime
#else
vdso_syscall __vdso_clock_gettime, __NR_clock_gettime64
#endif

#ifdef __NR_clock_getres
vdso_syscall __vdso_clock_getres, __NR_clock_getres
#else
vdso_syscall __vdso_clock_getres, __NR_clock_getres_time64
#endif

vdso_syscall __vdso_getcpu, __NR_getcpu

__vdso_flush_icache:
	/* qemu does not need to flush the icache */
	li	a0, 0
	ret
endf __vdso_flush_icache

	.cfi_endproc

/*
 * Start the unwind info at least one instruction before the signal
 * trampoline, because the unwinder will assume we are returning
 * after a call site.
 */

	.cfi_startproc simple
	.cfi_signal_frame

#define sizeof_reg	(__riscv_xlen / 4)
#define sizeof_freg	8
#define B_GR	(offsetof_uc_mcontext - sizeof_rt_sigframe)
#define B_FR	(offsetof_uc_mcontext - sizeof_rt_sigframe + offsetof_freg0)

	.cfi_def_cfa	2, sizeof_rt_sigframe

	/* Return address */
	.cfi_return_column 64
	.cfi_offset	64, B_GR + 0			/* pc */

	/* Integer registers */
	.cfi_offset	1, B_GR + 1 * sizeof_reg	/* r1 (ra) */
	.cfi_offset	2, B_GR + 2 * sizeof_reg	/* r2 (sp) */
	.cfi_offset	3, B_GR + 3 * sizeof_reg
	.cfi_offset	4, B_GR + 4 * sizeof_reg
	.cfi_offset	5, B_GR + 5 * sizeof_reg
	.cfi_offset	6, B_GR + 6 * sizeof_reg
	.cfi_offset	7, B_GR + 7 * sizeof_reg
	.cfi_offset	8, B_GR + 8 * sizeof_reg
	.cfi_offset	9, B_GR + 9 * sizeof_reg
	.cfi_offset	10, B_GR + 10 * sizeof_reg
	.cfi_offset	11, B_GR + 11 * sizeof_reg
	.cfi_offset	12, B_GR + 12 * sizeof_reg
	.cfi_offset	13, B_GR + 13 * sizeof_reg
	.cfi_offset	14, B_GR + 14 * sizeof_reg
	.cfi_offset	15, B_GR + 15 * sizeof_reg
	.cfi_offset	16, B_GR + 16 * sizeof_reg
	.cfi_offset	17, B_GR + 17 * sizeof_reg
	.cfi_offset	18, B_GR + 18 * sizeof_reg
	.cfi_offset	19, B_GR + 19 * sizeof_reg
	.cfi_offset	20, B_GR + 20 * sizeof_reg
	.cfi_offset	21, B_GR + 21 * sizeof_reg
	.cfi_offset	22, B_GR + 22 * sizeof_reg
	.cfi_offset	23, B_GR + 23 * sizeof_reg
	.cfi_offset	24, B_GR + 24 * sizeof_reg
	.cfi_offset	25, B_GR + 25 * sizeof_reg
	.cfi_offset	26, B_GR + 26 * sizeof_reg
	.cfi_offset	27, B_GR + 27 * sizeof_reg
	.cfi_offset	28, B_GR + 28 * sizeof_reg
	.cfi_offset	29, B_GR + 29 * sizeof_reg
	.cfi_offset	30, B_GR + 30 * sizeof_reg
	.cfi_offset	31, B_GR + 31 * sizeof_reg	/* r31 */

	.cfi_offset	32, B_FR + 0			/* f0 */
	.cfi_offset	33, B_FR + 1 * sizeof_freg	/* f1 */
	.cfi_offset	34, B_FR + 2 * sizeof_freg
	.cfi_offset	35, B_FR + 3 * sizeof_freg
	.cfi_offset	36, B_FR + 4 * sizeof_freg
	.cfi_offset	37, B_FR + 5 * sizeof_freg
	.cfi_offset	38, B_FR + 6 * sizeof_freg
	.cfi_offset	39, B_FR + 7 * sizeof_freg
	.cfi_offset	40, B_FR + 8 * sizeof_freg
	.cfi_offset	41, B_FR + 9 * sizeof_freg
	.cfi_offset	42, B_FR + 10 * sizeof_freg
	.cfi_offset	43, B_FR + 11 * sizeof_freg
	.cfi_offset	44, B_FR + 12 * sizeof_freg
	.cfi_offset	45, B_FR + 13 * sizeof_freg
	.cfi_offset	46, B_FR + 14 * sizeof_freg
	.cfi_offset	47, B_FR + 15 * sizeof_freg
	.cfi_offset	48, B_FR + 16 * sizeof_freg
	.cfi_offset	49, B_FR + 17 * sizeof_freg
	.cfi_offset	50, B_FR + 18 * sizeof_freg
	.cfi_offset	51, B_FR + 19 * sizeof_freg
	.cfi_offset	52, B_FR + 20 * sizeof_freg
	.cfi_offset	53, B_FR + 21 * sizeof_freg
	.cfi_offset	54, B_FR + 22 * sizeof_freg
	.cfi_offset	55, B_FR + 23 * sizeof_freg
	.cfi_offset	56, B_FR + 24 * sizeof_freg
	.cfi_offset	57, B_FR + 25 * sizeof_freg
	.cfi_offset	58, B_FR + 26 * sizeof_freg
	.cfi_offset	59, B_FR + 27 * sizeof_freg
	.cfi_offset	60, B_FR + 28 * sizeof_freg
	.cfi_offset	61, B_FR + 29 * sizeof_freg
	.cfi_offset	62, B_FR + 30 * sizeof_freg
	.cfi_offset	63, B_FR + 31 * sizeof_freg	/* f31 */

	nop

__vdso_rt_sigreturn:
	raw_syscall __NR_rt_sigreturn
endf __vdso_rt_sigreturn

	.cfi_endproc