| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 1 | //===- FaultMaps.cpp ------------------------------------------------------===// | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 2 | // | 
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 |  | 
| Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 9 | #include "llvm/CodeGen/FaultMaps.h" | 
| Eugene Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/Twine.h" | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 11 | #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 Zelenko | fa912a7 | 2017-02-27 22:45:06 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ErrorHandling.h" | 
|  | 18 | #include "llvm/Support/Format.h" | 
|  | 19 | #include "llvm/Support/raw_ostream.h" | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | using namespace llvm; | 
|  | 22 |  | 
|  | 23 | #define DEBUG_TYPE "faultmaps" | 
|  | 24 |  | 
|  | 25 | static const int FaultMapVersion = 1; | 
|  | 26 | const char *FaultMaps::WFMP = "Fault Maps: "; | 
|  | 27 |  | 
|  | 28 | FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {} | 
|  | 29 |  | 
| Sanjoy Das | baeb678 | 2015-06-15 19:29:44 +0000 | [diff] [blame] | 30 | void FaultMaps::recordFaultingOp(FaultKind FaultTy, | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 31 | 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 |  | 
|  | 49 | void 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 Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 64 | LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 65 |  | 
|  | 66 | // Header | 
|  | 67 | OS.EmitIntValue(FaultMapVersion, 1); // Version. | 
|  | 68 | OS.EmitIntValue(0, 1);               // Reserved. | 
|  | 69 | OS.EmitIntValue(0, 2);               // Reserved. | 
|  | 70 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 71 | LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 72 | OS.EmitIntValue(FunctionInfos.size(), 4); | 
|  | 73 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 74 | LLVM_DEBUG(dbgs() << WFMP << "functions:\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | for (const auto &FFI : FunctionInfos) | 
|  | 77 | emitFunctionInfo(FFI.first, FFI.second); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel, | 
|  | 81 | const FunctionFaultInfos &FFI) { | 
|  | 82 | MCStreamer &OS = *AP.OutStreamer; | 
|  | 83 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 84 | LLVM_DEBUG(dbgs() << WFMP << "  function addr: " << *FnLabel << "\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 85 | OS.EmitSymbolValue(FnLabel, 8); | 
|  | 86 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 87 | LLVM_DEBUG(dbgs() << WFMP << "  #faulting PCs: " << FFI.size() << "\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 88 | OS.EmitIntValue(FFI.size(), 4); | 
|  | 89 |  | 
|  | 90 | OS.EmitIntValue(0, 4); // Reserved | 
|  | 91 |  | 
|  | 92 | for (auto &Fault : FFI) { | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 93 | LLVM_DEBUG(dbgs() << WFMP << "    fault type: " | 
|  | 94 | << faultTypeToString(Fault.Kind) << "\n"); | 
| Sanjoy Das | baeb678 | 2015-06-15 19:29:44 +0000 | [diff] [blame] | 95 | OS.EmitIntValue(Fault.Kind, 4); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 96 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 97 | LLVM_DEBUG(dbgs() << WFMP << "    faulting PC offset: " | 
|  | 98 | << *Fault.FaultingOffsetExpr << "\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 99 | OS.EmitValue(Fault.FaultingOffsetExpr, 4); | 
|  | 100 |  | 
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 101 | LLVM_DEBUG(dbgs() << WFMP << "    fault handler PC offset: " | 
|  | 102 | << *Fault.HandlerOffsetExpr << "\n"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 103 | OS.EmitValue(Fault.HandlerOffsetExpr, 4); | 
|  | 104 | } | 
|  | 105 | } | 
|  | 106 |  | 
| Sanjoy Das | baeb678 | 2015-06-15 19:29:44 +0000 | [diff] [blame] | 107 | const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) { | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 108 | switch (FT) { | 
|  | 109 | default: | 
|  | 110 | llvm_unreachable("unhandled fault type!"); | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 111 | case FaultMaps::FaultingLoad: | 
|  | 112 | return "FaultingLoad"; | 
| Sanjoy Das | 2f63cbc | 2017-02-07 19:19:49 +0000 | [diff] [blame] | 113 | case FaultMaps::FaultingLoadStore: | 
|  | 114 | return "FaultingLoadStore"; | 
|  | 115 | case FaultMaps::FaultingStore: | 
|  | 116 | return "FaultingStore"; | 
| Sanjoy Das | c63244d | 2015-06-15 18:44:08 +0000 | [diff] [blame] | 117 | } | 
|  | 118 | } | 
| Sanjoy Das | 3f1bc3b | 2015-06-23 20:09:03 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | raw_ostream &llvm:: | 
|  | 121 | operator<<(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 |  | 
|  | 130 | raw_ostream &llvm:: | 
|  | 131 | operator<<(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 |  | 
|  | 139 | raw_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 | } |