jit_codecache.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "jit_codecache.h"
  6. #include "mem_alloc.h"
  7. #include "jit_compiler.h"
  8. static void *code_cache_pool = NULL;
  9. static uint32 code_cache_pool_size = 0;
  10. static mem_allocator_t code_cache_pool_allocator = NULL;
  11. bool
  12. jit_code_cache_init(uint32 code_cache_size)
  13. {
  14. int map_prot = MMAP_PROT_READ | MMAP_PROT_WRITE | MMAP_PROT_EXEC;
  15. int map_flags = MMAP_MAP_NONE;
  16. if (!(code_cache_pool =
  17. os_mmap(NULL, code_cache_size, map_prot, map_flags))) {
  18. return false;
  19. }
  20. if (!(code_cache_pool_allocator =
  21. mem_allocator_create(code_cache_pool, code_cache_size))) {
  22. os_munmap(code_cache_pool, code_cache_size);
  23. code_cache_pool = NULL;
  24. return false;
  25. }
  26. code_cache_pool_size = code_cache_size;
  27. return true;
  28. }
  29. void
  30. jit_code_cache_destroy()
  31. {
  32. mem_allocator_destroy(code_cache_pool_allocator);
  33. os_munmap(code_cache_pool, code_cache_pool_size);
  34. }
  35. void *
  36. jit_code_cache_alloc(uint32 size)
  37. {
  38. return mem_allocator_malloc(code_cache_pool_allocator, size);
  39. }
  40. void
  41. jit_code_cache_free(void *ptr)
  42. {
  43. if (ptr)
  44. mem_allocator_free(code_cache_pool_allocator, ptr);
  45. }
  46. bool
  47. jit_pass_register_jitted_code(JitCompContext *cc)
  48. {
  49. WASMModuleInstance *instance;
  50. WASMModule *module = cc->cur_wasm_module;
  51. WASMFunction *func = cc->cur_wasm_func;
  52. uint32 jit_func_idx = cc->cur_wasm_func_idx - module->import_function_count;
  53. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  54. && WASM_ENABLE_LAZY_JIT != 0
  55. os_mutex_lock(&module->instance_list_lock);
  56. #endif
  57. module->fast_jit_func_ptrs[jit_func_idx] = func->fast_jit_jitted_code =
  58. cc->jitted_addr_begin;
  59. #if WASM_ENABLE_FAST_JIT != 0 && WASM_ENABLE_JIT != 0 \
  60. && WASM_ENABLE_LAZY_JIT != 0
  61. instance = module->instance_list;
  62. while (instance) {
  63. if (instance->e->running_mode == Mode_Fast_JIT)
  64. instance->fast_jit_func_ptrs[jit_func_idx] = cc->jitted_addr_begin;
  65. instance = instance->e->next;
  66. }
  67. os_mutex_unlock(&module->instance_list_lock);
  68. #else
  69. (void)instance;
  70. #endif
  71. return true;
  72. }