main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "bh_platform.h"
  8. #include "bh_assert.h"
  9. #include "bh_log.h"
  10. #include "wasm_export.h"
  11. #if defined(BUILD_TARGET_RISCV64_LP64) || defined(BUILD_TARGET_RISCV32_ILP32)
  12. #include "test_wasm_riscv64.h"
  13. #else
  14. #include "test_wasm.h"
  15. #endif /* end of BUILD_TARGET_RISCV64_LP64 || BUILD_TARGET_RISCV32_ILP32 */
  16. #if defined(BUILD_TARGET_RISCV64_LP64) || defined(BUILD_TARGET_RISCV32_ILP32)
  17. #if defined(BUILD_TARGET_RISCV64_LP64)
  18. #define CONFIG_GLOBAL_HEAP_BUF_SIZE 4360
  19. #define CONFIG_APP_STACK_SIZE 288
  20. #define CONFIG_MAIN_THREAD_STACK_SIZE 2400
  21. #else
  22. #define CONFIG_GLOBAL_HEAP_BUF_SIZE 5120
  23. #define CONFIG_APP_STACK_SIZE 512
  24. #define CONFIG_MAIN_THREAD_STACK_SIZE 4096
  25. #endif
  26. #define CONFIG_APP_HEAP_SIZE 256
  27. #else /* else of BUILD_TARGET_RISCV64_LP64 || BUILD_TARGET_RISCV32_ILP32 */
  28. #define CONFIG_GLOBAL_HEAP_BUF_SIZE WASM_GLOBAL_HEAP_SIZE
  29. #define CONFIG_APP_STACK_SIZE 8192
  30. #define CONFIG_APP_HEAP_SIZE 8192
  31. #ifdef CONFIG_NO_OPTIMIZATIONS
  32. #define CONFIG_MAIN_THREAD_STACK_SIZE 8192
  33. #else
  34. #define CONFIG_MAIN_THREAD_STACK_SIZE 4096
  35. #endif
  36. #endif /* end of BUILD_TARGET_RISCV64_LP64 || BUILD_TARGET_RISCV32_ILP32 */
  37. static int app_argc;
  38. static char **app_argv;
  39. /**
  40. * Find the unique main function from a WASM module instance
  41. * and execute that function.
  42. *
  43. * @param module_inst the WASM module instance
  44. * @param argc the number of arguments
  45. * @param argv the arguments array
  46. *
  47. * @return true if the main function is called, false otherwise.
  48. */
  49. bool
  50. wasm_application_execute_main(wasm_module_inst_t module_inst, int argc,
  51. char *argv[]);
  52. static void *
  53. app_instance_main(wasm_module_inst_t module_inst)
  54. {
  55. const char *exception;
  56. wasm_function_inst_t func;
  57. wasm_exec_env_t exec_env;
  58. unsigned argv[2] = { 0 };
  59. if (wasm_runtime_lookup_function(module_inst, "main", NULL)
  60. || wasm_runtime_lookup_function(module_inst, "__main_argc_argv",
  61. NULL)) {
  62. LOG_VERBOSE("Calling main function\n");
  63. wasm_application_execute_main(module_inst, app_argc, app_argv);
  64. }
  65. else if ((func = wasm_runtime_lookup_function(module_inst, "app_main",
  66. NULL))) {
  67. exec_env =
  68. wasm_runtime_create_exec_env(module_inst, CONFIG_APP_HEAP_SIZE);
  69. if (!exec_env) {
  70. os_printf("Create exec env failed\n");
  71. return NULL;
  72. }
  73. LOG_VERBOSE("Calling app_main function\n");
  74. wasm_runtime_call_wasm(exec_env, func, 0, argv);
  75. if (!wasm_runtime_get_exception(module_inst)) {
  76. os_printf("result: 0x%x\n", argv[0]);
  77. }
  78. wasm_runtime_destroy_exec_env(exec_env);
  79. }
  80. else {
  81. os_printf("Failed to lookup function main or app_main to call\n");
  82. return NULL;
  83. }
  84. if ((exception = wasm_runtime_get_exception(module_inst)))
  85. os_printf("%s\n", exception);
  86. return NULL;
  87. }
  88. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  89. static char global_heap_buf[CONFIG_GLOBAL_HEAP_BUF_SIZE] = { 0 };
  90. #endif
  91. void
  92. iwasm_main(void *arg1, void *arg2, void *arg3)
  93. {
  94. int start, end;
  95. start = k_uptime_get_32();
  96. uint8 *wasm_file_buf = NULL;
  97. uint32 wasm_file_size;
  98. wasm_module_t wasm_module = NULL;
  99. wasm_module_inst_t wasm_module_inst = NULL;
  100. RuntimeInitArgs init_args;
  101. char error_buf[128];
  102. #if WASM_ENABLE_LOG != 0
  103. int log_verbose_level = 2;
  104. #endif
  105. (void)arg1;
  106. (void)arg2;
  107. (void)arg3;
  108. memset(&init_args, 0, sizeof(RuntimeInitArgs));
  109. #if WASM_ENABLE_GLOBAL_HEAP_POOL != 0
  110. init_args.mem_alloc_type = Alloc_With_Pool;
  111. init_args.mem_alloc_option.pool.heap_buf = global_heap_buf;
  112. init_args.mem_alloc_option.pool.heap_size = sizeof(global_heap_buf);
  113. #else
  114. #error Another memory allocation scheme than global heap pool is not implemented yet for Zephyr.
  115. #endif
  116. /* initialize runtime environment */
  117. if (!wasm_runtime_full_init(&init_args)) {
  118. printf("Init runtime environment failed.\n");
  119. return;
  120. }
  121. #if WASM_ENABLE_LOG != 0
  122. bh_log_set_verbose_level(log_verbose_level);
  123. #endif
  124. /* load WASM byte buffer from byte buffer of include file */
  125. wasm_file_buf = (uint8 *)wasm_test_file;
  126. wasm_file_size = sizeof(wasm_test_file);
  127. /* load WASM module */
  128. if (!(wasm_module = wasm_runtime_load(wasm_file_buf, wasm_file_size,
  129. error_buf, sizeof(error_buf)))) {
  130. printf("%s\n", error_buf);
  131. goto fail1;
  132. }
  133. /* instantiate the module */
  134. if (!(wasm_module_inst = wasm_runtime_instantiate(
  135. wasm_module, CONFIG_APP_STACK_SIZE, CONFIG_APP_HEAP_SIZE,
  136. error_buf, sizeof(error_buf)))) {
  137. printf("%s\n", error_buf);
  138. goto fail2;
  139. }
  140. /* invoke the main function */
  141. app_instance_main(wasm_module_inst);
  142. /* destroy the module instance */
  143. wasm_runtime_deinstantiate(wasm_module_inst);
  144. fail2:
  145. /* unload the module */
  146. wasm_runtime_unload(wasm_module);
  147. fail1:
  148. /* destroy runtime environment */
  149. wasm_runtime_destroy();
  150. end = k_uptime_get_32();
  151. printf("elapsed: %d\n", (end - start));
  152. }
  153. #define MAIN_THREAD_STACK_SIZE (CONFIG_MAIN_THREAD_STACK_SIZE)
  154. #define MAIN_THREAD_PRIORITY 5
  155. K_THREAD_STACK_DEFINE(iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE);
  156. static struct k_thread iwasm_main_thread;
  157. bool
  158. iwasm_init(void)
  159. {
  160. k_tid_t tid = k_thread_create(
  161. &iwasm_main_thread, iwasm_main_thread_stack, MAIN_THREAD_STACK_SIZE,
  162. iwasm_main, NULL, NULL, NULL, MAIN_THREAD_PRIORITY, 0, K_NO_WAIT);
  163. return tid ? true : false;
  164. }
  165. #if KERNEL_VERSION_NUMBER < 0x030400 /* version 3.4.0 */
  166. void
  167. main(void)
  168. {
  169. iwasm_init();
  170. }
  171. #else
  172. int
  173. main(void)
  174. {
  175. iwasm_init();
  176. return 0;
  177. }
  178. #endif