Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
triazo
/
kernel
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
3
Merge Requests
0
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ebfd2cc5
authored
Dec 07, 2015
by
triazo
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
start reworking memory. Again.
parent
991bcbe0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
15 deletions
src/mem.c
src/mem.h
src/mem.c
View file @
ebfd2cc5
...
...
@@ -5,10 +5,6 @@
#include "mem.h"
#include "serial.h"
// page_directory is currently defined once and should not be touched
// later. It must be page aligned for the MMU to read properly
page_directory_entry
page_directory
[
1024
]
__attribute__
((
aligned
(
4096
)));
// populate_page_table makes a page table at `table` mapping `num`
// pages to bytes after `start + offset*4KB`
// both table and start must be properly aligned
...
...
@@ -73,6 +69,22 @@ void page_free(void* page_start)
*
index
=
~
(
~*
index
|
(
1
<<
(
8
-
(
page_number
%
8
))));
}
// Just pages the entire addressable space. Takes about 4MB of space.
// Should be ~fine~ this won't break things I totally know what I'm doing
void
page_everything
(
page_directory_entry
*
pg_dir
)
{
for
(
int
i
=
0
;
i
<
1024
;
i
++
)
{
page_table_entry
*
pg_tab
=
(
page_table_entry
*
)
page_allocate
();
pg_dir
[
i
]
=
(
page_directory_entry
)((
uint32_t
)
pg_tab
|
PAGE_WRITABLE
|
PAGE_PRESENT
);
for
(
int
j
=
0
;
j
<
1024
;
j
++
)
{
void
*
page
=
(
void
*
)((
i
<<
22
)
|
(
j
<<
12
));
pg_tab
[
j
]
=
(
page_directory_entry
)((
uint32_t
)
page
|
PAGE_WRITABLE
|
PAGE_PRESENT
);
}
}
}
// This goes through all available memory, and maps 1 to 1 virtual memory to it.
page_directory_entry
*
mem_init_kern_tables
(
multiboot_memory_map
*
mmap
,
multiboot_memory_map
*
mmap_end
)
{
...
...
@@ -100,8 +112,8 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
// Lower memory should be first in the page directory and
// mapped one to one
populate_page_table
(
next_table
,
0
,
1024
,
0
);
page_directory
[
0
]
=
(
uint32_t
)
next_table
;
page_directory
[
0
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
kern_page_dir
[
0
]
=
(
uint32_t
)
next_table
;
kern_page_dir
[
0
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
current_dir_entry
=
1
;
/* next_table += 0x1000; */
...
...
@@ -112,8 +124,8 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
for
(
i
=
0
;
i
+
0x1000000
<
mmap
->
length
;
i
+=
0x1000000
)
{
populate_page_table
(
next_table
,
(
void
*
)(
intptr_t
)
mmap
->
base_addr
+
i
,
1024
,
0
);
page_directory
[
current_dir_entry
]
=
(
uint32_t
)
next_table
;
page_directory
[
current_dir_entry
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
kern_page_dir
[
current_dir_entry
]
=
(
uint32_t
)
next_table
;
kern_page_dir
[
current_dir_entry
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
current_dir_entry
+=
1
;
/* next_table += 0x1000; */
...
...
@@ -123,8 +135,8 @@ page_directory_entry* mem_init_kern_tables(multiboot_memory_map* mmap, multiboot
if
(
i
!=
mmap
->
length
)
{
populate_page_table
(
next_table
,
(
void
*
)(
intptr_t
)
mmap
->
base_addr
+
i
,
(
mmap
->
length
-
i
)
/
0x1000
,
0
);
page_directory
[
current_dir_entry
]
=
(
uint32_t
)
next_table
;
page_directory
[
current_dir_entry
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
kern_page_dir
[
current_dir_entry
]
=
(
uint32_t
)
next_table
;
kern_page_dir
[
current_dir_entry
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
current_dir_entry
+=
1
;
/* next_table += 0x1000; */
...
...
@@ -206,15 +218,15 @@ page_directory_entry* initiate_directory()
// Write Enabled: It can be both read from and written to
// (this is the 2 bit)
// Not Present: The page table is not present
page_directory
[
i
]
=
0
;
page_directory
[
i
]
|=
PAGE_WRITABLE
;
kernel_
page_directory
[
i
]
=
0
;
kernel_
page_directory
[
i
]
|=
PAGE_WRITABLE
;
}
// Set last directory entry to point back
page_directory
[
1023
]
=
(
uint32_t
)
page_directory
;
page_directory
[
1023
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
kernel_page_directory
[
1023
]
=
(
uint32_t
)
kernel_
page_directory
;
kernel_
page_directory
[
1023
]
|=
PAGE_WRITABLE
|
PAGE_PRESENT
;
return
page_directory
;
return
kernel_
page_directory
;
}
void
*
malloc
(
uint32_t
bytes
)
...
...
@@ -229,3 +241,21 @@ void* kmalloc(uint32_t size __attribute__((unused)))
{
return
page_allocate
();
}
// Debugging
void
*
page_table_inspect
(
page_directory_entry
*
pg_dir
,
void
*
addr
)
{
serial_writestring
(
"Inspecting page tables for address 0x"
);
serial_hexword
((
uint32_t
)
addr
);
serial_writestring
(
"
\n
Directory Entry: 0x"
);
page_directory_entry
page_dir_entry
=
pg_dir
[((
uint32_t
)
addr
)
>>
22
];
serial_hexword
(
page_dir_entry
);
serial_writestring
(
"
\n
Page Table Entry: 0x"
);
page_table_entry
*
page_table
=
(
page_table_entry
*
)((
uint32_t
)
page_dir_entry
&
0xfffff000
);
page_table_entry
pg_tab_entry
=
page_table
[((
uint32_t
)
addr
>>
12
)
%
1024
];
serial_hexword
(
pg_tab_entry
);
serial_writechar
(
'\n'
);
return
(
void
*
)((
uint32_t
)
pg_tab_entry
&
0xfffff000
);
}
src/mem.h
View file @
ebfd2cc5
...
...
@@ -27,6 +27,10 @@ typedef uint32_t page_table_entry;
extern
void
loadPageDirectory
(
page_directory_entry
*
);
extern
void
enablePaging
();
// page_directory is currently defined once and should not be touched
// later. It must be page aligned for the MMU to read properly
page_directory_entry
kernel_page_directory
[
1024
]
__attribute__
((
aligned
(
4096
)));
// Defined in linker.ld
extern
char
_kernel_end
[];
...
...
@@ -36,6 +40,7 @@ void page_free(void* page_start);
void
*
page_map
(
page_directory_entry
pgdir
[],
void
*
page_start
);
// Initilization
void
page_everything
(
page_directory_entry
*
pg_dir
);
page_directory_entry
*
mem_init_kern_tables
(
multiboot_memory_map
*
mmap
,
multiboot_memory_map
*
mmap_end
);
// Directory and table handling
...
...
@@ -51,6 +56,9 @@ void* memory_mark;
void
*
kmalloc
(
uint32_t
);
void
*
malloc
(
uint32_t
bytes
);
// Debugging
void
*
page_table_inspect
(
page_directory_entry
*
pg_dir
,
void
*
addr
);
// Required for elf loading - virtual memory and stuff
// TODO:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment