UnnamedOS
mmu.c
Go to the documentation of this file.
1 
14 #include <common.h>
15 #include <interrupts/isr.h>
16 #include <mem/mmu.h>
17 
19 typedef union {
20  struct {
21  uint8_t pr : 1,
22  rw : 1,
23  user : 1,
24  reserved : 1,
25  _if : 1;
26  } __attribute__((packed)) bits;
27  uint32_t dword;
29 
35  // Load the directory pointer into control register 3. (Also flushes the TLB!)
36  asm volatile("mov %0, %%cr3" : : "r" (page_directory));
37 }
38 
45  if (page_directory) {
46  mmu_load_page_directory(page_directory); // first load the initial directory
47  // then set bit 31 (the paging flag) in control register 0 to enable paging
48  asm volatile("mov %cr0, %eax; or $0x80000000, %eax; mov %eax, %cr0");
49  } else
50  asm volatile("mov %cr0, %eax; and $0x7FFFFFFF, %eax; mov %eax, %cr0");
51 }
52 
57 uint8_t mmu_get_paging() {
58  uint32_t cr0;
59  asm volatile("mov %%cr0, %0" : "=r" (cr0));
60  return cr0 >> 31;
61 }
62 
69 void mmu_flush_tlb(void* vaddr) {
70  // invlpg invalidates a page table entry in the TLB to notify it of a change.
71  // We pass the address as a "r"egister input. "memory" ensures GCC does not
72  asm volatile("invlpg (%0)" : : "r" (vaddr) : "memory"); // do funny things.
73 }
74 
81  uint32_t fault_address; // the virtual address that caused the page fault
83  asm volatile("mov %%cr2, %0" : "=r" (fault_address));
84  println("%4apage fault caused by the virtual address %08x\n"
85  "(%s while %s %s%s%s)%a", fault_address,
86  error.bits.pr ? "protection violation" : "non-present page",
87  error.bits.rw ? "writing" : "reading",
88  error.bits.user ? "in user space" : "in the kernel",
89  error.bits.reserved ? ", reserved write" : "",
90  error.bits._if ? ", instruction fetch" : "");
91  panic("%4aEX%02x (EIP=%08x)", cpu->intr, cpu->eip);
92  return cpu;
93 }
94 
96 void mmu_init() {
98 }
99 
void mmu_load_page_directory(page_directory_t *page_directory)
Loads a page directory into the CR3 register.
Definition: mmu.c:34
uint8_t rw
write flag
Definition: mmu.c:21
void mmu_flush_tlb(void *vaddr)
Flushes the Translation Lookaside Buffer for the given page.
Definition: mmu.c:69
static page_directory_t * page_directory
the current page directory
Definition: vmm.c:44
uint8_t _if
instruction flag fetch
Definition: mmu.c:21
The CPU&#39;s state when an interrupt occurs.
Definition: isr.h:58
uint8_t mmu_get_paging()
Returns whether paging is enabled or disabled.
Definition: mmu.c:57
uint32_t eip
the instruction to return to after the interrupt
Definition: isr.h:68
#define ISR_EXCEPTION(ex)
the interrupt vector for an exception
Definition: isr.h:12
void isr_register_handler(size_t intr, isr_handler_t handler)
Registers a handler to call whenever a given interrupt is fired.
Definition: isr.c:66
uint8_t pr
present flag
Definition: mmu.c:21
struct mmu_page_fault_error_t::@10 bits
bit field
An entry in a page directory.
Definition: vmm.h:25
uint8_t user
user flag
Definition: mmu.c:21
uint8_t reserved
reserved write flag
Definition: mmu.c:21
static cpu_state_t * mmu_handle_page_fault(cpu_state_t *cpu)
Handles page faults.
Definition: mmu.c:80
uint32_t error
hold an error code if an error occured, otherwise 0
Definition: isr.h:68
uint32_t intr
the interrupt vector of the fired interrupt
Definition: isr.h:68
void mmu_init()
Initializes the MMU.
Definition: mmu.c:96
page fault error information
Definition: mmu.c:19
Definition: stdint.h:22
void mmu_enable_paging(page_directory_t *page_directory)
Loads a page directory and enables paging.
Definition: mmu.c:44