2 * Copyright (c) 2003-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_SYSCALL_EMUL_BUF_HH__
30 #define __SIM_SYSCALL_EMUL_BUF_HH__
33 /// @file syscall_emul_buf.hh
35 /// This file defines buffer classes used to handle pointer arguments
36 /// in emulated syscalls.
40 #include "base/types.hh"
41 #include "mem/se_translating_port_proxy.hh"
44 * Base class for BufferArg and TypedBufferArg, Not intended to be
47 * The BufferArg classes represent buffers in target user space that
48 * are passed by reference to an (emulated) system call. Each
49 * instance provides an internal (simulator-space) buffer of the
50 * appropriate size and tracks the user-space address. The copyIn()
51 * and copyOut() methods copy the user-space buffer to and from the
52 * simulator-space buffer, respectively.
59 * Allocate a buffer of size 'size' representing the memory at
60 * target address 'addr'.
62 BaseBufferArg(Addr _addr, int _size)
63 : addr(_addr), size(_size), bufPtr(new uint8_t[size])
65 // clear out buffer: in case we only partially populate this,
66 // and then do a copyOut(), we want to make sure we don't
67 // introduce any random junk into the simulated address space
68 memset(bufPtr, 0, size);
71 ~BaseBufferArg() { delete [] bufPtr; }
74 * copy data into simulator space (read from target memory)
77 copyIn(PortProxy &memproxy)
79 memproxy.readBlob(addr, bufPtr, size);
80 return true; // no EFAULT detection for now
84 * copy data out of simulator space (write to target memory)
87 copyOut(PortProxy &memproxy)
89 memproxy.writeBlob(addr, bufPtr, size);
90 return true; // no EFAULT detection for now
94 const Addr addr; ///< address of buffer in target address space
95 const int size; ///< buffer size
96 uint8_t * const bufPtr; ///< pointer to buffer in simulator space
100 * BufferArg represents an untyped buffer in target user space that is
101 * passed by reference to an (emulated) system call.
103 class BufferArg : public BaseBufferArg
107 * Allocate a buffer of size 'size' representing the memory at
108 * target address 'addr'.
110 BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
113 * Return a pointer to the internal simulator-space buffer.
115 void *bufferPtr() { return bufPtr; }
119 * TypedBufferArg is a class template; instances of this template
120 * represent typed buffers in target user space that are passed by
121 * reference to an (emulated) system call.
123 * This template provides operator overloads for convenience, allowing
124 * for example the use of '->' to reference fields within a struct
128 class TypedBufferArg : public BaseBufferArg
132 * Allocate a buffer of type T representing the memory at target
133 * address 'addr'. The user can optionally specify a specific
134 * number of bytes to allocate to deal with structs that have
135 * variable-size arrays at the end.
137 TypedBufferArg(Addr _addr, int _size = sizeof(T))
138 : BaseBufferArg(_addr, _size)
142 * Convert TypedBufferArg<T> to a pointer to T that points to the
145 operator T*() { return (T *)bufPtr; }
148 * Convert TypedBufferArg<T> to a reference to T that references the
149 * internal buffer value.
151 T &operator*() { return *((T *)bufPtr); }
155 * Enable the use of '->' to reference fields where T is a struct
158 T* operator->() { return (T *)bufPtr; }
161 * Enable the use of '[]' to reference fields where T is an array
164 T &operator[](int i) { return ((T *)bufPtr)[i]; }
168 #endif // __SIM_SYSCALL_EMUL_BUF_HH__