blob: 23560b4cd136af86e650fa7b7a713b6a3d06916d [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,
Philip Reamescf6aafa2019-12-19 12:01:51 -080031 const MCSymbol *FaultingLabel,
Sanjoy Dasc63244d2015-06-15 18:44:08 +000032 const MCSymbol *HandlerLabel) {
33 MCContext &OutContext = AP.OutStreamer->getContext();
Sanjoy Dasc63244d2015-06-15 18:44:08 +000034
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.
Fangrui Song6d2d5892020-02-14 19:21:58 -080060 OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
Sanjoy Dasc63244d2015-06-15 18:44:08 +000061
Nicola Zaghend34e60c2018-05-14 12:53:11 +000062 LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000063
64 // Header
Fangrui Song77497102020-02-14 22:40:47 -080065 OS.emitIntValue(FaultMapVersion, 1); // Version.
66 OS.emitIntValue(0, 1); // Reserved.
Fangrui Song692e0c92020-02-29 08:25:22 -080067 OS.emitInt16(0); // Reserved.
Sanjoy Dasc63244d2015-06-15 18:44:08 +000068
Nicola Zaghend34e60c2018-05-14 12:53:11 +000069 LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
Fangrui Song692e0c92020-02-29 08:25:22 -080070 OS.emitInt32(FunctionInfos.size());
Sanjoy Dasc63244d2015-06-15 18:44:08 +000071
Nicola Zaghend34e60c2018-05-14 12:53:11 +000072 LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
Sanjoy Dasc63244d2015-06-15 18:44:08 +000073
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
Nicola Zaghend34e60c2018-05-14 12:53:11 +000082 LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
Fangrui Song6d2d5892020-02-14 19:21:58 -080083 OS.emitSymbolValue(FnLabel, 8);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000084
Nicola Zaghend34e60c2018-05-14 12:53:11 +000085 LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
Fangrui Song692e0c92020-02-29 08:25:22 -080086 OS.emitInt32(FFI.size());
Sanjoy Dasc63244d2015-06-15 18:44:08 +000087
Fangrui Song692e0c92020-02-29 08:25:22 -080088 OS.emitInt32(0); // Reserved
Sanjoy Dasc63244d2015-06-15 18:44:08 +000089
90 for (auto &Fault : FFI) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +000091 LLVM_DEBUG(dbgs() << WFMP << " fault type: "
92 << faultTypeToString(Fault.Kind) << "\n");
Fangrui Song692e0c92020-02-29 08:25:22 -080093 OS.emitInt32(Fault.Kind);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000094
Nicola Zaghend34e60c2018-05-14 12:53:11 +000095 LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
96 << *Fault.FaultingOffsetExpr << "\n");
Fangrui Song77497102020-02-14 22:40:47 -080097 OS.emitValue(Fault.FaultingOffsetExpr, 4);
Sanjoy Dasc63244d2015-06-15 18:44:08 +000098
Nicola Zaghend34e60c2018-05-14 12:53:11 +000099 LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
100 << *Fault.HandlerOffsetExpr << "\n");
Fangrui Song77497102020-02-14 22:40:47 -0800101 OS.emitValue(Fault.HandlerOffsetExpr, 4);
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000102 }
103}
104
Sanjoy Dasbaeb6782015-06-15 19:29:44 +0000105const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000106 switch (FT) {
107 default:
108 llvm_unreachable("unhandled fault type!");
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000109 case FaultMaps::FaultingLoad:
110 return "FaultingLoad";
Sanjoy Das2f63cbc2017-02-07 19:19:49 +0000111 case FaultMaps::FaultingLoadStore:
112 return "FaultingLoadStore";
113 case FaultMaps::FaultingStore:
114 return "FaultingStore";
Sanjoy Dasc63244d2015-06-15 18:44:08 +0000115 }
116}
Sanjoy Das3f1bc3b2015-06-23 20:09:03 +0000117
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}