• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===---------------------------- FaultMaps.cpp ---------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/CodeGen/FaultMaps.h"
11 
12 #include "llvm/CodeGen/AsmPrinter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCObjectFileInfo.h"
16 #include "llvm/MC/MCStreamer.h"
17 #include "llvm/Support/Debug.h"
18 
19 using namespace llvm;
20 
21 #define DEBUG_TYPE "faultmaps"
22 
23 static const int FaultMapVersion = 1;
24 const char *FaultMaps::WFMP = "Fault Maps: ";
25 
FaultMaps(AsmPrinter & AP)26 FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
27 
recordFaultingOp(FaultKind FaultTy,const MCSymbol * HandlerLabel)28 void FaultMaps::recordFaultingOp(FaultKind FaultTy,
29                                  const MCSymbol *HandlerLabel) {
30   MCContext &OutContext = AP.OutStreamer->getContext();
31   MCSymbol *FaultingLabel = OutContext.createTempSymbol();
32 
33   AP.OutStreamer->EmitLabel(FaultingLabel);
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 
serializeToFaultMapSection()47 void 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   DEBUG(dbgs() << "********** Fault Map Output **********\n");
63 
64   // Header
65   OS.EmitIntValue(FaultMapVersion, 1); // Version.
66   OS.EmitIntValue(0, 1);               // Reserved.
67   OS.EmitIntValue(0, 2);               // Reserved.
68 
69   DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
70   OS.EmitIntValue(FunctionInfos.size(), 4);
71 
72   DEBUG(dbgs() << WFMP << "functions:\n");
73 
74   for (const auto &FFI : FunctionInfos)
75     emitFunctionInfo(FFI.first, FFI.second);
76 }
77 
emitFunctionInfo(const MCSymbol * FnLabel,const FunctionFaultInfos & FFI)78 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
79                                  const FunctionFaultInfos &FFI) {
80   MCStreamer &OS = *AP.OutStreamer;
81 
82   DEBUG(dbgs() << WFMP << "  function addr: " << *FnLabel << "\n");
83   OS.EmitSymbolValue(FnLabel, 8);
84 
85   DEBUG(dbgs() << WFMP << "  #faulting PCs: " << FFI.size() << "\n");
86   OS.EmitIntValue(FFI.size(), 4);
87 
88   OS.EmitIntValue(0, 4); // Reserved
89 
90   for (auto &Fault : FFI) {
91     DEBUG(dbgs() << WFMP << "    fault type: "
92           << faultTypeToString(Fault.Kind) << "\n");
93     OS.EmitIntValue(Fault.Kind, 4);
94 
95     DEBUG(dbgs() << WFMP << "    faulting PC offset: "
96           << *Fault.FaultingOffsetExpr << "\n");
97     OS.EmitValue(Fault.FaultingOffsetExpr, 4);
98 
99     DEBUG(dbgs() << WFMP << "    fault handler PC offset: "
100           << *Fault.HandlerOffsetExpr << "\n");
101     OS.EmitValue(Fault.HandlerOffsetExpr, 4);
102   }
103 }
104 
105 
faultTypeToString(FaultMaps::FaultKind FT)106 const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
107   switch (FT) {
108   default:
109     llvm_unreachable("unhandled fault type!");
110 
111   case FaultMaps::FaultingLoad:
112     return "FaultingLoad";
113   }
114 }
115 
116 raw_ostream &llvm::
operator <<(raw_ostream & OS,const FaultMapParser::FunctionFaultInfoAccessor & FFI)117 operator<<(raw_ostream &OS,
118            const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
119   OS << "Fault kind: "
120      << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
121      << ", faulting PC offset: " << FFI.getFaultingPCOffset()
122      << ", handling PC offset: " << FFI.getHandlerPCOffset();
123   return OS;
124 }
125 
126 raw_ostream &llvm::
operator <<(raw_ostream & OS,const FaultMapParser::FunctionInfoAccessor & FI)127 operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
128   OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
129      << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
130   for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
131     OS << FI.getFunctionFaultInfoAt(i) << "\n";
132   return OS;
133 }
134 
operator <<(raw_ostream & OS,const FaultMapParser & FMP)135 raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
136   OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
137   OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
138 
139   if (FMP.getNumFunctions() == 0)
140     return OS;
141 
142   FaultMapParser::FunctionInfoAccessor FI;
143 
144   for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
145     FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
146     OS << FI;
147   }
148 
149   return OS;
150 }
151