blob: 361558a0e562d36a90bc4b6398013d430874a386 [file] [log] [blame]
Eugene Zelenko90d99202017-02-27 22:45:06 +00001//===- FaultMaps.cpp ------------------------------------------------------===//
Sanjoy Das1991e2a2015-06-15 18:44:08 +00002//
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
Chandler Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/CodeGen/FaultMaps.h"
Eugene Zelenko90d99202017-02-27 22:45:06 +000011#include "llvm/ADT/Twine.h"
Sanjoy Das1991e2a2015-06-15 18:44:08 +000012#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"
Eugene Zelenko90d99202017-02-27 22:45:06 +000018#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/Format.h"
20#include "llvm/Support/raw_ostream.h"
Sanjoy Das1991e2a2015-06-15 18:44:08 +000021
22using namespace llvm;
23
24#define DEBUG_TYPE "faultmaps"
25
26static const int FaultMapVersion = 1;
27const char *FaultMaps::WFMP = "Fault Maps: ";
28
29FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
30
Sanjoy Das18a05df2015-06-15 19:29:44 +000031void FaultMaps::recordFaultingOp(FaultKind FaultTy,
Sanjoy Das1991e2a2015-06-15 18:44:08 +000032 const MCSymbol *HandlerLabel) {
33 MCContext &OutContext = AP.OutStreamer->getContext();
34 MCSymbol *FaultingLabel = OutContext.createTempSymbol();
35
36 AP.OutStreamer->EmitLabel(FaultingLabel);
37
38 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
39 MCSymbolRefExpr::create(FaultingLabel, OutContext),
40 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
41
42 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
43 MCSymbolRefExpr::create(HandlerLabel, OutContext),
44 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
45
46 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
47 HandlerOffset);
48}
49
50void FaultMaps::serializeToFaultMapSection() {
51 if (FunctionInfos.empty())
52 return;
53
54 MCContext &OutContext = AP.OutStreamer->getContext();
55 MCStreamer &OS = *AP.OutStreamer;
56
57 // Create the section.
58 MCSection *FaultMapSection =
59 OutContext.getObjectFileInfo()->getFaultMapSection();
60 OS.SwitchSection(FaultMapSection);
61
62 // Emit a dummy symbol to force section inclusion.
63 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
64
Nicola Zaghen0818e782018-05-14 12:53:11 +000065 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +000066
67 // Header
68 OS.EmitIntValue(FaultMapVersion, 1); // Version.
69 OS.EmitIntValue(0, 1); // Reserved.
70 OS.EmitIntValue(0, 2); // Reserved.
71
Nicola Zaghen0818e782018-05-14 12:53:11 +000072 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +000073 OS.EmitIntValue(FunctionInfos.size(), 4);
74
Nicola Zaghen0818e782018-05-14 12:53:11 +000075 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +000076
77 for (const auto &FFI : FunctionInfos)
78 emitFunctionInfo(FFI.first, FFI.second);
79}
80
81void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
82 const FunctionFaultInfos &FFI) {
83 MCStreamer &OS = *AP.OutStreamer;
84
Nicola Zaghen0818e782018-05-14 12:53:11 +000085 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +000086 OS.EmitSymbolValue(FnLabel, 8);
87
Nicola Zaghen0818e782018-05-14 12:53:11 +000088 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +000089 OS.EmitIntValue(FFI.size(), 4);
90
91 OS.EmitIntValue(0, 4); // Reserved
92
93 for (auto &Fault : FFI) {
Nicola Zaghen0818e782018-05-14 12:53:11 +000094 LLVM_DEBUG(dbgs() << WFMP << " fault type: "
95 << faultTypeToString(Fault.Kind) << "\n");
Sanjoy Das18a05df2015-06-15 19:29:44 +000096 OS.EmitIntValue(Fault.Kind, 4);
Sanjoy Das1991e2a2015-06-15 18:44:08 +000097
Nicola Zaghen0818e782018-05-14 12:53:11 +000098 LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
99 << *Fault.FaultingOffsetExpr << "\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +0000100 OS.EmitValue(Fault.FaultingOffsetExpr, 4);
101
Nicola Zaghen0818e782018-05-14 12:53:11 +0000102 LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
103 << *Fault.HandlerOffsetExpr << "\n");
Sanjoy Das1991e2a2015-06-15 18:44:08 +0000104 OS.EmitValue(Fault.HandlerOffsetExpr, 4);
105 }
106}
107
Sanjoy Das18a05df2015-06-15 19:29:44 +0000108const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
Sanjoy Das1991e2a2015-06-15 18:44:08 +0000109 switch (FT) {
110 default:
111 llvm_unreachable("unhandled fault type!");
Sanjoy Das1991e2a2015-06-15 18:44:08 +0000112 case FaultMaps::FaultingLoad:
113 return "FaultingLoad";
Sanjoy Dasa0be0b12017-02-07 19:19:49 +0000114 case FaultMaps::FaultingLoadStore:
115 return "FaultingLoadStore";
116 case FaultMaps::FaultingStore:
117 return "FaultingStore";
Sanjoy Das1991e2a2015-06-15 18:44:08 +0000118 }
119}
Sanjoy Dasef42e112015-06-23 20:09:03 +0000120
121raw_ostream &llvm::
122operator<<(raw_ostream &OS,
123 const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
124 OS << "Fault kind: "
125 << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
126 << ", faulting PC offset: " << FFI.getFaultingPCOffset()
127 << ", handling PC offset: " << FFI.getHandlerPCOffset();
128 return OS;
129}
130
131raw_ostream &llvm::
132operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
133 OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
134 << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
135 for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
136 OS << FI.getFunctionFaultInfoAt(i) << "\n";
137 return OS;
138}
139
140raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
141 OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
142 OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
143
144 if (FMP.getNumFunctions() == 0)
145 return OS;
146
147 FaultMapParser::FunctionInfoAccessor FI;
148
149 for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
150 FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
151 OS << FI;
152 }
153
154 return OS;
155}