pc_event.cc revision 1717
1/*
2 * Copyright (c) 2002-2003 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 <algorithm>
30#include <map>
31#include <string>
32#include <utility>
33
34#include "base/trace.hh"
35#include "cpu/base.hh"
36#include "cpu/exec_context.hh"
37#include "cpu/pc_event.hh"
38#include "sim/debug.hh"
39#include "sim/root.hh"
40
41using namespace std;
42
43PCEventQueue::PCEventQueue()
44{}
45
46PCEventQueue::~PCEventQueue()
47{}
48
49bool
50PCEventQueue::remove(PCEvent *event)
51{
52    int removed = 0;
53    range_t range = equal_range(event);
54    for (iterator i = range.first; i != range.second; ++i) {
55        if (*i == event) {
56            DPRINTF(PCEvent, "PC based event removed at %#x: %s\n",
57                    event->pc(), event->descr());
58            pc_map.erase(i);
59            ++removed;
60        }
61    }
62
63    return removed > 0;
64}
65
66bool
67PCEventQueue::schedule(PCEvent *event)
68{
69    pc_map.push_back(event);
70    sort(pc_map.begin(), pc_map.end(), MapCompare());
71
72    DPRINTF(PCEvent, "PC based event scheduled for %#x: %s\n",
73            event->pc(), event->descr());
74
75    return true;
76}
77
78bool
79PCEventQueue::doService(ExecContext *xc)
80{
81    Addr pc = xc->regs.pc & ~0x3;
82    int serviced = 0;
83    range_t range = equal_range(pc);
84    for (iterator i = range.first; i != range.second; ++i) {
85        // Make sure that the pc wasn't changed as the side effect of
86        // another event.  This for example, prevents two invocations
87        // of the SkipFuncEvent.  Maybe we should have separate PC
88        // event queues for each processor?
89        if (pc != (xc->regs.pc & ~0x3))
90            continue;
91
92        DPRINTF(PCEvent, "PC based event serviced at %#x: %s\n",
93                (*i)->pc(), (*i)->descr());
94
95        (*i)->process(xc);
96        ++serviced;
97    }
98
99    return serviced > 0;
100}
101
102void
103PCEventQueue::dump() const
104{
105    const_iterator i = pc_map.begin();
106    const_iterator e = pc_map.end();
107
108    for (; i != e; ++i)
109        cprintf("%d: event at %#x: %s\n", curTick, (*i)->pc(),
110                (*i)->descr());
111}
112
113PCEventQueue::range_t
114PCEventQueue::equal_range(Addr pc)
115{
116    return std::equal_range(pc_map.begin(), pc_map.end(), pc, MapCompare());
117}
118
119BreakPCEvent::BreakPCEvent(PCEventQueue *q, const std::string &desc, bool del)
120    : PCEvent(q, desc), remove(del)
121{
122}
123
124void
125BreakPCEvent::process(ExecContext *xc)
126{
127    StringWrap name(xc->cpu->name() + ".break_event");
128    DPRINTFN("break event %s triggered\n", descr());
129    debug_break();
130    if (remove)
131        delete this;
132}
133
134#ifdef FULL_SYSTEM
135extern "C"
136void
137sched_break_pc_sys(System *sys, Addr addr)
138{
139    PCEvent *event = new BreakPCEvent(&sys->pcEventQueue, "debug break", true);
140    event->schedule(addr);
141}
142
143extern "C"
144void
145sched_break_pc(Addr addr)
146{
147     for (vector<System *>::iterator sysi = System::systemList.begin();
148          sysi != System::systemList.end(); ++sysi) {
149         sched_break_pc_sys(*sysi, addr);
150    }
151
152}
153#endif
154