Hi,
ezeaguerre wrote:After remapping the APIC I set the "Cache Disable" bit on the page table entry and invalidate it, but I'll try the "Cache Disable" on CR0 and WBINVD just to see what happens.
The cache disable flag in a page directory or page table has slightly strange behaviour. From a technical perspective, because other CPUs in the system may have the same physical page mapped as "cachable" the CPU will still do "cache coherency" stuff to ensure correctness. This means sending MESI messages over the bus when the area is modified or read to tell other CPUs to flush the cache line.
For a single CPU system (and multi-CPU systems where the same physical page is never cached elsewhere) these MESI messages are pointless, but the CPU doesn't know that. The end result is that it's mostly harmless but also increases bus traffic for no reason.
This doesn't apply if the MTRRs say the area is non-cacheable as the MTRRs must be the same on all CPUs, and therefore it's impossible for one CPU to cache an area while another doesn't, and the CPU doesn't need to send the MESI messages over the bus. Of course in this case it won't matter what the cache disable flag in the page directory or page table says - it's uncachable regardless.
ezeaguerre wrote:About the MTRRs I think they start all on 0 which means UC for all memory,
When the CPU first boots all MTRRs are disabled and everything is treated as uncachable. Then the BIOS does it's tricks and enables the MTRRs, so that by the time the OS is started all normal RAM is set to the "write-back" caching type (and other areas may be set to other caching types).
ezeaguerre wrote:and here comes another question: If I have UC on the MTRRs for all memory, does the "Cache Disable" bit on CR0 and/or the page table has any meaning?
In this case the setting of cache disable flag in CR0 wouldn't make any difference. However if the MTRRs are set to cache anything, then the flag in CR0 will disable that caching.
ezeaguerre wrote:P.S: Do I have to invalidate the caches after remapping the APIC or marking the page as "Cache Disable" and invalidate it is enough?
You must use WBINVD to flush all caches (or use the new CLFUSH instruction to flush effected cache lines) if, for any reason, the CPU may have the effected cache line/s in it's cache and you disable caching using any method. Please note that "for any reason" includes the BIOS using that cache line/s for something before your OS runs, and also includes "speculative execution" (where the CPU decides it might need the data and pre-loads the data into it's cache just in case).
The correct method (given that you can't guarantee that the old data isn't in the cache) would be to set the cache disable flag in CR0, then flush all caches, then change the address of the APIC and map the APIC into the address space with the cache disable flag in the page table set, and finally clear the cache disable flag in CR0. This ensures that the old data isn't in the cache beforehand, while also making sure the APIC's data isn't pre-loaded into the CPUs cache after the address of the APIC is changed but before it's mapped into the address space.
ezeaguerre wrote:And no, there isn't any special reason to remap the Local APIC, I just wanted to try it
![Razz :P](./images/smilies/icon_razz.gif)
Patient: It hurts when I poke myself in the eye with a screwdriver!
Doctor: Why are you poking yourself in the eye with a screwdriver?
Patient: There's no special reason, I just wanted to try it.
Doctor: Perhaps we can modify the screwdriver so it doesn't hurt so much...
Of course it's also entirely possible that there's other problems. I'm mostly familiar with Intel CPUs, but modern AMD CPUs have differences. Specifically they use Hyper-transport, which includes a "top of memory" field in it's configuration. It's entirely possible that AMD CPUs send all transactions that are below the "top of memory" to it's memory controller without checking if the transaction hits the local APIC or not.
Cheers,
Brendan