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.

126 lines
3.8 KiB

  1. page ,132
  2. title chkstk - C stack checking routine
  3. ;***
  4. ;chkstk.asm - C stack checking routine
  5. ;
  6. ; Copyright (c) 1985-1991, Microsoft Corporation. All rights reserved.
  7. ;
  8. ;Purpose:
  9. ; Provides support for automatic stack checking in C procedures
  10. ; when stack checking is enabled.
  11. ;
  12. ;Revision History:
  13. ; 04-21-87 SKS Added conditional assembly switch for STKHQQ = 0
  14. ; 07-23-87 MAG [1] Added run-time CS:IP error processing for QC
  15. ; 08-17-87 JLS [2] Remove all references to DGROUP
  16. ; 08-25-87 JLS [3] Shift include files
  17. ; 11-13-87 SKS OS/2 Reentrant version, add thread ID check
  18. ; 11-18-87 SKS Make STKHQQ an array (oops!)
  19. ; 12-14-87 SKS add .286p to allow PUSH immediate value
  20. ; 02-19-88 SKS Change minimum bottom limit to STACKSLOP, not 0
  21. ; 06-01-88 PHG Merge DLL and normal versions
  22. ; 09-21-88 WAJ initial 386 version
  23. ; 10-18-88 JCR Chkstk was trashing bx... not good on 386
  24. ; 06-06-89 JCR 386 mthread support
  25. ; 06-20-89 JCR 386: Removed _LOAD_DGROUP code
  26. ; 04-06-90 GJF Fixed the copyright.
  27. ; 06-21-90 GJF Rewritten to probe pages
  28. ; 10-15-90 GJF Restored _end and STKHQQ.
  29. ; 03-19-91 GJF Revised to preserve all registers except eax. Note
  30. ; this is _rchkstk functionality so there is no longer
  31. ; a separate _rchkstk routine.
  32. ; 08-01-91 GJF Got rid of _end and STKHQQ, except for Cruiser
  33. ; (probably not needed for Cruiser either) [_WIN32_].
  34. ; 09-27-91 JCR Merged Stevewo' changes from NT tree
  35. ;
  36. ;*******************************************************************************
  37. .xlist
  38. include cruntime.inc
  39. .list
  40. ; size of a page of memory
  41. _PAGESIZE_ equ 1000h
  42. ifdef _CRUISER_
  43. .data
  44. extrn pascal _end:dword ; stack bottom
  45. ifndef MTHREAD
  46. public pascal STKHQQ ; used by parasitic heap
  47. STKHQQ dd dataoffset _end+STACKSLOP ; initial value
  48. endif ;MTHREAD
  49. endif ;_CRUISER_
  50. CODESEG
  51. page
  52. ;***
  53. ;_chkstk - check stack upon procedure entry
  54. ;
  55. ;Purpose:
  56. ; Provide stack checking on procedure entry. Method is to simply probe
  57. ; each page of memory required for the stack in descending order. This
  58. ; causes the necessary pages of memory to be allocated via the guard
  59. ; page scheme, if possible. In the event of failure, the OS raises the
  60. ; _XCPT_UNABLE_TO_GROW_STACK exception.
  61. ;
  62. ; NOTE: Currently, the (EAX < _PAGESIZE_) code path falls through
  63. ; to the "lastpage" label of the (EAX >= _PAGESIZE_) code path. This
  64. ; is small; a minor speed optimization would be to special case
  65. ; this up top. This would avoid the painful save/restore of
  66. ; ecx and would shorten the code path by 4-6 instructions.
  67. ;
  68. ;Entry:
  69. ; EAX = size of local frame
  70. ;
  71. ;Exit:
  72. ; ESP = new stackframe, if successful
  73. ;
  74. ;Uses:
  75. ; EAX
  76. ;
  77. ;Exceptions:
  78. ; _XCPT_GUARD_PAGE_VIOLATION - May be raised on a page probe. NEVER TRAP
  79. ; THIS!!!! It is used by the OS to grow the
  80. ; stack on demand.
  81. ; _XCPT_UNABLE_TO_GROW_STACK - The stack cannot be grown. More precisely,
  82. ; the attempt by the OS memory manager to
  83. ; allocate another guard page in response
  84. ; to a _XCPT_GUARD_PAGE_VIOLATION has
  85. ; failed.
  86. ;
  87. ;*******************************************************************************
  88. labelP _alloca_probe, PUBLIC
  89. labelP _chkstk, PUBLIC
  90. push ecx ; save ecx
  91. mov ecx,esp ; compute new stack pointer in ecx
  92. add ecx,8 ; correct for return address and saved
  93. ; ecx value
  94. probepages:
  95. cmp eax,_PAGESIZE_ ; more than one page requested?
  96. jb short lastpage ; no
  97. sub ecx,_PAGESIZE_ ; yes, move down a page and...
  98. or dword ptr [ecx],0 ; ...probe it
  99. sub eax,_PAGESIZE_ ; adjust request
  100. jmp probepages
  101. lastpage:
  102. sub ecx,eax ; move stack down by eax and do a...
  103. or dword ptr [ecx],0 ; ...probe in case a page was crossed
  104. mov eax,esp ; save pointer to current tos
  105. mov esp,ecx ; set the new stack pointer
  106. mov ecx,dword ptr [eax] ; recover ecx
  107. mov eax,dword ptr [eax + 4] ; recover return address
  108. jmp eax ; return
  109. end