1b78f13911bfe6eda303e91ef215c87a165aae8aeAlexandre Rames// Copyright 2015, VIXL authors
25289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// All rights reserved.
35289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//
45289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// Redistribution and use in source and binary forms, with or without
55289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// modification, are permitted provided that the following conditions are met:
65289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//
75289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//   * Redistributions of source code must retain the above copyright notice,
85289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//     this list of conditions and the following disclaimer.
95289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//   * Redistributions in binary form must reproduce the above copyright notice,
105289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//     this list of conditions and the following disclaimer in the documentation
115289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//     and/or other materials provided with the distribution.
125289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//   * Neither the name of ARM Limited nor the names of its contributors may be
135289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//     used to endorse or promote products derived from this software without
145289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//     specific prior written permission.
155289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl//
165289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
175289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
185289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
195289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
205289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
215289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
225289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
235289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
245289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
255289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
2788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#include "test-utils.h"
28b68bacb75c1ab265fc539afa93964c7f51f35589Alexandre Rames
2978973f258039f6e96eba85f1b5ecdb14b3c51dbbPierre Langloisextern "C" {
3088c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#include <sys/mman.h>
3178973f258039f6e96eba85f1b5ecdb14b3c51dbbPierre Langlois}
325289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
3388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#include "globals-vixl.h"
34d3832965c62a8ad461b9ea9eb0994ca6b0a3da2cAlexandre Rames#include "aarch64/cpu-aarch64.h"
355289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
3688c46b84df005638546de5e4e965bdcc31352f48Pierre Langloisnamespace vixl {
375289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
382b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames// BSD uses `MAP_ANON` instead of the Linux `MAP_ANONYMOUS`. The `MAP_ANONYMOUS`
392b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames// alias should generally be available, but is not always, so define it manually
402b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames// if necessary.
412b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
422b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames#define MAP_ANONYMOUS MAP_ANON
432b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames#endif
442b5d561b62ab5d584428f56872f67631bce66ad2Alexandre Rames
455289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
4631dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gildayvoid ExecuteMemory(byte* buffer, size_t size, int offset) {
4788c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  void (*test_function)(void);
485289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
4931dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  VIXL_ASSERT((offset >= 0) && (static_cast<size_t>(offset) < size));
5031dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  VIXL_STATIC_ASSERT(sizeof(buffer) == sizeof(test_function));
5188c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  VIXL_STATIC_ASSERT(sizeof(uintptr_t) == sizeof(test_function));
5231dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  uintptr_t entry_point = reinterpret_cast<uintptr_t>(buffer);
5388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  entry_point += offset;
5488c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  memcpy(&test_function, &entry_point, sizeof(test_function));
555289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl
5631dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  USE(size);
5731dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday
58dd63b6bfaafaa64ea6538447000da6b671657076Pierre Langlois#if defined(__aarch64__) && defined(VIXL_INCLUDE_TARGET_AARCH64)
5931dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  aarch64::CPU::EnsureIAndDCacheCoherency(buffer, size);
609a9331faeba996d6c85e6e2a6355ccfc22c6cab6Rodolph Perfetta#elif defined(__arm__) && \
61bde2e4b5ce376456d50a972b6f3aaee3475f8786Pierre Langlois    (defined(VIXL_INCLUDE_TARGET_A32) || defined(VIXL_INCLUDE_TARGET_T32))
6288c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // TODO: Do not use __builtin___clear_cache and instead implement
6388c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  // `CPU::EnsureIAndDCacheCoherency` for aarch32.
6431dd2ae90d5e82871667fbf3ee2697a155e7c3acAlex Gilday  __builtin___clear_cache(buffer, reinterpret_cast<char*>(buffer) + size);
6588c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois#endif
6688c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois  test_function();
675289c5900fb214f2f6aa61e2a9263730dcf4cc17armvixl}
6888c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois
6988c46b84df005638546de5e4e965bdcc31352f48Pierre Langlois}  // namespace vixl
70