OSDev.org
https://forum.osdev.org/

Port I/O doesn't work with -O0
https://forum.osdev.org/viewtopic.php?f=1&t=57176
Page 1 of 1

Author:  Xeno [ Wed Mar 20, 2024 8:51 pm ]
Post subject:  Port I/O doesn't work with -O0

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;
}

Author:  Octocontrabass [ Wed Mar 20, 2024 9:24 pm ]
Post subject:  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:

Code:
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"?

Author:  Xeno [ Wed Mar 20, 2024 9:33 pm ]
Post subject:  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

Author:  Xeno [ Wed Mar 20, 2024 9:38 pm ]
Post subject:  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

Author:  Octocontrabass [ Wed Mar 20, 2024 9:41 pm ]
Post subject:  Re: Port I/O doesn't work with -O0

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.

Author:  Xeno [ Fri Mar 22, 2024 1:07 pm ]
Post subject:  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....

Author:  Xeno [ Fri Mar 22, 2024 1:08 pm ]
Post subject:  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

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/