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