blob: 600f72d320eb590818e1a4a9f5fa210eb0bdaf86 [file] [log] [blame]
Eugene Zelenkofa912a72017-02-27 22:45:06 +00001//===- FaultMaps.cpp ------------------------------------------------------===//
Sanjoy Dasc63244d2015-06-15 18:44:08 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Sanjoy Dasc63244d2015-06-15 18:44:08 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/CodeGen/FaultMaps.h"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000010#include "llvm/ADT/Twine.h"
Sanjoy Dasc63244d2015-06-15 18:44:08 +000011#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"
Eugene Zelenkofa912a72017-02-27 22:45:06 +000017#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/Format.h"
19#include "llvm/Support/raw_ostream.h"
Sanjoy Dasc63244d2015-06-15 18:44:08 +000020
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
Sanjoy Dasbaeb6782015-06-15 19:29:44 +000030void FaultMaps::recordFaultingOp(FaultKind FaultTy,
Sanjoy Dasc63244d2015-06-15 18:44:08 +000031 const MCSymbol *HandlerLabel) {
32 MCContext &OutContext = AP.OutStreamer->getContext();
33 MCSymbol *FaultingLabel = OutContext.createTempSymbol();
34
35 AP.OutStreamer->EmitLabel(FaultingLabel);
36
37 const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
38 MCSymbolRefExpr::create(FaultingLabel, OutContext),
39 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
40
41 const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
42 MCSymbolRefExpr::create(HandlerLabel, OutContext),
43 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
44
45 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
46 HandlerOffset);
47}
48
49void FaultMaps::serializeToFaultMapSection() {
50 if (FunctionInfos.empty())
51 return;
52
53 MCContext &OutContext = AP.OutStreamer->getContext();
54 MCStreamer &OS = *AP.OutStreamer;
55
56 // Create the section.
57 MCSection *FaultMapSection =
58 OutContext.getObjectFileInfo()->getFaultMapSection();
59 OS.SwitchSection(FaultMapSection);
60
61 // Emit a dummy symbol to force section inclusion.
62 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
63
Nicola Zaghend34e60c2018-05-14 12:53:11 +000064 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000065
66 // Header
67 OS.EmitIntValue(FaultMapVersion, 1); // Version.
68 OS.EmitIntValue(0, 1); // Reserved.
69 OS.EmitIntValue(0, 2); // Reserved.
70
Nicola Zaghend34e60c2018-05-14 12:53:11 +000071 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000072 OS.EmitIntValue(FunctionInfos.size(), 4);
73
Nicola Zaghend34e60c2018-05-14 12:53:11 +000074 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000075
76 for (const auto &FFI : FunctionInfos)
77 emitFunctionInfo(FFI.first, FFI.second);
78}
79
80void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
81 const FunctionFaultInfos &FFI) {
82 MCStreamer &OS = *AP.OutStreamer;
83
Nicola Zaghend34e60c2018-05-14 12:53:11 +000084 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000085 OS.EmitSymbolValue(FnLabel, 8);
86
Nicola Zaghend34e60c2018-05-14 12:53:11 +000087 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000088 OS.EmitIntValue(FFI.size(), 4);
89
90 OS.EmitIntValue(0, 4); // Reserved
91
92 for (auto &Fault : FFI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000093 LLVM_DEBUG(dbgs() << WFMP << " fault type: "
94 << faultTypeToString(Fault.Kind) << "\n");
Sanjoy Dasbaeb6782015-06-15 19:29:44 +000095 OS.EmitIntValue(Fault.Kind, 4);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000096
Nicola Zaghend34e60c2018-05-14 12:53:11 +000097 LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
98 << *Fault.FaultingOffsetExpr << "\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000099 OS.EmitValue(Fault.FaultingOffsetExpr, 4);
100
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000101 LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
102 << *Fault.HandlerOffsetExpr << "\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000103 OS.EmitValue(Fault.HandlerOffsetExpr, 4);
104 }
105}
106
Sanjoy Dasbaeb6782015-06-15 19:29:44 +0000107const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000108 switch (FT) {
109 default:
110 llvm_unreachable("unhandled fault type!");
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000111 case FaultMaps::FaultingLoad:
112 return "FaultingLoad";
Sanjoy Das2f63cbc2017-02-07 19:19:49 +0000113 case FaultMaps::FaultingLoadStore:
114 return "FaultingLoadStore";
115 case FaultMaps::FaultingStore:
116 return "FaultingStore";
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000117 }
118}
Sanjoy Das3f1bc3b2015-06-23 20:09:03 +0000119
120raw_ostream &llvm::
121operator<<(raw_ostream &OS,
122 const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
123 OS << "Fault kind: "
124 << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
125 << ", faulting PC offset: " << FFI.getFaultingPCOffset()
126 << ", handling PC offset: " << FFI.getHandlerPCOffset();
127 return OS;
128}
129
130raw_ostream &llvm::
131operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
132 OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
133 << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
134 for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
135 OS << FI.getFunctionFaultInfoAt(i) << "\n";
136 return OS;
137}
138
139raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
140 OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
141 OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
142
143 if (FMP.getNumFunctions() == 0)
144 return OS;
145
146 FaultMapParser::FunctionInfoAccessor FI;
147
148 for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
149 FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
150 OS << FI;
151 }
152
153 return OS;
154}