Processor Counter Monitor
mmio.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2012-2019, Intel Corporation
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 
7  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9  * Neither the name of Intel Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10 
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 */
13 // written by Roman Dementiev
14 // Patrick Konsor
15 //
16 
17 #pragma once
18 
24 #include "types.h"
25 
26 #ifdef _MSC_VER
27 #include "windows.h"
28 #include "winpmem\winpmem.h"
29 #else
30 #include <unistd.h>
31 #endif
32 
33 #include "mutex.h"
34 #include <memory>
35 
36 #ifdef _MSC_VER
37 class MMIORange
38 {
39 
40  static std::shared_ptr<WinPmem> pmem;
41  static PCM_Util::Mutex mutex;
42  static bool writeSupported;
43  uint64 startAddr;
44 
45  template <class T>
46  void writeInternal(uint64 offset, T val)
47  {
48  if (!writeSupported)
49  {
50  std::cerr << "PCM Error: MMIORange writes are not supported by the driver" << std::endl;
51  return;
52  }
53  if (readonly)
54  {
55  std::cerr << "PCM Error: attempting to write to a read-only MMIORange" << std::endl;
56  return;
57  }
58  mutex.lock();
59  pmem->write(startAddr + offset, val);
60  mutex.unlock();
61  }
62  template <class T>
63  void readInternal(uint64 offset, T & res)
64  {
65  mutex.lock();
66  pmem->read(startAddr + offset, res);
67  mutex.unlock();
68  }
69  const bool readonly;
70 public:
71  MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
72  uint32 read32(uint64 offset)
73  {
74  uint32 result = 0;
75  readInternal(offset, result);
76  return result;
77  }
78  uint64 read64(uint64 offset)
79  {
80  uint64 result = 0;
81  readInternal(offset, result);
82  return result;
83  }
84  void write32(uint64 offset, uint32 val)
85  {
86  writeInternal(offset, val);
87  }
88  void write64(uint64 offset, uint64 val)
89  {
90  writeInternal(offset, val);
91  }
92 };
93 
94 #elif defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || defined(__DragonFly__)
95 
96 class MMIORange
97 {
98  int32 fd;
99  char * mmapAddr;
100  const uint64 size;
101  const bool readonly;
102 public:
103  MMIORange(uint64 baseAddr_, uint64 size_, bool readonly_ = true);
104  uint32 read32(uint64 offset);
105  uint64 read64(uint64 offset);
106  void write32(uint64 offset, uint32 val);
107  void write64(uint64 offset, uint64 val);
108  ~MMIORange();
109 };
110 #endif
111 
112 
Definition: memoptest.cpp:35
Internal type and constant definitions.
Definition: mutex.h:14