Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

lschuermann
Copy link
Member

@lschuermann lschuermann commented Jan 18, 2021

Pull Request Overview

This pull request transitions the remaining llvm_asm! snippet of the rv32i towards the new Rust asm! macro (see #1923 for the respective tracking issue). It's split up into commits that represent atomic changes respectively.

In addition to the raw transition, this changes two things

  • it removes the unused abort() / _abort fuction. It was previously public & safe, such that untrusted code could simply call to it.

  • in switch_to_process, it forces the Riscv32iStoredState pointer to be passed in a specific register. This is important since, while we don't clobber any registers in the assembly block there, we do write to t0 after we've saved it to the stack. Later, we access the stored state pointer (from some unspecified register). If the compiler were to put this information in t0, we would access the overwritten contents instead. By fixing the state pointer to always be passed in a0 (which the compiler has done up to now), we can rely on being able to safely overwrite t0 after it's been saved on the stack.

    Notably, knowing the register where the stored state is passed in potentially enables an optimization where we don't need to save it to the stack twice (and hence save a memory write & read). However, implementing that made the ASM quite a bit more confusing, so I might implement that as a follow up PR, once I find the time to write it in a clean fashion.

All in all, the abort function assembly is removed

- 00015210 <abort>:
-    15210:	df1ea06f          	j	0 <_start>
-    15214:	00008067          	ret

and the switch_to_process function seems to generate the exact same assembly.

One weird thing about asm!(): I tried to replace the lui + addi based immediate load by a li pseudo-instruction, however the compiler complained that _return_to_kernel was not a valid immediate value there, whereas it continues to work with the %hi / %lo accessors. The solution™ is to insert the immediate value as a constant, however we can't refer to an assembly label there. Hence leaving it the way it is.

Testing Strategy

This pull request was tested by running a userspace app in the LiteX simulator and on the LiteX Arty board.

TODO or Help Wanted

This pull request still needs careful review. I can recommend stepping through the commits individually such that the changes can be reviewed separately.

Documentation Updated

  • Updated the relevant files in /docs, or no updates are required.

Formatting

  • Ran make prepush.

@github-actions github-actions bot added the arch/risc-v RISC-V architecture label Jan 18, 2021
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add all of the "", on each line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen some examples of other people using asm!() doing that. It has the added benefit of my editor and rustfmt aligning the instructions correctly and detecting whether I'm using the correct, spaces-based indentation. I think either would be fine with me though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wish they would add more examples to the book. The longest assembly block there is just two instructions.

Copy link
Member Author

@lschuermann lschuermann Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that the documentation is very sparse. Reading through the documentation again seems that using multiple strings was chosen exactly for the benefits I've noticed while using it (indentation, proper Rust comments). To cite them: "First, we can see that asm! allows multiple template string arguments; each one is treated as a separate line of assembly code, as if they were all joined together with newlines between them. This makes it easy to format assembly code."

As I said both is fine with me. Should I change it to be a multiline string?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm typically on the side of smaller commits are better. However, I'm also a believer that if rustfmt doesn't enforce it then it's completely up to the author. I am worried about a collision with my stack pointer PR however :)

alistair23
alistair23 previously approved these changes Jan 19, 2021
hudson-ayers
hudson-ayers previously approved these changes Jan 22, 2021
lschuermann and others added 5 commits February 4, 2021 11:57
This assures that the compiler may never choose to pass the pointer to
the Riscv32iStoredState in a register that is otherwise used and
overwritten in the assembly (e.g. t0).

Presumably this is a not a general issue, but specific to this
assembly block, as we first store all registers and later restore
them, but still rely on the values of the registers further down in
the assembly, while overwriting specific ones with temporary values.
This potentially dangerous function is not referenced anywhere and
hence can be removed, reducing the amount of unsafe code.
@bradjc bradjc dismissed stale reviews from hudson-ayers and alistair23 via 20c0698 February 4, 2021 16:59
@bradjc bradjc force-pushed the dev/rv32i-rust-asm branch from bc5c2e7 to 20c0698 Compare February 4, 2021 16:59
@bradjc
Copy link
Contributor

bradjc commented Feb 4, 2021

I did the rebase needed because of my stack pointer changes, and I simplified the PR to just the changes needed which makes the diff much easier to compare.

@bradjc bradjc added the last-call Final review period for a pull request. label Feb 4, 2021
@lschuermann
Copy link
Member Author

lschuermann commented Feb 5, 2021

simplified the PR to just the changes needed which makes the diff much easier to compare

Fair enough. I have a slight preference over the other syntax, but in the end what really matters is that we have a consistent style around the Tock kernel. If we were to change it to the multi-string syntax (which the book seems to have a slight preference over) we should do so throughout the kernel, in a separate commit.

Thanks for doing the work.

@bradjc
Copy link
Contributor

bradjc commented Feb 5, 2021

There is certainly discussion about this: rust-lang/style-team#152. But I assume rustfmt will not actually add the "", on each line, and I don't think we will actually be able to keep it consistent. But that doesn't mean we can't try.

@bradjc
Copy link
Contributor

bradjc commented Feb 5, 2021

bors r+

@lschuermann
Copy link
Member Author

lschuermann commented Feb 5, 2021

I don't think we will actually be able to keep it consistent. But that doesn't mean we can't try.

I didn't want to imply we must switch, and my personal preference probably doesn't matter much for the project. Sorry if my statements were a bit confusing.

I haven't seen the rustfmt discussion, thanks for sharing the link. Maybe the best approach is to let dust settle there and see what the most commonly accepted and preferred syntax is in a few months?

@bors
Copy link
Contributor

bors bot commented Feb 5, 2021

@bors bors bot merged commit 1182173 into tock:master Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch/risc-v RISC-V architecture last-call Final review period for a pull request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants