From 12b18e77fde8accad37f38e8eae9c98d65d68c48 Mon Sep 17 00:00:00 2001 From: tracer-ysyx Date: Wed, 13 Mar 2024 17:50:21 +0800 Subject: [PATCH] =?UTF-8?q?>=20compile=20NEMU=20ysyx=5F22040000=20?= =?UTF-8?q?=E6=9D=8E=E5=BF=83=E6=9D=A8=20Linux=20calcite=206.6.19=20#1-Nix?= =?UTF-8?q?OS=20SMP=20PREEMPT=5FDYNAMIC=20Fri=20Mar=20=201=2012:35:11=20UT?= =?UTF-8?q?C=202024=20x86=5F64=20GNU/Linux=20=2017:50:21=20=20up=20=20=207?= =?UTF-8?q?:26,=20=202=20users,=20=20load=20average:=200.39,=200.50,=200.5?= =?UTF-8?q?4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nemu/Kconfig | 15 ++++++++++++++- nemu/src/memory/paddr.c | 34 ++++++++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/nemu/Kconfig b/nemu/Kconfig index e4916c1..adc8183 100644 --- a/nemu/Kconfig +++ b/nemu/Kconfig @@ -159,7 +159,20 @@ config ITRACE_BUFFER config MTRACE depends on TRACE && TARGET_NATIVE_ELF && ENGINE_INTERPRETER bool "Enable memory tracer" - default n + + +config MTRACE_RANGE + depends on MTRACE + string "Memory trace active range" + default "0-fffffff" + help + Memory tracer will only print memory access in these ranges. + Use comma to seperate between ranges. + +config MTRACE_RANGE_MAX + depends on MTRACE + int "Max range count in MTRACE_RANGE" + default 10 config DIFFTEST depends on TARGET_NATIVE_ELF diff --git a/nemu/src/memory/paddr.c b/nemu/src/memory/paddr.c index a8bbf0b..52fddee 100644 --- a/nemu/src/memory/paddr.c +++ b/nemu/src/memory/paddr.c @@ -13,6 +13,7 @@ * See the Mulan PSL v2 for more details. ***************************************************************************************/ +#include "common.h" #include #include #include @@ -23,6 +24,11 @@ static uint8_t *pmem = NULL; #else // CONFIG_PMEM_GARRAY static uint8_t pmem[CONFIG_MSIZE] PG_ALIGN = {}; #endif +#ifdef CONFIG_MTRACE +static word_t mtrace_start[CONFIG_MTRACE_RANGE_MAX] = {0}; +static word_t mtrace_end[CONFIG_MTRACE_RANGE_MAX] = {0}; +static int range_count = 0; +#endif uint8_t* guest_to_host(paddr_t paddr) { return pmem + paddr - CONFIG_MBASE; } paddr_t host_to_guest(uint8_t *haddr) { return haddr - pmem + CONFIG_MBASE; } @@ -41,10 +47,34 @@ static void out_of_bound(paddr_t addr) { addr, PMEM_LEFT, PMEM_RIGHT, cpu.pc); } +#ifdef CONFIG_MTRACE +static void mtrace_print(char type, word_t addr, int len, word_t data) { + for (int i = 0; i < range_count; i++) + if (addr <= mtrace_end[i] && addr >= mtrace_start[i] ) { + printf("[TRACE] Mem %c " FMT_WORD "%d D " FMT_WORD "\n", type, addr, len, data); + break; + } +} +#endif + void init_mem() { #if defined(CONFIG_PMEM_MALLOC) pmem = malloc(CONFIG_MSIZE); assert(pmem); +#endif +#ifdef CONFIG_MTRACE + char range[sizeof(CONFIG_MTRACE_RANGE)] = CONFIG_MTRACE_RANGE; + char *saveptr, *ptr; + ptr = strtok_r(range, ",", &saveptr); + for (range_count = 0; range_count < CONFIG_MTRACE_RANGE_MAX; range_count++) { + word_t start, end; + Assert(sscanf(ptr, "%x-%x", &start, &end) == 2, "Config option MTRACE_RANGE has wrong format"); + mtrace_start[range_count] = start; + mtrace_end[range_count] = end; + + ptr = strtok_r(NULL, ",", &saveptr); + if (!ptr) break; + } #endif IFDEF(CONFIG_MEM_RANDOM, memset(pmem, rand(), CONFIG_MSIZE)); Log("physical memory area [" FMT_PADDR ", " FMT_PADDR "]", PMEM_LEFT, PMEM_RIGHT); @@ -57,13 +87,13 @@ word_t paddr_read(paddr_t addr, int len) { out_of_bound(addr); mtrace: - IFDEF(CONFIG_MTRACE, printf("R " FMT_WORD "%d D " FMT_WORD "\n", addr, len, result)); + IFDEF(CONFIG_MTRACE, mtrace_print('R', addr, len, result)); return result; } void paddr_write(paddr_t addr, int len, word_t data) { - IFDEF(CONFIG_MTRACE, printf("W " FMT_WORD "%d D " FMT_WORD "\n", addr, len, data)); + IFDEF(CONFIG_MTRACE, mtrace_print('W', addr, len, data)); if (likely(in_pmem(addr))) { pmem_write(addr, len, data); return; } IFDEF(CONFIG_DEVICE, mmio_write(addr, len, data); return); out_of_bound(addr);