init
This commit is contained in:
commit
2190a5c04d
18 changed files with 745 additions and 0 deletions
143
src/boot.asm
Normal file
143
src/boot.asm
Normal file
|
@ -0,0 +1,143 @@
|
|||
global start
|
||||
|
||||
extern long_mode_start
|
||||
|
||||
section .text
|
||||
bits 32
|
||||
|
||||
start:
|
||||
mov esp, stack_top ; stack
|
||||
mov edi, ebx ; move multiboot pointer to edi
|
||||
|
||||
; error suite
|
||||
call check_multiboot
|
||||
call check_cpuid
|
||||
call check_long_mode
|
||||
|
||||
; paging
|
||||
call set_up_page_tables
|
||||
call enable_paging
|
||||
|
||||
; 64-bit gdt
|
||||
lgdt [gdt64.pointer]
|
||||
jmp gdt64.code:long_mode_start ; change cs to gdt64 and long jump
|
||||
|
||||
set_up_page_tables:
|
||||
mov eax, p3_table ; map p3_table's address to the first entry in p4_table
|
||||
or eax, 0b11 ; present, writable
|
||||
mov [p4_table], eax ; p4_table's first entry points to p3_table
|
||||
|
||||
mov eax, p2_table ; sim.
|
||||
or eax, 0b11
|
||||
mov [p3_table], eax
|
||||
|
||||
mov ecx, 0 ; counter
|
||||
.map_p2_table:
|
||||
mov eax, 0x200000 ; 2MiB for a huge page
|
||||
mul ecx ; destination is ax register
|
||||
or eax, 0b10000011 ; huge ..... present, writable
|
||||
mov [p2_table + ecx * 8], eax ; map entry
|
||||
|
||||
inc ecx ; scuffed for-loop section
|
||||
cmp ecx, 512
|
||||
jne .map_p2_table
|
||||
|
||||
ret
|
||||
|
||||
enable_paging:
|
||||
mov eax, p4_table
|
||||
mov cr3, eax ; load p4_table to cr3, where it lives
|
||||
|
||||
mov eax, cr4
|
||||
or eax, 1 << 5
|
||||
mov cr4, eax ; set PAE flag in cr4
|
||||
|
||||
mov ecx, 0xC0000080
|
||||
rdmsr
|
||||
or eax, 1 << 8
|
||||
wrmsr ; set long bit in MSR
|
||||
|
||||
mov eax, cr0
|
||||
or eax, 1 << 31
|
||||
mov cr0, eax ; set paging bit in cr0
|
||||
ret
|
||||
|
||||
; # | error
|
||||
; 0 | .no_multiboot
|
||||
; 1 | .no_cpuid
|
||||
; 2 | .no_long_mode
|
||||
error: ; prints RE:R <al>. <al> is used to store error codes
|
||||
mov dword [0xb8000], 0x4f524f45
|
||||
mov dword [0xb8004], 0x4f3a4f52
|
||||
mov dword [0xb8008], 0x4f204f20
|
||||
mov byte [0xb800a], al
|
||||
hlt
|
||||
|
||||
check_multiboot:
|
||||
cmp eax, 0x36d76289 ; eax magic value
|
||||
jne .no_multiboot
|
||||
ret
|
||||
.no_multiboot:
|
||||
mov al, "0"
|
||||
jmp error
|
||||
|
||||
check_cpuid: ; check if cpuid flag is supported
|
||||
pushfd
|
||||
pop eax ; put flags into eax
|
||||
|
||||
mov ecx, eax ; copy to ecx
|
||||
xor eax, 1 << 21 ; flip the ID bit
|
||||
|
||||
push eax
|
||||
popfd ; return eax to flags
|
||||
|
||||
pushfd
|
||||
pop eax ; put flags to eax again
|
||||
|
||||
push ecx
|
||||
popfd ; restore flags to old version
|
||||
|
||||
cmp eax, ecx
|
||||
je .no_cpuid
|
||||
ret
|
||||
.no_cpuid:
|
||||
mov al, "1"
|
||||
jmp error
|
||||
|
||||
check_long_mode: ; checks ensure cpu is new enough to be 64 bit
|
||||
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, "2"
|
||||
jmp error
|
||||
|
||||
section .bss ; GRUB initialises this to 0
|
||||
|
||||
align 4096 ; tables must be aligned
|
||||
p4_table:
|
||||
resb 4096
|
||||
p3_table:
|
||||
resb 4096
|
||||
p2_table:
|
||||
resb 4096
|
||||
|
||||
stack_bottom:
|
||||
resb 4096 * 4 ; 16kB
|
||||
stack_top:
|
||||
|
||||
section .rodata
|
||||
gdt64:
|
||||
dq 0 ; null
|
||||
.code: equ $ - gdt64 ; code offset
|
||||
dq (1 << 43) | (1 << 44) | (1 << 47) | (1 << 53) ; code
|
||||
.pointer:
|
||||
dw $ - gdt64 - 1 ; $ is .pointer here; length of gdt
|
||||
dq gdt64 ; pointer to gdt table
|
7
src/grub.cfg
Normal file
7
src/grub.cfg
Normal file
|
@ -0,0 +1,7 @@
|
|||
set timeout=0
|
||||
set default=0
|
||||
|
||||
menuentry "rustos_boot" {
|
||||
multiboot2 /boot/kernel.bin
|
||||
boot
|
||||
}
|
26
src/lib.rs
Normal file
26
src/lib.rs
Normal file
|
@ -0,0 +1,26 @@
|
|||
#![no_std]
|
||||
|
||||
extern crate rlibc;
|
||||
|
||||
mod vga_buffer;
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
pub fn halt() -> ! {
|
||||
println!("we've halted");
|
||||
loop {
|
||||
x86_64::instructions::hlt();
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn rust_main(_multiboot_information_address: usize) {
|
||||
println!("Hello World!");
|
||||
halt()
|
||||
}
|
||||
|
||||
#[panic_handler]
|
||||
pub fn panic(info: &PanicInfo) -> ! {
|
||||
println!("Panic happened: {}", info);
|
||||
halt()
|
||||
}
|
19
src/long_mode_init.asm
Normal file
19
src/long_mode_init.asm
Normal file
|
@ -0,0 +1,19 @@
|
|||
global long_mode_start
|
||||
|
||||
section .text
|
||||
bits 64
|
||||
|
||||
long_mode_start:
|
||||
mov ax, 0
|
||||
mov ss, ax
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
extern rust_main ; make the jump to Rust!
|
||||
call rust_main
|
||||
|
||||
mov rax, 0x2f592f412f4b2f4f
|
||||
mov qword [0xb8000], rax
|
||||
hlt
|
15
src/multiboot_header.asm
Normal file
15
src/multiboot_header.asm
Normal file
|
@ -0,0 +1,15 @@
|
|||
section .multiboot_header
|
||||
header_start:
|
||||
dd 0xe85250d6 ; magic number multiboot 2
|
||||
dd 0 ; magic number protected mode i386
|
||||
dd header_end - header_start ; header length
|
||||
; checksum
|
||||
dd 0x100000000 - (0xe85250d6 + 0 + (header_end - header_start))
|
||||
|
||||
; multiboot tags
|
||||
|
||||
; end tag
|
||||
dw 0 ; type
|
||||
dw 0 ; flags
|
||||
dd 8 ; size
|
||||
header_end:
|
188
src/vga_buffer.rs
Normal file
188
src/vga_buffer.rs
Normal file
|
@ -0,0 +1,188 @@
|
|||
use core::fmt;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use volatile::Volatile;
|
||||
|
||||
// macro print ::: prints to vga_buffer
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ($crate::vga_buffer::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
// macro println ::: prints to vga_buffer with \n appended
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => ($crate::print!("\n"));
|
||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
// enum Color ::: defines color bytes for VGA buffer as u8
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Color {
|
||||
Black = 0,
|
||||
Blue = 1,
|
||||
Green = 2,
|
||||
Cyan = 3,
|
||||
Red = 4,
|
||||
Magenta = 5,
|
||||
Brown = 6,
|
||||
LightGray = 7,
|
||||
DarkGray = 8,
|
||||
LightBlue = 9,
|
||||
LightGreen = 10,
|
||||
LightCyan = 11,
|
||||
LightRed = 12,
|
||||
Pink = 13,
|
||||
Yellow = 14,
|
||||
White = 15,
|
||||
}
|
||||
|
||||
// const BUFFER_HEIGHT :::
|
||||
// const BUFFER_WIDTH :::
|
||||
const BUFFER_HEIGHT: usize = 25;
|
||||
const BUFFER_WIDTH: usize = 80;
|
||||
|
||||
// struct Buffer :::
|
||||
// chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT] :field: char array of text buffer.
|
||||
// Volatile ensures it'll never be optimized away because this buffer is a side effect
|
||||
#[repr(transparent)]
|
||||
struct Buffer {
|
||||
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||
}
|
||||
|
||||
// struct ColorCode ::: formatted for VGA buffer; first 4 foreground, last 4 background
|
||||
// (u8) :field:
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
struct ColorCode(u8);
|
||||
|
||||
impl ColorCode {
|
||||
// fn new ::: creates color code to VGA specs
|
||||
// foreground: Color :param:
|
||||
// background: Color :param:
|
||||
// : ColorCode :ret:
|
||||
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||
}
|
||||
}
|
||||
|
||||
// struct ScreenChar ::: specifies character and color of 1 character for VGA buffer
|
||||
// ascii_character: u8 :field:
|
||||
// color_code: ColorCode :field:
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
struct ScreenChar {
|
||||
ascii_character: u8,
|
||||
color_code: ColorCode,
|
||||
}
|
||||
|
||||
// struct Writer :::
|
||||
// column_position: usize :field:
|
||||
// color_code: ColorCode :field:
|
||||
// buffer: &'static mut Buffer :field: buffer to write to
|
||||
pub struct Writer {
|
||||
column_position: usize,
|
||||
color_code: ColorCode,
|
||||
buffer: &'static mut Buffer,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
// fn write_byte ::: writes byte to screen
|
||||
// &mut self :param: self whose buffer will be written to
|
||||
// byte: u8 :param: byte to write to screen. Must be writable
|
||||
pub fn write_byte(&mut self, byte: u8) {
|
||||
match byte {
|
||||
b'\n' => self.new_line(),
|
||||
byte => {
|
||||
// wraps automatically
|
||||
if self.column_position >= BUFFER_WIDTH {
|
||||
self.new_line();
|
||||
}
|
||||
|
||||
// bottom row of screen will be written to
|
||||
let row = BUFFER_HEIGHT - 1;
|
||||
let col = self.column_position;
|
||||
|
||||
let color_code = self.color_code;
|
||||
self.buffer.chars[row][col].write(ScreenChar {
|
||||
ascii_character: byte,
|
||||
color_code,
|
||||
});
|
||||
|
||||
// moves on to next byte's position preemptively
|
||||
self.column_position += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fn write_string :::
|
||||
// &mut self :param: self whose buffer will be written to
|
||||
// s: &str :param: string to write
|
||||
pub fn write_string(&mut self, s: &str) {
|
||||
for byte in s.bytes() {
|
||||
match byte {
|
||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||
// placeholder for unknown bytes
|
||||
_ => self.write_byte(0xfe),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// fn new_line ::: shifts buffer contents up, replaces cursor on the left
|
||||
// &mut self :param: self whose buffer will be written to
|
||||
fn new_line(&mut self) {
|
||||
for row in 1..BUFFER_HEIGHT {
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
let character = self.buffer.chars[row][col].read();
|
||||
self.buffer.chars[row - 1][col].write(character);
|
||||
}
|
||||
}
|
||||
self.clear_row(BUFFER_HEIGHT - 1);
|
||||
self.column_position = 0;
|
||||
}
|
||||
|
||||
// fn clear_row ::: replaces row with spaces
|
||||
// &mut self :param: self whose buffer will be written to
|
||||
// row: usize :param: row of buffer to clear
|
||||
fn clear_row(&mut self, row: usize) {
|
||||
let blank = ScreenChar {
|
||||
ascii_character: b' ',
|
||||
color_code: self.color_code,
|
||||
};
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
self.buffer.chars[row][col].write(blank);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Write for Writer {
|
||||
// fn write_str :::
|
||||
// &mut self :param: self whose buffer will be written to
|
||||
// s: &str :param: string to be written
|
||||
// : fmt::Result :ret:
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.write_string(s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
// WRITER: Mutex<Writer> :::
|
||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Magenta, Color::Black),
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||
});
|
||||
}
|
||||
|
||||
// fn _print ::: used for macro magic
|
||||
// args: fmt::Arguments :param:
|
||||
pub fn _print(args: fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
use x86_64::instructions::interrupts;
|
||||
interrupts::without_interrupts(|| {
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue