arch/riscv/lib/strnlen.S

Source file repositories/reference/linux-study-clean/arch/riscv/lib/strnlen.S

File Facts

System
Linux kernel
Corpus path
arch/riscv/lib/strnlen.S
Extension
.S
Size
2893 bytes
Lines
165
Domain
Architecture Layer
Bucket
arch/riscv
Inferred role
Architecture Layer: exported/initcall integration point
Status
integration implementation candidate

Why This File Exists

CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.

Dependency Surface

Detected Declarations

Annotated Snippet

#include <linux/linkage.h>
#include <asm/asm.h>
#include <asm/alternative-macros.h>
#include <asm/hwcap.h>

/* size_t strnlen(const char *s, size_t count) */
SYM_FUNC_START(strnlen)

	__ALTERNATIVE_CFG("nop", "j strnlen_zbb", 0, RISCV_ISA_EXT_ZBB,
		IS_ENABLED(CONFIG_RISCV_ISA_ZBB) && IS_ENABLED(CONFIG_TOOLCHAIN_HAS_ZBB))


	/*
	 * Returns
	 *   a0 - String length
	 *
	 * Parameters
	 *   a0 - String to measure
	 *   a1 - Max length of string
	 *
	 * Clobbers
	 *   t0, t1, t2
	 */
	addi	t1, a0, -1
	add	t2, a0, a1
1:
	addi	t1, t1, 1
	beq	t1, t2, 2f
	lbu	t0, 0(t1)
	bnez	t0, 1b
2:
	sub	a0, t1, a0
	ret


/*
 * Variant of strnlen using the ZBB extension if available
 */
#if defined(CONFIG_RISCV_ISA_ZBB) && defined(CONFIG_TOOLCHAIN_HAS_ZBB)
strnlen_zbb:

#ifdef CONFIG_CPU_BIG_ENDIAN
# define CZ	clz
# define SHIFT	sll
#else
# define CZ	ctz
# define SHIFT	srl
#endif

.option push
.option arch,+zbb

	/*
	 * Returns
	 *   a0 - String length
	 *
	 * Parameters
	 *   a0 - String to measure
	 *   a1 - Max length of string
	 *
	 * Clobbers
	 *   t0, t1, t2, t3, t4
	 */

	/* If maxlen is 0, return 0. */
	beqz	a1, 3f

	/* Number of irrelevant bytes in the first word. */
	andi	t2, a0, SZREG-1

Annotation

Implementation Notes