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