1/*
2 * Copyright (c) BayLibre SAS
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <zephyr/toolchain.h>
8#include <zephyr/linker/sections.h>
9
10_ASM_FILE_PROLOGUE
11
12/*
13 * These simple memset and memcpy alternatives are necessary as the optimized
14 * ones depend on the MMU to be active (see commit c5b898743a20).
15 *
16 * Furthermore, we can't implement those in C as the compiler is just too
17 * smart for its own good and replaces our simple loops into direct calls
18 * to memset or memcpy on its own.
19 */
20
21/* void z_early_memset(void *dst, int c, size_t n) */
22GTEXT(z_early_memset)
23SECTION_FUNC(TEXT, z_early_memset)
24
25	/* is dst pointer 8-bytes aligned? */
26	tst	x0, #0x7
27	b.ne	2f
28
29	/* at least 8 bytes to set? */
30	cmp	x2, #8
31	b.lo	2f
32
33	/* spread the byte value across whole 64 bits */
34	and	x8, x1, #0xff
35        mov	x9, #0x0101010101010101
36	mul	x8, x8, x9
37
381:	/* 8 bytes at a time */
39	sub	x2, x2, #8
40	cmp	x2, #7
41	str	x8, [x0], #8
42	b.hi	1b
43
442:	/* at least one byte to set? */
45	cbz	x2, 4f
46
473:	/* one byte at a time */
48	subs	x2, x2, #1
49	strb	w8, [x0], #1
50	b.ne	3b
51
524:	ret
53
54/* void z_early_memcpy(void *dst, const void *src, size_t n) */
55GTEXT(z_early_memcpy)
56SECTION_FUNC(TEXT, z_early_memcpy)
57
58	/* are dst and src pointers 8-bytes aligned? */
59	orr	x8, x1, x0
60	tst	x8, #0x7
61	b.ne	2f
62
63	/* at least 8 bytes to copy? */
64	cmp	x2, #8
65	b.lo	2f
66
671:	/* 8 bytes at a time */
68	ldr	x8, [x1], #8
69	sub	x2, x2, #8
70	cmp	x2, #7
71	str	x8, [x0], #8
72	b.hi	1b
73
742:	/* at least one byte to copy? */
75	cbz	x2, 4f
76
773:	/* one byte at a time */
78	ldrb	w8, [x1], #1
79	subs	x2, x2, #1
80	strb	w8, [x0], #1
81	b.ne	3b
82
834:	ret
84