Compare commits
	
		
			30 Commits
		
	
	
		
			0996d15bd4
			...
			feature/ht
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| aaebeaf023 | |||
| f4718c6de3 | |||
| 53de21eef9 | |||
| d443c89c87 | |||
| 9a2df32d57 | |||
| be0f783af8 | |||
| 1089800682 | |||
| a6a6f51f0b | |||
| 21e1f791ad | |||
| be6f5791fa | |||
| d907dc7f54 | |||
| 75e81ce236 | |||
| 82a70efdb8 | |||
| 978c3db06e | |||
| 0e88664ff7 | |||
| ac818f304d | |||
| ad60449073 | |||
| b45b3589fa | |||
| 1fb7e8fcea | |||
| 5f9d0beafb | |||
| 4c0d1c75aa | |||
| 2f3abf2f76 | |||
| 62768bf81e | |||
| f6be8ec006 | |||
| a8f56b6e27 | |||
| 76ea0db25d | |||
| ec1b820c18 | |||
| 64329cf0f6 | |||
| 9de0aed84d | |||
| bb4e2766d1 | 
| @@ -109,16 +109,6 @@ if(TARGET yaml-cpp::yaml-cpp) | ||||
|     target_link_libraries(${PROJECT_NAME} PUBLIC yaml-cpp::yaml-cpp) | ||||
| endif() | ||||
|  | ||||
| if(WITH_LLVM) | ||||
|     find_package(LLVM) | ||||
|     target_compile_definitions(${PROJECT_NAME} PUBLIC ${LLVM_DEFINITIONS}) | ||||
|     target_include_directories(${PROJECT_NAME} PUBLIC ${LLVM_INCLUDE_DIRS}) | ||||
|  | ||||
|     if(BUILD_SHARED_LIBS) | ||||
|         target_link_libraries(${PROJECT_NAME} PUBLIC ${LLVM_LIBRARIES}) | ||||
|     endif() | ||||
| endif() | ||||
|  | ||||
| set_target_properties(${PROJECT_NAME} PROPERTIES | ||||
|     VERSION ${PROJECT_VERSION} | ||||
|     FRAMEWORK FALSE | ||||
| @@ -262,3 +252,9 @@ if(TARGET scc-sysc) | ||||
|         INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} # headers | ||||
|     ) | ||||
| endif() | ||||
|  | ||||
| project(elfio-test) | ||||
| find_package(Boost COMPONENTS program_options thread REQUIRED) | ||||
|  | ||||
| add_executable(${PROJECT_NAME} src/elfio.cpp) | ||||
| target_link_libraries(${PROJECT_NAME} PUBLIC elfio::elfio) | ||||
|   | ||||
| @@ -131,8 +131,6 @@ struct ${coreDef.name.toLowerCase()}: public arch_if { | ||||
|  | ||||
|     uint8_t* get_regs_base_ptr() override; | ||||
|  | ||||
|     inline uint64_t get_icount() { return reg.icount; } | ||||
|  | ||||
|     inline bool should_stop() { return interrupt_sim; } | ||||
|  | ||||
|     inline uint64_t stop_code() { return interrupt_sim; } | ||||
| @@ -141,8 +139,6 @@ struct ${coreDef.name.toLowerCase()}: public arch_if { | ||||
|  | ||||
|     virtual iss::sync_type needed_sync() const { return iss::NO_SYNC; } | ||||
|  | ||||
|     inline uint32_t get_last_branch() { return reg.last_branch; } | ||||
|  | ||||
|  | ||||
| #pragma pack(push, 1) | ||||
|     struct ${coreDef.name}_regs {<% | ||||
|   | ||||
| @@ -45,17 +45,17 @@ namespace interp { | ||||
| using namespace sysc; | ||||
| volatile std::array<bool, ${array_count}> ${coreDef.name.toLowerCase()}_init = { | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|m_p|interp", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_m_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         }), | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p|interp", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%if(coreDef.name.toLowerCase()=="tgc5d" || coreDef.name.toLowerCase()=="tgc5e") {%>, | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p_clic_pmp|interp", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}, (iss::arch::features_e)(iss::arch::FEAT_PMP | iss::arch::FEAT_EXT_N | iss::arch::FEAT_CLIC)>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%}%> | ||||
| @@ -66,17 +66,17 @@ namespace llvm { | ||||
| using namespace sysc; | ||||
| volatile std::array<bool, ${array_count}> ${coreDef.name.toLowerCase()}_init = { | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|m_p|llvm", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_m_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         }), | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p|llvm", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%if(coreDef.name.toLowerCase()=="tgc5d" || coreDef.name.toLowerCase()=="tgc5e") {%>, | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p_clic_pmp|llvm", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}, (iss::arch::features_e)(iss::arch::FEAT_PMP | iss::arch::FEAT_EXT_N | iss::arch::FEAT_CLIC)>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%}%> | ||||
| @@ -88,17 +88,17 @@ namespace tcc { | ||||
| using namespace sysc; | ||||
| volatile std::array<bool, ${array_count}> ${coreDef.name.toLowerCase()}_init = { | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|m_p|tcc", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_m_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         }), | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p|tcc", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%if(coreDef.name.toLowerCase()=="tgc5d" || coreDef.name.toLowerCase()=="tgc5e") {%>, | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p_clic_pmp|tcc", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}, (iss::arch::features_e)(iss::arch::FEAT_PMP | iss::arch::FEAT_EXT_N | iss::arch::FEAT_CLIC)>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%}%> | ||||
| @@ -110,17 +110,17 @@ namespace asmjit { | ||||
| using namespace sysc; | ||||
| volatile std::array<bool, ${array_count}> ${coreDef.name.toLowerCase()}_init = { | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|m_p|asmjit", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_m_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         }), | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p|asmjit", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%if(coreDef.name.toLowerCase()=="tgc5d" || coreDef.name.toLowerCase()=="tgc5e") {%>, | ||||
|         iss_factory::instance().register_creator("${coreDef.name.toLowerCase()}|mu_p_clic_pmp|asmjit", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|             auto* cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|             auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::${coreDef.name.toLowerCase()}, (iss::arch::features_e)(iss::arch::FEAT_PMP | iss::arch::FEAT_EXT_N | iss::arch::FEAT_CLIC)>>(cc); | ||||
|             return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::${coreDef.name.toLowerCase()}*>(cpu), gdb_port)}}; | ||||
|         })<%}%> | ||||
|   | ||||
| @@ -96,7 +96,7 @@ protected: | ||||
|     using this_class = vm_impl<ARCH>; | ||||
|     using compile_func = continuation_e (this_class::*)(virt_addr_t&, code_word_t, jit_holder&); | ||||
|  | ||||
|     continuation_e gen_single_inst_behavior(virt_addr_t&, unsigned int &, jit_holder&) override; | ||||
|     continuation_e gen_single_inst_behavior(virt_addr_t&, jit_holder&) override; | ||||
|     enum globals_e {TVAL = 0, GLOBALS_SIZE}; | ||||
|     void gen_block_prologue(jit_holder& jh) override; | ||||
|     void gen_block_epilogue(jit_holder& jh) override; | ||||
| @@ -221,7 +221,7 @@ vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id) | ||||
|     }()) {} | ||||
|  | ||||
| template <typename ARCH> | ||||
| continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, jit_holder& jh) { | ||||
| continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, jit_holder& jh) { | ||||
|     enum {TRAP_ID=1<<16}; | ||||
|     code_word_t instr = 0; | ||||
|     phys_addr_t paddr(pc); | ||||
| @@ -233,7 +233,6 @@ continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned | ||||
|         return ILLEGAL_FETCH; | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001) | ||||
|         return JUMP_TO_SELF; | ||||
|     ++inst_cnt; | ||||
|     uint32_t inst_index = instr_decoder.decode_instr(instr); | ||||
|     compile_func f = nullptr; | ||||
|     if(inst_index < instr_descr.size()) | ||||
| @@ -263,6 +262,7 @@ void vm_impl<ARCH>::gen_instr_epilogue(jit_holder& jh) { | ||||
|     cmp(cc, current_trap_state, 0); | ||||
|     cc.jne(jh.trap_entry); | ||||
|     cc.inc(get_ptr_for(jh, traits::ICOUNT)); | ||||
|     cc.inc(get_ptr_for(jh, traits::CYCLE)); | ||||
| } | ||||
| template <typename ARCH> | ||||
| void vm_impl<ARCH>::gen_block_prologue(jit_holder& jh){ | ||||
| @@ -308,6 +308,7 @@ inline void vm_impl<ARCH>::gen_raise(jit_holder& jh, uint16_t trap_id, uint16_t | ||||
|     auto tmp1 = get_reg_for(cc, traits::TRAP_STATE); | ||||
|     mov(cc, tmp1, 0x80ULL << 24 | (cause << 16) | trap_id); | ||||
|     mov(cc, get_ptr_for(jh, traits::TRAP_STATE), tmp1); | ||||
|     cc.jmp(jh.trap_entry); | ||||
| } | ||||
| template <typename ARCH> | ||||
| template <typename T, typename> | ||||
|   | ||||
| @@ -199,9 +199,6 @@ template <typename CODE_WORD> void debug_fn(CODE_WORD insn) { | ||||
|     volatile CODE_WORD x = insn; | ||||
|     insn = 2 * x; | ||||
| } | ||||
|  | ||||
| template <typename ARCH> vm_impl<ARCH>::vm_impl() { this(new ARCH()); } | ||||
|  | ||||
| // according to | ||||
| // https://stackoverflow.com/questions/8871204/count-number-of-1s-in-binary-representation | ||||
| #ifdef __GCC__ | ||||
|   | ||||
| @@ -101,7 +101,7 @@ protected: | ||||
|         return super::gen_cond_assign(cond, this->gen_ext(trueVal, size), this->gen_ext(falseVal, size)); | ||||
|     } | ||||
|  | ||||
|     std::tuple<continuation_e, BasicBlock *> gen_single_inst_behavior(virt_addr_t &, unsigned int &, BasicBlock *) override; | ||||
|     std::tuple<continuation_e, BasicBlock *> gen_single_inst_behavior(virt_addr_t &, BasicBlock *) override; | ||||
|  | ||||
|     void gen_leave_behavior(BasicBlock *leave_blk) override; | ||||
|     void gen_raise_trap(uint16_t trap_id, uint16_t cause); | ||||
| @@ -204,7 +204,7 @@ private: | ||||
|             }; | ||||
|             this->builder.CreateCall(this->mod->getFunction("print_disass"), args); | ||||
|         } | ||||
| 		this->gen_sync(iss::PRE_SYNC, instr_descr.size()); | ||||
|         this->gen_sync(iss::PRE_SYNC, instr_descr.size()); | ||||
|         this->builder.CreateStore(this->builder.CreateLoad(this->get_typeptr(traits::NEXT_PC), get_reg_ptr(traits::NEXT_PC), true), | ||||
|                                    get_reg_ptr(traits::PC), true); | ||||
|         this->builder.CreateStore( | ||||
| @@ -244,7 +244,7 @@ vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id) | ||||
|  | ||||
| template <typename ARCH> | ||||
| std::tuple<continuation_e, BasicBlock *> | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, BasicBlock *this_block) { | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, BasicBlock *this_block) { | ||||
|     // we fetch at max 4 byte, alignment is 2 | ||||
|     enum {TRAP_ID=1<<16}; | ||||
|     code_word_t instr = 0; | ||||
| @@ -256,9 +256,10 @@ vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, | ||||
|     auto res = this->core.read(paddr, 4, data); | ||||
|     if (res != iss::Ok)  | ||||
|         return std::make_tuple(ILLEGAL_FETCH, nullptr); | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001) | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001){ | ||||
|         this->builder.CreateBr(this->leave_blk); | ||||
|         return std::make_tuple(JUMP_TO_SELF, nullptr); | ||||
|     ++inst_cnt; | ||||
|         } | ||||
|     uint32_t inst_index = instr_decoder.decode_instr(instr); | ||||
|     compile_func f = nullptr; | ||||
|     if(inst_index < instr_descr.size()) | ||||
| @@ -279,6 +280,7 @@ template <typename ARCH> | ||||
| void vm_impl<ARCH>::gen_raise_trap(uint16_t trap_id, uint16_t cause) { | ||||
|     auto *TRAP_val = this->gen_const(32, 0x80 << 24 | (cause << 16) | trap_id); | ||||
|     this->builder.CreateStore(TRAP_val, get_reg_ptr(traits::TRAP_STATE), true); | ||||
|     this->builder.CreateBr(this->trap_blk); | ||||
| } | ||||
|  | ||||
| template <typename ARCH> | ||||
| @@ -339,6 +341,10 @@ void vm_impl<ARCH>::gen_instr_epilogue(BasicBlock *bb) { | ||||
|     auto* icount_val = this->builder.CreateAdd( | ||||
|         this->builder.CreateLoad(this->get_typeptr(arch::traits<ARCH>::ICOUNT), get_reg_ptr(arch::traits<ARCH>::ICOUNT)), this->gen_const(64U, 1)); | ||||
|     this->builder.CreateStore(icount_val, get_reg_ptr(arch::traits<ARCH>::ICOUNT), false); | ||||
|     //increment cyclecount | ||||
|     auto* cycle_val = this->builder.CreateAdd( | ||||
|         this->builder.CreateLoad(this->get_typeptr(arch::traits<ARCH>::CYCLE), get_reg_ptr(arch::traits<ARCH>::CYCLE)), this->gen_const(64U, 1)); | ||||
|     this->builder.CreateStore(cycle_val, get_reg_ptr(arch::traits<ARCH>::CYCLE), false); | ||||
| } | ||||
|  | ||||
| } // namespace ${coreDef.name.toLowerCase()} | ||||
| @@ -381,4 +387,4 @@ volatile std::array<bool, 2> dummy = { | ||||
| }; | ||||
| } | ||||
| } | ||||
| // clang-format on | ||||
| // clang-format on | ||||
|   | ||||
| @@ -83,21 +83,21 @@ protected: | ||||
|     using vm_base<ARCH>::get_reg_ptr; | ||||
|  | ||||
|     using this_class = vm_impl<ARCH>; | ||||
|     using compile_ret_t = std::tuple<continuation_e>; | ||||
|     using compile_ret_t = continuation_e; | ||||
|     using compile_func = compile_ret_t (this_class::*)(virt_addr_t &pc, code_word_t instr, tu_builder&); | ||||
|  | ||||
|     inline const char *name(size_t index){return traits::reg_aliases.at(index);} | ||||
| <% | ||||
| if(fcsr != null) {%> | ||||
|     inline const char *fname(size_t index){return index < 32?name(index+traits::F0):"illegal";}    | ||||
|  | ||||
|     void add_prologue(tu_builder& tu) override; | ||||
| <%}%> | ||||
|     void add_prologue(tu_builder& tu) override; | ||||
|  | ||||
|     void setup_module(std::string m) override { | ||||
|         super::setup_module(m); | ||||
|     } | ||||
|  | ||||
|     compile_ret_t gen_single_inst_behavior(virt_addr_t &, unsigned int &, tu_builder&) override; | ||||
|     compile_ret_t gen_single_inst_behavior(virt_addr_t &, tu_builder&) override; | ||||
|  | ||||
|     void gen_trap_behavior(tu_builder& tu) override; | ||||
|  | ||||
| @@ -176,6 +176,7 @@ private: | ||||
|         auto cur_pc_val = tu.constant(pc.val, traits::reg_bit_widths[traits::PC]); | ||||
|         pc=pc+ ${instr.length/8}; | ||||
|         gen_set_pc(tu, pc, traits::NEXT_PC); | ||||
|         tu("(*cycle)++;"); | ||||
|         tu.open_scope(); | ||||
|         this->gen_set_tval(tu, instr); | ||||
|         <%instr.behavior.eachLine{%>${it} | ||||
| @@ -225,8 +226,8 @@ vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id) | ||||
|     }()) {} | ||||
|  | ||||
| template <typename ARCH> | ||||
| std::tuple<continuation_e> | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, tu_builder& tu) { | ||||
| continuation_e | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, tu_builder& tu) { | ||||
|     // we fetch at max 4 byte, alignment is 2 | ||||
|     enum {TRAP_ID=1<<16}; | ||||
|     code_word_t instr = 0; | ||||
| @@ -238,7 +239,6 @@ vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, | ||||
|         return ILLEGAL_FETCH; | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001)  | ||||
|         return JUMP_TO_SELF; | ||||
|     ++inst_cnt; | ||||
|     uint32_t inst_index = instr_decoder.decode_instr(instr); | ||||
|     compile_func f = nullptr; | ||||
|     if(inst_index < instr_descr.size()) | ||||
| @@ -273,10 +273,12 @@ template <typename ARCH> void vm_impl<ARCH>::gen_trap_behavior(tu_builder& tu) { | ||||
|     tu.store(traits::LAST_BRANCH, tu.constant(static_cast<int>(UNKNOWN_JUMP),32)); | ||||
|     tu("return *next_pc;"); | ||||
| } | ||||
| <% | ||||
| if(fcsr != null) {%> | ||||
| template <typename ARCH> void vm_impl<ARCH>::add_prologue(tu_builder& tu){ | ||||
|     std::ostringstream os; | ||||
|     os << tu.add_reg_ptr("trap_state", arch::traits<ARCH>::TRAP_STATE, this->regs_base_ptr); | ||||
|     os << tu.add_reg_ptr("pending_trap", arch::traits<ARCH>::PENDING_TRAP, this->regs_base_ptr); | ||||
|     os << tu.add_reg_ptr("cycle", arch::traits<ARCH>::CYCLE, this->regs_base_ptr); | ||||
| <%if(fcsr != null) {%> | ||||
|     os << "uint32_t (*fget_flags)()=" << (uintptr_t)&fget_flags << ";\\n"; | ||||
|     os << "uint32_t (*fadd_s)(uint32_t v1, uint32_t v2, uint8_t mode)=" << (uintptr_t)&fadd_s << ";\\n"; | ||||
|     os << "uint32_t (*fsub_s)(uint32_t v1, uint32_t v2, uint8_t mode)=" << (uintptr_t)&fsub_s << ";\\n"; | ||||
| @@ -303,9 +305,9 @@ template <typename ARCH> void vm_impl<ARCH>::add_prologue(tu_builder& tu){ | ||||
|     os << "uint64_t (*fcvt_32_64)(uint32_t v1, uint32_t op, uint8_t mode)=" << (uintptr_t)&fcvt_32_64 << ";\\n"; | ||||
|     os << "uint32_t (*fcvt_64_32)(uint64_t v1, uint32_t op, uint8_t mode)=" << (uintptr_t)&fcvt_64_32 << ";\\n"; | ||||
|     os << "uint32_t (*unbox_s)(uint64_t v)=" << (uintptr_t)&unbox_s << ";\\n"; | ||||
|     <%}%> | ||||
|     tu.add_prologue(os.str()); | ||||
| } | ||||
| <%}%> | ||||
|  | ||||
| } // namespace ${coreDef.name.toLowerCase()} | ||||
|  | ||||
|   | ||||
							
								
								
									
										35
									
								
								src/elfio.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								src/elfio.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,35 @@ | ||||
| #ifdef _MSC_VER | ||||
| #define _SCL_SECURE_NO_WARNINGS | ||||
| #define ELFIO_NO_INTTYPES | ||||
| #endif | ||||
|  | ||||
| #include <elfio/elfio_dump.hpp> | ||||
| #include <iostream> | ||||
|  | ||||
| using namespace ELFIO; | ||||
|  | ||||
| int main(int argc, char** argv) { | ||||
|     if(argc != 2) { | ||||
|         printf("Usage: elfdump <file_name>\n"); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     elfio reader; | ||||
|  | ||||
|     if(!reader.load(argv[1])) { | ||||
|         printf("File %s is not found or it is not an ELF file\n", argv[1]); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     dump::header(std::cout, reader); | ||||
|     dump::section_headers(std::cout, reader); | ||||
|     dump::segment_headers(std::cout, reader); | ||||
|     dump::symbol_tables(std::cout, reader); | ||||
|     dump::notes(std::cout, reader); | ||||
|     dump::modinfo(std::cout, reader); | ||||
|     dump::dynamic_tags(std::cout, reader); | ||||
|     dump::section_datas(std::cout, reader); | ||||
|     dump::segment_datas(std::cout, reader); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
| @@ -35,11 +35,15 @@ | ||||
| #ifndef _RISCV_HART_COMMON | ||||
| #define _RISCV_HART_COMMON | ||||
|  | ||||
| #include "iss/vm_types.h" | ||||
| #include <array> | ||||
| #include <cstdint> | ||||
| #include <elfio/elfio.hpp> | ||||
| #include <fmt/format.h> | ||||
| #include <iss/arch_if.h> | ||||
| #include <iss/log_categories.h> | ||||
| #include <limits> | ||||
| #include <sstream> | ||||
| #include <string> | ||||
| #include <unordered_map> | ||||
| #include <util/logging.h> | ||||
| @@ -55,8 +59,6 @@ | ||||
| namespace iss { | ||||
| namespace arch { | ||||
|  | ||||
| enum { tohost_dflt = 0xF0001000, fromhost_dflt = 0xF0001040 }; | ||||
|  | ||||
| enum features_e { FEAT_NONE, FEAT_PMP = 1, FEAT_EXT_N = 2, FEAT_CLIC = 4, FEAT_DEBUG = 8, FEAT_TCM = 16 }; | ||||
|  | ||||
| enum riscv_csr { | ||||
| @@ -312,58 +314,101 @@ inline void write_reg_uint32(uint64_t offs, uint32_t& reg, const uint8_t* const | ||||
| } | ||||
| struct riscv_hart_common { | ||||
|     riscv_hart_common(){}; | ||||
|     ~riscv_hart_common(){}; | ||||
|     ~riscv_hart_common() { | ||||
|         if(io_buf.str().length()) { | ||||
|             CPPLOG(INFO) << "tohost send '" << io_buf.str() << "'"; | ||||
|         } | ||||
|     }; | ||||
|     std::unordered_map<std::string, uint64_t> symbol_table; | ||||
|     uint64_t entry_address{0}; | ||||
|     uint64_t tohost = std::numeric_limits<uint64_t>::max(); | ||||
|     uint64_t fromhost = std::numeric_limits<uint64_t>::max(); | ||||
|     std::stringstream io_buf; | ||||
|  | ||||
|     std::unordered_map<std::string, uint64_t> get_sym_table(std::string name) { | ||||
|         if(!symbol_table.empty()) | ||||
|             return symbol_table; | ||||
|         FILE* fp = fopen(name.c_str(), "r"); | ||||
|         if(fp) { | ||||
|             std::array<char, 5> buf; | ||||
|             auto n = fread(buf.data(), 1, 4, fp); | ||||
|             fclose(fp); | ||||
|             if(n != 4) | ||||
|                 throw std::runtime_error("input file has insufficient size"); | ||||
|             buf[4] = 0; | ||||
|             if(strcmp(buf.data() + 1, "ELF") == 0) { | ||||
|                 // Create elfio reader | ||||
|                 ELFIO::elfio reader; | ||||
|                 // Load ELF data | ||||
|                 if(!reader.load(name)) | ||||
|                     throw std::runtime_error("could not process elf file"); | ||||
|                 // check elf properties | ||||
|                 if(reader.get_type() != ET_EXEC) | ||||
|                     throw std::runtime_error("wrong elf type in file"); | ||||
|                 if(reader.get_machine() != EM_RISCV) | ||||
|                     throw std::runtime_error("wrong elf machine in file"); | ||||
|                 const auto sym_sec = reader.sections[".symtab"]; | ||||
|                 if(SHT_SYMTAB == sym_sec->get_type() || SHT_DYNSYM == sym_sec->get_type()) { | ||||
|                     ELFIO::symbol_section_accessor symbols(reader, sym_sec); | ||||
|                     auto sym_no = symbols.get_symbols_num(); | ||||
|                     std::string name; | ||||
|                     ELFIO::Elf64_Addr value = 0; | ||||
|                     ELFIO::Elf_Xword size = 0; | ||||
|                     unsigned char bind = 0; | ||||
|                     unsigned char type = 0; | ||||
|                     ELFIO::Elf_Half section = 0; | ||||
|                     unsigned char other = 0; | ||||
|                     for(auto i = 0U; i < sym_no; ++i) { | ||||
|                         symbols.get_symbol(i, name, value, size, bind, type, section, other); | ||||
|                         if(name != "") { | ||||
|                             this->symbol_table[name] = value; | ||||
|     bool read_elf_file(std::string name, uint8_t expected_elf_class, | ||||
|                        std::function<iss::status(uint64_t, uint64_t, const uint8_t* const)> cb) { | ||||
|         // Create elfio reader | ||||
|         ELFIO::elfio reader; | ||||
|         // Load ELF data | ||||
|         if(reader.load(name)) { | ||||
|             // check elf properties | ||||
|             if(reader.get_class() != expected_elf_class) | ||||
|                 return false; | ||||
|             if(reader.get_type() != ELFIO::ET_EXEC) | ||||
|                 return false; | ||||
|             if(reader.get_machine() != ELFIO::EM_RISCV) | ||||
|                 return false; | ||||
|             entry_address = reader.get_entry(); | ||||
|             for(const auto& pseg : reader.segments) { | ||||
|                 const auto fsize = pseg->get_file_size(); // 0x42c/0x0 | ||||
|                 const auto seg_data = pseg->get_data(); | ||||
|                 const auto type = pseg->get_type(); | ||||
|                 if(type == 1 && fsize > 0) { | ||||
|                     auto res = cb(pseg->get_physical_address(), fsize, reinterpret_cast<const uint8_t* const>(seg_data)); | ||||
|                     if(res != iss::Ok) | ||||
|                         CPPLOG(ERR) << "problem writing " << fsize << "bytes to 0x" << std::hex << pseg->get_physical_address(); | ||||
|                 } | ||||
|             } | ||||
|             const auto sym_sec = reader.sections[".symtab"]; | ||||
|             if(ELFIO::SHT_SYMTAB == sym_sec->get_type() || ELFIO::SHT_DYNSYM == sym_sec->get_type()) { | ||||
|                 ELFIO::symbol_section_accessor symbols(reader, sym_sec); | ||||
|                 auto sym_no = symbols.get_symbols_num(); | ||||
|                 std::string name; | ||||
|                 ELFIO::Elf64_Addr value = 0; | ||||
|                 ELFIO::Elf_Xword size = 0; | ||||
|                 unsigned char bind = 0; | ||||
|                 unsigned char type = 0; | ||||
|                 ELFIO::Elf_Half section = 0; | ||||
|                 unsigned char other = 0; | ||||
|                 for(auto i = 0U; i < sym_no; ++i) { | ||||
|                     symbols.get_symbol(i, name, value, size, bind, type, section, other); | ||||
|                     if(name != "") { | ||||
|                         this->symbol_table[name] = value; | ||||
| #ifndef NDEBUG | ||||
|                             CPPLOG(DEBUG) << "Found Symbol " << name; | ||||
|                         CPPLOG(DEBUG) << "Found Symbol " << name; | ||||
| #endif | ||||
|                         } | ||||
|                     } | ||||
|                 } | ||||
|                 return symbol_table; | ||||
|                 try { | ||||
|                     tohost = symbol_table.at("tohost"); | ||||
|                 } catch(std::out_of_range& e) { | ||||
|                 } | ||||
|                 try { | ||||
|                     fromhost = symbol_table.at("fromhost"); | ||||
|                 } catch(std::out_of_range& e) { | ||||
|                 } | ||||
|             } | ||||
|             throw std::runtime_error(fmt::format("memory load file {} is not a valid elf file", name)); | ||||
|         } else | ||||
|             throw std::runtime_error(fmt::format("memory load file not found, check if {} is a valid file", name)); | ||||
|             return true; | ||||
|         } | ||||
|         return false; | ||||
|     }; | ||||
|     iss::status execute_sys_write(arch_if* aif, const std::array<uint64_t, 8>& loaded_payload, unsigned mem_type) { | ||||
|         uint64_t fd = loaded_payload[1]; | ||||
|         uint64_t buf_ptr = loaded_payload[2]; | ||||
|         uint64_t len = loaded_payload[3]; | ||||
|         std::vector<char> buf(len); | ||||
|         if(aif->read(address_type::PHYSICAL, access_type::DEBUG_READ, mem_type, buf_ptr, len, reinterpret_cast<uint8_t*>(buf.data()))) { | ||||
|             CPPLOG(ERR) << "SYS_WRITE buffer read went wrong"; | ||||
|             return iss::Err; | ||||
|         } | ||||
|         // we disregard the fd and just log to stdout | ||||
|         for(size_t i = 0; i < len; i++) { | ||||
|             if(buf[i] == '\n' || buf[i] == '\0') { | ||||
|                 CPPLOG(INFO) << "tohost send '" << io_buf.str() << "'"; | ||||
|                 io_buf.str(""); | ||||
|             } else | ||||
|                 io_buf << buf[i]; | ||||
|         } | ||||
|  | ||||
|         // Not sure what the correct return value should be | ||||
|         uint8_t ret_val = 1; | ||||
|         if(fromhost != std::numeric_limits<uint64_t>::max()) | ||||
|             if(aif->write(address_type::PHYSICAL, access_type::DEBUG_WRITE, mem_type, fromhost, 1, &ret_val)) { | ||||
|                 CPPLOG(ERR) << "Fromhost write went wrong"; | ||||
|                 return iss::Err; | ||||
|             } | ||||
|         return iss::Ok; | ||||
|     } | ||||
| }; | ||||
|  | ||||
| } // namespace arch | ||||
|   | ||||
| @@ -41,6 +41,11 @@ | ||||
| #include "iss/vm_if.h" | ||||
| #include "iss/vm_types.h" | ||||
| #include "riscv_hart_common.h" | ||||
| #include "util/logging.h" | ||||
| #include <algorithm> | ||||
| #include <cstdint> | ||||
| #include <elfio/elf_types.hpp> | ||||
| #include <limits> | ||||
| #include <stdexcept> | ||||
| #ifndef FMT_HEADER_ONLY | ||||
| #define FMT_HEADER_ONLY | ||||
| @@ -278,7 +283,7 @@ public: | ||||
|  | ||||
|     void disass_output(uint64_t pc, const std::string instr) override { | ||||
|         NSCLOG(INFO, LOGCAT) << fmt::format("0x{:016x}    {:40} [s:0x{:x};c:{}]", pc, instr, (reg_t)state.mstatus, | ||||
|                                             this->reg.icount + cycle_offset); | ||||
|                                             this->reg.cycle + cycle_offset); | ||||
|     }; | ||||
|  | ||||
|     iss::instrumentation_if* get_instrumentation_if() override { return &instr_if; } | ||||
| @@ -311,7 +316,7 @@ protected: | ||||
|  | ||||
|         uint64_t get_pendig_traps() override { return arch.reg.trap_state; } | ||||
|  | ||||
|         uint64_t get_total_cycles() override { return arch.reg.icount + arch.cycle_offset; } | ||||
|         uint64_t get_total_cycles() override { return arch.reg.cycle + arch.cycle_offset; } | ||||
|  | ||||
|         void update_last_instr_cycles(unsigned cycles) override { arch.cycle_offset += cycles - 1; } | ||||
|  | ||||
| @@ -321,7 +326,7 @@ protected: | ||||
|  | ||||
|         unsigned get_reg_size(unsigned num) override { return traits<BASE>::reg_bit_widths[num]; } | ||||
|  | ||||
|         std::unordered_map<std::string, uint64_t> get_symbol_table(std::string name) override { return arch.get_sym_table(name); } | ||||
|         std::unordered_map<std::string, uint64_t> const& get_symbol_table(std::string name) override { return arch.symbol_table; } | ||||
|  | ||||
|         riscv_hart_m_p<BASE, FEAT, LOGCAT>& arch; | ||||
|     }; | ||||
| @@ -343,9 +348,6 @@ protected: | ||||
|     int64_t instret_offset{0}; | ||||
|     uint64_t minstret_csr{0}; | ||||
|     reg_t fault_data; | ||||
|     uint64_t tohost = tohost_dflt; | ||||
|     uint64_t fromhost = fromhost_dflt; | ||||
|     bool tohost_lower_written = false; | ||||
|     riscv_instrumentation_if instr_if; | ||||
|  | ||||
|     semihosting_cb_t<reg_t> semihosting_cb; | ||||
| @@ -355,7 +357,6 @@ protected: | ||||
|     using csr_page_type = typename csr_type::page_type; | ||||
|     mem_type mem; | ||||
|     csr_type csr; | ||||
|     std::stringstream uart_buf; | ||||
|     std::unordered_map<reg_t, uint64_t> ptw; | ||||
|     std::unordered_map<uint64_t, uint8_t> atomic_reservation; | ||||
|     std::unordered_map<unsigned, rd_csr_f> csr_rd_cb; | ||||
| @@ -447,7 +448,6 @@ riscv_hart_m_p<BASE, FEAT, LOGCAT>::riscv_hart_m_p(feature_config cfg) | ||||
|     csr[marchid] = traits<BASE>::MARCHID_VAL; | ||||
|     csr[mimpid] = 1; | ||||
|  | ||||
|     uart_buf.str(""); | ||||
|     if(traits<BASE>::FLEN > 0) { | ||||
|         csr_rd_cb[fcsr] = &this_class::read_fcsr; | ||||
|         csr_wr_cb[fcsr] = &this_class::write_fcsr; | ||||
| @@ -573,57 +573,14 @@ riscv_hart_m_p<BASE, FEAT, LOGCAT>::riscv_hart_m_p(feature_config cfg) | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| std::pair<uint64_t, bool> riscv_hart_m_p<BASE, FEAT, LOGCAT>::load_file(std::string name, int type) { | ||||
|     get_sym_table(name); | ||||
|     try { | ||||
|         tohost = symbol_table.at("tohost"); | ||||
|         fromhost = symbol_table.at("fromhost"); | ||||
|     } catch(std::out_of_range& e) { | ||||
|     if(read_elf_file(name, sizeof(reg_t) == 4 ? ELFIO::ELFCLASS32 : ELFIO::ELFCLASS64, | ||||
|                      [this](uint64_t addr, uint64_t size, const uint8_t* const data) -> iss::status { | ||||
|                          return this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, addr, size, | ||||
|                                             data); | ||||
|                      })) { | ||||
|         return std::make_pair(entry_address, true); | ||||
|     } | ||||
|     FILE* fp = fopen(name.c_str(), "r"); | ||||
|     if(fp) { | ||||
|         std::array<char, 5> buf; | ||||
|         auto n = fread(buf.data(), 1, 4, fp); | ||||
|         fclose(fp); | ||||
|         if(n != 4) | ||||
|             throw std::runtime_error("input file has insufficient size"); | ||||
|         buf[4] = 0; | ||||
|         if(strcmp(buf.data() + 1, "ELF") == 0) { | ||||
|             // Create elfio reader | ||||
|             ELFIO::elfio reader; | ||||
|             // Load ELF data | ||||
|             if(!reader.load(name)) | ||||
|                 throw std::runtime_error("could not process elf file"); | ||||
|             // check elf properties | ||||
|             if(reader.get_class() != ELFCLASS32) | ||||
|                 if(sizeof(reg_t) == 4) | ||||
|                     throw std::runtime_error("wrong elf class in file"); | ||||
|             if(reader.get_type() != ET_EXEC) | ||||
|                 throw std::runtime_error("wrong elf type in file"); | ||||
|             if(reader.get_machine() != EM_RISCV) | ||||
|                 throw std::runtime_error("wrong elf machine in file"); | ||||
|             auto entry = reader.get_entry(); | ||||
|             for(const auto pseg : reader.segments) { | ||||
|                 const auto fsize = pseg->get_file_size(); // 0x42c/0x0 | ||||
|                 const auto seg_data = pseg->get_data(); | ||||
|                 const auto type = pseg->get_type(); | ||||
|                 if(type == 1 && fsize > 0) { | ||||
|                     auto res = this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, | ||||
|                                            pseg->get_physical_address(), fsize, reinterpret_cast<const uint8_t* const>(seg_data)); | ||||
|                     if(res != iss::Ok) | ||||
|                         CPPLOG(ERR) << "problem writing " << fsize << "bytes to 0x" << std::hex << pseg->get_physical_address(); | ||||
|                 } | ||||
|             } | ||||
|             for(const auto& sec : reader.sections) { | ||||
|                 if(sec->get_name() == ".tohost") { | ||||
|                     tohost = sec->get_address(); | ||||
|                     fromhost = tohost + 0x40; | ||||
|                 } | ||||
|             } | ||||
|             return std::make_pair(entry, true); | ||||
|         } | ||||
|         throw std::runtime_error(fmt::format("memory load file {} is not a valid elf file", name)); | ||||
|     } | ||||
|     throw std::runtime_error(fmt::format("memory load file not found, check if {} is a valid file", name)); | ||||
|     return std::make_pair(entry_address, false); | ||||
| } | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| @@ -654,7 +611,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read(const address_type type, co | ||||
|     try { | ||||
|         switch(space) { | ||||
|         case traits<BASE>::MEM: { | ||||
|             auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : length; | ||||
|             auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : std::min<unsigned>(length, sizeof(reg_t)); | ||||
|             if(unlikely(is_fetch(access) && (addr & (alignment - 1)))) { | ||||
|                 fault_data = addr; | ||||
|                 if(is_debug(access)) | ||||
| @@ -689,7 +646,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read(const address_type type, co | ||||
|                 } | ||||
|                 return res; | ||||
|             } catch(trap_access& ta) { | ||||
|                 if( (access & access_type::DEBUG) == 0) { | ||||
|                 if((access & access_type::DEBUG) == 0) { | ||||
|                     this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|                     fault_data = ta.addr; | ||||
|                 } | ||||
| @@ -750,7 +707,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write(const address_type type, c | ||||
|                       << std::hex << addr; | ||||
|         break; | ||||
|     default: | ||||
|         CPPLOG(TRACE) << prefix << "write of " << length << " bytes @addr " << addr; | ||||
|         CPPLOG(TRACE) << prefix << "write of " << length << " bytes @addr 0x" << std::hex << addr; | ||||
|     } | ||||
| #endif | ||||
|     try { | ||||
| @@ -764,7 +721,8 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write(const address_type type, c | ||||
|                 return iss::Err; | ||||
|             } | ||||
|             try { | ||||
|                 if(length > 1 && (addr & (length - 1)) && (access & access_type::DEBUG) != access_type::DEBUG) { | ||||
|                 auto alignment = std::min<unsigned>(length, sizeof(reg_t)); | ||||
|                 if(length > 1 && (addr & (alignment - 1)) && !is_debug(access)) { | ||||
|                     this->reg.trap_state = (1UL << 31) | 6 << 16; | ||||
|                     fault_data = addr; | ||||
|                     return iss::Err; | ||||
| @@ -784,7 +742,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write(const address_type type, c | ||||
|                 } else { | ||||
|                     res = write_mem(phys_addr, length, data); | ||||
|                 } | ||||
|                 if(unlikely(res != iss::Ok && (access & access_type::DEBUG) == 0)) { | ||||
|                 if(unlikely(res != iss::Ok && !is_debug(access))) { | ||||
|                     this->reg.trap_state = (1UL << 31) | (7UL << 16); // issue trap 7 (Store/AMO access fault) | ||||
|                     fault_data = addr; | ||||
|                 } | ||||
| @@ -794,38 +752,6 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write(const address_type type, c | ||||
|                 fault_data = ta.addr; | ||||
|                 return iss::Err; | ||||
|             } | ||||
|  | ||||
|             if((addr + length) > mem.size()) | ||||
|                 return iss::Err; | ||||
|             switch(addr) { | ||||
|             case 0x10013000: // UART0 base, TXFIFO reg | ||||
|             case 0x10023000: // UART1 base, TXFIFO reg | ||||
|                 uart_buf << (char)data[0]; | ||||
|                 if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|                     std::cout << uart_buf.str(); | ||||
|                     uart_buf.str(""); | ||||
|                 } | ||||
|                 return iss::Ok; | ||||
|             case 0x10008000: { // HFROSC base, hfrosccfg reg | ||||
|                 auto& p = mem(addr / mem.page_size); | ||||
|                 auto offs = addr & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 if(x & 0x40) | ||||
|                     x |= 0x80; // hfroscrdy = 1 if hfroscen==1 | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|             case 0x10008008: { // HFROSC base, pllcfg reg | ||||
|                 auto& p = mem(addr / mem.page_size); | ||||
|                 auto offs = addr & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 x |= 0x80; // set pll lock upon writing | ||||
|                 return iss::Ok; | ||||
|             } break; | ||||
|             default: { | ||||
|             } | ||||
|             } | ||||
|         } break; | ||||
|         case traits<BASE>::CSR: { | ||||
|             if(length != sizeof(reg_t)) | ||||
| @@ -908,7 +834,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_plain(unsigned addr, reg_t | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_cycle(unsigned addr, reg_t& val) { | ||||
|     auto cycle_val = this->reg.icount + cycle_offset; | ||||
|     auto cycle_val = this->reg.cycle + cycle_offset; | ||||
|     if(addr == mcycle) { | ||||
|         val = static_cast<reg_t>(cycle_val); | ||||
|     } else if(addr == mcycleh) { | ||||
| @@ -928,7 +854,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_cycle(unsigned addr, reg_t | ||||
|             mcycle_csr = (static_cast<uint64_t>(val) << 32) + (mcycle_csr & 0xffffffff); | ||||
|         } | ||||
|     } | ||||
|     cycle_offset = mcycle_csr - this->reg.icount; // TODO: relying on wrap-around | ||||
|     cycle_offset = mcycle_csr - this->reg.cycle; // TODO: relying on wrap-around | ||||
|     return iss::Ok; | ||||
| } | ||||
|  | ||||
| @@ -959,7 +885,7 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_instret(unsigned addr, reg | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_time(unsigned addr, reg_t& val) { | ||||
|     uint64_t time_val = this->reg.icount / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     uint64_t time_val = this->reg.cycle / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     if(addr == time) { | ||||
|         val = static_cast<reg_t>(time_val); | ||||
|     } else if(addr == timeh) { | ||||
| @@ -1138,59 +1064,51 @@ iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::read_mem(phys_addr_t paddr, unsi | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_m_p<BASE, FEAT, LOGCAT>::write_mem(phys_addr_t paddr, unsigned length, const uint8_t* const data) { | ||||
|     switch(paddr.val) { | ||||
|     // TODO remove UART, Peripherals should not be part of the ISS | ||||
|     case 0xFFFF0000: // UART0 base, TXFIFO reg | ||||
|         if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|             CPPLOG(INFO) << "UART" << ((paddr.val >> 12) & 0x3) << " send '" << uart_buf.str() << "'"; | ||||
|             uart_buf.str(""); | ||||
|         } else if(((char)data[0]) != '\r') | ||||
|             uart_buf << (char)data[0]; | ||||
|         break; | ||||
|     default: { | ||||
|         mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|         std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|         // tohost handling in case of riscv-test | ||||
|         if(paddr.access && iss::access_type::FUNC) { | ||||
|             auto tohost_upper = | ||||
|                 (traits<BASE>::XLEN == 32 && paddr.val == (tohost + 4)) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             auto tohost_lower = (traits<BASE>::XLEN == 32 && paddr.val == tohost) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             if(tohost_lower || tohost_upper) { | ||||
|                 uint64_t hostvar = *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)); | ||||
|                 // in case of 32 bit system, two writes to tohost are needed, only evaluate on the second (high) write | ||||
|                 if(tohost_upper && (tohost_lower || tohost_lower_written)) { | ||||
|                     switch(hostvar >> 48) { | ||||
|                     case 0: | ||||
|                         if(hostvar != 0x1) { | ||||
|                             CPPLOG(FATAL) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                           << "), stopping simulation"; | ||||
|                         } else { | ||||
|                             CPPLOG(INFO) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                          << "), stopping simulation"; | ||||
|                         } | ||||
|                         this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                         this->interrupt_sim = hostvar; | ||||
|                         break; | ||||
|                     case 0x0101: { | ||||
|                         char c = static_cast<char>(hostvar & 0xff); | ||||
|                         if(c == '\n' || c == 0) { | ||||
|                             CPPLOG(INFO) << "tohost send '" << uart_buf.str() << "'"; | ||||
|                             uart_buf.str(""); | ||||
|                         } else | ||||
|                             uart_buf << c; | ||||
|                     } break; | ||||
|                     default: | ||||
|                         break; | ||||
|                     } | ||||
|                     tohost_lower_written = false; | ||||
|                 } else if(tohost_lower) | ||||
|                     tohost_lower_written = true; | ||||
|             } else if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|                 uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|                 *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|     mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|     std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|     // tohost handling in case of riscv-test | ||||
|     // according to https://github.com/riscv-software-src/riscv-isa-sim/issues/364#issuecomment-607657754: | ||||
|     if(paddr.access && iss::access_type::FUNC) { | ||||
|         if(paddr.val == tohost) { | ||||
|             reg_t cur_data = *reinterpret_cast<const reg_t*>(data); | ||||
|             // Extract Device (bits 63:56) | ||||
|             uint8_t device = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 56) & 0xFF; | ||||
|             // Extract Command (bits 55:48) | ||||
|             uint8_t command = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 48) & 0xFF; | ||||
|             // Extract payload (bits 47:0) | ||||
|             uint64_t payload_addr = cur_data & 0xFFFFFFFFFFFFULL; | ||||
|             if(payload_addr & 1) { | ||||
|                 CPPLOG(FATAL) << "tohost value is 0x" << std::hex << payload_addr << std::dec << " (" << payload_addr | ||||
|                               << "), stopping simulation"; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } else if(device == 0 && command == 0) { | ||||
|                 std::array<uint64_t, 8> loaded_payload; | ||||
|                 if(read(address_type::PHYSICAL, access_type::DEBUG_READ, traits<BASE>::MEM, payload_addr, 8 * sizeof(uint64_t), | ||||
|                         reinterpret_cast<uint8_t*>(loaded_payload.data())) == iss::Err) | ||||
|                     CPPLOG(ERR) << "Syscall read went wrong"; | ||||
|                 uint64_t syscall_num = loaded_payload.at(0); | ||||
|                 if(syscall_num == 64) { // SYS_WRITE | ||||
|                     return execute_sys_write(this, loaded_payload, traits<BASE>::MEM); | ||||
|                 } else { | ||||
|                     CPPLOG(ERR) << "tohost syscall with number 0x" << std::hex << syscall_num << std::dec << " (" << syscall_num | ||||
|                                 << ") not implemented"; | ||||
|                     this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                     this->interrupt_sim = payload_addr; | ||||
|                     return iss::Ok; | ||||
|                 } | ||||
|             } else { | ||||
|                 CPPLOG(ERR) << "tohost functionality not implemented for device " << device << " and command " << command; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|         if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|             uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|             *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|         } | ||||
|     } | ||||
|     return iss::Ok; | ||||
| } | ||||
|   | ||||
| @@ -41,6 +41,11 @@ | ||||
| #include "iss/vm_if.h" | ||||
| #include "iss/vm_types.h" | ||||
| #include "riscv_hart_common.h" | ||||
| #include "util/logging.h" | ||||
| #include <algorithm> | ||||
| #include <cstdint> | ||||
| #include <elfio/elf_types.hpp> | ||||
| #include <limits> | ||||
| #include <stdexcept> | ||||
| #ifndef FMT_HEADER_ONLY | ||||
| #define FMT_HEADER_ONLY | ||||
| @@ -328,7 +333,7 @@ public: | ||||
|  | ||||
|     void disass_output(uint64_t pc, const std::string instr) override { | ||||
|         CLOG(INFO, disass) << fmt::format("0x{:016x}    {:40} [p:{};s:0x{:x};c:{}]", pc, instr, lvl[this->reg.PRIV], (reg_t)state.mstatus, | ||||
|                                           this->reg.icount + cycle_offset); | ||||
|                                           this->reg.cycle + cycle_offset); | ||||
|     }; | ||||
|  | ||||
|     iss::instrumentation_if* get_instrumentation_if() override { return &instr_if; } | ||||
| @@ -361,7 +366,7 @@ protected: | ||||
|  | ||||
|         uint64_t get_pendig_traps() override { return arch.reg.trap_state; } | ||||
|  | ||||
|         uint64_t get_total_cycles() override { return arch.reg.icount + arch.cycle_offset; } | ||||
|         uint64_t get_total_cycles() override { return arch.reg.cycle + arch.cycle_offset; } | ||||
|  | ||||
|         void update_last_instr_cycles(unsigned cycles) override { arch.cycle_offset += cycles - 1; } | ||||
|  | ||||
| @@ -371,7 +376,7 @@ protected: | ||||
|  | ||||
|         unsigned get_reg_size(unsigned num) override { return traits<BASE>::reg_bit_widths[num]; } | ||||
|  | ||||
|         std::unordered_map<std::string, uint64_t> get_symbol_table(std::string name) override { return arch.get_sym_table(name); } | ||||
|         std::unordered_map<std::string, uint64_t> const& get_symbol_table(std::string name) override { return arch.symbol_table; } | ||||
|  | ||||
|         riscv_hart_msu_vp<BASE>& arch; | ||||
|     }; | ||||
| @@ -393,9 +398,6 @@ protected: | ||||
|     uint64_t minstret_csr{0}; | ||||
|     reg_t fault_data; | ||||
|     std::array<vm_info, 2> vm; | ||||
|     uint64_t tohost = tohost_dflt; | ||||
|     uint64_t fromhost = fromhost_dflt; | ||||
|     bool tohost_lower_written = false; | ||||
|     riscv_instrumentation_if instr_if; | ||||
|  | ||||
|     std::function<void(arch_if*, reg_t, reg_t)> semihosting_cb; | ||||
| @@ -406,7 +408,6 @@ protected: | ||||
|     mem_type mem; | ||||
|     csr_type csr; | ||||
|     void update_vm_info(); | ||||
|     std::stringstream uart_buf; | ||||
|     std::unordered_map<reg_t, uint64_t> ptw; | ||||
|     std::unordered_map<uint64_t, uint8_t> atomic_reservation; | ||||
|     std::unordered_map<unsigned, rd_csr_f> csr_rd_cb; | ||||
| @@ -461,7 +462,6 @@ riscv_hart_msu_vp<BASE>::riscv_hart_msu_vp() | ||||
|     csr[marchid] = traits<BASE>::MARCHID_VAL; | ||||
|     csr[mimpid] = 1; | ||||
|  | ||||
|     uart_buf.str(""); | ||||
|     for(unsigned addr = mhpmcounter3; addr <= mhpmcounter31; ++addr) { | ||||
|         csr_rd_cb[addr] = &this_class::read_null; | ||||
|         csr_wr_cb[addr] = &this_class::write_csr_reg; | ||||
| @@ -557,71 +557,14 @@ riscv_hart_msu_vp<BASE>::riscv_hart_msu_vp() | ||||
| } | ||||
|  | ||||
| template <typename BASE> std::pair<uint64_t, bool> riscv_hart_msu_vp<BASE>::load_file(std::string name, int type) { | ||||
|     FILE* fp = fopen(name.c_str(), "r"); | ||||
|     if(fp) { | ||||
|         std::array<char, 5> buf; | ||||
|         auto n = fread(buf.data(), 1, 4, fp); | ||||
|         fclose(fp); | ||||
|         if(n != 4) | ||||
|             throw std::runtime_error("input file has insufficient size"); | ||||
|         buf[4] = 0; | ||||
|         if(strcmp(buf.data() + 1, "ELF") == 0) { | ||||
|             // Create elfio reader | ||||
|             ELFIO::elfio reader; | ||||
|             // Load ELF data | ||||
|             if(!reader.load(name)) | ||||
|                 throw std::runtime_error("could not process elf file"); | ||||
|             // check elf properties | ||||
|             if(reader.get_class() != ELFCLASS32) | ||||
|                 if(sizeof(reg_t) == 4) | ||||
|                     throw std::runtime_error("wrong elf class in file"); | ||||
|             if(reader.get_type() != ET_EXEC) | ||||
|                 throw std::runtime_error("wrong elf type in file"); | ||||
|             if(reader.get_machine() != EM_RISCV) | ||||
|                 throw std::runtime_error("wrong elf machine in file"); | ||||
|             auto entry = reader.get_entry(); | ||||
|             for(const auto pseg : reader.segments) { | ||||
|                 const auto fsize = pseg->get_file_size(); // 0x42c/0x0 | ||||
|                 const auto seg_data = pseg->get_data(); | ||||
|                 const auto type = pseg->get_type(); | ||||
|                 if(type == 1 && fsize > 0) { | ||||
|                     auto res = this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, | ||||
|                                            pseg->get_physical_address(), fsize, reinterpret_cast<const uint8_t* const>(seg_data)); | ||||
|                     if(res != iss::Ok) | ||||
|                         CPPLOG(ERR) << "problem writing " << fsize << "bytes to 0x" << std::hex << pseg->get_physical_address(); | ||||
|                 } | ||||
|             } | ||||
|             for(const auto sec : reader.sections) { | ||||
|                 if(sec->get_name() == ".symtab") { | ||||
|                     if(SHT_SYMTAB == sec->get_type() || SHT_DYNSYM == sec->get_type()) { | ||||
|                         ELFIO::symbol_section_accessor symbols(reader, sec); | ||||
|                         auto sym_no = symbols.get_symbols_num(); | ||||
|                         std::string name; | ||||
|                         ELFIO::Elf64_Addr value = 0; | ||||
|                         ELFIO::Elf_Xword size = 0; | ||||
|                         unsigned char bind = 0; | ||||
|                         unsigned char type = 0; | ||||
|                         ELFIO::Elf_Half section = 0; | ||||
|                         unsigned char other = 0; | ||||
|                         for(auto i = 0U; i < sym_no; ++i) { | ||||
|                             symbols.get_symbol(i, name, value, size, bind, type, section, other); | ||||
|                             if(name == "tohost") { | ||||
|                                 tohost = value; | ||||
|                             } else if(name == "fromhost") { | ||||
|                                 fromhost = value; | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 } else if(sec->get_name() == ".tohost") { | ||||
|                     tohost = sec->get_address(); | ||||
|                     fromhost = tohost + 0x40; | ||||
|                 } | ||||
|             } | ||||
|             return std::make_pair(entry, true); | ||||
|         } | ||||
|         throw std::runtime_error(fmt::format("memory load file {} is not a valid elf file", name)); | ||||
|     if(read_elf_file(name, sizeof(reg_t) == 4 ? ELFIO::ELFCLASS32 : ELFIO::ELFCLASS64, | ||||
|                      [this](uint64_t addr, uint64_t size, const uint8_t* const data) -> iss::status { | ||||
|                          return this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, addr, size, | ||||
|                                             data); | ||||
|                      })) { | ||||
|         return std::make_pair(entry_address, true); | ||||
|     } | ||||
|     throw std::runtime_error(fmt::format("memory load file not found, check if {} is a valid file", name)); | ||||
|     return std::make_pair(entry_address, false); | ||||
| } | ||||
|  | ||||
| template <typename BASE> | ||||
| @@ -639,7 +582,7 @@ iss::status riscv_hart_msu_vp<BASE>::read(const address_type type, const access_ | ||||
|     try { | ||||
|         switch(space) { | ||||
|         case traits<BASE>::MEM: { | ||||
|             auto alignment = is_fetch(access) ? (traits<BASE>::MISA_VAL & 0x100 ? 2 : 4) : length; | ||||
|             auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : std::min<unsigned>(length, sizeof(reg_t)); | ||||
|             if(unlikely(is_fetch(access) && (addr & (alignment - 1)))) { | ||||
|                 fault_data = addr; | ||||
|                 if(access && iss::access_type::DEBUG) | ||||
| @@ -671,8 +614,10 @@ iss::status riscv_hart_msu_vp<BASE>::read(const address_type type, const access_ | ||||
|                 } | ||||
|                 return res; | ||||
|             } catch(trap_access& ta) { | ||||
|                 this->reg.trap_state = (1 << 31) | ta.id; | ||||
|                 fault_data = ta.addr; | ||||
|                 if((access & access_type::DEBUG) == 0) { | ||||
|                     this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|                     fault_data = ta.addr; | ||||
|                 } | ||||
|                 return iss::Err; | ||||
|             } | ||||
|         } break; | ||||
| @@ -710,8 +655,10 @@ iss::status riscv_hart_msu_vp<BASE>::read(const address_type type, const access_ | ||||
|         } | ||||
|         return iss::Ok; | ||||
|     } catch(trap_access& ta) { | ||||
|         this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|         fault_data = ta.addr; | ||||
|         if((access & access_type::DEBUG) == 0) { | ||||
|             this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|             fault_data = ta.addr; | ||||
|         } | ||||
|         return iss::Err; | ||||
|     } | ||||
| } | ||||
| @@ -754,6 +701,7 @@ iss::status riscv_hart_msu_vp<BASE>::write(const address_type type, const access | ||||
|             } | ||||
|             phys_addr_t paddr = BASE::v2p(iss::addr_t{access, type, space, addr}); | ||||
|             try { | ||||
|                 // TODO: There is no check for alignment | ||||
|                 if(unlikely((addr & ~PGMASK) != ((addr + length - 1) & ~PGMASK))) { // we may cross a page boundary | ||||
|                     vm_info vm = hart_state_type::decode_vm_info(this->reg.PRIV, state.satp); | ||||
|                     if(vm.levels != 0) { // VM is active | ||||
| @@ -776,40 +724,6 @@ iss::status riscv_hart_msu_vp<BASE>::write(const address_type type, const access | ||||
|                 fault_data = ta.addr; | ||||
|                 return iss::Err; | ||||
|             } | ||||
|  | ||||
|             if((paddr.val + length) > mem.size()) | ||||
|                 return iss::Err; | ||||
|             switch(paddr.val) { | ||||
|             case 0x10013000: // UART0 base, TXFIFO reg | ||||
|             case 0x10023000: // UART1 base, TXFIFO reg | ||||
|                 uart_buf << (char)data[0]; | ||||
|                 if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|                     // CPPLOG(INFO)<<"UART"<<((paddr.val>>16)&0x3)<<" send | ||||
|                     // '"<<uart_buf.str()<<"'"; | ||||
|                     std::cout << uart_buf.str(); | ||||
|                     uart_buf.str(""); | ||||
|                 } | ||||
|                 return iss::Ok; | ||||
|             case 0x10008000: { // HFROSC base, hfrosccfg reg | ||||
|                 auto& p = mem(paddr.val / mem.page_size); | ||||
|                 auto offs = paddr.val & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 if(x & 0x40) | ||||
|                     x |= 0x80; // hfroscrdy = 1 if hfroscen==1 | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|             case 0x10008008: { // HFROSC base, pllcfg reg | ||||
|                 auto& p = mem(paddr.val / mem.page_size); | ||||
|                 auto offs = paddr.val & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 x |= 0x80; // set pll lock upon writing | ||||
|                 return iss::Ok; | ||||
|             } break; | ||||
|             default: { | ||||
|             } | ||||
|             } | ||||
|         } break; | ||||
|         case traits<BASE>::CSR: { | ||||
|             if(length != sizeof(reg_t)) | ||||
| @@ -841,8 +755,10 @@ iss::status riscv_hart_msu_vp<BASE>::write(const address_type type, const access | ||||
|         } | ||||
|         return iss::Ok; | ||||
|     } catch(trap_access& ta) { | ||||
|         this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|         fault_data = ta.addr; | ||||
|         if((access & access_type::DEBUG) == 0) { | ||||
|             this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|             fault_data = ta.addr; | ||||
|         } | ||||
|         return iss::Err; | ||||
|     } | ||||
| } | ||||
| @@ -889,7 +805,7 @@ template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::write_reg(unsigned | ||||
| } | ||||
|  | ||||
| template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::read_cycle(unsigned addr, reg_t& val) { | ||||
|     auto cycle_val = this->reg.icount + cycle_offset; | ||||
|     auto cycle_val = this->reg.cycle + cycle_offset; | ||||
|     if(addr == mcycle) { | ||||
|         val = static_cast<reg_t>(cycle_val); | ||||
|     } else if(addr == mcycleh) { | ||||
| @@ -910,7 +826,7 @@ template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::write_cycle(unsign | ||||
|             mcycle_csr = (static_cast<uint64_t>(val) << 32) + (mcycle_csr & 0xffffffff); | ||||
|         } | ||||
|     } | ||||
|     cycle_offset = mcycle_csr - this->reg.icount; // TODO: relying on wrap-around | ||||
|     cycle_offset = mcycle_csr - this->reg.cycle; // TODO: relying on wrap-around | ||||
|     return iss::Ok; | ||||
| } | ||||
|  | ||||
| @@ -938,7 +854,7 @@ template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::write_instret(unsi | ||||
| } | ||||
|  | ||||
| template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::read_time(unsigned addr, reg_t& val) { | ||||
|     uint64_t time_val = this->reg.icount / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     uint64_t time_val = this->reg.cycle / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     if(addr == time) { | ||||
|         val = static_cast<reg_t>(time_val); | ||||
|     } else if(addr == timeh) { | ||||
| @@ -1077,61 +993,51 @@ template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::read_mem(phys_addr | ||||
| } | ||||
|  | ||||
| template <typename BASE> iss::status riscv_hart_msu_vp<BASE>::write_mem(phys_addr_t paddr, unsigned length, const uint8_t* const data) { | ||||
|     switch(paddr.val) { | ||||
|     case 0xFFFF0000: // UART0 base, TXFIFO reg | ||||
|         if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|             CPPLOG(INFO) << "UART" << ((paddr.val >> 12) & 0x3) << " send '" << uart_buf.str() << "'"; | ||||
|             uart_buf.str(""); | ||||
|         } else if(((char)data[0]) != '\r') | ||||
|             uart_buf << (char)data[0]; | ||||
|         break; | ||||
|     default: { | ||||
|         mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|         std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|         // tohost handling in case of riscv-test | ||||
|         if(paddr.access && iss::access_type::FUNC) { | ||||
|             auto tohost_upper = | ||||
|                 (traits<BASE>::XLEN == 32 && paddr.val == (tohost + 4)) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             auto tohost_lower = (traits<BASE>::XLEN == 32 && paddr.val == tohost) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             if(tohost_lower || tohost_upper) { | ||||
|                 uint64_t hostvar = *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)); | ||||
|                 // in case of 32 bit system, two writes to tohost are needed, only evaluate on the second (high) write | ||||
|                 if(tohost_upper && (tohost_lower || tohost_lower_written)) { | ||||
|                     switch(hostvar >> 48) { | ||||
|                     case 0: | ||||
|                         if(hostvar != 0x1) { | ||||
|                             CPPLOG(FATAL) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                           << "), stopping simulation"; | ||||
|                         } else { | ||||
|                             CPPLOG(INFO) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                          << "), stopping simulation"; | ||||
|                         } | ||||
|                         this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                         this->interrupt_sim = hostvar; | ||||
| #ifndef WITH_TCC | ||||
|                         throw(iss::simulation_stopped(hostvar)); | ||||
| #endif | ||||
|                         break; | ||||
|                     case 0x0101: { | ||||
|                         char c = static_cast<char>(hostvar & 0xff); | ||||
|                         if(c == '\n' || c == 0) { | ||||
|                             CPPLOG(INFO) << "tohost send '" << uart_buf.str() << "'"; | ||||
|                             uart_buf.str(""); | ||||
|                         } else | ||||
|                             uart_buf << c; | ||||
|                     } break; | ||||
|                     default: | ||||
|                         break; | ||||
|                     } | ||||
|                     tohost_lower_written = false; | ||||
|                 } else if(tohost_lower) | ||||
|                     tohost_lower_written = true; | ||||
|             } else if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|                 uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|                 *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|     mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|     std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|     // tohost handling in case of riscv-test | ||||
|     // according to https://github.com/riscv-software-src/riscv-isa-sim/issues/364#issuecomment-607657754: | ||||
|     if(paddr.access && iss::access_type::FUNC) { | ||||
|         if(paddr.val == tohost) { | ||||
|             reg_t cur_data = *reinterpret_cast<const reg_t*>(data); | ||||
|             // Extract Device (bits 63:56) | ||||
|             uint8_t device = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 56) & 0xFF; | ||||
|             // Extract Command (bits 55:48) | ||||
|             uint8_t command = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 48) & 0xFF; | ||||
|             // Extract payload (bits 47:0) | ||||
|             uint64_t payload_addr = cur_data & 0xFFFFFFFFFFFFULL; | ||||
|             if(payload_addr & 1) { | ||||
|                 CPPLOG(FATAL) << "tohost value is 0x" << std::hex << payload_addr << std::dec << " (" << payload_addr | ||||
|                               << "), stopping simulation"; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } else if(device == 0 && command == 0) { | ||||
|                 std::array<uint64_t, 8> loaded_payload; | ||||
|                 if(read(address_type::PHYSICAL, access_type::DEBUG_READ, traits<BASE>::MEM, payload_addr, 8 * sizeof(uint64_t), | ||||
|                         reinterpret_cast<uint8_t*>(loaded_payload.data())) == iss::Err) | ||||
|                     CPPLOG(ERR) << "Syscall read went wrong"; | ||||
|                 uint64_t syscall_num = loaded_payload.at(0); | ||||
|                 if(syscall_num == 64) { // SYS_WRITE | ||||
|                     return execute_sys_write(this, loaded_payload, traits<BASE>::MEM); | ||||
|                 } else { | ||||
|                     CPPLOG(ERR) << "tohost syscall with number 0x" << std::hex << syscall_num << std::dec << " (" << syscall_num | ||||
|                                 << ") not implemented"; | ||||
|                     this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                     this->interrupt_sim = payload_addr; | ||||
|                     return iss::Ok; | ||||
|                 } | ||||
|             } else { | ||||
|                 CPPLOG(ERR) << "tohost functionality not implemented for device " << device << " and command " << command; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|         if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|             uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|             *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|         } | ||||
|     } | ||||
|     return iss::Ok; | ||||
| } | ||||
|   | ||||
| @@ -41,6 +41,11 @@ | ||||
| #include "iss/vm_if.h" | ||||
| #include "iss/vm_types.h" | ||||
| #include "riscv_hart_common.h" | ||||
| #include "util/logging.h" | ||||
| #include <algorithm> | ||||
| #include <cstdint> | ||||
| #include <elfio/elf_types.hpp> | ||||
| #include <limits> | ||||
| #include <stdexcept> | ||||
| #ifndef FMT_HEADER_ONLY | ||||
| #define FMT_HEADER_ONLY | ||||
| @@ -305,7 +310,7 @@ public: | ||||
|  | ||||
|     void disass_output(uint64_t pc, const std::string instr) override { | ||||
|         NSCLOG(INFO, LOGCAT) << fmt::format("0x{:016x}    {:40} [p:{};s:0x{:x};c:{}]", pc, instr, lvl[this->reg.PRIV], (reg_t)state.mstatus, | ||||
|                                             this->reg.icount + cycle_offset); | ||||
|                                             this->reg.cycle + cycle_offset); | ||||
|     }; | ||||
|  | ||||
|     iss::instrumentation_if* get_instrumentation_if() override { return &instr_if; } | ||||
| @@ -338,7 +343,7 @@ protected: | ||||
|  | ||||
|         uint64_t get_pendig_traps() override { return arch.reg.trap_state; } | ||||
|  | ||||
|         uint64_t get_total_cycles() override { return arch.reg.icount + arch.cycle_offset; } | ||||
|         uint64_t get_total_cycles() override { return arch.reg.cycle + arch.cycle_offset; } | ||||
|  | ||||
|         void update_last_instr_cycles(unsigned cycles) override { arch.cycle_offset += cycles - 1; } | ||||
|  | ||||
| @@ -348,7 +353,7 @@ protected: | ||||
|  | ||||
|         unsigned get_reg_size(unsigned num) override { return traits<BASE>::reg_bit_widths[num]; } | ||||
|  | ||||
|         std::unordered_map<std::string, uint64_t> get_symbol_table(std::string name) override { return arch.get_sym_table(name); } | ||||
|         std::unordered_map<std::string, uint64_t> const& get_symbol_table(std::string name) override { return arch.symbol_table; } | ||||
|  | ||||
|         riscv_hart_mu_p<BASE, FEAT, LOGCAT>& arch; | ||||
|     }; | ||||
| @@ -370,9 +375,6 @@ protected: | ||||
|     int64_t instret_offset{0}; | ||||
|     uint64_t minstret_csr{0}; | ||||
|     reg_t fault_data; | ||||
|     uint64_t tohost = tohost_dflt; | ||||
|     uint64_t fromhost = fromhost_dflt; | ||||
|     bool tohost_lower_written = false; | ||||
|     riscv_instrumentation_if instr_if; | ||||
|  | ||||
|     semihosting_cb_t<reg_t> semihosting_cb; | ||||
| @@ -382,7 +384,6 @@ protected: | ||||
|     using csr_page_type = typename csr_type::page_type; | ||||
|     mem_type mem; | ||||
|     csr_type csr; | ||||
|     std::stringstream uart_buf; | ||||
|     std::unordered_map<reg_t, uint64_t> ptw; | ||||
|     std::unordered_map<uint64_t, uint8_t> atomic_reservation; | ||||
|     std::unordered_map<unsigned, rd_csr_f> csr_rd_cb; | ||||
| @@ -477,7 +478,6 @@ riscv_hart_mu_p<BASE, FEAT, LOGCAT>::riscv_hart_mu_p(feature_config cfg) | ||||
|     csr[marchid] = traits<BASE>::MARCHID_VAL; | ||||
|     csr[mimpid] = 1; | ||||
|  | ||||
|     uart_buf.str(""); | ||||
|     if(traits<BASE>::FLEN > 0) { | ||||
|         csr_rd_cb[fcsr] = &this_class::read_fcsr; | ||||
|         csr_wr_cb[fcsr] = &this_class::write_fcsr; | ||||
| @@ -651,71 +651,14 @@ riscv_hart_mu_p<BASE, FEAT, LOGCAT>::riscv_hart_mu_p(feature_config cfg) | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| std::pair<uint64_t, bool> riscv_hart_mu_p<BASE, FEAT, LOGCAT>::load_file(std::string name, int type) { | ||||
|     FILE* fp = fopen(name.c_str(), "r"); | ||||
|     if(fp) { | ||||
|         std::array<char, 5> buf; | ||||
|         auto n = fread(buf.data(), 1, 4, fp); | ||||
|         fclose(fp); | ||||
|         if(n != 4) | ||||
|             throw std::runtime_error("input file has insufficient size"); | ||||
|         buf[4] = 0; | ||||
|         if(strcmp(buf.data() + 1, "ELF") == 0) { | ||||
|             // Create elfio reader | ||||
|             ELFIO::elfio reader; | ||||
|             // Load ELF data | ||||
|             if(!reader.load(name)) | ||||
|                 throw std::runtime_error("could not process elf file"); | ||||
|             // check elf properties | ||||
|             if(reader.get_class() != ELFCLASS32) | ||||
|                 if(sizeof(reg_t) == 4) | ||||
|                     throw std::runtime_error("wrong elf class in file"); | ||||
|             if(reader.get_type() != ET_EXEC) | ||||
|                 throw std::runtime_error("wrong elf type in file"); | ||||
|             if(reader.get_machine() != EM_RISCV) | ||||
|                 throw std::runtime_error("wrong elf machine in file"); | ||||
|             auto entry = reader.get_entry(); | ||||
|             for(const auto pseg : reader.segments) { | ||||
|                 const auto fsize = pseg->get_file_size(); // 0x42c/0x0 | ||||
|                 const auto seg_data = pseg->get_data(); | ||||
|                 const auto type = pseg->get_type(); | ||||
|                 if(type == 1 && fsize > 0) { | ||||
|                     auto res = this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, | ||||
|                                            pseg->get_physical_address(), fsize, reinterpret_cast<const uint8_t* const>(seg_data)); | ||||
|                     if(res != iss::Ok) | ||||
|                         CPPLOG(ERR) << "problem writing " << fsize << "bytes to 0x" << std::hex << pseg->get_physical_address(); | ||||
|                 } | ||||
|             } | ||||
|             for(const auto sec : reader.sections) { | ||||
|                 if(sec->get_name() == ".symtab") { | ||||
|                     if(SHT_SYMTAB == sec->get_type() || SHT_DYNSYM == sec->get_type()) { | ||||
|                         ELFIO::symbol_section_accessor symbols(reader, sec); | ||||
|                         auto sym_no = symbols.get_symbols_num(); | ||||
|                         std::string name; | ||||
|                         ELFIO::Elf64_Addr value = 0; | ||||
|                         ELFIO::Elf_Xword size = 0; | ||||
|                         unsigned char bind = 0; | ||||
|                         unsigned char type = 0; | ||||
|                         ELFIO::Elf_Half section = 0; | ||||
|                         unsigned char other = 0; | ||||
|                         for(auto i = 0U; i < sym_no; ++i) { | ||||
|                             symbols.get_symbol(i, name, value, size, bind, type, section, other); | ||||
|                             if(name == "tohost") { | ||||
|                                 tohost = value; | ||||
|                             } else if(name == "fromhost") { | ||||
|                                 fromhost = value; | ||||
|                             } | ||||
|                         } | ||||
|                     } | ||||
|                 } else if(sec->get_name() == ".tohost") { | ||||
|                     tohost = sec->get_address(); | ||||
|                     fromhost = tohost + 0x40; | ||||
|                 } | ||||
|             } | ||||
|             return std::make_pair(entry, true); | ||||
|         } | ||||
|         throw std::runtime_error(fmt::format("memory load file {} is not a valid elf file", name)); | ||||
|     if(read_elf_file(name, sizeof(reg_t) == 4 ? ELFIO::ELFCLASS32 : ELFIO::ELFCLASS64, | ||||
|                      [this](uint64_t addr, uint64_t size, const uint8_t* const data) -> iss::status { | ||||
|                          return this->write(iss::address_type::PHYSICAL, iss::access_type::DEBUG_WRITE, traits<BASE>::MEM, addr, size, | ||||
|                                             data); | ||||
|                      })) { | ||||
|         return std::make_pair(entry_address, true); | ||||
|     } | ||||
|     throw std::runtime_error(fmt::format("memory load file not found, check if {} is a valid file", name)); | ||||
|     return std::make_pair(entry_address, false); | ||||
| } | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| @@ -842,7 +785,7 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read(const address_type type, c | ||||
|                     return iss::Err; | ||||
|                 } | ||||
|             } | ||||
|             auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : length; | ||||
|             auto alignment = is_fetch(access) ? (has_compressed() ? 2 : 4) : std::min<unsigned>(length, sizeof(reg_t)); | ||||
|             if(unlikely(is_fetch(access) && (addr & (alignment - 1)))) { | ||||
|                 fault_data = addr; | ||||
|                 if(is_debug(access)) | ||||
| @@ -877,8 +820,10 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read(const address_type type, c | ||||
|                 } | ||||
|                 return res; | ||||
|             } catch(trap_access& ta) { | ||||
|                 this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|                 fault_data = ta.addr; | ||||
|                 if((access & access_type::DEBUG) == 0) { | ||||
|                     this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|                     fault_data = ta.addr; | ||||
|                 } | ||||
|                 return iss::Err; | ||||
|             } | ||||
|         } break; | ||||
| @@ -905,8 +850,10 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read(const address_type type, c | ||||
|         } | ||||
|         return iss::Ok; | ||||
|     } catch(trap_access& ta) { | ||||
|         this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|         fault_data = ta.addr; | ||||
|         if((access & access_type::DEBUG) == 0) { | ||||
|             this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|             fault_data = ta.addr; | ||||
|         } | ||||
|         return iss::Err; | ||||
|     } | ||||
| } | ||||
| @@ -957,7 +904,8 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write(const address_type type, | ||||
|                 return iss::Err; | ||||
|             } | ||||
|             try { | ||||
|                 if(length > 1 && (addr & (length - 1)) && (access & access_type::DEBUG) != access_type::DEBUG) { | ||||
|                 auto alignment = std::min<unsigned>(length, sizeof(reg_t)); | ||||
|                 if(length > 1 && (addr & (alignment - 1)) && !is_debug(access)) { | ||||
|                     this->reg.trap_state = (1UL << 31) | 6 << 16; | ||||
|                     fault_data = addr; | ||||
|                     return iss::Err; | ||||
| @@ -987,38 +935,6 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write(const address_type type, | ||||
|                 fault_data = ta.addr; | ||||
|                 return iss::Err; | ||||
|             } | ||||
|  | ||||
|             if((addr + length) > mem.size()) | ||||
|                 return iss::Err; | ||||
|             switch(addr) { | ||||
|             case 0x10013000: // UART0 base, TXFIFO reg | ||||
|             case 0x10023000: // UART1 base, TXFIFO reg | ||||
|                 uart_buf << (char)data[0]; | ||||
|                 if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|                     std::cout << uart_buf.str(); | ||||
|                     uart_buf.str(""); | ||||
|                 } | ||||
|                 return iss::Ok; | ||||
|             case 0x10008000: { // HFROSC base, hfrosccfg reg | ||||
|                 auto& p = mem(addr / mem.page_size); | ||||
|                 auto offs = addr & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 if(x & 0x40) | ||||
|                     x |= 0x80; // hfroscrdy = 1 if hfroscen==1 | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|             case 0x10008008: { // HFROSC base, pllcfg reg | ||||
|                 auto& p = mem(addr / mem.page_size); | ||||
|                 auto offs = addr & mem.page_addr_mask; | ||||
|                 std::copy(data, data + length, p.data() + offs); | ||||
|                 auto& x = *(p.data() + offs + 3); | ||||
|                 x |= 0x80; // set pll lock upon writing | ||||
|                 return iss::Ok; | ||||
|             } break; | ||||
|             default: { | ||||
|             } | ||||
|             } | ||||
|         } break; | ||||
|         case traits<BASE>::CSR: { | ||||
|             if(length != sizeof(reg_t)) | ||||
| @@ -1045,8 +961,10 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write(const address_type type, | ||||
|         } | ||||
|         return iss::Ok; | ||||
|     } catch(trap_access& ta) { | ||||
|         this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|         fault_data = ta.addr; | ||||
|         if((access & access_type::DEBUG) == 0) { | ||||
|             this->reg.trap_state = (1UL << 31) | ta.id; | ||||
|             fault_data = ta.addr; | ||||
|         } | ||||
|         return iss::Err; | ||||
|     } | ||||
| } | ||||
| @@ -1099,7 +1017,7 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write_plain(unsigned addr, reg_ | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read_cycle(unsigned addr, reg_t& val) { | ||||
|     auto cycle_val = this->reg.icount + cycle_offset; | ||||
|     auto cycle_val = this->reg.cycle + cycle_offset; | ||||
|     if(addr == mcycle) { | ||||
|         val = static_cast<reg_t>(cycle_val); | ||||
|     } else if(addr == mcycleh) { | ||||
| @@ -1119,7 +1037,7 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write_cycle(unsigned addr, reg_ | ||||
|             mcycle_csr = (static_cast<uint64_t>(val) << 32) + (mcycle_csr & 0xffffffff); | ||||
|         } | ||||
|     } | ||||
|     cycle_offset = mcycle_csr - this->reg.icount; // TODO: relying on wrap-around | ||||
|     cycle_offset = mcycle_csr - this->reg.cycle; // TODO: relying on wrap-around | ||||
|     return iss::Ok; | ||||
| } | ||||
|  | ||||
| @@ -1150,7 +1068,7 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write_instret(unsigned addr, re | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read_time(unsigned addr, reg_t& val) { | ||||
|     uint64_t time_val = this->reg.icount / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     uint64_t time_val = this->reg.cycle / (100000000 / 32768 - 1); //-> ~3052; | ||||
|     if(addr == time) { | ||||
|         val = static_cast<reg_t>(time_val); | ||||
|     } else if(addr == timeh) { | ||||
| @@ -1365,65 +1283,53 @@ iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::read_mem(phys_addr_t paddr, uns | ||||
|     } | ||||
|     return iss::Ok; | ||||
| } | ||||
|  | ||||
| template <typename BASE, features_e FEAT, typename LOGCAT> | ||||
| iss::status riscv_hart_mu_p<BASE, FEAT, LOGCAT>::write_mem(phys_addr_t paddr, unsigned length, const uint8_t* const data) { | ||||
|     switch(paddr.val) { | ||||
|     // TODO remove UART, Peripherals should not be part of the ISS | ||||
|     case 0xFFFF0000: // UART0 base, TXFIFO reg | ||||
|         if(((char)data[0]) == '\n' || data[0] == 0) { | ||||
|             CPPLOG(INFO) << "UART" << ((paddr.val >> 12) & 0x3) << " send '" << uart_buf.str() << "'"; | ||||
|             uart_buf.str(""); | ||||
|         } else if(((char)data[0]) != '\r') | ||||
|             uart_buf << (char)data[0]; | ||||
|         break; | ||||
|     default: { | ||||
|         mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|         std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|         // tohost handling in case of riscv-test | ||||
|         if(paddr.access && iss::access_type::FUNC) { | ||||
|             auto tohost_upper = | ||||
|                 (traits<BASE>::XLEN == 32 && paddr.val == (tohost + 4)) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             auto tohost_lower = (traits<BASE>::XLEN == 32 && paddr.val == tohost) || (traits<BASE>::XLEN == 64 && paddr.val == tohost); | ||||
|             if(tohost_lower || tohost_upper) { | ||||
|                 uint64_t hostvar = *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)); | ||||
|                 // in case of 32 bit system, two writes to tohost are needed, only evaluate on the second (high) write | ||||
|                 if(tohost_upper && (tohost_lower || tohost_lower_written)) { | ||||
|                     switch(hostvar >> 48) { | ||||
|                     case 0: | ||||
|                         if(hostvar != 0x1) { | ||||
|                             CPPLOG(FATAL) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                           << "), stopping simulation"; | ||||
|                         } else { | ||||
|                             CPPLOG(INFO) << "tohost value is 0x" << std::hex << hostvar << std::dec << " (" << hostvar | ||||
|                                          << "), stopping simulation"; | ||||
|                         } | ||||
|                         this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                         this->interrupt_sim = hostvar; | ||||
| #ifndef WITH_TCC | ||||
|                         throw(iss::simulation_stopped(hostvar)); | ||||
| #endif | ||||
|                         break; | ||||
|                     case 0x0101: { | ||||
|                         char c = static_cast<char>(hostvar & 0xff); | ||||
|                         if(c == '\n' || c == 0) { | ||||
|                             CPPLOG(INFO) << "tohost send '" << uart_buf.str() << "'"; | ||||
|                             uart_buf.str(""); | ||||
|                         } else | ||||
|                             uart_buf << c; | ||||
|                     } break; | ||||
|                     default: | ||||
|                         break; | ||||
|                     } | ||||
|                     tohost_lower_written = false; | ||||
|                 } else if(tohost_lower) | ||||
|                     tohost_lower_written = true; | ||||
|             } else if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|                 uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|                 *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|     mem_type::page_type& p = mem(paddr.val / mem.page_size); | ||||
|     std::copy(data, data + length, p.data() + (paddr.val & mem.page_addr_mask)); | ||||
|     // tohost handling in case of riscv-test | ||||
|     // according to https://github.com/riscv-software-src/riscv-isa-sim/issues/364#issuecomment-607657754: | ||||
|     if(paddr.access && iss::access_type::FUNC) { | ||||
|         if(paddr.val == tohost) { | ||||
|             reg_t cur_data = *reinterpret_cast<const reg_t*>(data); | ||||
|             // Extract Device (bits 63:56) | ||||
|             uint8_t device = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 56) & 0xFF; | ||||
|             // Extract Command (bits 55:48) | ||||
|             uint8_t command = traits<BASE>::XLEN == 32 ? 0 : (cur_data >> 48) & 0xFF; | ||||
|             // Extract payload (bits 47:0) | ||||
|             uint64_t payload_addr = cur_data & 0xFFFFFFFFFFFFULL; | ||||
|             if(payload_addr & 1) { | ||||
|                 CPPLOG(FATAL) << "tohost value is 0x" << std::hex << payload_addr << std::dec << " (" << payload_addr | ||||
|                               << "), stopping simulation"; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } else if(device == 0 && command == 0) { | ||||
|                 std::array<uint64_t, 8> loaded_payload; | ||||
|                 if(read(address_type::PHYSICAL, access_type::DEBUG_READ, traits<BASE>::MEM, payload_addr, 8 * sizeof(uint64_t), | ||||
|                         reinterpret_cast<uint8_t*>(loaded_payload.data())) == iss::Err) | ||||
|                     CPPLOG(ERR) << "Syscall read went wrong"; | ||||
|                 uint64_t syscall_num = loaded_payload.at(0); | ||||
|                 if(syscall_num == 64) { // SYS_WRITE | ||||
|                     return execute_sys_write(this, loaded_payload, traits<BASE>::MEM); | ||||
|                 } else { | ||||
|                     CPPLOG(ERR) << "tohost syscall with number 0x" << std::hex << syscall_num << std::dec << " (" << syscall_num | ||||
|                                 << ") not implemented"; | ||||
|                     this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                     this->interrupt_sim = payload_addr; | ||||
|                     return iss::Ok; | ||||
|                 } | ||||
|             } else { | ||||
|                 CPPLOG(ERR) << "tohost functionality not implemented for device " << device << " and command " << command; | ||||
|                 this->reg.trap_state = std::numeric_limits<uint32_t>::max(); | ||||
|                 this->interrupt_sim = payload_addr; | ||||
|                 return iss::Ok; | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|         if((traits<BASE>::XLEN == 32 && paddr.val == fromhost + 4) || (traits<BASE>::XLEN == 64 && paddr.val == fromhost)) { | ||||
|             uint64_t fhostvar = *reinterpret_cast<uint64_t*>(p.data() + (fromhost & mem.page_addr_mask)); | ||||
|             *reinterpret_cast<uint64_t*>(p.data() + (tohost & mem.page_addr_mask)) = fhostvar; | ||||
|         } | ||||
|     } | ||||
|     return iss::Ok; | ||||
| } | ||||
|   | ||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -39,8 +39,6 @@ | ||||
| #include <iss/iss.h> | ||||
|  | ||||
| #include <array> | ||||
| #include <iostream> | ||||
| #include <fstream> | ||||
| #include <memory> | ||||
| #ifndef FMT_HEADER_ONLY | ||||
| #define FMT_HEADER_ONLY | ||||
| @@ -109,7 +107,7 @@ public: | ||||
|     status process_query(unsigned int& mask, const rp_thread_ref& arg, rp_thread_info& info) override; | ||||
|  | ||||
|     status thread_list_query(int first, const rp_thread_ref& arg, std::vector<rp_thread_ref>& result, size_t max_num, size_t& num, | ||||
|             bool& done) override; | ||||
|                              bool& done) override; | ||||
|  | ||||
|     status current_thread_query(rp_thread_ref& thread) override; | ||||
|  | ||||
| @@ -140,15 +138,11 @@ protected: | ||||
|     rp_thread_ref thread_idx; | ||||
| }; | ||||
|  | ||||
| template <typename ARCH> | ||||
| typename std::enable_if<iss::arch::traits<ARCH>::FLEN!=0, unsigned>::type get_f0_offset() { | ||||
| template <typename ARCH> typename std::enable_if<iss::arch::traits<ARCH>::FLEN != 0, unsigned>::type get_f0_offset() { | ||||
|     return iss::arch::traits<ARCH>::F0; | ||||
| } | ||||
|  | ||||
| template <typename ARCH> | ||||
| typename std::enable_if<iss::arch::traits<ARCH>::FLEN==0, unsigned>::type get_f0_offset() { | ||||
|     return 0; | ||||
| } | ||||
| template <typename ARCH> typename std::enable_if<iss::arch::traits<ARCH>::FLEN == 0, unsigned>::type get_f0_offset() { return 0; } | ||||
|  | ||||
| template <typename ARCH> status riscv_target_adapter<ARCH>::set_gen_thread(rp_thread_ref& thread) { | ||||
|     thread_idx = thread; | ||||
| @@ -197,14 +191,14 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::read_registers(std:: | ||||
|     auto start_reg = arch::traits<ARCH>::X0; | ||||
|     for(size_t i = 0; i < 33; ++i) { | ||||
|         if(i < arch::traits<ARCH>::RFS || i == arch::traits<ARCH>::PC) { | ||||
|             auto reg_no = i<32? start_reg + i: arch::traits<ARCH>::PC; | ||||
|             auto reg_no = i < 32 ? start_reg + i : arch::traits<ARCH>::PC; | ||||
|             unsigned offset = traits<ARCH>::reg_byte_offsets[reg_no]; | ||||
|             for(size_t j = 0; j < arch::traits<ARCH>::XLEN/8; ++j) { | ||||
|             for(size_t j = 0; j < arch::traits<ARCH>::XLEN / 8; ++j) { | ||||
|                 data.push_back(*(reg_base + offset + j)); | ||||
|                 avail.push_back(0xff); | ||||
|             } | ||||
|         } else { | ||||
|             for(size_t j = 0; j < arch::traits<ARCH>::XLEN/8; ++j) { | ||||
|             for(size_t j = 0; j < arch::traits<ARCH>::XLEN / 8; ++j) { | ||||
|                 data.push_back(0); | ||||
|                 avail.push_back(0); | ||||
|             } | ||||
| @@ -229,15 +223,15 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::write_registers(cons | ||||
|     auto start_reg = arch::traits<ARCH>::X0; | ||||
|     auto* reg_base = core->get_regs_base_ptr(); | ||||
|     auto iter = data.data(); | ||||
|     auto iter_end = data.data()+data.size(); | ||||
|     auto iter_end = data.data() + data.size(); | ||||
|     for(size_t i = 0; i < 33 && iter < iter_end; ++i) { | ||||
|         auto reg_width = arch::traits<ARCH>::XLEN / 8; | ||||
|         if(i < arch::traits<ARCH>::RFS) { | ||||
|             auto offset = traits<ARCH>::reg_byte_offsets[start_reg + i]; | ||||
|             std::copy(iter, iter + reg_width, reg_base+offset); | ||||
|             std::copy(iter, iter + reg_width, reg_base + offset); | ||||
|         } else if(i == 32) { | ||||
|             auto offset = traits<ARCH>::reg_byte_offsets[arch::traits<ARCH>::PC]; | ||||
|             std::copy(iter, iter + reg_width, reg_base+offset); | ||||
|             std::copy(iter, iter + reg_width, reg_base + offset); | ||||
|         } | ||||
|         iter += reg_width; | ||||
|     } | ||||
| @@ -246,7 +240,7 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::write_registers(cons | ||||
|         auto reg_width = arch::traits<ARCH>::FLEN / 8; | ||||
|         for(size_t i = 0; i < 32 && iter < iter_end; ++i) { | ||||
|             unsigned offset = traits<ARCH>::reg_byte_offsets[fstart_reg + i]; | ||||
|             std::copy(iter, iter + reg_width, reg_base+offset); | ||||
|             std::copy(iter, iter + reg_width, reg_base + offset); | ||||
|             iter += reg_width; | ||||
|         } | ||||
|     } | ||||
| @@ -255,7 +249,7 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::write_registers(cons | ||||
|  | ||||
| template <typename ARCH> | ||||
| status riscv_target_adapter<ARCH>::read_single_register(unsigned int reg_no, std::vector<uint8_t>& data, std::vector<uint8_t>& avail) { | ||||
|     if(reg_no <csr_offset) { | ||||
|     if(reg_no < csr_offset) { | ||||
|         // auto reg_size = arch::traits<ARCH>::reg_bit_width(static_cast<typename | ||||
|         // arch::traits<ARCH>::reg_e>(reg_no))/8; | ||||
|         auto* reg_base = core->get_regs_base_ptr(); | ||||
| @@ -349,7 +343,7 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::add_break(break_type | ||||
|         auto eaddr = map_addr({iss::access_type::FETCH, iss::address_type::PHYSICAL, 0, addr + length}); | ||||
|         target_adapter_base::bp_lut.addEntry(++target_adapter_base::bp_count, saddr.val, eaddr.val - saddr.val); | ||||
|         CPPLOG(TRACE) << "Adding breakpoint with handle " << target_adapter_base::bp_count << " for addr 0x" << std::hex << saddr.val | ||||
|                 << std::dec; | ||||
|                       << std::dec; | ||||
|         CPPLOG(TRACE) << "Now having " << target_adapter_base::bp_lut.size() << " breakpoints"; | ||||
|         return Ok; | ||||
|     } | ||||
| @@ -379,7 +373,7 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::remove_break(break_t | ||||
|  | ||||
| template <typename ARCH> | ||||
| status riscv_target_adapter<ARCH>::resume_from_addr(bool step, int sig, uint64_t addr, rp_thread_ref thread, | ||||
|         std::function<void(unsigned)> stop_callback) { | ||||
|                                                     std::function<void(unsigned)> stop_callback) { | ||||
|     auto* reg_base = core->get_regs_base_ptr(); | ||||
|     auto reg_width = arch::traits<ARCH>::reg_bit_widths[arch::traits<ARCH>::PC] / 8; | ||||
|     auto offset = traits<ARCH>::reg_byte_offsets[arch::traits<ARCH>::PC]; | ||||
| @@ -398,17 +392,20 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::target_xml_query(std | ||||
|             oss << "  <architectureriscv:rv64</architecture>\n"; | ||||
|         oss << "  <feature name=\"org.gnu.gdb.riscv.cpu\">\n"; | ||||
|         auto reg_base_num = iss::arch::traits<ARCH>::X0; | ||||
|         for(auto i = 0U; i<iss::arch::traits<ARCH>::RFS; ++i) { | ||||
|             oss << "    <reg name=\"x" << i << "\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[reg_base_num + i] << "\" type=\"int\" regnum=\"" << i << "\"/>\n"; | ||||
|         for(auto i = 0U; i < iss::arch::traits<ARCH>::RFS; ++i) { | ||||
|             oss << "    <reg name=\"x" << i << "\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[reg_base_num + i] | ||||
|                 << "\" type=\"int\" regnum=\"" << i << "\"/>\n"; | ||||
|         } | ||||
|         oss << "    <reg name=\"pc\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[iss::arch::traits<ARCH>::PC] << "\" type=\"code_ptr\" regnum=\"" << 32U << "\"/>\n"; | ||||
|         oss << "    <reg name=\"pc\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[iss::arch::traits<ARCH>::PC] | ||||
|             << "\" type=\"code_ptr\" regnum=\"" << 32U << "\"/>\n"; | ||||
|         oss << "  </feature>\n"; | ||||
|         if(iss::arch::traits<ARCH>::FLEN > 0) { | ||||
|             oss << "  <feature name=\"org.gnu.gdb.riscv.fpu\">\n"; | ||||
|             auto reg_base_num =  get_f0_offset<ARCH>(); | ||||
|             auto type = iss::arch::traits<ARCH>::FLEN==32?"ieee_single":"riscv_double"; | ||||
|             for(auto i = 0U; i<32; ++i) { | ||||
|                 oss << "    <reg name=\"f" << i << "\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[reg_base_num + i] << "\" type=\""<<type<<"\" regnum=\"" << i+33 << "\"/>\n"; | ||||
|             auto reg_base_num = get_f0_offset<ARCH>(); | ||||
|             auto type = iss::arch::traits<ARCH>::FLEN == 32 ? "ieee_single" : "riscv_double"; | ||||
|             for(auto i = 0U; i < 32; ++i) { | ||||
|                 oss << "    <reg name=\"f" << i << "\" bitsize=\"" << iss::arch::traits<ARCH>::reg_bit_widths[reg_base_num + i] | ||||
|                     << "\" type=\"" << type << "\" regnum=\"" << i + 33 << "\"/>\n"; | ||||
|             } | ||||
|             oss << "    <reg name=\"fcsr\" bitsize=\"" << iss::arch::traits<ARCH>::XLEN << "\" regnum=\"103\" type int/>\n"; | ||||
|             oss << "    <reg name=\"fflags\" bitsize=\"" << iss::arch::traits<ARCH>::XLEN << "\" regnum=\"101\" type int/>\n"; | ||||
| @@ -420,16 +417,18 @@ template <typename ARCH> status riscv_target_adapter<ARCH>::target_xml_query(std | ||||
|         std::vector<uint8_t> avail; | ||||
|         data.resize(sizeof(typename traits<ARCH>::reg_t)); | ||||
|         avail.resize(sizeof(typename traits<ARCH>::reg_t)); | ||||
|         for(auto i = 0U; i<4096; ++i) { | ||||
|         for(auto i = 0U; i < 4096; ++i) { | ||||
|             typed_addr_t<iss::address_type::PHYSICAL> a(iss::access_type::DEBUG_READ, traits<ARCH>::CSR, i); | ||||
|             std::fill(avail.begin(), avail.end(), 0xff); | ||||
|             auto res = core->read(a, data.size(), data.data()); | ||||
|             if(res == iss::Ok) { | ||||
|                 oss << "    <reg name=\"" << get_csr_name(i) << "\" bitsize=\"" << iss::arch::traits<ARCH>::XLEN << "\"  type=\"int\" regnum=\"" << (i + csr_offset) << "\"/>\n"; | ||||
|                 oss << "    <reg name=\"" << get_csr_name(i) << "\" bitsize=\"" << iss::arch::traits<ARCH>::XLEN | ||||
|                     << "\"  type=\"int\" regnum=\"" << (i + csr_offset) << "\"/>\n"; | ||||
|             } | ||||
|         } | ||||
|         oss << "  </feature>\n"; | ||||
|         oss << "</target>\n"; | ||||
|         csr_xml = oss.str(); | ||||
|     } | ||||
|     out_buf = csr_xml; | ||||
|     return Ok; | ||||
|   | ||||
							
								
								
									
										17
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								src/main.cpp
									
									
									
									
									
								
							| @@ -141,7 +141,10 @@ int main(int argc, char* argv[]) { | ||||
|             std::tie(cpu, vm) = f.create(isa_opt, clim["gdb-port"].as<unsigned>(), &semihosting_cb); | ||||
|         } | ||||
|         if(!cpu) { | ||||
|             CPPLOG(ERR) << "Could not create cpu for isa " << isa_opt << " and backend " << clim["backend"].as<std::string>() << std::endl; | ||||
|             auto list = f.get_names(); | ||||
|             std::sort(std::begin(list), std::end(list)); | ||||
|             CPPLOG(ERR) << "Could not create cpu for isa " << isa_opt << " and backend " << clim["backend"].as<std::string>() << "\n" | ||||
|                         << "Available implementations (core|platform|backend):\n  - " << util::join(list, "\n  - ") << std::endl; | ||||
|             return 127; | ||||
|         } | ||||
|         if(!vm) { | ||||
| @@ -203,13 +206,21 @@ int main(int argc, char* argv[]) { | ||||
|         if(clim.count("elf")) | ||||
|             for(std::string input : clim["elf"].as<std::vector<std::string>>()) { | ||||
|                 auto start_addr = vm->get_arch()->load_file(input); | ||||
|                 if(start_addr.second) // FIXME: this always evaluates to true as load file always returns <sth, true> | ||||
|                 if(start_addr.second) | ||||
|                     start_address = start_addr.first; | ||||
|                 else { | ||||
|                     LOG(ERR) << "Error occured while loading file " << input << std::endl; | ||||
|                     return 1; | ||||
|                 } | ||||
|             } | ||||
|         for(std::string input : args) { | ||||
|             auto start_addr = vm->get_arch()->load_file(input); // treat remaining arguments as elf files | ||||
|             if(start_addr.second) // FIXME: this always evaluates to true as load file always returns <sth, true> | ||||
|             if(start_addr.second) | ||||
|                 start_address = start_addr.first; | ||||
|             else { | ||||
|                 LOG(ERR) << "Error occured while loading file " << input << std::endl; | ||||
|                 return 1; | ||||
|             } | ||||
|         } | ||||
|         if(clim.count("reset")) { | ||||
|             auto str = clim["reset"].as<std::string>(); | ||||
|   | ||||
| @@ -42,7 +42,6 @@ | ||||
| #include <iss/plugin/loader.h> | ||||
| #endif | ||||
| #include "sc_core_adapter_if.h" | ||||
| #include <iss/arch/tgc_mapper.h> | ||||
| #include <scc/report.h> | ||||
| #include <util/ities.h> | ||||
| #include <iostream> | ||||
| @@ -208,8 +207,7 @@ core_complex<BUSWIDTH>::core_complex(sc_module_name const& name) | ||||
| } | ||||
| #endif | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::init() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::init() { | ||||
|     trc = new core_trace(); | ||||
|     ibus.register_invalidate_direct_mem_ptr([=](uint64_t start, uint64_t end) -> void { | ||||
|         auto lut_entry = fetch_lut.getEntry(start); | ||||
| @@ -254,19 +252,16 @@ void core_complex<BUSWIDTH>::init() { | ||||
| #endif | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| core_complex<BUSWIDTH>::~core_complex() { | ||||
| template <unsigned int BUSWIDTH> core_complex<BUSWIDTH>::~core_complex() { | ||||
|     delete cpu; | ||||
|     delete trc; | ||||
|     for(auto* p : plugin_list) | ||||
|         delete p; | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::trace(sc_trace_file* trf) const {} | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::trace(sc_trace_file* trf) const {} | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::before_end_of_elaboration() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::before_end_of_elaboration() { | ||||
|     SCCDEBUG(SCMOD) << "instantiating iss::arch::tgf with " << GET_PROP_VALUE(backend) << " backend"; | ||||
|     // cpu = scc::make_unique<core_wrapper>(this); | ||||
|     cpu = new core_wrapper(this); | ||||
| @@ -307,8 +302,7 @@ void core_complex<BUSWIDTH>::before_end_of_elaboration() { | ||||
|     } | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::start_of_simulation() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::start_of_simulation() { | ||||
|     // quantum_keeper.reset(); | ||||
|     if(GET_PROP_VALUE(elf_file).size() > 0) { | ||||
|         istringstream is(GET_PROP_VALUE(elf_file)); | ||||
| @@ -331,8 +325,7 @@ void core_complex<BUSWIDTH>::start_of_simulation() { | ||||
|     } | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| bool core_complex<BUSWIDTH>::disass_output(uint64_t pc, const std::string instr_str) { | ||||
| template <unsigned int BUSWIDTH> bool core_complex<BUSWIDTH>::disass_output(uint64_t pc, const std::string instr_str) { | ||||
|     if(trc->m_db == nullptr) | ||||
|         return false; | ||||
|     if(trc->tr_handle.is_active()) | ||||
| @@ -346,8 +339,7 @@ bool core_complex<BUSWIDTH>::disass_output(uint64_t pc, const std::string instr_ | ||||
|     return true; | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::forward() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::forward() { | ||||
| #ifndef CWR_SYSTEMC | ||||
|     set_clock_period(clk_i.read()); | ||||
| #else | ||||
| @@ -356,30 +348,24 @@ void core_complex<BUSWIDTH>::forward() { | ||||
| #endif | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::set_clock_period(sc_core::sc_time period) { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::set_clock_period(sc_core::sc_time period) { | ||||
|     curr_clk = period; | ||||
|     if(period == SC_ZERO_TIME) | ||||
|         cpu->set_interrupt_execution(true); | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::rst_cb() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::rst_cb() { | ||||
|     if(rst_i.read()) | ||||
|         cpu->set_interrupt_execution(true); | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::sw_irq_cb() { cpu->local_irq(3, sw_irq_i.read()); } | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::sw_irq_cb() { cpu->local_irq(3, sw_irq_i.read()); } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::timer_irq_cb() { cpu->local_irq(7, timer_irq_i.read()); } | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::timer_irq_cb() { cpu->local_irq(7, timer_irq_i.read()); } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::ext_irq_cb() { cpu->local_irq(11, ext_irq_i.read()); } | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::ext_irq_cb() { cpu->local_irq(11, ext_irq_i.read()); } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::local_irq_cb() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::local_irq_cb() { | ||||
|     for(auto i = 0U; i < local_irq_i.size(); ++i) { | ||||
|         if(local_irq_i[i].event()) { | ||||
|             cpu->local_irq(16 + i, local_irq_i[i].read()); | ||||
| @@ -387,8 +373,7 @@ void core_complex<BUSWIDTH>::local_irq_cb() { | ||||
|     } | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| void core_complex<BUSWIDTH>::run() { | ||||
| template <unsigned int BUSWIDTH> void core_complex<BUSWIDTH>::run() { | ||||
|     wait(SC_ZERO_TIME); // separate from elaboration phase | ||||
|     do { | ||||
|         wait(SC_ZERO_TIME); | ||||
| @@ -406,8 +391,7 @@ void core_complex<BUSWIDTH>::run() { | ||||
|     sc_stop(); | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| bool core_complex<BUSWIDTH>::read_mem(uint64_t addr, unsigned length, uint8_t* const data, bool is_fetch) { | ||||
| template <unsigned int BUSWIDTH> bool core_complex<BUSWIDTH>::read_mem(uint64_t addr, unsigned length, uint8_t* const data, bool is_fetch) { | ||||
|     auto& dmi_lut = is_fetch ? fetch_lut : read_lut; | ||||
|     auto lut_entry = dmi_lut.getEntry(addr); | ||||
|     if(lut_entry.get_granted_access() != tlm::tlm_dmi::DMI_ACCESS_NONE && addr + length <= lut_entry.get_end_address() + 1) { | ||||
| @@ -465,8 +449,7 @@ bool core_complex<BUSWIDTH>::read_mem(uint64_t addr, unsigned length, uint8_t* c | ||||
|     } | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| bool core_complex<BUSWIDTH>::write_mem(uint64_t addr, unsigned length, const uint8_t* const data) { | ||||
| template <unsigned int BUSWIDTH> bool core_complex<BUSWIDTH>::write_mem(uint64_t addr, unsigned length, const uint8_t* const data) { | ||||
|     auto lut_entry = write_lut.getEntry(addr); | ||||
|     if(lut_entry.get_granted_access() != tlm::tlm_dmi::DMI_ACCESS_NONE && addr + length <= lut_entry.get_end_address() + 1) { | ||||
|         auto offset = addr - lut_entry.get_start_address(); | ||||
| @@ -514,8 +497,7 @@ bool core_complex<BUSWIDTH>::write_mem(uint64_t addr, unsigned length, const uin | ||||
|     } | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| bool core_complex<BUSWIDTH>::read_mem_dbg(uint64_t addr, unsigned length, uint8_t* const data) { | ||||
| template <unsigned int BUSWIDTH> bool core_complex<BUSWIDTH>::read_mem_dbg(uint64_t addr, unsigned length, uint8_t* const data) { | ||||
|     tlm::tlm_generic_payload gp; | ||||
|     gp.set_command(tlm::TLM_READ_COMMAND); | ||||
|     gp.set_address(addr); | ||||
| @@ -525,8 +507,7 @@ bool core_complex<BUSWIDTH>::read_mem_dbg(uint64_t addr, unsigned length, uint8_ | ||||
|     return dbus->transport_dbg(gp) == length; | ||||
| } | ||||
|  | ||||
| template <unsigned int BUSWIDTH> | ||||
| bool core_complex<BUSWIDTH>::write_mem_dbg(uint64_t addr, unsigned length, const uint8_t* const data) { | ||||
| template <unsigned int BUSWIDTH> bool core_complex<BUSWIDTH>::write_mem_dbg(uint64_t addr, unsigned length, const uint8_t* const data) { | ||||
|     write_buf.resize(length); | ||||
|     std::copy(data, data + length, write_buf.begin()); // need to copy as TLM does not guarantee data integrity | ||||
|     tlm::tlm_generic_payload gp; | ||||
|   | ||||
| @@ -33,10 +33,10 @@ | ||||
| #ifndef _SYSC_CORE_COMPLEX_H_ | ||||
| #define _SYSC_CORE_COMPLEX_H_ | ||||
|  | ||||
| #include <scc/signal_opt_ports.h> | ||||
| #include <scc/tick2time.h> | ||||
| #include <scc/traceable.h> | ||||
| #include <scc/utilities.h> | ||||
| #include <scc/signal_opt_ports.h> | ||||
| #include <tlm/scc/initiator_mixin.h> | ||||
| #include <tlm/scc/scv/tlm_rec_initiator_socket.h> | ||||
| #ifdef CWR_SYSTEMC | ||||
| @@ -71,28 +71,27 @@ struct core_complex_if { | ||||
|  | ||||
|     virtual ~core_complex_if() = default; | ||||
|  | ||||
|     virtual bool read_mem(uint64_t addr, unsigned length, uint8_t* const data, bool is_fetch) =0; | ||||
|     virtual bool read_mem(uint64_t addr, unsigned length, uint8_t* const data, bool is_fetch) = 0; | ||||
|  | ||||
|     virtual bool write_mem(uint64_t addr, unsigned length, const uint8_t* const data)  =0; | ||||
|     virtual bool write_mem(uint64_t addr, unsigned length, const uint8_t* const data) = 0; | ||||
|  | ||||
|     virtual bool read_mem_dbg(uint64_t addr, unsigned length, uint8_t* const data)  =0; | ||||
|     virtual bool read_mem_dbg(uint64_t addr, unsigned length, uint8_t* const data) = 0; | ||||
|  | ||||
|     virtual bool write_mem_dbg(uint64_t addr, unsigned length, const uint8_t* const data)  =0; | ||||
|     virtual bool write_mem_dbg(uint64_t addr, unsigned length, const uint8_t* const data) = 0; | ||||
|  | ||||
|     virtual bool disass_output(uint64_t pc, const std::string instr)  =0; | ||||
|     virtual bool disass_output(uint64_t pc, const std::string instr) = 0; | ||||
|  | ||||
|     virtual unsigned get_last_bus_cycles() =0; | ||||
|     virtual unsigned get_last_bus_cycles() = 0; | ||||
|  | ||||
|     //! Allow quantum keeper handling | ||||
|     virtual void sync(uint64_t) =0; | ||||
|     virtual void sync(uint64_t) = 0; | ||||
|  | ||||
|     virtual char const* hier_name() = 0; | ||||
|  | ||||
|     scc::sc_in_opt<uint64_t> mtime_i{"mtime_i"}; | ||||
| }; | ||||
|  | ||||
| template <unsigned int BUSWIDTH = scc::LT> | ||||
| class core_complex : public sc_core::sc_module, public scc::traceable, public core_complex_if { | ||||
| template <unsigned int BUSWIDTH = scc::LT> class core_complex : public sc_core::sc_module, public scc::traceable, public core_complex_if { | ||||
| public: | ||||
|     tlm::scc::initiator_mixin<tlm::tlm_initiator_socket<BUSWIDTH>> ibus{"ibus"}; | ||||
|  | ||||
| @@ -208,9 +207,7 @@ public: | ||||
|  | ||||
|     void set_clock_period(sc_core::sc_time period); | ||||
|  | ||||
|     char const* hier_name() override { | ||||
|         return name(); | ||||
|     } | ||||
|     char const* hier_name() override { return name(); } | ||||
|  | ||||
| protected: | ||||
|     void before_end_of_elaboration() override; | ||||
|   | ||||
| @@ -62,12 +62,12 @@ using namespace sysc; | ||||
| volatile std::array<bool, 2> tgc_init = { | ||||
|     iss_factory::instance().register_creator("tgc5c|m_p|llvm", | ||||
|                                              [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|                                                  auto cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|                                                  auto cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|                                                  auto* cpu = new sc_core_adapter<arch::riscv_hart_m_p<arch::tgc5c>>(cc); | ||||
|                                                  return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::tgc5c*>(cpu), gdb_port)}}; | ||||
|                                              }), | ||||
|     iss_factory::instance().register_creator("tgc5c|mu_p|llvm", [](unsigned gdb_port, void* data) -> iss_factory::base_t { | ||||
|         auto cc = reinterpret_cast<sysc::tgfs::core_complex*>(data); | ||||
|         auto cc = reinterpret_cast<sysc::tgfs::core_complex_if*>(data); | ||||
|         auto* cpu = new sc_core_adapter<arch::riscv_hart_mu_p<arch::tgc5c>>(cc); | ||||
|         return {sysc::sc_cpu_ptr{cpu}, vm_ptr{create(static_cast<arch::tgc5c*>(cpu), gdb_port)}}; | ||||
|     })}; | ||||
|   | ||||
| @@ -55,8 +55,8 @@ public: | ||||
|             s << "[p:" << lvl[this->reg.PRIV] << ";s:0x" << std::hex << std::setfill('0') << std::setw(sizeof(reg_t) * 2) | ||||
|               << (reg_t)this->state.mstatus << std::dec << ";c:" << this->reg.icount + this->cycle_offset << "]"; | ||||
|             SCCDEBUG(owner->hier_name()) << "disass: " | ||||
|                                     << "0x" << std::setw(16) << std::right << std::setfill('0') << std::hex << pc << "\t\t" << std::setw(40) | ||||
|                                     << std::setfill(' ') << std::left << instr << s.str(); | ||||
|                                          << "0x" << std::setw(16) << std::right << std::setfill('0') << std::hex << pc << "\t\t" | ||||
|                                          << std::setw(40) << std::setfill(' ') << std::left << instr << s.str(); | ||||
|         } | ||||
|     }; | ||||
|  | ||||
| @@ -113,7 +113,7 @@ public: | ||||
|  | ||||
|     iss::status read_csr(unsigned addr, reg_t& val) override { | ||||
|         if((addr == iss::arch::time || addr == iss::arch::timeh)) { | ||||
|             uint64_t time_val = owner->mtime_i.get_interface()? owner->mtime_i.read():0; | ||||
|             uint64_t time_val = owner->mtime_i.get_interface() ? owner->mtime_i.read() : 0; | ||||
|             if(addr == iss::arch::time) { | ||||
|                 val = static_cast<reg_t>(time_val); | ||||
|             } else if(addr == iss::arch::timeh) { | ||||
|   | ||||
| @@ -94,7 +94,7 @@ protected: | ||||
|     using this_class = vm_impl<ARCH>; | ||||
|     using compile_func = continuation_e (this_class::*)(virt_addr_t&, code_word_t, jit_holder&); | ||||
|  | ||||
|     continuation_e gen_single_inst_behavior(virt_addr_t&, unsigned int &, jit_holder&) override; | ||||
|     continuation_e gen_single_inst_behavior(virt_addr_t&, jit_holder&) override; | ||||
|     enum globals_e {TVAL = 0, GLOBALS_SIZE}; | ||||
|     void gen_block_prologue(jit_holder& jh) override; | ||||
|     void gen_block_epilogue(jit_holder& jh) override; | ||||
| @@ -1421,23 +1421,21 @@ private: | ||||
|         } | ||||
|         else{ | ||||
|             if(rd!=0){ | ||||
|                 { | ||||
|                 auto label_then = cc.newLabel(); | ||||
|                 auto label_merge = cc.newLabel(); | ||||
|                 auto tmp_reg = get_reg_for(cc, 1); | ||||
|                 auto label_then11 = cc.newLabel(); | ||||
|                 auto label_merge11 = cc.newLabel(); | ||||
|                 auto tmp_reg11 = get_reg(cc, 8, false); | ||||
|                 cmp(cc, gen_ext(cc,  | ||||
|                     load_reg_from_mem(jh, traits::X0 + rs1), 32, true), (int16_t)sext<12>(imm)); | ||||
|                 cc.jl(label_then); | ||||
|                 mov(cc, tmp_reg,0); | ||||
|                 cc.jmp(label_merge); | ||||
|                 cc.bind(label_then); | ||||
|                 mov(cc, tmp_reg,1); | ||||
|                 cc.bind(label_merge); | ||||
|                 cc.jl(label_then11); | ||||
|                 mov(cc, tmp_reg11,0); | ||||
|                 cc.jmp(label_merge11); | ||||
|                 cc.bind(label_then11); | ||||
|                 mov(cc, tmp_reg11, 1); | ||||
|                 cc.bind(label_merge11); | ||||
|                 mov(cc, get_ptr_for(jh, traits::X0+ rd), | ||||
|                       gen_ext(cc, tmp_reg | ||||
|                       gen_ext(cc, tmp_reg11 | ||||
|                       , 32, false) | ||||
|                 ); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         auto returnValue = CONT; | ||||
| @@ -1484,22 +1482,20 @@ private: | ||||
|         } | ||||
|         else{ | ||||
|             if(rd!=0){ | ||||
|                 { | ||||
|                 auto label_then = cc.newLabel(); | ||||
|                 auto label_merge = cc.newLabel(); | ||||
|                 auto tmp_reg = get_reg_for(cc, 1); | ||||
|                 auto label_then12 = cc.newLabel(); | ||||
|                 auto label_merge12 = cc.newLabel(); | ||||
|                 auto tmp_reg12 = get_reg(cc, 8, false); | ||||
|                 cmp(cc, load_reg_from_mem(jh, traits::X0 + rs1), (uint32_t)((int16_t)sext<12>(imm))); | ||||
|                 cc.jb(label_then); | ||||
|                 mov(cc, tmp_reg,0); | ||||
|                 cc.jmp(label_merge); | ||||
|                 cc.bind(label_then); | ||||
|                 mov(cc, tmp_reg,1); | ||||
|                 cc.bind(label_merge); | ||||
|                 cc.jb(label_then12); | ||||
|                 mov(cc, tmp_reg12,0); | ||||
|                 cc.jmp(label_merge12); | ||||
|                 cc.bind(label_then12); | ||||
|                 mov(cc, tmp_reg12, 1); | ||||
|                 cc.bind(label_merge12); | ||||
|                 mov(cc, get_ptr_for(jh, traits::X0+ rd), | ||||
|                       gen_ext(cc, tmp_reg | ||||
|                       gen_ext(cc, tmp_reg12 | ||||
|                       , 32, false) | ||||
|                 ); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         auto returnValue = CONT; | ||||
| @@ -1992,24 +1988,22 @@ private: | ||||
|         } | ||||
|         else{ | ||||
|             if(rd!=0){ | ||||
|                 { | ||||
|                 auto label_then = cc.newLabel(); | ||||
|                 auto label_merge = cc.newLabel(); | ||||
|                 auto tmp_reg = get_reg_for(cc, 1); | ||||
|                 auto label_then13 = cc.newLabel(); | ||||
|                 auto label_merge13 = cc.newLabel(); | ||||
|                 auto tmp_reg13 = get_reg(cc, 8, false); | ||||
|                 cmp(cc, gen_ext(cc,  | ||||
|                     load_reg_from_mem(jh, traits::X0 + rs1), 32, true), gen_ext(cc,  | ||||
|                     load_reg_from_mem(jh, traits::X0 + rs2), 32, true)); | ||||
|                 cc.jl(label_then); | ||||
|                 mov(cc, tmp_reg,0); | ||||
|                 cc.jmp(label_merge); | ||||
|                 cc.bind(label_then); | ||||
|                 mov(cc, tmp_reg,1); | ||||
|                 cc.bind(label_merge); | ||||
|                 cc.jl(label_then13); | ||||
|                 mov(cc, tmp_reg13,0); | ||||
|                 cc.jmp(label_merge13); | ||||
|                 cc.bind(label_then13); | ||||
|                 mov(cc, tmp_reg13, 1); | ||||
|                 cc.bind(label_merge13); | ||||
|                 mov(cc, get_ptr_for(jh, traits::X0+ rd), | ||||
|                       gen_ext(cc, tmp_reg | ||||
|                       gen_ext(cc, tmp_reg13 | ||||
|                       , 32, false) | ||||
|                 ); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         auto returnValue = CONT; | ||||
| @@ -2056,22 +2050,20 @@ private: | ||||
|         } | ||||
|         else{ | ||||
|             if(rd!=0){ | ||||
|                 { | ||||
|                 auto label_then = cc.newLabel(); | ||||
|                 auto label_merge = cc.newLabel(); | ||||
|                 auto tmp_reg = get_reg_for(cc, 1); | ||||
|                 auto label_then14 = cc.newLabel(); | ||||
|                 auto label_merge14 = cc.newLabel(); | ||||
|                 auto tmp_reg14 = get_reg(cc, 8, false); | ||||
|                 cmp(cc, load_reg_from_mem(jh, traits::X0 + rs1), load_reg_from_mem(jh, traits::X0 + rs2)); | ||||
|                 cc.jb(label_then); | ||||
|                 mov(cc, tmp_reg,0); | ||||
|                 cc.jmp(label_merge); | ||||
|                 cc.bind(label_then); | ||||
|                 mov(cc, tmp_reg,1); | ||||
|                 cc.bind(label_merge); | ||||
|                 cc.jb(label_then14); | ||||
|                 mov(cc, tmp_reg14,0); | ||||
|                 cc.jmp(label_merge14); | ||||
|                 cc.bind(label_then14); | ||||
|                 mov(cc, tmp_reg14, 1); | ||||
|                 cc.bind(label_merge14); | ||||
|                 mov(cc, get_ptr_for(jh, traits::X0+ rd), | ||||
|                       gen_ext(cc, tmp_reg | ||||
|                       gen_ext(cc, tmp_reg14 | ||||
|                       , 32, false) | ||||
|                 ); | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|         auto returnValue = CONT; | ||||
| @@ -2511,10 +2503,10 @@ private: | ||||
|         gen_instr_prologue(jh); | ||||
|         cc.comment("//behavior:"); | ||||
|         /*generate behavior*/ | ||||
|         InvokeNode* call_wait; | ||||
|         InvokeNode* call_wait_15; | ||||
|         jh.cc.comment("//call_wait"); | ||||
|         jh.cc.invoke(&call_wait, &wait, FuncSignature::build<void, int32_t>()); | ||||
|         setArg(call_wait, 0, 1); | ||||
|         jh.cc.invoke(&call_wait_15, &wait, FuncSignature::build<void, int32_t>()); | ||||
|         setArg(call_wait_15, 0, 1); | ||||
|         auto returnValue = CONT; | ||||
|          | ||||
|         gen_sync(jh, POST_SYNC, 41); | ||||
| @@ -4788,7 +4780,7 @@ vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id) | ||||
|     }()) {} | ||||
|  | ||||
| template <typename ARCH> | ||||
| continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, jit_holder& jh) { | ||||
| continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, jit_holder& jh) { | ||||
|     enum {TRAP_ID=1<<16}; | ||||
|     code_word_t instr = 0; | ||||
|     phys_addr_t paddr(pc); | ||||
| @@ -4800,7 +4792,6 @@ continuation_e vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned | ||||
|         return ILLEGAL_FETCH; | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001) | ||||
|         return JUMP_TO_SELF; | ||||
|     ++inst_cnt; | ||||
|     uint32_t inst_index = instr_decoder.decode_instr(instr); | ||||
|     compile_func f = nullptr; | ||||
|     if(inst_index < instr_descr.size()) | ||||
| @@ -4830,6 +4821,7 @@ void vm_impl<ARCH>::gen_instr_epilogue(jit_holder& jh) { | ||||
|     cmp(cc, current_trap_state, 0); | ||||
|     cc.jne(jh.trap_entry); | ||||
|     cc.inc(get_ptr_for(jh, traits::ICOUNT)); | ||||
|     cc.inc(get_ptr_for(jh, traits::CYCLE)); | ||||
| } | ||||
| template <typename ARCH> | ||||
| void vm_impl<ARCH>::gen_block_prologue(jit_holder& jh){ | ||||
| @@ -4875,6 +4867,7 @@ inline void vm_impl<ARCH>::gen_raise(jit_holder& jh, uint16_t trap_id, uint16_t | ||||
|     auto tmp1 = get_reg_for(cc, traits::TRAP_STATE); | ||||
|     mov(cc, tmp1, 0x80ULL << 24 | (cause << 16) | trap_id); | ||||
|     mov(cc, get_ptr_for(jh, traits::TRAP_STATE), tmp1); | ||||
|     cc.jmp(jh.trap_entry); | ||||
| } | ||||
| template <typename ARCH> | ||||
| template <typename T, typename> | ||||
|   | ||||
| @@ -203,8 +203,8 @@ uint32_t fclass_s(uint32_t v1) { | ||||
|     uA.f = a; | ||||
|     uiA = uA.ui; | ||||
|  | ||||
|     uint_fast16_t infOrNaN = expF32UI(uiA) == 0xFF; | ||||
|     uint_fast16_t subnormalOrZero = expF32UI(uiA) == 0; | ||||
|     bool infOrNaN = expF32UI(uiA) == 0xFF; | ||||
|     bool subnormalOrZero = expF32UI(uiA) == 0; | ||||
|     bool sign = signF32UI(uiA); | ||||
|     bool fracZero = fracF32UI(uiA) == 0; | ||||
|     bool isNaN = isNaNF32UI(uiA); | ||||
| @@ -217,9 +217,13 @@ uint32_t fclass_s(uint32_t v1) { | ||||
| } | ||||
|  | ||||
| uint32_t fconv_d2f(uint64_t v1, uint8_t mode) { | ||||
|     bool isNan = isNaNF64UI(v1); | ||||
|     bool isSNaN = softfloat_isSigNaNF64UI(v1); | ||||
|     softfloat_roundingMode = rmm_map.at(mode); | ||||
|     bool nan = (v1 & defaultNaNF64UI) == defaultNaNF64UI; | ||||
|     if(nan) { | ||||
|     softfloat_exceptionFlags = 0; | ||||
|     if(isNan) { | ||||
|         if(isSNaN) | ||||
|             softfloat_raiseFlags(softfloat_flag_invalid); | ||||
|         return defaultNaNF32UI; | ||||
|     } else { | ||||
|         float32_t res = f64_to_f32(float64_t{v1}); | ||||
| @@ -228,11 +232,11 @@ uint32_t fconv_d2f(uint64_t v1, uint8_t mode) { | ||||
| } | ||||
|  | ||||
| uint64_t fconv_f2d(uint32_t v1, uint8_t mode) { | ||||
|     bool nan = (v1 & defaultNaNF32UI) == defaultNaNF32UI; | ||||
|     if(nan) { | ||||
|     bool infOrNaN = expF32UI(v1) == 0xFF; | ||||
|     bool subnormalOrZero = expF32UI(v1) == 0; | ||||
|     if(infOrNaN || subnormalOrZero) { | ||||
|         return defaultNaNF64UI; | ||||
|     } else { | ||||
|         softfloat_roundingMode = rmm_map.at(mode); | ||||
|         float64_t res = f32_to_f64(float32_t{v1}); | ||||
|         return res.v; | ||||
|     } | ||||
| @@ -312,22 +316,23 @@ uint64_t fcmp_d(uint64_t v1, uint64_t v2, uint32_t op) { | ||||
| } | ||||
|  | ||||
| uint64_t fcvt_d(uint64_t v1, uint32_t op, uint8_t mode) { | ||||
|  | ||||
|     float64_t v1f{v1}; | ||||
|     softfloat_exceptionFlags = 0; | ||||
|     float64_t r; | ||||
|     switch(op) { | ||||
|     case 0: { // l->d, fp to int32 | ||||
|     case 0: { // l from d | ||||
|         int64_t res = f64_to_i64(v1f, rmm_map.at(mode), true); | ||||
|         return (uint64_t)res; | ||||
|     } | ||||
|     case 1: { // lu->s | ||||
|     case 1: { // lu from d | ||||
|         uint64_t res = f64_to_ui64(v1f, rmm_map.at(mode), true); | ||||
|         return res; | ||||
|     } | ||||
|     case 2: // s->l | ||||
|     case 2: // d from l | ||||
|         r = i64_to_f64(v1); | ||||
|         return r.v; | ||||
|     case 3: // s->lu | ||||
|     case 3: // d from lu | ||||
|         r = ui64_to_f64(v1); | ||||
|         return r.v; | ||||
|     } | ||||
| @@ -335,12 +340,24 @@ uint64_t fcvt_d(uint64_t v1, uint32_t op, uint8_t mode) { | ||||
| } | ||||
|  | ||||
| uint64_t fmadd_d(uint64_t v1, uint64_t v2, uint64_t v3, uint32_t op, uint8_t mode) { | ||||
|     // op should be {softfloat_mulAdd_subProd(2), softfloat_mulAdd_subC(1)} | ||||
|     uint64_t F64_SIGN = 1ULL << 63; | ||||
|     switch(op) { | ||||
|     case 0: // FMADD_D | ||||
|         break; | ||||
|     case 1: // FMSUB_D | ||||
|         v3 ^= F64_SIGN; | ||||
|         break; | ||||
|     case 2: // FNMADD_D | ||||
|         v1 ^= F64_SIGN; | ||||
|         v3 ^= F64_SIGN; | ||||
|         break; | ||||
|     case 3: // FNMSUB_D | ||||
|         v1 ^= F64_SIGN; | ||||
|         break; | ||||
|     } | ||||
|     softfloat_roundingMode = rmm_map.at(mode); | ||||
|     softfloat_exceptionFlags = 0; | ||||
|     float64_t res = softfloat_mulAddF64(v1, v2, v3, op & 0x1); | ||||
|     if(op > 1) | ||||
|         res.v ^= 1ULL << 63; | ||||
|     float64_t res = softfloat_mulAddF64(v1, v2, v3, 0); | ||||
|     return res.v; | ||||
| } | ||||
|  | ||||
| @@ -376,8 +393,8 @@ uint64_t fclass_d(uint64_t v1) { | ||||
|     uA.f = a; | ||||
|     uiA = uA.ui; | ||||
|  | ||||
|     uint_fast16_t infOrNaN = expF64UI(uiA) == 0x7FF; | ||||
|     uint_fast16_t subnormalOrZero = expF64UI(uiA) == 0; | ||||
|     bool infOrNaN = expF64UI(uiA) == 0x7FF; | ||||
|     bool subnormalOrZero = expF64UI(uiA) == 0; | ||||
|     bool sign = signF64UI(uiA); | ||||
|     bool fracZero = fracF64UI(uiA) == 0; | ||||
|     bool isNaN = isNaNF64UI(uiA); | ||||
|   | ||||
| @@ -275,9 +275,6 @@ template <typename CODE_WORD> void debug_fn(CODE_WORD insn) { | ||||
|     volatile CODE_WORD x = insn; | ||||
|     insn = 2 * x; | ||||
| } | ||||
|  | ||||
| template <typename ARCH> vm_impl<ARCH>::vm_impl() { this(new ARCH()); } | ||||
|  | ||||
| // according to | ||||
| // https://stackoverflow.com/questions/8871204/count-number-of-1s-in-binary-representation | ||||
| #ifdef __GCC__ | ||||
| @@ -709,9 +706,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t load_address = (uint32_t)((uint64_t)(*(X+rs1) ) + (uint64_t)((int16_t)sext<12>(imm) )); | ||||
|                                         int8_t res_27 = super::template read_mem<int8_t>(traits::MEM, load_address); | ||||
|                                         int8_t res_1 = super::template read_mem<int8_t>(traits::MEM, load_address); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         int8_t res = (int8_t)res_27; | ||||
|                                         int8_t res = (int8_t)res_1; | ||||
|                                         if(rd != 0) { | ||||
|                                             *(X+rd) = (uint32_t)res; | ||||
|                                         } | ||||
| @@ -740,9 +737,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t load_address = (uint32_t)((uint64_t)(*(X+rs1) ) + (uint64_t)((int16_t)sext<12>(imm) )); | ||||
|                                         int16_t res_28 = super::template read_mem<int16_t>(traits::MEM, load_address); | ||||
|                                         int16_t res_2 = super::template read_mem<int16_t>(traits::MEM, load_address); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         int16_t res = (int16_t)res_28; | ||||
|                                         int16_t res = (int16_t)res_2; | ||||
|                                         if(rd != 0) { | ||||
|                                             *(X+rd) = (uint32_t)res; | ||||
|                                         } | ||||
| @@ -771,9 +768,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t load_address = (uint32_t)((uint64_t)(*(X+rs1) ) + (uint64_t)((int16_t)sext<12>(imm) )); | ||||
|                                         int32_t res_29 = super::template read_mem<int32_t>(traits::MEM, load_address); | ||||
|                                         int32_t res_3 = super::template read_mem<int32_t>(traits::MEM, load_address); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         int32_t res = (int32_t)res_29; | ||||
|                                         int32_t res = (int32_t)res_3; | ||||
|                                         if(rd != 0) { | ||||
|                                             *(X+rd) = (uint32_t)res; | ||||
|                                         } | ||||
| @@ -802,9 +799,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t load_address = (uint32_t)((uint64_t)(*(X+rs1) ) + (uint64_t)((int16_t)sext<12>(imm) )); | ||||
|                                         uint8_t res_30 = super::template read_mem<uint8_t>(traits::MEM, load_address); | ||||
|                                         uint8_t res_4 = super::template read_mem<uint8_t>(traits::MEM, load_address); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint8_t res = res_30; | ||||
|                                         uint8_t res = res_4; | ||||
|                                         if(rd != 0) { | ||||
|                                             *(X+rd) = (uint32_t)res; | ||||
|                                         } | ||||
| @@ -833,9 +830,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t load_address = (uint32_t)((uint64_t)(*(X+rs1) ) + (uint64_t)((int16_t)sext<12>(imm) )); | ||||
|                                         uint16_t res_31 = super::template read_mem<uint16_t>(traits::MEM, load_address); | ||||
|                                         uint16_t res_5 = super::template read_mem<uint16_t>(traits::MEM, load_address); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint16_t res = res_31; | ||||
|                                         uint16_t res = res_5; | ||||
|                                         if(rd != 0) { | ||||
|                                             *(X+rd) = (uint32_t)res; | ||||
|                                         } | ||||
| @@ -1541,9 +1538,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                     else { | ||||
|                                         uint32_t xrs1 = *(X+rs1); | ||||
|                                         if(rd != 0) { | ||||
|                                             uint32_t res_32 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                             uint32_t res_6 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                             if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                             uint32_t xrd = res_32; | ||||
|                                             uint32_t xrd = res_6; | ||||
|                                             super::template write_mem<uint32_t>(traits::CSR, csr, xrs1); | ||||
|                                             if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                             *(X+rd) = xrd; | ||||
| @@ -1576,9 +1573,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                         raise(0, traits::RV_CAUSE_ILLEGAL_INSTRUCTION); | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t res_33 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         uint32_t res_7 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint32_t xrd = res_33; | ||||
|                                         uint32_t xrd = res_7; | ||||
|                                         uint32_t xrs1 = *(X+rs1); | ||||
|                                         if(rs1 != 0) { | ||||
|                                             super::template write_mem<uint32_t>(traits::CSR, csr, xrd | xrs1); | ||||
| @@ -1611,9 +1608,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                         raise(0, traits::RV_CAUSE_ILLEGAL_INSTRUCTION); | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t res_34 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         uint32_t res_8 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint32_t xrd = res_34; | ||||
|                                         uint32_t xrd = res_8; | ||||
|                                         uint32_t xrs1 = *(X+rs1); | ||||
|                                         if(rs1 != 0) { | ||||
|                                             super::template write_mem<uint32_t>(traits::CSR, csr, xrd & ~ xrs1); | ||||
| @@ -1646,9 +1643,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                         raise(0, traits::RV_CAUSE_ILLEGAL_INSTRUCTION); | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t res_35 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         uint32_t res_9 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint32_t xrd = res_35; | ||||
|                                         uint32_t xrd = res_9; | ||||
|                                         super::template write_mem<uint32_t>(traits::CSR, csr, (uint32_t)zimm); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         if(rd != 0) { | ||||
| @@ -1678,9 +1675,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                         raise(0, traits::RV_CAUSE_ILLEGAL_INSTRUCTION); | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t res_36 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         uint32_t res_10 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint32_t xrd = res_36; | ||||
|                                         uint32_t xrd = res_10; | ||||
|                                         if(zimm != 0) { | ||||
|                                             super::template write_mem<uint32_t>(traits::CSR, csr, xrd | (uint32_t)zimm); | ||||
|                                             if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
| @@ -1712,9 +1709,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                                         raise(0, traits::RV_CAUSE_ILLEGAL_INSTRUCTION); | ||||
|                                     } | ||||
|                                     else { | ||||
|                                         uint32_t res_37 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         uint32_t res_11 = super::template read_mem<uint32_t>(traits::CSR, csr); | ||||
|                                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                                         uint32_t xrd = res_37; | ||||
|                                         uint32_t xrd = res_11; | ||||
|                                         if(zimm != 0) { | ||||
|                                             super::template write_mem<uint32_t>(traits::CSR, csr, xrd & ~ ((uint32_t)zimm)); | ||||
|                                             if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
| @@ -2049,9 +2046,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                     // execute instruction | ||||
|                     { | ||||
|                         uint32_t offs = (uint32_t)((uint64_t)(*(X+rs1 + 8) ) + (uint64_t)(uimm )); | ||||
|                         int32_t res_38 = super::template read_mem<int32_t>(traits::MEM, offs); | ||||
|                         int32_t res_12 = super::template read_mem<int32_t>(traits::MEM, offs); | ||||
|                         if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                         *(X+rd + 8) = (uint32_t)(int32_t)res_38; | ||||
|                         *(X+rd + 8) = (uint32_t)(int32_t)res_12; | ||||
|                     } | ||||
|                     break; | ||||
|                 }// @suppress("No break at end of case") | ||||
| @@ -2475,9 +2472,9 @@ typename vm_base<ARCH>::virt_addr_t vm_impl<ARCH>::execute_inst(finish_cond_e co | ||||
|                         } | ||||
|                         else { | ||||
|                             uint32_t offs = (uint32_t)((uint64_t)(*(X+2) ) + (uint64_t)(uimm )); | ||||
|                             int32_t res_39 = super::template read_mem<int32_t>(traits::MEM, offs); | ||||
|                             int32_t res_13 = super::template read_mem<int32_t>(traits::MEM, offs); | ||||
|                             if(this->core.reg.trap_state>=0x80000000UL) throw memory_access_exception(); | ||||
|                             *(X+rd) = (uint32_t)(int32_t)res_39; | ||||
|                             *(X+rd) = (uint32_t)(int32_t)res_13; | ||||
|                         } | ||||
|                     } | ||||
|                     break; | ||||
|   | ||||
| @@ -97,7 +97,7 @@ protected: | ||||
|         return super::gen_cond_assign(cond, this->gen_ext(trueVal, size), this->gen_ext(falseVal, size)); | ||||
|     } | ||||
|  | ||||
|     std::tuple<continuation_e, BasicBlock *> gen_single_inst_behavior(virt_addr_t &, unsigned int &, BasicBlock *) override; | ||||
|     std::tuple<continuation_e, BasicBlock *> gen_single_inst_behavior(virt_addr_t &, BasicBlock *) override; | ||||
|  | ||||
|     void gen_leave_behavior(BasicBlock *leave_blk) override; | ||||
|     void gen_raise_trap(uint16_t trap_id, uint16_t cause); | ||||
| @@ -1490,7 +1490,7 @@ private: | ||||
|                 ), | ||||
|                 this->gen_const(8,1), | ||||
|                 this->gen_const(8,0), | ||||
|                 1), 32), | ||||
|                 8), 32), | ||||
|                 get_reg_ptr(rd + traits::X0), false); | ||||
|             } | ||||
|         } | ||||
| @@ -1543,7 +1543,7 @@ private: | ||||
|                 ), | ||||
|                 this->gen_const(8,1), | ||||
|                 this->gen_const(8,0), | ||||
|                 1), 32), | ||||
|                 8), 32), | ||||
|                 get_reg_ptr(rd + traits::X0), false); | ||||
|             } | ||||
|         } | ||||
| @@ -2057,7 +2057,7 @@ private: | ||||
|                 , | ||||
|                 this->gen_const(8,1), | ||||
|                 this->gen_const(8,0), | ||||
|                 1), 32), | ||||
|                 8), 32), | ||||
|                 get_reg_ptr(rd + traits::X0), false); | ||||
|             } | ||||
|         } | ||||
| @@ -2110,7 +2110,7 @@ private: | ||||
|                 , | ||||
|                 this->gen_const(8,1), | ||||
|                 this->gen_const(8,0), | ||||
|                 1), 32), | ||||
|                 8), 32), | ||||
|                 get_reg_ptr(rd + traits::X0), false); | ||||
|             } | ||||
|         } | ||||
| @@ -2553,11 +2553,10 @@ private: | ||||
|          | ||||
|         this->gen_instr_prologue(); | ||||
|         /*generate behavior*/ | ||||
|         auto wait_arg0 = this->gen_const(8,1); | ||||
|         std::vector<Value*> wait_args{ | ||||
|             wait_arg0 | ||||
|         std::vector<Value*> wait_231_args{ | ||||
|             this->gen_ext(this->gen_const(8,1), 32) | ||||
|         }; | ||||
|         this->builder.CreateCall(this->mod->getFunction("wait"), wait_args); | ||||
|         this->builder.CreateCall(this->mod->getFunction("wait"), wait_231_args); | ||||
|         bb = BasicBlock::Create(this->mod->getContext(), "entry", this->func, this->leave_blk); | ||||
|         auto returnValue = std::make_tuple(CONT,bb); | ||||
|          | ||||
| @@ -2719,7 +2718,7 @@ private: | ||||
|                 csr, | ||||
|                 this->builder.CreateAnd( | ||||
|                    xrd, | ||||
|                    this->builder.CreateNeg(xrs1)) | ||||
|                    this->builder.CreateNot(xrs1)) | ||||
|                 ); | ||||
|             } | ||||
|             if(rd!=0) { | ||||
| @@ -4898,7 +4897,7 @@ private: | ||||
|             }; | ||||
|             this->builder.CreateCall(this->mod->getFunction("print_disass"), args); | ||||
|         } | ||||
| 		this->gen_sync(iss::PRE_SYNC, instr_descr.size()); | ||||
|         this->gen_sync(iss::PRE_SYNC, instr_descr.size()); | ||||
|         this->builder.CreateStore(this->builder.CreateLoad(this->get_typeptr(traits::NEXT_PC), get_reg_ptr(traits::NEXT_PC), true), | ||||
|                                    get_reg_ptr(traits::PC), true); | ||||
|         this->builder.CreateStore( | ||||
| @@ -4938,7 +4937,7 @@ vm_impl<ARCH>::vm_impl(ARCH &core, unsigned core_id, unsigned cluster_id) | ||||
|  | ||||
| template <typename ARCH> | ||||
| std::tuple<continuation_e, BasicBlock *> | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, BasicBlock *this_block) { | ||||
| vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, BasicBlock *this_block) { | ||||
|     // we fetch at max 4 byte, alignment is 2 | ||||
|     enum {TRAP_ID=1<<16}; | ||||
|     code_word_t instr = 0; | ||||
| @@ -4950,9 +4949,10 @@ vm_impl<ARCH>::gen_single_inst_behavior(virt_addr_t &pc, unsigned int &inst_cnt, | ||||
|     auto res = this->core.read(paddr, 4, data); | ||||
|     if (res != iss::Ok)  | ||||
|         return std::make_tuple(ILLEGAL_FETCH, nullptr); | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001) | ||||
|     if (instr == 0x0000006f || (instr&0xffff)==0xa001){ | ||||
|         this->builder.CreateBr(this->leave_blk); | ||||
|         return std::make_tuple(JUMP_TO_SELF, nullptr); | ||||
|     ++inst_cnt; | ||||
|         } | ||||
|     uint32_t inst_index = instr_decoder.decode_instr(instr); | ||||
|     compile_func f = nullptr; | ||||
|     if(inst_index < instr_descr.size()) | ||||
| @@ -4973,6 +4973,7 @@ template <typename ARCH> | ||||
| void vm_impl<ARCH>::gen_raise_trap(uint16_t trap_id, uint16_t cause) { | ||||
|     auto *TRAP_val = this->gen_const(32, 0x80 << 24 | (cause << 16) | trap_id); | ||||
|     this->builder.CreateStore(TRAP_val, get_reg_ptr(traits::TRAP_STATE), true); | ||||
|     this->builder.CreateBr(this->trap_blk); | ||||
| } | ||||
|  | ||||
| template <typename ARCH> | ||||
| @@ -5033,6 +5034,10 @@ void vm_impl<ARCH>::gen_instr_epilogue(BasicBlock *bb) { | ||||
|     auto* icount_val = this->builder.CreateAdd( | ||||
|         this->builder.CreateLoad(this->get_typeptr(arch::traits<ARCH>::ICOUNT), get_reg_ptr(arch::traits<ARCH>::ICOUNT)), this->gen_const(64U, 1)); | ||||
|     this->builder.CreateStore(icount_val, get_reg_ptr(arch::traits<ARCH>::ICOUNT), false); | ||||
|     //increment cyclecount | ||||
|     auto* cycle_val = this->builder.CreateAdd( | ||||
|         this->builder.CreateLoad(this->get_typeptr(arch::traits<ARCH>::CYCLE), get_reg_ptr(arch::traits<ARCH>::CYCLE)), this->gen_const(64U, 1)); | ||||
|     this->builder.CreateStore(cycle_val, get_reg_ptr(arch::traits<ARCH>::CYCLE), false); | ||||
| } | ||||
|  | ||||
| } // namespace tgc5c | ||||
| @@ -5075,4 +5080,4 @@ volatile std::array<bool, 2> dummy = { | ||||
| }; | ||||
| } | ||||
| } | ||||
| // clang-format on | ||||
| // clang-format on | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user