1/*
2 * thunks_32.S - assembly helpers for mixed-bitness code
3 * Copyright (c) 2015 Denys Vlasenko
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * General Public License for more details.
13 *
14 * These are little helpers that make it easier to switch bitness on
15 * the fly.
16 */
17
18	.text
19	.code32
20
21	.global call64_from_32
22	.type call32_from_64, @function
23
24	// 4(%esp): function to call
25call64_from_32:
26	// Fetch function address
27	mov	4(%esp), %eax
28
29	// Save registers which are callee-clobbered by 64-bit ABI
30	push	%ecx
31	push	%edx
32	push	%esi
33	push	%edi
34
35	// Switch to long mode
36	jmp	$0x33,$1f
371:	.code64
38
39	// Call the function
40	call	*%rax
41
42	// Switch to compatibility mode
43	push	$0x23  /* USER32_CS */
44	.code32; push $1f; .code64 /* hack: can't have X86_64_32S relocation in 32-bit ELF */
45	lretq
461:	.code32
47
48	pop	%edi
49	pop	%esi
50	pop	%edx
51	pop	%ecx
52
53	ret
54
55.size call64_from_32, .-call64_from_32
56