-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
arch: riscv: handle interrupt level for CLIC #75581
Open
jimmyzhe
wants to merge
2
commits into
zephyrproject-rtos:main
Choose a base branch
from
jimmyzhe:fixed_riscv_clic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
zephyr_sources(entry.S) | ||
zephyr_sources(soc.c) | ||
zephyr_sources(soc_irq.S) | ||
|
||
zephyr_include_directories(.) | ||
|
||
|
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,18 @@ | ||
/* | ||
* Copyright (c) 2024 Andes Technology Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef SOC_RISCV_GD32_GD32VF103_SOC_CONTEXT_H | ||
#define SOC_RISCV_GD32_GD32VF103_SOC_CONTEXT_H | ||
|
||
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE | ||
|
||
#define SOC_ESF_MEMBERS | ||
|
||
#define SOC_ESF_INIT | ||
|
||
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */ | ||
|
||
#endif /* SOC_RISCV_GD32_GD32VF103_SOC_CONTEXT_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2024 Andes Technology Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/offsets.h> | ||
#include <zephyr/toolchain.h> | ||
#include <zephyr/arch/riscv/irq.h> | ||
|
||
/* Exports */ | ||
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE | ||
GTEXT(__soc_save_context) | ||
GTEXT(__soc_restore_context) | ||
#endif | ||
|
||
#ifdef CONFIG_RISCV_SOC_CONTEXT_SAVE | ||
|
||
SECTION_FUNC(exception.other, __soc_save_context) | ||
|
||
ret | ||
|
||
SECTION_FUNC(exception.other, __soc_restore_context) | ||
|
||
/* | ||
* For Nuclei ECLIC, the interrupt level (mintstatus.MIL) is restored | ||
* from the previous interrupt level (mcause.MPIL) only if | ||
* mcause.interrupt is set when executing MRET. | ||
* Always set the next context's mcause.interrupt to ensure the | ||
* interrupt level is restored correctly after MRET. | ||
*/ | ||
addi a0, a0, -__struct_arch_esf_soc_context_OFFSET | ||
lw t0, __struct_arch_esf_mcause_OFFSET(a0) | ||
li t1, 1 << RISCV_MCAUSE_IRQ_POS | ||
or t0, t0, t1 | ||
sw t0, __struct_arch_esf_mcause_OFFSET(a0) | ||
|
||
ret | ||
|
||
#endif /* CONFIG_RISCV_SOC_CONTEXT_SAVE */ |
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,16 @@ | ||
/* | ||
* Copyright (c) 2024 Andes Technology Corporation | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef SOC_RISCV_GD32_GD32VF103_SOC_OFFSETS_H_ | ||
#define SOC_RISCV_GD32_GD32VF103_SOC_OFFSETS_H_ | ||
|
||
#ifdef CONFIG_RISCV_SOC_OFFSETS | ||
|
||
#define GEN_SOC_OFFSET_SYMS() | ||
|
||
#endif /* CONFIG_RISCV_SOC_OFFSETS */ | ||
|
||
#endif /* SOC_RISCV_GD32_GD32VF103_SOC_OFFSETS_H_*/ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for all this masking instead of just restoring the saved mcause value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's what I mean.
According to CLIC doc, when CLIC mode is enabled,
mcause
has new fieldsmcause.mpp
andmcause.mpie
that mirrormstatus.mpp
andmstatus.mpie
.Restoring the entire
mcause
here pollutes the previousmstatus
settings.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But mstatus is also restored later (line -689/+707), so it doesn't really matter if you "pollute" it?
Besides, if you end up in a situation where the thread context of mcause and mstatus are inconsistent then I'd say you have bigger problems, as the thread state would essentially be corrupt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes,
mcause.mpp
andmcause.mpie
fields are always overwritten by the later storing tomstatus
. The mirror fields inmstatus
andmcause
always remain the same in the stack frame because they are restored together.However, the RISC-V PMP stack guard and userspace mechanism are based on
mstatus.mpp
. I am still figuring out if it is safe whenmcause.mpp
is restored before access to the stack (line -704/+705).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure this is correct? Sounds a bit strange that they would depends on the previous privilege mode (
mpp
) instead of the current one.I don't think
mstatus.mpp
would take any effect until after executingmret
to get back to whatever mode you were in...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I misunderstood the userspace. Only the PMP stack guard is based on
mstatus.mpp
.According to The RISC-V Instruction Set Manual: Volume II 3.1.6.3. Memory Privilege in mstatus Register, we can use
mstatus.mprv
andmstatus.mpp
to perform load/store operations with MMU translation or PMP protection in M-mode.The PMP stack guard has used this to detect thread and interrupt stack overflow since PR #44651.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the restoring for
mcause
. It now saves and restores the entiremcause
and avoids storing to memory between restoringmcause
andmstatus
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aah, I hadn't seen this one before - sorry about that, you were right about MPP being used in some cases. 🙂
I think your updated code looks much better though 👍