-
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.
riscv: Add jump-label implementation
Add jump-label implementation based on the ARM64 version and add CONFIG_JUMP_LABEL=y to the defconfigs. Signed-off-by: Emil Renner Berthing <kernel@esmil.dk> Reviewed-by: Björn Töpel <bjorn.topel@gmail.com> Tested-by: Björn Töpel <bjorn.topel@gmail.com> Signed-off-by: Palmer Dabbelt <palmerdabbelt@google.com>
- Loading branch information
Emil Renner Berthing
authored and
Palmer Dabbelt
committed
Jul 30, 2020
1 parent
11a54f4
commit ebc00dd
Showing
9 changed files
with
122 additions
and
1 deletion.
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
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,60 @@ | ||
/* SPDX-License-Identifier: GPL-2.0-only */ | ||
/* | ||
* Copyright (C) 2020 Emil Renner Berthing | ||
* | ||
* Based on arch/arm64/include/asm/jump_label.h | ||
*/ | ||
#ifndef __ASM_JUMP_LABEL_H | ||
#define __ASM_JUMP_LABEL_H | ||
|
||
#ifndef __ASSEMBLY__ | ||
|
||
#include <linux/types.h> | ||
#include <asm/asm.h> | ||
|
||
#define JUMP_LABEL_NOP_SIZE 4 | ||
|
||
static __always_inline bool arch_static_branch(struct static_key *key, | ||
bool branch) | ||
{ | ||
asm_volatile_goto( | ||
" .option push \n\t" | ||
" .option norelax \n\t" | ||
" .option norvc \n\t" | ||
"1: nop \n\t" | ||
" .option pop \n\t" | ||
" .pushsection __jump_table, \"aw\" \n\t" | ||
" .align " RISCV_LGPTR " \n\t" | ||
" .long 1b - ., %l[label] - . \n\t" | ||
" " RISCV_PTR " %0 - . \n\t" | ||
" .popsection \n\t" | ||
: : "i"(&((char *)key)[branch]) : : label); | ||
|
||
return false; | ||
label: | ||
return true; | ||
} | ||
|
||
static __always_inline bool arch_static_branch_jump(struct static_key *key, | ||
bool branch) | ||
{ | ||
asm_volatile_goto( | ||
" .option push \n\t" | ||
" .option norelax \n\t" | ||
" .option norvc \n\t" | ||
"1: jal zero, %l[label] \n\t" | ||
" .option pop \n\t" | ||
" .pushsection __jump_table, \"aw\" \n\t" | ||
" .align " RISCV_LGPTR " \n\t" | ||
" .long 1b - ., %l[label] - . \n\t" | ||
" " RISCV_PTR " %0 - . \n\t" | ||
" .popsection \n\t" | ||
: : "i"(&((char *)key)[branch]) : : label); | ||
|
||
return false; | ||
label: | ||
return true; | ||
} | ||
|
||
#endif /* __ASSEMBLY__ */ | ||
#endif /* __ASM_JUMP_LABEL_H */ |
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,53 @@ | ||
// SPDX-License-Identifier: GPL-2.0-only | ||
/* | ||
* Copyright (C) 2020 Emil Renner Berthing | ||
* | ||
* Based on arch/arm64/kernel/jump_label.c | ||
*/ | ||
#include <linux/jump_label.h> | ||
#include <linux/kernel.h> | ||
#include <linux/memory.h> | ||
#include <linux/mutex.h> | ||
#include <asm/bug.h> | ||
#include <asm/patch.h> | ||
|
||
#define RISCV_INSN_NOP 0x00000013U | ||
#define RISCV_INSN_JAL 0x0000006fU | ||
|
||
void arch_jump_label_transform(struct jump_entry *entry, | ||
enum jump_label_type type) | ||
{ | ||
void *addr = (void *)jump_entry_code(entry); | ||
u32 insn; | ||
|
||
if (type == JUMP_LABEL_JMP) { | ||
long offset = jump_entry_target(entry) - jump_entry_code(entry); | ||
|
||
if (WARN_ON(offset & 1 || offset < -524288 || offset >= 524288)) | ||
return; | ||
|
||
insn = RISCV_INSN_JAL | | ||
(((u32)offset & GENMASK(19, 12)) << (12 - 12)) | | ||
(((u32)offset & GENMASK(11, 11)) << (20 - 11)) | | ||
(((u32)offset & GENMASK(10, 1)) << (21 - 1)) | | ||
(((u32)offset & GENMASK(20, 20)) << (31 - 20)); | ||
} else { | ||
insn = RISCV_INSN_NOP; | ||
} | ||
|
||
mutex_lock(&text_mutex); | ||
patch_text_nosync(addr, &insn, sizeof(insn)); | ||
mutex_unlock(&text_mutex); | ||
} | ||
|
||
void arch_jump_label_transform_static(struct jump_entry *entry, | ||
enum jump_label_type type) | ||
{ | ||
/* | ||
* We use the same instructions in the arch_static_branch and | ||
* arch_static_branch_jump inline functions, so there's no | ||
* need to patch them up here. | ||
* The core will call arch_jump_label_transform when those | ||
* instructions need to be replaced. | ||
*/ | ||
} |