1/* 2 * Copyright (c) 2014 Advanced Micro Devices, Inc. 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 * Author: Steve Reinhardt 29 */ 30 31#ifndef __SIM_EMUL_DRIVER_HH 32#define __SIM_EMUL_DRIVER_HH 33 34#include <string> 35 36#include "params/EmulatedDriver.hh" 37#include "sim/sim_object.hh" 38 39class Process; 40class ThreadContext; 41 42/** 43 * EmulatedDriver is an abstract base class for fake SE-mode device drivers. 44 * 45 * Specific drivers that allow applications to communicate with simulated 46 * hardware inside gem5 can be created by deriving from this class and 47 * overriding the abstract virtual methods. 48 * 49 * Currently only open(), ioctl(), and mmap() calls are supported, but other 50 * calls (e.g., read(), write()) could be added as needed. 51 */ 52class EmulatedDriver : public SimObject 53{ 54 protected: 55 /** 56 * filename for opening this driver (under /dev) 57 */ 58 const std::string &filename; 59 60 public: 61 EmulatedDriver(EmulatedDriverParams *p) 62 : SimObject(p), filename(p->filename) 63 { 64 } 65 66 /** 67 * Check for a match with this driver's filename. 68 */ 69 bool match(const std::string &s) const { return (s == filename); } 70 71 /** 72 * Abstract method, invoked when the user program calls open() on 73 * the device driver. The parameters are the same as those passed 74 * to openFunc() (q.v.). 75 * @return A newly allocated target fd, or -1 on error. 76 */ 77 virtual int open(ThreadContext *tc, int mode, int flags) = 0; 78 79 /** 80 * Abstract method, invoked when the user program calls ioctl() on 81 * the file descriptor returned by a previous open(). The parameters 82 * are the same as those passed in to ioctlFunc() (q.v.). 83 * @return The return code for the ioctl, or the negation of the errno 84 * (see the SyscallReturn class). 85 */ 86 virtual int ioctl(ThreadContext *tc, unsigned req) = 0; 87 88 /** 89 * Virtual method, invoked when the user program calls mmap() on 90 * the file descriptor returned by a previous open(). The parameters 91 * are the same as those passed in to mmapFunc() (q.v.). 92 * @return The return ptr for the mmap, or the negation of the errno 93 * (see the SyscallReturn class). 94 */ 95 virtual Addr mmap(ThreadContext *tc, Addr start, uint64_t length, 96 int prot, int tgtFlags, int tgtFd, int offset) 97 { return -EBADF; } 98}; 99 100#endif // __SIM_EMUL_DRIVER_HH 101