OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 27, 2024 4:24 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Port I/O doesn't work with -O0
PostPosted: Wed Mar 20, 2024 8:51 pm 
Offline
Member
Member

Joined: Tue Oct 10, 2023 7:40 pm
Posts: 51
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;
}


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Wed Mar 20, 2024 9:24 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
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"?


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Wed Mar 20, 2024 9:33 pm 
Offline
Member
Member

Joined: Tue Oct 10, 2023 7:40 pm
Posts: 51
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


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Wed Mar 20, 2024 9:38 pm 
Offline
Member
Member

Joined: Tue Oct 10, 2023 7:40 pm
Posts: 51
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


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Wed Mar 20, 2024 9:41 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
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.


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Fri Mar 22, 2024 1:07 pm 
Offline
Member
Member

Joined: Tue Oct 10, 2023 7:40 pm
Posts: 51
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....


Top
 Profile  
 
 Post subject: Re: Port I/O doesn't work with -O0
PostPosted: Fri Mar 22, 2024 1:08 pm 
Offline
Member
Member

Joined: Tue Oct 10, 2023 7:40 pm
Posts: 51
Just tested, I have fixed it lol, just left it I guess and forgot to take it off


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot], SemrushBot [Bot] and 63 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group