Leaked source code of windows server 2003
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.

107 lines
3.0 KiB

  1. TITLE "Runtime Stack Checking"
  2. ;++
  3. ;
  4. ; Copyright (c) 2000 Microsoft Corporation
  5. ;
  6. ; Module Name:
  7. ;
  8. ; chkstk.s
  9. ;
  10. ; Abstract:
  11. ;
  12. ; This module implements runtime stack checking.
  13. ;
  14. ; Author:
  15. ;
  16. ; David N. Cutler (davec) 20-Oct-2000
  17. ;
  18. ; Environment:
  19. ;
  20. ; Any mode.
  21. ;
  22. ;--
  23. include ksamd64.inc
  24. subttl "Check Stack"
  25. ;++
  26. ;
  27. ; ULONG64
  28. ; __chkstk (
  29. ; VOID
  30. ; )
  31. ;
  32. ; Routine Description:
  33. ;
  34. ; This function provides runtime stack checking for local allocations
  35. ; that are more than a page and for storage dynamically allocated with
  36. ; the alloca function. Stack checking consists of probing downward in
  37. ; the stack a page at a time. If the current stack commitment is exceeded,
  38. ; then the system will automatically attempts to expand the stack. If the
  39. ; attempt succeeds, then another page is committed. Otherwise, a stack
  40. ; overflow exception is raised. It is the responsibility of the caller to
  41. ; handle this exception.
  42. ;
  43. ; N.B. This routine is called using a non-standard calling sequence since
  44. ; it is typically called from within the prologue. The allocation size
  45. ; argument is in register rax and it must be preserved. Registers r10
  46. ; and r11 used by this function and are not preserved.
  47. ;
  48. ; The typical calling sequence from the prologue is:
  49. ;
  50. ; mov rax, allocation-size ; set requested stack frame size
  51. ; call __chkstk ; check stack page allocation
  52. ; sub rsp, rax ; allocate stack frame
  53. ;
  54. ; Arguments:
  55. ;
  56. ; None.
  57. ;
  58. ; Implicit Arguments:
  59. ;
  60. ; Allocation (rax) - Supplies the size of the allocation on the stack.
  61. ;
  62. ; Return Value:
  63. ;
  64. ; The allocation size is returned as the function value.
  65. ;
  66. ;--
  67. LEAF_ENTRY __chkstk, _TEXT$00
  68. ifdef NTOS_KERNEL_RUNTIME
  69. ret ; return
  70. else
  71. lea r10, 8[rsp] ; compute requested stack address
  72. sub r10, rax ;
  73. ;
  74. ; If the new stack address is greater than the current stack limit, then the
  75. ; pages have already been allocated and nothing further needs to be done.
  76. ;
  77. mov r11, gs:[TeStackLimit] ; get current stack limit
  78. cmp r10, r11 ; check if stack within limits
  79. jae short cs20 ; if ae, stack within limits
  80. ;
  81. ; The new stack address is not within the currently allocated stack. Probe
  82. ; pages downward in the stack until all pages have been allocated or a stack
  83. ; overflow occurs in which case an exception will be raised.
  84. ;
  85. and r10w, not (PAGE_SIZE - 1) ; round down new stack address
  86. cs10: lea r11, (-PAGE_SIZE)[r11] ; get next lower page address
  87. mov byte ptr [r11], 0 ; probe stack address
  88. cmp r10, r11 ; check if end of probe range
  89. jne short cs10 ; if ne, not end of probe range
  90. cs20: ret ; return
  91. endif
  92. LEAF_END __chkstk, _TEXT$00
  93. end