Subversion Repositories Kolibri OS

Rev

Rev 5998 | Rev 6792 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2016. All rights reserved. ;;
  4. ;;  Distributed under terms of the GNU General Public License.  ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 6502 $
  9.  
  10.  
  11. DRV_COMPAT   equ  5  ;minimal required drivers version
  12. DRV_CURRENT  equ  6  ;current drivers model version
  13.  
  14. DRV_VERSION equ (DRV_COMPAT shl 16) or DRV_CURRENT
  15. PID_KERNEL  equ 1    ;os_idle thread
  16.  
  17.  
  18.  
  19. align 4
  20. proc get_notify stdcall, p_ev:dword
  21.  
  22. .wait:
  23.         mov     ebx, [current_slot]
  24.         test    dword [ebx+APPDATA.event_mask], EVENT_NOTIFY
  25.         jz      @f
  26.         and     dword [ebx+APPDATA.event_mask], not EVENT_NOTIFY
  27.         mov     edi, [p_ev]
  28.         mov     dword [edi], EV_INTR
  29.         mov     eax, [ebx+APPDATA.event]
  30.         mov     dword [edi+4], eax
  31.         ret
  32. @@:
  33.         call    change_task
  34.         jmp     .wait
  35. endp
  36.  
  37. align 4
  38. proc pci_read32 stdcall, bus:dword, devfn:dword, reg:dword
  39.         push    ebx
  40.         xor     eax, eax
  41.         xor     ebx, ebx
  42.         mov     ah, byte [bus]
  43.         mov     al, 6
  44.         mov     bh, byte [devfn]
  45.         mov     bl, byte [reg]
  46.         call    pci_read_reg
  47.         pop     ebx
  48.         ret
  49. endp
  50.  
  51. align 4
  52. proc pci_read16 stdcall, bus:dword, devfn:dword, reg:dword
  53.         push    ebx
  54.         xor     eax, eax
  55.         xor     ebx, ebx
  56.         mov     ah, byte [bus]
  57.         mov     al, 5
  58.         mov     bh, byte [devfn]
  59.         mov     bl, byte [reg]
  60.         call    pci_read_reg
  61.         pop     ebx
  62.         ret
  63. endp
  64.  
  65. align 4
  66. proc pci_read8 stdcall, bus:dword, devfn:dword, reg:dword
  67.         push    ebx
  68.         xor     eax, eax
  69.         xor     ebx, ebx
  70.         mov     ah, byte [bus]
  71.         mov     al, 4
  72.         mov     bh, byte [devfn]
  73.         mov     bl, byte [reg]
  74.         call    pci_read_reg
  75.         pop     ebx
  76.         ret
  77. endp
  78.  
  79. align 4
  80. proc pci_write8 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
  81.         push    ebx
  82.         xor     eax, eax
  83.         xor     ebx, ebx
  84.         mov     ah, byte [bus]
  85.         mov     al, 8
  86.         mov     bh, byte [devfn]
  87.         mov     bl, byte [reg]
  88.         mov     ecx, [val]
  89.         call    pci_write_reg
  90.         pop     ebx
  91.         ret
  92. endp
  93.  
  94. align 4
  95. proc pci_write16 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
  96.         push    ebx
  97.         xor     eax, eax
  98.         xor     ebx, ebx
  99.         mov     ah, byte [bus]
  100.         mov     al, 9
  101.         mov     bh, byte [devfn]
  102.         mov     bl, byte [reg]
  103.         mov     ecx, [val]
  104.         call    pci_write_reg
  105.         pop     ebx
  106.         ret
  107. endp
  108.  
  109. align 4
  110. proc pci_write32 stdcall, bus:dword, devfn:dword, reg:dword, val:dword
  111.         push    ebx
  112.         xor     eax, eax
  113.         xor     ebx, ebx
  114.         mov     ah, byte [bus]
  115.         mov     al, 10
  116.         mov     bh, byte [devfn]
  117.         mov     bl, byte [reg]
  118.         mov     ecx, [val]
  119.         call    pci_write_reg
  120.         pop     ebx
  121.         ret
  122. endp
  123.  
  124. handle     equ  IOCTL.handle
  125. io_code    equ  IOCTL.io_code
  126. input      equ  IOCTL.input
  127. inp_size   equ  IOCTL.inp_size
  128. output     equ  IOCTL.output
  129. out_size   equ  IOCTL.out_size
  130.  
  131.  
  132. align 4
  133. proc srv_handler stdcall, ioctl:dword
  134.         mov     esi, [ioctl]
  135.         test    esi, esi
  136.         jz      .err
  137.  
  138.         mov     edi, [esi+handle]
  139.         cmp     [edi+SRV.magic], ' SRV'
  140.         jne     .fail
  141.  
  142.         cmp     [edi+SRV.size], sizeof.SRV
  143.         jne     .fail
  144.  
  145. ;        stdcall [edi+SRV.srv_proc], esi
  146.         mov     eax, [edi+SRV.srv_proc]
  147.         test    eax, eax
  148.         jz      .fail
  149.         stdcall eax, esi
  150.         ret
  151. .fail:
  152.         xor     eax, eax
  153.         not     eax
  154.         mov     [esi+output], eax
  155.         mov     [esi+out_size], 4
  156.         ret
  157. .err:
  158.         xor     eax, eax
  159.         not     eax
  160.         ret
  161. endp
  162.  
  163. ; param
  164. ;  ecx= io_control
  165. ;
  166. ; retval
  167. ;  eax= error code
  168.  
  169. align 4
  170. srv_handlerEx:
  171.         cmp     ecx, OS_BASE
  172.         jae     .fail
  173.  
  174.         mov     eax, [ecx+handle]
  175.         cmp     [eax+SRV.magic], ' SRV'
  176.         jne     .fail
  177.  
  178.         cmp     [eax+SRV.size], sizeof.SRV
  179.         jne     .fail
  180.  
  181. ;        stdcall [eax+SRV.srv_proc], ecx
  182.         mov     eax, [eax+SRV.srv_proc]
  183.         test    eax, eax
  184.         jz      .fail
  185.         stdcall eax, ecx
  186.         ret
  187. .fail:
  188.         or      eax, -1
  189.         ret
  190.  
  191. restore  handle
  192. restore  io_code
  193. restore  input
  194. restore  inp_size
  195. restore  output
  196. restore  out_size
  197.  
  198. align 4
  199. proc get_service stdcall, sz_name:dword
  200.         mov     eax, [sz_name]
  201.         test    eax, eax
  202.         jnz     @F
  203.         ret
  204. @@:
  205.         mov     edx, [srv.fd]
  206. @@:
  207.         cmp     edx, srv.fd-SRV.fd
  208.         je      .not_load
  209.  
  210.         stdcall strncmp, edx, [sz_name], 16
  211.         test    eax, eax
  212.         mov     eax, edx
  213.         je      .nothing
  214.  
  215.         mov     edx, [edx+SRV.fd]
  216.         jmp     @B
  217. .not_load:
  218.         mov     eax, [sz_name]
  219.         push    edi
  220.         sub     esp, 36
  221.         mov     edi, esp
  222.         mov     dword [edi], '/sys'
  223.         mov     dword [edi+4], '/dri'
  224.         mov     dword [edi+8], 'vers'
  225.         mov     byte [edi+12], '/'
  226. @@:
  227.         mov     dl, [eax]
  228.         mov     [edi+13], dl
  229.         inc     eax
  230.         inc     edi
  231.         test    dl, dl
  232.         jnz     @b
  233.         mov     dword [edi+12], '.sys'
  234.         mov     byte [edi+16], 0
  235.         mov     edi, esp
  236.         stdcall load_pe_driver, edi, 0
  237.         add     esp, 36
  238.         pop     edi
  239. .nothing:
  240.         ret
  241. endp
  242.  
  243. reg_service:
  244.         xor     eax, eax
  245.         mov     ecx, [esp+8]
  246.         jecxz   .nothing
  247.         push    sizeof.SRV
  248.         push    ecx
  249.         pushd   [esp+12]
  250.         call    reg_service_ex
  251. .nothing:
  252.         ret     8
  253.  
  254. reg_usb_driver:
  255.         push    sizeof.USBSRV
  256.         pushd   [esp+12]
  257.         pushd   [esp+12]
  258.         call    reg_service_ex
  259.         test    eax, eax
  260.         jz      .nothing
  261.         mov     ecx, [esp+12]
  262.         mov     [eax+USBSRV.usb_func], ecx
  263. .nothing:
  264.         ret     12
  265.  
  266. proc reg_service_ex stdcall, name:dword, handler:dword, srvsize:dword
  267.  
  268.         push    ebx
  269.  
  270.         xor     eax, eax
  271.  
  272.         cmp     [name], eax
  273.         je      .fail
  274.  
  275. ;        cmp     [handler], eax
  276. ;        je      .fail
  277.  
  278.         mov     eax, [srvsize]
  279.         call    malloc
  280.         test    eax, eax
  281.         jz      .fail
  282.  
  283.         push    esi
  284.         push    edi
  285.         mov     edi, eax
  286.         mov     esi, [name]
  287.         movsd
  288.         movsd
  289.         movsd
  290.         movsd
  291.         pop     edi
  292.         pop     esi
  293.  
  294.         mov     [eax+SRV.magic], ' SRV'
  295.         mov     [eax+SRV.size], sizeof.SRV
  296.  
  297.         mov     ebx, srv.fd-SRV.fd
  298.         mov     edx, [ebx+SRV.fd]
  299.         mov     [eax+SRV.fd], edx
  300.         mov     [eax+SRV.bk], ebx
  301.         mov     [ebx+SRV.fd], eax
  302.         mov     [edx+SRV.bk], eax
  303.  
  304.         mov     ecx, [handler]
  305.         mov     [eax+SRV.srv_proc], ecx
  306.         pop     ebx
  307.         ret
  308. .fail:
  309.         xor     eax, eax
  310.         pop     ebx
  311.         ret
  312. endp
  313.  
  314. align 4
  315. proc get_proc stdcall, exp:dword, sz_name:dword
  316.  
  317.         mov     edx, [exp]
  318. .next:
  319.         mov     eax, [edx]
  320.         test    eax, eax
  321.         jz      .end
  322.  
  323.         push    edx
  324.         stdcall strncmp, eax, [sz_name], 16
  325.         pop     edx
  326.         test    eax, eax
  327.         jz      .ok
  328.  
  329.         add     edx, 8
  330.         jmp     .next
  331. .ok:
  332.         mov     eax, [edx+4]
  333. .end:
  334.         ret
  335. endp
  336.  
  337. align 4
  338. proc get_coff_sym stdcall, pSym:dword,count:dword, sz_sym:dword
  339.  
  340. @@:
  341.         stdcall strncmp, [pSym], [sz_sym], 8
  342.         test    eax, eax
  343.         jz      .ok
  344.         add     [pSym], 18
  345.         dec     [count]
  346.         jnz     @b
  347.         xor     eax, eax
  348.         ret
  349. .ok:
  350.         mov     eax, [pSym]
  351.         mov     eax, [eax+8]
  352.         ret
  353. endp
  354.  
  355. align 4
  356. proc get_curr_task
  357.         mov     eax, [CURRENT_TASK]
  358.         shl     eax, 8
  359.         ret
  360. endp
  361.  
  362. align 4
  363. proc get_fileinfo stdcall, file_name:dword, info:dword
  364.            locals
  365.              cmd     dd ?
  366.              offset  dd ?
  367.                      dd ?
  368.              count   dd ?
  369.              buff    dd ?
  370.                      db ?
  371.              name    dd ?
  372.            endl
  373.  
  374.         xor     eax, eax
  375.         mov     ebx, [file_name]
  376.         mov     ecx, [info]
  377.  
  378.         mov     [cmd], 5
  379.         mov     [offset], eax
  380.         mov     [offset+4], eax
  381.         mov     [count], eax
  382.         mov     [buff], ecx
  383.         mov     byte [buff+4], al
  384.         mov     [name], ebx
  385.  
  386.         mov     eax, 70
  387.         lea     ebx, [cmd]
  388.         int     0x40
  389.         ret
  390. endp
  391.  
  392. align 4
  393. proc read_file stdcall,file_name:dword, buffer:dword, off:dword,\
  394.                                      bytes:dword
  395.            locals
  396.              cmd     dd ?
  397.              offset  dd ?
  398.                      dd ?
  399.              count   dd ?
  400.              buff    dd ?
  401.                      db ?
  402.              name    dd ?
  403.            endl
  404.  
  405.         xor     eax, eax
  406.         mov     ebx, [file_name]
  407.         mov     ecx, [off]
  408.         mov     edx, [bytes]
  409.         mov     esi, [buffer]
  410.  
  411.         mov     [cmd], eax
  412.         mov     [offset], ecx
  413.         mov     [offset+4], eax
  414.         mov     [count], edx
  415.         mov     [buff], esi
  416.         mov     byte [buff+4], al
  417.         mov     [name], ebx
  418.  
  419.         pushad
  420.         lea     ebx, [cmd]
  421.         call    file_system_lfn_protected
  422.         popad
  423.         ret
  424. endp
  425.  
  426. ; description
  427. ;  allocate kernel memory and loads the specified file
  428. ;
  429. ; param
  430. ;  file_name= path to file
  431. ;
  432. ; retval
  433. ;  eax= file image in kernel memory
  434. ;  ebx= size of file
  435. ;
  436. ; warging
  437. ;  You mast call kernel_free() to delete each file
  438. ;  loaded by the load_file() function
  439.  
  440. align 4
  441. proc load_file stdcall, file_name:dword
  442.            locals
  443.              attr       dd ?
  444.              flags      dd ?
  445.              cr_time    dd ?
  446.              cr_date    dd ?
  447.              acc_time   dd ?
  448.              acc_date   dd ?
  449.              mod_time   dd ?
  450.              mod_date   dd ?
  451.              file_size  dd ?
  452.  
  453.              file       dd ?
  454.              file2      dd ?
  455.            endl
  456.  
  457.         push    esi
  458.         push    edi
  459.  
  460.         lea     eax, [attr]
  461.         stdcall get_fileinfo, [file_name], eax
  462.         test    eax, eax
  463.         jnz     .fail
  464.  
  465.         mov     eax, [file_size]
  466.         cmp     eax, 1024*1024*16
  467.         ja      .fail
  468.  
  469.         stdcall kernel_alloc, [file_size]
  470.         mov     [file], eax
  471.         test    eax, eax
  472.         jz      .fail
  473.  
  474.         stdcall read_file, [file_name], eax, dword 0, [file_size]
  475.         cmp     ebx, [file_size]
  476.         jne     .cleanup
  477.  
  478.         mov     eax, [file]
  479.         cmp     dword [eax], 0x4B43504B
  480.         jne     .exit
  481.         mov     ebx, [eax+4]
  482.         mov     [file_size], ebx
  483.         stdcall kernel_alloc, ebx
  484.  
  485.         test    eax, eax
  486.         jz      .cleanup
  487.  
  488.         mov     [file2], eax
  489.  
  490.         pushad
  491.         mov     ecx, unpack_mutex
  492.         call    mutex_lock
  493.         popad
  494.  
  495.         stdcall unpack, [file], eax
  496.  
  497.         pushad
  498.         mov     ecx, unpack_mutex
  499.         call    mutex_unlock
  500.         popad
  501.  
  502.         stdcall kernel_free, [file]
  503.         mov     eax, [file2]
  504.         mov     ebx, [file_size]
  505. .exit:
  506.         push    eax
  507.         lea     edi, [eax+ebx]    ;cleanup remain space
  508.         mov     ecx, 4096         ;from file end
  509.         and     ebx, 4095
  510.         jz      @f
  511.         sub     ecx, ebx
  512.         xor     eax, eax
  513.         cld
  514.         rep stosb
  515. @@:
  516.         mov     ebx, [file_size]
  517.         pop     eax
  518.         pop     edi
  519.         pop     esi
  520.         ret
  521. .cleanup:
  522.         stdcall kernel_free, [file]
  523. .fail:
  524.         xor     eax, eax
  525.         xor     ebx, ebx
  526.         pop     edi
  527.         pop     esi
  528.         ret
  529. endp
  530.  
  531. ; description
  532. ;  allocate user memory and loads the specified file
  533. ;
  534. ; param
  535. ;  file_name= path to file
  536. ;
  537. ; retval
  538. ;  eax= file image in user memory
  539. ;  ebx= size of file
  540. ;
  541. ; warging
  542. ;  You mast call kernel_free() to delete each file
  543. ;  loaded by the load_file() function
  544.  
  545. align 4
  546. proc load_file_umode stdcall, file_name:dword
  547.            locals
  548.              attr       dd ?
  549.              flags      dd ?
  550.              cr_time    dd ?
  551.              cr_date    dd ?
  552.              acc_time   dd ?
  553.              acc_date   dd ?
  554.              mod_time   dd ?
  555.              mod_date   dd ?
  556.              file_size  dd ?
  557.  
  558.              km_file    dd ?
  559.              um_file    dd ?
  560.            endl
  561.  
  562.         push    esi
  563.         push    edi
  564.         push    ebx
  565.  
  566.         lea     eax, [attr]
  567.         stdcall get_fileinfo, [file_name], eax   ;find file and get info
  568.         test    eax, eax
  569.         jnz     .err_1
  570.  
  571.         mov     eax, [file_size]
  572.         cmp     eax, 1024*1024*16                ;to be enough for anybody (c)
  573.         ja      .err_1
  574.                                                  ;it is very likely that the file is packed
  575.         stdcall kernel_alloc, [file_size]        ;with kpack, so allocate memory from kernel heap
  576.         mov     [km_file], eax
  577.         test    eax, eax
  578.         jz      .err_1
  579.  
  580.         stdcall read_file, [file_name], eax, dword 0, [file_size]
  581.         cmp     ebx, [file_size]
  582.  
  583.         jne     .err_2
  584.  
  585.         mov     eax, [km_file]
  586.         cmp     dword [eax], 0x4B43504B          ; check kpack signature
  587.         jne     .raw_file
  588.  
  589.         mov     ebx, [eax+4]                     ;get real size of file
  590.         mov     [file_size], ebx
  591.         stdcall user_alloc, ebx                  ;and allocate space from user heap
  592.         mov     [um_file], eax
  593.         test    eax, eax
  594.         jz      .err_2
  595.  
  596.         mov     edx, [file_size]                 ;preallocate page memory
  597.         shr     eax, 10
  598.         lea     edi, [page_tabs+eax]
  599.         add     edx, 4095
  600.         shr     edx, 12
  601. @@:
  602.         call    alloc_page
  603.         test    eax, eax
  604.         jz      .err_3
  605.  
  606.         or      eax, PG_UWR
  607.         stosd
  608.         dec     edx
  609.         jnz     @B
  610.  
  611.         pushad
  612.         mov     ecx, unpack_mutex
  613.         call    mutex_lock
  614.  
  615.         stdcall unpack, [km_file], [um_file]
  616.  
  617.         mov     ecx, unpack_mutex
  618.         call    mutex_unlock
  619.         popad
  620.  
  621.         stdcall kernel_free, [km_file]           ;we don't need packed file anymore
  622. .exit:
  623.  
  624.         mov     edi, [um_file]
  625.         mov     esi, [um_file]
  626.         mov     eax, [file_size]
  627.         mov     edx, eax
  628.  
  629.         add     edi, eax                         ;cleanup remain space
  630.         mov     ecx, 4096                        ;from file end
  631.         and     eax, 4095
  632.         jz      @f
  633.         sub     ecx, eax
  634.         xor     eax, eax
  635.         cld
  636.         rep stosb
  637. @@:
  638.         mov     eax, [um_file]
  639.  
  640.         pop     ebx
  641.         pop     edi
  642.         pop     esi
  643.         ret
  644.  
  645. .raw_file:                                       ; sometimes we load unpacked file
  646.         stdcall user_alloc, ebx                  ; allocate space from user heap
  647.         mov     [um_file], eax
  648.  
  649.         test    eax, eax
  650.         jz      .err_2
  651.  
  652.         shr     eax, 10                          ; and remap pages.
  653.  
  654.         mov     ecx, [file_size]
  655.         add     ecx, 4095
  656.         shr     ecx, 12
  657.  
  658.         mov     esi, [km_file]
  659.         shr     esi, 10
  660.         add     esi, page_tabs
  661.  
  662.         lea     edi, [page_tabs+eax]
  663.  
  664.         cld
  665. @@:
  666.         lodsd
  667.         and     eax, 0xFFFFF000
  668.         or      eax, PG_UWR
  669.         stosd
  670.         loop    @B
  671.  
  672.         stdcall free_kernel_space, [km_file]     ; release allocated kernel space
  673.         jmp     .exit                            ; physical pages still in use
  674. .err_3:
  675.         stdcall user_free, [um_file]
  676. .err_2:
  677.         stdcall kernel_free, [km_file]
  678. .err_1:
  679.         xor     eax, eax
  680.         xor     edx, edx
  681.  
  682.         pop     ebx
  683.         pop     edi
  684.         pop     esi
  685.         ret
  686. endp
  687.  
  688.  
  689. uglobal
  690. align 4
  691. unpack_mutex MUTEX
  692. endg
  693.  
  694. align 4
  695. proc get_proc_ex stdcall uses ebx esi, proc_name:dword, imports:dword
  696.         mov     ebx, [imports]
  697.         test    ebx, ebx
  698.         jz      .end
  699.         xor     esi, esi
  700. .look_up:
  701.  
  702.         mov     eax, [ebx+32]
  703.         mov     eax, [OS_BASE+eax+esi*4]
  704.         add     eax, OS_BASE
  705.         stdcall strncmp, eax, [proc_name], 256
  706.         test    eax, eax
  707.         jz      .ok
  708.  
  709.         inc     esi
  710.         cmp     esi, [ebx+24]
  711.         jb      .look_up
  712. .end:
  713.         xor     eax, eax
  714.         ret
  715. .ok:
  716.         mov     eax, [ebx+28]
  717.         mov     eax, [OS_BASE+eax+esi*4]
  718.         add     eax, OS_BASE
  719.         ret
  720. endp
  721.  
  722. align 4
  723. proc fix_coff_symbols stdcall uses ebx esi, sec:dword, symbols:dword,\
  724.                       sym_count:dword, strings:dword, imports:dword
  725.            locals
  726.              retval dd ?
  727.            endl
  728.  
  729.         mov     edi, [symbols]
  730.         mov     [retval], 1
  731. .fix:
  732.         movzx   ebx, [edi+COFF_SYM.SectionNumber]
  733.         test    ebx, ebx
  734.         jnz     .internal
  735.         mov     eax, dword [edi+COFF_SYM.Name]
  736.         test    eax, eax
  737.         jnz     @F
  738.  
  739.         mov     edi, [edi+4]
  740.         add     edi, [strings]
  741. @@:
  742.         push    edi
  743.         stdcall get_proc_ex, edi, [imports]
  744.         pop     edi
  745.  
  746.         xor     ebx, ebx
  747.         test    eax, eax
  748.         jnz     @F
  749.  
  750.         ; disable debug msg
  751.         ;mov     esi, msg_unresolved
  752.         ;call    sys_msg_board_str
  753.         ;mov     esi, edi
  754.         ;call    sys_msg_board_str
  755.         ;mov     esi, msg_CR
  756.         ;call    sys_msg_board_str
  757.  
  758.         mov     [retval], 0
  759. @@:
  760.         mov     edi, [symbols]
  761.         mov     [edi+COFF_SYM.Value], eax
  762.         jmp     .next
  763. .internal:
  764.         cmp     bx, -1
  765.         je      .next
  766.         cmp     bx, -2
  767.         je      .next
  768.  
  769.         dec     ebx
  770.         shl     ebx, 3
  771.         lea     ebx, [ebx+ebx*4]
  772.         add     ebx, [sec]
  773.  
  774.         mov     eax, [ebx+COFF_SECTION.VirtualAddress]
  775.         add     [edi+COFF_SYM.Value], eax
  776. .next:
  777.         add     edi, sizeof.COFF_SYM
  778.         mov     [symbols], edi
  779.         dec     [sym_count]
  780.         jnz     .fix
  781.         mov     eax, [retval]
  782.         ret
  783. endp
  784.  
  785. align 4
  786. proc fix_coff_relocs stdcall uses ebx esi, coff:dword, sym:dword, \
  787.         delta:dword
  788.            locals
  789.              n_sec     dd ?
  790.            endl
  791.  
  792.         mov     eax, [coff]
  793.         movzx   ebx, [eax+COFF_HEADER.nSections]
  794.         mov     [n_sec], ebx
  795.         lea     esi, [eax+20]
  796. .fix_sec:
  797.         mov     edi, [esi+COFF_SECTION.PtrReloc]
  798.         add     edi, [coff]
  799.  
  800.         movzx   ecx, [esi+COFF_SECTION.NumReloc]
  801.         test    ecx, ecx
  802.         jz      .next
  803. .reloc_loop:
  804.         mov     ebx, [edi+COFF_RELOC.SymIndex]
  805.         add     ebx, ebx
  806.         lea     ebx, [ebx+ebx*8]
  807.         add     ebx, [sym]
  808.  
  809.         mov     edx, [ebx+COFF_SYM.Value]
  810.  
  811.         cmp     [edi+COFF_RELOC.Type], 6
  812.         je      .dir_32
  813.  
  814.         cmp     [edi+COFF_RELOC.Type], 20
  815.         jne     .next_reloc
  816. .rel_32:
  817.         mov     eax, [edi+COFF_RELOC.VirtualAddress]
  818.         add     eax, [esi+COFF_SECTION.VirtualAddress]
  819.         sub     edx, eax
  820.         sub     edx, 4
  821.         jmp     .fix
  822. .dir_32:
  823.         mov     eax, [edi+COFF_RELOC.VirtualAddress]
  824.         add     eax, [esi+COFF_SECTION.VirtualAddress]
  825. .fix:
  826.         add     eax, [delta]
  827.         add     [eax], edx
  828. .next_reloc:
  829.         add     edi, 10
  830.         dec     ecx
  831.         jnz     .reloc_loop
  832. .next:
  833.         add     esi, sizeof.COFF_SECTION
  834.         dec     [n_sec]
  835.         jnz     .fix_sec
  836. .exit:
  837.         ret
  838. endp
  839.  
  840. align 4
  841. proc rebase_coff stdcall uses ebx esi, coff:dword, sym:dword, \
  842.         delta:dword
  843.            locals
  844.              n_sec     dd ?
  845.            endl
  846.  
  847.         mov     eax, [coff]
  848.         movzx   ebx, [eax+COFF_HEADER.nSections]
  849.         mov     [n_sec], ebx
  850.         lea     esi, [eax+20]
  851.         mov     edx, [delta]
  852. .fix_sec:
  853.         mov     edi, [esi+COFF_SECTION.PtrReloc]
  854.         add     edi, [coff]
  855.  
  856.         movzx   ecx, [esi+COFF_SECTION.NumReloc]
  857.         test    ecx, ecx
  858.         jz      .next
  859. .reloc_loop:
  860.         cmp     [edi+COFF_RELOC.Type], 6
  861.         jne     .next_reloc
  862. .dir_32:
  863.         mov     eax, [edi+COFF_RELOC.VirtualAddress]
  864.         add     eax, [esi+COFF_SECTION.VirtualAddress]
  865.         add     [eax+edx], edx
  866. .next_reloc:
  867.         add     edi, 10
  868.         dec     ecx
  869.         jnz     .reloc_loop
  870. .next:
  871.         add     esi, sizeof.COFF_SECTION
  872.         dec     [n_sec]
  873.         jnz     .fix_sec
  874. .exit:
  875.         ret
  876. endp
  877.  
  878. ; in: edx -> COFF_SECTION struct
  879. ; out: eax = alignment as mask for bits to drop
  880. coff_get_align:
  881. ; Rules:
  882. ; - if alignment is not given, use default = 4K;
  883. ; - if alignment is given and is no more than 4K, use it;
  884. ; - if alignment is more than 4K, revert to 4K.
  885.         push    ecx
  886.         mov     cl, byte [edx+COFF_SECTION.Characteristics+2]
  887.         mov     eax, 1
  888.         shr     cl, 4
  889.         dec     cl
  890.         js      .default
  891.         cmp     cl, 12
  892.         jbe     @f
  893. .default:
  894.         mov     cl, 12
  895. @@:
  896.         shl     eax, cl
  897.         pop     ecx
  898.         dec     eax
  899.         ret
  900.  
  901. align 4
  902. proc load_library stdcall, file_name:dword
  903.     locals
  904.         fullname    dd  ?
  905.         fileinfo    rb  40
  906.         coff        dd  ?
  907.         img_base    dd  ?
  908.     endl
  909.  
  910. ; resolve file name
  911.         stdcall kernel_alloc, maxPathLength
  912.         mov     [fullname], eax
  913.         mov     ebx, [file_name]
  914.         stdcall get_full_file_name, eax, maxPathLength
  915.         test    eax, eax
  916.         jz      .fail
  917. ; scan for required DLL in list of already loaded for this process,
  918. ; ignore timestamp
  919.         cli
  920.         mov     esi, [current_process]
  921.         mov     edi, [fullname]
  922.         mov     ebx, [esi+PROC.dlls_list_ptr]
  923.         test    ebx, ebx
  924.         jz      .not_in_process
  925.         mov     esi, [ebx+HDLL.fd]
  926. .scan_in_process:
  927.         cmp     esi, ebx
  928.         jz      .not_in_process
  929.         mov     eax, [esi+HDLL.parent]
  930.         add     eax, DLLDESCR.name
  931.         stdcall strncmp, eax, edi, -1
  932.         test    eax, eax
  933.         jnz     .next_in_process
  934. ; simple variant: load DLL which is already loaded in this process
  935. ; just increment reference counters and return address of exports table
  936.         inc     [esi+HDLL.refcount]
  937.         mov     ecx, [esi+HDLL.parent]
  938.         inc     [ecx+DLLDESCR.refcount]
  939.         mov     eax, [ecx+DLLDESCR.exports]
  940.         sub     eax, [ecx+DLLDESCR.defaultbase]
  941.         add     eax, [esi+HDLL.base]
  942.         sti
  943.         push    eax
  944.         stdcall kernel_free, [fullname]
  945.         pop     eax
  946.         ret
  947.  
  948. .next_in_process:
  949.         mov     esi, [esi+HDLL.fd]
  950.         jmp     .scan_in_process
  951.  
  952. .not_in_process:
  953. ; scan in full list, compare timestamp
  954.         sti
  955.         lea     eax, [fileinfo]
  956.         stdcall get_fileinfo, edi, eax
  957.         test    eax, eax
  958.         jnz     .fail
  959.         cli
  960.         mov     esi, [dll_list.fd]
  961. .scan_for_dlls:
  962.         cmp     esi, dll_list
  963.         jz      .load_new
  964.         lea     eax, [esi+DLLDESCR.name]
  965.         stdcall strncmp, eax, edi, -1
  966.         test    eax, eax
  967.         jnz     .continue_scan
  968. .test_prev_dll:
  969.         mov     eax, dword [fileinfo+24]; last modified time
  970.         mov     edx, dword [fileinfo+28]; last modified date
  971.         cmp     dword [esi+DLLDESCR.timestamp], eax
  972.         jnz     .continue_scan
  973.         cmp     dword [esi+DLLDESCR.timestamp+4], edx
  974.         jz      .dll_already_loaded
  975. .continue_scan:
  976.         mov     esi, [esi+DLLDESCR.fd]
  977.         jmp     .scan_for_dlls
  978.  
  979. ; new DLL
  980. .load_new:
  981.         sti
  982. ; load file
  983.         stdcall load_file, edi
  984.         test    eax, eax
  985.         jz      .fail
  986.         mov     [coff], eax
  987.         mov     dword [fileinfo+32], ebx
  988.  
  989. ; allocate DLLDESCR struct; size is DLLDESCR.sizeof plus size of DLL name
  990.         mov     esi, edi
  991.         mov     ecx, -1
  992.         xor     eax, eax
  993.         repnz scasb
  994.         not     ecx
  995.         lea     eax, [ecx+sizeof.DLLDESCR]
  996.         push    ecx
  997.         call    malloc
  998.         pop     ecx
  999.         test    eax, eax
  1000.         jz      .fail_and_free_coff
  1001. ; save timestamp
  1002.         lea     edi, [eax+DLLDESCR.name]
  1003.         rep movsb
  1004.         mov     esi, eax
  1005.         mov     eax, dword [fileinfo+24]
  1006.         mov     dword [esi+DLLDESCR.timestamp], eax
  1007.         mov     eax, dword [fileinfo+28]
  1008.         mov     dword [esi+DLLDESCR.timestamp+4], eax
  1009.  
  1010. ; calculate size of loaded DLL
  1011.         mov     edx, [coff]
  1012.         movzx   ecx, [edx+COFF_HEADER.nSections]
  1013.         xor     ebx, ebx
  1014.  
  1015.         add     edx, 20
  1016. @@:
  1017.         call    coff_get_align
  1018.         add     ebx, eax
  1019.         not     eax
  1020.         and     ebx, eax
  1021.         add     ebx, [edx+COFF_SECTION.SizeOfRawData]
  1022.         add     edx, sizeof.COFF_SECTION
  1023.         dec     ecx
  1024.         jnz     @B
  1025. ; it must be nonzero and not too big
  1026.         mov     [esi+DLLDESCR.size], ebx
  1027.         test    ebx, ebx
  1028.         jz      .fail_and_free_dll
  1029.         cmp     ebx, MAX_DEFAULT_DLL_ADDR-MIN_DEFAULT_DLL_ADDR
  1030.         ja      .fail_and_free_dll
  1031. ; allocate memory for kernel-side image
  1032.         stdcall kernel_alloc, ebx
  1033.         test    eax, eax
  1034.         jz      .fail_and_free_dll
  1035.         mov     [esi+DLLDESCR.data], eax
  1036. ; calculate preferred base address
  1037.         add     ebx, 0x1FFF
  1038.         and     ebx, not 0xFFF
  1039.         mov     ecx, [dll_cur_addr]
  1040.         lea     edx, [ecx+ebx]
  1041.         cmp     edx, MAX_DEFAULT_DLL_ADDR
  1042.         jb      @f
  1043.         mov     ecx, MIN_DEFAULT_DLL_ADDR
  1044.         lea     edx, [ecx+ebx]
  1045. @@:
  1046.         mov     [esi+DLLDESCR.defaultbase], ecx
  1047.         mov     [dll_cur_addr], edx
  1048.  
  1049. ; copy sections and set correct values for VirtualAddress'es in headers
  1050.         push    esi
  1051.         mov     edx, [coff]
  1052.         movzx   ebx, [edx+COFF_HEADER.nSections]
  1053.         mov     edi, eax
  1054.         add     edx, 20
  1055.         cld
  1056. @@:
  1057.         call    coff_get_align
  1058.         add     ecx, eax
  1059.         add     edi, eax
  1060.         not     eax
  1061.         and     ecx, eax
  1062.         and     edi, eax
  1063.         mov     [edx+COFF_SECTION.VirtualAddress], ecx
  1064.         add     ecx, [edx+COFF_SECTION.SizeOfRawData]
  1065.         mov     esi, [edx+COFF_SECTION.PtrRawData]
  1066.         push    ecx
  1067.         mov     ecx, [edx+COFF_SECTION.SizeOfRawData]
  1068.         test    esi, esi
  1069.         jnz     .copy
  1070.         xor     eax, eax
  1071.         rep stosb
  1072.         jmp     .next
  1073. .copy:
  1074.         add     esi, [coff]
  1075.         rep movsb
  1076. .next:
  1077.         pop     ecx
  1078.         add     edx, sizeof.COFF_SECTION
  1079.         dec     ebx
  1080.         jnz     @B
  1081.         pop     esi
  1082.  
  1083. ; save some additional data from COFF file
  1084. ; later we will use COFF header, headers for sections and symbol table
  1085. ; and also relocations table for all sections
  1086.         mov     edx, [coff]
  1087.         mov     ebx, [edx+COFF_HEADER.pSymTable]
  1088.         mov     edi, dword [fileinfo+32]
  1089.         sub     edi, ebx
  1090.         jc      .fail_and_free_data
  1091.         mov     [esi+DLLDESCR.symbols_lim], edi
  1092.         add     ebx, edx
  1093.         movzx   ecx, [edx+COFF_HEADER.nSections]
  1094.         lea     ecx, [ecx*5]
  1095.         lea     edi, [edi+ecx*8+20]
  1096.         add     edx, 20
  1097. @@:
  1098.         movzx   eax, [edx+COFF_SECTION.NumReloc]
  1099.         lea     eax, [eax*5]
  1100.         lea     edi, [edi+eax*2]
  1101.         add     edx, sizeof.COFF_SECTION
  1102.         sub     ecx, 5
  1103.         jnz     @b
  1104.         stdcall kernel_alloc, edi
  1105.         test    eax, eax
  1106.         jz      .fail_and_free_data
  1107.         mov     edx, [coff]
  1108.         movzx   ecx, [edx+COFF_HEADER.nSections]
  1109.         lea     ecx, [ecx*5]
  1110.         lea     ecx, [ecx*2+5]
  1111.         mov     [esi+DLLDESCR.coff_hdr], eax
  1112.         push    esi
  1113.         mov     esi, edx
  1114.         mov     edi, eax
  1115.         rep movsd
  1116.         pop     esi
  1117.         mov     [esi+DLLDESCR.symbols_ptr], edi
  1118.         push    esi
  1119.         mov     ecx, [edx+COFF_HEADER.nSymbols]
  1120.         mov     [esi+DLLDESCR.symbols_num], ecx
  1121.         mov     ecx, [esi+DLLDESCR.symbols_lim]
  1122.         mov     esi, ebx
  1123.         rep movsb
  1124.         pop     esi
  1125.         mov     ebx, [esi+DLLDESCR.coff_hdr]
  1126.         push    esi
  1127.         movzx   eax, [edx+COFF_HEADER.nSections]
  1128.         lea     edx, [ebx+20]
  1129. @@:
  1130.         movzx   ecx, [edx+COFF_SECTION.NumReloc]
  1131.         lea     ecx, [ecx*5]
  1132.         mov     esi, [edx+COFF_SECTION.PtrReloc]
  1133.         mov     [edx+COFF_SECTION.PtrReloc], edi
  1134.         sub     [edx+COFF_SECTION.PtrReloc], ebx
  1135.         add     esi, [coff]
  1136.         shr     ecx, 1
  1137.         rep movsd
  1138.         adc     ecx, ecx
  1139.         rep movsw
  1140.         add     edx, sizeof.COFF_SECTION
  1141.         dec     eax
  1142.         jnz     @b
  1143.         pop     esi
  1144.  
  1145. ; fixup symbols
  1146.         mov     edx, ebx
  1147.         mov     eax, [ebx+COFF_HEADER.nSymbols]
  1148.         add     edx, 20
  1149.         mov     ecx, [esi+DLLDESCR.symbols_num]
  1150.         lea     ecx, [ecx*9]
  1151.         add     ecx, ecx
  1152.         add     ecx, [esi+DLLDESCR.symbols_ptr]
  1153.  
  1154.         stdcall fix_coff_symbols, edx, [esi+DLLDESCR.symbols_ptr], eax, \
  1155.                 ecx, 0
  1156. ;          test eax, eax
  1157. ;          jnz @F
  1158. ;
  1159. ;@@:
  1160.  
  1161.         stdcall get_coff_sym, [esi+DLLDESCR.symbols_ptr], [ebx+COFF_HEADER.nSymbols], szEXPORTS
  1162.         test    eax, eax
  1163.         jnz     @F
  1164.  
  1165.         stdcall get_coff_sym, [esi+DLLDESCR.symbols_ptr], [ebx+COFF_HEADER.nSymbols], sz_EXPORTS
  1166. @@:
  1167.         mov     [esi+DLLDESCR.exports], eax
  1168.  
  1169. ; fix relocs in the hidden copy in kernel memory to default address
  1170. ; it is first fix; usually this will be enough, but second fix
  1171. ; can be necessary if real load address will not equal assumption
  1172.         mov     eax, [esi+DLLDESCR.data]
  1173.         sub     eax, [esi+DLLDESCR.defaultbase]
  1174.         stdcall fix_coff_relocs, ebx, [esi+DLLDESCR.symbols_ptr], eax
  1175.  
  1176.         stdcall kernel_free, [coff]
  1177.  
  1178.         cli
  1179. ; initialize DLLDESCR struct
  1180.         and     dword [esi+DLLDESCR.refcount], 0; no HDLLs yet; later it will be incremented
  1181.         mov     [esi+DLLDESCR.fd], dll_list
  1182.         mov     eax, [dll_list.bk]
  1183.         mov     [dll_list.bk], esi
  1184.         mov     [esi+DLLDESCR.bk], eax
  1185.         mov     [eax+DLLDESCR.fd], esi
  1186. .dll_already_loaded:
  1187.         stdcall kernel_free, [fullname]
  1188.         inc     [esi+DLLDESCR.refcount]
  1189.         push    esi
  1190.         call    init_heap
  1191.         pop     esi
  1192.         mov     edi, [esi+DLLDESCR.size]
  1193.         stdcall user_alloc_at, [esi+DLLDESCR.defaultbase], edi
  1194.         test    eax, eax
  1195.         jnz     @f
  1196.         stdcall user_alloc, edi
  1197.         test    eax, eax
  1198.         jz      .fail_and_dereference
  1199. @@:
  1200.         mov     [img_base], eax
  1201.         mov     eax, sizeof.HDLL
  1202.         call    malloc
  1203.         test    eax, eax
  1204.         jz      .fail_and_free_user
  1205.         mov     ebx, [CURRENT_TASK]
  1206.         shl     ebx, 5
  1207.         mov     edx, [CURRENT_TASK+ebx+TASKDATA.pid]
  1208.         mov     [eax+HDLL.pid], edx
  1209.         push    eax
  1210.         call    init_dlls_in_thread
  1211.         pop     ebx
  1212.         test    eax, eax
  1213.         jz      .fail_and_free_user
  1214.         mov     edx, [eax+HDLL.fd]
  1215.         mov     [ebx+HDLL.fd], edx
  1216.         mov     [ebx+HDLL.bk], eax
  1217.         mov     [eax+HDLL.fd], ebx
  1218.         mov     [edx+HDLL.bk], ebx
  1219.         mov     eax, ebx
  1220.         mov     ebx, [img_base]
  1221.         mov     [eax+HDLL.base], ebx
  1222.         mov     [eax+HDLL.size], edi
  1223.         mov     [eax+HDLL.refcount], 1
  1224.         mov     [eax+HDLL.parent], esi
  1225.         mov     edx, ebx
  1226.         shr     edx, 12
  1227.         or      dword [page_tabs+(edx-1)*4], DONT_FREE_BLOCK
  1228. ; copy entries of page table from kernel-side image to usermode
  1229. ; use copy-on-write for user-mode image, so map as readonly
  1230.         xor     edi, edi
  1231.         mov     ecx, [esi+DLLDESCR.data]
  1232.         shr     ecx, 12
  1233. .map_pages_loop:
  1234.         mov     eax, [page_tabs+ecx*4]
  1235.         and     eax, not 0xFFF
  1236.         or      al, PG_UR
  1237.         xchg    eax, [page_tabs+edx*4]
  1238.         test    al, 1
  1239.         jz      @f
  1240.         call    free_page
  1241. @@:
  1242.         invlpg  [ebx+edi]
  1243.         inc     ecx
  1244.         inc     edx
  1245.         add     edi, 0x1000
  1246.         cmp     edi, [esi+DLLDESCR.size]
  1247.         jb      .map_pages_loop
  1248.  
  1249. ; if real user-mode base is not equal to preferred base, relocate image
  1250.         sub     ebx, [esi+DLLDESCR.defaultbase]
  1251.         jz      @f
  1252.         stdcall rebase_coff, [esi+DLLDESCR.coff_hdr], [esi+DLLDESCR.symbols_ptr], ebx
  1253. @@:
  1254.  
  1255.         mov     eax, [esi+DLLDESCR.exports]
  1256.         sub     eax, [esi+DLLDESCR.defaultbase]
  1257.         add     eax, [img_base]
  1258.         sti
  1259.         ret
  1260.  
  1261. .fail_and_free_data:
  1262.         stdcall kernel_free, [esi+DLLDESCR.data]
  1263. .fail_and_free_dll:
  1264.         mov     eax, esi
  1265.         call    free
  1266. .fail_and_free_coff:
  1267.         stdcall kernel_free, [coff]
  1268. .fail:
  1269.         stdcall kernel_free, [fullname]
  1270.         xor     eax, eax
  1271.         ret
  1272.  
  1273. .fail_and_free_user:
  1274.         stdcall user_free, [img_base]
  1275. .fail_and_dereference:
  1276.         mov     eax, 1  ; delete 1 reference
  1277.         call    dereference_dll
  1278.         sti
  1279.         xor     eax, eax
  1280.         ret
  1281. endp
  1282.  
  1283. ; initialize [APPDATA.dlls_list_ptr] for given thread
  1284. ; DLL is per-process object, so APPDATA.dlls_list_ptr must be
  1285. ; kept in sync for all threads of one process.
  1286. ; out: eax = APPDATA.dlls_list_ptr if all is OK,
  1287. ; NULL if memory allocation failed
  1288. init_dlls_in_thread:
  1289.         mov     ebx, [current_process]
  1290.         mov     eax, [ebx+PROC.dlls_list_ptr]
  1291.         test    eax, eax
  1292.         jnz     .ret
  1293.  
  1294.         mov     eax, 8
  1295.         call    malloc                               ; FIXME
  1296.         test    eax, eax
  1297.         jz      .ret
  1298.  
  1299.         mov     [eax], eax
  1300.         mov     [eax+4], eax
  1301.  
  1302.         mov     ebx, [current_process]
  1303.         mov     [ebx+PROC.dlls_list_ptr], eax
  1304. .ret:
  1305.         ret
  1306.  
  1307. ; in: eax = number of references to delete, esi -> DLLDESCR struc
  1308. dereference_dll:
  1309.         sub     [esi+DLLDESCR.refcount], eax
  1310.         jnz     .ret
  1311.         mov     eax, [esi+DLLDESCR.fd]
  1312.         mov     edx, [esi+DLLDESCR.bk]
  1313.         mov     [eax+DLLDESCR.bk], edx
  1314.         mov     [edx+DLLDESCR.fd], eax
  1315.         stdcall kernel_free, [esi+DLLDESCR.coff_hdr]
  1316.         stdcall kernel_free, [esi+DLLDESCR.data]
  1317.         mov     eax, esi
  1318.         call    free
  1319. .ret:
  1320.         ret
  1321.  
  1322. destroy_hdll:
  1323.         push    ebx ecx esi edi
  1324.         mov     ebx, [eax+HDLL.base]
  1325.         mov     esi, [eax+HDLL.parent]
  1326.         mov     edx, [esi+DLLDESCR.size]
  1327.  
  1328.         push    eax
  1329.         mov     esi, [eax+HDLL.parent]
  1330.         mov     eax, [eax+HDLL.refcount]
  1331.         call    dereference_dll
  1332.         pop     eax
  1333.         mov     edx, [eax+HDLL.bk]
  1334.         mov     ebx, [eax+HDLL.fd]
  1335.         mov     [ebx+HDLL.bk], edx
  1336.         mov     [edx+HDLL.fd], ebx
  1337.         call    free
  1338.         pop     edi esi ecx ebx
  1339.         ret
  1340.  
  1341. ; ecx -> APPDATA for slot, esi = dlls_list_ptr
  1342. destroy_all_hdlls:
  1343.         test    esi, esi
  1344.         jz      .ret
  1345. .loop:
  1346.         mov     eax, [esi+HDLL.fd]
  1347.         cmp     eax, esi
  1348.         jz      free
  1349.         call    destroy_hdll
  1350.         jmp     .loop
  1351. .ret:
  1352.         ret
  1353.  
  1354. align 4
  1355. stop_all_services:
  1356.         push    ebp
  1357.         mov     edx, [srv.fd]
  1358. .next:
  1359.         cmp     edx, srv.fd-SRV.fd
  1360.         je      .done
  1361.         cmp     [edx+SRV.magic], ' SRV'
  1362.         jne     .next
  1363.         cmp     [edx+SRV.size], sizeof.SRV
  1364.         jne     .next
  1365.  
  1366.         mov     ebx, [edx+SRV.entry]
  1367.         mov     edx, [edx+SRV.fd]
  1368.         test    ebx, ebx
  1369.         jz      .next
  1370.  
  1371.         push    edx
  1372.         mov     ebp, esp
  1373.         push    0
  1374.         push    -1
  1375.         call    ebx
  1376.         mov     esp, ebp
  1377.         pop     edx
  1378.         jmp     .next
  1379. .done:
  1380.         pop     ebp
  1381.         ret
  1382.  
  1383. ; param
  1384. ;  eax= size
  1385. ;  ebx= pid
  1386.  
  1387. align 4
  1388. create_kernel_object:
  1389.  
  1390.         push    ebx
  1391.         call    malloc
  1392.         pop     ebx
  1393.         test    eax, eax
  1394.         jz      .fail
  1395.  
  1396.         mov     ecx, [current_slot]
  1397.         add     ecx, APP_OBJ_OFFSET
  1398.  
  1399.         pushfd
  1400.         cli
  1401.         mov     edx, [ecx+APPOBJ.fd]
  1402.         mov     [eax+APPOBJ.fd], edx
  1403.         mov     [eax+APPOBJ.bk], ecx
  1404.         mov     [eax+APPOBJ.pid], ebx
  1405.  
  1406.         mov     [ecx+APPOBJ.fd], eax
  1407.         mov     [edx+APPOBJ.bk], eax
  1408.         popfd
  1409. .fail:
  1410.         ret
  1411.  
  1412. ; param
  1413. ;  eax= object
  1414.  
  1415. align 4
  1416. destroy_kernel_object:
  1417.  
  1418.         pushfd
  1419.         cli
  1420.         mov     ebx, [eax+APPOBJ.fd]
  1421.         mov     ecx, [eax+APPOBJ.bk]
  1422.         mov     [ebx+APPOBJ.bk], ecx
  1423.         mov     [ecx+APPOBJ.fd], ebx
  1424.         popfd
  1425.  
  1426.         xor     edx, edx       ;clear common header
  1427.         mov     [eax], edx
  1428.         mov     [eax+4], edx
  1429.         mov     [eax+8], edx
  1430.         mov     [eax+12], edx
  1431.         mov     [eax+16], edx
  1432.  
  1433.         call    free           ;release object memory
  1434.         ret
  1435.  
  1436. ; param
  1437. ;  ecx= size
  1438.  
  1439. align 4
  1440. create_object:
  1441.  
  1442.         push    esi
  1443.         push    edi
  1444.         pushfd
  1445.         cli
  1446.  
  1447.         mov     esi, [current_process]
  1448.         mov     eax, [esi+PROC.ht_free]
  1449.         mov     edi, [esi+PROC.ht_next]
  1450.         dec     eax
  1451.         js      .err0
  1452.  
  1453.         mov     [esi+PROC.ht_free], eax
  1454.         mov     eax, [esi+PROC.htab+edi*4]
  1455.         mov     [esi+PROC.ht_next], eax
  1456.         popfd
  1457.  
  1458.         mov     eax, ecx
  1459.         call    malloc
  1460.         test    eax, eax
  1461.         jz      .err1
  1462.  
  1463.         mov     [eax+FUTEX.handle], edi
  1464.         mov     [esi+PROC.htab+edi*4], eax
  1465.         pop     edi
  1466.         pop     esi
  1467.         ret
  1468.  
  1469. .err1:
  1470.         pushfd
  1471.         cli
  1472.  
  1473.         mov     eax, [esi+PROC.ht_next]
  1474.         mov     [esi+PROC.htab+edi*4], eax
  1475.         mov     [esi+PROC.ht_next], edi
  1476.         inc     [esi+PROC.ht_free]
  1477. .err0:
  1478.         popfd
  1479.         pop     edi
  1480.         pop     esi
  1481.         xor     eax, eax
  1482.         ret
  1483.  
  1484.