1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 /*!
21 * \file runtime_t.cc
22 */
23 #include <tvm/runtime/c_runtime_api.h>
24 #include <tvm/runtime/packed_func.h>
25 #include "../../c_runtime_api.cc"
26 #include "../../cpu_device_api.cc"
27 #include "../../module.cc"
28 #include "../../module_util.cc"
29 #include "../../registry.cc"
30 #include "../../system_lib_module.cc"
31 #include "../../thread_pool.cc"
32 #include "../../workspace_pool.cc"
33 #include "ecall_registry.h"
34 #include "runtime.h"
35 #include "threading_backend.cc"
36
37 namespace tvm {
38 namespace runtime {
39 namespace sgx {
40
41 extern "C" {
42
tvm_ecall_init(TVMRetValueHandle ret)43 void tvm_ecall_init(TVMRetValueHandle ret) {}
44
tvm_ecall_packed_func(int func_id,const TVMValue * arg_values,const int * type_codes,int num_args,TVMRetValueHandle ret)45 void tvm_ecall_packed_func(int func_id,
46 const TVMValue* arg_values,
47 const int* type_codes,
48 int num_args,
49 TVMRetValueHandle ret) {
50 const PackedFunc* f = ECallRegistry::Get(func_id);
51 CHECK(f != nullptr) << "ecall function not found.";
52
53 TVMRetValue rv;
54 f->CallPacked(TVMArgs(arg_values, type_codes, num_args), &rv);
55
56 int ret_type_code = rv.type_code();
57 if (ret_type_code == kNull) return;
58
59 TVMValue ret_value;
60 if (ret_type_code == kBytes || ret_type_code == kStr) {
61 // allocate a buffer in untrusted, copy the values in
62 std::string bytes = rv;
63
64 void* ret_buf;
65 TVM_SGX_CHECKED_CALL(tvm_ocall_reserve_space(
66 &ret_buf, bytes.size() + sizeof(TVMByteArray), sizeof(uint64_t)));
67
68 char* data_buf = static_cast<char*>(ret_buf) + sizeof(TVMByteArray);
69 memcpy(data_buf, bytes.data(), bytes.size());
70
71 TVMByteArray* arr = static_cast<TVMByteArray*>(ret_buf);
72 arr->data = data_buf;
73 arr->size = bytes.size();
74
75 ret_value = TVMValue{.v_handle = arr};
76 ret_type_code = kBytes;
77 } else {
78 rv.MoveToCHost(&ret_value, &ret_type_code);
79 }
80 TVM_SGX_CHECKED_CALL(tvm_ocall_set_return(ret, &ret_value, &ret_type_code, 1));
81 }
82
83 } // extern "C"
84
85 TVM_REGISTER_ENCLAVE_FUNC("__tvm_main__")
__anon854f1df80102(TVMArgs args, TVMRetValue* rv) 86 .set_body([](TVMArgs args, TVMRetValue* rv) {
87 Module mod = (*Registry::Get("module._GetSystemLib"))();
88 mod.GetFunction("default_function").CallPacked(args, rv);
89 });
90
91 } // namespace sgx
92 } // namespace runtime
93 } // namespace tvm
94