gem5  v20.1.0.0
vma.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "sim/vma.hh"
30 
31 #include <sys/mman.h>
32 #include <sys/stat.h>
33 
34 #include "base/types.hh"
35 #include "config/the_isa.hh"
36 
37 void
38 VMA::fillMemPages(Addr start, Addr size, PortProxy &port) const
39 {
40  auto offset = start - _addrRange.start();
41 
45  if (offset < _hostBufLen) {
46  auto size = std::min(_hostBufLen - offset, _pageBytes);
47  port.writeBlob(start, (uint8_t*)_hostBuf + offset, size);
48  }
49 }
50 
51 bool
53 {
54  return (r.start() > _addrRange.start() && r.end() < _addrRange.end());
55 }
56 
57 void
59 {
60  if (hasHostBuf()) {
61  auto nonoverlap_len = slice_addr - _addrRange.start();
62  _hostBufLen = std::min(_hostBufLen, nonoverlap_len);
63  }
64 
65  _addrRange = AddrRange(_addrRange.start(), slice_addr);
66 
67  DPRINTF(Vma, "slice right vma start %#x end %#x\n", _addrRange.start(),
68  _addrRange.end());
69 
70  sanityCheck();
71 }
72 
73 void
75 {
76  if (hasHostBuf()) {
77  auto overlap_len = slice_addr - _addrRange.start();
78 
79  if (overlap_len >= _hostBufLen) {
80  _hostBufLen = 0;
81  _hostBuf = nullptr;
82  _origHostBuf = nullptr;
83  } else {
84  _hostBufLen -= overlap_len;
85  }
86 
87  _hostBuf = (void *)((uint8_t *)_hostBuf + overlap_len);
88  }
89 
90  _addrRange = AddrRange(slice_addr, _addrRange.end());
91 
92  DPRINTF(Vma, "slice left vma start %#x end %#x\n", _addrRange.start(),
93  _addrRange.end());
94 
95  sanityCheck();
96 }
97 
98 void
100 {
104  assert(_addrRange.start() != _addrRange.end());
105 
109  assert(_addrRange.start() < _addrRange.end());
110 
115  assert((_addrRange.start() % _pageBytes) == 0);
116  assert((_addrRange.end() % _pageBytes) == 0);
117 }
118 
120  off_t offset)
121  : _buffer(nullptr), _length(length)
122 {
123  panic_if(_length == 0, "Tried to mmap file of length zero");
124 
125  struct stat file_stat;
126  if (fstat(fd, &file_stat) > 0) {
127  panic("Cannot stat file: %s\n", strerror(errno));
128  }
129 
130  // Don't bother mapping more than the actual file size
131  panic_if(offset > file_stat.st_size,
132  "Tried to mmap with offset greater than file size");
133  _length = std::min((size_t)(file_stat.st_size - offset), _length);
134 
135  // cannot call mmap with _length == 0
136  if (_length) {
137  _buffer = mmap(NULL, _length, PROT_READ,
138  MAP_PRIVATE, fd, offset);
139  if (_buffer == MAP_FAILED) {
140  panic("Failed to map file into host address space: %s",
141  strerror(errno));
142  }
143  } else {
144  panic("Tried to mmap 0 bytes");
145  }
146 }
147 
149 {
150  if (_buffer) {
151  panic_if(munmap(_buffer, _length) == -1,
152  "mmap: failed to unmap file-backed host memory: %s",
153  strerror(errno));
154  }
155 }
length
uint8_t length
Definition: inet.hh:422
VMA::sanityCheck
void sanityCheck()
Definition: vma.cc:99
VMA::sliceRegionLeft
void sliceRegionLeft(Addr slice_addr)
Remove the address range to the left of slice_addr.
Definition: vma.cc:74
ArmISA::fd
Bitfield< 14, 12 > fd
Definition: types.hh:159
VMA::MappedFileBuffer::~MappedFileBuffer
~MappedFileBuffer()
Definition: vma.cc:148
AddrRange::end
Addr end() const
Get the end address of the range.
Definition: addr_range.hh:321
VMA::_hostBufLen
uint64_t _hostBufLen
Length of host buffer for this virtual memory area.
Definition: vma.hh:167
VMA::_origHostBuf
std::shared_ptr< MappedFileBuffer > _origHostBuf
The host file backing will be chopped up and reassigned as pages are mapped, remapped,...
Definition: vma.hh:157
VMA::sliceRegionRight
void sliceRegionRight(Addr slice_addr)
Remove the address range to the right of slice_addr.
Definition: vma.cc:58
VMA::hasHostBuf
bool hasHostBuf() const
Check if the virtual memory area has an equivalent buffer on the host machine.
Definition: vma.hh:80
PortProxy::writeBlob
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Definition: port_proxy.hh:187
VMA::_hostBuf
void * _hostBuf
Host buffer ptr for this virtual memory area.
Definition: vma.hh:162
AddrRange
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:68
VMA::size
Addr size()
Defer AddrRange related calls to the AddrRange.
Definition: vma.hh:109
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
MipsISA::r
r
Definition: pra_constants.hh:95
VMA::MappedFileBuffer::_length
size_t _length
Definition: vma.hh:194
VMA::MappedFileBuffer::_buffer
void * _buffer
Definition: vma.hh:193
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:197
PortProxy
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:80
AddrRange::start
Addr start() const
Get the start address of the range.
Definition: addr_range.hh:314
VMA::_addrRange
AddrRange _addrRange
Address range for this virtual memory area.
Definition: vma.hh:143
types.hh
VMA::isStrictSuperset
bool isStrictSuperset(const AddrRange &range) const
Returns true if desired range exists within this virtual memory area and does not include the start a...
Definition: vma.cc:52
VMA::MappedFileBuffer::MappedFileBuffer
MappedFileBuffer(int fd, size_t length, off_t offset)
Definition: vma.cc:119
VMA::start
Addr start()
Definition: vma.hh:110
VMA::fillMemPages
void fillMemPages(Addr start, Addr size, PortProxy &port) const
Copy memory from a buffer which resides on the host machine into a section of memory on the target.
Definition: vma.cc:38
vma.hh
VMA::_pageBytes
Addr _pageBytes
Number of bytes in an OS page.
Definition: vma.hh:148
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153

Generated on Wed Sep 30 2020 14:02:14 for gem5 by doxygen 1.8.17