cargo fmt
This commit is contained in:
parent
042405c597
commit
9ee7a85dcc
14
src/gdt.rs
14
src/gdt.rs
@ -1,8 +1,8 @@
|
||||
use x86_64::VirtAddr;
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use lazy_static::lazy_static;
|
||||
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor};
|
||||
use x86_64::structures::gdt::SegmentSelector;
|
||||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable};
|
||||
use x86_64::structures::tss::TaskStateSegment;
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
||||
|
||||
@ -25,7 +25,13 @@ lazy_static! {
|
||||
let mut gdt = GlobalDescriptorTable::new();
|
||||
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
|
||||
let tss_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
|
||||
(gdt, Selectors { code_selector, tss_selector })
|
||||
(
|
||||
gdt,
|
||||
Selectors {
|
||||
code_selector,
|
||||
tss_selector,
|
||||
},
|
||||
)
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,23 +1,22 @@
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
||||
use crate::{println, print};
|
||||
use lazy_static::lazy_static;
|
||||
use crate::gdt;
|
||||
use crate::hlt_loop;
|
||||
use crate::{print, println};
|
||||
use lazy_static::lazy_static;
|
||||
use pic8259::ChainedPics;
|
||||
use spin;
|
||||
use crate::hlt_loop;
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
||||
|
||||
lazy_static! {
|
||||
static ref IDT: InterruptDescriptorTable = {
|
||||
let mut idt = InterruptDescriptorTable::new();
|
||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||
unsafe {
|
||||
idt.double_fault.set_handler_fn(double_fault_handler)
|
||||
idt.double_fault
|
||||
.set_handler_fn(double_fault_handler)
|
||||
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
||||
}
|
||||
idt[InterruptIndex::Timer.as_usize()]
|
||||
.set_handler_fn(timer_interrupt_handler);
|
||||
idt[InterruptIndex::Keyboard.as_usize()]
|
||||
.set_handler_fn(keyboard_interrupt_handler);
|
||||
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer_interrupt_handler);
|
||||
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard_interrupt_handler);
|
||||
idt.page_fault.set_handler_fn(page_fault_handler);
|
||||
idt
|
||||
};
|
||||
@ -31,7 +30,10 @@ extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
||||
println!("EXCEPTION: BREAKPOINT\n{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn double_fault_handler(stack_frame: InterruptStackFrame, _error_code: u64) -> ! {
|
||||
extern "x86-interrupt" fn double_fault_handler(
|
||||
stack_frame: InterruptStackFrame,
|
||||
_error_code: u64,
|
||||
) -> ! {
|
||||
panic!("EXCEPTION: DOUBLE FAULT\n{:#?}", stack_frame);
|
||||
}
|
||||
|
||||
@ -70,8 +72,9 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
|
||||
use x86_64::instructions::port::Port;
|
||||
|
||||
lazy_static! {
|
||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
||||
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore));
|
||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> = Mutex::new(
|
||||
Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore)
|
||||
);
|
||||
}
|
||||
|
||||
let mut keyboard = KEYBOARD.lock();
|
||||
@ -92,7 +95,10 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac
|
||||
}
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn page_fault_handler(stack_frame: InterruptStackFrame, error_code: PageFaultErrorCode) {
|
||||
extern "x86-interrupt" fn page_fault_handler(
|
||||
stack_frame: InterruptStackFrame,
|
||||
error_code: PageFaultErrorCode,
|
||||
) {
|
||||
use x86_64::registers::control::Cr2;
|
||||
println!("EXCEPTION: PAGE FAULT");
|
||||
println!("Accessed Address: {:?}", Cr2::read());
|
||||
|
@ -6,11 +6,11 @@
|
||||
#![feature(abi_x86_interrupt)]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
pub mod gdt;
|
||||
pub mod interrupts;
|
||||
pub mod memory;
|
||||
pub mod serial;
|
||||
pub mod vga_buffer;
|
||||
pub mod interrupts;
|
||||
pub mod gdt;
|
||||
pub mod memory;
|
||||
|
||||
pub trait Testable {
|
||||
fn run(&self) -> ();
|
||||
|
10
src/main.rs
10
src/main.rs
@ -4,15 +4,15 @@
|
||||
#![test_runner(anos::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use anos::println;
|
||||
use bootloader::{BootInfo, entry_point};
|
||||
use bootloader::{entry_point, BootInfo};
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
entry_point!(kernel_main);
|
||||
|
||||
fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
use anos::memory::{ self, BootInfoFrameAllocator };
|
||||
use x86_64::{ structures::paging::Page, VirtAddr };
|
||||
use anos::memory::{self, BootInfoFrameAllocator};
|
||||
use x86_64::{structures::paging::Page, VirtAddr};
|
||||
|
||||
println!("Hello World{}", "!");
|
||||
anos::init();
|
||||
@ -25,7 +25,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
|
||||
memory::create_example_mapping(page, &mut mapper, &mut frame_allocator);
|
||||
|
||||
let page_ptr: *mut u64 = page.start_address().as_mut_ptr();
|
||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e)};
|
||||
unsafe { page_ptr.offset(400).write_volatile(0x_f021_f077_f065_f04e) };
|
||||
|
||||
// 触发一个中断
|
||||
x86_64::instructions::interrupts::int3();
|
||||
|
@ -33,9 +33,7 @@ pub fn create_example_mapping(
|
||||
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
||||
let flags = Flags::PRESENT | Flags::WRITABLE;
|
||||
|
||||
let map_to_result = unsafe {
|
||||
mapper.map_to(page, frame, flags, frame_allocator)
|
||||
};
|
||||
let map_to_result = unsafe { mapper.map_to(page, frame, flags, frame_allocator) };
|
||||
map_to_result.expect("map_to failed").flush();
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
use uart_16550::SerialPort;
|
||||
use spin::Mutex;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use uart_16550::SerialPort;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref SERIAL1: Mutex<SerialPort> = {
|
||||
|
@ -3,10 +3,10 @@
|
||||
// 这实际上是通过内存操作的副作用来实现的
|
||||
// 但由于我们从未访问过这段内存,所以它可能会在未来的 Rust 编译器中被优化掉
|
||||
// 为了告诉编译器,这部分针对内存的操作是我们故意的,就需要用到 volatile crate
|
||||
use volatile::Volatile;
|
||||
use core::fmt;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use volatile::Volatile;
|
||||
|
||||
#[allow(dead_code)] // 允许未使用的代码
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)] // 启用 copy 语义
|
||||
@ -71,7 +71,7 @@ impl Writer {
|
||||
if self.column_position >= BUFFER_WIDTH {
|
||||
self.new_line();
|
||||
}
|
||||
let row = BUFFER_HEIGHT -1;
|
||||
let row = BUFFER_HEIGHT - 1;
|
||||
let col = self.column_position;
|
||||
let color_code = self.color_code;
|
||||
// 将 ScreenChar 写入到 Buffer 的相应位置
|
||||
@ -88,11 +88,11 @@ impl Writer {
|
||||
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.buffer.chars[row - 1][col].write(character);
|
||||
}
|
||||
}
|
||||
// 清空最下面的一行
|
||||
self.clear_row(BUFFER_HEIGHT -1);
|
||||
self.clear_row(BUFFER_HEIGHT - 1);
|
||||
// 将当前列的位置设为 0
|
||||
self.column_position = 0;
|
||||
}
|
||||
@ -161,19 +161,22 @@ pub fn _print(args: fmt::Arguments) {
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_simple() { // 测试单行 println!
|
||||
fn test_println_simple() {
|
||||
// 测试单行 println!
|
||||
println!("test_println_simple output");
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_many() { // 测试多行 println!
|
||||
fn test_println_many() {
|
||||
// 测试多行 println!
|
||||
for _ in 0..10 {
|
||||
println!("test_println_many output");
|
||||
}
|
||||
}
|
||||
|
||||
#[test_case]
|
||||
fn test_println_output() { // 测试字符是否真的打印到了屏幕上
|
||||
fn test_println_output() {
|
||||
// 测试字符是否真的打印到了屏幕上
|
||||
use core::fmt::Write;
|
||||
use x86_64::instructions::interrupts;
|
||||
|
||||
|
@ -4,8 +4,8 @@
|
||||
#![test_runner(anos::test_runner)]
|
||||
#![reexport_test_harness_main = "test_main"]
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
use anos::println;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
|
@ -1,8 +1,8 @@
|
||||
#![no_std]
|
||||
#![no_main]
|
||||
|
||||
use anos::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||
use core::panic::PanicInfo;
|
||||
use anos::{QemuExitCode, exit_qemu, serial_println, serial_print};
|
||||
|
||||
#[panic_handler]
|
||||
fn panic(_info: &PanicInfo) -> ! {
|
||||
|
@ -2,8 +2,8 @@
|
||||
#![no_main]
|
||||
#![feature(abi_x86_interrupt)]
|
||||
|
||||
use anos::{exit_qemu, serial_print, serial_println, QemuExitCode};
|
||||
use core::panic::PanicInfo;
|
||||
use anos::{exit_qemu, QemuExitCode, serial_println, serial_print};
|
||||
use lazy_static::lazy_static;
|
||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
||||
|
||||
@ -43,7 +43,10 @@ pub fn init_test_idt() {
|
||||
TEST_IDT.load();
|
||||
}
|
||||
|
||||
extern "x86-interrupt" fn test_double_fault_handler(_stack_frame: InterruptStackFrame, _error_code: u64) -> ! {
|
||||
extern "x86-interrupt" fn test_double_fault_handler(
|
||||
_stack_frame: InterruptStackFrame,
|
||||
_error_code: u64,
|
||||
) -> ! {
|
||||
serial_println!("[ok]");
|
||||
exit_qemu(QemuExitCode::Success);
|
||||
loop {}
|
||||
|
Loading…
Reference in New Issue
Block a user