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