QEMU中指令添加相关(1):测试程序
1、汇编.s入口汇编
#See LICENSE forlicense details.
.section
.text.init,“ax”,@progbits
.globl _start
_start:
csrr t0, mhartid # read
hardware thread id (hart
stands for hardware thread
),将线程id放到t0寄存器
bnez t0, halt # run only
on the first hardware thread (hartid == 0), halt all the other threads,判断t0值是否为0,为0则向下执行,否则跳转到halt
la sp, stack_top # setup
stack pointer //将栈顶的地址加载到sp寄存器中
j main #跳转到main标签,即执行主程序
halt:
j halt # enter
the infinite loop
2、主程序
static int a = 3;
static int b = 256;
static volatile int
uart = (int)0x10013000;
static int
custom48(long addr,long addr1)
{
int ret = 0;
asm volatile (
"jobget48 %[dest],%[src1],%[src2]"
:[dest]"=r"(ret)
:[src1]"r"(addr),[src2]"r"(addr1)
);
return ret;
}
static int
custom64(long addr,long addr1)
{
int ret = 0;
asm volatile (
"jobget64
%[dest],%[src1],%[src2]"
:[dest]"=r"(ret)
:[src1]"r"(addr),[src2]"r"(addr1)
);
return ret;
}
void main()
{
int ret1 = 0;
int ret2 = 0;
ret1 = custom48((long)&a,
(long)&b);
ret2 = custom64((long)&a,
(long)&b);
if(ret1)
{
uart[0] = 'o';
uart[0] = 'k';
uart[0] = '4';
}
if(ret2) {
uart[0] = 'o';
uart[0] = 'k';
uart[0] = '6';
}
else
{
uart[0] = 'e';
uart[0] = 'r';
uart[0] = 'r';
}
while(1);
}
3、Makefile文件
PREFIX =
/home/s29363/riscv/install-64-bm/bin/riscv64-unknown-elf-
CC = $(PREFIX)gcc
LD = $(PREFIX)ld
OBJDUMP =
$(PREFIX)objdump
QEMU =
/home/s29363/qemu/qemu-system/install-s64/bin/qemu-system-riscv64
CFLAGS = -static
-mcmodel=medany -fvisibility=hidden -ffunction-sections -fdata-sections
-nostdlib -nostartfiles -nostdinc -g -c -O0
LDFLAGS = -T
/home/s29363/qemu/qemu-system/riscv-test/default.lds --nmagic --gc-sections
QEMU_FLAGS = -nographic
-machine sifive_u -bios none -kernel
TARGET = hello
$(TARGET):start.o
hello.o
@$(LD) $(LDFLAGS) start.o hello.o -o
$(TARGET)
start.o:
@$(CC) $(CFLAGS) start.s -o start.o
hello.o:
@$(CC) $(CFLAGS) hello.c -o hello.o
.PHONY: qemu
qemu:
$(QEMU) $(QEMU_FLAGS) hello
.PHONY: dis
dis:hello.o
@$(OBJDUMP) -d hello.o
@rm -rf hello.o
.PHONY: clean
clean:
@rm -rf $(TARGET) start.o hello.o