Skip to content
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

riscv: sfence.vma inline assembly is miscompiled #63747

Closed
X547 opened this issue Jul 7, 2023 · 4 comments
Closed

riscv: sfence.vma inline assembly is miscompiled #63747

X547 opened this issue Jul 7, 2023 · 4 comments
Labels
ABI Application Binary Interface backend:RISC-V

Comments

@X547
Copy link
Contributor

X547 commented Jul 7, 2023

Clang: https://godbolt.org/z/afaMvnMrr
GCC: https://godbolt.org/z/nYEK9ozjP

Note that Clang use hardwired zero X0 register for both global and ASID 0 sfence.vma instruction, but GCC avoid using X0 when using ASID.

From RISC-V privileged spec:

If rs1=x0 and rs2=x0, the fence orders all reads and writes made to any level of the page
tables, for all address spaces. The fence also invalidates all address-translation cache entries,
for all address spaces.

If rs1=x0 and rs2̸=x0, the fence orders all reads and writes made to any level of the page
tables, but only for the address space identified by integer register rs2. Accesses to global
mappings (see Section 4.3.1) are not ordered. The fence also invalidates all address-translation
cache entries matching the address space identified by integer register rs2, except for entries
containing global mappings.

If the implementation does not provide ASIDs, or software chooses to always use ASID 0,
then after every satp write, software should execute SFENCE.VMA with rs1=x0. In the
common case that no global translations have been modified, rs2 should be set to a register
other than x0 but which contains the value zero
, so that global translations are not flushed.

@EugeneZelenko EugeneZelenko added backend:RISC-V ABI Application Binary Interface and removed new issue labels Jul 7, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 7, 2023

@llvm/issue-subscribers-backend-risc-v

@topperc
Copy link
Collaborator

topperc commented Jul 7, 2023

The inline assembly instruction isn't parsed by the compiler. So I think this must mean that gcc will never use x0 to pass 0 to an 'r' constraint?

@X547
Copy link
Contributor Author

X547 commented Jul 7, 2023

So I think this must mean that gcc will never use x0 to pass 0 to an 'r' constraint?

Yes it seems: https://godbolt.org/z/va6MoM7nb

It is not safe to automatically use X0 for zero constant in inline assembly because X0 is sometimes used to encode different instruction, for example sfence.vma to invalidate ASID, not global pages.

@topperc
Copy link
Collaborator

topperc commented Jul 7, 2023

Candidate patch https://reviews.llvm.org/D154744

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ABI Application Binary Interface backend:RISC-V
Projects
None yet
Development

No branches or pull requests

4 participants