8. 执行一段程序后正常退出 (下)

1 概述

  • 到目前为止实现的主要功能:
    • 实现全部目标指令集 (RV32I, Zicsr)
    • 实现 load ELF 文件的功能
  • 这次要能成功运行计算 MD5 的程序.

2 启动代码&链接脚本

  • 用于 RRV-ISS 的 C 程序要使用这里的启动代码和链接脚本.
  • 使用的启动代码
    .section .text.init,"ax",@progbits
    .globl main                  # Declare the main function
    .globl trap_vector           # Declare the trap handler
    .globl _start
_start:
    # Initialize stack pointer (SP)
    la sp, _stack_top            # Load the stack top address into SP
    
    # Set the trap vector address
    la t0, trap_vector
    csrw mtvec, t0
    # csrw mtvec trap_vector       

    # Clear the BSS section (all variables in .bss should be zeroed)
    la t0, _bss_start            # Load start address of .bss into t0
    la t1, _bss_end              # Load end address of .bss into t1
    li t2, 0                     # Load 0 into t2 (the value to clear)

clear_bss:
    bgeu t0, t1, bss_cleared     # If t0 >= t1, jump to bss_cleared
    sw t2, 0(t0)                 # Store 0 to memory at address t0
    addi t0, t0, 4               # Increment t0 (move to the next word)
    j clear_bss                  # Repeat until the whole .bss section is cleared

bss_cleared:
    call main                    # Call the main function
    
    li a7, 93                    # Load the syscall NO into a7 (93 for exit)
    ecall

    # Infinite loop if main returns (bare-metal systems typically don't return from main)
hang:
    j hang                       # Infinite loop to prevent falling off the edge

trap_vector:
    j hang                       # Trap handler (for this example, just loop)

#     .section .stack              # Define the stack section
#     .space 1024                  # Allocate 1024 bytes for the stack
# _stack_top:
  • 使用的链接脚本
/* Linker script for a bare-metal RISC-V program */

OUTPUT_FORMAT("elf32-littleriscv", "elf32-littleriscv",
              "elf32-littleriscv")
OUTPUT_ARCH(riscv)
ENTRY(_start)

/* Define the memory layout */
MEMORY
{
    FLASH (rx)  : ORIGIN = 0x80000000, LENGTH = 512K    /* Flash memory */
    RAM   (rwx) : ORIGIN = 0x80080000, LENGTH = 512K    /* RAM for stack and data */
}

/* Sections definition */
SECTIONS
{
    /* Code section */
    .text :
    {
        /*_start = .;                    Define the program entry point */
        *(.text.init)
    } > FLASH

    .text :
    {
        *(.text)                       /* All .text sections (code) */
        *(.text.*)                     /* All other text sections */
    } > FLASH

    /* Read-only data section */
    .rodata :
    {
        *(.rodata)                     /* Read-only data section */
        *(.rodata.*)
    } > FLASH

    /* Initialized data section */
    .data : 
    {
        _data_start = .;               /* Start of data */
        *(.data)                       /* Initialized data */
        _data_end = .;                 /* End of data */
    } > RAM AT> FLASH

    /* Uninitialized data section (BSS) */
    .bss :
    {
        _bss_start = .;                /* Start of BSS */
        *(.bss)                        /* Uninitialized data */
        *(COMMON)
        _bss_end = .;                  /* End of BSS */
    } > RAM

    /* Stack section */
    .stack (NOLOAD) :
    {
        _stack_top = ORIGIN(RAM) + LENGTH(RAM);  /* Stack top address */
    } > RAM
}

/* Provide symbols to be used in the assembly code */
PROVIDE(_stack_top = ORIGIN(RAM) + LENGTH(RAM));  /* Stack pointer initialized to the top of RAM */

3 RRV-ISS 退出机制实现

拦截 target 中, 编号为 93 的 ecall, 从而退出模拟器.

  • 用于测试退出 RRV-ISS 的程序 1
int main(void)
{
    return 0;
}
  • 用于测试退出 RRV-ISS 的程序 2
int main(void)
{
    return 0x1234;
}

4 运行计算 MD5-sum 的程序

同样的 MD5 程序在 Ubuntu 24.04 上运行成功,在 RRV-ISS 上运行失败.

4.1 为 MD5 程序用的 RISC-V 指令做集成测试

  • 使用下面的代码加载一条指令到 RRV-ISS
    let add_instr = instr.to_le_bytes();
    let _ = sim.load_bin_program(&add_instr, MEMORY_BASE_ADDRESS);

    // step 3. set the reset vector
    sim.set_reset_vector(MEMORY_BASE_ADDRESS.try_into().unwrap());
  • 让 RRV-ISS 在执行时,把运行的 RISC-V 的汇编指令按顺序写入文件,每条指令的打印格式如下: 0x80000000 (0x00100117) AUIPC x2, 1048576
  • 使用 python 脚本,把重复的指令删除;把指令做了集成测试后没有发现问题.
#!/usr/bin/env python3

def process_file(input_file, output_file):
    # Set to store unique values encountered in the third column
    seen_third_columns = set()
    # List to store lines that should be written to the output file
    lines_to_keep = []

    with open(input_file, 'r') as file:
        for line in file:
            # Strip newline characters and split the line
            parts = line.strip().split()
            
            if len(parts) < 3:
                # If the line has fewer than three columns, skip it
                # lines_to_keep.append(line)
                continue

            # Get the value of the third column
            third_column = parts[2]
            # Skip if the third column is not alphabetic
            if not third_column.isalpha():
                continue

            if third_column not in seen_third_columns:
                # If the value in the third column hasn't been seen before, add it to the set and keep the line
                seen_third_columns.add(third_column)
                lines_to_keep.append(line)

    
    with open(output_file, 'w') as file:
        for line in lines_to_keep:
            file.write(line)

input_file = 'md5_bare.asm'
output_file = 'md5_bare_unique.asm'

process_file(input_file, output_file)

4.2 使用打印定位问题

4.2.1 在 RRV-ISS 中实现 uart_printf

  • 实现 UART
    • 这里只是为了能够打印消息, UART 是简易的实现
    • 打印有行缓冲
  • 实现 uart_printf

4.2.2 在 MD5 程序中添加打印

  • 问题打印
        rl=bedf9dfb, x:dfbbedf9, n:0000000c // X86 上的打印
[UART0] rl=fffffdfb, x:dfbbedf9, n:0000000c // RRV-ISS上的打印

  • 问题相关代码
static void md5Step(uint32_t *buffer, const uint32_t *input){
    ...
    for(unsigned int i = 0; i < 64; ++i){
        ...
        uint32_t rl = rotateLeft(AA + E + K[i] + input[j], S[i]);
        usr_printf("rl=%08x, x:%08x, n:%08x\n", rl, AA + E + K[i] + input[j], S[i]);
        ...
    }
    ...
}


/*
 * Rotates a 32-bit word left by n bits
 */
uint32_t rotateLeft(uint32_t x, uint32_t n){
    return (x << n) | (x >> (32 - n));
}

4.2.3 定位问题并解决

  • rotateLeft 反汇编
uint32_t rotateLeft(uint32_t x, uint32_t n){
80000130:   fe010113            addi    sp,sp,-32
80000134:   00812e23            sw  s0,28(sp)
80000138:   02010413            addi    s0,sp,32
8000013c:   fea42623            sw  a0,-20(s0)
80000140:   feb42423            sw  a1,-24(s0)
/home/zsw/workspace/ws_develop/ISS-PG/ISS-PG/RRV-ISS-WS-formal/tests/tests/data/md5/md5.c:73
    return (x << n) | (x >> (32 - n));
80000144:   fec42783            lw  a5,-20(s0)
80000148:   fe842703            lw  a4,-24(s0)
8000014c:   00e79733            sll a4,a5,a4
80000150:   fe842683            lw  a3,-24(s0)
80000154:   40d006b3            neg a3,a3
80000158:   01f6f693            andi    a3,a3,31
8000015c:   00d7d7b3            srl a5,a5,a3
80000160:   00e7e7b3            or  a5,a5,a4
/home/zsw/workspace/ws_develop/ISS-PG/ISS-PG/RRV-ISS-WS-formal/tests/tests/data/md5/md5.c:74
}
80000164:   00078513            mv  a0,a5
80000168:   01c12403            lw  s0,28(sp)
8000016c:   02010113            addi    sp,sp,32
80000170:   00008067            ret
  • srl 的执行有bug: 应该是无符号数右移;集成测试中没有发现 srl 的 bug 的原因是,集成测试时被移位的数值是正数; MD5 中的 srl 移位的数值是负数.
--- a/rv_core/src/execute/rv_i.rs
+++ b/rv_core/src/execute/rv_i.rs
@@ -529,7 +529,7 @@ pub(crate) fn execute_srl(
         raw,
         core,
         disasm,
-        |rs1, rs2| (rs1 >> rs2) as GprUnsigned,
+        |rs1, rs2| ((rs1 as GprUnsigned) >> rs2) as GprUnsigned,
         "SRL",
     )
 }

4.3 RRV-ISS 成功运行 MD5 程序

在定位问题的过程中,还做了下面的两个事情:

  • 调整 log 信息: log 级别, log 格式;
  • 使用 clap 解析命令行参数,添加了下面的几个参数:
Usage: rrv-iss [OPTIONS] --file-path <FILE_PATH>

Options:
  -f, --file-path <FILE_PATH>      Path to the ELF/bin file
  -e, --entry-point <ENTRY_POINT>  Entry point address
  -i, --instr-file <INSTR_FILE>    The path to instruction log file
  -l, --log-level <LOG_LEVEL>      The log level for RVV-ISS [default: warn] [possible values: error, warn, info, debug, trace]
  -n, --no-ansi                    If log file of RVV-ISS running with no ansi color
  -h, --help                       Print help
  -V, --version                    Print version

5 开发进度整理

在 "0. 概述" 中, 计划的 12 个步骤中的 1-7, 11 已经完成.

~~1. 准备开发环境:Rust开发环境、RISC-V GNU toolchain

  1. 设计模拟器架构
  2. 执行一条 add 指令
  3. 执行 branch 和 jump & link 指令
  4. 执行 load/store 指令
  5. 执行 ecall/ebreak/csr 指令
  6. 执行一段程序,并正常退出~~
  7. 与 Spike 的 trace 文件对比
  8. 运行 RISC-V 官方的测试
  9. 添加 timer 并处理 timer 中断
    11. 添加 uart
  10. 运行 FreeRTOS

6 源码仓库

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容