summaryrefslogtreecommitdiffstats
path: root/tools/io.hpp
blob: 48e70bf17654acddb4b7f3f2d476d03828b8c6c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once

#include "internal/sys.hpp"

#include <cstdint>
#include <string>

namespace host_tool
{

class HostIoInterface
{
  public:
    virtual ~HostIoInterface() = default;

    /**
     * Attempt to read bytes from offset to the destination from the host
     * memory device.
     *
     * @param[in] offset - offset into the host memory device.
     * @param[in] length - the number of bytes to copy from source.
     * @param[in] destination - where to write the bytes.
     * @return true on success, false on failure (such as unable to initialize
     * device).
     */
    virtual bool read(const std::size_t offset, const std::size_t length,
                      void* const destination) = 0;

    /**
     * Attempt to write bytes from source to offset into the host memory device.
     *
     * @param[in] offset - offset into the host memory device.
     * @param[in] length - the number of bytes to copy from source.
     * @param[in] source - the souce of the bytes to copy to the memory device.
     * @return true on success, false on failure (such as unable to initialize
     * device).
     */
    virtual bool write(const std::size_t offset, const std::size_t length,
                       const void* const source) = 0;
};

class DevMemDevice : public HostIoInterface
{
  public:
    explicit DevMemDevice(const internal::Sys* sys = &internal::sys_impl) :
        sys(sys)
    {
    }

    ~DevMemDevice() = default;

    /* Don't allow copying, assignment or move assignment, only moving. */
    DevMemDevice(const DevMemDevice&) = delete;
    DevMemDevice& operator=(const DevMemDevice&) = delete;
    DevMemDevice(DevMemDevice&&) = default;
    DevMemDevice& operator=(DevMemDevice&&) = delete;

    bool read(const std::size_t offset, const std::size_t length,
              void* const destination) override;

    bool write(const std::size_t offset, const std::size_t length,
               const void* const source) override;

  private:
    static const std::string devMemPath;
    int devMemFd = -1;
    void* devMemMapped = nullptr;
    const internal::Sys* sys;
};

class PpcMemDevice : public HostIoInterface
{
  public:
    explicit PpcMemDevice(const std::string ppcMemPath,
                          const internal::Sys* sys = &internal::sys_impl) :
        ppcMemPath(ppcMemPath),
        sys(sys)
    {
    }

    ~PpcMemDevice() override;

    /* Don't allow copying or assignment, only moving. */
    PpcMemDevice(const PpcMemDevice&) = delete;
    PpcMemDevice& operator=(const PpcMemDevice&) = delete;
    PpcMemDevice(PpcMemDevice&&) = default;
    PpcMemDevice& operator=(PpcMemDevice&&) = default;

    bool read(const std::size_t offset, const std::size_t length,
              void* const destination) override;

    bool write(const std::size_t offset, const std::size_t length,
               const void* const source) override;

  private:
    int ppcMemFd = -1;
    const std::string ppcMemPath;
    const internal::Sys* sys;
};

} // namespace host_tool
OpenPOWER on IntegriCloud