Pain-free HAL and ARMop calling from C

I’m here to plug a macro-based approach I developed for making HAL and ARMop calls from within the SMP module. It allows you to call HAL and ARMop routines as if they were native C functions, with minimal overhead, and minimal setup fuss.

Apart from a bit of macro soup, the only downside is that it doesn’t allow the calls to easily be made from assembler. But with some tweaks that could be supported as well.

You use macros to declare what calls you’re interested in, and their argument/return types. These are listed near the bottom of the header files. E.g. Cache_CleanInvalidateRange is the only ARMop I’m interested in, it accepts two void * parameters and returns a void result.

On startup you need to make an initialisation call (armops_init / halcalls_init), which will fetch the relevant routine addresses from the OS and fill in the variables that are necessary to allow the routines to be called.

The header has two modes of operation; including it normally just declares the “functions” so that they can be called. The second mode is used by the corresponding C file so that it can build up a list of routines it needs to look up in its initialisation call.

For ARMops, the routines are made available as standard C function pointers. C doesn’t require any special syntax to call a function pointer, so they can be invoked directly.

For the HAL calls, the macros construct inline functions which use inline assembler to perform the call. Most of the inline assembler is just boilerplate that Norcroft will be able to optimise out (moving parameters to the correct registers), so the cost shouldn’t be any higher than invoking a C function. (Note that the current version uses BLX, so will need tweaking it you want to use it on pre-ARMv5)