diff --git a/Cargo.lock b/Cargo.lock index 64664435a6aedc0a2e12c798db2bed4e7eedc05b..63eea287741ec1c571615cd7fb2d4c1ae37f6bce 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "reverse" +name = "mips-rt" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 2fed6485efaae314456567a7ece90aeec1a76d2e..4d91a45aeb12971b8ec677b8b3eda95413782148 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "reverse" +name = "mips-rt" version = "0.1.0" authors = ["pln <Per Lindgren>"] edition = "2018" @@ -7,6 +7,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +# r0 = "0.2.2" [profile.release] @@ -15,4 +16,4 @@ opt-level = 3 # Aggressive optimization debug = true # keep debug symbols lto = true # Enable Link Time Optimization codegen-units = 1 # Reduce number of codegen units to increase optimizations. -panic = 'abort' # Abort on panic \ No newline at end of file +panic = 'abort' # Abort on panic diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..670cdc3de741dcd72b16357d2aa0ff200c198d0d --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# MIPS-3k Proof Of Concept + +A minimal crate for experimenting with bare metal Rust for the MIPS-3k. + +## Compilation + +- cargo install xargo +- rustup \ No newline at end of file diff --git a/examples/reverse.rs b/examples/reverse.rs new file mode 100644 index 0000000000000000000000000000000000000000..f4bccf065d5be7b1375b7c49da1cec150197844b --- /dev/null +++ b/examples/reverse.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +// #![feature(const_generics)] +// fn rev<const X: usize>(from: &[u8; X], to: &mut [u8; X]) { +// if from.len() > 0 {} +// } + +extern crate mips_rt; + +fn rev(from: &[u8], to: &mut [u8]) { + let len = from.len(); + if len > 0 { + rev(&from[1..], &mut to[..len - 1]); + to[len - 1] = from[0]; + } +} + +const FROM: &[u8] = b"Hello Word!"; +const SIZE: usize = FROM.len(); + +#[no_mangle] +fn main() { + let mut to: [u8; SIZE] = [0; SIZE]; + rev(&FROM[..], &mut to); + unsafe { + core::ptr::read_volatile(&to); + } +} diff --git a/objdump_mips.s b/objdump_mips.s index bbb5cc2aa5e0c111ec1b3efc0c09862d52a4d891..2aaa2908ec0a591a77aa134a6b0ca1bea44ae594 100644 --- a/objdump_mips.s +++ b/objdump_mips.s @@ -1,5 +1,5 @@ -target/mipsel-unknown-none/release/reverse: file format ELF32-mips +target/mipsel-unknown-none/release/examples/reverse: file format ELF32-mips Disassembly of section .text: @@ -7,65 +7,72 @@ Disassembly of section .text: 00000000 _start: ; pub extern "C" fn _start() -> ! { 0: f0 ff bd 27 addiu $sp, $sp, -16 - 4: 0c 00 bf af sw $ra, 12($sp) -; rev(&FROM[..], &mut to); - 8: 00 00 01 3c lui $1, 0 - c: 00 00 a6 27 addiu $6, $sp, 0 - 10: 0b 00 05 24 addiu $5, $zero, 11 - 14: 0b 00 07 24 addiu $7, $zero, 11 -; let mut to: [u8; SIZE] = [0; SIZE]; - 18: 0a 00 a0 ab swl $zero, 10($sp) - 1c: 07 00 a0 bb swr $zero, 7($sp) - 20: 04 00 a0 af sw $zero, 4($sp) - 24: 00 00 a0 af sw $zero, 0($sp) -; rev(&FROM[..], &mut to); - 28: 19 00 00 0c jal 100 <reverse.a07il72d-cgu.0+0x64> - 2c: f8 00 24 24 addiu $4, $1, 248 -; intrinsics::volatile_load(src) - 30: 0a 00 a1 93 lbu $1, 10($sp) - 34: 09 00 a1 93 lbu $1, 9($sp) - 38: 08 00 a1 93 lbu $1, 8($sp) - 3c: 07 00 a1 93 lbu $1, 7($sp) - 40: 06 00 a1 93 lbu $1, 6($sp) - 44: 05 00 a1 93 lbu $1, 5($sp) - 48: 04 00 a1 93 lbu $1, 4($sp) - 4c: 03 00 a1 93 lbu $1, 3($sp) - 50: 02 00 a1 93 lbu $1, 2($sp) - 54: 01 00 a1 93 lbu $1, 1($sp) - 58: 00 00 a1 93 lbu $1, 0($sp) -; loop {} - 5c: 17 00 00 08 j 92 <reverse.a07il72d-cgu.0+0x5c> - 60: 00 00 00 00 nop +; #[lang = "eh_personality"] + 4: 1c 00 00 0c jal 112 <reverse.9oy966i4-cgu.0+0x70> + 8: 00 00 00 00 nop + c: 0d 00 00 00 break -00000064 reverse::rev::h99451ae97e77db4d: -; } - 64: 15 00 a0 10 beqz $5, 88 <reverse.a07il72d-cgu.0+0xbc> +00000010 reverse::rev::h045b7f4ff3ea22c3: ; if len > 0 { - 68: 00 00 00 00 nop - 6c: f0 ff bd 27 addiu $sp, $sp, -16 - 70: 0c 00 bf af sw $ra, 12($sp) - 74: 08 00 b2 af sw $18, 8($sp) - 78: 04 00 b1 af sw $17, 4($sp) - 7c: 00 00 b0 af sw $16, 0($sp) + 10: 15 00 a0 10 beqz $5, 88 <reverse.9oy966i4-cgu.0+0x68> + 14: 00 00 00 00 nop + 18: f0 ff bd 27 addiu $sp, $sp, -16 + 1c: 0c 00 bf af sw $ra, 12($sp) + 20: 08 00 b2 af sw $18, 8($sp) + 24: 04 00 b1 af sw $17, 4($sp) + 28: 00 00 b0 af sw $16, 0($sp) ; from_raw_parts(slice.as_ptr().add(self.start), self.end - self.start) - 80: ff ff b2 24 addiu $18, $5, -1 - 84: 25 88 80 00 move $17, $4 + 2c: ff ff b2 24 addiu $18, $5, -1 + 30: 25 88 80 00 move $17, $4 ; intrinsics::offset(self, count) - 88: 01 00 84 24 addiu $4, $4, 1 - 8c: 25 80 c0 00 move $16, $6 + 34: 01 00 84 24 addiu $4, $4, 1 + 38: 25 80 c0 00 move $16, $6 ; rev(&from[1..], &mut to[..len - 1]); - 90: 25 28 40 02 move $5, $18 - 94: 19 00 00 0c jal 100 <reverse.a07il72d-cgu.0+0x64> - 98: 25 38 40 02 move $7, $18 + 3c: 25 28 40 02 move $5, $18 + 40: 04 00 00 0c jal 16 <reverse.9oy966i4-cgu.0+0x10> + 44: 25 38 40 02 move $7, $18 ; to[len - 1] = from[0]; - 9c: 21 08 12 02 addu $1, $16, $18 - a0: 00 00 22 92 lbu $2, 0($17) - a4: 00 00 b0 8f lw $16, 0($sp) - a8: 04 00 b1 8f lw $17, 4($sp) - ac: 08 00 b2 8f lw $18, 8($sp) - b0: 0c 00 bf 8f lw $ra, 12($sp) - b4: 00 00 22 a0 sb $2, 0($1) - b8: 10 00 bd 27 addiu $sp, $sp, 16 + 48: 21 08 12 02 addu $1, $16, $18 + 4c: 00 00 22 92 lbu $2, 0($17) + 50: 00 00 b0 8f lw $16, 0($sp) + 54: 04 00 b1 8f lw $17, 4($sp) + 58: 08 00 b2 8f lw $18, 8($sp) + 5c: 0c 00 bf 8f lw $ra, 12($sp) + 60: 00 00 22 a0 sb $2, 0($1) + 64: 10 00 bd 27 addiu $sp, $sp, 16 ; } - bc: 08 00 e0 03 jr $ra - c0: 00 00 00 00 nop + 68: 08 00 e0 03 jr $ra + 6c: 00 00 00 00 nop + +00000070 main: +; fn main() -> ! { + 70: f0 ff bd 27 addiu $sp, $sp, -16 + 74: 0c 00 bf af sw $ra, 12($sp) +; rev(&FROM[..], &mut to); + 78: 00 00 01 3c lui $1, 0 + 7c: 00 00 a6 27 addiu $6, $sp, 0 + 80: 0b 00 05 24 addiu $5, $zero, 11 + 84: 0b 00 07 24 addiu $7, $zero, 11 +; let mut to: [u8; SIZE] = [0; SIZE]; + 88: 0a 00 a0 ab swl $zero, 10($sp) + 8c: 07 00 a0 bb swr $zero, 7($sp) + 90: 04 00 a0 af sw $zero, 4($sp) + 94: 00 00 a0 af sw $zero, 0($sp) +; rev(&FROM[..], &mut to); + 98: 04 00 00 0c jal 16 <reverse.9oy966i4-cgu.0+0x10> + 9c: 08 01 24 24 addiu $4, $1, 264 +; intrinsics::volatile_load(src) + a0: 0a 00 a1 93 lbu $1, 10($sp) + a4: 09 00 a1 93 lbu $1, 9($sp) + a8: 08 00 a1 93 lbu $1, 8($sp) + ac: 07 00 a1 93 lbu $1, 7($sp) + b0: 06 00 a1 93 lbu $1, 6($sp) + b4: 05 00 a1 93 lbu $1, 5($sp) + b8: 04 00 a1 93 lbu $1, 4($sp) + bc: 03 00 a1 93 lbu $1, 3($sp) + c0: 02 00 a1 93 lbu $1, 2($sp) + c4: 01 00 a1 93 lbu $1, 1($sp) + c8: 00 00 a1 93 lbu $1, 0($sp) +; loop {} + cc: 33 00 00 08 j 204 <reverse.9oy966i4-cgu.0+0xcc> + d0: 00 00 00 00 nop diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..7b9304f34965ddda715166907a083a427db14143 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,39 @@ +#![feature(lang_items)] +#![no_std] +#![no_main] + +#[panic_handler] +fn my_panic(_info: &core::panic::PanicInfo) -> ! { + loop {} +} + +// The reset vector, a pointer into the reset handler +#[link_section = ".init"] +#[no_mangle] +pub extern "C" fn _start() -> ! { + extern "C" { + // These symbols come from `link.x` + static mut __sbss: u32; + static mut __ebss: u32; + + static mut __sdata: u32; + static mut __edata: u32; + static __sidata: u32; + } + + extern "Rust" { + // This symbol will be provided by the user via `#[entry]` + fn main() -> !; + + } + + // Initialize RAM + // r0::zero_bss(&mut __sbss, &mut __ebss); + // r0::init_data(&mut __sdata, &mut __edata, &__sidata); + + unsafe { main() } + loop {} +} + +#[lang = "eh_personality"] +extern "C" fn eh_personality() {} diff --git a/src/main.rs b/src/main.rs deleted file mode 100644 index 5841a7fef6a942d7c86bfb1ed45b800a4df7c000..0000000000000000000000000000000000000000 --- a/src/main.rs +++ /dev/null @@ -1,53 +0,0 @@ -// #![feature(const_generics)] -// fn rev<const X: usize>(from: &[u8; X], to: &mut [u8; X]) { -// if from.len() > 0 {} -// } -#![feature(lang_items)] -#![no_std] -#![no_main] - -// use panic_abort; -// use libc; - -fn rev(from: &[u8], to: &mut [u8]) { - let len = from.len(); - if len > 0 { - rev(&from[1..], &mut to[..len - 1]); - to[len - 1] = from[0]; - } -} - -const FROM: &[u8] = b"Hello Word!"; -const SIZE: usize = FROM.len(); - -#[no_mangle] -pub extern "C" fn main() { - let mut to: [u8; SIZE] = [0; SIZE]; - rev(&FROM[..], &mut to); - unsafe { - core::ptr::read_volatile(&to); - } - - // println!("from : {:?}", std::str::from_utf8(FROM).unwrap()); - // println!("to : {:?}", std::str::from_utf8(&to).unwrap()); -} - -#[panic_handler] -fn my_panic(_info: &core::panic::PanicInfo) -> ! { - loop {} -} - -// The reset vector, a pointer into the reset handler -#[link_section = ".init"] -#[no_mangle] -pub extern "C" fn _start() -> ! { - main(); - loop {} -} - -#[lang = "eh_personality"] -extern "C" fn eh_personality() {} -// #[lang = "panic_fmt"] -// extern "C" fn panic_fmt() -> ! { -// loop {} -// }