feat: Added the "from_pfn" in PhysFrame#593
Conversation
Freax13
left a comment
There was a problem hiding this comment.
Thank you for your contribution!
Yeah, if one is primarily working with PFNs, the conversions can become a bit tedious. Also having written a decent amount of code centered around PFNs recently, I welcome this addition.
| } | ||
| } | ||
|
|
||
| /// Returns the frame by a physical frame number. |
There was a problem hiding this comment.
Let's add a panic section to the doc comment.
Let's also mention that the conversion takes the page size into consideration. For example, converting pfn 512 with page size 4K will not return the same address compared to converting pfn 512 with page size 2M.
The way I usually see it used, pfn is based around the smallest page size on the system. For example, AFAIK, on x86-64, Linux always computes the pfn with a 4K page size even though the hardware also supports other page sizes. Your proposal is not to do that and instead consider the page size. I'm fine with that, I just want to make sure that we communicate that to our users.
There was a problem hiding this comment.
We just use pfn * page size, the panick seems not needed...
There was a problem hiding this comment.
That's fair.
Let's still add a section explaining how we interpret PFNs with respect to different page sizes though.
| /// Also, if you want to return from syscall, you | ||
| /// shall use "sysretq" instead of "syscall". |
There was a problem hiding this comment.
Do you mean 'Also, if you want to return from syscall, you shall use "sysretq" instead of "sysret"'?
| /// Also, if you want to return from syscall, you | |
| /// shall use "sysretq" instead of "syscall". | |
| /// To return to 64-bit user space code, use `sysretq`. | |
| /// To return to 32-bit user space code, use `sysret`. |
There was a problem hiding this comment.
Yes. (my brain boomed). As the issue that you have replied, i use "sysret" wrongly (correct one is "sysretq"). To make other user notice this, i added this comment.
This crate is for "x86_64" architectures, so i don't think we shall mention "use sysret if use 32-bit" (otherwise you should delete that "-16")
*sorry my phone has no backtick and my eng is not well
@Freax13 , I just checked your suggestions and did some improvements. However, it's 11pm for me currently. As a junior high student, I just finish my zhongkao (an important exam), and i can have rest, so that i can continually do more updates tomorrow morning. All in all, good night 🌙 |
|
Seems I have an good idea: Perhaps we can define a enum, |
|
To be honest i have waited for you for a long time... Oh, god, That's freaking long😨😪 |
It's been two days. that's really not that long, lol. You need to be more patient :). Most of have busy lives. I'll take a look now. |
| #[inline] | ||
| #[rustversion::attr(since(1.61), const)] | ||
| pub fn from_start_address(address: PhysAddr) -> Result<Self, AddressNotAligned> { | ||
| pub fn from_start_address(address: PhysAddr) -> Result<Self, PageError> { |
There was a problem hiding this comment.
Changing this would be a breaking change. Let's not do that.
Let's use different error types for from_start_address and from_pfn instead of a single common error type.
There was a problem hiding this comment.
Okay, I will revert to original one then
So, you mean I shall define another struct that is an error type as well
There was a problem hiding this comment.
Changing this would be a breaking change. Let's not do that.
Let's use different error types for
from_start_addressandfrom_pfninstead of a single common error type.
Refactored, currently using another error type, PhysicalAddressNotValid, which is exist here.
| #[rustversion::attr(since(1.61), const)] | ||
| pub fn from_pfn(pfn: u64) -> Result<Self, PageError> { | ||
| // Check: is the PFN overflowed? | ||
| if pfn > (u64::MAX / S::SIZE) { |
There was a problem hiding this comment.
Besides checking for an integer overflow, we also need to make sure that the the result is a valid physical address. Let's not reimplement that part here and just call PhysAddr::try_new instead. Once we have a PhysAddr we can use that to directly construct Self without going through Self::from_pfn_unchecked.
There was a problem hiding this comment.
Well, I even did not notice that fn... But okay, i will try.
| } | ||
| } | ||
|
|
||
| /// Returns the frame by a physical frame number. |
There was a problem hiding this comment.
That's fair.
Let's still add a section explaining how we interpret PFNs with respect to different page sizes though.
Okay, I was too tired because it is very late. I'm sorry about this breaking change. Because it is my developing habit. I will reverse it later and learn more About the APIs. But, I am outside and go back in 1d. Once I backed, I will do the suggested change immediately |
|
Just modified the code as your requirements, see it again :) |
Freax13
left a comment
There was a problem hiding this comment.
Just a heads up. I have some local changes to improve the documentation. I'll push those before merging this PR.
Okay, I will sync once you push that. |
|
I am backed and resolved that problem by using Also, I improved the docs: If the multiplication caused overflow, an Now look look again if you're free :)
|
|
Okay then, I added a new error type |
|
I'd say this is ready. Let's wait for #595 to be merged first, so that CI stops complaining about the recent nightly breakage. |
- Change `cs_sysret - 16` as `cs_sysret` rust-osdev#592
- Added a fn in `PhysFrame` so that we can create a frame easily (instead of converting)
…ctions to implement more about it
- Use `checked_mul` to ensure that the mul operation won't cause panick - Updated docs about `PhysAddrNotValid` to make it much clear rust-osdev#523
- Slightly reword things - Add more examples
InvalidPfn is more in line with the existing error names (VirtAddrNotValid, PhysAddrNotValid). Also move it down in the file. `PhysFrame` is arguably more important, so let's make sure it's at the top of the file.
a7ea828 to
4fcf6a6
Compare
Freax13
left a comment
There was a problem hiding this comment.
LGTM, thanks! ![]()
btw, this is my first PR in my life
Great job on your first PR! Keep up the good work!
You're welcome! I will do more better work in the future😄 I will keep up making good things, as ur wish 😎 |

Preface
Sometimes, I know its PFN number (espeically in frame allocator), but I have to convert it again and again - That's annoying, and may cause performance problem.
So, the
from_pfnis one way to solve this problem - No need to convert, write that shitty long code!Changelog
from_pfnthat was inPhysFrame