Commit 6f768eab by malcolm

Merge branch 'paging' into 'master'

Paging

Enables paging. I have yet to determine exactly how this affects the rest of the system.

See merge request !7
parents 490cafc1 36eb51a1
#if !defined(__cplusplus)
#include <stdbool.h> /* C doesn't have booleans by default. */ #include <stdbool.h> /* C doesn't have booleans by default. */
#endif
#include <stddef.h> #include <stddef.h>
#include <stdint.h>
/* Check if the compiler thinks we are targeting the wrong operating system. */
#if defined(__linux__)
#error "You are not using a cross-compiler, you will most certainly run into trouble"
#endif
/* This tutorial will only work for the 32-bit ix86 targets. */
#if !defined(__i386__)
#error "This tutorial needs to be compiled with a ix86-elf compiler"
#endif
#include "serial.h" #include "serial.h"
#include "term.h" #include "term.h"
#include "mem.h"
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" /* Use C linkage for kernel_main. */ extern "C" /* Use C linkage for kernel_main. */
...@@ -31,4 +21,14 @@ void kernel_main() { ...@@ -31,4 +21,14 @@ void kernel_main() {
serial_writestring("First Serial Test!\n"); serial_writestring("First Serial Test!\n");
terminal_writestring("Hello, kernel World!\n"); terminal_writestring("Hello, kernel World!\n");
serial_writestring("Second Serial Test\n"); serial_writestring("Second Serial Test\n");
// Enable paging of memory. For now there is only one virtual
// memory space defined by page_dir
page_directory_entry* page_dir;
page_dir = initiate_directory();
initiate_page_table(page_dir);
loadPageDirectory(page_dir);
enablePaging();
serial_writestring("Finished Initilizing memory\n");
} }
...@@ -2,6 +2,7 @@ C=/home/adam/college/kernel/cross/bin/i686-elf-gcc ...@@ -2,6 +2,7 @@ C=/home/adam/college/kernel/cross/bin/i686-elf-gcc
all: all:
/usr/bin/nasm -felf32 head.asm -o boot.o /usr/bin/nasm -felf32 head.asm -o boot.o
/usr/bin/nasm -felf32 paging.asm
$C *.c -std=gnu99 -ffreestanding -O2 -Wall -Wextra -c $C *.c -std=gnu99 -ffreestanding -O2 -Wall -Wextra -c
$C -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib *.o -lgcc $C -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib *.o -lgcc
......
/*
mem.c is the main file for dealing with virtual memory and paging.
*/
#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)));
// page_table_one is the first page table, and at the beginning the
// only page table in the page directory
page_table_entry page_table_one[1024] __attribute__((aligned(4096)));
page_directory_entry* initiate_directory() {
// set each entry to not present
int i;
for(i = 0; i < 1024; i++)
{
// This sets the following flags to the pages:
// Supervisor: Only kernel-mode can access them
// 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;
}
return page_directory;
}
void initiate_page_table(page_directory_entry* page_dir) {
// holds the physical address where we want to start mapping these pages to.
// in this case, we want to map these pages to the very beginning of memory.
unsigned int i;
//we will fill all 1024 entries in the table, mapping 4 megabytes
for(i = 0; i < 1024; i++)
{
// As the address is page aligned, it will always leave 12 bits zeroed.
// Those bits are used by the attributes:
// -> supervisor level, read/write, present.
page_table_one[i] = 0;
page_table_one[i] |= i << 12;
page_table_one[i] |= PAGE_WRITABLE | PAGE_PRESENT;
}
// attributes: supervisor level, read/write, present
page_dir[0] = (uint32_t) page_table_one;
page_dir[0] |= PAGE_WRITABLE | PAGE_PRESENT;
}
/*
mem.h
*/
#ifndef _MEM_INC
#define _MEM_INT
#include <stdbool.h>
#include <stdint.h>
// Flags for pages directory entries and table entries
#define PAGE_PRESENT 0x1
#define PAGE_WRITABLE 0x2
#define PAGE_USER 0x4
#define PAGE_WRITE_THROUGH 0x8
#define PAGE_CACHE_DISABLED 0x10
#define PAGE_ACCESSED 0x20
#define PAGE_DIRTY 0x80
#define PAGE_SIZE_FLAG 0x80
typedef uint32_t page_directory_entry;
typedef uint32_t page_table_entry;
// These two are defined in the assembly file paging.asm
extern void loadPageDirectory(page_directory_entry*);
extern void enablePaging();
// Defined in the file mem.c
page_directory_entry* initiate_directory();
void initiate_page_table(page_directory_entry* page_dir);
#endif
section .text
global loadPageDirectory
loadPageDirectory:
push ebp
mov ebp, esp
mov eax, [esp+8]
mov cr3, eax
mov esp, ebp
pop ebp
ret
global enablePaging
enablePaging:
push ebp
mov ebp, esp
mov eax, cr0
or eax, 0x80000000
mov cr0, eax
mov esp, ebp
pop ebp
ret
...@@ -32,3 +32,18 @@ void serial_writestring(char* s) { ...@@ -32,3 +32,18 @@ void serial_writestring(char* s) {
s++; s++;
} }
} }
const char* hexchars = "0123456789abcdef";
void serial_hexword(uint32_t x) {
for (int i = 8; i != 0; i--) {
serial_writechar(hexchars[x>>i*4&0xf]);
}
}
void serial_hexstring(void* s, uint32_t n) {
char* d = (char*)s;
for (uint32_t i = 0; i < n; i++) {
serial_writechar(hexchars[(d[i]>>4)&0xf]);
serial_writechar(hexchars[d[i]&0xf]);
}
}
...@@ -2,7 +2,11 @@ ...@@ -2,7 +2,11 @@
serial.h contains headers for the serial interaction functions serial.h contains headers for the serial interaction functions
*/ */
#ifndef _SERIAL_INC
#define _SERIAL_INC
#include "asm.h" #include "asm.h"
#include <stdint.h>
// Initilizes the first serial port // Initilizes the first serial port
void serial_init(); void serial_init();
...@@ -15,3 +19,9 @@ void serial_writechar(char a); ...@@ -15,3 +19,9 @@ void serial_writechar(char a);
// Write a null terminated string to serial // Write a null terminated string to serial
void serial_writestring(char* s); void serial_writestring(char* s);
// Function to write a 4 byte word toserial in hex
void serial_hexword(uint32_t x);
void serial_hexstring(void* s, uint32_t n);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment