aboutsummaryrefslogtreecommitdiff
path: root/src/mem/vmm.c
blob: 431f0df7641be37310fe8769001e72db7165a93e (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "../mem.h"
#include "paging.h"

extern struct DirectoryEntry k_pagedir[1024];
extern struct TableEntry k_ptable0x300[1024];

extern const unsigned MULTIBOOT_SIZE;
extern const unsigned VADDR_BASE;

unsigned
to_vaddr(unsigned paddr)
{
  return paddr + (unsigned)&VADDR_BASE - (unsigned)&MULTIBOOT_SIZE;
}

unsigned int
vmm_map(unsigned int paddr, unsigned int vaddr)
{
  if (paddr & 0xfff || vaddr & 0xfff) return 0;

  const unsigned table_idx = vaddr >> 22;             // high 10 bits
  const unsigned entry_idx = (vaddr >> 12) & 0x3ff;   // low 10 bits

  if (k_pagedir[table_idx].present == 0) return 0;
  struct TableEntry *table = (struct TableEntry *)to_vaddr(k_pagedir[table_idx].address << 12);

  table[entry_idx].address = paddr >> 12;
  table[entry_idx].present = 1;
  table[entry_idx].writeable = 1;

  return vaddr;
}

void
alloc4M()
{
  // enable pse in cr4
  asm volatile(R"(
  movl %cr4, %eax
  orl $0x10, %eax
  movl %eax, %cr4
)");

  struct DirectoryEntry4MB *directory = (struct DirectoryEntry4MB *)&k_pagedir[0x301];
  directory->address_low = 0x1;
  directory->present = 1;
  directory->writeable = 1;
  directory->pagesize = 1;
}