2
0

FaultMaps.cpp 4.7 KB

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