pseudo_inst.cc revision 7811
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
382170SN/A#include "arch/vtophys.hh"
395882Snate@binkert.org#include "base/debug.hh"
406658Snate@binkert.org#include "config/full_system.hh"
416658Snate@binkert.org#include "config/the_isa.hh"
421717SN/A#include "cpu/base.hh"
432680Sktlim@umich.edu#include "cpu/thread_context.hh"
442313SN/A#include "cpu/quiesce_event.hh"
455529Snate@binkert.org#include "params/BaseCPU.hh"
463565Sgblack@eecs.umich.edu#include "sim/pseudo_inst.hh"
47298SN/A#include "sim/serialize.hh"
485606Snate@binkert.org#include "sim/sim_events.hh"
49298SN/A#include "sim/sim_exit.hh"
50695SN/A#include "sim/stat_control.hh"
51695SN/A#include "sim/stats.hh"
52954SN/A#include "sim/system.hh"
536118Snate@binkert.org
545780Ssteve.reinhardt@amd.com#if FULL_SYSTEM
556118Snate@binkert.org#include "arch/kernel_stats.hh"
562080SN/A#include "sim/vptr.hh"
575780Ssteve.reinhardt@amd.com#endif
58298SN/A
59299SN/Ausing namespace std;
601052SN/A
61729SN/Ausing namespace Stats;
622107SN/Ausing namespace TheISA;
63298SN/A
645504Snate@binkert.orgnamespace PseudoInst {
655504Snate@binkert.org
665780Ssteve.reinhardt@amd.com#if FULL_SYSTEM
675780Ssteve.reinhardt@amd.com
685504Snate@binkert.orgvoid
695504Snate@binkert.orgarm(ThreadContext *tc)
70298SN/A{
715504Snate@binkert.org    if (tc->getKernelStats())
725504Snate@binkert.org        tc->getKernelStats()->arm();
735504Snate@binkert.org}
745504Snate@binkert.org
755504Snate@binkert.orgvoid
765504Snate@binkert.orgquiesce(ThreadContext *tc)
775504Snate@binkert.org{
785529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce)
795504Snate@binkert.org        return;
805504Snate@binkert.org
815504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesce()\n", tc->getCpuPtr()->name());
825504Snate@binkert.org
835504Snate@binkert.org    tc->suspend();
845504Snate@binkert.org    if (tc->getKernelStats())
855504Snate@binkert.org        tc->getKernelStats()->quiesce();
865504Snate@binkert.org}
875504Snate@binkert.org
885504Snate@binkert.orgvoid
895504Snate@binkert.orgquiesceNs(ThreadContext *tc, uint64_t ns)
905504Snate@binkert.org{
915529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce || ns == 0)
925504Snate@binkert.org        return;
935504Snate@binkert.org
945504Snate@binkert.org    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
955504Snate@binkert.org
967064Snate@binkert.org    Tick resume = curTick + SimClock::Int::ns * ns;
975504Snate@binkert.org
985606Snate@binkert.org    mainEventQueue.reschedule(quiesceEvent, resume, true);
995504Snate@binkert.org
1005504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesceNs(%d) until %d\n",
1015504Snate@binkert.org            tc->getCpuPtr()->name(), ns, resume);
1025504Snate@binkert.org
1035504Snate@binkert.org    tc->suspend();
1045504Snate@binkert.org    if (tc->getKernelStats())
1055504Snate@binkert.org        tc->getKernelStats()->quiesce();
1065504Snate@binkert.org}
1075504Snate@binkert.org
1085504Snate@binkert.orgvoid
1095504Snate@binkert.orgquiesceCycles(ThreadContext *tc, uint64_t cycles)
1105504Snate@binkert.org{
1115529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_quiesce || cycles == 0)
1125504Snate@binkert.org        return;
1135504Snate@binkert.org
1145504Snate@binkert.org    EndQuiesceEvent *quiesceEvent = tc->getQuiesceEvent();
1155504Snate@binkert.org
1165504Snate@binkert.org    Tick resume = curTick + tc->getCpuPtr()->ticks(cycles);
1175504Snate@binkert.org
1185606Snate@binkert.org    mainEventQueue.reschedule(quiesceEvent, resume, true);
1195504Snate@binkert.org
1205504Snate@binkert.org    DPRINTF(Quiesce, "%s: quiesceCycles(%d) until %d\n",
1215504Snate@binkert.org            tc->getCpuPtr()->name(), cycles, resume);
1225504Snate@binkert.org
1235504Snate@binkert.org    tc->suspend();
1245504Snate@binkert.org    if (tc->getKernelStats())
1255504Snate@binkert.org        tc->getKernelStats()->quiesce();
1265504Snate@binkert.org}
1275504Snate@binkert.org
1285504Snate@binkert.orguint64_t
1295504Snate@binkert.orgquiesceTime(ThreadContext *tc)
1305504Snate@binkert.org{
1317064Snate@binkert.org    return (tc->readLastActivate() - tc->readLastSuspend()) /
1327064Snate@binkert.org        SimClock::Int::ns;
1335504Snate@binkert.org}
1345504Snate@binkert.org
1355780Ssteve.reinhardt@amd.com#endif
1365780Ssteve.reinhardt@amd.com
1375741Snate@binkert.orguint64_t
1385741Snate@binkert.orgrpns(ThreadContext *tc)
1395741Snate@binkert.org{
1407064Snate@binkert.org    return curTick / SimClock::Int::ns;
1415741Snate@binkert.org}
1425741Snate@binkert.org
1435504Snate@binkert.orgvoid
1445808Snate@binkert.orgwakeCPU(ThreadContext *tc, uint64_t cpuid)
1455808Snate@binkert.org{
1465808Snate@binkert.org    System *sys = tc->getSystemPtr();
1475808Snate@binkert.org    ThreadContext *other_tc = sys->threadContexts[cpuid];
1485808Snate@binkert.org    if (other_tc->status() == ThreadContext::Suspended)
1495808Snate@binkert.org        other_tc->activate();
1505808Snate@binkert.org}
1515808Snate@binkert.org
1525808Snate@binkert.orgvoid
1535504Snate@binkert.orgm5exit(ThreadContext *tc, Tick delay)
1545504Snate@binkert.org{
1557064Snate@binkert.org    Tick when = curTick + delay * SimClock::Int::ns;
1565606Snate@binkert.org    Event *event = new SimLoopExitEvent("m5_exit instruction encountered", 0);
1575606Snate@binkert.org    mainEventQueue.schedule(event, when);
1585504Snate@binkert.org}
1595504Snate@binkert.org
1605780Ssteve.reinhardt@amd.com#if FULL_SYSTEM
1615780Ssteve.reinhardt@amd.com
1625504Snate@binkert.orgvoid
1635504Snate@binkert.orgloadsymbol(ThreadContext *tc)
1645504Snate@binkert.org{
1655504Snate@binkert.org    const string &filename = tc->getCpuPtr()->system->params()->symbolfile;
1665504Snate@binkert.org    if (filename.empty()) {
1675504Snate@binkert.org        return;
168711SN/A    }
169711SN/A
1705504Snate@binkert.org    std::string buffer;
1715504Snate@binkert.org    ifstream file(filename.c_str());
172310SN/A
1735504Snate@binkert.org    if (!file)
1745504Snate@binkert.org        fatal("file error: Can't open symbol table file %s\n", filename);
1753373Sstever@eecs.umich.edu
1765504Snate@binkert.org    while (!file.eof()) {
1775504Snate@binkert.org        getline(file, buffer);
1785504Snate@binkert.org
1795504Snate@binkert.org        if (buffer.empty())
1805504Snate@binkert.org            continue;
1815504Snate@binkert.org
1826227Snate@binkert.org        string::size_type idx = buffer.find(' ');
1835504Snate@binkert.org        if (idx == string::npos)
1845504Snate@binkert.org            continue;
1855504Snate@binkert.org
1865504Snate@binkert.org        string address = "0x" + buffer.substr(0, idx);
1875504Snate@binkert.org        eat_white(address);
1885504Snate@binkert.org        if (address.empty())
1895504Snate@binkert.org            continue;
1905504Snate@binkert.org
1915504Snate@binkert.org        // Skip over letter and space
1925504Snate@binkert.org        string symbol = buffer.substr(idx + 3);
1935504Snate@binkert.org        eat_white(symbol);
1945504Snate@binkert.org        if (symbol.empty())
1955504Snate@binkert.org            continue;
1965504Snate@binkert.org
1975504Snate@binkert.org        Addr addr;
1985504Snate@binkert.org        if (!to_number(address, addr))
1995504Snate@binkert.org            continue;
2005504Snate@binkert.org
2015504Snate@binkert.org        if (!tc->getSystemPtr()->kernelSymtab->insert(addr, symbol))
2025504Snate@binkert.org            continue;
2035504Snate@binkert.org
2045504Snate@binkert.org
2055504Snate@binkert.org        DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
2065504Snate@binkert.org    }
2075504Snate@binkert.org    file.close();
2085504Snate@binkert.org}
2095504Snate@binkert.org
2105504Snate@binkert.orgvoid
2115780Ssteve.reinhardt@amd.comaddsymbol(ThreadContext *tc, Addr addr, Addr symbolAddr)
2125780Ssteve.reinhardt@amd.com{
2135780Ssteve.reinhardt@amd.com    char symb[100];
2145780Ssteve.reinhardt@amd.com    CopyStringOut(tc, symb, symbolAddr, 100);
2155780Ssteve.reinhardt@amd.com    std::string symbol(symb);
2165780Ssteve.reinhardt@amd.com
2175780Ssteve.reinhardt@amd.com    DPRINTF(Loader, "Loaded symbol: %s @ %#llx\n", symbol, addr);
2185780Ssteve.reinhardt@amd.com
2195780Ssteve.reinhardt@amd.com    tc->getSystemPtr()->kernelSymtab->insert(addr,symbol);
2205952Ssaidi@eecs.umich.edu    debugSymbolTable->insert(addr,symbol);
2215780Ssteve.reinhardt@amd.com}
2225780Ssteve.reinhardt@amd.com
2235780Ssteve.reinhardt@amd.com#endif
2245780Ssteve.reinhardt@amd.com
2255780Ssteve.reinhardt@amd.com
2265780Ssteve.reinhardt@amd.comvoid
2275504Snate@binkert.orgresetstats(ThreadContext *tc, Tick delay, Tick period)
2285504Snate@binkert.org{
2295529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
2305504Snate@binkert.org        return;
2315504Snate@binkert.org
2325504Snate@binkert.org
2337064Snate@binkert.org    Tick when = curTick + delay * SimClock::Int::ns;
2347064Snate@binkert.org    Tick repeat = period * SimClock::Int::ns;
2355504Snate@binkert.org
2365504Snate@binkert.org    Stats::StatEvent(false, true, when, repeat);
2375504Snate@binkert.org}
2385504Snate@binkert.org
2395504Snate@binkert.orgvoid
2405504Snate@binkert.orgdumpstats(ThreadContext *tc, Tick delay, Tick period)
2415504Snate@binkert.org{
2425529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
2435504Snate@binkert.org        return;
2445504Snate@binkert.org
2455504Snate@binkert.org
2467064Snate@binkert.org    Tick when = curTick + delay * SimClock::Int::ns;
2477064Snate@binkert.org    Tick repeat = period * SimClock::Int::ns;
2485504Snate@binkert.org
2495504Snate@binkert.org    Stats::StatEvent(true, false, when, repeat);
2505504Snate@binkert.org}
2515504Snate@binkert.org
2525504Snate@binkert.orgvoid
2535504Snate@binkert.orgdumpresetstats(ThreadContext *tc, Tick delay, Tick period)
2545504Snate@binkert.org{
2555529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_statistics_insts)
2565504Snate@binkert.org        return;
2575504Snate@binkert.org
2585504Snate@binkert.org
2597064Snate@binkert.org    Tick when = curTick + delay * SimClock::Int::ns;
2607064Snate@binkert.org    Tick repeat = period * SimClock::Int::ns;
2615504Snate@binkert.org
2625504Snate@binkert.org    Stats::StatEvent(true, true, when, repeat);
2635504Snate@binkert.org}
2645504Snate@binkert.org
2655504Snate@binkert.orgvoid
2665504Snate@binkert.orgm5checkpoint(ThreadContext *tc, Tick delay, Tick period)
2675504Snate@binkert.org{
2685529Snate@binkert.org    if (!tc->getCpuPtr()->params()->do_checkpoint_insts)
2695504Snate@binkert.org        return;
2705504Snate@binkert.org
2717064Snate@binkert.org    Tick when = curTick + delay * SimClock::Int::ns;
2727064Snate@binkert.org    Tick repeat = period * SimClock::Int::ns;
2735504Snate@binkert.org
2745606Snate@binkert.org    Event *event = new SimLoopExitEvent("checkpoint", 0, repeat);
2755606Snate@binkert.org    mainEventQueue.schedule(event, when);
2765504Snate@binkert.org}
2775504Snate@binkert.org
2785780Ssteve.reinhardt@amd.com#if FULL_SYSTEM
2795780Ssteve.reinhardt@amd.com
2805504Snate@binkert.orguint64_t
2815504Snate@binkert.orgreadfile(ThreadContext *tc, Addr vaddr, uint64_t len, uint64_t offset)
2825504Snate@binkert.org{
2835504Snate@binkert.org    const string &file = tc->getSystemPtr()->params()->readfile;
2845504Snate@binkert.org    if (file.empty()) {
2855504Snate@binkert.org        return ULL(0);
286310SN/A    }
287299SN/A
2885504Snate@binkert.org    uint64_t result = 0;
2892188SN/A
2905504Snate@binkert.org    int fd = ::open(file.c_str(), O_RDONLY, 0);
2915504Snate@binkert.org    if (fd < 0)
2925504Snate@binkert.org        panic("could not open file %s\n", file);
2932235SN/A
2945504Snate@binkert.org    if (::lseek(fd, offset, SEEK_SET) < 0)
2955504Snate@binkert.org        panic("could not seek: %s", strerror(errno));
2963368Sstever@eecs.umich.edu
2975504Snate@binkert.org    char *buf = new char[len];
2985504Snate@binkert.org    char *p = buf;
2995504Snate@binkert.org    while (len > 0) {
3005504Snate@binkert.org        int bytes = ::read(fd, p, len);
3015504Snate@binkert.org        if (bytes <= 0)
3025504Snate@binkert.org            break;
3033368Sstever@eecs.umich.edu
3045504Snate@binkert.org        p += bytes;
3055504Snate@binkert.org        result += bytes;
3065504Snate@binkert.org        len -= bytes;
3072188SN/A    }
3082188SN/A
3095504Snate@binkert.org    close(fd);
3105504Snate@binkert.org    CopyIn(tc, vaddr, buf, result);
3115504Snate@binkert.org    delete [] buf;
3125504Snate@binkert.org    return result;
3135504Snate@binkert.org}
3142188SN/A
3155780Ssteve.reinhardt@amd.com#endif
3165780Ssteve.reinhardt@amd.com
3175504Snate@binkert.orgvoid
3185504Snate@binkert.orgdebugbreak(ThreadContext *tc)
3195504Snate@binkert.org{
3205504Snate@binkert.org    debug_break();
3215504Snate@binkert.org}
3222235SN/A
3235504Snate@binkert.orgvoid
3245504Snate@binkert.orgswitchcpu(ThreadContext *tc)
3255504Snate@binkert.org{
3265504Snate@binkert.org    exitSimLoop("switchcpu");
3275504Snate@binkert.org}
3283368Sstever@eecs.umich.edu
3297811Ssteve.reinhardt@amd.com} // namespace PseudoInst
330