-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RISC-V: add infrastructure to allow different str* implementations
Depending on supported extensions on specific RISC-V cores, optimized str* functions might make sense. This adds basic infrastructure to allow patching the function calls via alternatives later on. The Linux kernel provides standard implementations for string functions but when architectures want to extend them, they need to provide their own. The added generic string functions are done in assembler (taken from disassembling the main-kernel functions for now) to allow us to control the used registers and extend them with optimized variants. This doesn't override the compiler's use of builtin replacements. So still first of all the compiler will select if a builtin will be better suitable i.e. for known strings. For all regular cases we will want to later select possible optimized variants and in the worst case fall back to the generic implemention added with this change. Reviewed-by: Andrew Jones <ajones@ventanamicro.com> Signed-off-by: Heiko Stuebner <heiko.stuebner@vrull.eu> Reviewed-by: Conor Dooley <conor.dooley@microchip.com> Link: https://lore.kernel.org/r/20230113212301.3534711-2-heiko@sntech.de Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
- Loading branch information
Heiko Stuebner
authored and
Palmer Dabbelt
committed
Jan 31, 2023
1 parent
61a9b71
commit 56e0790
Showing
7 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
|
||
#include <linux/linkage.h> | ||
#include <asm/asm.h> | ||
#include <asm-generic/export.h> | ||
|
||
/* int strcmp(const char *cs, const char *ct) */ | ||
SYM_FUNC_START(strcmp) | ||
/* | ||
* Returns | ||
* a0 - comparison result, value like strcmp | ||
* | ||
* Parameters | ||
* a0 - string1 | ||
* a1 - string2 | ||
* | ||
* Clobbers | ||
* t0, t1 | ||
*/ | ||
1: | ||
lbu t0, 0(a0) | ||
lbu t1, 0(a1) | ||
addi a0, a0, 1 | ||
addi a1, a1, 1 | ||
bne t0, t1, 2f | ||
bnez t0, 1b | ||
li a0, 0 | ||
ret | ||
2: | ||
/* | ||
* strcmp only needs to return (< 0, 0, > 0) values | ||
* not necessarily -1, 0, +1 | ||
*/ | ||
sub a0, t0, t1 | ||
ret | ||
SYM_FUNC_END(strcmp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
|
||
#include <linux/linkage.h> | ||
#include <asm/asm.h> | ||
#include <asm-generic/export.h> | ||
|
||
/* int strlen(const char *s) */ | ||
SYM_FUNC_START(strlen) | ||
/* | ||
* Returns | ||
* a0 - string length | ||
* | ||
* Parameters | ||
* a0 - String to measure | ||
* | ||
* Clobbers: | ||
* t0, t1 | ||
*/ | ||
mv t1, a0 | ||
1: | ||
lbu t0, 0(t1) | ||
beqz t0, 2f | ||
addi t1, t1, 1 | ||
j 1b | ||
2: | ||
sub a0, t1, a0 | ||
ret | ||
SYM_FUNC_END(strlen) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
|
||
#include <linux/linkage.h> | ||
#include <asm/asm.h> | ||
#include <asm-generic/export.h> | ||
|
||
/* int strncmp(const char *cs, const char *ct, size_t count) */ | ||
SYM_FUNC_START(strncmp) | ||
/* | ||
* Returns | ||
* a0 - comparison result, value like strncmp | ||
* | ||
* Parameters | ||
* a0 - string1 | ||
* a1 - string2 | ||
* a2 - number of characters to compare | ||
* | ||
* Clobbers | ||
* t0, t1, t2 | ||
*/ | ||
li t2, 0 | ||
1: | ||
beq a2, t2, 2f | ||
lbu t0, 0(a0) | ||
lbu t1, 0(a1) | ||
addi a0, a0, 1 | ||
addi a1, a1, 1 | ||
bne t0, t1, 3f | ||
addi t2, t2, 1 | ||
bnez t0, 1b | ||
2: | ||
li a0, 0 | ||
ret | ||
3: | ||
/* | ||
* strncmp only needs to return (< 0, 0, > 0) values | ||
* not necessarily -1, 0, +1 | ||
*/ | ||
sub a0, t0, t1 | ||
ret | ||
SYM_FUNC_END(strncmp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters