110496Ssteve.reinhardt@amd.com/*
210496Ssteve.reinhardt@amd.com * Copyright (c) 2014 Advanced Micro Devices, Inc.
310496Ssteve.reinhardt@amd.com * All rights reserved
410496Ssteve.reinhardt@amd.com *
510496Ssteve.reinhardt@amd.com * Redistribution and use in source and binary forms, with or without
610496Ssteve.reinhardt@amd.com * modification, are permitted provided that the following conditions are
710496Ssteve.reinhardt@amd.com * met: redistributions of source code must retain the above copyright
810496Ssteve.reinhardt@amd.com * notice, this list of conditions and the following disclaimer;
910496Ssteve.reinhardt@amd.com * redistributions in binary form must reproduce the above copyright
1010496Ssteve.reinhardt@amd.com * notice, this list of conditions and the following disclaimer in the
1110496Ssteve.reinhardt@amd.com * documentation and/or other materials provided with the distribution;
1210496Ssteve.reinhardt@amd.com * neither the name of the copyright holders nor the names of its
1310496Ssteve.reinhardt@amd.com * contributors may be used to endorse or promote products derived from
1410496Ssteve.reinhardt@amd.com * this software without specific prior written permission.
1510496Ssteve.reinhardt@amd.com *
1610496Ssteve.reinhardt@amd.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1710496Ssteve.reinhardt@amd.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1810496Ssteve.reinhardt@amd.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1910496Ssteve.reinhardt@amd.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2010496Ssteve.reinhardt@amd.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2110496Ssteve.reinhardt@amd.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2210496Ssteve.reinhardt@amd.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2310496Ssteve.reinhardt@amd.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2410496Ssteve.reinhardt@amd.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2510496Ssteve.reinhardt@amd.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2610496Ssteve.reinhardt@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710496Ssteve.reinhardt@amd.com *
2810496Ssteve.reinhardt@amd.com * Author: Steve Reinhardt
2910496Ssteve.reinhardt@amd.com */
3010496Ssteve.reinhardt@amd.com
3110496Ssteve.reinhardt@amd.com#ifndef __SIM_EMUL_DRIVER_HH
3210496Ssteve.reinhardt@amd.com#define __SIM_EMUL_DRIVER_HH
3310496Ssteve.reinhardt@amd.com
3410496Ssteve.reinhardt@amd.com#include <string>
3510496Ssteve.reinhardt@amd.com
3610496Ssteve.reinhardt@amd.com#include "params/EmulatedDriver.hh"
3710496Ssteve.reinhardt@amd.com#include "sim/sim_object.hh"
3810496Ssteve.reinhardt@amd.com
3911851Sbrandon.potter@amd.comclass Process;
4010496Ssteve.reinhardt@amd.comclass ThreadContext;
4110496Ssteve.reinhardt@amd.com
4210496Ssteve.reinhardt@amd.com/**
4310496Ssteve.reinhardt@amd.com * EmulatedDriver is an abstract base class for fake SE-mode device drivers.
4410496Ssteve.reinhardt@amd.com *
4510496Ssteve.reinhardt@amd.com * Specific drivers that allow applications to communicate with simulated
4610496Ssteve.reinhardt@amd.com * hardware inside gem5 can be created by deriving from this class and
4710496Ssteve.reinhardt@amd.com * overriding the abstract virtual methods.
4810496Ssteve.reinhardt@amd.com *
4911624Smichael.lebeane@amd.com * Currently only open(), ioctl(), and mmap() calls are supported, but other
5011624Smichael.lebeane@amd.com * calls (e.g., read(), write()) could be added as needed.
5110496Ssteve.reinhardt@amd.com */
5210496Ssteve.reinhardt@amd.comclass EmulatedDriver : public SimObject
5310496Ssteve.reinhardt@amd.com{
5410496Ssteve.reinhardt@amd.com  protected:
5510496Ssteve.reinhardt@amd.com    /**
5610496Ssteve.reinhardt@amd.com     * filename for opening this driver (under /dev)
5710496Ssteve.reinhardt@amd.com     */
5810496Ssteve.reinhardt@amd.com    const std::string &filename;
5910496Ssteve.reinhardt@amd.com
6010496Ssteve.reinhardt@amd.com  public:
6110496Ssteve.reinhardt@amd.com    EmulatedDriver(EmulatedDriverParams *p)
6210496Ssteve.reinhardt@amd.com        : SimObject(p), filename(p->filename)
6310496Ssteve.reinhardt@amd.com    {
6410496Ssteve.reinhardt@amd.com    }
6510496Ssteve.reinhardt@amd.com
6610496Ssteve.reinhardt@amd.com    /**
6710496Ssteve.reinhardt@amd.com     * Check for a match with this driver's filename.
6810496Ssteve.reinhardt@amd.com     */
6910496Ssteve.reinhardt@amd.com    bool match(const std::string &s) const { return (s == filename); }
7010496Ssteve.reinhardt@amd.com
7110496Ssteve.reinhardt@amd.com    /**
7210496Ssteve.reinhardt@amd.com     * Abstract method, invoked when the user program calls open() on
7310496Ssteve.reinhardt@amd.com     * the device driver.  The parameters are the same as those passed
7410496Ssteve.reinhardt@amd.com     * to openFunc() (q.v.).
7510496Ssteve.reinhardt@amd.com     * @return A newly allocated target fd, or -1 on error.
7610496Ssteve.reinhardt@amd.com     */
7713995Sbrandon.potter@amd.com    virtual int open(ThreadContext *tc, int mode, int flags) = 0;
7810496Ssteve.reinhardt@amd.com
7910496Ssteve.reinhardt@amd.com    /**
8010496Ssteve.reinhardt@amd.com     * Abstract method, invoked when the user program calls ioctl() on
8110496Ssteve.reinhardt@amd.com     * the file descriptor returned by a previous open().  The parameters
8210496Ssteve.reinhardt@amd.com     * are the same as those passed in to ioctlFunc() (q.v.).
8310496Ssteve.reinhardt@amd.com     * @return The return code for the ioctl, or the negation of the errno
8410496Ssteve.reinhardt@amd.com     * (see the SyscallReturn class).
8510496Ssteve.reinhardt@amd.com     */
8613995Sbrandon.potter@amd.com    virtual int ioctl(ThreadContext *tc, unsigned req) = 0;
8711624Smichael.lebeane@amd.com
8811624Smichael.lebeane@amd.com    /**
8911624Smichael.lebeane@amd.com     * Virtual method, invoked when the user program calls mmap() on
9011624Smichael.lebeane@amd.com     * the file descriptor returned by a previous open().  The parameters
9111624Smichael.lebeane@amd.com     * are the same as those passed in to mmapFunc() (q.v.).
9211624Smichael.lebeane@amd.com     * @return The return ptr for the mmap, or the negation of the errno
9311624Smichael.lebeane@amd.com     * (see the SyscallReturn class).
9411624Smichael.lebeane@amd.com     */
9513995Sbrandon.potter@amd.com    virtual Addr mmap(ThreadContext *tc, Addr start, uint64_t length,
9613995Sbrandon.potter@amd.com                      int prot, int tgtFlags, int tgtFd, int offset)
9713995Sbrandon.potter@amd.com                      { return -EBADF; }
9810496Ssteve.reinhardt@amd.com};
9910496Ssteve.reinhardt@amd.com
10010496Ssteve.reinhardt@amd.com#endif // __SIM_EMUL_DRIVER_HH
101