2017-02-28 06:45:06 +08:00
|
|
|
//===- FaultMaps.cpp ------------------------------------------------------===//
|
2015-06-16 02:44:08 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-06-16 02:44:08 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/FaultMaps.h"
|
2017-02-28 06:45:06 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-06-16 02:44:08 +08:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2017-02-28 06:45:06 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2015-06-16 02:44:08 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "faultmaps"
|
|
|
|
|
|
|
|
static const int FaultMapVersion = 1;
|
|
|
|
const char *FaultMaps::WFMP = "Fault Maps: ";
|
|
|
|
|
|
|
|
FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {}
|
|
|
|
|
2015-06-16 03:29:44 +08:00
|
|
|
void FaultMaps::recordFaultingOp(FaultKind FaultTy,
|
2019-12-20 04:01:51 +08:00
|
|
|
const MCSymbol *FaultingLabel,
|
2015-06-16 02:44:08 +08:00
|
|
|
const MCSymbol *HandlerLabel) {
|
|
|
|
MCContext &OutContext = AP.OutStreamer->getContext();
|
|
|
|
|
|
|
|
const MCExpr *FaultingOffset = MCBinaryExpr::createSub(
|
|
|
|
MCSymbolRefExpr::create(FaultingLabel, OutContext),
|
|
|
|
MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
|
|
|
|
|
|
|
|
const MCExpr *HandlerOffset = MCBinaryExpr::createSub(
|
|
|
|
MCSymbolRefExpr::create(HandlerLabel, OutContext),
|
|
|
|
MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext);
|
|
|
|
|
|
|
|
FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset,
|
|
|
|
HandlerOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultMaps::serializeToFaultMapSection() {
|
|
|
|
if (FunctionInfos.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCContext &OutContext = AP.OutStreamer->getContext();
|
|
|
|
MCStreamer &OS = *AP.OutStreamer;
|
|
|
|
|
|
|
|
// Create the section.
|
|
|
|
MCSection *FaultMapSection =
|
|
|
|
OutContext.getObjectFileInfo()->getFaultMapSection();
|
|
|
|
OS.SwitchSection(FaultMapSection);
|
|
|
|
|
|
|
|
// Emit a dummy symbol to force section inclusion.
|
2020-02-15 11:21:58 +08:00
|
|
|
OS.emitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps")));
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** Fault Map Output **********\n");
|
2015-06-16 02:44:08 +08:00
|
|
|
|
|
|
|
// Header
|
2020-02-15 14:40:47 +08:00
|
|
|
OS.emitIntValue(FaultMapVersion, 1); // Version.
|
|
|
|
OS.emitIntValue(0, 1); // Reserved.
|
2020-03-01 00:25:22 +08:00
|
|
|
OS.emitInt16(0); // Reserved.
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n");
|
2020-03-01 00:25:22 +08:00
|
|
|
OS.emitInt32(FunctionInfos.size());
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << "functions:\n");
|
2015-06-16 02:44:08 +08:00
|
|
|
|
|
|
|
for (const auto &FFI : FunctionInfos)
|
|
|
|
emitFunctionInfo(FFI.first, FFI.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel,
|
|
|
|
const FunctionFaultInfos &FFI) {
|
|
|
|
MCStreamer &OS = *AP.OutStreamer;
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n");
|
2020-02-15 11:21:58 +08:00
|
|
|
OS.emitSymbolValue(FnLabel, 8);
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n");
|
2020-03-01 00:25:22 +08:00
|
|
|
OS.emitInt32(FFI.size());
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2020-03-01 00:25:22 +08:00
|
|
|
OS.emitInt32(0); // Reserved
|
2015-06-16 02:44:08 +08:00
|
|
|
|
|
|
|
for (auto &Fault : FFI) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << " fault type: "
|
|
|
|
<< faultTypeToString(Fault.Kind) << "\n");
|
2020-03-01 00:25:22 +08:00
|
|
|
OS.emitInt32(Fault.Kind);
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << " faulting PC offset: "
|
|
|
|
<< *Fault.FaultingOffsetExpr << "\n");
|
2020-02-15 14:40:47 +08:00
|
|
|
OS.emitValue(Fault.FaultingOffsetExpr, 4);
|
2015-06-16 02:44:08 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << WFMP << " fault handler PC offset: "
|
|
|
|
<< *Fault.HandlerOffsetExpr << "\n");
|
2020-02-15 14:40:47 +08:00
|
|
|
OS.emitValue(Fault.HandlerOffsetExpr, 4);
|
2015-06-16 02:44:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-16 03:29:44 +08:00
|
|
|
const char *FaultMaps::faultTypeToString(FaultMaps::FaultKind FT) {
|
2015-06-16 02:44:08 +08:00
|
|
|
switch (FT) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unhandled fault type!");
|
|
|
|
case FaultMaps::FaultingLoad:
|
|
|
|
return "FaultingLoad";
|
2017-02-08 03:19:49 +08:00
|
|
|
case FaultMaps::FaultingLoadStore:
|
|
|
|
return "FaultingLoadStore";
|
|
|
|
case FaultMaps::FaultingStore:
|
|
|
|
return "FaultingStore";
|
2015-06-16 02:44:08 +08:00
|
|
|
}
|
|
|
|
}
|