Skip to content

Commit 53126d5

Browse files
committedSep 16, 2018
paging: expose get/alloc/free page functions
1 parent 8a257b2 commit 53126d5

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed
 

‎src/paging.c

+35-35
Original file line numberDiff line numberDiff line change
@@ -68,38 +68,6 @@ static uint32_t get_first_frame(void)
6868
return -1;
6969
}
7070

71-
static void alloc_frame(page_t *page, bool_t kernel, bool_t writable)
72-
{
73-
if (page->frame != 0)
74-
{
75-
return; // frame already allocated
76-
}
77-
78-
uint32_t index = get_first_frame();
79-
if (index == (uint32_t)-1)
80-
{
81-
PANIC("out of frames");
82-
}
83-
84-
set_frame_state(index * PAGE_SIZE, true);
85-
page->present = true;
86-
page->rw = writable ? true : false;
87-
page->user = kernel ? false : true;
88-
page->frame = index;
89-
}
90-
91-
UNUSED_FUNC static void free_frame(page_t *page)
92-
{
93-
uint32_t frame;
94-
if (!(frame = page->frame))
95-
{
96-
return; // no allocated frame
97-
}
98-
99-
set_frame_state(frame, false);
100-
page->frame = NULL;
101-
}
102-
10371
static void switch_directory(page_directory_t *new)
10472
{
10573
current_directory = new;
@@ -110,7 +78,7 @@ static void switch_directory(page_directory_t *new)
11078
asm volatile("mov %0, %%cr0" :: "r" (cr0));
11179
}
11280

113-
static page_t *get_page(page_directory_t *dir, uint32_t address, bool_t create)
81+
page_t *paging_get_page(page_directory_t *dir, uint32_t address, bool_t create)
11482
{
11583
// Turn address into an index
11684
uint32_t index = address / PAGE_SIZE;
@@ -138,6 +106,38 @@ static page_t *get_page(page_directory_t *dir, uint32_t address, bool_t create)
138106
}
139107
}
140108

109+
void paging_alloc_frame(page_t *page, bool_t kernel, bool_t writable)
110+
{
111+
if (page->frame != 0)
112+
{
113+
return; // frame already allocated
114+
}
115+
116+
uint32_t index = get_first_frame();
117+
if (index == (uint32_t)-1)
118+
{
119+
PANIC("out of frames");
120+
}
121+
122+
set_frame_state(index * PAGE_SIZE, true);
123+
page->present = true;
124+
page->rw = writable ? true : false;
125+
page->user = kernel ? false : true;
126+
page->frame = index;
127+
}
128+
129+
UNUSED_FUNC void paging_free_frame(page_t *page)
130+
{
131+
uint32_t frame;
132+
if (!(frame = page->frame))
133+
{
134+
return; // no allocated frame
135+
}
136+
137+
set_frame_state(frame, false);
138+
page->frame = NULL;
139+
}
140+
141141
static void handle_fault(registers_t registers)
142142
{
143143
// Faulting address is stored in the CR2 register
@@ -183,8 +183,8 @@ void paging_init(void)
183183
// as if paging wasn't enabled.
184184
for (uint32_t i = 0; i < placement_address; i += PAGE_SIZE)
185185
{
186-
page_t *page = get_page(kernel_directory, i, true);
187-
alloc_frame(page, false, false);
186+
page_t *page = paging_get_page(kernel_directory, i, true);
187+
paging_alloc_frame(page, false, false);
188188
}
189189

190190
// Register page fault handler

‎src/paging.h

+11
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,15 @@ typedef struct
4242
uint32_t physAddress;
4343
} page_directory_t;
4444

45+
// Get the page for the specified address from the given directory, optionally
46+
// creating the page if it does not already exist.
47+
page_t *paging_get_page(page_directory_t *dir, uint32_t address, bool_t create);
48+
49+
// Allocate a frame for the given page with the specified permissions.
50+
void paging_alloc_frame(page_t *page, bool_t kernel, bool_t writable);
51+
52+
// Free an allocated frame for the given page.
53+
void paging_free_frame(page_t *page);
54+
55+
// Initialise paging.
4556
void paging_init(void);

0 commit comments

Comments
 (0)
Please sign in to comment.