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

Support for global allocator #352

Open
piosystems opened this issue Mar 14, 2023 · 8 comments
Open

Support for global allocator #352

piosystems opened this issue Mar 14, 2023 · 8 comments

Comments

@piosystems
Copy link

Is there any support for global allocator. Concatenating string slices is not feasible without heap allocator.

@piosystems
Copy link
Author

I have attempted using the following code to setup default global allocator. Any concrete example on how this could work with bootloader?

use alloc::alloc::*;

/// The global allocator type.
#[derive(Default)]
pub struct Allocator;

unsafe impl GlobalAlloc for Allocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc(layout.size() as u32) as *mut u8
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
free(ptr as *mut c_void);
}
}

/// If there is an out of memory error, just panic.
#[alloc_error_handler]
fn my_allocator_error(_layout: Layout) -> ! {
panic!("out of memory");
}

/// The static global allocator.
#[global_allocator]
static GLOBAL_ALLOCATOR: Allocator = Allocator;

@phil-opp
Copy link
Member

The bootloader does no longer exist after transferring control to your kernel. So you have to implement the memory management yourself. This includes page table table allocation and creating your own allocator. See the "Memory Management" section on https://os.phil-opp.com/ for details.

If you don't want to manage memory, you could create an UEFI application directly. See https://github.com/rust-osdev/uefi-rs/tree/main/template for an example. By using the uefi crate you have direct access to the UEFI boot services provided by the firmware. This allows you to allocate pages through the BootServices::allocate_pool function. You can also include the higher-level uefi-services crate, which will automaticall set up a global allocator for you.

Hope this help!

@piosystems
Copy link
Author

Thank you for the feedback. I am following your "Memory Management" section on https://os.phil-opp.com/ but the flow seem not to be compatible with bootloader_api 0.11.x. For example, the newer bootinfo has no memory_map member anymore which is expected by memory::BootInfoFrameAllocator. Instead, there is memory_regions which is available for use. Can you post a quick example update of memory allocation that works with bootloader_api 0.11.x?

@piosystems
Copy link
Author

piosystems commented Mar 21, 2023

Thank you for the feedback. I am following your "Memory Management" section on https://os.phil-opp.com/ but the flow seem not to be compatible with bootloader_api 0.11.x. For example, the newer bootinfo has no memory_map member anymore which is expected by memory::BootInfoFrameAllocator. Instead, there is memory_regions which is available for use. Can you post a quick example update of memory allocation that works with bootloader_api 0.11.x?

I am still stuck with the fact that for bootloader_api 0.11.x, the boot_info: &'static mut bootloader_api::BootInfo passed to my entry_point does not have memory_map, contrary to what is indicated in https://os.phil-opp.com/ which seems to have been written for earlier versions. Attempts to find ways to use the Allocators in https://os.phil-opp.com/ have been unsuccessful. Am I missing something? Can you give an example of the use of FixedSizeBlockAllocator with bootloader_api 0.11.x?

@yavko
Copy link

yavko commented Mar 22, 2023

Thank you for the feedback. I am following your "Memory Management" section on os.phil-opp.com but the flow seem not to be compatible with bootloader_api 0.11.x. For example, the newer bootinfo has no memory_map member anymore which is expected by memory::BootInfoFrameAllocator. Instead, there is memory_regions which is available for use. Can you post a quick example update of memory allocation that works with bootloader_api 0.11.x?

I am still stuck with the fact that for bootloader_api 0.11.x, the boot_info: &'static mut bootloader_api::BootInfo passed to my entry_point does not have memory_map, contrary to what is indicated in os.phil-opp.com which seems to have been written for earlier versions. Attempts to find ways to use the Allocators in os.phil-opp.com have been unsuccessful. Am I missing something? Can you give an example of the use of FixedSizeBlockAllocator with bootloader_api 0.11.x?

I'm pretty sure its been replaced with memory_regions

@piosystems
Copy link
Author

I'm pretty sure its been replaced with memory_regions

How do you initialize any Allocator of choice with addresses indicated in memory_regions? Any working example?

@yavko
Copy link

yavko commented Mar 22, 2023

I'm pretty sure its been replaced with memory_regions

How do you initialize any Allocator of choice with addresses indicated in memory_regions? Any working example?

i think like so, though havent been able to test (with BootInfoFrameAllocator):

initialization

image

Updated frame allocator

image
image
image

@piosystems
Copy link
Author

piosystems commented Mar 22, 2023

I'm pretty sure its been replaced with memory_regions

How do you initialize any Allocator of choice with addresses indicated in memory_regions? Any working example?

i think like so, though havent been able to test (with BootInfoFrameAllocator):

initialization

image

Updated frame allocator

image image image

From what I can see in your posted snippets, it is obvious that my codes are not up to date. Can you point me to the updated repository content url?

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

No branches or pull requests

3 participants