gem5  v22.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 
36 namespace gem5
37 {
38 
39 void
40 VMA::fillMemPages(Addr start, Addr size, PortProxy &port) const
41 {
42  auto offset = start - _addrRange.start();
43 
47  if (offset < _hostBufLen) {
48  auto size = std::min(_hostBufLen - offset, _pageBytes);
49  port.writeBlob(start, (uint8_t*)_hostBuf + offset, size);
50  }
51 }
52 
53 bool
55 {
56  return (r.start() > _addrRange.start() && r.end() < _addrRange.end());
57 }
58 
59 void
61 {
62  if (hasHostBuf()) {
63  auto nonoverlap_len = slice_addr - _addrRange.start();
64  _hostBufLen = std::min(_hostBufLen, nonoverlap_len);
65  }
66 
67  _addrRange = AddrRange(_addrRange.start(), slice_addr);
68 
69  DPRINTF(Vma, "slice right vma start %#x end %#x\n", _addrRange.start(),
70  _addrRange.end());
71 
72  sanityCheck();
73 }
74 
75 void
77 {
78  if (hasHostBuf()) {
79  auto overlap_len = slice_addr - _addrRange.start();
80 
81  if (overlap_len >= _hostBufLen) {
82  _hostBufLen = 0;
83  _hostBuf = nullptr;
84  _origHostBuf = nullptr;
85  } else {
86  _hostBufLen -= overlap_len;
87  }
88 
89  _hostBuf = (void *)((uint8_t *)_hostBuf + overlap_len);
90  }
91 
92  _addrRange = AddrRange(slice_addr, _addrRange.end());
93 
94  DPRINTF(Vma, "slice left vma start %#x end %#x\n", _addrRange.start(),
95  _addrRange.end());
96 
97  sanityCheck();
98 }
99 
100 void
102 {
106  assert(_addrRange.start() != _addrRange.end());
107 
111  assert(_addrRange.start() < _addrRange.end());
112 
117  assert((_addrRange.start() % _pageBytes) == 0);
118  assert((_addrRange.end() % _pageBytes) == 0);
119 }
120 
122  off_t offset)
123  : _buffer(nullptr), _length(length)
124 {
125  panic_if(_length == 0, "Tried to mmap file of length zero");
126 
127  struct stat file_stat;
128  if (fstat(fd, &file_stat) > 0) {
129  panic("Cannot stat file: %s\n", strerror(errno));
130  }
131 
132  // Don't bother mapping more than the actual file size
133  panic_if(offset > file_stat.st_size,
134  "Tried to mmap with offset greater than file size");
135  _length = std::min((size_t)(file_stat.st_size - offset), _length);
136 
137  // cannot call mmap with _length == 0
138  if (_length) {
139  _buffer = mmap(NULL, _length, PROT_READ,
140  MAP_PRIVATE, fd, offset);
141  if (_buffer == MAP_FAILED) {
142  panic("Failed to map file into host address space: %s",
143  strerror(errno));
144  }
145  } else {
146  panic("Tried to mmap 0 bytes");
147  }
148 }
149 
151 {
152  if (_buffer) {
153  panic_if(munmap(_buffer, _length) == -1,
154  "mmap: failed to unmap file-backed host memory: %s",
155  strerror(errno));
156  }
157 }
158 
159 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
The AddrRange class encapsulates an address range, and supports a number of tests to check if two ran...
Definition: addr_range.hh:82
This object is a proxy for a port or other object which implements the functional response protocol,...
Definition: port_proxy.hh:87
void writeBlob(Addr addr, const void *p, int size) const
Same as tryWriteBlob, but insists on success.
Definition: port_proxy.hh:192
MappedFileBuffer(int fd, size_t length, off_t offset)
Definition: vma.cc:121
Addr size()
Defer AddrRange related calls to the AddrRange.
Definition: vma.hh:112
Addr _pageBytes
Number of bytes in an OS page.
Definition: vma.hh:151
void sliceRegionLeft(Addr slice_addr)
Remove the address range to the left of slice_addr.
Definition: vma.cc:76
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:40
uint64_t _hostBufLen
Length of host buffer for this virtual memory area.
Definition: vma.hh:170
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:54
AddrRange _addrRange
Address range for this virtual memory area.
Definition: vma.hh:146
void sanityCheck()
Definition: vma.cc:101
void * _hostBuf
Host buffer ptr for this virtual memory area.
Definition: vma.hh:165
std::shared_ptr< MappedFileBuffer > _origHostBuf
The host file backing will be chopped up and reassigned as pages are mapped, remapped,...
Definition: vma.hh:160
Addr start()
Definition: vma.hh:113
bool hasHostBuf() const
Check if the virtual memory area has an equivalent buffer on the host machine.
Definition: vma.hh:83
void sliceRegionRight(Addr slice_addr)
Remove the address range to the right of slice_addr.
Definition: vma.cc:60
Addr end() const
Get the end address of the range.
Definition: addr_range.hh:350
Addr start() const
Get the start address of the range.
Definition: addr_range.hh:343
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
Bitfield< 14, 12 > fd
Definition: types.hh:150
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 5 > r
Definition: pagetable.hh:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147

Generated on Wed Dec 21 2022 10:22:40 for gem5 by doxygen 1.9.1