FaultMaps.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #include "llvm/CodeGen/FaultMaps.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/CodeGen/AsmPrinter.h"
  11. #include "llvm/MC/MCContext.h"
  12. #include "llvm/MC/MCExpr.h"
  13. #include "llvm/MC/MCObjectFileInfo.h"
  14. #include "llvm/MC/MCStreamer.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/Format.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "faultmaps"
  21. static const int FaultMapVersion = 1;
  22. const char *FaultMaps::WFMP = "Fault Maps: ";
  23. FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
  24. void FaultMaps::recordFaultingOp(FaultKind FaultTy,
  25. const MCSymbol *HandlerLabel) {
  26. MCContext &OutContext = AP.OutStreamer->getContext();
  27. MCSymbol *FaultingLabel = OutContext.createTempSymbol();
  28. AP.OutStreamer->EmitLabel(FaultingLabel);
  29. const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
  30. MCSymbolRefExpr::create(FaultingLabel, OutContext),
  31. MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
  32. const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
  33. MCSymbolRefExpr::create(HandlerLabel, OutContext),
  34. MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
  35. FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
  36. HandlerOffset);
  37. }
  38. void FaultMaps::serializeToFaultMapSection() {
  39. if (FunctionInfos.empty())
  40. return;
  41. MCContext &OutContext = AP.OutStreamer->getContext();
  42. MCStreamer &OS = *AP.OutStreamer;
  43. // Create the section.
  44. MCSection *FaultMapSection =
  45. OutContext.getObjectFileInfo()->getFaultMapSection();
  46. OS.SwitchSection(FaultMapSection);
  47. // Emit a dummy symbol to force section inclusion.
  48. OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
  49. LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
  50. // Header
  51. OS.EmitIntValue(FaultMapVersion, 1); // Version.
  52. OS.EmitIntValue(0, 1); // Reserved.
  53. OS.EmitIntValue(0, 2); // Reserved.
  54. LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
  55. OS.EmitIntValue(FunctionInfos.size(), 4);
  56. LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
  57. for (const auto &FFI : FunctionInfos)
  58. emitFunctionInfo(FFI.first, FFI.second);
  59. }
  60. void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
  61. const FunctionFaultInfos &FFI) {
  62. MCStreamer &OS = *AP.OutStreamer;
  63. LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
  64. OS.EmitSymbolValue(FnLabel, 8);
  65. LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
  66. OS.EmitIntValue(FFI.size(), 4);
  67. OS.EmitIntValue(0, 4); // Reserved
  68. for (auto &Fault : FFI) {
  69. LLVM_DEBUG(dbgs() << WFMP << " fault type: "
  70. << faultTypeToString(Fault.Kind) << "\n");
  71. OS.EmitIntValue(Fault.Kind, 4);
  72. LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
  73. << *Fault.FaultingOffsetExpr << "\n");
  74. OS.EmitValue(Fault.FaultingOffsetExpr, 4);
  75. LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
  76. << *Fault.HandlerOffsetExpr << "\n");
  77. OS.EmitValue(Fault.HandlerOffsetExpr, 4);
  78. }
  79. }
  80. const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
  81. switch (FT) {
  82. default:
  83. llvm_unreachable("unhandled fault type!");
  84. case FaultMaps::FaultingLoad:
  85. return "FaultingLoad";
  86. case FaultMaps::FaultingLoadStore:
  87. return "FaultingLoadStore";
  88. case FaultMaps::FaultingStore:
  89. return "FaultingStore";
  90. }
  91. }
  92. raw_ostream &llvm::
  93. operator<<(raw_ostream &OS,
  94. const FaultMapParser::FunctionFaultInfoAccessor &FFI) {
  95. OS << "Fault kind: "
  96. << FaultMaps::faultTypeToString((FaultMaps::FaultKind)FFI.getFaultKind())
  97. << ", faulting PC offset: " << FFI.getFaultingPCOffset()
  98. << ", handling PC offset: " << FFI.getHandlerPCOffset();
  99. return OS;
  100. }
  101. raw_ostream &llvm::
  102. operator<<(raw_ostream &OS, const FaultMapParser::FunctionInfoAccessor &FI) {
  103. OS << "FunctionAddress: " << format_hex(FI.getFunctionAddr(), 8)
  104. << ", NumFaultingPCs: " << FI.getNumFaultingPCs() << "\n";
  105. for (unsigned i = 0, e = FI.getNumFaultingPCs(); i != e; ++i)
  106. OS << FI.getFunctionFaultInfoAt(i) << "\n";
  107. return OS;
  108. }
  109. raw_ostream &llvm::operator<<(raw_ostream &OS, const FaultMapParser &FMP) {
  110. OS << "Version: " << format_hex(FMP.getFaultMapVersion(), 2) << "\n";
  111. OS << "NumFunctions: " << FMP.getNumFunctions() << "\n";
  112. if (FMP.getNumFunctions() == 0)
  113. return OS;
  114. FaultMapParser::FunctionInfoAccessor FI;
  115. for (unsigned i = 0, e = FMP.getNumFunctions(); i != e; ++i) {
  116. FI = (i == 0) ? FMP.getFirstFunctionInfo() : FI.getNextFunctionInfo();
  117. OS << FI;
  118. }
  119. return OS;
  120. }