I am not able to read or write ports if i use flag -O0 in gcc
I noticed this while debuging some other randome thing and it doest seem to work at all.
compile from script: ~/opt/cross/bin/x86_64-elf-gcc -c -I./kheaders -s "$file" -o "${prefix}${filename%.c}.o" -O0 -mgeneral-regs-only -nostdlib -nostdinc -ffreestanding -m64 -lgcc -fno-zero-initialized-in-bss -fno-asynchronous-unwind-tables -mcmodel=large -D__is_kernel -mno-red-zone
my code:
//IO
void outb(uint16_t port, uint8_t byte) {
gas(
"pushq %%rdx\n"
"pushq %%rax\n"
"mov %0, %%al\n"
"movw %1, %%dx\n"
"out %%al, %%dx\n"
"popq %%rax\n"
"popq %%rdx"
: : "r" (byte), "r" (port) :
);
}
uint8_t inb(uint16_t port) {
uint8_t out = 0;
gas(
"pushq %%rdx\n"
"movw %1, %%dx\n"
"in %%dx, %%al\n"
"mov %%al, %0\n"
"popq %%rdx"
: "=r" (out) : "r" (port) : "rax"
);
return out;
}
Port I/O doesn't work with -O0
-
- Member
- Posts: 5486
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Port I/O doesn't work with -O0
You can tell GCC to use specific registers instead of allowing it to choose any register. Right now, your code breaks if GCC chooses RAX or RDX.
You should end up with something like this:
Additionally...
You should end up with something like this:
Code: Select all
void outb(uint16_t port, uint8_t byte) {
asm volatile(
"out %0, %1\n"
: : "a" (byte), "Nd" (port) :
);
}
uint8_t inb(uint16_t port) {
uint8_t out = 0;
asm volatile(
"in %1, %0\n"
: "=a" (out) : "Nd" (port) :
);
return out;
}
Why are you using this option? It sounds like your bootloader is broken.Xeno wrote:-fno-zero-initialized-in-bss
Why are you using this option instead of "-mcmodel=kernel"?Xeno wrote:-mcmodel=large
Re: Port I/O doesn't work with -O0
Um yes I use a custom bootloader and i load a flat binary, no elf
I use that because as i dont use an elf, as of right now, I cant "load" the kernel and initilize it
And I my linking script is incompatible with the kernel flag, thats why i dont use that one. I may change it but as of now it best fits my kernel road map
I use that because as i dont use an elf, as of right now, I cant "load" the kernel and initilize it
And I my linking script is incompatible with the kernel flag, thats why i dont use that one. I may change it but as of now it best fits my kernel road map
Re: Port I/O doesn't work with -O0
Thank you for the solution to the port I/O, that cleared up a lot of my IO problems, lol, Im not used to inline assebly used to do all nasm/fasm
-
- Member
- Posts: 5486
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Port I/O doesn't work with -O0
The .bss section should get initialized when you link/convert your kernel to a flat binary. It might be a problem with that instead of a problem with your bootloader.Xeno wrote:Um yes I use a custom bootloader and i load a flat binary, no elf
I use that because as i dont use an elf, as of right now, I cant "load" the kernel and initilize it
Re: Port I/O doesn't work with -O0
hm actuly now that you mention it , I think I did fix that in my linkin process. I dont think I need that flag anymore....
Re: Port I/O doesn't work with -O0
Just tested, I have fixed it lol, just left it I guess and forgot to take it off