summaryrefslogtreecommitdiff
path: root/kernel/vmem.h
blob: 8e538bb746cba0a4f70ee05af89cc89490ab825f (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
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
75
76
77
78
79
80
81
82
83
/**
 * @file
 * http://www.brokenthorn.com/Resources/OSDev18.html
 * https://wiki.osdev.org/Memory_Map_(x86)
 *
 * Paging
 * ======
 *
 * The Smallest Managable Unit is a Page containing 4096 (0x1000) bytes.
 *
 * A Page Directory holds 1024 tables each mapping 1024 pages, totaling
 * in 0x100000000 bytes of addressable space (4gigs).
 *
 * The first 32megs will get ALWAYS identity mapped (hold kernel code/kernel stack etc)
 * This are the first 8 page-tables
 *
 * Layout
 * ======
 *
 * We are aiming for the following layout:
 *
 * 0xFFFFFFFF
 * 0xC0000000 RESERVED
 *
 * 0xB0000000 User Stack TOP (Grows down) |
 *            env, argc, argv ...         |
 *                                        |   alltogether ~2.5gigs
 * .......... User Heap (brk())           |
 * 0x08000000 User Code                   |
 *                |
 *                | almost 100 megs  (in kernel alloc/free?)
 *                |
 * 0x02000000 FoolOS TSS Stack TOP (max ~8mb) GROWS DOWN
 *            leave few empty pages under stack as guard.
 *
 * 0x01800000 FoolOS Stack TOP (max ~8mb) GROWS DOWN
 *            leave few empty pages under stack as guard.
 *
 * 0x01000000 
 * 0x00EFFFFF RESERVED
 * 0x00100000 FoolOS Kernel (max ~14mb)
 * 0x00007BFF RESERVED
 * 0x00007000 16-bit SMP entry (max ~3kb)
 * 0x00000500
 * 0x00000000 RESERVED
 *
 */


//! i86 architecture defines 1024 entries per table--do not change
#define PAGES_PER_TABLE 1024
#define PAGES_PER_DIR	1024

#define PAGE_DIRECTORY_INDEX(x) (((x) >> 22) & 0x3ff)
#define PAGE_TABLE_INDEX(x) (((x) >> 12) & 0x3ff)
#define PAGE_GET_PHYSICAL_ADDRESS(x) (*x & ~0xfff)

//! page table represents 4mb address space
#define PTABLE_ADDR_SPACE_SIZE 0x400000

//! directory table represents 4gb address space
#define DTABLE_ADDR_SPACE_SIZE 0x100000000

//! page sizes are 4kb
#define PAGE_SIZE 4096
 
//! page table entry
typedef uint32_t pt_entry;

//! a page directery entry
typedef uint32_t pd_entry;

//! page table
typedef struct ptable_struct {
    pt_entry m_entries[PAGES_PER_TABLE];
}ptable ;
 
//! page directory
typedef struct pdirectory_struct {
    pd_entry m_entries[PAGES_PER_DIR];
}pdirectory;

pdirectory* vmem_init(uint32_t kernel_blocks,uint32_t fb_addr);