Page 1 of 1
Need some explainations
Posted: Tue Jan 11, 2022 9:17 pm
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?
Re: Need some explainations
Posted: Tue Jan 11, 2022 10:38 pm
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.
Re: Need some explainations
Posted: Wed Jan 12, 2022 1:13 am
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?
Re: Need some explainations
Posted: Wed Jan 12, 2022 5:46 am
by iansjack
The clue is in the name Memory-Mapped Input/Output.
Re: Need some explainations
Posted: Thu Jan 13, 2022 2:01 am
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.
Re: Need some explainations
Posted: Thu Jan 13, 2022 2:22 pm
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!
Re: Need some explainations
Posted: Sat Jan 15, 2022 1:00 am
by davmac314
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.