0

I am using gcc's extended asm to invoke a system call. I am working on a proprietary RTOS on a PowerPC (Freescale MPC5200B).

According to gcc's documentation I should add all registers that the assembly code uses - and that are neither input nor output - to the clobbers list, because gcc does not analyse the assembly code and thus does not know which registers are being altered.

The problem is that I don't know which registers the system call alters. In fact I'm observing a case where the system call alters a register holding a pointer. After the system call has returned, the pointer in the register is being used, which leads to an invalid memory access.

How should I deal with this situation?

Jan Schatz
  • 334
  • 2
  • 12
  • 1
    The syscall instruction itself modifies _RCX_ and _R11_ : http://www.felixcloutier.com/x86/SYSCALL.html . However Linux will also changes _RAX_ with the result of the operation. The latter register is documented in the [System V 64-Bit ABI](https://web.archive.org/web/20160706074221/http://www.x86-64.org/documentation/abi.pdf) in section A.2.1 _Calling Convention_ which states: _2. A system-call is done via the syscall instruction. The kernel destroys registers %rcx and %r11._ – Michael Petch Sep 07 '16 at 12:40
  • And _5. Returning from the syscall, register %rax contains the result of the system-call. A value in the range between -4095 and -1 indicates an error, it is -errno._ – Michael Petch Sep 07 '16 at 12:44
  • *How should I deal with this situation?* declare clobbers on rcx and r11, and use `"=a"(retval)` so gcc knows the result is in rax. BTW, the [x86 tag wiki](http://stackoverflow.com/tags/x86/info) has links to lots of good stuff, including the dup-target that contains the answer to your question about which regs are destroyed by system calls. (I'm not saying you should have found it on your own; this is one of those cases where the right search terms are hard to come up with if you don't already know.) – Peter Cordes Sep 07 '16 at 12:46
  • I am neither on Linux nor on x86. I am working on a proprietary RTOS on a PowerPC. But I think your comments have pointed me to the right direction. – Jan Schatz Sep 07 '16 at 13:35
  • @PeterCordes : Given the fresh information in the comments, this is no longer a duplicate. I at least jumped to the wrong conclusion. – Michael Petch Sep 07 '16 at 14:38
  • The proprietary RTOS you are using will probably have some kind of ABI that details what you need to know. They might even conform to one of the Power ABIs. Unless we knew what RTOS it is probably a bit difficult to answer without generalizations. – Michael Petch Sep 07 '16 at 15:09
  • 1
    Future readers: This was temporarily closed as a dup of http://stackoverflow.com/questions/2535989/what-are-the-calling-conventions-for-unix-linux-system-calls-on-x86-64, which explains which registers are preserved across system calls in the x86-64 and x86-32 System V ABIs (namely: almost all of them). I agree with @MichaelPetch that this is no longer a dup. The question deserved to be closed until it was clarified what OS and architecture it was, though! – Peter Cordes Sep 07 '16 at 16:31

1 Answers1

1

For future readers:

The general answer is that you can find the registers altered by a system call in the documentation of the system's ABI.

For my system (Freescale MPC5200B) I found the answer is the IBM Application Note 'Developing PowerPC Embedded Application Binary Interface (EABI) Compliant Programs'.

So I added the registers marked volatile (namely R3..R12, F0..F13 and the flags register) to the clobbers list.

Jan Schatz
  • 334
  • 2
  • 12