13761SN/A/*
23761SN/A * Copyright (c) 2003-2005 The Regents of The University of Michigan
33761SN/A * All rights reserved.
43761SN/A *
53761SN/A * Redistribution and use in source and binary forms, with or without
63761SN/A * modification, are permitted provided that the following conditions are
73761SN/A * met: redistributions of source code must retain the above copyright
83761SN/A * notice, this list of conditions and the following disclaimer;
93761SN/A * redistributions in binary form must reproduce the above copyright
103761SN/A * notice, this list of conditions and the following disclaimer in the
113761SN/A * documentation and/or other materials provided with the distribution;
123761SN/A * neither the name of the copyright holders nor the names of its
133761SN/A * contributors may be used to endorse or promote products derived from
143761SN/A * this software without specific prior written permission.
153761SN/A *
163761SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
173761SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
183761SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
193761SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
203761SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
213761SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
223761SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233761SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243761SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253761SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
263761SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273761SN/A *
283761SN/A * Authors: Gabe Black
293761SN/A */
303761SN/A
313761SN/A#ifndef __SIM_SYSCALLRETURN_HH__
323761SN/A#define __SIM_SYSCALLRETURN_HH__
333761SN/A
3411800Sbrandon.potter@amd.com#include <inttypes.h>
353761SN/A
3610223SN/A/**
3710223SN/A * This class represents the return value from an emulated system call,
3810223SN/A * including any errno setting.
3910223SN/A *
4010223SN/A * On some platforms, the return value and errno are encoded in a
4110223SN/A * single signed integer.  A value less than zero but greater than
4210223SN/A * -4096 indicates an error, and the value is the negation of the
4310223SN/A * errno value.  Otherwise, the call was successful and the integer is
4410223SN/A * the return value.  (Large negative numbers are considered
4510223SN/A * successful to allow syscalls to return pointers to high memory,
4610223SN/A * e.g., stack addresses.)  See, for example, Appendix A of the AMD64
4710223SN/A * ABI spec at http://www.x86-64.org/documentation/abi.pdf.
4810223SN/A *
4910223SN/A * Other platforms use a more complex interface, returning a value and
5010223SN/A * an error code in separate registers.
5110223SN/A *
5210223SN/A * This class is designed to support both types of interfaces.
5310223SN/A */
543761SN/Aclass SyscallReturn
553761SN/A{
563761SN/A  public:
573761SN/A
5810223SN/A    /// For simplicity, allow the object to be initialized with a
5910223SN/A    /// single signed integer using the same positive=success,
6010223SN/A    /// negative=-errno convention described above.
6110223SN/A    ///
6210223SN/A    /// Typically this constructor is used as a default type
6310223SN/A    /// conversion, so a bare integer is used where a SyscallReturn
6410223SN/A    /// value is expected, e.g., as the return value from a system
6510223SN/A    /// call emulation function ('return 0;' or 'return -EFAULT;').
6610223SN/A    SyscallReturn(int64_t v)
6710500SN/A        : value(v), retryFlag(false)
6810223SN/A    {}
693761SN/A
7010500SN/A    /// Pseudo-constructor to create an instance with the retry flag set.
7110500SN/A    static SyscallReturn retry()
7210500SN/A    {
7310500SN/A        SyscallReturn s(0);
7410500SN/A        s.retryFlag = true;
7510500SN/A        return s;
7610500SN/A    }
7710500SN/A
783761SN/A    ~SyscallReturn() {}
793761SN/A
8010223SN/A    /// Was the system call successful?
8110223SN/A    bool successful() const
823761SN/A    {
8310223SN/A        return (value >= 0 || value <= -4096);
843761SN/A    }
853761SN/A
8610500SN/A    /// Does the syscall need to be retried?
8710500SN/A    bool needsRetry() const { return retryFlag; }
8810500SN/A
8910223SN/A    /// The return value
9010223SN/A    int64_t returnValue() const
9110223SN/A    {
9210223SN/A        assert(successful());
9310223SN/A        return value;
9410223SN/A    }
953761SN/A
9610223SN/A    /// The errno value
9710223SN/A    int errnoValue() const
9810223SN/A    {
9910223SN/A        assert(!successful());
10010223SN/A        return -value;
10110223SN/A    }
10210223SN/A
10310223SN/A    /// The encoded value (as described above)
10410223SN/A    int64_t encodedValue() const
10510223SN/A    {
10610223SN/A        return value;
10710223SN/A    }
10810223SN/A
10910223SN/A  private:
11010223SN/A
11110223SN/A    int64_t value;
11210500SN/A
11310500SN/A    bool retryFlag;
1143761SN/A};
1153761SN/A
1163761SN/A#endif
117