2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/ex/error.c
5 * PURPOSE: Error Functions and Status/Exception Dispatching/Raising
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES *****************************************************************/
15 #define TAG_ERR ' rrE'
17 /* GLOBALS ****************************************************************/
19 BOOLEAN ExReadyForErrors
= FALSE
;
20 PVOID ExpDefaultErrorPort
= NULL
;
21 PEPROCESS ExpDefaultErrorPortProcess
= NULL
;
23 /* FUNCTIONS ****************************************************************/
26 * @name ExpSystemErrorHandler
33 * @param NumberOfParameters
36 * @param UnicodeStringParameterMask
42 * @param ValidResponseOptions
55 ExpSystemErrorHandler(IN NTSTATUS ErrorStatus
,
56 IN ULONG NumberOfParameters
,
57 IN ULONG UnicodeStringParameterMask
,
58 IN PULONG_PTR Parameters
,
61 ULONG_PTR BugCheckParameters
[MAXIMUM_HARDERROR_PARAMETERS
] = {0, 0, 0, 0};
65 ASSERT(NumberOfParameters
<= MAXIMUM_HARDERROR_PARAMETERS
);
68 * KeBugCheck expects MAXIMUM_HARDERROR_PARAMETERS parameters,
69 * but we might get called with less, so use a local buffer here.
71 for (i
= 0; i
< NumberOfParameters
; i
++)
74 BugCheckParameters
[i
] = Parameters
[i
];
78 KeBugCheckEx(FATAL_UNHANDLED_HARD_ERROR
,
80 (ULONG_PTR
)BugCheckParameters
,
83 return STATUS_SUCCESS
;
87 * @name ExpRaiseHardError
94 * @param NumberOfParameters
97 * @param UnicodeStringParameterMask
103 * @param ValidResponseOptions
116 ExpRaiseHardError(IN NTSTATUS ErrorStatus
,
117 IN ULONG NumberOfParameters
,
118 IN ULONG UnicodeStringParameterMask
,
119 IN PULONG_PTR Parameters
,
120 IN ULONG ValidResponseOptions
,
123 PEPROCESS Process
= PsGetCurrentProcess();
124 PETHREAD Thread
= PsGetCurrentThread();
125 UCHAR Buffer
[PORT_MAXIMUM_MESSAGE_LENGTH
];
126 PHARDERROR_MSG Message
= (PHARDERROR_MSG
)Buffer
;
129 KPROCESSOR_MODE PreviousMode
= KeGetPreviousMode();
132 /* Check if this error will shutdown the system */
133 if (ValidResponseOptions
== OptionShutdownSystem
)
135 /* Check for privilege */
136 if (!SeSinglePrivilegeCheck(SeShutdownPrivilege
, PreviousMode
))
139 return STATUS_PRIVILEGE_NOT_HELD
;
142 /* Don't handle any new hard errors */
143 ExReadyForErrors
= FALSE
;
146 /* Check if hard errors are not disabled */
147 if (!Thread
->HardErrorsAreDisabled
)
149 /* Check if we can't do errors anymore, and this is serious */
150 if ((!ExReadyForErrors
) && (NT_ERROR(ErrorStatus
)))
152 /* Use the system handler */
153 ExpSystemErrorHandler(ErrorStatus
,
155 UnicodeStringParameterMask
,
157 (PreviousMode
!= KernelMode
) ? TRUE
: FALSE
);
161 /* Check if we have an exception port */
162 if (Process
->ExceptionPort
)
164 /* Check if hard errors should be processed */
165 if (Process
->DefaultHardErrorProcessing
& 1)
168 PortHandle
= Process
->ExceptionPort
;
172 /* It's disabled, check if the error overrides it */
173 if (ErrorStatus
& 0x10000000)
175 /* Use the port anyway */
176 PortHandle
= Process
->ExceptionPort
;
187 /* Check if hard errors are enabled */
188 if (Process
->DefaultHardErrorProcessing
& 1)
190 /* Use our default system port */
191 PortHandle
= ExpDefaultErrorPort
;
195 /* It's disabled, check if the error overrides it */
196 if (ErrorStatus
& 0x10000000)
198 /* Use the port anyway */
199 PortHandle
= ExpDefaultErrorPort
;
209 /* If hard errors are disabled, do nothing */
210 if (Thread
->HardErrorsAreDisabled
) PortHandle
= NULL
;
212 /* Now check if we have a port */
215 /* Check if this is the default process */
216 if (Process
== ExpDefaultErrorPortProcess
)
218 /* We can't handle the error, check if this is critical */
219 if (NT_ERROR(ErrorStatus
))
221 /* It is, invoke the system handler */
222 ExpSystemErrorHandler(ErrorStatus
,
224 UnicodeStringParameterMask
,
226 (PreviousMode
!= KernelMode
) ? TRUE
: FALSE
);
228 /* If we survived, return to caller */
229 *Response
= ResponseReturnToCaller
;
230 return STATUS_SUCCESS
;
234 /* Setup the LPC Message */
235 Message
->h
.u1
.Length
= (sizeof(HARDERROR_MSG
) << 16) |
236 (sizeof(HARDERROR_MSG
) - sizeof(PORT_MESSAGE
));
237 Message
->h
.u2
.ZeroInit
= LPC_ERROR_EVENT
;
238 Message
->Status
= ErrorStatus
&~ 0x10000000;
239 Message
->ValidResponseOptions
= ValidResponseOptions
;
240 Message
->UnicodeStringParameterMask
= UnicodeStringParameterMask
;
241 Message
->NumberOfParameters
= NumberOfParameters
;
242 KeQuerySystemTime(&Message
->ErrorTime
);
244 /* Copy the parameters */
245 if (Parameters
) RtlMoveMemory(&Message
->Parameters
,
247 sizeof(ULONG_PTR
) * NumberOfParameters
);
249 /* Send the LPC Message */
250 Status
= LpcRequestWaitReplyPort(PortHandle
,
253 if (NT_SUCCESS(Status
))
255 /* Check what kind of response we got */
256 if ((Message
->Response
!= ResponseReturnToCaller
) &&
257 (Message
->Response
!= ResponseNotHandled
) &&
258 (Message
->Response
!= ResponseAbort
) &&
259 (Message
->Response
!= ResponseCancel
) &&
260 (Message
->Response
!= ResponseIgnore
) &&
261 (Message
->Response
!= ResponseNo
) &&
262 (Message
->Response
!= ResponseOk
) &&
263 (Message
->Response
!= ResponseRetry
) &&
264 (Message
->Response
!= ResponseYes
) &&
265 (Message
->Response
!= ResponseTryAgain
) &&
266 (Message
->Response
!= ResponseContinue
))
268 /* Reset to a default one */
269 Message
->Response
= ResponseReturnToCaller
;
272 /* Set the response */
273 *Response
= Message
->Response
;
279 *Response
= ResponseReturnToCaller
;
280 Status
= STATUS_SUCCESS
;
288 * @name ExRaiseAccessViolation
291 * The ExRaiseAccessViolation routine can be used with structured exception
292 * handling to throw a driver-determined exception for a memory access
293 * violation that occurs when a driver processes I/O requests.
294 * See: http://msdn.microsoft.com/library/en-us/Kernel_r/hh/Kernel_r/k102_71b4c053-599c-4a6d-8a59-08aae6bdc534.xml.asp?frame=true
295 * http://www.osronline.com/ddkx/kmarch/k102_814i.htm
304 ExRaiseAccessViolation(VOID
)
306 /* Raise the Right Status */
307 RtlRaiseStatus(STATUS_ACCESS_VIOLATION
);
311 * @name ExRaiseDatatypeMisalignment
314 * ExRaiseDatatypeMisalignment raises an exception with the exception
315 * code set to STATUS_DATATYPE_MISALIGNMENT
317 * http://www.osronline.com/ddkx/kmarch/k102_814i.htm
326 ExRaiseDatatypeMisalignment(VOID
)
328 /* Raise the Right Status */
329 RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT
);
333 * @name ExSystemExceptionFilter
336 * TODO: Add description
345 ExSystemExceptionFilter(VOID
)
347 return KeGetPreviousMode() != KernelMode
?
348 EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH
;
352 * @name ExRaiseHardError
355 * See NtRaiseHardError
360 * @param NumberOfParameters
361 * Number of optional parameters in Parameters array
363 * @param UnicodeStringParameterMask
364 * Optional string parameter (can be only one per error code)
367 * Array of ULONG parameters for use in error message string
369 * @param ValidResponseOptions
370 * See HARDERROR_RESPONSE_OPTION for possible values description
373 * Pointer to HARDERROR_RESPONSE enumeration
382 ExRaiseHardError(IN NTSTATUS ErrorStatus
,
383 IN ULONG NumberOfParameters
,
384 IN ULONG UnicodeStringParameterMask
,
385 IN PULONG_PTR Parameters
,
386 IN ULONG ValidResponseOptions
,
390 UNICODE_STRING CapturedParams
[MAXIMUM_HARDERROR_PARAMETERS
];
392 PULONG_PTR UserData
= NULL
, ParameterBase
;
393 PUNICODE_STRING StringBase
;
399 /* Check if we have parameters */
402 /* Check if we have strings */
403 if (UnicodeStringParameterMask
)
405 /* Add the maximum possible size */
406 Size
= (sizeof(ULONG_PTR
) + sizeof(UNICODE_STRING
)) *
407 MAXIMUM_HARDERROR_PARAMETERS
+ sizeof(UNICODE_STRING
);
409 /* Loop each parameter */
410 for (i
= 0; i
< NumberOfParameters
; i
++)
412 /* Check if it's part of the mask */
413 if (UnicodeStringParameterMask
& (1 << i
))
416 RtlMoveMemory(&CapturedParams
[i
],
418 sizeof(UNICODE_STRING
));
420 /* Increase the size */
421 Size
+= CapturedParams
[i
].MaximumLength
;
425 /* Allocate the user data region */
426 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
432 if (!NT_SUCCESS(Status
)) return Status
;
434 /* Set the pointers to our various data */
435 ParameterBase
= UserData
;
436 StringBase
= (PVOID
)((ULONG_PTR
)UserData
+
438 MAXIMUM_HARDERROR_PARAMETERS
);
439 BufferBase
= (PVOID
)((ULONG_PTR
)StringBase
+
440 sizeof(UNICODE_STRING
) *
441 MAXIMUM_HARDERROR_PARAMETERS
);
443 /* Loop parameters again */
444 for (i
= 0; i
< NumberOfParameters
; i
++)
446 /* Check if we're in the mask */
447 if (UnicodeStringParameterMask
& (1 << i
))
449 /* Update the base */
450 ParameterBase
[i
] = (ULONG_PTR
)&StringBase
[i
];
452 /* Copy the string buffer */
453 RtlMoveMemory(BufferBase
,
454 CapturedParams
[i
].Buffer
,
455 CapturedParams
[i
].MaximumLength
);
458 CapturedParams
[i
].Buffer
= BufferBase
;
460 /* Copy the string structure */
461 RtlMoveMemory(&StringBase
[i
],
463 sizeof(UNICODE_STRING
));
465 /* Update the pointer */
466 BufferBase
+= CapturedParams
[i
].MaximumLength
;
470 /* No need to copy any strings */
471 ParameterBase
[i
] = Parameters
[i
];
477 /* Just keep the data as is */
478 UserData
= Parameters
;
482 /* Now call the worker function */
483 Status
= ExpRaiseHardError(ErrorStatus
,
485 UnicodeStringParameterMask
,
487 ValidResponseOptions
,
490 /* Check if we had done user-mode allocation */
491 if ((UserData
) && (UserData
!= Parameters
))
493 /* We did! Delete it */
495 ZwFreeVirtualMemory(NtCurrentProcess(),
501 /* Return status and the response */
502 *Response
= SafeResponse
;
507 * @name NtRaiseHardError
510 * This function sends HARDERROR_MSG LPC message to listener
511 * (typically CSRSS.EXE). See NtSetDefaultHardErrorPort for more information
512 * See: http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Error/NtRaiseHardError.html
517 * @param NumberOfParameters
518 * Number of optional parameters in Parameters array
520 * @param UnicodeStringParameterMask
521 * Optional string parameter (can be only one per error code)
524 * Array of ULONG_PTR parameters for use in error message string
526 * @param ValidResponseOptions
527 * See HARDERROR_RESPONSE_OPTION for possible values description
530 * Pointer to HARDERROR_RESPONSE enumeration
534 * @remarks NtRaiseHardError is easy way to display message in GUI
535 * without loading Win32 API libraries
540 NtRaiseHardError(IN NTSTATUS ErrorStatus
,
541 IN ULONG NumberOfParameters
,
542 IN ULONG UnicodeStringParameterMask
,
543 IN PULONG_PTR Parameters
,
544 IN ULONG ValidResponseOptions
,
547 NTSTATUS Status
= STATUS_SUCCESS
;
548 PULONG_PTR SafeParams
= NULL
;
550 UNICODE_STRING SafeString
;
553 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
555 /* Validate parameter count */
556 if (NumberOfParameters
> MAXIMUM_HARDERROR_PARAMETERS
)
559 return STATUS_INVALID_PARAMETER_2
;
562 /* Make sure we have some at least */
563 if ((Parameters
) && !(NumberOfParameters
))
566 return STATUS_INVALID_PARAMETER_2
;
569 /* Check if we were called from user-mode */
570 if (PreviousMode
!= KernelMode
)
572 /* First validate the responses */
573 switch (ValidResponseOptions
)
575 /* Check all valid cases */
576 case OptionAbortRetryIgnore
:
579 case OptionRetryCancel
:
581 case OptionYesNoCancel
:
582 case OptionShutdownSystem
:
585 /* Anything else is invalid */
587 return STATUS_INVALID_PARAMETER_4
;
590 /* Enter SEH Block */
593 /* Validate the response pointer */
594 ProbeForWriteUlong(Response
);
596 /* Check if we have parameters */
599 /* Validate the parameter pointers */
600 ParamSize
= sizeof(ULONG_PTR
) * NumberOfParameters
;
601 ProbeForRead(Parameters
, ParamSize
, sizeof(ULONG_PTR
));
603 /* Allocate a safe buffer */
604 SafeParams
= ExAllocatePoolWithTag(PagedPool
,
609 RtlCopyMemory(SafeParams
, Parameters
, ParamSize
);
611 /* Nowo check if there's strings in it */
612 if (UnicodeStringParameterMask
)
614 /* Loop every string */
615 for (i
= 0; i
< NumberOfParameters
; i
++)
617 /* Check if this parameter is a string */
618 if (UnicodeStringParameterMask
& (1 << i
))
620 /* Probe the structure */
621 ProbeForRead((PVOID
)SafeParams
[i
],
622 sizeof(UNICODE_STRING
),
626 RtlCopyMemory(&SafeString
,
627 (PVOID
)SafeParams
[i
],
628 sizeof(UNICODE_STRING
));
630 /* Probe the buffer */
631 ProbeForRead(SafeString
.Buffer
,
632 SafeString
.MaximumLength
,
639 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
641 /* Free captured buffer */
642 if (SafeParams
) ExFreePool(SafeParams
);
644 /* Return the exception code */
645 _SEH2_YIELD(return _SEH2_GetExceptionCode());
649 /* Call the system function directly, because we probed */
650 ExpRaiseHardError(ErrorStatus
,
652 UnicodeStringParameterMask
,
654 ValidResponseOptions
,
660 SafeParams
= Parameters
;
663 * Call the Executive Function. It will probe and copy pointers to
666 ExRaiseHardError(ErrorStatus
,
668 UnicodeStringParameterMask
,
670 ValidResponseOptions
,
674 /* Check if we were called in user-mode */
675 if (PreviousMode
!= KernelMode
)
677 /* That means we have a buffer to free */
678 if (SafeParams
) ExFreePool(SafeParams
);
680 /* Enter SEH Block for return */
683 /* Return the response */
684 *Response
= SafeResponse
;
686 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
688 /* Get the exception code */
689 Status
= _SEH2_GetExceptionCode();
695 /* Return the response */
696 *Response
= SafeResponse
;
704 * @name NtSetDefaultHardErrorPort
707 * NtSetDefaultHardErrorPort is typically called only once. After call,
708 * kernel set BOOLEAN flag named _ExReadyForErrors to TRUE, and all other
709 * tries to change default port are broken with STATUS_UNSUCCESSFUL error code
710 * See: http://www.windowsitlibrary.com/Content/356/08/2.html
711 * http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Error/NtSetDefaultHardErrorPort.html
714 * Handle to named port object
718 * @remarks Privileges: SE_TCB_PRIVILEGE
723 NtSetDefaultHardErrorPort(IN HANDLE PortHandle
)
725 KPROCESSOR_MODE PreviousMode
= ExGetPreviousMode();
726 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
728 /* Check if we have the Privilege */
729 if (!SeSinglePrivilegeCheck(SeTcbPrivilege
, PreviousMode
))
731 DPRINT1("NtSetDefaultHardErrorPort: Caller requires "
732 "the SeTcbPrivilege privilege!\n");
733 return STATUS_PRIVILEGE_NOT_HELD
;
736 /* Only called once during bootup, make sure we weren't called yet */
737 if (!ExReadyForErrors
)
739 /* Reference the port */
740 Status
= ObReferenceObjectByHandle(PortHandle
,
744 (PVOID
*)&ExpDefaultErrorPort
,
746 if (NT_SUCCESS(Status
))
749 ExpDefaultErrorPortProcess
= PsGetCurrentProcess();
750 ExReadyForErrors
= TRUE
;
754 /* Return status to caller */
762 /* Not supported in Kernel Mode */
763 RtlRaiseStatus(STATUS_NOT_IMPLEMENTED
);