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

Get mutable references to the memory information #140

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

YtvwlD
Copy link
Contributor

@YtvwlD YtvwlD commented Jun 19, 2023

This is (hopefully) the last part I split off of #133.

This PR adds the functions basic_memory_info_tag_mut, memory_map_tag_mut adn efi_memory_map_tag_mut. The memory map tags each get their own memory_areas_mut.

This may sound wrong at first and yes, this is really hacky, but I'm afraid that I need this.

The correct way to pass memory information from the bootloader to the kernel would be

  1. gather memory information
  2. build the appropriate tags
  3. pass them into the builder
  4. build the information struct

But steps 2 and 4 allocate memory which in turn might invalidate the gathered memory map.
One could either risk this and just pass possibly outdated information – or do it the other way round:

  1. approximate how much memory areas there are
  2. build the whole information struct
  3. get the correct memory information
  4. get the appropriate tags, mutably
  5. put the information in there

(The downside to this approach is that there may be empty areas at the end (which seems to be fine) or that there are not enough areas.)

multiboot2/src/lib.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@phip1611 phip1611 left a comment

Choose a reason for hiding this comment

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

fine with me. Users of this library could do this anyway with unsafe magic.. so just give them the necessary tools directly.

Please address the two open points. Otherwise, LGTM

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 19, 2023

Oh, yes, I missed that while rebasing.

@phip1611
Copy link
Collaborator

phip1611 commented Jun 19, 2023

But steps 2 and 4 allocate memory which in turn might invalidate the gathered memory map.

Wait. On legacy boot systems, the memory map describes which regions of the physical memory are valid RAM, which is for ACPI, and which is reserved. It does not contain the information that the area where your bootloader lives is "taken". A bootloader has to find this information by itself.. by it's load address and it's length.

Hence, also the hope that an allocation should be reflected in the memory map is wrong/not possible.

In a UEFI boot flow, the memory map reflects which memory is taken by your image/kernel. If you use a heap that is statically backed inside the binary, there is no need to update the memory map.

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 19, 2023

Hm, I'm talking about UEFI and the default allocator: When I create boxes for the tags, they'll get put somewhere on the heap. This might be inside an existing memory region or it might allocate a new LOADER_DATA region.

The legacy map is only really accurate after calling ExitBootServices, I fear (after which the allocator doesn't work anymore).

Doing static allocations might work, but I'm not quite fond of that.

@phip1611
Copy link
Collaborator

phip1611 commented Jun 20, 2023

// SAFETY: BootInformation contains a const ptr to memory that is never mutated.
// Sending this pointer to other threads is sound.
unsafe impl Send for BootInformation {}

If we merge this, this needs to be removed, probably. Another breaking change. I think this could be okay, as one can wrap the MBI in a (custom) Mutex..

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 20, 2023

Huh, I don't know? The struct does need to be mutable for these methods to be called, but I'm not sure that's enough.

@phip1611
Copy link
Collaborator

I'm going to postpone this a bit and merge a few other things first. The rebase needed here will have a few conflicts - I'm sorry for that!

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 26, 2023

Sure, just ping me when I should rebase.

@phip1611
Copy link
Collaborator

ping :)

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 26, 2023

So, this works, but it really moves the mut through the whole codebase and I'm not happy about it.

Edit: It's somehow usable, but the lifetimes of the _mut methods are wrong. I'm not able to set all memory information.

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jun 26, 2023

I don't understand the miri failure, but this has better lifetimes and the integration test should work. I'm still not happy about having to touch so much code.

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jul 3, 2023

Using AsMut might help with not-carrying-mut-through-the-whole-code, but I'm unsure whether this would look any better.

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jul 9, 2023

I think this looks better, though I'm not sure combining EFIMemoryAreaIter and EFIMemoryAreaIterMut is the way to go. (Combining TagIter and TagIterMut didn't work because there's pointers in them.)

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jul 9, 2023

That unnecessary pub(self) wasn't introduced by this pull request.

@phip1611
Copy link
Collaborator

phip1611 commented Jul 13, 2023

After thinking a lot about this, I came to the conclusion that this is indeed helpful. However, I'm unsure if the proposed design is too invasive. I think, there is a simpler approach. Let me try something and I'll get back to you! :) Thanks for your patience!

TL;DR: My idea is to use UnsafeCell under the hood

@YtvwlD
Copy link
Contributor Author

YtvwlD commented Jul 14, 2023

Oh, great!

(Don't mind me, this is just a rebase.)

@phip1611 phip1611 added this to the multiboot2: v0.20 milestone Sep 21, 2023
@YtvwlD
Copy link
Contributor Author

YtvwlD commented Apr 15, 2024

(This is just a rebase.)

@phip1611 phip1611 self-requested a review April 15, 2024 12:18
@phip1611
Copy link
Collaborator

Hey. I somehow forgot about this, sorry. I'll look into it soon, hopefully. But I'm already concerned about API complexity and cognitive load

@phip1611 phip1611 self-assigned this Apr 15, 2024
@YtvwlD
Copy link
Contributor Author

YtvwlD commented Apr 26, 2024

Yeah sure, take your time. I think this is the simplest change I can come up with right now, but if you find a simpler one, that'd be great.

@phip1611 phip1611 removed this from the multiboot2: v0.20 milestone May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants