1/*
2 * Userspace and service handler hooks
3 *
4 * Copyright (c) 2020 BayLibre, SAS
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9#include <zephyr/toolchain.h>
10#include <zephyr/linker/sections.h>
11#include <offsets_short.h>
12#include <zephyr/arch/cpu.h>
13#include <zephyr/syscall.h>
14#include <zephyr/kernel_structs.h>
15#include <zephyr/arch/riscv/csr.h>
16#include "asm_macros.inc"
17
18/* exports */
19GTEXT(arch_user_string_nlen)
20GTEXT(z_riscv_user_string_nlen_fault_start)
21GTEXT(z_riscv_user_string_nlen_fault_end)
22GTEXT(z_riscv_user_string_nlen_fixup)
23
24/*
25 * size_t arch_user_string_nlen(const char *s, size_t maxsize, int *err_arg)
26 */
27SECTION_FUNC(TEXT, arch_user_string_nlen)
28	li	a5, 0		# Counter
29	sw	a5, 0(a2)	# Init error value to 0
30
31loop:
32	add	a4, a0, a5	# Determine character address
33z_riscv_user_string_nlen_fault_start:
34	lbu	a4, 0(a4)	# Load string's character
35z_riscv_user_string_nlen_fault_end:
36	beqz	a4, exit	# Test string's end of line
37
38	bne	a5, a1, continue	# Check if max length is reached
39
40exit:
41	mv	a0, a5		# Return counter value (length)
42	ret
43
44continue:
45	addi	a5, a5, 1	# Increment counter
46	j	loop
47
48z_riscv_user_string_nlen_fixup:
49	li	a4, -1		# Put error to -1
50	sw	a4, 0(a2)
51	j	exit
52