support reloc type 28 and allow turntbl to exit

This commit is contained in:
husky 2025-09-08 22:30:10 -07:00
parent a9e9586509
commit e004212c9c
2 changed files with 32 additions and 1 deletions

View file

@ -76,6 +76,19 @@ fn patch_lo12_iins(mut instruction: u32, pointer: u32) -> u32 {
instruction
}
fn patch_lo12_sins(mut instruction: u32, pointer: u32) -> u32 {
// bits 0..5 are shifted seven left
// bits 5..12 are shifted 25 left
const MASK_1: u32 = 0x1F;
const TARGET_1: u32 = 0xF80;
const MASK_2: u32 = 0xFE0;
const TARGET_2: u32 = 0xFE000000;
let lo_1 = ((pointer & MASK_1) << 7) & TARGET_1;
let lo_2 = ((pointer & MASK_2) << 20) & TARGET_2;
instruction = (instruction & !(TARGET_1 | TARGET_2)) | lo_1 | lo_2;
instruction
}
fn sap_addr(target_pointer: u32, target_segment_base: u32, relocation_pointer: u32, current_segment_base: u32) -> u32 {
(target_pointer).wrapping_sub(relocation_pointer)
}
@ -102,6 +115,13 @@ pub fn apply_relocation(segment_buffer: &mut [u8], current_segment_base: u32, ta
i_ins = patch_lo12_iins(i_ins, addr);
segment_buffer[i_ins_ptr..i_ins_ptr+4].copy_from_slice(&i_ins.to_le_bytes());
}
x if x == RiscVRelocationType::Lo12S as u16 => {
let mut s_ins_ptr = relocation_header.relocation_pointer as usize;
let mut s_ins = u32::from_le_bytes(segment_buffer[s_ins_ptr..s_ins_ptr+4].try_into().unwrap());
let addr = relocation_header.target_pointer as u32 + target_segment_base;
s_ins = patch_lo12_sins(s_ins, addr);
segment_buffer[s_ins_ptr..s_ins_ptr+4].copy_from_slice(&s_ins.to_le_bytes());
}
x => {
unhandled_callback(x);
}

View file

@ -1,10 +1,11 @@
#![no_std]
#![no_main]
use core::sync::atomic::{AtomicBool, Ordering};
use crate::terminal::{print, println};
use liblbos::fs::{DirectoryReader, FileSystem};
static TEST: &[u8] = b"test";
static NO_EXIT: AtomicBool = AtomicBool::new(false);
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
@ -131,6 +132,13 @@ fn execute(cmd: &str) {
"ls" => {
lsdir();
}
"exit" => {
if NO_EXIT.load(Ordering::Relaxed) {
println("cannot exit as task 1");
} else {
liblbos::syscalls::exit();
}
}
_ => {
attempt_run_file(cmd);
}
@ -157,6 +165,9 @@ mod terminal;
#[unsafe(no_mangle)]
extern "C" fn main() {
let tid = liblbos::syscalls::current_task();
NO_EXIT.store(tid == 1, Ordering::Relaxed);
print("\n\n");
print("turntable v");
const VERSION: &str = env!("CARGO_PKG_VERSION");