Subversion Repositories Kolibri OS

Rev

Rev 69 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. virtual at 0
  2. physical_mem_block:
  3.   .start rd 1
  4.   .size  rd 1
  5.   .flags rd 1  ;0-free, pid-used.  
  6. .sizeof:
  7. end virtual
  8. max_physical_mem_blocks = 24
  9. uglobal
  10.   num_physical_mem_blocks rd 1
  11.   physical_mem_blocks rd 3*max_physical_mem_blocks
  12. endg
  13. Init_Physical_Memory_Manager:
  14.   pushad
  15.   mov  edi,physical_mem_blocks
  16.   mov  ecx,3*max_physical_mem_blocks
  17.   xor  eax,eax
  18.   cld
  19.   rep  stosd
  20.   mov  dword [num_physical_mem_blocks],2
  21.   mov  [physical_mem_blocks+physical_mem_block.start],0x60000
  22.   mov  [physical_mem_blocks+physical_mem_block.size],0x20000 ;128Kb
  23.   mov  [physical_mem_blocks+physical_mem_block.sizeof+physical_mem_block.start],0x780000
  24.   mov  [physical_mem_blocks+physical_mem_block.sizeof+physical_mem_block.size],0x80000 ;512Kb
  25.   popad
  26.   ret
  27. Insert_Block:
  28. ;input:
  29. ;  eax - handle
  30. ;output:
  31. ;  none
  32.   push eax ecx esi edi
  33.   sub  eax,[num_physical_mem_blocks]
  34.   neg  eax
  35.   mov  edi,physical_mem_block.sizeof
  36.   imul eax,edi
  37.   shr  eax,2
  38.   mov  ecx,eax
  39.   mov  esi,[num_physical_mem_blocks]
  40.   imul esi,edi
  41.   add  esi,physical_mem_blocks
  42.   lea  edi,[esi+physical_mem_block.sizeof]
  43.   std
  44.   rep  movsd
  45.   pop  edi esi ecx eax
  46.   ret
  47. Delete_Block:
  48. ;input:
  49. ;  eax - handle
  50. ;output:
  51. ;  none
  52.   pushad
  53.   mov  edi,eax
  54.   sub  eax,[num_physical_mem_blocks]
  55.   neg  eax
  56.   dec  eax
  57.   mov  esi,physical_mem_block.sizeof
  58.   imul eax,esi
  59.   imul edi,esi
  60.   add  edi,physical_mem_blocks
  61.   lea  esi,[edi+physical_mem_block.sizeof]
  62.   mov  ecx,eax
  63.   shr  ecx,2
  64.   cld
  65.   rep  movsd
  66.   popad
  67.   ret
  68. Allocate_Physical_Block:
  69. ;input:
  70. ;  eax - size
  71. ;output:
  72. ;  eax - address or 0 if not enough memory.
  73.   pushad
  74.   cmp  [num_physical_mem_blocks],max_physical_mem_blocks
  75.   jge  .error
  76.   mov  ebx,eax
  77.   xor  eax,eax
  78.   mov  esi,physical_mem_blocks
  79. .loop:
  80.   cmp  dword [esi+physical_mem_block.flags],0
  81.   jnz  .next
  82.   cmp  [esi+physical_mem_block.size],ebx
  83.   jg   .addblock
  84.   jz   .noaddblock
  85. .next:
  86.   inc  eax
  87.   add  esi,physical_mem_block.sizeof
  88.   cmp  eax,[num_physical_mem_blocks]
  89.   jl   .loop
  90. .error:
  91.   popad
  92.   xor  eax,eax
  93.   ret  
  94. .noaddblock:
  95.   mov  eax,[esi+physical_mem_block.start]
  96.   mov  [esp+28],eax
  97.   mov  eax,[0x3010]
  98.   mov  eax,[eax+TASKDATA.pid]
  99.   mov  [esi+physical_mem_block.flags],eax
  100.   popad
  101.   ret
  102. .addblock:
  103.   call Insert_Block
  104.   inc  dword [num_physical_mem_blocks]
  105.   mov  eax,[esi+physical_mem_block.start]
  106.   mov  [esp+28],eax
  107.   mov  ecx,[0x3010]
  108.   mov  ecx,[ecx+TASKDATA.pid]
  109.   mov  [esi+physical_mem_block.flags],ecx
  110.   mov  ecx,[esi+physical_mem_block.size]
  111.   mov  [esi+physical_mem_block.size],ebx
  112.   sub  ecx,ebx
  113.   mov  [esi+physical_mem_block.sizeof+physical_mem_block.size],ecx  
  114.   add  ebx,[esi+physical_mem_block.start]
  115.   mov  [esi+physical_mem_block.sizeof+physical_mem_block.start],ebx
  116.   mov  dword [esi+physical_mem_block.sizeof+physical_mem_block.flags],0
  117.   popad
  118.   ret
  119. Free_Physical_Block:
  120. ;input:
  121. ;  eax - address
  122. ;output:
  123. ;  none
  124.   pushad
  125.   test eax,eax
  126.   jz   .ret
  127.   mov  ebx,eax
  128.   xor  eax,eax
  129.   mov  esi,physical_mem_blocks
  130. .loop:
  131.   cmp  ebx,[esi+physical_mem_block.start]
  132.   jz   .endloop
  133.   inc  eax
  134.   add  esi,physical_mem_block.sizeof
  135.   cmp  eax,[num_physical_mem_blocks]
  136.   jl   .loop
  137.   jmp  .ret
  138. .endloop:
  139.   mov  dword [esi+physical_mem_block.flags],0
  140.   test eax,eax
  141.   jz   .no_union_previous
  142.   cmp  dword [esi-physical_mem_block.sizeof+physical_mem_block.flags],0
  143.   jnz  .no_union_previous
  144.   mov  ebx,[esi-physical_mem_block.sizeof+physical_mem_block.start]
  145.   add  ebx,[esi-physical_mem_block.sizeof+physical_mem_block.size]
  146.   cmp  ebx,[esi+physical_mem_block.start]
  147.   jnz  .no_union_previous
  148.   mov  ebx,[esi+physical_mem_block.size]
  149.   add  [esi-physical_mem_block.sizeof+physical_mem_block.size],ebx
  150.   call Delete_Block
  151.   dec  eax
  152.   dec  [num_physical_mem_blocks]
  153. .no_union_previous:
  154.   inc  eax
  155.   cmp  eax,[num_physical_mem_blocks]
  156.   jge  .no_union_next
  157.   cmp  dword [esi+physical_mem_block.sizeof+physical_mem_block.flags],0
  158.   jnz  .no_union_next
  159.   mov  ebx,[esi+physical_mem_block.start]
  160.   add  ebx,[esi+physical_mem_block.size]
  161.   cmp  ebx,[esi+physical_mem_block.sizeof+physical_mem_block.start]
  162.   jnz  .no_union_next
  163.   mov  ebx,[esi+physical_mem_block.sizeof+physical_mem_block.size]
  164.   add  [esi+physical_mem_block.size],ebx
  165.   call Delete_Block
  166.   dec  [num_physical_mem_blocks]
  167. .no_union_next:
  168. .ret:
  169.   popad
  170.   ret
  171.  
  172. sys_allocate_physical_block:
  173. ;eax - subfunction number
  174.   mov  eax,ebx
  175.   call Allocate_Physical_Block
  176.   mov  [esp+36],eax
  177.   ret
  178. sys_free_physical_block:
  179. ;eax - subfunction number
  180.   mov  eax,ebx
  181.   call Free_Physical_Block
  182.   ret
  183. sys_set_buffer:
  184.   add  ecx,std_application_base_address
  185. isys_set_buffer:                 ;for using in kernel
  186. ;eax - subfunction number
  187. ;ebx - physical address
  188. ;ecx - buffer start
  189. ;edx - buffer size
  190.   lea  edi,[ebx+second_base_address]
  191.   mov  esi,ecx
  192.   mov  ecx,edx
  193.   cld
  194.   rep  movsb
  195.   ret
  196. sys_get_buffer:
  197.   add  ecx,std_application_base_address
  198. isys_get_buffer:                 ;for using in kernel
  199. ;eax - subfunction number
  200. ;ebx - physical address
  201. ;ecx - buffer start
  202. ;edx - buffer size
  203.   mov  edi,ecx
  204.   lea  esi,[ebx+second_base_address]
  205.   mov  ecx,edx
  206.   cld
  207.   rep  movsb
  208.   ret
  209. sys_internal_services:
  210.   cmp  eax,4
  211.   jle  sys_sheduler
  212.   cmp  eax,5
  213.   jz   sys_allocate_physical_block
  214.   cmp  eax,6
  215.   jz   sys_free_physical_block
  216.   cmp  eax,7
  217.   jz   sys_set_buffer
  218.   cmp  eax,8
  219.   jz   sys_get_buffer
  220.   ret