Commit 13d08cf
committed
Don't inline zero_page function
There is a current crash in patina on x86 when zero_page gets inlined in
a certain way, small changes to the code generation cause no
crash to occur. However, when zero_page gets inlined on x86, rdi
in the outer function can get clobbered and not restored, which causes
undefined behavior. In the example seen, the root page of the page table
is supposed to be 0x50000, stored in rdi, but rdi gets used to track
incrementing the zeroing writes to this page and ends up at 0x51000,
which gets stored as the page table root, causing a write because there
is a mismatch when this gets used later.
There is a greater investigation needed here in two parts:
- Whether this function is needed to be written in assembly. It was
written so to avoid the compiler from optimizing out the memory write
that it thinks may not be used, but after discussion with the compiler
team, this can be avoided another way. However, that move is considered
risky, as if the compiler decides to throw away a zeroing of the page
table, we will have garbage entries, causing undefined behavior.
- Whether this function should be written in a separate asm file as a
global_asm import. Doing this avoids the use of inline asm and puts all
register usage in our control. However, it also removes the niceties of
inline_asm, namely only doing asm for the parts required to be so and
doing the rest in Rust. Also, this is a larger change and so considered
risky.
In order to mitigate the current crash, the simplest change is made
first: don't allow the zero_page functions to be inlined. This was
seen to boot and under a debugger the proper push/pop of rdi was
occurring. This is done for AARCH64 as well, even though it does not
use explicit registers (x86 must in order to use the rep instruction)
in order to keep the implementation similar and avoid similar potential
problems. Other AARCH64 asm was reviewed and determined to not use
explicit general purpose registers, and so left alone.
Finally, the x86 function was incorrectly declaring that it preserves_flags,
when it in fact explicitly clears the direction flag, so that is removed.
There is a task filed in patina to review all usage of inline asm in
patina and associated repos because there is a high chance that it has
been done incorrectly; there are many common pitfalls that the Rust
compiler can take when special care is not given to inline asm.1 parent 1a35492 commit 13d08cf
2 files changed
+8
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
360 | 360 | | |
361 | 361 | | |
362 | 362 | | |
| 363 | + | |
| 364 | + | |
363 | 365 | | |
364 | 366 | | |
365 | 367 | | |
366 | 368 | | |
| 369 | + | |
367 | 370 | | |
368 | 371 | | |
369 | 372 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
92 | 96 | | |
93 | 97 | | |
94 | 98 | | |
| |||
99 | 103 | | |
100 | 104 | | |
101 | 105 | | |
102 | | - | |
| 106 | + | |
103 | 107 | | |
104 | 108 | | |
105 | 109 | | |
| |||
0 commit comments