1*0b57cec5SDimitry Andric //===- FaultMaps.cpp ------------------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "llvm/CodeGen/FaultMaps.h"
10*0b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
11*0b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
12*0b57cec5SDimitry Andric #include "llvm/MC/MCContext.h"
13*0b57cec5SDimitry Andric #include "llvm/MC/MCExpr.h"
14*0b57cec5SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
15*0b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h"
16*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric using namespace llvm;
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #define DEBUG_TYPE "faultmaps"
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric static const int FaultMapVersion = 1;
24*0b57cec5SDimitry Andric const char *FaultMaps::WFMP = "Fault Maps: ";
25*0b57cec5SDimitry Andric 
FaultMaps(AsmPrinter & AP)26*0b57cec5SDimitry Andric FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
27*0b57cec5SDimitry Andric 
recordFaultingOp(FaultKind FaultTy,const MCSymbol * FaultingLabel,const MCSymbol * HandlerLabel)28*0b57cec5SDimitry Andric void FaultMaps::recordFaultingOp(FaultKind FaultTy,
29*0b57cec5SDimitry Andric                                  const MCSymbol *FaultingLabel,
30*0b57cec5SDimitry Andric                                  const MCSymbol *HandlerLabel) {
31*0b57cec5SDimitry Andric   MCContext &OutContext = AP.OutStreamer->getContext();
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric   const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
34*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(FaultingLabel, OutContext),
35*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
36*0b57cec5SDimitry Andric 
37*0b57cec5SDimitry Andric   const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
38*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(HandlerLabel, OutContext),
39*0b57cec5SDimitry Andric       MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
40*0b57cec5SDimitry Andric 
41*0b57cec5SDimitry Andric   FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
42*0b57cec5SDimitry Andric                                               HandlerOffset);
43*0b57cec5SDimitry Andric }
44*0b57cec5SDimitry Andric 
serializeToFaultMapSection()45*0b57cec5SDimitry Andric void FaultMaps::serializeToFaultMapSection() {
46*0b57cec5SDimitry Andric   if (FunctionInfos.empty())
47*0b57cec5SDimitry Andric     return;
48*0b57cec5SDimitry Andric 
49*0b57cec5SDimitry Andric   MCContext &OutContext = AP.OutStreamer->getContext();
50*0b57cec5SDimitry Andric   MCStreamer &OS = *AP.OutStreamer;
51*0b57cec5SDimitry Andric 
52*0b57cec5SDimitry Andric   // Create the section.
53*0b57cec5SDimitry Andric   MCSection *FaultMapSection =
54*0b57cec5SDimitry Andric       OutContext.getObjectFileInfo()->getFaultMapSection();
55*0b57cec5SDimitry Andric   OS.switchSection(FaultMapSection);
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric   // Emit a dummy symbol to force section inclusion.
58*0b57cec5SDimitry Andric   OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
59*0b57cec5SDimitry Andric 
60*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
61*0b57cec5SDimitry Andric 
62*0b57cec5SDimitry Andric   // Header
63*0b57cec5SDimitry Andric   OS.emitIntValue(FaultMapVersion, 1); // Version.
64*0b57cec5SDimitry Andric   OS.emitIntValue(0, 1);               // Reserved.
65*0b57cec5SDimitry Andric   OS.emitInt16(0);                     // Reserved.
66*0b57cec5SDimitry Andric 
67*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
68*0b57cec5SDimitry Andric   OS.emitInt32(FunctionInfos.size());
69*0b57cec5SDimitry Andric 
70*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
71*0b57cec5SDimitry Andric 
72*0b57cec5SDimitry Andric   for (const auto &FFI : FunctionInfos)
73*0b57cec5SDimitry Andric     emitFunctionInfo(FFI.first, FFI.second);
74*0b57cec5SDimitry Andric }
75*0b57cec5SDimitry Andric 
emitFunctionInfo(const MCSymbol * FnLabel,const FunctionFaultInfos & FFI)76*0b57cec5SDimitry Andric void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
77*0b57cec5SDimitry Andric                                  const FunctionFaultInfos &FFI) {
78*0b57cec5SDimitry Andric   MCStreamer &OS = *AP.OutStreamer;
79*0b57cec5SDimitry Andric 
80*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << WFMP << "  function addr: " << *FnLabel << "\n");
81*0b57cec5SDimitry Andric   OS.emitSymbolValue(FnLabel, 8);
82*0b57cec5SDimitry Andric 
83*0b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << WFMP << "  #faulting PCs: " << FFI.size() << "\n");
84*0b57cec5SDimitry Andric   OS.emitInt32(FFI.size());
85*0b57cec5SDimitry Andric 
86*0b57cec5SDimitry Andric   OS.emitInt32(0); // Reserved
87*0b57cec5SDimitry Andric 
88*0b57cec5SDimitry Andric   for (const auto &Fault : FFI) {
89*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << WFMP << "    fault type: "
90*0b57cec5SDimitry Andric                       << faultTypeToString(Fault.Kind) << "\n");
91*0b57cec5SDimitry Andric     OS.emitInt32(Fault.Kind);
92*0b57cec5SDimitry Andric 
93*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << WFMP << "    faulting PC offset: "
94*0b57cec5SDimitry Andric                       << *Fault.FaultingOffsetExpr << "\n");
95*0b57cec5SDimitry Andric     OS.emitValue(Fault.FaultingOffsetExpr, 4);
96*0b57cec5SDimitry Andric 
97*0b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << WFMP << "    fault handler PC offset: "
98*0b57cec5SDimitry Andric                       << *Fault.HandlerOffsetExpr << "\n");
99*0b57cec5SDimitry Andric     OS.emitValue(Fault.HandlerOffsetExpr, 4);
100*0b57cec5SDimitry Andric   }
101*0b57cec5SDimitry Andric }
102*0b57cec5SDimitry Andric 
faultTypeToString(FaultMaps::FaultKind FT)103*0b57cec5SDimitry Andric const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
104*0b57cec5SDimitry Andric   switch (FT) {
105*0b57cec5SDimitry Andric   default:
106*0b57cec5SDimitry Andric     llvm_unreachable("unhandled fault type!");
107*0b57cec5SDimitry Andric   case FaultMaps::FaultingLoad:
108*0b57cec5SDimitry Andric     return "FaultingLoad";
109*0b57cec5SDimitry Andric   case FaultMaps::FaultingLoadStore:
110*0b57cec5SDimitry Andric     return "FaultingLoadStore";
111*0b57cec5SDimitry Andric   case FaultMaps::FaultingStore:
112*0b57cec5SDimitry Andric     return "FaultingStore";
113*0b57cec5SDimitry Andric   }
114*0b57cec5SDimitry Andric }
115*0b57cec5SDimitry Andric