Author Topic: Cortex M4 overlay trickery sought  (Read 1889 times)

0 Members and 2 Guests are viewing this topic.

Offline laugensalmTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ch
Cortex M4 overlay trickery sought
« on: October 21, 2025, 10:12:52 am »
I have an existing set of OS/apps that is larger than the MCU RAM, therefore I've deployed some simple cache logic for a custom architecture performing overlay from a SPI flash. Now I'm looking at porting this construct into a Cortex M4 with MPU (Memory Protection Unit).
Code overlay, as long as position independent, seems to be no problem so far. So, in short words, the overlay code is allocated by the linker script at a 'virtual' address at 0x400000, for instance, the MPU is configured to protect this range against execution and the MemFault handler does the necessary loading.
Once there are repeated accesses to this address space, this becomes inefficient, as the MPU obviously won't internally translate the address (or cache the code page) and the exception handler will be unnecessarily entered.
This becomes painful in particular when data access to such a virtual segment occurs. Before going into the overhead of a dynamic linker implementation: Does someone have another idea?
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 6136
  • Country: gb
  • Doing electronics since the 1960s...
Re: Cortex M4 overlay trickery sought
« Reply #1 on: October 21, 2025, 10:52:50 am »
Check this out
https://www.eevblog.com/forum/microcontrollers/st-32f417-any-way-to-make-an-spi-sram-to-look-like-normal-ram/

There probably isn't an efficient way to do this, but you may not need a lot of speed in your application.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline laugensalmTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ch
Re: Cortex M4 overlay trickery sought
« Reply #2 on: October 21, 2025, 11:24:28 am »
Yeah, looks like this is the default way to go on a M4 with working MPU. What I am concerned about is unneccessarily triggering a MemFault exception. When using absolute addressing within this virtual segment, every access to the virtual segment will jump into the handler and do nothing except burning cycles, as long as the same code page is in memory (as determined by the 'software' cache).

Else, speed is not much of an issue here, unless it's organized badly. More or less the overlay is 'apps' that use components from the OS library that is in ROM. The other thing is larger data chunks in the SPI flash that would make sense being cached. Here I don't yet see an elegant solution yet that avoids the exception triggering overhead. I would be surprised if the Cortex M4 architecture did not implement such a simple hit&miss logic somewhere, but yes, I can also live with the -mpic options to use pc-relative adressing as far as possible.
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: Cortex M4 overlay trickery sought
« Reply #3 on: October 21, 2025, 12:49:00 pm »
Using the MPU would make more sense, if you could lift the MPU protection once the given segment is loaded. This would imply a possibility to remap the physical memory into virtual addresses, a simplistic version would simply use aliasing (given the vast address space this would be trivial to implement, by simply not decoding some of the address bits). Such remap/aliasing is beyond the processor itself, implemented in the bus fabric; in other words, it's not something specified with the processor. You did not tell us which mcu family do you intend to use, but I doubt any of those out there has implemented such feature for the internal memories (beyond simplistic remaps such as the FLASH being aliasable to 1 extra address space in the STM32). I also doubt you are in the position to have a chip custom-made to include such feature, else you wouldn't be asking here :-) . You can implement such thing on your own in external parallel memory, if given mcu has support for external static memories; but then that's extra circuitry and I strongly suspect you want to avoid that.

JW
 

Offline laugensalmTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ch
Re: Cortex M4 overlay trickery sought
« Reply #4 on: October 21, 2025, 01:46:24 pm »
The thing is that the address aliasing it not specified/documented, let aside being well-defined - apart from the implementation, the order of bus or DMA accesses could cause total 'random' behaviour. Plus, I don't actually know what the default setting without the MPU protection would do, if I'd get a Hardfault instead when MPU protection is lifted. But, if I get really bored, I might give it a try and report :-)

Actually, I am playing with existing hardware, unfortunately not based on a STM32 core (rather a sparsely documented chinese clone). If I was to choose, I would bake the previous (FPGA based) MCU setup onto the PCB.
 

Offline abyrvalg

  • Frequent Contributor
  • **
  • Posts: 898
  • Country: es
Re: Cortex M4 overlay trickery sought
« Reply #5 on: October 21, 2025, 02:01:12 pm »
What is the relationship between apps? Do they need to run in parallel/call each other? Or you load a single app, it does it's job (calling only ROM OS lib), terminates and you load another app?
For a "single app" case (assuming there is enough RAM to run a single app) you can just place it in RAM and statically link against ROM (GCC supports linking a *.sym file containing external symbols in a form memcpy = 0x00003710; dram_buf = 0x1C000000; ...)
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 6136
  • Country: gb
  • Doing electronics since the 1960s...
Re: Cortex M4 overlay trickery sought
« Reply #6 on: October 21, 2025, 03:11:47 pm »
Presumably the way to do this is to map the "virtual memory" device to some nonexistent address which, when accessed, will generate a trap. The trap code (see the thread I linked) then decodes the instruction which caused the trap and re-executes it in a way which returns the data from the virtual memory. That's for a read, and same for a write.

One would make it simpler by specifying that only a specific asm instruction could be used to access this VM.

Many years ago I did this to implement a /0 trap such that the processor would continue past it, rather than just crash. On the 32F4xx you can optionally ignore /0 errors but not all CPUs have that option.

No idea how it could work with DMA. If a DMA access causes a trap, how can you tell it was a DMA access?
« Last Edit: October 21, 2025, 04:21:31 pm by peter-h »
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: Cortex M4 overlay trickery sought
« Reply #7 on: October 21, 2025, 05:00:39 pm »
I don't actually know what the default setting without the MPU protection would do, if I'd get a Hardfault instead when MPU protection is lifted.
In Cortex-M4, HardFault is almost always an escalation from one of the three particular Faults (MemManage, Bus, Usage). It's almost universally experienced as the particular Faults are by default disabled thus escalate...

Attempt to access addresses which are not decoded by the system should lead to BusFaults.

JW
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: sk
Re: Cortex M4 overlay trickery sought
« Reply #8 on: October 21, 2025, 05:48:08 pm »
No idea how it could work with DMA. If a DMA access causes a trap, how can you tell it was a DMA access?
DMA won't cause any Fault, those are processor-specific. What happens beyond the processor is up to the "integrator", i.e. it's depends on the particular mcu/family. Specifically, the MPU is internal to the processor, so MPU settings won't affect DMA at all. Accessing undefined memory areas will probably in all sane incarnations lead to some signalling, e.g. in STM32 it will result in Transfer Error flag in the DMA being set (and given DMA channel/stream disabled); that then may result in an interrupt, if such is enabled. So, in your case, the DMA interrupt handler would need to resolve the Transfer Error case, look at the DMA memory pointer, and based on that do whatever is needed to perform the access to/from the physical memory from/to presumably some RAM cache.

JW
 

Offline Doctorandus_P

  • Super Contributor
  • ***
  • Posts: 5399
  • Country: nl
Re: Cortex M4 overlay trickery sought
« Reply #9 on: October 21, 2025, 05:49:43 pm »
The book below has a 30 page chapter about overlays.

Beginning STM32: Developing with FreeRTOS, libopencm3 and GCC
Warren Gay
St. Catharines, Ontario, Canada
ISBN-13 (pbk): 978-1-4842-3623-9     
ISBN-13 (electronic): 978-1-4842-3624-6

I quite like this book, but it is a bit dated. It handles only the (quite old) stm32f103 and libopemcm3 seemed to have never gained much traction, if you want cross platform compatibility, I guess CMSIS is the way to go, but Each uC family has different peripherals, so porting would take some effort.

From what I understand, overlays were originally intended to implement disk swapping in the era that (mostly) PC's were limited to 1MB of ram. And moving that data around is always going to be slow.

In is book Warran Gay also writes about an "Overlay Manager", which is (I think) a function that is called to load the overlay code into RAM before execution. He uses SPI, because the example uses stuff stored on an SPI Eeprom, but the function itself is just code and you could use DMA too. It would be nice if you could start loading the overlay into ram "a while before you need it", so it's already loaded when you need it, but I never looked into those details.

Sometimes I'm still dreaming about extending FreeRTOS into a generic purpose OS. Goal is to have a standard kernel, so you have: networking (with terminal emulator), storage with a file system and some other default / house keeping tasks, and then load and run several applications each as a single FreeRTOS task. But in reality It's a bit of a too big bite for me. I'm a bit stuck on "blinking led" projects for STM32 for about 10+ years now.

« Last Edit: October 21, 2025, 06:01:14 pm by Doctorandus_P »
 

Offline peter-h

  • Super Contributor
  • ***
  • Posts: 6136
  • Country: gb
  • Doing electronics since the 1960s...
Re: Cortex M4 overlay trickery sought
« Reply #10 on: October 22, 2025, 02:32:50 pm »
I think the bulk of the work in implementing the bus fault -> external (SPI?) RAM idea is that, if you want it to work with normal C, you need the bus fault handler to decode a whole range of possible instructions, because you will never know what the compiler will be using.

A much easier implementation would limit itself to specific instructions for reading and writing, but then it would not be code transparent. You would have to access the external memory via C functions which would contain inline asm.
Z80 Z180 Z280 Z8 S8 8031 8051 H8/300 H8/500 80x86 90S1200 32F417
 

Offline laugensalmTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ch
Re: Cortex M4 overlay trickery sought
« Reply #11 on: October 22, 2025, 08:40:01 pm »
DMA indeed bypasses any of the CPU logic - as it says, it's direct memory access, and typically glues directly to a dual port RAM block using its own bus. DMA isn't much of an issue here, although I use DMA to pull the data from the SPI flash into the cache block of the mapped SRAM for the overlay (on that custom design). But let's forget about DMA for now.

I think the bulk of the work in implementing the bus fault -> external (SPI?) RAM idea is that, if you want it to work with normal C, you need the bus fault handler to decode a whole range of possible instructions, because you will never know what the compiler will be using.

A much easier implementation would limit itself to specific instructions for reading and writing, but then it would not be code transparent. You would have to access the external memory via C functions which would contain inline asm.

The handler should not need to decode anything at all, that's already way more complex than a simple dynamic loader, I fear. Actually, I don't need 'dynamic' at all.
For a single function, it would just load the overlay in the exception handler, get the failure address from MMFAR, mask it for the physical address and jump, if I'm not mistaken (need to yet verify sane behaviour in the hardware simulator).

As far as I see, the MPU is lacking the feature to do a simple virtual/physical mapping, which in my caching logic is a simple and/or combinatorial, such that I don't need to do any compiler-specific trampoline stuff apart from the exception handler performing the data pulling from the SPI flash. All the rest of the app-linking and allocating in 'virtual' space is done by a linker file. I actually just realized I had this somewhat documented here: https://section5.ch/index.php/2015/09/28/virtual-rom-on-small-fpgas/

When not using the MPU, I assume that a BusFault is generated (provided the implementation specific bus logic covers that). So, this ends up just being a different exception handler, but the problem is the same, that an exception will be thrown on every subsequent absolute addressing within the virtual space, just lacking the fine grained control via the MPU.

One safe way out of the exception overhead situation would be a tool parsing the ELF and calculating the proper VMA/LMA offsets for the overlay 'app' like objcopy does, then no pc-relative or absolute confusion would get into the way. Haven't got a real concept yet, but this is likely the next thing I'd try. Then I just have to make sure none of the 'virtual' addresses ends up in a pointer elsewhere...
 
The following users thanked this post: pardo-bsso

Offline laugensalmTopic starter

  • Regular Contributor
  • *
  • Posts: 170
  • Country: ch
Re: Cortex M4 overlay trickery sought
« Reply #12 on: October 25, 2025, 01:31:49 pm »
Here's a bit of an update: So, the actual MPU mechanisms work sane for swapping out the virtual pages from SPI flash, as so far verified in the hardware simulator. For instruction fetch violations, the frame address can be taken from the stack, for data access violations via MMFAR. So far so good. The ugly part turns out to be the relocating, where a simple 'OVERLAY' segment in the linker script won't do. Since all is built as a static big .elf file with the specific virtual pages containing the overlay code, all inter-references between 'system' (ROM/startup code) and 'app' code (overlay) are also taken care of by the linker, with the special property of forcing a long call from 'system' to 'app' in order to trigger the dynamic loading from flash. In order to avoid the unnecessary triggering of the overlay handler, I'm using `CFLAGS+=-fPIC -msingle-pic-base` in order to be able to manipulate the GOT with the offsets `virtual versus physical`.
I've found some more code samples on dynamic linking and overlay app notes (which require explicit page loading, though), so far it appears this is the leanest method, unless I run into another scenario.
So the problem boils down eventually to a fine grained control over:
- What  address the code is emitted for by the compiler ('inter-app sanity')
- What virtual address the 'app' function is entry-called by in order to trigger overlay

Then the general question is whether such behaviour should be handled at compiler or at linker level (let aside the frustration that the ARM designers did not count in a simple address mapping scheme). In both domains, there seems to be lack of functionality in terms of gcc attributes to create either user defined trampolines (such as veneers) or from the ld side to apply a virtual calling address for a given function, based on what memory segment the caller is in. Am I correct, did I miss something?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->