Saturday, April 24, 2010

Saturday, April 17, 2010

LKML Summary Podcast – A semi-daily summary of LKML traffic

kernelpodcast.org provides a semi-daily summary of email traffic on the Linux Kernel Mailing List (LKML) in the form of a podcast and audio transcripts. It was started by Jon Masters as a means to force himself to keep up with the LKML.

Monday, April 05, 2010

Calling a C function from Assembly

Recently I had a requirement of having to call a C function from assembly (in the Linux kernel). One might wonder why someone would have to call a C function from assembly. Well… the reason is I wanted a dump of registers before I put the processor (OMAP) to sleep. The sleep code in Linux is written in assembly and I wanted the dump in a place just before the final call (to sleep) is made. Writing an assembly routine would be too difficult.

I could have taken a dump before the code jumped to assembly and then followed the assembly code to see what changes are being made to which register to arrive at the register values before the processor is put to sleep. In fact I did that in my initial investigation. I could have also stored the values of the registers in a place in memory and printed them once the code gets back to C. However, I wanted to find a way to call a C function from assembly because that seemed very challenging and moreover, why should we make life difficult (by writing code in assembly) when we have a high level language to solve our problems.

I am not going deep in to the details of what all I tried, but here is what I did finally.

ARM’s SWI (software interrupts) instructions are used to execute system calls. On execution of the SWI instruction, the control is transferred to the kernel and depending on the number (which is part of the SWI instruction), the kernel executes the appropriate SWI handler (a system call). I used this method to call a system call of my own, which I used to dump the registers. The SWI instruction is a simple assembly instruction (something like swi 0x00900142).

I know that implementing a new system call for such small things is not recommended, but this was mainly for debugging an issue. This is not going to be part of any distribution.

More details about implementing a system call can be found by Googling.