Skip to content
Merged
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
6 changes: 2 additions & 4 deletions .gdbinit
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
set output-radix 16
set disassembly-flavor intel
set osabi none
#add-symbol-file build/kernel/x86_64-unknown-none/debug/libjos.a
#set substitute-path /cygdrive/c/Users/Jakob/Documents/workspace/os-series/src/ c:\\Users\\Jakob\\Documents\\workspace\\os-series\\userland\\src\\
#set remote get-thread-information-block-address-packet off
add-symbol-file build/userspace/x86_64-unknown-none/debug/helloworld
set substitute-path /root/env/userland/ c:/Users/Jakob/Documents/workspace/os-series/userland/
add-symbol-file c:/Users/Jakob/Documents/workspace/os-series/userland/dash-0.5.13/src/dash
display/5i $pc
display/20xg $sp

Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ tools/perf/chrome_trace.json
tools/perf/cachegrind.out
tools/perf/profile.proto
tools/perf/output.pb.gz
tools/perf/__pycache__/
tools/perf/__pycache__/
userland/dash-0.5.13/
dash-0.5.13.tar.gz
30 changes: 17 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ $(x86_64_asm_object_files): build/x86_64/%.o : kernel/asm/%.asm

.PHONY: setup
setup: $(x86_64_asm_object_files)
mkdir -p build/kernel
mkdir -p build/userspace/x86_64-unknown-none/debug/
mkdir -p build/kernel && \
mkdir -p build/userspace/x86_64-unknown-none/debug/ && \
mkdir -p dist/x86_64 && \
wget https://github.com/Daivuk/PureDOOM/raw/48376ddd6bbdb70085dab91feb1c6ceef80fa9b7/doom1.wad -N
cd storage && sh generateExt2Img.sh && cd ..
wget -P userland/src/doom/ https://raw.githubusercontent.com/Daivuk/PureDOOM/48376ddd6bbdb70085dab91feb1c6ceef80fa9b7/PureDOOM.h -N
test -f userland/src/doom/doom1.wad || wget https://github.com/Daivuk/PureDOOM/raw/48376ddd6bbdb70085dab91feb1c6ceef80fa9b7/doom1.wad -O userland/src/doom/doom1.wad && \
test -f userland/src/doom/PureDOOM.h || wget -P userland/src/doom/ https://raw.githubusercontent.com/Daivuk/PureDOOM/48376ddd6bbdb70085dab91feb1c6ceef80fa9b7/PureDOOM.h -N

.PHONY: iso
iso:
gcc userland/src/doom/main.c userland/src/doom/libc.c -static -nostdlib -fno-builtin -g -o build/userspace/x86_64-unknown-none/debug/helloworld -Wl,--gc-sections
objcopy --input binary --output elf64-x86-64 --binary-architecture i386 build/userspace/x86_64-unknown-none/debug/helloworld build/userspace/x86_64-unknown-none/debug/helloworld.o && \
x86_64-elf-ld -n -o dist/x86_64/kernel.bin --unresolved-symbols=report-all -z noexecstack -T targets/x86_64/linker.ld $(x86_64_asm_object_files) build/kernel/x86_64-unknown-none/debug/libjos.a build/userspace/x86_64-unknown-none/debug/helloworld.o && \
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin && \
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso
.PHONY: userland
userland:
gcc userland/src/doom/main.c userland/src/doom/libc.c -static -nostdlib -fno-builtin -g -o build/userspace/x86_64-unknown-none/debug/doom -Wl,--gc-sections --sysroot=/root/env/userland/src/doom && \
test -f dash-0.5.13.tar.gz || wget https://git.kernel.org/pub/scm/utils/dash/dash.git/snapshot/dash-0.5.13.tar.gz -N && \
tar -xzf dash-0.5.13.tar.gz -C userland/ && \
cd userland && chmod a+x build_dash.sh && ./build_dash.sh -c && cd .. && \
cd storage && sh generateExt2Img.sh && cd ..

.PHONY: kernel
kernel:
cargo rustc --manifest-path kernel/Cargo.toml --target-dir build/kernel/ -- -C no-redzone=on -C target-feature=-sse -C link-arg=-Ttargets/x86_64/linker.ld
$(MAKE) iso

.PHONY: iso
iso:
x86_64-elf-ld -n -o dist/x86_64/kernel.bin --unresolved-symbols=report-all -z noexecstack -T targets/x86_64/linker.ld $(x86_64_asm_object_files) build/kernel/x86_64-unknown-none/debug/libjos.a && \
cp dist/x86_64/kernel.bin targets/x86_64/iso/boot/kernel.bin && \
grub-mkrescue /usr/lib/grub/i386-pc -o dist/x86_64/kernel.iso targets/x86_64/iso

.PHONY: all
all: setup kernel
all: setup userland kernel
2 changes: 1 addition & 1 deletion buildenv_rust/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN echo "deb http://security.debian.org/debian-security bullseye-security main

RUN set -x \
&& apt-get update \
&& apt-get install -y curl build-essential nasm xorriso grub-pc-bin grub-common make wget libgmp3-dev libmpfr-dev libisl-dev libmpc-dev texinfo make bzip2 patch sudo qemu-system-x86 e2fsprogs
&& apt-get install -y curl build-essential nasm xorriso grub-pc-bin grub-common make wget libgmp3-dev libmpfr-dev libisl-dev libmpc-dev texinfo make bzip2 patch sudo qemu-system-x86 e2fsprogs automake autoconf libtool

# Pull binutils and gcc source code
RUN set -x \
Expand Down
41 changes: 41 additions & 0 deletions kernel/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ impl FileHandle {
}
}

pub fn size(&self) -> u32 {
self.inode.size
}

pub fn read(&mut self, buffer: *mut u8, size: usize) -> u64 {
let _event = core::hint::black_box(crate::instrument!());

Expand Down Expand Up @@ -135,6 +139,26 @@ impl FileHandle {
}
return 0;
}

pub fn stat(&self) -> Stat {
let _event = core::hint::black_box(crate::instrument!());

Stat {
st_dev: 0,
st_ino: 0,
st_mode: self.inode.mode as u64,
st_nlink: self.inode.links_count as u64,
st_uid: self.inode.uid as u64,
st_gid: self.inode.gid as u64,
st_rdev: 0,
st_size: self.inode.size as u64,
st_blksize: FILE_SYSTEM.block_size as u64,
st_blocks: self.inode.blocks as u64,
st_atime: self.inode.atime,
st_mtime: self.inode.mtime,
st_ctime: self.inode.ctime,
}
}
}

//https://slideplayer.com/slide/16554195/96/images/48/Linux+Example:+Ext2/3+Disk+Layout.jpg
Expand Down Expand Up @@ -229,6 +253,23 @@ pub struct Inode {
_remaining: [u8; 28], // Combined remaining fields
}

#[repr(C, packed)]
pub struct Stat {
pub st_dev: u64,
pub st_ino: u64,
pub st_mode: u64,
pub st_nlink: u64,
pub st_uid: u64,
pub st_gid: u64,
pub st_rdev: u64,
pub st_size: u64,
pub st_blksize: u64,
pub st_blocks: u64,
pub st_atime: u32,
pub st_mtime: u32,
pub st_ctime: u32,
}

#[repr(C, packed)]
#[derive(Debug, Copy, Clone)]
struct DirectoryEntry {
Expand Down
28 changes: 26 additions & 2 deletions kernel/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::userland;
use crate::util::out_port_b;
use core::arch::asm;
use core::arch::global_asm;
use core::sync::atomic::AtomicUsize;

global_asm!(include_str!("interrupt.S"));

Expand Down Expand Up @@ -46,6 +47,9 @@ static mut IDT_ENTRIES: [IdtEntryStruct; 256] = [IdtEntryStruct {
reserved: 0,
}; 256];

pub static mut STDIN_BUFFER: [char; 0x1000] = ['\0'; 0x1000];
pub static STDIN_BUFFER_POS: AtomicUsize = AtomicUsize::new(0);

#[unsafe(no_mangle)]
pub extern "C" fn isr_handler(error_code: u64, int_no: u64) {
let _event = core::hint::black_box(crate::instrument!());
Expand Down Expand Up @@ -77,7 +81,15 @@ pub extern "C" fn isr_handler(error_code: u64, int_no: u64) {
panic!("Unhandled page fault: cr2={:#x}, ec={:#x}", cr2, error_code);
}
} else {
panic!("Unhandled exception");
let cr2: u64;
unsafe {
asm!("mov {}, cr2", out(reg) cr2);
}

panic!(
"Unhandled exception: int_no={}, cr2={:#x}, ec={:#x}",
int_no, cr2, error_code
);
}
}
_ => DEBUG!("ISR {}", int_no),
Expand Down Expand Up @@ -132,7 +144,19 @@ pub extern "C" fn irq_handler(int_no: u64) {

let key = keyboard::get_key_for_scancode(scancode as u8);

kprint!("{}", key);
if key != 0xfe as char {
unsafe {
STDIN_BUFFER[STDIN_BUFFER_POS.load(core::sync::atomic::Ordering::Relaxed)] =
key;
}
STDIN_BUFFER_POS.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
unsafe {
STDIN_BUFFER[STDIN_BUFFER_POS.load(core::sync::atomic::Ordering::Relaxed)] =
'\0';
}

kprint!("{}", key);
}

let lcontrol: char = 0x1d as char;

Expand Down
6 changes: 3 additions & 3 deletions kernel/src/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ static SCANCODES: [char; 69] = [
'1',
'2',
'3',
'4',
'$',
'5',
'6',
'7',
Expand All @@ -28,7 +28,7 @@ static SCANCODES: [char; 69] = [
'o',
'p',
0xfe as char,
0xfe as char,
'+' as char,
'\n', //VK_RETURN -> map to ascii line feed
0x1d as char, //VK_LCONTROL
'a',
Expand All @@ -52,7 +52,7 @@ static SCANCODES: [char; 69] = [
'b',
'n',
'm',
0xfe as char,
';',
0xfe as char,
0xfe as char,
0xfe as char,
Expand Down
27 changes: 19 additions & 8 deletions kernel/src/kprint.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::mem_config;
use crate::mem_config::KERNEL_HIGHER_HALF_BASE;
use crate::serial;
// add better formatting options, see https://os.phil-opp.com/vga-text-mode/#a-kprintln-macro
Expand Down Expand Up @@ -37,22 +36,27 @@ static mut CURRENT_COL: u64 = 0;

pub struct KPrinter {
pub color: Colors,
pub written: u64,
}

impl core::fmt::Write for KPrinter {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
crate::kprint::kprint(s, self.color);
let n = crate::kprint::kprint(s, self.color);
self.written += n;
Ok(())
}
}

#[macro_export]
macro_rules! kprint_internal {
($color:expr, $with_newline:expr, $($arg:tt)*) => {{
let mut kprinter = crate::kprint::KPrinter { color: $color };
let mut kprinter = crate::kprint::KPrinter { color: $color, written: 0 };
core::fmt::write(&mut kprinter, core::format_args!($($arg)*)).unwrap();
if $with_newline {
crate::kprint::kprint_char('\n', $color);
kprinter.written += 1;
}
kprinter.written
}};
}

Expand All @@ -62,7 +66,8 @@ macro_rules! kprintlncolor {
kprint_internal!($color, true, $($arg)*)
};
() => {
crate::kprint::kprint_char('\n', $color)
crate::kprint::kprint_char('\n', $color);
return 0;
};
}

Expand All @@ -72,7 +77,8 @@ macro_rules! kprintcolor {
crate::kprint_internal!($color, false, $($arg)*)
};
() => {
crate::kprint::kprint_char('\n')
crate::kprint::kprint_char('\n');
return 0;
};
}

Expand All @@ -82,7 +88,8 @@ macro_rules! kprintln {
crate::kprint_internal!(crate::kprint::Colors::KPrintColorBlack, true, $($arg)*)
};
() => {
crate::kprint::kprint_char('\n')
crate::kprint::kprint_char('\n');
return 0;
};
}

Expand All @@ -92,7 +99,8 @@ macro_rules! kprint {
crate::kprint_internal!(crate::kprint::Colors::KPrintColorBlack, false, $($arg)*)
};
() => {
crate::kprint::kprint_char('\n')
crate::kprint::kprint_char('\n');
return 0;
};
}

Expand Down Expand Up @@ -152,10 +160,13 @@ fn scroll_line() {
}
}

pub fn kprint(text: &str, color: Colors) {
pub fn kprint(text: &str, color: Colors) -> u64 {
let mut written = 0;
for character in text.chars() {
kprint_char(character, color);
written += 1;
}
written
}

pub fn _kprint_text_at_pos(text: &str, row: u64, column: u64, color: Colors) {
Expand Down
2 changes: 1 addition & 1 deletion kernel/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ macro_rules! DEBUG {
#[macro_export]
macro_rules! INFO {
($($arg:tt)*) => {
crate::log_with_level!(crate::kprint::Colors::KPrintColorBlack, "[DEBUG] ", $($arg)*)
crate::log_with_level!(crate::kprint::Colors::KPrintColorBlack, "[DEBUG] ", $($arg)*);
};
}

Expand Down
2 changes: 1 addition & 1 deletion kernel/src/mem_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ pub const MAX_PAGE_FRAMES: usize = 0x100000000 / PAGE_SIZE; // 4 GiB total memor
/// Virtual memory layout constants
pub const KERNEL_HIGHER_HALF_BASE: usize = 0xffff_8000_0000_0000;
pub const KERNEL_STACK_TOP_ADDRESS: usize = 0xffff_ffff_ffff_ffff;
pub const USERSPACE_STACK_TOP_ADDRESS: usize = 0x0000_7fff_ffff_ffff;
pub const USERSPACE_STACK_TOP_ADDRESS: usize = 0x0000_7fff_ffff_fff0;
Loading