kernel/eka/kernel/sexec.cpp
branchRCL_3
changeset 44 3e88ff8f41d5
parent 43 c1f20ce4abcf
child 45 9e2d4f7f5028
equal deleted inserted replaced
43:c1f20ce4abcf 44:3e88ff8f41d5
    34 	TFullName fn;
    34 	TFullName fn;
    35 	TFullName match;
    35 	TFullName match;
    36 	TFindHandle h;
    36 	TFindHandle h;
    37 	Kern::KUDesGet(match,aName);
    37 	Kern::KUDesGet(match,aName);
    38 	kumemget32(&h,&aFindHandle,sizeof(h));
    38 	kumemget32(&h,&aFindHandle,sizeof(h));
    39 	__KTRACE_OPT(KEXEC,Kern::Printf("ObjN: %S %08x", &match, h.Handle()));
    39 	__KTRACE_OPT(KEXEC,Kern::Printf("ObjN: %lS %08x", &match, h.Handle()));
    40 	NKern::ThreadEnterCS();
    40 	NKern::ThreadEnterCS();
    41 	TInt r=pC->FindByFullName(h, match, fn);
    41 	TInt r=pC->FindByFullName(h, match, fn);
    42 	NKern::ThreadLeaveCS();
    42 	NKern::ThreadLeaveCS();
    43 	Kern::KUDesPut(aName,fn);
    43 	Kern::KUDesPut(aName,fn);
    44 	kumemput32(&aFindHandle,&h,sizeof(h));
    44 	kumemput32(&aFindHandle,&h,sizeof(h));
    94 
    94 
    95 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChunkTop"));
    95 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChunkTop"));
    96 	return aChunk->Top();
    96 	return aChunk->Top();
    97 	}
    97 	}
    98 
    98 
    99 TInt ExecHandler::MutexWait(DMutex* aMutex, TInt aTimeout)
    99 void ExecHandler::MutexWait(DMutex* aMutex)
   100 //
   100 //
   101 // Wait for the mutex.
   101 // Wait for the mutex.
   102 //
   102 //
   103 	{
   103 	{
   104 	if (aTimeout)	// 0 means wait forever, -1 means poll
   104 
   105 		{
   105 //	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::MutexWait"));
   106 		if (aTimeout<-1)
   106 	aMutex->Wait();
   107 			{
       
   108 			return KErrArgument;
       
   109 			}
       
   110 		if (aTimeout>0)
       
   111 			{
       
   112 			// Convert microseconds to NTimer ticks, rounding up
       
   113 			TInt ntp = NKern::TickPeriod();
       
   114 			aTimeout += ntp-1;
       
   115 			aTimeout /= ntp;
       
   116 			}
       
   117 		}
       
   118 	return aMutex->Wait(aTimeout);
       
   119 	}
   107 	}
   120 
   108 
   121 void ExecHandler::MutexSignal(DMutex* aMutex)
   109 void ExecHandler::MutexSignal(DMutex* aMutex)
   122 //
   110 //
   123 // Signal the mutex.
   111 // Signal the mutex.
   565 TInt ExecHandler::SemaphoreWait(DSemaphore* aSemaphore, TInt aTimeout)
   553 TInt ExecHandler::SemaphoreWait(DSemaphore* aSemaphore, TInt aTimeout)
   566 //
   554 //
   567 // Wait for a signal.
   555 // Wait for a signal.
   568 //
   556 //
   569 	{
   557 	{
       
   558 
   570 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SemaphoreWait"));
   559 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::SemaphoreWait"));
   571 	if (aTimeout)	// 0 means wait forever, -1 means poll
   560 	if (aTimeout)
   572 		{
   561 		{
   573 		if (aTimeout<-1)
   562 		if (aTimeout<0)
   574 			{
   563 			{
   575 			NKern::UnlockSystem();
   564 			NKern::UnlockSystem();
   576 			return KErrArgument;
   565 			return KErrArgument;
   577 			}
   566 			}
   578 		if (aTimeout>0)
   567 
   579 			{
   568 		// Convert microseconds to NTimer ticks, rounding up
   580 			// Convert microseconds to NTimer ticks, rounding up
   569 		TInt ntp = NKern::TickPeriod();
   581 			TInt ntp = NKern::TickPeriod();
   570 		aTimeout += ntp-1;
   582 			aTimeout += ntp-1;
   571 		aTimeout /= ntp;
   583 			aTimeout /= ntp;
       
   584 			}
       
   585 		}
   572 		}
   586 	return aSemaphore->Wait(aTimeout);
   573 	return aSemaphore->Wait(aTimeout);
   587 	}
   574 	}
   588 
   575 
   589 void ExecHandler::SemaphoreSignal1(DSemaphore* aSemaphore)
   576 void ExecHandler::SemaphoreSignal1(DSemaphore* aSemaphore)
   823 TInt ExecHandler::ChunkCreate(TOwnerType aType, const TDesC8* aName, TChunkCreate& anInfo)
   810 TInt ExecHandler::ChunkCreate(TOwnerType aType, const TDesC8* aName, TChunkCreate& anInfo)
   824 	{
   811 	{
   825 	TKName n;
   812 	TKName n;
   826 	if (aName)
   813 	if (aName)
   827 		Kern::KUDesGet(n,*aName);
   814 		Kern::KUDesGet(n,*aName);
   828 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChunkCreate %S",&n));
   815 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ChunkCreate %lS",&n));
   829 	TChunkCreate uinfo;
   816 	TChunkCreate uinfo;
   830 	SChunkCreateInfo info;
   817 	SChunkCreateInfo info;
   831 	kumemget32(&uinfo,&anInfo,sizeof(uinfo));
   818 	kumemget32(&uinfo,&anInfo,sizeof(uinfo));
   832 	info.iGlobal=uinfo.iAtt & TChunkCreate::EGlobal;
   819 	info.iGlobal=uinfo.iAtt & TChunkCreate::EGlobal;
   833 	info.iAtt = uinfo.iAtt&TChunkCreate::EChunkCreateAttMask;
   820 	info.iAtt = uinfo.iAtt&TChunkCreate::EChunkCreateAttMask;
   949 
   936 
   950 
   937 
   951 
   938 
   952 TInt ExecHandler::OpenObject(TObjectType aObjType, const TDesC8& aName, TOwnerType aType)
   939 TInt ExecHandler::OpenObject(TObjectType aObjType, const TDesC8& aName, TOwnerType aType)
   953 	{
   940 	{
       
   941 	__KTRACE_OPT(KTHREAD,Kern::Printf("Exec::OpenObject %lS",&aName));
   954 	TFullName n;
   942 	TFullName n;
   955 	Kern::KUDesGet(n,aName);
   943 	Kern::KUDesGet(n,aName);
   956 	__KTRACE_OPT(KTHREAD,Kern::Printf("Exec::OpenObject %S",&n));
       
   957 	if (Kern::ValidateFullName(n)!=KErrNone)
   944 	if (Kern::ValidateFullName(n)!=KErrNone)
   958 		K::PanicKernExec(EBadName);
   945 		K::PanicKernExec(EBadName);
   959 	if ((TUint)aObjType>=(TUint)ENumObjectTypes)
   946 	if ((TUint)aObjType>=(TUint)ENumObjectTypes)
   960 		K::PanicKernExec(EBadObjectType);
   947 		K::PanicKernExec(EBadObjectType);
   961 	TInt h=0;
   948 	TInt h=0;
  1023 	const TDesC* pN=NULL;
  1010 	const TDesC* pN=NULL;
  1024 	if (aName)
  1011 	if (aName)
  1025 		{
  1012 		{
  1026 		Kern::KUDesGet(n,*aName);
  1013 		Kern::KUDesGet(n,*aName);
  1027 		pN=&n;
  1014 		pN=&n;
  1028 		__KTRACE_OPT(KSEMAPHORE,Kern::Printf("Exec::MutexCreate %S",pN));
       
  1029 		}
  1015 		}
  1030 	else if (aType==EOwnerThread)
  1016 	else if (aType==EOwnerThread)
  1031 		pO=TheCurrentThread;
  1017 		pO=TheCurrentThread;
  1032 	else
  1018 	else
  1033 		pO=TheCurrentThread->iOwningProcess;
  1019 		pO=TheCurrentThread->iOwningProcess;
       
  1020 	__KTRACE_OPT(KSEMAPHORE,Kern::Printf("Exec::MutexCreate %lS",aName));
  1034 	NKern::ThreadEnterCS();
  1021 	NKern::ThreadEnterCS();
  1035 	DMutex* pM;
  1022 	DMutex* pM;
  1036 	TInt r=K::MutexCreate(pM, *pN, pO, ETrue, KMutexOrdUser);
  1023 	TInt r=K::MutexCreate(pM, *pN, pO, ETrue, KMutexOrdUser);
  1037 	if (r==KErrNone)
  1024 	if (r==KErrNone)
  1038 		{
  1025 		{
  1047 	return r;
  1034 	return r;
  1048 	}
  1035 	}
  1049 
  1036 
  1050 TInt ExecHandler::SemaphoreCreate(const TDesC8* aName, TInt aCount, TOwnerType aType)
  1037 TInt ExecHandler::SemaphoreCreate(const TDesC8* aName, TInt aCount, TOwnerType aType)
  1051 	{
  1038 	{
       
  1039 	__KTRACE_OPT(KSEMAPHORE,Kern::Printf("Exec::SemaphoreCreate %lS",aName));
  1052 	TKName n;
  1040 	TKName n;
  1053 	DObject* pO=NULL;
  1041 	DObject* pO=NULL;
  1054 	const TDesC* pN=NULL;
  1042 	const TDesC* pN=NULL;
  1055 	if (aName)
  1043 	if (aName)
  1056 		{
  1044 		{
  1057 		Kern::KUDesGet(n,*aName);
  1045 		Kern::KUDesGet(n,*aName);
  1058 		pN=&n;
  1046 		pN=&n;
  1059 		__KTRACE_OPT(KSEMAPHORE,Kern::Printf("Exec::SemaphoreCreate %S",pN));
       
  1060 		}
  1047 		}
  1061 	else if (aType==EOwnerThread)
  1048 	else if (aType==EOwnerThread)
  1062 		pO=TheCurrentThread;
  1049 		pO=TheCurrentThread;
  1063 	else
  1050 	else
  1064 		pO=TheCurrentThread->iOwningProcess;
  1051 		pO=TheCurrentThread->iOwningProcess;
  1241 
  1228 
  1242 TInt ExecHandler::ThreadRename(TInt aHandle, const TDesC8& aName)
  1229 TInt ExecHandler::ThreadRename(TInt aHandle, const TDesC8& aName)
  1243 	{
  1230 	{
  1244 	TKName n;
  1231 	TKName n;
  1245 	Kern::KUDesGet(n,aName);
  1232 	Kern::KUDesGet(n,aName);
  1246 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadRename %S",&n));
  1233 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadRename %lS",&n));
  1247 	NKern::LockSystem();
  1234 	NKern::LockSystem();
  1248 	DThread* pT=(DThread*)K::ThreadEnterCS(aHandle,EThread);
  1235 	DThread* pT=(DThread*)K::ThreadEnterCS(aHandle,EThread);
  1249 	if (pT!=TheCurrentThread &&
  1236 	if (pT!=TheCurrentThread &&
  1250 		pT->iOwningProcess->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone)
  1237 		pT->iOwningProcess->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone)
  1251 		{
  1238 		{
  1266 
  1253 
  1267 TInt ExecHandler::ProcessRename(TInt aHandle, const TDesC8& aName)
  1254 TInt ExecHandler::ProcessRename(TInt aHandle, const TDesC8& aName)
  1268 	{
  1255 	{
  1269 	TKName n;
  1256 	TKName n;
  1270 	Kern::KUDesGet(n,aName);
  1257 	Kern::KUDesGet(n,aName);
  1271 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ProcessRename %S",&n));
  1258 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ProcessRename %lS",&n));
  1272 	NKern::LockSystem();
  1259 	NKern::LockSystem();
  1273 	DProcess* pP=(DProcess*)K::ThreadEnterCS(aHandle,EProcess);
  1260 	DProcess* pP=(DProcess*)K::ThreadEnterCS(aHandle,EProcess);
  1274 	if (pP->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone)
  1261 	if (pP->iSecurityZone!=TheCurrentThread->iOwningProcess->iSecurityZone)
  1275 		{
  1262 		{
  1276 		if(TheSuperPage().KernelConfigFlags() & EKernelConfigPlatSecEnforcement)
  1263 		if(TheSuperPage().KernelConfigFlags() & EKernelConfigPlatSecEnforcement)
  1626 			NKern::ThreadLeaveCS();
  1613 			NKern::ThreadLeaveCS();
  1627 			kumemput32(a2, (TAny*)&failures, sizeof(TUint));
  1614 			kumemput32(a2, (TAny*)&failures, sizeof(TUint));
  1628 			break;
  1615 			break;
  1629 			}
  1616 			}
  1630 
  1617 
  1631 		case EDbgGetAllocFail:
       
  1632 			{
       
  1633 			NKern::ThreadEnterCS();
       
  1634 			TInt allocFail = K::Allocator->__DbgGetAllocFail();
       
  1635 			NKern::ThreadLeaveCS();
       
  1636 			kumemput32(a2, (TAny*)&allocFail, sizeof(TInt));
       
  1637 			break;
       
  1638 			}
       
  1639 
       
  1640 		default:
  1618 		default:
  1641 			panic=EBadKernelHeapDebugFunction;
  1619 			panic=EBadKernelHeapDebugFunction;
  1642 			break;
  1620 			break;
  1643 		}
  1621 		}
  1644 	if (panic>KMinTInt)
  1622 	if (panic>KMinTInt)
  1918 
  1896 
  1919 TInt ExecHandler::ThreadCreate(const TDesC8& aName, TOwnerType aType, SThreadCreateInfo& aInfo)
  1897 TInt ExecHandler::ThreadCreate(const TDesC8& aName, TOwnerType aType, SThreadCreateInfo& aInfo)
  1920 	{
  1898 	{
  1921 	TKName n;
  1899 	TKName n;
  1922 	Kern::KUDesGet(n,aName);
  1900 	Kern::KUDesGet(n,aName);
  1923 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadCreate %S",&n));
  1901 	__KTRACE_OPT(KEXEC,Kern::Printf("Exec::ThreadCreate %lS",&n));
  1924 	TUint32 infoBuf[KMaxThreadCreateInfo/sizeof(TUint32)];
  1902 	TUint32 infoBuf[KMaxThreadCreateInfo/sizeof(TUint32)];
  1925 	SThreadCreateInfo& info = *(SThreadCreateInfo*)infoBuf;
  1903 	SThreadCreateInfo& info = *(SThreadCreateInfo*)infoBuf;
  1926 	kumemget32(&info, &aInfo, sizeof(SThreadCreateInfo));
  1904 	kumemget32(&info, &aInfo, sizeof(SThreadCreateInfo));
  1927 	TInt r = ( (info.iTotalSize < (TInt)sizeof(SThreadCreateInfo)) || (info.iTotalSize > KMaxThreadCreateInfo) || (info.iTotalSize & 7) )
  1905 	TInt r = ( (info.iTotalSize < (TInt)sizeof(SThreadCreateInfo)) || (info.iTotalSize > KMaxThreadCreateInfo) || (info.iTotalSize & 7) )
  1928 		? KErrArgument : KErrNone;
  1906 		? KErrArgument : KErrNone;
  1953  * Kernel-side executive calls
  1931  * Kernel-side executive calls
  1954  ********************************************/
  1932  ********************************************/
  1955 
  1933 
  1956 TInt K::MutexCreate(DMutex*& aMutex, const TDesC& aName, DObject* anOwner, TBool aVisible, TUint aOrder)
  1934 TInt K::MutexCreate(DMutex*& aMutex, const TDesC& aName, DObject* anOwner, TBool aVisible, TUint aOrder)
  1957 	{
  1935 	{
  1958 	__KTRACE_OPT(KSEMAPHORE,Kern::Printf("K::MutexCreate %S owner %O visible=%d order=%02x",&aName,anOwner,aVisible,aOrder));
  1936 	__KTRACE_OPT(KSEMAPHORE,Kern::Printf("K::MutexCreate %lS owner %O visible=%d order=%02x",&aName,anOwner,aVisible,aOrder));
  1959 	DMutex* pM=new DMutex;
  1937 	DMutex* pM=new DMutex;
  1960 	TInt r=KErrNoMemory;
  1938 	TInt r=KErrNoMemory;
  1961 	if (pM)
  1939 	if (pM)
  1962 		{
  1940 		{
  1963 		r=pM->Create(anOwner, &aName, aVisible, aOrder);
  1941 		r=pM->Create(anOwner, &aName, aVisible, aOrder);
  2020 	@post Calling thread is in a critical section.
  1998 	@post Calling thread is in a critical section.
  2021 */
  1999 */
  2022 EXPORT_C TInt Kern::ThreadCreate(SThreadCreateInfo& aInfo)
  2000 EXPORT_C TInt Kern::ThreadCreate(SThreadCreateInfo& aInfo)
  2023 	{
  2001 	{
  2024 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::ThreadCreate");		
  2002 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::ThreadCreate");		
  2025 	__KTRACE_OPT(KEXEC,Kern::Printf("Kern::ThreadCreate %S",&aInfo.iName));
  2003 	__KTRACE_OPT(KEXEC,Kern::Printf("Kern::ThreadCreate %lS",&aInfo.iName));
  2026 	aInfo.iHandle=NULL;
  2004 	aInfo.iHandle=NULL;
  2027 	DThread* pT=NULL;
  2005 	DThread* pT=NULL;
  2028 	TBool svc = aInfo.iType!=EThreadUser;
  2006 	TBool svc = aInfo.iType!=EThreadUser;
  2029 	DProcess* pP = svc ? K::TheKernelProcess : TheCurrentThread->iOwningProcess;
  2007 	DProcess* pP = svc ? K::TheKernelProcess : TheCurrentThread->iOwningProcess;
  2030 	aInfo.iSupervisorStack = NULL;
  2008 	aInfo.iSupervisorStack = NULL;
  2166 		P::NormalizeExecutableFileName(n);
  2144 		P::NormalizeExecutableFileName(n);
  2167 	Kern::KUDesPut(aFileName,n);
  2145 	Kern::KUDesPut(aFileName,n);
  2168 	}
  2146 	}
  2169 
  2147 
  2170 #ifdef MONITOR_THREAD_CPU_TIME
  2148 #ifdef MONITOR_THREAD_CPU_TIME
  2171 TInt ExecHandler::ThreadGetCpuTime(TInt aThreadHandle, Int64& aTime)
  2149 TInt ExecHandler::ThreadGetCpuTime(DThread* aThread, Int64& aTime)
  2172 	{
  2150 	{
  2173 	TUint64 t = 0;
  2151 #ifndef __SMP__
  2174 	if (aThreadHandle == KCurrentThreadHandle)
  2152 	TInt64 time = (1000000 * aThread->iNThread.iTotalCpuTime) / NKern::FastCounterFrequency();
  2175 		{
  2153 	NKern::UnlockSystem();
  2176 		t = NKern::ThreadCpuTime(NKern::CurrentThread());
  2154 	kumemput32(&aTime, &time, sizeof(TInt64));
  2177 		}
  2155 #else
  2178 	else
  2156 	TUint64 t = NKern::ThreadCpuTime(&aThread->iNThread);
  2179 		{
  2157 	NKern::UnlockSystem();
  2180 		NKern::LockSystem();
       
  2181 		DThread* pT = (DThread*)K::ObjectFromHandle(aThreadHandle, EThread);
       
  2182 		t = NKern::ThreadCpuTime(&pT->iNThread);
       
  2183 		NKern::UnlockSystem();
       
  2184 		}
       
  2185 	TUint32 f = NKern::CpuTimeMeasFreq();
  2158 	TUint32 f = NKern::CpuTimeMeasFreq();
  2186 	TUint64 t2 = t>>32;
  2159 	TUint64 t2 = t>>32;
  2187 	t = (t & KMaxTUint32)*1000000;
  2160 	t = ((t<<32)>>32)*1000000;
  2188 	t2 *= 1000000;
  2161 	t2 *= 1000000;
  2189 	t2 += (t>>32);
  2162 	t2 += (t>>32);
  2190 	t &= TUint64(KMaxTUint32);
  2163 	t &= TUint64(KMaxTUint32);
  2191 	TUint64 q2 = t2/f;
  2164 	TUint64 q2 = t2/f;
  2192 	t2 -= q2*f;
  2165 	t2 -= q2*f;
  2193 	t += (t2<<32);
  2166 	t += (t2<<32);
  2194 	TUint64 q = t/f;
  2167 	TUint64 q = t/f;
  2195 	q += (q2<<32);
  2168 	q += (q2<<32);
  2196 	kumemput32(&aTime, &q, sizeof(TInt64));
  2169 	kumemput32(&aTime, &q, sizeof(TInt64));
       
  2170 #endif
  2197 	return KErrNone;
  2171 	return KErrNone;
  2198 	}
  2172 	}
  2199 #else
  2173 #else		
  2200 TInt ExecHandler::ThreadGetCpuTime(TInt /*aThreadHandle*/, Int64& /*aTime*/)
  2174 TInt ExecHandler::ThreadGetCpuTime(DThread* /*aThread*/, Int64& /*aTime*/)
  2201 	{
  2175 	{
       
  2176 	NKern::UnlockSystem();
  2202 	return KErrNotSupported;
  2177 	return KErrNotSupported;
  2203 	}
  2178 	}
  2204 #endif
  2179 #endif
  2205 
  2180 
  2206 TInt ExecHandler::SetMemoryThresholds(TInt aLowThreshold, TInt aGoodThreshold)
  2181 TInt ExecHandler::SetMemoryThresholds(TInt aLowThreshold, TInt aGoodThreshold)
  2221 	{
  2196 	{
  2222 
  2197 
  2223 	__KTRACE_OPT(KBOOT,Kern::Printf("File server thread registered"));
  2198 	__KTRACE_OPT(KBOOT,Kern::Printf("File server thread registered"));
  2224 	DThread* pT = TheCurrentThread;
  2199 	DThread* pT = TheCurrentThread;
  2225 	DProcess* pP = pT->iOwningProcess;
  2200 	DProcess* pP = pT->iOwningProcess;
       
  2201 	pP->iFlags |= (KThreadFlagProcessCritical | KProcessFlagSystemPermanent);
       
  2202 	pT->iFlags |= KThreadFlagSystemPermanent;
  2226 	if (K::TheFileServerProcess && K::TheFileServerProcess!=pP)
  2203 	if (K::TheFileServerProcess && K::TheFileServerProcess!=pP)
  2227 		K::PanicCurrentThread(EAccessDenied);
  2204 		K::PanicCurrentThread(EAccessDenied);
  2228 	pP->iFlags |= (KThreadFlagProcessCritical | KProcessFlagSystemPermanent);
       
  2229 	pT->iFlags |= KThreadFlagSystemPermanent;
       
  2230 	K::TheFileServerProcess=pP;
  2205 	K::TheFileServerProcess=pP;
  2231 	K::ThreadEnterCS();
  2206 	K::ThreadEnterCS();
  2232 	pP->SetPriority(EPriorityFileServer);
  2207 	pP->SetPriority(EPriorityFileServer);
  2233 	M::FsRegisterThread();
  2208 	M::FsRegisterThread();
  2234 	K::ThreadLeaveCS();
  2209 	K::ThreadLeaveCS();