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

Paste Description for Memory Manager

Der Memory Manager von ToasterOS.

Memory Manager
Sunday, October 29th, 2006 at 4:00:01am MST 

  1. ; **********************************************************
  2.  
  3. ;       Name: Memory Manager
  4. ;       Autor: Toaster Burger
  5. ;       Version: 1.00
  6. ;       Date: 26.11.2005
  7. ;       last Update: 10.03.2006
  8. ;       see document: ToasterOS.pdf
  9.  
  10. ; **********************************************************
  11.  
  12. [bits 32]
  13. CPU 486
  14.  
  15. %define Type_System
  16. %include "interface.asm"
  17. %include "Lp Global Tables.asm"
  18.  
  19. org Memory_Manager
  20.  
  21.  
  22.  
  23. jmp Set_Kernel_Memory
  24.  
  25. jmp New_Memory
  26. jmp Free_Memory
  27.  
  28. jmp Get_Process_Handle
  29. jmp Check_Process_Handle
  30. jmp Set_Process_CR3
  31. jmp Get_Process_CR3
  32.  
  33.  
  34. jmp Create_VAS
  35. jmp Expand_VAS
  36. jmp Destroy_VAS
  37.  
  38. jmp Create_System_VAS
  39. jmp Expand_System_VAS
  40.  
  41.  
  42. jmp Copy_Process_Data
  43.  
  44.  
  45. ; the native API
  46. jmp Enable_Memory_Management
  47. jmp Map_Memory
  48. jmp New_Page_Table
  49. jmp New_Page_Frame
  50.  
  51. jmp Map_self_Memory
  52. jmp New_self_Page_Table
  53.  
  54.  
  55.  
  56.  
  57. ; set the gates for the functions to export
  58.  
  59. ;jmp dword Set_Memory
  60.  
  61. ;jmp dword Create_VAS
  62. ;jmp dword Destroy_VAS
  63. ;jmp dword Expand_VAS
  64. ;jmp dword System_VAS
  65.  
  66. ;jmp dword Get_CR3
  67.  
  68. ;jmp dword Check_Process_Handle
  69.  
  70. ;jmp dword Copy_Process_Data
  71.  
  72. ;jmp dword Get_static_Page
  73.  
  74. ;jmp dword Reserve_Memory
  75. ;jmp dword Deserve_Memory
  76.  
  77.  
  78.  
  79.  
  80.  
  81. ; include the Public Heap Manager Functions
  82. %include "Public Heap Manager Functions.asm"
  83.  
  84. %define System_Memory_Heap_descriptors   512*1024 / descriptor_size
  85. %define SMHs                    System_Memory_Heap_descriptors
  86.  
  87. %define Page_Info_Array_size          20000h
  88. %define PIAs                    Page_Info_Array_size
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. Enable_Memory_Management:
  97.  
  98. ; calling macro:        API
  99. ; user:   Memory Manager
  100. ; stack size:      unknown
  101. ; heap size:        unknown
  102. ; Multitasking    n/u
  103. ; Multithreading        n/u
  104. ; name:   "Enable Memory Management"
  105. ; date:   Thursday, 28th September 2006
  106. ; autor:                Peter Kleissner
  107. ; publisher:        Vienna Computing
  108.  
  109. ; style:
  110. ;   API Enable_Memory_Management, Memory
  111.  
  112. ;   Memory = size of the physical memory in bytes
  113. ;   return = void
  114.  
  115. ;   This function enables the Memory Management.
  116. ;   It sets up the Memory Management buffers, the Heap Tree.
  117.  
  118. Enter_System    28
  119.  
  120.  
  121. ; create the System Memory Heap
  122. call Create_System_Memory_Heap
  123.  
  124. ; set the memory to physical present
  125. call Set_Memory_block_internal
  126.  
  127.  
  128. Leave_System
  129.  
  130. ret
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. New_Memory:
  139.  
  140. ; calling macro:        API
  141. ; user:   System
  142. ; stack size:      unknown
  143. ; heap size:        unknown
  144. ; Multitasking    n/u
  145. ; Multithreading        n/u
  146. ; name:   "New physical Memory"
  147. ; date:   Thursday, 28th September 2006
  148. ; autor:                Peter Kleissner
  149. ; publisher:        Vienna Computing
  150.  
  151. ; style:
  152. ;   API New_Memory, size
  153.  
  154. ;   size = size in bytes of needed memory
  155. ;   return = address of memory
  156.  
  157. Enter_System    28
  158.  
  159.  
  160. ; use the System Memory Heap
  161. mov [Heap_Tree],dword Memory_Heap_Tree
  162. mov [Heap_Tree_descriptors],dword SMHs
  163.  
  164.  
  165. ; call the sub function
  166. call New_Polymorph
  167.  
  168.  
  169. New_Exit:
  170. Leave_System
  171.  
  172. ret
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180. Free_Memory:
  181.  
  182. ; calling macro:        API
  183. ; user:   System
  184. ; stack size:      unknown
  185. ; heap size:        unknown
  186. ; Multitasking    n/u
  187. ; Multithreading        n/u
  188. ; name:   "Free physical Memory"
  189. ; date:   Thursday, 28th September 2006
  190. ; autor:                Peter Kleissner
  191. ; publisher:        Vienna Computing
  192.  
  193. ; style:
  194. ;   API Free_Memory, memory
  195.  
  196. ;   memory = memory to free, returned by "New Memory"
  197.  
  198. Enter_System    28
  199.  
  200.  
  201. ; use the System Memory Heap
  202. mov [Heap_Tree],dword Memory_Heap_Tree
  203. mov [Heap_Tree_descriptors],dword SMHs
  204.  
  205. ; call the sub function
  206. call Free_Polymorph
  207.  
  208.  
  209. Leave_System
  210.  
  211. ret
  212.  
  213.  
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223. Create_System_Memory_Heap:
  224.  
  225. ; LAPI Create_System_Memory_Heap
  226.  
  227. ; creates the System Memory Heap, with following conditions:
  228. ;     Heap = physical 0 - 4 GB (without Extensions)
  229. ;     Heap size = max. 4 GB (without Extensions)
  230. ;     Heap Tree = Memory_Heap_Tree
  231. ;     Heap Tree size = 512 KB
  232.  
  233.  
  234. ; create the System Memory Heap Tree
  235. mov edi,Memory_Heap_Tree
  236.  
  237.  
  238. ; create the first descriptor of the Heap Tree
  239. ;    eax = physical memory size in bytes
  240. shr eax,1
  241. stosd                     ; store the Heap Segment Size
  242.  
  243. mov al,10001000b
  244. stosb                     ; store the state
  245.  
  246. xor eax,eax
  247. stosd                     ; store Pointer 1
  248. stosd                     ; store Pointer 2
  249.  
  250.  
  251. ; set the variables
  252. mov [Heap_Tree],dword Memory_Heap_Tree
  253. mov [Heap_Tree_descriptors],dword SMHs
  254.  
  255.  
  256. ret
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269. ; Process Handle functions:
  270. ;    Get Process Handle
  271. ;    Check Process Handle
  272. ;    Set Process CR3
  273. ;    Get Process CR3
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282. Get_Process_Handle:
  283.  
  284. ; API Get_Process_Handle
  285.  
  286. ; return = Process Handle
  287.  
  288. Enter_System    28
  289.  
  290.  
  291. mov esi,Process_Info_Array
  292. mov ecx,PIAs/4
  293.  
  294.  
  295. Get_Process_Handle_np:
  296. lodsd
  297. or eax,eax
  298. jz Process_Handle_found
  299. loop Get_Process_Handle_np
  300.  
  301. stc
  302. mov eax,No_free_Process
  303. jmp Get_Process_Handle_Exit
  304.  
  305.  
  306. Process_Handle_found:
  307. ; reserve the fount Process Handle
  308. sub esi,4
  309. mov [esi],dword 1
  310.  
  311. ; store the Handle (equal to the number of the free Process Structure) in eax
  312. mov eax,PIAs/4 - 1
  313. sub eax,ecx
  314.  
  315.  
  316. Get_Process_Handle_Exit:
  317. Leave_System
  318.  
  319. ret
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329. Check_Process_Handle:
  330.  
  331. ; API Check_Process_Handle, Handle
  332.  
  333. ; Handle = Process Handle to check of validity
  334.  
  335. Enter_System    28
  336.  
  337.  
  338. mov eax,[Param1]
  339.  
  340. ; check the boundary
  341. cmp eax,PIAs/4 - 1
  342. jg Invalid_Process_Handle
  343.  
  344. lea esi,[Process_Info_Array + 4*eax]
  345. lodsd
  346.  
  347. or eax,eax
  348. jnz Check_Process_Handle_Exit
  349.  
  350.  
  351. Invalid_Process_Handle:
  352. stc
  353. mov eax,Invalid_Handle
  354.  
  355.  
  356. Check_Process_Handle_Exit:
  357. Leave_System
  358.  
  359. ret
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371. Set_Process_CR3:
  372.  
  373. ; API Set_Process_CR3, Handle, CR3
  374.  
  375. ; (undocumented)
  376. ;    the CR3 value may be set to zero to destroy and invalidate the Process Handle
  377.  
  378. Enter_System    28
  379.  
  380.  
  381. movzx eax,word [Param1]
  382. lea esi,[Process_Info_Array + 4*eax]
  383.  
  384. mov eax,[Param2]
  385. stosd
  386.  
  387.  
  388. Leave_System
  389.  
  390. ret
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402. Get_Process_CR3:
  403.  
  404. ; API Get_Process_CR3, Handle
  405.  
  406. ; Handle = Process Handle
  407. ; return = CR3 Register of Process
  408.  
  409. Enter_System    28
  410.  
  411.  
  412. ; load the cr3 register from the table
  413. movzx eax,word [Param1]
  414. lea esi,[Process_Info_Array + 4*eax]
  415. lodsd
  416.  
  417.  
  418. Leave_System
  419.  
  420. ret
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438. ; Virtual Address Space functions:
  439. ;    Set_Kernel_Memory
  440. ;    ?
  441. ;   
  442. ;   
  443.  
  444.  
  445. ;jmp dword Create_VAS
  446. ;jmp dword Destroy_VAS
  447. ;jmp dword Expand_VAS
  448. ;jmp dword System_VAS
  449.  
  450.  
  451.  
  452.  
  453.  
  454. Set_Kernel_Memory:
  455.  
  456. ; API Set_Kernel_Memory, Address
  457.  
  458. ; Address = physical address of the Memory Manager
  459. ; return = void
  460.  
  461. ; enables paging, and sets the Kernel memory
  462.  
  463. ;   stack
  464. ;    + Address
  465. ;    - Param1
  466.  
  467.  
  468. %define Temp_Memory_size                   System_Var-32
  469. %define Temp_Memory_Manager_Address        System_Var-36
  470. %define Temp_Mem_Page_Table_#0             System_Var-40
  471. %define Temp_Mem_Page_Table_#128           System_Var-44
  472. %define Temp_Mem_Page_Table_#192           System_Var-48
  473.  
  474.  
  475. ; enter to a new API-like stack
  476. pop ebx
  477. pop eax
  478. mov esp,101000h
  479. push eax
  480. sub esp,8
  481. push ebx
  482.  
  483. Enter_System    48
  484.  
  485.  
  486.  
  487. ; check the memory size
  488. xor eax,eax
  489. mov ecx,2*1024*1024*1024
  490.  
  491.  
  492. Check_Memory_size:
  493. ; if ecx = 16 end loop
  494. cmp ecx,16
  495. je Check_Memory_size_Exit
  496.  
  497. add eax,ecx
  498.  
  499. mov [eax],dword 55AA77FFh
  500. cmp [eax],dword 55AA77FFh
  501. je Check_Memory_size_next
  502.  
  503. ; else the memory is not available
  504. sub eax,ecx
  505. shr ecx,1
  506. jmp Check_Memory_size
  507.  
  508. Check_Memory_size_next:
  509. shr ecx,1
  510. jmp Check_Memory_size
  511.  
  512.  
  513. Check_Memory_size_Exit:
  514. mov [Temp_Memory_size],eax
  515.  
  516.  
  517.  
  518.  
  519. ; the Kernel memory pages, direct after the Memory Manager
  520. ;    Page 0:  Page Directory for System
  521. ;    Page 1:  Page Table 0
  522. ;    Page 2:  Page Table 128
  523. ;    Page 3:  Page Table 192
  524. ;    512 KB: Memory Heap Tree
  525.  
  526.  
  527.  
  528. ; set the Kernel memory
  529. mov eax,[Param1]
  530. mov [Temp_Memory_Manager_Address],eax
  531. add eax,64*1024
  532. mov ebx,eax
  533.  
  534. ; store the link to the Page Table 0
  535. add ebx,4096 + Page_TF_entry_S_g
  536. mov [Temp_Mem_Page_Table_#0],ebx
  537. mov [eax + 4*768 + 4*0],ebx
  538.  
  539. ; store the link to the Page Table 128
  540. add ebx,4096
  541. mov [Temp_Mem_Page_Table_#128],ebx
  542. mov [eax + 4*768 + 4*128],ebx
  543.  
  544. ; store the link to the Page Table 192
  545. add ebx,4096
  546. mov [Temp_Mem_Page_Table_#192],ebx
  547. mov [eax + 4*768 + 4*192],ebx
  548.  
  549.  
  550. ; map the GDT and the Paging Switcher
  551. add eax,4096
  552. mov [eax+4*16],dword 10000h + Page_TF_entry_S_g
  553. mov [eax+4*256],dword 100000h + Page_TF_entry_S_g
  554.  
  555. ; map the Memory Manager
  556. add eax,4096
  557. mov ebx,[Param1]
  558. add ebx,Page_TF_entry_S_g
  559. mov [eax+4*128],ebx
  560.  
  561. ; map the Virtual Window Page Table
  562. add eax,4096
  563. mov [eax+4*128],eax
  564.  
  565.  
  566. ; map the Memory Heap Tree
  567. sub eax,2*4096
  568. add ebx,64*1024 + 4096
  569. mov ecx,512*1024 / 4096
  570.  
  571. Link_Memory_Heap_Tree:
  572. mov [eax],ebx
  573. add eax,4
  574. add ebx,4096
  575. loop Link_Memory_Heap_Tree
  576.  
  577.  
  578.  
  579. ; set the CR3
  580. mov eax,[Param1]
  581. add eax,64*1024
  582. mov cr3,eax
  583.  
  584.  
  585.  
  586. ; copy the Paging switcher to memory 100000h
  587. mov esi,[Param1]
  588. add esi,Memory_Manager_Switcher-Memory_Manager
  589. mov edi,100000h
  590. mov ecx,Memory_Manager_Switcher_ret - Memory_Manager_Switcher
  591.  
  592. rep movsb
  593.  
  594.  
  595. ; jump to the Paging Switcher
  596. jmp dword Code_Selector:100000h
  597.  
  598.  
  599.  
  600. Memory_Manager_Switcher:
  601. ; the Paging switcher
  602.  
  603. ; enable Paging
  604. mov eax,cr0
  605. or eax,80000000h
  606. mov cr0,eax
  607.  
  608. ; enable the Global Page Extension
  609. mov eax,cr4
  610. or eax,010000000b
  611. mov cr4,eax
  612.  
  613. jmp dword Code_Selector:Memory_Manager_Switcher_ret
  614.  
  615.  
  616. Memory_Manager_Switcher_ret:
  617. ; here, paging is activated
  618.  
  619.  
  620.  
  621. ; copy the Temp variables back
  622. mov eax,[Temp_Memory_size]
  623. mov [Memory_size],eax
  624. mov ebx,[Temp_Memory_Manager_Address]
  625. mov [Memory_Manager_Address],ebx
  626. mov ecx,[Temp_Mem_Page_Table_#0]
  627. mov [Mem_Page_Table_#0],ecx
  628. mov edx,[Temp_Mem_Page_Table_#128]
  629. mov [Mem_Page_Table_#128],edx
  630. mov esi,[Temp_Mem_Page_Table_#192]
  631. mov [Mem_Page_Table_#192],esi
  632.  
  633.  
  634.  
  635. ; Create the Memory Heap Tree
  636. API Enable_Memory_Management, [Memory_size]
  637.  
  638.  
  639.  
  640. ; Set the physical available Memory
  641.  
  642. ; set the variables
  643. mov [Heap_Tree],dword Memory_Heap_Tree
  644. mov [Heap_Tree_descriptors],dword SMHs
  645. mov [address],dword 0
  646. m2m dword [Param1],dword [Memory_size]
  647.  
  648. ; and call the sub function
  649. call Set_Memory_block_internal
  650.  
  651.  
  652.  
  653. ; preserve the memory of the Memory Manager
  654. mov [Memory_Manager_Address],eax
  655. m2m dword [address],dword [Memory_Manager_Address]
  656. mov [Param1],dword 2*1024*1024
  657. call Preserve_Memory_internal
  658.  
  659.  
  660.  
  661. ; reserve the whole first MB
  662. mov [address],dword 0
  663. mov [Param1],dword 1024*1024
  664. call Reserve_Memory_internal
  665.  
  666.  
  667.  
  668.  
  669.  
  670. ; physical Kernel memory, 2048 KB
  671. ;   Memory Manager, 64 KB
  672. ;   Page 0 - 3, 16 KB
  673. ;   Memory Heap Tree, 512 KB
  674. ;   Kernel_NT_Emulator, 4 KB
  675. ;   Kernel stack, 16 KB
  676. ;   Task Table, 384 KB
  677. ;   Process Info Array, 128 KB
  678. ;   OS runtime info, 8 KB
  679. ;   Name Buffer, 8 KB
  680. ;   File Handles, 32 KB
  681. ;   TSS, 16 KB
  682. ;   TSS vm86, 16 KB
  683. ;   API Buffer (for System), 16 KB
  684. ;   Message Queue (for System), 16 KB
  685. ;   Main Heap Tree (for System), 16 KB
  686.  
  687.  
  688. mov edx,[Memory_Manager_Address]
  689. add edx,64*1024 + 16*1024 + 512*1024
  690. or edx,Page_TF_entry_S_g
  691.  
  692.  
  693.  
  694.  
  695.  
  696. ; physical lowlevel memory, starting at 10000h
  697. ;   GDT, 2 KB
  698. ;   IDT, 2 KB
  699. ;   IVT, 2 KB
  700.  
  701.  
  702.  
  703.  
  704. ; set the Kernel memory
  705.  
  706.  
  707.  
  708. ; set the GDT, IDT and IVT
  709. API Map_self_Memory, GDT, 10000h + Page_TF_entry_S_g, 2
  710.  
  711. ; set the Kernel NT Emulator
  712. API Map_self_Memory, Kernel_NT_Emulator, edx, 1
  713. add edx,4*1024
  714.  
  715.  
  716.  
  717. ; switch to the GDT and IDT (use the stack)
  718. sub esp,6
  719.  
  720. ; load the GDT Register
  721. mov [esp],word GDT_Limit
  722. mov [esp+02h],dword GDT
  723. lgdt [esp]
  724.  
  725. ; load the IDT Register
  726. mov [esp],word IDT_Limit
  727. mov [esp+02h],dword IDT
  728. lidt [esp]
  729.  
  730. ; restore the original stack pointer
  731. add esp,6
  732.  
  733.  
  734. jmp Code_Selector:Prefetch_Clear
  735. Prefetch_Clear:
  736.  
  737.  
  738.  
  739. ; set the BIOS Data Area, Data Area
  740. API Map_self_Memory, BIOS_Data_Area, 0400h + Page_TF_entry_S_g, 1
  741.  
  742. ; set the Extended BIOS Data Area
  743. mov ebx,[BIOS_Data_Area + 0040Eh]
  744. shl ebx,4
  745. or ebx,Page_TF_entry_S_g
  746. API Map_self_Memory, Extended_BIOS_Data_Area, ebx, 1
  747.  
  748.  
  749.  
  750. ; set the VESA BIOS memorys
  751. API Map_self_Memory, VESA_BIOS_A0000h, 0A0000h + Page_TF_entry_S_g, 64/4
  752. API Map_self_Memory, VESA_BIOS_B0000h, 0B0000h + Page_TF_entry_S_g, 64/4
  753. API Map_self_Memory, VESA_BIOS_B8000h, 0B8000h + Page_TF_entry_S_g, 32/4
  754. API Map_self_Memory, VESA_BIOS_image, 0C0000h + Page_TF_entry_S_g, 32/4
  755. API Map_self_Memory, Textmode_Buffer, 0B8000h + Page_TF_entry_S_g, 32/4
  756.  
  757.  
  758.  
  759. ; set the Kernel data memory
  760.  
  761. ; set the Kernel stack
  762. leave
  763. pop ecx
  764. mov esp,[Memory_Manager_Address]
  765. add esp,64*1024
  766. API Map_self_Memory, Kernel_Stack - 16*1024, edx, 16/4
  767. add edx,16*1024
  768. mov esp,Kernel_Stack
  769. push ecx
  770.  
  771. ; set the Task Table
  772. API Map_self_Memory, Task_Table, edx, 384/4
  773. add edx,384*1024
  774.  
  775. ; set the Process Info Array
  776. API Map_self_Memory, Process_Info_Array, edx, 128/4
  777. add edx,128*1024
  778.  
  779. ; set the OS runtime info
  780. API Map_self_Memory, OS_info, edx, 8/4
  781. add edx,8*1024
  782.  
  783. ; set the Name Buffer
  784. API Map_self_Memory, Name_Buffer, edx, 8/4
  785. add edx,8*1024
  786.  
  787. ; set the File Handles
  788. API Map_self_Memory, File_Handles, edx, 32/4
  789. add edx,32*1024
  790.  
  791. ; set the TSS
  792. API Map_self_Memory, TSS, edx, 16/4
  793. add edx,16*1024
  794.  
  795. ; set the vm86 TSS
  796. API Map_self_Memory, TSS_vm86, edx, 16/4
  797. add edx,16*1024
  798.  
  799. mov [Last_Kernel_Memory],edx
  800.  
  801.  
  802.  
  803. ; set the Kernel module code memory (at this time, dynamic!)
  804.  
  805. API New_Memory, 64*1024
  806. mov edx,eax
  807. API Map_self_Memory, BIOS_wrapper, edx, 64/4
  808.  
  809. API New_Memory, 64*1024
  810. mov edx,eax
  811. API Map_self_Memory, Desktop_Viewer, edx, 64/4
  812.  
  813. API New_Memory, 64*1024
  814. mov edx,eax
  815. API Map_self_Memory, Drive_Manager, edx, 64/4
  816.  
  817. API New_Memory, 64*1024
  818. mov edx,eax
  819. API Map_self_Memory, Driver_model, edx, 64/4
  820.  
  821. API New_Memory, 64*1024
  822. mov edx,eax
  823. API Map_self_Memory, Error_Manager, edx, 64/4
  824.  
  825. API New_Memory, 64*1024
  826. mov edx,eax
  827. API Map_self_Memory, Execute_module, edx, 64/4
  828.  
  829. API New_Memory, 64*1024
  830. mov edx,eax
  831. API Map_self_Memory, Heap_Manager, edx, 64/4
  832.  
  833. API New_Memory, 64*1024
  834. mov edx,eax
  835. API Map_self_Memory, IDT_Manager, edx, 64/4
  836.  
  837. API New_Memory, 64*1024
  838. mov edx,eax
  839. API Map_self_Memory, Security_Module, edx, 64/4
  840.  
  841. API New_Memory, 64*1024
  842. mov edx,eax
  843. API Map_self_Memory, System_Loader, edx, 64/4
  844.  
  845. API New_Memory, 64*1024
  846. mov edx,eax
  847. API Map_self_Memory, Task_Switcher, edx, 64/4
  848.  
  849. API New_Memory, 64*1024
  850. mov edx,eax
  851. API Map_self_Memory, Timer, edx, 64/4
  852.  
  853. API New_Memory, 64*1024
  854. mov edx,eax
  855. API Map_self_Memory, Utils, edx, 64/4
  856.  
  857. API New_Memory, 64*1024
  858. mov edx,eax
  859. API Map_self_Memory, VESA_GDI, edx, 64/4
  860.  
  861. API New_Memory, 64*1024
  862. mov edx,eax
  863. API Map_self_Memory, VESA_GUI, edx, 64/4
  864.  
  865.  
  866.  
  867. ; set the User Data Area
  868.  
  869. ; create and set the Page Table #224
  870. API New_self_Page_Table, Page_Table_#224, 1, Page_TF_entry_S
  871. mov ebx,eax
  872. API Map_self_Memory, Page_Table_#224, ebx, 1
  873.  
  874. ; set the API Buffer
  875. API Map_self_Memory, API_Buffer, [Last_Kernel_Memory], 16/4
  876. add [Last_Kernel_Memory],dword 16*1024
  877.  
  878. ; set the Message Queue
  879. API Map_self_Memory, Message_Queue, [Last_Kernel_Memory], 16/4
  880. add [Last_Kernel_Memory],dword 16*1024
  881.  
  882. ; set the Kernel stack (stack lowercase!)
  883. API Map_self_Memory, Kernel_stack, [Last_Kernel_Memory], 1
  884. add [Last_Kernel_Memory],dword 4*1024
  885.  
  886. ; set the Main Heap Tree
  887. API Map_self_Memory, Main_Heap_Tree, [Last_Kernel_Memory], 16/4
  888. add [Last_Kernel_Memory],dword 16*1024
  889.  
  890.  
  891.  
  892. ; set the TSS (the Stacks in the TSS)
  893. mov [TSS+24],dword 0
  894. mov [TSS+20],dword 0
  895. mov [TSS+16],dword 0
  896. mov [TSS+12],dword 0
  897.  
  898. mov [TSS+8],word Data_Selector
  899. mov [TSS+4],dword Kernel_stack + 4096
  900.  
  901.  
  902.  
  903. ; set a system Process Handle
  904. mov eax,cr3
  905. API Set_Process_CR3, 0, eax
  906.  
  907.  
  908.  
  909. ; It work !
  910. ;        S
  911.  
  912.  
  913.  
  914.  
  915. ; return = Memory size
  916. mov eax,[Memory_size]
  917.  
  918. ret
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.  
  930.  
  931. Map_Memory:
  932.  
  933. ; LAPI Map_Memory, linear Address, physical Address, pages
  934.  
  935. ; physical Address = Page Base Address
  936.  
  937. Enter_System    28
  938.  
  939.  
  940.  
  941. ; get the CR3 Register
  942. API Get_Process_CR3, [Param4]
  943. mov [Param4],eax
  944.  
  945.  
  946.  
  947. ; set in the Virtual Window 1 the linear Address to the physical one
  948. Map_Memory_l2p:
  949.  
  950. ; link the Page Directory with the Virtual Window 1
  951. mov eax,[Param4]
  952. and eax,0FFFFF000h
  953. or eax,Page_TF_entry_S
  954. mov [Page_Table_#192+1*4],eax
  955. invlpg [User_Window_1]
  956.  
  957. ; link the Page Table with the Virtual Window 1
  958. mov ebx,[Param1]
  959. shr ebx,12+10
  960. shl ebx,2
  961. mov eax,[ebx + User_Window_1]
  962. and eax,0FFFFF000h
  963. or eax,Page_TF_entry_S
  964. mov [Page_Table_#192+1*4],eax
  965. invlpg [User_Window_1]
  966.  
  967.  
  968. ; set the Address of the Page Frame in the Virtual Window
  969. Map_Memory_l2p_set:
  970. mov ebx,[Param1]
  971. add [Param1],dword 4096
  972. shr ebx,12-2
  973. and ebx,0FFCh
  974.  
  975. mov eax,[Param2]
  976. add [Param2],dword 4096
  977. mov [ebx + User_Window_1],eax
  978. add ebx,4
  979.  
  980.  
  981.  
  982. ; last page?
  983. dec dword [Param3]
  984. jz Map_Memory_Exits
  985.  
  986.  
  987. ; link the next table if neccesary
  988. cmp ebx,4096
  989. jne Map_Memory_l2p_set
  990.  
  991. ; else map next Page Table
  992. jmp Map_Memory_l2p
  993.  
  994.  
  995.  
  996. Map_Memory_Exits:
  997. clc
  998.  
  999. Map_Memory_Exit:
  1000. Leave_System
  1001.  
  1002. ret
  1003.  
  1004.  
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015. Map_self_Memory:
  1016.  
  1017. ; LAPI Map_self_Memory, linear Address, physical Address, pages
  1018.  
  1019. ; physical Address = Page Base Address
  1020.  
  1021. Enter_System    28
  1022.  
  1023.  
  1024. ; set in the Virtual Window 1 the linear Address to the physical one
  1025. Map_self_Memory_l2p:
  1026.  
  1027. ; link the Page Directory with the Virtual Window 1
  1028. mov eax,cr3
  1029. and eax,0FFFFF000h
  1030. or eax,Page_TF_entry_S
  1031. mov [Page_Table_#192+1*4],eax
  1032. invlpg [User_Window_1]
  1033.  
  1034. ; link the Page Table with the Virtual Window 1
  1035. mov ebx,[Param1]
  1036. shr ebx,12+10
  1037. shl ebx,2
  1038. mov eax,[ebx + User_Window_1]
  1039. and eax,0FFFFF000h
  1040. or eax,Page_TF_entry_S
  1041. mov [Page_Table_#192+1*4],eax
  1042. invlpg [User_Window_1]
  1043.  
  1044.  
  1045. ; set the Address of the Page Frame in the Virtual Window
  1046. Map_self_Memory_l2p_set:
  1047. mov ebx,[Param1]
  1048. add [Param1],dword 4096
  1049. shr ebx,12-2
  1050. and ebx,0FFCh
  1051.  
  1052. mov eax,[Param2]
  1053. add [Param2],dword 4096
  1054. mov [ebx + User_Window_1],eax
  1055. add ebx,4
  1056.  
  1057.  
  1058.  
  1059. ; last page?
  1060. dec dword [Param3]
  1061. jz Map_self_Memory_Exits
  1062.  
  1063.  
  1064. ; link the next table if neccesary
  1065. cmp ebx,4096
  1066. jne Map_self_Memory_l2p_set
  1067.  
  1068. ; else map next Page Table
  1069. jmp Map_self_Memory_l2p
  1070.  
  1071.  
  1072.  
  1073. Map_self_Memory_Exits:
  1074. clc
  1075.  
  1076. Map_self_Memory_Exit:
  1077. Leave_System
  1078.  
  1079. ret
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.  
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093. New_Page_Table:
  1094.  
  1095. ; LAPI New_Page_Table, linear Address, Tables, Attributes, Handle
  1096.  
  1097. Enter_System    28
  1098.  
  1099.  
  1100. ; get the CR3 Register
  1101. API Get_Process_CR3, [Param4]
  1102. mov [Param4],eax
  1103.  
  1104.  
  1105. ; link the Page Directory with the Virtual Window 1
  1106. mov eax,[Param4]
  1107. and eax,0FFFFF000h
  1108. or eax,Page_TF_entry_S
  1109. mov [Page_Table_#192+1*4],eax
  1110. invlpg [User_Window_1]
  1111.  
  1112.  
  1113. ; get a new Page Table
  1114. Next_Page_Table:
  1115. API New_Memory, 4096, New_Page_Table_Exit
  1116.  
  1117. ; link the Page Table in the Page Directory
  1118. or eax,[Param3]
  1119. mov ebx,[Param1]
  1120. shr ebx,12+10
  1121. shl ebx,2
  1122. mov [User_Window_1 + ebx],eax
  1123.  
  1124.  
  1125. ; last Page Table?
  1126. add [Param1],dword 4*1024*1024
  1127. dec dword [Param2]
  1128. jnz Next_Page_Table
  1129.  
  1130. clc
  1131.  
  1132.  
  1133. New_Page_Table_Exit:
  1134. Leave_System
  1135.  
  1136. ret
  1137.  
  1138.  
  1139.  
  1140.  
  1141.  
  1142.  
  1143.  
  1144. New_self_Page_Table:
  1145.  
  1146. ; LAPI New_Page_Table, linear Address, Tables, Attributes
  1147.  
  1148. Enter_System    28
  1149.  
  1150.  
  1151. ; link the Page Directory with the Virtual Window 1
  1152. mov eax,cr3
  1153. and eax,0FFFFF000h
  1154. or eax,Page_TF_entry_S
  1155. mov [Page_Table_#192+1*4],eax
  1156. invlpg [User_Window_1]
  1157.  
  1158.  
  1159. ; get a new Page Table
  1160. Next_self_Page_Table:
  1161. API New_Memory, 4096, New_Page_Table_Exit
  1162.  
  1163. ; link the Page Table in the Page Directory
  1164. or eax,[Param3]
  1165. mov ebx,[Param1]
  1166. shr ebx,12+10
  1167. shl ebx,2
  1168. mov [User_Window_1 + ebx],eax
  1169.  
  1170.  
  1171. ; last Page Table?
  1172. add [Param1],dword 4*1024*1024
  1173. dec dword [Param2]
  1174. jnz Next_self_Page_Table
  1175.  
  1176. clc
  1177.  
  1178.  
  1179. Leave_System
  1180.  
  1181. ret
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194. New_Page_Frame:
  1195.  
  1196. ; LAPI New_Page_Frame, linear Address, Frames, Attributes, Handle
  1197.  
  1198. Enter_System    28
  1199.  
  1200.  
  1201. ; get the CR3 Register
  1202. API Get_Process_CR3, [Param4]
  1203. mov [Param4],eax
  1204.  
  1205.  
  1206. New_Page_Frame_l2p:
  1207.  
  1208. ; link the Page Directory with the Virtual Window 1
  1209. mov eax,[Param4]
  1210. and eax,0FFFFF000h
  1211. or eax,Page_TF_entry_S
  1212. mov [Page_Table_#192+1*4],eax
  1213. invlpg [User_Window_1]
  1214.  
  1215. ; link the Page Table with the Virtual Window 1
  1216. mov ebx,[Param1]
  1217. shr ebx,12+10
  1218. shl ebx,2
  1219. mov eax,[ebx + User_Window_1]
  1220. and eax,0FFFFF000h
  1221. or eax,Page_TF_entry_S
  1222. mov [Page_Table_#192+1*4],eax
  1223. invlpg [User_Window_1]
  1224.  
  1225.  
  1226. ; create the Page Frame within the Virtual Window 1
  1227. New_Page_Frame_l2p_set:
  1228. API New_Memory, 4096, New_Page_Frame_Exit
  1229. or eax,[Param3]
  1230. mov ebx,[Param1]
  1231. add [Param1],dword 4096
  1232. shr ebx,12-2
  1233. and ebx,0FFCh
  1234. mov [User_Window_1 + ebx],eax
  1235. add ebx,4
  1236.  
  1237.  
  1238. ; last page?
  1239. dec dword [Param2]
  1240. jz New_Page_Frame_Exits
  1241.  
  1242.  
  1243. ; create the next frame if neccesary
  1244. cmp ebx,4096
  1245. jne New_Page_Frame_l2p_set
  1246.  
  1247. ; else map next Page Table
  1248. jmp New_Page_Frame_l2p
  1249.  
  1250.  
  1251. New_Page_Frame_Exits:
  1252. clc
  1253.  
  1254. New_Page_Frame_Exit:
  1255. Leave_System
  1256.  
  1257. ret
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.  
  1267.  
  1268.  
  1269.  
  1270. Create_VAS:
  1271.  
  1272. ; calling macro:        API
  1273. ; user:   System
  1274. ; stack size:      unknown
  1275. ; heap size:        unknown
  1276. ; Multitasking    n/u
  1277. ; Multithreading        n/u
  1278. ; name:   "Create Virtual Address Space"
  1279. ; date:   Wednesday, 25 October 2006
  1280. ; autor:                Peter Kleissner
  1281. ; publisher:        Vienna Computing
  1282.  
  1283. ; style:
  1284. ;   API Create_VAS, size
  1285.  
  1286. ;   size = size of user memory in bytes
  1287. ;   return = Process Handle
  1288.  
  1289. %define Process_Handle                     System_Var-32
  1290. %define Page_Directory                     System_Var-32
  1291.  
  1292. Enter_System    32
  1293.  
  1294.  
  1295. ; get a Process Handle
  1296. API Get_Process_Handle, Create_VAS_Exit
  1297. mov [Process_Handle],eax
  1298.  
  1299.  
  1300. ; create the Page Directory
  1301. API New_Memory, 4096, Create_VAS_Exite
  1302.  
  1303. ; link the CR3 with the Page Directory
  1304. mov [Page_Directory],eax
  1305. mov ebx,eax
  1306. or ebx,Page_TF_entry_U
  1307. API Set_Process_CR3, [Process_Handle], ebx
  1308.  
  1309.  
  1310. ; link the System Page Tables
  1311. mov [Page_Table_#192 + 4],ebx
  1312. invlpg [User_Window_1]
  1313.  
  1314. mov eax,[Mem_Page_Table_#0]
  1315. mov [User_Window_1 + 4*768 + 4*0],eax
  1316. mov eax,[Mem_Page_Table_#128]
  1317. mov [User_Window_1 + 4*768 + 4*128],eax
  1318. mov eax,[Mem_Page_Table_#192]
  1319. mov [User_Window_1 + 4*768 + 4*192],eax
  1320.  
  1321.  
  1322.  
  1323. ; create the User Memory
  1324.  
  1325. ; create the Page Tables
  1326. mov ebx,[Param1]
  1327. shr ebx,22
  1328.  
  1329. API New_Page_Table, 0, ebx, Page_TF_entry_U, Create_VAS_Exite, [Process_Handle]
  1330.  
  1331.  
  1332. ; create the Page Frames
  1333. mov ebx,[Param1]
  1334. shr ebx,12
  1335.  
  1336. API New_Page_Frame, 0, ebx, Page_TF_entry_U, Create_VAS_Exite, [Process_Handle]
  1337.  
  1338.  
  1339.  
  1340. Create_VAS_Exit:
  1341. mov eax,[Process_Handle]
  1342. Leave_System
  1343.  
  1344. clc
  1345. ret
  1346.  
  1347.  
  1348. Create_VAS_Exite:
  1349. API Destroy_VAS, [Process_Handle]
  1350. Leave_System
  1351.  
  1352. xor eax,eax
  1353. stc
  1354. ret
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366. Expand_VAS:
  1367.  
  1368. ; calling macro:        API
  1369. ; user:   System
  1370. ; stack size:      unknown
  1371. ; heap size:        unknown
  1372. ; Multitasking    n/u
  1373. ; Multithreading        n/u
  1374. ; name:   "Expand Virtual Address Space"
  1375. ; date:   Wednesday, 25 October 2006
  1376. ; autor:                Peter Kleissner
  1377. ; publisher:        Vienna Computing
  1378.  
  1379. ; style:
  1380. ;   API Expand_VAS, Handle, address, size
  1381.  
  1382. ;   size = size of user memory in bytes started at address
  1383. ;   may used on a Parity Error to preserve the physical pages!
  1384.  
  1385. Enter_System    28
  1386.  
  1387.  
  1388. ; check the validity of the Handle
  1389. API Check_Process_Handle, [Param1], Expand_VAS_Exit
  1390.  
  1391.  
  1392. ; create the Page Tables
  1393. mov ebx,[Param1]
  1394. add ebx,003FFFFFh
  1395. shr ebx,22
  1396.  
  1397. API New_Page_Table, [Param2], ebx, Page_TF_entry_U, [Param1]
  1398.  
  1399.  
  1400. ; create the Page Frames
  1401. mov ebx,[Param1]
  1402. add ebx,4098
  1403. shr ebx,12
  1404.  
  1405. API New_Page_Frame, [Param2], ebx, Page_TF_entry_U, [Param1]
  1406.  
  1407.  
  1408. Expand_VAS_Exit:
  1409. Leave_System
  1410.  
  1411. ret
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422. Destroy_VAS:
  1423.  
  1424. ; calling macro:        API
  1425. ; user:   System
  1426. ; stack size:      unknown
  1427. ; heap size:        unknown
  1428. ; Multitasking    n/u
  1429. ; Multithreading        n/u
  1430. ; name:   "Destroy Virtual Address Space"
  1431. ; date:   Wednesday, 25 October 2006
  1432. ; autor:                Peter Kleissner
  1433. ; publisher:        Vienna Computing
  1434.  
  1435. ; style:
  1436. ;   API Destroy_VAS, Handle
  1437.  
  1438. ;   Handle mustn't be Process(Sender).Handle!
  1439.  
  1440. Enter_System    28
  1441.  
  1442.  
  1443. ; check the validity of the Handle
  1444. API Check_Process_Handle, [Param1], Destroy_VAS_Exit
  1445.  
  1446.  
  1447. ; get the CR3 Register, and destroy the Handle
  1448. API Get_Process_CR3, [Param1]
  1449. push eax
  1450. API Set_Process_CR3, [Param1], 0
  1451. pop dword [Param1]
  1452.  
  1453.  
  1454.  
  1455.  
  1456. ; link the Virtual Window 1 with the Page Directory
  1457. and eax,0FFFFF000h
  1458.  
  1459. ; free the virtual Frame
  1460. or eax,eax
  1461. jz Destroy_VAS_Exit
  1462. or eax,Page_TF_entry_S
  1463. mov [Page_Table_#192+1*4],eax
  1464. invlpg [User_Window_1]
  1465.  
  1466.  
  1467.  
  1468.  
  1469. mov ecx,1024
  1470.  
  1471. Destroy_VAS_Page_Tables:
  1472.  
  1473. ; link the Virtual Window 2 with the Page Table
  1474. mov eax,[4*ecx + User_Window_1]
  1475. and eax,0FFFFF000h
  1476.  
  1477. ; free the virtual Frame
  1478. or eax,eax
  1479. jz Destroyed_VAS_Page_Tables
  1480. or eax,Page_TF_entry_S
  1481. mov [Page_Table_#192+2*4],eax
  1482. invlpg [User_Window_2]
  1483.  
  1484.  
  1485.  
  1486.     push ecx
  1487.     mov ecx,1024
  1488.    
  1489.     Destroy_VAS_Page_Frames:
  1490.    
  1491.       ; free the virtual Frame
  1492.       mov ebx,[4*ecx + User_Window_2]
  1493.       and ebx,0FFFFF000h
  1494.       or ebx,ebx
  1495.       jz Destroyed_VAS_Page_Frames
  1496.      
  1497.       ; free the physical Frame
  1498.       API Free_Memory, ebx
  1499.      
  1500.       Destroyed_VAS_Page_Frames:
  1501.    
  1502.     loop Destroy_VAS_Page_Frames
  1503.    
  1504.     pop ecx
  1505.  
  1506.  
  1507.  
  1508. ; free the physical Frame
  1509. mov ebx,[4*ecx + User_Window_1]
  1510. and ebx,0FFFFF000h
  1511. API Free_Memory, ebx
  1512.  
  1513. Destroyed_VAS_Page_Tables:
  1514.  
  1515.  
  1516.  
  1517. loop Destroy_VAS_Page_Tables
  1518.  
  1519.  
  1520.  
  1521. ; free the physical Frame
  1522. mov ebx,[Param1]
  1523. and ebx,0FFFFF000h
  1524. API Free_Memory, ebx
  1525.  
  1526.  
  1527.  
  1528.  
  1529.  
  1530. Destroy_VAS_Exit:
  1531. Leave_System
  1532.  
  1533. ret
  1534.  
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546.  
  1547.  
  1548.  
  1549.  
  1550. Create_System_VAS:
  1551.  
  1552. ; calling macro:        API
  1553. ; user:   System
  1554. ; stack size:      unknown
  1555. ; heap size:        unknown
  1556. ; Multitasking    n/u
  1557. ; Multithreading        n/u
  1558. ; name:   "Create System Virtual Address Space"
  1559. ; date:   Wednesday, 25 October 2006
  1560. ; autor:                Peter Kleissner
  1561. ; publisher:        Vienna Computing
  1562.  
  1563. ; style:
  1564. ;   API Create_VAS, size
  1565.  
  1566. ;   size = size of user memory in bytes
  1567. ;   return = Process Handle
  1568.  
  1569. %define Process_Handle                     System_Var-32
  1570. %define Page_Directory                     System_Var-32
  1571.  
  1572. Enter_System    32
  1573.  
  1574.  
  1575. ; get a Process Handle
  1576. API Get_Process_Handle, Create_VAS_Exit
  1577. mov [Process_Handle],eax
  1578.  
  1579.  
  1580. ; create the Page Directory
  1581. API New_Memory, 4096, Create_VAS_Exite
  1582.  
  1583. ; link the CR3 with the Page Directory
  1584. mov [Page_Directory],eax
  1585. mov ebx,eax
  1586. or ebx,Page_TF_entry_S
  1587. API Set_Process_CR3, [Process_Handle], ebx
  1588.  
  1589.  
  1590. ; link the System Page Tables
  1591. mov [Page_Table_#192 + 4],ebx
  1592. invlpg [User_Window_1]
  1593.  
  1594. mov eax,[Mem_Page_Table_#0]
  1595. mov [User_Window_1 + 4*768 + 4*0],eax
  1596. mov eax,[Mem_Page_Table_#128]
  1597. mov [User_Window_1 + 4*768 + 4*128],eax
  1598. mov eax,[Mem_Page_Table_#192]
  1599. mov [User_Window_1 + 4*768 + 4*192],eax
  1600.  
  1601.  
  1602.  
  1603. ; create the User Memory
  1604.  
  1605. ; create the Page Tables
  1606. mov ebx,[Param1]
  1607. shr ebx,22
  1608.  
  1609. API New_Page_Table, 0, ebx, Page_TF_entry_S, Create_VAS_Exite, [Process_Handle]
  1610.  
  1611.  
  1612. ; create the Page Frames
  1613. mov ebx,[Param1]
  1614. shr ebx,12
  1615.  
  1616. API New_Page_Frame, 0, ebx, Page_TF_entry_S, Create_VAS_Exite, [Process_Handle]
  1617.  
  1618.  
  1619.  
  1620. jmp Create_VAS_Exit
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626.  
  1627.  
  1628.  
  1629.  
  1630.  
  1631.  
  1632.  
  1633. Expand_System_VAS:
  1634.  
  1635. ; calling macro:        API
  1636. ; user:   System
  1637. ; stack size:      unknown
  1638. ; heap size:        unknown
  1639. ; Multitasking    n/u
  1640. ; Multithreading        n/u
  1641. ; name:   "Expand System Virtual Address Space"
  1642. ; date:   Wednesday, 25 October 2006
  1643. ; autor:                Peter Kleissner
  1644. ; publisher:        Vienna Computing
  1645.  
  1646. ; style:
  1647. ;   API Expand_VAS, Handle, address, size
  1648.  
  1649. ;   size = size of user memory in bytes started at address
  1650. ;   may used on a Parity Error to preserve the physical pages!
  1651.  
  1652. Enter_System    28
  1653.  
  1654.  
  1655. ; check the validity of the Handle
  1656. API Check_Process_Handle, [Param1], Expand_VAS_Exit
  1657.  
  1658.  
  1659. ; create the Page Tables
  1660. mov ebx,[Param1]
  1661. add ebx,003FFFFFh
  1662. shr ebx,22
  1663.  
  1664. API New_Page_Table, [Param2], ebx, Page_TF_entry_S, [Param1]
  1665.  
  1666.  
  1667. ; create the Page Frames
  1668. mov ebx,[Param1]
  1669. add ebx,4098
  1670. shr ebx,12
  1671.  
  1672. API New_Page_Frame, [Param2], ebx, Page_TF_entry_S, [Param1]
  1673.  
  1674.  
  1675. jmp Expand_VAS_Exit
  1676.  
  1677.  
  1678.  
  1679.  
  1680.  
  1681.  
  1682.  
  1683.  
  1684.  
  1685.  
  1686.  
  1687.  
  1688.  
  1689. Copy_Process_Data:
  1690.  
  1691. ; calling macro:        API
  1692. ; user:   System
  1693. ; stack size:      unknown
  1694. ; heap size:        unknown
  1695. ; Multitasking    n/u
  1696. ; Multithreading        n/u
  1697. ; name:   "Copy Data over different Virtual Address Spaces"
  1698. ; date:   Wednesday, 25 October 2006
  1699. ; autor:                Peter Kleissner
  1700. ; publisher:        Vienna Computing
  1701.  
  1702. ; style:
  1703. ;   API Copy_Process_Data, Handle, Handle, Source, Destination, Size
  1704.  
  1705. ;   Handle = source Process Handle
  1706. ;   Handle = destination Process Handle
  1707. ;   Source = source Address
  1708. ;   Destination = destination Address
  1709. ;   Size = size (in bytes) of the data to copy
  1710.  
  1711. Enter_System    28
  1712.  
  1713.  
  1714. ; check the validity of the Handles
  1715. API Check_Process_Handle, [Param1], Copy_Process_Data_Exit
  1716. API Check_Process_Handle, [Param2], Copy_Process_Data_Exit
  1717.  
  1718.  
  1719. ; link the Virtual Window 1 with the Page Directory of #1
  1720. API Get_Process_CR3, [Param1]
  1721. and eax,0FFFFF000h
  1722. or eax,eax
  1723. jz Copy_Process_Data_Exit
  1724. or eax,Page_TF_entry_S
  1725. mov [Page_Table_#192+1*4],eax
  1726. invlpg [User_Window_1]
  1727.  
  1728.  
  1729. ; link the Virtual Window 2 with the Page Directory of #2
  1730. API Get_Process_CR3, [Param2]
  1731. and eax,0FFFFF000h
  1732. or eax,eax
  1733. jz Copy_Process_Data_Exit
  1734. or eax,Page_TF_entry_S
  1735. mov [Page_Table_#192+2*4],eax
  1736. invlpg [User_Window_2]
  1737.  
  1738.  
  1739.  
  1740. call Copy_Memory_Area
  1741.  
  1742.  
  1743.  
  1744. Copy_Process_Data_Exit:
  1745. Leave_System
  1746.  
  1747. ret
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753.  
  1754.  
  1755.  
  1756.  
  1757.  
  1758.  
  1759.  
  1760. Copy_Memory_Area:
  1761.  
  1762.  
  1763. ; link the Virtual Window 3 with the correct Page Table of #1
  1764. mov eax,[Param3]
  1765. shr eax,22
  1766. shl eax,2
  1767. mov ebx,[User_Window_1 + eax]
  1768. or ebx,Page_TF_entry_S
  1769. mov [Page_Table_#192+3*4],ebx
  1770. invlpg [User_Window_3]
  1771.  
  1772. ; link the Virtual Window 3 with the correct Page Frame of #1
  1773. mov eax,[Param3]
  1774. shr eax,12-2
  1775. and eax,0FFCh
  1776. or eax,Page_TF_entry_S
  1777. mov ebx,[User_Window_3 + eax]
  1778. or ebx,Page_TF_entry_S
  1779. mov [Page_Table_#192+3*4],ebx
  1780. invlpg [User_Window_3]
  1781.  
  1782.  
  1783. ; link the Virtual Window 4 with the correct Page Table of #2
  1784. mov eax,[Param4]
  1785. shr eax,22
  1786. shl eax,2
  1787. mov ebx,[User_Window_2 + eax]
  1788. or ebx,Page_TF_entry_S
  1789. mov [Page_Table_#192+4*4],ebx
  1790. invlpg [User_Window_4]
  1791.  
  1792. ; link the Virtual Window 4 with the correct Page Frame of #2
  1793. mov eax,[Param4]
  1794. shr eax,12-2
  1795. and eax,0FFCh
  1796. or eax,Page_TF_entry_S
  1797. mov ebx,[User_Window_4 + eax]
  1798. or ebx,Page_TF_entry_S
  1799. mov [Page_Table_#192+4*4],ebx
  1800. invlpg [User_Window_4]
  1801.  
  1802.  
  1803.  
  1804. ; copy 4096 - source.PageOffset or 4096 - destination.PageOffset, the less one
  1805. mov ecx,4096
  1806. mov esi,[Param3]
  1807. and esi,0FFFh
  1808. sub ecx,esi
  1809.  
  1810. mov ebx,4096
  1811. mov edi,[Param3]
  1812. and edi,0FFFh
  1813. sub ebx,edi
  1814.  
  1815. cmp ecx,ebx
  1816. jle Copy_Memory_Area_size
  1817. mov ecx,ebx
  1818. Copy_Memory_Area_size:
  1819.  
  1820.  
  1821. ; copy eax or Param5 bytes, take the less one
  1822. cmp ecx,[Param5]
  1823. jle Copy_Memory_Area_size1
  1824. mov ecx,[Param5]
  1825. Copy_Memory_Area_size1:
  1826.  
  1827. sub [Param5],ecx
  1828.  
  1829.  
  1830. ; copy the spanned data area
  1831. rep movsb
  1832.  
  1833.  
  1834. ; if not zero, copy next data
  1835. cmp [Param5],dword 0
  1836. jne Copy_Memory_Area
  1837.  
  1838.  
  1839. ret
  1840.  
  1841.  
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.  
  1861.  
  1862.  
  1863.  
  1864.  
  1865.  
  1866.  
  1867.  
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873.  
  1874.  
  1875. ; public variables
  1876. Mem_Page_Table_#0     dd        0       ; data memory
  1877. Mem_Page_Table_#128   dd        0       ; code memory
  1878. Mem_Page_Table_#192   dd        0       ; Virtual Windows
  1879.  
  1880. Memory_size           dd        0
  1881.  
  1882. Memory_Manager_Address   dd        0
  1883.  
  1884. Last_Kernel_Memory    dd        0

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.

worth-right
fantasy-obligation