dendrobates-t-azureus/src/main.rs

162 lines
4.3 KiB
Rust
Raw Normal View History

// main.rs
// main file of the kernel
#![no_std] // This is a free standing program
#![no_main] // This has no crt0
#![feature(custom_test_frameworks)]
#![test_runner(dendrobates_tinctoreus_azureus::test_runner)]
#![reexport_test_harness_main = "test_main"]
2019-11-14 14:26:37 +01:00
extern crate alloc;
2019-11-13 14:26:39 +01:00
2019-11-14 14:26:37 +01:00
use alloc::boxed::Box;
use bootloader::{entry_point, BootInfo};
2019-12-22 15:24:21 +01:00
use cache_info;
use core::panic::PanicInfo;
2019-11-14 14:26:37 +01:00
use dendrobates_tinctoreus_azureus::allocator;
2019-11-18 11:11:43 +01:00
use polling_serial::serial_print;
2019-11-14 14:26:37 +01:00
use polling_serial::serial_println;
use vga_buffer; // required for custom panic handler
2019-11-14 14:26:37 +01:00
use vga_buffer::println;
use x86_64;
2019-11-13 14:26:39 +01:00
#[cfg(not(test))]
use dendrobates_tinctoreus_azureus::hlt_loop;
2019-11-14 14:26:37 +01:00
use dendrobates_tinctoreus_azureus::memory::create_example_mapping;
2019-11-13 14:26:39 +01:00
#[cfg(not(test))]
use vga_buffer::{set_colors, Color, ForegroundColor};
// Custom panic handler, required for freestanding program
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
serial_println!("{}", info);
set_colors(ForegroundColor::LightRed, Color::Blue);
println!("{}", info);
x86_64::instructions::bochs_breakpoint();
hlt_loop();
}
entry_point!(kernel_main);
// Kernel entry point
2019-11-13 15:36:46 +01:00
fn kernel_main(boot_info: &'static BootInfo) -> ! {
// TODO: Take care of cpuid stuff and set-up all floating point exetnsions
// TODO: We may also need to enable debug registers ?
println!("Hello Blue Frog");
dendrobates_tinctoreus_azureus::init();
x86_64::instructions::interrupts::int3(); // new
#[cfg(test)]
test_main();
x86_64::instructions::interrupts::int3();
2019-11-13 15:36:46 +01:00
use dendrobates_tinctoreus_azureus::memory;
2019-11-13 17:32:22 +01:00
use x86_64::structures::paging::{MapperAllSizes, PageTable};
use x86_64::{structures::paging::Page, VirtAddr};
2019-11-13 15:36:46 +01:00
let phys_mem_offset = VirtAddr::new(boot_info.physical_memory_offset);
// new: initialize a mapper
2019-11-14 14:26:37 +01:00
let mut frame_allocator =
unsafe { memory::BootInfoFrameAllocator::init(&boot_info.memory_map) };
2019-11-13 17:32:22 +01:00
let mut mapper = unsafe { memory::init(phys_mem_offset) };
2019-11-13 15:36:46 +01:00
let addresses = [
// the identity-mapped vga buffer page
0xb8000,
// some code page
0x201008,
// some stack page
0x0100_0020_1a10,
// virtual address mapped to physical address 0
boot_info.physical_memory_offset,
];
for &address in &addresses {
let virt = VirtAddr::new(address);
// new: use the `mapper.translate_addr` method
let phys = mapper.translate_addr(virt);
serial_println!("{:?} -> {:?}", virt, phys);
}
2019-11-14 14:26:37 +01:00
allocator::init_heap(&mut mapper, &mut frame_allocator).expect("heap initialization failed");
2019-11-13 17:32:22 +01:00
2019-11-14 14:26:37 +01:00
let x = Box::new(41);
2019-11-13 17:32:22 +01:00
let caches = cache_info::get_cache_info();
serial_println!("Caches:");
serial_println!("{:?}", caches);
2019-12-22 15:24:21 +01:00
2019-11-18 11:11:43 +01:00
serial_print!("Input a character: ");
let c = { polling_serial::SERIAL1.lock().read() };
serial_println!("\nYoutyped '{:x}'", c);
serial_println!("Preparing nasty fault...");
unsafe {
2019-11-13 14:12:07 +01:00
*(0xdead_beef as *mut u64) = 42;
}
serial_println!("Survived ? oO");
// magic break ?
// x86_64::instructions::bochs_breakpoint();
panic!("Ooops Sorry");
}
#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
use dendrobates_tinctoreus_azureus::test_panic_handler;
test_panic_handler(info);
}
#[test_case]
fn float_test() {
serial_println!("Testing float computations...");
use volatile::Volatile;
// Make a few floating points test;
let vf: f32 = 84798.0;
let vd: f64 = 0.828494623655914;
let a: Volatile<f32> = Volatile::new(42.0);
let b: Volatile<f32> = Volatile::new(2019.);
let rf = a.read() * b.read();
let c: Volatile<f64> = Volatile::new(15.410);
let d: Volatile<f64> = Volatile::new(18.600);
let rd = c.read() / d.read();
serial_print!(
" {:?} * {:?} = {:?} expected {:?}...",
a.read(),
b.read(),
rf,
vf
);
2019-11-13 14:26:39 +01:00
if rf == vf {
serial_println!("[ok]");
} else {
serial_println!("[fail]");
}
serial_print!(
" {:?} / {:?} = {:?} expected {:?}...",
c.read(),
d.read(),
rd,
vd
);
2019-11-13 14:26:39 +01:00
if rd == vd {
serial_println!("[ok]");
} else {
serial_println!("[fail]");
}
assert_eq!(rf, vf);
assert_eq!(rd, vd);
serial_println!("Testing float computations... [ok]");
}