summaryrefslogtreecommitdiff
path: root/rts/linker/CacheFlush.c
blob: 206b2ef7ef2744a6e2980fdca0405d465d2f58f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Platform-dependent cache flushing logic */

#include "Rts.h"
#include "linker/CacheFlush.h"

#if defined(arm_HOST_ARCH)

void
ocFlushInstructionCache( ObjectCode *oc )
{
    int i;
    // Object code
    for (i=0; i < oc->n_sections; i++) {
        Section *s = &oc->sections[i];
        // This is a bit too broad but we don't have any way to determine what
        // is certainly code
        if (s->kind == SECTIONKIND_CODE_OR_RODATA)
            __clear_cache(s->start, (void*) ((uintptr_t) s->start + s->size));
    }

    // Jump islands
    // Note the (+1) to ensure that the last symbol extra is covered by the
    // flush.
    __clear_cache(oc->symbol_extras, &oc->symbol_extras[oc->n_symbol_extras+1]);
}

#elif defined(powerpc_HOST_ARCH)
/*
   ocFlushInstructionCache

   Flush the data & instruction caches.
   Because the PPC has split data/instruction caches, we have to
   do that whenever we modify code at runtime.
 */

static void
ocFlushInstructionCacheFrom(void* begin, size_t length)
{
    size_t         n = (length + 3) / 4;
    unsigned long* p = begin;

    while (n--)
    {
        __asm__ volatile ( "dcbf 0,%0\n\t"
                           "sync\n\t"
                           "icbi 0,%0"
                           :
                           : "r" (p)
                         );
        p++;
    }
    __asm__ volatile ( "sync\n\t"
                       "isync"
                     );
}

void
ocFlushInstructionCache( ObjectCode *oc )
{
    /* The main object code */
    ocFlushInstructionCacheFrom(oc->image + oc->misalignment, oc->fileSize);

    /* Jump Islands */
    ocFlushInstructionCacheFrom(oc->symbol_extras, sizeof(SymbolExtra) * oc->n_symbol_extras);
}

#else

void ocFlushInstructionCache( ObjectCode *oc STG_UNUSED ) {}

#endif /* powerpc_HOST_ARCH */