Memory Management Unit: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
No edit summary
m Bot: Replace deprecated source tag with syntaxhighlight
 
(One intermediate revision by the same user not shown)
Line 21:
To clarify: A processor with a MMU that provides virtual memory has an on-chip cache of "translations". Each "translation record/entry" tells the CPU the mapping of one virtual address to one physical address. Let us imaging this on-chip cache as a big lookup array of entries that are of this form:
 
<sourcesyntaxhighlight lang="C">
// Abstract model of a TLB.
 
Line 39:
// Instance of a hardware Translation Lookaside Buffer.
struct tlb_cache_record_t hw_tlb[CPU_MODEL_MAX_TLB_ENTRIES];
</syntaxhighlight>
</source>
 
Your processor's TLB is essentially a hash lookup table of entries that tell what physical address each page refers to. When you enable paging, every address reference is sent out to the TLB for lookup. The CPU does something like this internally:
 
<sourcesyntaxhighlight lang="C">
// Model routine for a TLB lookup.
 
Line 58:
return 0;
}
</syntaxhighlight>
</source>
 
If the TLB contains an entry for that virtual address, that virtual address's recorded physical address is returned. The CPU *does not* care about the actual state of the REAL translation in memory! *You* are responsible for ensuring that the information in the processor's TLB is correct. Let us pretend that our processor's TLB has an entry in it that records the virtual address 0xC0103000 as pointing to the physical address 0x11807000. Assume that your kernel has changed this information in the page tables in RAM; Your writing to physical RAM does not affect the on-chip TLB. UNLESS you tell the processor to flush that TLB entry for 0xC0103000 from its TLB, the next time 0xC0103000 is referenced, the CPU will look into the TLB, and go right on ahead to send out the physical address 0x11807000 that the TLB *says* the address corresponds to.
Line 64:
A processor architecture would normally, then provide an instruction to invalidate TLB entries, either en masse, or one by one, or however the CPU designers decided. Let's try to model a TLB flush. In our model CPU architecture, there is an instruction that software can issue which will invalidate one virtual address. It is called: TLBFLSH. An OS would invoke this on our model architecture by doing something like this:
 
<sourcesyntaxhighlight lang="C">
asm volatile ("TLBFLSH %0\n\t"::"r" (virtual_address));
</syntaxhighlight>
</source>
 
And on to our model:
<sourcesyntaxhighlight lang="C">
// Modelled function for a flush of the TLB modelled earlier on.
 
Line 83:
};
}
</syntaxhighlight>
</source>
 
Now please understand that a CPU will, as long as you enable its MMU, *always* use the TLB before sending an address out onto the address bus. That is, once you enable the MMU, until you take it off, you have essentially "trapped" yourself in a virtual address space. Unless you can edit that virtual address space by editing the page tables, and invalidating the TLB entries for virtual addresses, you have no means of changing where your kernel can read from/write to in RAM. Enabling paging means that ALL of your data and instruction fetch addresses will pass through the TLB first.