1//===- FaultMaps.cpp ------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/CodeGen/FaultMaps.h"
10#include "llvm/ADT/Twine.h"
11#include "llvm/CodeGen/AsmPrinter.h"
12#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
14#include "llvm/MC/MCObjectFileInfo.h"
15#include "llvm/MC/MCStreamer.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/Format.h"
19#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
23#define DEBUG_TYPE "faultmaps"
24
25static const int FaultMapVersion = 1;
26const char *FaultMaps::WFMP = "Fault Maps: ";
27
28FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
29
30void FaultMaps::recordFaultingOp(FaultKind FaultTy,
31                                 const MCSymbol *FaultingLabel,
32                                 const MCSymbol *HandlerLabel) {
33  MCContext &OutContext = AP.OutStreamer->getContext();
34
35  const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
36      MCSymbolRefExpr::create(FaultingLabel, OutContext),
37      MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
38
39  const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
40      MCSymbolRefExpr::create(HandlerLabel, OutContext),
41      MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
42
43  FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
44                                              HandlerOffset);
45}
46
47void FaultMaps::serializeToFaultMapSection() {
48  if (FunctionInfos.empty())
49    return;
50
51  MCContext &OutContext = AP.OutStreamer->getContext();
52  MCStreamer &OS = *AP.OutStreamer;
53
54  // Create the section.
55  MCSection *FaultMapSection =
56      OutContext.getObjectFileInfo()->getFaultMapSection();
57  OS.SwitchSection(FaultMapSection);
58
59  // Emit a dummy symbol to force section inclusion.
60  OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
61
62  LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
63
64  // Header
65  OS.emitIntValue(FaultMapVersion, 1); // Version.
66  OS.emitIntValue(0, 1);               // Reserved.
67  OS.emitInt16(0);                     // Reserved.
68
69  LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
70  OS.emitInt32(FunctionInfos.size());
71
72  LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
73
74  for (const auto &FFI : FunctionInfos)
75    emitFunctionInfo(FFI.first, FFI.second);
76}
77
78void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
79                                 const FunctionFaultInfos &FFI) {
80  MCStreamer &OS = *AP.OutStreamer;
81
82  LLVM_DEBUG(dbgs() << WFMP << "  function addr: " << *FnLabel << "\n");
83  OS.emitSymbolValue(FnLabel, 8);
84
85  LLVM_DEBUG(dbgs() << WFMP << "  #faulting PCs: " << FFI.size() << "\n");
86  OS.emitInt32(FFI.size());
87
88  OS.emitInt32(0); // Reserved
89
90  for (auto &Fault : FFI) {
91    LLVM_DEBUG(dbgs() << WFMP << "    fault type: "
92                      << faultTypeToString(Fault.Kind) << "\n");
93    OS.emitInt32(Fault.Kind);
94
95    LLVM_DEBUG(dbgs() << WFMP << "    faulting PC offset: "
96                      << *Fault.FaultingOffsetExpr << "\n");
97    OS.emitValue(Fault.FaultingOffsetExpr, 4);
98
99    LLVM_DEBUG(dbgs() << WFMP << "    fault handler PC offset: "
100                      << *Fault.HandlerOffsetExpr << "\n");
101    OS.emitValue(Fault.HandlerOffsetExpr, 4);
102  }
103}
104
105const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
106  switch (FT) {
107  default:
108    llvm_unreachable("unhandled fault type!");
109  case FaultMaps::FaultingLoad:
110    return "FaultingLoad";
111  case FaultMaps::FaultingLoadStore:
112    return "FaultingLoadStore";
113  case FaultMaps::FaultingStore:
114    return "FaultingStore";
115  }
116}
117
118raw_ostream &llvm::
119operator<<(raw_ostream &OS,
120           const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
121  OS << "Fault kind: "
122     << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
123     << ", faulting PC offset: " << FFI.getFaultingPCOffset()
124     << ", handling PC offset: " << FFI.getHandlerPCOffset();
125  return OS;
126}
127
128raw_ostream &llvm::
129operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
130  OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
131     << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
132  for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
133    OS << FI.getFunctionFaultInfoAt(i) << "\n";
134  return OS;
135}
136
137raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
138  OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
139  OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
140
141  if (FMP.getNumFunctions() == 0)
142    return OS;
143
144  FaultMapParser::FunctionInfoAccessor FI;
145
146  for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
147    FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
148    OS << FI;
149  }
150
151  return OS;
152}
153