aboutsummaryrefslogtreecommitdiff
path: root/kernel/include/mm/tlb.h
blob: 836be4e527cec172ade35b5d4c9a5273b175f34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#include "kernel.h"
#include "types.h"

#include "mm/page.h"

/* Invalidates any entries from the TLB which contain
 * mappings for the given virtual address. */
static inline void tlb_flush(uintptr_t vaddr)
{
    __asm__ volatile("invlpg (%0)" ::"r"(vaddr));
}

/* Invalidates any entries for count pages starting at
 * vaddr from the TLB. If this range is very large it may
 * be more efficient to call tlb_flush_all to invalidate
 * the entire TLB. */
static inline void tlb_flush_range(uintptr_t vaddr, size_t count)
{
    for (size_t i = 0; i < count; i++, vaddr += PAGE_SIZE)
    {
        tlb_flush(vaddr);
    }
}

/* Invalidates the entire TLB. */
static inline void tlb_flush_all()
{
    uintptr_t pdir;
    __asm__ volatile("movq %%cr3, %0"
                     : "=r"(pdir));
    __asm__ volatile("movq %0, %%cr3" ::"r"(pdir)
                     : "memory");
}