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

[Question] 内核堆的位置在哪? #153

Open
templklklk opened this issue Nov 20, 2022 · 2 comments
Open

[Question] 内核堆的位置在哪? #153

templklklk opened this issue Nov 20, 2022 · 2 comments
Labels
question Further information is requested

Comments

@templklklk
Copy link

heap_allocator.rs的测试代码可以推出内核堆在bss段。


pub fn heap_test() {
    use alloc::boxed::Box;
    use alloc::vec::Vec;
    extern "C" {
        fn sbss();
        fn ebss();
    }
    let bss_range = sbss as usize..ebss as usize;
    let a = Box::new(5);
    assert_eq!(*a, 5);
    assert!(bss_range.contains(&(a.as_ref() as *const _ as usize)));
    drop(a);
    let mut v: Vec<usize> = Vec::new();
    for i in 0..500 {
        v.push(i);
    }
    for (i, vi) in v.iter().enumerate().take(500) {
        assert_eq!(*vi, i);
    }
    assert!(bss_range.contains(&(v.as_ptr() as usize)));
    drop(v);
    info!("heap_test passed!");
}

请问
1.内核堆在bss段的具体位置,从哪开始到哪结束,在哪段代码设置的?
2.为什么内核堆要在bss段?

@templklklk templklklk added the question Further information is requested label Nov 20, 2022
@CelestialMelody
Copy link

CelestialMelody commented Nov 22, 2022

我也不是很理解这部分,不过这让我联想到一些东西:
我们之前为操作系统分配了操作系统的运行栈,参考 entry.asm 与 linker.ld 文件:

    . = ALIGN(4K);
    edata = .;
    sbss_with_stack = .;
    .bss : {
        *(.bss.stack)
        sbss = .;
        *(.bss .bss.*)
        *(.sbss .sbss.*)
    }

我们常见的内存布局如下:
image

可以看见,堆相关的内存在内存布局中与 .bss 段非常接近,猜测这里是没有刻意在 linker.ld 中指明与 堆相关 的内存布局,而是交给 buddy_system_allocator::LockedHeap 进行处理。

@YdrMaster
Copy link
Collaborator

这里的“内核堆”托管的空间就是一个空白的大数组。“空白的大数组”自然在 .bss 上。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants