Initial test

This commit is contained in:
DataHoarder 2021-10-23 13:15:32 +02:00
parent a18b89a143
commit 609c0b00e7
9 changed files with 151 additions and 6 deletions

View file

@ -3,5 +3,5 @@ project(src)
set(CMAKE_CXX_STANDARD 14)
add_executable(fsmd src/fsmd.cpp src/device/PCIEDevice.cpp src/device/PCIEDevice.h)
add_executable(fsmd src/fsmd.cpp src/device/PCIEDevice.cpp src/device/PCIEDevice.h src/FM10K.cpp src/FM10K.h)
add_executable(fsm src/fsm.cpp)

1
src/FM10K.cpp Normal file
View file

@ -0,0 +1 @@
#include "FM10K.h"

25
src/FM10K.h Normal file
View file

@ -0,0 +1,25 @@
#pragma once
#include <cstdint>
#include <algorithm>
#include <memory>
#include "device/Device.h"
#include "registers/MGMT.h"
class FM10K {
public:
static const uint64_t BAR4_SIZE = 0x0000000004000000;
static const uint64_t BAR4_OFFSET = 0x0;
explicit FM10K(std::unique_ptr<Device> device) : m_device(std::move(device)) {
}
private:
std::unique_ptr<Device> m_device;
};

View file

@ -1,5 +1,13 @@
#pragma once
class Device{
virtual void get() const;
public:
virtual ~Device()= default;
virtual bool valid() const = 0;
virtual uint32_t get32(uint64_t address) const = 0;
virtual void set32(uint64_t address, uint32_t value) const = 0;
virtual uint64_t get64(uint64_t address) const = 0;
virtual void set64(uint64_t address, uint64_t value) const = 0;
};

View file

@ -1,6 +1,10 @@
#include <cstring>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "PCIEDevice.h"
#include "../FM10K.h"
std::vector<PCIEDevice::DeviceEntry> PCIEDevice::DeviceEntry::find() {
std::vector<PCIEDevice::DeviceEntry> entries;
@ -74,3 +78,50 @@ std::vector<PCIEDevice::DeviceEntry> PCIEDevice::DeviceEntry::find() {
return entries;
}
PCIEDevice::PCIEDevice(PCIEDevice::DeviceEntry device) : m_device (std::move(device)){
m_bar4Resource = open(m_device.getPath().c_str(), O_RDWR);
if(m_bar4Resource <= 0){
//TODO printf("Unable to open BAR4 resource %s\n", m_device.getPath().c_str());
return;
}
m_bar4mmap = mmap(nullptr, FM10K::BAR4_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_bar4Resource, 0);
if(m_bar4mmap == MAP_FAILED){
//TODO printf("Unable to map BAR4 resource %s\n", m_device.getPath().c_str());
close(m_bar4Resource);
m_bar4Resource = 0;
m_bar4mmap = nullptr;
return;
}
}
PCIEDevice::~PCIEDevice() {
if(m_bar4mmap != nullptr){
if(munmap(m_bar4mmap, FM10K::BAR4_SIZE) != 0){
//Error
}
m_bar4mmap = nullptr;
}
if(m_bar4Resource > 0){
close(m_bar4Resource);
m_bar4Resource = 0;
}
}
uint32_t PCIEDevice::get32(uint64_t address) const {
return *(((volatile uintptr_t *) m_bar4mmap) + address);
}
void PCIEDevice::set32(uint64_t address, uint32_t value) const {
*(volatile uintptr_t *) (((uintptr_t *) m_bar4mmap) + address) = value;
}
uint64_t PCIEDevice::get64(uint64_t address) const {
return *(((volatile uintptr_t *) m_bar4mmap) + address);
}
void PCIEDevice::set64(uint64_t address, uint64_t value) const {
*(uintptr_t volatile *) (((uintptr_t *) m_bar4mmap) + address) = value;
}

View file

@ -1,7 +1,6 @@
#pragma once
#include <string>
#include <utility>
#include <vector>
#include "Device.h"
@ -34,12 +33,28 @@ public:
uint16_t m_class;
};
PCIEDevice(const std::string& path){
explicit PCIEDevice(PCIEDevice::DeviceEntry device);
uint32_t get32(uint64_t address) const override;
void set32(uint64_t address, uint32_t value) const override;
uint64_t get64(uint64_t address) const override;
void set64(uint64_t address, uint64_t value) const override;
const PCIEDevice::DeviceEntry& getDeviceEntry() const{
return m_device;
}
bool valid() const override{
return m_bar4mmap != nullptr && m_bar4Resource > 0;
}
~PCIEDevice();
private:
std::string m_path;
int m_bar4Resource = 0;
void* m_bar4mmap = nullptr;
PCIEDevice::DeviceEntry m_device;
};

View file

@ -1,6 +1,20 @@
#include <iostream>
#include <memory>
#include "device/PCIEDevice.h"
int main() {
std::cout << "Hello, World!" << std::endl;
auto entries = PCIEDevice::DeviceEntry::find();
std::cout << "Found " << entries.size() << " FM10K device(s)." << std::endl;
for(auto& entry : entries){
auto dev = std::make_unique<PCIEDevice>(entry);
std::cout << "dev.path: " << dev->getDeviceEntry().getPath() << std::endl;
std::cout << "dev.vendor: " << dev->getDeviceEntry().getVendor() << std::endl;
std::cout << "dev.class: " << dev->getDeviceEntry().getClass() << std::endl;
std::cout << "DEVICE_CFG: " << dev->get32(0x4) << std::endl;
std::cout << "BSM_ARGS: " << dev->get32(0xC00) << std::endl;
std::cout << "FUSE_DATA(64): " << dev->get64(0xC0E) << std::endl;
}
return 0;
}

8
src/registers/MGMT.h Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include "pack.h"
namespace MGMT{
}

23
src/registers/pack.h Normal file
View file

@ -0,0 +1,23 @@
#pragma once
#include "stdint.h"
#if __GNUC__ > 4 || (__GNUC__ == 4 && (__GNUC_MINOR__ >= 4)) || __clang__ || __TINYC__
#if defined(__TINYC__)
#pragma pack(1)
#endif
#define PACKED(x, s) union { \
s value; \
x __attribute__((packed)) fields; \
}
#define PACKEDM(x, s, n) union { \
s value[n]; \
x __attribute__((packed)) fields; \
}
#else
#error "Packed bit-fields of type char were not properly bit-packed on many targets prior to GCC 4.4, upgrade to GCC >= 4.4."
#endif