summaryrefslogtreecommitdiff
path: root/src/arch/x86/include/mode_switch.h
blob: 54969b2e67c26d5f82bb1a644e593cd40104586e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* SPDX-License-Identifier: GPL-2.0-only */

#include <stdint.h>

#if ENV_X86_64
/*
 * Assembly code that drops into protected mode and calls the function
 * specified as first argument, which must have been compiled for x86_32.
 * After the function returns it enters long mode again.
 * The function pointer destination must be below 4GiB in physical memory.
 *
 * The called function has 0-3 arguments and returns an int.
 */
int protected_mode_call_wrapper(uint32_t func_ptr,
				uint32_t opt_arg1,
				uint32_t opt_arg2,
				uint32_t opt_arg3);

/*
 * Drops into protected mode and calls the function, which must have been compiled for x86_32.
 * After the function returns it enters long mode again.
 * The function pointer destination must be below 4GiB in physical memory.
 *
 * The called function doesn't have arguments and returns an int.
 */
static inline int protected_mode_call(void *func)
{
	return protected_mode_call_wrapper((uintptr_t)func, 0, 0, 0);
}

/*
 * Drops into protected mode and calls the function, which must have been compiled for x86_32.
 * After the function returns it enters long mode again.
 * The function pointer destination must be below 4GiB in physical memory.
 * Only the lower 32bits of the argument are passed to the called function.
 *
 * The called function have one argument and returns an int.
 */
static inline int protected_mode_call_1arg(void *func, uint32_t arg1)
{
	return protected_mode_call_wrapper((uintptr_t)func, arg1, 0, 0);
}

/*
 * Drops into protected mode and calls the function, which must have been compiled for x86_32.
 * After the function returns it enters long mode again.
 * The function pointer destination must be below 4GiB in physical memory.
 * Only the lower 32bits of the argument are passed to the called function.
 *
 * The called function has two arguments and returns an int.
 */
static inline int protected_mode_call_2arg(void *func, uint32_t arg1, uint32_t arg2)
{
	return protected_mode_call_wrapper((uintptr_t)func, arg1, arg2, 0);
}

/*
 * Drops into protected mode and calls the function, which must have been compiled for x86_32.
 * After the function returns it enters long mode again.
 * The function pointer destination must be below 4GiB in physical memory.
 * Only the lower 32bits of the argument are passed to the called function.
 *
 * The called function has two arguments and returns an int.
 */
static inline int protected_mode_call_3arg(void *func, uint32_t arg1, uint32_t arg2,
					   uint32_t arg3)
{
	return protected_mode_call_wrapper((uintptr_t)func, arg1, arg2, arg3);
}
#else
static inline int protected_mode_call(void *func)
{
	int (*doit)(void) = func;

	return doit();
}

static inline int protected_mode_call_1arg(void *func, uint32_t arg1)
{
	int (*doit)(uint32_t arg1) = func;

	return doit(arg1);
}

static inline int protected_mode_call_2arg(void *func, uint32_t arg1, uint32_t arg2)
{
	int (*doit)(uint32_t arg1, uint32_t arg2) = func;

	return doit(arg1, arg2);
}

static inline int protected_mode_call_3arg(void *func, uint32_t arg1, uint32_t arg2,
					   uint32_t arg3)
{
	int (*doit)(uint32_t arg1, uint32_t arg2, uint32_t arg3) = func;

	return doit(arg1, arg2, arg3);
}
#endif