aboutsummaryrefslogtreecommitdiff
path: root/compiler-rt/lib/xray/xray_s390x.cpp
blob: 599485435671bfdf213f9044b2e4800f5ed657f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//===-- xray_s390x.cpp ------------------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a dynamic runtime instrumentation system.
//
// Implementation of s390x routines.
//
//===----------------------------------------------------------------------===//
#include "sanitizer_common/sanitizer_common.h"
#include "xray_defs.h"
#include "xray_interface_internal.h"
#include <cassert>
#include <cstring>

bool __xray::patchFunctionEntry(const bool Enable, const uint32_t FuncId,
                                const XRaySledEntry &Sled,
                                const XRayTrampolines &Trampolines,
                                bool LogArgs) XRAY_NEVER_INSTRUMENT {
  uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
  // TODO: Trampoline addresses are currently inserted at compile-time, using
  //       __xray_FunctionEntry and __xray_FunctionExit only.
  //       To support DSO instrumentation, trampolines have to be written during
  //       patching (see implementation on X86_64, e.g.).
  if (Enable) {
    // The resulting code is:
    //   stmg    %r2, %r15, 16(%r15)
    //   llilf   %2, FuncID
    //   brasl   %r14, __xray_FunctionEntry@GOT
    // The FuncId and the stmg instruction must be written.

    // Write FuncId into llilf.
    Address[2] = FuncId;
    // Write last part of stmg.
    reinterpret_cast<uint16_t *>(Address)[2] = 0x24;
    // Write first part of stmg.
    Address[0] = 0xeb2ff010;
  } else {
    // j +16 instructions.
    Address[0] = 0xa7f4000b;
  }
  return true;
}

bool __xray::patchFunctionExit(
    const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
    const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
  uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
  // TODO: Trampoline addresses are currently inserted at compile-time, using
  //       __xray_FunctionEntry and __xray_FunctionExit only.
  //       To support DSO instrumentation, trampolines have to be written during
  //       patching (see implementation on X86_64, e.g.).
  if (Enable) {
    // The resulting code is:
    //   stmg    %r2, %r15, 24(%r15)
    //   llilf   %2,FuncID
    //   j       __xray_FunctionEntry@GOT
    // The FuncId and the stmg instruction must be written.

    // Write FuncId into llilf.
    Address[2] = FuncId;
    // Write last part of of stmg.
    reinterpret_cast<uint16_t *>(Address)[2] = 0x24;
    // Write first part of stmg.
    Address[0] = 0xeb2ff010;
  } else {
    // br %14 instruction.
    reinterpret_cast<uint16_t *>(Address)[0] = 0x07fe;
  }
  return true;
}

bool __xray::patchFunctionTailExit(
    const bool Enable, const uint32_t FuncId, const XRaySledEntry &Sled,
    const XRayTrampolines &Trampolines) XRAY_NEVER_INSTRUMENT {
  return patchFunctionExit(Enable, FuncId, Sled, Trampolines);
}

bool __xray::patchCustomEvent(const bool Enable, const uint32_t FuncId,
                              const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
  // TODO Implement.
  return false;
}

bool __xray::patchTypedEvent(const bool Enable, const uint32_t FuncId,
                             const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
  // TODO Implement.
  return false;
}

extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
  // TODO this will have to be implemented in the trampoline assembly file.
}

extern "C" void __xray_FunctionTailExit() XRAY_NEVER_INSTRUMENT {
  // For PowerPC, calls to __xray_FunctionEntry and __xray_FunctionExit
  // are statically inserted into the sled. Tail exits are handled like normal
  // function exits. This trampoline is therefore not implemented.
  // This stub is placed here to avoid linking issues.
}