Update linker & Hello k210/qemu(release mode).

This commit is contained in:
Yifan Wu 2020-11-12 17:13:57 +08:00
parent dc4f1e3dbf
commit 6a96d7d122
4 changed files with 100 additions and 9 deletions

33
os/src/console.rs Normal file
View file

@ -0,0 +1,33 @@
use core::fmt::{self, Write};
use crate::sbi::console_putchar;
struct Stdout;
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.chars() {
console_putchar(c as usize);
}
Ok(())
}
}
pub fn print(args: fmt::Arguments) {
Stdout.write_fmt(args).unwrap();
}
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!($fmt $(, $($arg)+)?));
}
}
#[macro_export]
macro_rules! println {
($fmt: literal $(, $($arg: tt)+)?) => {
$crate::console::print(format_args!(concat!($fmt, "\n") $(, $($arg)+)?));
}
}

View file

@ -10,36 +10,33 @@ SECTIONS
stext = .; stext = .;
.text : { .text : {
*(.text.entry) *(.text.entry)
*(.text) *(.text .text.*)
} }
. = ALIGN(4K); . = ALIGN(4K);
etext = .; etext = .;
srodata = .; srodata = .;
.rodata : { .rodata : {
*(.rodata) *(.rodata .rodata.*)
} }
. = ALIGN(4K); . = ALIGN(4K);
erodata = .; erodata = .;
sdata = .; sdata = .;
.data : { .data : {
*(.data) *(.data .data.*)
} }
. = ALIGN(4K); . = ALIGN(4K);
edata = .; edata = .;
sbss = .;
.bss : { .bss : {
*(.bss) *(.bss.stack)
sbss = .;
*(.bss .bss.*)
} }
. = ALIGN(4K); . = ALIGN(4K);
ebss = .; ebss = .;
.stack : {
*(.bss.stack)
}
ekernel = .; ekernel = .;
/DISCARD/ : { /DISCARD/ : {

View file

@ -1,12 +1,34 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(global_asm)] #![feature(global_asm)]
#![feature(llvm_asm)]
mod lang_items; mod lang_items;
mod sbi;
#[macro_use]
mod console;
global_asm!(include_str!("entry.asm")); global_asm!(include_str!("entry.asm"));
#[no_mangle] #[no_mangle]
pub fn rust_main() -> ! { pub fn rust_main() -> ! {
println!("Hello, world!");
extern "C" {
fn stext();
fn etext();
fn srodata();
fn erodata();
fn sdata();
fn edata();
fn sbss();
fn ebss();
fn boot_stack();
fn boot_stack_top();
};
println!(".text [{:#x}, {:#x})", stext as usize, etext as usize);
println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize);
println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize);
println!("boot_stack [{:#x}, {:#x})", boot_stack as usize, boot_stack_top as usize);
println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize);
loop {} loop {}
} }

39
os/src/sbi.rs Normal file
View file

@ -0,0 +1,39 @@
#![allow(unused)]
const SBI_SET_TIMER: usize = 0;
const SBI_CONSOLE_PUTCHAR: usize = 1;
const SBI_CONSOLE_GETCHAR: usize = 2;
const SBI_CLEAR_IPI: usize = 3;
const SBI_SEND_IPI: usize = 4;
const SBI_REMOTE_FENCE_I: usize = 5;
const SBI_REMOTE_SFENCE_VMA: usize = 6;
const SBI_REMOTE_SFENCE_VMA_ASID: usize = 7;
const SBI_SHUTDOWN: usize = 8;
#[inline(always)]
fn sbi_call(which: usize, arg0: usize, arg1: usize, arg2: usize) -> usize {
let mut ret;
unsafe {
llvm_asm!("ecall"
: "={x10}" (ret)
: "{x10}" (arg0), "{x11}" (arg1), "{x12}" (arg2), "{x17}" (which)
: "memory"
: "volatile"
);
}
ret
}
pub fn console_putchar(c: usize) {
sbi_call(SBI_CONSOLE_PUTCHAR, c, 0, 0);
}
pub fn console_getchar() -> usize {
sbi_call(SBI_CONSOLE_GETCHAR, 0, 0, 0)
}
pub fn shutdown() -> ! {
sbi_call(SBI_SHUTDOWN, 0, 0, 0);
panic!("It should shutdown!");
}