TLB: Difference between revisions

885 bytes added ,  29 days ago
m
Bot: Replace deprecated source tag with syntaxhighlight
[unchecked revision][unchecked revision]
(→‎Implications for OSDev: more updates on invlpg, and capitalisation of some list items)
m (Bot: Replace deprecated source tag with syntaxhighlight)
 
(6 intermediate revisions by 5 users not shown)
Line 1:
AThe '''Translation Lookaside Buffer''' ('''TLB''') is a cache of memory page translations (i.e.employed pagein tablemany entries).systems with memory paging capability. When the processor needs to translate a given virtual address into a physical address, the TLB is checkedconsulted first. On x86 systems, TLB misses are handled transparently by hardware. Only if the page directory/table entry (or any higher level directory entry) is not present in-core will the operating system be notified (by the means of a page fault exception.)
Translation Lookaside Buffer
 
== Usage implications ==
==Implementation==
Like a regular CPU cache, the TLB is ''mostly'' transparent. There are sometwo cases which the OSoperating system must be aware of:.
A TLB is a cache of memory translations (i.e. page table entries). When the processor needs to translate a given virtual address into a physical address, the TLB is checked first. On x86 systems, TLB misses are handled transparently by hardware. Only if the page table entry (or any higher level directory entry) is not present will the operating system be notified.
 
=== Modification of paging structures ===
==Implications for OSDev==
#:The TLB is not awaretransparently informed of changes you makemade to thepaging pagestructures. tablesTherefore (atthe anyTLB level).has to Ifbe flushed youupon makesuch a change, you must flush the TLBs. On x86 systems, this can be done by writing to the page tabledirectory base register (CR3). That is:<br>
Like a cache, the TLB is ''mostly'' transparent. There are some cases which the OS must be aware of:
<syntaxhighlight lang="asm">
# Writing to page tables
movl %cr3,%eax
#:The TLB is not aware of changes you make to the page tables (at any level). If you make a change, you must flush the TLBs. On x86, this can be done by writing to the page table base register (CR3). That is:<br>
movl %eax,%cr3
#:<code>mov EAX, CR3<br>mov CR3, EAX</code>
</syntaxhighlight>
#:Note: setting the global (G) bit in a page directory/table entry will prevent that entry from being flushed. This is useful for pinning interrupt handlers in place.
#:<br />
#:An alternate (and better) method is to use the invlpg instruction, which should be used instead of the above method when doing small mapping modifications (creation, removing, changing). Mostly, invlpg is used in functions such as "unmap" and "remap" in order to avoid the old cached lookup being used. If invlpg or some other TLB flush method had not been used, the mapping would remain cached causing potentially strange behaviour across the system.
#:<br />
#:Note that changing/reloading CR3 should only need to be done when switching between process address spaces. Using it to completely flush TLBs is really quite overkill in most situations.
# Multi-processor consistency
#:The above is more complicated in the multi-processor case. If another processor could also be affected by a page table write (because of shared memory, or multiple threads from the same process), you must also flush the TLBs on those processors. This will require some form of inter-processor communication.
 
#:An alternate (and better) method is to use the <code>invlpg</code> instruction, which should be used instead of the above method when doing small mapping modifications (creation, removing, changing.). Mostly, <code>invlpg</code> is mostly used in functionspage suchunmapping asand "unmap"remapping and "remap"routines in order to avoidinvalidate thea oldprevious cached lookup being usedtranslation. If <code>invlpg</code> or some other TLB flush method had not been used, the mapping would remain cached, causingproducing potentially strange behaviour across theundefined systemconsequences.
==Links==
 
* TLB at [http://en.wikipedia.org/wiki/Translation_lookaside_buffer Wikipedia]
However, please note that the <code>invlpg</code> instruction was introduced in the i486 ISA and is not part of the i386 ISA, thereby requiring a properly written i386-compatible kernel to use conditional inclusion of relevant code at compilation time depending on the target machine. An example routine declaration and source code follow:
<syntaxhighlight lang="c">
void vm_page_inval(void *);
</syntaxhighlight>
<syntaxhighlight lang="asm">
#include <kconfig.h>
 
.globl vm_page_inval
vm_page_inval:
#if TARGET_MACHINE >= TARGET_MACHINE_I486
movl 4(%esp),%eax
invlpg (%eax)
#else /* TARGET_MACHINE < TARGET_MACHINE_I486 */
movl %cr3,%eax
movl %eax,%cr3
#endif
ret
</syntaxhighlight>
 
#:Note that changing/reloading CR3 should only need to be done when switching between process address spaces. Using it to completely flush TLBs is really quite overkill in most situations.
 
#=== Multi-processor consistency ===
#:The above is more complicated in the multi-processor case. If another processor could also be affected by a page table write (because of shared memory, or multiple threads from the same process), you must also flush the TLBs on those processors. This will require some form of inter-processor communication.
 
== See Also ==
=== External Links ===
* TLB at [http://en.wikipedia.org/wiki/Translation_lookaside_buffer The Wikipedia TLB article]
 
[[Category:X86]]
[[Category:Paging]]