Loading the kernel is now successful
This commit is contained in:
Adrien Bourmault 2019-03-08 08:43:44 +01:00 committed by GitHub
parent 7be54a602c
commit bbb4931238
22 changed files with 246 additions and 214 deletions

2
.gitignore vendored
View File

@ -12,6 +12,7 @@ qemu.log
*.obj
*.elf
*.S
build/obj/*/*/*/[^.]*
# CNAME STUFF
*.yml
@ -54,6 +55,7 @@ build/bin/s**
*.hex
*.bin
*.img
build/bin/[^.]*
# Debug files
*.dSYM/

View File

@ -42,8 +42,9 @@ tests:
#Programs
ASM=nasm
ASMFLAGS=
BOOTFLAGS=-f bin
LD=ld
ASMFLAGS=-f elf64
LDFLAGS= -melf_x86_64
#Folders
MBRDIR=boot/grub
@ -51,6 +52,8 @@ LOADERDIR=boot/loader
OBJDIR=build/obj
BINDIR=build/bin
l_objects=./build/obj/kaleid/crtlib/memory.o ./build/obj/kaleid/crtlib/rand.o ./build/obj/kaleid/crtlib/string.o ./build/obj/kaleid/crtlib/ultoa.o ./build/obj/kaleid/crtlib/strtol.o ./build/obj/kaleid/crtlib/utoa.o ./build/obj/kaleid/crtlib/status.o ./build/obj/kaleid/crtlib/atoul.o ./build/obj/kaleid/crtlib/atol.o ./build/obj/kaleid/crtlib/itoa.o ./build/obj/kaleid/crtlib/ltoa.o ./build/obj/kaleid/crtlib/atou.o ./build/obj/kaleid/crtlib/arith.o ./build/obj/kaleid/crtlib/atoi.o ./build/obj/kaleid/extras/prog.o ./build/obj/kaleid/extras/argv.o ./build/obj/kaleid/kernel/init/table.o ./build/obj/kaleid/kernel/init/init.o ./build/obj/kaleid/kernel/io/vga.o ./build/obj/kaleid/kernel/io/cursor.o ./build/obj/kaleid/kernel/io/term.o ./build/obj/kaleid/kernel/ke/panic.o ./build/obj/boot/loader.o
#Color codes
CL='\033[0;32m'
CL2='\033[1;31m'
@ -65,17 +68,19 @@ boot.mbr: $(BINDIR)/disk.img $(MBRDIR)/grub.cfg
@rmdir $(BINDIR)/disk
boot.loader.asm: $(LOADERDIR)/loader.asm
@echo ${CL2}[boot.loader.asm]${NC} Making loader.bin...${CL3}
@$(ASM) $(BOOTFLAGS) $(LOADERDIR)/loader.asm -o $(OBJDIR)/boot/loader.bin > /dev/null
@echo ${CL2}[boot.loader.asm]${NC} Making loader...${CL3}
@$(ASM) $(ASMFLAGS) $(LOADERDIR)/loader.asm -o $(OBJDIR)/boot/loader.o > /dev/null
@echo ${CL2}[boot.loader.asm]${CL} OK${CL3}
loader: boot.loader.asm
loader: boot.loader.asm link copykernel
copykernel:
@mkdir -p $(BINDIR)/disk
@echo ${CL2}[loader]${NC} Constructing kernel loader...${CL3}
@echo ${CL2}[disk]${NC} Integrating kernel...${CL3}
@$(MBRDIR)/mount.sh $(BINDIR)/disk.img $(BINDIR)/disk
@cp $(OBJDIR)/boot/loader.bin $(BINDIR)/disk/boot/loader.bin
@cp $(BINDIR)/kaleid $(BINDIR)/disk/boot/kaleid
@$(MBRDIR)/umount.sh $(BINDIR)/disk
@echo ${CL2}[loader]${CL} OK${CL3}
@echo ${CL2}[disk]${CL} OK${CL3}
@rmdir $(BINDIR)/disk
make_disk:
@ -85,20 +90,27 @@ make_disk:
testloader: loader
@qemu-system-x86_64 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log &
@ndisasm $(OBJDIR)/boot/loader.bin -b 32 > loader_dism.asm
@ndisasm $(OBJDIR)/boot/loader -b 32 > loader_dism.asm
testloader32: loader
@qemu-system-i386 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -enable-kvm 2> qemu.log &
@ndisasm $(OBJDIR)/boot/loader.bin -b 32 > loader_dism.asm
@ndisasm $(OBJDIR)/boot/loader -b 32 > loader_dism.asm
debugloader: loader
@qemu-system-x86_64 -hda build/bin/disk.img -d cpu_reset,guest_errors,pcall,int -s -S -enable-kvm 2> qemu.log &
@ndisasm $(OBJDIR)/boot/loader.bin -b 32 > loader_dism.asm
@ndisasm $(OBJDIR)/boot/loader -b 32 > loader_dism.asm
boot: make_disk boot.mbr loader
@echo ${CL2}[[boot]]${CL} Terminated without error.${CL3}
all: boot kernel
all: kernel boot
@echo ${CL2}[[all]]${CL} Terminated without error.${CL3}
link:
@$(LD) $(LDFLAGS) -T build/kernel.ld $(l_objects) -o $(OBJDIR)/boot/kaleid.x86_64
@x86_64-elf-objcopy -I elf64-x86-64 -O elf32-i386 $(OBJDIR)/boot/kaleid.x86_64 $(BINDIR)/kaleid
clean:
rm -Rf $(BINDIR)/*
rm -Rf $(OBJDIR)/*/*/*/*.o

View File

@ -106,7 +106,7 @@ kernel: common
COMPILE_KERNEL(io/cursor)
COMPILE_KERNEL(io/term)
COMPILE_KERNEL(io/vga)
LINK_KERNEL(kaleid-kernel.elf)
//LINK_KERNEL(kaleid-kernel.elf)
//----------------------------------------------------------------------------#

View File

@ -27,6 +27,6 @@ set timeout=5
set default=0 #Set the default menu entry
menuentry "OS/K (pre-pre-alpha 0.0.1)" {
multiboot /boot/loader.bin # The multiboot command replaces the kernel command
multiboot /boot/kaleid # The multiboot command replaces the kernel command
boot
}

View File

@ -44,3 +44,14 @@ bitemporize:
loop .looping
pop rcx
ret
tritemporize:
push rcx
mov rcx, 20
.looping:
push rcx
call bitemporize
pop rcx
loop .looping
pop rcx
ret

View File

@ -44,7 +44,7 @@ Is64Bits:
jz .no_64 ; If it's not set, there is no long mode
ret
.no_64:
mov ax, "01" ; ERROR 01 : 64bits unsupported
mov ax, "03" ; ERROR 03 : 64bits unsupported
jmp Error
; ---------------------------------------------------------------------------- ;

View File

@ -1,26 +0,0 @@
//=--------------------------------------------------------------------------=//
// GNU GPL OS/K //
// //
// Desc: ELF64 Parser and Loader //
// (x86_64 architecture only) //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//=--------------------------------------------------------------------------=//
int stub;

View File

@ -1,33 +0,0 @@
;=----------------------------------------------------------------------------=;
; GNU GPL OS/K ;
; ;
; Desc: Basic File Allocation Table Long mode Driver ;
; (x86_64 architecture only) ;
; ;
; ;
; Copyright © 2018-2019 The OS/K Team ;
; ;
; This file is part of OS/K. ;
; ;
; OS/K is free software: you can redistribute it and/or modify ;
; it under the terms of the GNU General Public License as published by ;
; the Free Software Foundation, either version 3 of the License, or ;
; (at your option) any later version. ;
; ;
; OS/K is distributed in the hope that it will be useful, ;
; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
; GNU General Public License for more details. ;
; ;
; You should have received a copy of the GNU General Public License ;
; along with OS/K. If not, see <https://www.gnu.org/licenses/>. ;
;=----------------------------------------------------------------------------=;
[BITS 64]
;; GLOBAL DATA
UserData dw 0
;; TEXT
nop

View File

@ -25,23 +25,27 @@
;;VIDEO
%define TRAM 0x0B8000 ; [T]ext[RAM]
%define VRAM 0x0A0000 ; [V]ideo[RAM]
%define TRAM 0xB8000 ; [T]ext[RAM]
%define VRAM 0xA0000 ; [V]ideo[RAM]
%define VGA_HEIGHT 80
;; GLOBAL DATA
NextTRAM dq 0xB8000 ; Last position of cursor
VGA_HEIGHT64 dq 80
NextTRAM32 dq 0xB8000 ; Last position of cursor
VGA_X32 dq 0
VGA_HEIGHT64 dq VGA_HEIGHT
VGA_X dq 0
;; TEXT
[BITS 64]
clear:
;-----------------------------------------------------------------------;
; x64/LM Clear Text Screen Function ;
;-----------------------------------------------------------------------;
clear:
mov qword [NextTRAM], TRAM
mov edi, TRAM
push rsi
@ -56,12 +60,13 @@ clear:
pop rdi
ret
write:
;-----------------------------------------------------------------------;
; x64/LM Text Printing Functions ;
; bl : color code ;
; esi : string address ;
;-----------------------------------------------------------------------;
write:
mov edi, [NextTRAM] ; TRAM ADDRESS
push rsi
push rdi
@ -124,3 +129,47 @@ dump:
pop rsi
add qword [NextTRAM], 1000 ; Cursor moving : 1120 = 80 * 2 * 7 lignes
ret
[BITS 32]
;-----------------------------------------------------------------------;
; x32 Text Printing Functions ;
; bl : color code ;
; esi : string address ;
;-----------------------------------------------------------------------;
write32:
mov edi, TRAM ; TRAM ADDRESS
push esi
push edi
.pLoop:
lodsb
cmp al, 0 ; while @al, i.e. while we're not hitting '\0'
je .pEnd
stosb ; text subpixel
mov al, bl
stosb ; color subpixel
jmp .pLoop
.pEnd:
pop edi
pop esi
ret
disable_cursor:
pushf
push eax
push edx
mov dx, 0x3D4
mov al, 0xA ; low cursor shape register
out dx, al
inc dx
mov al, 0x20 ; bits 6-7 unused, bit 5 disables the cursor, bits 0-4 control the cursor shape
out dx, al
pop edx
pop eax
popf
ret
[BITS 64]

View File

@ -1,7 +1,7 @@
;=----------------------------------------------------------------------------=;
; GNU GPL OS/K ;
; ;
; Desc: Kernel (second stage) Loader for OS/K ;
; Desc: Kernel Loader for OS/K ;
; (x86_64 architecture only) ;
; ;
; ;
@ -23,36 +23,29 @@
; along with OS/K. If not, see <https://www.gnu.org/licenses/>. ;
;=----------------------------------------------------------------------------=;
%define DEBUG
%include "boot/loader/multiboot/header.inc"
%include "boot/loader/multiboot/check.inc"
%include "boot/loader/cpu/cpuid.inc"
%include "boot/loader/mem/management.inc"
%include "boot/loader/io/terminal.inc"
%include "boot/loader/cpu/cpu.inc"
%include "boot/loader/mem/structures.inc"
[BITS 32]
[global _start]
[ORG 0x100000] ; Where GRUB loads us.
[global MB_start]
%include "boot/loader/multiboot/header.inc"
;; NORMAL ENTRY POINT, BUT A LITTLE BIT UNUSED SINCE WE NEVER USE IT BECAUSE...
_start:
mov ax, cs ; correcting cs after the horrible far jump
mov ds, ax ; hm... And ds too
mov es, ax ; And es because it is jealous
;mov [Bootdrv], dl
xor dl, dl
jmp 0x0000:_loader ; pas sûr
[section .multiboot]
;; MAGNIFICENT MULTIBOOT HEADER FOR GRUB ------------------------------------ ;;
MB_header:
align 4
ALIGN 4
dd MB_HEADER_MAGIC
dd MB_HEADER_FLAGS
dd CHECKSUM
dd MB_header ; Header address
dd _start ; Address of code entry point
dd 00 ; (end of code) not necessary
dd 00 ; (bss) not necessary
dd MB_start ; entry address GRUB will start at
;; MULTIBOOT POINT ENTRY FOR GRUB ------------------------------------------- ;;
[section .text]
;;MULTIBOOT POINT ENTRY FOR GRUB ------------------------------------------- ;;
MB_start:
mov esp, KERNEL_STACK ; Setup the stack
push 0 ; Reset EFLAGS
@ -65,30 +58,29 @@ MB_start:
jmp Die ; Aufwiedersehen
;; THE HOLES ---------------------------------------------------------------- ;;
; ---------------------------------------------------------------------------- ;
; Prints 'ERR:XX' where 'XX' is the str in AX ;
; ---------------------------------------------------------------------------- ;
Error:
mov dword [0xb8000], 0x4f524f45
mov dword [0xb8004], 0x4f3a4f52
mov dword [0xb8008], 0x4f204f20
mov byte [0xb800a], al
mov byte [0xb800c], ah
mov byte [0xb800d], 0x4f
mov word [CODE], ax
push esi
mov bl, 0x0c
mov esi, ERGO
call write32
pop esi
jmp Die
ERGO : db 219, 219, 219, " Error "
CODE : db "00"
db 0x0
; ---------------------------------------------------------------------------- ;
; Kills the mind of your computer to get it prostrated ;
; ---------------------------------------------------------------------------- ;
Die:
cli
hlt ; die nooooow
;jmp 0xF000:0xFFF0
jmp $
;; THE CODE ----------------------------------------------------------------- ;;
; ---------------------------------------------------------------------------- ;
; _loader ;
; ;
@ -103,10 +95,7 @@ Die:
_loader:
jmp lbegin
%include "boot/loader/multiboot/check.inc"
%include "boot/loader/cpu/cpuid.inc"
%include "boot/loader/mem/structures.inc"
%include "boot/loader/mem/management.inc"
LOGO: db 219, 219, 219, " OS/K", 0
lbegin:
call MB_check
@ -118,18 +107,24 @@ lbegin:
call Setup_paging
call Go64
push esi
mov bl, 0x0E
mov esi, LOGO
call write32
pop esi
call disable_cursor
lgdt [GDT64.pointer]
jmp GDT64.code:l64
jmp GDT64.code:_loader64
[BITS 64]
%include "boot/loader/io/terminal.inc"
%include "boot/loader/io/ata.inc"
%include "boot/loader/cpu/cpu.inc"
Salut db 0x09, " Booting OS/K ", 0x09, 0x0A, 0x0D, 0x0
ReadAttempt db 0x09, " Attempt to read a sector with ATA commands...", 0
l64:
Salut db "Now in x64 long mode", 0x0A, 0x0D, 0x0
GoKernel db "Launching Kernel...", 0
_loader64:
;; Some cleanup
mov ax, 0
mov ss, ax
@ -138,6 +133,10 @@ l64:
mov fs, ax
mov gs, ax
call bitemporize
mov qword [NextTRAM], TRAM+80*4
;; Hello world
mov bl, 0x0A
mov esi, Salut
@ -145,16 +144,14 @@ l64:
;; Read ATA
mov bl, 0x0F
mov esi, ReadAttempt
mov esi, GoKernel
call write
call temporize
;mov ecx, 4000
call tritemporize
extern StartKern
jmp StartKern
;; Reading a sector of the disk
mov bl, 1
mov bh, 1
call ata_read
jmp Die

View File

@ -31,24 +31,24 @@
Setup_paging:
;; Map the first PML4 entry to PDP table
mov eax, PDP_table
or eax, 0b11 ; present + writable
or eax, 1 << 1 | 1 << 0 ; present + writable
mov [PML4_table], eax
;; Map the first PDP entry to PD table
mov eax, PD_table
or eax, 0b11 ; present + writable
or eax, 1 << 1 | 1 << 0 ; present + writable
mov [PDP_table], eax
;; Map each PD entry to a 'huge' 2MiB page
mov ecx, 0 ; counter variable
mov ecx, 0x0 ; counter variable
.map_p2_table:
;; map ecx-th PD entry to a huge page that starts at address 2MiB*ecx
mov eax, 0x200000
mul ecx ; start address of ecx-th page
or eax, 0b10000011 ; present + writable + huge
or eax, 1 << 7 | 1 << 1 | 1 << 0 ; present + writable + huge
mov [PD_table + ecx * 8], eax
inc ecx
cmp ecx, 512 ; if counter == 512, the whole PD table is mapped
cmp ecx, 512 ; PD table is mapped if 512
jne .map_p2_table ; else map the next entry
ret
@ -68,14 +68,14 @@ Go64:
mov cr4, eax ; Enable PAE
;; Activate long mode
mov ecx, 0xC0000080 ; address of MSR
rdmsr ; read MSR
mov ecx, 0xC0000080 ; Address of MSR
rdmsr ; Read MSR
or eax, 1 << 8 ; LME = 1. (Long Mode Enable)
wrmsr ; write MSR
wrmsr ; Write MSR
;; Enable paging
mov eax, cr0
or eax, 1 << 31 ; make MSR bit 31 (PG = Paging) to 1 :
or eax, 1 << 31 ; Make MSR bit 31 (PG = Paging) to 1 :
; |1|000000000000000000000000000000
; |
; `------ Paging bit
@ -93,16 +93,16 @@ Go64:
; ---------------------------------------------------------------------------- ;
CheckA20:
pushad
mov edi,0x112345 ;odd megabyte address.
mov esi,0x012345 ;even megabyte address.
mov [esi],esi ;making sure that both addresses contain diffrent values.
mov [edi],edi ;(if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi))
cmpsd ;compare addresses to see if the're equivalent.
mov edi, 0x112345 ; Odd megabyte address.
mov esi, 0x012345 ; Even megabyte address.
mov [esi],esi ; Making sure that both addresses contain diffrent values.
mov [edi],edi ; (if A20 line is cleared the two pointers would point to the address 0x012345 that would contain 0x112345 (edi))
cmpsd ; Compare addresses to see if the're equivalent.
popad
jne .A20_on ;if not equivalent , A20 line is set.
jne .A20_on ; If not equivalent , A20 line is set.
jmp .A20_err
.A20_on:
ret
.A20_err:
mov ax, "03" ; ERROR 03 : A20 line failed
mov ax, "04" ; ERROR 04 : A20 line failed
jmp Error

View File

@ -24,7 +24,7 @@
;=----------------------------------------------------------------------------=;
[BITS 32]
section .rodata
;; GDT WITH DOC
ALIGN 4096
GDT64:
@ -44,6 +44,7 @@ GDT64:
dq GDT64
;; EMPTY PAGE TABLES (identity of the first 1GiB)
section .bss
ALIGN 4096
PML4_table:
resb 4096
@ -51,3 +52,4 @@ PDP_table:
resb 4096
PD_table:
resb 4096

View File

@ -24,11 +24,11 @@
;=----------------------------------------------------------------------------=;
;; MULTIBOOT HEADER
MB_AOUT_KLUDGE equ 1 << 16 ; We are not an ELF executable
MB_ALIGN equ 1 << 0 ; Ask to align loaded modules on page boundaries
MB_AOUT_KLUDGE equ 0 << 16 ; if we are not an ELF executable
MB_ALIGN equ 0 << 0 ; Ask to align loaded modules on page boundaries
MB_MEMINFO equ 1 << 1 ; Ask to provide memory map
MB_HEADER_MAGIC equ 0x1badb002
MB_GRUB_MAGIC equ 0x2badb002
MB_HEADER_FLAGS equ MB_AOUT_KLUDGE|MB_ALIGN|MB_MEMINFO
MB_HEADER_FLAGS equ 0x0 ; MB_AOUT_KLUDGE|MB_ALIGN|MB_MEMINFO
CHECKSUM equ -(MB_HEADER_MAGIC + MB_HEADER_FLAGS)
KERNEL_STACK equ 0x00200000 ; Stack starts at the 2mb address & grows down

Binary file not shown.

View File

@ -1,44 +1,68 @@
ENTRY(StartKern)
SECTIONS
{
. = 1M;
/*----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: OS/K KERNEL LINKER //
// (x86_64 architecture only) //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------*/
.text : AT(ADDR(.text) - 1M)
ENTRY(MB_start) /* the name of the entry label */
SECTIONS {
. = 0x00100000; /* the code should be loaded at 1 MB */
.boot ALIGN (0x1000) : /* align at 4 KB */
{
_code = .;
*(.text)
*(.rodata*)
. = ALIGN(4096);
*(.multiboot)
}
.data : AT(ADDR(.data) - 1M)
.text ALIGN (0x1000) : /* align at 4 KB */
{
*(.text) /* all text sections from all files */
}
.data ALIGN (0x1000) :
{
_data = .;
*(.data)
. = ALIGN(4096);
}
.eh_frame : AT(ADDR(.eh_frame) - 1M)
.eh_frame ALIGN (0x1000) :
{
_ehframe = .;
*(.eh_frame)
. = ALIGN(4096);
}
.bss : AT(ADDR(.bss) - 1M)
.bss ALIGN (0x1000) : /* align at 4 KB */
{
_bss = .;
*(.bss)
*(COMMON)
. = ALIGN(4096);
*(.bss) /* all bss sections from all files */
}
_end = .;
.rodata ALIGN (0x1000) : /* align at 4 KB */
{
*(.rodata) /* all rodata sections from all files */
}
/DISCARD/ :
{
*(.comment)
}
}

Binary file not shown.

Binary file not shown.

View File

@ -139,7 +139,6 @@ still_going:
mov dx, 0x1f0 ; Data port - data comes in and out of here.
rep insw
pop rdi
%ifdef DEBUG
mov bl, 0x0F
mov esi, buffer
call dump
@ -147,11 +146,6 @@ still_going:
mov esi, ended
call write
add qword [NextTRAM], 120 ; Cursor moving : 1120 = 80 * 2 * 7 lignes
%else
mov bl, 0x0A
mov esi, Pass
call write
%endif
pop rdx
pop rcx
pop rbx