aboutsummaryrefslogtreecommitdiff
path: root/src/mem/vmm.c
blob: a07dd72ce0a69cd0ac9f8fc01538c54decbd7662 (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
#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)
{
  struct TableEntry *table;
  const unsigned table_idx = vaddr >> 22;           /* high 10 bits */
  const unsigned entry_idx = (vaddr >> 12) & 0x3ff; /* low 10 bits */

  if (paddr & 0xfff || vaddr & 0xfff) return 0;

  if (k_pagedir[table_idx].present == 0) return 0;
  table = (struct TableEntry *)to_vaddr(k_pagedir[table_idx].address << 12);
  table[entry_idx].address = (paddr >> 12) & 0xfffff;
  table[entry_idx].present = 1;
  table[entry_idx].writeable = 1;

  return vaddr;
}

void
alloc4M()
{
  struct DirectoryEntry4MB *directory;

  /* enable pse in cr4 */
  __asm__("movl %cr4, %eax; orl $0x10, %eax; movl %eax, %cr4");

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