1/*
2 * Function calling ABI conversion from Linux to EFI for x86_64
3 *
4 * Copyright (C) 2007 Intel Corp
5 *	Bibo Mao <bibo.mao@intel.com>
6 *	Huang Ying <ying.huang@intel.com>
7 */
8
9#include <linux/linkage.h>
10#include <asm/segment.h>
11#include <asm/msr.h>
12#include <asm/processor-flags.h>
13#include <asm/page_types.h>
14
15#define SAVE_XMM			\
16	mov %rsp, %rax;			\
17	subq $0x70, %rsp;		\
18	and $~0xf, %rsp;		\
19	mov %rax, (%rsp);		\
20	mov %cr0, %rax;			\
21	clts;				\
22	mov %rax, 0x8(%rsp);		\
23	movaps %xmm0, 0x60(%rsp);	\
24	movaps %xmm1, 0x50(%rsp);	\
25	movaps %xmm2, 0x40(%rsp);	\
26	movaps %xmm3, 0x30(%rsp);	\
27	movaps %xmm4, 0x20(%rsp);	\
28	movaps %xmm5, 0x10(%rsp)
29
30#define RESTORE_XMM			\
31	movaps 0x60(%rsp), %xmm0;	\
32	movaps 0x50(%rsp), %xmm1;	\
33	movaps 0x40(%rsp), %xmm2;	\
34	movaps 0x30(%rsp), %xmm3;	\
35	movaps 0x20(%rsp), %xmm4;	\
36	movaps 0x10(%rsp), %xmm5;	\
37	mov 0x8(%rsp), %rsi;		\
38	mov %rsi, %cr0;			\
39	mov (%rsp), %rsp
40
41	/* stolen from gcc */
42	.macro FLUSH_TLB_ALL
43	movq %r15, efi_scratch(%rip)
44	movq %r14, efi_scratch+8(%rip)
45	movq %cr4, %r15
46	movq %r15, %r14
47	andb $0x7f, %r14b
48	movq %r14, %cr4
49	movq %r15, %cr4
50	movq efi_scratch+8(%rip), %r14
51	movq efi_scratch(%rip), %r15
52	.endm
53
54	.macro SWITCH_PGT
55	cmpb $0, efi_scratch+24(%rip)
56	je 1f
57	movq %r15, efi_scratch(%rip)		# r15
58	# save previous CR3
59	movq %cr3, %r15
60	movq %r15, efi_scratch+8(%rip)		# prev_cr3
61	movq efi_scratch+16(%rip), %r15		# EFI pgt
62	movq %r15, %cr3
63	1:
64	.endm
65
66	.macro RESTORE_PGT
67	cmpb $0, efi_scratch+24(%rip)
68	je 2f
69	movq efi_scratch+8(%rip), %r15
70	movq %r15, %cr3
71	movq efi_scratch(%rip), %r15
72	FLUSH_TLB_ALL
73	2:
74	.endm
75
76ENTRY(efi_call)
77	SAVE_XMM
78	mov (%rsp), %rax
79	mov 8(%rax), %rax
80	subq $48, %rsp
81	mov %r9, 32(%rsp)
82	mov %rax, 40(%rsp)
83	mov %r8, %r9
84	mov %rcx, %r8
85	mov %rsi, %rcx
86	SWITCH_PGT
87	call *%rdi
88	RESTORE_PGT
89	addq $48, %rsp
90	RESTORE_XMM
91	ret
92ENDPROC(efi_call)
93
94	.data
95ENTRY(efi_scratch)
96	.fill 3,8,0
97	.byte 0
98	.quad 0
99