gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
36namespace gem5
37{
38
39void
40VMA::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
53bool
55{
56 return (r.start() > _addrRange.start() && r.end() < _addrRange.end());
57}
58
59void
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
73}
74
75void
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
98}
99
100void
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), _offset(offset)
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:210
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, uint64_t size) const
Same as tryWriteBlob, but insists on success.
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:116
Addr _pageBytes
Number of bytes in an OS page.
Definition vma.hh:155
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:174
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:150
void sanityCheck()
Definition vma.cc:101
void * _hostBuf
Host buffer ptr for this virtual memory area.
Definition vma.hh:169
std::shared_ptr< MappedFileBuffer > _origHostBuf
The host file backing will be chopped up and reassigned as pages are mapped, remapped,...
Definition vma.hh:164
Addr start()
Definition vma.hh:117
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.
Addr start() const
Get the start address of the range.
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition logging.hh:214
Bitfield< 14, 12 > fd
Definition types.hh:150
Bitfield< 23, 0 > offset
Definition types.hh:144
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0