From fc3c60d970501e88ed170717c177116c5c21ab74 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Thu, 22 Feb 2018 18:54:08 +0100 Subject: [PATCH] use objdump -h build/kernel-x86.bin to show sections, linker scripts squishes relevant ones together --- kernel-rs/Cargo.toml | 1 + kernel-rs/src/arch/x86/boot.asm | 20 ++++------- kernel-rs/src/arch/x86/linker.ld | 14 ++++---- kernel-rs/src/arch/x86/start.asm | 17 ++++----- kernel-rs/src/lib.rs | 60 +++++++++++++++++++++----------- 5 files changed, 60 insertions(+), 52 deletions(-) diff --git a/kernel-rs/Cargo.toml b/kernel-rs/Cargo.toml index 3bb6580b..c128e21e 100644 --- a/kernel-rs/Cargo.toml +++ b/kernel-rs/Cargo.toml @@ -8,3 +8,4 @@ crate-type = ["staticlib"] [dependencies] rlibc = "1.0" +multiboot2 = "0.1.0" diff --git a/kernel-rs/src/arch/x86/boot.asm b/kernel-rs/src/arch/x86/boot.asm index 51629f02..4a7dfad4 100644 --- a/kernel-rs/src/arch/x86/boot.asm +++ b/kernel-rs/src/arch/x86/boot.asm @@ -4,11 +4,10 @@ extern x86_start section .text bits 32 start: - mov esp, head_stack - mov edi, ebx + push ebx call check_multiboot - lgdt [GDTR.ptr] + lgdt [GDTR.ptr] ; load the new gdt jmp GDTR.gdt_cs:x86_start error: @@ -78,18 +77,11 @@ GDTR: DW 0x0 ; Limit ( bits 0 -15 ) DW 0x0 ; Base ( bits 0 -15 ) DB 0x0 ; Base ( bits 16 -23 ) - DB 0xF2 ; [ Access Flags: 0x9A=11110110b = (present)|(Privilege Ring 3=11b)|(1)|(data => 0)|(expand up => 1)|(readable)|(0) ] - DB 0xCF ; [ Flags: C=1100b = (granularity)|(32bit)|(!64bit)|(0) ] / [ Limits: (bits 16-19): F=1111b ] + DB 0x00 ; [ Access Flags: 0x9A=11110110b = (present)|(Privilege Ring 3=11b)|(1)|(data => 0)|(expand up => 1)|(readable)|(0) ] + DB 0x00 ; [ Flags: C=1100b = (granularity)|(32bit)|(!64bit)|(0) ] / [ Limits: (bits 16-19): F=1111b ] DB 0x0 ; Base ( bits 24 -31 ) .gdt_bottom: .ptr: - DW .gdt_bottom - .gdt_top - 1 - DD .gdt_top - - -section .bss -align 4 -stack_end: - resb 4096 * 4 -head_stack: + DW .gdt_bottom - .gdt_top - 1 ; length of the structure minus 1 + DD .gdt_top ; pointer to top of gdt diff --git a/kernel-rs/src/arch/x86/linker.ld b/kernel-rs/src/arch/x86/linker.ld index c0555f73..21070800 100644 --- a/kernel-rs/src/arch/x86/linker.ld +++ b/kernel-rs/src/arch/x86/linker.ld @@ -13,11 +13,13 @@ SECTIONS { . = 1M; kernel_start = . ; /* ensure that the multiboot header is at the beginning */ - .boot : {KEEP(*(.multiboot_header))} - .text : {*(.text)} - /* .rodata BLOCK(4K) : ALIGN(4K) {*(.rodata)} */ - /* .data BLOCK(4K) : ALIGN(4K) {*(.data)} */ + .multiboot_header : { KEEP(*(.multiboot_header)) } + .text : { *(.text .text.*) } + .rodata : { *(.rodata .rodata.*) } + .data.rel.ro : { *(.data.rel.ro.local*) *(.data.rel.ro .data.rel.ro.*) } + .debug : { *(.debug_*) } + /* load stack */ - .bss : {*(.bss)} - kernel_end = . ; +.bss : {*(.bss .bss.*)} +/* kernel_end = . ; */ } diff --git a/kernel-rs/src/arch/x86/start.asm b/kernel-rs/src/arch/x86/start.asm index 0ac7dd3d..01589d74 100644 --- a/kernel-rs/src/arch/x86/start.asm +++ b/kernel-rs/src/arch/x86/start.asm @@ -4,17 +4,12 @@ extern kmain section .text bits 32 x86_start: -; we should clear register but it does not work, it's okay with 0x10 instead of 0 -mov ax, 0x10 -mov ss, ax -mov ds, ax -mov es, ax -mov fs, ax -mov gs, ax - -; PRINT OK -; mov dword [0xb8000], 0x2f4b2f4f -; hlt + mov ax, 0x10 ; 16 bytes (0x10) is where the offset for data section (gdt_ds) + mov ss, ax + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax call kmain diff --git a/kernel-rs/src/lib.rs b/kernel-rs/src/lib.rs index 1c42b201..2e42ab22 100644 --- a/kernel-rs/src/lib.rs +++ b/kernel-rs/src/lib.rs @@ -1,4 +1,4 @@ -//! project hosted at [https://github.com/jzck/kernel] +//! project hosted at (https://github.com/jzck/kernel) #![no_std] #![feature(lang_items)] @@ -7,10 +7,10 @@ #![feature(asm)] //needed by cpuio for inline asm extern crate rlibc; +extern crate multiboot2; -#[macro_use] /// 80x25 screen and simplistic terminal driver -pub mod vga; +#[macro_use] pub mod vga; /// kernel init and environment pub mod context; /// PS/2 detection and processing @@ -24,24 +24,42 @@ use context::CONTEXT; use vga::{Color, ColorCode}; #[no_mangle] -pub extern fn kmain() -> ! { - unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } - print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}", - format_args!("{: ^80}", r#" ,--, "#), - format_args!("{: ^80}", r#" ,--.'| ,----, "#), - format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), - format_args!("{: ^80}", r#",---.'| : ' ,----,' | "#), - format_args!("{: ^80}", r#"; : | | ; | : . ; "#), - format_args!("{: ^80}", r#"| | : _' | ; |.' / "#), - format_args!("{: ^80}", r#": : |.' | `----'/ ; "#), - format_args!("{: ^80}", r#"| ' ' ; : / ; / "#), - format_args!("{: ^80}", r#"\ \ .'. | ; / /-, "#), - format_args!("{: ^80}", r#" `---`: | ' / / /.`| "#), - format_args!("{: ^80}", r#" ' ; |./__; : "#), - format_args!("{: ^80}", r#" | : ;| : .' "#), - format_args!("{: ^80}", r#" ' ,/ ; | .' "#), - format_args!("{: ^80}", r#" '--' `---' "#)); - unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Black); } +pub extern fn kmain(multiboot_information_address: usize) -> ! { + // unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Cyan); } + // print!("{}{}{}{}{}{}{}{}{}{}{}{}{}{}", + // format_args!("{: ^80}", r#" ,--, "#), + // format_args!("{: ^80}", r#" ,--.'| ,----, "#), + // format_args!("{: ^80}", r#" ,--, | : .' .' \ "#), + // format_args!("{: ^80}", r#",---.'| : ' ,----,' | "#), + // format_args!("{: ^80}", r#"; : | | ; | : . ; "#), + // format_args!("{: ^80}", r#"| | : _' | ; |.' / "#), + // format_args!("{: ^80}", r#": : |.' | `----'/ ; "#), + // format_args!("{: ^80}", r#"| ' ' ; : / ; / "#), + // format_args!("{: ^80}", r#"\ \ .'. | ; / /-, "#), + // format_args!("{: ^80}", r#" `---`: | ' / / /.`| "#), + // format_args!("{: ^80}", r#" ' ; |./__; : "#), + // format_args!("{: ^80}", r#" | : ;| : .' "#), + // format_args!("{: ^80}", r#" ' ,/ ; | .' "#), + // format_args!("{: ^80}", r#" '--' `---' "#)); + // unsafe { CONTEXT.current_term().color_code = ColorCode::new(Color::White, Color::Black); } + let boot_info = unsafe{ multiboot2::load(multiboot_information_address) }; + let memory_map_tag = boot_info.memory_map_tag() + .expect("Memory map tag required"); + + println!("memory areas:"); + for area in memory_map_tag.memory_areas() { + println!(" start: 0x{:x}, length: 0x{:x}", + area.base_addr, area.length); + } + + let elf_sections_tag = boot_info.elf_sections_tag() + .expect("Elf-sections tag required"); + + println!("kernel sections:"); + for section in elf_sections_tag.sections() { + println!(" addr: 0x{:x}, size: 0x{:x}, flags: 0x{:x}", + section.addr, section.size, section.flags); + } unsafe { CONTEXT.vga1.prompt();CONTEXT.vga1.flush(); } unsafe { CONTEXT.vga2.prompt(); }