1/*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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 * Authors: Steve Reinhardt
29 */
30
31#ifndef __SIM_SYSCALL_EMUL_BUF_HH__
32#define __SIM_SYSCALL_EMUL_BUF_HH__
33
34///
35/// @file syscall_emul_buf.hh
36///
37/// This file defines buffer classes used to handle pointer arguments
38/// in emulated syscalls.
39
40#include <cstring>
41
42#include "base/types.hh"
43#include "mem/se_translating_port_proxy.hh"
44
45/**
46 * Base class for BufferArg and TypedBufferArg, Not intended to be
47 * used directly.
48 *
49 * The BufferArg classes represent buffers in target user space that
50 * are passed by reference to an (emulated) system call.  Each
51 * instance provides an internal (simulator-space) buffer of the
52 * appropriate size and tracks the user-space address.  The copyIn()
53 * and copyOut() methods copy the user-space buffer to and from the
54 * simulator-space buffer, respectively.
55 */
56class BaseBufferArg {
57
58  public:
59
60    /**
61     * Allocate a buffer of size 'size' representing the memory at
62     * target address 'addr'.
63     */
64    BaseBufferArg(Addr _addr, int _size)
65        : addr(_addr), size(_size), bufPtr(new uint8_t[size])
66    {
67        // clear out buffer: in case we only partially populate this,
68        // and then do a copyOut(), we want to make sure we don't
69        // introduce any random junk into the simulated address space
70        memset(bufPtr, 0, size);
71    }
72
73    ~BaseBufferArg() { delete [] bufPtr; }
74
75    /**
76     * copy data into simulator space (read from target memory)
77     */
78    bool
79    copyIn(PortProxy &memproxy)
80    {
81        memproxy.readBlob(addr, bufPtr, size);
82        return true;    // no EFAULT detection for now
83    }
84
85    /**
86     * copy data out of simulator space (write to target memory)
87     */
88    bool
89    copyOut(PortProxy &memproxy)
90    {
91        memproxy.writeBlob(addr, bufPtr, size);
92        return true;    // no EFAULT detection for now
93    }
94
95  protected:
96    const Addr addr;        ///< address of buffer in target address space
97    const int size;         ///< buffer size
98    uint8_t * const bufPtr; ///< pointer to buffer in simulator space
99};
100
101/**
102 * BufferArg represents an untyped buffer in target user space that is
103 * passed by reference to an (emulated) system call.
104 */
105class BufferArg : public BaseBufferArg
106{
107  public:
108    /**
109     * Allocate a buffer of size 'size' representing the memory at
110     * target address 'addr'.
111     */
112    BufferArg(Addr _addr, int _size) : BaseBufferArg(_addr, _size) { }
113
114    /**
115     * Return a pointer to the internal simulator-space buffer.
116     */
117    void *bufferPtr() { return bufPtr; }
118};
119
120/**
121 * TypedBufferArg is a class template; instances of this template
122 * represent typed buffers in target user space that are passed by
123 * reference to an (emulated) system call.
124 *
125 * This template provides operator overloads for convenience, allowing
126 * for example the use of '->' to reference fields within a struct
127 * type.
128 */
129template <class T>
130class TypedBufferArg : public BaseBufferArg
131{
132  public:
133    /**
134     * Allocate a buffer of type T representing the memory at target
135     * address 'addr'. The user can optionally specify a specific
136     * number of bytes to allocate to deal with structs that have
137     * variable-size arrays at the end.
138     */
139    TypedBufferArg(Addr _addr, int _size = sizeof(T))
140        : BaseBufferArg(_addr, _size)
141    { }
142
143    /**
144     * Convert TypedBufferArg<T> to a pointer to T that points to the
145     * internal buffer.
146     */
147    operator T*() { return (T *)bufPtr; }
148
149    /**
150     * Convert TypedBufferArg<T> to a reference to T that references the
151     * internal buffer value.
152     */
153    T &operator*()       { return *((T *)bufPtr); }
154
155
156    /**
157     * Enable the use of '->' to reference fields where T is a struct
158     * type.
159     */
160    T* operator->()      { return (T *)bufPtr; }
161
162    /**
163     * Enable the use of '[]' to reference fields where T is an array
164     * type.
165     */
166    T &operator[](int i) { return ((T *)bufPtr)[i]; }
167};
168
169
170#endif // __SIM_SYSCALL_EMUL_BUF_HH__
171