Search code examples
cgccmemory-alignmentpowerpc

creating a C function with a given size in the text segment


I'm programming an embedded 32 system with a 32 kbyte 8-way set associative L2 instruction cache. To avoid cache thrashing we align functions in a way such that the text of a set of functions called at a high frequency (think interrupt code) ends up in separate cache sets. We do this by inserting dummy functions as needed, e.g.

void high_freq1(void)
{
   ...
}

void dummy(void)
{
   __asm__(/* Silly opcodes to fill ~100 to ~1000 bytes of text segment */);
}

void high_freq2(void)
{
   ...
}

This strikes me as ugly and suboptimal. What I'd like to do is

  • avoid __asm__ entirely and use pure C89 (maybe C99)
  • find a way to create the needed dummy() spacer that the GCC optimizer does not touch
  • the size of the dummy() spacer should be configurable as a multiple of 4 bytes. Typical spacers are 260 to 1000 bytes.
  • should be feasible for a set of about 50 functions out of a total of 500 functions

I'm also willing to explore entirely new techniques of placing a set of selected functions in a way so they aren't mapped to the same cache lines. Can a linker script do this?


Solution

  • Maybe linker scripts are the way to go. The GNU linker can use these I think... I've used LD files for the AVR and on MQX both of which we using GCC based compilers... might help...

    You can define your memory sections etc and what goes where... Each time I come to write one its been so long since the last I have to read up again...

    Have a search for SVR3-style command files to gem up.

    DISCLAIMER: Following example for a very specific compiler... but the SVR3-like format is pretty general... you'll have to read up for your system

    For example you can use commands like...

    ApplicationStart = 0x...;
    MemoryBlockSize = 0x...;
    ApplicationDataSize  = 0x...;
    ApplicationLength    = MemoryBlockSize - ApplicationDataSize;
    
    MEMORY {
        RAM: ORIGIN = 0x...                LENGTH = 1M
        ROM: ORIGIN = ApplicationStart     LENGTH = ApplicationLength   
    }
    

    This defines three memory sections for the linker. Then you can say things like

    SECTIONS
    {
        GROUP :
        {       
            .text :
            {
                * (.text)
                * (.init , '.init$*')
                * (.fini , '.fini$*')
            }
    
            .my_special_text ALIGN(32): 
            {
                * (.my_special_text)
            } 
    
            .initdat ALIGN(4):
            // Blah blah
        } > ROM
        // SNIP
    }
    

    The SECTIONS command tells the linker how to map input sections into output sections, and how to place the output sections in memory... Here we're saying what is going into the ROM output section, which we defined in the MEMORY definition above. The bit possible of interest to you is .my_special_text. In your code you can then do things like...

    __attribute__ ((section(".my_special_text")))
    void MySpecialFunction(...)
    {
        ....
    }
    

    The linker will put any function preceded by the __attribute__ statement into the my_special_text section. In the above example this is placed into ROM on the next 4 byte aligned boundary after the text section, but you can put it anyway you like. So you could make a few sections, one for each of the functions you describe, and make sure the addresses won't cause clashes...

    You can the size and memory location of the section using linker defined variables of the form

    extern char_fsection_name[]; // Set to the address of the start of section_name
    extern char_esection_name[]; // Set to the first byte following section_name
    

    So for this example...

    extern char _fmy_special_text[]; // Set to the address of the start of section_name
    extern char _emy_special_text[]; // Set to the first byte following section_name