0

How can I use mmap to get the start and end address of a function? I want to execute the function and then call __clear_cache. To clear everything from the cache that has just been executed. clear_cache requires the start and end address.

One function in my code represents a test case, I need to clear the cache in order to benchmark correctly. I am using Linux 3.7 and C.

I need to use mmap and not malloc as said here:

How clear and invalidate ARM v7 processor cache from User Mode on Linux 2.6.35

Currently I have just made an mmap that is 32kb in size, the size of my cache. But how to get the start and end address and map it to a particular function?

I have done this

//In the header

extern void __clear_cache (char*, char*);

//Function pointer to get address. typedef void (*_func_pointer) (void);

in the .c

_func_pointer = test_func;

uint32_t *  map_to_function = mmap(
        NULL,
        32768,  // 32kb -- Whole Cache.
        PROT_READ | PROT_WRITE | PROT_EXEC,
        MAP_PRIVATE | MAP_ANONYMOUS,
        -1,
        0);
if (map_to_function == MAP_FAILED) {
    printf("Could not mmap a memory buffer with the proper permissions.\n");
    return -1;
}


test_func = (_func_pointer ) map_to_function ;


//Run Test case 1:

 Some how run function 1 and get start and end address.

__clear_cache((char*)start_address, (char*)end_address);

Thanks for any help

Community
  • 1
  • 1
user1876942
  • 1,411
  • 2
  • 20
  • 32
  • Is function_1 an actual C function? Or is it a set of emitted OPCODES? Bc. I'm thinking you might be confusing what the question at the link you provided is actually discussing. They are talking about generating code, like in a JIT, which is stored in memory provided by mmap. If your function was written in C and compiled into your program, there is no way with standard C to get the end address or size of the function in bytes. – Benjamin Maurer Feb 24 '14 at 12:21
  • So, I can only use __clear_cache by coping that way in the link? Is there an easier way to call __clear_cache, I know my function, just want to clear the cache. Yes, I am just using normal C code and an actual C function. – user1876942 Feb 24 '14 at 12:29
  • Well there is no standard way. There might be some hacks around that. Maybe you could describe why you even need to do that. Maybe there is a better way to achieve that. Even the doc. of the functions hints, that it is meant for self-modifying code or code generators: http://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html – Benjamin Maurer Feb 24 '14 at 12:32
  • My end goal is just to call __clear_cache(). To do this I need to tell it my start and end address. So, after every function how do I know what to clear? I could just clear the whole cache, is that is somehow easier. I need to clear the cache for benchmarking. – user1876942 Feb 24 '14 at 12:39
  • That is the only function that allows clearing the cache from user side code. – user1876942 Feb 24 '14 at 12:40

1 Answers1

0

As described in the comments to your question, mmap is only necessary for self modifying code or code generation.

The start of your function is obviously just the function pointer. But there is no standard C way to get the length of a function or a pointer to its end.

One thing you could try, is to use a GCC extension, that gives you the address of a label, the unary '&&' operator. So you could modify your function to have a label at the end:

...
    do_stuff();
MYFUNC_END:
    return;
}

Not sure if you can put a label after the last statement, just before the closing curly braces. I don't think so.

Anyway, then you can just do:

__clear_cache((char *)myfunc, (char *) &&MYFUNC_END);
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49
  • OK, thanks a lot for the help. I guess it maybe just easier to delete the important data. So, if I have a double array A. I can clear that like this: __clear_cache((char*)A, (char*)&A[N][N]); – user1876942 Feb 24 '14 at 13:06
  • Are you even using an ARM CPU? I did some research, and I think there is no implementation for x86. So it would do nothing on such a CPU. – Benjamin Maurer Feb 24 '14 at 13:20
  • Yes, sorry forgot to say. I am using the ARM Cortex A9. – user1876942 Feb 24 '14 at 13:22