Merge zizzer:/bk/m5 into isabel.reinhardt.house:/z/stever/bk/m5
[gem5.git] / sim / 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 "debug.hh"
35 #include "exec_context.hh"
36 #include "pc_event.hh"
37 #include "trace.hh"
38 #include "universe.hh"
39
40 #ifdef FULL_SYSTEM
41 #include "arguments.hh"
42 #include "pmap.h"
43 #include "kernel.hh"
44 #include "memory_control.hh"
45 #include "cpu.hh"
46 #include "system.hh"
47 #include "bpred.hh"
48 #endif
49
50 using namespace std;
51
52 PCEventQueue::PCEventQueue()
53 {}
54
55 PCEventQueue::~PCEventQueue()
56 {}
57
58 bool
59 PCEventQueue::remove(PCEvent *event)
60 {
61 int removed = 0;
62 range_t range = equal_range(event);
63 for (iterator i = range.first; i != range.second; ++i) {
64 if (*i == event) {
65 DPRINTF(PCEvent, "PC based event removed at %#x: %s\n",
66 event->pc(), event->descr());
67 pc_map.erase(i);
68 ++removed;
69 }
70 }
71
72 return removed > 0;
73 }
74
75 bool
76 PCEventQueue::schedule(PCEvent *event)
77 {
78 pc_map.push_back(event);
79 sort(pc_map.begin(), pc_map.end(), MapCompare());
80
81 DPRINTF(PCEvent, "PC based event scheduled for %#x: %s\n",
82 event->pc(), event->descr());
83
84 return true;
85 }
86
87 bool
88 PCEventQueue::service(ExecContext *xc)
89 {
90 Addr pc = xc->regs.pc;
91 int serviced = 0;
92 range_t range = equal_range(pc);
93 for (iterator i = range.first; i != range.second; ++i) {
94 // Make sure that the pc wasn't changed as the side effect of
95 // another event. This for example, prevents two invocations
96 // of the SkipFuncEvent. Maybe we should have separate PC
97 // event queues for each processor?
98 if (pc != xc->regs.pc)
99 continue;
100
101 DPRINTF(PCEvent, "PC based event serviced at %#x: %s\n",
102 (*i)->pc(), (*i)->descr());
103
104 (*i)->process(xc);
105 ++serviced;
106 }
107
108 return serviced > 0;
109 }
110
111 void
112 PCEventQueue::dump() const
113 {
114 const_iterator i = pc_map.begin();
115 const_iterator e = pc_map.end();
116
117 for (; i != e; ++i)
118 cprintf("%d: event at %#x: %s\n", curTick, (*i)->pc(),
119 (*i)->descr());
120 }
121
122 PCEventQueue::range_t
123 PCEventQueue::equal_range(Addr pc)
124 {
125 return std::equal_range(pc_map.begin(), pc_map.end(), pc, MapCompare());
126 }
127
128 #ifdef FULL_SYSTEM
129 void
130 SkipFuncEvent::process(ExecContext *xc)
131 {
132 Addr newpc = xc->regs.intRegFile[ReturnAddressReg];
133
134 DPRINTF(PCEvent, "skipping %s: pc=%x, newpc=%x\n", description,
135 xc->regs.pc, newpc);
136
137 xc->regs.pc = newpc;
138 xc->regs.npc = xc->regs.pc + sizeof(MachInst);
139
140 BranchPred *bp = xc->cpu->getBranchPred();
141 if (bp != NULL) {
142 bp->popRAS(xc->thread_num);
143 }
144 }
145
146 void
147 BadAddrEvent::process(ExecContext *xc)
148 {
149 // The following gross hack is the equivalent function to the
150 // annotation for vmunix::badaddr in:
151 // simos/simulation/apps/tcl/osf/tlaser.tcl
152
153 uint64_t a0 = xc->regs.intRegFile[ArgumentReg0];
154
155 if (a0 < ALPHA_K0SEG_BASE || a0 >= ALPHA_K1SEG_BASE ||
156 xc->memCtrl->badaddr(ALPHA_K0SEG_TO_PHYS(a0) & PA_IMPL_MASK)) {
157
158 DPRINTF(BADADDR, "badaddr arg=%#x bad\n", a0);
159 xc->regs.intRegFile[ReturnValueReg] = 0x1;
160 SkipFuncEvent::process(xc);
161 }
162 else
163 DPRINTF(BADADDR, "badaddr arg=%#x good\n", a0);
164 }
165
166 void
167 PrintfEvent::process(ExecContext *xc)
168 {
169 if (DTRACE(Printf)) {
170 DebugOut() << curTick << ": " << xc->cpu->name() << ": ";
171
172 AlphaArguments args(xc);
173 Kernel::Printf(args);
174 }
175 }
176
177 void
178 DebugPrintfEvent::process(ExecContext *xc)
179 {
180 if (DTRACE(DebugPrintf)) {
181 if (!raw)
182 DebugOut() << curTick << ": " << xc->cpu->name() << ": ";
183
184 AlphaArguments args(xc);
185 Kernel::Printf(args);
186 }
187 }
188
189 void
190 DumpMbufEvent::process(ExecContext *xc)
191 {
192 if (DTRACE(DebugPrintf)) {
193 AlphaArguments args(xc);
194 Kernel::DumpMbuf(args);
195 }
196 }
197 #endif
198
199 BreakPCEvent::BreakPCEvent(PCEventQueue *q, const std::string &desc, bool del)
200 : PCEvent(q, desc), remove(del)
201 {
202 }
203
204 void
205 BreakPCEvent::process(ExecContext *xc)
206 {
207 debug_break();
208 if (remove)
209 delete this;
210 }
211
212 #ifdef FULL_SYSTEM
213 extern "C"
214 void
215 sched_break_pc_sys(System *sys, Addr addr)
216 {
217 PCEvent *event = new BreakPCEvent(&sys->pcEventQueue, "debug break", true);
218 event->schedule(addr);
219 }
220
221 extern "C"
222 void
223 sched_break_pc(Addr addr)
224 {
225 for (vector<System *>::iterator sysi = System::systemList.begin();
226 sysi != System::systemList.end(); ++sysi) {
227 sched_break_pc_sys(*sysi, addr);
228 }
229
230 }
231 #endif