xref: /aosp_15_r20/external/coreboot/src/arch/x86/include/mode_switch.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <stdint.h>
4 
5 #if ENV_X86_64
6 /*
7  * Assembly code that drops into protected mode and calls the function
8  * specified as first argument, which must have been compiled for x86_32.
9  * After the function returns it enters long mode again.
10  * The function pointer destination must be below 4GiB in physical memory.
11  *
12  * The called function has 0-3 arguments and returns an int.
13  */
14 int protected_mode_call_wrapper(uint32_t func_ptr,
15 				uint32_t opt_arg1,
16 				uint32_t opt_arg2,
17 				uint32_t opt_arg3);
18 
19 /*
20  * Drops into protected mode and calls the function, which must have been compiled for x86_32.
21  * After the function returns it enters long mode again.
22  * The function pointer destination must be below 4GiB in physical memory.
23  *
24  * The called function doesn't have arguments and returns an int.
25  */
protected_mode_call(void * func)26 static inline int protected_mode_call(void *func)
27 {
28 	return protected_mode_call_wrapper((uintptr_t)func, 0, 0, 0);
29 }
30 
31 /*
32  * Drops into protected mode and calls the function, which must have been compiled for x86_32.
33  * After the function returns it enters long mode again.
34  * The function pointer destination must be below 4GiB in physical memory.
35  * Only the lower 32bits of the argument are passed to the called function.
36  *
37  * The called function have one argument and returns an int.
38  */
protected_mode_call_1arg(void * func,uint32_t arg1)39 static inline int protected_mode_call_1arg(void *func, uint32_t arg1)
40 {
41 	return protected_mode_call_wrapper((uintptr_t)func, arg1, 0, 0);
42 }
43 
44 /*
45  * Drops into protected mode and calls the function, which must have been compiled for x86_32.
46  * After the function returns it enters long mode again.
47  * The function pointer destination must be below 4GiB in physical memory.
48  * Only the lower 32bits of the argument are passed to the called function.
49  *
50  * The called function has two arguments and returns an int.
51  */
protected_mode_call_2arg(void * func,uint32_t arg1,uint32_t arg2)52 static inline int protected_mode_call_2arg(void *func, uint32_t arg1, uint32_t arg2)
53 {
54 	return protected_mode_call_wrapper((uintptr_t)func, arg1, arg2, 0);
55 }
56 
57 /*
58  * Drops into protected mode and calls the function, which must have been compiled for x86_32.
59  * After the function returns it enters long mode again.
60  * The function pointer destination must be below 4GiB in physical memory.
61  * Only the lower 32bits of the argument are passed to the called function.
62  *
63  * The called function has two arguments and returns an int.
64  */
protected_mode_call_3arg(void * func,uint32_t arg1,uint32_t arg2,uint32_t arg3)65 static inline int protected_mode_call_3arg(void *func, uint32_t arg1, uint32_t arg2,
66 					   uint32_t arg3)
67 {
68 	return protected_mode_call_wrapper((uintptr_t)func, arg1, arg2, arg3);
69 }
70 #else
protected_mode_call(void * func)71 static inline int protected_mode_call(void *func)
72 {
73 	int (*doit)(void) = func;
74 
75 	return doit();
76 }
77 
protected_mode_call_1arg(void * func,uint32_t arg1)78 static inline int protected_mode_call_1arg(void *func, uint32_t arg1)
79 {
80 	int (*doit)(uint32_t arg1) = func;
81 
82 	return doit(arg1);
83 }
84 
protected_mode_call_2arg(void * func,uint32_t arg1,uint32_t arg2)85 static inline int protected_mode_call_2arg(void *func, uint32_t arg1, uint32_t arg2)
86 {
87 	int (*doit)(uint32_t arg1, uint32_t arg2) = func;
88 
89 	return doit(arg1, arg2);
90 }
91 
protected_mode_call_3arg(void * func,uint32_t arg1,uint32_t arg2,uint32_t arg3)92 static inline int protected_mode_call_3arg(void *func, uint32_t arg1, uint32_t arg2,
93 					   uint32_t arg3)
94 {
95 	int (*doit)(uint32_t arg1, uint32_t arg2, uint32_t arg3) = func;
96 
97 	return doit(arg1, arg2, arg3);
98 }
99 #endif
100