From 13bc0fc2f9f9d5e59922fd7c4960b0303316ccc1 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 4 Feb 2023 03:13:22 -0600 Subject: [PATCH] Initial --- .envrc | 1 + .gitignore | 5 + Cargo.lock | 390 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 10 ++ Makefile | 4 + flake.lock | 93 +++++++++++ flake.nix | 33 ++++ main.c | 5 + rustfmt.toml | 2 + src/emulator.rs | 102 ++++++++++++ src/instruction.rs | 8 + src/main.rs | 34 ++++ 12 files changed, 687 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 Makefile create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 main.c create mode 100644 rustfmt.toml create mode 100644 src/emulator.rs create mode 100644 src/instruction.rs create mode 100644 src/main.rs diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0673277 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/target +.direnv + +result* +*.o diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..5510e9d --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,390 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anyhow" +version = "1.0.68" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" + +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[package]] +name = "cc" +version = "1.0.79" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f13b9c79b5d1dd500d20ef541215a6423c75829ef43117e1b4d17fd8af0b5d76" +dependencies = [ + "bitflags", + "clap_derive", + "clap_lex", + "is-terminal", + "once_cell", + "strsim", + "termcolor", +] + +[[package]] +name = "clap_derive" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "684a277d672e91966334af371f1a7b5833f9aa00b07c84e92fbce95e00208ce8" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "783fe232adfca04f90f56201b26d79682d4cd2625e0bc7290b95123afe558ade" +dependencies = [ + "os_str_bytes", +] + +[[package]] +name = "errno" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f639046355ee4f37944e44f60642c6f3a7efa3cf6b78c78a0d989a8ce6c396a1" +dependencies = [ + "errno-dragonfly", + "libc", + "winapi", +] + +[[package]] +name = "errno-dragonfly" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +dependencies = [ + "cc", + "libc", +] + +[[package]] +name = "goblin" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "572564d6cba7d09775202c8e7eebc4d534d5ae36578ab402fb21e182a0ac9505" +dependencies = [ + "log", + "plain", + "scroll", +] + +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + +[[package]] +name = "hermit-abi" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "856b5cb0902c2b6d65d5fd97dfa30f9b70c7538e770b98eab5ed52d8db923e01" + +[[package]] +name = "io-lifetimes" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1abeb7a0dd0f8181267ff8adc397075586500b81b28a73e8a0208b00fc170fb3" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "is-terminal" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0a45d56fe973d6db23972bf5bc46f988a4a2385deac9cc29572f09daef" +dependencies = [ + "hermit-abi", + "io-lifetimes", + "rustix", + "windows-sys", +] + +[[package]] +name = "libc" +version = "0.2.139" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" + +[[package]] +name = "linux-raw-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "once_cell" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66" + +[[package]] +name = "os_str_bytes" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee" + +[[package]] +name = "plain" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4596b6d070b27117e987119b4dac604f3c58cfb0b191112e24771b2faeac1a6" + +[[package]] +name = "proc-macro-error" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869" +dependencies = [ + "proc-macro2", + "quote", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ef7d57beacfaf2d8aee5937dab7b7f28de3cb8b1828479bb5de2a7106f2bae2" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "riscv64-emulator" +version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "goblin", + "scroll", +] + +[[package]] +name = "rustix" +version = "0.36.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43abb88211988493c1abb44a70efa56ff0ce98f233b7b276146f1f3f7ba9644" +dependencies = [ + "bitflags", + "errno", + "io-lifetimes", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "scroll" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04c565b551bafbef4157586fa379538366e4385d42082f255bfd96e4fe8519da" +dependencies = [ + "scroll_derive", +] + +[[package]] +name = "scroll_derive" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdbda6ac5cd1321e724fa9cee216f3a61885889b896f073b8f82322789c5250e" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + +[[package]] +name = "syn" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "termcolor" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be55cf8942feac5c765c2c993422806843c9a9a45d4d5c407ad6dd2ea95eb9b6" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "unicode-ident" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-sys" +version = "0.45.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e2522491fbfcd58cc84d47aeb2958948c4b8982e9a2d8a2a35bbaed431390e7" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3887528ad530ba7bdbb1faa8275ec7a1155a45ffa57c37993960277145d640" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf4d1122317eddd6ff351aa852118a2418ad4214e6613a50e0191f7004372605" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.42.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..9ecf27e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "riscv64-emulator" +version = "0.1.0" +edition = "2021" + +[dependencies] +anyhow = "1.0.68" +clap = { version = "4.1.4", features = ["derive"] } +goblin = "0.6.0" +scroll = "0.11.0" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4ca9bb7 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CC := riscv64-unknown-linux-gnu-gcc + +main.o: main.c + $(CC) -o $@ $< diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..2dd3492 --- /dev/null +++ b/flake.lock @@ -0,0 +1,93 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1675405418, + "narHash": "sha256-5Tvd/8kFl+jQ+uYqam/Q1ZRWLNrkF97zRX/l0pUitpU=", + "owner": "nix-community", + "repo": "fenix", + "rev": "8d7682dba94f6a3ad9370d17a4092addcbb23544", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1667395993, + "narHash": "sha256-nuEHfE/LcWyuSWnS8t12N1wc105Qtau+/OdUAjtQ0rA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "5aed5285a952e0b949eb3ba02c12fa4fcfef535f", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1675183161, + "narHash": "sha256-Zq8sNgAxDckpn7tJo7V1afRSk2eoVbu3OjI1QklGLNg=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "e1e1b192c1a5aab2960bf0a0bd53a2e8124fa18e", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1667629849, + "narHash": "sha256-P+v+nDOFWicM4wziFK9S/ajF2lc0N2Rg9p6Y35uMoZI=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "3bacde6273b09a21a8ccfba15586fb165078fb62", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs_2" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1675345086, + "narHash": "sha256-Y7VnQlwTckCG4ia1zHSxF1mYKfNjfWUlzRyoLTbLn5U=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "04850a192cd82f94ddbd3f4b202d0e4ea3e62daa", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..6c85168 --- /dev/null +++ b/flake.nix @@ -0,0 +1,33 @@ +{ + inputs = { fenix.url = "github:nix-community/fenix"; }; + + outputs = { self, nixpkgs, flake-utils, fenix }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ fenix.overlays.default ]; + }; + + toolchain = pkgs.fenix.stable; + + targetPkgs = pkgs.pkgsCross.riscv64; + in rec { + legacyPackages = pkgs; + + devShell = pkgs.mkShell { + inputsFrom = [ ]; + + packages = (with pkgs; [ + cargo-watch + cargo-deny + cargo-edit + + targetPkgs.stdenv.cc + (qemu.override { + hostCpuTargets = [ "${targetPkgs.system}-user" ]; + }) + ]) ++ (with toolchain; [ cargo rustc rustfmt clippy ]); + }; + }); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..9b216a5 --- /dev/null +++ b/main.c @@ -0,0 +1,5 @@ +#include + +int main() { + printf("Hello, world!\n"); +} diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..4c1eefa --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,2 @@ +max_width = 80 +tab_spaces = 2 diff --git a/src/emulator.rs b/src/emulator.rs new file mode 100644 index 0000000..97766d9 --- /dev/null +++ b/src/emulator.rs @@ -0,0 +1,102 @@ +use std::collections::HashMap; + +use anyhow::Result; +use goblin::elf::Elf; +use scroll::{Endian, Pread}; + +// IMPORTANT PAGES IN THE RISC-V SPEC +// https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf +// +// - 103 (Ch. 19) - Instruction Set Listings (Opcodes) + +use crate::instruction::Instruction; + +pub type Address = u64; + +pub struct Emulator<'a> { + contents: &'a [u8], + elf: Elf<'a>, + endianness: Endian, + + program_counter: u64, + registers: [u64; 32], + memory_map: HashMap, +} + +impl<'a> Emulator<'a> { + pub fn new(contents: &'a [u8]) -> Result { + let elf = Elf::parse(&contents)?; + let endianness = elf.header.endianness()?; + Ok(Emulator { + contents, + elf, + endianness, + + program_counter: 0, + registers: [0; 32], + memory_map: HashMap::new(), + }) + } + + pub fn run(&mut self) -> Result<()> { + // Set up memory segments + for program_header in self.elf.program_headers.iter() { + let start = program_header.p_offset as usize; + let end = start + program_header.p_memsz as usize; + let memory_slice = &self.contents[start..end]; + self.memory_map.insert(program_header.p_vaddr, memory_slice); + } + + println!("Elf entrypoint: 0x{:x?}", self.elf.entry); + self.program_counter = self.elf.entry; + + self.step()?; + self.step()?; + + Ok(()) + } + + fn step(&mut self) -> Result<()> { + let instruction = self.decode_instruction_at(self.program_counter)?; + self.program_counter += instruction.len as u64; + Ok(()) + } + + fn decode_instruction_at(&self, addr: Address) -> Result { + let data = self.read_memory_at(addr, 2).unwrap(); + let data_num = data.pread_with::(0, self.endianness)?; + + // If the instruction has 11 at the end, it's longer + if data_num & 0b11 == 0b11 { + let data = self.read_memory_at(addr, 4).unwrap(); + let data_num = data.pread_with::(0, self.endianness)?; + println!("{:b}", data_num); + + // TODO: If the instruction ends with 11111, it's longer + + // JAL + if data_num & 0b1111111 == 0b1101111 { + return Ok(Instruction { len: 4 }); + } + } else { + println!("{:b}", data_num); + } + + todo!() + } + + /// Read n bytes from addr + // TODO: Could speed this up massively + fn read_memory_at(&self, addr: Address, n: usize) -> Option<&[u8]> { + for (memory_addr, memory_slice) in self.memory_map.iter() { + if addr >= *memory_addr && addr < memory_addr + memory_slice.len() as u64 + { + let start = (addr - *memory_addr) as usize; + let end = (start + n) as usize; + return Some(&memory_slice[start..end]); + } + } + + None + } +} diff --git a/src/instruction.rs b/src/instruction.rs new file mode 100644 index 0000000..73eb71b --- /dev/null +++ b/src/instruction.rs @@ -0,0 +1,8 @@ +#[derive(Debug)] +pub struct Instruction { + /// The number of bytes in this instruction + pub len: usize, +} + +#[derive(Debug)] +pub enum Op {} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..04c93ef --- /dev/null +++ b/src/main.rs @@ -0,0 +1,34 @@ +mod emulator; +mod instruction; + +use std::fs::File; +use std::io::Read; +use std::path::PathBuf; + +use anyhow::Result; +use clap::Parser; + +use crate::emulator::Emulator; + +#[derive(Debug, Parser)] +struct Opt { + input_path: PathBuf, +} + +fn main() -> Result<()> { + let opt = Opt::parse(); + + println!("Hello, world! {opt:?}"); + + let contents = { + let mut contents = Vec::new(); + let mut file = File::open(&opt.input_path)?; + file.read_to_end(&mut contents)?; + contents + }; + + let mut emulator = Emulator::new(&contents)?; + emulator.run()?; + + Ok(()) +}