| // Copyright 2023 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #ifndef MPACT_RISCV_RISCV_RISCV32_HTIF_SEMIHOST_H_ |
| #define MPACT_RISCV_RISCV_RISCV32_HTIF_SEMIHOST_H_ |
| |
| #include <cstdint> |
| #include <string> |
| |
| #include "absl/container/flat_hash_map.h" |
| #include "absl/functional/any_invocable.h" |
| #include "mpact/sim/generic/data_buffer.h" |
| #include "mpact/sim/util/memory/memory_interface.h" |
| #include "mpact/sim/util/memory/memory_watcher.h" |
| |
| // This class implements semi-hosting for code generated by the RiscV tools |
| // in third_party/chipyard. Instead of using breakpoint or sys call instructions |
| // the tools assume memory watchpoints. |
| |
| namespace mpact { |
| namespace sim { |
| namespace riscv { |
| |
| class RiscV32HtifSemiHost { |
| public: |
| using HaltCallback = absl::AnyInvocable<void()>; |
| using ErrorCallback = absl::AnyInvocable<void(std::string)>; |
| |
| // A struct for the set of addresses used in this semi-hosting. |
| struct SemiHostAddresses { |
| uint64_t tohost_ready; |
| uint64_t tohost; |
| uint64_t fromhost_ready; |
| uint64_t fromhost; |
| }; |
| |
| RiscV32HtifSemiHost(util::MemoryWatcher *watcher, |
| util::MemoryInterface *memory, |
| const SemiHostAddresses &magic_addresses); |
| |
| RiscV32HtifSemiHost(util::MemoryWatcher *watcher, |
| util::MemoryInterface *memory, |
| const SemiHostAddresses &magic_addresses, |
| HaltCallback halt_callback, ErrorCallback error_callback); |
| |
| ~RiscV32HtifSemiHost(); |
| |
| // Set magic addresses. |
| void SetMagicAddresses(const SemiHostAddresses &magic_addresses); |
| // Set the halt callback. |
| void SetHaltCallback(HaltCallback halt_callback); |
| // Set the error callback. |
| void SetErrorCallback(ErrorCallback error_callback); |
| // Callback function used by memory watchpoint class. |
| void StoreEvent(uint64_t address, int size); |
| |
| private: |
| // A local data buffer factory. |
| generic::DataBufferFactory db_factory_; |
| HaltCallback halt_callback_; |
| ErrorCallback error_callback_; |
| // Memory watcher. |
| util::MemoryWatcher *watcher_; |
| // Memory interface (bypassing the watchpoints). |
| util::MemoryInterface *memory_; |
| SemiHostAddresses magic_addresses_; |
| // File descriptor map. |
| absl::flat_hash_map<int, int> fd_map_; |
| }; |
| |
| } // namespace riscv |
| } // namespace sim |
| } // namespace mpact |
| |
| #endif // MPACT_RISCV_RISCV_RISCV32_HTIF_SEMIHOST_H_ |