2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/config/cmsysini.c
5 * PURPOSE: Configuration Manager - System Initialization Code
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
9 /* INCLUDES ******************************************************************/
16 POBJECT_TYPE CmpKeyObjectType
;
17 PCMHIVE CmiVolatileHive
;
18 LIST_ENTRY CmpHiveListHead
;
19 ERESOURCE CmpRegistryLock
;
20 KGUARDED_MUTEX CmpSelfHealQueueLock
;
21 LIST_ENTRY CmpSelfHealQueueListHead
;
22 KEVENT CmpLoadWorkerEvent
;
23 LONG CmpLoadWorkerIncrement
;
24 PEPROCESS CmpSystemProcess
;
25 BOOLEAN HvShutdownComplete
;
26 PVOID CmpRegistryLockCallerCaller
, CmpRegistryLockCaller
;
27 BOOLEAN CmpFlushStarveWriters
;
28 BOOLEAN CmpFlushOnLockRelease
;
29 BOOLEAN CmpSpecialBootCondition
;
31 BOOLEAN CmpForceForceFlush
;
32 BOOLEAN CmpWasSetupBoot
;
34 /* FUNCTIONS *****************************************************************/
38 CmpRosGetHardwareHive(OUT PULONG Length
)
40 PLIST_ENTRY ListHead
, NextEntry
;
41 PMEMORY_ALLOCATION_DESCRIPTOR MdBlock
= NULL
;
43 /* Loop the memory descriptors */
44 ListHead
= &KeLoaderBlock
->MemoryDescriptorListHead
;
45 NextEntry
= ListHead
->Flink
;
46 while (NextEntry
!= ListHead
)
48 /* Get the current block */
49 MdBlock
= CONTAINING_RECORD(NextEntry
,
50 MEMORY_ALLOCATION_DESCRIPTOR
,
53 /* Check if this is an registry block */
54 if (MdBlock
->MemoryType
== LoaderRegistryData
)
56 /* Check if it's not the SYSTEM hive that we already initialized */
57 if ((MdBlock
->BasePage
) !=
58 (((ULONG_PTR
)KeLoaderBlock
->RegistryBase
&~ KSEG0_BASE
) >> PAGE_SHIFT
))
60 /* Hardware hive break out */
65 /* Go to the next block */
66 NextEntry
= MdBlock
->ListEntry
.Flink
;
69 /* We need a hardware hive */
71 *Length
= MdBlock
->PageCount
<< PAGE_SHIFT
;
72 return (PVOID
)((MdBlock
->BasePage
<< PAGE_SHIFT
) | KSEG0_BASE
);
77 CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName
,
83 ULONG HiveDisposition
, LogDisposition
;
84 HANDLE FileHandle
= NULL
, LogHandle
= NULL
;
86 ULONG Operation
, FileType
;
93 /* Open or create the hive files */
94 Status
= CmpOpenHiveFiles(HiveName
,
104 if (!NT_SUCCESS(Status
)) return Status
;
106 /* Check if we have a log handle */
107 FileType
= (LogHandle
) ? HFILE_TYPE_LOG
: HFILE_TYPE_PRIMARY
;
109 /* Check if we created or opened the hive */
110 if (HiveDisposition
== FILE_CREATED
)
112 /* Do a create operation */
113 Operation
= HINIT_CREATE
;
118 /* Open it as a file */
119 Operation
= HINIT_FILE
;
123 /* Check if we're sharing hives */
124 if (CmpShareSystemHives
)
126 /* Then force using the primary hive */
127 FileType
= HFILE_TYPE_PRIMARY
;
130 /* Get rid of the log handle */
136 /* Check if we're too late */
137 if (HvShutdownComplete
)
141 if (LogHandle
) ZwClose(LogHandle
);
142 return STATUS_TOO_LATE
;
145 /* Initialize the hive */
146 Status
= CmpInitializeHive((PCMHIVE
*)&NewHive
,
156 if (!NT_SUCCESS(Status
))
160 if (LogHandle
) ZwClose(LogHandle
);
164 /* Success, return hive */
167 /* ROS: Init root key cell and prepare the hive */
168 if (Operation
== HINIT_CREATE
) CmCreateRootNode(&NewHive
->Hive
, L
"");
169 CmPrepareHive(&NewHive
->Hive
);
171 /* Duplicate the hive name */
172 NewHive
->FileFullPath
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
175 if (NewHive
->FileFullPath
.Buffer
)
177 /* Copy the string */
178 RtlCopyMemory(NewHive
->FileFullPath
.Buffer
,
181 NewHive
->FileFullPath
.Length
= HiveName
->Length
;
182 NewHive
->FileFullPath
.MaximumLength
= HiveName
->MaximumLength
;
186 return STATUS_SUCCESS
;
191 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
193 OBJECT_ATTRIBUTES ObjectAttributes
;
194 UNICODE_STRING KeyName
, ValueName
= {0};
197 ASSERT(LoaderBlock
!= NULL
);
199 /* Setup attributes for loader options */
200 RtlInitUnicodeString(&KeyName
,
201 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
203 InitializeObjectAttributes(&ObjectAttributes
,
205 OBJ_CASE_INSENSITIVE
,
208 Status
= NtOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
209 if (!NT_SUCCESS(Status
)) goto Quickie
;
211 /* Key opened, now write to the key */
212 RtlInitUnicodeString(&KeyName
, L
"SystemStartOptions");
213 Status
= NtSetValueKey(KeyHandle
,
217 CmpLoadOptions
.Buffer
,
218 CmpLoadOptions
.Length
);
219 if (!NT_SUCCESS(Status
)) goto Quickie
;
221 /* Setup value name for system boot device */
222 RtlInitUnicodeString(&KeyName
, L
"SystemBootDevice");
223 RtlCreateUnicodeStringFromAsciiz(&ValueName
, LoaderBlock
->NtBootPathName
);
224 Status
= NtSetValueKey(KeyHandle
,
232 /* Free the buffers */
233 RtlFreeUnicodeString(&ValueName
);
235 /* Close the key and return */
238 /* Return the status */
239 return (ExpInTextModeSetup
? STATUS_SUCCESS
: Status
);
244 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
246 UNICODE_STRING ConfigName
= RTL_CONSTANT_STRING(L
"Control\\IDConfigDB");
247 UNICODE_STRING SelectName
=
248 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\Select");
249 UNICODE_STRING KeyName
;
250 OBJECT_ATTRIBUTES ObjectAttributes
;
251 CHAR ValueInfoBuffer
[128];
252 PKEY_VALUE_FULL_INFORMATION ValueInfo
;
254 WCHAR UnicodeBuffer
[128];
255 HANDLE SelectHandle
, KeyHandle
, ConfigHandle
= NULL
, ProfileHandle
= NULL
;
256 HANDLE ParentHandle
= NULL
;
257 ULONG ControlSet
, HwProfile
;
258 ANSI_STRING TempString
;
260 ULONG ResultLength
, Disposition
;
261 PLOADER_PARAMETER_EXTENSION LoaderExtension
;
264 /* Open the select key */
265 InitializeObjectAttributes(&ObjectAttributes
,
267 OBJ_CASE_INSENSITIVE
,
270 Status
= NtOpenKey(&SelectHandle
, KEY_READ
, &ObjectAttributes
);
271 if (!NT_SUCCESS(Status
))
273 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
274 if (!LoaderBlock
->RegistryBase
)
276 /* Use hard-coded setting */
281 /* Fail for real boots */
285 /* Open the current value */
286 RtlInitUnicodeString(&KeyName
, L
"Current");
287 Status
= NtQueryValueKey(SelectHandle
,
289 KeyValueFullInformation
,
291 sizeof(ValueInfoBuffer
),
293 NtClose(SelectHandle
);
294 if (!NT_SUCCESS(Status
)) return Status
;
296 /* Get the actual value pointer, and get the control set ID */
297 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
298 ControlSet
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
300 /* Create the current control set key */
302 RtlInitUnicodeString(&KeyName
,
303 L
"\\Registry\\Machine\\System\\CurrentControlSet");
304 InitializeObjectAttributes(&ObjectAttributes
,
306 OBJ_CASE_INSENSITIVE
,
310 Status
= NtCreateKey(&KeyHandle
,
315 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
317 if (!NT_SUCCESS(Status
)) return Status
;
320 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
322 /* Initialize the symbolic link name */
324 "\\Registry\\Machine\\System\\ControlSet%03ld",
326 RtlInitAnsiString(&TempString
, Buffer
);
328 /* Create a Unicode string out of it */
329 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
330 KeyName
.Buffer
= UnicodeBuffer
;
331 Status
= RtlAnsiStringToUnicodeString(&KeyName
, &TempString
, FALSE
);
334 Status
= NtSetValueKey(KeyHandle
,
335 &CmSymbolicLinkValueName
,
340 if (!NT_SUCCESS(Status
)) return Status
;
342 /* Get the configuration database key */
343 InitializeObjectAttributes(&ObjectAttributes
,
345 OBJ_CASE_INSENSITIVE
,
348 Status
= NtOpenKey(&ConfigHandle
, KEY_READ
, &ObjectAttributes
);
351 /* Check if we don't have one */
352 if (!NT_SUCCESS(Status
))
354 /* Cleanup and exit */
359 /* Now get the current config */
360 RtlInitUnicodeString(&KeyName
, L
"CurrentConfig");
361 Status
= NtQueryValueKey(ConfigHandle
,
363 KeyValueFullInformation
,
365 sizeof(ValueInfoBuffer
),
368 /* Set pointer to buffer */
369 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
371 /* Check if we failed or got a non DWORD-value */
372 if (!(NT_SUCCESS(Status
)) || (ValueInfo
->Type
!= REG_DWORD
)) goto Cleanup
;
374 /* Get the hadware profile */
375 HwProfile
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
377 /* Open the hardware profile key */
378 RtlInitUnicodeString(&KeyName
,
379 L
"\\Registry\\Machine\\System\\CurrentControlSet"
380 L
"\\Hardware Profiles");
381 InitializeObjectAttributes(&ObjectAttributes
,
383 OBJ_CASE_INSENSITIVE
,
386 Status
= NtOpenKey(&ParentHandle
, KEY_READ
, &ObjectAttributes
);
387 if (!NT_SUCCESS(Status
))
389 /* Exit and clean up */
394 /* Build the profile name */
395 sprintf(Buffer
, "%04ld", HwProfile
);
396 RtlInitAnsiString(&TempString
, Buffer
);
398 /* Convert it to Unicode */
399 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
400 KeyName
.Buffer
= UnicodeBuffer
;
401 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
404 ASSERT(Status
== STATUS_SUCCESS
);
406 /* Open the associated key */
407 InitializeObjectAttributes(&ObjectAttributes
,
409 OBJ_CASE_INSENSITIVE
,
412 Status
= NtOpenKey(&ProfileHandle
,
413 KEY_READ
| KEY_WRITE
,
415 if (!NT_SUCCESS (Status
))
417 /* Cleanup and exit */
422 /* Check if we have a loader block extension */
423 LoaderExtension
= LoaderBlock
->Extension
;
426 ASSERTMSG("ReactOS doesn't support NTLDR Profiles yet!\n", FALSE
);
429 /* Create the current hardware profile key */
430 RtlInitUnicodeString(&KeyName
,
431 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
432 L
"Hardware Profiles\\Current");
433 InitializeObjectAttributes(&ObjectAttributes
,
435 OBJ_CASE_INSENSITIVE
,
438 Status
= NtCreateKey(&KeyHandle
,
443 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
445 if (NT_SUCCESS(Status
))
448 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
450 /* Create the profile name */
452 "\\Registry\\Machine\\System\\CurrentControlSet\\"
453 "Hardware Profiles\\%04ld",
455 RtlInitAnsiString(&TempString
, Buffer
);
457 /* Convert it to Unicode */
458 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
459 KeyName
.Buffer
= UnicodeBuffer
;
460 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
463 ASSERT(STATUS_SUCCESS
== Status
);
466 Status
= NtSetValueKey(KeyHandle
,
467 &CmSymbolicLinkValueName
,
475 /* Close every opened handle */
477 if (ConfigHandle
) NtClose(ConfigHandle
);
478 if (ProfileHandle
) NtClose(ProfileHandle
);
479 if (ParentHandle
) NtClose(ParentHandle
);
482 return STATUS_SUCCESS
;
487 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName
,
488 IN HANDLE RootDirectory
,
489 IN PCMHIVE RegistryHive
,
491 IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
493 OBJECT_ATTRIBUTES ObjectAttributes
;
494 UNICODE_STRING RemainingPath
;
495 PCM_KEY_BODY ParentKey
;
498 UNICODE_STRING ObjectName
;
499 OBJECT_CREATE_INFORMATION ObjectCreateInfo
;
500 CM_PARSE_CONTEXT ParseContext
= {0};
504 /* Setup the object attributes */
505 InitializeObjectAttributes(&ObjectAttributes
,
507 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
511 /* Setup the parse context */
512 ParseContext
.CreateLink
= TRUE
;
513 ParseContext
.CreateOperation
= TRUE
;
514 ParseContext
.ChildHive
.KeyHive
= &RegistryHive
->Hive
;
516 /* Check if we have a root keycell or if we need to create it */
520 ParseContext
.ChildHive
.KeyCell
= HCELL_NIL
;
525 ParseContext
.ChildHive
.KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
528 /* Create the link node */
529 Status
= ObOpenObjectByName(&ObjectAttributes
,
533 KEY_READ
| KEY_WRITE
,
534 (PVOID
)&ParseContext
,
537 /* Capture all the info */
538 Status
= ObpCaptureObjectAttributes(&ObjectAttributes
,
543 if (!NT_SUCCESS(Status
)) return Status
;
546 Status
= CmFindObject(&ObjectCreateInfo
,
554 /* Let go of captured attributes and name */
555 ObpReleaseCapturedAttributes(&ObjectCreateInfo
);
556 if (ObjectName
.Buffer
) ObpFreeObjectNameBuffer(&ObjectName
);
558 /* Get out of here if we failed */
559 if (!NT_SUCCESS(Status
)) return Status
;
561 /* Scan for no name */
562 if (!(RemainingPath
.Length
) || (RemainingPath
.Buffer
[0] == UNICODE_NULL
))
565 ObDereferenceObject(ParentKey
);
566 return STATUS_OBJECT_NAME_NOT_FOUND
;
569 /* Scan for leading backslash */
570 while ((RemainingPath
.Length
) &&
571 (*RemainingPath
.Buffer
== OBJ_NAME_PATH_SEPARATOR
))
574 RemainingPath
.Length
-= sizeof(WCHAR
);
575 RemainingPath
.MaximumLength
-= sizeof(WCHAR
);
576 RemainingPath
.Buffer
++;
579 /* Create the link node */
580 Status
= CmpCreateLinkNode(ParentKey
->KeyControlBlock
->KeyHive
,
581 ParentKey
->KeyControlBlock
->KeyCell
,
587 ParentKey
->KeyControlBlock
,
589 if (!NT_SUCCESS(Status
))
592 DPRINT1("CmpLinkHiveToMaster failed: %lx\n", Status
);
593 ObDereferenceObject(ParentKey
);
597 /* Free the create information */
598 ObpFreeAndReleaseCapturedAttributes(OBJECT_TO_OBJECT_HEADER(NewKey
)->ObjectCreateInfo
);
599 OBJECT_TO_OBJECT_HEADER(NewKey
)->ObjectCreateInfo
= NULL
;
601 /* Mark the hive as clean */
602 RegistryHive
->Hive
.DirtyFlag
= FALSE
;
604 /* Update KCB information */
605 NewKey
->KeyControlBlock
->KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
606 NewKey
->KeyControlBlock
->KeyHive
= &RegistryHive
->Hive
;
608 /* Reference the new key */
609 ObReferenceObject(NewKey
);
611 /* Link this key to the parent */
612 InsertTailList(&ParentKey
->KeyControlBlock
->KeyBodyListHead
, &NewKey
->KeyBodyList
);
613 return STATUS_SUCCESS
;
618 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
621 ANSI_STRING LoadString
;
626 UNICODE_STRING KeyName
;
627 PCMHIVE SystemHive
= NULL
;
628 UNICODE_STRING HiveName
= RTL_CONSTANT_STRING(L
"SYSTEM");
629 PSECURITY_DESCRIPTOR SecurityDescriptor
;
632 /* Setup the ansi string */
633 RtlInitAnsiString(&LoadString
, LoaderBlock
->LoadOptions
);
635 /* Allocate the unicode buffer */
636 Length
= LoadString
.Length
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
637 Buffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
641 KEBUGCHECKEX(BAD_SYSTEM_CONFIG_INFO
, 3, 1, (ULONG_PTR
)LoaderBlock
, 0);
644 /* Setup the unicode string */
645 RtlInitEmptyUnicodeString(&CmpLoadOptions
, Buffer
, (USHORT
)Length
);
647 /* Add the load options and null-terminate */
648 RtlAnsiStringToUnicodeString(&CmpLoadOptions
, &LoadString
, FALSE
);
649 CmpLoadOptions
.Buffer
[LoadString
.Length
] = UNICODE_NULL
;
650 CmpLoadOptions
.Length
+= sizeof(WCHAR
);
652 /* Get the System Hive base address */
653 HiveBase
= LoaderBlock
->RegistryBase
;
657 ((PHBASE_BLOCK
)HiveBase
)->Length
= LoaderBlock
->RegistryLength
;
658 Status
= CmpInitializeHive((PCMHIVE
*)&SystemHive
,
668 if (!NT_SUCCESS(Status
)) return FALSE
;
669 CmPrepareHive(&SystemHive
->Hive
);
671 /* Set the hive filename */
672 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
673 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
675 /* We imported, no need to create a new hive */
678 /* Manually set the hive as volatile, if in Live CD mode */
679 if (CmpShareSystemHives
) SystemHive
->Hive
.HiveFlags
= HIVE_VOLATILE
;
684 Status
= CmpInitializeHive(&SystemHive
,
694 if (!NT_SUCCESS(Status
)) return FALSE
;
696 /* Set the hive filename */
697 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
698 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
700 /* Tell CmpLinkHiveToMaster to allocate a hive */
704 /* Save the boot type */
705 if (SystemHive
) CmpBootType
= SystemHive
->Hive
.BaseBlock
->BootType
;
707 /* Are we in self-healing mode? */
710 /* Disable self-healing internally and check if boot type wanted it */
714 /* We're disabled, so bugcheck */
715 KEBUGCHECKEX(BAD_SYSTEM_CONFIG_INFO
,
718 (ULONG_PTR
)SystemHive
,
723 /* Create the default security descriptor */
724 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
726 /* Attach it to the system key */
727 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\SYSTEM");
728 Status
= CmpLinkHiveToMaster(&KeyName
,
734 /* Free the security descriptor */
735 ExFreePool(SecurityDescriptor
);
736 if (!NT_SUCCESS(Status
)) return FALSE
;
738 /* Add the hive to the hive list */
739 CmpMachineHiveList
[3].CmHive
= (PCMHIVE
)SystemHive
;
747 CmpCreateObjectTypes(VOID
)
749 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
751 GENERIC_MAPPING CmpKeyMapping
= {KEY_READ
,
757 /* Initialize the Key object type */
758 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
759 RtlInitUnicodeString(&Name
, L
"Key");
760 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
761 ObjectTypeInitializer
.DefaultPagedPoolCharge
= sizeof(CM_KEY_BODY
);
762 ObjectTypeInitializer
.GenericMapping
= CmpKeyMapping
;
763 ObjectTypeInitializer
.PoolType
= PagedPool
;
764 ObjectTypeInitializer
.ValidAccessMask
= KEY_ALL_ACCESS
;
765 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
766 ObjectTypeInitializer
.DeleteProcedure
= CmpDeleteKeyObject
;
767 ObjectTypeInitializer
.ParseProcedure
= CmpParseKey
;
768 ObjectTypeInitializer
.SecurityProcedure
= CmpSecurityMethod
;
769 ObjectTypeInitializer
.QueryNameProcedure
= CmpQueryKeyName
;
770 //ObjectTypeInitializer.CloseProcedure = CmpCloseKeyObject;
771 ObjectTypeInitializer
.SecurityRequired
= TRUE
;
774 return ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &CmpKeyObjectType
);
779 CmpCreateRootNode(IN PHHIVE Hive
,
781 OUT PHCELL_INDEX Index
)
783 UNICODE_STRING KeyName
;
784 PCM_KEY_NODE KeyCell
;
785 LARGE_INTEGER SystemTime
;
788 /* Initialize the node name and allocate it */
789 RtlInitUnicodeString(&KeyName
, Name
);
790 *Index
= HvAllocateCell(Hive
,
791 FIELD_OFFSET(CM_KEY_NODE
, Name
) +
792 CmpNameSize(Hive
, &KeyName
),
795 if (*Index
== HCELL_NIL
) return FALSE
;
797 /* Set the cell index and get the data */
798 Hive
->BaseBlock
->RootCell
= *Index
;
799 KeyCell
= (PCM_KEY_NODE
)HvGetCell(Hive
, *Index
);
800 if (!KeyCell
) return FALSE
;
803 KeyCell
->Signature
= (USHORT
)CM_KEY_NODE_SIGNATURE
;
804 KeyCell
->Flags
= KEY_HIVE_ENTRY
| KEY_NO_DELETE
;
805 KeQuerySystemTime(&SystemTime
);
806 KeyCell
->LastWriteTime
= SystemTime
;
807 KeyCell
->Parent
= HCELL_NIL
;
808 KeyCell
->SubKeyCounts
[Stable
] = 0;
809 KeyCell
->SubKeyCounts
[Volatile
] = 0;
810 KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
811 KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
812 KeyCell
->ValueList
.Count
= 0;
813 KeyCell
->ValueList
.List
= HCELL_NIL
;
814 KeyCell
->Security
= HCELL_NIL
;
815 KeyCell
->Class
= HCELL_NIL
;
816 KeyCell
->ClassLength
= 0;
817 KeyCell
->MaxNameLen
= 0;
818 KeyCell
->MaxClassLen
= 0;
819 KeyCell
->MaxValueNameLen
= 0;
820 KeyCell
->MaxValueDataLen
= 0;
822 /* Copy the name (this will also set the length) */
823 KeyCell
->NameLength
= CmpCopyName(Hive
, (PWCHAR
)KeyCell
->Name
, &KeyName
);
825 /* Check if the name was compressed */
826 if (KeyCell
->NameLength
< KeyName
.Length
)
829 KeyCell
->Flags
|= KEY_COMP_NAME
;
833 HvReleaseCell(Hive
, *Index
);
839 CmpCreateRegistryRoot(VOID
)
841 UNICODE_STRING KeyName
;
842 OBJECT_ATTRIBUTES ObjectAttributes
;
843 PCM_KEY_BODY RootKey
;
844 HCELL_INDEX RootIndex
;
846 PCM_KEY_NODE KeyCell
;
847 PSECURITY_DESCRIPTOR SecurityDescriptor
;
848 PCM_KEY_CONTROL_BLOCK Kcb
;
851 /* Setup the root node */
852 if (!CmpCreateRootNode(&CmiVolatileHive
->Hive
, L
"REGISTRY", &RootIndex
))
858 /* Create '\Registry' key. */
859 RtlInitUnicodeString(&KeyName
, L
"\\Registry");
860 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
861 InitializeObjectAttributes(&ObjectAttributes
,
863 OBJ_CASE_INSENSITIVE
,
866 Status
= ObCreateObject(KernelMode
,
875 ExFreePool(SecurityDescriptor
);
876 if (!NT_SUCCESS(Status
)) return FALSE
;
878 /* Sanity check, and get the key cell */
879 ASSERT((&CmiVolatileHive
->Hive
)->ReleaseCellRoutine
== NULL
);
880 KeyCell
= (PCM_KEY_NODE
)HvGetCell(&CmiVolatileHive
->Hive
, RootIndex
);
881 if (!KeyCell
) return FALSE
;
884 RtlInitUnicodeString(&KeyName
, L
"Registry");
885 Kcb
= CmpCreateKeyControlBlock(&CmiVolatileHive
->Hive
,
891 if (!Kcb
) return FALSE
;
893 /* Initialize the object */
894 RootKey
->KeyControlBlock
= Kcb
;
895 RootKey
->Type
= TAG('k', 'v', '0', '2');
896 RootKey
->NotifyBlock
= NULL
;
897 RootKey
->ProcessID
= PsGetCurrentProcessId();
899 /* Insert the key into the namespace */
900 Status
= ObInsertObject(RootKey
,
905 &CmpRegistryRootHandle
);
906 if (!NT_SUCCESS(Status
)) return FALSE
;
908 /* Reference the key again so that we never lose it */
909 Status
= ObReferenceObjectByHandle(CmpRegistryRootHandle
,
915 if (!NT_SUCCESS(Status
)) return FALSE
;
917 /* Completely sucessful */
923 CmpGetRegistryPath(IN PWCHAR ConfigPath
)
925 OBJECT_ATTRIBUTES ObjectAttributes
;
928 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
;
929 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
930 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
931 ULONG BufferSize
,ResultSize
;
933 /* Check if we are booted in setup */
934 if (ExpInTextModeSetup
)
936 /* Setup the object attributes */
937 InitializeObjectAttributes(&ObjectAttributes
,
939 OBJ_CASE_INSENSITIVE
,
943 Status
= ZwOpenKey(&KeyHandle
,
946 if (!NT_SUCCESS(Status
)) return Status
;
948 /* Allocate the buffer */
949 BufferSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 4096;
950 ValueInfo
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_CM
);
955 return STATUS_INSUFFICIENT_RESOURCES
;
958 /* Query the value */
959 Status
= ZwQueryValueKey(KeyHandle
,
961 KeyValuePartialInformation
,
966 if (!NT_SUCCESS(Status
))
969 ExFreePool(ValueInfo
);
973 /* Copy the config path and null-terminate it */
974 RtlCopyMemory(ConfigPath
,
976 ValueInfo
->DataLength
);
977 ConfigPath
[ValueInfo
->DataLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
978 ExFreePool(ValueInfo
);
982 /* Just use default path */
983 wcscpy(ConfigPath
, L
"\\SystemRoot");
986 /* Add registry path */
987 wcscat(ConfigPath
, L
"\\System32\\Config\\");
990 return STATUS_SUCCESS
;
995 CmpLoadHiveThread(IN PVOID StartContext
)
997 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
998 UNICODE_STRING TempName
, FileName
, RegName
;
999 ULONG FileStart
, RegStart
, i
, ErrorResponse
, WorkerCount
, Length
;
1000 ULONG PrimaryDisposition
, SecondaryDisposition
, ClusterSize
;
1002 HANDLE PrimaryHandle
, LogHandle
;
1003 NTSTATUS Status
= STATUS_SUCCESS
;
1004 PVOID ErrorParameters
;
1007 /* Get the hive index, make sure it makes sense */
1008 i
= (ULONG
)StartContext
;
1009 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1010 DPRINT1("[HiveLoad] Parallel Thread: %d\n", i
);
1012 /* We were started */
1013 CmpMachineHiveList
[i
].ThreadStarted
= TRUE
;
1015 /* Build the file name and registry name strings */
1016 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1017 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1019 /* Now build the system root path */
1020 CmpGetRegistryPath(ConfigPath
);
1021 RtlInitUnicodeString(&TempName
, ConfigPath
);
1022 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1023 FileStart
= FileName
.Length
;
1025 /* And build the registry root path */
1026 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1027 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1028 RegStart
= RegName
.Length
;
1030 /* Build the base name */
1031 RegName
.Length
= RegStart
;
1032 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1033 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1035 /* Check if this is a child of the root */
1036 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1038 /* Then setup the whole name */
1039 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1040 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1043 /* Now Add tge rest if the file name */
1044 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1045 FileName
.Length
= FileStart
;
1046 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1047 if (!CmpMachineHiveList
[i
].CmHive
)
1049 /* We need to allocate a new hive structure */
1050 CmpMachineHiveList
[i
].Allocate
= TRUE
;
1052 /* Load the hive file */
1053 DPRINT1("[HiveLoad]: Load from file %wZ\n", &FileName
);
1054 Status
= CmpInitHiveFromFile(&FileName
,
1055 CmpMachineHiveList
[i
].HHiveFlags
,
1057 &CmpMachineHiveList
[i
].Allocate
,
1059 if (!(NT_SUCCESS(Status
)) || !(CmHive
->FileHandles
[HFILE_TYPE_LOG
]))
1061 /* We failed or couldn't get a log file, raise a hard error */
1062 ErrorParameters
= &FileName
;
1063 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1066 (PULONG_PTR
)&ErrorParameters
,
1071 /* Set the hive flags and newly allocated hive pointer */
1072 CmHive
->Flags
= CmpMachineHiveList
[i
].CmHiveFlags
;
1073 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1077 CmHive
= CmpMachineHiveList
[i
].CmHive
;
1078 /* We already have a hive, is it volatile? */
1079 if (!(CmHive
->Hive
.HiveFlags
& HIVE_VOLATILE
))
1081 DPRINT1("[HiveLoad]: Open from file %wZ\n", &FileName
);
1083 /* It's now, open the hive file and log */
1084 Status
= CmpOpenHiveFiles(&FileName
,
1088 &PrimaryDisposition
,
1089 &SecondaryDisposition
,
1094 if (!(NT_SUCCESS(Status
)) || !(LogHandle
))
1096 /* Couldn't open the hive or its log file, raise a hard error */
1097 ErrorParameters
= &FileName
;
1098 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1101 (PULONG_PTR
)&ErrorParameters
,
1105 /* And bugcheck for posterity's sake */
1106 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 0, i
, Status
);
1109 /* Save the file handles. This should remove our sync hacks */
1110 CmHive
->FileHandles
[HFILE_TYPE_LOG
] = LogHandle
;
1111 CmHive
->FileHandles
[HFILE_TYPE_PRIMARY
] = PrimaryHandle
;
1113 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1114 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1115 CmHive
->Hive
.HiveFlags
&= ~HIVE_NOLAZYFLUSH
;
1117 /* Get the real size of the hive */
1118 Length
= CmHive
->Hive
.Storage
[Stable
].Length
+ HBLOCK_SIZE
;
1120 /* Check if the cluster size doesn't match */
1121 if (CmHive
->Hive
.Cluster
!= ClusterSize
) ASSERT(FALSE
);
1123 /* Set the file size */
1124 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1126 /* This shouldn't fail */
1130 /* Another thing we don't support is NTLDR-recovery */
1131 if (CmHive
->Hive
.BaseBlock
->BootRecover
) ASSERT(FALSE
);
1133 /* Finally, set our allocated hive to the same hive we've had */
1134 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1135 ASSERT(CmpMachineHiveList
[i
].CmHive
== CmpMachineHiveList
[i
].CmHive2
);
1140 CmpMachineHiveList
[i
].ThreadFinished
= TRUE
;
1142 /* Check if we're the last worker */
1143 WorkerCount
= InterlockedIncrement(&CmpLoadWorkerIncrement
);
1144 if (WorkerCount
== CM_NUMBER_OF_MACHINE_HIVES
)
1146 /* Signal the event */
1147 KeSetEvent(&CmpLoadWorkerEvent
, 0, FALSE
);
1150 /* Kill the thread */
1151 PsTerminateSystemThread(Status
);
1156 CmpInitializeHiveList(IN USHORT Flag
)
1158 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1159 UNICODE_STRING TempName
, FileName
, RegName
;
1162 ULONG FileStart
, RegStart
, i
;
1163 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1166 /* Allow writing for now */
1169 /* Build the file name and registry name strings */
1170 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1171 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1173 /* Now build the system root path */
1174 CmpGetRegistryPath(ConfigPath
);
1175 RtlInitUnicodeString(&TempName
, ConfigPath
);
1176 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1177 FileStart
= FileName
.Length
;
1179 /* And build the registry root path */
1180 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1181 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1182 RegStart
= RegName
.Length
;
1184 /* Setup the event to synchronize workers */
1185 KeInitializeEvent(&CmpLoadWorkerEvent
, SynchronizationEvent
, FALSE
);
1187 /* Enter special boot condition */
1188 CmpSpecialBootCondition
= TRUE
;
1190 /* Create the SD for the root hives */
1191 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1193 /* Loop every hive we care about */
1194 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1196 /* Make sure the list is setup */
1197 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1199 /* Create a thread to handle this hive */
1200 Status
= PsCreateSystemThread(&Thread
,
1207 if (NT_SUCCESS(Status
))
1209 /* We don't care about the handle -- the thread self-terminates */
1214 /* Can't imagine this happening */
1215 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 3, i
, Status
);
1219 /* Make sure we've reached the end of the list */
1220 ASSERT(CmpMachineHiveList
[i
].Name
== NULL
);
1222 /* Wait for hive loading to finish */
1223 KeWaitForSingleObject(&CmpLoadWorkerEvent
,
1229 /* Exit the special boot condition and make sure all workers completed */
1230 CmpSpecialBootCondition
= FALSE
;
1231 ASSERT(CmpLoadWorkerIncrement
== CM_NUMBER_OF_MACHINE_HIVES
);
1233 /* Loop hives again */
1234 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1236 /* Make sure the thread ran and finished */
1237 ASSERT(CmpMachineHiveList
[i
].ThreadFinished
== TRUE
);
1238 ASSERT(CmpMachineHiveList
[i
].ThreadStarted
== TRUE
);
1240 /* Check if this was a new hive */
1241 if (!CmpMachineHiveList
[i
].CmHive
)
1243 /* Make sure we allocated something */
1244 ASSERT(CmpMachineHiveList
[i
].CmHive2
!= NULL
);
1246 /* Build the base name */
1247 RegName
.Length
= RegStart
;
1248 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1249 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1251 /* Check if this is a child of the root */
1252 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1254 /* Then setup the whole name */
1255 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1256 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1259 /* Now link the hive to its master */
1260 DPRINT1("[HiveLoad]: Link %wZ\n", &RegName
);
1261 Status
= CmpLinkHiveToMaster(&RegName
,
1263 CmpMachineHiveList
[i
].CmHive2
,
1264 CmpMachineHiveList
[i
].Allocate
,
1265 SecurityDescriptor
);
1266 if (Status
!= STATUS_SUCCESS
)
1268 /* Linking needs to work */
1269 KeBugCheckEx(CONFIG_LIST_FAILED
, 11, Status
, i
, (ULONG_PTR
)&RegName
);
1272 /* Check if we had to allocate a new hive */
1273 if (CmpMachineHiveList
[i
].Allocate
)
1275 /* Sync the new hive */
1276 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1280 /* Check if we created a new hive */
1281 if (CmpMachineHiveList
[i
].CmHive2
)
1283 /* TODO: Add to HiveList key */
1287 /* Get rid of the SD */
1288 ExFreePool(SecurityDescriptor
);
1290 /* FIXME: Link SECURITY to SAM */
1292 /* FIXME: Link S-1-5-18 to .Default */
1299 OBJECT_ATTRIBUTES ObjectAttributes
;
1300 UNICODE_STRING KeyName
;
1303 PCMHIVE HardwareHive
;
1306 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1309 /* Check if this is PE-boot */
1310 if (InitIsWinPEMode
)
1312 /* Set registry to PE mode */
1313 CmpMiniNTBoot
= TRUE
;
1314 CmpShareSystemHives
= TRUE
;
1317 /* Initialize the hive list and lock */
1318 InitializeListHead(&CmpHiveListHead
);
1319 ExInitializePushLock((PVOID
)&CmpHiveListHeadLock
);
1320 ExInitializePushLock((PVOID
)&CmpLoadHiveLock
);
1322 /* Initialize registry lock */
1323 ExInitializeResourceLite(&CmpRegistryLock
);
1325 /* Initialize the cache */
1326 CmpInitializeCache();
1328 /* Initialize allocation and delayed dereferencing */
1329 CmpInitCmPrivateAlloc();
1330 CmpInitCmPrivateDelayAlloc();
1331 CmpInitDelayDerefKCBEngine();
1333 /* Initialize callbacks */
1336 /* Initialize self healing */
1337 KeInitializeGuardedMutex(&CmpSelfHealQueueLock
);
1338 InitializeListHead(&CmpSelfHealQueueListHead
);
1340 /* Save the current process and lock the registry */
1341 CmpSystemProcess
= PsGetCurrentProcess();
1343 /* Create the key object types */
1344 Status
= CmpCreateObjectTypes();
1345 if (!NT_SUCCESS(Status
))
1348 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 1, Status
, 0);
1351 /* Build the master hive */
1352 Status
= CmpInitializeHive((PCMHIVE
*)&CmiVolatileHive
,
1362 if (!NT_SUCCESS(Status
))
1365 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 2, Status
, 0);
1368 /* Create the \REGISTRY key node */
1369 if (!CmpCreateRegistryRoot())
1372 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 3, 0, 0);
1375 /* Create the default security descriptor */
1376 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1378 /* Create '\Registry\Machine' key. */
1379 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE");
1380 InitializeObjectAttributes(&ObjectAttributes
,
1382 OBJ_CASE_INSENSITIVE
,
1384 SecurityDescriptor
);
1385 Status
= NtCreateKey(&KeyHandle
,
1386 KEY_READ
| KEY_WRITE
,
1392 if (!NT_SUCCESS(Status
))
1395 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 5, Status
, 0);
1398 /* Close the handle */
1401 /* Create '\Registry\User' key. */
1402 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\USER");
1403 InitializeObjectAttributes(&ObjectAttributes
,
1405 OBJ_CASE_INSENSITIVE
,
1407 SecurityDescriptor
);
1408 Status
= NtCreateKey(&KeyHandle
,
1409 KEY_READ
| KEY_WRITE
,
1415 if (!NT_SUCCESS(Status
))
1418 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 6, Status
, 0);
1421 /* Close the handle */
1424 /* Initialize the system hive */
1425 if (!CmpInitializeSystemHive(KeLoaderBlock
))
1428 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 7, 0, 0);
1431 /* Create the 'CurrentControlSet' link. */
1432 Status
= CmpCreateControlSet(KeLoaderBlock
);
1433 if (!NT_SUCCESS(Status
))
1436 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 8, Status
, 0);
1439 /* Import the hardware hive (FIXME: We should create it from scratch) */
1440 BaseAddress
= CmpRosGetHardwareHive(&Length
);
1441 ((PHBASE_BLOCK
)BaseAddress
)->Length
= Length
;
1442 Status
= CmpInitializeHive((PCMHIVE
*)&HardwareHive
,
1443 HINIT_MEMORY
, //HINIT_CREATE,
1444 HIVE_VOLATILE
| HIVE_NOLAZYFLUSH
,
1446 BaseAddress
, // NULL,
1452 CmPrepareHive(&HardwareHive
->Hive
);
1453 if (!NT_SUCCESS(Status
))
1456 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 11, Status
, 0);
1459 /* Add the hive to the hive list */
1460 CmpMachineHiveList
[0].CmHive
= (PCMHIVE
)HardwareHive
;
1462 /* Attach it to the machine key */
1463 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE");
1464 Status
= CmpLinkHiveToMaster(&KeyName
,
1466 (PCMHIVE
)HardwareHive
,
1468 SecurityDescriptor
);
1469 if (!NT_SUCCESS(Status
))
1472 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 12, Status
, 0);
1475 /* FIXME: Add to HiveList key */
1477 /* Free the security descriptor */
1478 ExFreePool(SecurityDescriptor
);
1480 /* Fill out the Hardware key with the ARC Data from the Loader */
1481 Status
= CmpInitializeHardwareConfiguration(KeLoaderBlock
);
1482 if (!NT_SUCCESS(Status
))
1485 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 13, Status
, 0);
1488 /* Initialize machine-dependent information into the registry */
1489 Status
= CmpInitializeMachineDependentConfiguration(KeLoaderBlock
);
1490 if (!NT_SUCCESS(Status
))
1493 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 14, Status
, 0);
1496 /* Initialize volatile registry settings */
1497 Status
= CmpSetSystemValues(KeLoaderBlock
);
1498 if (!NT_SUCCESS(Status
))
1501 KEBUGCHECKEX(CONFIG_INITIALIZATION_FAILED
, 1, 15, Status
, 0);
1504 /* Free the load options */
1505 ExFreePool(CmpLoadOptions
.Buffer
);
1507 /* If we got here, all went well */
1513 CmpLockRegistryExclusive(VOID
)
1515 /* Enter a critical region and lock the registry */
1516 KeEnterCriticalRegion();
1517 ExAcquireResourceExclusiveLite(&CmpRegistryLock
, TRUE
);
1520 ASSERT(CmpFlushStarveWriters
== 0);
1521 RtlGetCallersAddress(&CmpRegistryLockCaller
, &CmpRegistryLockCallerCaller
);
1526 CmpLockRegistry(VOID
)
1528 /* Enter a critical region */
1529 KeEnterCriticalRegion();
1531 /* Check if we have to starve writers */
1532 if (CmpFlushStarveWriters
)
1534 /* Starve exlusive waiters */
1535 ExAcquireSharedStarveExclusive(&CmpRegistryLock
, TRUE
);
1539 /* Just grab the lock */
1540 ExAcquireResourceSharedLite(&CmpRegistryLock
, TRUE
);
1546 CmpTestRegistryLock(VOID
)
1549 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1554 CmpTestRegistryLockExclusive(VOID
)
1557 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1562 CmpUnlockRegistry(VOID
)
1565 CMP_ASSERT_REGISTRY_LOCK();
1567 /* Check if we should flush the registry */
1568 if (CmpFlushOnLockRelease
)
1570 /* The registry should be exclusively locked for this */
1571 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1573 /* Flush the registry */
1574 CmpDoFlushAll(TRUE
);
1575 CmpFlushOnLockRelease
= FALSE
;
1578 /* Release the lock and leave the critical region */
1579 ExReleaseResourceLite(&CmpRegistryLock
);
1580 KeLeaveCriticalRegion();
1585 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1
,
1588 ULONG Index1
, Index2
;
1591 CMP_ASSERT_REGISTRY_LOCK();
1593 /* Get hash indexes */
1594 Index1
= GET_HASH_INDEX(ConvKey1
);
1595 Index2
= GET_HASH_INDEX(ConvKey2
);
1597 /* See which one is highest */
1598 if (Index1
< Index2
)
1600 /* Grab them in the proper order */
1601 CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1602 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1606 /* Grab the second one first, then the first */
1607 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1608 if (Index1
!= Index2
) CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1614 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1
,
1617 ULONG Index1
, Index2
;
1620 CMP_ASSERT_REGISTRY_LOCK();
1622 /* Get hash indexes */
1623 Index1
= GET_HASH_INDEX(ConvKey1
);
1624 Index2
= GET_HASH_INDEX(ConvKey2
);
1625 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey2
).Owner
== KeGetCurrentThread()) ||
1626 (CmpTestRegistryLockExclusive()));
1628 /* See which one is highest */
1629 if (Index1
< Index2
)
1631 /* Grab them in the proper order */
1632 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1633 (CmpTestRegistryLockExclusive()));
1634 CmpReleaseKcbLockByKey(ConvKey2
);
1635 CmpReleaseKcbLockByKey(ConvKey1
);
1639 /* Release the first one first, then the second */
1640 if (Index1
!= Index2
)
1642 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1643 (CmpTestRegistryLockExclusive()));
1644 CmpReleaseKcbLockByKey(ConvKey1
);
1646 CmpReleaseKcbLockByKey(ConvKey2
);
1652 CmShutdownSystem(VOID
)
1654 /* Kill the workers and fush all hives */
1655 CmpShutdownWorkers();
1656 CmpDoFlushAll(TRUE
);