-
-
Notifications
You must be signed in to change notification settings - Fork 858
/
tlb.h
74 lines (68 loc) · 2.48 KB
/
tlb.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef TLB_H
#define TLB_H
#include <string.h>
#include "emu/mmu.h"
#include "debug.h"
struct tlb_entry {
page_t page;
page_t page_if_writable;
uintptr_t data_minus_addr;
};
#define TLB_BITS 10
#define TLB_SIZE (1 << TLB_BITS)
struct tlb {
struct mmu *mmu;
page_t dirty_page;
unsigned mem_changes;
// this is basically one of the return values of tlb_handle_miss, tlb_{read,write}, and __tlb_{read,write}_cross_page
// yes, this sucks
addr_t segfault_addr;
struct tlb_entry entries[TLB_SIZE];
};
#define TLB_INDEX(addr) (((addr >> PAGE_BITS) & (TLB_SIZE - 1)) ^ (addr >> (PAGE_BITS + TLB_BITS)))
#define TLB_PAGE(addr) (addr & 0xfffff000)
#define TLB_PAGE_EMPTY 1
void tlb_refresh(struct tlb *tlb, struct mmu *mmu);
void tlb_free(struct tlb *tlb);
void tlb_flush(struct tlb *tlb);
void *tlb_handle_miss(struct tlb *tlb, addr_t addr, int type);
forceinline __no_instrument void *__tlb_read_ptr(struct tlb *tlb, addr_t addr) {
struct tlb_entry entry = tlb->entries[TLB_INDEX(addr)];
if (entry.page == TLB_PAGE(addr)) {
void *address = (void *) (entry.data_minus_addr + addr);
posit(address != NULL);
return address;
}
return tlb_handle_miss(tlb, addr, MEM_READ);
}
bool __tlb_read_cross_page(struct tlb *tlb, addr_t addr, char *out, unsigned size);
forceinline __no_instrument bool tlb_read(struct tlb *tlb, addr_t addr, void *out, unsigned size) {
if (PGOFFSET(addr) > PAGE_SIZE - size)
return __tlb_read_cross_page(tlb, addr, out, size);
void *ptr = __tlb_read_ptr(tlb, addr);
if (ptr == NULL)
return false;
memcpy(out, ptr, size);
return true;
}
forceinline __no_instrument void *__tlb_write_ptr(struct tlb *tlb, addr_t addr) {
struct tlb_entry entry = tlb->entries[TLB_INDEX(addr)];
if (entry.page_if_writable == TLB_PAGE(addr)) {
tlb->dirty_page = TLB_PAGE(addr);
void *address = (void *) (entry.data_minus_addr + addr);
posit(address != NULL);
return address;
}
return tlb_handle_miss(tlb, addr, MEM_WRITE);
}
bool __tlb_write_cross_page(struct tlb *tlb, addr_t addr, const char *value, unsigned size);
forceinline __no_instrument bool tlb_write(struct tlb *tlb, addr_t addr, const void *value, unsigned size) {
if (PGOFFSET(addr) > PAGE_SIZE - size)
return __tlb_write_cross_page(tlb, addr, value, size);
void *ptr = __tlb_write_ptr(tlb, addr);
if (ptr == NULL)
return false;
memcpy(ptr, value, size);
return true;
}
#endif