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

Higher Half? #159

Closed
Andy-Python-Programmer opened this issue May 13, 2021 · 7 comments · Fixed by #161
Closed

Higher Half? #159

Andy-Python-Programmer opened this issue May 13, 2021 · 7 comments · Fixed by #161

Comments

@Andy-Python-Programmer
Copy link

Will be nice to support higher half kernels.

@phil-opp
Copy link
Member

Have you tried to specify a custom virtual base address via an --image-base <addr> argument when building your kernel? Alternatively, you can use a linker script for this. The bootloader should be able to load the kernel into the virtual address space at exactly the specified address, which should also be possible with addresses in the higher half. I say "should" because I have not tested this yet since the UEFI rewrite. Please let me know if it doesn't work so that we can fix it!

@Andy-Python-Programmer
Copy link
Author

Andy-Python-Programmer commented May 14, 2021

Alternatively, you can use a linker script for this.

I used the following linker script and is verified on the limine bootloader that it works fine and the higher offset value is fine.

ENTRY(_start)
OUTPUT_FORMAT(elf64-x86-64)

KERNEL_OFFSET = 0xFFFF800000100000;

SECTIONS {
    . = KERNEL_OFFSET;

    . += SIZEOF_HEADERS;
    . = ALIGN(4096);

    .text : AT(ADDR(.text) - KERNEL_OFFSET) {
        __text_start = .;
        *(.text*)
        . = ALIGN(4096);
        __text_end = .;
    }

    .rodata : AT(ADDR(.rodata) - KERNEL_OFFSET) {
        __rodata_start = .;
        *(.rodata*)
        . = ALIGN(4096);
        __rodata_end = .;
    }

    .data : AT(ADDR(.data) - KERNEL_OFFSET) {
        __data_start = .;
        *(.data*)
        . = ALIGN(4096);
        __data_end = .;
        __bss_start = .;
        *(.bss*)
        . = ALIGN(4096);
        __bss_end = .;
    }

    .tdata : AT(ADDR(.tdata) - KERNEL_OFFSET) {
        __tdata_start = .;
        *(.tdata*)
        . = ALIGN(4096);
        __tdata_end = .;
        __tbss_start = .;
        *(.tbss*)
        . += 8;
        . = ALIGN(4096);
        __tbss_end = .;
    }

    __kernel_end = .;

    /DISCARD/ : {
        *(.comment*)
        *(.eh_frame*)
        *(.gcc_except_table*)
        *(.note*)
        *(.rel.eh_frame*)
    }
}

image

In the elf loader you probably have to subtract the KERNEL_OFFSET from the elf section's vaddr.

@phil-opp
Copy link
Member

Ah, I think I found the issue. We're reserving only two frames to identity map the context switch function, but that is not enough when the kernel lives in a different level 3 table because then we need to create new level 3/2/1 tables as well.

Regarding the linker script: You shouldn't need the AT hack at all (after I fixed the mentioned bug). The bootloader should be capable of loading kernels directly to their higher half addresses.

@Andy-Python-Programmer
Copy link
Author

Andy-Python-Programmer commented May 14, 2021

Ah, I think I found the issue. We're reserving only two frames to identity map the context switch function, but that is not enough when the kernel lives in a different level 3 table because then we need to create new level 3/2/1 tables as well.

Regarding the linker script: You shouldn't need the AT hack at all (after I fixed the mentioned bug). The bootloader should be capable of loading kernels directly to their higher half addresses.

So is it like a quick fix?

Regarding the linker script: You shouldn't need the AT hack at all (after I fixed the mentioned bug). The bootloader should be capable of loading kernels directly to their higher half addresses.

Ah thanks!

@Andy-Python-Programmer
Copy link
Author

Andy-Python-Programmer commented May 14, 2021

@phil-opp I added a fix but I am still not sure as my heap does not work after this though.

pub const HEAP_START: usize = 0xfffffe8000000000;
pub const HEAP_SIZE: usize = 100 * 1024;

@phil-opp
Copy link
Member

@Andy-Python-Programmer Were you able to solve the heap problem?

@Andy-Python-Programmer
Copy link
Author

@Andy-Python-Programmer Were you able to solve the heap problem?

Yeah thanks!

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 a pull request may close this issue.

2 participants