Skip to content

Commit 4a89922

Browse files
committed
arm64: klib: Optimised memory functions
This patch introduces AArch64-specific memory functions (memcpy, memmove, memchr, memset). These functions are not optimised for any CPU implementation but can be used as a starting point once hardware is available. Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
1 parent 7920720 commit 4a89922

8 files changed

Lines changed: 246 additions & 2 deletions

File tree

‎arch/arm64/include/asm/Kbuild‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ generic-y += shmbuf.h
3939
generic-y += sizes.h
4040
generic-y += socket.h
4141
generic-y += sockios.h
42-
generic-y += string.h
4342
generic-y += switch_to.h
4443
generic-y += swab.h
4544
generic-y += termbits.h

‎arch/arm64/include/asm/string.h‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (C) 2013 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
#ifndef __ASM_STRING_H
17+
#define __ASM_STRING_H
18+
19+
#define __HAVE_ARCH_MEMCPY
20+
extern void *memcpy(void *, const void *, __kernel_size_t);
21+
22+
#define __HAVE_ARCH_MEMMOVE
23+
extern void *memmove(void *, const void *, __kernel_size_t);
24+
25+
#define __HAVE_ARCH_MEMCHR
26+
extern void *memchr(const void *, int, __kernel_size_t);
27+
28+
#define __HAVE_ARCH_MEMSET
29+
extern void *memset(void *, int, __kernel_size_t);
30+
31+
#endif

‎arch/arm64/kernel/arm64ksyms.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,9 @@ EXPORT_SYMBOL(__atomic_hash);
4646

4747
/* physical memory */
4848
EXPORT_SYMBOL(memstart_addr);
49+
50+
/* string / mem functions */
51+
EXPORT_SYMBOL(memset);
52+
EXPORT_SYMBOL(memcpy);
53+
EXPORT_SYMBOL(memmove);
54+
EXPORT_SYMBOL(memchr);

‎arch/arm64/lib/Makefile‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
lib-y := bitops.o delay.o \
22
strncpy_from_user.o strnlen_user.o clear_user.o \
33
copy_from_user.o copy_to_user.o copy_in_user.o \
4-
copy_page.o clear_page.o
4+
copy_page.o clear_page.o \
5+
memchr.o memcpy.o memmove.o memset.o

‎arch/arm64/lib/memchr.S‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Based on arch/arm/lib/memchr.S
3+
*
4+
* Copyright (C) 1995-2000 Russell King
5+
* Copyright (C) 2013 ARM Ltd.
6+
*
7+
* This program is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License version 2 as
9+
* published by the Free Software Foundation.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include <linux/linkage.h>
21+
#include <asm/assembler.h>
22+
23+
/*
24+
* Find a character in an area of memory.
25+
*
26+
* Parameters:
27+
* x0 - buf
28+
* x1 - c
29+
* x2 - n
30+
* Returns:
31+
* x0 - address of first occurrence of 'c' or 0
32+
*/
33+
ENTRY(memchr)
34+
and w1, w1, #0xff
35+
1: subs x2, x2, #1
36+
b.mi 2f
37+
ldrb w3, [x0], #1
38+
cmp w3, w1
39+
b.ne 1b
40+
sub x0, x0, #1
41+
ret
42+
2: mov x0, #0
43+
ret
44+
ENDPROC(memchr)

‎arch/arm64/lib/memcpy.S‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2013 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <linux/linkage.h>
18+
#include <asm/assembler.h>
19+
20+
/*
21+
* Copy a buffer from src to dest (alignment handled by the hardware)
22+
*
23+
* Parameters:
24+
* x0 - dest
25+
* x1 - src
26+
* x2 - n
27+
* Returns:
28+
* x0 - dest
29+
*/
30+
ENTRY(memcpy)
31+
mov x4, x0
32+
subs x2, x2, #8
33+
b.mi 2f
34+
1: ldr x3, [x1], #8
35+
subs x2, x2, #8
36+
str x3, [x4], #8
37+
b.pl 1b
38+
2: adds x2, x2, #4
39+
b.mi 3f
40+
ldr w3, [x1], #4
41+
sub x2, x2, #4
42+
str w3, [x4], #4
43+
3: adds x2, x2, #2
44+
b.mi 4f
45+
ldrh w3, [x1], #2
46+
sub x2, x2, #2
47+
strh w3, [x4], #2
48+
4: adds x2, x2, #1
49+
b.mi 5f
50+
ldrb w3, [x1]
51+
strb w3, [x4]
52+
5: ret
53+
ENDPROC(memcpy)

‎arch/arm64/lib/memmove.S‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright (C) 2013 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <linux/linkage.h>
18+
#include <asm/assembler.h>
19+
20+
/*
21+
* Move a buffer from src to test (alignment handled by the hardware).
22+
* If dest <= src, call memcpy, otherwise copy in reverse order.
23+
*
24+
* Parameters:
25+
* x0 - dest
26+
* x1 - src
27+
* x2 - n
28+
* Returns:
29+
* x0 - dest
30+
*/
31+
ENTRY(memmove)
32+
cmp x0, x1
33+
b.ls memcpy
34+
add x4, x0, x2
35+
add x1, x1, x2
36+
subs x2, x2, #8
37+
b.mi 2f
38+
1: ldr x3, [x1, #-8]!
39+
subs x2, x2, #8
40+
str x3, [x4, #-8]!
41+
b.pl 1b
42+
2: adds x2, x2, #4
43+
b.mi 3f
44+
ldr w3, [x1, #-4]!
45+
sub x2, x2, #4
46+
str w3, [x4, #-4]!
47+
3: adds x2, x2, #2
48+
b.mi 4f
49+
ldrh w3, [x1, #-2]!
50+
sub x2, x2, #2
51+
strh w3, [x4, #-2]!
52+
4: adds x2, x2, #1
53+
b.mi 5f
54+
ldrb w3, [x1, #-1]
55+
strb w3, [x4, #-1]
56+
5: ret
57+
ENDPROC(memmove)

‎arch/arm64/lib/memset.S‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2013 ARM Ltd.
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License version 2 as
6+
* published by the Free Software Foundation.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include <linux/linkage.h>
18+
#include <asm/assembler.h>
19+
20+
/*
21+
* Fill in the buffer with character c (alignment handled by the hardware)
22+
*
23+
* Parameters:
24+
* x0 - buf
25+
* x1 - c
26+
* x2 - n
27+
* Returns:
28+
* x0 - buf
29+
*/
30+
ENTRY(memset)
31+
mov x4, x0
32+
and w1, w1, #0xff
33+
orr w1, w1, w1, lsl #8
34+
orr w1, w1, w1, lsl #16
35+
orr x1, x1, x1, lsl #32
36+
subs x2, x2, #8
37+
b.mi 2f
38+
1: str x1, [x4], #8
39+
subs x2, x2, #8
40+
b.pl 1b
41+
2: adds x2, x2, #4
42+
b.mi 3f
43+
sub x2, x2, #4
44+
str w1, [x4], #4
45+
3: adds x2, x2, #2
46+
b.mi 4f
47+
sub x2, x2, #2
48+
strh w1, [x4], #2
49+
4: adds x2, x2, #1
50+
b.mi 5f
51+
strb w1, [x4]
52+
5: ret
53+
ENDPROC(memset)

0 commit comments

Comments
 (0)