This commit is contained in:
Gvidas Juknevičius 2024-05-23 21:39:00 +03:00
commit 6de42c3dba
Signed by: MCorange
GPG Key ID: 12B1346D720B7FBB
15 changed files with 445 additions and 0 deletions

7
.cargo/config.toml Normal file
View File

@ -0,0 +1,7 @@
[unstable]
bindeps = true
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
[build]
target = "x86_64-unknown-none"

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
/boot/target

16
Cargo.lock generated Normal file
View File

@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "poppin-kernel"
version = "0.1.0"
dependencies = [
"log",
]

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[workspace]
members = [
"kernel"
]
exclude = ["boot"]
resolver = "2"

182
boot/boot.s Normal file
View File

@ -0,0 +1,182 @@
global _start
extern long_mode_start
section .text
bits 32
_start:
; mov edx, error_messages.no_long_mode
; mov ecx, error_messages.attrib
; call _puts
; .loop:
; jmp .loop
mov esp, stack_top
mov edi, ebx ; move Multiboot info pointer to edi
call check_multiboot
call check_cpuid
call check_long_mode
call set_up_page_tables
call enable_paging
; load the 64-bit GDT
lgdt [gdt64.pointer]
jmp gdt64.code:long_mode_start
; print `OK` to screen
mov dword [0xb8000], 0x2f4b2f4f
hlt
check_multiboot:
cmp eax, 0x36d76289
jne .no_multiboot
ret
.no_multiboot:
mov al, "0"
jmp error
check_cpuid:
; Check if CPUID is supported by attempting to flip the ID bit (bit 21)
; in the FLAGS register. If we can flip it, CPUID is available.
; Copy FLAGS in to EAX via stack
pushfd
pop eax
; Copy to ECX as well for comparing later on
mov ecx, eax
; Flip the ID bit
xor eax, 1 << 21
; Copy EAX to FLAGS via the stack
push eax
popfd
; Copy FLAGS back to EAX (with the flipped bit if CPUID is supported)
pushfd
pop eax
; Restore FLAGS from the old version stored in ECX (i.e. flipping the
; ID bit back if it was ever flipped).
push ecx
popfd
; Compare EAX and ECX. If they are equal then that means the bit
; wasn't flipped, and CPUID isn't supported.
cmp eax, ecx
je .no_cpuid
ret
.no_cpuid:
mov al, "1"
jmp error
check_long_mode:
; test if extended processor info in available
mov eax, 0x80000000 ; implicit argument for cpuid
cpuid ; get highest supported argument
cmp eax, 0x80000001 ; it needs to be at least 0x80000001
jb .no_long_mode ; if it's less, the CPU is too old for long mode
; use extended info to test if long mode is available
mov eax, 0x80000001 ; argument for extended processor info
cpuid ; returns various feature bits in ecx and edx
test edx, 1 << 29 ; test if the LM-bit is set in the D-register
jz .no_long_mode ; If it's not set, there is no long mode
ret
.no_long_mode:
mov al, "2"
jmp error
set_up_page_tables:
; map first P4 entry to P3 table
mov eax, p3_table
or eax, 0b11 ; present + writable
mov [p4_table], eax
; map first P3 entry to P2 table
mov eax, p2_table
or eax, 0b11 ; present + writable
mov [p3_table], eax
; map each P2 entry to a huge 2MiB page
mov ecx, 0 ; counter variable
.map_p2_table:
; map ecx-th P2 entry to a huge page that starts at address 2MiB*ecx
mov eax, 0x200000 ; 2MiB
mul ecx ; start address of ecx-th page
or eax, 0b10000011 ; present + writable + huge
mov [p2_table + ecx * 8], eax ; map ecx-th entry
inc ecx ; increase counter
cmp ecx, 512 ; if counter == 512, the whole P2 table is mapped
jne .map_p2_table ; else map the next entry
ret
enable_paging:
; load P4 to cr3 register (cpu uses this to access the P4 table)
mov eax, p4_table
mov cr3, eax
; enable PAE-flag in cr4 (Physical Address Extension)
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
; set the long mode bit in the EFER MSR (model specific register)
mov ecx, 0xC0000080
rdmsr
or eax, 1 << 8
wrmsr
; enable paging in the cr0 register
mov eax, cr0
or eax, 1 << 31
mov cr0, eax
ret
; Prints `ERR: ` and the given error code to screen and hangs.
; parameter: error code (in ascii) in al
error:
mov dword [0xb8000], 0x4f524f45
mov dword [0xb8004], 0x4f3a4f52
mov dword [0xb8008], 0x4f204f20
mov byte [0xb800a], al
hlt
section .bss
align 4096
global mem_start
mem_start:
resb 4096 * 9
mem_end:
p4_table:
resb 4096
p3_table:
resb 4096
p2_table:
resb 4096
stack_bottom:
resb 4096 * 4
stack_top:
section .data
gdt64:
dq 0 ; zero entry
.code: equ $ - gdt64 ; new
dq (1<<43) | (1<<44) | (1<<47) | (1<<53) ; code segment
.pointer:
dw $ - gdt64 - 1
dq gdt64

7
boot/grub.cfg Normal file
View File

@ -0,0 +1,7 @@
set timeout=0
set default=0
menuentry "poppin" {
multiboot2 /boot/poppin.bin
}

22
boot/long_mode.s Normal file
View File

@ -0,0 +1,22 @@
global long_mode_start
extern kmain
section .text
bits 64
long_mode_start:
; load 0 into all data segment registers
mov ax, 0
mov ss, ax
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
; call the rust main
extern kmain
call kmain
; print `OKAY` to screen
mov rax, 0x2f592f412f4b2f4f
mov qword [0xb8000], rax
hlt

15
boot/multiboot.s Normal file
View File

@ -0,0 +1,15 @@
section .multiboot_header
header_start:
dd 0xe85250d6 ; magic number (multiboot 2)
dd 0 ; architecture 0 (protected mode i386)
dd header_end - header_start ; header length
; checksum
dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
; insert optional multiboot tags here
; required end tag
dw 0 ; type
dw 0 ; flags
dd 8 ; size
header_end:

105
build.sh Executable file
View File

@ -0,0 +1,105 @@
BUILD_DIR="./target"
BIN="$BUILD_DIR/poppin.bin"
ISO="$BUILD_DIR/poppin.iso"
set -e
function cmd_exists() {
if ! command -v $1 &> /dev/null
then
echo "The command '$1' could not be found please install it"
exit 1
fi
}
function pre_build() {
cmd_exists cargo
cmd_exists xorriso
cmd_exists grub-file
cmd_exists ld
cmd_exists grub-mkrescue
cmd_exists qemu-system-x86_64
}
function build_kernel() {
echo "INFO: Building poppin-kernel"
pushd kernel > /dev/null
cargo build --release --target-dir ../${BUILD_DIR}
popd > /dev/null
}
function build_boot() {
echo "INFO: Building boot code"
nasm -felf64 -o $BUILD_DIR/long_mode.o boot/long_mode.s
nasm -felf64 -o $BUILD_DIR/boot.o boot/boot.s
nasm -felf64 -o $BUILD_DIR/multiboot.o boot/multiboot.s
}
function link(){
echo "INFO: Linking"
ld -melf_x86_64 -n \
-T linker.ld \
-o ${BIN} \
${BUILD_DIR}/multiboot.o \
${BUILD_DIR}/boot.o \
${BUILD_DIR}/long_mode.o \
${BUILD_DIR}/x86_64-unknown-none/release/libpoppin_kernel.a
}
function check_mb() {
if grub-file --is-x86-multiboot2 $BIN; then
echo "INFO: Kernel conatins multiboot2"
else
echo "ERROR: Kernel doesnt contain multiboot2"
exit 1
fi
}
function make_iso() {
echo "INFO: Building ISO"
mkdir -p ${BUILD_DIR}/iso/boot/grub
cp ${BUILD_DIR}/poppin.bin ${BUILD_DIR}/iso/boot/poppin.bin
cp boot/grub.cfg ${BUILD_DIR}/iso/boot/grub/grub.cfg
grub-mkrescue -o ${ISO} ${BUILD_DIR}/iso > /dev/null 2>&1
}
function run_qemu() {
qemu-system-x86_64 -cdrom $ISO
}
function print_help() {
echo "Usage: ./build.sh [command]"
echo "Commands:"
echo " run-qemu - Compiles and Runs the built iso with qemu"
echo " build - Compiles everything"
}
function build() {
pre_build
build_kernel
build_boot
link
check_mb
make_iso
}
case $1 in
run-qemu)
build
run_qemu
;;
build)
build
;;
*)
if [ $# -gt 0 ]; then
echo "ERROR: Unknown option '$1'"
fi
print_help
exit 1
;;
esac

1
kernel/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

14
kernel/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "poppin-kernel"
version = "0.1.0"
edition = "2021"
test=false
[lib]
name="poppin_kernel"
test=false
crate-type=["staticlib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
log = "0.4.21"

20
kernel/src/lib.rs Normal file
View File

@ -0,0 +1,20 @@
#![no_std]
#![no_main]
mod logger;
#[no_mangle]
extern "C" fn kmain() -> ! {
loop {
}
}
#[panic_handler]
fn panic(_pi: &core::panic::PanicInfo) -> ! {
loop {}
}

19
kernel/src/logger.rs Normal file
View File

@ -0,0 +1,19 @@
use log::{Record, Level, Metadata};
struct SimpleLogger {
}
impl log::Log for SimpleLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Info
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
println!("{} - {}", record.level(), record.args());
}
}
fn flush(&self) {}
}

27
linker.ld Normal file
View File

@ -0,0 +1,27 @@
ENTRY(_start)
SECTIONS {
. = 1M;
.boot :
{
/* ensure that the multiboot header is at the beginning */
KEEP(*(.multiboot_header))
}
.text :
{
*(.text .text.*)
}
.rodata :
{
*(.rodata .rodata.*)
}
.data.rel.ro :
{
*(.data.rel.ro.local*) *(.data.rel.ro .data.rel.ro.*)
}
}

1
rust_toolchain Normal file
View File

@ -0,0 +1 @@
nightly