Merge vm1.(none):/home/stever/bk/newmem
[gem5.git] / src / cpu / pc_event.hh
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 * Authors: Nathan Binkert
29 * Steve Reinhardt
30 */
31
32 #ifndef __PC_EVENT_HH__
33 #define __PC_EVENT_HH__
34
35 #include <vector>
36
37 #include "base/misc.hh"
38
39 class ThreadContext;
40 class PCEventQueue;
41
42 class PCEvent
43 {
44 protected:
45 std::string description;
46 PCEventQueue *queue;
47 Addr evpc;
48
49 public:
50 PCEvent(PCEventQueue *q, const std::string &desc, Addr pc);
51
52 virtual ~PCEvent() { if (queue) remove(); }
53
54 // for DPRINTF
55 virtual const std::string name() const { return description; }
56
57 std::string descr() const { return description; }
58 Addr pc() const { return evpc; }
59
60 bool remove();
61 virtual void process(ThreadContext *tc) = 0;
62 };
63
64 class PCEventQueue
65 {
66 protected:
67 typedef PCEvent * record_t;
68 class MapCompare {
69 public:
70 bool operator()(const record_t &l, const record_t &r) const {
71 return l->pc() < r->pc();
72 }
73 bool operator()(const record_t &l, Addr pc) const {
74 return l->pc() < pc;
75 }
76 bool operator()(Addr pc, const record_t &r) const {
77 return pc < r->pc();
78 }
79 };
80 typedef std::vector<record_t> map_t;
81
82 public:
83 typedef map_t::iterator iterator;
84 typedef map_t::const_iterator const_iterator;
85
86 protected:
87 typedef std::pair<iterator, iterator> range_t;
88 typedef std::pair<const_iterator, const_iterator> const_range_t;
89
90 protected:
91 map_t pc_map;
92
93 bool doService(ThreadContext *tc);
94
95 public:
96 PCEventQueue();
97 ~PCEventQueue();
98
99 bool remove(PCEvent *event);
100 bool schedule(PCEvent *event);
101 bool service(ThreadContext *tc)
102 {
103 if (pc_map.empty())
104 return false;
105
106 return doService(tc);
107 }
108
109 range_t equal_range(Addr pc);
110 range_t equal_range(PCEvent *event) { return equal_range(event->pc()); }
111
112 void dump() const;
113 };
114
115
116 inline
117 PCEvent::PCEvent(PCEventQueue *q, const std::string &desc, Addr pc)
118 : description(desc), queue(q), evpc(pc)
119 {
120 queue->schedule(this);
121 }
122
123 inline bool
124 PCEvent::remove()
125 {
126 if (!queue)
127 panic("cannot remove an uninitialized event;");
128
129 return queue->remove(this);
130 }
131
132 class BreakPCEvent : public PCEvent
133 {
134 protected:
135 bool remove;
136
137 public:
138 BreakPCEvent(PCEventQueue *q, const std::string &desc, Addr addr,
139 bool del = false);
140 virtual void process(ThreadContext *tc);
141 };
142
143 #endif // __PC_EVENT_HH__