171d5a254SDimitry Andric //===- FaultMaps.cpp ------------------------------------------------------===//
23a0822f0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63a0822f0SDimitry Andric //
73a0822f0SDimitry Andric //===----------------------------------------------------------------------===//
83a0822f0SDimitry Andric
97ab83427SDimitry Andric #include "llvm/CodeGen/FaultMaps.h"
1071d5a254SDimitry Andric #include "llvm/ADT/Twine.h"
113a0822f0SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h"
123a0822f0SDimitry Andric #include "llvm/MC/MCContext.h"
133a0822f0SDimitry Andric #include "llvm/MC/MCExpr.h"
143a0822f0SDimitry Andric #include "llvm/MC/MCObjectFileInfo.h"
153a0822f0SDimitry Andric #include "llvm/MC/MCStreamer.h"
163a0822f0SDimitry Andric #include "llvm/Support/Debug.h"
1771d5a254SDimitry Andric #include "llvm/Support/ErrorHandling.h"
183a0822f0SDimitry Andric
193a0822f0SDimitry Andric using namespace llvm;
203a0822f0SDimitry Andric
213a0822f0SDimitry Andric #define DEBUG_TYPE "faultmaps"
223a0822f0SDimitry Andric
233a0822f0SDimitry Andric static const int FaultMapVersion = 1;
243a0822f0SDimitry Andric const char *FaultMaps::WFMP = "Fault Maps: ";
253a0822f0SDimitry Andric
FaultMaps(AsmPrinter & AP)263a0822f0SDimitry Andric FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
273a0822f0SDimitry Andric
recordFaultingOp(FaultKind FaultTy,const MCSymbol * FaultingLabel,const MCSymbol * HandlerLabel)283a0822f0SDimitry Andric void FaultMaps::recordFaultingOp(FaultKind FaultTy,
29706b4fc4SDimitry Andric const MCSymbol *FaultingLabel,
303a0822f0SDimitry Andric const MCSymbol *HandlerLabel) {
313a0822f0SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext();
323a0822f0SDimitry Andric
333a0822f0SDimitry Andric const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
343a0822f0SDimitry Andric MCSymbolRefExpr::create(FaultingLabel, OutContext),
353a0822f0SDimitry Andric MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
363a0822f0SDimitry Andric
373a0822f0SDimitry Andric const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
383a0822f0SDimitry Andric MCSymbolRefExpr::create(HandlerLabel, OutContext),
393a0822f0SDimitry Andric MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
403a0822f0SDimitry Andric
413a0822f0SDimitry Andric FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
423a0822f0SDimitry Andric HandlerOffset);
433a0822f0SDimitry Andric }
443a0822f0SDimitry Andric
serializeToFaultMapSection()453a0822f0SDimitry Andric void FaultMaps::serializeToFaultMapSection() {
463a0822f0SDimitry Andric if (FunctionInfos.empty())
473a0822f0SDimitry Andric return;
483a0822f0SDimitry Andric
493a0822f0SDimitry Andric MCContext &OutContext = AP.OutStreamer->getContext();
503a0822f0SDimitry Andric MCStreamer &OS = *AP.OutStreamer;
513a0822f0SDimitry Andric
523a0822f0SDimitry Andric // Create the section.
533a0822f0SDimitry Andric MCSection *FaultMapSection =
543a0822f0SDimitry Andric OutContext.getObjectFileInfo()->getFaultMapSection();
55145449b1SDimitry Andric OS.switchSection(FaultMapSection);
563a0822f0SDimitry Andric
573a0822f0SDimitry Andric // Emit a dummy symbol to force section inclusion.
58cfca06d7SDimitry Andric OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
593a0822f0SDimitry Andric
60eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
613a0822f0SDimitry Andric
623a0822f0SDimitry Andric // Header
63cfca06d7SDimitry Andric OS.emitIntValue(FaultMapVersion, 1); // Version.
64cfca06d7SDimitry Andric OS.emitIntValue(0, 1); // Reserved.
65cfca06d7SDimitry Andric OS.emitInt16(0); // Reserved.
663a0822f0SDimitry Andric
67eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
68cfca06d7SDimitry Andric OS.emitInt32(FunctionInfos.size());
693a0822f0SDimitry Andric
70eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
713a0822f0SDimitry Andric
723a0822f0SDimitry Andric for (const auto &FFI : FunctionInfos)
733a0822f0SDimitry Andric emitFunctionInfo(FFI.first, FFI.second);
743a0822f0SDimitry Andric }
753a0822f0SDimitry Andric
emitFunctionInfo(const MCSymbol * FnLabel,const FunctionFaultInfos & FFI)763a0822f0SDimitry Andric void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
773a0822f0SDimitry Andric const FunctionFaultInfos &FFI) {
783a0822f0SDimitry Andric MCStreamer &OS = *AP.OutStreamer;
793a0822f0SDimitry Andric
80eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
81cfca06d7SDimitry Andric OS.emitSymbolValue(FnLabel, 8);
823a0822f0SDimitry Andric
83eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
84cfca06d7SDimitry Andric OS.emitInt32(FFI.size());
853a0822f0SDimitry Andric
86cfca06d7SDimitry Andric OS.emitInt32(0); // Reserved
873a0822f0SDimitry Andric
884b4fe385SDimitry Andric for (const auto &Fault : FFI) {
89eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << " fault type: "
903a0822f0SDimitry Andric << faultTypeToString(Fault.Kind) << "\n");
91cfca06d7SDimitry Andric OS.emitInt32(Fault.Kind);
923a0822f0SDimitry Andric
93eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
943a0822f0SDimitry Andric << *Fault.FaultingOffsetExpr << "\n");
95cfca06d7SDimitry Andric OS.emitValue(Fault.FaultingOffsetExpr, 4);
963a0822f0SDimitry Andric
97eb11fae6SDimitry Andric LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
983a0822f0SDimitry Andric << *Fault.HandlerOffsetExpr << "\n");
99cfca06d7SDimitry Andric OS.emitValue(Fault.HandlerOffsetExpr, 4);
1003a0822f0SDimitry Andric }
1013a0822f0SDimitry Andric }
1023a0822f0SDimitry Andric
faultTypeToString(FaultMaps::FaultKind FT)1033a0822f0SDimitry Andric const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
1043a0822f0SDimitry Andric switch (FT) {
1053a0822f0SDimitry Andric default:
1063a0822f0SDimitry Andric llvm_unreachable("unhandled fault type!");
1073a0822f0SDimitry Andric case FaultMaps::FaultingLoad:
1083a0822f0SDimitry Andric return "FaultingLoad";
10971d5a254SDimitry Andric case FaultMaps::FaultingLoadStore:
11071d5a254SDimitry Andric return "FaultingLoadStore";
11171d5a254SDimitry Andric case FaultMaps::FaultingStore:
11271d5a254SDimitry Andric return "FaultingStore";
1133a0822f0SDimitry Andric }
1143a0822f0SDimitry Andric }
115