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