diff --git a/src/cpu/rv32cpu.c b/src/cpu/rv32cpu.c index 603ccff..1e9afc1 100644 --- a/src/cpu/rv32cpu.c +++ b/src/cpu/rv32cpu.c @@ -9,7 +9,7 @@ #include rv32_cpu_t* cpu0; -pthread_mutex_t* cpu0_mutex; +pthread_mutex_t cpu0_mutex; typedef union RAW_INSTRUCTION { @@ -41,7 +41,7 @@ static void cpu_print_instruction(instruction_t* instruction); void cpu_init() { cpu0 = calloc(1, sizeof(rv32_cpu_t)); - pthread_mutex_init(cpu0_mutex, 0); + pthread_mutex_init(&cpu0_mutex, 0); cpu0->regs.zero = 0; } @@ -647,8 +647,9 @@ void cpu_loop(rv32_cpu_t* cpu) { while(1) { - // Aquire CPU mutex - pthread_mutex_lock(cpu0_mutex); + // Aquire CPU and memory mutex + pthread_mutex_lock(&cpu0_mutex); + pthread_mutex_lock(&memory_mutex); // Fetch raw_instruction_t raw_instruction; @@ -674,8 +675,9 @@ void cpu_loop(rv32_cpu_t* cpu) cpu->pc += 4; - // Let go of cpu mutex - pthread_mutex_unlock(cpu0_mutex); + // 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 a2f947d..74846e0 100644 --- a/src/cpu/rv32cpu.h +++ b/src/cpu/rv32cpu.h @@ -189,7 +189,7 @@ typedef struct RV32_CPU } rv32_cpu_t; extern rv32_cpu_t* cpu0; -extern pthread_mutex_t* cpu0_mutex; +extern pthread_mutex_t cpu0_mutex; void cpu_init(); void cpu_loop(rv32_cpu_t* cpu); diff --git a/src/gdbstub/gdbstub.c b/src/gdbstub/gdbstub.c index b22911f..8bfa5e0 100644 --- a/src/gdbstub/gdbstub.c +++ b/src/gdbstub/gdbstub.c @@ -184,7 +184,7 @@ void gdbstub_thread_gdb() char resp[32 * 8 + 8 + 1] = {0}; // Obtain CPU0 mutex - pthread_mutex_lock(cpu0_mutex); + pthread_mutex_lock(&cpu0_mutex); // All general purpose registers in host byte order as chars for(size_t i = 0; i < 32; i++) @@ -198,7 +198,7 @@ void gdbstub_thread_gdb() snprintf(resp + 32 * 8, 9, "%08x", pc); // Let go of CPU0 mutex - pthread_mutex_unlock(cpu0_mutex); + pthread_mutex_unlock(&cpu0_mutex); // Final packet size, send packet size_t size = 32 * 8 + 8; @@ -209,7 +209,7 @@ void gdbstub_thread_gdb() // G : write all registers -> read and set all registers // Obtain CPU0 mutex - pthread_mutex_lock(cpu0_mutex); + pthread_mutex_lock(&cpu0_mutex); // All general purpose registers in host byte order as chars for(size_t i = 1; i < 32; i++) @@ -228,7 +228,7 @@ void gdbstub_thread_gdb() cpu0->pc = pc; // Let go of CPU0 Mutex - pthread_mutex_unlock(cpu0_mutex); + pthread_mutex_unlock(&cpu0_mutex); gdbstub_send_packet("OK", 2); } @@ -239,6 +239,9 @@ void gdbstub_thread_gdb() uint32_t length; sscanf(packet + 1, "%x,%x", &address, &length); + // Aquire memory mutex + pthread_mutex_lock(&memory_mutex); + char data[length * 2 + 1]; for(size_t i = 0; i < length; i++) { @@ -246,6 +249,9 @@ void gdbstub_thread_gdb() snprintf(data + i * 2, 3, "%02x", value); } + // Let go of memory mutex + pthread_mutex_unlock(&memory_mutex); + gdbstub_send_packet(data, length * 2); } else if(packet[0] == 'M') @@ -260,6 +266,9 @@ void gdbstub_thread_gdb() data_start++; data_start++; + // Aquire memory mutex + pthread_mutex_lock(&memory_mutex); + for(size_t i = 0; i < length; i++) { uint32_t value; @@ -267,6 +276,9 @@ void gdbstub_thread_gdb() memory[mmu_translate(address + i)] = value; } + // Let go of memory mutex + pthread_mutex_unlock(&memory_mutex); + gdbstub_send_packet("OK", 2); } else if(packet[0] == 's') diff --git a/src/memory/memory.c b/src/memory/memory.c index 0bd3622..1156f34 100644 --- a/src/memory/memory.c +++ b/src/memory/memory.c @@ -2,8 +2,10 @@ #include "vriscv.h" uint8_t* memory; +pthread_mutex_t memory_mutex; void mem_init() { memory = malloc(memory_size); + pthread_mutex_init(&memory_mutex, 0); } diff --git a/src/memory/memory.h b/src/memory/memory.h index 8fb8975..d49ddef 100644 --- a/src/memory/memory.h +++ b/src/memory/memory.h @@ -2,8 +2,10 @@ #define MEMORY_H #include +#include extern uint8_t* memory; +extern pthread_mutex_t memory_mutex; void mem_init();