GCC doesnt work with -O0

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

GCC doesnt work with -O0

Post 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
Octocontrabass
Member
Member
Posts: 5501
Joined: Mon Mar 25, 2013 7:01 pm

Re: GCC doesnt work with -O0

Post 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?
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post 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
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post 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.
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post by Xeno »

Probably from the clock interrupt.
Octocontrabass
Member
Member
Posts: 5501
Joined: Mon Mar 25, 2013 7:01 pm

Re: GCC doesnt work with -O0

Post 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.
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post by Xeno »

My kernel is loaded at 0xffffFA0000000000
Octocontrabass
Member
Member
Posts: 5501
Joined: Mon Mar 25, 2013 7:01 pm

Re: GCC doesnt work with -O0

Post by Octocontrabass »

Why? If you load it at 0xFFFFFFFF80000000 you can use the kernel code model, which will make your kernel smaller and faster.
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post 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
Octocontrabass
Member
Member
Posts: 5501
Joined: Mon Mar 25, 2013 7:01 pm

Re: GCC doesnt work with -O0

Post by Octocontrabass »

Why can't you allocate those kernel data structures at 0xFFFFFA0000000000 and link your kernel at (or above) 0xFFFFFFFF80000000?
Xeno
Member
Member
Posts: 56
Joined: Tue Oct 10, 2023 7:40 pm

Re: GCC doesnt work with -O0

Post by Xeno »

I might but i think it would conflict with my goals...I'll look into it
Post Reply