pseudo_inst.cc revision 2080
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#include <errno.h>
30#include <fcntl.h>
31#include <unistd.h>
32#include <cstdio>
33
34#include <string>
35
36#include "arch/alpha/pseudo_inst.hh"
37#include "arch/alpha/vtophys.hh"
38#include "cpu/base.hh"
39#include "cpu/sampler/sampler.hh"
40#include "cpu/exec_context.hh"
41#include "kern/kernel_stats.hh"
42#include "sim/param.hh"
43#include "sim/serialize.hh"
44#include "sim/sim_exit.hh"
45#include "sim/stat_control.hh"
46#include "sim/stats.hh"
47#include "sim/system.hh"
48#include "sim/debug.hh"
49#include "sim/vptr.hh"
50
51using namespace std;
52
53extern Sampler *SampCPU;
54
55using namespace Stats;
56
57namespace AlphaPseudo
58{
59    bool doStatisticsInsts;
60    bool doCheckpointInsts;
61    bool doQuiesce;
62
63    void
64    arm(ExecContext *xc)
65    {
66        xc->kernelStats->arm();
67    }
68
69    void
70    quiesce(ExecContext *xc)
71    {
72        if (!doQuiesce)
73            return;
74
75        xc->suspend();
76        xc->kernelStats->quiesce();
77    }
78
79    void
80    ivlb(ExecContext *xc)
81    {
82        xc->kernelStats->ivlb();
83    }
84
85    void
86    ivle(ExecContext *xc)
87    {
88    }
89
90    void
91    m5exit_old(ExecContext *xc)
92    {
93        SimExit(curTick, "m5_exit_old instruction encountered");
94    }
95
96    void
97    m5exit(ExecContext *xc)
98    {
99        Tick delay = xc->regs.intRegFile[16];
100        Tick when = curTick + delay * Clock::Int::ns;
101        SimExit(when, "m5_exit instruction encountered");
102    }
103
104    void
105    resetstats(ExecContext *xc)
106    {
107        if (!doStatisticsInsts)
108            return;
109
110        Tick delay = xc->regs.intRegFile[16];
111        Tick period = xc->regs.intRegFile[17];
112
113        Tick when = curTick + delay * Clock::Int::ns;
114        Tick repeat = period * Clock::Int::ns;
115
116        using namespace Stats;
117        SetupEvent(Reset, when, repeat);
118    }
119
120    void
121    dumpstats(ExecContext *xc)
122    {
123        if (!doStatisticsInsts)
124            return;
125
126        Tick delay = xc->regs.intRegFile[16];
127        Tick period = xc->regs.intRegFile[17];
128
129        Tick when = curTick + delay * Clock::Int::ns;
130        Tick repeat = period * Clock::Int::ns;
131
132        using namespace Stats;
133        SetupEvent(Dump, when, repeat);
134    }
135
136    void
137    addsymbol(ExecContext *xc)
138    {
139        Addr addr = xc->regs.intRegFile[16];
140        char symb[100];
141        CopyString(xc, symb, xc->regs.intRegFile[17], 100);
142        std::string symbol(symb);
143
144        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
145
146        xc->system->kernelSymtab->insert(addr,symbol);
147    }
148
149    void
150    dumpresetstats(ExecContext *xc)
151    {
152        if (!doStatisticsInsts)
153            return;
154
155        Tick delay = xc->regs.intRegFile[16];
156        Tick period = xc->regs.intRegFile[17];
157
158        Tick when = curTick + delay * Clock::Int::ns;
159        Tick repeat = period * Clock::Int::ns;
160
161        using namespace Stats;
162        SetupEvent(Dump|Reset, when, repeat);
163    }
164
165    void
166    m5checkpoint(ExecContext *xc)
167    {
168        if (!doCheckpointInsts)
169            return;
170
171        Tick delay = xc->regs.intRegFile[16];
172        Tick period = xc->regs.intRegFile[17];
173
174        Tick when = curTick + delay * Clock::Int::ns;
175        Tick repeat = period * Clock::Int::ns;
176
177        Checkpoint::setup(when, repeat);
178    }
179
180    void
181    readfile(ExecContext *xc)
182    {
183        const string &file = xc->cpu->system->params->readfile;
184        if (file.empty()) {
185            xc->regs.intRegFile[0] = ULL(0);
186            return;
187        }
188
189        Addr vaddr = xc->regs.intRegFile[16];
190        uint64_t len = xc->regs.intRegFile[17];
191        uint64_t offset = xc->regs.intRegFile[18];
192        uint64_t result = 0;
193
194        int fd = ::open(file.c_str(), O_RDONLY, 0);
195        if (fd < 0)
196            panic("could not open file %s\n", file);
197
198        if (::lseek(fd, offset, SEEK_SET) < 0)
199            panic("could not seek: %s", strerror(errno));
200
201        char *buf = new char[len];
202        char *p = buf;
203        while (len > 0) {
204            int bytes = ::read(fd, p, len);
205            if (bytes <= 0)
206                break;
207
208            p += bytes;
209            result += bytes;
210            len -= bytes;
211        }
212
213        close(fd);
214        CopyIn(xc, vaddr, buf, result);
215        delete [] buf;
216        xc->regs.intRegFile[0] = result;
217    }
218
219    class Context : public ParamContext
220    {
221      public:
222        Context(const string &section) : ParamContext(section) {}
223        void checkParams();
224    };
225
226    Context context("pseudo_inst");
227
228    Param<bool> __quiesce(&context, "quiesce",
229                          "enable quiesce instructions",
230                          true);
231    Param<bool> __statistics(&context, "statistics",
232                             "enable statistics pseudo instructions",
233                             true);
234    Param<bool> __checkpoint(&context, "checkpoint",
235                             "enable checkpoint pseudo instructions",
236                             true);
237
238    void
239    Context::checkParams()
240    {
241        doQuiesce = __quiesce;
242        doStatisticsInsts = __statistics;
243        doCheckpointInsts = __checkpoint;
244    }
245
246    void debugbreak(ExecContext *xc)
247    {
248        debug_break();
249    }
250
251    void switchcpu(ExecContext *xc)
252    {
253        if (SampCPU)
254            SampCPU->switchCPUs();
255    }
256}
257