Page 1 of 1

GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 5:19 pm
by Xeno
When i compile my Kernel with no optimization flags or with -O0 (same thing), it randomly works/doesn't work or causes faults or print doesn't work or i get weird results from kmemcpy. it also causes the output to grow to 332k from 44k in size. I am clueless to what is going on... I'v tried "fixing" functions but nothing works, and all works fine with -O1, -O2, -O3, -Ofast and -Os.

Compile line from script:
~/opt/cross/bin/x86_64-elf-gcc -I./kheaders -c -s $1 -o $2 -mgeneral-regs-only -ffreestanding -O0 -mcmodel=large

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:17 pm
by Octocontrabass
I see you're not passing "-mno-red-zone" to your cross-compiler. If you have interrupts enabled, they'll corrupt the red zone (unless you use the IST for all interrupts, which is a questionable design choice). Optimizations tend to reduce usage of the red zone, so that might be why you haven't seen problems when optimizations are enabled.

I see you're also using the large code model instead of the kernel code model. Why is that?

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:22 pm
by Xeno
Funny thing it just kinda randomly does it... for some reason ld doesnt work with kernel
I will try the red zone flag, thank you

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:24 pm
by Xeno
Ok thank you, that seems to be the problem...but it was weird if i ran it several times it would fail once in a while even with no code changes.

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:26 pm
by Xeno
Probably from the clock interrupt.

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:31 pm
by Octocontrabass
Xeno wrote:for some reason ld doesnt work with kernel
The reason is usually that the kernel code model requires your linker script to place your kernel between 0xFFFFFFFF80000000 and 0xFFFFFFFFFFFFFFFF, and your linker script probably doesn't do that.

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:31 pm
by Xeno
My kernel is loaded at 0xffffFA0000000000

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:32 pm
by Octocontrabass
Why? If you load it at 0xFFFFFFFF80000000 you can use the kernel code model, which will make your kernel smaller and faster.

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:35 pm
by Xeno
I have it maped there so that i can allocate the virtual memory above it for kernel data structures (page maps/memory bitmap etc) with all user data below
This is also for a method of memory allocation I designed, it uses a different design for virtual memory, then the typical idea

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 6:45 pm
by Octocontrabass
Why can't you allocate those kernel data structures at 0xFFFFFA0000000000 and link your kernel at (or above) 0xFFFFFFFF80000000?

Re: GCC doesnt work with -O0

Posted: Fri Nov 17, 2023 9:43 pm
by Xeno
I might but i think it would conflict with my goals...I'll look into it