Higher half, c main
This commit is contained in:
		
							parent
							
								
									0f28d5ae84
								
							
						
					
					
						commit
						eea52c1717
					
				@ -1,10 +1,29 @@
 | 
			
		||||
ENTRY(_start)
 | 
			
		||||
 | 
			
		||||
KERNEL_OFFSET = 0xFFFFFF8000000000;
 | 
			
		||||
KERNEL_START = 0x10000;
 | 
			
		||||
 | 
			
		||||
SECTIONS
 | 
			
		||||
{
 | 
			
		||||
    .text :
 | 
			
		||||
    . = KERNEL_START + KERNEL_OFFSET;
 | 
			
		||||
    kernel_start = .;
 | 
			
		||||
    .text : AT(ADDR(.text) - KERNEL_OFFSET)
 | 
			
		||||
    {
 | 
			
		||||
        *(.multiboot)
 | 
			
		||||
        *(.text)
 | 
			
		||||
    }
 | 
			
		||||
    .rodata : AT(ADDR(.rodata) - KERNEL_OFFSET)
 | 
			
		||||
    {
 | 
			
		||||
        *(.rodata*)
 | 
			
		||||
    }
 | 
			
		||||
    .data : AT(ADDR(.data) - KERNEL_OFFSET)
 | 
			
		||||
    {
 | 
			
		||||
        *(.data)
 | 
			
		||||
    }
 | 
			
		||||
    .bss : AT(ADDR(.bss) - KERNEL_OFFSET)
 | 
			
		||||
    {
 | 
			
		||||
        *(.COMMON)
 | 
			
		||||
        *(.bss)
 | 
			
		||||
    }
 | 
			
		||||
    kernel_end = .;
 | 
			
		||||
}
 | 
			
		||||
@ -7,7 +7,7 @@ CC := ${TARGET}-gcc
 | 
			
		||||
SRC := $(wildcard **/*.[cS])
 | 
			
		||||
OBJ := $(patsubst %, %.o, $(basename $(SRC)))
 | 
			
		||||
 | 
			
		||||
CFLAGS := -Wall -Wextra -pedantic -ffreestanding
 | 
			
		||||
CFLAGS := -Wall -Wextra -pedantic -ffreestanding -mcmodel=large
 | 
			
		||||
CFLAGS += -ggdb -O0
 | 
			
		||||
ASFLAGS += -ggdb
 | 
			
		||||
CPPFLAGS += -I include
 | 
			
		||||
 | 
			
		||||
@ -17,7 +17,7 @@ _start:
 | 
			
		||||
    cli
 | 
			
		||||
 | 
			
		||||
    // Temporary stack for booting
 | 
			
		||||
    mov esp, offset BootStack
 | 
			
		||||
    mov esp, offset V2P(BootStack)
 | 
			
		||||
 | 
			
		||||
    // Enable Page Address Extension
 | 
			
		||||
    mov eax, cr4
 | 
			
		||||
@ -25,7 +25,7 @@ _start:
 | 
			
		||||
    mov cr4, eax
 | 
			
		||||
 | 
			
		||||
    // Load Page Table (defined in boot/boot_PT.S)
 | 
			
		||||
    mov eax, offset BootP4
 | 
			
		||||
    mov eax, offset V2P(BootP4)
 | 
			
		||||
    mov cr3, eax
 | 
			
		||||
 | 
			
		||||
    // Enable Long Mode
 | 
			
		||||
@ -40,10 +40,10 @@ _start:
 | 
			
		||||
    mov cr0, eax
 | 
			
		||||
 | 
			
		||||
    // Load GDT (defined in cpu/gdt.c)
 | 
			
		||||
    lgdt [GDTp]
 | 
			
		||||
    lgdt [V2P(GDTp)]
 | 
			
		||||
 | 
			
		||||
    // Jump into long mode
 | 
			
		||||
    jmp 0x8:long_mode_start
 | 
			
		||||
    jmp 0x8:V2P(long_mode_start)
 | 
			
		||||
 | 
			
		||||
.code64
 | 
			
		||||
long_mode_start:
 | 
			
		||||
@ -54,4 +54,36 @@ long_mode_start:
 | 
			
		||||
    mov ds, eax
 | 
			
		||||
    mov es, eax
 | 
			
		||||
 | 
			
		||||
    movabs rax, offset upper_memory
 | 
			
		||||
    jmp rax
 | 
			
		||||
 | 
			
		||||
upper_memory:
 | 
			
		||||
    movabs rax, offset BootStack
 | 
			
		||||
    mov rsp, rax
 | 
			
		||||
 | 
			
		||||
    mov rax, 0
 | 
			
		||||
    movabs [BootP4], rax
 | 
			
		||||
 | 
			
		||||
    mov rax, cr3
 | 
			
		||||
    mov cr3, rax
 | 
			
		||||
 | 
			
		||||
    movabs rax, offset GDTp
 | 
			
		||||
    lgdt [rax]
 | 
			
		||||
    mov rax, 0
 | 
			
		||||
    mov ss, eax
 | 
			
		||||
    mov ds, eax
 | 
			
		||||
    mov es, eax
 | 
			
		||||
 | 
			
		||||
    movabs rax, offset .reload_cs
 | 
			
		||||
    pushq 0x8
 | 
			
		||||
    push rax
 | 
			
		||||
    retfq
 | 
			
		||||
 | 
			
		||||
.reload_cs:
 | 
			
		||||
 | 
			
		||||
.extern kmain
 | 
			
		||||
    movabs rax, offset kmain
 | 
			
		||||
    call rax
 | 
			
		||||
    
 | 
			
		||||
    hlt
 | 
			
		||||
    jmp $
 | 
			
		||||
@ -6,17 +6,18 @@
 | 
			
		||||
.global BootP4
 | 
			
		||||
 | 
			
		||||
BootP4:
 | 
			
		||||
    .quad offset BootP3 + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .rept ENTRIES_PER_PT - 1
 | 
			
		||||
    .quad offset V2P(BootP3) + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .rept ENTRIES_PER_PT - 2
 | 
			
		||||
        .quad 0
 | 
			
		||||
    .endr
 | 
			
		||||
    .quad offset V2P(BootP3) + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
BootP3:
 | 
			
		||||
    .quad offset BootP2 + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .quad offset V2P(BootP2) + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .rept ENTRIES_PER_PT - 1
 | 
			
		||||
        .quad 0
 | 
			
		||||
    .endr
 | 
			
		||||
BootP2:
 | 
			
		||||
    .quad offset BootP1 + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .quad offset V2P(BootP1) + (PAGE_PRESENT | PAGE_WRITE)
 | 
			
		||||
    .rept ENTRIES_PER_PT - 1
 | 
			
		||||
        .quad 0
 | 
			
		||||
    .endr
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										25
									
								
								src/kernel/boot/kmain.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/kernel/boot/kmain.c
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,25 @@
 | 
			
		||||
#include <memory.h>
 | 
			
		||||
 | 
			
		||||
void clear_screen()
 | 
			
		||||
{
 | 
			
		||||
    unsigned char *vidmem = P2V(0xB8000);
 | 
			
		||||
    for(int i=0; i < 80*24*2; i++)
 | 
			
		||||
        *vidmem++ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void print_string(char *str)
 | 
			
		||||
{
 | 
			
		||||
    unsigned char *vidmem = P2V(0xB8000);
 | 
			
		||||
    while(*str)
 | 
			
		||||
    {
 | 
			
		||||
        *vidmem++ = *str++;
 | 
			
		||||
        *vidmem++ = 0x7;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kmain()
 | 
			
		||||
{
 | 
			
		||||
    clear_screen();
 | 
			
		||||
    print_string("Hello from c, world!");
 | 
			
		||||
    for(;;);
 | 
			
		||||
}
 | 
			
		||||
@ -1,5 +1,16 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#define KERNEL_OFFSET 0xFFFFFF8000000000
 | 
			
		||||
 | 
			
		||||
#ifdef __ASSEMBLER__
 | 
			
		||||
#define V2P(a) ((a) - KERNEL_OFFSET)
 | 
			
		||||
#define P2V(a) ((a) + KERNEL_OFFSET)
 | 
			
		||||
#else
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
#define V2P(a) ((uintptr_t)(a) &~KERNEL_OFFSET)
 | 
			
		||||
#define P2V(a) ((void *)((uintptr_t)(a) | KERNEL_OFFSET))
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#define PAGE_SIZE 0x1000
 | 
			
		||||
 | 
			
		||||
#define PAGE_PRESENT 0x001
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user