Part of Slepp's ProjectsPastebinTURLImagebinFilebin
Feedback -- English French German Japanese
Create Upload Newest Tools Donate
Sign In | Create Account

Paste Description for Task Switcher

The Task Switcher of the ToasterOS written by Toaster.

Task Switcher
Sunday, July 23rd, 2006 at 1:46:14am MDT 

  1. ; **********************************************************
  2.  
  3. ;       Name: Task Switcher
  4. ;       Autor: Toaster Burger
  5. ;       Version: 1.00
  6. ;       Date: 26.11.2005
  7. ;       last Update: 26.11.2005
  8. ;       see document: ToasterOS.pdf
  9.  
  10. ; **********************************************************
  11.  
  12. [bits 32]
  13. CPU 386
  14.  
  15. %define Type_System
  16. %include "interface.asm"
  17.  
  18. %define Handle_Size      6
  19.  
  20. org Task_Switcher
  21.  
  22.  
  23. jmp dword Enable_Scheduler
  24.  
  25. jmp dword Create_Task
  26. jmp dword Delete_Task
  27. jmp dword Destroy_Task
  28.  
  29. jmp dword Check_Task_Handle
  30.  
  31. jmp dword Dispatcher
  32.  
  33. jmp dword Finite
  34.  
  35. jmp dword Create_Task_System
  36.  
  37. jmp dword Switch_Task
  38.  
  39. jmp dword Get_Current_Process
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. Enable_Scheduler:                                   ; enable (start) the Scheduler
  49.  
  50. ; API Enable_Scheduler
  51.  
  52. Enter_System_Environment
  53.  
  54.  
  55. ; command CPUID available (test bit 21 of eflags to set) ?
  56. pushfd
  57. or [esp],dword 1000000000000000000000b
  58. popfd
  59. pushfd
  60. pop eax
  61. test eax,1000000000000000000000b
  62. jz No_FPU_MMX_SSE2_support
  63.  
  64. ; check if the FPU, MMX, SSE (re)store command is available
  65. mov eax,1
  66. CPU PentiumPro
  67. cpuid
  68. CPU 386
  69. test edx,1000000000000000000000000b
  70. jz No_FPU_MMX_SSE2_support
  71. jmp Write_Task_Table
  72.  
  73.  
  74. No_FPU_MMX_SSE2_support:
  75. ; if here the fxsave/fxrstor opcodes are not supported, so delete them
  76. mov [Store_FPU_MMX_SSE_state],dword 90909090h
  77. mov [Store_FPU_MMX_SSE_state+4],word 9090h
  78. mov [Store_FPU_MMX_SSE_state+6],byte 90h
  79. mov [Restore_FPU_MMX_SSE_state],dword 90909090h
  80. mov [Restore_FPU_MMX_SSE_state+4],word 9090h
  81. mov [Restore_FPU_MMX_SSE_state+6],byte 90h
  82.  
  83.  
  84. Write_Task_Table:
  85. mov [Current_Task],dword Task_Table
  86. xor ecx,ecx                              ; loop counter = 65536
  87. xor eax,eax
  88. mov edi,Task_Table
  89.  
  90. rep stosw                                          ; erase all words
  91. rep stosd                                          ; erase all dwords
  92.  
  93. Leave_System_Environment
  94.  
  95. ret
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. Create_Task_System:
  104.  
  105. ; API Create_Task_System, Handle, Process, Stack, Address, Data, Size
  106.  
  107. ; Handle = Process Handle (of the caller's one)
  108. ; Process = Process Handle of the Process to start the Task
  109. ; Stack = start stack (esp) of the new task (the value stack is from high to low)
  110. ; Address = start address (eip) of the new task
  111. ; Size = bytes to copy from Data (source) to the new Tasks stack (destination)
  112.  
  113. ; NOTES:
  114. ; The Data is bytewise copied to the stack from low to high, and the new Task's
  115. ; stack pointer will point BEFORE the data.
  116. ; This function adds to the data the Task Handle, so esp+0 points to the Handle
  117. ; and esp+4 to the user sepcific data.
  118. ; The caller won't get the Handle.
  119.  
  120. Enter_System_Environment
  121.  
  122.  
  123. ; set the correct Stack Pointer
  124. mov eax,[Param6]
  125. sub [Param3],eax
  126.  
  127.  
  128.  
  129. ; search a free Task Handle
  130. mov esi,Task_Table
  131. xor ecx,ecx                              ; loop counter = 65536
  132. Enter_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (enter)
  133.  
  134. Search_free_System_Task:
  135. add esi,02h
  136.  
  137. lodsd
  138. or eax,eax
  139. jz Task_System_found
  140. loop Search_free_System_Task
  141.  
  142. ; if here, there is no free Task Handle
  143. Leave_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (leave)
  144. stc
  145. mov eax,No_free_Task
  146. jmp Create_Task_System_Exit
  147.  
  148.  
  149. Task_System_found:                              ; if comes here, a free Task Handle is found
  150. Leave_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (leave)
  151. lea edi,[esi-06h]
  152.  
  153. ; store the Process Handle
  154. mov ax,[Param2]
  155. stosw
  156.  
  157. ; store the Stack Pointer
  158. mov eax,[Param3]
  159. sub eax,12+(4*5)+32+108
  160. stosd
  161.  
  162.  
  163.  
  164. ; set the stack for the new process on the current stack
  165. ; push following values in the correct direction:
  166. ;     gs, fs, es, ds, ad, FPU, iret, Handle (4, 4, 4, 4, 32, 108, 12, 4)
  167.  
  168. sub esp,12+(4*5)+32+108
  169. mov edi,esp
  170. mov ebx,esp
  171.  
  172. ; set gs, fs, es, ds
  173. xor eax,eax
  174. stosd
  175. stosd
  176. mov eax,Data_Selector
  177. stosd
  178. stosd
  179.  
  180. ; set edi, esi, ebp & esp (all points to the start of stack)
  181. mov eax,[Param3]
  182. stosd
  183. stosd
  184. stosd
  185. stosd
  186.  
  187. ; xor eax, ebx, ecx & edx
  188. xor eax,eax
  189. stosd
  190. stosd
  191. stosd
  192. stosd
  193.  
  194. ; FPU registers (use current)
  195. fsave [edi]
  196. add edi,108
  197.  
  198. ; store the iret (eip, cs, flags)
  199. mov eax,[Param4]
  200. stosd
  201. mov eax,Code_Selector
  202. stosd
  203. mov eax,User_EFLAG
  204. stosd
  205.  
  206. ; store the Task Handle
  207. xor eax,eax
  208. sub eax,ecx                              ; 65536 - loop counter
  209. stosd
  210.  
  211.  
  212. ; check whether the data is to copy (or not)
  213. cmp [Param6],dword 0
  214. je Copy_System_finished
  215.  
  216. ; copy the data
  217. API Copy_Process_Data, [Param1], [Param2], [Param5], [Param3], [Param6]
  218. jc Create_Task_System_Exit
  219.  
  220. Copy_System_finished:
  221.  
  222.  
  223. ; copy the (system) stack
  224. sub [Param3],dword (12+(4*5)+32+108)
  225. API Copy_Process_Data, [Param1], [Param2], ebx, [Param3], (12+(4*5)+32+108)
  226.  
  227.  
  228.  
  229. ; reset the stack (delete temporary other process stack)
  230. add esp,12+(4*5)+32+108
  231.  
  232.  
  233. Create_Task_System_Exit:
  234. Leave_System_Environment
  235.  
  236. ret
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244. Create_Task:
  245.  
  246. ; API Create_Task, Handle, Process, Stack, Address, Data, Size
  247.  
  248. ; Handle = Process Handle (of the caller's one)
  249. ; Process = Process Handle of the Process to start the Task
  250. ; Stack = start stack (esp) of the new task (the value stack is from high to low)
  251. ; Address = start address (eip) of the new task
  252. ; Size = bytes to copy from Data (source) to the new Tasks stack (destination)
  253.  
  254. ; NOTES:
  255. ; The Data is bytewise copied to the stack from low to high, and the new Task's
  256. ; stack pointer will point BEFORE the data.
  257. ; This function adds to the data the Task Handle, so esp+0 points to the Handle
  258. ; and esp+4 to the user sepcific data.
  259. ; The caller won't get the Handle.
  260.  
  261. Enter_System_Environment
  262.  
  263.  
  264. ; set the correct Stack Pointer
  265. mov eax,[Param6]
  266. sub [Param3],eax
  267.  
  268.  
  269.  
  270. ; search a free Task Handle
  271. mov esi,Task_Table
  272. xor ecx,ecx                              ; loop counter = 65536
  273. Enter_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (enter)
  274.  
  275. Search_free_Task:
  276. add esi,02h
  277.  
  278. lodsd
  279. or eax,eax
  280. jz Task_found
  281. loop Search_free_Task
  282.  
  283. ; if here, there is no free Task Handle
  284. Leave_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (leave)
  285. stc
  286. mov eax,No_free_Task
  287. jmp Create_Task_Exit
  288.  
  289.  
  290. Task_found:                              ; if comes here, a free Task Handle is found
  291. Leave_Multitasking MT_Task_Switcher               ; coordinate Multitasking access (leave)
  292. lea edi,[esi-06h]
  293.  
  294. ; store the Process Handle
  295. mov ax,[Param2]
  296. stosw
  297.  
  298. ; store the Stack Pointer
  299. mov eax,[Param3]
  300. sub eax,12+(4*5)+32+108
  301. stosd
  302.  
  303.  
  304.  
  305. ; set the stack for the new process on the current stack
  306. ; push following values in the correct direction:
  307. ;     gs, fs, es, ds, ad, FPU, iret, Handle (4, 4, 4, 4, 32, 108, 12, 4)
  308.  
  309. sub esp,12+(4*5)+32+108
  310. mov edi,esp
  311. mov ebx,esp
  312.  
  313. ; set gs, fs, es, ds
  314. xor eax,eax
  315. stosd
  316. stosd
  317. mov eax,Data_Selector_User
  318. stosd
  319. stosd
  320.  
  321. ; set edi, esi, ebp & esp (all points to the start of stack)
  322. mov eax,[Param3]
  323. stosd
  324. stosd
  325. stosd
  326. stosd
  327.  
  328. ; xor eax, ebx, ecx & edx
  329. xor eax,eax
  330. stosd
  331. stosd
  332. stosd
  333. stosd
  334.  
  335. ; FPU registers (use current)
  336. fsave [edi]
  337. add edi,108
  338.  
  339. ; store the iret (eip, cs, flags)
  340. mov eax,[Param4]
  341. stosd
  342. mov eax,Code_Selector_User
  343. stosd
  344. mov eax,User_EFLAG
  345. stosd
  346.  
  347. ; store the Task Handle
  348. sub eax,ecx                              ; 65536 - loop counter
  349. stosd
  350.  
  351.  
  352. ; check whether the data is to copy (or not)
  353. cmp [Param6],dword 0
  354. je Copy_finished
  355.  
  356. ; copy the data
  357. API Copy_Process_Data, [Param1], [Param2], [Param5], [Param3], [Param6]
  358. jc Create_Task_Exit
  359.  
  360. Copy_finished:
  361.  
  362.  
  363. ; copy the (system) stack
  364. sub [Param3],dword (12+(4*5)+32+108)
  365. API Copy_Process_Data, [Param1], [Param2], ebx, [Param3], (12+(4*5)+32+108)
  366.  
  367.  
  368.  
  369. ; reset the stack (delete temporary other process stack)
  370. add esp,12+(4*5)+32+108
  371.  
  372.  
  373. Create_Task_Exit:
  374. Leave_System_Environment
  375.  
  376. ret
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383. Delete_Task:
  384.  
  385. ; API Delete_Task, Handle
  386.  
  387. ; Handle = Handle of the Task to delete; returned (on the stack) by Create_Task
  388.  
  389. Enter_System_Environment
  390.  
  391. ; edi = address of the Handle
  392. imul edi,[Param1],Handle_Size
  393. add edi,Task_Table
  394.  
  395. xor eax,eax
  396. stosw
  397. stosd
  398.  
  399. Leave_System_Environment
  400.  
  401. ret
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409. Destroy_Task:
  410.  
  411. ; API Destroy_Task, Task_ID
  412.  
  413. ; (undocumented)
  414.  
  415. Enter_System_Environment
  416.  
  417. mov esi,Task_Table
  418. xor ecx,ecx                              ; loop counter = 65536
  419. mov bx,[Param1]
  420.  
  421. Destroy_free_Task:
  422. lodsw
  423. cmp ax,bx
  424. jne Destroy_free_Task_nt
  425.  
  426. mov [esi-2],word 0
  427. mov [esi],dword 0
  428.  
  429. Destroy_free_Task_nt:
  430. add esi,04h
  431. loop Destroy_free_Task
  432.  
  433. Leave_System_Environment
  434.  
  435. ret
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.  
  443. Finite:
  444.  
  445. ; (undocumented)
  446.  
  447.  
  448. ; test register content ???
  449. ;   - esp
  450.  
  451. ; test if current cr3 register/handle is in Task Table ???
  452.  
  453. Enter_MT        dispatcher_state
  454.  
  455. jmp Scheduler
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463. Dispatcher:
  464.  
  465. Enter_MT        dispatcher_state
  466.  
  467.  
  468. ; 1. store the current Task's state
  469.  
  470. sub esp,108
  471. fsave [esp]
  472. pushad
  473. mpush ds, es, fs, gs
  474.  
  475. ; store special FPU, MMX and SSE state or do nop
  476. Store_FPU_MMX_SSE_state:
  477. CPU PentiumPro
  478. fxsave [FPU_MMX_SSE2_State]
  479. CPU 386
  480.  
  481. ; store the stack
  482. mov ebx,[Current_Task]
  483. mov [ebx+2],esp
  484.  
  485.  
  486. ; 2. execute the (internal) Scheduler
  487. Scheduler:
  488. add [Current_Task],dword Handle_Size
  489. cmp [Current_Task],dword Task_Table_End
  490. jl Scheduler_Next
  491.  
  492. mov [Current_Task],dword Task_Table
  493.  
  494. Scheduler_Next:
  495. mov ebx,[Current_Task]
  496. mov eax,[ebx+2]
  497. or eax,eax
  498. jz Scheduler
  499.  
  500. ; restore the stack
  501. mov esp,[ebx+2]
  502.  
  503.  
  504. ; 3. restore the new Task's state
  505. movzx ecx,word [ebx]
  506. API Get_CR3, ecx
  507. mov cr3,eax
  508.  
  509. ; restore special FPU, MMX and SSE state or do nop
  510. Restore_FPU_MMX_SSE_state:
  511. CPU PentiumPro
  512. fxrstor [FPU_MMX_SSE2_State]
  513. CPU 386
  514.  
  515. mpop ds, es, fs, gs
  516. popad
  517. frstor [esp]
  518. add esp,108
  519.  
  520.  
  521. Leave_MT        dispatcher_state
  522.  
  523. ret
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531. Switch_Task:
  532.  
  533. ; (undocumented)
  534.  
  535. Enter_System
  536.  
  537.  
  538. ; set the return stack
  539. pushfd
  540. push cs
  541. push Switch_Task_ret
  542.  
  543. ; switch the task
  544. call Dispatcher
  545.  
  546. iret
  547.  
  548.  
  549. Switch_Task_ret:
  550. Leave_System
  551.  
  552. ret
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560. Check_Task_Handle:
  561.  
  562. ; API Check_Task_Handle, Handle, Process_Handle
  563.  
  564. ; Handle = Handle of the Task to check of validity; returned (on the stack) by Create_Task
  565. ; Process Handle = Process Handle of the Task (have to be the same as by Create_Task)
  566.  
  567. Enter_System_Environment
  568.  
  569. ; ebx = address of the Handle
  570. imul ebx,[Param1],Handle_Size
  571. add edi,Task_Table
  572.  
  573. ; check the boundarys
  574. cmp [Param1],dword Task_Table_End
  575. jnc Check_Task_Handle_Invalid
  576.  
  577. ; check if the Task_Handle(current).Process_Handle is euqal to the as parameter's one
  578. mov ax,[ebx]
  579. cmp ax,[Param2]
  580. jne Check_Task_Handle_Invalid
  581.  
  582. ; esp = 0 ?
  583. cmp [ebx+2],TPointer NULL
  584. je Check_Task_Handle_Invalid
  585.  
  586. ; Process Handle valid?
  587. API Check_Process_Handle, TProcessHandle [Param2]
  588. jc Check_Task_Handle_Exit
  589.  
  590. xor eax,eax
  591. clc
  592. jmp Check_Task_Handle_Exit
  593.  
  594.  
  595. Check_Task_Handle_Invalid:
  596. stc
  597. mov eax,Invalid_Handle
  598.  
  599.  
  600. Check_Task_Handle_Exit:
  601. Leave_System_Environment
  602.  
  603. ret
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611. Get_Current_Process:
  612.  
  613. ; (undocumented)
  614.  
  615. Enter_System
  616.  
  617. mov esi,[Current_Task]
  618. movzx eax,word [esi]
  619.  
  620. Leave_System
  621.  
  622. ret
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630. ; Current_Task is a direct pointer to the current Task into the Task Table
  631. Current_Task        dd  0
  632. FPU_MMX_SSE          dd   "no"
  633. dispatcher_state        db      0
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640. ; the Task Table contains information about the Tasks, the descriptor format:
  641.  
  642. ; Process Handle        (word)
  643. ; esp                   (dword)
  644. ; ------------------------------
  645. ; a Task description    6 Bytes

advertising

Update the Post

Either update this post and resubmit it with changes, or make a new post.

You may also comment on this post.

update paste below
details of the post (optional)

Note: Only the paste content is required, though the following information can be useful to others.

Save name / title?

(space separated, optional)



Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.

fantasy-obligation