pseudo_inst.cc revision 9457:a4739b6f799d
1/*
2 * Copyright (c) 2010-2011 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder.  You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Copyright (c) 2011 Advanced Micro Devices, Inc.
15 * Copyright (c) 2003-2006 The Regents of The University of Michigan
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions are
20 * met: redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer;
22 * redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution;
25 * neither the name of the copyright holders nor the names of its
26 * contributors may be used to endorse or promote products derived from
27 * this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Authors: Nathan Binkert
42 */
43
44#include <fcntl.h>
45#include <unistd.h>
46
47#include <cerrno>
48#include <fstream>
49#include <string>
50
51#include "arch/kernel_stats.hh"
52#include "arch/vtophys.hh"
53#include "base/debug.hh"
54#include "base/output.hh"
55#include "config/the_isa.hh"
56#include "cpu/base.hh"
57#include "cpu/quiesce_event.hh"
58#include "cpu/thread_context.hh"
59#include "debug/Loader.hh"
60#include "debug/Quiesce.hh"
61#include "debug/WorkItems.hh"
62#include "params/BaseCPU.hh"
63#include "sim/full_system.hh"
64#include "sim/pseudo_inst.hh"
65#include "sim/serialize.hh"
66#include "sim/sim_events.hh"
67#include "sim/sim_exit.hh"
68#include "sim/stat_control.hh"
69#include "sim/stats.hh"
70#include "sim/system.hh"
71#include "sim/vptr.hh"
72
73using namespace std;
74
75using namespace Stats;
76using namespace TheISA;
77
78namespace PseudoInst {
79
80static inline void
81panicFsOnlyPseudoInst(const char *name)
82{
83    panic("Pseudo inst \"%s\" is only available in Full System mode.");
84}
85
86void
87arm(ThreadContext *tc)
88{
89    if (!FullSystem)
90        panicFsOnlyPseudoInst("arm");
91
92    if (tc->getKernelStats())
93        tc->getKernelStats()->arm();
94}
95
96void
97quiesce(ThreadContext *tc)
98{
99    if (!FullSystem)
100        panicFsOnlyPseudoInst("quiesce");
101
102    if (!tc->getCpuPtr()->params()->do_quiesce)
103        return;
104
105    DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
106
107    tc->suspend();
108    if (tc->getKernelStats())
109        tc->getKernelStats()->quiesce();
110}
111
112void
113quiesceSkip(ThreadContext *tc)
114{
115    if (!FullSystem)
116        panicFsOnlyPseudoInst("quiesceSkip");
117
118    BaseCPU *cpu = tc->getCpuPtr();
119
120    if (!cpu->params()->do_quiesce)
121        return;
122
123    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
124
125    Tick resume = curTick() + 1;
126
127    cpu->reschedule(quiesceEvent, resume, true);
128
129    DPRINTF(Quiesce, "%s: quiesceSkip() until %d\n",
130            cpu->name(), resume);
131
132    tc->suspend();
133    if (tc->getKernelStats())
134        tc->getKernelStats()->quiesce();
135}
136
137void
138quiesceNs(ThreadContext *tc, uint64_t ns)
139{
140    if (!FullSystem)
141        panicFsOnlyPseudoInst("quiesceNs");
142
143    BaseCPU *cpu = tc->getCpuPtr();
144
145    if (!cpu->params()->do_quiesce || ns == 0)
146        return;
147
148    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
149
150    Tick resume = curTick() + SimClock::Int::ns * ns;
151
152    cpu->reschedule(quiesceEvent, resume, true);
153
154    DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
155            cpu->name(), ns, resume);
156
157    tc->suspend();
158    if (tc->getKernelStats())
159        tc->getKernelStats()->quiesce();
160}
161
162void
163quiesceCycles(ThreadContext *tc, uint64_t cycles)
164{
165    if (!FullSystem)
166        panicFsOnlyPseudoInst("quiesceCycles");
167
168    BaseCPU *cpu = tc->getCpuPtr();
169
170    if (!cpu->params()->do_quiesce || cycles == 0)
171        return;
172
173    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
174
175    Tick resume = cpu->clockEdge(Cycles(cycles));
176
177    cpu->reschedule(quiesceEvent, resume, true);
178
179    DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
180            cpu->name(), cycles, resume);
181
182    tc->suspend();
183    if (tc->getKernelStats())
184        tc->getKernelStats()->quiesce();
185}
186
187uint64_t
188quiesceTime(ThreadContext *tc)
189{
190    if (!FullSystem) {
191        panicFsOnlyPseudoInst("quiesceTime");
192        return 0;
193    }
194
195    return (tc->readLastActivate() - tc->readLastSuspend()) /
196        SimClock::Int::ns;
197}
198
199uint64_t
200rpns(ThreadContext *tc)
201{
202    return curTick() / SimClock::Int::ns;
203}
204
205void
206wakeCPU(ThreadContext *tc, uint64_t cpuid)
207{
208    System *sys = tc->getSystemPtr();
209    ThreadContext *other_tc = sys->threadContexts[cpuid];
210    if (other_tc->status() == ThreadContext::Suspended)
211        other_tc->activate();
212}
213
214void
215m5exit(ThreadContext *tc, Tick delay)
216{
217    Tick when = curTick() + delay * SimClock::Int::ns;
218    exitSimLoop("m5_exit instruction encountered", 0, when);
219}
220
221void
222m5fail(ThreadContext *tc, Tick delay, uint64_t code)
223{
224    Tick when = curTick() + delay * SimClock::Int::ns;
225    exitSimLoop("m5_fail instruction encountered", code, when);
226}
227
228void
229loadsymbol(ThreadContext *tc)
230{
231    if (!FullSystem)
232        panicFsOnlyPseudoInst("loadsymbol");
233
234    const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
235    if (filename.empty()) {
236        return;
237    }
238
239    std::string buffer;
240    ifstream file(filename.c_str());
241
242    if (!file)
243        fatal("file error: Can't open symbol table file %s\n", filename);
244
245    while (!file.eof()) {
246        getline(file, buffer);
247
248        if (buffer.empty())
249            continue;
250
251        string::size_type idx = buffer.find(' ');
252        if (idx == string::npos)
253            continue;
254
255        string address = "0x" + buffer.substr(0, idx);
256        eat_white(address);
257        if (address.empty())
258            continue;
259
260        // Skip over letter and space
261        string symbol = buffer.substr(idx + 3);
262        eat_white(symbol);
263        if (symbol.empty())
264            continue;
265
266        Addr addr;
267        if (!to_number(address, addr))
268            continue;
269
270        if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
271            continue;
272
273
274        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
275    }
276    file.close();
277}
278
279void
280addsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
281{
282    if (!FullSystem)
283        panicFsOnlyPseudoInst("addSymbol");
284
285    char symb[100];
286    CopyStringOut(tc, symb, symbolAddr, 100);
287    std::string symbol(symb);
288
289    DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
290
291    tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
292    debugSymbolTable->insert(addr,symbol);
293}
294
295uint64_t
296initParam(ThreadContext *tc)
297{
298    if (!FullSystem) {
299        panicFsOnlyPseudoInst("initParam");
300        return 0;
301    }
302
303    return tc->getCpuPtr()->system->init_param;
304}
305
306
307void
308resetstats(ThreadContext *tc, Tick delay, Tick period)
309{
310    if (!tc->getCpuPtr()->params()->do_statistics_insts)
311        return;
312
313
314    Tick when = curTick() + delay * SimClock::Int::ns;
315    Tick repeat = period * SimClock::Int::ns;
316
317    Stats::schedStatEvent(false, true, when, repeat);
318}
319
320void
321dumpstats(ThreadContext *tc, Tick delay, Tick period)
322{
323    if (!tc->getCpuPtr()->params()->do_statistics_insts)
324        return;
325
326
327    Tick when = curTick() + delay * SimClock::Int::ns;
328    Tick repeat = period * SimClock::Int::ns;
329
330    Stats::schedStatEvent(true, false, when, repeat);
331}
332
333void
334dumpresetstats(ThreadContext *tc, Tick delay, Tick period)
335{
336    if (!tc->getCpuPtr()->params()->do_statistics_insts)
337        return;
338
339
340    Tick when = curTick() + delay * SimClock::Int::ns;
341    Tick repeat = period * SimClock::Int::ns;
342
343    Stats::schedStatEvent(true, true, when, repeat);
344}
345
346void
347m5checkpoint(ThreadContext *tc, Tick delay, Tick period)
348{
349    if (!tc->getCpuPtr()->params()->do_checkpoint_insts)
350        return;
351
352    Tick when = curTick() + delay * SimClock::Int::ns;
353    Tick repeat = period * SimClock::Int::ns;
354
355    exitSimLoop("checkpoint", 0, when, repeat);
356}
357
358uint64_t
359readfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
360{
361    if (!FullSystem) {
362        panicFsOnlyPseudoInst("readfile");
363        return 0;
364    }
365
366    const string &file = tc->getSystemPtr()->params()->readfile;
367    if (file.empty()) {
368        return ULL(0);
369    }
370
371    uint64_t result = 0;
372
373    int fd = ::open(file.c_str(), O_RDONLY, 0);
374    if (fd < 0)
375        panic("could not open file %s\n", file);
376
377    if (::lseek(fd, offset, SEEK_SET) < 0)
378        panic("could not seek: %s", strerror(errno));
379
380    char *buf = new char[len];
381    char *p = buf;
382    while (len > 0) {
383        int bytes = ::read(fd, p, len);
384        if (bytes <= 0)
385            break;
386
387        p += bytes;
388        result += bytes;
389        len -= bytes;
390    }
391
392    close(fd);
393    CopyIn(tc, vaddr, buf, result);
394    delete [] buf;
395    return result;
396}
397
398uint64_t
399writefile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset,
400            Addr filename_addr)
401{
402    ostream *os;
403
404    // copy out target filename
405    char fn[100];
406    std::string filename;
407    CopyStringOut(tc, fn, filename_addr, 100);
408    filename = std::string(fn);
409
410    if (offset == 0) {
411        // create a new file (truncate)
412        os = simout.create(filename, true);
413    } else {
414        // do not truncate file if offset is non-zero
415        // (ios::in flag is required as well to keep the existing data
416        //  intact, otherwise existing data will be zeroed out.)
417        os = simout.openFile(simout.directory() + filename,
418                            ios::in | ios::out | ios::binary);
419    }
420    if (!os)
421        panic("could not open file %s\n", filename);
422
423    // seek to offset
424    os->seekp(offset);
425
426    // copy out data and write to file
427    char *buf = new char[len];
428    CopyOut(tc, buf, vaddr, len);
429    os->write(buf, len);
430    if (os->fail() || os->bad())
431        panic("Error while doing writefile!\n");
432
433    simout.close(os);
434
435    delete [] buf;
436
437    return len;
438}
439
440void
441debugbreak(ThreadContext *tc)
442{
443    Debug::breakpoint();
444}
445
446void
447switchcpu(ThreadContext *tc)
448{
449    exitSimLoop("switchcpu");
450}
451
452//
453// This function is executed when annotated work items begin.  Depending on
454// what the user specified at the command line, the simulation may exit and/or
455// take a checkpoint when a certain work item begins.
456//
457void
458workbegin(ThreadContext *tc, uint64_t workid, uint64_t threadid)
459{
460    tc->getCpuPtr()->workItemBegin();
461    System *sys = tc->getSystemPtr();
462    const System::Params *params = sys->params();
463    sys->workItemBegin(threadid, workid);
464
465    DPRINTF(WorkItems, "Work Begin workid: %d, threadid %d\n", workid,
466            threadid);
467
468    //
469    // If specified, determine if this is the specific work item the user
470    // identified
471    //
472    if (params->work_item_id == -1 || params->work_item_id == workid) {
473
474        uint64_t systemWorkBeginCount = sys->incWorkItemsBegin();
475        int cpuId = tc->getCpuPtr()->cpuId();
476
477        if (params->work_cpus_ckpt_count != 0 &&
478            sys->markWorkItem(cpuId) >= params->work_cpus_ckpt_count) {
479            //
480            // If active cpus equals checkpoint count, create checkpoint
481            //
482            exitSimLoop("checkpoint");
483        }
484
485        if (systemWorkBeginCount == params->work_begin_ckpt_count) {
486            //
487            // Note: the string specified as the cause of the exit event must
488            // exactly equal "checkpoint" inorder to create a checkpoint
489            //
490            exitSimLoop("checkpoint");
491        }
492
493        if (systemWorkBeginCount == params->work_begin_exit_count) {
494            //
495            // If a certain number of work items started, exit simulation
496            //
497            exitSimLoop("work started count reach");
498        }
499
500        if (cpuId == params->work_begin_cpu_id_exit) {
501            //
502            // If work started on the cpu id specified, exit simulation
503            //
504            exitSimLoop("work started on specific cpu");
505        }
506    }
507}
508
509//
510// This function is executed when annotated work items end.  Depending on
511// what the user specified at the command line, the simulation may exit and/or
512// take a checkpoint when a certain work item ends.
513//
514void
515workend(ThreadContext *tc, uint64_t workid, uint64_t threadid)
516{
517    tc->getCpuPtr()->workItemEnd();
518    System *sys = tc->getSystemPtr();
519    const System::Params *params = sys->params();
520    sys->workItemEnd(threadid, workid);
521
522    DPRINTF(WorkItems, "Work End workid: %d, threadid %d\n", workid, threadid);
523
524    //
525    // If specified, determine if this is the specific work item the user
526    // identified
527    //
528    if (params->work_item_id == -1 || params->work_item_id == workid) {
529
530        uint64_t systemWorkEndCount = sys->incWorkItemsEnd();
531        int cpuId = tc->getCpuPtr()->cpuId();
532
533        if (params->work_cpus_ckpt_count != 0 &&
534            sys->markWorkItem(cpuId) >= params->work_cpus_ckpt_count) {
535            //
536            // If active cpus equals checkpoint count, create checkpoint
537            //
538            exitSimLoop("checkpoint");
539        }
540
541        if (params->work_end_ckpt_count != 0 &&
542            systemWorkEndCount == params->work_end_ckpt_count) {
543            //
544            // If total work items completed equals checkpoint count, create
545            // checkpoint
546            //
547            exitSimLoop("checkpoint");
548        }
549
550        if (params->work_end_exit_count != 0 &&
551            systemWorkEndCount == params->work_end_exit_count) {
552            //
553            // If total work items completed equals exit count, exit simulation
554            //
555            exitSimLoop("work items exit count reached");
556        }
557    }
558}
559
560} // namespace PseudoInst
561