diff --git a/nemu/include/func.h b/nemu/include/ftrace.h similarity index 58% rename from nemu/include/func.h rename to nemu/include/ftrace.h index 125b5f0..f41f984 100644 --- a/nemu/include/func.h +++ b/nemu/include/ftrace.h @@ -9,8 +9,10 @@ typedef struct { char * name; } func_t; -func_t *func_table; -const char *get_func_name(vaddr_t addr); +extern func_t *func_table; +void ftrace_call(vaddr_t); +void ftrace_return(vaddr_t); +// const char *get_func_name(vaddr_t addr); #endif #endif \ No newline at end of file diff --git a/nemu/src/isa/riscv32/inst.c b/nemu/src/isa/riscv32/inst.c index b7aeac5..f2edfdc 100644 --- a/nemu/src/isa/riscv32/inst.c +++ b/nemu/src/isa/riscv32/inst.c @@ -18,6 +18,7 @@ #include #include #include +#include #define R(i) gpr(i) #define Mr vaddr_read @@ -59,6 +60,16 @@ static void do_branch(Decode *s, bool condition, word_t offset) { } } +static void ftrace(Decode *s, int rd, vaddr_t dst) { + uint32_t i = s->isa.inst.val; + int rs1 = BITS(i, 19, 15); + if(rs1 == 1 && rd == 0) { + ftrace_return(dst); + } else { + ftrace_call(dst); + } +} + static int decode_exec(Decode *s) { int rd = 0; word_t src1 = 0, src2 = 0, imm = 0; @@ -74,8 +85,8 @@ static int decode_exec(Decode *s) { INSTPAT("??????? ????? ????? ??? ????? 01101 11", lui , U, R(rd) = imm); INSTPAT("??????? ????? ????? ??? ????? 00101 11", auipc , U, R(rd) = s->pc + imm); - INSTPAT("??????? ????? ????? ??? ????? 11011 11", jal , J, do {s->dnpc = s->pc + imm; R(rd) = s->pc + 4; } while(0)); - INSTPAT("??????? ????? ????? ??? ????? 11001 11", jalr , I, do {s->dnpc = src1 + imm; R(rd) = s->pc + 4; } while(0)); + INSTPAT("??????? ????? ????? ??? ????? 11011 11", jal , J, do {s->dnpc = s->pc + imm; R(rd) = s->pc + 4; ftrace_call(s->pc + imm); } while(0)); + INSTPAT("??????? ????? ????? ??? ????? 11001 11", jalr , I, do {s->dnpc = src1 + imm; R(rd) = s->pc + 4; ftrace(s, rd, src1 + imm); } while(0)); INSTPAT("??????? ????? ????? 000 ????? 11000 11", beq , B, do_branch(s, src1 == src2, imm)); INSTPAT("??????? ????? ????? 001 ????? 11000 11", bne , B, do_branch(s, src1 != src2, imm)); INSTPAT("??????? ????? ????? 100 ????? 11000 11", blt , B, do_branch(s, (sword_t)src1 < (sword_t)src2, imm)); diff --git a/nemu/src/utils/ftrace.c b/nemu/src/utils/ftrace.c index 55d4801..a756578 100644 --- a/nemu/src/utils/ftrace.c +++ b/nemu/src/utils/ftrace.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include // Put this into another file #ifdef CONFIG_FTRACE @@ -16,7 +16,7 @@ static int cmp_func_t(const void *a, const void *b) { return ((func_t *)a)->start > ((func_t *)b)->start; } -const char *get_func_name(vaddr_t addr) { +static const char *get_func_name(vaddr_t addr) { int l = 0, r = func_table_len - 1; while(l <= r) { int mid = (l + r) / 2; @@ -98,3 +98,12 @@ failed_nosym: Error("Failed reading elf file"); return; } + +void ftrace_call(vaddr_t addr) { + printf("call [%s@0x%x]", get_func_name(addr), addr); +} + +void ftrace_return(vaddr_t addr) { + printf("ret [%s@0x%x]", get_func_name(addr), addr); +} +