kernel/eka/kernel/sutils.cpp
changeset 47 46fffbe7b5a7
parent 45 329ab0095843
equal deleted inserted replaced
46:0bf4040442f9 47:46fffbe7b5a7
  4208 	TClientRequest::CallbackFunc(aData, aReason);
  4208 	TClientRequest::CallbackFunc(aData, aReason);
  4209 	}
  4209 	}
  4210 
  4210 
  4211 // Implementation of kernel pin APIs
  4211 // Implementation of kernel pin APIs
  4212 
  4212 
  4213 /*
  4213 /**
  4214 Create an object which can be used to pin virtual memory.
  4214 Create an object which can be used to pin virtual memory.
  4215 
  4215 
  4216 @param aPinObject A reference to a pointer which is set to the newly-created object on success.
  4216 @param aPinObject A reference to a pointer which is set to the newly-created object on success.
  4217 
  4217 
  4218 @return KErrNone, if successful, otherwise one of the other system-wide error codes.
  4218 @return KErrNone, if successful, otherwise one of the other system-wide error codes.
  4225 @pre Suitable for use in a device driver.
  4225 @pre Suitable for use in a device driver.
  4226 
  4226 
  4227 @see Kern::DestroyVirtualPinObject()
  4227 @see Kern::DestroyVirtualPinObject()
  4228 
  4228 
  4229 @prototype
  4229 @prototype
  4230 @internalTechnology
       
  4231 */
  4230 */
  4232 EXPORT_C TInt Kern::CreateVirtualPinObject(TVirtualPinObject*& aPinObject)
  4231 EXPORT_C TInt Kern::CreateVirtualPinObject(TVirtualPinObject*& aPinObject)
  4233 	{
  4232 	{
  4234 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreateVirtualPinObject");			
  4233 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreateVirtualPinObject");			
  4235 	return M::CreateVirtualPinObject(aPinObject);
  4234 	return M::CreateVirtualPinObject(aPinObject);
  4236 	}
  4235 	}
  4237 
  4236 
  4238 /*
  4237 /**
  4239 Pin an area of virtual memory.
  4238 Pin an area of virtual memory.
  4240 
  4239 
  4241 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4240 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4242 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual physical
  4241 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual physical
  4243 RAM used is not guaranteed to stay the same however, as it could be replaced in the process of RAM
  4242 RAM used is not guaranteed to stay the same however, as it could be replaced in the process of RAM
  4263 @pre Can be used in a device driver.
  4262 @pre Can be used in a device driver.
  4264 
  4263 
  4265 @see Kern::UnpinVirtualMemory()
  4264 @see Kern::UnpinVirtualMemory()
  4266 
  4265 
  4267 @prototype
  4266 @prototype
  4268 @internalTechnology
       
  4269 */
  4267 */
  4270 EXPORT_C TInt Kern::PinVirtualMemory(TVirtualPinObject* aPinObject, TLinAddr aStart, TUint aSize, DThread* aThread)
  4268 EXPORT_C TInt Kern::PinVirtualMemory(TVirtualPinObject* aPinObject, TLinAddr aStart, TUint aSize, DThread* aThread)
  4271 	{
  4269 	{
  4272 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::PinVirtualMemory");
  4270 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::PinVirtualMemory");
  4273 	if (aThread == NULL)
  4271 	if (aThread == NULL)
  4278 	TInt r = M::PinVirtualMemory(aPinObject, aStart, aSize, aThread);
  4276 	TInt r = M::PinVirtualMemory(aPinObject, aStart, aSize, aThread);
  4279 	NKern::ThreadLeaveCS();
  4277 	NKern::ThreadLeaveCS();
  4280 	return r;
  4278 	return r;
  4281 	}
  4279 	}
  4282 
  4280 
  4283 /*
  4281 /**
  4284 Pin an area of virtual memory.
  4282 Pin an area of virtual memory.
  4285 
  4283 
  4286 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4284 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4287 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual phyiscal
  4285 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual phyiscal
  4288 RAM used is not guaranteed to stay the same however, as it could be replaced in the process of RAM
  4286 RAM used is not guaranteed to stay the same however, as it could be replaced in the process of RAM
  4292 client thread, so that when it is accessed from a different thread later on (for example from a DFC
  4290 client thread, so that when it is accessed from a different thread later on (for example from a DFC
  4293 thread) there is no possibility of taking page faults.
  4291 thread) there is no possibility of taking page faults.
  4294 
  4292 
  4295 Note that this operation may fail with KErrNoMemory.
  4293 Note that this operation may fail with KErrNoMemory.
  4296 
  4294 
       
  4295 Note - Instances of TVirtualPinObject are not protected from concurrent operations being performed 
       
  4296 with them by separate threads i.e. it is the responsibility of the creator of a TVirtualPinObject 
       
  4297 instance to ensure that it will not be pinned, unpinned or destroyed by more than one thread at a time.
       
  4298 
  4297 @param aPinObject  A virtual pin object previously created by calling Kern::CreateVirtualPinObject().
  4299 @param aPinObject  A virtual pin object previously created by calling Kern::CreateVirtualPinObject().
  4298 @param aDes	       A TClientBuffer object representing a client descriptor to pin.
  4300 @param aDes	       A TClientBuffer object representing a client descriptor to pin.
  4299 @param aThread	   The thread that owns the memory to pin, or NULL to use the current thread.
  4301 @param aThread	   The thread that owns the memory to pin, or NULL to use the current thread.
  4300 
  4302 
  4301 @return KErrNone, if successful, otherwse one of the other system-wide error codes.
  4303 @return KErrNone, if successful, otherwse one of the other system-wide error codes.
  4307 @pre Can be used in a device driver.
  4309 @pre Can be used in a device driver.
  4308 
  4310 
  4309 @see Kern::UnpinVirtualMemory()
  4311 @see Kern::UnpinVirtualMemory()
  4310 
  4312 
  4311 @prototype
  4313 @prototype
  4312 @internalTechnology
       
  4313 */
  4314 */
  4314 EXPORT_C TInt Kern::PinVirtualMemory(TVirtualPinObject* aPinObject, const TClientBuffer& aDes, DThread* aThread)
  4315 EXPORT_C TInt Kern::PinVirtualMemory(TVirtualPinObject* aPinObject, const TClientBuffer& aDes, DThread* aThread)
  4315 	{
  4316 	{
  4316 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::PinVirtualMemory");
  4317 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::PinVirtualMemory");
  4317 	if (aThread == NULL)
  4318 	if (aThread == NULL)
  4324 	NKern::ThreadEnterCS();
  4325 	NKern::ThreadEnterCS();
  4325 	TInt r = M::PinVirtualMemory(aPinObject, (TLinAddr)aDes.DataPtr(), length, aThread);
  4326 	TInt r = M::PinVirtualMemory(aPinObject, (TLinAddr)aDes.DataPtr(), length, aThread);
  4326 	NKern::ThreadLeaveCS();
  4327 	NKern::ThreadLeaveCS();
  4327 	return r;
  4328 	return r;
  4328 	}
  4329 	}
  4329 /*
  4330 /**
  4330 Create a pin object and then pin an area of virtual memory in the current address space.  If 
  4331 Create a pin object and then pin an area of virtual memory in the current address space.  If 
  4331 an error occurs then no pin object will exist
  4332 an error occurs then no pin object will exist
  4332 
  4333 
  4333 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4334 The act of pinning virtual memory means that the memory in the specified virtual address range is
  4334 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual physical
  4335 guaranteed to remain in system RAM while it is pinned, unless it is decommited.  The actual physical
  4339 client thread, so that when it is accessed from a different thread later on (for example from a DFC
  4340 client thread, so that when it is accessed from a different thread later on (for example from a DFC
  4340 thread) there is no possibility of taking page faults.
  4341 thread) there is no possibility of taking page faults.
  4341 
  4342 
  4342 Note that this operation may fail with KErrNoMemory.
  4343 Note that this operation may fail with KErrNoMemory.
  4343 
  4344 
       
  4345 Note - Instances of TVirtualPinObject are not protected from concurrent operations being performed 
       
  4346 with them by separate threads i.e. it is the responsibility of the creator of a TVirtualPinObject 
       
  4347 instance to ensure that it will not be pinned, unpinned or destroyed by more than one thread at a time.
       
  4348 
  4344 @param aPinObject	A reference to a pointer which is set to the newly-created object on success.
  4349 @param aPinObject	A reference to a pointer which is set to the newly-created object on success.
  4345 @param aStart		The start address of the memory to pin.
  4350 @param aStart		The start address of the memory to pin.
  4346 @param aSize		The size of the memory to pin in bytes.
  4351 @param aSize		The size of the memory to pin in bytes.
  4347 
  4352 
  4348 @return KErrNone, if successful, otherwise one of the other system-wide error codes.
  4353 @return KErrNone, if successful, otherwise one of the other system-wide error codes.
  4356 
  4361 
  4357 @see Kern::UnpinVirtualMemory()
  4362 @see Kern::UnpinVirtualMemory()
  4358 @see Kern::DestroyVirtualPinObject()
  4363 @see Kern::DestroyVirtualPinObject()
  4359 
  4364 
  4360 @prototype
  4365 @prototype
  4361 @internalTechnology
       
  4362 */
  4366 */
  4363 EXPORT_C TInt Kern::CreateAndPinVirtualMemory(TVirtualPinObject*& aPinObject, TLinAddr aStart, TUint aSize)
  4367 EXPORT_C TInt Kern::CreateAndPinVirtualMemory(TVirtualPinObject*& aPinObject, TLinAddr aStart, TUint aSize)
  4364 	{
  4368 	{
  4365 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreateAndPinVirtualMemory");
  4369 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreateAndPinVirtualMemory");
  4366 	return M::CreateAndPinVirtualMemory(aPinObject, aStart, aSize);
  4370 	return M::CreateAndPinVirtualMemory(aPinObject, aStart, aSize);
  4367 	}
  4371 	}
  4368 
  4372 
  4369 
  4373 
  4370 /*
  4374 /**
  4371 Unpin an area of memory previously pinned by calling Kern::PinVirtualMemory().
  4375 Unpin an area of memory previously pinned by calling Kern::PinVirtualMemory().
  4372 
  4376 
  4373 @param aPinObject  The virtual pin object used to pin the memory.
  4377 @param aPinObject  The virtual pin object used to pin the memory.
  4374 
  4378 
  4375 @pre Interrupts must be enabled.
  4379 @pre Interrupts must be enabled.
  4379 @pre Can be used in a device driver.
  4383 @pre Can be used in a device driver.
  4380 
  4384 
  4381 @see Kern::PinVirtualMemory()
  4385 @see Kern::PinVirtualMemory()
  4382 
  4386 
  4383 @prototype
  4387 @prototype
  4384 @internalTechnology
       
  4385 */
  4388 */
  4386 EXPORT_C void Kern::UnpinVirtualMemory(TVirtualPinObject* aPinObject)
  4389 EXPORT_C void Kern::UnpinVirtualMemory(TVirtualPinObject* aPinObject)
  4387 	{
  4390 	{
  4388 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::UnpinVirtualMemory");
  4391 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::UnpinVirtualMemory");
  4389 	NKern::ThreadEnterCS();
  4392 	NKern::ThreadEnterCS();
  4390 	M::UnpinVirtualMemory(aPinObject);
  4393 	M::UnpinVirtualMemory(aPinObject);
  4391 	NKern::ThreadLeaveCS();
  4394 	NKern::ThreadLeaveCS();
  4392 	}
  4395 	}
  4393 
  4396 
  4394 /*
  4397 /**
  4395 Dispose of a virtual pin object which is no longer required.
  4398 Dispose of a virtual pin object which is no longer required.
  4396 
  4399 
  4397 Any memory pinned by the object is unpinned first.
  4400 Any memory pinned by the object is unpinned first.
  4398 
  4401 
  4399 @param	aPinObject	A reference to a pointer to the pin object to destroy.
  4402 @param	aPinObject	A reference to a pointer to the pin object to destroy.
  4407 @pre Suitable for use in a device driver.
  4410 @pre Suitable for use in a device driver.
  4408 
  4411 
  4409 @see Kern::CreateVirtualPinObject()
  4412 @see Kern::CreateVirtualPinObject()
  4410 
  4413 
  4411 @prototype
  4414 @prototype
  4412 @internalTechnology
       
  4413 */
  4415 */
  4414 EXPORT_C void Kern::DestroyVirtualPinObject(TVirtualPinObject*& aPinObject)
  4416 EXPORT_C void Kern::DestroyVirtualPinObject(TVirtualPinObject*& aPinObject)
  4415 	{
  4417 	{
  4416 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::DestroyVirtualPinObject");			
  4418 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::DestroyVirtualPinObject");			
  4417 	M::DestroyVirtualPinObject(aPinObject);
  4419 	M::DestroyVirtualPinObject(aPinObject);
  4418 	}
  4420 	}
  4419 
  4421 
  4420 /**
  4422 /**
  4421 Creates an object which is used to pin physical memory. Suported by Kernel running flexible memory model.
  4423 Creates an object which is used to pin physical memory. Supported by Kernel running flexible memory model.
       
  4424 
       
  4425 Note - Instances of TPhysicalPinObject are not protected from concurrent operations being performed 
       
  4426 with them by separate threads i.e. it is the responsibility of the creator of a TPhysicalPinObject 
       
  4427 instance to ensure that it will not be pinned, unpinned or destroyed by more than one thread at a time.
  4422 
  4428 
  4423 @param aPinObject A reference to a pointer which is set to the newly-created object on success.
  4429 @param aPinObject A reference to a pointer which is set to the newly-created object on success.
  4424 
  4430 
  4425 @return KErrNotSupported on memory models other then flexible.
  4431 @return KErrNotSupported on memory models other then flexible.
  4426 		KErrNone, if successful, otherwise one of the other system-wide error codes.
  4432 		KErrNone, if successful, otherwise one of the other system-wide error codes.
  4441 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreatePhysicalPinObject");
  4447 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreatePhysicalPinObject");
  4442 	return M::CreatePhysicalPinObject(aPinObject);
  4448 	return M::CreatePhysicalPinObject(aPinObject);
  4443 	}
  4449 	}
  4444 
  4450 
  4445 /**
  4451 /**
  4446 Pins an area of physical memory. Suported by Kernel running flexible memory model.
  4452 Pins an area of physical memory. Supported by Kernel running flexible memory model.
  4447 
  4453 
  4448 The physical memory to pin is defined by its existing virtual mapping (by aLinAddr, aSize & aThread parameters).
  4454 The physical memory to pin is defined by its existing virtual mapping (by aStart, aSize & aThread parameters).
  4449 On return, aPhysicalAddress will hold physical address (if memory is mapped contigiously) and aPhysicalPageList
  4455 On return, aAddress will hold physical address (if memory is mapped contigiously) and aPages
  4450 area will be populated with the list of physical pages of the mapping. aColour will hold the mapping colour
  4456 area will be populated with the list of physical pages of the mapping. aColour will hold the mapping colour
  4451 of the first physical page in the mapping.
  4457 of the first physical page in the mapping.
  4452 
  4458 
  4453 This operation is provided to enable device drivers to operate DMA transfers on memory which is not mapped to
  4459 This operation is provided to enable device drivers to operate DMA transfers on memory which is not mapped to
  4454 Kernel address space (but to user client's, instead).
  4460 Kernel address space (but to user client's, instead).
  4455 
  4461 
  4456 The act of pinning physical memory means that it is guaranteed to be excluded from RAM defragmentation.
  4462 The act of pinning physical memory means that it is guaranteed to be excluded from RAM defragmentation.
  4457 However, it can still be the subject of data paging. Physically pinned memory is also guaranteed not to be
  4463 However, it can still be the subject of demand paging. Physically pinned memory is also guaranteed not to be
  4458 reused for some other purpose - even if the process owning the memory decommits it or terminates.
  4464 reused for some other purpose - even if the process owning the memory decommits it or terminates.
  4459 
  4465 
  4460 Note that this operation may fail with KErrNoMemory.
  4466 Note that this operation may fail with KErrNoMemory.
  4461 
  4467 
  4462 @param aPinObject		A physical pin object previously created by calling Kern::CreatePhysicalPinObject().
  4468 @param aPinObject		A physical pin object previously created by calling Kern::CreatePhysicalPinObject().
  4463 @param aLinAddr			Virtual address of memory to pin.
  4469 @param aStart			Virtual address of memory to pin.
  4464 @param aSize			The length (in bytes) of memory to pin.
  4470 @param aSize			The length (in bytes) of memory to pin.
  4465 @param aThread	   		The thread that owns the memory to pin, or NULL to use the current thread.
  4471 @param aThread	   		The thread that owns the memory to pin, or NULL to use the current thread.
  4466 @param aReadOnlyMemory  Set to ETrue if the content of physical memory is not going to change while being
  4472 @param aReadOnlyMemory  Set to ETrue if the content of physical memory is not going to change while being
  4467 						pinned, e.g. if it is DMA copied into H/W. Set to EFalse otherwise.
  4473 						pinned, e.g. if it is DMA copied into H/W. Set to EFalse otherwise.
  4468 						Setting this argument to ETrue will improve the performance when/if memory is paged out.
  4474 						Setting this argument to ETrue will improve the performance when/if memory is paged out.
  4489 @pre No fast mutex can be held.
  4495 @pre No fast mutex can be held.
  4490 @pre Call in a thread context.
  4496 @pre Call in a thread context.
  4491 @pre Can be used in a device driver.
  4497 @pre Can be used in a device driver.
  4492 
  4498 
  4493 @see Kern::UnpinPhysicalMemory()
  4499 @see Kern::UnpinPhysicalMemory()
  4494 @see Cache::SyncPhysicalMemoryBeforeDmaWrite
  4500 @see Cache::SyncPhysicalMemoryBeforeDmaWrite()
  4495 @see Cache::SyncPhysicalMemoryBeforeDmaRead
  4501 @see Cache::SyncPhysicalMemoryBeforeDmaRead()
  4496 @see Cache::SyncPhysicalMemoryAfterDmaRead
  4502 @see Cache::SyncPhysicalMemoryAfterDmaRead()
  4497 @prototype
  4503 @prototype
  4498  */
  4504  */
  4499 EXPORT_C TInt Kern::PinPhysicalMemory(TPhysicalPinObject* aPinObject, TLinAddr aStart, TUint aSize, TBool aReadOnlyMemory,
  4505 EXPORT_C TInt Kern::PinPhysicalMemory(TPhysicalPinObject* aPinObject, TLinAddr aStart, TUint aSize, TBool aReadOnlyMemory,
  4500 				 				TPhysAddr& aAddress, TPhysAddr* aPages, TUint32& aMapAttr, TUint& aColour, DThread* aThread)
  4506 				 				TPhysAddr& aAddress, TPhysAddr* aPages, TUint32& aMapAttr, TUint& aColour, DThread* aThread)
  4501 	{
  4507 	{
  4535 	M::UnpinPhysicalMemory(aPinObject);
  4541 	M::UnpinPhysicalMemory(aPinObject);
  4536 	NKern::ThreadLeaveCS();
  4542 	NKern::ThreadLeaveCS();
  4537 	return KErrNone;
  4543 	return KErrNone;
  4538 	}
  4544 	}
  4539 
  4545 
  4540 /*
  4546 /**
  4541 Dispose of a physical pin object which is no longer required.
  4547 Dispose of a physical pin object which is no longer required.
  4542 
  4548 
  4543 Any memory pinned by the object is unpinned first.
  4549 Any memory pinned by the object is unpinned first.
  4544 
  4550 
  4545 @param	aPinObject	A reference to a pointer to the pin object to destroy.
  4551 @param	aPinObject	A reference to a pointer to the pin object to destroy.
  4563 	{
  4569 	{
  4564 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::DestroyPhysicalPinObject");
  4570 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::DestroyPhysicalPinObject");
  4565 	M::DestroyPhysicalPinObject(aPinObject);
  4571 	M::DestroyPhysicalPinObject(aPinObject);
  4566 	return KErrNone;
  4572 	return KErrNone;
  4567 	}
  4573 	}
       
  4574 
       
  4575 
       
  4576 /**
       
  4577 Creates an object which is used to map user side memory in the kernel address space 
       
  4578 and also physically pin the memory. Supported by Kernel running the flexible memory model.
       
  4579 
       
  4580 When the map object will only be used to map non-demand paged memory aMaxReserveSize can be used 
       
  4581 to pre-reserve the resources that would be required for Kern::MapAndPinMemory().  This will 
       
  4582 prevent Kern::MapAndPinMemory() failing due to low memory and will reduce the time to execute
       
  4583 Kern::MapAndPinMemory() but only when the mapping object is used to map non-demand paged memory.
       
  4584 
       
  4585 Note - Instances of TKernelMapObject are not protected from concurrent operations being performed 
       
  4586 with them by separate threads i.e. it is the responsibility of the creator of a TKernelMapObject 
       
  4587 instance to ensure that it will not be mapped, unmapped or destroyed by more than one thread at a time.
       
  4588 
       
  4589 @param aMapObject 		A reference to a pointer which is set to the newly created object on success.
       
  4590 @param aMaxReserveSize	When set to zero (the default) the resources required to map memory will be
       
  4591 						allocated when Kern::MapAndPinMemory() is invoked with the newly created map 
       
  4592 						object.	 When not set to zero all resources required for the mapping object 
       
  4593 						to map up to aMaxReserveSize bytes will be reserved when creating the mapping 
       
  4594 						object.  When the mapping object is used to map non-demand paged memory, 
       
  4595 						this will prevent Kern::MapAndPinMemory() from returning KErrNoMemory 
       
  4596 						when invoked with the mapping object.  However, it will limit the mapping 
       
  4597 						object to being able to map a maximum of aMaxReserveSize bytes and may also
       
  4598 						waste resources if it is used to map less than aMaxReserveSize bytes.
       
  4599 
       
  4600 @return KErrNotSupported on memory models other then flexible.
       
  4601 		KErrNone, if successful, otherwise one of the other system-wide error codes.
       
  4602 
       
  4603 @pre Calling thread must be in a critical section
       
  4604 @pre Interrupts must be enabled.
       
  4605 @pre Kernel must be unlocked.
       
  4606 @pre No fast mutex can be held.
       
  4607 @pre Call in a thread context.
       
  4608 @pre Suitable for use in a device driver.
       
  4609 
       
  4610 @see Kern::DestroyKernelMapObject()
       
  4611 @see Kern::MapAndPinMemory()
       
  4612 
       
  4613 @prototype
       
  4614 */
       
  4615 EXPORT_C TInt Kern::CreateKernelMapObject(TKernelMapObject*& aMapObject, TUint aMaxReserveSize)
       
  4616 	{
       
  4617 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::CreateKernelMapObject");			
       
  4618 	return M::CreateKernelMapObject(aMapObject, aMaxReserveSize);
       
  4619 	}
       
  4620 
       
  4621 
       
  4622 /**
       
  4623 Maps an area of memory into the kernel address space and physically pins it. Supported by Kernel running 
       
  4624 the flexible memory model.
       
  4625 
       
  4626 The memory to map and pin is defined by its existing virtual mapping (via the aStart, aSize & aThread parameters).
       
  4627 On return, if aPages is not NULL, aPages will be populated with the list of physical pages mapped.
       
  4628 
       
  4629 This operation is provided to enable device drivers to not only operate DMA transfers on memory which was not 
       
  4630 originally mapped to the kernel address space (but to user client's, instead) but also read or modify the data 
       
  4631 from kernel context, e.g. a DFC.
       
  4632 
       
  4633 The act of mapping the memory means that it is guaranteed to be excluded from RAM defragmentation.  The new mapping 
       
  4634 will also be protected from the effects of demand paging.  Once mapped the memory is also guaranteed not to be reused for 
       
  4635 some other purpose - even if the process owning the memory decommits it or terminates.
       
  4636 
       
  4637 Note that this operation may fail with KErrNoMemory if the kernel mapping object was created with aMaxReserveSize==0
       
  4638 or the memory to be mapped is demand paged.
       
  4639 
       
  4640 @param aMapObject		A kernel map object previously created by calling Kern::CreateKernelMapObject().
       
  4641 @param aThread	   		The thread that owns the memory to map, or NULL to use the current thread.
       
  4642 @param aStart			Virtual address of memory to map.
       
  4643 @param aSize			The length (in bytes) of memory to map.
       
  4644 @param aMapAttributes	A bit mask of TKernelMapAttributes attributes for the mapping.
       
  4645 @param aKernelAddr		Set to the base linear address of the new kernel mapping.  This is the address to use when accessing
       
  4646 						the memory from kernel side and to pass to cache maintence operations before and after performing
       
  4647 						DMA operations.
       
  4648 @param aPages			Points to area of TPhysAddr which will on exit hold the addresses of the physical pages contained
       
  4649 						in the specified region. The array must be large enough to hold the whole list of pages in the region.
       
  4650 						Set aPages to NULL if the physical addresses of the pages being mapped are not required.
       
  4651 
       
  4652 @return 				KErrNone, if successful,
       
  4653 						KErrInUse if aMapObject is already mapping some memory,
       
  4654 						KErrArgument if aSize is larger than the argument aMaxReserveSize of Kern::CreateKernelMapObject()
       
  4655 						when creating aMapObject or aMapAttributes has invalid bits set.
       
  4656 						KErrNotSupported on memory models other then flexible.
       
  4657 						Otherwise one of the other system-wide error codes.
       
  4658 
       
  4659 @pre Interrupts must be enabled.
       
  4660 @pre Kernel must be unlocked.
       
  4661 @pre No fast mutex can be held.
       
  4662 @pre Call in a thread context.
       
  4663 @pre Can be used in a device driver.
       
  4664 
       
  4665 @see Kern::UnmapAndUnpinMemory()
       
  4666 @see Cache::SyncMemoryBeforeDmaWrite()
       
  4667 @see Cache::SyncMemoryBeforeDmaRead()
       
  4668 @see Cache::SyncMemoryAfterDmaRead()
       
  4669 @prototype
       
  4670  */
       
  4671 EXPORT_C TInt Kern::MapAndPinMemory(TKernelMapObject* aMapObject, DThread* aThread, TLinAddr aStart, TUint aSize, 
       
  4672 									TUint aMapAttributes, TLinAddr& aKernelAddr, TPhysAddr* aPages)
       
  4673 	{
       
  4674 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::MapAndPinMemory");
       
  4675 	if (~EKernelMap_ValidMask & aMapAttributes)
       
  4676 		{// Invalid mapping attribute flags set.
       
  4677 		return KErrArgument;
       
  4678 		}
       
  4679 	if (aThread == NULL)
       
  4680 		aThread = TheCurrentThread;
       
  4681 	if (aSize == 0)
       
  4682 		return KErrNone;
       
  4683 	NKern::ThreadEnterCS();
       
  4684 	TInt r = M::MapAndPinMemory(aMapObject, aThread, aStart, aSize, aMapAttributes, aKernelAddr, aPages);
       
  4685 	NKern::ThreadLeaveCS();
       
  4686 	return r;
       
  4687 	}
       
  4688 
       
  4689 
       
  4690 /**
       
  4691 Unmaps and unpins an area of memory previously mapped by calling Kern::MapAndPinMemory().
       
  4692 
       
  4693 @param aMapObject  The kernel mapping object used to map and pin the memory.
       
  4694 
       
  4695 @pre Interrupts must be enabled.
       
  4696 @pre Kernel must be unlocked.
       
  4697 @pre No fast mutex can be held.
       
  4698 @pre Call in a thread context.
       
  4699 @pre Can be used in a device driver.
       
  4700 
       
  4701 @see Kern::MapAndPinMemory()
       
  4702 
       
  4703 @prototype
       
  4704 */
       
  4705 EXPORT_C void Kern::UnmapAndUnpinMemory(TKernelMapObject* aMapObject)
       
  4706 	{
       
  4707 	CHECK_PRECONDITIONS(MASK_THREAD_STANDARD,"Kern::UnmapAndUnpinMemory");
       
  4708 	NKern::ThreadEnterCS();
       
  4709 	M::UnmapAndUnpinMemory(aMapObject);
       
  4710 	NKern::ThreadLeaveCS();
       
  4711 	}
       
  4712 
       
  4713 
       
  4714 /**
       
  4715 Dispose of a kernel mapping object which is no longer required.
       
  4716 
       
  4717 Any memory mapped and pinned by the object is unmapped and unpinned first.
       
  4718 
       
  4719 @param	aMapObject	A reference to a pointer to the mapping object to destroy.
       
  4720 					This pointer will be set to NULL on return.
       
  4721 
       
  4722 @pre Calling thread must be in a critical section
       
  4723 @pre Interrupts must be enabled.
       
  4724 @pre Kernel must be unlocked.
       
  4725 @pre No fast mutex can be held.
       
  4726 @pre Call in a thread context.
       
  4727 @pre Suitable for use in a device driver.
       
  4728 
       
  4729 @see Kern::CreateKernelMapObject()
       
  4730 
       
  4731 @prototype
       
  4732 */
       
  4733 EXPORT_C void Kern::DestroyKernelMapObject(TKernelMapObject*& aMapObject)
       
  4734 	{
       
  4735 	CHECK_PRECONDITIONS(MASK_THREAD_CRITICAL,"Kern::DestroyKernelMapObject");
       
  4736 	M::DestroyKernelMapObject(aMapObject);
       
  4737 	}