Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Minor] Adds VGA support and jumps corectly to 64 bit operating #17

Merged
merged 33 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
30f2c9e
add: vga file from feat/bootloader
Mrgoblings Nov 2, 2024
c0d90a7
update: compile with "cargo build" instead of "cargo rustc". Linker a…
Mrgoblings Nov 2, 2024
b836bca
chore: clean up the build scipt
Mrgoblings Nov 2, 2024
2362353
chore: set types for build script variables
Mrgoblings Nov 2, 2024
a99f9a0
update: makefile now corrected for the changes in building
Mrgoblings Nov 2, 2024
e9b195f
chore: corected make clean and removed "out/" from .gitignore
Mrgoblings Nov 2, 2024
55d8ac4
test: Cargo.toml on root project
Mrgoblings Nov 2, 2024
5fba93e
chore: clean repo from target/ dir
Mrgoblings Nov 2, 2024
9f3fc36
remove: Cargo config in main dir
Mrgoblings Nov 2, 2024
d60f745
add: Makefile run quemu with vnc
Mrgoblings Nov 4, 2024
804f126
fix: os now linked with the correct output
Mrgoblings Nov 8, 2024
9d9bfca
refactor: BREAKS! wrong multiboot header
Mrgoblings Nov 8, 2024
9ff1c98
fix: linker + add Makfile for only bootloader
Mrgoblings Nov 12, 2024
273ce14
fix: correct check_multiboot magic
Mrgoblings Nov 12, 2024
538dcf9
add: rust linked
Mrgoblings Nov 12, 2024
0554d96
chore: prepare for pr
Mrgoblings Nov 12, 2024
7b68d96
chore: pr template titles made bigger
Mrgoblings Nov 12, 2024
477b7cc
chore
Mrgoblings Nov 12, 2024
3d9ec3b
temp: make the test compile the OS
Mrgoblings Nov 12, 2024
d4ca3c0
ci: workflows refactor
Mrgoblings Nov 12, 2024
ee27837
ci: wrong target
Mrgoblings Nov 12, 2024
e8ee15a
ci: rustup not configured correctly
Mrgoblings Nov 12, 2024
d2b75b2
ci: target set to nightly
Mrgoblings Nov 12, 2024
895762a
ci: rustup component set to unknown-none
Mrgoblings Nov 12, 2024
21ed39e
ci: clean the ci files
Mrgoblings Nov 12, 2024
ce8ed35
ci: add rust-src
Mrgoblings Nov 14, 2024
7aa5440
ci: add xorriso
Mrgoblings Nov 19, 2024
76b562f
ci: add component "nightly-x86_64-unknown-linux-gnu"
Mrgoblings Nov 19, 2024
8330a46
ci: refactor
Mrgoblings Nov 19, 2024
72c371b
ci: add nightly toolchain
Mrgoblings Nov 19, 2024
c2650f9
ci: rust-src component to nightly toolchain
Mrgoblings Nov 19, 2024
08d78c7
refactor: clean build output
Mrgoblings Nov 19, 2024
845eaa0
ci: release.yaml dependencies
Mrgoblings Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
**Referenced issue**
## Referenced issue
Resolves _GITHUB_ISSUE_NUMBER_

**Description**
## Description
A clear and concise description of what changes you have made.

**Root Cause (if fixing a bug)**
## Root Cause (if fixing a bug)
What was the cause of the bug?

**Breaking change?**
## Breaking change?
Is the change you are introducing going to affect already other existing resources?

- [ ] No, this code change does not affect other project resources.
- [ ] Yes, the code changes modify already existing project resources.

**Additional context**
## Additional context
Add any other context about the pull request, if applicable.
6 changes: 4 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ jobs:
- name: Install additional dependencies
run: |
sudo apt update
sudo apt install -y nasm grub-pc-bin grub-common make mtools
rustup target add x86_64-unknown-none
sudo apt install -y nasm grub-pc-bin grub-common make mtools xorriso
rustup update nightly
rustup target add x86_64-unknown-none --toolchain nightly
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu

- name: Build project
run: make no-run
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ jobs:
- name: Install additional dependencies
run: |
sudo apt update
sudo apt install -y nasm grub-pc-bin grub-common make mtools
rustup target add x86_64-unknown-none
sudo apt install -y nasm grub-pc-bin grub-common make mtools xorriso
rustup update nightly
rustup target add x86_64-unknown-none --toolchain nightly
rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu

- name: Run Tests
run: make test
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
target/
AnasOS/boot/kernel
.vscode/

*.bin
*.out
*.o
*.elf
*.iso
out/

AnasOS/boot/kernel
*.iso
4 changes: 3 additions & 1 deletion AnasOS/boot/grub/grub.cfg
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
set timeout=5

menuentry "AnasOS"{
multiboot /boot/kernel
multiboot2 /boot/kernel
boot
}
27 changes: 13 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
.PHONY: run clean

all: clean out-folder bootload kernel-rust image run
all: clean kernel-rust image run

no-run: clean out-folder bootload kernel-rust image
vnc: clean kernel-rust image run-vnc

test:
echo "No tests for now"
no-run: clean kernel-rust image

out-folder:
-@mkdir out
test: no-run
echo "Compiled the OS successfully"

kernel-rust:
@cd ./anasos-kernel && cargo rustc --release --target x86_64-unknown-none -- --emit obj
@cp `ls -1 ./anasos-kernel/target/x86_64-unknown-none/release/deps/*.o | head -n 1` ./out/kernel.o

bootload:
@nasm -f elf64 bootloader/boot.asm -o ./out/boot.o
@cd ./anasos-kernel && cargo build --release

image:
ld -m elf_x86_64 -T bootloader/linker.ld -o AnasOS/boot/kernel out/boot.o out/kernel.o
@cp ./anasos-kernel/target/x86_64-unknown-none/release/anasos-kernel AnasOS/boot/kernel
grub-mkrescue -o AnasOS.iso AnasOS/

run:
qemu-system-x86_64 AnasOS.iso

run-vnc:
qemu-system-x86_64 AnasOS.iso -vnc :0

clean:
-@rm -r out
-@rm -r anasos-kernel/target
-@cd ./anasos-kernel && cargo clean > /dev/null 2>&1
-@rm AnasOS/boot/kernel > /dev/null 2>&1
-@rm AnasOS.iso > /dev/null 2>&1
4 changes: 2 additions & 2 deletions anasos-kernel/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[build]
target = ["x86_64-unknown-none"]

[target.x86_64-unknown-none]
[target.x86_64-unknown-none]
rustflags = [
"-C", "code-model=kernel",
"-C", "relocation-model=static",
"-C", "panic=abort"
"-C", "panic=abort",
]

[unstable]
Expand Down
1 change: 0 additions & 1 deletion anasos-kernel/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion anasos-kernel/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion anasos-kernel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
name = "anasos-kernel"
version = "0.1.0"
version = "1.1.0"
authors = ["Emil Momchev <[email protected]>"]
edition = "2021"

build = "build.rs"

[dependencies]
volatile = "0.2.6"
spin = "0.9.8"
Expand Down
8 changes: 8 additions & 0 deletions anasos-kernel/bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
all:
nasm -f elf64 header.asm -o header.o
nasm -f elf64 boot-64.asm -o boot-64.o
nasm -f elf64 boot.asm -o boot.o

ld -m elf_x86_64 -T ../linker.ld -o ../../AnasOS/boot/kernel boot.o boot-64.o header.o
grub-mkrescue -o AnasOS.iso ../../AnasOS/
qemu-system-x86_64 AnasOS.iso
18 changes: 18 additions & 0 deletions anasos-kernel/bootloader/boot-64.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
GLOBAL long_mode_start
EXTERN _start

SECTION .text
BITS 64
long_mode_start:
; load null to all data segmant registers (needed for the cpu to work as intended)
MOV ax, 0
MOV ss, ax
MOV ds, ax
MOV es, ax
MOV fs, ax
MOV gs, ax

CALL _start

HLT

138 changes: 138 additions & 0 deletions anasos-kernel/bootloader/boot.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
GLOBAL start
EXTERN long_mode_start

SECTION .text
BITS 32

start:
MOV esp, stack_top
CALL check_multiboot
call check_cpuid
CALL check_long_mode

CALL setup_page_tables
CALL enable_paging

LGDT [gdt64.pointer]
JMP gdt64.code_segment:long_mode_start

HLT

check_multiboot:
CMP eax, 0x36d76289
JNE .no_multiboot
RET
.no_multiboot:
MOV al, "M"
JMP error

check_cpuid:
PUSHFD
POP eax
MOV ecx, eax
XOR eax, 1 << 21
PUSH eax
POPFD
PUSHFD
POP eax
PUSH ecx
POPFD
CMP eax, ecx
JE .no_cpuid
RET
.no_cpuid:
MOV al, "C"
JMP error

check_long_mode:
MOV eax, 0x80000000
CPUID
CMP eax, 0x80000001
JB .no_long_mode

MOV eax, 0x80000001
CPUID
TEST edx, 1 << 29
JZ .no_long_mode

RET
.no_long_mode:
MOV al, "L"
JMP error

setup_page_tables:
; identity maping (a phisical addres is linked to the same virtual addres)

MOV eax, page_table_l3
OR eax, 0b11 ; present, writable
MOV [page_table_l4], eax

MOV eax, page_table_l2
OR eax, 0b11 ; present, writable
MOV [page_table_l3], eax

MOV ecx, 0 ; counter
.loop_setup_page_tables:

MOV eax, 0x200000 ; 2MiB
MUL ecx
OR eax, 0b10000011 ; huge-page, present, writable
MOV [page_table_l2 + ecx * 8], eax

INC ecx
CMP ecx, 512 ; checks if the whole table is mapped
JNE .loop_setup_page_tables

RET

enable_paging:
; pass the page table location to the cpu
MOV eax, page_table_l4
MOV cr3, eax

; enable Phisical Address Extension (PAE)
MOV eax, cr4
OR eax, 1 << 5
MOV cr4, eax

; enable long mode
MOV ecx, 0xC0000080
RDMSR ; Read Model Specific Register instruction
OR eax, 1 << 8
WRMSR ; Write Model Specific Register instruction

; enable paging
MOV eax, cr0
OR eax, 1 << 31
MOV cr0, eax

RET

error:
; print "ERR: X", where X is the error code
MOV dword [0xB8000], 0x4F524F45
MOV dword [0xB8004], 0x4F3A4F52
MOV dword [0xB8008], 0x4F204F20
MOV byte [0xB800C], al
HLT

SECTION .bss
ALIGN 4096
page_table_l4:
RESB 4096
page_table_l3:
RESB 4096
page_table_l2:
RESB 4096
stack_bottom:
RESB 4096 * 4 ; bytes reserved for stack
stack_top:

SECTION .rodata
gdt64:
dq 0 ; zero entry
.code_segment: EQU $ - gdt64
dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53) ; code segment
.pointer:
dw $ - gdt64 - 1
dq gdt64
16 changes: 16 additions & 0 deletions anasos-kernel/bootloader/header.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
SECTION .multiboot_header
header_start:
; magic number
DD 0xe85250d6 ; multiboot2
; architecture
DD 0 ; protected mode i386
; header length
DD header_end - header_start
; checksum
DD 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))

; end tag
DW 0
DW 0
DD 8
header_end:
45 changes: 45 additions & 0 deletions anasos-kernel/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use std::env;
use std::process::Command;

fn main() {
let target_dir: String = env::var("OUT_DIR").unwrap();

let boot_o_path: String = format!("{}/boot.o", target_dir);
let boot_64_o_path: String = format!("{}/boot-64.o", target_dir);
let header_o_path = format!("{}/header.o", target_dir);


let status_boot = Command::new("nasm")
Mrgoblings marked this conversation as resolved.
Show resolved Hide resolved
.args(&["-f", "elf64", "bootloader/boot.asm", "-o", &boot_o_path])
.status()
.expect("Failed to assemble boot.asm");

if !status_boot.success() {
panic!("Assembly of boot.asm failed with status: {}", status_boot);
}

let status_boot_64 = Command::new("nasm")
.args(&["-f", "elf64", "bootloader/boot-64.asm", "-o", &boot_64_o_path])
.status()
.expect("Failed to assemble boot-64.asm");

if !status_boot_64.success() {
panic!("Assembly of boot.asm failed with status: {}", status_boot_64);
}

let status_header = Command::new("nasm")
.args(&["-f", "elf64", "bootloader/header.asm", "-o", &header_o_path])
.status()
.expect("Failed to assemble header.asm");

if !status_header.success() {
panic!("Assembly of header.asm failed with status: {}", status_header);
}


// Custom linker arguments
println!("cargo:rustc-link-arg=-Tlinker.ld");
println!("cargo:rustc-link-arg={}", boot_o_path);
println!("cargo:rustc-link-arg={}", boot_64_o_path);
println!("cargo:rustc-link-arg={}", header_o_path);
}
Loading
Loading