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