1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kernel/eka/nkernsmp/x86/ncsched.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,146 @@
1.4 +// Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32\nkernsmp\x86\ncsched.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +// NThreadBase member data
1.22 +#define __INCLUDE_NTHREADBASE_DEFINES__
1.23 +
1.24 +#include <x86.h>
1.25 +#include <apic.h>
1.26 +
1.27 +
1.28 +// Called by a thread which has been forced to exit
1.29 +// Kernel locked on entry
1.30 +extern "C" void __fastcall do_forced_exit(NThreadBase* aT)
1.31 + {
1.32 + __NK_ASSERT_ALWAYS(aT->iFastMutexDefer != 1);
1.33 + aT->iFastMutexDefer = 0;
1.34 + aT->Exit();
1.35 + }
1.36 +
1.37 +extern "C" NThreadBase* __fastcall select_next_thread(TSubScheduler* aS)
1.38 + {
1.39 + return aS->SelectNextThread();
1.40 + }
1.41 +
1.42 +extern "C" void __fastcall queue_dfcs(TSubScheduler* aS)
1.43 + {
1.44 + aS->QueueDfcs();
1.45 + }
1.46 +
1.47 +extern "C" void NewThreadTrace(NThread* a)
1.48 + {
1.49 + __ACQUIRE_BTRACE_LOCK();
1.50 + BTraceData.iHandler(BTRACE_HEADER_C(4,BTrace::ECpuUsage,BTrace::ENewThreadContext),0,(TUint32)a,0,0,0,0,0);
1.51 + __RELEASE_BTRACE_LOCK();
1.52 + }
1.53 +
1.54 +extern "C" void __fastcall send_ipi(TUint32);
1.55 +extern "C" void __fastcall do_send_resched_ipis(TUint32);
1.56 +
1.57 +extern "C" void send_resched_ipi(TInt aCpu)
1.58 + {
1.59 + TSubScheduler& ss = TheSubSchedulers[aCpu];
1.60 + __KTRACE_OPT(KSCHED2,DEBUGPRINT("@%d",aCpu));
1.61 + send_ipi((TUint32)ss.i_APICID);
1.62 + }
1.63 +
1.64 +extern "C" void send_resched_ipis(TUint32 aMask)
1.65 + {
1.66 + __KTRACE_OPT(KSCHED2,DEBUGPRINT("@%02x",aMask));
1.67 +#ifdef __USE_LOGICAL_DEST_MODE__
1.68 + do_send_resched_ipis(aMask);
1.69 +#else
1.70 + TInt i=0;
1.71 + while (aMask)
1.72 + {
1.73 + if (aMask&1)
1.74 + send_resched_ipi(i);
1.75 + aMask>>=1;
1.76 + ++i;
1.77 + }
1.78 +#endif
1.79 + }
1.80 +
1.81 +extern "C" void send_resched_ipi_and_wait(TInt aCpu)
1.82 + {
1.83 + TSubScheduler& ss = TheSubSchedulers[aCpu];
1.84 + __KTRACE_OPT(KSCHED2,DEBUGPRINT("@@%d",aCpu));
1.85 + volatile TUint32& irqc = (volatile TUint32&)ss.i_IrqCount;
1.86 + volatile TInt& irqn = (volatile TInt&)ss.i_IrqNestCount;
1.87 + TUint32 irqc0 = irqc;
1.88 + mb();
1.89 + send_ipi((TUint32)ss.i_APICID);
1.90 + mb();
1.91 + while (!ss.iRescheduleNeededFlag || (irqn<0 && irqc==irqc0))
1.92 + {
1.93 + __chill();
1.94 + }
1.95 + mb(); // guaranteed to observe final thread state after this
1.96 + }
1.97 +
1.98 +void TSubScheduler::SaveTimesliceTimer(NThreadBase* aT)
1.99 + {
1.100 + if (aT->iTime>0 && !aT->i_NThread_Initial)
1.101 + {
1.102 + TUint32 remain32 = read_apic_reg(CURRCNT);
1.103 + TUint64 x(remain32);
1.104 + x *= TUint32(i_TimerMultI);
1.105 + x += 0x00800000u;
1.106 + x >>= 24;
1.107 + aT->iTime = (TInt)x;
1.108 + }
1.109 + write_apic_reg(INITCNT, 0);
1.110 + }
1.111 +
1.112 +
1.113 +/* Update aOld's execution time and set up the timer for aNew
1.114 + Update this CPU's timestamp value
1.115 +
1.116 + if (!aOld) aOld=iInitialThread
1.117 + if (!aNew) aNew=iInitialThread
1.118 + if new thread has a timeslice, start the timeslice timer
1.119 + update the last reschedule time
1.120 + update the run time for the old thread
1.121 + update the reschedule count for the new thread and the current CPU
1.122 + */
1.123 +void TSubScheduler::UpdateThreadTimes(NThreadBase* aOld, NThreadBase* aNew)
1.124 + {
1.125 + if (!aOld)
1.126 + aOld = iInitialThread;
1.127 + if (!aNew)
1.128 + aNew = iInitialThread;
1.129 + if (aNew->iTime>0)
1.130 + {
1.131 + TUint32 remain32 = (TUint32)aNew->iTime;
1.132 + TUint64 x(remain32);
1.133 + x *= TUint32(i_TimerMultF);
1.134 + x += 0x80000000u;
1.135 + x >>= 32;
1.136 + write_apic_reg(LVTTMR, TIMESLICE_VECTOR);
1.137 + write_apic_reg(INITCNT, (TUint32)x);
1.138 + }
1.139 + if (aNew!=aOld)
1.140 + {
1.141 + TUint64 now = NKern::Timestamp();
1.142 + aOld->iTotalCpuTime64 += (now - iLastTimestamp64);
1.143 + iLastTimestamp64 = now;
1.144 + ++iReschedCount64;
1.145 + ++aNew->iRunCount64;
1.146 + }
1.147 + }
1.148 +
1.149 +