added code to reset segmentation and enable paging; added fmt_xl2a() in kio; added debug prints in k_bootstrap() since kernel is crashing when trying to enable paging

git-svn-id: svn://anubis/hos/trunk@49 5b3e749e-e535-0410-8002-a9bb6afbdfca
This commit is contained in:
josh 2009-07-13 22:58:48 +00:00
parent 0fb590a407
commit df719b4281
6 changed files with 81 additions and 10 deletions

View File

@ -8,7 +8,7 @@
%define MULTIBOOT_HEADER_FLAGS 0x00010003
%define VIRTUAL_OFFSET 0xE0000000 ; kernel virtual addr
%define CONSOLE_MEMORY VIRTUAL_OFFSET + 0xB8000
%define CONSOLE_MEMORY 0xB8000
%define PAGE_SIZE 0x1000 ; 4KB pages
@ -16,7 +16,7 @@
extern _end, _bss
; Symbols from C
extern k_bootstrap, bootstrap_stack
extern k_bootstrap, bootstrap_stack, mm_gdtr
;-------------------------------------------------------
[section .text]
@ -48,7 +48,7 @@ multiboot_header:
;**************************************************************************
multiboot_entry:
mov cx, 0x0700 + 'a'
mov [CONSOLE_MEMORY-VIRTUAL_OFFSET+160*8+0*2], cx
mov [CONSOLE_MEMORY+160*8+0*2], cx
lgdt [gdtr_tmp32-VIRTUAL_OFFSET] ; load temporary GDTR
jmp KERNEL_CODE_32_TMP_SEG:segmented_start
@ -67,7 +67,7 @@ segmented_start:
mov esp, bootstrap_stack+4096-4 ; set up temporary stack space
mov cx, 0x0700 + 'b'
mov [CONSOLE_MEMORY+160*8+1*2], cx
mov [CONSOLE_MEMORY+VIRTUAL_OFFSET+160*8+1*2], cx
add ebx, VIRTUAL_OFFSET
push eax ; multiboot bootloader magic value
@ -76,13 +76,37 @@ segmented_start:
add esp, 8
mov cx, 0x0700 + 'e'
mov [CONSOLE_MEMORY+160*8+4*2], cx
mov [CONSOLE_MEMORY+VIRTUAL_OFFSET+160*8+4*2], cx
lgdt [mm_gdtr-VIRTUAL_OFFSET] ; load permanent GDTR
jmp 0x8:segmentation_disabled-VIRTUAL_OFFSET
;**************************************************************************
;* At this point both segmentation and paging are disabled but we can *
;* now enable paging *
;**************************************************************************
segmentation_disabled:
mov cx, 0x10
mov ss, cx
mov ds, cx
mov es, cx
mov gs, cx
mov fs, cx
; at this point paging is still disabled but the PDBR is valid and
; points to a page directory that has all of RAM mapped to the
; beginning of the kernel's virtual address space, and the entire
; kernel mapped in above 0xE000_0000.
; turn on paging
mov eax, cr0
bts eax, 31
mov cr0, eax
; OK, segmentation is disabled and paging is active!
mov cx, 0x0700 + 'f'
mov [CONSOLE_MEMORY+160*8+5*2], cx
idle_loop:
hlt
jmp idle_loop

View File

@ -49,6 +49,15 @@ u32_t k_bootstrap(mb_info_t * mb_info, u32_t mb_magic)
kio_bootstrap();
kprintf("Hello from kprintf()! %d, %x, %u, %l\n", 42, 0xabcd0123, 0xFFFFFFFF, -1234567891234ll);
#if 0
kprintf("d1: 0x%X\n", MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 1));
kprintf("d2: 0x%X\n", MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 0));
for (;;)
;
#endif
return 0;
}

View File

@ -12,6 +12,7 @@ static void fmt_u2a(char * buf, unsigned int val);
static void fmt_ll2a(char * buf, long long val);
static void fmt_ull2a(char * buf, unsigned long long val);
static void fmt_x2a(char * buf, unsigned int val);
static void fmt_xl2a(char * buf, unsigned long long val);
static void fmt_o2a(char * buf, unsigned int val);
static int cursor_x, cursor_y;
@ -76,6 +77,10 @@ void kvprintf(const char * fmt, va_list args)
fmt_x2a(tmpbuf, va_arg(args, unsigned int));
kputs(tmpbuf);
break;
case 'X':
fmt_xl2a(tmpbuf, va_arg(args, unsigned long long));
kputs(tmpbuf);
break;
case '%':
kputc('%');
break;
@ -228,6 +233,26 @@ static void fmt_x2a(char * buf, unsigned int val)
*buf = '\0';
}
static void fmt_xl2a(char * buf, unsigned long long val)
{
fmt_x2a(buf, val >> 32);
buf += strlen(buf);
bool printing = ( (val >> 32) != 0 );
for (int s = 28; s >= 0; s -= 4)
{
unsigned int n = (val >> s) & 0xF;
if (n)
{
printing = true;
}
if (printing)
{
*buf++ = "0123456789abcdef"[n];
}
}
*buf = '\0';
}
static void fmt_o2a(char * buf, unsigned int val)
{
bool printing = false;

View File

@ -12,6 +12,16 @@ void strcpy(char * dst, const char * src)
*dst = '\0';
}
u32_t strlen(const char * s)
{
u32_t len = 0;
while (*s++)
{
len++;
}
return len;
}
void memcpy(u8_t * dst, u8_t * src, u32_t size)
{
for (u32_t n = 0; n < size; n++)

View File

@ -10,6 +10,8 @@ extern "C" {
void strcpy(char * dst, const char * src);
u32_t strlen(const char * s);
void memcpy(u8_t * dst, u8_t * src, u32_t size);
void memcpyw(u16_t * dst, u16_t * src, u32_t size);
void memcpyd(u32_t * dst, u32_t * src, u32_t size);

View File

@ -15,6 +15,7 @@ static int mm_mmap_num_entries = 0;
static int mm_num_free_pages = 0;
u32_t * mm_free_page_ptr = NULL;
gdtr_t mm_gdtr;
static u64_t * mm_gdt;
/**************************************************************************
* This function is run in segmented memory before paging is in effect. *
@ -145,12 +146,12 @@ void mm_bootstrap()
/* set up the global descriptor table */
u32_t gdt_base = mm_page_alloc();
u64_t * gdt = (u64_t *) ((u32_t) gdt_base + (u32_t) KERNEL_OFFSET);
memsetd((u32_t *) gdt, 0, PAGE_SIZE / 4);
mm_gdtr.length = PAGE_SIZE - 1;
mm_gdt = (u64_t *) ((u32_t) gdt_base + (u32_t) KERNEL_OFFSET);
mm_gdt[0] = 0x0ull;
mm_gdt[1] = MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 1);
mm_gdt[2] = MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 0);
mm_gdtr.length = 3*sizeof(mm_gdt[0]) - 1;
mm_gdtr.phys_addr = gdt_base;
gdt[1] = MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 1);
gdt[2] = MAKE_DESCRIPTOR(0, 0xFFFFF, 1, 0, 1, 0);
/* set the page directory base register */
set_cr3(page_directory);