From 6bf7cd966b40e1ac271a8683e6df5a6cf7f72372 Mon Sep 17 00:00:00 2001 From: Gregor Haas Date: Tue, 17 Jan 2023 14:16:57 -0800 Subject: [PATCH] Add example for physical device use --- examples/CMakeLists.txt | 1 + examples/devshare/CMakeLists.txt | 41 +++++++++++++++++++++++++++++++ examples/devshare/eapp/devshare.c | 32 ++++++++++++++++++++++++ examples/devshare/host/host.cpp | 22 +++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 examples/devshare/CMakeLists.txt create mode 100644 examples/devshare/eapp/devshare.c create mode 100644 examples/devshare/host/host.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 6806081fd..2d17b4442 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -35,3 +35,4 @@ add_subdirectory(hello) add_subdirectory(hello-native) add_subdirectory(attestation) add_subdirectory(tests) +add_subdirectory(devshare) \ No newline at end of file diff --git a/examples/devshare/CMakeLists.txt b/examples/devshare/CMakeLists.txt new file mode 100644 index 000000000..9df61183b --- /dev/null +++ b/examples/devshare/CMakeLists.txt @@ -0,0 +1,41 @@ +set(eapp_bin devshare) +set(eapp_src eapp/devshare.c) +set(host_bin devshare-runner) +set(host_src host/host.cpp) +set(package_name "devshare.ke") +set(package_script "./devshare-runner devshare eyrie-rt") + +if(RISCV32) + set(eyrie_plugins "freemem io_syscall linux_syscall env_setup rv32") +else() + set(eyrie_plugins "freemem io_syscall linux_syscall env_setup") +endif() + +# eapp + +add_executable(${eapp_bin} ${eapp_src}) +target_link_libraries(${eapp_bin} "-static" ${KEYSTONE_LIB_EAPP}) + +# host + +add_executable(${host_bin} ${host_src}) +target_link_libraries(${host_bin} ${KEYSTONE_LIB_HOST} ${KEYSTONE_LIB_EDGE}) + +# add target for Eyrie runtime (see keystone.cmake) + +set(eyrie_files_to_copy .options_log eyrie-rt) +add_eyrie_runtime(${eapp_bin}-eyrie + ${eyrie_plugins} + ${eyrie_files_to_copy}) + +# add target for packaging (see keystone.cmake) + +add_keystone_package(${eapp_bin}-package + ${package_name} + ${package_script} + ${eyrie_files_to_copy} ${eapp_bin} ${host_bin}) + +add_dependencies(${eapp_bin}-package ${eapp_bin}-eyrie) + +# add package to the top-level target +add_dependencies(examples ${eapp_bin}-package) diff --git a/examples/devshare/eapp/devshare.c b/examples/devshare/eapp/devshare.c new file mode 100644 index 000000000..4859d6d26 --- /dev/null +++ b/examples/devshare/eapp/devshare.c @@ -0,0 +1,32 @@ +#include +#include + +#include "app/syscall.h" + +#define SECURE_DEVICE "uart@10001000" + +int main() +{ + int ret, i; + ret = claim_mmio(SECURE_DEVICE, + strlen(SECURE_DEVICE)); + if(ret < 0) { + printf("Failed to claim " SECURE_DEVICE "\n"); + return -1; + } + + for(i = 0; i < 1000; i++) { + printf("Writing to UART: %i!\n", i); + fflush(stdout); + } + + // todo do something with the device + + ret = release_mmio(SECURE_DEVICE, + strlen(SECURE_DEVICE)); + if(ret < 0) { + printf("Failed to release " SECURE_DEVICE "\n"); + } + + return 0; +} diff --git a/examples/devshare/host/host.cpp b/examples/devshare/host/host.cpp new file mode 100644 index 000000000..1bab5c509 --- /dev/null +++ b/examples/devshare/host/host.cpp @@ -0,0 +1,22 @@ + +#include "edge/edge_call.h" +#include "host/keystone.h" + +using namespace Keystone; + +int main(int argc, char **argv) { + Enclave enclave; + Params params; + + params.setFreeMemSize(1024 * 1024); + params.setUntrustedMem(DEFAULT_UNTRUSTED_PTR, 1024 * 1024); + + enclave.init(argv[1], argv[2], params); + + enclave.registerOcallDispatch(incoming_call_dispatch); + edge_call_init_internals( + (uintptr_t)enclave.getSharedBuffer(), enclave.getSharedBufferSize()); + + enclave.run(); + return 0; +} \ No newline at end of file