Processor Counter Monitor
mmio.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 // Copyright (c) 2012-2019, Intel Corporation
3 // written by Roman Dementiev
4 // Patrick Konsor
5 //
6 
7 #pragma once
8 
14 #include "types.h"
15 
16 #ifdef _MSC_VER
17 #include "windows.h"
18 #include "winpmem\winpmem.h"
19 #include "Winmsrdriver\msrstruct.h"
20 #else
21 #include <unistd.h>
22 #endif
23 
24 #include "mutex.h"
25 #include <memory>
26 
27 namespace pcm {
28 
29 #ifdef _MSC_VER
30 
31 class MMIORangeInterface
32 {
33 public:
34  virtual uint32 read32(uint64 offset) = 0;
35  virtual uint64 read64(uint64 offset) = 0;
36  virtual void write32(uint64 offset, uint32 val) = 0;
37  virtual void write64(uint64 offset, uint64 val) = 0;
38  virtual ~MMIORangeInterface() {}
39 };
40 
41 class WinPmemMMIORange : public MMIORangeInterface
42 {
43  static std::shared_ptr<WinPmem> pmem;
44  static Mutex mutex;
45  static bool writeSupported;
46  uint64 startAddr;
47 
48  template <class T>
49  void writeInternal(uint64 offset, T val)
50  {
51  if (!writeSupported)
52  {
53  std::cerr << "PCM Error: MMIORange writes are not supported by the driver\n";
54  return;
55  }
56  if (readonly)
57  {
58  std::cerr << "PCM Error: attempting to write to a read-only MMIORange\n";
59  return;
60  }
61  mutex.lock();
62  pmem->write(startAddr + offset, val);
63  mutex.unlock();
64  }
65  template <class T>
66  void readInternal(uint64 offset, T & res)
67  {
68  mutex.lock();
69  pmem->read(startAddr + offset, res);
70  mutex.unlock();
71  }
72  const bool readonly;
73 public:
74  WinPmemMMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
75  uint32 read32(uint64 offset)
76  {
77  uint32 result = 0;
78  readInternal(offset, result);
79  return result;
80  }
81  uint64 read64(uint64 offset)
82  {
83  uint64 result = 0;
84  readInternal(offset, result);
85  return result;
86  }
87  void write32(uint64 offset, uint32 val)
88  {
89  writeInternal(offset, val);
90  }
91  void write64(uint64 offset, uint64 val)
92  {
93  writeInternal(offset, val);
94  }
95 };
96 
97 class OwnMMIORange : public MMIORangeInterface
98 {
99  HANDLE hDriver;
100  char * mmapAddr;
101 public:
102  OwnMMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
103  uint32 read32(uint64 offset);
104  uint64 read64(uint64 offset);
105  void write32(uint64 offset, uint32 val);
106  void write64(uint64 offset, uint64 val);
107  ~OwnMMIORange();
108 };
109 
110 class MMIORange
111 {
112  std::shared_ptr<MMIORangeInterface> impl;
113 public:
114  MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
115  uint32 read32(uint64 offset)
116  {
117  return impl->read32(offset);
118  }
119  uint64 read64(uint64 offset)
120  {
121  return impl->read64(offset);
122  }
123  void write32(uint64 offset, uint32 val)
124  {
125  impl->write32(offset, val);
126  }
127  void write64(uint64 offset, uint64 val)
128  {
129  impl->write64(offset, val);
130  }
131 };
132 
133 #elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
134 
135 class MMIORange
136 {
137 #ifndef __APPLE__
138  int32 fd;
139 #endif
140  char * mmapAddr;
141  const uint64 size;
142 #ifndef __APPLE__
143  const bool readonly;
144 #endif
145 public:
146  MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
147  uint32 read32(uint64 offset);
148  uint64 read64(uint64 offset);
149  void write32(uint64 offset, uint32 val);
150  void write64(uint64 offset, uint64 val);
151  ~MMIORange();
152 };
153 #endif
154 
155 } // namespace pcm
Definition: memoptest.cpp:24
Internal type and constant definitions.
Definition: bw.cpp:12