| 1 | //===- FaultMaps.cpp ------------------------------------------------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/CodeGen/FaultMaps.h" |
| 10 | #include "llvm/ADT/Twine.h" |
| 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" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | #define DEBUG_TYPE "faultmaps" |
| 22 | |
| 23 | static const int FaultMapVersion = 1; |
| 24 | const char *FaultMaps::WFMP = "Fault Maps: " ; |
| 25 | |
| 26 | FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {} |
| 27 | |
| 28 | void FaultMaps::recordFaultingOp(FaultKind FaultTy, |
| 29 | const MCSymbol *FaultingLabel, |
| 30 | const MCSymbol *HandlerLabel) { |
| 31 | MCContext &OutContext = AP.OutStreamer->getContext(); |
| 32 | |
| 33 | const MCExpr *FaultingOffset = MCBinaryExpr::createSub( |
| 34 | LHS: MCSymbolRefExpr::create(Symbol: FaultingLabel, Ctx&: OutContext), |
| 35 | RHS: MCSymbolRefExpr::create(Symbol: AP.CurrentFnSymForSize, Ctx&: OutContext), Ctx&: OutContext); |
| 36 | |
| 37 | const MCExpr *HandlerOffset = MCBinaryExpr::createSub( |
| 38 | LHS: MCSymbolRefExpr::create(Symbol: HandlerLabel, Ctx&: OutContext), |
| 39 | RHS: MCSymbolRefExpr::create(Symbol: AP.CurrentFnSymForSize, Ctx&: OutContext), Ctx&: OutContext); |
| 40 | |
| 41 | FunctionInfos[AP.CurrentFnSym].emplace_back(args&: FaultTy, args&: FaultingOffset, |
| 42 | args&: HandlerOffset); |
| 43 | } |
| 44 | |
| 45 | void FaultMaps::serializeToFaultMapSection() { |
| 46 | if (FunctionInfos.empty()) |
| 47 | return; |
| 48 | |
| 49 | MCContext &OutContext = AP.OutStreamer->getContext(); |
| 50 | MCStreamer &OS = *AP.OutStreamer; |
| 51 | |
| 52 | // Create the section. |
| 53 | MCSection *FaultMapSection = |
| 54 | OutContext.getObjectFileInfo()->getFaultMapSection(); |
| 55 | OS.switchSection(Section: FaultMapSection); |
| 56 | |
| 57 | // Emit a dummy symbol to force section inclusion. |
| 58 | OS.emitLabel(Symbol: OutContext.getOrCreateSymbol(Name: Twine("__LLVM_FaultMaps" ))); |
| 59 | |
| 60 | LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n" ); |
| 61 | |
| 62 | // Header |
| 63 | OS.emitIntValue(Value: FaultMapVersion, Size: 1); // Version. |
| 64 | OS.emitIntValue(Value: 0, Size: 1); // Reserved. |
| 65 | OS.emitInt16(Value: 0); // Reserved. |
| 66 | |
| 67 | LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n" ); |
| 68 | OS.emitInt32(Value: FunctionInfos.size()); |
| 69 | |
| 70 | LLVM_DEBUG(dbgs() << WFMP << "functions:\n" ); |
| 71 | |
| 72 | for (const auto &FFI : FunctionInfos) |
| 73 | emitFunctionInfo(FnLabel: FFI.first, FFI: FFI.second); |
| 74 | } |
| 75 | |
| 76 | void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel, |
| 77 | const FunctionFaultInfos &FFI) { |
| 78 | MCStreamer &OS = *AP.OutStreamer; |
| 79 | |
| 80 | LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n" ); |
| 81 | OS.emitSymbolValue(Sym: FnLabel, Size: 8); |
| 82 | |
| 83 | LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n" ); |
| 84 | OS.emitInt32(Value: FFI.size()); |
| 85 | |
| 86 | OS.emitInt32(Value: 0); // Reserved |
| 87 | |
| 88 | for (const auto &Fault : FFI) { |
| 89 | OS.emitInt32(Value: Fault.Kind); |
| 90 | OS.emitValue(Value: Fault.FaultingOffsetExpr, Size: 4); |
| 91 | OS.emitValue(Value: Fault.HandlerOffsetExpr, Size: 4); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) { |
| 96 | switch (FT) { |
| 97 | default: |
| 98 | llvm_unreachable("unhandled fault type!" ); |
| 99 | case FaultMaps::FaultingLoad: |
| 100 | return "FaultingLoad" ; |
| 101 | case FaultMaps::FaultingLoadStore: |
| 102 | return "FaultingLoadStore" ; |
| 103 | case FaultMaps::FaultingStore: |
| 104 | return "FaultingStore" ; |
| 105 | } |
| 106 | } |
| 107 | |