1/*
2 * Copyright 2008, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6#include <asm_defs.h>
7
8
9call_stub:
10	// push a pointer to arguments and a pointer to ourselves on the stack
11	lea		4(%esp), %eax
12	push	%eax
13	call	1f
141:
15	pop		%eax
16	subl	$(1b - call_stub), %eax
17	push	%eax
18
19	// call the wrapper function
20	movl	(call_stub_callback_address - call_stub)(%eax), %eax
21	call	*%eax
22				// returns a pointer to the actual function
23	lea		8(%esp), %esp
24
25	jmp		*%eax
26
27.align 4
28call_stub_callback_address:
29	.long	0
30call_stub_end:
31
32
33// size_t arch_call_stub_size();
34FUNCTION(arch_call_stub_size):
35	movl	$(call_stub_end - call_stub), %eax
36	ret
37FUNCTION_END(arch_call_stub_size)
38
39
40
41// void arch_init_call_stub(void* stub,
42//		void* (*callback)(const void* stub, const void* args),
43//		void* function);
44FUNCTION(arch_init_call_stub):
45	push	%ebp
46	movl	%esp, %ebp
47
48	// stub address to %edi
49	push	%edi
50	movl	8(%ebp), %edi
51
52	// copy the stub
53	movl	$(call_stub_end - call_stub), %eax
54	push	%eax
55	movl	$call_stub, %eax
56	push	%eax
57	push	%edi
58	call	memcpy
59	lea		12(%esp), %esp
60
61	// set the callback address in the stub
62	movl	12(%ebp), %eax
63	movl	%eax, (call_stub_callback_address - call_stub)(%edi)
64
65	// restore %edi
66	pop		%edi
67
68	movl	%ebp, %esp
69	pop		%ebp
70	ret
71FUNCTION_END(arch_init_call_stub)
72