Subversion Repositories Kolibri OS

Rev

Rev 9692 | Rev 9823 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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