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.

655 lines
15 KiB

  1. title SCHEDULE - task scheduler
  2. .xlist
  3. include kernel.inc
  4. include tdb.inc
  5. include newexe.inc
  6. .list
  7. .386
  8. externFP WriteOutProfiles
  9. externFP GlobalCompact
  10. externFP GetFreeSpace
  11. ;if KDEBUG
  12. ;externFP OutputDebugString
  13. ;endif
  14. DataBegin
  15. externB Kernel_InDOS
  16. externB Kernel_flags
  17. externB InScheduler
  18. externB fProfileDirty
  19. externB fProfileMaybeStale
  20. externB fPokeAtSegments
  21. externW WinFlags
  22. ;externW EMSCurPID
  23. ;externW cur_drive_owner
  24. externW curTDB
  25. externW Win_PDB
  26. externW LockTDB
  27. externW headTDB
  28. externW hShell
  29. externW pGlobalHeap
  30. externW hExeHead
  31. externW f8087
  32. externW
  33. externW PagingFlags
  34. externD pDisplayCritSec
  35. externD pIsUserIdle
  36. if KDEBUG
  37. externB fPreloadSeg
  38. endif
  39. staticW PokeCount,0
  40. DataEnd
  41. sBegin CODE
  42. assumes cs,CODE
  43. assumes ds,NOTHING
  44. assumes es,NOTHING
  45. externNP LoadSegment
  46. externNP DeleteTask
  47. externNP InsertTask
  48. externNP DiscardFreeBlocks
  49. externNP ShrinkHeap
  50. if SDEBUG
  51. externNP DebugSwitchOut
  52. externNP DebugSwitchIn
  53. endif
  54. ;-----------------------------------------------------------------------;
  55. ; Reschedule ;
  56. ; ;
  57. ; This routine does the task switching. ;
  58. ; ;
  59. ; Arguments: ;
  60. ; none ;
  61. ; Returns: ;
  62. ; nothing ;
  63. ; Error Returns: ;
  64. ; nothnig ;
  65. ; Registers Preserved: ;
  66. ; AX,BX,CX,DX,DI,SI,BP,DS,ES ;
  67. ; Registers Destroyed: ;
  68. ; none ;
  69. ; Calls: ;
  70. ; DeleteTask ;
  71. ; InsertTask ;
  72. ; SaveState ;
  73. ; ;
  74. ; History: ;
  75. ; ;
  76. ; Mon 07-Aug-1989 21:53:42 -by- David N. Weise [davidw] ;
  77. ; Removed the WinOldApp support and DEC rainbow support. ;
  78. ; ;
  79. ; Fri 07-Apr-1989 22:16:02 -by- David N. Weise [davidw] ;
  80. ; Added support for task ExeHeaders above The Line in Large ;
  81. ; Frame EMS. ;
  82. ; ;
  83. ; Sat Aug 15, 1987 11:41:35p -by- David N. Weise [davidw] ;
  84. ; Commented out the cli and sti around the swapping of states. ;
  85. ; Sweet Lord, what will this break? ;
  86. ; ;
  87. ; Fri Feb 06, 1987 00:09:20a -by- David N. Weise [davidw] ;
  88. ; Put in support for DirectedYield. ;
  89. ; ;
  90. ; Tue Feb 03, 1987 08:21:53p -by- David N. Weise [davidw] ;
  91. ; Got rid of going inside of DOS for InDOS and ErrorMode. Task ;
  92. ; switching should be much better under Windows386 now. ;
  93. ; ;
  94. ; Mon Sep 29, 1986 05:27:29p -by- Charles Whitmer [chuckwh] ;
  95. ; Made it recognize threads. It now does a fast context switch if ;
  96. ; just switching between two threads in the same process. ;
  97. ; ;
  98. ; Mon Sep 29, 1986 05:24:16p -by- Charles Whitmer [chuckwh] ;
  99. ; Documented it. ;
  100. ;-----------------------------------------------------------------------;
  101. assumes ds, nothing
  102. assumes es, nothing
  103. cProc Reschedule,<PUBLIC,FAR>
  104. cBegin nogen
  105. ; get all the registers up on the stack
  106. inc bp
  107. push bp
  108. mov bp,sp
  109. push ds
  110. push si
  111. push di
  112. push ax
  113. push cx
  114. push es
  115. push bx
  116. push dx
  117. public BootSchedule
  118. BootSchedule:
  119. call TaskSwitchProfileUpdate ;Update profile information
  120. public ExitSchedule
  121. ExitSchedule:
  122. ; see if we're supposed to do a directed yield
  123. SetKernelDS es
  124. mov cx,curTDB
  125. jcxz search_the_queue ; this happens first after boot time!
  126. mov ds,cx
  127. cmp ds:[TDB_sig],TDB_SIGNATURE
  128. jnz short search_the_queue ; task may have suicided
  129. xor cx,cx
  130. xchg ds:[TDB_Yield_to],cx
  131. jcxz search_the_queue ; nope
  132. mov ds,cx
  133. cmp ds:[TDB_nevents],0 ; anything for this guy?
  134. jnz found_one
  135. ; run down the task queue looking for someone with events
  136. search_the_queue:
  137. CheckKernelDS es
  138. mov ax,HeadTDB
  139. keep_searching:
  140. or ax,ax ; Anyone to schedule?
  141. jnz short try_this_one ; Yes, go do it
  142. ; if no one is dispatchable, do an idle interrupt
  143. xor PagingFlags, 4
  144. test PagingFlags, 4
  145. jnz short NoDiscarding ; Every other time through!
  146. test PagingFlags, 8 ; An app has exited?
  147. jz short NoShrink
  148. push es
  149. call ShrinkHeap
  150. pop es
  151. NoShrink:
  152. ;;; or PagingFlags, 2 ; Causes early exit from GlobalCompact
  153. ;;; mov ax, -1
  154. ;;; push es
  155. ;;; cCall GlobalCompact,<ax,ax>
  156. ;;; pop es
  157. ;;; and PagingFlags, not 2
  158. test byte ptr WinFlags[1], WF1_PAGING
  159. jz short NoDiscarding
  160. test PagingFlags, 1
  161. jz short NoDiscarding
  162. call DiscardFreeBlocks
  163. jmps search_the_queue
  164. NoDiscarding:
  165. mov ax, 1 ; Assume User is idle
  166. cmp word ptr pIsUserIdle[2], 0
  167. je short go_idle
  168. call pIsUserIdle
  169. SetKernelDS es
  170. go_idle:
  171. cmp fPokeAtSegments, 0 ; Disabled?
  172. je short @F ; yep, skip
  173. or ax, ax ; Only if USER is idle
  174. jz short @F
  175. inc PokeCount
  176. test PokeCount, 1Fh ; Only every 32 times through
  177. jnz short @F
  178. call PokeAtSegments
  179. jc search_the_queue
  180. @@:
  181. push ax ; Ralph's paranoia
  182. int 28h ; No, generate DOS idle int
  183. pop ax
  184. xor bx, bx
  185. or ax, ax
  186. jnz short tell_ralph
  187. or bl, 1
  188. tell_ralph:
  189. mov ax,1689h ; Do an idle to Win386.
  190. int 2fh
  191. jmp search_the_queue
  192. get_out_fast:
  193. jmp reschedule_done
  194. ; see if the given task has events
  195. try_this_one:
  196. mov ds,ax
  197. mov ax,ds:[TDB_next]
  198. cmp ds:[TDB_nevents],0 ; anything for this guy?
  199. jz keep_searching
  200. ; is this the guy we're already running?
  201. found_one:
  202. mov di,ds ; DI = handle of new task
  203. cmp di,curTDB
  204. jz short get_out_fast
  205. ; is there a locked task?
  206. mov cx,LockTDB ; are any tasks super priority?
  207. jcxz no_locked_task
  208. cmp cx,di ; are we it?
  209. jnz short get_out_fast ; No => don't switch
  210. no_locked_task:
  211. ; don't switch if in DOS or int 24 handler
  212. cmp Kernel_InDOS,0 ; if inside INT2[0,4] handler
  213. jnz keep_searching ; ...don't reschedule
  214. inc InScheduler ; set flag for INT 24...
  215. inc ds:[TDB_priority] ; lower task priority
  216. push es
  217. UnSetKernelDS es
  218. cCall DeleteTask,<ds> ; remove him from queue
  219. cCall InsertTask,<ds> ; put him back further down
  220. dec ds:[TDB_priority] ; restore former priority
  221. pop es
  222. ReSetKernelDS es
  223. ; Around saving state and restoring state go critical on memory
  224. ; heap access because of EMS.
  225. push es
  226. mov es,pGlobalHeap
  227. UnSetKernelDS es
  228. inc es:[gi_lrulock]
  229. pop es
  230. ReSetKernelDS es
  231. ; Signature is trashed when we suicide so we dont save state
  232. ; for the non-existant task.
  233. mov di,ds ; DI = destination task
  234. xor si,si ; SI is an argument to RestoreState
  235. mov es,curTDB ; ES = current TDB
  236. UnSetKernelDS es
  237. mov ax,es
  238. or ax,ax
  239. jz short dont_save_stack
  240. cmp es:[TDB_sig],TDB_SIGNATURE
  241. jnz short dont_save_stack
  242. mov si,es ; SI = current task
  243. ; save the present stack
  244. mov es:[TDB_taskSS],ss
  245. mov es:[TDB_taskSP],sp
  246. dont_save_stack:
  247. ; get onto a temporary stack while we switch the EEMS memory
  248. mov ax,es
  249. or ax,ax
  250. jz short dont_save_state
  251. cmp es:[TDB_sig],TDB_SIGNATURE
  252. jnz short dont_save_state
  253. cCall SaveState,<si>
  254. push ds
  255. mov ds,ax
  256. if SDEBUG
  257. call DebugSwitchOut
  258. endif
  259. pop ds
  260. dont_save_state:
  261. SetKernelDS es
  262. mov curTDB,di
  263. mov ax, ds:[TDB_PDB] ; Set our idea of the PDB
  264. mov Win_PDB, ax
  265. ;;;mov ax,ds:[TDB_EMSPID]
  266. ;;;mov EMSCurPID,ax
  267. cmp f8087, 0
  268. je short no_fldcw
  269. .8087
  270. fnclex
  271. fldcw ds:[TDB_FCW]
  272. no_fldcw:
  273. or Kernel_flags,kf_restore_CtrlC OR kf_restore_disk
  274. if SDEBUG
  275. call DebugSwitchIn
  276. endif
  277. fast_switch:
  278. mov cx,ds:[TDB_taskSS] ; Switch to new task stack.
  279. mov ax,ds:[TDB_taskSP]
  280. mov ss,cx
  281. mov sp,ax
  282. mov curTDB,di
  283. dec InScheduler ; reset flag for INT 24
  284. mov al,Kernel_Flags[2]
  285. and Kernel_Flags[2],NOT KF2_WIN386CRAZINESS
  286. ; Tell the display driver to speak its mind. added 20 feb 1990
  287. test al,KF2_WIN386CRAZINESS
  288. jz short @F
  289. xor ax,ax
  290. push es ; preserve es
  291. cCall pDisplayCritSec,<ax>
  292. pop es
  293. @@:
  294. ; Around saving state and restoring state go critical on memory
  295. ; arena access because of EMS.
  296. mov es,pGlobalHeap
  297. UnSetKernelDS es
  298. dec es:[gi_lrulock]
  299. ; pop the task's registers off the stack
  300. reschedule_done:
  301. pop dx
  302. pop bx
  303. pop es
  304. pop cx
  305. pop ax
  306. pop di
  307. pop si
  308. pop ds
  309. pop bp
  310. dec bp
  311. public dispatch
  312. dispatch:
  313. ret
  314. cEnd nogen
  315. ;-----------------------------------------------------------------------;
  316. ; LockCurrentTask ;
  317. ; ;
  318. ; Hack procedure to make the current task god if the passed boolean ;
  319. ; is true. ;
  320. ; If false, then demotes the current god to a mere mortal. Self ;
  321. ; inflicted by definition. ;
  322. ; ;
  323. ; DavidDS: Note, the USER function, LockMyTask should be called for ;
  324. ; Windows apps instead of this if you expect the input queue to work ;
  325. ; properly. ;
  326. ; ;
  327. ; Arguments: ;
  328. ; ParmW lock ; ;
  329. ; Returns: ;
  330. ; ;
  331. ; Error Returns: ;
  332. ; ;
  333. ; Registers Preserved: ;
  334. ; CX,DX,DI,SI,DS,ES ;
  335. ; ;
  336. ; Registers Destroyed: ;
  337. ; AX,BX ;
  338. ; ;
  339. ; Calls: ;
  340. ; ;
  341. ; History: ;
  342. ; ;
  343. ; Sun Jan 04, 1987 04:37:11p -by- David N. Weise [davidw] ;
  344. ; Added this nifty comment block. ;
  345. ;-----------------------------------------------------------------------;
  346. assumes ds, nothing
  347. assumes es, nothing
  348. cProc LockCurrentTask,<PUBLIC,FAR>
  349. ; ParmW lock
  350. cBegin nogen ; Crock to save space
  351. mov bx,sp
  352. mov ax,ss:[bx][4] ; get the argument
  353. push ds
  354. SetKernelDS
  355. or ax,ax
  356. jz short lct1
  357. mov ax,curTDB
  358. lct1:
  359. mov LockTDB,ax
  360. pop ds
  361. UnSetKernelDS
  362. ret 2
  363. cEnd nogen
  364. ;-----------------------------------------------------------------------;
  365. ; IsTaskLocked ;
  366. ; ;
  367. ; Another hack procedure to determine if the current task is locked. ;
  368. ; A non-NULL value is returned if the task is locked and NULL is ;
  369. ; returned is the task is not locked. ;
  370. ; ;
  371. ; Arguments: ;
  372. ; ; ;
  373. ; Returns: ;
  374. ; The value of LockTDB ;
  375. ; Error Returns: ;
  376. ; ;
  377. ; Registers Preserved: ;
  378. ; All but AX ;
  379. ; ;
  380. ; Registers Destroyed: ;
  381. ; AX ;
  382. ; ;
  383. ; Calls: ;
  384. ; ;
  385. ; History: ;
  386. ; ;
  387. ; (Tue 20-Oct-1987 : bobgu) Created this thing. ;
  388. ;-----------------------------------------------------------------------;
  389. assumes ds, nothing
  390. assumes es, nothing
  391. cProc IsTaskLocked,<PUBLIC,FAR>
  392. cBegin nogen
  393. push ds
  394. SetKernelDS
  395. mov ax,LockTDB
  396. pop ds
  397. UnSetKernelDS
  398. ret
  399. cEnd nogen
  400. ;-----------------------------------------------------------------------;
  401. ; SaveState ;
  402. ; ;
  403. ; Saves the state of the current MS-DOS process. This means the per ;
  404. ; task interrupt vectors, the drive and directory, EEMS land if any, ;
  405. ; and old app stuff if any. ;
  406. ; ;
  407. ; Arguments: ;
  408. ; parmW destination ;
  409. ; ;
  410. ; Returns: ;
  411. ; DS returned in AX. ;
  412. ; ;
  413. ; Error Returns: ;
  414. ; ;
  415. ; Registers Preserved: ;
  416. ; ;
  417. ; Registers Destroyed: ;
  418. ; ;
  419. ; Calls: ;
  420. ; ;
  421. ; History: ;
  422. ; ;
  423. ; Mon 07-Aug-1989 21:53:42 -by- David N. Weise [davidw] ;
  424. ; Removed the WinOldApp support. ;
  425. ; ;
  426. ; Tue Feb 03, 1987 08:21:53p -by- David N. Weise [davidw] ;
  427. ; Got rid of the rest of the DOS version dependencies. ;
  428. ; ;
  429. ; Thu Jan 22, 1987 03:15:15a -by- David N. Weise [davidw] ;
  430. ; Took out the saving of the ^C state, DTA address, and ErrorMode. ;
  431. ; ;
  432. ; Sun Jan 04, 1987 04:40:44p -by- David N. Weise [davidw] ;
  433. ; Added this nifty comment block. ;
  434. ;-----------------------------------------------------------------------;
  435. assumes ds, nothing
  436. assumes es, nothing
  437. cProc SaveState,<PUBLIC,NEAR>,<si,di,ds>
  438. parmW destination
  439. cBegin
  440. cld
  441. SetKernelDS
  442. mov ax,f8087
  443. UnSetKernelDS
  444. mov ds,destination
  445. or ax,ax
  446. jz short no_fstcw
  447. .8087
  448. fstcw ds:[TDB_FCW]
  449. no_fstcw:
  450. test ds:[TDB_Drive],10000000b; if hi bit set....
  451. jnz short ss_ret ; ...no need to get dir
  452. mov ah,19h
  453. int 21h
  454. mov dl,al
  455. inc dl
  456. or al,10000000b
  457. mov ds:[TDB_Drive],al ; save it (A=0, B=1, etc.)
  458. mov si,TDB_Directory
  459. mov byte ptr [si],'\' ; set "\"
  460. inc si
  461. mov ah,47h ; get current directory...
  462. int 21h
  463. jnc short ss_ret
  464. mov byte ptr [si-1],0 ; indicate error with null byte
  465. ss_ret: mov ax,ds
  466. cEnd
  467. cProc TaskSwitchProfileUpdate,<PUBLIC,NEAR>
  468. cBegin
  469. SetKernelDS es
  470. ;** See if we need to write out the profile string cache. We
  471. ;** do this at task switch time.
  472. cmp es:fProfileDirty, 0
  473. je short PU_NoFlushProfiles
  474. push es
  475. call WriteOutProfiles
  476. pop es
  477. PU_NoFlushProfiles:
  478. ;** Set the flag saying that the profile cache MAY be invalid.
  479. ;** If a task switch occurs, on the next Get/SetProfileXXX() we
  480. ;** will have to see if the file has been modified by this
  481. ;** (or any other) task
  482. mov es:fProfileMaybeStale,1
  483. cEnd
  484. assumes ds, nothing
  485. assumes es, nothing
  486. .386
  487. cProc PokeAtSegments,<PUBLIC,NEAR>
  488. cBegin
  489. push ds
  490. push es
  491. pusha
  492. cCall GetFreeSpace,<2> ; Ignore discardable
  493. or dx, dx ; Insist on > 64k free
  494. if KDEBUG
  495. jz never_again
  496. else
  497. jz short never_again
  498. endif
  499. SetKernelDS
  500. test WinFlags[1], WF1_PAGING
  501. jz short have_room
  502. sub sp, 30h ; Room for info
  503. smov es, ss
  504. mov di, sp
  505. mov ax, 0500h ; Get Paging Info
  506. int 31h
  507. mov eax, es:[di][14h] ; Free pages
  508. add sp, 30h
  509. cmp eax, 16 ; Insist on 64k
  510. jb short never_again
  511. have_room:
  512. smov es, ds
  513. ResetKernelDS es
  514. mov ds, hExehead
  515. UnsetKernelDS
  516. next_exe:
  517. mov cx, ds:[ne_cseg] ; Module has segments?
  518. jcxz no_segs ; no, on to next module
  519. mov di, 1 ; Segment number
  520. mov si, ds:[ne_segtab] ; Pointer to segment table
  521. next_seg:
  522. test ds:[si].ns_flags, NSLOADED ; This segment in memory?
  523. jnz short seg_here ; yes, look at next one
  524. test ds:[si].ns_flags, NSDISCARD ; Only load if discardable
  525. jz short seg_here ; Skip this one
  526. if KDEBUG
  527. push ds
  528. SetKernelDS
  529. mov fPreloadSeg, 1
  530. pop ds
  531. UnsetKernelDS
  532. endif
  533. cCall LoadSegment,<ds,di,-1,-1>
  534. if KDEBUG
  535. push ds
  536. SetKernelDS
  537. mov fPreloadSeg, 0
  538. pop ds
  539. UnsetKernelDS
  540. endif
  541. stc ; We loaded something!
  542. jmps all_done
  543. seg_here:
  544. lea si, [si + size NEW_SEG1] ; On to next segment
  545. inc di
  546. loop next_seg ; looked at all in this module?
  547. no_segs:
  548. mov cx, ds:[ne_pnextexe] ; On to next module
  549. jcxz all_done ; End of list?
  550. mov ax, ds
  551. mov ds, cx
  552. cmp ax, hShell ; Only boot time modules
  553. jne short next_exe
  554. UnSetKernelDS es
  555. never_again:
  556. SetKernelDS
  557. mov fPokeAtSegments, 0 ; That's all folks
  558. clc
  559. all_done:
  560. popa
  561. pop es
  562. pop ds
  563. cEnd
  564. sEnd CODE
  565. end