64 bit kernel in zig

Hello everyone, I came here if someone who knows about operating systems made from scratch can help me with my RenuxOS project because my kernel is 32 bit and everything works perfectly when I put multiboot 2 on it. I tried several ways to make a basic VGA drive that I made myself work. I tried searching the internet about it and nothing. Link here GitHub - renuxteam/renuxos-src: RenuxOS source code (NEW)

When I put the vga driver in the kernel in 32 bit (multiboot1) it shows correctly when it is multiboot 2 and I compile it always gives the black screen

I’m not a Kernel Developer, but that question seems incredibly broad. Could you narrow it down to more? The site isn’t a place to find contributors to random Open Source Projects, our members will need more details if they are going to help.

I’m trying to make a 64 bit kernel but I can’t do it.

ENTRY(_start)

SECTIONS
{
    . = 0xFFFFFFFF80000000;

    .text : {
        *(.text)
        *(.text.*)
    }

    .rodata : {
        *(.rodata*)
    }

    .data : {
        *(.data*)
    }

    .bss : {
        *(.bss*)
    }

    /DISCARD/ : {
        *(.eh_frame)
        *(.eh_frame_hdr)
        *(.comment)
        *(.note.*)
        *(.debug_*)
    }
}

and give this error

install
└─ install kernel.elf
   └─ zig build-exe kernel.elf Debug x86_64-freestanding-none 4 errors
error: ld.lld: .zig-cache/o/a0edbe42db853200db88404b1bf53995/kernel.elf.o:(function kernel_main.kernel_main: .text+0x66): relocation R_X86_64_32 out of range: 18446744071562069879 is not in [0, 4294967295]; references section '.rodata.str1.1'
    note: referenced by kernel_main.zig:8
error: ld.lld: .zig-cache/o/a0edbe42db853200db88404b1bf53995/kernel.elf.o:(function kernel_main.kernel_main: .text+0x79): relocation R_X86_64_32 out of range: 18446744071562069893 is not in [0, 4294967295]; references section '.rodata.str1.1'
    note: referenced by kernel_main.zig:9
error: ld.lld: .zig-cache/o/a0edbe42db853200db88404b1bf53995/kernel.elf.o:(function drivers.video.vga.setColor: .text+0xe4): relocation R_X86_64_32 out of range: 18446744071562071104 is not in [0, 4294967295]; references section '.data'
    note: referenced by vga.zig:140 (/home/renan/Projetos/zig/git/renuxos-src/kernel/drivers/video/vga.zig:140)
error: ld.lld: .zig-cache/o/a0edbe42db853200db88404b1bf53995/kernel.elf.o:(function debug.FullPanic((function 'defaultPanic')).integerOverflow: .text+0x475): relocation R_X86_64_32 out of range: 18446744071562069848 is not in [0, 4294967295]; references section '.rodata.str1.1'
    note: referenced by debug.zig:91 (/usr/lib/std/debug.zig:91)
error: the following command failed with 4 compilation errors:
/usr/bin/zig build-exe -fllvm .zig-cache/o/a0edbe42db853200db88404b1bf53995/kernel.elf.o .zig-cache/o/92edbf45db7d3f7fdbcc01013006cc27/main.o .zig-cache/o/2f0d7c183574f3d52fc6d469c6b7e585/boot.o -fno-PIC -mno-red-zone -ODebug -target x86_64-freestanding-none -mcpu baseline -I /home/renan/Projetos/zig/git/renuxos-src/.zig-cache/o/32084e15aba5af217855ea0fd911848b -I /home/renan/Projetos/zig/git/renuxos-src/.zig-cache/o/32084e15aba5af217855ea0fd911848b -I /home/renan/Projetos/zig/git/renuxos-src/.zig-cache/o/32084e15aba5af217855ea0fd911848b --cache-dir .zig-cache --global-cache-dir /home/renan/.cache/zig --name kernel.elf -static --script /home/renan/Projetos/zig/git/renuxos-src/linker/linker.ld --zig-lib-dir /usr/lib/ -fno-lto --listen=-
Build Summary: 6/9 steps succeeded; 1 failed
install transitive failure
└─ install kernel.elf transitive failure
   └─ zig build-exe kernel.elf Debug x86_64-freestanding-none 4 errors
error: the following build command failed with exit code 1:
.zig-cache/o/e3b787882b4f2e6e7616a00d29643afd/build /usr/bin/zig /usr/lib /home/renan/Projetos/zig/git/renuxos-src .zig-cache /home/renan/.cache/zig --seed 0x11210be9 -Z4da566590b212647

try:

KERNEL_START = 0xFFFFFFFF80000000;
SECTIONS
{
    . = KERNEL_START;
    .text : AT(ADDR(.text) - KERNEL_START) {
        ...
    }
    .rodata : AT(ADDR(.rodata) - KERNEL_START) {
        ...
    }
    ...
}

The linker seems to assume that all symbols have to be inside 32-bit address space (within [0, 4294967295]).

Try passing -mcmodel=kernel. This will assume your code lives in the range [-2147483648, -1] ([0xFFFFFFFF80000000, 0xFFFFFFFFFFFFFFFF]).

zig build-exe --help:

-mcmodel=[model]          Limit range of code and data virtual addresses
    default
    extreme
    kernel
    large
    medany
    medium
    medlow
    medmid
    normal
    small
    tiny

The model names were apparently adopted from GCC:

Relevant issue:

1 Like

I did some x86 64bit kernel stuff in Zig, everything I did is on codeberg. For example, I have the zig_os repository, which shows a very simple bootloader loading a very simple kernel. That might be interesting for you.
And I am doing the loup-os project, where I currently have a kernel that is half-broken for x86_64 (because of paging mess, but if you comment that code out, something should be printed out) and a working kernel for risc-v.
Note however that both kernels for x86 are designed for UEFI with a custom bootloader (stuff you don’t have to worry about when doing it with multiboot), and the risc-v kernel is loaded directly.