Page 1 of 1

Port I/O doesn't work with -O0

Posted: Wed Mar 20, 2024 8:51 pm
by Xeno
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;
}

Re: Port I/O doesn't work with -O0

Posted: Wed Mar 20, 2024 9:24 pm
by Octocontrabass
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:

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;
}
Additionally...
Xeno wrote:-fno-zero-initialized-in-bss
Why are you using this option? It sounds like your bootloader is broken.
Xeno wrote:-mcmodel=large
Why are you using this option instead of "-mcmodel=kernel"?

Re: Port I/O doesn't work with -O0

Posted: Wed Mar 20, 2024 9:33 pm
by Xeno
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

Re: Port I/O doesn't work with -O0

Posted: Wed Mar 20, 2024 9:38 pm
by Xeno
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

Re: Port I/O doesn't work with -O0

Posted: Wed Mar 20, 2024 9:41 pm
by Octocontrabass
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
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.

Re: Port I/O doesn't work with -O0

Posted: Fri Mar 22, 2024 1:07 pm
by Xeno
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

Posted: Fri Mar 22, 2024 1:08 pm
by Xeno
Just tested, I have fixed it lol, just left it I guess and forgot to take it off