1b8021494Sopenharmony_ci// Copyright 2015, VIXL authors
2b8021494Sopenharmony_ci// All rights reserved.
3b8021494Sopenharmony_ci//
4b8021494Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
5b8021494Sopenharmony_ci// modification, are permitted provided that the following conditions are met:
6b8021494Sopenharmony_ci//
7b8021494Sopenharmony_ci//   * Redistributions of source code must retain the above copyright notice,
8b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer.
9b8021494Sopenharmony_ci//   * Redistributions in binary form must reproduce the above copyright notice,
10b8021494Sopenharmony_ci//     this list of conditions and the following disclaimer in the documentation
11b8021494Sopenharmony_ci//     and/or other materials provided with the distribution.
12b8021494Sopenharmony_ci//   * Neither the name of ARM Limited nor the names of its contributors may be
13b8021494Sopenharmony_ci//     used to endorse or promote products derived from this software without
14b8021494Sopenharmony_ci//     specific prior written permission.
15b8021494Sopenharmony_ci//
16b8021494Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17b8021494Sopenharmony_ci// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18b8021494Sopenharmony_ci// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19b8021494Sopenharmony_ci// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20b8021494Sopenharmony_ci// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b8021494Sopenharmony_ci// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22b8021494Sopenharmony_ci// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23b8021494Sopenharmony_ci// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24b8021494Sopenharmony_ci// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b8021494Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b8021494Sopenharmony_ci
27b8021494Sopenharmony_ci#include "test-utils.h"
28b8021494Sopenharmony_ci
29b8021494Sopenharmony_ci#include <string.h>
30b8021494Sopenharmony_ci
31b8021494Sopenharmony_ciextern "C" {
32b8021494Sopenharmony_ci#include <sys/mman.h>
33b8021494Sopenharmony_ci}
34b8021494Sopenharmony_ci
35b8021494Sopenharmony_ci#include "globals-vixl.h"
36b8021494Sopenharmony_ci
37b8021494Sopenharmony_ci#ifdef VIXL_INCLUDE_TARGET_AARCH64
38b8021494Sopenharmony_ci#include "aarch64/cpu-aarch64.h"
39b8021494Sopenharmony_ci#endif
40b8021494Sopenharmony_ci
41b8021494Sopenharmony_cinamespace vixl {
42b8021494Sopenharmony_ci
43b8021494Sopenharmony_ci// BSD uses `MAP_ANON` instead of the Linux `MAP_ANONYMOUS`. The `MAP_ANONYMOUS`
44b8021494Sopenharmony_ci// alias should generally be available, but is not always, so define it manually
45b8021494Sopenharmony_ci// if necessary.
46b8021494Sopenharmony_ci#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
47b8021494Sopenharmony_ci#define MAP_ANONYMOUS MAP_ANON
48b8021494Sopenharmony_ci#endif
49b8021494Sopenharmony_ci
50b8021494Sopenharmony_ci
51b8021494Sopenharmony_civoid ExecuteMemory(byte* buffer, size_t size, int byte_offset) {
52b8021494Sopenharmony_ci  void (*test_function)(void);
53b8021494Sopenharmony_ci
54b8021494Sopenharmony_ci  VIXL_ASSERT((byte_offset >= 0) && (static_cast<size_t>(byte_offset) < size));
55b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(sizeof(buffer) == sizeof(test_function));
56b8021494Sopenharmony_ci  VIXL_STATIC_ASSERT(sizeof(uintptr_t) == sizeof(test_function));
57b8021494Sopenharmony_ci  uintptr_t entry_point = reinterpret_cast<uintptr_t>(buffer);
58b8021494Sopenharmony_ci  entry_point += byte_offset;
59b8021494Sopenharmony_ci  memcpy(&test_function, &entry_point, sizeof(test_function));
60b8021494Sopenharmony_ci
61b8021494Sopenharmony_ci  USE(size);
62b8021494Sopenharmony_ci
63b8021494Sopenharmony_ci#if defined(__aarch64__) && defined(VIXL_INCLUDE_TARGET_AARCH64)
64b8021494Sopenharmony_ci  aarch64::CPU::EnsureIAndDCacheCoherency(buffer, size);
65b8021494Sopenharmony_ci#elif defined(__arm__) && \
66b8021494Sopenharmony_ci    (defined(VIXL_INCLUDE_TARGET_A32) || defined(VIXL_INCLUDE_TARGET_T32))
67b8021494Sopenharmony_ci  // TODO: Do not use __builtin___clear_cache and instead implement
68b8021494Sopenharmony_ci  // `CPU::EnsureIAndDCacheCoherency` for aarch32.
69b8021494Sopenharmony_ci  __builtin___clear_cache(buffer, reinterpret_cast<char*>(buffer) + size);
70b8021494Sopenharmony_ci#else
71b8021494Sopenharmony_ci  // This helper requires a native (non-Simulator) environment.
72b8021494Sopenharmony_ci  VIXL_ABORT_WITH_MSG("Cannot ExecuteMemory(...): unsupported platform.");
73b8021494Sopenharmony_ci#endif
74b8021494Sopenharmony_ci  test_function();
75b8021494Sopenharmony_ci}
76b8021494Sopenharmony_ci
77b8021494Sopenharmony_ci}  // namespace vixl
78