1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 #include <stddef.h>
18 #include <uefi/types.h>
19 
20 #include "switch_stack.h"
21 
22 constexpr size_t kIoStackSize = 1024ul * 32;
23 
24 void* get_io_stack();
25 
26 template <typename Arg1, EfiStatus (*Func)(Arg1)>
switch_stack_wrapper()27 EfiStatus (*switch_stack_wrapper())(Arg1 arg) {
28   auto trampoline = [](Arg1 arg) -> EfiStatus {
29     void* io_stack = reinterpret_cast<char*>(get_io_stack()) + kIoStackSize;
30     return static_cast<EfiStatus>(call_with_stack(io_stack, Func, arg));
31   };
32   return trampoline;
33 }
34 
35 template <typename Arg1, typename Arg2, EfiStatus (*Func)(Arg1, Arg2)>
switch_stack_wrapper()36 EfiStatus (*switch_stack_wrapper())(Arg1 arg1, Arg2 arg2) {
37   auto trampoline = [](Arg1 arg1, Arg2 arg2) -> EfiStatus {
38     void* io_stack = reinterpret_cast<char*>(get_io_stack()) + kIoStackSize;
39     return static_cast<EfiStatus>(call_with_stack(io_stack, Func, arg1, arg2));
40   };
41   return trampoline;
42 }
43 
44 template <typename Arg1, typename Arg2, typename Arg3,
45           EfiStatus (*Func)(Arg1, Arg2, Arg3)>
switch_stack_wrapper()46 EfiStatus (*switch_stack_wrapper())(Arg1 arg1, Arg2 arg2, Arg3 arg3) {
47   auto trampoline = [](Arg1 arg1, Arg2 arg2, Arg3 arg3) -> EfiStatus {
48     void* io_stack = reinterpret_cast<char*>(get_io_stack()) + kIoStackSize;
49     return static_cast<EfiStatus>(
50         call_with_stack(io_stack, Func, arg1, arg2, arg3));
51   };
52   return trampoline;
53 }
54 
55 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
56           EfiStatus (*Func)(Arg1, Arg2, Arg3, Arg4)>
switch_stack_wrapper()57 EfiStatus (*switch_stack_wrapper())(Arg1, Arg2, Arg3, Arg4) {
58   auto trampoline = [](Arg1 arg1, Arg2 arg2, Arg3 arg3,
59                        Arg4 arg4) -> EfiStatus {
60     void* io_stack = reinterpret_cast<char*>(get_io_stack()) + kIoStackSize;
61     return static_cast<EfiStatus>(
62         call_with_stack(io_stack, Func, arg1, arg2, arg3, arg4));
63   };
64   return trampoline;
65 }
66