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: Gabe Black
29 */
30
31#ifndef __SIM_SYSCALLRETURN_HH__
32#define __SIM_SYSCALLRETURN_HH__
33
34#include <inttypes.h>
35
36/**
37 * This class represents the return value from an emulated system call,
38 * including any errno setting.
39 *
40 * On some platforms, the return value and errno are encoded in a
41 * single signed integer.  A value less than zero but greater than
42 * -4096 indicates an error, and the value is the negation of the
43 * errno value.  Otherwise, the call was successful and the integer is
44 * the return value.  (Large negative numbers are considered
45 * successful to allow syscalls to return pointers to high memory,
46 * e.g., stack addresses.)  See, for example, Appendix A of the AMD64
47 * ABI spec at http://www.x86-64.org/documentation/abi.pdf.
48 *
49 * Other platforms use a more complex interface, returning a value and
50 * an error code in separate registers.
51 *
52 * This class is designed to support both types of interfaces.
53 */
54class SyscallReturn
55{
56  public:
57
58    /// For simplicity, allow the object to be initialized with a
59    /// single signed integer using the same positive=success,
60    /// negative=-errno convention described above.
61    ///
62    /// Typically this constructor is used as a default type
63    /// conversion, so a bare integer is used where a SyscallReturn
64    /// value is expected, e.g., as the return value from a system
65    /// call emulation function ('return 0;' or 'return -EFAULT;').
66    SyscallReturn(int64_t v)
67        : value(v), retryFlag(false)
68    {}
69
70    /// Pseudo-constructor to create an instance with the retry flag set.
71    static SyscallReturn retry()
72    {
73        SyscallReturn s(0);
74        s.retryFlag = true;
75        return s;
76    }
77
78    ~SyscallReturn() {}
79
80    /// Was the system call successful?
81    bool successful() const
82    {
83        return (value >= 0 || value <= -4096);
84    }
85
86    /// Does the syscall need to be retried?
87    bool needsRetry() const { return retryFlag; }
88
89    /// The return value
90    int64_t returnValue() const
91    {
92        assert(successful());
93        return value;
94    }
95
96    /// The errno value
97    int errnoValue() const
98    {
99        assert(!successful());
100        return -value;
101    }
102
103    /// The encoded value (as described above)
104    int64_t encodedValue() const
105    {
106        return value;
107    }
108
109  private:
110
111    int64_t value;
112
113    bool retryFlag;
114};
115
116#endif
117