Port I/O doesn't 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

Port I/O doesn't work with -O0

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

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

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

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

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

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

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

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

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

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

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

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

Post by Xeno »

Just tested, I have fixed it lol, just left it I guess and forgot to take it off
Post Reply