2 * Function calling ABI conversion from Linux to EFI for x86_64
4 * Copyright (C) 2007 Intel Corp
5 * Bibo Mao <bibo.mao@intel.com>
6 * Huang Ying <ying.huang@intel.com>
7 * Copyright (C) 2012 Felipe Contreras <felipe.contreras@gmail.com>
10 #if !defined(HAVE_USE_MS_ABI)
12 * EFI calling conventions are documented at:
13 * http://msdn.microsoft.com/en-us/library/ms235286%28v=vs.80%29.aspx
14 * ELF calling conventions are documented at:
15 * http://www.x86-64.org/documentation/abi.pdf
17 * Basically here are the conversion rules:
18 * a) our function pointer is in %rdi
19 * b) rsi through r8 (elf) aka rcx through r9 (ms) require stack space
20 * on the MS side even though it's not getting used at all.
21 * c) 8(%rsp) is always aligned to 16 in ELF, so %rsp is shifted 8 bytes extra
22 * d) arguments are as follows: (elf -> ms)
23 * 1) rdi -> rcx (32 saved)
24 * 2) rsi -> rdx (32 saved)
25 * 3) rdx -> r8 (32 saved)
26 * 4) rcx -> r9 (32 saved)
27 * 5) r8 -> 32(%rsp) (32 saved)
28 * 6) r9 -> 40(%rsp) (48 saved)
29 * 7) 8(%rsp) -> 48(%rsp) (48 saved)
30 * 8) 16(%rsp) -> 56(%rsp) (64 saved)
31 * 9) 24(%rsp) -> 64(%rsp) (64 saved)
32 * 10) 32(%rsp) -> 72(%rsp) (80 saved)
33 * e) because the first argument we recieve in a thunker is actually the
34 * function to be called, arguments are offset as such:
36 * 1) rsi -> rcx (32 saved)
37 * 2) rdx -> rdx (32 saved)
38 * 3) rcx -> r8 (32 saved)
39 * 4) r8 -> r9 (32 saved)
40 * 5) r9 -> 32(%rsp) (32 saved)
41 * 6) 8(%rsp) -> 40(%rsp) (48 saved)
42 * 7) 16(%rsp) -> 48(%rsp) (48 saved)
43 * 8) 24(%rsp) -> 56(%rsp) (64 saved)
44 * 9) 32(%rsp) -> 64(%rsp) (64 saved)
45 * 10) 40(%rsp) -> 72(%rsp) (80 saved)
46 * f) arguments need to be moved in opposite order to avoid clobbering
119 mov 56+16(%rsp), %rax
134 mov 72+24(%rsp), %rax
136 mov 72+16(%rsp), %rax
151 mov 72+32(%rsp), %rax
153 mov 72+24(%rsp), %rax
155 mov 72+16(%rsp), %rax
170 mov 88+40(%rsp), %rax
172 mov 88+32(%rsp), %rax
174 mov 88+24(%rsp), %rax
176 mov 88+16(%rsp), %rax