Page 1 of 1

How to define different [global_allocator]s

Posted: Tue Mar 05, 2024 2:01 pm
by ebeneEinHalb
Hey everyone, we're in need of someone well-versed in Rust:
We're currently working on a monolithic operating system (x86) in Rust. Our cargo workspace looks roughly like this:

Code: Select all

|--src (contains all kernel related code, omitted here)
|  |--main.rs
|  |--...
|
|--user
|  |--arch
|  |  |--link.ld
|  |--bin
|     |--app1.rs
|     |--app2.rs
|
|--Cargo.toml
|--build.rs
During the compilation of the project workspace we create a initrd containing the flattened binaries of the user directory:

Code: Select all

objcopy -O binary --set-section-flags .bss=alloc,load,contents <app> <flat binary>
The kernel later copies these binaries at runtime from the initrd into their corresponding address space.
Now we want to use different heap allocators for the userspace applications and the kernel. We created a heap allocator for our kernel within our kernelspace (<32MiB):

Code: Select all

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();
// Later initialised in main
The issue arises when we try to create a #[global_allocator] in one of our applications now:

Code: Select all

#![no_std]
#![no_main]

extern crate alloc;

use os::syscall;

#[global_allocator]
static ALLOCATOR: LockedHeap = LockedHeap::empty();

#[no_mangle]
pub extern "C" fn start() -> ! {
    let mem_size = 3 * Page::SIZE;
    let mem = syscall::mmap(mem_size);
    unsafe {
        ALLOCATOR.lock().init(mem, mem_size);
    }
    
    let v = vec![1, 2, 3];
    syscall::serial!("{:?}", v);

    syscall::exit();
}
Running this application pretty much immediately triggers a pagefault at a memory location inside our kernel code (~above 1MiB).

So how do we need to change our setup/code to enable the user space applications to use a different heap so they can make use of the alloc crate?

We've tried compiling the user applications in a completely different workspace, under the assumption that the combined workspace was the root of this problem, but this didn't help.
We've also explored the option of using the new 'https://github.com/rust-lang/wg-allocators/issues/7' feature, but this isn't really an option since its still a WIP and even if you only use 'new_in(...)' and friends you still need to at least declare a global_allocator.

Help would be greatly appreciated :).