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 ******************************************************************/
15 POBJECT_TYPE CmpKeyObjectType
;
16 PCMHIVE CmiVolatileHive
;
17 LIST_ENTRY CmpHiveListHead
;
18 ERESOURCE CmpRegistryLock
;
19 KGUARDED_MUTEX CmpSelfHealQueueLock
;
20 LIST_ENTRY CmpSelfHealQueueListHead
;
21 KEVENT CmpLoadWorkerEvent
;
22 LONG CmpLoadWorkerIncrement
;
23 PEPROCESS CmpSystemProcess
;
24 BOOLEAN HvShutdownComplete
;
25 PVOID CmpRegistryLockCallerCaller
, CmpRegistryLockCaller
;
26 BOOLEAN CmpFlushStarveWriters
;
27 BOOLEAN CmpFlushOnLockRelease
;
28 BOOLEAN CmpSpecialBootCondition
;
30 BOOLEAN CmpForceForceFlush
;
31 BOOLEAN CmpWasSetupBoot
;
33 extern BOOLEAN CmFirstTime
;
35 /* FUNCTIONS *****************************************************************/
39 CmpDeleteKeyObject(PVOID DeletedObject
)
41 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)DeletedObject
;
42 PCM_KEY_CONTROL_BLOCK Kcb
;
43 REG_KEY_HANDLE_CLOSE_INFORMATION KeyHandleCloseInfo
;
44 REG_POST_OPERATION_INFORMATION PostOperationInfo
;
48 /* First off, prepare the handle close information callback */
49 PostOperationInfo
.Object
= KeyBody
;
50 KeyHandleCloseInfo
.Object
= KeyBody
;
51 Status
= CmiCallRegisteredCallbacks(RegNtPreKeyHandleClose
,
53 if (!NT_SUCCESS(Status
))
55 /* If we failed, notify the post routine */
56 PostOperationInfo
.Status
= Status
;
57 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
61 /* Acquire hive lock */
64 /* Make sure this is a valid key body */
65 if (KeyBody
->Type
== TAG('k', 'y', '0', '2'))
68 Kcb
= KeyBody
->KeyControlBlock
;
72 DelistKeyBodyFromKCB(KeyBody
, FALSE
);
75 /* Dereference the KCB */
76 CmpDelayDerefKeyControlBlock(Kcb
);
80 /* Release the registry lock */
83 /* Do the post callback */
84 PostOperationInfo
.Status
= STATUS_SUCCESS
;
85 CmiCallRegisteredCallbacks(RegNtPostKeyHandleClose
, &PostOperationInfo
);
90 CmpCloseKeyObject(IN PEPROCESS Process OPTIONAL
,
92 IN ACCESS_MASK GrantedAccess
,
93 IN ULONG ProcessHandleCount
,
94 IN ULONG SystemHandleCount
)
96 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)Object
;
99 /* Don't do anything if we're not the last handle */
100 if (SystemHandleCount
> 1) return;
102 /* Make sure we're a valid key body */
103 if (KeyBody
->Type
== TAG('k', 'y', '0', '2'))
105 /* Don't do anything if we don't have a notify block */
106 if (!KeyBody
->NotifyBlock
) return;
108 /* This shouldn't happen yet */
115 CmpQueryKeyName(IN PVOID ObjectBody
,
117 IN OUT POBJECT_NAME_INFORMATION ObjectNameInfo
,
119 OUT PULONG ReturnLength
,
120 IN KPROCESSOR_MODE PreviousMode
)
122 PUNICODE_STRING KeyName
;
123 NTSTATUS Status
= STATUS_SUCCESS
;
124 PCM_KEY_BODY KeyBody
= (PCM_KEY_BODY
)ObjectBody
;
125 PCM_KEY_CONTROL_BLOCK Kcb
= KeyBody
->KeyControlBlock
;
127 /* Acquire hive lock */
130 /* Lock KCB shared */
131 CmpAcquireKcbLockShared(Kcb
);
133 /* Check if it's a deleted block */
136 /* Release the locks */
137 CmpReleaseKcbLock(Kcb
);
140 /* Let the caller know it's deleted */
141 return STATUS_KEY_DELETED
;
145 KeyName
= CmpConstructName(Kcb
);
147 /* Release the locks */
148 CmpReleaseKcbLock(Kcb
);
151 /* Check if we got the name */
152 if (!KeyName
) return STATUS_INSUFFICIENT_RESOURCES
;
154 /* Set the returned length */
155 *ReturnLength
= KeyName
->Length
+ sizeof(OBJECT_NAME_INFORMATION
) + sizeof(WCHAR
);
157 /* Check if it fits into the provided buffer */
158 if ((Length
< sizeof(OBJECT_NAME_INFORMATION
)) ||
159 (Length
< (*ReturnLength
- sizeof(OBJECT_NAME_INFORMATION
))))
161 /* Free the buffer allocated by CmpConstructName */
162 ExFreePool(KeyName
->Buffer
);
164 /* Return buffer length failure */
165 return STATUS_INFO_LENGTH_MISMATCH
;
168 /* Fill in the result */
171 /* Return data to user */
172 ObjectNameInfo
->Name
.Buffer
= (PWCHAR
)(ObjectNameInfo
+ 1);
173 ObjectNameInfo
->Name
.MaximumLength
= KeyName
->Length
;
174 ObjectNameInfo
->Name
.Length
= KeyName
->Length
;
176 /* Copy string content*/
177 RtlCopyMemory(ObjectNameInfo
->Name
.Buffer
, KeyName
->Buffer
, *ReturnLength
);
182 Status
= _SEH_GetExceptionCode();
186 /* Free the buffer allocated by CmpConstructName */
194 CmpInitHiveFromFile(IN PCUNICODE_STRING HiveName
,
200 ULONG HiveDisposition
, LogDisposition
;
201 HANDLE FileHandle
= NULL
, LogHandle
= NULL
;
203 ULONG Operation
, FileType
;
210 /* Open or create the hive files */
211 Status
= CmpOpenHiveFiles(HiveName
,
221 if (!NT_SUCCESS(Status
)) return Status
;
223 /* Check if we have a log handle */
224 FileType
= (LogHandle
) ? HFILE_TYPE_LOG
: HFILE_TYPE_PRIMARY
;
226 /* Check if we created or opened the hive */
227 if (HiveDisposition
== FILE_CREATED
)
229 /* Do a create operation */
230 Operation
= HINIT_CREATE
;
235 /* Open it as a file */
236 Operation
= HINIT_FILE
;
240 /* Check if we're sharing hives */
241 if (CmpShareSystemHives
)
243 /* Then force using the primary hive */
244 FileType
= HFILE_TYPE_PRIMARY
;
247 /* Get rid of the log handle */
253 /* Check if we're too late */
254 if (HvShutdownComplete
)
258 if (LogHandle
) ZwClose(LogHandle
);
259 return STATUS_TOO_LATE
;
262 /* Initialize the hive */
263 Status
= CmpInitializeHive((PCMHIVE
*)&NewHive
,
273 if (!NT_SUCCESS(Status
))
277 if (LogHandle
) ZwClose(LogHandle
);
281 /* Success, return hive */
284 /* ROS: Init root key cell and prepare the hive */
285 if (Operation
== HINIT_CREATE
) CmCreateRootNode(&NewHive
->Hive
, L
"");
287 /* Duplicate the hive name */
288 NewHive
->FileFullPath
.Buffer
= ExAllocatePoolWithTag(PagedPool
,
291 if (NewHive
->FileFullPath
.Buffer
)
293 /* Copy the string */
294 RtlCopyMemory(NewHive
->FileFullPath
.Buffer
,
297 NewHive
->FileFullPath
.Length
= HiveName
->Length
;
298 NewHive
->FileFullPath
.MaximumLength
= HiveName
->MaximumLength
;
302 return STATUS_SUCCESS
;
307 CmpSetSystemValues(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
309 OBJECT_ATTRIBUTES ObjectAttributes
;
310 UNICODE_STRING KeyName
, ValueName
= {0};
313 ASSERT(LoaderBlock
!= NULL
);
315 /* Setup attributes for loader options */
316 RtlInitUnicodeString(&KeyName
,
317 L
"\\REGISTRY\\MACHINE\\SYSTEM\\CurrentControlSet\\"
319 InitializeObjectAttributes(&ObjectAttributes
,
321 OBJ_CASE_INSENSITIVE
,
324 Status
= NtOpenKey(&KeyHandle
, KEY_WRITE
, &ObjectAttributes
);
325 if (!NT_SUCCESS(Status
)) goto Quickie
;
327 /* Key opened, now write to the key */
328 RtlInitUnicodeString(&KeyName
, L
"SystemStartOptions");
329 Status
= NtSetValueKey(KeyHandle
,
333 CmpLoadOptions
.Buffer
,
334 CmpLoadOptions
.Length
);
335 if (!NT_SUCCESS(Status
)) goto Quickie
;
337 /* Setup value name for system boot device */
338 RtlInitUnicodeString(&KeyName
, L
"SystemBootDevice");
339 RtlCreateUnicodeStringFromAsciiz(&ValueName
, LoaderBlock
->NtBootPathName
);
340 Status
= NtSetValueKey(KeyHandle
,
348 /* Free the buffers */
349 RtlFreeUnicodeString(&ValueName
);
351 /* Close the key and return */
354 /* Return the status */
355 return (ExpInTextModeSetup
? STATUS_SUCCESS
: Status
);
360 CmpCreateControlSet(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
362 UNICODE_STRING ConfigName
= RTL_CONSTANT_STRING(L
"Control\\IDConfigDB");
363 UNICODE_STRING SelectName
=
364 RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\System\\Select");
365 UNICODE_STRING KeyName
;
366 OBJECT_ATTRIBUTES ObjectAttributes
;
367 CHAR ValueInfoBuffer
[128];
368 PKEY_VALUE_FULL_INFORMATION ValueInfo
;
370 WCHAR UnicodeBuffer
[128];
371 HANDLE SelectHandle
, KeyHandle
, ConfigHandle
= NULL
, ProfileHandle
= NULL
;
372 HANDLE ParentHandle
= NULL
;
373 ULONG ControlSet
, HwProfile
;
374 ANSI_STRING TempString
;
376 ULONG ResultLength
, Disposition
;
377 PLOADER_PARAMETER_EXTENSION LoaderExtension
;
380 /* Open the select key */
381 InitializeObjectAttributes(&ObjectAttributes
,
383 OBJ_CASE_INSENSITIVE
,
386 Status
= NtOpenKey(&SelectHandle
, KEY_READ
, &ObjectAttributes
);
387 if (!NT_SUCCESS(Status
))
389 /* ReactOS Hack: Hard-code current to 001 for SetupLdr */
390 if (!LoaderBlock
->RegistryBase
)
392 /* Build the ControlSet001 key */
393 RtlInitUnicodeString(&KeyName
,
394 L
"\\Registry\\Machine\\System\\ControlSet001");
395 InitializeObjectAttributes(&ObjectAttributes
,
397 OBJ_CASE_INSENSITIVE
,
400 Status
= NtCreateKey(&KeyHandle
,
407 if (!NT_SUCCESS(Status
)) return Status
;
409 /* Don't need the handle */
412 /* Use hard-coded setting */
417 /* Fail for real boots */
421 /* Open the current value */
422 RtlInitUnicodeString(&KeyName
, L
"Current");
423 Status
= NtQueryValueKey(SelectHandle
,
425 KeyValueFullInformation
,
427 sizeof(ValueInfoBuffer
),
429 NtClose(SelectHandle
);
430 if (!NT_SUCCESS(Status
)) return Status
;
432 /* Get the actual value pointer, and get the control set ID */
433 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
434 ControlSet
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
436 /* Create the current control set key */
438 RtlInitUnicodeString(&KeyName
,
439 L
"\\Registry\\Machine\\System\\CurrentControlSet");
440 InitializeObjectAttributes(&ObjectAttributes
,
442 OBJ_CASE_INSENSITIVE
,
445 Status
= NtCreateKey(&KeyHandle
,
450 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
452 if (!NT_SUCCESS(Status
)) return Status
;
455 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
457 /* Initialize the symbolic link name */
459 "\\Registry\\Machine\\System\\ControlSet%03ld",
461 RtlInitAnsiString(&TempString
, Buffer
);
463 /* Create a Unicode string out of it */
464 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
465 KeyName
.Buffer
= UnicodeBuffer
;
466 Status
= RtlAnsiStringToUnicodeString(&KeyName
, &TempString
, FALSE
);
469 Status
= NtSetValueKey(KeyHandle
,
470 &CmSymbolicLinkValueName
,
475 if (!NT_SUCCESS(Status
)) return Status
;
477 /* Get the configuration database key */
478 InitializeObjectAttributes(&ObjectAttributes
,
480 OBJ_CASE_INSENSITIVE
,
483 Status
= NtOpenKey(&ConfigHandle
, KEY_READ
, &ObjectAttributes
);
486 /* Check if we don't have one */
487 if (!NT_SUCCESS(Status
))
489 /* Cleanup and exit */
494 /* Now get the current config */
495 RtlInitUnicodeString(&KeyName
, L
"CurrentConfig");
496 Status
= NtQueryValueKey(ConfigHandle
,
498 KeyValueFullInformation
,
500 sizeof(ValueInfoBuffer
),
503 /* Set pointer to buffer */
504 ValueInfo
= (PKEY_VALUE_FULL_INFORMATION
)ValueInfoBuffer
;
506 /* Check if we failed or got a non DWORD-value */
507 if (!(NT_SUCCESS(Status
)) || (ValueInfo
->Type
!= REG_DWORD
)) goto Cleanup
;
509 /* Get the hadware profile */
510 HwProfile
= *(PULONG
)((PUCHAR
)ValueInfo
+ ValueInfo
->DataOffset
);
512 /* Open the hardware profile key */
513 RtlInitUnicodeString(&KeyName
,
514 L
"\\Registry\\Machine\\System\\CurrentControlSet"
515 L
"\\Hardware Profiles");
516 InitializeObjectAttributes(&ObjectAttributes
,
518 OBJ_CASE_INSENSITIVE
,
521 Status
= NtOpenKey(&ParentHandle
, KEY_READ
, &ObjectAttributes
);
522 if (!NT_SUCCESS(Status
))
524 /* Exit and clean up */
529 /* Build the profile name */
530 sprintf(Buffer
, "%04ld", HwProfile
);
531 RtlInitAnsiString(&TempString
, Buffer
);
533 /* Convert it to Unicode */
534 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
535 KeyName
.Buffer
= UnicodeBuffer
;
536 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
539 ASSERT(Status
== STATUS_SUCCESS
);
541 /* Open the associated key */
542 InitializeObjectAttributes(&ObjectAttributes
,
544 OBJ_CASE_INSENSITIVE
,
547 Status
= NtOpenKey(&ProfileHandle
,
548 KEY_READ
| KEY_WRITE
,
550 if (!NT_SUCCESS (Status
))
552 /* Cleanup and exit */
557 /* Check if we have a loader block extension */
558 LoaderExtension
= LoaderBlock
->Extension
;
561 ASSERTMSG("ReactOS doesn't support NTLDR Profiles yet!\n", FALSE
);
564 /* Create the current hardware profile key */
565 RtlInitUnicodeString(&KeyName
,
566 L
"\\Registry\\Machine\\System\\CurrentControlSet\\"
567 L
"Hardware Profiles\\Current");
568 InitializeObjectAttributes(&ObjectAttributes
,
570 OBJ_CASE_INSENSITIVE
,
573 Status
= NtCreateKey(&KeyHandle
,
578 REG_OPTION_VOLATILE
| REG_OPTION_CREATE_LINK
,
580 if (NT_SUCCESS(Status
))
583 ASSERT(Disposition
== REG_CREATED_NEW_KEY
);
585 /* Create the profile name */
587 "\\Registry\\Machine\\System\\CurrentControlSet\\"
588 "Hardware Profiles\\%04ld",
590 RtlInitAnsiString(&TempString
, Buffer
);
592 /* Convert it to Unicode */
593 KeyName
.MaximumLength
= sizeof(UnicodeBuffer
);
594 KeyName
.Buffer
= UnicodeBuffer
;
595 Status
= RtlAnsiStringToUnicodeString(&KeyName
,
598 ASSERT(STATUS_SUCCESS
== Status
);
601 Status
= NtSetValueKey(KeyHandle
,
602 &CmSymbolicLinkValueName
,
610 /* Close every opened handle */
612 if (ConfigHandle
) NtClose(ConfigHandle
);
613 if (ProfileHandle
) NtClose(ProfileHandle
);
614 if (ParentHandle
) NtClose(ParentHandle
);
617 return STATUS_SUCCESS
;
622 CmpLinkHiveToMaster(IN PUNICODE_STRING LinkName
,
623 IN HANDLE RootDirectory
,
624 IN PCMHIVE RegistryHive
,
626 IN PSECURITY_DESCRIPTOR SecurityDescriptor
)
628 OBJECT_ATTRIBUTES ObjectAttributes
;
630 CM_PARSE_CONTEXT ParseContext
= {0};
632 PCM_KEY_BODY KeyBody
;
635 /* Setup the object attributes */
636 InitializeObjectAttributes(&ObjectAttributes
,
638 OBJ_CASE_INSENSITIVE
| OBJ_KERNEL_HANDLE
,
642 /* Setup the parse context */
643 ParseContext
.CreateLink
= TRUE
;
644 ParseContext
.CreateOperation
= TRUE
;
645 ParseContext
.ChildHive
.KeyHive
= &RegistryHive
->Hive
;
647 /* Check if we have a root keycell or if we need to create it */
651 ParseContext
.ChildHive
.KeyCell
= HCELL_NIL
;
656 ParseContext
.ChildHive
.KeyCell
= RegistryHive
->Hive
.BaseBlock
->RootCell
;
659 /* Create the link node */
660 Status
= ObOpenObjectByName(&ObjectAttributes
,
664 KEY_READ
| KEY_WRITE
,
665 (PVOID
)&ParseContext
,
667 if (!NT_SUCCESS(Status
)) return Status
;
669 /* Mark the hive as clean */
670 RegistryHive
->Hive
.DirtyFlag
= FALSE
;
672 /* ReactOS Hack: Keep alive */
673 Status
= ObReferenceObjectByHandle(KeyHandle
,
679 ASSERT(NT_SUCCESS(Status
));
681 /* Close the extra handle */
683 return STATUS_SUCCESS
;
688 CmpInitializeSystemHive(IN PLOADER_PARAMETER_BLOCK LoaderBlock
)
691 ANSI_STRING LoadString
;
696 UNICODE_STRING KeyName
;
697 PCMHIVE SystemHive
= NULL
;
698 UNICODE_STRING HiveName
= RTL_CONSTANT_STRING(L
"SYSTEM");
699 PSECURITY_DESCRIPTOR SecurityDescriptor
;
702 /* Setup the ansi string */
703 RtlInitAnsiString(&LoadString
, LoaderBlock
->LoadOptions
);
705 /* Allocate the unicode buffer */
706 Length
= LoadString
.Length
* sizeof(WCHAR
) + sizeof(UNICODE_NULL
);
707 Buffer
= ExAllocatePoolWithTag(PagedPool
, Length
, TAG_CM
);
711 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 3, 1, (ULONG_PTR
)LoaderBlock
, 0);
714 /* Setup the unicode string */
715 RtlInitEmptyUnicodeString(&CmpLoadOptions
, Buffer
, (USHORT
)Length
);
717 /* Add the load options and null-terminate */
718 RtlAnsiStringToUnicodeString(&CmpLoadOptions
, &LoadString
, FALSE
);
719 CmpLoadOptions
.Buffer
[LoadString
.Length
] = UNICODE_NULL
;
720 CmpLoadOptions
.Length
+= sizeof(WCHAR
);
722 /* Get the System Hive base address */
723 HiveBase
= LoaderBlock
->RegistryBase
;
727 ((PHBASE_BLOCK
)HiveBase
)->Length
= LoaderBlock
->RegistryLength
;
728 Status
= CmpInitializeHive((PCMHIVE
*)&SystemHive
,
738 if (!NT_SUCCESS(Status
)) return FALSE
;
740 /* Set the hive filename */
741 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
742 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
744 /* We imported, no need to create a new hive */
747 /* Manually set the hive as volatile, if in Live CD mode */
748 if (CmpShareSystemHives
) SystemHive
->Hive
.HiveFlags
= HIVE_VOLATILE
;
753 Status
= CmpInitializeHive(&SystemHive
,
763 if (!NT_SUCCESS(Status
)) return FALSE
;
765 /* Set the hive filename */
766 RtlCreateUnicodeString(&SystemHive
->FileFullPath
,
767 L
"\\SystemRoot\\System32\\Config\\SYSTEM");
769 /* Tell CmpLinkHiveToMaster to allocate a hive */
773 /* Save the boot type */
774 if (SystemHive
) CmpBootType
= SystemHive
->Hive
.BaseBlock
->BootType
;
776 /* Are we in self-healing mode? */
779 /* Disable self-healing internally and check if boot type wanted it */
783 /* We're disabled, so bugcheck */
784 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
,
787 (ULONG_PTR
)SystemHive
,
792 /* Create the default security descriptor */
793 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
795 /* Attach it to the system key */
796 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\SYSTEM");
797 Status
= CmpLinkHiveToMaster(&KeyName
,
803 /* Free the security descriptor */
804 ExFreePool(SecurityDescriptor
);
805 if (!NT_SUCCESS(Status
)) return FALSE
;
807 /* Add the hive to the hive list */
808 CmpMachineHiveList
[3].CmHive
= (PCMHIVE
)SystemHive
;
816 CmpCreateObjectTypes(VOID
)
818 OBJECT_TYPE_INITIALIZER ObjectTypeInitializer
;
820 GENERIC_MAPPING CmpKeyMapping
= {KEY_READ
,
826 /* Initialize the Key object type */
827 RtlZeroMemory(&ObjectTypeInitializer
, sizeof(ObjectTypeInitializer
));
828 RtlInitUnicodeString(&Name
, L
"Key");
829 ObjectTypeInitializer
.Length
= sizeof(ObjectTypeInitializer
);
830 ObjectTypeInitializer
.DefaultPagedPoolCharge
= sizeof(CM_KEY_BODY
);
831 ObjectTypeInitializer
.GenericMapping
= CmpKeyMapping
;
832 ObjectTypeInitializer
.PoolType
= PagedPool
;
833 ObjectTypeInitializer
.ValidAccessMask
= KEY_ALL_ACCESS
;
834 ObjectTypeInitializer
.UseDefaultObject
= TRUE
;
835 ObjectTypeInitializer
.DeleteProcedure
= CmpDeleteKeyObject
;
836 ObjectTypeInitializer
.ParseProcedure
= CmpParseKey
;
837 ObjectTypeInitializer
.SecurityProcedure
= CmpSecurityMethod
;
838 ObjectTypeInitializer
.QueryNameProcedure
= CmpQueryKeyName
;
839 ObjectTypeInitializer
.CloseProcedure
= CmpCloseKeyObject
;
840 ObjectTypeInitializer
.SecurityRequired
= TRUE
;
843 return ObCreateObjectType(&Name
, &ObjectTypeInitializer
, NULL
, &CmpKeyObjectType
);
848 CmpCreateRootNode(IN PHHIVE Hive
,
850 OUT PHCELL_INDEX Index
)
852 UNICODE_STRING KeyName
;
853 PCM_KEY_NODE KeyCell
;
854 LARGE_INTEGER SystemTime
;
857 /* Initialize the node name and allocate it */
858 RtlInitUnicodeString(&KeyName
, Name
);
859 *Index
= HvAllocateCell(Hive
,
860 FIELD_OFFSET(CM_KEY_NODE
, Name
) +
861 CmpNameSize(Hive
, &KeyName
),
864 if (*Index
== HCELL_NIL
) return FALSE
;
866 /* Set the cell index and get the data */
867 Hive
->BaseBlock
->RootCell
= *Index
;
868 KeyCell
= (PCM_KEY_NODE
)HvGetCell(Hive
, *Index
);
869 if (!KeyCell
) return FALSE
;
872 KeyCell
->Signature
= (USHORT
)CM_KEY_NODE_SIGNATURE
;
873 KeyCell
->Flags
= KEY_HIVE_ENTRY
| KEY_NO_DELETE
;
874 KeQuerySystemTime(&SystemTime
);
875 KeyCell
->LastWriteTime
= SystemTime
;
876 KeyCell
->Parent
= HCELL_NIL
;
877 KeyCell
->SubKeyCounts
[Stable
] = 0;
878 KeyCell
->SubKeyCounts
[Volatile
] = 0;
879 KeyCell
->SubKeyLists
[Stable
] = HCELL_NIL
;
880 KeyCell
->SubKeyLists
[Volatile
] = HCELL_NIL
;
881 KeyCell
->ValueList
.Count
= 0;
882 KeyCell
->ValueList
.List
= HCELL_NIL
;
883 KeyCell
->Security
= HCELL_NIL
;
884 KeyCell
->Class
= HCELL_NIL
;
885 KeyCell
->ClassLength
= 0;
886 KeyCell
->MaxNameLen
= 0;
887 KeyCell
->MaxClassLen
= 0;
888 KeyCell
->MaxValueNameLen
= 0;
889 KeyCell
->MaxValueDataLen
= 0;
891 /* Copy the name (this will also set the length) */
892 KeyCell
->NameLength
= CmpCopyName(Hive
, (PWCHAR
)KeyCell
->Name
, &KeyName
);
894 /* Check if the name was compressed */
895 if (KeyCell
->NameLength
< KeyName
.Length
)
898 KeyCell
->Flags
|= KEY_COMP_NAME
;
902 HvReleaseCell(Hive
, *Index
);
908 CmpCreateRegistryRoot(VOID
)
910 UNICODE_STRING KeyName
;
911 OBJECT_ATTRIBUTES ObjectAttributes
;
912 PCM_KEY_BODY RootKey
;
913 HCELL_INDEX RootIndex
;
915 PCM_KEY_NODE KeyCell
;
916 PSECURITY_DESCRIPTOR SecurityDescriptor
;
917 PCM_KEY_CONTROL_BLOCK Kcb
;
920 /* Setup the root node */
921 if (!CmpCreateRootNode(&CmiVolatileHive
->Hive
, L
"REGISTRY", &RootIndex
))
927 /* Create '\Registry' key. */
928 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
929 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
930 InitializeObjectAttributes(&ObjectAttributes
,
932 OBJ_CASE_INSENSITIVE
,
935 Status
= ObCreateObject(KernelMode
,
944 ExFreePool(SecurityDescriptor
);
945 if (!NT_SUCCESS(Status
)) return FALSE
;
947 /* Sanity check, and get the key cell */
948 ASSERT((&CmiVolatileHive
->Hive
)->ReleaseCellRoutine
== NULL
);
949 KeyCell
= (PCM_KEY_NODE
)HvGetCell(&CmiVolatileHive
->Hive
, RootIndex
);
950 if (!KeyCell
) return FALSE
;
953 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY");
954 Kcb
= CmpCreateKeyControlBlock(&CmiVolatileHive
->Hive
,
960 if (!Kcb
) return FALSE
;
962 /* Initialize the object */
963 RootKey
->KeyControlBlock
= Kcb
;
964 RootKey
->Type
= TAG('k', 'y', '0', '2');
965 RootKey
->NotifyBlock
= NULL
;
966 RootKey
->ProcessID
= PsGetCurrentProcessId();
969 EnlistKeyBodyWithKCB(RootKey
, 0);
971 /* Insert the key into the namespace */
972 Status
= ObInsertObject(RootKey
,
977 &CmpRegistryRootHandle
);
978 if (!NT_SUCCESS(Status
)) return FALSE
;
980 /* Reference the key again so that we never lose it */
981 Status
= ObReferenceObjectByHandle(CmpRegistryRootHandle
,
987 if (!NT_SUCCESS(Status
)) return FALSE
;
989 /* Completely sucessful */
995 CmpGetRegistryPath(IN PWCHAR ConfigPath
)
997 OBJECT_ATTRIBUTES ObjectAttributes
;
1000 PKEY_VALUE_PARTIAL_INFORMATION ValueInfo
;
1001 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
1002 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
1003 ULONG BufferSize
, ResultSize
;
1005 /* Check if we are booted in setup */
1006 if (ExpInTextModeSetup
)
1008 /* Setup the object attributes */
1009 InitializeObjectAttributes(&ObjectAttributes
,
1011 OBJ_CASE_INSENSITIVE
,
1015 Status
= ZwOpenKey(&KeyHandle
,
1018 if (!NT_SUCCESS(Status
)) return Status
;
1020 /* Allocate the buffer */
1021 BufferSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 4096;
1022 ValueInfo
= ExAllocatePoolWithTag(PagedPool
, BufferSize
, TAG_CM
);
1027 return STATUS_INSUFFICIENT_RESOURCES
;
1030 /* Query the value */
1031 Status
= ZwQueryValueKey(KeyHandle
,
1033 KeyValuePartialInformation
,
1038 if (!NT_SUCCESS(Status
))
1041 ExFreePool(ValueInfo
);
1045 /* Copy the config path and null-terminate it */
1046 RtlCopyMemory(ConfigPath
,
1048 ValueInfo
->DataLength
);
1049 ConfigPath
[ValueInfo
->DataLength
/ sizeof(WCHAR
)] = UNICODE_NULL
;
1050 ExFreePool(ValueInfo
);
1054 /* Just use default path */
1055 wcscpy(ConfigPath
, L
"\\SystemRoot");
1058 /* Add registry path */
1059 wcscat(ConfigPath
, L
"\\System32\\Config\\");
1062 return STATUS_SUCCESS
;
1067 CmpLoadHiveThread(IN PVOID StartContext
)
1069 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1070 UNICODE_STRING TempName
, FileName
, RegName
;
1071 ULONG FileStart
, RegStart
, i
, ErrorResponse
, WorkerCount
, Length
;
1072 ULONG PrimaryDisposition
, SecondaryDisposition
, ClusterSize
;
1074 HANDLE PrimaryHandle
, LogHandle
;
1075 NTSTATUS Status
= STATUS_SUCCESS
;
1076 PVOID ErrorParameters
;
1079 /* Get the hive index, make sure it makes sense */
1080 i
= (ULONG
)StartContext
;
1081 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1083 /* We were started */
1084 CmpMachineHiveList
[i
].ThreadStarted
= TRUE
;
1086 /* Build the file name and registry name strings */
1087 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1088 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1090 /* Now build the system root path */
1091 CmpGetRegistryPath(ConfigPath
);
1092 RtlInitUnicodeString(&TempName
, ConfigPath
);
1093 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1094 FileStart
= FileName
.Length
;
1096 /* And build the registry root path */
1097 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1098 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1099 RegStart
= RegName
.Length
;
1101 /* Build the base name */
1102 RegName
.Length
= RegStart
;
1103 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1104 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1106 /* Check if this is a child of the root */
1107 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1109 /* Then setup the whole name */
1110 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1111 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1114 /* Now add the rest of the file name */
1115 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1116 FileName
.Length
= FileStart
;
1117 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1118 if (!CmpMachineHiveList
[i
].CmHive
)
1120 /* We need to allocate a new hive structure */
1121 CmpMachineHiveList
[i
].Allocate
= TRUE
;
1123 /* Load the hive file */
1124 Status
= CmpInitHiveFromFile(&FileName
,
1125 CmpMachineHiveList
[i
].HHiveFlags
,
1127 &CmpMachineHiveList
[i
].Allocate
,
1129 if (!(NT_SUCCESS(Status
)) ||
1130 (!(CmHive
->FileHandles
[HFILE_TYPE_LOG
]) && !(CmpMiniNTBoot
))) // HACK
1132 /* We failed or couldn't get a log file, raise a hard error */
1133 ErrorParameters
= &FileName
;
1134 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1137 (PULONG_PTR
)&ErrorParameters
,
1142 /* Set the hive flags and newly allocated hive pointer */
1143 CmHive
->Flags
= CmpMachineHiveList
[i
].CmHiveFlags
;
1144 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1148 /* We already have a hive, is it volatile? */
1149 CmHive
= CmpMachineHiveList
[i
].CmHive
;
1150 if (!(CmHive
->Hive
.HiveFlags
& HIVE_VOLATILE
))
1152 /* It's now, open the hive file and log */
1153 Status
= CmpOpenHiveFiles(&FileName
,
1157 &PrimaryDisposition
,
1158 &SecondaryDisposition
,
1163 if (!(NT_SUCCESS(Status
)) || !(LogHandle
))
1165 /* Couldn't open the hive or its log file, raise a hard error */
1166 ErrorParameters
= &FileName
;
1167 NtRaiseHardError(STATUS_CANNOT_LOAD_REGISTRY_FILE
,
1170 (PULONG_PTR
)&ErrorParameters
,
1174 /* And bugcheck for posterity's sake */
1175 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 0, i
, Status
);
1178 /* Save the file handles. This should remove our sync hacks */
1179 CmHive
->FileHandles
[HFILE_TYPE_LOG
] = LogHandle
;
1180 CmHive
->FileHandles
[HFILE_TYPE_PRIMARY
] = PrimaryHandle
;
1182 /* Allow lazy flushing since the handles are there -- remove sync hacks */
1183 //ASSERT(CmHive->Hive.HiveFlags & HIVE_NOLAZYFLUSH);
1184 CmHive
->Hive
.HiveFlags
&= ~HIVE_NOLAZYFLUSH
;
1186 /* Get the real size of the hive */
1187 Length
= CmHive
->Hive
.Storage
[Stable
].Length
+ HBLOCK_SIZE
;
1189 /* Check if the cluster size doesn't match */
1190 if (CmHive
->Hive
.Cluster
!= ClusterSize
) ASSERT(FALSE
);
1192 /* Set the file size */
1193 //if (!CmpFileSetSize((PHHIVE)CmHive, HFILE_TYPE_PRIMARY, Length, Length))
1195 /* This shouldn't fail */
1199 /* Another thing we don't support is NTLDR-recovery */
1200 if (CmHive
->Hive
.BaseBlock
->BootRecover
) ASSERT(FALSE
);
1202 /* Finally, set our allocated hive to the same hive we've had */
1203 CmpMachineHiveList
[i
].CmHive2
= CmHive
;
1204 ASSERT(CmpMachineHiveList
[i
].CmHive
== CmpMachineHiveList
[i
].CmHive2
);
1209 CmpMachineHiveList
[i
].ThreadFinished
= TRUE
;
1211 /* Check if we're the last worker */
1212 WorkerCount
= InterlockedIncrement(&CmpLoadWorkerIncrement
);
1213 if (WorkerCount
== CM_NUMBER_OF_MACHINE_HIVES
)
1215 /* Signal the event */
1216 KeSetEvent(&CmpLoadWorkerEvent
, 0, FALSE
);
1219 /* Kill the thread */
1220 PsTerminateSystemThread(Status
);
1225 CmpInitializeHiveList(IN USHORT Flag
)
1227 WCHAR FileBuffer
[MAX_PATH
], RegBuffer
[MAX_PATH
], ConfigPath
[MAX_PATH
];
1228 UNICODE_STRING TempName
, FileName
, RegName
;
1231 ULONG FileStart
, RegStart
, i
;
1232 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1235 /* Allow writing for now */
1238 /* Build the file name and registry name strings */
1239 RtlInitEmptyUnicodeString(&FileName
, FileBuffer
, MAX_PATH
);
1240 RtlInitEmptyUnicodeString(&RegName
, RegBuffer
, MAX_PATH
);
1242 /* Now build the system root path */
1243 CmpGetRegistryPath(ConfigPath
);
1244 RtlInitUnicodeString(&TempName
, ConfigPath
);
1245 RtlAppendStringToString((PSTRING
)&FileName
, (PSTRING
)&TempName
);
1246 FileStart
= FileName
.Length
;
1248 /* And build the registry root path */
1249 RtlInitUnicodeString(&TempName
, L
"\\REGISTRY\\");
1250 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1251 RegStart
= RegName
.Length
;
1253 /* Setup the event to synchronize workers */
1254 KeInitializeEvent(&CmpLoadWorkerEvent
, SynchronizationEvent
, FALSE
);
1256 /* Enter special boot condition */
1257 CmpSpecialBootCondition
= TRUE
;
1259 /* Create the SD for the root hives */
1260 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1262 /* Loop every hive we care about */
1263 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1265 /* Make sure the list is setup */
1266 ASSERT(CmpMachineHiveList
[i
].Name
!= NULL
);
1268 /* Create a thread to handle this hive */
1269 Status
= PsCreateSystemThread(&Thread
,
1276 if (NT_SUCCESS(Status
))
1278 /* We don't care about the handle -- the thread self-terminates */
1283 /* Can't imagine this happening */
1284 KeBugCheckEx(BAD_SYSTEM_CONFIG_INFO
, 9, 3, i
, Status
);
1288 /* Make sure we've reached the end of the list */
1289 ASSERT(CmpMachineHiveList
[i
].Name
== NULL
);
1291 /* Wait for hive loading to finish */
1292 KeWaitForSingleObject(&CmpLoadWorkerEvent
,
1298 /* Exit the special boot condition and make sure all workers completed */
1299 CmpSpecialBootCondition
= FALSE
;
1300 ASSERT(CmpLoadWorkerIncrement
== CM_NUMBER_OF_MACHINE_HIVES
);
1302 /* Loop hives again */
1303 for (i
= 0; i
< CM_NUMBER_OF_MACHINE_HIVES
; i
++)
1305 /* Make sure the thread ran and finished */
1306 ASSERT(CmpMachineHiveList
[i
].ThreadFinished
== TRUE
);
1307 ASSERT(CmpMachineHiveList
[i
].ThreadStarted
== TRUE
);
1309 /* Check if this was a new hive */
1310 if (!CmpMachineHiveList
[i
].CmHive
)
1312 /* Make sure we allocated something */
1313 ASSERT(CmpMachineHiveList
[i
].CmHive2
!= NULL
);
1315 /* Build the base name */
1316 RegName
.Length
= RegStart
;
1317 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].BaseName
);
1318 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1320 /* Check if this is a child of the root */
1321 if (RegName
.Buffer
[RegName
.Length
/ sizeof(WCHAR
) - 1] == '\\')
1323 /* Then setup the whole name */
1324 RtlInitUnicodeString(&TempName
, CmpMachineHiveList
[i
].Name
);
1325 RtlAppendStringToString((PSTRING
)&RegName
, (PSTRING
)&TempName
);
1328 /* Now link the hive to its master */
1329 Status
= CmpLinkHiveToMaster(&RegName
,
1331 CmpMachineHiveList
[i
].CmHive2
,
1332 CmpMachineHiveList
[i
].Allocate
,
1333 SecurityDescriptor
);
1334 if (Status
!= STATUS_SUCCESS
)
1336 /* Linking needs to work */
1337 KeBugCheckEx(CONFIG_LIST_FAILED
, 11, Status
, i
, (ULONG_PTR
)&RegName
);
1340 /* Check if we had to allocate a new hive */
1341 if (CmpMachineHiveList
[i
].Allocate
)
1343 /* Sync the new hive */
1344 //HvSyncHive((PHHIVE)(CmpMachineHiveList[i].CmHive2));
1348 /* Check if we created a new hive */
1349 if (CmpMachineHiveList
[i
].CmHive2
)
1351 /* TODO: Add to HiveList key */
1355 /* Get rid of the SD */
1356 ExFreePool(SecurityDescriptor
);
1358 /* FIXME: Link SECURITY to SAM */
1360 /* FIXME: Link S-1-5-18 to .Default */
1367 OBJECT_ATTRIBUTES ObjectAttributes
;
1368 UNICODE_STRING KeyName
;
1371 PCMHIVE HardwareHive
;
1372 PSECURITY_DESCRIPTOR SecurityDescriptor
;
1375 /* Check if this is PE-boot */
1376 if (InitIsWinPEMode
)
1378 /* Set registry to PE mode */
1379 CmpMiniNTBoot
= TRUE
;
1380 CmpShareSystemHives
= TRUE
;
1383 /* Initialize the hive list and lock */
1384 InitializeListHead(&CmpHiveListHead
);
1385 ExInitializePushLock((PVOID
)&CmpHiveListHeadLock
);
1386 ExInitializePushLock((PVOID
)&CmpLoadHiveLock
);
1388 /* Initialize registry lock */
1389 ExInitializeResourceLite(&CmpRegistryLock
);
1391 /* Initialize the cache */
1392 CmpInitializeCache();
1394 /* Initialize allocation and delayed dereferencing */
1395 CmpInitCmPrivateAlloc();
1396 CmpInitCmPrivateDelayAlloc();
1397 CmpInitDelayDerefKCBEngine();
1399 /* Initialize callbacks */
1402 /* Initialize self healing */
1403 KeInitializeGuardedMutex(&CmpSelfHealQueueLock
);
1404 InitializeListHead(&CmpSelfHealQueueListHead
);
1406 /* Save the current process and lock the registry */
1407 CmpSystemProcess
= PsGetCurrentProcess();
1409 /* Create the key object types */
1410 Status
= CmpCreateObjectTypes();
1411 if (!NT_SUCCESS(Status
))
1414 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 1, Status
, 0);
1417 /* Build the master hive */
1418 Status
= CmpInitializeHive((PCMHIVE
*)&CmiVolatileHive
,
1428 if (!NT_SUCCESS(Status
))
1431 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 2, Status
, 0);
1434 /* Create the \REGISTRY key node */
1435 if (!CmpCreateRegistryRoot())
1438 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 3, 0, 0);
1441 /* Create the default security descriptor */
1442 SecurityDescriptor
= CmpHiveRootSecurityDescriptor();
1444 /* Create '\Registry\Machine' key. */
1445 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\MACHINE");
1446 InitializeObjectAttributes(&ObjectAttributes
,
1448 OBJ_CASE_INSENSITIVE
,
1450 SecurityDescriptor
);
1451 Status
= NtCreateKey(&KeyHandle
,
1452 KEY_READ
| KEY_WRITE
,
1458 if (!NT_SUCCESS(Status
))
1461 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 5, Status
, 0);
1464 /* Close the handle */
1467 /* Create '\Registry\User' key. */
1468 RtlInitUnicodeString(&KeyName
, L
"\\REGISTRY\\USER");
1469 InitializeObjectAttributes(&ObjectAttributes
,
1471 OBJ_CASE_INSENSITIVE
,
1473 SecurityDescriptor
);
1474 Status
= NtCreateKey(&KeyHandle
,
1475 KEY_READ
| KEY_WRITE
,
1481 if (!NT_SUCCESS(Status
))
1484 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 6, Status
, 0);
1487 /* Close the handle */
1490 /* Initialize the system hive */
1491 if (!CmpInitializeSystemHive(KeLoaderBlock
))
1494 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 7, 0, 0);
1497 /* Create the 'CurrentControlSet' link. */
1498 Status
= CmpCreateControlSet(KeLoaderBlock
);
1499 if (!NT_SUCCESS(Status
))
1502 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 8, Status
, 0);
1505 /* Create the hardware hive */
1506 Status
= CmpInitializeHive((PCMHIVE
*)&HardwareHive
,
1516 if (!NT_SUCCESS(Status
))
1519 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 11, Status
, 0);
1522 /* Add the hive to the hive list */
1523 CmpMachineHiveList
[0].CmHive
= (PCMHIVE
)HardwareHive
;
1525 /* Attach it to the machine key */
1526 RtlInitUnicodeString(&KeyName
, L
"\\Registry\\Machine\\HARDWARE");
1527 Status
= CmpLinkHiveToMaster(&KeyName
,
1529 (PCMHIVE
)HardwareHive
,
1531 SecurityDescriptor
);
1532 if (!NT_SUCCESS(Status
))
1535 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 12, Status
, 0);
1538 /* FIXME: Add to HiveList key */
1540 /* Free the security descriptor */
1541 ExFreePool(SecurityDescriptor
);
1543 /* Fill out the Hardware key with the ARC Data from the Loader */
1544 Status
= CmpInitializeHardwareConfiguration(KeLoaderBlock
);
1545 if (!NT_SUCCESS(Status
))
1548 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 13, Status
, 0);
1551 /* Initialize machine-dependent information into the registry */
1552 Status
= CmpInitializeMachineDependentConfiguration(KeLoaderBlock
);
1553 if (!NT_SUCCESS(Status
))
1556 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 14, Status
, 0);
1559 /* Initialize volatile registry settings */
1560 Status
= CmpSetSystemValues(KeLoaderBlock
);
1561 if (!NT_SUCCESS(Status
))
1564 KeBugCheckEx(CONFIG_INITIALIZATION_FAILED
, 1, 15, Status
, 0);
1567 /* Free the load options */
1568 ExFreePool(CmpLoadOptions
.Buffer
);
1570 /* If we got here, all went well */
1576 CmpLockRegistryExclusive(VOID
)
1578 /* Enter a critical region and lock the registry */
1579 KeEnterCriticalRegion();
1580 ExAcquireResourceExclusiveLite(&CmpRegistryLock
, TRUE
);
1583 ASSERT(CmpFlushStarveWriters
== 0);
1584 RtlGetCallersAddress(&CmpRegistryLockCaller
, &CmpRegistryLockCallerCaller
);
1589 CmpLockRegistry(VOID
)
1591 /* Enter a critical region */
1592 KeEnterCriticalRegion();
1594 /* Check if we have to starve writers */
1595 if (CmpFlushStarveWriters
)
1597 /* Starve exlusive waiters */
1598 ExAcquireSharedStarveExclusive(&CmpRegistryLock
, TRUE
);
1602 /* Just grab the lock */
1603 ExAcquireResourceSharedLite(&CmpRegistryLock
, TRUE
);
1609 CmpTestRegistryLock(VOID
)
1612 return !ExIsResourceAcquiredSharedLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1617 CmpTestRegistryLockExclusive(VOID
)
1620 return !ExIsResourceAcquiredExclusiveLite(&CmpRegistryLock
) ? FALSE
: TRUE
;
1625 CmpUnlockRegistry(VOID
)
1628 CMP_ASSERT_REGISTRY_LOCK();
1630 /* Check if we should flush the registry */
1631 if (CmpFlushOnLockRelease
)
1633 /* The registry should be exclusively locked for this */
1634 CMP_ASSERT_EXCLUSIVE_REGISTRY_LOCK();
1636 /* Flush the registry */
1637 CmpDoFlushAll(TRUE
);
1638 CmpFlushOnLockRelease
= FALSE
;
1641 /* Release the lock and leave the critical region */
1642 ExReleaseResourceLite(&CmpRegistryLock
);
1643 KeLeaveCriticalRegion();
1648 CmpAcquireTwoKcbLocksExclusiveByKey(IN ULONG ConvKey1
,
1651 ULONG Index1
, Index2
;
1654 CMP_ASSERT_REGISTRY_LOCK();
1656 /* Get hash indexes */
1657 Index1
= GET_HASH_INDEX(ConvKey1
);
1658 Index2
= GET_HASH_INDEX(ConvKey2
);
1660 /* See which one is highest */
1661 if (Index1
< Index2
)
1663 /* Grab them in the proper order */
1664 CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1665 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1669 /* Grab the second one first, then the first */
1670 CmpAcquireKcbLockExclusiveByKey(ConvKey2
);
1671 if (Index1
!= Index2
) CmpAcquireKcbLockExclusiveByKey(ConvKey1
);
1677 CmpReleaseTwoKcbLockByKey(IN ULONG ConvKey1
,
1680 ULONG Index1
, Index2
;
1683 CMP_ASSERT_REGISTRY_LOCK();
1685 /* Get hash indexes */
1686 Index1
= GET_HASH_INDEX(ConvKey1
);
1687 Index2
= GET_HASH_INDEX(ConvKey2
);
1688 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey2
).Owner
== KeGetCurrentThread()) ||
1689 (CmpTestRegistryLockExclusive()));
1691 /* See which one is highest */
1692 if (Index1
< Index2
)
1694 /* Grab them in the proper order */
1695 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1696 (CmpTestRegistryLockExclusive()));
1697 CmpReleaseKcbLockByKey(ConvKey2
);
1698 CmpReleaseKcbLockByKey(ConvKey1
);
1702 /* Release the first one first, then the second */
1703 if (Index1
!= Index2
)
1705 ASSERT((GET_HASH_ENTRY(CmpCacheTable
, ConvKey1
).Owner
== KeGetCurrentThread()) ||
1706 (CmpTestRegistryLockExclusive()));
1707 CmpReleaseKcbLockByKey(ConvKey1
);
1709 CmpReleaseKcbLockByKey(ConvKey2
);
1715 CmShutdownSystem(VOID
)
1717 /* Kill the workers and flush all hives */
1718 if (!CmFirstTime
) CmpShutdownWorkers();
1719 CmpDoFlushAll(TRUE
);