|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Emulated memory map implementation (see emu_memmap.h). More...
#include "emu_memmap.h"#include <errno.h>#include <sys/mman.h>#include "emu_console.h"#include "emu_host_io_internal.h"#include "emu_mmio.h"Go to the source code of this file.
Data Structures | |
| struct | aliased_window_t |
| One guest window that resolves onto a shared host aperture. More... | |
Macros | |
| #define | MAP_ANONYMOUS MAP_ANON |
| Portable spelling of the anonymous-mapping flag. | |
Enumerations | |
| enum | ospi_xip_map_t : uint64_t { k_ospi_xip_base = 0x80000000UL , k_ospi_ns_base = 0x90000000UL } |
| enum | tsn_cal_seed_t : uint32_t { k_tsn_cal_addr = 0x02C1EDA0U , k_tsn_cal_tscdr = 3000U , k_tsn_cal_tscdr2 = 1000U } |
| enum | backing_index_t : size_t { k_backing_sram = 0U , k_backing_sdram = 1U , k_backing_ospi = 2U } |
| Identity of each shared aperture within a workspace. More... | |
| enum | backing_geometry_t : uint64_t { k_sram_size = 0x001D4000UL , k_sdram_size = 0x04000000UL , k_ospi_size = 0x04000000UL , k_ns_sram2_offset = 0x00100000UL , k_ns_sram2_size = 0x00080000UL , k_logical_bytes = k_sram_size + k_sdram_size + k_ospi_size } |
| Exact logical extents of the three shared apertures. More... | |
| enum | mmap_argument_t : int { k_mmap_no_fd = -1 , k_mmap_no_offset = 0 } |
| Fixed arguments for an anonymous host aperture mapping. More... | |
Functions | |
| static RA8_INTERNAL emu_memmap_result_t | internal_result (emu_memmap_status_t status, int os_error) |
| Construct one exact lifecycle result. | |
| static RA8_INTERNAL uint8_t * | internal_window_host (const emu_memmap_workspace_t *workspace, uint64_t base, uint64_t size) |
| Resolve a guest region base to its shared host aperture address. | |
| static RA8_INTERNAL bool | internal_backing_open (uint64_t size, emu_memmap_backing_t *backing) |
| Acquire one zero-filled, lazily-committed host aperture mapping. | |
| static RA8_INTERNAL void | internal_backings_close (emu_memmap_workspace_t *workspace) |
| Release every acquired aperture mapping in a workspace. | |
| static RA8_INTERNAL void | internal_seed_tsn (uc_engine *uc) |
| Seed deterministic factory TSN calibration words. | |
| static RA8_INTERNAL bool | internal_map_periph_mmio (uc_engine *uc) |
| Map the Secure peripheral window and its IDAU bit[28] NS alias. | |
| static RA8_INTERNAL bool | internal_map_regions (const emu_memmap_workspace_t *workspace, uc_engine *uc) |
| Map every guest region into one engine, sharing the apertures. | |
| emu_memmap_result_t | emu_memmap_requirements (void) |
| Report the immutable aliased-aperture backing geometry. | |
| emu_memmap_result_t | emu_memmap_open (emu_memmap_workspace_t *workspace) |
| Create three independent, lazily-committed host aperture mappings. | |
| emu_memmap_result_t | emu_memmap_attach (emu_memmap_workspace_t *workspace, uc_engine *uc) |
| Map one Unicorn engine onto the shared aperture backing. | |
| bool | emu_memmap_detach (emu_memmap_workspace_t *workspace, uc_engine *uc) |
| Remove one engine binding before closing that Unicorn engine. | |
| bool | emu_memmap_close (emu_memmap_workspace_t *workspace) |
| Release every host aperture mapping after all engines detached. | |
| const mem_region_t * | emu_memmap_regions (uint32_t *count) |
| Implementation of emu_memmap_regions() – static table access. | |
| uint64_t | emu_memmap_mram_base (void) |
| Implementation of emu_memmap_mram_base() – named MRAM lookup. | |
Variables | |
| static const mem_region_t | s_regions [] |
Emulated memory map implementation (see emu_memmap.h).
Keeps SRAM, SDRAM and OSPI bytes in three workspace-owned host mappings. Every attached engine binds the Secure aperture and its IDAU bit[28] Non-secure alias onto the SAME host pages with uc_mem_map_ptr, so Secure/Non-secure coherence and cpu0/cpu1 coherence are structural: a guest store stays on the translator's fast path and costs nothing beyond the store itself. All other ordinary regions remain private Unicorn mappings.
Definition in file emu_memmap.c.
| #define MAP_ANONYMOUS MAP_ANON |
Portable spelling of the anonymous-mapping flag.
Build-configuration alias only: macOS SDKs that predate the MAP_ANONYMOUS spelling supply the same flag as MAP_ANON. Defined only when the platform header did not, so a host that has the standard name keeps it.
Definition at line 37 of file emu_memmap.c.
Referenced by internal_backing_open().
| enum backing_geometry_t : uint64_t |
Exact logical extents of the three shared apertures.
Definition at line 169 of file emu_memmap.c.
| enum backing_index_t : size_t |
Identity of each shared aperture within a workspace.
| Enumerator | |
|---|---|
| k_backing_sram | On-chip SRAM aperture index. |
| k_backing_sdram | External SDRAM aperture index. |
| k_backing_ospi | OSPI XIP aperture index. |
Definition at line 162 of file emu_memmap.c.
| enum mmap_argument_t : int |
Fixed arguments for an anonymous host aperture mapping.
| Enumerator | |
|---|---|
| k_mmap_no_fd | Anonymous mappings are backed by no descriptor. |
| k_mmap_no_offset | Anonymous mappings start at offset zero. |
Definition at line 180 of file emu_memmap.c.
| enum ospi_xip_map_t : uint64_t |
| Enumerator | |
|---|---|
| k_ospi_xip_base | OSPI XIP window: Secure physical base. |
| k_ospi_ns_base | OSPI XIP window: NS alias (IDAU bit[28]=1). |
Definition at line 140 of file emu_memmap.c.
| enum tsn_cal_seed_t : uint32_t |
| Enumerator | |
|---|---|
| k_tsn_cal_addr | TSCDR (+0x00), TSCDR2 (+0x04). |
| k_tsn_cal_tscdr | 12-bit calibration code at +125 degC. |
| k_tsn_cal_tscdr2 | 12-bit calibration code at -40 degC. |
Definition at line 155 of file emu_memmap.c.
|
nodiscard |
Map one Unicorn engine onto the shared aperture backing.
Maps every RA8D2 region into uc. The six aliased windows – the SRAM/SRAM2, SDRAM and OSPI Secure views and their IDAU bit[28] Non-secure aliases – are bound with uc_mem_map_ptr onto the workspace's host pages, so a store through any of them is immediately visible through every other one and in every other engine bound to the same workspace, at no per-store cost. Every other region is an ordinary private Unicorn mapping.
| [in,out] | workspace | Open aperture backing. |
| [in,out] | uc | Fresh Unicorn engine. |
| k_emu_memmap_ok | The engine is mapped and the binding recorded. |
| k_emu_memmap_invalid | A null or unopened argument was supplied. |
| k_emu_memmap_unicorn | A region map or the MMIO window install failed. |
uc has no guest memory mappings. workspace is open and has a free binding slot. uc and must detach before closing it. Definition at line 463 of file emu_memmap.c.
References emu_memmap_binding_t::active, emu_memmap_workspace::bindings, internal_map_periph_mmio(), internal_map_regions(), internal_result(), internal_seed_tsn(), k_emu_memmap_binding_count, k_emu_memmap_invalid, k_emu_memmap_ok, k_emu_memmap_unicorn, and emu_memmap_workspace::open.
Referenced by internal_cpu1_engine_init(), and internal_main_open_engine().
| bool emu_memmap_close | ( | emu_memmap_workspace_t * | workspace | ) |
Release every host aperture mapping after all engines detached.
Refuses while any binding is active, because a bound engine holds uc_mem_map_ptr references into the very pages this call unmaps.
| [in,out] | workspace | Open or already-closed caller workspace. |
| true | Every aperture mapping was released, or none was owned. |
| false | A binding is still active and nothing was released. |
workspace is non-null. workspace was detached first. Definition at line 500 of file emu_memmap.c.
References emu_memmap_binding_t::active, emu_memmap_workspace::bindings, internal_backings_close(), k_emu_memmap_binding_count, and emu_memmap_workspace::open.
Referenced by internal_main_load_images(), main(), and priv_run_cleanup().
| bool emu_memmap_detach | ( | emu_memmap_workspace_t * | workspace, |
| uc_engine * | uc ) |
Remove one engine binding before closing that Unicorn engine.
Releases the binding slot uc occupies. The engine keeps its mappings until the caller closes it, so detach must happen before emu_memmap_close unmaps the host pages those mappings point at.
| [in,out] | workspace | Open backing containing the binding. |
| [in,out] | uc | Live bound engine. |
| true | The binding for uc was found and released. |
| false | A null argument, or uc was not bound. |
workspace and uc are non-null. uc. Definition at line 486 of file emu_memmap.c.
References emu_memmap_binding_t::active, emu_memmap_workspace::bindings, k_emu_memmap_binding_count, and emu_memmap_binding_t::uc.
Referenced by emu_cpu1_close(), internal_cpu1_engine_init(), internal_main_load_images(), and priv_run_cleanup().
| uint64_t emu_memmap_mram_base | ( | void | ) |
Implementation of emu_memmap_mram_base() – named MRAM lookup.
Return the MRAM boot-vector base.
Definition at line 523 of file emu_memmap.c.
References s_regions.
Referenced by internal_main_reset_vector(), internal_main_run_loaded(), internal_on_intr(), and warm_reboot().
|
nodiscard |
Create three independent, lazily-committed host aperture mappings.
Acquires one page-aligned zero-filled host mapping per shared aperture (SRAM, SDRAM, OSPI). The mappings are anonymous, so the 130 MiB of logical guest memory costs resident memory only for the pages the firmware actually touches. Distinct workspaces own distinct mappings and never share bytes.
| [out] | workspace | Receives the owned mappings on success. |
| k_emu_memmap_ok | Every aperture mapping was acquired. |
| k_emu_memmap_invalid | workspace was null or already open. |
| k_emu_memmap_backing | A host mapping failed; os_error holds errno. |
workspace is non-null. workspace is not already open; re-opening one is rejected rather than leaking the apertures it already owns. workspace untouched. Definition at line 439 of file emu_memmap.c.
References emu_memmap_workspace::backings, internal_backing_open(), internal_backings_close(), internal_result(), k_backing_ospi, k_backing_sdram, k_backing_sram, k_emu_memmap_backing, k_emu_memmap_backing_count, k_emu_memmap_invalid, k_emu_memmap_ok, k_ospi_size, k_sdram_size, k_sram_size, and emu_memmap_workspace::open.
Referenced by internal_main_open_memory().
| const mem_region_t * emu_memmap_regions | ( | uint32_t * | count | ) |
Implementation of emu_memmap_regions() – static table access.
Return the static emulated memory-region table.
Definition at line 516 of file emu_memmap.c.
References s_regions.
|
nodiscard |
Report the immutable aliased-aperture backing geometry.
Names the exact number of logical guest bytes the three shared apertures span, before any workspace or engine exists, so a caller can report the emulated memory footprint without opening anything.
| value | A result whose status is always k_emu_memmap_ok. |
Definition at line 434 of file emu_memmap.c.
References internal_result(), and k_emu_memmap_ok.
|
static |
Acquire one zero-filled, lazily-committed host aperture mapping.
Uses an anonymous mapping rather than an allocator: the pages are page-aligned (which uc_mem_map_ptr requires), start zeroed, and cost resident memory only once the firmware touches them, so mapping 130 MiB of guest memory does not cost 130 MiB of host memory.
| [in] | size | Exact aperture byte length. |
| [out] | backing | Receives the acquired mapping. |
| true | backing holds a live page-aligned host mapping. |
| false | The host refused the mapping; errno describes why. |
backing is non-null. size is a non-zero multiple of k_page_size. backing untouched. Definition at line 289 of file emu_memmap.c.
References k_mmap_no_fd, k_mmap_no_offset, MAP_ANONYMOUS, and RA8_INTERNAL.
Referenced by emu_memmap_open().
|
static |
Release every acquired aperture mapping in a workspace.
Unmaps each live aperture and clears its record, so a partially acquired workspace and a fully closed one are released by the same code.
| [in,out] | workspace | Caller-owned workspace whose apertures are released. |
workspace is non-null. Definition at line 316 of file emu_memmap.c.
References emu_memmap_workspace::backings, emu_memmap_backing_t::host, k_emu_memmap_backing_count, RA8_INTERNAL, and emu_memmap_backing_t::size.
Referenced by emu_memmap_close(), and emu_memmap_open().
|
static |
Map the Secure peripheral window and its IDAU bit[28] NS alias.
Installs the modelled peripheral callbacks over 0x40000000 and over the Non-secure alias at 0x50000000, so an NS image reaches the same models.
| [in,out] | uc | Unicorn engine receiving the MMIO windows. |
| true | Both the Secure and Non-secure peripheral windows are live. |
| false | Unicorn refused a window; a diagnostic was written to stderr. |
uc is a fresh engine with no peripheral window installed. Definition at line 363 of file emu_memmap.c.
References k_ns_alias_bit, k_periph_base, k_periph_size, mmio_read(), mmio_write(), priv_emu_io_errf(), and RA8_INTERNAL.
Referenced by emu_memmap_attach().
|
static |
Map every guest region into one engine, sharing the apertures.
Each region either resolves to a shared host aperture – and is bound with uc_mem_map_ptr so it and its alias are one state – or becomes an ordinary private Unicorn mapping. Because the shared pages already hold the workspace's bytes, a freshly attached engine observes everything written before it existed with no replay step.
| [in] | workspace | Open workspace owning the aperture mappings. |
| [in,out] | uc | Unicorn engine receiving the regions. |
| true | The engine carries the complete RA8D2 region map. |
| false | A region map failed; a diagnostic was written to stderr. |
workspace is open with every aperture acquired. uc has no guest memory mappings. Definition at line 415 of file emu_memmap.c.
References mem_region_t::base, internal_window_host(), mem_region_t::name, priv_emu_io_errf(), RA8_INTERNAL, s_regions, and mem_region_t::size.
Referenced by emu_memmap_attach().
|
static |
Construct one exact lifecycle result.
Construct one exact lifecycle result; every lifecycle entry point reports the same immutable aperture geometry alongside its status so a caller never has to correlate two calls.
| [in] | status | Status value published by the operation. |
| [in] | os_error | Host error code captured on failure, or zero. |
| value | The operation-specific result value. |
Definition at line 218 of file emu_memmap.c.
References k_logical_bytes, and RA8_INTERNAL.
Referenced by emu_memmap_attach(), emu_memmap_open(), and emu_memmap_requirements().
|
static |
Seed deterministic factory TSN calibration words.
Writes the two-point trim pair into the engine's TRIM page (see tsn_cal_seed_t). The page is ordinary private Unicorn memory, so the seed is applied per engine at attach time.
| [in,out] | uc | Unicorn engine whose TRIM page receives the seed. |
uc. uc holds TSCDR at k_tsn_cal_addr and TSCDR2 at the next word. Definition at line 340 of file emu_memmap.c.
References emu_mem_write(), k_tsn_cal_addr, k_tsn_cal_tscdr, k_tsn_cal_tscdr2, and RA8_INTERNAL.
Referenced by emu_memmap_attach().
|
static |
Resolve a guest region base to its shared host aperture address.
Looks the region up in the aliased-window table and returns the host address the window starts at, after proving the whole window fits inside the aperture. Any region that is not an aliased window returns nullptr and is mapped as ordinary private Unicorn memory.
| [in] | workspace | Open workspace owning the aperture mappings. |
| [in] | base | Guest base address of the region being mapped. |
| [in] | size | Guest byte length of the region being mapped. |
| nullptr | The region is not shared, or it would overrun its aperture. |
workspace is open and every aperture mapping is acquired. Every guest window bound to shared host pages, Secure and NS alike.
Definition at line 244 of file emu_memmap.c.
References aliased_window_t::backing, emu_memmap_workspace::backings, aliased_window_t::base, emu_memmap_backing_t::host, k_backing_ospi, k_backing_sdram, k_backing_sram, k_ns_sdram_base, k_ns_sram2_base, k_ns_sram2_offset, k_ospi_ns_base, k_ospi_xip_base, k_sdram_base, k_sram_base, aliased_window_t::offset, and emu_memmap_backing_t::size.
Referenced by internal_map_regions().
|
static |
Definition at line 41 of file emu_memmap.c.