Need some explainations

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
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Need some explainations

Post by NeonLightions »

Hi,
I'm trying to implement HPET on my hobby OS. After I read this article: https://wiki.osdev.org/HPET, I want to ask one thing: Can you explain this line?

Code: Select all

write_register_64(timer_configuration(n), (ioapic_input << 9) | (1 << 2));
Not exactly that line, but the function write_register_64. I don't know how to implement it. Can anyone help me?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Need some explainations

Post by Octocontrabass »

It's a function that writes a 64-bit value to a MMIO register.

If you're writing your kernel in C, you should probably write the function in assembly so you can ensure compiler optimizations won't reorder or remove the write.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Need some explainations

Post by NeonLightions »

Octocontrabass wrote:It's a function that writes a 64-bit value to a MMIO register.
That's the problem, I don't know what I should write. I have worked with registers, memory,... but this is my first time with MMIO register. In my case, is that I have to use port and outX/inX?
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Need some explainations

Post by iansjack »

The clue is in the name Memory-Mapped Input/Output.
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Need some explainations

Post by davmac314 »

NeonLightions wrote:That's the problem, I don't know what I should write. I have worked with registers, memory,... but this is my first time with MMIO register. In my case, is that I have to use port and outX/inX?
No, "memory mapped" means you read and write it as if it was memory. Literally just store or read a value from the correct address.

You should use a volatile pointer to avoid the compiler re-ordering, collapsing or eliding the loads and stores.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Need some explainations

Post by Octocontrabass »

davmac314 wrote:You should use a volatile pointer to avoid the compiler re-ordering, collapsing or eliding the loads and stores.
The C standard still gives compilers some flexibility in how they implement volatile. GCC seems to handle it in a way that's appropriate for MMIO, but I'm pretty sure Clang does not!
davmac314
Member
Member
Posts: 121
Joined: Mon Jul 05, 2021 6:57 pm

Re: Need some explainations

Post by davmac314 »

Octocontrabass wrote:The C standard still gives compilers some flexibility in how they implement volatile. GCC seems to handle it in a way that's appropriate for MMIO, but I'm pretty sure Clang does not!
I know that volatile has historically been problematic in both GCC and Clang (https://llvm.org/pubs/2008-10-EMSOFT-Volatiles.pdf) but I'm not aware of any current bugs in code generation around use of volatile in Clang. I'd be interested to see any example.
Post Reply