Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.0 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. callback.c
  5. Abstract:
  6. This module implements user mode call back services.
  7. Author:
  8. David N. Cutler (davec) 5-Jul-2000
  9. Environment:
  10. Kernel mode only.
  11. Revision History:
  12. --*/
  13. #include "ki.h"
  14. #pragma alloc_text(PAGE, KeUserModeCallback)
  15. NTSTATUS
  16. KeUserModeCallback (
  17. IN ULONG ApiNumber,
  18. IN PVOID InputBuffer,
  19. IN ULONG InputLength,
  20. OUT PVOID *OutputBuffer,
  21. IN PULONG OutputLength
  22. )
  23. /*++
  24. Routine Description:
  25. This function call out from kernel mode to a user mode function.
  26. Arguments:
  27. ApiNumber - Supplies the API number.
  28. InputBuffer - Supplies a pointer to a structure that is copied
  29. to the user stack.
  30. InputLength - Supplies the length of the input structure.
  31. Outputbuffer - Supplies a pointer to a variable that receives
  32. the address of the output buffer.
  33. Outputlength - Supplies a pointer to a variable that receives
  34. the length of the output buffer.
  35. Return Value:
  36. If the callout cannot be executed, then an error status is returned.
  37. Otherwise, the status returned by the callback function is returned.
  38. --*/
  39. {
  40. PUCALLOUT_FRAME CalloutFrame;
  41. ULONG Length;
  42. ULONG64 OldStack;
  43. NTSTATUS Status;
  44. PKTRAP_FRAME TrapFrame;
  45. PVOID ValueBuffer;
  46. ULONG ValueLength;
  47. ASSERT(KeGetPreviousMode() == UserMode);
  48. //
  49. // Get the user mode stack pointer and attempt to copy input buffer
  50. // to the user stack.
  51. //
  52. TrapFrame = KeGetCurrentThread()->TrapFrame;
  53. OldStack = TrapFrame->Rsp;
  54. try {
  55. //
  56. // Compute new user mode stack address, probe for writability, and
  57. // copy the input buffer to the user stack.
  58. //
  59. Length = ((InputLength + STACK_ROUND) & ~STACK_ROUND) + UCALLOUT_FRAME_LENGTH;
  60. CalloutFrame = (PUCALLOUT_FRAME)((OldStack - Length) & ~STACK_ROUND);
  61. ProbeForWrite(CalloutFrame, Length, STACK_ALIGN);
  62. RtlCopyMemory(CalloutFrame + 1, InputBuffer, InputLength);
  63. //
  64. // Fill in callout arguments.
  65. //
  66. CalloutFrame->Buffer = (PVOID)(CalloutFrame + 1);
  67. CalloutFrame->Length = InputLength;
  68. CalloutFrame->ApiNumber = ApiNumber;
  69. CalloutFrame->MachineFrame.Rsp = OldStack;
  70. CalloutFrame->MachineFrame.Rip = TrapFrame->Rip;
  71. //
  72. // If an exception occurs during the probe of the user stack, then
  73. // always handle the exception and return the exception code as the
  74. // status value.
  75. //
  76. } except (EXCEPTION_EXECUTE_HANDLER) {
  77. return GetExceptionCode();
  78. }
  79. //
  80. // Call user mode.
  81. //
  82. TrapFrame->Rsp = (ULONG64)CalloutFrame;
  83. Status = KiCallUserMode(OutputBuffer, OutputLength);
  84. //
  85. // When returning from user mode, any drawing done to the GDI TEB
  86. // batch must be flushed.
  87. //
  88. if (((PTEB)KeGetCurrentThread()->Teb)->GdiBatchCount > 0) {
  89. TrapFrame->Rsp -= 256;
  90. KeGdiFlushUserBatch();
  91. }
  92. TrapFrame->Rsp = OldStack;
  93. return Status;
  94. }