Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Push 2024 11 27 #885

Merged
merged 16 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 38 additions & 8 deletions src/mmio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ WinPmemMMIORange::WinPmemMMIORange(uint64 baseAddr_, uint64 /* size_ */, bool re
mutex.unlock();
}

MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_, bool silent)
MMIORange::MMIORange(const uint64 baseAddr_, const uint64 size_, const bool readonly_, const bool silent_, const int core) :
silent(silent_)
{
auto hDriver = openMSRDriver();
if (hDriver != INVALID_HANDLE_VALUE)
Expand All @@ -98,7 +99,7 @@ MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_, bool silent
CloseHandle(hDriver);
if (status == TRUE && reslength == sizeof(uint64) && result == 1)
{
impl = std::make_shared<OwnMMIORange>(baseAddr_, size_, readonly_);
impl = std::make_shared<OwnMMIORange>(baseAddr_, size_, readonly_, core);
return;
}
else
Expand All @@ -109,11 +110,18 @@ MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_, bool silent
}
}
}

if (core >= 0)
{
throw std::runtime_error("WinPmem does not support core affinity");
}
impl = std::make_shared<WinPmemMMIORange>(baseAddr_, size_, readonly_);
}

OwnMMIORange::OwnMMIORange(uint64 baseAddr_, uint64 size_, bool /* readonly_ */)
OwnMMIORange::OwnMMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool /* readonly_ */,
const int core_) :
core(core_)
{
hDriver = openMSRDriver();
MMAP_Request req{};
Expand All @@ -132,20 +140,24 @@ OwnMMIORange::OwnMMIORange(uint64 baseAddr_, uint64 size_, bool /* readonly_ */)

uint32 OwnMMIORange::read32(uint64 offset)
{
CoreAffinityScope _(core);
return *((uint32*)(mmapAddr + offset));
}

uint64 OwnMMIORange::read64(uint64 offset)
{
CoreAffinityScope _(core);
return *((uint64*)(mmapAddr + offset));
}

void OwnMMIORange::write32(uint64 offset, uint32 val)
{
CoreAffinityScope _(core);
*((uint32*)(mmapAddr + offset)) = val;
}
void OwnMMIORange::write64(uint64 offset, uint64 val)
{
CoreAffinityScope _(core);
*((uint64*)(mmapAddr + offset)) = val;
}

Expand All @@ -164,10 +176,16 @@ OwnMMIORange::~OwnMMIORange()

#include "PCIDriverInterface.h"

MMIORange::MMIORange(uint64 physical_address, uint64 size_, bool, bool silent) :
MMIORange::MMIORange(const uint64 physical_address, const uint64 size_, const bool, const bool silent_, const int core_) :
mmapAddr(NULL),
size(size_)
size(size_),
silent(silent_),
core(core_)
{
if (core_ >= 0)
{
throw std::runtime_error("MMIORange on MacOSX does not support core affinity");
}
if (size > 4096)
{
if (!silent)
Expand All @@ -183,13 +201,15 @@ MMIORange::MMIORange(uint64 physical_address, uint64 size_, bool, bool silent) :

uint32 MMIORange::read32(uint64 offset)
{
warnAlignment<4>("MMIORange::read32", silent, offset);
uint32 val = 0;
PCIDriver_readMemory32((uint8_t *)mmapAddr + offset, &val);
return val;
}

uint64 MMIORange::read64(uint64 offset)
{
warnAlignment<8>("MMIORange::read64", silent, offset);
uint64 val = 0;
PCIDriver_readMemory64((uint8_t *)mmapAddr + offset, &val);
return val;
Expand All @@ -211,11 +231,13 @@ MMIORange::~MMIORange()

#elif defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)

MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_, bool silent) :
MMIORange::MMIORange(const uint64 baseAddr_, const uint64 size_, const bool readonly_, const bool silent_, const int core_) :
fd(-1),
mmapAddr(NULL),
size(size_),
readonly(readonly_)
readonly(readonly_),
silent(silent_),
core(core_)
{
const int oflag = readonly ? O_RDONLY : O_RDWR;
int handle = ::open("/dev/mem", oflag);
Expand Down Expand Up @@ -252,16 +274,22 @@ MMIORange::MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_, bool silent

uint32 MMIORange::read32(uint64 offset)
{
warnAlignment<4>("MMIORange::read32", silent, offset);
CoreAffinityScope _(core);
return *((uint32 *)(mmapAddr + offset));
}

uint64 MMIORange::read64(uint64 offset)
{
warnAlignment<8>("MMIORange::read64", silent, offset);
CoreAffinityScope _(core);
return *((uint64 *)(mmapAddr + offset));
}

void MMIORange::write32(uint64 offset, uint32 val)
{
warnAlignment<4>("MMIORange::write32", silent, offset);
CoreAffinityScope _(core);
if (readonly)
{
std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
Expand All @@ -271,6 +299,8 @@ void MMIORange::write32(uint64 offset, uint32 val)
}
void MMIORange::write64(uint64 offset, uint64 val)
{
warnAlignment<8>("MMIORange::write64", silent, offset);
CoreAffinityScope _(core);
if (readonly)
{
std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
Expand Down
38 changes: 35 additions & 3 deletions src/mmio.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,23 @@
#endif

#include "mutex.h"
#include "utils.h"
#include <memory>

namespace pcm {

class CoreAffinityScope // sets core affinity if core >= 0, nop otherwise
{
std::shared_ptr<TemporalThreadAffinity> affinity{nullptr};
CoreAffinityScope(const CoreAffinityScope&) = delete;
CoreAffinityScope& operator = (const CoreAffinityScope&) = delete;
public:
CoreAffinityScope(const int core)
: affinity((core >= 0) ? std::make_shared<TemporalThreadAffinity>(core) : nullptr)
{
}
};

#ifdef _MSC_VER

class MMIORangeInterface
Expand Down Expand Up @@ -98,10 +111,14 @@ class OwnMMIORange : public MMIORangeInterface
{
HANDLE hDriver;
char * mmapAddr;
const int core;
OwnMMIORange(const OwnMMIORange&) = delete;
OwnMMIORange& operator = (const OwnMMIORange&) = delete;
public:
OwnMMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
OwnMMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const int core_ = -1);
uint32 read32(uint64 offset);
uint64 read64(uint64 offset);
void write32(uint64 offset, uint32 val);
Expand All @@ -112,24 +129,33 @@ class OwnMMIORange : public MMIORangeInterface
class MMIORange
{
std::shared_ptr<MMIORangeInterface> impl;
const bool silent;
MMIORange(const MMIORange &) = delete;
MMIORange & operator = (const MMIORange &) = delete;
public:
MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true, bool silent = false);
MMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const bool silent_ = false,
const int core = -1);
uint32 read32(uint64 offset)
{
warnAlignment<4>("MMIORange::read32", silent, offset);
return impl->read32(offset);
}
uint64 read64(uint64 offset)
{
warnAlignment<8>("MMIORange::read64", silent, offset);
return impl->read64(offset);
}
void write32(uint64 offset, uint32 val)
{
warnAlignment<4>("MMIORange::write32", silent, offset);
impl->write32(offset, val);
}
void write64(uint64 offset, uint64 val)
{
warnAlignment<8>("MMIORange::write64", silent, offset);
impl->write64(offset, val);
}
};
Expand All @@ -146,10 +172,16 @@ class MMIORange
#ifndef __APPLE__
const bool readonly;
#endif
const bool silent;
const int core;
MMIORange(const MMIORange &) = delete;
MMIORange & operator = (const MMIORange &) = delete;
public:
MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true, bool silent = false);
MMIORange( const uint64 baseAddr_,
const uint64 size_,
const bool readonly_ = true,
const bool silent_ = false,
const int core_ = -1);
uint32 read32(uint64 offset);
uint64 read64(uint64 offset);
void write32(uint64 offset, uint32 val);
Expand Down
21 changes: 20 additions & 1 deletion src/pci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
#include "Winmsrdriver\msrstruct.h"
#include "winring0/OlsDef.h"
#include "winring0/OlsApiInitExt.h"
#include "utils.h"
#endif

#include "utils.h"

#if defined (__FreeBSD__) || defined(__DragonFly__)
#include <sys/pciio.h>
#endif
Expand Down Expand Up @@ -83,6 +84,7 @@ bool PciHandle::exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 func

int32 PciHandle::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandle::read32", false, offset);
if (hDriver != INVALID_HANDLE_VALUE)
{
PCICFG_Request req;
Expand Down Expand Up @@ -113,6 +115,7 @@ int32 PciHandle::read32(uint64 offset, uint32 * value)

int32 PciHandle::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandle::write32", false, offset);
if (hDriver != INVALID_HANDLE_VALUE)
{
PCICFG_Request req;
Expand All @@ -139,6 +142,7 @@ int32 PciHandle::write32(uint64 offset, uint32 value)

int32 PciHandle::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandle::read64", false, offset);
if (hDriver != INVALID_HANDLE_VALUE)
{
PCICFG_Request req;
Expand Down Expand Up @@ -208,18 +212,21 @@ bool PciHandle::exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 func

int32 PciHandle::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandle::read32", false, offset);
uint32_t pci_address = FORM_PCI_ADDR(bus, device, function, (uint32_t)offset);
return PCIDriver_read32(pci_address, value);
}

int32 PciHandle::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandle::write32", false, offset);
uint32_t pci_address = FORM_PCI_ADDR(bus, device, function, (uint32_t)offset);
return PCIDriver_write32(pci_address, value);
}

int32 PciHandle::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandle::read64", false, offset);
uint32_t pci_address = FORM_PCI_ADDR(bus, device, function, (uint32_t)offset);
return PCIDriver_read64(pci_address, value);
}
Expand Down Expand Up @@ -289,6 +296,7 @@ bool PciHandle::exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 func

int32 PciHandle::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandle::read32", false, offset);
struct pci_io pi;
int ret;

Expand All @@ -308,6 +316,7 @@ int32 PciHandle::read32(uint64 offset, uint32 * value)

int32 PciHandle::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandle::write32", false, offset);
struct pci_io pi;
int ret;

Expand All @@ -327,6 +336,7 @@ int32 PciHandle::write32(uint64 offset, uint32 value)

int32 PciHandle::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandle::read64", false, offset);
struct pci_io pi;
int32 ret;

Expand Down Expand Up @@ -415,16 +425,19 @@ bool PciHandle::exists(uint32 groupnr_, uint32 bus_, uint32 device_, uint32 func

int32 PciHandle::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandle::read32", false, offset);
return ::pread(fd, (void *)value, sizeof(uint32), offset);
}

int32 PciHandle::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandle::write32", false, offset);
return ::pwrite(fd, (const void *)&value, sizeof(uint32), offset);
}

int32 PciHandle::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandle::read64", false, offset);
size_t res = ::pread(fd, (void *)value, sizeof(uint64), offset);
if(res != sizeof(uint64))
{
Expand Down Expand Up @@ -532,16 +545,19 @@ bool PciHandleM::exists(uint32 /*groupnr_*/, uint32 /* bus_*/, uint32 /* device_

int32 PciHandleM::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandleM::read32", false, offset);
return ::pread(fd, (void *)value, sizeof(uint32), offset + base_addr);
}

int32 PciHandleM::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandleM::write32", false, offset);
return ::pwrite(fd, (const void *)&value, sizeof(uint32), offset + base_addr);
}

int32 PciHandleM::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandleM::read64", false, offset);
return ::pread(fd, (void *)value, sizeof(uint64), offset + base_addr);
}

Expand Down Expand Up @@ -682,20 +698,23 @@ bool PciHandleMM::exists(uint32 /*groupnr_*/, uint32 /*bus_*/, uint32 /*device_*

int32 PciHandleMM::read32(uint64 offset, uint32 * value)
{
warnAlignment<4>("PciHandleMM::read32", false, offset);
*value = *((uint32 *)(mmapAddr + offset));

return sizeof(uint32);
}

int32 PciHandleMM::write32(uint64 offset, uint32 value)
{
warnAlignment<4>("PciHandleMM::write32", false, offset);
*((uint32 *)(mmapAddr + offset)) = value;

return sizeof(uint32);
}

int32 PciHandleMM::read64(uint64 offset, uint64 * value)
{
warnAlignment<4>("PciHandleMM::read64", false, offset);
read32(offset, (uint32 *)value);
read32(offset + sizeof(uint32), ((uint32 *)value) + 1);

Expand Down
4 changes: 2 additions & 2 deletions src/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ void processDVSEC(MatchFunc matchFunc, ProcessFunc processFunc)
// std::cerr << "Intel device scan. found " << std::hex << group << ":" << bus << ":" << device << ":" << function << " " << device_id << std::dec;
uint32 status{0};
PciHandleType h(group, bus, device, function);
h.read32(6, &status); // read status
if (status & 0x10) // has capability list
h.read32(4, &status); // read status
if (status & 0x100000) // has capability list
{
// std::cerr << "Intel device scan. found "<< std::hex << group << ":" << bus << ":" << device << ":" << function << " " << device_id << " with capability list\n" << std::dec;
VSEC header;
Expand Down
Loading