From f52699a8bf609e813eb31ab9df5ab130d44ac9a2 Mon Sep 17 00:00:00 2001 From: vhaudiquet Date: Sun, 8 Oct 2023 17:42:44 +0200 Subject: [PATCH] Multithread gdbstub and execution, with cont/halt --- src/cpu/rv32cpu.c | 9 +++++++++ src/cpu/rv32cpu.h | 7 +++++++ src/gdbstub/gdbstub.c | 12 +++++++++++- src/main.c | 1 + 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/cpu/rv32cpu.c b/src/cpu/rv32cpu.c index 1e9afc1..8cc68c1 100644 --- a/src/cpu/rv32cpu.c +++ b/src/cpu/rv32cpu.c @@ -42,6 +42,7 @@ void cpu_init() { cpu0 = calloc(1, sizeof(rv32_cpu_t)); pthread_mutex_init(&cpu0_mutex, 0); + pthread_cond_init(&cpu0->sim_condition, 0); cpu0->regs.zero = 0; } @@ -651,6 +652,9 @@ void cpu_loop(rv32_cpu_t* cpu) pthread_mutex_lock(&cpu0_mutex); pthread_mutex_lock(&memory_mutex); + while(!cpu->sim_ticks_left) + pthread_cond_wait(&cpu0->sim_condition, &cpu0_mutex); + // Fetch raw_instruction_t raw_instruction; if(cpu->pc > memory_size - 4) @@ -675,6 +679,11 @@ void cpu_loop(rv32_cpu_t* cpu) cpu->pc += 4; + // Update simulation data + cpu->sim_ticks_done++; + if(cpu->sim_ticks_left != (-1)) + cpu->sim_ticks_left--; + // Let go of cpu and memory mutex pthread_mutex_unlock(&cpu0_mutex); pthread_mutex_unlock(&memory_mutex); diff --git a/src/cpu/rv32cpu.h b/src/cpu/rv32cpu.h index 74846e0..01465a9 100644 --- a/src/cpu/rv32cpu.h +++ b/src/cpu/rv32cpu.h @@ -3,6 +3,7 @@ #include #include +#include /* * This is a structure encoding for the registers of @@ -184,8 +185,14 @@ typedef struct RV32_CPU_REGS typedef struct RV32_CPU { + // CPU values rv32_cpu_regs_t regs; uint32_t pc; + + // Simulation data + ssize_t sim_ticks_left; // -1 : simulate forever + size_t sim_ticks_done; + pthread_cond_t sim_condition; } rv32_cpu_t; extern rv32_cpu_t* cpu0; diff --git a/src/gdbstub/gdbstub.c b/src/gdbstub/gdbstub.c index 8bfa5e0..d556dd2 100644 --- a/src/gdbstub/gdbstub.c +++ b/src/gdbstub/gdbstub.c @@ -24,6 +24,7 @@ typedef struct sockaddr sockaddr_t; socket_t gdbstub_server_socket; socket_t gdb_socket; +pthread_t gdbstub_thread; void gdbstub_thread_gdb(); static void gdbstub_handle_ctrlc(); @@ -159,7 +160,7 @@ void gdbstub_wait_for_connection() gdb_socket = c_socket; - gdbstub_thread_gdb(); + pthread_create(&gdbstub_thread, 0, (void*) gdbstub_thread_gdb, 0); } void gdbstub_thread_gdb() @@ -285,6 +286,8 @@ void gdbstub_thread_gdb() { // s : single-step + // TODO : Implement that correctly + // Send back halt reason // Halt Reason : Signal 05 (SIGTRAP) char* resp = "S05"; @@ -298,6 +301,10 @@ void gdbstub_thread_gdb() send(gdb_socket, "+", 1, 0); // Continue simulation + pthread_mutex_lock(&cpu0_mutex); + cpu0->sim_ticks_left = -1; + pthread_mutex_unlock(&cpu0_mutex); + pthread_cond_signal(&cpu0->sim_condition); } else gdbstub_send_unsupported(); } @@ -310,6 +317,9 @@ void gdbstub_thread_gdb() static void gdbstub_handle_ctrlc() { // Halt the simulation + pthread_mutex_lock(&cpu0_mutex); + cpu0->sim_ticks_left = 0; + pthread_mutex_unlock(&cpu0_mutex); // Send back halt signal to gdb // Halt Reason : Signal 05 (SIGTRAP) diff --git a/src/main.c b/src/main.c index eb7b64e..4bdae12 100644 --- a/src/main.c +++ b/src/main.c @@ -28,6 +28,7 @@ int main(int argc, char** argv) } // CPU simulation : create cpu0 thread + if(!gdbstub) cpu0->sim_ticks_left = -1; // Simulate forever pthread_t cpu0_thread; pthread_create(&cpu0_thread, 0, (void*) cpu_loop, cpu0);