├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ └── bug_report.md └── workflows │ └── ci.yml ├── .gitignore ├── .gitmodules ├── Kconfig ├── LICENSE ├── Makefile ├── README.md ├── configs ├── .gitignore ├── riscv32-pa_defconfig ├── riscv64-dual-xs-ref-debug_defconfig ├── riscv64-dual-xs-ref_defconfig ├── riscv64-gem5-multicore-ref_defconfig ├── riscv64-gem5-ref_defconfig ├── riscv64-nutshell-diff-spike_defconfig ├── riscv64-nutshell-ref_defconfig ├── riscv64-nutshell_defconfig ├── riscv64-pa_defconfig ├── riscv64-spm-ref-xs_defconfig ├── riscv64-spm-xs_defconfig ├── riscv64-xs-ahead-ref_defconfig ├── riscv64-xs-clang-ref_defconfig ├── riscv64-xs-clang_defconfig ├── riscv64-xs-cpt-with-flash_defconfig ├── riscv64-xs-cpt-with-libcheckpoint_defconfig ├── riscv64-xs-cpt_defconfig ├── riscv64-xs-diff-spike-agnostic_defconfig ├── riscv64-xs-diff-spike-withflash_defconfig ├── riscv64-xs-diff-spike_defconfig ├── riscv64-xs-fpga_defconfig ├── riscv64-xs-novga_defconfig ├── riscv64-xs-ref-debug_defconfig ├── riscv64-xs-ref_bitmap_defconfig ├── riscv64-xs-ref_defconfig ├── riscv64-xs-southlake-debug_defconfig ├── riscv64-xs-southlake-fpga_defconfig ├── riscv64-xs-southlake-ref-debug_defconfig ├── riscv64-xs-southlake-ref_defconfig ├── riscv64-xs-southlake_defconfig ├── riscv64-xs-spmem-ref_defconfig ├── riscv64-xs-spmem-so-ref_defconfig ├── riscv64-xs_bitmap_defconfig └── riscv64-xs_defconfig ├── include ├── base │ └── output.h ├── checkpoint │ ├── cpt_env.h │ ├── path_manager.h │ ├── semantic_point.h │ ├── serializer.h │ └── simpoint.h ├── common.h ├── cpu │ ├── cpu.h │ ├── decode.h │ ├── difftest.h │ ├── exec.h │ └── ifetch.h ├── debug.h ├── device │ ├── alarm.h │ ├── flash.h │ ├── map.h │ └── mmio.h ├── iostream3 │ └── zfstream.h ├── isa.h ├── macro.h ├── memory │ ├── host-tlb.h │ ├── host.h │ ├── image_loader.h │ ├── paddr.h │ ├── sparseram.h │ ├── store_queue_wrapper.h │ └── vaddr.h ├── profiling │ └── profiling_control.h ├── rtl │ ├── fp.h │ ├── pseudo.h │ └── rtl.h └── utils.h ├── lib-include └── difftest.h ├── modify_pointdir_structure.py ├── onesimpoint.py ├── resource ├── .gitignore ├── bbl │ ├── Makefile │ ├── bbl.lds │ └── src │ │ ├── atomic.h │ │ ├── bits.h │ │ ├── disabled_hart_mask.h │ │ ├── emulation.c │ │ ├── emulation.h │ │ ├── encoding.h │ │ ├── flush_icache.c │ │ ├── mcall.h │ │ ├── mentry.S │ │ ├── misaligned_ldst.c │ │ ├── mtrap.c │ │ ├── mtrap.h │ │ ├── muldiv_emulation.c │ │ ├── pk.h │ │ ├── snprintf.c │ │ ├── unprivileged_memory.h │ │ └── vm.h ├── debian │ └── README.md ├── mips-elf │ ├── README.md │ └── mips.dummy ├── sdcard │ ├── README.md │ └── nemu.c ├── simpoint │ └── do_simpoint_clustering.py └── softfloat │ └── .gitignore ├── scripts ├── Dockerfile ├── Libcheckpoint.mk ├── LibcheckpointAlpha.mk ├── build.mk ├── checkpoint_example │ ├── checkpoint.sh │ ├── checkpoint_env.sh │ ├── cluster.sh │ ├── manual_oneshot_cpt.sh │ ├── manual_uniform_cpt.sh │ ├── profiling.sh │ ├── rebuild_fw_payload.sh │ ├── repo.list │ ├── semantic_checkpoint.sh │ └── uniform_cpt.sh ├── compilation-test.sh ├── config.mk ├── generate_so_for_difftest.sh ├── generate_so_from_docker.sh ├── git.mk ├── isa.mk ├── manual-take.sh ├── nanopb.mk ├── repos.mk ├── restore_zstd.sh ├── restore_zstd_into_flash.sh ├── runall.sh ├── softfloat.mk ├── take_zstd.sh └── take_zstd_into_flash.sh ├── src ├── base │ └── output.cpp ├── checkpoint │ ├── cpt_env.c │ ├── path_manager.cpp │ ├── semantic_point.cpp │ ├── serializer.cpp │ └── simpoint.cpp ├── cpu │ ├── cpu-exec.c │ ├── difftest │ │ ├── dut.c │ │ └── ref.c │ └── tcache.c ├── device │ ├── Kconfig │ ├── alarm.c │ ├── audio.c │ ├── device.c │ ├── disk.c │ ├── flash.c │ ├── intr.c │ ├── io │ │ ├── map.c │ │ ├── mmio.c │ │ └── port-io.c │ ├── keyboard.c │ ├── mmc.h │ ├── plic.c │ ├── sdcard.c │ ├── serial.c │ ├── timer.c │ ├── uart_snps.c │ ├── uartlite.c │ └── vga.c ├── engine │ └── interpreter │ │ ├── c_op.h │ │ ├── fp.c │ │ ├── host-fp.h │ │ ├── hostcall.c │ │ ├── init.c │ │ ├── rtl-basic.h │ │ ├── rtl-fp.h │ │ └── softfloat-fp.h ├── iostream3 │ └── zfstream.cpp ├── isa │ ├── mips32 │ │ ├── difftest │ │ │ ├── dut.c │ │ │ └── ref.c │ │ ├── include │ │ │ ├── isa-all-instr.h │ │ │ ├── isa-def.h │ │ │ └── isa-exec.h │ │ ├── init.c │ │ ├── instr │ │ │ ├── compute.h │ │ │ ├── control.h │ │ │ ├── decode.c │ │ │ ├── ldst.h │ │ │ ├── muldiv.h │ │ │ ├── special.h │ │ │ └── system.h │ │ ├── local-include │ │ │ ├── intr.h │ │ │ ├── reg.h │ │ │ └── rtl.h │ │ ├── logo.c │ │ ├── reg.c │ │ └── system │ │ │ ├── intr.c │ │ │ ├── mmu.c │ │ │ └── priv.c │ ├── riscv32 │ │ ├── difftest │ │ │ ├── dut.c │ │ │ └── ref.c │ │ ├── include │ │ │ ├── isa-all-instr.h │ │ │ ├── isa-def.h │ │ │ └── isa-exec.h │ │ ├── init.c │ │ ├── instr │ │ │ ├── compress.h │ │ │ ├── compute.h │ │ │ ├── control.h │ │ │ ├── decode.c │ │ │ ├── ldst.h │ │ │ ├── muldiv.h │ │ │ ├── pseudo.h │ │ │ ├── special.h │ │ │ └── system.h │ │ ├── local-include │ │ │ ├── intr.h │ │ │ ├── reg.h │ │ │ └── rtl.h │ │ ├── logo.c │ │ ├── reg.c │ │ └── system │ │ │ ├── intr.c │ │ │ ├── mmu.c │ │ │ └── priv.c │ ├── riscv64 │ │ ├── Kconfig │ │ ├── clint.c │ │ ├── difftest │ │ │ ├── dut.c │ │ │ └── ref.c │ │ ├── include │ │ │ ├── isa-all-instr.h │ │ │ ├── isa-def.h │ │ │ └── isa-exec.h │ │ ├── init.c │ │ ├── instr │ │ │ ├── decode.c │ │ │ ├── fp.c │ │ │ ├── priv │ │ │ │ ├── decode.h │ │ │ │ ├── exec.h │ │ │ │ └── system.c │ │ │ ├── pseudo.h │ │ │ ├── rocc.h │ │ │ ├── rva │ │ │ │ ├── amo.c │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvb │ │ │ │ ├── exec.h │ │ │ │ ├── rvintrin.c │ │ │ │ └── rvintrin.h │ │ │ ├── rvc │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvcbo │ │ │ │ ├── cbo_impl.c │ │ │ │ ├── cbo_impl.h │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvd │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvf │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvi │ │ │ │ ├── compute.h │ │ │ │ ├── control.h │ │ │ │ ├── decode.h │ │ │ │ ├── exec.h │ │ │ │ ├── fence.h │ │ │ │ ├── ldst.h │ │ │ │ └── ldst_trig.h │ │ │ ├── rvk │ │ │ │ ├── aes_common.c │ │ │ │ ├── aes_common.h │ │ │ │ ├── crypto_impl.c │ │ │ │ ├── crypto_impl.h │ │ │ │ ├── decode.h │ │ │ │ ├── exec.h │ │ │ │ └── sm4_common.h │ │ │ ├── rvm │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvv │ │ │ │ ├── decode.h │ │ │ │ ├── exec.h │ │ │ │ ├── vcfg.h │ │ │ │ ├── vcommon.c │ │ │ │ ├── vcommon.h │ │ │ │ ├── vcompute.h │ │ │ │ ├── vcompute_impl.c │ │ │ │ ├── vcompute_impl.h │ │ │ │ ├── vldst.h │ │ │ │ ├── vldst_impl.c │ │ │ │ ├── vldst_impl.h │ │ │ │ ├── vreg.h │ │ │ │ └── vreg_impl.c │ │ │ ├── rvzfa │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvzfh │ │ │ │ ├── decode.h │ │ │ │ └── exec.h │ │ │ ├── rvzicond │ │ │ │ └── exec.h │ │ │ ├── special.h │ │ │ └── vp.c │ │ ├── local-include │ │ │ ├── aia.h │ │ │ ├── csr.h │ │ │ ├── encoding.h │ │ │ ├── intr.h │ │ │ ├── reg.h │ │ │ ├── rtl.h │ │ │ ├── trapinfo.h │ │ │ ├── trigger.h │ │ │ └── vreg.h │ │ ├── logo.c │ │ ├── reg.c │ │ └── system │ │ │ ├── aia.c │ │ │ ├── intr.c │ │ │ ├── mmu.c │ │ │ ├── priv.c │ │ │ ├── timer.c │ │ │ └── trigger.c │ └── x86 │ │ ├── Kconfig │ │ ├── device │ │ ├── i8237a.c │ │ ├── i8253.c │ │ ├── i8259a.c │ │ ├── ioport80.c │ │ └── mc146818rtc.c │ │ ├── difftest │ │ ├── dut.c │ │ └── ref.c │ │ ├── exec │ │ ├── all-instr.h │ │ ├── exec.c │ │ └── fp.h │ │ ├── include │ │ ├── isa-all-instr.h │ │ ├── isa-def.h │ │ └── isa-exec.h │ │ ├── init.c │ │ ├── instr │ │ ├── arith.h │ │ ├── bit.h │ │ ├── cc.h │ │ ├── control.h │ │ ├── data-mov.h │ │ ├── decode.c │ │ ├── eflags.c │ │ ├── eflags.h │ │ ├── fp.c │ │ ├── lazycc.h │ │ ├── logic.h │ │ ├── misc.h │ │ ├── rt.h │ │ ├── special.h │ │ ├── string.h │ │ ├── system.h │ │ └── vector.h │ │ ├── kvm │ │ └── kvm.c │ │ ├── local-include │ │ ├── intr.h │ │ ├── reg.h │ │ └── rtl.h │ │ ├── logo.c │ │ ├── reg.c │ │ └── system │ │ ├── intr.c │ │ ├── mmu.c │ │ └── priv.c ├── memory │ ├── Kconfig │ ├── host-tlb.c │ ├── paddr.c │ ├── sparseram.cpp │ ├── store_queue_wrapper.cpp │ └── vaddr.c ├── monitor │ ├── aligncheck.c │ ├── image_loader.c │ ├── monitor.c │ ├── ui.c │ └── watchpoint.c ├── nemu-main.c ├── profiling │ └── profiling_control.c ├── user │ ├── init │ │ ├── riscv64.c │ │ └── x86.c │ ├── loader.c │ ├── memory.c │ ├── syscall-riscv64.h │ ├── syscall-x86.h │ ├── syscall.c │ └── user.h └── utils │ ├── br_log.c │ ├── dynamic-config.c │ ├── expr.c │ ├── filename.c │ ├── iqueue.c │ ├── log.c │ ├── state.c │ └── timer.c └── tools ├── export ├── .gitignore ├── ics-export.py ├── rv64-export.py └── utils.py ├── fixdep ├── Makefile └── fixdep.c ├── gen-expr ├── .gitignore ├── Makefile └── gen-expr.c ├── kconfig ├── .gitignore ├── Makefile ├── conf.c ├── confdata.c ├── expr.c ├── expr.h ├── lexer.l ├── list.h ├── lkc.h ├── lkc_proto.h ├── lxdialog │ ├── checklist.c │ ├── dialog.h │ ├── inputbox.c │ ├── menubox.c │ ├── textbox.c │ ├── util.c │ └── yesno.c ├── mconf.c ├── menu.c ├── parser.y ├── preprocess.c ├── symbol.c └── util.c ├── kvm-diff ├── Makefile ├── include │ └── paddr.h └── src │ └── kvm.c ├── qemu-dl-diff ├── Makefile ├── include │ └── common.h └── src │ ├── diff-test.c │ ├── hack.c │ └── isa │ ├── mips32 │ └── init.c │ ├── riscv32 │ └── init.c │ ├── riscv64 │ └── init.c │ └── x86 │ ├── init.c │ └── intr.c └── qemu-socket-diff ├── Makefile ├── include ├── common.h ├── isa │ ├── mips32.h │ ├── riscv32.h │ ├── riscv64.h │ └── x86.h └── protocol.h └── src ├── diff-test.c ├── gdb-host.c ├── isa ├── mips32 │ └── init.c ├── riscv32 │ └── init.c ├── riscv64 │ └── init.c └── x86 │ └── init.c └── protocol.c /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @good-circle @Anzooooo @cebarobot 2 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | 12 | - What workload are you running? Bare-metal workload, bbl+Linux+application or checkpoints? 13 | - What is the expected behavior? What is the actual behavior? 14 | - Can this issue be consistently reproduced? If so, what are the steps to reproduce? Can this issue be reproduced in latest master branch? 15 | - Can you upload your bare-metal image or checkpoint to somewhere to help us to reproduce the issue? 16 | 17 | **To Reproduce** 18 | Steps to reproduce the behavior: 19 | - Build command: typically `make xxx_config; make -j` 20 | - Running command: typically 21 | ``` 22 | ./build/riscv64-nemu-interpreter /path/to/checkpoint-or-image -b 23 | ``` 24 | - The link to your image or checkpoint; Or the way to build the image (if not to complicated to build) 25 | 26 | **Expected behavior** 27 | A clear and concise description of what you expected to happen. 28 | 29 | **Error log or Screenshots** 30 | 31 | If applicable, plz provide you error log. 32 | 33 | If applicable, add screenshots to help explain your problem. 34 | 35 | 36 | **Necessary information on versions** 37 | - NEMU version: [e.g. master branch or commid: deadbeaf] 38 | 39 | **Additional information** 40 | If you build the full-system image your self, you can optionally provide version information of following components: 41 | - riscv-linux 42 | - riscv-rootfs 43 | - initramfs.txt (attch it if possible) 44 | - riscv-pk or OpenSBI 45 | - GCPT restorer (checkpoint restorer), it might be NEMU in older versions or OpenXiangShan/LibCheckpointAlpha or OpenXiangShan/LibCheckpoint in newer versions 46 | 47 | **Additional context** 48 | Add any other context about the problem here. 49 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.* 2 | * 3 | !*/ 4 | !Makefile 5 | !*.mk 6 | !*.[cSh] 7 | !*.cpp 8 | !*.lds 9 | !.gitignore 10 | !Dockerfile 11 | !README.md 12 | !Kconfig 13 | !.github/workflows/ci.yml 14 | !scripts/*.sh 15 | !scripts/checkpoint_example/* 16 | include/config 17 | include/generated 18 | build 19 | .vscode 20 | .idea 21 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "ready-to-run"] 2 | path = ready-to-run 3 | url = https://github.com/OpenXiangShan/ready-to-run.git 4 | [submodule "resource/simpoint/simpoint_repo"] 5 | path = resource/simpoint/simpoint_repo 6 | url = https://github.com/shinezyy/SimPoint.3.2-fix.git 7 | -------------------------------------------------------------------------------- /configs/.gitignore: -------------------------------------------------------------------------------- 1 | !*_defconfig 2 | -------------------------------------------------------------------------------- /configs/riscv32-pa_defconfig: -------------------------------------------------------------------------------- 1 | CONFIG_CC_CLANG=y 2 | CONFIG_CC_O3=y 3 | CONFIG_CC_DEBUG=y 4 | # CONFIG_VGA_SHOW_SCREEN is not set 5 | -------------------------------------------------------------------------------- /configs/riscv64-pa_defconfig: -------------------------------------------------------------------------------- 1 | CONFIG_ISA_riscv64=y 2 | # CONFIG_CC_CLANG=y 3 | CONFIG_CC_O3=y 4 | CONFIG_PC_RESET_OFFSET=0x0 5 | # CONFIG_VGA_SHOW_SCREEN is not set 6 | -------------------------------------------------------------------------------- /include/checkpoint/cpt_env.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __CHECKPOINT_CPT_ENV__ 17 | #define __CHECKPOINT_CPT_ENV__ 18 | 19 | enum { GZ_FORMAT, ZSTD_FORMAT }; 20 | 21 | extern char *output_base_dir; 22 | extern char *config_name; 23 | extern char *workload_name; 24 | extern char *simpoints_dir; 25 | extern int cpt_id; 26 | extern char *cpt_file; 27 | extern char *restorer; 28 | extern char compress_file_format; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /include/checkpoint/path_manager.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | // 17 | // Created by zyy on 2020/11/21. 18 | // 19 | 20 | #ifndef NEMU_PATH_MANAGER_H 21 | #define NEMU_PATH_MANAGER_H 22 | 23 | #include 24 | #include 25 | 26 | namespace fs = std::filesystem; 27 | 28 | class PathManager 29 | { 30 | 31 | std::string statsBaseDir; 32 | std::string configName; 33 | std::string workloadName; 34 | 35 | uint64_t cptID; 36 | 37 | std::string workloadPath; 38 | fs::path outputPath; 39 | fs::path simpointPath; 40 | 41 | public: 42 | void init(); 43 | 44 | void incCptID(); 45 | 46 | uint64_t getCptID() const {return cptID;} 47 | 48 | std::string getOutputPath() const; 49 | 50 | std::string getWorkloadPath() const {return workloadPath;}; 51 | 52 | std::string getSimpointPath() const; 53 | 54 | void setSimpointProfilingOutputDir(); 55 | void setCheckpointingOutputDir(); 56 | }; 57 | 58 | extern PathManager pathManager; 59 | 60 | #endif //NEMU_PATH_MANAGER_H 61 | -------------------------------------------------------------------------------- /include/checkpoint/semantic_point.h: -------------------------------------------------------------------------------- 1 | #ifndef __SEMANTIC_POINT__ 2 | #define __SEMANTIC_POINT__ 3 | 4 | #include "common.h" 5 | 6 | void semantic_point_init(const char* semantic_point_path); 7 | void semantic_point_profile(vaddr_t pc, bool is_control, uint64_t instr_count); 8 | bool check_semantic_point(); 9 | bool enable_semantic_point_cpt(); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /include/common.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #ifndef __COMMON_H__ 18 | #define __COMMON_H__ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | #if CONFIG_MBASE + CONFIG_MSIZE > 0x100000000ul || defined(CONFIG_SHARE) 29 | #define PMEM64 1 30 | #endif 31 | 32 | typedef MUXDEF(CONFIG_ISA64, uint64_t, uint32_t) word_t; 33 | typedef MUXDEF(CONFIG_ISA64, int64_t, int32_t) sword_t; 34 | #define FMT_WORD MUXDEF(CONFIG_ISA64, "0x%016lx", "0x%08x") 35 | 36 | typedef word_t rtlreg_t; 37 | typedef word_t vaddr_t; 38 | typedef MUXDEF(PMEM64, uint64_t, uint32_t) paddr_t; 39 | #define FMT_PADDR MUXDEF(PMEM64, "0x%016lx", "0x%08x") 40 | typedef uint16_t ioaddr_t; 41 | 42 | #define CP printf("%s: %d\n", __FILE__, __LINE__);fflush( stdout ); 43 | struct DynamicConfig { 44 | bool ignore_illegal_mem_access; 45 | bool debug_difftest; 46 | bool enable_store_log; 47 | }; 48 | extern struct DynamicConfig dynamic_config; 49 | void update_dynamic_config(void* config); 50 | 51 | #include 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /include/cpu/exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __CPU_EXEC_H__ 17 | #define __CPU_EXEC_H__ 18 | 19 | #include 20 | 21 | #ifdef CONFIG_PERF_OPT 22 | #define finish_label exec_finish 23 | #define def_label(l) l: 24 | #define def_EHelper(name) \ 25 | s ++; \ 26 | goto finish_label; /* this is for the previous def_EHelper() */ \ 27 | def_label(concat(exec_, name)) 28 | #define def_finish() def_label(finish_label) 29 | #else 30 | #define def_EHelper(name) static inline void concat(exec_, name) (Decode *s) 31 | #endif 32 | 33 | #if 0 34 | #define IDEXW(idx, id, ex, w) CASE_ENTRY(idx, concat(decode_, id), concat(exec_, ex), w) 35 | #define IDEX(idx, id, ex) IDEXW(idx, id, ex, 0) 36 | #define EXW(idx, ex, w) IDEXW(idx, empty, ex, w) 37 | #define EX(idx, ex) EXW(idx, ex, 0) 38 | #define EMPTY(idx) EX(idx, inv) 39 | 40 | // set_width() is defined in src/isa/$isa/exec/exec.c 41 | #define CASE_ENTRY(idx, id, ex, w) case idx: set_width(s, w); id(s); ex(s); break; 42 | #endif 43 | 44 | #endif 45 | -------------------------------------------------------------------------------- /include/cpu/ifetch.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __CPU_IFETCH_H__ 17 | 18 | #include 19 | 20 | static inline uint32_t instr_fetch(vaddr_t *pc, int len) { 21 | uint32_t instr = vaddr_ifetch(*pc, len); 22 | #ifdef ENABLE_DIFFTEST_INSTR_QUEUE 23 | extern void add_instr(uint8_t *instr, int len); 24 | add_instr((void *)&instr, len); 25 | #endif 26 | #ifdef CONFIG_DEBUG 27 | uint8_t *p_instr = (void *)&instr; 28 | int i; 29 | extern char log_bytebuf[80]; 30 | for (i = 0; i < len; i ++) { 31 | int l = strlen(log_bytebuf); 32 | snprintf(log_bytebuf + l, sizeof(log_bytebuf) - l, "%02x ", p_instr[i]); 33 | } 34 | #endif 35 | (*pc) += len; 36 | return instr; 37 | } 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /include/device/alarm.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __DEVICE_ALARM_H__ 17 | #define __DEVICE_ALARM_H__ 18 | 19 | typedef void (*alarm_handler_t) (); 20 | void add_alarm_handle(alarm_handler_t h); 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /include/device/flash.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __DEVICE_FLASH_H__ 17 | #define __DEVICE_FLASH_H__ 18 | 19 | #ifdef CONFIG_HAS_FLASH 20 | uint8_t* get_flash_base(); 21 | uint64_t get_flash_size(); 22 | #endif 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /include/device/mmio.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __DEVICE_MMIO_H__ 17 | #define __DEVICE_MMIO_H__ 18 | 19 | #include 20 | 21 | bool mmio_is_real_device(paddr_t addr); 22 | word_t mmio_read(paddr_t addr, int len); 23 | void mmio_write(paddr_t addr, int len, word_t data); 24 | bool is_in_mmio(paddr_t addr); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /include/memory/host-tlb.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MEMORY_HOST_TLB_H__ 17 | #define __MEMORY_HOST_TLB_H__ 18 | 19 | #include 20 | 21 | struct Decode; 22 | word_t hosttlb_read(struct Decode *s, vaddr_t vaddr, int len, int type); 23 | void hosttlb_write(struct Decode *s, vaddr_t vaddr, int len, word_t data); 24 | void hosttlb_init(); 25 | void hosttlb_flush(vaddr_t vaddr); 26 | 27 | #endif 28 | -------------------------------------------------------------------------------- /include/memory/host.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MEMORY_HOST_H__ 17 | #define __MEMORY_HOST_H__ 18 | 19 | #include 20 | 21 | static inline word_t host_read(void *addr, int len) { 22 | switch (len) { 23 | case 1: 24 | Logm("load: addr = %p, len = %d, data = 0x%x", addr, len, *(uint8_t *)addr); 25 | return *(uint8_t *)addr; 26 | case 2: 27 | Logm("load: addr = %p, len = %d, data = 0x%x", addr, len, *(uint16_t *)addr); 28 | return *(uint16_t *)addr; 29 | case 4: 30 | Logm("load: addr = %p, len = %d, data = 0x%x", addr, len, *(uint32_t *)addr); 31 | return *(uint32_t *)addr; 32 | #ifdef CONFIG_ISA64 33 | case 8: 34 | Logm("load: addr = %p, len = %d, data = 0x%lx", addr, len, *(uint64_t *)addr); 35 | return *(uint64_t *)addr; 36 | #endif 37 | default: MUXDEF(CONFIG_RT_CHECK, assert(0), return 0); 38 | } 39 | } 40 | 41 | static inline void host_write(void *addr, int len, word_t data) { 42 | Logm("write: addr = %p, len = %d, data = 0x%lx", addr, len, data); 43 | switch (len) { 44 | case 1: *(uint8_t *)addr = data; return; 45 | case 2: *(uint16_t *)addr = data; return; 46 | case 4: *(uint32_t *)addr = data; return; 47 | IFDEF(CONFIG_ISA64, case 8: *(uint64_t *)addr = data; return); 48 | IFDEF(CONFIG_RT_CHECK, default: assert(0)); 49 | } 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /include/memory/image_loader.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __IMAGE_LOADER_H__ 17 | #define __IMAGE_LOADER_H__ 18 | 19 | #include 20 | 21 | 22 | long load_gz_img(const char *filename); 23 | 24 | long load_zstd_img(const char *filename); 25 | 26 | long load_img(char *img_name, const char *which_img, uint8_t* load_start, size_t img_size); 27 | 28 | void fill_memory(const char* img_file, const char* flash_image, const char* cpt_image, int64_t* img_size, int64_t* flash_size); 29 | 30 | #endif // __IMAGE_LOADER_H__ 31 | -------------------------------------------------------------------------------- /include/memory/store_queue_wrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef NEMU_STORE_QUEUE_WRAPPER_H 2 | #define NEMU_STORE_QUEUE_WRAPPER_H 3 | 4 | #include 5 | #include 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | #ifdef CONFIG_STORE_LOG 12 | void store_log_stack_reset(); 13 | void store_log_stack_push(store_log_t log); 14 | void store_log_stack_pop(); 15 | store_log_t store_log_stack_top(); 16 | bool store_log_stack_empty(); 17 | #ifdef CONFIG_LIGHTQS 18 | void spec_store_log_stack_reset(); 19 | void spec_store_log_stack_push(store_log_t log); 20 | void spec_store_log_stack_pop(); 21 | store_log_t spec_store_log_stack_top(); 22 | bool spec_store_log_stack_empty(); 23 | void spec_store_log_stack_copy(); 24 | #endif // CONFIG_LIGHTQS 25 | #endif // CONFIG_STORE_LOG 26 | 27 | #ifdef CONFIG_DIFFTEST_STORE_COMMIT 28 | void store_queue_reset(); 29 | void store_queue_push(store_commit_t store_commit); 30 | void store_queue_pop(); 31 | store_commit_t store_queue_front(); 32 | store_commit_t store_queue_back(); 33 | size_t store_queue_size(); 34 | bool store_queue_empty(); 35 | #endif //CONFIG_DIFFTEST_STORE_COMMIT 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | 41 | #endif //NEMU_STORE_QUEUE_WRAPPER_H 42 | -------------------------------------------------------------------------------- /include/memory/vaddr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MEMORY_VADDR_H__ 17 | #define __MEMORY_VADDR_H__ 18 | 19 | #include 20 | 21 | struct Decode; 22 | word_t vaddr_ifetch(vaddr_t addr, int len); 23 | word_t vaddr_read(struct Decode *s, vaddr_t addr, int len, int mmu_mode); 24 | void vaddr_write(struct Decode *s, vaddr_t addr, int len, word_t data, int mmu_mode); 25 | 26 | word_t vaddr_read_safe(vaddr_t addr, int len); 27 | 28 | #define PAGE_SHIFT 12 29 | #define PAGE_SIZE (1ul << PAGE_SHIFT) 30 | #define PAGE_MASK (PAGE_SIZE - 1) 31 | 32 | int force_raise_pf(vaddr_t vaddr, int type); 33 | int force_raise_gpf(vaddr_t vaddr, int type); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /include/profiling/profiling_control.h: -------------------------------------------------------------------------------- 1 | #ifndef __PROFILING_CONTROL_H__ 2 | #define __PROFILING_CONTROL_H__ 3 | 4 | #include 5 | 6 | enum ProfilingState{ 7 | NoProfiling =0, 8 | SimpointProfiling, 9 | }; 10 | 11 | enum CheckpointState{ 12 | NoCheckpoint=0, 13 | SimpointCheckpointing, 14 | SemanticCheckpointing, 15 | UniformCheckpointing, 16 | ManualOneShotCheckpointing, 17 | ManualUniformCheckpointing, 18 | CheckpointOnNEMUTrap, 19 | }; 20 | 21 | extern int profiling_state; 22 | extern int checkpoint_state; 23 | extern uint64_t checkpoint_interval; 24 | extern uint64_t warmup_interval; 25 | extern uint64_t checkpoint_icount_base; 26 | 27 | extern bool recvd_manual_oneshot_cpt; 28 | extern bool recvd_manual_uniform_cpt; 29 | 30 | extern bool force_cpt_mmode; 31 | 32 | extern bool workload_loaded; 33 | extern bool donot_skip_boot; 34 | 35 | void start_profiling(); 36 | 37 | #endif // __PROFILING_CONTROL_H__ 38 | -------------------------------------------------------------------------------- /modify_pointdir_structure.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import subprocess 3 | import os 4 | import time 5 | import signal 6 | import argparse 7 | import shutil 8 | 9 | parser = argparse.ArgumentParser() 10 | parser.add_argument('-d', '--cpts-dir', type=str, required=True) 11 | args = parser.parse_args() 12 | base_dir = args.cpts_dir 13 | 14 | # base_dir = '/nfs/home/share/zhangchuanqi/gcpt_cpts/simpoint-spec06-gcb-o2-50M' 15 | 16 | os.chdir(base_dir) 17 | 18 | workloads = os.listdir(base_dir) 19 | workloads = list(filter(lambda x: len(x.split('_'))<=2, workloads)) 20 | 21 | print(workloads) 22 | 23 | for w in workloads: 24 | workload_dir = os.path.join(base_dir, w) 25 | cpt_idxs = os.listdir(workload_dir) 26 | for cidx in cpt_idxs: 27 | if not cidx.isdigit(): 28 | continue 29 | cpt_dir = os.path.join(workload_dir, cidx) 30 | cpt_files = os.listdir(cpt_dir) 31 | for cpt_file in cpt_files: 32 | if cpt_file.endswith('gz'): 33 | try: 34 | weight = float(cpt_file.split('_')[-2]) 35 | except: 36 | weight = 0.0 37 | if weight < 0.000001: 38 | continue 39 | cpt_path = os.path.join(cpt_dir, cpt_file) 40 | new_cpt_dirname = f'{w}{cpt_file[:-4]}' 41 | key = cpt_file.split('_')[1] 42 | new_cpt_dir = os.path.join(base_dir, new_cpt_dirname, '0') 43 | os.makedirs(new_cpt_dir, exist_ok=True) 44 | new_cpt_path = os.path.join(new_cpt_dir, f'_{key}_.gz') 45 | # print(f'mv {cpt_path} {new_cpt_path}') 46 | shutil.move(cpt_path, new_cpt_path) 47 | print(f'rm -rf {workload_dir}') 48 | shutil.rmtree(workload_dir) 49 | -------------------------------------------------------------------------------- /onesimpoint.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | import subprocess 3 | import os 4 | import time 5 | import signal 6 | import argparse 7 | 8 | parser = argparse.ArgumentParser() 9 | parser.add_argument("--cluster", type=str, required=True) 10 | parser.add_argument('--cpt-interval', type=int, default=100000000) 11 | parser.add_argument('-w', '--workload', type=str, required=True) 12 | parser.add_argument('-b', '--bbl', type=str, required=True) 13 | parser.add_argument("--nemu-home", type=str, default='/nfs/home/zhangchuanqi/lvna/for_xs/xs-env/nemu-tracing') 14 | parser.add_argument('--simpoint-cluster-dir', type=str, default='/nfs/home/share/zhangchuanqi/gcpt_cluster') 15 | parser.add_argument('--cpts-dir', type=str, default='/nfs/home/share/zhangchuanqi/gcpt_cpts') 16 | 17 | args = parser.parse_args() 18 | 19 | os.environ['NEMU_HOME'] = args.nemu_home 20 | os.chdir(args.nemu_home) 21 | 22 | 23 | generate_bbv_cmd = f'./build/riscv64-nemu-interpreter -b -D outputs -C {args.cluster} ' + \ 24 | f'--cpt-interval {args.cpt_interval} ' + \ 25 | '-r resource/gcpt_restore/build/gcpt.bin ' + \ 26 | f'{args.bbl} ' + \ 27 | f'-w {args.workload} ' + \ 28 | '--simpoint-profile ' 29 | 30 | print(generate_bbv_cmd) 31 | # subprocess.run(generate_bbv_cmd, shell=True) 32 | 33 | real_cluster_dir = os.path.join(args.simpoint_cluster_dir, args.cluster) 34 | real_workload_simpoint_dir = os.path.join(args.simpoint_cluster_dir, args.cluster, args.workload) 35 | os.makedirs(real_workload_simpoint_dir, exist_ok=True) 36 | 37 | generate_weights_cmd = f'./resource/simpoint/simpoint_repo/bin/simpoint \ 38 | -loadFVFile ./outputs/{args.cluster}/{args.workload}/simpoint_bbv.gz \ 39 | -saveSimpoints {real_workload_simpoint_dir}/simpoints0 -saveSimpointWeights {real_workload_simpoint_dir}/weights0 \ 40 | -inputVectorsGzipped -maxK 30 -numInitSeeds 2 -iters 1000 -seedkm 123456 -seedproj 654321' 41 | 42 | 43 | print(generate_weights_cmd) 44 | # subprocess.run(generate_weights_cmd, shell=True) 45 | 46 | take_cpt_cmd = f'./build/riscv64-nemu-interpreter {args.bbl} \ 47 | -D {args.cpts_dir} -w {args.workload} -C {args.cluster} \ 48 | -b -S {real_cluster_dir} --cpt-interval {args.cpt_interval} \ 49 | -r resource/gcpt_restore/build/gcpt.bin' 50 | 51 | print(take_cpt_cmd) 52 | subprocess.run(take_cpt_cmd, shell=True) -------------------------------------------------------------------------------- /resource/.gitignore: -------------------------------------------------------------------------------- 1 | gcpt_restore 2 | nanopb 3 | LibCheckpoint 4 | -------------------------------------------------------------------------------- /resource/bbl/Makefile: -------------------------------------------------------------------------------- 1 | NAME = bbl 2 | 3 | BUILD_DIR ?= ./build 4 | 5 | OBJ_DIR ?= $(BUILD_DIR)/obj 6 | BINARY ?= $(BUILD_DIR)/$(NAME) 7 | 8 | .DEFAULT_GOAL = app 9 | 10 | # Compilation flags 11 | CROSS_COMPILE = riscv64-linux-gnu- 12 | CC = $(CROSS_COMPILE)gcc 13 | LD = $(CROSS_COMPILE)ld 14 | OBJDUMP = $(CROSS_COMPILE)objdump 15 | OBJCOPY = $(CROSS_COMPILE)objcopy 16 | INCLUDES = $(addprefix -I, $(INC_DIR)) 17 | CFLAGS += -fno-PIE -mcmodel=medany -O2 -MMD -Wall -Werror $(INCLUDES) 18 | 19 | # Files to be compiled 20 | SRCS = $(shell find src/ -name "*.[cS]") 21 | OBJS = $(addprefix $(OBJ_DIR)/, $(addsuffix .o, $(basename $(SRCS)))) 22 | 23 | # Compilation patterns 24 | $(OBJ_DIR)/%.o: %.c 25 | @mkdir -p $(dir $@) && echo + CC $< 26 | @$(CC) $(CFLAGS) -c -o $@ $< 27 | 28 | $(OBJ_DIR)/%.o: %.S 29 | @mkdir -p $(dir $@) && echo + AS $< 30 | @$(CC) $(CFLAGS) -c -o $@ $< 31 | 32 | 33 | # Dependencies 34 | -include $(OBJS:.o=.d) 35 | 36 | $(BINARY): $(OBJS) 37 | @echo + LD $@ 38 | @$(LD) -O2 -T bbl.lds -o $@ $^ 39 | @$(OBJDUMP) -d $@ > $@.txt 40 | @$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $@ $@.bin 41 | 42 | app: $(BINARY) 43 | 44 | clean: 45 | -rm -rf $(BUILD_DIR) 46 | -------------------------------------------------------------------------------- /resource/bbl/src/bits.h: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | #ifndef _RISCV_BITS_H 4 | #define _RISCV_BITS_H 5 | 6 | #define likely(x) __builtin_expect((x), 1) 7 | #define unlikely(x) __builtin_expect((x), 0) 8 | 9 | #define ROUNDUP(a, b) ((((a)-1)/(b)+1)*(b)) 10 | #define ROUNDDOWN(a, b) ((a)/(b)*(b)) 11 | 12 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) 13 | #define MIN(a, b) ((a) < (b) ? (a) : (b)) 14 | #define CLAMP(a, lo, hi) MIN(MAX(a, lo), hi) 15 | 16 | #define EXTRACT_FIELD(val, which) (((val) & (which)) / ((which) & ~((which)-1))) 17 | #define INSERT_FIELD(val, which, fieldval) (((val) & ~(which)) | ((fieldval) * ((which) & ~((which)-1)))) 18 | 19 | #define STR(x) XSTR(x) 20 | #define XSTR(x) #x 21 | 22 | #if __riscv_xlen == 64 23 | # define SLL32 sllw 24 | # define STORE sd 25 | # define LOAD ld 26 | # define LWU lwu 27 | # define LOG_REGBYTES 3 28 | #else 29 | # define SLL32 sll 30 | # define STORE sw 31 | # define LOAD lw 32 | # define LWU lw 33 | # define LOG_REGBYTES 2 34 | #endif 35 | #define REGBYTES (1 << LOG_REGBYTES) 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /resource/bbl/src/disabled_hart_mask.h: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | #ifndef DISABLED_HART_MASK_H 4 | #define DISABLED_HART_MASK_H 5 | extern long disabled_hart_mask; 6 | #endif 7 | -------------------------------------------------------------------------------- /resource/bbl/src/flush_icache.c: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | void __riscv_flush_icache(void) { 4 | __asm__ volatile ("fence.i"); 5 | } 6 | -------------------------------------------------------------------------------- /resource/bbl/src/mcall.h: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | #ifndef _RISCV_SBI_H 4 | #define _RISCV_SBI_H 5 | 6 | #define SBI_SET_TIMER 0 7 | #define SBI_CONSOLE_PUTCHAR 1 8 | #define SBI_CONSOLE_GETCHAR 2 9 | #define SBI_CLEAR_IPI 3 10 | #define SBI_SEND_IPI 4 11 | #define SBI_REMOTE_FENCE_I 5 12 | #define SBI_REMOTE_SFENCE_VMA 6 13 | #define SBI_REMOTE_SFENCE_VMA_ASID 7 14 | #define SBI_SHUTDOWN 8 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /resource/bbl/src/muldiv_emulation.c: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | #include "emulation.h" 4 | 5 | #ifndef __riscv_muldiv 6 | 7 | #if __riscv_xlen == 64 8 | typedef __int128 double_int; 9 | #else 10 | typedef int64_t double_int; 11 | #endif 12 | 13 | // These routines rely on the compiler to turn these operations into libcalls 14 | // when not natively supported. So work on making those go fast. 15 | 16 | DECLARE_EMULATION_FUNC(emulate_mul_div) 17 | { 18 | uintptr_t rs1 = GET_RS1(insn, regs), rs2 = GET_RS2(insn, regs), val; 19 | 20 | if ((insn & MASK_MUL) == MATCH_MUL) 21 | val = rs1 * rs2; 22 | else if ((insn & MASK_DIV) == MATCH_DIV) 23 | val = (intptr_t)rs1 / (intptr_t)rs2; 24 | else if ((insn & MASK_DIVU) == MATCH_DIVU) 25 | val = rs1 / rs2; 26 | else if ((insn & MASK_REM) == MATCH_REM) 27 | val = (intptr_t)rs1 % (intptr_t)rs2; 28 | else if ((insn & MASK_REMU) == MATCH_REMU) 29 | val = rs1 % rs2; 30 | else if ((insn & MASK_MULH) == MATCH_MULH) 31 | val = ((double_int)(intptr_t)rs1 * (double_int)(intptr_t)rs2) >> (8 * sizeof(rs1)); 32 | else if ((insn & MASK_MULHU) == MATCH_MULHU) 33 | val = ((double_int)rs1 * (double_int)rs2) >> (8 * sizeof(rs1)); 34 | else if ((insn & MASK_MULHSU) == MATCH_MULHSU) 35 | val = ((double_int)(intptr_t)rs1 * (double_int)rs2) >> (8 * sizeof(rs1)); 36 | else 37 | return truly_illegal_insn(regs, mcause, mepc, mstatus, insn); 38 | 39 | SET_RD(insn, regs, val); 40 | } 41 | 42 | #if __riscv_xlen == 64 43 | 44 | DECLARE_EMULATION_FUNC(emulate_mul_div32) 45 | { 46 | uint32_t rs1 = GET_RS1(insn, regs), rs2 = GET_RS2(insn, regs); 47 | int32_t val; 48 | 49 | if ((insn & MASK_MULW) == MATCH_MULW) 50 | val = rs1 * rs2; 51 | else if ((insn & MASK_DIVW) == MATCH_DIVW) 52 | val = (int32_t)rs1 / (int32_t)rs2; 53 | else if ((insn & MASK_DIVUW) == MATCH_DIVUW) 54 | val = rs1 / rs2; 55 | else if ((insn & MASK_REMW) == MATCH_REMW) 56 | val = (int32_t)rs1 % (int32_t)rs2; 57 | else if ((insn & MASK_REMUW) == MATCH_REMUW) 58 | val = rs1 % rs2; 59 | else 60 | return truly_illegal_insn(regs, mcause, mepc, mstatus, insn); 61 | 62 | SET_RD(insn, regs, val); 63 | } 64 | 65 | #endif 66 | #endif 67 | -------------------------------------------------------------------------------- /resource/bbl/src/vm.h: -------------------------------------------------------------------------------- 1 | // See LICENSE for license details. 2 | 3 | #ifndef _VM_H 4 | #define _VM_H 5 | 6 | #include "encoding.h" 7 | #include 8 | 9 | #define MEGAPAGE_SIZE ((uintptr_t)(RISCV_PGSIZE << RISCV_PGLEVEL_BITS)) 10 | #if __riscv_xlen == 64 11 | # define SATP_MODE_CHOICE INSERT_FIELD(0, SATP64_MODE, SATP_MODE_SV39) 12 | # define VA_BITS 39 13 | # define GIGAPAGE_SIZE (MEGAPAGE_SIZE << RISCV_PGLEVEL_BITS) 14 | #else 15 | # define SATP_MODE_CHOICE INSERT_FIELD(0, SATP32_MODE, SATP_MODE_SV32) 16 | # define VA_BITS 32 17 | #endif 18 | 19 | typedef uintptr_t pte_t; 20 | extern pte_t* root_page_table; 21 | 22 | static inline void flush_tlb() 23 | { 24 | asm volatile ("sfence.vma"); 25 | } 26 | 27 | static inline pte_t pte_create(uintptr_t ppn, int type) 28 | { 29 | return (ppn << PTE_PPN_SHIFT) | PTE_V | type; 30 | } 31 | 32 | static inline pte_t ptd_create(uintptr_t ppn) 33 | { 34 | return pte_create(ppn, PTE_V); 35 | } 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /resource/mips-elf/README.md: -------------------------------------------------------------------------------- 1 | This is a dummy ELF file used by qemu-system-mips32 to start. 2 | -------------------------------------------------------------------------------- /resource/mips-elf/mips.dummy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenXiangShan/NEMU/60feea904119b96c4bba4407e5f9418c9be0f35c/resource/mips-elf/mips.dummy -------------------------------------------------------------------------------- /resource/sdcard/README.md: -------------------------------------------------------------------------------- 1 | 2 | # NEMU sdhost驱动 3 | 4 | 本驱动裁剪自`linux/drivers/mmc/host/bcm2835.c`, 去除了DMA和中断, 改成直接轮询, 处理器无需支持DMA和中断即可运行. 5 | 6 | ## 使用方法 7 | 8 | * 将本目录下的`nemu.c`复制到`linux/drivers/mmc/host/`目录下 9 | * 在`linux/drivers/mmc/host/Makefile`中添加一行`obj-y += nemu.o` 10 | * 在menuconfig中取消`General setup -> Initial RAM filesystem and RAM disk (initramfs/initrd) support` 11 | * 在menuconfig中选中`Device Drivers -> MMC/SD/SDIO card support` 12 | * 在dts中加入以下节点 13 | ``` 14 | / { 15 | soc { 16 | sdhci: mmc { 17 | compatible = "nemu-sdhost"; 18 | reg = <0x0 0xa3000000 0x0 0x1000>; 19 | }; 20 | }; 21 | 22 | chosen { 23 | bootargs = "root=/dev/mmcblk0p1 rootfstype=ext4 ro rootwait earlycon"; 24 | }; 25 | }; 26 | ``` 27 | 28 | ## 在没有中断的处理器上访问SD卡 29 | 30 | 访问真实的SD卡需要等待一定的延迟, 这需要处理器的中断机制对内核支持计时的功能. 31 | 在没有中断机制的处理器上, 我们可以修改内核的部分代码, 使得无需等待这些延迟, 32 | 来达到确定性可重复的仿真效果. 33 | 34 | 具体只需修改以下文件: 35 | ```diff 36 | --- linux/drivers/mmc/core/block.c 37 | +++ linux/drivers/mmc/core/block.c 38 | @@ -983,6 +983,7 @@ static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, 39 | int err = 0; 40 | u32 status; 41 | 42 | + return err; 43 | do { 44 | bool done = time_after(jiffies, timeout); 45 | 46 | --- linux/drivers/mmc/core/core.h 47 | +++ linux/drivers/mmc/core/core.h 48 | @@ -64,6 +64,7 @@ void mmc_set_initial_state(struct mmc_host *host); 49 | 50 | static inline void mmc_delay(unsigned int ms) 51 | { 52 | + return; 53 | if (ms <= 20) 54 | usleep_range(ms * 1000, ms * 1250); 55 | else 56 | ``` 57 | 58 | 注意: 上述修改仅能用于模拟和仿真, 修改后将不能在真实的SD卡上运行!!! 59 | -------------------------------------------------------------------------------- /resource/softfloat/.gitignore: -------------------------------------------------------------------------------- 1 | repo/ 2 | -------------------------------------------------------------------------------- /scripts/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM centos:7 2 | 3 | # use baseurl instead of mirrorlist 4 | RUN sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo 5 | RUN sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo 6 | RUN sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo 7 | RUN yum clean all && yum makecache 8 | 9 | RUN yum install -y centos-release-scl 10 | # use baseurl instead of mirrorlist in CentOS-SCLo-scl.repo 11 | RUN sed -i s/mirror.centos.org/vault.centos.org/g /etc/yum.repos.d/*.repo 12 | RUN sed -i s/^#.*baseurl=http/baseurl=http/g /etc/yum.repos.d/*.repo 13 | RUN sed -i s/^mirrorlist=http/#mirrorlist=http/g /etc/yum.repos.d/*.repo 14 | RUN yum clean all && yum makecache 15 | 16 | RUN yum install -y git dtc devtoolset-11 llvm-toolset-7 bison flex 17 | RUN echo "source /opt/rh/devtoolset-11/enable" >> /etc/profile 18 | RUN echo "source /opt/rh/llvm-toolset-7/enable" >> /etc/profile 19 | -------------------------------------------------------------------------------- /scripts/Libcheckpoint.mk: -------------------------------------------------------------------------------- 1 | LIBCHECKPOINT_CROSS_COMPILE ?= riscv64-unknown-linux-gnu- 2 | 3 | LIBCHECKPOINT_REPO = $(RESOURCE_PATH)/LibCheckpoint 4 | LIBCHECKPOINT_BIN = $(LIBCHECKPOINT_REPO)/build/gcpt.bin 5 | LIBCHECKPOINT_MEMLAYOUT_HEADER = $(LIBCHECKPOINT_REPO)/src/checkpoint.proto 6 | 7 | ifeq ($(wildcard $(LIBCHECKPOINT_MEMLAYOUT_HEADER)),) 8 | $(shell git clone --depth=1 https://github.com/OpenXiangShan/LibCheckpoint.git $(LIBCHECKPOINT_REPO) 1>&2) 9 | $(shell cd $(LIBCHECKPOINT_REPO) && git submodule update --init 1>&2) 10 | endif 11 | 12 | ifdef CONFIG_LIBCHECKPOINT_RESTORER 13 | CFLAGS += -I$(NANOPB_REPO) 14 | CFLAGS += -I$(LIBCHECKPOINT_REPO)/export_include 15 | SRCS-y += $(LIBCHECKPOINT_REPO)/src/checkpoint.pb.c 16 | SRCS-y += $(NANOPB_REPO)/pb_common.c 17 | SRCS-y += $(NANOPB_REPO)/pb_decode.c 18 | SRCS-y += $(NANOPB_REPO)/pb_encode.c 19 | 20 | src/checkpoint/path_manager.cpp: $(LIBCHECKPOINT_REPO)/src/checkpoint.pb.c 21 | src/checkpoint/serializer.cpp: $(LIBCHECKPOINT_REPO)/src/checkpoint.pb.c 22 | endif 23 | 24 | $(LIBCHECKPOINT_REPO)/src/checkpoint.pb.c: $(LIBCHECKPOINT_BIN) 25 | 26 | libcheckpoint_bin: $(LIBCHECKPOINT_BIN) 27 | 28 | $(LIBCHECKPOINT_BIN): 29 | cd $(LIBCHECKPOINT_REPO) && ./configure && cd $(NEMU_HOME) 30 | $(MAKE) -s -C $(LIBCHECKPOINT_REPO) CROSS_COMPILE=$(LIBCHECKPOINT_CROSS_COMPILE) 31 | 32 | clean-libcheckpoint: 33 | $(MAKE) -s -C $(LIBCHECKPOINT_REPO) clean 34 | 35 | .PHONY: libcheckpoint_bin clean-libcheckpoint 36 | 37 | -------------------------------------------------------------------------------- /scripts/LibcheckpointAlpha.mk: -------------------------------------------------------------------------------- 1 | CPT_CROSS_COMPILE ?= riscv64-unknown-linux-gnu- 2 | 3 | CPT_REPO = $(RESOURCE_PATH)/gcpt_restore 4 | CPT_BIN = $(CPT_REPO)/build/gcpt.bin 5 | CPT_MEMLAYOUT_HEADER = $(CPT_REPO)/src/restore_rom_addr.h 6 | 7 | ifeq ($(wildcard $(CPT_MEMLAYOUT_HEADER)),) 8 | $(shell git clone --depth=1 https://github.com/OpenXiangShan/LibCheckpointAlpha.git $(CPT_REPO) 1>&2) 9 | endif 10 | 11 | gcpt_restore_bin: $(CPT_BIN) 12 | 13 | $(CPT_BIN): 14 | $(Q)$(MAKE) $(silent) -C $(CPT_REPO) CROSS_COMPILE=$(CPT_CROSS_COMPILE) 15 | 16 | clean-libcheckpointalpha: 17 | $(Q)$(MAKE) -s -C $(CPT_REPO) clean 18 | 19 | .PHONY: gcpt_restore_bin clean-libcheckpointalpha 20 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/checkpoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | checkpoint(){ 6 | set -x 7 | workload=$1 8 | 9 | export CLUSTER=$RESULT/cluster 10 | log=$LOG_PATH/checkpoint_logs 11 | mkdir -p $log 12 | $NEMU ${BBL_PATH}/${workload}.bin \ 13 | -D $RESULT -w ${workload} -C spec-cpt${cluster_times} \ 14 | -b -S $CLUSTER --cpt-interval $interval \ 15 | --checkpoint-format zstd > $log/${workload}-out.txt 2>$log/${workload}-err.txt 16 | } 17 | 18 | export -f checkpoint 19 | 20 | checkpoint bbl 21 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/checkpoint_env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export NEMU_HOME= 4 | export NEMU=$NEMU_HOME/build/riscv64-nemu-interpreter 5 | export GCPT=$NEMU_HOME/resource/gcpt_restore/build/gcpt.bin 6 | export SIMPOINT=$NEMU_HOME/resource/simpoint/simpoint_repo/bin/simpoint 7 | 8 | export BBL_PATH= 9 | export LOG_PATH=$NEMU_HOME/checkpoint_example_result/logs 10 | export RESULT=$NEMU_HOME/checkpoint_example_result 11 | export profiling_result_name=simpoint-profiling 12 | export PROFILING_RES=$RESULT/$profiling_result_name 13 | export interval=$((20*1000*1000)) 14 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/cluster.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | cluster(){ 6 | set -x 7 | workload=$1 8 | 9 | export CLUSTER=$RESULT/cluster/${workload} 10 | mkdir -p $CLUSTER 11 | 12 | random1=`head -20 /dev/urandom | cksum | cut -c 1-6` 13 | random2=`head -20 /dev/urandom | cksum | cut -c 1-6` 14 | 15 | log=$LOG_PATH/cluster_logs/cluster 16 | mkdir -p $log 17 | 18 | $SIMPOINT \ 19 | -loadFVFile $PROFILING_RES/${workload}/simpoint_bbv.gz \ 20 | -saveSimpoints $CLUSTER/simpoints0 -saveSimpointWeights $CLUSTER/weights0 \ 21 | -inputVectorsGzipped -maxK 30 -numInitSeeds 2 -iters 1000 -seedkm ${random1} -seedproj ${random2} \ 22 | > $log/${workload}-out.txt 2> $log/${workload}-err.txt 23 | } 24 | 25 | export -f cluster 26 | 27 | cluster bbl 28 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/manual_oneshot_cpt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | 6 | 7 | # can not generate checkpoint when exec in shell script 8 | manual_oneshot_cpt(){ 9 | set -x 10 | workload=$1 11 | log=$LOG_PATH/manual_oneshot/${workload} 12 | mkdir -p $log 13 | 14 | name="manual_oneshot" 15 | 16 | $NEMU ${BBL_PATH}/${workload}-bbl-linux-spec.bin \ 17 | -D $RESULT -w $workload -C $name \ 18 | -b --cpt-interval ${interval} \ 19 | --manual-oneshot-cpt --checkpoint-format zstd > $log/${workload}-out.txt 2>${log}/${workload}-err.txt 20 | 21 | } 22 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/manual_uniform_cpt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | # cannot generate checkpoint when exec in shell script 6 | manual_uniform_cpt(){ 7 | set -x 8 | workload=$1 9 | log=$LOG_PATH/manual_uniform 10 | mkdir -p $log 11 | name="manual_uniform" 12 | 13 | $NEMU ${BBL_PATH}/${workload}-bbl-linux-spec.bin \ 14 | -D $RESULT -w $workload -C $name \ 15 | -b --cpt-interval ${interval} \ 16 | --manual-uniform-cpt --checkpoint-format zstd > $log/${workload}-out.txt 2>${log}/${workload}-err.txt 17 | 18 | } 19 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/profiling.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | profiling(){ 6 | set -x 7 | workload=$1 8 | log=$LOG_PATH/profiling_logs 9 | mkdir -p $log 10 | 11 | $NEMU ${BBL_PATH}/${workload}.bin \ 12 | -D $RESULT -w $workload -C $profiling_result_name \ 13 | -b --simpoint-profile --cpt-interval ${interval} \ 14 | -r $GCPT > $log/${workload}-out.txt 2>${log}/${workload}-err.txt 15 | } 16 | 17 | export -f profiling 18 | 19 | profiling bbl 20 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/rebuild_fw_payload.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # usage: FW_PAYLOAD=/path/to/fw_payload.bin CROSS_COMPILE=/path/to/riscv64-unknown-linux-gnu- bash rebuild_fw_payload.sh 4 | # get extract_fw_payload/build/gcpt.bin 5 | 6 | REPO_LIST="repo.list" 7 | WORKSPACE=$(pwd) 8 | 9 | set -x 10 | 11 | if [ ! -f "$REPO_LIST" ]; then 12 | echo "" 13 | exit 1 14 | fi 15 | 16 | while IFS= read -r REPO_URL || [[ -n "$REPO_URL" ]]; do 17 | REPO_NAME=$(basename "$REPO_URL" .git) 18 | 19 | if [ -d "$REPO_NAME" ]; then 20 | echo "Updating repository '$REPO_NAME'..." 21 | cd "$REPO_NAME" && git pull && cd .. 22 | else 23 | echo "Clone repo 'REPO_URL' into '$REPO_NAME'..." 24 | git clone "$REPO_URL" 25 | fi 26 | done < "$REPO_LIST" 27 | 28 | cd nemu_board/dts && bash build_single_core_for_nemu.sh && cd $WORKSPACE 29 | 30 | export RESULT=extract_fw_payload 31 | 32 | mkdir $RESULT 33 | 34 | # note: default payload offset in opensbi is 2M, if you have modified FW_PAYLOAD_OFFSET, modify the next line of command to match this value 35 | dd if=$FW_PAYLOAD of=$RESULT/kernel.Image bs=1M skip=2 36 | 37 | kernel_list=extrack_fw_payload/kernel.Image 38 | 39 | fw_payload_bin="$kernel_list" 40 | 41 | fw_payload_bin_size=$(stat -c%s "$fw_payload_bin") 42 | 43 | fw_payload_fdt_addr=$(( ( (fw_payload_bin_size + 0x800000 + 0xfffff) / 0x100000 ) * 0x100000 + 0x80000000 )) 44 | 45 | printf "SPEC: %s, file size: %X, fw_payload_fdt_addr: %X\n" "$kernel_list" "$fw_payload_bin_size" "$fw_payload_fdt_addr" 46 | 47 | make -C $(pwd)/opensbi/ O=$(pwd)/$RESULT/opensbi PLATFORM=generic FW_PAYLOAD_PATH="$(pwd)/$RESULT/kernel.Image" FW_FDT_PATH=$(pwd)/nemu_board/dts/build/xiangshan.dtb FW_PAYLOAD_OFFSET=0x100000 FW_PAYLOAD_FDT_ADDR=0x$(printf '%X' $fw_payload_fdt_addr) -j10 48 | 49 | make -C $(pwd)/LibCheckpointAlpha/ O=$(pwd)/$RESULT GCPT_PAYLOAD_PATH="$(pwd)/$RESULT/opensbi/platform/generic/firmware/fw_payload.bin" 50 | 51 | 52 | 53 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/repo.list: -------------------------------------------------------------------------------- 1 | https://github.com/riscv-software-src/opensbi.git 2 | https://github.com/OpenXiangShan/LibCheckpointAlpha.git 3 | https://github.com/OpenXiangShan/nemu_board.git 4 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/semantic_checkpoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | 6 | semantic_cpt(){ 7 | set -x 8 | workload=$1 9 | semantic_cpt_profiling_file=$2 10 | log=$LOG_PATH/semantic_cpt/${workload} 11 | mkdir -p $log 12 | name="semantic_cpt" 13 | $NEMU ${BBL_PATH}/${workload} \ 14 | -D $RESULT -w $workload -C $name \ 15 | -b --checkpoint-format zstd \ 16 | --semantic-cpt ${semantic_cpt_profiling_file} \ 17 | > $log/${workload}-out.txt 2>${log}/${workload}-err.txt 18 | } 19 | 20 | export -f semantic_cpt 21 | semantic_cpt astar_rivers 22 | -------------------------------------------------------------------------------- /scripts/checkpoint_example/uniform_cpt.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source checkpoint_env.sh 4 | 5 | uniform_cpt(){ 6 | set -x 7 | workload=$1 8 | log=$LOG_PATH/uniform 9 | mkdir -p $log 10 | name="uniform" 11 | 12 | $NEMU ${BBL_PATH}/${workload}.bin \ 13 | -D $RESULT -w $workload -C $name \ 14 | -b -u --cpt-interval ${interval} --dont-skip-boot \ 15 | --checkpoint-format zstd > $log/${workload}-out.txt 2>${log}/${workload}-err.txt 16 | } 17 | 18 | export -f uniform_cpt 19 | 20 | uniform_cpt bbl 21 | -------------------------------------------------------------------------------- /scripts/compilation-test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | INFO="\033[1m" 4 | ERROR="\033[1;31m" 5 | GOOD="\033[1;32m" 6 | NC="\033[0m" 7 | 8 | failed_configs=() 9 | 10 | if [ ! -f "$NEMU_HOME/src/nemu-main.c" ]; then 11 | echo -e "${ERROR}NEMU_HOME=$NEMU_HOME is not a NEMU repo${NC}" 12 | exit 1 13 | fi 14 | 15 | echo -e "${INFO}Start compilation test for all configurations${NC}" 16 | make clean-all 17 | 18 | for config_path in configs/riscv64-*defconfig; do 19 | if [[ -f "$config_path" ]]; then 20 | config=$(basename "$config_path") 21 | echo "::group::$config" 22 | echo -e "${INFO}Building configuration: $config${NC}" 23 | 24 | make clean 25 | make $config 26 | if [ $? -ne 0 ]; then 27 | echo -e "${ERROR}Failed to setup configuration $config${NC}" 28 | failed_configs+=("$config") 29 | continue 30 | fi 31 | 32 | make -j8 33 | if [ $? -ne 0 ]; then 34 | echo -e "${ERROR}Compilation failed for configuration: $config${NC}" 35 | failed_configs+=("$config") 36 | continue 37 | fi 38 | 39 | echo -e "${GOOD}Successfully built: $config${NC}" 40 | echo "::endgroup::" 41 | else 42 | echo -e "${ERROR}No riscv64-*defconfig files found in configs directory.${NC}" 43 | exit 1 44 | fi 45 | done 46 | 47 | if [ ${#failed_configs[@]} -ne 0 ]; then 48 | echo -e "${ERROR}The following configurations failed to build:${NC}" 49 | for failed_config in "${failed_configs[@]}"; do 50 | echo "$failed_config" 51 | done 52 | exit 1 53 | else 54 | echo -e "${GOOD}All configurations built successfully.${NC}" 55 | exit 0 56 | fi 57 | -------------------------------------------------------------------------------- /scripts/generate_so_for_difftest.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | set -v 5 | 6 | export NEMU_HOME=/root/NEMU 7 | 8 | # gcpt_restore is not needed for ref-so. Just skip it in Makefile. 9 | mkdir -p $(pwd)/resource/gcpt_restore/src 10 | touch $(pwd)/resource/gcpt_restore/src/restore_rom_addr.h 11 | mkdir -p $(pwd)/resource/gcpt_restore/build 12 | touch $(pwd)/resource/gcpt_restore/build/gcpt.bin 13 | 14 | artifact_dir=$(pwd)/artifact 15 | mkdir -p $artifact_dir 16 | make clean 17 | make riscv64-xs-ref_defconfig 18 | make -j 19 | cp build/riscv64-nemu-interpreter-so ${artifact_dir}/riscv64-nemu-interpreter-so 20 | 21 | make clean 22 | make riscv64-xs-ref-debug_defconfig 23 | make -j 24 | cp build/riscv64-nemu-interpreter-so ${artifact_dir}/riscv64-nemu-interpreter-debug-so 25 | 26 | make clean 27 | make riscv64-dual-xs-ref_defconfig 28 | make -j 29 | cp build/riscv64-nemu-interpreter-so ${artifact_dir}/riscv64-nemu-interpreter-dual-so 30 | 31 | make clean 32 | make riscv64-dual-xs-ref-debug_defconfig 33 | make -j 34 | cp build/riscv64-nemu-interpreter-so ${artifact_dir}/riscv64-nemu-interpreter-dual-debug-so 35 | 36 | make clean 37 | make riscv64-xs-ref_bitmap_defconfig 38 | make -j 39 | cp build/riscv64-nemu-interpreter-so ${artifact_dir}/riscv64-nemu-interpreter-bitmap-so 40 | -------------------------------------------------------------------------------- /scripts/generate_so_from_docker.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | docker build -t difftest-so-env ./scripts && \ 3 | docker run --rm --volume=$(pwd):/root/NEMU --workdir /root/NEMU difftest-so-env bash -l scripts/generate_so_for_difftest.sh 4 | -------------------------------------------------------------------------------- /scripts/git.mk: -------------------------------------------------------------------------------- 1 | STUID = 191220000 2 | STUNAME = 张三 3 | 4 | # DO NOT modify the following code!!! 5 | 6 | GITFLAGS = -q --author='tracer-ics2020 ' --no-verify --allow-empty 7 | 8 | #ifndef __ICS_EXPORT 9 | ifdef __NOT_DEFINED 10 | #else 11 | # prototype: git_commit(msg) 12 | define git_commit 13 | -@git add $(NEMU_HOME)/.. -A --ignore-errors 14 | -@while (test -e .git/index.lock); do sleep 0.1; done 15 | -@(echo "> $(1)" && echo $(STUID) && hostnamectl && uptime) | git commit -F - $(GITFLAGS) 16 | -@sync 17 | endef 18 | #endif 19 | #ifndef __ICS_EXPORT 20 | endif 21 | #endif 22 | -------------------------------------------------------------------------------- /scripts/isa.mk: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | ISA ?= x86 17 | ISAS = $(shell ls $(NEMU_HOME)/src/isa/) 18 | ifeq ($(filter $(ISAS), $(ISA)), ) # ISA must be valid 19 | $(error Invalid ISA=$(ISA). Supported: $(ISAS)) 20 | endif 21 | NAME := $(ISA)-$(NAME) 22 | CFLAGS += -D__ISA_$(ISA)__=1 23 | -------------------------------------------------------------------------------- /scripts/manual-take.sh: -------------------------------------------------------------------------------- 1 | ./build/riscv64-nemu-interpreter \ 2 | --cpt-interval 100000000 -u -b \ 3 | -D output_top \ 4 | -C test_manual \ 5 | -w linux \ 6 | -r ./resource/gcpt_restore/build/gcpt.bin \ 7 | --manual-uniform-cpt \ 8 | $n/projects/rv-linux/riscv-pk/bbl-debian.bin 9 | -------------------------------------------------------------------------------- /scripts/nanopb.mk: -------------------------------------------------------------------------------- 1 | NANOPB_CROSS_COMPILE ?= riscv64-unknown-linux-gnu- 2 | 3 | NANOPB_REPO = $(RESOURCE_PATH)/nanopb 4 | 5 | ifeq ($(wildcard $(NANOPB_REPO)),) 6 | $(shell git clone --depth=1 https://github.com/nanopb/nanopb.git $(NANOPB_REPO) 1>&2) 7 | endif 8 | 9 | -------------------------------------------------------------------------------- /scripts/repos.mk: -------------------------------------------------------------------------------- 1 | RESOURCE_PATH := $(NEMU_HOME)/resource 2 | 3 | include $(NEMU_HOME)/scripts/softfloat.mk 4 | include $(NEMU_HOME)/scripts/nanopb.mk 5 | include $(NEMU_HOME)/scripts/Libcheckpoint.mk 6 | include $(NEMU_HOME)/scripts/LibcheckpointAlpha.mk 7 | 8 | clean-repos: clean-softfloat clean-libcheckpointalpha clean-libcheckpoint 9 | -------------------------------------------------------------------------------- /scripts/restore_zstd.sh: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | cpt=`find output_top/test/linux/50000000 -name "*.zstd"` 17 | ./build/riscv64-nemu-interpreter -b\ 18 | --diff ${SPIKE_SO}\ 19 | -I 100000000 \ 20 | $cpt 21 | -------------------------------------------------------------------------------- /scripts/restore_zstd_into_flash.sh: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | mem_cpt=`find output_top/test/linux/50000000 -name "*_memory_.zstd"` 17 | flash_cpt=`find output_top/test/linux/50000000 -name "*_flash_.zstd"` 18 | 19 | ./build/riscv64-nemu-interpreter -b \ 20 | --diff ${SPIKE_SO} \ 21 | --flash-image $flash_cpt -I 100000000 \ 22 | --cpt-restorer $NEMU_HOME/resource/gcpt_restore/build/gcpt.bin \ 23 | $mem_cpt 24 | 25 | -------------------------------------------------------------------------------- /scripts/runall.sh: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | #!/bin/bash 17 | 18 | ISA=${1#*ISA=} 19 | CPUTEST_PATH=$NEMU_HOME/../am-kernels/tests/cpu-tests 20 | 21 | echo "compiling NEMU..." 22 | if make -C $NEMU_HOME ISA=$ISA; then 23 | echo "NEMU compile OK" 24 | else 25 | echo "NEMU compile error... exit..." 26 | exit 27 | fi 28 | 29 | echo "compiling testcases..." 30 | if make -C $CPUTEST_PATH ARCH=$ISA-nemu &> /dev/null; then 31 | echo "testcases compile OK" 32 | else 33 | echo "testcases compile error... exit..." 34 | exit 35 | fi 36 | 37 | files=`ls $CPUTEST_PATH/build/*-$ISA-nemu.bin` 38 | ori_log="$NEMU_HOME/build/nemu-log.txt" 39 | 40 | for file in $files; do 41 | base=`basename $file | sed -e "s/-$ISA-nemu.bin//"` 42 | printf "[%14s] " $base 43 | logfile=$NEMU_HOME/build/$base-log.txt 44 | make -C $NEMU_HOME ISA=$ISA run ARGS="-b -l $ori_log $file" &> $logfile 45 | 46 | if (grep 'nemu: .*HIT GOOD TRAP' $logfile > /dev/null) then 47 | echo -e "\033[1;32mPASS!\033[0m" 48 | rm $logfile 49 | else 50 | echo -e "\033[1;31mFAIL!\033[0m see $logfile for more information" 51 | if (test -e $ori_log) then 52 | echo -e "\n\n===== the original log.txt =====\n" >> $logfile 53 | cat $ori_log >> $logfile 54 | fi 55 | fi 56 | done 57 | -------------------------------------------------------------------------------- /scripts/softfloat.mk: -------------------------------------------------------------------------------- 1 | ifdef CONFIG_FPU_SOFT 2 | 3 | SOFTFLOAT = resource/softfloat/build/softfloat.a 4 | 5 | SOFTFLOAT_REPO_PATH = resource/softfloat/repo 6 | ifeq ($(wildcard $(SOFTFLOAT_REPO_PATH)/COPYING.txt),) 7 | $(shell git clone --depth=1 https://github.com/ucb-bar/berkeley-softfloat-3 $(SOFTFLOAT_REPO_PATH) 1>&2) 8 | endif 9 | 10 | SOFTFLOAT_BUILD_PATH = $(abspath $(SOFTFLOAT_REPO_PATH)/build/Linux-x86_64-GCC) 11 | 12 | INC_DIR += $(SOFTFLOAT_REPO_PATH)/source/include 13 | INC_DIR += $(SOFTFLOAT_REPO_PATH)/source/$(SPECIALIZE_TYPE) 14 | LIBS += $(SOFTFLOAT) 15 | 16 | ifeq ($(ISA),riscv64) 17 | SPECIALIZE_TYPE = RISCV 18 | else 19 | SPECIALIZE_TYPE = 8086-SSE 20 | endif 21 | 22 | ifdef CONFIG_SHARE 23 | SOFTFLOAT_OPTS_DEFAULT = -DSOFTFLOAT_ROUND_ODD -DINLINE_LEVEL=5 \ 24 | -DSOFTFLOAT_FAST_DIV32TO16 -DSOFTFLOAT_FAST_DIV64TO32 25 | SOFTFLOAT_OPTS_OVERRIDE = SOFTFLOAT_OPTS="$(SOFTFLOAT_OPTS_DEFAULT) -fPIC" 26 | endif 27 | 28 | $(SOFTFLOAT): 29 | SPECIALIZE_TYPE=$(SPECIALIZE_TYPE) $(SOFTFLOAT_OPTS_OVERRIDE) $(MAKE) -s -C $(SOFTFLOAT_BUILD_PATH) all 30 | mkdir -p $(@D) 31 | ln -sf $(SOFTFLOAT_BUILD_PATH)/softfloat.a $@ 32 | 33 | endif 34 | 35 | clean-softfloat: 36 | ifdef CONFIG_FPU_SOFT 37 | $(MAKE) -s -C $(SOFTFLOAT_BUILD_PATH) clean 38 | endif 39 | 40 | .PHONY: clean-softfloat 41 | 42 | -------------------------------------------------------------------------------- /scripts/take_zstd.sh: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | ./build/riscv64-nemu-interpreter \ 17 | --cpt-interval 50000000 -u -b \ 18 | -D output_top \ 19 | -C test \ 20 | -w linux \ 21 | -I 350000000 $V_WORKLOAD_HOME/OpenSBI_Linux6.6_h264ref_sss_vectorized \ 22 | --checkpoint-format zstd 23 | -------------------------------------------------------------------------------- /scripts/take_zstd_into_flash.sh: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | ./build/riscv64-nemu-interpreter \ 17 | --cpt-interval 50000000 -u -b \ 18 | -D output_top \ 19 | -C test \ 20 | -w linux \ 21 | -I 350000000 $V_WORKLOAD_HOME/OpenSBI_Linux6.6_h264ref_sss_vectorized \ 22 | --store-cpt-in-flash \ 23 | --checkpoint-format zstd 24 | -------------------------------------------------------------------------------- /src/checkpoint/cpt_env.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | char *output_base_dir = NULL; 19 | char *config_name = NULL; 20 | char *workload_name = NULL; 21 | char *simpoints_dir = NULL; 22 | int cpt_id = -1; 23 | char *cpt_file = NULL; 24 | char *restorer = NULL; 25 | -------------------------------------------------------------------------------- /src/device/alarm.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include "device/alarm.h" 18 | #include 19 | #include 20 | 21 | #define TIMER_HZ 60 22 | #define MAX_HANDLER 8 23 | 24 | static alarm_handler_t handler[MAX_HANDLER] = {}; 25 | static int idx = 0; 26 | 27 | void add_alarm_handle(alarm_handler_t h) { 28 | assert(idx < MAX_HANDLER); 29 | handler[idx ++] = h; 30 | } 31 | 32 | static void alarm_sig_handler(int signum) { 33 | int i; 34 | for (i = 0; i < idx; i ++) { 35 | handler[i](); 36 | } 37 | } 38 | 39 | void init_alarm() { 40 | struct sigaction s; 41 | memset(&s, 0, sizeof(s)); 42 | s.sa_handler = alarm_sig_handler; 43 | int ret = sigaction(SIGVTALRM, &s, NULL); 44 | Assert(ret == 0, "Can not set signal handler"); 45 | 46 | struct itimerval it = {}; 47 | it.it_value.tv_sec = 0; 48 | it.it_value.tv_usec = 1000000 / TIMER_HZ; 49 | it.it_interval = it.it_value; 50 | ret = setitimer(ITIMER_VIRTUAL, &it, NULL); 51 | Assert(ret == 0, "Can not set timer"); 52 | } 53 | -------------------------------------------------------------------------------- /src/device/intr.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | void dev_raise_intr() { 19 | cpu.INTR = true; 20 | } 21 | -------------------------------------------------------------------------------- /src/device/plic.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | uint8_t *plic_base = NULL; 5 | #define PLIC_SIZE (0x4000000) 6 | 7 | static void plic_io_handler(uint32_t offset, int len, bool is_write) { 8 | // Fake plic handler, empty now 9 | return; 10 | } 11 | 12 | void init_plic(const char *flash_img) { 13 | plic_base = new_space(PLIC_SIZE); // TOO MUCH 14 | add_mmio_map("plic", CONFIG_PLIC_ADDRESS, plic_base, PLIC_SIZE, plic_io_handler); 15 | } 16 | -------------------------------------------------------------------------------- /src/device/timer.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include 19 | 20 | static uint32_t *rtc_port_base = NULL; 21 | 22 | static void rtc_io_handler(uint32_t offset, int len, bool is_write) { 23 | assert(offset == 0 || offset == 4); 24 | if (!is_write && offset == 4) { 25 | uint64_t us = get_time(); 26 | rtc_port_base[0] = (uint32_t)us; 27 | rtc_port_base[1] = us >> 32; 28 | } 29 | } 30 | 31 | static void timer_intr() { 32 | if (nemu_state.state == NEMU_RUNNING) { 33 | extern void dev_raise_intr(); 34 | dev_raise_intr(); 35 | } 36 | } 37 | 38 | void init_timer() { 39 | rtc_port_base = (uint32_t *)new_space(8); 40 | add_pio_map ("rtc", CONFIG_RTC_PORT, rtc_port_base, 8, rtc_io_handler); 41 | add_mmio_map("rtc", CONFIG_RTC_MMIO, rtc_port_base, 8, rtc_io_handler); 42 | add_alarm_handle(timer_intr); 43 | } 44 | -------------------------------------------------------------------------------- /src/engine/interpreter/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #include 18 | 19 | #ifndef CONFIG_SHARE 20 | void ui_mainloop(); 21 | 22 | void engine_start() { 23 | /* Receive commands from user. */ 24 | ui_mainloop(); 25 | } 26 | #endif 27 | -------------------------------------------------------------------------------- /src/isa/mips32/difftest/dut.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "../local-include/reg.h" 19 | #include 20 | 21 | bool isa_difftest_checkregs(CPU_state *ref_r, vaddr_t pc) { 22 | #ifndef __ICS_EXPORT 23 | #define check_reg(r) same = difftest_check_reg(str(r), pc, ref_r->r, cpu.r) && same 24 | bool same = true; 25 | if (memcmp(&cpu, ref_r, sizeof(cpu.gpr))) { 26 | int i; 27 | for (i = 0; i < ARRLEN(cpu.gpr); i ++) { 28 | difftest_check_reg(reg_name(i, 4), pc, ref_r->gpr[i]._32, cpu.gpr[i]._32); 29 | } 30 | same = false; 31 | } 32 | check_reg(pc); 33 | check_reg(lo); 34 | check_reg(hi); 35 | 36 | return same; 37 | #else 38 | return false; 39 | #endif 40 | } 41 | 42 | void isa_difftest_attach() { 43 | } 44 | -------------------------------------------------------------------------------- /src/isa/mips32/difftest/ref.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "../local-include/intr.h" 19 | 20 | void isa_difftest_regcpy(void *dut, bool direction) { 21 | if (direction == DIFFTEST_TO_REF) memcpy(&cpu, dut, DIFFTEST_REG_SIZE); 22 | else memcpy(dut, &cpu, DIFFTEST_REG_SIZE); 23 | } 24 | 25 | void isa_difftest_raise_intr(word_t NO) { 26 | cpu.pc = raise_intr(NO, cpu.pc); 27 | } 28 | -------------------------------------------------------------------------------- /src/isa/mips32/include/isa-all-instr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include "../local-include/rtl.h" 18 | 19 | #define INSTR_NULLARY(f) \ 20 | f(inv) f(nemu_trap) f(syscall) f(eret) f(tlbwr) f(tlbwi) f(tlbp) f(ret) 21 | #define INSTR_UNARY(f) \ 22 | f(j) f(jal) f(mfhi) f(mflo) f(mthi) f(mtlo) 23 | #define INSTR_BINARY(f) \ 24 | f(lui) f(jr) f(jalr) f(clz) \ 25 | f(lw) f(sw) f(lh) f(lb) f(lhu) f(lbu) f(sh) f(sb) f(swl) f(swr) f(lwl) f(lwr) 26 | #define INSTR_TERNARY(f) \ 27 | f(add) f(sub) f(slt) f(sltu) f(and) f(or) f(xor) f(nor) f(sll) f(srl) f(sra) \ 28 | f(addi) f(slti) f(sltui) f(andi) f(ori) f(xori) f(slli) f(srli) f(srai) f(movz) f(movn) \ 29 | f(beq) f(bne) f(blez) f(bltz) f(bgtz) f(bgez) \ 30 | f(mul) f(mult) f(multu) f(div) f(divu) \ 31 | f(mfc0) f(mtc0) 32 | 33 | def_all_EXEC_ID(); 34 | -------------------------------------------------------------------------------- /src/isa/mips32/include/isa-exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../instr/compute.h" 17 | #include "../instr/control.h" 18 | #include "../instr/ldst.h" 19 | #include "../instr/muldiv.h" 20 | #include "../instr/system.h" 21 | #include "../instr/special.h" 22 | -------------------------------------------------------------------------------- /src/isa/mips32/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | 19 | // this is not consistent with uint8_t 20 | // but it is ok since we do not access the array directly 21 | static const uint32_t img [] = { 22 | 0x3c048000, // lui a0, 0x8000 23 | 0xac800000, // sw zero, 0(a0) 24 | 0x8c820000, // lw v0,0(a0) 25 | 0xf0000000, // nemu_trap 26 | }; 27 | 28 | static void restart() { 29 | /* Set the initial program counter. */ 30 | cpu.pc = RESET_VECTOR; 31 | 32 | /* The zero register is always 0. */ 33 | cpu.gpr[0]._32 = 0; 34 | } 35 | 36 | void init_isa() { 37 | /* Load built-in image. */ 38 | memcpy(guest_to_host(RESET_VECTOR), img, sizeof(img)); 39 | 40 | /* Initialize this virtual computer system. */ 41 | restart(); 42 | #ifndef __ICS_EXPORT 43 | void init_mmu(); 44 | init_mmu(); 45 | #endif 46 | } 47 | -------------------------------------------------------------------------------- /src/isa/mips32/instr/control.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #define difftest_skip_delay_slot() \ 17 | IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(2, 1)) 18 | 19 | def_EHelper(j) { 20 | difftest_skip_delay_slot(); 21 | rtl_j(s, id_dest->imm); 22 | } 23 | 24 | def_EHelper(jal) { 25 | difftest_skip_delay_slot(); 26 | rtl_li(s, ®_l(31), id_src2->imm); 27 | rtl_j(s, id_dest->imm); 28 | } 29 | 30 | def_EHelper(ret) { 31 | difftest_skip_delay_slot(); 32 | rtl_jr(s, ®_l(31)); 33 | } 34 | 35 | def_EHelper(jr) { 36 | difftest_skip_delay_slot(); 37 | rtl_jr(s, dsrc1); 38 | } 39 | 40 | def_EHelper(jalr) { 41 | difftest_skip_delay_slot(); 42 | rtl_li(s, ddest, id_src2->imm); 43 | rtl_jr(s, dsrc1); 44 | } 45 | 46 | def_EHelper(bne) { 47 | difftest_skip_delay_slot(); 48 | rtl_jrelop(s, RELOP_NE, dsrc1, dsrc2, id_dest->imm); 49 | } 50 | 51 | def_EHelper(beq) { 52 | difftest_skip_delay_slot(); 53 | rtl_jrelop(s, RELOP_EQ, dsrc1, dsrc2, id_dest->imm); 54 | } 55 | 56 | def_EHelper(blez) { 57 | difftest_skip_delay_slot(); 58 | rtl_jrelop(s, RELOP_LE, dsrc1, dsrc2, id_dest->imm); 59 | } 60 | 61 | def_EHelper(bltz) { 62 | difftest_skip_delay_slot(); 63 | rtl_jrelop(s, RELOP_LT, dsrc1, rz, id_dest->imm); 64 | } 65 | 66 | def_EHelper(bgtz) { 67 | difftest_skip_delay_slot(); 68 | rtl_jrelop(s, RELOP_GT, dsrc1, dsrc2, id_dest->imm); 69 | } 70 | 71 | def_EHelper(bgez) { 72 | difftest_skip_delay_slot(); 73 | rtl_jrelop(s, RELOP_GE, dsrc1, rz, id_dest->imm); 74 | } 75 | -------------------------------------------------------------------------------- /src/isa/mips32/instr/muldiv.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(mfhi) { 17 | rtl_mv(s, ddest, &cpu.hi); 18 | } 19 | 20 | def_EHelper(mflo) { 21 | rtl_mv(s, ddest, &cpu.lo); 22 | } 23 | 24 | def_EHelper(mthi) { 25 | rtl_mv(s, &cpu.hi, dsrc1); 26 | } 27 | 28 | def_EHelper(mtlo) { 29 | rtl_mv(s, &cpu.lo, dsrc1); 30 | } 31 | 32 | def_EHelper(mul) { 33 | rtl_mulu_lo(s, ddest, dsrc1, dsrc2); 34 | } 35 | 36 | def_EHelper(mult) { 37 | rtl_mulu_lo(s, &cpu.lo, dsrc1, dsrc2); 38 | rtl_muls_hi(s, &cpu.hi, dsrc1, dsrc2); 39 | } 40 | 41 | def_EHelper(multu) { 42 | rtl_mulu_lo(s, &cpu.lo, dsrc1, dsrc2); 43 | rtl_mulu_hi(s, &cpu.hi, dsrc1, dsrc2); 44 | } 45 | 46 | def_EHelper(div) { 47 | rtl_divs_q(s, &cpu.lo, dsrc1, dsrc2); 48 | rtl_divs_r(s, &cpu.hi, dsrc1, dsrc2); 49 | } 50 | 51 | def_EHelper(divu) { 52 | rtl_divu_q(s, &cpu.lo, dsrc1, dsrc2); 53 | rtl_divu_r(s, &cpu.hi, dsrc1, dsrc2); 54 | } 55 | -------------------------------------------------------------------------------- /src/isa/mips32/instr/special.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(inv) { 17 | save_globals(s); 18 | rtl_hostcall(s, HOSTCALL_INV, NULL, NULL, 0); 19 | longjmp_context(NEMU_EXEC_END); 20 | } 21 | 22 | def_EHelper(nemu_trap) { 23 | save_globals(s); 24 | rtl_hostcall(s, HOSTCALL_EXIT, NULL, &cpu.gpr[2]._32, 0); // gpr[2] is $v0 25 | longjmp_context(NEMU_EXEC_END); 26 | } 27 | -------------------------------------------------------------------------------- /src/isa/mips32/instr/system.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../local-include/intr.h" 17 | 18 | def_EHelper(syscall) { 19 | rtl_trap(s, s->pc, EX_SYSCALL); 20 | rtl_priv_jr(s, t0); 21 | } 22 | 23 | def_EHelper(eret) { 24 | rtl_hostcall(s, HOSTCALL_PRIV, s0, NULL, PRIV_ERET); 25 | rtl_jr(s, s0); 26 | } 27 | 28 | def_EHelper(mfc0) { 29 | rtl_hostcall(s, HOSTCALL_CSR, dsrc2, NULL, id_dest->imm); 30 | } 31 | 32 | def_EHelper(mtc0) { 33 | rtl_hostcall(s, HOSTCALL_CSR, NULL, dsrc2, id_dest->imm); 34 | } 35 | 36 | def_EHelper(tlbwr) { 37 | rtl_hostcall(s, HOSTCALL_PRIV, NULL, NULL, PRIV_TLBWR); 38 | } 39 | 40 | def_EHelper(tlbwi) { 41 | rtl_hostcall(s, HOSTCALL_PRIV, NULL, NULL, PRIV_TLBWI); 42 | } 43 | 44 | def_EHelper(tlbp) { 45 | rtl_hostcall(s, HOSTCALL_PRIV, NULL, NULL, PRIV_TLBP); 46 | } 47 | -------------------------------------------------------------------------------- /src/isa/mips32/local-include/intr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MIPS32_INTR_H__ 17 | #define __MIPS32_INTR_H__ 18 | 19 | #include 20 | 21 | #define EX_SYSCALL 8 22 | #define EX_TLB_LD 2 23 | #define EX_TLB_ST 3 24 | #define TLB_REFILL 0x80 25 | 26 | #define MEM_OK 0 27 | 28 | word_t raise_intr(uint32_t NO, vaddr_t epc); 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /src/isa/mips32/local-include/reg.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MIPS32_REG_H__ 17 | #define __MIPS32_REG_H__ 18 | 19 | #include 20 | 21 | enum { PRIV_ERET, PRIV_TLBWR, PRIV_TLBWI, PRIV_TLBP }; 22 | 23 | static inline int check_reg_index(int index) { 24 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 25 | return index; 26 | } 27 | 28 | #define reg_l(index) (cpu.gpr[check_reg_index(index)]._32) 29 | 30 | static inline const char* reg_name(int index, int width) { 31 | extern const char* regsl[]; 32 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 33 | return regsl[index]; 34 | } 35 | 36 | static inline const char* cp0_name(int index) { 37 | extern const char* cp0[]; 38 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 39 | return cp0[index]; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /src/isa/mips32/local-include/rtl.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MIPS32_RTL_H__ 17 | #define __MIPS32_RTL_H__ 18 | 19 | #include 20 | #include "reg.h" 21 | 22 | static inline def_rtl(mux, rtlreg_t* dest, const rtlreg_t* cond, 23 | const rtlreg_t* src1, const rtlreg_t* src2) { 24 | // dest <- (cond ? src1 : src2) 25 | rtl_setrelopi(s, RELOP_EQ, s0, cond, 0); 26 | rtl_subi(s, s0, s0, 1); 27 | // s0 = mask 28 | rtl_and(s, s1, src1, s0); 29 | rtl_not(s, s0, s0); 30 | rtl_and(s, dest, src2, s0); 31 | rtl_or(s, dest, dest, s1); 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/isa/mips32/reg.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include "local-include/reg.h" 18 | 19 | const char *regsl[] = { 20 | "$0", "at", "v0", "v1", "a0", "a1", "a2", "a3", 21 | "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", 22 | "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", 23 | "t8", "t9", "k0", "k1", "gp", "sp", "s8", "ra" 24 | }; 25 | 26 | #ifndef __ICS_EXPORT 27 | const char *cp0[] = { 28 | "index", "???", "entrylo0", "entrylo1", 29 | "???", "???", "???", "???", 30 | "???", "entryhi", "???", "status", 31 | "cause", "epc", "???", "???", 32 | "???", "???", "???", "???", 33 | "???", "???", "???", "???", 34 | "???", "???", "???", "???", 35 | "???", "???", "???", "???" 36 | }; 37 | 38 | void isa_reg_display() { 39 | int i; 40 | for (i = 0; i < 32; i ++) { 41 | printf("%s: 0x%08x ", regsl[i], cpu.gpr[i]._32); 42 | if (i % 4 == 3) printf("\n"); 43 | } 44 | printf("pc: 0x%08x\n", cpu.pc); 45 | } 46 | 47 | word_t isa_reg_str2val(const char *s, bool *success) { 48 | int i; 49 | *success = true; 50 | for (i = 0; i < 32; i ++) { 51 | if (strcmp(regsl[i], s) == 0) return reg_l(i); 52 | } 53 | 54 | if (strcmp("pc", s) == 0) return cpu.pc; 55 | 56 | *success = false; 57 | return 0; 58 | } 59 | #else 60 | void isa_reg_display() { 61 | } 62 | 63 | word_t isa_reg_str2val(const char *s, bool *success) { 64 | return 0; 65 | } 66 | #endif 67 | -------------------------------------------------------------------------------- /src/isa/mips32/system/intr.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../local-include/rtl.h" 17 | #include "../local-include/intr.h" 18 | 19 | #ifndef __ICS_EXPORT 20 | #include 21 | 22 | word_t raise_intr(uint32_t NO, vaddr_t epc) { 23 | #define EX_ENTRY 0x80000180 24 | vaddr_t target = (NO & TLB_REFILL) ? 0x80000000 : EX_ENTRY; 25 | NO &= ~TLB_REFILL; 26 | cpu.cause = NO << 2; 27 | cpu.epc = epc; 28 | cpu.status.exl = 1; 29 | 30 | difftest_skip_dut(1, 2); 31 | 32 | return target; 33 | } 34 | 35 | void isa_query_intr() { 36 | if (cpu.INTR && (cpu.status.ie) && !(cpu.status.exl)) { 37 | cpu.INTR = false; 38 | cpu.pc = raise_intr(0, cpu.pc); 39 | } 40 | } 41 | #else 42 | word_t raise_intr(uint32_t NO, vaddr_t epc) { 43 | /* TODO: Trigger an interrupt/exception with ``NO''. 44 | * That is, use ``NO'' to index the IDT. 45 | */ 46 | 47 | return 0; 48 | } 49 | 50 | void isa_query_intr() { 51 | } 52 | #endif 53 | -------------------------------------------------------------------------------- /src/isa/riscv32/difftest/dut.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "../local-include/reg.h" 19 | #include 20 | 21 | bool isa_difftest_checkregs(CPU_state *ref_r, vaddr_t pc) { 22 | #ifndef __ICS_EXPORT 23 | if (memcmp(&cpu.gpr[1], &ref_r->gpr[1], DIFFTEST_REG_SIZE - sizeof(cpu.gpr[0]))) { 24 | int i; 25 | // do not check $0 26 | for (i = 1; i < ARRLEN(cpu.gpr); i ++) { 27 | difftest_check_reg(reg_name(i, 4), pc, ref_r->gpr[i]._32, cpu.gpr[i]._32); 28 | } 29 | difftest_check_reg("pc", pc, ref_r->pc, cpu.pc); 30 | return false; 31 | } 32 | return true; 33 | #else 34 | return false; 35 | #endif 36 | } 37 | 38 | void isa_difftest_attach() { 39 | } 40 | -------------------------------------------------------------------------------- /src/isa/riscv32/difftest/ref.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "../local-include/intr.h" 19 | 20 | void isa_difftest_regcpy(void *dut, bool direction) { 21 | if (direction == DIFFTEST_TO_REF) memcpy(&cpu, dut, DIFFTEST_REG_SIZE); 22 | else memcpy(dut, &cpu, DIFFTEST_REG_SIZE); 23 | } 24 | 25 | void isa_difftest_raise_intr(word_t NO) { 26 | cpu.pc = raise_intr(NO, cpu.pc); 27 | } 28 | -------------------------------------------------------------------------------- /src/isa/riscv32/include/isa-all-instr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | 19 | #define INSTR_NULLARY(f) \ 20 | f(inv) f(nemu_trap) f(ecall) f(ebreak) f(sret) f(sfence_vma) f(p_ret) 21 | 22 | #define INSTR_UNARY(f) \ 23 | f(p_li_0) f(p_li_1) \ 24 | f(p_inc) f(p_dec) 25 | 26 | #define INSTR_BINARY(f) \ 27 | f(lui) f(auipc) f(jal) \ 28 | f(lw) f(sw) f(lh) f(lb) f(lhu) f(lbu) f(sh) f(sb) \ 29 | f(c_j) f(c_jal) f(c_jr) \ 30 | f(c_beqz) f(c_bnez) f(c_mv) \ 31 | f(lw_mmu) f(sw_mmu) f(lh_mmu) f(lb_mmu) f(lhu_mmu) f(lbu_mmu) f(sh_mmu) f(sb_mmu) 32 | 33 | #define INSTR_TERNARY(f) \ 34 | f(add) f(sll) f(srl) f(slt) f(sltu) f(xor) f(or) f(sub) f(sra) f(and) \ 35 | f(addi) f(slli) f(srli) f(slti) f(sltui) f(xori) f(ori) f(srai) f(andi) \ 36 | f(jalr) f(beq) f(bne) f(blt) f(bge) f(bltu) f(bgeu) \ 37 | f(mul) f(mulh) f(mulhu) f(mulhsu) f(div) f(divu) f(rem) f(remu) \ 38 | f(c_li) f(c_addi) f(c_slli) f(c_srli) f(c_srai) f(c_andi) \ 39 | f(c_add) f(c_and) f(c_or) f(c_xor) f(c_sub) \ 40 | f(p_blez) f(p_bgez) f(p_bltz) f(p_bgtz) 41 | 42 | #define INSTR_TERNARY_CSR(f) \ 43 | f(csrrw) f(csrrs) 44 | 45 | def_all_EXEC_ID(); 46 | -------------------------------------------------------------------------------- /src/isa/riscv32/include/isa-exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../instr/pseudo.h" 17 | #include "../instr/compress.h" 18 | #include "../instr/compute.h" 19 | #include "../instr/control.h" 20 | #include "../instr/ldst.h" 21 | #include "../instr/muldiv.h" 22 | #include "../instr/system.h" 23 | #include "../instr/special.h" 24 | -------------------------------------------------------------------------------- /src/isa/riscv32/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | 19 | // this is not consistent with uint8_t 20 | // but it is ok since we do not access the array directly 21 | static const uint32_t img [] = { 22 | 0x800002b7, // lui t0,0x80000 23 | 0x0002a023, // sw zero,0(t0) 24 | 0x0002a503, // lw a0,0(t0) 25 | 0x0000006b, // nemu_trap 26 | }; 27 | 28 | static void restart() { 29 | /* Set the initial program counter. */ 30 | cpu.pc = RESET_VECTOR; 31 | 32 | /* The zero register is always 0. */ 33 | cpu.gpr[0]._32 = 0; 34 | #ifndef __ICS_EXPORT 35 | cpu.sstatus.val = 0x000c0100; 36 | #endif 37 | } 38 | 39 | void init_isa() { 40 | /* Load built-in image. */ 41 | memcpy(guest_to_host(RESET_VECTOR), img, sizeof(img)); 42 | 43 | /* Initialize this virtual computer system. */ 44 | restart(); 45 | } 46 | -------------------------------------------------------------------------------- /src/isa/riscv32/instr/control.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __ICS_EXPORT 17 | 18 | def_EHelper(jal) { 19 | rtl_li(s, ddest, id_src2->imm); 20 | rtl_j(s, id_src1->imm); 21 | } 22 | 23 | def_EHelper(jalr) { 24 | rtl_addi(s, s0, dsrc1, id_src2->imm); 25 | // IFDEF(CONFIG_ENGINE_INTERPRETER, rtl_andi(s, s0, s0, ~0x1u)); 26 | rtl_li(s, ddest, s->snpc); 27 | IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 2)); 28 | rtl_jr(s, s0); 29 | } 30 | 31 | def_EHelper(beq) { 32 | rtl_jrelop(s, RELOP_EQ, dsrc1, dsrc2, id_dest->imm); 33 | } 34 | 35 | def_EHelper(bne) { 36 | rtl_jrelop(s, RELOP_NE, dsrc1, dsrc2, id_dest->imm); 37 | } 38 | 39 | def_EHelper(blt) { 40 | rtl_jrelop(s, RELOP_LT, dsrc1, dsrc2, id_dest->imm); 41 | } 42 | 43 | def_EHelper(bge) { 44 | rtl_jrelop(s, RELOP_GE, dsrc1, dsrc2, id_dest->imm); 45 | } 46 | 47 | def_EHelper(bltu) { 48 | rtl_jrelop(s, RELOP_LTU, dsrc1, dsrc2, id_dest->imm); 49 | } 50 | 51 | def_EHelper(bgeu) { 52 | rtl_jrelop(s, RELOP_GEU, dsrc1, dsrc2, id_dest->imm); 53 | } 54 | #endif 55 | -------------------------------------------------------------------------------- /src/isa/riscv32/instr/ldst.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifdef __ICS_EXPORT 17 | def_EHelper(lw) { 18 | rtl_lms(s, ddest, dsrc1, id_src2->imm, 4); 19 | } 20 | 21 | def_EHelper(sw) { 22 | rtl_sm(s, ddest, dsrc1, id_src2->imm, 4); 23 | } 24 | #else 25 | #define def_ldst_template(name, rtl_instr, width, mmu_mode) \ 26 | def_EHelper(name) { \ 27 | concat(rtl_, rtl_instr) (s, ddest, dsrc1, id_src2->imm, width, mmu_mode); \ 28 | } 29 | 30 | #define def_all_ldst(suffix, mmu_mode) \ 31 | def_ldst_template(concat(lw , suffix), lms, 4, mmu_mode) \ 32 | def_ldst_template(concat(lh , suffix), lms, 2, mmu_mode) \ 33 | def_ldst_template(concat(lb , suffix), lms, 1, mmu_mode) \ 34 | def_ldst_template(concat(lhu, suffix), lm , 2, mmu_mode) \ 35 | def_ldst_template(concat(lbu, suffix), lm , 1, mmu_mode) \ 36 | def_ldst_template(concat(sw , suffix), sm , 4, mmu_mode) \ 37 | def_ldst_template(concat(sh , suffix), sm , 2, mmu_mode) \ 38 | def_ldst_template(concat(sb , suffix), sm , 1, mmu_mode) 39 | 40 | def_all_ldst(, MMU_DIRECT) 41 | def_all_ldst(_mmu, MMU_TRANSLATE) 42 | #endif 43 | -------------------------------------------------------------------------------- /src/isa/riscv32/instr/muldiv.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __ICS_EXPORT 17 | def_EHelper(mul) { 18 | rtl_mulu_lo(s, ddest, dsrc1, dsrc2); 19 | } 20 | 21 | def_EHelper(mulh) { 22 | rtl_muls_hi(s, ddest, dsrc1, dsrc2); 23 | } 24 | 25 | def_EHelper(mulhu) { 26 | rtl_mulu_hi(s, ddest, dsrc1, dsrc2); 27 | } 28 | 29 | def_EHelper(mulhsu) { 30 | // Algorithm: 31 | // We want to obtain ans = mulhsu(a, b). 32 | // Consider mulhu(a, b). 33 | // If a >= 0, then ans = mulhu(a, b); 34 | // If a = -x < 0, then a = 2^32 - x in two's complement 35 | // Then 36 | // mulhu(a, b) = mulhu(2^32 -x , b) = ((2^32 - x)b) >> 32 37 | // = ((2^32b) >> 32) + ((-xb) >> 32) 38 | // = b + mulhsu(a, b) = b + ans 39 | // Therefore, ans = mulhu(a, b) - b 40 | // 41 | // In the end, ans = (a < 0 ? mulhu(a, b) - b : mulhu(a, b)) 42 | // = mulhu(a, b) - (a < 0 ? b : 0) 43 | 44 | rtl_sari(s, s0, dsrc1, 31); 45 | rtl_and(s, s0, dsrc2, s0); // s0 = (id_src1->val < 0 ? id_src2->val : 0) 46 | rtl_mulu_hi(s, s1, dsrc1, dsrc2); 47 | rtl_sub(s, ddest, s1, s0); 48 | } 49 | 50 | def_EHelper(div) { 51 | rtl_divs_q(s, ddest, dsrc1, dsrc2); 52 | } 53 | 54 | def_EHelper(divu) { 55 | rtl_divu_q(s, ddest, dsrc1, dsrc2); 56 | } 57 | 58 | def_EHelper(rem) { 59 | rtl_divs_r(s, ddest, dsrc1, dsrc2); 60 | } 61 | 62 | def_EHelper(remu) { 63 | rtl_divu_r(s, ddest, dsrc1, dsrc2); 64 | } 65 | #endif 66 | -------------------------------------------------------------------------------- /src/isa/riscv32/instr/special.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(inv) { 17 | save_globals(s); 18 | rtl_hostcall(s, HOSTCALL_INV, NULL, NULL, 0); 19 | longjmp_context(NEMU_EXEC_END); 20 | } 21 | 22 | def_EHelper(nemu_trap) { 23 | save_globals(s); 24 | rtl_hostcall(s, HOSTCALL_EXIT, NULL, &cpu.gpr[10]._32, 0); // gpr[10] is $a0 25 | longjmp_context(NEMU_EXEC_END); 26 | } 27 | -------------------------------------------------------------------------------- /src/isa/riscv32/instr/system.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __ICS_EXPORT 17 | 18 | #define csr_difftest() IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 3)) 19 | #define priv_difftest() IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 2)) 20 | 21 | def_EHelper(csrrw) { 22 | csr_difftest(); 23 | rtl_hostcall(s, HOSTCALL_CSR, ddest, dsrc1, id_src2->imm); 24 | rtl_priv_next(s); 25 | } 26 | 27 | def_EHelper(csrrs) { 28 | csr_difftest(); 29 | rtl_hostcall(s, HOSTCALL_CSR, s0, NULL, id_src2->imm); 30 | rtl_or(s, s1, s0, dsrc1); 31 | rtl_mv(s, ddest, s0); 32 | rtl_hostcall(s, HOSTCALL_CSR, NULL, s1, id_src2->imm); 33 | rtl_priv_next(s); 34 | } 35 | 36 | def_EHelper(ecall) { 37 | priv_difftest(); 38 | rtl_trap(s, s->pc, 9); 39 | rtl_priv_jr(s, t0); 40 | } 41 | 42 | def_EHelper(sret) { 43 | priv_difftest(); 44 | rtl_hostcall(s, HOSTCALL_PRIV, s0, NULL, 0x102); 45 | rtl_priv_jr(s, s0); 46 | } 47 | 48 | def_EHelper(sfence_vma) { 49 | priv_difftest(); 50 | rtl_hostcall(s, HOSTCALL_PRIV, NULL, NULL, 0x120); 51 | rtl_priv_next(s); 52 | } 53 | #endif 54 | -------------------------------------------------------------------------------- /src/isa/riscv32/local-include/intr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV32_INTR_H__ 17 | #define __RISCV32_INTR_H__ 18 | 19 | #include 20 | 21 | word_t raise_intr(uint32_t NO, vaddr_t epc); 22 | 23 | #endif 24 | -------------------------------------------------------------------------------- /src/isa/riscv32/local-include/reg.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV32_REG_H__ 17 | #define __RISCV32_REG_H__ 18 | 19 | #include 20 | 21 | static inline int check_reg_index(int index) { 22 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 23 | return index; 24 | } 25 | 26 | #define reg_l(index) (cpu.gpr[check_reg_index(index)]._32) 27 | 28 | static inline const char* reg_name(int index, int width) { 29 | extern const char* regsl[]; 30 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 31 | return regsl[index]; 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/isa/riscv32/local-include/rtl.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV32_RTL_H__ 17 | #define __RISCV32_RTL_H__ 18 | 19 | #include 20 | #include "reg.h" 21 | 22 | // no isa-dependent rtl instructions 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /src/isa/riscv32/reg.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include "local-include/reg.h" 18 | 19 | const char *regsl[] = { 20 | "$0", "ra", "sp", "gp", "tp", "t0", "t1", "t2", 21 | "s0", "s1", "a0", "a1", "a2", "a3", "a4", "a5", 22 | "a6", "a7", "s2", "s3", "s4", "s5", "s6", "s7", 23 | "s8", "s9", "s10", "s11", "t3", "t4", "t5", "t6" 24 | }; 25 | 26 | #ifndef __ICS_EXPORT 27 | void isa_reg_display() { 28 | int i; 29 | for (i = 0; i < 32; i ++) { 30 | printf("%s: 0x%08x ", regsl[i], cpu.gpr[i]._32); 31 | if (i % 4 == 3) printf("\n"); 32 | } 33 | printf("pc: 0x%08x\n", cpu.pc); 34 | } 35 | 36 | word_t isa_reg_str2val(const char *s, bool *success) { 37 | int i; 38 | *success = true; 39 | for (i = 0; i < 32; i ++) { 40 | if (strcmp(regsl[i], s) == 0) return reg_l(i); 41 | } 42 | 43 | if (strcmp("pc", s) == 0) return cpu.pc; 44 | 45 | *success = false; 46 | return 0; 47 | } 48 | #else 49 | void isa_reg_display() { 50 | } 51 | 52 | word_t isa_reg_str2val(const char *s, bool *success) { 53 | return 0; 54 | } 55 | #endif 56 | -------------------------------------------------------------------------------- /src/isa/riscv32/system/intr.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../local-include/rtl.h" 17 | #include "../local-include/intr.h" 18 | 19 | #ifndef __ICS_EXPORT 20 | word_t raise_intr(uint32_t NO, vaddr_t epc) { 21 | cpu.scause = NO; 22 | cpu.sepc = epc; 23 | cpu.sstatus.spie = cpu.sstatus.sie; 24 | cpu.sstatus.sie = 0; 25 | return cpu.stvec; 26 | } 27 | 28 | void isa_query_intr() { 29 | if (cpu.INTR && cpu.sstatus.sie) { 30 | cpu.INTR = false; 31 | cpu.pc = raise_intr(0x80000005, cpu.pc); 32 | } 33 | } 34 | 35 | #else 36 | word_t raise_intr(uint32_t NO, vaddr_t epc) { 37 | /* TODO: Trigger an interrupt/exception with ``NO''. 38 | * That is, use ``NO'' to index the IDT. 39 | */ 40 | 41 | return 0; 42 | } 43 | 44 | void isa_query_intr() { 45 | } 46 | #endif 47 | -------------------------------------------------------------------------------- /src/isa/riscv64/clint.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "local-include/csr.h" 19 | 20 | #define CLINT_MTIMECMP (0x4000 / sizeof(clint_base[0])) 21 | #define CLINT_MTIME (0xBFF8 / sizeof(clint_base[0])) 22 | 23 | static uint64_t *clint_base = NULL; 24 | 25 | void set_mtime(uint64_t new_value); 26 | void update_riscv_timer(); 27 | void init_mtimer_regs(void * p_mtime, void * p_mtimecmp); 28 | 29 | #ifdef CONFIG_LIGHTQS 30 | uint64_t clint_snapshot, spec_clint_snapshot; 31 | 32 | void clint_take_snapshot() { 33 | clint_snapshot = clint_base[CLINT_MTIME]; 34 | } 35 | 36 | void clint_take_spec_snapshot() { 37 | spec_clint_snapshot = clint_base[CLINT_MTIME]; 38 | } 39 | 40 | void clint_restore_snapshot(uint64_t restore_inst_cnt) { 41 | if (spec_clint_snapshot <= restore_inst_cnt) { 42 | clint_snapshot = spec_clint_snapshot; 43 | } 44 | clint_base[CLINT_MTIME] = clint_snapshot; 45 | } 46 | #endif // CONFIG_LIGHTQS 47 | 48 | static void clint_io_handler(uint32_t offset, int len, bool is_write) { 49 | #ifdef CONFIG_LIGHTQS_DEBUG 50 | printf("clint op write %d addr %x\n", is_write, offset); 51 | #endif // CONFIG_LIGHTQS_DEBUG 52 | if (is_write && offset == CLINT_MTIME) { 53 | set_mtime(clint_base[CLINT_MTIME]); 54 | } else { 55 | update_riscv_timer(); 56 | } 57 | } 58 | 59 | void init_clint() { 60 | clint_base = (uint64_t *)new_space(0x10000); 61 | init_mtimer_regs(&clint_base[CLINT_MTIME], &clint_base[CLINT_MTIMECMP]); 62 | add_mmio_map("clint", CONFIG_CLINT_MMIO, (uint8_t *)clint_base, 0x10000, clint_io_handler); 63 | } 64 | -------------------------------------------------------------------------------- /src/isa/riscv64/include/isa-exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #include "../instr/pseudo.h" 18 | #include "../instr/rvi/exec.h" 19 | #include "../instr/rvc/exec.h" 20 | #include "../instr/rvm/exec.h" 21 | #ifndef CONFIG_FPU_NONE 22 | #include "../instr/rvf/exec.h" 23 | #include "../instr/rvd/exec.h" 24 | #endif // CONFIG_FPU_NONE 25 | #include "../instr/rva/exec.h" 26 | #include "../instr/priv/exec.h" 27 | #ifdef CONFIG_RVB 28 | #include "../instr/rvb/exec.h" 29 | #endif 30 | #ifdef CONFIG_RVK 31 | #include "../instr/rvk/exec.h" 32 | #endif 33 | #ifdef CONFIG_RVV 34 | #include "../instr/rvv/exec.h" 35 | #endif 36 | #ifdef CONFIG_RV_ZICOND 37 | #include "../instr/rvzicond/exec.h" 38 | #endif 39 | #ifdef CONFIG_RV_ZFH_MIN 40 | #include "../instr/rvzfh/exec.h" 41 | #endif 42 | #ifdef CONFIG_RV_CBO 43 | #include "../instr/rvcbo/exec.h" 44 | #endif 45 | #ifdef CONFIG_RV_ZFA 46 | #include "../instr/rvzfa/exec.h" 47 | #endif 48 | #include "../instr/special.h" 49 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/priv/system.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #include 18 | #include 19 | #include 20 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rocc.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | rtlreg_t rocc_sdi(uint32_t funct7, uint32_t funct3, rtlreg_t rs1, rtlreg_t rs2); 17 | 18 | static inline def_EHelper(rocc3) { 19 | *ddest = rocc_sdi(s->isa.instr.r.funct7, s->isa.instr.r.funct3, *dsrc1, *dsrc2); 20 | print_asm_template3(rocc3); 21 | } 22 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rva/exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #define def_AMO_EHelper(name) \ 17 | def_EHelper(name) { \ 18 | extern void rtl_amo_slow_path(Decode *s, rtlreg_t *dest, const rtlreg_t *src1, const rtlreg_t *src2); \ 19 | rtl_amo_slow_path(s, ddest, dsrc1, dsrc2); \ 20 | } 21 | 22 | #if defined(CONFIG_DEBUG) || defined(CONFIG_SHARE) 23 | #define AMO_LIST(f, s) \ 24 | f(concat3(lr , _, s)) \ 25 | f(concat3(sc , _, s)) \ 26 | f(concat3(amoswap, _, s)) \ 27 | f(concat3(amoadd , _, s)) \ 28 | f(concat3(amoor , _, s)) \ 29 | f(concat3(amoand , _, s)) \ 30 | f(concat3(amoxor , _, s)) \ 31 | f(concat3(amomaxu, _, s)) \ 32 | f(concat3(amomax , _, s)) \ 33 | f(concat3(amominu, _, s)) \ 34 | f(concat3(amomin , _, s)) 35 | 36 | #define AMO_CAS_LIST(f) \ 37 | f(concat3(amocas , _, w)) \ 38 | f(concat3(amocas , _, d)) \ 39 | f(concat3(amocas , _, q)) 40 | 41 | AMO_LIST(def_AMO_EHelper, d) 42 | AMO_LIST(def_AMO_EHelper, w) 43 | #ifdef CONFIG_RV_ZACAS 44 | AMO_CAS_LIST(def_AMO_EHelper) 45 | #endif // CONFIG_RV_ZACAS 46 | #else 47 | def_AMO_EHelper(atomic) 48 | #endif 49 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvcbo/cbo_impl.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2025 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "cpu/decode.h" 17 | 18 | void cbo_zero(Decode *s); 19 | void cbo_inval(Decode *s); 20 | void cbo_flush(Decode *s); 21 | void cbo_clean(Decode *s); 22 | 23 | void cbo_zero_mmu(Decode *s); 24 | void cbo_inval_mmu(Decode *s); 25 | void cbo_flush_mmu(Decode *s); 26 | void cbo_clean_mmu(Decode *s); -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvcbo/decode.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_THelper(cbo) { 17 | int mmu_mode = isa_mmu_state(); 18 | if (mmu_mode == MMU_DIRECT) { 19 | def_INSTR_TAB("0000000 00100 ????? ??? ????? ????? ??", cbo_zero); 20 | def_INSTR_TAB("0000000 00000 ????? ??? ????? ????? ??", cbo_inval); 21 | def_INSTR_TAB("0000000 00010 ????? ??? ????? ????? ??", cbo_flush); 22 | def_INSTR_TAB("0000000 00001 ????? ??? ????? ????? ??", cbo_clean); 23 | } else if (mmu_mode == MMU_TRANSLATE) { 24 | def_INSTR_TAB("0000000 00100 ????? ??? ????? ????? ??", cbo_zero_mmu); 25 | def_INSTR_TAB("0000000 00000 ????? ??? ????? ????? ??", cbo_inval_mmu); 26 | def_INSTR_TAB("0000000 00010 ????? ??? ????? ????? ??", cbo_flush_mmu); 27 | def_INSTR_TAB("0000000 00001 ????? ??? ????? ????? ??", cbo_clean_mmu); 28 | } else { assert(0); } 29 | 30 | return EXEC_ID_inv; 31 | } 32 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvi/exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "compute.h" 17 | 18 | #ifndef CONFIG_TDATA1_MCONTROL6 19 | #include "ldst.h" 20 | #else 21 | #include "ldst_trig.h" 22 | #endif 23 | 24 | #include "control.h" 25 | #include "fence.h" 26 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvi/fence.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(fence_i) { 17 | IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 2)); 18 | set_sys_state_flag(SYS_STATE_FLUSH_TCACHE); 19 | rtl_priv_next(s); 20 | } 21 | 22 | def_EHelper(fence) { 23 | IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 2)); 24 | } 25 | 26 | #ifdef CONFIG_RV_ZIHINTPAUSE 27 | def_EHelper(pause) { 28 | IFNDEF(CONFIG_DIFFTEST_REF_NEMU, difftest_skip_dut(1, 2)); 29 | } 30 | #endif //CONFIG_RV_ZIHINTPAUSE 31 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvi/ldst.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #define def_ldst_template(name, rtl_instr, width, mmu_mode) \ 17 | def_EHelper(name) { \ 18 | concat(rtl_, rtl_instr) (s, ddest, dsrc1, id_src2->imm, width, mmu_mode); \ 19 | } 20 | 21 | #define def_all_ldst(suffix, mmu_mode) \ 22 | def_ldst_template(concat(ld , suffix), lms, 8, mmu_mode) \ 23 | def_ldst_template(concat(lw , suffix), lms, 4, mmu_mode) \ 24 | def_ldst_template(concat(lh , suffix), lms, 2, mmu_mode) \ 25 | def_ldst_template(concat(lb , suffix), lms, 1, mmu_mode) \ 26 | def_ldst_template(concat(lwu, suffix), lm , 4, mmu_mode) \ 27 | def_ldst_template(concat(lhu, suffix), lm , 2, mmu_mode) \ 28 | def_ldst_template(concat(lbu, suffix), lm , 1, mmu_mode) \ 29 | def_ldst_template(concat(sd , suffix), sm , 8, mmu_mode) \ 30 | def_ldst_template(concat(sw , suffix), sm , 4, mmu_mode) \ 31 | def_ldst_template(concat(sh , suffix), sm , 2, mmu_mode) \ 32 | def_ldst_template(concat(sb , suffix), sm , 1, mmu_mode) 33 | 34 | def_all_ldst(, MMU_DIRECT) 35 | def_all_ldst(_mmu, MMU_TRANSLATE) 36 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvi/ldst_trig.h: -------------------------------------------------------------------------------- 1 | #define def_ld_template_with_trigger(name, rtl_instr, width, mmu_mode) \ 2 | def_EHelper(name) { \ 3 | trig_action_t action = TRIG_ACTION_NONE; \ 4 | const vaddr_t vaddr = *dsrc1 + id_src2->imm; \ 5 | action = check_triggers_mcontrol6(cpu.TM, TRIG_OP_LOAD, vaddr, TRIGGER_NO_VALUE); \ 6 | trigger_handler(TRIG_TYPE_MCONTROL6, action, vaddr); \ 7 | concat(rtl_, rtl_instr) (s, ddest, dsrc1, id_src2->imm, width, mmu_mode); \ 8 | } 9 | 10 | #define def_st_template_with_trigger(name, rtl_instr, width, mmu_mode) \ 11 | def_EHelper(name) { \ 12 | trig_action_t action = TRIG_ACTION_NONE; \ 13 | const vaddr_t vaddr = *dsrc1 + id_src2->imm; \ 14 | const word_t data = *ddest; \ 15 | action = check_triggers_mcontrol6(cpu.TM, TRIG_OP_STORE, vaddr, data); \ 16 | trigger_handler(TRIG_TYPE_MCONTROL6, action, vaddr); \ 17 | concat(rtl_, rtl_instr) (s, ddest, dsrc1, id_src2->imm, width, mmu_mode); \ 18 | } 19 | 20 | #define def_all_ldst(suffix, mmu_mode) \ 21 | def_ld_template_with_trigger(concat(ld , suffix), lms, 8, mmu_mode) \ 22 | def_ld_template_with_trigger(concat(lw , suffix), lms, 4, mmu_mode) \ 23 | def_ld_template_with_trigger(concat(lh , suffix), lms, 2, mmu_mode) \ 24 | def_ld_template_with_trigger(concat(lb , suffix), lms, 1, mmu_mode) \ 25 | def_ld_template_with_trigger(concat(lwu, suffix), lm , 4, mmu_mode) \ 26 | def_ld_template_with_trigger(concat(lhu, suffix), lm , 2, mmu_mode) \ 27 | def_ld_template_with_trigger(concat(lbu, suffix), lm , 1, mmu_mode) \ 28 | def_st_template_with_trigger(concat(sd , suffix), sm , 8, mmu_mode) \ 29 | def_st_template_with_trigger(concat(sw , suffix), sm , 4, mmu_mode) \ 30 | def_st_template_with_trigger(concat(sh , suffix), sm , 2, mmu_mode) \ 31 | def_st_template_with_trigger(concat(sb , suffix), sm , 1, mmu_mode) 32 | 33 | def_all_ldst(, MMU_DIRECT) 34 | def_all_ldst(_mmu, MMU_TRANSLATE) 35 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvk/crypto_impl.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV64_RVK_CRYPTO_IMPL_H__ 17 | #define __RISCV64_RVK_CRYPTO_IMPL_H__ 18 | 19 | #ifdef CONFIG_RVK 20 | 21 | #include 22 | #include 23 | 24 | int32_t sha256sum0(int32_t rs1); 25 | int32_t sha256sum1(int32_t rs1); 26 | int32_t sha256sig0(int32_t rs1); 27 | int32_t sha256sig1(int32_t rs1); 28 | 29 | int64_t sha512sum0(int64_t rs1); 30 | int64_t sha512sum1(int64_t rs1); 31 | int64_t sha512sig0(int64_t rs1); 32 | int64_t sha512sig1(int64_t rs1); 33 | 34 | int32_t sm3p0(int32_t rs1); 35 | int32_t sm3p1(int32_t rs1); 36 | 37 | int64_t aes64es(int64_t rs1, int64_t rs2); 38 | int64_t aes64esm(int64_t rs1, int64_t rs2); 39 | int64_t aes64ds(int64_t rs1, int64_t rs2); 40 | int64_t aes64dsm(int64_t rs1, int64_t rs2); 41 | int64_t aes64im(int64_t rs1); 42 | int64_t aes64ks1i(int64_t rs1, int64_t rs2); 43 | int64_t aes64ks2(int64_t rs1, int64_t rs2); 44 | 45 | int32_t sm4ed(int32_t rs1, int32_t rs2, uint8_t funct7); 46 | int32_t sm4ks(int32_t rs1, int32_t rs2, uint8_t funct7); 47 | 48 | #endif // CONFIG_RVK 49 | #endif // __RISCV64_RVK_CRYPTO_IMPL_H__ 50 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvk/decode.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #ifndef RVK_DECODE_H 18 | #define RVK_DECODE_H 19 | #include 20 | #include "../rvi/decode.h" 21 | 22 | static inline def_DHelper(aes64ks1i) { 23 | sword_t simm = BITS(s->isa.instr.i.simm11_0, 3, 0); 24 | decode_op_i(s, id_src2, simm, false); 25 | } 26 | #endif // RVK_DECODE_H 27 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvm/decode.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_THelper(rvm) { 17 | def_INSTR_TAB("??????? ????? ????? 000 ????? ????? ??", mul); 18 | def_INSTR_TAB("??????? ????? ????? 001 ????? ????? ??", mulh); 19 | def_INSTR_TAB("??????? ????? ????? 010 ????? ????? ??", mulhsu); 20 | def_INSTR_TAB("??????? ????? ????? 011 ????? ????? ??", mulhu); 21 | def_INSTR_TAB("??????? ????? ????? 100 ????? ????? ??", div); 22 | def_INSTR_TAB("??????? ????? ????? 101 ????? ????? ??", divu); 23 | def_INSTR_TAB("??????? ????? ????? 110 ????? ????? ??", rem); 24 | def_INSTR_TAB("??????? ????? ????? 111 ????? ????? ??", remu); 25 | return EXEC_ID_inv; 26 | } 27 | 28 | def_THelper(rvm32) { 29 | def_INSTR_TAB("??????? ????? ????? 000 ????? ????? ??", mulw); 30 | def_INSTR_TAB("??????? ????? ????? 100 ????? ????? ??", divw); 31 | def_INSTR_TAB("??????? ????? ????? 101 ????? ????? ??", divuw); 32 | def_INSTR_TAB("??????? ????? ????? 110 ????? ????? ??", remw); 33 | def_INSTR_TAB("??????? ????? ????? 111 ????? ????? ??", remuw); 34 | return EXEC_ID_inv; 35 | } 36 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvv/exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "vcfg.h" 17 | #include "vcompute.h" 18 | #include "vldst.h" 19 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvv/vcfg.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #ifdef CONFIG_RVV 19 | 20 | #include "cpu/exec.h" 21 | #include "../local-include/vreg.h" 22 | #include "../local-include/csr.h" 23 | #include 24 | #include "../local-include/intr.h" 25 | #include "../local-include/rtl.h" 26 | #include 27 | #include "vcommon.h" 28 | #include "vcompute_impl.h" 29 | 30 | def_EHelper(vsetvl) { 31 | 32 | require_vector(false); 33 | rtl_lr(s, &(s->src1.val), s->src1.reg, 4); 34 | rtl_lr(s, &(s->src2.val), s->src2.reg, 4); 35 | int mode = get_mode(s); 36 | set_vtype_vl(s, mode); 37 | vp_set_dirty(); 38 | } 39 | 40 | def_EHelper(vsetvli) { 41 | 42 | require_vector(false); 43 | rtl_lr(s, &(s->src1.val), s->src1.reg, 4); 44 | rtl_li(s, &(s->src2.val), s->isa.instr.v_opsimm.v_zimm); 45 | int mode = get_mode(s); 46 | set_vtype_vl(s, mode); 47 | vp_set_dirty(); 48 | } 49 | 50 | def_EHelper(vsetivli) { 51 | 52 | require_vector(false); 53 | rtl_li(s, &(s->src1.val), s->isa.instr.v_vseti.v_zimm5); 54 | rtl_li(s, &(s->src2.val), s->isa.instr.v_vseti.v_zimm); 55 | set_vtype_vl(s, 0); 56 | vp_set_dirty(); 57 | } 58 | 59 | #endif // CONFIG_RVV -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvv/vcommon.h: -------------------------------------------------------------------------------- 1 | #include 2 | #ifdef CONFIG_RVV 3 | #ifndef __RISCV64_VCOMMON_H__ 4 | #define __RISCV64_VCOMMON_H__ 5 | 6 | #include "cpu/exec.h" 7 | #include "../local-include/csr.h" 8 | #include "../local-include/intr.h" 9 | #include "../local-include/rtl.h" 10 | 11 | uint8_t check_vstart_ignore(Decode *s); 12 | void check_vstart_exception(Decode *s); 13 | bool check_vlmul_sew_illegal(rtlreg_t vtype_req); 14 | void set_NAN(rtlreg_t* fpreg, uint64_t vsew); 15 | bool check_isFpCanonicalNAN(rtlreg_t* fpreg, uint64_t vsew); 16 | void vp_set_dirty(); 17 | 18 | int get_mode(Decode *s); 19 | void set_vtype_vl(Decode *s, int mode); 20 | void predecode_vls(Decode *s); 21 | 22 | #endif 23 | #endif -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvv/vldst_impl.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #ifdef CONFIG_RVV 18 | 19 | #ifndef __RISCV64_VLDST_IMPL_H__ 20 | #define __RISCV64_VLDST_IMPL_H__ 21 | 22 | #include "cpu/exec.h" 23 | #include "vreg.h" 24 | #include "../local-include/csr.h" 25 | #include "../local-include/rtl.h" 26 | #include 27 | #include "../local-include/intr.h" 28 | #include 29 | #include "vcommon.h" 30 | 31 | #define id_src (&s->src1) 32 | #define id_src2 (&s->src2) 33 | #define id_dest (&s->dest) 34 | 35 | // vector load 36 | #define MODE_UNIT 0 37 | #define MODE_STRIDED 1 38 | #define MODE_INDEXED 2 39 | #define MODE_MASK 3 40 | 41 | // vector load 42 | void vld(Decode *s, int mode, int mmu_mode); 43 | void vldx(Decode *s, int mmu_mode); 44 | void vlr(Decode *s, int mmu_mode); 45 | void vldff(Decode *s, int mode, int mmu_mode); 46 | 47 | // vector store 48 | void vst(Decode *s, int mode, int mmu_mode); 49 | void vstx(Decode *s, int mmu_mode); 50 | void vsr(Decode *s, int mmu_mode); 51 | 52 | #endif // __RISCV64_VLDST_IMPL_H__ 53 | 54 | #endif // CONFIG_RVV -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvzfa/decode.h: -------------------------------------------------------------------------------- 1 | #ifdef CONFIG_RV_ZFA 2 | def_THelper(op_zfa) { 3 | 4 | def_INSTR_IDTAB("11110 00 00001 ????? 000 ????? ????? ??", r2fr, fli_s); 5 | def_INSTR_IDTAB("00101 00 ????? ????? 010 ????? ????? ??", fr, fminm_s); 6 | def_INSTR_IDTAB("00101 00 ????? ????? 011 ????? ????? ??", fr, fmaxm_s); 7 | def_INSTR_IDTAB("01000 00 00100 ????? ??? ????? ????? ??", fr, fround_s); 8 | def_INSTR_IDTAB("01000 00 00101 ????? ??? ????? ????? ??", fr, froundnx_s); 9 | def_INSTR_IDTAB("10100 00 ????? ????? 100 ????? ????? ??", fr2r, fleq_s); 10 | def_INSTR_IDTAB("10100 00 ????? ????? 101 ????? ????? ??", fr2r, fltq_s); 11 | 12 | def_INSTR_IDTAB("11110 01 00001 ????? 000 ????? ????? ??", r2fr, fli_d); 13 | def_INSTR_IDTAB("00101 01 ????? ????? 010 ????? ????? ??", fr, fminm_d); 14 | def_INSTR_IDTAB("00101 01 ????? ????? 011 ????? ????? ??", fr, fmaxm_d); 15 | def_INSTR_IDTAB("01000 01 00100 ????? ??? ????? ????? ??", fr, fround_d); 16 | def_INSTR_IDTAB("01000 01 00101 ????? ??? ????? ????? ??", fr, froundnx_d); 17 | def_INSTR_IDTAB("10100 01 ????? ????? 100 ????? ????? ??", fr2r, fleq_d); 18 | def_INSTR_IDTAB("10100 01 ????? ????? 101 ????? ????? ??", fr2r, fltq_d); 19 | def_INSTR_IDTAB("11000 01 01000 ????? 001 ????? ????? ??", fr2r, fcvtmod_w_d); 20 | 21 | #ifdef CONFIG_RV_ZFH 22 | def_INSTR_IDTAB("11110 10 00001 ????? 000 ????? 1010011", r2fr, fli_h); 23 | def_INSTR_IDTAB("00101 10 ????? ????? 010 ????? 1010011", fr, fminm_h); 24 | def_INSTR_IDTAB("00101 10 ????? ????? 011 ????? 1010011", fr, fmaxm_h); 25 | def_INSTR_IDTAB("01000 10 00100 ????? ??? ????? 1010011", fr, fround_h); 26 | def_INSTR_IDTAB("01000 10 00101 ????? ??? ????? 1010011", fr, froundnx_h); 27 | def_INSTR_IDTAB("10100 10 ????? ????? 100 ????? 1010011", fr2r, fleq_h); 28 | def_INSTR_IDTAB("10100 10 ????? ????? 101 ????? 1010011", fr2r, fltq_h); 29 | #endif // CONFIG_RV_ZFH 30 | 31 | return EXEC_ID_inv; 32 | } 33 | #endif // CONFIG_RV_ZFA 34 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/rvzicond/exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2024 Beijing Institute of Open Source Chip 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifdef CONFIG_RV_ZICOND 17 | 18 | def_EHelper(czero_eqz) { 19 | *ddest = *dsrc2 == 0 ? 0 : *dsrc1; 20 | } 21 | 22 | def_EHelper(czero_nez) { 23 | *ddest = *dsrc2 != 0 ? 0 : *dsrc1; 24 | } 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/special.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #include "../local-include/intr.h" 18 | #include 19 | #include "cpu/difftest.h" 20 | 21 | def_EHelper(inv) { 22 | save_globals(s); 23 | #ifdef CONFIG_REPORT_ILLEGAL_INSTR 24 | rtl_hostcall(s, HOSTCALL_INV, NULL, NULL, NULL, 0); 25 | longjmp_context(NEMU_EXEC_END); 26 | #else 27 | longjmp_exception(EX_II); 28 | #endif 29 | } 30 | 31 | def_EHelper(rt_inv) { 32 | save_globals(s); 33 | longjmp_exception(EX_II); 34 | } 35 | 36 | def_EHelper(nemu_trap) { 37 | save_globals(s); 38 | Log("nemu_trap case %lx",cpu.gpr[10]._64); 39 | if (cpu.gpr[10]._64 == 0x100) { 40 | extern void disable_time_intr(); 41 | disable_time_intr(); 42 | cpu.mie = mie->val; 43 | difftest_skip_ref(); 44 | } else if (cpu.gpr[10]._64 == 0x101) { 45 | if (!workload_loaded) { 46 | start_profiling(); 47 | difftest_skip_ref(); 48 | } 49 | } else { 50 | rtl_hostcall(s, HOSTCALL_EXIT,NULL, &cpu.gpr[10]._64, NULL, 0); // gpr[10] is $a0 51 | longjmp_context(NEMU_EXEC_END); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/isa/riscv64/instr/vp.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #ifdef CONFIG_RVV 18 | #include "../local-include/csr.h" 19 | #include "../local-include/intr.h" 20 | #include 21 | #include 22 | 23 | bool vp_enable() { 24 | #ifdef CONFIG_MODE_USER 25 | return true; 26 | #else // !CONFIG_MODE_USER 27 | if (mstatus->vs == 0) { 28 | return false; 29 | } 30 | #ifdef CONFIG_RVH 31 | if (cpu.v && vsstatus->vs == 0) { 32 | return false; 33 | } 34 | #endif // CONFIG_RVH 35 | return true; 36 | #endif // CONFIG_MODE_USER 37 | } 38 | 39 | void vp_set_dirty() { 40 | mstatus->vs = EXT_CONTEXT_DIRTY; 41 | #ifdef CONFIG_RVH 42 | if (cpu.v == 1) { 43 | vsstatus->vs = EXT_CONTEXT_DIRTY; 44 | } 45 | #endif //CONFIG_RVH 46 | IFDEF(CONFIG_DIFFTEST_DIRTY_FS_VS, ref_difftest_dirty_fsvs(SSTATUS_VS)); 47 | } 48 | #endif // CONFIG_RVV -------------------------------------------------------------------------------- /src/isa/riscv64/local-include/aia.h: -------------------------------------------------------------------------------- 1 | #ifdef CONFIG_RV_IMSIC 2 | 3 | #ifndef __AIA_H__ 4 | #define __AIA_H__ 5 | 6 | #include "common.h" 7 | #include "csr.h" 8 | #include "intr.h" 9 | 10 | // 0,4,8 is reserved 11 | #define IPRIO_ENABLE_NUM 61 12 | #define IPRIO_NUM 8 13 | 14 | typedef struct { 15 | word_t val; 16 | } Iprio; 17 | 18 | typedef struct IpriosModule { 19 | Iprio iprios[IPRIO_NUM]; 20 | } IpriosModule; 21 | 22 | typedef struct { 23 | bool enable; 24 | bool isZero; 25 | bool greaterThan255; 26 | uint8_t priority; 27 | } IpriosEnable; 28 | 29 | typedef struct IpriosSort { 30 | IpriosEnable ipriosEnable[IPRIO_ENABLE_NUM]; 31 | } IpriosSort; 32 | 33 | typedef struct { 34 | uint8_t hviprios[24]; 35 | } Hviprios; 36 | 37 | void set_iprios_sort(uint64_t topi_gather, IpriosSort* iprios_sort, IpriosModule* iprios, uint8_t xei, mtopei_t* xtopei); 38 | void set_viprios_sort(uint64_t topi_gather); 39 | uint8_t high_iprio(IpriosSort* ipriosSort, uint8_t xei); 40 | uint8_t get_prio_idx_in_group(uint8_t irq); 41 | 42 | extern int interrupt_default_prio[IPRIO_ENABLE_NUM]; 43 | 44 | bool no_mtopi(); 45 | bool no_stopi(); 46 | bool no_vstopi(); 47 | 48 | #endif // __AIA_H__ 49 | #endif // CONFIG_RV_IMSIC -------------------------------------------------------------------------------- /src/isa/riscv64/local-include/reg.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #ifndef __RISCV64_REG_H__ 18 | #define __RISCV64_REG_H__ 19 | 20 | #include 21 | 22 | static inline int check_reg_index(int index) { 23 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 24 | return index; 25 | } 26 | 27 | #define reg_l(index) (cpu.gpr[check_reg_index(index)]._64) 28 | 29 | static inline const char* reg_name(int index, int width) { 30 | extern const char* regsl[]; 31 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 32 | return regsl[index]; 33 | } 34 | 35 | // Floating Point Regs 36 | #define fpreg_l(index) (cpu.fpr[check_reg_index(index)]._64) 37 | 38 | static inline const char* fpreg_name(int index, int width){ 39 | extern const char* fpregsl[]; 40 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 32)); 41 | return fpregsl[index]; 42 | } 43 | 44 | #ifdef CONFIG_RV_PMA_CSR 45 | typedef struct { 46 | uint64_t base_addr; 47 | uint64_t range; 48 | uint64_t l; 49 | uint64_t c; 50 | uint64_t t; // atomic 51 | uint64_t a; 52 | uint64_t x; 53 | uint64_t w; 54 | uint64_t r; 55 | } PMAConfig; 56 | 57 | typedef struct PMAConfigModule { 58 | PMAConfig pmaconfigs[CONFIG_RV_PMA_ACTIVE_NUM]; 59 | } PMAConfigModule; 60 | #endif 61 | #endif 62 | -------------------------------------------------------------------------------- /src/isa/riscv64/local-include/trapinfo.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 4 | * 5 | * NEMU is licensed under Mulan PSL v2. 6 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 7 | * You may obtain a copy of Mulan PSL v2 at: 8 | * http://license.coscl.org.cn/MulanPSL2 9 | * 10 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 | * 14 | * See the Mulan PSL v2 for more details. 15 | ***************************************************************************************/ 16 | 17 | #ifndef __TRAPINFO_H__ 18 | #define __TRAPINFO_H__ 19 | 20 | #include "common.h" 21 | 22 | typedef struct { 23 | word_t tval; 24 | word_t tval2; 25 | word_t tinst; 26 | } trap_info_t; 27 | 28 | void clear_trapinfo(); 29 | 30 | 31 | #endif // __TRAPINFO_H__ -------------------------------------------------------------------------------- /src/isa/x86/Kconfig: -------------------------------------------------------------------------------- 1 | menu "ISA-dependent Options for x86" 2 | 3 | choice 4 | prompt "Optimization for condition code evaluation" 5 | default x86_CC_SKIP 6 | config x86_CC_SKIP 7 | bool "Skip CC" 8 | config x86_CC_LAZY 9 | bool "Lazy CC" 10 | config x86_CC_NONE 11 | bool "none" 12 | endchoice 13 | 14 | config PA 15 | bool "Enable PA version" 16 | default n 17 | 18 | endmenu 19 | -------------------------------------------------------------------------------- /src/isa/x86/device/i8237a.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define I8237A_PAGE_0_PORT 0x87 19 | 20 | static uint8_t *i8237a_base = NULL; 21 | 22 | static void i8237a_io_handler(uint32_t offset, int len, bool is_write) { 23 | } 24 | 25 | void init_i8237a() { 26 | i8237a_base = (void *)new_space(1); 27 | add_pio_map("i8237a-page0", I8237A_PAGE_0_PORT, i8237a_base, 1, i8237a_io_handler); 28 | i8237a_base[0] = 0xff; 29 | } 30 | -------------------------------------------------------------------------------- /src/isa/x86/device/i8253.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define I8253_PORT 0x40 19 | 20 | static uint8_t *i8253_base = NULL; 21 | 22 | static void i8253_io_handler(uint32_t offset, int len, bool is_write) { 23 | } 24 | 25 | void init_i8253() { 26 | i8253_base = (void *)new_space(4); 27 | add_pio_map("i8253-pit", I8253_PORT, i8253_base, 4, i8253_io_handler); 28 | } 29 | -------------------------------------------------------------------------------- /src/isa/x86/device/i8259a.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define I8259A_MASTER_PORT 0x20 19 | #define I8259A_SLAVE_PORT 0xa0 20 | 21 | static uint8_t *i8259a_base = NULL; 22 | 23 | static void i8259a_io_handler(uint32_t offset, int len, bool is_write) { 24 | } 25 | 26 | void init_i8259a() { 27 | i8259a_base = (void *)new_space(2); 28 | add_pio_map("i8259a-master", I8259A_MASTER_PORT, i8259a_base, 2, i8259a_io_handler); 29 | add_pio_map("i8259a-slave", I8259A_SLAVE_PORT, i8259a_base, 2, i8259a_io_handler); 30 | } 31 | -------------------------------------------------------------------------------- /src/isa/x86/device/ioport80.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | // this is an empty device in QEMU 19 | #define IOPORT80_PORT 0x80 20 | 21 | static uint8_t *ioport80_base = NULL; 22 | 23 | static void ioport80_io_handler(uint32_t offset, int len, bool is_write) { 24 | } 25 | 26 | void init_ioport80() { 27 | ioport80_base = (void *)new_space(1); 28 | add_pio_map("ioport80", IOPORT80_PORT, ioport80_base, 1, ioport80_io_handler); 29 | } 30 | -------------------------------------------------------------------------------- /src/isa/x86/device/mc146818rtc.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define RTC_PORT 0x70 19 | 20 | static uint8_t *rtc_base = NULL; 21 | 22 | static void rtc_io_handler(uint32_t offset, int len, bool is_write) { 23 | } 24 | 25 | void init_mc146818rtc() { 26 | rtc_base = (void *)new_space(2); 27 | add_pio_map("mc146818rtc", RTC_PORT, rtc_base, 2, rtc_io_handler); 28 | } 29 | -------------------------------------------------------------------------------- /src/isa/x86/difftest/ref.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | #include "../local-include/intr.h" 19 | 20 | void isa_difftest_regcpy(void *dut, bool direction) { 21 | if (direction == DIFFTEST_TO_REF) memcpy(&cpu, dut, DIFFTEST_REG_SIZE); 22 | else memcpy(dut, &cpu, DIFFTEST_REG_SIZE); 23 | } 24 | 25 | void isa_difftest_raise_intr(word_t NO) { 26 | cpu.pc = raise_intr(NO, cpu.pc); 27 | } 28 | -------------------------------------------------------------------------------- /src/isa/x86/exec/all-instr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../local-include/rtl.h" 17 | 18 | #include "arith.h" 19 | #include "control.h" 20 | #include "data-mov.h" 21 | #include "logic.h" 22 | #include "string.h" 23 | #include "system.h" 24 | #include "eflags.h" 25 | #include "bit.h" 26 | #include "misc.h" 27 | #include "vector.h" 28 | #include "fp.h" 29 | 30 | def_EHelper(nop); 31 | def_EHelper(inv); 32 | def_EHelper(nemu_trap); 33 | -------------------------------------------------------------------------------- /src/isa/x86/include/isa-all-instr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include "../local-include/rtl.h" 18 | #include "../instr/rt.h" 19 | 20 | #define INSTR_NULLARY(f) \ 21 | f(inv) f(nemu_trap) f(nop) \ 22 | f(ret) f(leave) \ 23 | f(cwtl) f(cltd) \ 24 | f(movs) \ 25 | f(rep_movs) f(rep_stos) \ 26 | f(pushf) f(pusha) f(popa) \ 27 | f(clc) f(stc) f(cld) \ 28 | f(iret) \ 29 | f(cpuid) f(rdtsc) 30 | 31 | #define INSTR_UNARY(f) \ 32 | f(call) f(jcc) f(jmp) f(setcc) f(call_E) f(jmp_E) f(ret_imm) f(jecxz) \ 33 | f(push) f(pop) \ 34 | f(inc) f(dec) f(neg) f(not) \ 35 | f(mul) f(imul1) f(div) f(idiv) \ 36 | f(lgdt) f(lidt) f(ltr) f(_int) 37 | 38 | #define INSTR_BINARY(f) \ 39 | f(mov) f(add) f(sub) f(adc) f(sbb) f(cmp) \ 40 | f(and) f(or) f(test) f(xor) f(shl) f(shr) f(sar) f(rol) f(ror) \ 41 | f(lea) f(movzb) f(movzw) f(movsb) f(movsw) \ 42 | f(imul2) \ 43 | f(bsr) f(bt) f(xchg) f(cmpxchg) \ 44 | f(in) f(out) f(mov_cr2r) f(mov_r2cr) \ 45 | f(cmovcc) \ 46 | f(mov_rm2sreg) \ 47 | f(movq_E2xmm) f(movq_xmm2E) f(movdqa_E2xmm) f(psrlq) f(movd_xmm2E) f(pxor) \ 48 | f(xadd) 49 | 50 | #define INSTR_TERNARY(f) \ 51 | f(imul3) f(shld) f(shrd) 52 | 53 | def_all_EXEC_ID(); 54 | -------------------------------------------------------------------------------- /src/isa/x86/include/isa-exec.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../instr/data-mov.h" 17 | #include "../instr/arith.h" 18 | #include "../instr/logic.h" 19 | #include "../instr/control.h" 20 | #include "../instr/string.h" 21 | #include "../instr/bit.h" 22 | #include "../instr/eflags.h" 23 | #include "../instr/system.h" 24 | #include "../instr/misc.h" 25 | #include "../instr/special.h" 26 | #include "../instr/vector.h" 27 | -------------------------------------------------------------------------------- /src/isa/x86/instr/eflags.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(pushf) { 17 | void rtl_compute_eflags(Decode *s, rtlreg_t *dest); 18 | rtl_compute_eflags(s, s0); 19 | rtl_push(s, s0); 20 | 21 | extern void difftest_fix_eflags(void *arg); 22 | difftest_set_patch(difftest_fix_eflags, (void *)(uintptr_t)cpu.esp); 23 | } 24 | 25 | def_EHelper(clc) { 26 | rtl_set_CF(s, rz); 27 | } 28 | 29 | def_EHelper(stc) { 30 | rtl_li(s, s0, 1); 31 | rtl_set_CF(s, s0); 32 | } 33 | 34 | def_EHelper(cld) { 35 | rtl_set_DF(s, rz); 36 | } 37 | 38 | #if 0 39 | static inline def_EHelper(std) { 40 | rtl_li(s, s0, 1); 41 | rtl_set_DF(s, s0); 42 | print_asm("std"); 43 | } 44 | 45 | static inline def_EHelper(cli) { 46 | rtl_set_IF(s, rz); 47 | print_asm("cli"); 48 | } 49 | 50 | static inline def_EHelper(sti) { 51 | rtl_li(s, s0, 1); 52 | rtl_set_IF(s, s0); 53 | print_asm("sti"); 54 | } 55 | 56 | static inline def_EHelper(popf) { 57 | void rtl_set_eflags(Decode *s, const rtlreg_t *src); 58 | rtl_pop(s, s0); 59 | rtl_set_eflags(s, s0); 60 | print_asm("popf"); 61 | } 62 | 63 | static inline def_EHelper(sahf) { 64 | void rtl_set_eflags(Decode *s, const rtlreg_t *src); 65 | void rtl_compute_eflags(Decode *s, rtlreg_t *dest); 66 | 67 | rtl_compute_eflags(s, s0); 68 | rtl_andi(s, s0, s0, ~0xff); 69 | rtl_lr(s, s1, R_AH, 1); 70 | rtl_or(s, s0, s0, s1); 71 | rtl_set_eflags(s, s0); 72 | 73 | print_asm("sahf"); 74 | } 75 | #endif 76 | -------------------------------------------------------------------------------- /src/isa/x86/instr/fp.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "../local-include/intr.h" 17 | #include 18 | #include 19 | #include 20 | 21 | uint32_t isa_fp_get_rm(Decode *s) { 22 | assert(0); 23 | } 24 | 25 | void isa_fp_set_ex(uint32_t ex) { 26 | assert(0); 27 | } 28 | -------------------------------------------------------------------------------- /src/isa/x86/instr/misc.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(cpuid) { 17 | rtl_mv(s, &cpu.eax, rz); 18 | rtl_mv(s, &cpu.ebx, rz); 19 | rtl_mv(s, &cpu.ecx, rz); 20 | rtl_mv(s, &cpu.edx, rz); 21 | difftest_skip_ref(); 22 | } 23 | 24 | def_EHelper(rdtsc) { 25 | #if defined(CONFIG_DETERMINISTIC) || defined(CONFIG_ENGINE_INTERPRETER) 26 | rtl_li(s, &cpu.edx, 0); 27 | rtl_li(s, &cpu.eax, 0); 28 | #else 29 | uint64_t tsc = get_time(); 30 | cpu.edx = tsc >> 32; 31 | cpu.eax = tsc & 0xffffffff; 32 | #endif 33 | 34 | difftest_skip_ref(); 35 | } 36 | 37 | #if 0 38 | static inline def_EHelper(fwait) { 39 | print_asm("fwait"); 40 | } 41 | 42 | static inline def_EHelper(fpu) { 43 | rtl_trap(s, cpu.pc, 7); 44 | } 45 | 46 | static inline def_EHelper(hlt) { 47 | rtl_trap(s, s->seq_pc, IRQ_TIMER); 48 | if (ref_difftest_raise_intr) ref_difftest_raise_intr(IRQ_TIMER); 49 | print_asm("hlt"); 50 | } 51 | #endif 52 | -------------------------------------------------------------------------------- /src/isa/x86/instr/special.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | def_EHelper(nop) { 17 | } 18 | 19 | def_EHelper(inv) { 20 | save_globals(s); 21 | rtl_hostcall(s, HOSTCALL_INV, NULL, NULL, NULL, 0); 22 | longjmp_context(NEMU_EXEC_END); 23 | } 24 | 25 | def_EHelper(nemu_trap) { 26 | rtl_hostcall(s, HOSTCALL_EXIT, NULL, &cpu.eax, NULL, 0); 27 | longjmp_context(NEMU_EXEC_END); 28 | } 29 | -------------------------------------------------------------------------------- /src/isa/x86/local-include/intr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __X86_INTR_H__ 17 | #define __X86_INTR_H__ 18 | 19 | #include 20 | 21 | #ifdef CONFIG_PA 22 | #define IRQ_TIMER 32 23 | #else 24 | #define IRQ_TIMER 48 25 | #endif 26 | 27 | word_t raise_intr(word_t NO, vaddr_t ret_addr); 28 | #ifndef CONFIG_PA 29 | #define return_on_mem_ex() do { if (cpu.mem_exception != 0) return; } while (0) 30 | #else 31 | #define return_on_mem_ex() 32 | #endif 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /src/isa/x86/local-include/reg.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __X86_REG_H__ 17 | #define __X86_REG_H__ 18 | 19 | #include 20 | 21 | enum { PRIV_IRET }; 22 | 23 | static inline int check_reg_index(int index) { 24 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 8)); 25 | return index; 26 | } 27 | 28 | #define reg_l(index) (cpu.gpr[check_reg_index(index)]._32) 29 | #define reg_w(index) (cpu.gpr[check_reg_index(index)]._16) 30 | #define reg_b(index) (cpu.gpr[check_reg_index(index) & 0x3]._8[index >> 2]) 31 | 32 | static inline const char* reg_name(int index, int width) { 33 | extern const char* regsl[]; 34 | extern const char* regsw[]; 35 | extern const char* regsb[]; 36 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < 8)); 37 | 38 | switch (width) { 39 | case 4: return regsl[index]; 40 | case 1: return regsb[index]; 41 | case 2: return regsw[index]; 42 | default: assert(0); 43 | } 44 | } 45 | 46 | static inline const char* sreg_name(int index) { 47 | const char *name[] = { "es", "cs", "ss", "ds", "fs", "gs" }; 48 | IFDEF(CONFIG_RT_CHECK, assert(index >= 0 && index < ARRLEN(name))); 49 | return name[index]; 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /src/memory/store_queue_wrapper.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #ifdef CONFIG_STORE_LOG 6 | std::stack store_log_stack; 7 | void store_log_stack_reset() { store_log_stack = {};} 8 | void store_log_stack_push(store_log_t log) { store_log_stack.push(log);} 9 | void store_log_stack_pop() { store_log_stack.pop();} 10 | store_log_t store_log_stack_top() {return store_log_stack.top();} 11 | bool store_log_stack_empty() { return store_log_stack.empty();} 12 | #ifdef CONFIG_LIGHTQS 13 | std::stack spec_store_log_stack; 14 | void spec_store_log_stack_reset() { spec_store_log_stack = {};} 15 | void spec_store_log_stack_push(store_log_t log) { spec_store_log_stack.push(log);} 16 | void spec_store_log_stack_pop() { spec_store_log_stack.pop();} 17 | store_log_t spec_store_log_stack_top() { return spec_store_log_stack.top();} 18 | bool spec_store_log_stack_empty() { return spec_store_log_stack.empty();} 19 | void spec_store_log_stack_copy() { store_log_stack = spec_store_log_stack;} 20 | #endif // CONFIG_LIGHTQS 21 | #endif // CONFIG_STORE_LOG 22 | 23 | #ifdef CONFIG_DIFFTEST_STORE_COMMIT 24 | 25 | std::queue cpp_store_event_queue; 26 | 27 | void store_queue_reset() { 28 | cpp_store_event_queue = {}; 29 | } 30 | 31 | void store_queue_push(store_commit_t store_commit) { 32 | cpp_store_event_queue.push(store_commit); 33 | } 34 | 35 | void store_queue_pop() { 36 | cpp_store_event_queue.pop(); 37 | } 38 | 39 | store_commit_t store_queue_front() { 40 | auto store_commit = cpp_store_event_queue.front(); 41 | return store_commit; 42 | } 43 | 44 | store_commit_t store_queue_back() { 45 | auto store_commit = cpp_store_event_queue.back(); 46 | return store_commit; 47 | } 48 | 49 | size_t store_queue_size() { 50 | return cpp_store_event_queue.size(); 51 | } 52 | 53 | bool store_queue_empty() { 54 | return cpp_store_event_queue.empty(); 55 | } 56 | 57 | #endif 58 | -------------------------------------------------------------------------------- /src/nemu-main.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #ifndef CONFIG_SHARE 18 | void init_monitor(int, char *[]); 19 | void engine_start(); 20 | int is_exit_status_bad(); 21 | 22 | int main(int argc, char *argv[]) { 23 | /* Initialize the monitor. */ 24 | init_monitor(argc, argv); 25 | 26 | /* Start engine. */ 27 | engine_start(); 28 | 29 | return is_exit_status_bad(); 30 | } 31 | #endif 32 | -------------------------------------------------------------------------------- /src/profiling/profiling_control.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int profiling_state = NoProfiling; 4 | int checkpoint_state = NoCheckpoint; 5 | bool checkpoint_taking = false; 6 | uint64_t checkpoint_interval = 0; 7 | uint64_t warmup_interval = 0; 8 | uint64_t checkpoint_icount_base = 0; 9 | 10 | bool recvd_manual_oneshot_cpt = false; 11 | bool recvd_manual_uniform_cpt = false; 12 | 13 | bool force_cpt_mmode = false; 14 | 15 | bool donot_skip_boot=false; 16 | bool workload_loaded=false; 17 | 18 | void start_profiling() { 19 | workload_loaded=true; 20 | uint64_t get_abs_instr_count(); 21 | checkpoint_icount_base = get_abs_instr_count(); 22 | Log("Start profiling. Setting inst count base to Current inst count %lu.", checkpoint_icount_base); 23 | } 24 | 25 | #ifdef CONFIG_SHARE 26 | // empty definition on share 27 | void simpoint_profiling(uint64_t pc, bool is_control, uint64_t abs_instr_count) {} 28 | #endif -------------------------------------------------------------------------------- /src/user/init/riscv64.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #ifdef CONFIG_ISA_riscv64 19 | void init_csr(); 20 | 21 | void isa_init_user(word_t sp) { 22 | init_csr(); 23 | cpu.mode = MODE_U; 24 | cpu.gpr[2]._64 = sp; 25 | //cpu.edx = 0; // no handler for atexit() 26 | } 27 | #endif 28 | -------------------------------------------------------------------------------- /src/user/init/x86.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #ifdef CONFIG_ISA_x86 19 | // we only maintain base of the segment here 20 | uint32_t GDT[4] = {0}; 21 | 22 | void isa_init_user(word_t sp) { 23 | cpu.esp = sp; 24 | cpu.edx = 0; // no handler for atexit() 25 | cpu.sreg[CSR_CS].val = 0xb; cpu.sreg[CSR_CS].base = 0; 26 | cpu.sreg[CSR_DS].val = 0xb; cpu.sreg[CSR_DS].base = 0; 27 | cpu.sreg[CSR_ES].val = 0xb; cpu.sreg[CSR_ES].base = 0; 28 | cpu.sreg[CSR_FS].val = 0xb; cpu.sreg[CSR_FS].base = 0; 29 | } 30 | #endif 31 | -------------------------------------------------------------------------------- /src/user/user.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __USER_H__ 17 | #define __USER_H__ 18 | 19 | #ifndef __cplusplus 20 | #define _GNU_SOURCE 21 | #endif 22 | 23 | #include 24 | #include 25 | 26 | typedef struct { 27 | word_t entry; 28 | word_t brk; 29 | word_t brk_page; 30 | word_t program_brk; 31 | word_t phdr; 32 | int phent; 33 | int phnum; 34 | int std_fd[3]; 35 | } user_state_t; 36 | 37 | extern user_state_t user_state; 38 | 39 | void *user_mmap(void *addr, size_t length, int prot, 40 | int flags, int fd, off_t offset); 41 | int user_munmap(void *addr, size_t length); 42 | void *user_mremap(void *old_addr, size_t old_size, size_t new_size, 43 | int flags, void *new_addr); 44 | 45 | static inline uint8_t* user_to_host(word_t uaddr) { 46 | return (uint8_t *)(uintptr_t)uaddr; 47 | } 48 | 49 | static inline word_t host_to_user(void *haddr) { 50 | return (word_t)(uintptr_t)haddr; 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /src/utils/dynamic-config.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | struct DynamicConfig dynamic_config = {}; 19 | void update_dynamic_config(void* config) { 20 | memcpy((void*)&dynamic_config, config, sizeof(struct DynamicConfig)); 21 | // printf("NEMU dynamic_config update\n"); 22 | // printf(" - ignore_illegal_mem_access %x\n", dynamic_config.ignore_illegal_mem_access); 23 | // printf(" - debug_difftest %x\n", dynamic_config.debug_difftest); 24 | } 25 | -------------------------------------------------------------------------------- /src/utils/filename.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2020-2022 Institute of Computing Technology, Chinese Academy of Sciences 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "debug.h" 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | bool is_gz_file(const char *filename) 23 | { 24 | int fd = -1; 25 | 26 | fd = open(filename, O_RDONLY); 27 | assert(fd); 28 | 29 | uint8_t buf[2]; 30 | 31 | size_t sz = read(fd, buf, 2); 32 | if (sz != 2) { 33 | close(fd); 34 | xpanic("Couldn't read magic bytes from object file %s", filename); 35 | } 36 | 37 | close(fd); 38 | 39 | const uint8_t gz_magic[2] = {0x1f, 0x8B}; 40 | return memcmp(buf, gz_magic, 2) == 0; 41 | } 42 | 43 | bool is_zstd_file(const char *filename){ 44 | int fd = -1; 45 | 46 | fd = open(filename, O_RDONLY); 47 | assert(fd); 48 | 49 | uint8_t buf[4]; 50 | 51 | size_t sz = read(fd, buf, 4); 52 | if (sz != 4) { 53 | close(fd); 54 | xpanic("Couldn't read magic bytes from object file %s", filename); 55 | } 56 | 57 | close(fd); 58 | 59 | const uint8_t zstd_magic[4] = {0x28, 0xB5, 0x2F, 0xFD}; 60 | return memcmp(buf, zstd_magic, 4) == 0; 61 | } -------------------------------------------------------------------------------- /src/utils/iqueue.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define INSTR_QUEUE_SIZE (1 << 5) 19 | 20 | static struct { 21 | vaddr_t pc; 22 | uint8_t instr[20]; 23 | uint8_t ilen; 24 | } iqueue[INSTR_QUEUE_SIZE]; 25 | static uint32_t q_idx = 0; 26 | 27 | void iqueue_commit(vaddr_t pc, uint8_t *instr_buf, uint8_t ilen) { 28 | iqueue[q_idx].pc = pc; 29 | iqueue[q_idx].ilen = ilen; 30 | assert(ilen < 20); 31 | memcpy(iqueue[q_idx].instr, instr_buf, ilen); 32 | q_idx = (q_idx + 1) % INSTR_QUEUE_SIZE; 33 | } 34 | 35 | void iqueue_dump() { 36 | int i; 37 | int victim_idx = (q_idx - 1) % INSTR_QUEUE_SIZE; 38 | printf("======== instruction queue =========\n"); 39 | for (i = 0; i < INSTR_QUEUE_SIZE; i ++) { 40 | printf("%5s " FMT_WORD ": ", (i == victim_idx ? "-->" : ""), iqueue[i].pc); 41 | int j; 42 | for (j = 0; j < iqueue[i].ilen; j ++) { 43 | printf("%02x ", iqueue[i].instr[j]); 44 | } 45 | printf("\n"); 46 | } 47 | printf("======== instruction queue end =========\n"); 48 | } 49 | -------------------------------------------------------------------------------- /src/utils/state.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #ifdef CONFIG_SHARE 19 | NEMUState nemu_state = { .state = NEMU_RUNNING }; 20 | #else 21 | NEMUState nemu_state = { .state = NEMU_STOP }; 22 | #endif // CONFIG_SHARE 23 | 24 | int is_exit_status_bad() { 25 | int good = (nemu_state.state == NEMU_END && nemu_state.halt_ret == 0) || 26 | (nemu_state.state == NEMU_QUIT); 27 | if (!good) { 28 | Log("NEMU exit with bad state: %i, halt ret: %i", nemu_state.state, nemu_state.halt_ret); 29 | } else { 30 | Log("NEMU exit with good state: %i, halt ret: %i", nemu_state.state, nemu_state.halt_ret); 31 | } 32 | extern void log_close(); 33 | log_close(); 34 | return !good; 35 | } 36 | -------------------------------------------------------------------------------- /src/utils/timer.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include MUXDEF(CONFIG_TIMER_GETTIMEOFDAY, , ) 18 | 19 | IFDEF(CONFIG_TIMER_CLOCK_GETTIME, 20 | static_assert(CLOCKS_PER_SEC == 1000000, "CLOCKS_PER_SEC != 1000000")); 21 | IFDEF(CONFIG_TIMER_CLOCK_GETTIME, 22 | static_assert(sizeof(clock_t) == 8, "sizeof(clock_t) != 8")); 23 | 24 | static uint64_t boot_time = 0; 25 | 26 | static uint64_t get_time_internal() { 27 | #ifdef CONFIG_TIMER_GETTIMEOFDAY 28 | struct timeval now; 29 | gettimeofday(&now, NULL); 30 | uint64_t us = now.tv_sec * 1000000 + now.tv_usec; 31 | #else 32 | struct timespec now; 33 | clock_gettime(CLOCK_MONOTONIC_COARSE, &now); 34 | uint64_t us = now.tv_sec * 1000000 + now.tv_nsec / 1000; 35 | #endif 36 | return us; 37 | } 38 | 39 | void init_time() { 40 | boot_time = get_time_internal(); 41 | } 42 | 43 | uint64_t get_time() { 44 | if (boot_time == 0) init_time(); 45 | uint64_t now = get_time_internal(); 46 | return now - boot_time; 47 | } 48 | -------------------------------------------------------------------------------- /tools/export/.gitignore: -------------------------------------------------------------------------------- 1 | output/ 2 | -------------------------------------------------------------------------------- /tools/export/ics-export.py: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | #!/usr/bin/env python3 17 | 18 | from utils import export 19 | 20 | WHITE_LIST = [ 21 | r'.gitignore', 22 | r'^/*$', 23 | r'Makefile', 24 | r'^/src/', 25 | r'^/include/', 26 | r'^/tools/gen-expr/', 27 | r'^/tools/kvm-diff/', 28 | r'^/tools/qemu-diff/', 29 | ] 30 | 31 | BLACK_LIST = [ 32 | r'/build/', 33 | r'/export/', 34 | r'/.git/', 35 | r'riscv64', 36 | r'/difftest/ref.c', 37 | r'^/resource/', 38 | r'^/src/device/sdcard.c', 39 | r'^/src/device/mmc.h', 40 | r'^/src/engine/rv64', 41 | r'^/src/isa/mips32/local-include/intr.h', 42 | r'^/src/isa/x86/exec/eflags.c', 43 | r'^/src/isa/x86/exec/lazycc.h', 44 | r'^/src/isa/x86/exec/string.h', 45 | r'^/src/isa/x86/kvm/', 46 | r'^/src/isa/x86/local-include/mmu.h', 47 | r'^/src/memory/vaddr.c', 48 | ] 49 | 50 | export(WHITE_LIST, BLACK_LIST) 51 | -------------------------------------------------------------------------------- /tools/export/rv64-export.py: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | #!/usr/bin/env python3 17 | 18 | from utils import export 19 | 20 | WHITE_LIST = [ 21 | r'.gitignore', 22 | r'riscv64', 23 | r'^/*$', 24 | r'Makefile', 25 | r'^/src/*$', 26 | r'^/src/monitor/*$', 27 | r'^/src/monitor/debug/*$', 28 | r'^/src/monitor/difftest/*$', 29 | r'^/src/device/*$', 30 | r'^/src/device/io/*$', 31 | r'^/src/engine/interpreter/*$', 32 | r'^/src/memory/*$', 33 | r'^/include/*$', 34 | r'^/include/cpu/*$', 35 | r'^/include/device/*$', 36 | r'^/include/memory/*$', 37 | r'^/include/monitor/*$', 38 | r'^/include/rtl/*$', 39 | ] 40 | 41 | BLACK_LIST = [ 42 | r'/build/', 43 | r'/export/', 44 | r'/gen-expr/', 45 | r'/kvm-diff/', 46 | r'/recorder/', 47 | r'/.git/', 48 | r'mips32', 49 | r'riscv32', 50 | r'x86', 51 | r'/resource/bbl', 52 | r'/engine/rv64', 53 | r'/device/audio.c', 54 | r'/device/vga.c', 55 | r'/device/keyboard.c', 56 | r'runall.sh', 57 | ] 58 | 59 | export(WHITE_LIST, BLACK_LIST) 60 | -------------------------------------------------------------------------------- /tools/export/utils.py: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | from pathlib import Path 17 | from os import system 18 | import re, shutil 19 | 20 | NEMU_HOME = (Path(__file__) / '../../..').resolve() 21 | EXPORT_DIR = NEMU_HOME / 'tools' / 'export' / 'output' 22 | UNIFDEF_FLAGS = '-D__ICS_EXPORT -UISA64 -U__ENGINE_rv64__ -ULAZY_CC' 23 | UNIFDEF_EXTRA_FILE_LIST = [ 24 | r'^/runall.sh', 25 | r'^/Makefile.git', 26 | ] 27 | 28 | def convert(r): 29 | return r.replace('.', r'\.').replace('*', r'[^/]*') 30 | 31 | def list_filter(path, xs): 32 | for x in xs: 33 | if re.search(convert(x), path): 34 | return True 35 | return False 36 | 37 | def export(white_list, black_list): 38 | def files(): 39 | for abspath in NEMU_HOME.rglob('*'): 40 | if abspath.is_file(): 41 | path = abspath.relative_to(NEMU_HOME) 42 | path_str = '/' + str(path) 43 | white = list_filter(path_str, white_list) 44 | black = list_filter(path_str, black_list) 45 | if white and not black: 46 | print('COPY', path) 47 | yield abspath, path 48 | 49 | try: 50 | shutil.rmtree(EXPORT_DIR) 51 | except: 52 | pass 53 | 54 | for abspath, relpath in files(): 55 | src = abspath 56 | dst = EXPORT_DIR / relpath 57 | dst.parent.mkdir(parents=True, exist_ok=True) 58 | if dst.match('*.[ch]') or list_filter('/' + str(relpath), UNIFDEF_EXTRA_FILE_LIST): 59 | system("unifdef " + UNIFDEF_FLAGS + ' ' + str(src) + " > " + str(dst)) 60 | else: 61 | shutil.copyfile(src, dst) 62 | -------------------------------------------------------------------------------- /tools/fixdep/Makefile: -------------------------------------------------------------------------------- 1 | NAME = fixdep 2 | SRCS = fixdep.c 3 | include $(NEMU_HOME)/scripts/build.mk 4 | -------------------------------------------------------------------------------- /tools/gen-expr/.gitignore: -------------------------------------------------------------------------------- 1 | .code.c 2 | -------------------------------------------------------------------------------- /tools/gen-expr/Makefile: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | NAME = gen-expr 17 | SRCS = gen-expr.c 18 | include $(NEMU_HOME)/scripts/build.mk 19 | -------------------------------------------------------------------------------- /tools/kconfig/.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | !lexer.l 3 | !parser.y 4 | -------------------------------------------------------------------------------- /tools/kconfig/Makefile: -------------------------------------------------------------------------------- 1 | NAME = conf 2 | obj := build 3 | SRCS += confdata.c expr.c preprocess.c symbol.c util.c 4 | SRCS += $(obj)/lexer.lex.c $(obj)/parser.tab.c 5 | CFLAGS += -DYYDEBUG 6 | INC_DIR += . 7 | 8 | ifeq ($(NAME),conf) 9 | SRCS += conf.c 10 | else ifeq ($(NAME),mconf) 11 | SRCS += mconf.c $(shell find lxdialog/ -name "*.c") 12 | LIBS += -lncurses -ltinfo 13 | else 14 | $(error bad target=$(NAME)) 15 | endif 16 | 17 | include $(NEMU_HOME)/scripts/build.mk 18 | 19 | $(obj)/lexer.lex.o: $(obj)/parser.tab.h 20 | $(obj)/lexer.lex.c: lexer.l $(obj)/parser.tab.h 21 | @echo + LEX $@ 22 | @flex -o $@ $< 23 | 24 | $(obj)/parser.tab.c $(obj)/parser.tab.h: parser.y 25 | @echo + YACC $@ 26 | @bison -v $< --defines=$(obj)/parser.tab.h -o $(obj)/parser.tab.c 27 | 28 | conf: 29 | @$(MAKE) -s 30 | 31 | mconf: 32 | @$(MAKE) -s NAME=mconf 33 | 34 | .PHONY: conf mconf 35 | -------------------------------------------------------------------------------- /tools/kconfig/lkc_proto.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #include 3 | 4 | /* confdata.c */ 5 | void conf_parse(const char *name); 6 | int conf_read(const char *name); 7 | int conf_read_simple(const char *name, int); 8 | int conf_write_defconfig(const char *name); 9 | int conf_write(const char *name); 10 | int conf_write_autoconf(int overwrite); 11 | bool conf_get_changed(void); 12 | void conf_set_changed_callback(void (*fn)(void)); 13 | void conf_set_message_callback(void (*fn)(const char *s)); 14 | 15 | /* symbol.c */ 16 | extern struct symbol * symbol_hash[SYMBOL_HASHSIZE]; 17 | 18 | struct symbol * sym_lookup(const char *name, int flags); 19 | struct symbol * sym_find(const char *name); 20 | const char * sym_escape_string_value(const char *in); 21 | struct symbol ** sym_re_search(const char *pattern); 22 | const char * sym_type_name(enum symbol_type type); 23 | void sym_calc_value(struct symbol *sym); 24 | enum symbol_type sym_get_type(struct symbol *sym); 25 | bool sym_tristate_within_range(struct symbol *sym,tristate tri); 26 | bool sym_set_tristate_value(struct symbol *sym,tristate tri); 27 | tristate sym_toggle_tristate_value(struct symbol *sym); 28 | bool sym_string_valid(struct symbol *sym, const char *newval); 29 | bool sym_string_within_range(struct symbol *sym, const char *str); 30 | bool sym_set_string_value(struct symbol *sym, const char *newval); 31 | bool sym_is_changeable(struct symbol *sym); 32 | struct property * sym_get_choice_prop(struct symbol *sym); 33 | const char * sym_get_string_value(struct symbol *sym); 34 | 35 | const char * prop_get_type_name(enum prop_type type); 36 | 37 | /* preprocess.c */ 38 | enum variable_flavor { 39 | VAR_SIMPLE, 40 | VAR_RECURSIVE, 41 | VAR_APPEND, 42 | }; 43 | void env_write_dep(FILE *f, const char *auto_conf_name); 44 | void variable_add(const char *name, const char *value, 45 | enum variable_flavor flavor); 46 | void variable_all_del(void); 47 | char *expand_dollar(const char **str); 48 | char *expand_one_token(const char **str); 49 | 50 | /* expr.c */ 51 | void expr_print(struct expr *e, void (*fn)(void *, struct symbol *, const char *), void *data, int prevtoken); 52 | -------------------------------------------------------------------------------- /tools/kvm-diff/Makefile: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | NAME = kvm 17 | SRCS = $(shell find src/ -name "*.c") 18 | 19 | SHARE = 1 20 | INC_DIR += $(NEMU_HOME)/include $(NEMU_HOME)/src/isa/x86/include 21 | ISA = x86 22 | 23 | include $(NEMU_HOME)/scripts/isa.mk 24 | include $(NEMU_HOME)/scripts/build.mk 25 | -------------------------------------------------------------------------------- /tools/kvm-diff/include/paddr.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | // this is an empty file to avoid compile error 17 | -------------------------------------------------------------------------------- /tools/qemu-dl-diff/Makefile: -------------------------------------------------------------------------------- 1 | NAME = qemu 2 | SRCS = $(shell find src/ -name "*.c" | grep -v "isa") 3 | SRCS += $(shell find src/isa/$(ISA) -name "*.c") 4 | 5 | SHARE = 1 6 | CFLAGS += -DNEMU_HOME=$(NEMU_HOME) 7 | 8 | include $(NEMU_HOME)/scripts/isa.mk 9 | include $(NEMU_HOME)/scripts/build.mk 10 | -------------------------------------------------------------------------------- /tools/qemu-dl-diff/include/common.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __COMMON_H__ 17 | #define __COMMON_H__ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | typedef uint32_t paddr_t; 28 | 29 | #endif 30 | -------------------------------------------------------------------------------- /tools/qemu-dl-diff/src/isa/mips32/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | #define _str(x) # x 19 | #define str(x) _str(x) 20 | 21 | char *isa_qemu_argv[] = { 22 | "/usr/bin/qemu-system-mipsel", 23 | "-nographic", "-S", "-serial", "none", "-monitor", "none", 24 | "-machine", "mipssim", "-kernel", str(NEMU_HOME) "/resource/mips-elf/mips.dummy", 25 | NULL 26 | }; 27 | 28 | void init_isa() { 29 | } 30 | -------------------------------------------------------------------------------- /tools/qemu-dl-diff/src/isa/riscv64/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | 18 | char *isa_qemu_argv[] = { 19 | "/usr/bin/qemu-system-riscv64", 20 | "-nographic", "-S", "-serial", "none", "-monitor", "none", 21 | NULL 22 | }; 23 | 24 | void init_isa() { 25 | } 26 | -------------------------------------------------------------------------------- /tools/qemu-dl-diff/src/isa/x86/intr.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include 17 | #include 18 | 19 | static void (*qemu_do_interrupt_all)(void *cpu, int intno, 20 | int is_int, int error_code, uint32_t next_eip, int is_hw) = NULL; 21 | 22 | void* get_loaded_addr(char *sym, int type); 23 | void* qemu_get_cpu(); 24 | void hack_fun_return_1(char *funname); 25 | 26 | void isa_raise_intr(uint64_t NO) { 27 | qemu_do_interrupt_all(qemu_get_cpu(), NO, 0, 0, 0, 1); 28 | } 29 | 30 | void init_intr() { 31 | qemu_do_interrupt_all = get_loaded_addr("do_interrupt_all", STT_FUNC); 32 | hack_fun_return_1("x86_cpu_has_work"); 33 | } 34 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/Makefile: -------------------------------------------------------------------------------- 1 | #*************************************************************************************** 2 | # Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | # 4 | # NEMU is licensed under Mulan PSL v2. 5 | # You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | # You may obtain a copy of Mulan PSL v2 at: 7 | # http://license.coscl.org.cn/MulanPSL2 8 | # 9 | # THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | # EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | # MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | # 13 | # See the Mulan PSL v2 for more details. 14 | #**************************************************************************************/ 15 | 16 | NAME = qemu 17 | SRCS = $(shell find src/ -name "*.c" | grep -v "isa") 18 | SRCS += $(shell find src/isa/$(ISA) -name "*.c") 19 | 20 | SHARE = 1 21 | CFLAGS += -DNEMU_HOME=$(NEMU_HOME) -D_ISA_H_=\"isa/$(ISA).h\" 22 | 23 | include $(NEMU_HOME)/scripts/isa.mk 24 | include $(NEMU_HOME)/scripts/build.mk 25 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/common.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __COMMON_H__ 17 | #define __COMMON_H__ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | typedef uint32_t paddr_t; 28 | 29 | #include "protocol.h" 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/isa/mips32.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __MIPS32_H__ 17 | #define __MIPS32_H__ 18 | 19 | #define _str(x) # x 20 | #define str(x) _str(x) 21 | 22 | #define ISA_QEMU_BIN "qemu-system-mipsel" 23 | #define ISA_QEMU_ARGS "-machine", "mipssim",\ 24 | "-kernel", str(NEMU_HOME) "/resource/mips-elf/mips.dummy", 25 | 26 | union isa_gdb_regs { 27 | struct { 28 | uint32_t gpr[32]; 29 | uint32_t status, lo, hi, badvaddr, cause, pc; 30 | }; 31 | struct { 32 | uint32_t array[77]; 33 | }; 34 | }; 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/isa/riscv32.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV32_H__ 17 | #define __RISCV32_H__ 18 | 19 | #define ISA_QEMU_BIN "qemu-system-riscv32" 20 | #define ISA_QEMU_ARGS 21 | 22 | union isa_gdb_regs { 23 | struct { 24 | uint32_t gpr[32]; 25 | uint32_t pc; 26 | }; 27 | struct { 28 | uint32_t array[77]; 29 | }; 30 | }; 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/isa/riscv64.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __RISCV64_H__ 17 | #define __RISCV64_H__ 18 | 19 | #define ISA_QEMU_BIN "qemu-system-riscv64" 20 | #define ISA_QEMU_ARGS 21 | 22 | union isa_gdb_regs { 23 | struct { 24 | uint64_t gpr[32]; 25 | uint64_t fpr[32]; 26 | uint64_t pc; 27 | }; 28 | struct { 29 | uint32_t array[DIFFTEST_REG_SIZE/sizeof(uint32_t)]; 30 | }; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/isa/x86.h: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #ifndef __X86_H__ 17 | #define __X86_H__ 18 | 19 | #define ISA_QEMU_BIN "qemu-system-i386" 20 | #define ISA_QEMU_ARGS 21 | 22 | union isa_gdb_regs { 23 | struct { 24 | uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi; 25 | uint32_t eip, eflags; 26 | uint32_t cs, ss, ds, es, fs, gs; 27 | }; 28 | struct { 29 | uint32_t array[77]; 30 | }; 31 | }; 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/include/protocol.h: -------------------------------------------------------------------------------- 1 | /* Simple interface of a GDB remote protocol client. 2 | * Copyright (C) 2015 Red Hat Inc. 3 | * 4 | * This file is part of gdb-toys. 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 3 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include _ISA_H_ 22 | 23 | struct gdb_conn; 24 | 25 | uint16_t gdb_decode_hex(uint8_t msb, uint8_t lsb); 26 | uint64_t gdb_decode_hex_str(uint8_t *bytes); 27 | 28 | uint8_t hex_encode(uint8_t digit); 29 | 30 | struct gdb_conn *gdb_begin_inet(const char *addr, uint16_t port); 31 | 32 | void gdb_end(struct gdb_conn *conn); 33 | 34 | void gdb_send(struct gdb_conn *conn, const uint8_t *command, size_t size); 35 | 36 | uint8_t *gdb_recv(struct gdb_conn *conn, size_t *size); 37 | 38 | const char * gdb_start_noack(struct gdb_conn *conn); 39 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/src/isa/mips32/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | void init_isa() { 17 | } 18 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/src/isa/riscv32/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | #include "common.h" 17 | 18 | bool gdb_memcpy_to_qemu(uint32_t, void *, int); 19 | bool gdb_getregs(union isa_gdb_regs *); 20 | bool gdb_setregs(union isa_gdb_regs *); 21 | void difftest_exec(uint64_t n); 22 | 23 | static uint32_t initcode[] = { 24 | 0x800006b7, // lui a3,0x8000 25 | 0x03c68693, // addi a3,a3,0x3c # 800003c 26 | 0x30569073, // csrw mtvec,a3 27 | 28 | 0xfff00513, // li a0, -1 29 | 0x01f00593, // li a1, 31 30 | 0x3b051073, // csrw pmpaddr0, a0 31 | 0x3a059073, // csrw pmpcfg0, a1 32 | 33 | 0x000c1637, // lui a2,0xc2 # c0001 34 | 0x80060613, // addi a2,a2,-2048 # c0800 35 | 0x30061073, // csrw mstatus,a2 36 | 37 | 0x00468693, // addi a3,a3,0x10 # 8000040 38 | 0x34169073, // csrw mepc,a3 39 | 40 | 0x30251073, // csrw medeleg, a0 41 | 42 | 0x30200073, // mret 43 | 44 | // here: 45 | 0x0000006f, // j here # spin 46 | 0x0000006f, // # spin 47 | 0x0000006f, // # spin 48 | 0x0000006f, // # spin 49 | }; 50 | 51 | 52 | void init_isa() { 53 | // put initcode to QEMU to setup a PMP to permit access to all of memory in S mode 54 | bool ok = gdb_memcpy_to_qemu(0x80000000, initcode, sizeof(initcode)); 55 | assert(ok == 1); 56 | 57 | union isa_gdb_regs r; 58 | gdb_getregs(&r); 59 | 60 | r.pc = 0x80000000; 61 | ok = gdb_setregs(&r); 62 | assert(ok == 1); 63 | 64 | // execute enough instructions 65 | difftest_exec(20); 66 | } 67 | -------------------------------------------------------------------------------- /tools/qemu-socket-diff/src/isa/riscv64/init.c: -------------------------------------------------------------------------------- 1 | /*************************************************************************************** 2 | * Copyright (c) 2014-2021 Zihao Yu, Nanjing University 3 | * 4 | * NEMU is licensed under Mulan PSL v2. 5 | * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 | * You may obtain a copy of Mulan PSL v2 at: 7 | * http://license.coscl.org.cn/MulanPSL2 8 | * 9 | * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 10 | * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 11 | * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 12 | * 13 | * See the Mulan PSL v2 for more details. 14 | ***************************************************************************************/ 15 | 16 | void init_isa() { 17 | } 18 | --------------------------------------------------------------------------------