pseudo_inst.cc revision 5741
1298SN/A/*
22188SN/A * Copyright (c) 2003-2006 The Regents of The University of Michigan
3298SN/A * All rights reserved.
4298SN/A *
5298SN/A * Redistribution and use in source and binary forms, with or without
6298SN/A * modification, are permitted provided that the following conditions are
7298SN/A * met: redistributions of source code must retain the above copyright
8298SN/A * notice, this list of conditions and the following disclaimer;
9298SN/A * redistributions in binary form must reproduce the above copyright
10298SN/A * notice, this list of conditions and the following disclaimer in the
11298SN/A * documentation and/or other materials provided with the distribution;
12298SN/A * neither the name of the copyright holders nor the names of its
13298SN/A * contributors may be used to endorse or promote products derived from
14298SN/A * this software without specific prior written permission.
15298SN/A *
16298SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17298SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18298SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19298SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20298SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21298SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22298SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23298SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24298SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25298SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26298SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
29298SN/A */
30298SN/A
311642SN/A#include <errno.h>
32954SN/A#include <fcntl.h>
33956SN/A#include <unistd.h>
34956SN/A
354078Sbinkertn@umich.edu#include <fstream>
36299SN/A#include <string>
37299SN/A
385529Snate@binkert.org#include "arch/kernel_stats.hh"
392170SN/A#include "arch/vtophys.hh"
403089Ssaidi@eecs.umich.edu#include "base/annotate.hh"
411717SN/A#include "cpu/base.hh"
422680Sktlim@umich.edu#include "cpu/thread_context.hh"
432313SN/A#include "cpu/quiesce_event.hh"
445529Snate@binkert.org#include "params/BaseCPU.hh"
453565Sgblack@eecs.umich.edu#include "sim/pseudo_inst.hh"
46298SN/A#include "sim/serialize.hh"
475606Snate@binkert.org#include "sim/sim_events.hh"
48298SN/A#include "sim/sim_exit.hh"
49695SN/A#include "sim/stat_control.hh"
50695SN/A#include "sim/stats.hh"
51954SN/A#include "sim/system.hh"
521052SN/A#include "sim/debug.hh"
532080SN/A#include "sim/vptr.hh"
54298SN/A
55299SN/Ausing namespace std;
561052SN/A
57729SN/Ausing namespace Stats;
582107SN/Ausing namespace TheISA;
59298SN/A
605504Snate@binkert.orgnamespace PseudoInst {
615504Snate@binkert.org
625504Snate@binkert.orgvoid
635504Snate@binkert.orgarm(ThreadContext *tc)
64298SN/A{
655504Snate@binkert.org    if (tc->getKernelStats())
665504Snate@binkert.org        tc->getKernelStats()->arm();
675504Snate@binkert.org}
685504Snate@binkert.org
695504Snate@binkert.orgvoid
705504Snate@binkert.orgquiesce(ThreadContext *tc)
715504Snate@binkert.org{
725529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce)
735504Snate@binkert.org        return;
745504Snate@binkert.org
755504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
765504Snate@binkert.org
775504Snate@binkert.org    tc->suspend();
785504Snate@binkert.org    if (tc->getKernelStats())
795504Snate@binkert.org        tc->getKernelStats()->quiesce();
805504Snate@binkert.org}
815504Snate@binkert.org
825504Snate@binkert.orgvoid
835504Snate@binkert.orgquiesceNs(ThreadContext *tc, uint64_t ns)
845504Snate@binkert.org{
855529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce || ns == 0)
865504Snate@binkert.org        return;
875504Snate@binkert.org
885504Snate@binkert.org    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
895504Snate@binkert.org
905504Snate@binkert.org    Tick resume = curTick + Clock::Int::ns * ns;
915504Snate@binkert.org
925606Snate@binkert.org    mainEventQueue.reschedule(quiesceEvent, resume, true);
935504Snate@binkert.org
945504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
955504Snate@binkert.org            tc->getCpuPtr()->name(), ns, resume);
965504Snate@binkert.org
975504Snate@binkert.org    tc->suspend();
985504Snate@binkert.org    if (tc->getKernelStats())
995504Snate@binkert.org        tc->getKernelStats()->quiesce();
1005504Snate@binkert.org}
1015504Snate@binkert.org
1025504Snate@binkert.orgvoid
1035504Snate@binkert.orgquiesceCycles(ThreadContext *tc, uint64_t cycles)
1045504Snate@binkert.org{
1055529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce || cycles == 0)
1065504Snate@binkert.org        return;
1075504Snate@binkert.org
1085504Snate@binkert.org    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
1095504Snate@binkert.org
1105504Snate@binkert.org    Tick resume = curTick + tc->getCpuPtr()->ticks(cycles);
1115504Snate@binkert.org
1125606Snate@binkert.org    mainEventQueue.reschedule(quiesceEvent, resume, true);
1135504Snate@binkert.org
1145504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
1155504Snate@binkert.org            tc->getCpuPtr()->name(), cycles, resume);
1165504Snate@binkert.org
1175504Snate@binkert.org    tc->suspend();
1185504Snate@binkert.org    if (tc->getKernelStats())
1195504Snate@binkert.org        tc->getKernelStats()->quiesce();
1205504Snate@binkert.org}
1215504Snate@binkert.org
1225504Snate@binkert.orguint64_t
1235504Snate@binkert.orgquiesceTime(ThreadContext *tc)
1245504Snate@binkert.org{
1255504Snate@binkert.org    return (tc->readLastActivate() - tc->readLastSuspend()) / Clock::Int::ns;
1265504Snate@binkert.org}
1275504Snate@binkert.org
1285741Snate@binkert.orguint64_t
1295741Snate@binkert.orgrpns(ThreadContext *tc)
1305741Snate@binkert.org{
1315741Snate@binkert.org    return curTick / Clock::Int::ns;
1325741Snate@binkert.org}
1335741Snate@binkert.org
1345504Snate@binkert.orgvoid
1355504Snate@binkert.orgm5exit(ThreadContext *tc, Tick delay)
1365504Snate@binkert.org{
1375504Snate@binkert.org    Tick when = curTick + delay * Clock::Int::ns;
1385606Snate@binkert.org    Event *event = new SimLoopExitEvent("m5_exit instruction encountered", 0);
1395606Snate@binkert.org    mainEventQueue.schedule(event, when);
1405504Snate@binkert.org}
1415504Snate@binkert.org
1425504Snate@binkert.orgvoid
1435504Snate@binkert.orgloadsymbol(ThreadContext *tc)
1445504Snate@binkert.org{
1455504Snate@binkert.org    const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
1465504Snate@binkert.org    if (filename.empty()) {
1475504Snate@binkert.org        return;
148711SN/A    }
149711SN/A
1505504Snate@binkert.org    std::string buffer;
1515504Snate@binkert.org    ifstream file(filename.c_str());
152310SN/A
1535504Snate@binkert.org    if (!file)
1545504Snate@binkert.org        fatal("file error: Can't open symbol table file %s\n", filename);
1553373Sstever@eecs.umich.edu
1565504Snate@binkert.org    while (!file.eof()) {
1575504Snate@binkert.org        getline(file, buffer);
1585504Snate@binkert.org
1595504Snate@binkert.org        if (buffer.empty())
1605504Snate@binkert.org            continue;
1615504Snate@binkert.org
1625504Snate@binkert.org        int idx = buffer.find(' ');
1635504Snate@binkert.org        if (idx == string::npos)
1645504Snate@binkert.org            continue;
1655504Snate@binkert.org
1665504Snate@binkert.org        string address = "0x" + buffer.substr(0, idx);
1675504Snate@binkert.org        eat_white(address);
1685504Snate@binkert.org        if (address.empty())
1695504Snate@binkert.org            continue;
1705504Snate@binkert.org
1715504Snate@binkert.org        // Skip over letter and space
1725504Snate@binkert.org        string symbol = buffer.substr(idx + 3);
1735504Snate@binkert.org        eat_white(symbol);
1745504Snate@binkert.org        if (symbol.empty())
1755504Snate@binkert.org            continue;
1765504Snate@binkert.org
1775504Snate@binkert.org        Addr addr;
1785504Snate@binkert.org        if (!to_number(address, addr))
1795504Snate@binkert.org            continue;
1805504Snate@binkert.org
1815504Snate@binkert.org        if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
1825504Snate@binkert.org            continue;
1835504Snate@binkert.org
1845504Snate@binkert.org
1855504Snate@binkert.org        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
1865504Snate@binkert.org    }
1875504Snate@binkert.org    file.close();
1885504Snate@binkert.org}
1895504Snate@binkert.org
1905504Snate@binkert.orgvoid
1915504Snate@binkert.orgresetstats(ThreadContext *tc, Tick delay, Tick period)
1925504Snate@binkert.org{
1935529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
1945504Snate@binkert.org        return;
1955504Snate@binkert.org
1965504Snate@binkert.org
1975504Snate@binkert.org    Tick when = curTick + delay * Clock::Int::ns;
1985504Snate@binkert.org    Tick repeat = period * Clock::Int::ns;
1995504Snate@binkert.org
2005504Snate@binkert.org    Stats::StatEvent(false, true, when, repeat);
2015504Snate@binkert.org}
2025504Snate@binkert.org
2035504Snate@binkert.orgvoid
2045504Snate@binkert.orgdumpstats(ThreadContext *tc, Tick delay, Tick period)
2055504Snate@binkert.org{
2065529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
2075504Snate@binkert.org        return;
2085504Snate@binkert.org
2095504Snate@binkert.org
2105504Snate@binkert.org    Tick when = curTick + delay * Clock::Int::ns;
2115504Snate@binkert.org    Tick repeat = period * Clock::Int::ns;
2125504Snate@binkert.org
2135504Snate@binkert.org    Stats::StatEvent(true, false, when, repeat);
2145504Snate@binkert.org}
2155504Snate@binkert.org
2165504Snate@binkert.orgvoid
2175504Snate@binkert.orgaddsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
2185504Snate@binkert.org{
2195504Snate@binkert.org    char symb[100];
2205504Snate@binkert.org    CopyStringOut(tc, symb, symbolAddr, 100);
2215504Snate@binkert.org    std::string symbol(symb);
2225504Snate@binkert.org
2235504Snate@binkert.org    DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
2245504Snate@binkert.org
2255504Snate@binkert.org    tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
2265504Snate@binkert.org}
2275504Snate@binkert.org
2285504Snate@binkert.orgvoid
2295504Snate@binkert.orgdumpresetstats(ThreadContext *tc, Tick delay, Tick period)
2305504Snate@binkert.org{
2315529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
2325504Snate@binkert.org        return;
2335504Snate@binkert.org
2345504Snate@binkert.org
2355504Snate@binkert.org    Tick when = curTick + delay * Clock::Int::ns;
2365504Snate@binkert.org    Tick repeat = period * Clock::Int::ns;
2375504Snate@binkert.org
2385504Snate@binkert.org    Stats::StatEvent(true, true, when, repeat);
2395504Snate@binkert.org}
2405504Snate@binkert.org
2415504Snate@binkert.orgvoid
2425504Snate@binkert.orgm5checkpoint(ThreadContext *tc, Tick delay, Tick period)
2435504Snate@binkert.org{
2445529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_checkpoint_insts)
2455504Snate@binkert.org        return;
2465504Snate@binkert.org
2475504Snate@binkert.org    Tick when = curTick + delay * Clock::Int::ns;
2485504Snate@binkert.org    Tick repeat = period * Clock::Int::ns;
2495504Snate@binkert.org
2505606Snate@binkert.org    Event *event = new SimLoopExitEvent("checkpoint", 0, repeat);
2515606Snate@binkert.org    mainEventQueue.schedule(event, when);
2525504Snate@binkert.org}
2535504Snate@binkert.org
2545504Snate@binkert.orguint64_t
2555504Snate@binkert.orgreadfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
2565504Snate@binkert.org{
2575504Snate@binkert.org    const string &file = tc->getSystemPtr()->params()->readfile;
2585504Snate@binkert.org    if (file.empty()) {
2595504Snate@binkert.org        return ULL(0);
260310SN/A    }
261299SN/A
2625504Snate@binkert.org    uint64_t result = 0;
2632188SN/A
2645504Snate@binkert.org    int fd = ::open(file.c_str(), O_RDONLY, 0);
2655504Snate@binkert.org    if (fd < 0)
2665504Snate@binkert.org        panic("could not open file %s\n", file);
2672235SN/A
2685504Snate@binkert.org    if (::lseek(fd, offset, SEEK_SET) < 0)
2695504Snate@binkert.org        panic("could not seek: %s", strerror(errno));
2703368Sstever@eecs.umich.edu
2715504Snate@binkert.org    char *buf = new char[len];
2725504Snate@binkert.org    char *p = buf;
2735504Snate@binkert.org    while (len > 0) {
2745504Snate@binkert.org        int bytes = ::read(fd, p, len);
2755504Snate@binkert.org        if (bytes <= 0)
2765504Snate@binkert.org            break;
2773368Sstever@eecs.umich.edu
2785504Snate@binkert.org        p += bytes;
2795504Snate@binkert.org        result += bytes;
2805504Snate@binkert.org        len -= bytes;
2812188SN/A    }
2822188SN/A
2835504Snate@binkert.org    close(fd);
2845504Snate@binkert.org    CopyIn(tc, vaddr, buf, result);
2855504Snate@binkert.org    delete [] buf;
2865504Snate@binkert.org    return result;
2875504Snate@binkert.org}
2882188SN/A
2895504Snate@binkert.orgvoid
2905504Snate@binkert.orgdebugbreak(ThreadContext *tc)
2915504Snate@binkert.org{
2925504Snate@binkert.org    debug_break();
2935504Snate@binkert.org}
2942235SN/A
2955504Snate@binkert.orgvoid
2965504Snate@binkert.orgswitchcpu(ThreadContext *tc)
2975504Snate@binkert.org{
2985504Snate@binkert.org    exitSimLoop("switchcpu");
2995504Snate@binkert.org}
3003368Sstever@eecs.umich.edu
3015504Snate@binkert.org/* namespace PseudoInst */ }
302