thread_context_impl.hh revision 8208
19356Snilay@cs.wisc.edu/*
29356Snilay@cs.wisc.edu * Copyright (c) 2010 ARM Limited
39356Snilay@cs.wisc.edu * All rights reserved
49356Snilay@cs.wisc.edu *
59356Snilay@cs.wisc.edu * The license below extends only to copyright in the software and shall
69356Snilay@cs.wisc.edu * not be construed as granting a license to any other intellectual
79356Snilay@cs.wisc.edu * property including but not limited to intellectual property relating
89356Snilay@cs.wisc.edu * to a hardware implementation of the functionality of the software
99356Snilay@cs.wisc.edu * licensed hereunder.  You may use the software subject to the license
109356Snilay@cs.wisc.edu * terms below provided that you ensure that this notice is replicated
119356Snilay@cs.wisc.edu * unmodified and in its entirety in all distributions of the software,
129356Snilay@cs.wisc.edu * modified or unmodified, in source code or in binary form.
139356Snilay@cs.wisc.edu *
149356Snilay@cs.wisc.edu * Copyright (c) 2004-2006 The Regents of The University of Michigan
159356Snilay@cs.wisc.edu * All rights reserved.
169356Snilay@cs.wisc.edu *
179356Snilay@cs.wisc.edu * Redistribution and use in source and binary forms, with or without
189356Snilay@cs.wisc.edu * modification, are permitted provided that the following conditions are
199356Snilay@cs.wisc.edu * met: redistributions of source code must retain the above copyright
209356Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer;
219356Snilay@cs.wisc.edu * redistributions in binary form must reproduce the above copyright
229356Snilay@cs.wisc.edu * notice, this list of conditions and the following disclaimer in the
239356Snilay@cs.wisc.edu * documentation and/or other materials provided with the distribution;
249356Snilay@cs.wisc.edu * neither the name of the copyright holders nor the names of its
259356Snilay@cs.wisc.edu * contributors may be used to endorse or promote products derived from
269356Snilay@cs.wisc.edu * this software without specific prior written permission.
279356Snilay@cs.wisc.edu *
289356Snilay@cs.wisc.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
299356Snilay@cs.wisc.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
309356Snilay@cs.wisc.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
319356Snilay@cs.wisc.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
329356Snilay@cs.wisc.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
339356Snilay@cs.wisc.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
349356Snilay@cs.wisc.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
359356Snilay@cs.wisc.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
369356Snilay@cs.wisc.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
379356Snilay@cs.wisc.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
389356Snilay@cs.wisc.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
399356Snilay@cs.wisc.edu *
409356Snilay@cs.wisc.edu * Authors: Kevin Lim
419356Snilay@cs.wisc.edu *          Korey Sewell
429356Snilay@cs.wisc.edu */
439356Snilay@cs.wisc.edu
449356Snilay@cs.wisc.edu#include "arch/registers.hh"
459356Snilay@cs.wisc.edu#include "config/the_isa.hh"
469356Snilay@cs.wisc.edu#include "cpu/o3/thread_context.hh"
479356Snilay@cs.wisc.edu#include "cpu/quiesce_event.hh"
489356Snilay@cs.wisc.edu
499356Snilay@cs.wisc.edu#if FULL_SYSTEM
509356Snilay@cs.wisc.edutemplate <class Impl>
519356Snilay@cs.wisc.eduVirtualPort *
529356Snilay@cs.wisc.eduO3ThreadContext<Impl>::getVirtPort()
539356Snilay@cs.wisc.edu{
549356Snilay@cs.wisc.edu    return thread->getVirtPort();
559356Snilay@cs.wisc.edu}
569356Snilay@cs.wisc.edu
579356Snilay@cs.wisc.edutemplate <class Impl>
589356Snilay@cs.wisc.eduvoid
599356Snilay@cs.wisc.eduO3ThreadContext<Impl>::dumpFuncProfile()
609356Snilay@cs.wisc.edu{
619356Snilay@cs.wisc.edu    thread->dumpFuncProfile();
629356Snilay@cs.wisc.edu}
639356Snilay@cs.wisc.edu#endif
649356Snilay@cs.wisc.edu
659356Snilay@cs.wisc.edutemplate <class Impl>
669356Snilay@cs.wisc.eduvoid
679356Snilay@cs.wisc.eduO3ThreadContext<Impl>::takeOverFrom(ThreadContext *old_context)
689356Snilay@cs.wisc.edu{
699356Snilay@cs.wisc.edu    // some things should already be set up
709356Snilay@cs.wisc.edu#if FULL_SYSTEM
719356Snilay@cs.wisc.edu    assert(getSystemPtr() == old_context->getSystemPtr());
729356Snilay@cs.wisc.edu#else
739356Snilay@cs.wisc.edu    assert(getProcessPtr() == old_context->getProcessPtr());
749356Snilay@cs.wisc.edu#endif
759356Snilay@cs.wisc.edu
769356Snilay@cs.wisc.edu    // copy over functional state
779356Snilay@cs.wisc.edu    setStatus(old_context->status());
789356Snilay@cs.wisc.edu    copyArchRegs(old_context);
799356Snilay@cs.wisc.edu    setContextId(old_context->contextId());
809356Snilay@cs.wisc.edu    setThreadId(old_context->threadId());
819356Snilay@cs.wisc.edu
829356Snilay@cs.wisc.edu#if !FULL_SYSTEM
839356Snilay@cs.wisc.edu    thread->funcExeInst = old_context->readFuncExeInst();
849356Snilay@cs.wisc.edu#else
859356Snilay@cs.wisc.edu    EndQuiesceEvent *other_quiesce = old_context->getQuiesceEvent();
869356Snilay@cs.wisc.edu    if (other_quiesce) {
879356Snilay@cs.wisc.edu        // Point the quiesce event's TC at this TC so that it wakes up
889356Snilay@cs.wisc.edu        // the proper CPU.
899356Snilay@cs.wisc.edu        other_quiesce->tc = this;
909356Snilay@cs.wisc.edu    }
919356Snilay@cs.wisc.edu    if (thread->quiesceEvent) {
929356Snilay@cs.wisc.edu        thread->quiesceEvent->tc = this;
939356Snilay@cs.wisc.edu    }
949356Snilay@cs.wisc.edu
959356Snilay@cs.wisc.edu    // Transfer kernel stats from one CPU to the other.
969356Snilay@cs.wisc.edu    thread->kernelStats = old_context->getKernelStats();
979356Snilay@cs.wisc.edu//    storeCondFailures = 0;
989356Snilay@cs.wisc.edu    cpu->lockFlag = false;
999356Snilay@cs.wisc.edu#endif
1009356Snilay@cs.wisc.edu
101    old_context->setStatus(ThreadContext::Halted);
102
103    thread->inSyscall = false;
104    thread->trapPending = false;
105}
106
107template <class Impl>
108void
109O3ThreadContext<Impl>::activate(int delay)
110{
111    DPRINTF(O3CPU, "Calling activate on Thread Context %d\n",
112            threadId());
113
114    if (thread->status() == ThreadContext::Active)
115        return;
116
117#if FULL_SYSTEM
118    thread->lastActivate = curTick();
119#endif
120
121    thread->setStatus(ThreadContext::Active);
122
123    // status() == Suspended
124    cpu->activateContext(thread->threadId(), delay);
125}
126
127template <class Impl>
128void
129O3ThreadContext<Impl>::suspend(int delay)
130{
131    DPRINTF(O3CPU, "Calling suspend on Thread Context %d\n",
132            threadId());
133
134    if (thread->status() == ThreadContext::Suspended)
135        return;
136
137#if FULL_SYSTEM
138    thread->lastActivate = curTick();
139    thread->lastSuspend = curTick();
140#endif
141/*
142#if FULL_SYSTEM
143    // Don't change the status from active if there are pending interrupts
144    if (cpu->checkInterrupts()) {
145        assert(status() == ThreadContext::Active);
146        return;
147    }
148#endif
149*/
150    thread->setStatus(ThreadContext::Suspended);
151    cpu->suspendContext(thread->threadId());
152}
153
154template <class Impl>
155void
156O3ThreadContext<Impl>::halt(int delay)
157{
158    DPRINTF(O3CPU, "Calling halt on Thread Context %d\n",
159            threadId());
160
161    if (thread->status() == ThreadContext::Halted)
162        return;
163
164    thread->setStatus(ThreadContext::Halted);
165    cpu->haltContext(thread->threadId());
166}
167
168template <class Impl>
169void
170O3ThreadContext<Impl>::regStats(const std::string &name)
171{
172#if FULL_SYSTEM
173    thread->kernelStats = new TheISA::Kernel::Statistics(cpu->system);
174    thread->kernelStats->regStats(name + ".kern");
175#endif
176}
177
178template <class Impl>
179void
180O3ThreadContext<Impl>::serialize(std::ostream &os)
181{
182#if FULL_SYSTEM
183    if (thread->kernelStats)
184        thread->kernelStats->serialize(os);
185#endif
186
187}
188
189template <class Impl>
190void
191O3ThreadContext<Impl>::unserialize(Checkpoint *cp, const std::string &section)
192{
193#if FULL_SYSTEM
194    if (thread->kernelStats)
195        thread->kernelStats->unserialize(cp, section);
196#endif
197
198}
199
200#if FULL_SYSTEM
201template <class Impl>
202Tick
203O3ThreadContext<Impl>::readLastActivate()
204{
205    return thread->lastActivate;
206}
207
208template <class Impl>
209Tick
210O3ThreadContext<Impl>::readLastSuspend()
211{
212    return thread->lastSuspend;
213}
214
215template <class Impl>
216void
217O3ThreadContext<Impl>::profileClear()
218{
219    thread->profileClear();
220}
221
222template <class Impl>
223void
224O3ThreadContext<Impl>::profileSample()
225{
226    thread->profileSample();
227}
228#endif
229
230template <class Impl>
231void
232O3ThreadContext<Impl>::copyArchRegs(ThreadContext *tc)
233{
234    // Prevent squashing
235    thread->inSyscall = true;
236    TheISA::copyRegs(tc, this);
237    thread->inSyscall = false;
238
239#if !FULL_SYSTEM
240    this->thread->funcExeInst = tc->readFuncExeInst();
241#endif
242}
243
244template <class Impl>
245void
246O3ThreadContext<Impl>::clearArchRegs()
247{
248    cpu->isa[thread->threadId()].clear();
249}
250
251template <class Impl>
252uint64_t
253O3ThreadContext<Impl>::readIntReg(int reg_idx)
254{
255    reg_idx = cpu->isa[thread->threadId()].flattenIntIndex(reg_idx);
256    return cpu->readArchIntReg(reg_idx, thread->threadId());
257}
258
259template <class Impl>
260TheISA::FloatReg
261O3ThreadContext<Impl>::readFloatReg(int reg_idx)
262{
263    reg_idx = cpu->isa[thread->threadId()].flattenFloatIndex(reg_idx);
264    return cpu->readArchFloatReg(reg_idx, thread->threadId());
265}
266
267template <class Impl>
268TheISA::FloatRegBits
269O3ThreadContext<Impl>::readFloatRegBits(int reg_idx)
270{
271    reg_idx = cpu->isa[thread->threadId()].flattenFloatIndex(reg_idx);
272    return cpu->readArchFloatRegInt(reg_idx, thread->threadId());
273}
274
275template <class Impl>
276void
277O3ThreadContext<Impl>::setIntReg(int reg_idx, uint64_t val)
278{
279    reg_idx = cpu->isa[thread->threadId()].flattenIntIndex(reg_idx);
280    cpu->setArchIntReg(reg_idx, val, thread->threadId());
281
282    // Squash if we're not already in a state update mode.
283    if (!thread->trapPending && !thread->inSyscall) {
284        cpu->squashFromTC(thread->threadId());
285    }
286}
287
288template <class Impl>
289void
290O3ThreadContext<Impl>::setFloatReg(int reg_idx, FloatReg val)
291{
292    reg_idx = cpu->isa[thread->threadId()].flattenFloatIndex(reg_idx);
293    cpu->setArchFloatReg(reg_idx, val, thread->threadId());
294
295    if (!thread->trapPending && !thread->inSyscall) {
296        cpu->squashFromTC(thread->threadId());
297    }
298}
299
300template <class Impl>
301void
302O3ThreadContext<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
303{
304    reg_idx = cpu->isa[thread->threadId()].flattenFloatIndex(reg_idx);
305    cpu->setArchFloatRegInt(reg_idx, val, thread->threadId());
306
307    // Squash if we're not already in a state update mode.
308    if (!thread->trapPending && !thread->inSyscall) {
309        cpu->squashFromTC(thread->threadId());
310    }
311}
312
313template <class Impl>
314void
315O3ThreadContext<Impl>::pcState(const TheISA::PCState &val)
316{
317    cpu->pcState(val, thread->threadId());
318
319    // Squash if we're not already in a state update mode.
320    if (!thread->trapPending && !thread->inSyscall) {
321        cpu->squashFromTC(thread->threadId());
322    }
323}
324
325template <class Impl>
326int
327O3ThreadContext<Impl>::flattenIntIndex(int reg)
328{
329    return cpu->isa[thread->threadId()].flattenIntIndex(reg);
330}
331
332template <class Impl>
333int
334O3ThreadContext<Impl>::flattenFloatIndex(int reg)
335{
336    return cpu->isa[thread->threadId()].flattenFloatIndex(reg);
337}
338
339template <class Impl>
340void
341O3ThreadContext<Impl>::setMiscRegNoEffect(int misc_reg, const MiscReg &val)
342{
343    cpu->setMiscRegNoEffect(misc_reg, val, thread->threadId());
344
345    // Squash if we're not already in a state update mode.
346    if (!thread->trapPending && !thread->inSyscall) {
347        cpu->squashFromTC(thread->threadId());
348    }
349}
350
351template <class Impl>
352void
353O3ThreadContext<Impl>::setMiscReg(int misc_reg,
354                                                const MiscReg &val)
355{
356    cpu->setMiscReg(misc_reg, val, thread->threadId());
357
358    // Squash if we're not already in a state update mode.
359    if (!thread->trapPending && !thread->inSyscall) {
360        cpu->squashFromTC(thread->threadId());
361    }
362}
363
364