Subversion Repositories Kolibri OS

Rev

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

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