Subversion Repositories Kolibri OS

Rev

Rev 9872 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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