os/kernelhwsrv/kernel/eka/euser/epoc/up_utl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // e32\euser\epoc\up_utl.cpp
    15 // 
    16 //
    17 
    18 #include <u32exec.h>
    19 #include "up_std.h"
    20 #include <e32svr.h>
    21 #include <e32uid.h>
    22 
    23 #ifdef __EPOC32__
    24 #include <e32rom.h>
    25 #endif
    26 
    27 #include <e32atomics.h>
    28 
    29 //#define __DEBUG_IMAGE__ 1
    30 #if defined(__DEBUG_IMAGE__) && defined (__EPOC32__)
    31 extern RDebug debug;
    32 #define __IF_DEBUG(t) {debug.t;}
    33 #else
    34 #define __IF_DEBUG(t)
    35 #endif
    36 
    37 
    38 /**
    39 Atomically (i.e. in a manner which is safe against concurrent access by other
    40 threads) increments a TInt value by 1.
    41 
    42 As an example of its use, the function is used in the implementation of
    43 critical sections.
    44 
    45 @param aValue A reference to an integer whose value is to be incremented. 
    46               On return contains the incremented value.
    47               
    48 @return The value of aValue before it is incremented.
    49 
    50 @see User::LockedDec
    51 @see RCrticalSection
    52 */
    53 EXPORT_C TInt User::LockedInc(TInt& aValue)
    54 	{
    55 	return (TInt)__e32_atomic_add_ord32(&aValue, 1);
    56 	}
    57 
    58 
    59 /**
    60 Atomically (i.e. in a manner which is safe against concurrent access by other
    61 threads) decrements a TInt value by 1.
    62 
    63 As an example of its use, the function is used in the implementation of
    64 critical sections.
    65 
    66 @param aValue A reference to an integer whose value is to be decremented. 
    67               On return contains the decremented value.
    68               
    69 @return The value of aValue before it is decremented.
    70 
    71 @see User::LockedInc
    72 @see RCrticalSection
    73 */
    74 EXPORT_C TInt User::LockedDec(TInt& aValue)
    75 	{
    76 	return (TInt)__e32_atomic_add_ord32(&aValue, 0xFFFFFFFF);
    77 	}
    78 
    79 
    80 
    81 
    82 EXPORT_C TInt User::SafeInc(TInt& aValue)
    83 /**
    84 Atomically increments the specified value by 1, if the value is > 0.
    85 
    86 @param aValue The value to be incremented; on return the incremented value.
    87 
    88 @return The original value of aValue
    89 */
    90 	{
    91 	return __e32_atomic_tas_ord32(&aValue, 1, 1, 0);
    92 	}
    93 
    94 
    95 
    96 
    97 EXPORT_C TInt User::SafeDec(TInt &aValue)
    98 /**
    99 Atomically decrements the specified value by 1, if the value is > 0.
   100 
   101 @param aValue The value to be decremented; on return the decremented value.
   102 
   103 @return The original value of aValue
   104 */
   105 	{
   106 	return __e32_atomic_tas_ord32(&aValue, 1, -1, 0);
   107 	}
   108 
   109 
   110 EXPORT_C void UserSvr::WsRegisterThread()
   111 //
   112 // Register the window server thread.
   113 //
   114     {
   115 
   116 	Exec::WsRegisterThread();
   117 	}
   118 
   119 EXPORT_C void UserSvr::FsRegisterThread()
   120 //
   121 // Register the file server thread.
   122 //
   123 	{
   124 	Exec::FsRegisterThread();
   125 	}
   126 
   127 EXPORT_C void UserSvr::RegisterTrustedChunk(TInt aHandle)
   128 /**
   129 Registers file server's chunk intended for DMA transfer.
   130 @internalComponent
   131 @released 
   132  */
   133 	{
   134 	Exec::RegisterTrustedChunk(aHandle);
   135 	}
   136 
   137 EXPORT_C TInt UserHeap::SetupThreadHeap(TBool, SStdEpocThreadCreateInfo& aInfo)
   138 /**
   139 @internalComponent
   140 */
   141 	{
   142 	TInt r = KErrNone;
   143 	if (!aInfo.iAllocator && aInfo.iHeapInitialSize>0)
   144 		{
   145 		// new heap required
   146 		RHeap* pH = NULL;
   147 		r = CreateThreadHeap(aInfo, pH);
   148 		}
   149 	else if (aInfo.iAllocator)
   150 		{
   151 		// sharing a heap
   152 		RAllocator* pA = aInfo.iAllocator;
   153 		pA->Open();
   154 		User::SwitchAllocator(pA);
   155 		}
   156 	return r;
   157 	}
   158 
   159 
   160 
   161 
   162 EXPORT_C void User::HandleException(TAny* aInfo)
   163 /**
   164 Enables the current thread to handle an exception.
   165 
   166 The function is called by the kernel.
   167 
   168 @param aInfo A pointer to a TExcType type containing the exception information.
   169 
   170 @see TExcType
   171 */
   172 	{
   173 
   174 	const TUint32* p = (const TUint32*)aInfo;
   175 	TUint32 excType = p[0];
   176 	TExceptionHandler f = Exec::ExceptionHandler(KCurrentThreadHandle);
   177 	TRAPD(r, (*f)(TExcType(excType)) );
   178 	Exec::ThreadSetFlags(KCurrentThreadHandle,KThreadFlagLastChance,0);
   179 	if (r!=KErrNone)
   180 		User::Leave(r);
   181 	}
   182 
   183 
   184 
   185 
   186 #ifdef __EPOC32__
   187 EXPORT_C TInt User::IsRomAddress(TBool &aBool, TAny *aPtr)
   188 /**
   189 Tests whether the specified address is in the ROM.
   190 
   191 @param aBool True, if the address at aPtr is within the ROM; false, 
   192              otherwise.
   193 @param aPtr  The address to be tested.
   194 
   195 @return Always KErrNone.
   196 */
   197     {
   198     
   199 	TUint a = (TUint)aPtr;
   200 	TUint main_start = UserSvr::RomHeaderAddress();
   201 	TUint main_end = main_start + ((TRomHeader*)main_start)->iUncompressedSize;
   202 	aBool = (a>=main_start && a<main_end);
   203 	if (aBool)
   204 		return KErrNone;  // address is in main ROM
   205 	
   206 	TUint rda = UserSvr::RomRootDirectoryAddress();
   207 	
   208 	// We assume here, the primary rom starts a multiple of 4k.
   209 	if (rda > main_end)
   210 		{
   211 		// ASSUMPTIONS HERE
   212 		// 1. root directory is past the end of the main ROM so there must be an extension ROM
   213 		// 2. the ROM file system in the extension ROM is at the beginning of the ROM (similar to the
   214 		//    main ROM)
   215 		// 3. the extension ROM is mapped starting at a megabyte boundary
   216 		// Thus the address of the extension ROM header may be obtained by rounding the root directory
   217 		// address down to the next megabyte boundary.
   218          
   219  		TUint ext_start = rda &~ 0x000fffffu;
   220 		TUint ext_base = ((TExtensionRomHeader*)ext_start)->iRomBase;
   221 		TUint ext_end = ext_start + ((TExtensionRomHeader*)ext_start)->iUncompressedSize;
   222 		aBool = (ext_base==ext_start && a>=ext_start && a<ext_end);
   223  		}    return KErrNone;
   224     }
   225 #endif
   226 
   227 
   228 #ifdef __MARM__
   229 EXPORT_C void E32Loader::GetV7StubAddresses(TLinAddr& aExe, TLinAddr& aDll)
   230 	{
   231 	// Only need V7 support on ARM platforms
   232 	aExe = (TLinAddr)&E32Loader::V7ExeEntryStub;
   233 	aDll = (TLinAddr)&E32Loader::V7DllEntryStub;
   234 	}
   235 #endif
   236 
   237 #ifdef __MARM__
   238 // Only need V7 support on ARM platforms
   239 
   240 _LIT(KEka1EntryStubName, "eka1_entry_stub.dll");
   241 extern "C" TLinAddr GetEka1ExeEntryPoint()
   242 	{
   243 	TLinAddr a = 0;
   244 	RLibrary l;
   245 	TInt r = l.Load(KEka1EntryStubName, KNullDesC, TUidType(KDynamicLibraryUid, KEka1EntryStubUid));
   246 	if (r == KErrNone)
   247 		r = l.Duplicate(RThread(), EOwnerProcess);
   248 	if (r == KErrNone)
   249 		{
   250 		a = (TLinAddr)l.Lookup(1);
   251 		if (!a)
   252 			r = KErrNotSupported;
   253 		}
   254 	if (r != KErrNone)
   255 		RThread().Kill(r);
   256 	Exec::SetReentryPoint(a);
   257 	return a;
   258 	}
   259 #endif