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.

443 lines
16 KiB

  1. title "Call Out to User Mode"
  2. ;++
  3. ;
  4. ; Copyright (c) 1994 Microsoft Corporation
  5. ;
  6. ; Module Name:
  7. ;
  8. ; callout.asm
  9. ;
  10. ; Abstract:
  11. ;
  12. ; This module implements the code necessary to call out from kernel
  13. ; mode to user mode.
  14. ;
  15. ; Author:
  16. ;
  17. ; David N. Cutler (davec) 1-Nov-1994
  18. ;
  19. ; Environment:
  20. ;
  21. ; Kernel mode only.
  22. ;
  23. ; Revision History:
  24. ;
  25. ;--
  26. .386p
  27. .xlist
  28. include ks386.inc
  29. include i386\kimacro.inc
  30. include callconv.inc
  31. .list
  32. extrn _KiServiceExit:PROC
  33. extrn _KeUserCallbackDispatcher:DWORD
  34. EXTRNP _MmGrowKernelStack,1
  35. _TEXT SEGMENT DWORD PUBLIC 'CODE'
  36. ASSUME DS:FLAT, ES:FLAT, SS:FLAT, FS:NOTHING, GS:NOTHING
  37. page ,132
  38. subttl "Call User Mode Function"
  39. ;++
  40. ;
  41. ; NTSTATUS
  42. ; KiCallUserMode (
  43. ; IN PVOID *Outputbuffer,
  44. ; IN PULONG OutputLength
  45. ; )
  46. ;
  47. ; Routine Description:
  48. ;
  49. ; This function calls a user mode function from kernel mode.
  50. ;
  51. ; N.B. This function calls out to user mode and the NtCallbackReturn
  52. ; function returns back to the caller of this function. Therefore,
  53. ; the stack layout must be consistent between the two routines.
  54. ;
  55. ; Arguments:
  56. ;
  57. ; OutputBuffer - Supplies a pointer to the variable that receivies
  58. ; the address of the output buffer.
  59. ;
  60. ; OutputLength - Supplies a pointer to a variable that receives
  61. ; the length of the output buffer.
  62. ;
  63. ; Return Value:
  64. ;
  65. ; The final status of the call out function is returned as the status
  66. ; of the function.
  67. ;
  68. ; N.B. This function does not return to its caller. A return to the
  69. ; caller is executed when a NtCallbackReturn system service is
  70. ; executed.
  71. ;
  72. ; N.B. This function does return to its caller if a kernel stack
  73. ; expansion is required and the attempted expansion fails.
  74. ;
  75. ;--
  76. ;
  77. ; To support the debugger, the callback stack frame is now defined in i386.h.
  78. ; If the stack frame is changed, i386.h must be updated and geni386
  79. ; rebuilt and run, then rebuild this file and ntos\kd.
  80. ;
  81. ; The FPO record below must also be updated to correctly represent
  82. ; the stack frame.
  83. ;
  84. cPublicProc _KiCallUserMode, 2
  85. .FPO (3, 2, 4, 4, 0, 0)
  86. ;
  87. ; Save nonvolatile registers.
  88. ;
  89. push ebp ; save nonvolatile registers
  90. push ebx ;
  91. push esi ;
  92. push edi ;
  93. ;
  94. ; Check if sufficient room is available on the kernel stack for another
  95. ; system call.
  96. ;
  97. mov ebx,PCR[PcPrcbData + PbCurrentThread] ; get current thread address
  98. lea eax,[esp]-KERNEL_LARGE_STACK_COMMIT ; compute bottom address
  99. cmp eax,[ebx]+ThStackLimit ; check if limit exceeded
  100. jae short Kcb10 ; if ae, limit not exceeded
  101. stdCall _MmGrowKernelStack,<esp> ; attempt to grow kernel stack
  102. or eax, eax ; check for successful completion
  103. jne Kcb20 ; if ne, attempt to grow failed
  104. mov eax, [ebx].ThStackLimit ; get new stack limit
  105. mov PCR[PcStackLimit], eax ; set new stack limit
  106. ;
  107. ; Get the address of the current thread and save the previous trap frame
  108. ; and calback stack addresses in the current frame. Also save the new
  109. ; callback stack address in the thread object.
  110. ;
  111. Kcb10: push [ebx].ThCallbackStack ; save callback stack address
  112. mov edx,[ebx].ThTrapFrame ; get current trap frame address
  113. push edx ; save trap frame address
  114. mov esi,[ebx].ThInitialStack ; get initial stack address
  115. push esi ; save initial stack address
  116. mov [ebx].ThCallbackStack,esp ; save callback stack address
  117. KcbPrologEnd: ; help for the debugger
  118. ;
  119. ; Copy the numeric save area from the previous save area to the new save
  120. ; area and establish a new initial kernel stack.
  121. ;
  122. ;
  123. ; Make sure that the destination NPX Save area is 16-byte aligned
  124. ; as required by fxsave\fxrstor
  125. ;
  126. and esp, 0fffffff0h
  127. mov edi,esp ; set new initial stack address
  128. sub esp,NPX_FRAME_LENGTH ; compute destination NPX save area
  129. sub esi,NPX_FRAME_LENGTH ; compute source NPX save area
  130. cli ; disable interrupts
  131. mov ecx,[esi].FpControlWord ; copy NPX state to new frame
  132. mov [esp].FpControlWord,ecx ;
  133. mov ecx,[esi].FpStatusWord ;
  134. mov [esp].FpStatusWord,ecx ;
  135. mov ecx,[esi].FpTagWord ;
  136. mov [esp].FpTagWord,ecx ;
  137. mov ecx,[esi].FxMXCsr ;
  138. mov [esp].FxMXCsr,ecx ;
  139. mov ecx,[esi].FpCr0NpxState ;
  140. mov [esp].FpCr0NpxState,ecx ;
  141. mov esi,PCR[PcTss] ; get address of task switch segment
  142. mov [ebx].ThInitialStack,edi ; reset initial stack address
  143. mov PCR[PcInitialStack],esp ; set stack check base address
  144. sub esp,TsV86Gs - TsHardwareSegSs ; bias for missing V86 fields
  145. mov [esi].TssEsp0,esp ; set kernel entry stack address
  146. ;
  147. ; Construct a trap frame to facilitate the transfer into user mode via
  148. ; the standard system call exit.
  149. ;
  150. sub esp,TsHardwareSegSs + 4 ; allocate trap frame
  151. mov ebp,esp ; set address of trap frame
  152. mov ecx,(TsHardwareSegSs - TsSegFs + 4) / 4; set repeat count
  153. lea edi,[esp].TsSegFs ; set destination address
  154. lea esi,[edx].TsSegFs ; set source address
  155. rep movsd ; copy trap information
  156. test byte ptr [ebx]+ThDebugActive, -1 ; Do we need to restore Debug reg?
  157. jnz short Kcb18 ; Yes, go save them.
  158. Kcb15: mov eax,_KeUserCallbackDispatcher ; st address of callback dispatchr
  159. mov [esp].TsEip,eax ;
  160. mov eax,PCR[PcExceptionList] ; get current exception list
  161. mov [esp].TsExceptionList,eax ; set previous exception list
  162. mov eax,[edx].TsPreviousPreviousMode ; get previous mode
  163. mov [esp].TsPreviousPreviousMode,eax ; set previous mode
  164. sti ; enable interrupts
  165. SET_DEBUG_DATA ; set system call debug data for exit
  166. jmp _KiServiceExit ; exit through service dispatch
  167. Kcb18:
  168. mov ecx,(TsDr7 - TsDr0 + 4) / 4; set repeat count
  169. lea edi,[esp].TsDr0 ; set destination address
  170. lea esi,[edx].TsDr0 ; set source address
  171. rep movsd ; copy trap information
  172. jmp short Kcb15
  173. ;
  174. ; An attempt to grow the kernel stack failed.
  175. ;
  176. Kcb20: pop edi ; restore nonvolitile register
  177. pop esi ;
  178. pop ebx ;
  179. pop ebp ;
  180. stdRET _KiCallUserMode
  181. stdENDP _KiCallUserMode
  182. page ,132
  183. subttl "Switch Kernel Stack"
  184. ;++
  185. ;
  186. ; PVOID
  187. ; KeSwitchKernelStack (
  188. ; IN PVOID StackBase,
  189. ; IN PVOID StackLimit
  190. ; )
  191. ;
  192. ; Routine Description:
  193. ;
  194. ; This function switches to the specified large kernel stack.
  195. ;
  196. ; N.B. This function can ONLY be called when there are no variables
  197. ; in the stack that refer to other variables in the stack, i.e.,
  198. ; there are no pointers into the stack.
  199. ;
  200. ; Arguments:
  201. ;
  202. ; StackBase (esp + 4) - Supplies a pointer to the base of the new kernel
  203. ; stack.
  204. ;
  205. ; StackLimit (esp + 8) - Suplies a pointer to the limit of the new kernel
  206. ; stack.
  207. ;
  208. ; Return Value:
  209. ;
  210. ; The old kernel stack is returned as the function value.
  211. ;
  212. ;--
  213. SsStkBs equ 4 ; new kernel stack base address
  214. SsStkLm equ 8 ; new kernel stack limit address
  215. cPublicProc _KeSwitchKernelStack, 2
  216. ;
  217. ; Save the address of the new stack and copy the old stack to the new
  218. ; stack.
  219. ;
  220. push esi ; save string move registers
  221. push edi ;
  222. mov edx,PCR[PcPrcbData + PbCurrentThread] ; get current thread address
  223. mov edi,[esp]+SsStkBs + 8 ; get new kernel stack base address
  224. mov ecx,[edx].ThStackBase ; get current stack base address
  225. sub ebp,ecx ; relocate the callers frame pointer
  226. add ebp,edi ;
  227. mov eax,[edx].ThTrapFrame ; relocate the current trap frame address
  228. sub eax,ecx ;
  229. add eax,edi ;
  230. mov [edx].ThTrapFrame,eax ;
  231. sub ecx,esp ; compute length of copy
  232. sub edi,ecx ; set destination address of copy
  233. mov esi,esp ; set source address of copy
  234. push edi ; save new stack pointer address
  235. rep movsb ; copy old stack to new stack
  236. pop edi ; restore new stack pointer address
  237. ;
  238. ; Switch to the new kernel stack and return the address of the old kernel
  239. ; stack.
  240. ;
  241. mov eax,[edx].ThStackBase ; get old kernel stack base address
  242. mov ecx,[esp]+SsStkBs + 8 ; get new kernel stack base address
  243. mov esi,[esp]+SsStkLm + 8 ; get new kernel stack limit address
  244. cli ; disable interrupts
  245. mov [edx].ThStackBase,ecx ; set new kernel stack base address
  246. mov [edx].ThStackLimit,esi ; set new kernel stack limit address
  247. mov byte ptr [edx].ThLargeStack, 1 ; set large stack TRUE
  248. mov [edx].ThInitialStack,ecx ; set new initial stack address
  249. sub ecx,NPX_FRAME_lENGTH ; compute NPX save area address
  250. mov PCR[PcInitialStack],ecx ; set stack check base address
  251. mov PCR[PcStackLimit],esi ; set stack check limit address
  252. mov edx,PCR[PcTss] ; get address of task switch segment
  253. sub ecx,TsV86Gs - TsHardwareSegSs ; bias for missing V86 fields
  254. mov [edx].TssEsp0,ecx ; set kernel entry stack address
  255. mov esp,edi ; set new stack pointer address
  256. sti ;
  257. pop edi ; restore string move registers
  258. pop esi ;
  259. stdRET _KeSwitchKernelStack
  260. stdENDP _KeSwitchKernelStack
  261. page ,132
  262. subttl "Get User Mode Stack Address"
  263. ;++
  264. ;
  265. ; PULONG
  266. ; KiGetUserModeStackAddress (
  267. ; VOID
  268. ; )
  269. ;
  270. ; Routine Description:
  271. ;
  272. ; This function returns the address of the user stack address in the
  273. ; current trap frame.
  274. ;
  275. ; Arguments:
  276. ;
  277. ; None.
  278. ;
  279. ; Return Value:
  280. ;
  281. ; The address of the user stack address.
  282. ;
  283. ;--
  284. cPublicProc _KiGetUserModeStackAddress, 0
  285. mov eax,PCR[PcPrcbData + PbCurrentThread] ; get current thread address
  286. mov eax,[eax].ThTrapFrame ; get current trap frame address
  287. lea eax,[eax].TsHardwareEsp ; get address of stack address
  288. stdRET _KiGetUserModeStackAddress
  289. stdENDP _KiGetUserModeStackAddress
  290. page ,132
  291. subttl "Return from User Mode Callback"
  292. ;++
  293. ;
  294. ; NTSTATUS
  295. ; NtCallbackReturn (
  296. ; IN PVOID OutputBuffer OPTIONAL,
  297. ; IN ULONG OutputLength,
  298. ; IN NTSTATUS Status
  299. ; )
  300. ;
  301. ; Routine Description:
  302. ;
  303. ; This function returns from a user mode callout to the kernel
  304. ; mode caller of the user mode callback function.
  305. ;
  306. ; N.B. This function returns to the function that called out to user
  307. ; mode and the KiCallUserMode function calls out to user mode.
  308. ; Therefore, the stack layout must be consistent between the
  309. ; two routines.
  310. ;
  311. ; Arguments:
  312. ;
  313. ; OutputBuffer - Supplies an optional pointer to an output buffer.
  314. ;
  315. ; OutputLength - Supplies the length of the output buffer.
  316. ;
  317. ; Status - Supplies the status value returned to the caller of the
  318. ; callback function.
  319. ;
  320. ; Return Value:
  321. ;
  322. ; If the callback return cannot be executed, then an error status is
  323. ; returned. Otherwise, the specified callback status is returned to
  324. ; the caller of the callback function.
  325. ;
  326. ; N.B. This function returns to the function that called out to user
  327. ; mode is a callout is currently active.
  328. ;
  329. ;--
  330. cPublicProc _NtCallbackReturn, 3
  331. mov eax,PCR[PcPrcbData + PbCurrentThread] ; get current thread address
  332. mov ecx,[eax].ThCallbackStack ; get callback stack address
  333. cmp ecx, 0
  334. je CbExit ; if zero, no callback stack present
  335. ;
  336. ; Restore the current exception list from the saved exception list in the
  337. ; current trap frame, restore the trap frame and callback stack addresses,
  338. ; store the output buffer address and length, and set the service status.
  339. ;
  340. mov ebx,[eax].ThTrapFrame ; get current trap frame address
  341. mov edx,[ebx].TsExceptionList ; get saved exception list address
  342. mov PCR[PcExceptionList],edx ; restore exception list address
  343. mov edi,[esp] + 4 ; get output buffer address
  344. mov esi,[esp] + 8 ; get output buffer length
  345. mov ebp,[esp] + 12 ; get callout service status
  346. mov ebx,[ecx].CuOutBf ; get address to store output buffer
  347. mov [ebx],edi ; store output buffer address
  348. mov ebx,[ecx].CuOutLn ; get address to store output length
  349. mov [ebx],esi ; store output buffer length
  350. mov ebx,[ecx] ; get previous initial stack address
  351. cli ; disable interrupt
  352. mov esi,PCR[PcInitialStack] ; get source NPX save area address
  353. mov [eax].ThInitialStack,ebx ; restore initial stack address
  354. sub ebx,NPX_FRAME_LENGTH ; compute destination NPX save area
  355. mov edx,[esi].FpControlWord ; copy NPX state to previous frame
  356. mov [ebx].FpControlWord,edx ;
  357. mov edx,[esi].FpStatusWord ;
  358. mov [ebx].FpStatusWord,edx ;
  359. mov edx,[esi].FpTagWord ;
  360. mov [ebx].FpTagWord,edx ;
  361. mov edx,[esi].FxMXCsr ;
  362. mov [ebx].FxMXCsr,edx ;
  363. mov edx,[esi].FpCr0NpxState ;
  364. mov [ebx].FpCr0NpxState,edx ;
  365. mov edx,PCR[PcTss] ; get address of task switch segment
  366. mov PCR[PcInitialStack],ebx ; restore stack check base address
  367. sub ebx,TsV86Gs - TsHardwareSegSs ; bias for missing V86 fields
  368. mov [edx].TssEsp0,ebx ; restore kernel entry stack address
  369. mov esp,ecx ; trim stack back to callback frame
  370. add esp,4 ;
  371. sti ; enable interrupts
  372. pop [eax].ThTrapFrame ; restore current trap frame address
  373. pop [eax].ThCallbackStack ; restore callback stack address
  374. mov eax,ebp ; set callback service status
  375. ;
  376. ; Restore nonvolatile registers, clean call parameters from stack, and
  377. ; return to callback caller.
  378. ;
  379. pop edi ; restore nonvolatile registers
  380. pop esi ;
  381. pop ebx ;
  382. pop ebp ;
  383. pop edx ; save return address
  384. add esp,8 ; remove parameters from stack
  385. jmp edx ; return to callback caller
  386. ;
  387. ; No callback is currently active.
  388. ;
  389. CbExit: mov eax,STATUS_NO_CALLBACK_ACTIVE ; set service status
  390. stdRET _NtCallBackReturn
  391. stdENDP _NtCallbackReturn
  392. _TEXT ends
  393. end