CPU privilege modes
This commit is contained in:
		@@ -45,6 +45,7 @@ void cpu_init()
 | 
				
			|||||||
	pthread_mutex_init(&cpu0_mutex, 0);
 | 
						pthread_mutex_init(&cpu0_mutex, 0);
 | 
				
			||||||
	pthread_cond_init(&cpu0->sim_condition, 0);
 | 
						pthread_cond_init(&cpu0->sim_condition, 0);
 | 
				
			||||||
	cpu0->regs.zero = 0;
 | 
						cpu0->regs.zero = 0;
 | 
				
			||||||
 | 
						cpu0->privilege_mode = MACHINE;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void cpu_decode(raw_instruction_t raw_instruction, instruction_t* output)
 | 
					static void cpu_decode(raw_instruction_t raw_instruction, instruction_t* output)
 | 
				
			||||||
@@ -523,8 +524,10 @@ static void cpu_execute(rv32_cpu_t* cpu, instruction_t* instruction)
 | 
				
			|||||||
							fprintf(stderr, "SRET: We don't support that.\n");
 | 
												fprintf(stderr, "SRET: We don't support that.\n");
 | 
				
			||||||
							break;
 | 
												break;
 | 
				
			||||||
						case IMM_MRET:
 | 
											case IMM_MRET:
 | 
				
			||||||
							// Act like a normal ret/jalr, with destination address being CSR_MEPC content
 | 
												// Ret to destination address : CSR_MEPC content
 | 
				
			||||||
							fprintf(stderr, "Warning: MRET: We don't support privilege mode change\n");
 | 
												// Change privilege mode to SUPERVISOR
 | 
				
			||||||
 | 
												// TODO : Pop lower-privilege interrupt enable and privilege mode stack
 | 
				
			||||||
 | 
												cpu->privilege_mode = SUPERVISOR;
 | 
				
			||||||
							cpu->pc = cpu->csr[CSR_MEPC] - 4;
 | 
												cpu->pc = cpu->csr[CSR_MEPC] - 4;
 | 
				
			||||||
							break;
 | 
												break;
 | 
				
			||||||
						default:
 | 
											default:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,13 +7,20 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
#include "csr.h"
 | 
					#include "csr.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef enum RVCPU_PRIVILEGE_MODE
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						USER,
 | 
				
			||||||
 | 
						SUPERVISOR,
 | 
				
			||||||
 | 
						MACHINE
 | 
				
			||||||
 | 
					} rvcpu_privilege_mode_t;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*
 | 
					/*
 | 
				
			||||||
* This is a structure encoding for the registers of
 | 
					 * This is a structure encoding for the registers of
 | 
				
			||||||
* the rv32 cpu.
 | 
					 * the rv32 cpu.
 | 
				
			||||||
* It allows access of register x0 using :
 | 
					 * It allows access of register x0 using :
 | 
				
			||||||
* structname.x0, structname.zero, structname.x[0]
 | 
					 * structname.x0, structname.zero, structname.x[0]
 | 
				
			||||||
* This way, access can be really flexible
 | 
					 * This way, access can be really flexible
 | 
				
			||||||
*/
 | 
					 */
 | 
				
			||||||
typedef struct RV32_CPU_REGS
 | 
					typedef struct RV32_CPU_REGS
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	union
 | 
						union
 | 
				
			||||||
@@ -188,10 +195,12 @@ typedef struct RV32_CPU_REGS
 | 
				
			|||||||
typedef struct RV32_CPU
 | 
					typedef struct RV32_CPU
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	// CPU values
 | 
						// CPU values
 | 
				
			||||||
    rv32_cpu_regs_t regs;
 | 
						rv32_cpu_regs_t regs;
 | 
				
			||||||
    uint32_t pc;
 | 
						uint32_t pc;
 | 
				
			||||||
	uint32_t csr[CSR_COUNT];
 | 
						uint32_t csr[CSR_COUNT];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						rvcpu_privilege_mode_t privilege_mode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Simulation data
 | 
						// Simulation data
 | 
				
			||||||
	ssize_t sim_ticks_left; // -1 : simulate forever
 | 
						ssize_t sim_ticks_left; // -1 : simulate forever
 | 
				
			||||||
	size_t sim_ticks_done;
 | 
						size_t sim_ticks_done;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user