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

[WIP] Basic Userspace #81

Draft
wants to merge 18 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ kernel/Cargo.lock
build/
kernel/target/
*.bxrc
serial.log
73 changes: 35 additions & 38 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
build_containing_dir := build

debug ?= 0

ifneq ($(debug), 1)
ifneq (debug, 1)
else ifndef log_level
log_level := debug
endif

ifndef log_level
log_level := ""
endif
log_level ?= ""

ifeq ($(debug), 1)
nasm_flags := -f elf64 -F dwarf -g
build_type := debug
qemu_flags := -s -m 256M -d int -no-reboot -no-shutdown -monitor stdio -serial file:serial.log
cargo_flags := --features $(log_level)
kernel_cargo_flags := --features $(log_level)
else
nasm_flags := -f elf64
cargo_flags := --release --features $(log_level)
rustflags := "-C code-model=kernel"
kernel_cargo_flags := --release --features $(log_level)
init_cargo_flags := --release
build_type := release
qemu_flags := -m 256M -serial file:serial.log
endif

ifeq ($(wait_for_gdb), 1)
qemu_flags := -s -S
qemu_flags += -S
endif

linker_script := cfg/linker.ld
out_dir := $(build_containing_dir)/$(build_type)
grub_cfg := cfg/grub.cfg
out_dir = $(build_containing_dir)/$(build_type)
asm_dir := kernel/src/asm
rust_crate_dir := kernel
rust_kernel := $(out_dir)/libflower_kernel.a
target := x86_64-unknown-flower-none
asm_source_files := $(wildcard $(asm_dir)/*.asm)
asm_obj_files = $(patsubst $(asm_dir)/%.asm, $(out_dir)/%.o, $(asm_source_files))
grub_iso := $(out_dir)/flower.iso

kernel_crate_dir := kernel/
kernel_lib := $(out_dir)/libflower_kernel.a
kernel := $(out_dir)/kernel.elf

kernel = $(out_dir)/kernel.elf
grub_iso = $(out_dir)/flower.iso
init_crate_dir := init/
init_lib := $(out_dir)/libinit.a
init := $(out_dir)/init.elf

default: build

.PHONY: clean run build $(rust_kernel) iso test
$(grub_iso): $(kernel) $(grub_cfg)
@cp $(grub_cfg) $(out_dir)/isofiles/boot/grub/
@cp $(kernel) $(out_dir)/isofiles/boot/
@grub-mkrescue -o $(out_dir)/flower.iso $(out_dir)/isofiles

test:
cd $(rust_crate_dir) && \
@cd $(rust_crate_dir) && \
cargo test

build: $(kernel)
iso: $(grub_iso)

# Run with qemu
run: $(grub_iso)
@qemu-system-x86_64 -cdrom $(grub_iso) $(qemu_flags) -m 128M
qemu-system-x86_64 -cdrom $(grub_iso) $(qemu_flags)

$(grub_iso): rm_old $(kernel) $(init) $(grub_cfg)
@cp $(grub_cfg) $(out_dir)/isofiles/boot/grub/
@cp $(kernel) $(out_dir)/isofiles/boot/
@cp $(init) $(out_dir)/isofiles/boot/
grub-mkrescue -o $(out_dir)/flower.iso $(out_dir)/isofiles

$(kernel): $(kernel_crate_dir)/**/* makedirs
@$(MAKE) nasm_flags="$(nasm_flags)" build_type="$(build_type)" cargo_flags="$(kernel_cargo_flags)" -s \
-C $(kernel_crate_dir)

$(init): $(init_crate_dir)/**/* makedirs
@$(MAKE) nasm_flags="$(nasm_flags)" build_type="$(build_type)" cargo_flags="$(init_cargo_flags)" \
-s -C $(init_crate_dir)

# Clean build dir
clean:
Expand All @@ -71,17 +79,6 @@ makedirs:
@mkdir -p $(out_dir)/isofiles
@mkdir -p $(out_dir)/isofiles/boot/grub

# Compile rust
$(rust_kernel): $(rust_crate_dir)/**/*
@cd $(rust_crate_dir) && \
RUST_TARGET_PATH=$(shell pwd)/$(rust_crate_dir) RUSTFLAGS=$(rustflags) cargo xbuild --target $(target) $(cargo_flags)
@rm -f $(rust_kernel)
@mv $(rust_crate_dir)/target/$(target)/$(build_type)/libflower_kernel.a $(rust_kernel)

# Compile kernel.elf
$(kernel): $(asm_obj_files) $(linker_script) $(rust_kernel)
@ld -n -T $(linker_script) -o $(kernel) $(asm_obj_files) $(rust_kernel) --gc-sections

# Compile asm files
$(out_dir)/%.o: $(asm_dir)/%.asm makedirs
@nasm $(nasm_flags) $< -o $@
rm_old:
@rm -f $(kernel_lib)
@rm -f $(init_lib)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ Much thanks to:
- Omarrx024,
- [Mintsuki](https://github.com/mintsuki),
- [Korona](https://github.com/avdgrinten),
- [Qookie](https://gitlab.com/qookei),
- and [Safsom](https://github.com/asfsom);
- the people part of the [rust-osdev](https://github.com/rust-osdev), such as
- the people part of the [rust-osdev](https://github.com/rust-osdev) organisation, such as
- [Isaac Woods](https://github.com/IsaacWoods);
- the [OsDev wiki](http://wiki.osdev.org);
- [Bare Metal Rust](http://www.randomhacks.net/bare-metal-rust/);
Expand Down
15 changes: 8 additions & 7 deletions cfg/grub.cfg
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
set timeout=0
set default=0

menuentry "FlowerOS" {
multiboot2 /boot/kernel.elf
boot
}
set timeout=0
set default=0

menuentry "FlowerOS" {
multiboot2 /boot/kernel.elf
module2 --nounzip /boot/init.elf
boot
}
17 changes: 17 additions & 0 deletions init/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "init"
version = "0.1.0"
authors = ["Restioson <[email protected]>"]
edition = "2018"

[lib]
crate-type = ["staticlib"]

[profile.release]
panic = "abort"

[profile.dev]
panic = "abort"
opt-level = 3

[dependencies]
25 changes: 25 additions & 0 deletions init/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
build_containing_dir := ../build

target := x86_64-unknown-flower-none
out_dir := $(build_containing_dir)/$(build_type)
asm_dir := src/asm
asm_source_files := $(wildcard $(asm_dir)/*.asm)
asm_obj_files := $(patsubst $(asm_dir)/%.asm, $(out_dir)/%.o, $(asm_source_files))
linker_script := linker.ld
init_lib := $(out_dir)/libinit.a
init := $(out_dir)/init.elf

default: $(init)

# Compile rust
$(init_lib):
@RUST_TARGET_PATH=$(shell pwd) cargo xbuild --target $(target) $(cargo_flags)
@mv target/$(target)/$(build_type)/libinit.a $(init_lib)

# Link elf
$(init): $(asm_obj_files) $(linker_script) $(init_lib)
@ld -n -T $(linker_script) -o $(init) $(asm_obj_files) $(init_lib) --gc-sections

# Compile asm files
$(out_dir)/%.o: $(asm_dir)/%.asm
@nasm $(nasm_flags) $< -o $@
40 changes: 40 additions & 0 deletions init/linker.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
ENTRY(start)

SECTIONS {

. = 1M;

/* Rustc emits lots of *really small* sections, so let's concatenate them */

.text : {
KEEP(*(.text.start))
*(.text .text.*)
. = ALIGN(4K);
}

.rodata : {
*(.rodata .rodata.*)
. = ALIGN(4K);
}

.data.rel.ro : {
*(.data.rel.ro.local*) *(.data.rel.ro .data.rel.ro.*)
. = ALIGN(4K);
}

.data : {
*(.data .data.*)
. = ALIGN(4K);
}

.got : {
*(.got .got.*)
. = ALIGN(4K);
}

.bss : {
*(COMMON)
*(.bss .bss.*)
. = ALIGN(4K);
}
}
8 changes: 8 additions & 0 deletions init/src/asm/start.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern main
global start

section .text.start
bits 64

start:
call main
15 changes: 15 additions & 0 deletions init/src/lang.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use core::panic::PanicInfo;

// A note on the `#[no_mangle]`s:
// Apparently, removing them makes it link-error with undefined symbols, so we include them

#[lang = "eh_personality"]
#[no_mangle]
extern fn eh_personality() {}

// TODO
#[panic_handler]
#[no_mangle]
extern fn panic_fmt(_info: &PanicInfo) -> ! {
loop {}
}
10 changes: 10 additions & 0 deletions init/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![no_std]

#![feature(lang_items)]

mod lang;

#[no_mangle]
pub extern "C" fn main() {
loop {}
}
30 changes: 15 additions & 15 deletions kernel/x86_64-unknown-flower-none.json → init/x86_64-unknown-flower-none.json
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"arch": "x86_64",
"cpu": "x86-64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-none-none",
"linker-flavor": "gcc",
"no-compiler-rt": true,
"os": "flower",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-sse4a,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
"disable-redzone": true,
"eliminate-frame-pointer": false
}
{
"arch": "x86_64",
"cpu": "x86-64",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-none-none",
"linker-flavor": "gcc",
"no-compiler-rt": true,
"os": "flower",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-sse4a,-3dnow,-3dnowa,-avx,-avx2,+soft-float",
"disable-redzone": true,
"eliminate-frame-pointer": false
}
19 changes: 15 additions & 4 deletions kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@ opt-level = 3
rlibc = "^1.0.0"
volatile = "^0.2.3"
spin = "^0.4.6"
x86_64 = "0.2"
bitflags = "^1.0.1"
bit_field = "^0.9.0"
log = "^0.4.3"
static_assertions = "^0.2.5"
acpi = "0.2"

[dependencies.x86_64]
git = "https://github.com/Restioson/x86_64"
branch = "gdt"

[dependencies.multiboot2]
git = "https://github.com/rust-osdev/multiboot2-elf64/"
rev = "9ce9247eee220a0bc2ca480c23c7572132c5ae9c"

[dependencies.acpi]
version = "0.2"

[dependencies.arrayvec]
version = "^0.4.7"
default-features = false

[dependencies.crossbeam]
git = "https://github.com/taiki-e/crossbeam/"
default-features = false
branch = "no-std"

[dependencies.array-init]
version = "^0.0.3"
features = ["nightly"]
Expand All @@ -43,6 +49,11 @@ features = ["nightly"]
version = "^1.0.0"
features = ["spin_no_std"]

[dependencies.ccl]
version = "^1.2.2"
default-features = false
features = ["no_std"]

[features]
default = []

Expand Down
26 changes: 26 additions & 0 deletions kernel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
build_containing_dir := ../build

target := x86_64-unknown-flowerkernel-none
out_dir := $(build_containing_dir)/$(build_type)
asm_dir := src/asm
asm_source_files := $(wildcard $(asm_dir)/*.asm)
asm_obj_files := $(patsubst $(asm_dir)/%.asm, $(out_dir)/%.o, $(asm_source_files))
linker_script := linker.ld
kernel_lib := $(out_dir)/libflower_kernel.a
kernel := $(out_dir)/kernel.elf
rustflags := "-C code-model=kernel"

default: $(kernel)

# Compile rust
$(kernel_lib):
@RUST_TARGET_PATH=$(shell pwd) RUSTFLAGS=$(rustflags) cargo xbuild --target $(target) $(cargo_flags)
@mv target/$(target)/$(build_type)/libflower_kernel.a $(kernel_lib)

# Link elf
$(kernel): $(asm_obj_files) $(linker_script) $(kernel_lib)
@ld -n -T $(linker_script) -o $(kernel) $(asm_obj_files) $(kernel_lib) --gc-sections

# Compile asm files
$(out_dir)/%.o: $(asm_dir)/%.asm
@nasm $(nasm_flags) $< -o $@
Loading