123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- /*++
- Copyright (c) 2012 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- sysres.c
- Abstract:
- This module implements management of builtin system resources.
- Author:
- Evan Green 17-Oct-2012
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- #include "keinit.h"
- #include "kep.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define SYSTEM_RESOURCE_ALLOCATION_TAG 0x52737953 // 'RsyS'
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Define a lock that protects access to the system resources and the system
- // resource list head.
- //
- KSPIN_LOCK KeSystemResourceSpinLock;
- LIST_ENTRY KeSystemResourceListHead;
- //
- // ------------------------------------------------------------------ Functions
- //
- KERNEL_API
- PSYSTEM_RESOURCE_HEADER
- KeAcquireSystemResource (
- SYSTEM_RESOURCE_TYPE ResourceType
- )
- /*++
- Routine Description:
- This routine attempts to find an unacquired system resource of the given
- type.
- Arguments:
- ResourceType - Supplies the type of builtin resource to acquire.
- Return Value:
- Returns a pointer to a resource of the given type on success.
- NULL on failure.
- --*/
- {
- return KepGetSystemResource(ResourceType, TRUE);
- }
- KERNEL_API
- VOID
- KeReleaseSystemResource (
- PSYSTEM_RESOURCE_HEADER ResourceHeader
- )
- /*++
- Routine Description:
- This routine releases a system resource.
- Arguments:
- ResourceHeader - Supplies a pointer to the resource header to release back
- to the system.
- Return Value:
- None.
- --*/
- {
- BOOL Enabled;
- Enabled = ArDisableInterrupts();
- KeAcquireSpinLock(&KeSystemResourceSpinLock);
- ResourceHeader->Acquired = FALSE;
- KeReleaseSpinLock(&KeSystemResourceSpinLock);
- if (Enabled != FALSE) {
- ArEnableInterrupts();
- }
- return;
- }
- KSTATUS
- KepInitializeSystemResources (
- PKERNEL_INITIALIZATION_BLOCK Parameters,
- ULONG Phase
- )
- /*++
- Routine Description:
- This routine initializes the system resource manager.
- Arguments:
- Parameters - Supplies a pointer to the kernel initialization block.
- Phase - Supplies the phase. Valid values are 0 and 1.
- Return Value:
- Status code.
- --*/
- {
- PLIST_ENTRY CurrentEntry;
- PSYSTEM_RESOURCE_HEADER GenericHeader;
- ULONG NewEntrySize;
- PSYSTEM_RESOURCE_HEADER NewHeader;
- KSTATUS Status;
- LIST_ENTRY TemporaryListHead;
- Status = STATUS_SUCCESS;
- //
- // In phase 0, initialize the spin lock and move all resources off of the
- // loader block. Pools are not yet available.
- //
- if (Phase == 0) {
- KeInitializeSpinLock(&KeSystemResourceSpinLock);
- INITIALIZE_LIST_HEAD(&KeSystemResourceListHead);
- if (LIST_EMPTY(&(Parameters->SystemResourceListHead)) == FALSE) {
- MOVE_LIST(&(Parameters->SystemResourceListHead),
- &KeSystemResourceListHead);
- INITIALIZE_LIST_HEAD(&(Parameters->SystemResourceListHead));
- }
- } else {
- ASSERT(Phase == 1);
- //
- // In preparation for all boot mappings being released, reallocate
- // each entry in non-paged pool. Start by putting all current entries
- // on a temporary list.
- //
- INITIALIZE_LIST_HEAD(&TemporaryListHead);
- if (LIST_EMPTY(&KeSystemResourceListHead) == FALSE) {
- MOVE_LIST(&KeSystemResourceListHead, &TemporaryListHead);
- INITIALIZE_LIST_HEAD(&KeSystemResourceListHead);
- }
- //
- // Grab each item off the temporary list.
- //
- while (!LIST_EMPTY(&TemporaryListHead)) {
- CurrentEntry = TemporaryListHead.Next;
- LIST_REMOVE(CurrentEntry);
- GenericHeader = LIST_VALUE(CurrentEntry,
- SYSTEM_RESOURCE_HEADER,
- ListEntry);
- //
- // Determine the size of the entry.
- //
- switch (GenericHeader->Type) {
- case SystemResourceFrameBuffer:
- NewEntrySize = sizeof(SYSTEM_RESOURCE_FRAME_BUFFER);
- break;
- case SystemResourceHardwareModule:
- NewEntrySize = sizeof(SYSTEM_RESOURCE_HARDWARE_MODULE);
- break;
- case SystemResourceRamDisk:
- NewEntrySize = sizeof(SYSTEM_RESOURCE_RAM_DISK);
- break;
- case SystemResourceDebugDevice:
- NewEntrySize = sizeof(SYSTEM_RESOURCE_DEBUG_DEVICE);
- break;
- //
- // Unknown resource type. Something bad has probably happened.
- //
- default:
- ASSERT(FALSE);
- Status = STATUS_UNSUCCESSFUL;
- goto InitializeSystemResourcesEnd;
- }
- //
- // Allocate and initialize the new entry.
- //
- NewHeader = MmAllocateNonPagedPool(NewEntrySize,
- SYSTEM_RESOURCE_ALLOCATION_TAG);
- if (NewHeader == NULL) {
- Status = STATUS_NO_MEMORY;
- goto InitializeSystemResourcesEnd;
- }
- RtlCopyMemory(NewHeader, GenericHeader, NewEntrySize);
- //
- // Insert the new entry onto the main list. Simply drop the old
- // boot entry as it will get cleaned up later during MM
- // initialization.
- //
- INSERT_BEFORE(&(NewHeader->ListEntry), &KeSystemResourceListHead);
- }
- }
- InitializeSystemResourcesEnd:
- return Status;
- }
- PSYSTEM_RESOURCE_HEADER
- KepGetSystemResource (
- SYSTEM_RESOURCE_TYPE ResourceType,
- BOOL Acquire
- )
- /*++
- Routine Description:
- This routine attempts to find an unacquired system resource of the given
- type.
- Arguments:
- ResourceType - Supplies the type of builtin resource to acquire.
- Acquire - Supplies a boolean indicating if the resource should be acquired
- or not.
- Return Value:
- Returns a pointer to a resource of the given type on success.
- NULL on failure.
- --*/
- {
- PLIST_ENTRY CurrentEntry;
- BOOL Enabled;
- PSYSTEM_RESOURCE_HEADER Header;
- //
- // Acquire the high level lock.
- //
- Enabled = ArDisableInterrupts();
- KeAcquireSpinLock(&KeSystemResourceSpinLock);
- CurrentEntry = KeSystemResourceListHead.Next;
- while (CurrentEntry != &KeSystemResourceListHead) {
- Header = LIST_VALUE(CurrentEntry, SYSTEM_RESOURCE_HEADER, ListEntry);
- if ((Header->Type == ResourceType) && (Header->Acquired == FALSE)) {
- if (Acquire != FALSE) {
- Header->Acquired = TRUE;
- }
- break;
- }
- CurrentEntry = CurrentEntry->Next;
- }
- if (CurrentEntry == &KeSystemResourceListHead) {
- Header = NULL;
- }
- //
- // Release the high level lock.
- //
- KeReleaseSpinLock(&KeSystemResourceSpinLock);
- if (Enabled != FALSE) {
- ArEnableInterrupts();
- }
- return Header;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|