2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
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.
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.
29 #ifndef __SIM_PROXY_PTR_HH__
30 #define __SIM_PROXY_PTR_HH__
34 #include <type_traits>
36 #include "base/logging.hh"
37 #include "base/types.hh"
38 #include "sim/guest_abi.hh"
40 template <typename Proxy>
44 std::shared_ptr<Proxy> proxy;
48 std::unique_ptr<uint8_t[]> data;
52 void markClean() { dirty = false; }
56 std::shared_ptr<Proxy> getProxy() const { return proxy; }
58 void markDirty() { dirty = true; }
59 bool isDirty() { return dirty; }
65 assert(sizeof(T) <= size);
67 return *reinterpret_cast<T *>(data.get());
74 assert(sizeof(T) <= size);
75 return *reinterpret_cast<T *>(data.get());
79 flush(bool force=false)
81 if (force || isDirty()) {
82 proxy->writeBlob(ptr, data.get(), size);
90 panic_if(isDirty(), "Overwriting dirty ProxyPtr.");
91 proxy->readBlob(ptr, data.get(), size);
94 Addr addr() const { return ptr; }
96 ProxyPtrBuffer(std::shared_ptr<Proxy> _proxy, Addr _ptr, size_t _size) :
97 proxy(_proxy), ptr(_ptr), size(_size), data(new uint8_t[_size])
102 ~ProxyPtrBuffer() { flush(); }
105 template <typename T, typename Proxy>
109 std::shared_ptr<Proxy> proxy;
110 std::shared_ptr<ProxyPtrBuffer<Proxy>> buffer;
112 template <typename O, typename P>
113 friend class ProxyPtr;
115 void nullCheck() const { panic_if(!buffer, "Accessing null ProxyPtr."); }
121 buffer.reset(new ProxyPtrBuffer<Proxy>(proxy, ptr, sizeof(T)));
123 buffer.reset((ProxyPtrBuffer<Proxy> *)nullptr);
126 ConstProxyPtr(Addr _ptr, std::shared_ptr<Proxy> _proxy) : proxy(_proxy)
131 using CPP = ConstProxyPtr<T, Proxy>;
136 template <typename ...Args,
137 typename std::enable_if_t<std::is_constructible<
138 Proxy, Args&&...>::value, int> = 0>
139 explicit ConstProxyPtr(Addr _ptr, Args&&... args) :
140 proxy(std::make_shared<Proxy>(args...))
144 template <typename ...Args,
145 typename std::enable_if_t<std::is_constructible<
146 Proxy, Args&&...>::value, int> = 0>
147 explicit ConstProxyPtr(Args&&... args) :
148 proxy(std::make_shared<Proxy>(args...))
153 template <typename O, typename Enabled=
154 typename std::enable_if_t<std::is_assignable<T *, O *>::value>>
155 ConstProxyPtr(const ConstProxyPtr<O, Proxy> &other) :
156 proxy(other.proxy), buffer(other.buffer)
159 ConstProxyPtr(const CPP &other) :
160 proxy(other.proxy), buffer(other.buffer)
170 Addr addr() const { return buffer ? buffer->addr() : 0; }
171 operator bool() const { return (bool)buffer; }
173 template <typename A>
174 typename std::enable_if_t<std::is_integral<A>::value, CPP>
175 operator + (A a) const
177 return CPP(addr() + a * sizeof(T), proxy);
180 template <typename A>
181 typename std::enable_if_t<std::is_integral<A>::value, CPP>
182 operator - (A a) const
184 return CPP(addr() - a * sizeof(T), proxy);
188 operator - (const CPP &other) const
190 return (addr() - other.addr()) / sizeof(T);
194 operator = (const CPP &other)
197 buffer = other.buffer;
202 operator = (const Addr &a)
208 operator const T *() const
210 return buffer ? &buffer->template asConst<T>() : nullptr;
217 return buffer->template asConst<T>();
223 return &buffer->template asConst<T>();
227 template <typename T, typename Proxy, typename A>
228 typename std::enable_if_t<std::is_integral<A>::value, ConstProxyPtr<T, Proxy>>
229 operator + (A a, const ConstProxyPtr<T, Proxy> &other)
234 template <typename T, typename Proxy>
235 class ProxyPtr : public ConstProxyPtr<T, Proxy>
238 using CPP = ConstProxyPtr<T, Proxy>;
239 using PP = ProxyPtr<T, Proxy>;
241 ProxyPtr(Addr _ptr, std::shared_ptr<Proxy> _proxy) : CPP(_ptr, _proxy) {}
244 template <typename ...Args,
245 typename std::enable_if_t<std::is_constructible<
246 Proxy, Args&&...>::value, int> = 0>
247 explicit ProxyPtr(Addr _ptr, Args&&... args) : CPP(_ptr, args...) {}
248 template <typename ...Args,
249 typename std::enable_if_t<std::is_constructible<
250 Proxy, Args&&...>::value, int> = 0>
251 explicit ProxyPtr(Args&&... args) : CPP(0, args...) {}
253 template <typename O, typename Enabled=
254 typename std::enable_if_t<std::is_assignable<T *, O *>::value>>
255 ProxyPtr(const ProxyPtr<O, Proxy> &other) : CPP(other) {}
257 ProxyPtr(const PP &other) : CPP(other) {}
258 operator bool() const { return (bool)this->buffer; }
261 flush(bool force=false)
264 this->buffer->flush(force);
267 template <typename A>
268 typename std::enable_if_t<std::is_integral<A>::value, PP>
269 operator + (A a) const
271 return PP(this->addr() + a * sizeof(T), this->proxy);
274 template <typename A>
275 typename std::enable_if_t<std::is_integral<A>::value, PP>
276 operator - (A a) const
278 return PP(this->addr() - a * sizeof(T), this->proxy);
282 operator - (const PP &other) const
284 return (this->addr() - other.addr()) / sizeof(T);
288 operator = (const PP &other)
290 this->proxy = other.proxy;
291 this->buffer = other.buffer;
296 operator = (const Addr &a)
302 using CPP::operator const T *;
305 return this->buffer ? &this->buffer->template as<T>() : nullptr;
308 using CPP::operator *;
313 return this->buffer->template as<T>();
316 using CPP::operator ->;
321 return &this->buffer->template as<T>();
325 template <typename T, typename Proxy, typename A>
326 typename std::enable_if_t<std::is_integral<A>::value, ProxyPtr<T, Proxy>>
327 operator + (A a, const ProxyPtr<T, Proxy> &other)
335 template <typename ABI, typename T, typename Proxy>
336 struct Argument<ABI, ProxyPtr<T, Proxy>>
338 static ProxyPtr<T, Proxy>
339 get(ThreadContext *tc, typename ABI::State &state)
341 return ProxyPtr<T, Proxy>(Argument<ABI, Addr>::get(tc, state), tc);
345 template <typename ABI, typename T, typename Proxy>
346 struct Argument<ABI, ConstProxyPtr<T, Proxy>>
348 static ConstProxyPtr<T, Proxy>
349 get(ThreadContext *tc, typename ABI::State &state)
351 return ConstProxyPtr<T, Proxy>(
352 Argument<ABI, Addr>::get(tc, state), tc);
356 } // namespace GuestABI
358 template <typename T, typename Proxy>
360 operator << (std::ostream &os, const ConstProxyPtr<T, Proxy> &vptr)
362 ccprintf(os, "%#x", vptr.addr());
366 class SETranslatingPortProxy;
368 template <typename T>
369 using ConstVPtr = ConstProxyPtr<T, SETranslatingPortProxy>;
370 template <typename T>
371 using VPtr = ProxyPtr<T, SETranslatingPortProxy>;
373 #endif // __SIM_PROXY_PTR_HH__