Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2288 clevermous 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
5363 yogev_ezra 3
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
2288 clevermous 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7
 
8
$Revision: 5365 $
9
 
10
 
11
align 4
12
proc alloc_page
13
 
14
        pushfd
15
        cli
16
        push    ebx
17
;//-
18
        cmp     [pg_data.pages_free], 1
19
        jle     .out_of_memory
20
;//-
21
 
22
        mov     ebx, [page_start]
23
        mov     ecx, [page_end]
24
.l1:
25
        bsf     eax, [ebx];
26
        jnz     .found
27
        add     ebx, 4
28
        cmp     ebx, ecx
29
        jb      .l1
30
        pop     ebx
31
        popfd
32
        xor     eax, eax
33
        ret
34
.found:
35
;//-
36
        dec     [pg_data.pages_free]
37
        jz      .out_of_memory
38
;//-
39
        btr     [ebx], eax
40
        mov     [page_start], ebx
41
        sub     ebx, sys_pgmap
42
        lea     eax, [eax+ebx*8]
43
        shl     eax, 12
44
;//-       dec [pg_data.pages_free]
45
        pop     ebx
46
        popfd
47
        ret
48
;//-
49
.out_of_memory:
50
        mov     [pg_data.pages_free], 1
51
        xor     eax, eax
52
        pop     ebx
53
        popfd
54
        ret
55
;//-
56
endp
57
 
58
align 4
59
proc alloc_pages stdcall, count:dword
60
        pushfd
61
        push    ebx
62
        push    edi
63
        cli
64
        mov     eax, [count]
65
        add     eax, 7
66
        shr     eax, 3
67
        mov     [count], eax
68
;//-
69
        mov     ebx, [pg_data.pages_free]
70
        sub     ebx, 9
71
        js      .out_of_memory
72
        shr     ebx, 3
73
        cmp     eax, ebx
74
        jg      .out_of_memory
75
;//-
76
        mov     ecx, [page_start]
77
        mov     ebx, [page_end]
78
.find:
79
        mov     edx, [count]
80
        mov     edi, ecx
81
.match:
82
        cmp     byte [ecx], 0xFF
83
        jne     .next
84
        dec     edx
85
        jz      .ok
86
        inc     ecx
87
        cmp     ecx, ebx
88
        jb      .match
89
.out_of_memory:
90
.fail:
91
        xor     eax, eax
92
        pop     edi
93
        pop     ebx
94
        popfd
95
        ret
96
.next:
97
        inc     ecx
98
        cmp     ecx, ebx
99
        jb      .find
100
        pop     edi
101
        pop     ebx
102
        popfd
103
        xor     eax, eax
104
        ret
105
.ok:
106
        sub     ecx, edi
107
        inc     ecx
108
        push    esi
109
        mov     esi, edi
110
        xor     eax, eax
111
        rep stosb
112
        sub     esi, sys_pgmap
113
        shl     esi, 3+12
114
        mov     eax, esi
115
        mov     ebx, [count]
116
        shl     ebx, 3
117
        sub     [pg_data.pages_free], ebx
118
        pop     esi
119
        pop     edi
120
        pop     ebx
121
        popfd
122
        ret
123
endp
124
 
125
align 4
4424 Serge 126
;proc map_page stdcall,lin_addr:dword,phis_addr:dword,flags:dword
127
map_page:
2288 clevermous 128
        push    ebx
5357 serge 129
        mov     eax, [esp+12]              ; phis_addr
4424 Serge 130
        or      eax, [esp+16]              ; flags
5357 serge 131
        and     eax, [pte_valid_mask]
4424 Serge 132
        mov     ebx, [esp+8]               ; lin_addr
2288 clevermous 133
        shr     ebx, 12
134
        mov     [page_tabs+ebx*4], eax
4424 Serge 135
        mov     eax, [esp+8]               ; lin_addr
136
        pop     ebx
2288 clevermous 137
        invlpg  [eax]
4424 Serge 138
        ret     12
2288 clevermous 139
 
140
align 4
141
map_space:    ;not implemented
142
 
143
 
144
        ret
145
 
146
 
147
align 4
148
proc free_page
149
;arg:  eax  page address
150
        pushfd
151
        cli
152
        shr     eax, 12                       ;page index
153
        bts     dword [sys_pgmap], eax        ;that's all!
154
        cmc
155
        adc     [pg_data.pages_free], 0
156
        shr     eax, 3
157
        and     eax, not 3                    ;dword offset from page_map
158
        add     eax, sys_pgmap
159
        cmp     [page_start], eax
160
        ja      @f
161
        popfd
162
        ret
163
@@:
164
        mov     [page_start], eax
165
        popfd
166
        ret
167
endp
168
 
2414 Serge 169
align 4
2288 clevermous 170
proc map_io_mem stdcall, base:dword, size:dword, flags:dword
171
 
172
        push    ebx
173
        push    edi
174
        mov     eax, [size]
175
        add     eax, [base]
176
        add     eax, 4095
177
        and     eax, -4096
178
        mov     ecx, [base]
179
        and     ecx, -4096
180
        sub     eax, ecx
181
        mov     [size], eax
182
 
183
        stdcall alloc_kernel_space, eax
184
        test    eax, eax
185
        jz      .fail
186
        push    eax
187
 
188
        mov     edi, 0x1000
189
        mov     ebx, eax
190
        mov     ecx, [size]
191
        mov     edx, [base]
192
        shr     eax, 12
193
        shr     ecx, 12
194
        or      edx, [flags]
5357 serge 195
        and     edx, [pte_valid_mask]
2288 clevermous 196
@@:
197
        mov     [page_tabs+eax*4], edx
198
        invlpg  [ebx]
199
        inc     eax
200
        add     ebx, edi
201
        add     edx, edi
202
        loop    @B
203
 
204
        pop     eax
205
        mov     edx, [base]
206
        and     edx, 4095
207
        add     eax, edx
208
.fail:
209
        pop     edi
210
        pop     ebx
211
        ret
212
endp
213
 
214
; param
215
;  eax= page base + page flags
216
;  ebx= linear address
217
;  ecx= count
218
 
219
align 4
220
commit_pages:
221
        test    ecx, ecx
222
        jz      .fail
223
 
224
        push    edi
225
        push    eax
226
        push    ecx
227
        mov     ecx, pg_data.mutex
228
        call    mutex_lock
229
        pop     ecx
230
        pop     eax
231
 
5357 serge 232
        and     eax, [pte_valid_mask ]
2288 clevermous 233
        mov     edi, ebx
234
        shr     edi, 12
235
        lea     edi, [page_tabs+edi*4]
236
@@:
237
        stosd
238
        invlpg  [ebx]
239
        add     eax, 0x1000
240
        add     ebx, 0x1000
241
        loop    @B
242
 
243
        pop     edi
244
 
245
        mov     ecx, pg_data.mutex
246
        call    mutex_unlock
247
.fail:
248
        ret
249
 
250
 
251
; param
252
;  eax= base
253
;  ecx= count
254
 
255
align 4
256
release_pages:
257
 
258
        push    ebp
259
        push    esi
260
        push    edi
261
        push    ebx
262
 
263
        mov     esi, eax
264
        mov     edi, eax
265
 
266
        shr     esi, 12
267
        lea     esi, [page_tabs+esi*4]
268
 
269
        push    ecx
270
        mov     ecx, pg_data.mutex
271
        call    mutex_lock
272
        pop     ecx
273
 
274
        mov     ebp, [pg_data.pages_free]
275
        mov     ebx, [page_start]
276
        mov     edx, sys_pgmap
277
@@:
278
        xor     eax, eax
279
        xchg    eax, [esi]
280
        invlpg  [edi]
281
 
282
        test    eax, 1
283
        jz      .next
284
 
285
        shr     eax, 12
286
        bts     [edx], eax
287
        cmc
288
        adc     ebp, 0
289
        shr     eax, 3
290
        and     eax, -4
291
        add     eax, edx
292
        cmp     eax, ebx
293
        jae     .next
294
 
295
        mov     ebx, eax
296
.next:
297
        add     edi, 0x1000
298
        add     esi, 4
299
        loop    @B
300
 
301
        mov     [pg_data.pages_free], ebp
302
        mov     ecx, pg_data.mutex
303
        call    mutex_unlock
304
 
305
        pop     ebx
306
        pop     edi
307
        pop     esi
308
        pop     ebp
309
        ret
310
 
311
; param
312
;  eax= base
313
;  ecx= count
314
 
315
align 4
316
unmap_pages:
317
 
318
        push    edi
319
 
320
        mov     edi, eax
321
        mov     edx, eax
322
 
323
        shr     edi, 10
324
        add     edi, page_tabs
325
 
326
        xor     eax, eax
327
@@:
328
        stosd
329
        invlpg  [edx]
330
        add     edx, 0x1000
331
        loop    @b
332
 
333
        pop     edi
334
        ret
335
 
336
 
337
align 4
338
proc map_page_table stdcall, lin_addr:dword, phis_addr:dword
339
        push    ebx
340
        mov     ebx, [lin_addr]
341
        shr     ebx, 22
342
        mov     eax, [phis_addr]
343
        and     eax, not 0xFFF
5365 serge 344
        or      eax, PG_UWR
2288 clevermous 345
        mov     dword [master_tab+ebx*4], eax
346
        mov     eax, [lin_addr]
347
        shr     eax, 10
348
        add     eax, page_tabs
349
        invlpg  [eax]
350
        pop     ebx
351
        ret
352
endp
353
 
5057 clevermous 354
uglobal
355
sb16_buffer_allocated db 0
356
endg
357
 
358
; Allocates [.size] bytes so that the target memory block
359
; is inside one 64K page for 24-bit DMA controller,
360
; that is, somewhere between 00xx0000h and 00xxFFFFh.
361
proc alloc_dma24
362
; Implementation note.
363
; The only user of that function is SB16 driver,
364
; so just return a statically allocated buffer.
365
virtual at esp
366
                dd      ? ; return address
367
.size           dd      ?
368
end virtual
369
        cmp     [sb16_buffer_allocated], 0
370
        jnz     .fail
371
        inc     [sb16_buffer_allocated]
372
        mov     eax, SB16Buffer
373
        ret     4
374
.fail:
375
        xor     eax, eax
376
        ret     4
377
endp
378
 
5032 clevermous 379
; Allocates a physical page for master page table
380
; that duplicates first Mb of OS_BASE at address 0;
381
; used for starting APs and for shutting down,
382
; where it is important to execute code in trivial-mapped pages.
383
; Returns eax = allocated physical page.
384
proc create_trampoline_pgmap
385
; The only non-trivial moment:
386
; we need a linear address to fill information,
387
; but we don't need it outside of this function,
388
; so we're returning physical address.
389
; Therefore, allocate memory with kernel_alloc,
390
; this will allocate physical page and a linear address somewhere,
391
; and deallocate only linear address with free_kernel_space.
392
        stdcall kernel_alloc, 0x1000
393
        mov     edi, eax
394
        mov     esi, master_tab
395
        mov     ecx, 1024
396
        rep movsd
397
        mov     ecx, [master_tab+(OS_BASE shr 20)]
398
        mov     [eax], ecx
399
        mov     edi, eax
400
        call    get_pg_addr
401
        push    eax
402
        stdcall free_kernel_space, edi
403
        pop     eax
404
        ret
405
endp
406
 
2288 clevermous 407
align 4
408
proc init_LFB
409
           locals
410
             pg_count dd ?
411
           endl
412
 
413
        cmp     dword [LFBAddress], -1
414
        jne     @f
3732 Serge 415
        mov     [BOOT_VARS+BOOT_MTRR], byte 2
2398 mario79 416
; max VGA=640*480*4=1228800 bytes
417
; + 32*640*4=81920 bytes for mouse pointer
418
        stdcall alloc_pages, ((1228800+81920)/4096)
2288 clevermous 419
 
420
        push    eax
421
        call    alloc_page
422
        stdcall map_page_table, LFB_BASE, eax
423
        pop     eax
5356 serge 424
        or      eax, PG_UWR
2288 clevermous 425
        mov     ebx, LFB_BASE
2398 mario79 426
; max VGA=640*480*4=1228800 bytes
427
; + 32*640*4=81920 bytes for mouse pointer
428
        mov     ecx, (1228800+81920)/4096
2288 clevermous 429
        call    commit_pages
430
        mov     [LFBAddress], dword LFB_BASE
431
        ret
432
@@:
433
        test    [SCR_MODE], word 0100000000000000b
434
        jnz     @f
3732 Serge 435
        mov     [BOOT_VARS+BOOT_MTRR], byte 2
2288 clevermous 436
        ret
437
@@:
438
        call    init_mtrr
439
 
440
        mov     edx, LFB_BASE
441
        mov     esi, [LFBAddress]
442
        mov     edi, 0x00C00000
443
        mov     dword [exp_lfb+4], edx
444
 
445
        shr     edi, 12
446
        mov     [pg_count], edi
447
        shr     edi, 10
448
 
449
        bt      [cpu_caps], CAPS_PSE
450
        jnc     .map_page_tables
5356 serge 451
        or      esi, PDE_LARGE+PG_UWR
5130 serge 452
        mov     edx, sys_proc+PROC.pdt_0+(LFB_BASE shr 20)
2288 clevermous 453
@@:
454
        mov     [edx], esi
455
        add     edx, 4
456
        add     esi, 0x00400000
457
        dec     edi
458
        jnz     @B
459
 
460
        bt      [cpu_caps], CAPS_PGE
461
        jnc     @F
5130 serge 462
        or      dword [sys_proc+PROC.pdt_0+(LFB_BASE shr 20)], PG_GLOBAL
2288 clevermous 463
@@:
464
        mov     dword [LFBAddress], LFB_BASE
465
        mov     eax, cr3      ;flush TLB
466
        mov     cr3, eax
467
        ret
468
 
469
.map_page_tables:
470
 
471
@@:
472
        call    alloc_page
473
        stdcall map_page_table, edx, eax
474
        add     edx, 0x00400000
475
        dec     edi
476
        jnz     @B
477
 
478
        mov     eax, [LFBAddress]
479
        mov     edi, page_tabs + (LFB_BASE shr 10)
5359 serge 480
        or      eax, PG_GLOBAL+PG_UWR
481
        and     eax, [pte_valid_mask]
2288 clevermous 482
        mov     ecx, [pg_count]
483
        cld
484
@@:
485
        stosd
486
        add     eax, 0x1000
5359 serge 487
        loop    @B
2288 clevermous 488
 
489
        mov     dword [LFBAddress], LFB_BASE
490
        mov     eax, cr3      ;flush TLB
491
        mov     cr3, eax
492
 
493
        ret
494
endp
495
 
496
align 4
497
proc new_mem_resize stdcall, new_size:dword
498
 
2594 Serge 499
        push    ebx
500
        push    esi
501
        push    edi
2288 clevermous 502
 
2594 Serge 503
        mov     edx, [current_slot]
5130 serge 504
        mov     ebx, [edx+APPDATA.process]
505
 
506
        cmp     [ebx+PROC.heap_base], 0
2594 Serge 507
        jne     .exit
508
 
2288 clevermous 509
        mov     edi, [new_size]
510
        add     edi, 4095
511
        and     edi, not 4095
512
        mov     [new_size], edi
513
 
5130 serge 514
        mov     esi, [ebx+PROC.mem_used]
2288 clevermous 515
        add     esi, 4095
516
        and     esi, not 4095
517
 
518
        cmp     edi, esi
2594 Serge 519
        ja      .expand
520
        je      .exit
2288 clevermous 521
 
2594 Serge 522
        mov     ebx, edi
2288 clevermous 523
        shr     edi, 12
524
        shr     esi, 12
2594 Serge 525
 
526
        mov     ecx, pg_data.mutex
527
        call    mutex_lock
2288 clevermous 528
@@:
529
        mov     eax, [app_page_tabs+edi*4]
530
        test    eax, 1
531
        jz      .next
2594 Serge 532
 
533
        mov     dword [app_page_tabs+edi*4], 0
2288 clevermous 534
        invlpg  [ebx]
535
        call    free_page
536
 
537
.next:
2594 Serge 538
        inc     edi
2595 Serge 539
        add     ebx, 0x1000
2288 clevermous 540
        cmp     edi, esi
541
        jb      @B
542
 
2594 Serge 543
        mov     ecx, pg_data.mutex
544
        call    mutex_unlock
545
 
2288 clevermous 546
.update_size:
2594 Serge 547
        mov     edx, [current_slot]
2288 clevermous 548
        mov     ebx, [new_size]
5130 serge 549
        mov     edx, [edx+APPDATA.process]
550
        mov     [edx+PROC.mem_used], ebx
2594 Serge 551
.exit:
552
        pop     edi
553
        pop     esi
554
        pop     ebx
2288 clevermous 555
        xor     eax, eax
556
        ret
2594 Serge 557
 
2288 clevermous 558
.expand:
559
 
2594 Serge 560
        mov     ecx, pg_data.mutex
561
        call    mutex_lock
2288 clevermous 562
 
2594 Serge 563
        xchg    esi, edi
564
 
565
        push    esi                   ;new size
566
        push    edi                   ;old size
567
 
2288 clevermous 568
        add     edi, 0x3FFFFF
569
        and     edi, not(0x3FFFFF)
570
        add     esi, 0x3FFFFF
571
        and     esi, not(0x3FFFFF)
572
 
2594 Serge 573
        cmp     edi, esi
2288 clevermous 574
        jae     .grow
2594 Serge 575
 @@:
2288 clevermous 576
        call    alloc_page
577
        test    eax, eax
2594 Serge 578
        jz      .exit_fail
2288 clevermous 579
 
580
        stdcall map_page_table, edi, eax
581
 
582
        push    edi
583
        shr     edi, 10
584
        add     edi, page_tabs
585
        mov     ecx, 1024
586
        xor     eax, eax
587
        cld
588
        rep stosd
589
        pop     edi
590
 
591
        add     edi, 0x00400000
592
        cmp     edi, esi
593
        jb      @B
594
.grow:
2594 Serge 595
        pop     edi                   ;old size
596
        pop     ecx                   ;new size
2288 clevermous 597
 
2594 Serge 598
        shr     edi, 10
599
        shr     ecx, 10
600
        sub     ecx, edi
601
        shr     ecx, 2                ;pages count
602
        mov     eax, 2
603
 
604
        add     edi, app_page_tabs
2288 clevermous 605
        rep stosd
606
 
2594 Serge 607
        mov     ecx, pg_data.mutex
608
        call    mutex_unlock
2288 clevermous 609
 
610
        jmp     .update_size
2594 Serge 611
 
612
.exit_fail:
2288 clevermous 613
        mov     ecx, pg_data.mutex
614
        call    mutex_unlock
615
 
2594 Serge 616
        add     esp, 8
617
        pop     edi
618
        pop     esi
619
        pop     ebx
2288 clevermous 620
        xor     eax, eax
621
        inc     eax
622
        ret
623
endp
624
 
2594 Serge 625
 
2288 clevermous 626
; param
627
;  eax= linear address
628
;
629
; retval
630
;  eax= phisical page address
631
 
632
align 4
633
get_pg_addr:
3166 clevermous 634
        sub     eax, OS_BASE
635
        cmp     eax, 0x400000
636
        jb      @f
2288 clevermous 637
        shr     eax, 12
3166 clevermous 638
        mov     eax, [page_tabs+(eax+(OS_BASE shr 12))*4]
639
@@:
2288 clevermous 640
        and     eax, 0xFFFFF000
641
        ret
642
 
643
 
644
align 4
645
; Now it is called from core/sys32::exc_c (see stack frame there)
646
proc page_fault_handler
647
 
648
    .err_addr   equ ebp-4
649
 
650
        push    ebx               ;save exception number (#PF)
651
        mov     ebp, esp
652
        mov     ebx, cr2
653
        push    ebx               ;that is locals: .err_addr = cr2
654
        inc     [pg_data.pages_faults]
655
 
656
        mov     eax, [pf_err_code]
657
 
658
        cmp     ebx, OS_BASE      ;ebx == .err_addr
3539 clevermous 659
        jb      .user_space       ;страница в памяти приложения ;
2288 clevermous 660
 
661
        cmp     ebx, page_tabs
3539 clevermous 662
        jb      .kernel_space     ;страница в памяти ядра
2288 clevermous 663
 
664
        cmp     ebx, kernel_tabs
3539 clevermous 665
        jb      .alloc;.app_tabs  ;таблицы страниц приложения ;
666
                                  ;просто создадим одну
667
if 0 ;пока это просто лишнее
2288 clevermous 668
        cmp     ebx, LFB_BASE
3539 clevermous 669
        jb      .core_tabs        ;таблицы страниц ядра
670
                                  ;Ошибка
2288 clevermous 671
  .lfb:
3539 clevermous 672
                                  ;область LFB
673
                                  ;Ошибка
2288 clevermous 674
        jmp     .fail
675
end if
676
.core_tabs:
677
.fail:  ;simply return to caller
678
        mov     esp, ebp
679
        pop     ebx               ;restore exception number (#PF)
680
        ret
681
 
682
.user_space:
5356 serge 683
        test    eax, PG_READ
3539 clevermous 684
        jnz     .err_access       ;Страница присутствует
685
                                  ;Ошибка доступа ?
2288 clevermous 686
 
687
        shr     ebx, 12
688
        mov     ecx, ebx
689
        shr     ecx, 10
690
        mov     edx, [master_tab+ecx*4]
5356 serge 691
        test    edx, PG_READ
3539 clevermous 692
        jz      .fail             ;таблица страниц не создана
693
                                  ;неверный адрес в программе
2288 clevermous 694
 
695
        mov     eax, [page_tabs+ebx*4]
696
        test    eax, 2
3539 clevermous 697
        jz      .fail             ;адрес не зарезервирован для ;
698
                                  ;использования. Ошибка
2288 clevermous 699
.alloc:
700
        call    alloc_page
701
        test    eax, eax
702
        jz      .fail
703
 
5356 serge 704
        stdcall map_page, [.err_addr], eax, PG_UWR
2288 clevermous 705
 
706
        mov     edi, [.err_addr]
707
        and     edi, 0xFFFFF000
708
        mov     ecx, 1024
709
        xor     eax, eax
710
       ;cld     ;caller is duty for this
711
        rep stosd
712
.exit:  ;iret with repeat fault instruction
713
        add     esp, 12;clear in stack: locals(.err_addr) + #PF + ret_to_caller
714
        restore_ring3_context
715
        iretd
716
 
717
.err_access:
718
; access denied? this may be a result of copy-on-write protection for DLL
719
; check list of HDLLs
720
        and     ebx, not 0xFFF
5130 serge 721
        mov     eax, [current_process]
722
        mov     eax, [eax+PROC.dlls_list_ptr]
2288 clevermous 723
        test    eax, eax
724
        jz      .fail
725
        mov     esi, [eax+HDLL.fd]
726
.scan_hdll:
727
        cmp     esi, eax
728
        jz      .fail
729
        mov     edx, ebx
730
        sub     edx, [esi+HDLL.base]
731
        cmp     edx, [esi+HDLL.size]
732
        jb      .fault_in_hdll
733
.scan_hdll.next:
734
        mov     esi, [esi+HDLL.fd]
735
        jmp     .scan_hdll
736
.fault_in_hdll:
737
; allocate new page, map it as rw and copy data
738
        call    alloc_page
739
        test    eax, eax
740
        jz      .fail
5356 serge 741
        stdcall map_page, ebx, eax, PG_UWR
2288 clevermous 742
        mov     edi, ebx
743
        mov     ecx, 1024
744
        sub     ebx, [esi+HDLL.base]
745
        mov     esi, [esi+HDLL.parent]
746
        mov     esi, [esi+DLLDESCR.data]
747
        add     esi, ebx
748
        rep movsd
749
        jmp     .exit
750
 
751
.kernel_space:
5356 serge 752
        test    eax, PG_READ
3539 clevermous 753
        jz      .fail   ;страница не присутствует
2288 clevermous 754
 
755
        test    eax, 12 ;U/S (+below)
3539 clevermous 756
        jnz     .fail   ;приложение обратилось к памяти
757
                        ;ядра
2288 clevermous 758
       ;test    eax, 8
3539 clevermous 759
       ;jnz     .fail   ;установлен зарезервированный бит
760
                        ;в таблицах страниц. добавлено в P4/Xeon
2288 clevermous 761
 
3539 clevermous 762
;попытка записи в защищённую страницу ядра
2288 clevermous 763
 
764
        cmp     ebx, tss._io_map_0
765
        jb      .fail
766
 
767
        cmp     ebx, tss._io_map_0+8192
768
        jae     .fail
769
 
770
; io permission map
771
; copy-on-write protection
772
 
773
        call    alloc_page
774
        test    eax, eax
775
        jz      .fail
776
 
777
        push    eax
5356 serge 778
        stdcall map_page, [.err_addr], eax, dword PG_SWR
2288 clevermous 779
        pop     eax
780
        mov     edi, [.err_addr]
781
        and     edi, -4096
782
        lea     esi, [edi+(not tss._io_map_0)+1]; -tss._io_map_0
783
 
784
        mov     ebx, esi
785
        shr     ebx, 12
786
        mov     edx, [current_slot]
5356 serge 787
        or      eax, PG_SWR
2288 clevermous 788
        mov     [edx+APPDATA.io_map+ebx*4], eax
789
 
790
        add     esi, [default_io_map]
791
        mov     ecx, 4096/4
792
       ;cld     ;caller is duty for this
793
        rep movsd
794
        jmp     .exit
795
endp
796
 
797
; returns number of mapped bytes
5130 serge 798
proc map_mem_ipc stdcall, lin_addr:dword,slot:dword,\
2288 clevermous 799
                      ofs:dword,buf_size:dword,req_access:dword
5130 serge 800
        locals
801
             count   dd ?
802
             process dd ?
803
        endl
2288 clevermous 804
 
5130 serge 805
        mov     [count], 0
2288 clevermous 806
        cmp     [buf_size], 0
807
        jz      .exit
808
 
809
        mov     eax, [slot]
810
        shl     eax, 8
5130 serge 811
        mov     eax, [SLOT_BASE+eax+APPDATA.process]
812
        test    eax, eax
813
        jz      .exit
2288 clevermous 814
 
5130 serge 815
        mov     [process], eax
2288 clevermous 816
        mov     ebx, [ofs]
817
        shr     ebx, 22
5130 serge 818
        mov     eax, [eax+PROC.pdt_0+ebx*4]                 ;get page table
819
        mov     esi, [ipc_ptab]
2288 clevermous 820
        and     eax, 0xFFFFF000
821
        jz      .exit
5356 serge 822
        stdcall map_page, esi, eax, PG_SWR
2288 clevermous 823
@@:
824
        mov     edi, [lin_addr]
825
        and     edi, 0xFFFFF000
826
        mov     ecx, [buf_size]
827
        add     ecx, 4095
828
        shr     ecx, 12
5130 serge 829
        inc     ecx                  ; ???????????
2288 clevermous 830
 
831
        mov     edx, [ofs]
832
        shr     edx, 12
833
        and     edx, 0x3FF
834
.map:
835
        stdcall safe_map_page, [slot], [req_access], [ofs]
836
        jnc     .exit
5130 serge 837
        add     [count], PAGE_SIZE
838
        add     [ofs], PAGE_SIZE
2288 clevermous 839
        dec     ecx
840
        jz      .exit
5130 serge 841
 
842
        add     edi, PAGE_SIZE
2288 clevermous 843
        inc     edx
5130 serge 844
        cmp     edx, 1024
2288 clevermous 845
        jnz     .map
5130 serge 846
 
2288 clevermous 847
        inc     ebx
5130 serge 848
        mov     eax, [process]
849
        mov     eax, [eax+PROC.pdt_0+ebx*4]
2288 clevermous 850
        and     eax, 0xFFFFF000
851
        jz      .exit
5130 serge 852
 
5356 serge 853
        stdcall map_page, esi, eax, PG_SWR
2288 clevermous 854
        xor     edx, edx
855
        jmp     .map
856
.exit:
5130 serge 857
        mov     eax, [count]
2288 clevermous 858
        ret
859
endp
860
 
861
proc map_memEx stdcall, lin_addr:dword,slot:dword,\
862
                        ofs:dword,buf_size:dword,req_access:dword
5130 serge 863
        locals
864
             count   dd ?
865
             process dd ?
866
        endl
2288 clevermous 867
 
5130 serge 868
        mov     [count], 0
2288 clevermous 869
        cmp     [buf_size], 0
870
        jz      .exit
871
 
872
        mov     eax, [slot]
873
        shl     eax, 8
5130 serge 874
        mov     eax, [SLOT_BASE+eax+APPDATA.process]
875
        test    eax, eax
876
        jz      .exit
2288 clevermous 877
 
5130 serge 878
        mov     [process], eax
2288 clevermous 879
        mov     ebx, [ofs]
880
        shr     ebx, 22
5130 serge 881
        mov     eax, [eax+PROC.pdt_0+ebx*4]                 ;get page table
882
        mov     esi, [proc_mem_tab]
2288 clevermous 883
        and     eax, 0xFFFFF000
884
        jz      .exit
5356 serge 885
        stdcall map_page, esi, eax, PG_SWR
2288 clevermous 886
@@:
887
        mov     edi, [lin_addr]
888
        and     edi, 0xFFFFF000
889
        mov     ecx, [buf_size]
890
        add     ecx, 4095
891
        shr     ecx, 12
5130 serge 892
        inc     ecx                  ; ???????????
2288 clevermous 893
 
894
        mov     edx, [ofs]
895
        shr     edx, 12
896
        and     edx, 0x3FF
897
.map:
898
        stdcall safe_map_page, [slot], [req_access], [ofs]
899
        jnc     .exit
5130 serge 900
        add     [count], PAGE_SIZE
901
        add     [ofs], PAGE_SIZE
902
        dec     ecx
903
        jz      .exit
904
 
905
        add     edi, PAGE_SIZE
2288 clevermous 906
        inc     edx
5130 serge 907
        cmp     edx, 1024
2288 clevermous 908
        jnz     .map
5130 serge 909
 
910
        inc     ebx
911
        mov     eax, [process]
912
        mov     eax, [eax+PROC.pdt_0+ebx*4]
913
        and     eax, 0xFFFFF000
914
        jz      .exit
915
 
5356 serge 916
        stdcall map_page, esi, eax, PG_SWR
5130 serge 917
        xor     edx, edx
918
        jmp     .map
2288 clevermous 919
.exit:
5130 serge 920
        mov     eax, [count]
2288 clevermous 921
        ret
922
endp
923
 
924
; in: esi+edx*4 = pointer to page table entry
925
; in: [slot], [req_access], [ofs] on the stack
926
; in: edi = linear address to map
927
; out: CF cleared <=> failed
928
; destroys: only eax
929
proc safe_map_page stdcall, slot:dword, req_access:dword, ofs:dword
930
        mov     eax, [esi+edx*4]
5356 serge 931
        test    al, PG_READ
2288 clevermous 932
        jz      .not_present
933
        test    al, PG_WRITE
934
        jz      .resolve_readonly
935
; normal case: writable page, just map with requested access
936
.map:
937
        stdcall map_page, edi, eax, [req_access]
938
        stc
939
.fail:
940
        ret
941
.not_present:
942
; check for alloc-on-demand page
943
        test    al, 2
944
        jz      .fail
945
; allocate new page, save it to source page table
946
        push    ecx
947
        call    alloc_page
948
        pop     ecx
949
        test    eax, eax
950
        jz      .fail
5356 serge 951
        or      al, PG_UWR
2288 clevermous 952
        mov     [esi+edx*4], eax
953
        jmp     .map
954
.resolve_readonly:
955
; readonly page, probably copy-on-write
956
; check: readonly request of readonly page is ok
957
        test    [req_access], PG_WRITE
958
        jz      .map
959
; find control structure for this page
960
        pushf
961
        cli
962
        cld
963
        push    ebx ecx
964
        mov     eax, [slot]
965
        shl     eax, 8
5130 serge 966
        mov     eax, [SLOT_BASE+eax+APPDATA.process]
967
        mov     eax, [eax+PROC.dlls_list_ptr]
2288 clevermous 968
        test    eax, eax
969
        jz      .no_hdll
970
        mov     ecx, [eax+HDLL.fd]
971
.scan_hdll:
972
        cmp     ecx, eax
973
        jz      .no_hdll
974
        mov     ebx, [ofs]
975
        and     ebx, not 0xFFF
976
        sub     ebx, [ecx+HDLL.base]
977
        cmp     ebx, [ecx+HDLL.size]
978
        jb      .hdll_found
979
        mov     ecx, [ecx+HDLL.fd]
980
        jmp     .scan_hdll
981
.no_hdll:
982
        pop     ecx ebx
983
        popf
984
        clc
985
        ret
986
.hdll_found:
987
; allocate page, save it in page table, map it, copy contents from base
988
        mov     eax, [ecx+HDLL.parent]
989
        add     ebx, [eax+DLLDESCR.data]
990
        call    alloc_page
991
        test    eax, eax
992
        jz      .no_hdll
5356 serge 993
        or      al, PG_UWR
2288 clevermous 994
        mov     [esi+edx*4], eax
995
        stdcall map_page, edi, eax, [req_access]
996
        push    esi edi
997
        mov     esi, ebx
998
        mov     ecx, 4096/4
999
        rep movsd
1000
        pop     edi esi
1001
        pop     ecx ebx
1002
        popf
1003
        stc
1004
        ret
1005
endp
1006
 
1007
sys_IPC:
1008
;input:
1009
;  ebx=1 - set ipc buffer area
1010
;    ecx=address of buffer
1011
;    edx=size of buffer
1012
;  eax=2 - send message
1013
;    ebx=PID
1014
;    ecx=address of message
1015
;    edx=size of message
1016
 
1017
        dec     ebx
1018
        jnz     @f
1019
 
1020
        mov     eax, [current_slot]
1021
        pushf
1022
        cli
1023
        mov     [eax+APPDATA.ipc_start], ecx    ;set fields in extended information area
1024
        mov     [eax+APPDATA.ipc_size], edx
1025
 
1026
        add     edx, ecx
1027
        add     edx, 4095
1028
        and     edx, not 4095
1029
 
1030
.touch:
1031
        mov     eax, [ecx]
1032
        add     ecx, 0x1000
1033
        cmp     ecx, edx
1034
        jb      .touch
1035
 
1036
        popf
1037
        mov     [esp+32], ebx   ;ebx=0
1038
        ret
1039
 
1040
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1041
;2
1042
@@:
1043
        dec     ebx
1044
        jnz     @f
1045
 
1046
        stdcall sys_ipc_send, ecx, edx, esi
1047
        mov     [esp+32], eax
1048
        ret
1049
@@:
1050
        or      eax, -1
1051
        mov     [esp+32], eax
1052
        ret
1053
 
1054
proc sys_ipc_send stdcall, PID:dword, msg_addr:dword, msg_size:dword
1055
           locals
1056
             dst_slot   dd ?
1057
             dst_offset dd ?
1058
             buf_size   dd ?
1059
             used_buf   dd ?
1060
           endl
1061
 
1062
        pushf
1063
        cli
1064
 
1065
        mov     eax, [PID]
1066
        call    pid_to_slot
1067
        test    eax, eax
1068
        jz      .no_pid
1069
 
1070
        mov     [dst_slot], eax
1071
        shl     eax, 8
5130 serge 1072
        mov     edi, [eax+SLOT_BASE+APPDATA.ipc_start] ;is ipc area defined?
2288 clevermous 1073
        test    edi, edi
1074
        jz      .no_ipc_area
1075
 
1076
        mov     ebx, edi
1077
        and     ebx, 0xFFF
1078
        mov     [dst_offset], ebx
1079
 
5130 serge 1080
        mov     esi, [eax+SLOT_BASE+APPDATA.ipc_size]
2288 clevermous 1081
        mov     [buf_size], esi
1082
 
1083
        mov     ecx, [ipc_tmp]
1084
        cmp     esi, 0x40000-0x1000; size of [ipc_tmp] minus one page
1085
        jbe     @f
1086
        push    esi edi
1087
        add     esi, 0x1000
1088
        stdcall alloc_kernel_space, esi
1089
        mov     ecx, eax
1090
        pop     edi esi
1091
@@:
1092
        mov     [used_buf], ecx
5130 serge 1093
        stdcall map_mem_ipc, ecx, [dst_slot], \
5356 serge 1094
                edi, esi, PG_SWR
2288 clevermous 1095
 
1096
        mov     edi, [dst_offset]
1097
        add     edi, [used_buf]
1098
        cmp     dword [edi], 0
1099
        jnz     .ipc_blocked          ;if dword [buffer]<>0 - ipc blocked now
1100
 
1101
        mov     edx, dword [edi+4]
1102
        lea     ebx, [edx+8]
1103
        add     ebx, [msg_size]
1104
        cmp     ebx, [buf_size]
1105
        ja      .buffer_overflow       ;esi<0 - not enough memory in buffer
1106
 
1107
        mov     dword [edi+4], ebx
1108
        mov     eax, [TASK_BASE]
1109
        mov     eax, [eax+0x04]        ;eax - our PID
1110
        add     edi, edx
1111
        mov     [edi], eax
1112
        mov     ecx, [msg_size]
1113
 
1114
        mov     [edi+4], ecx
1115
        add     edi, 8
1116
        mov     esi, [msg_addr]
1117
       ;    add esi, new_app_base
1118
        cld
1119
        rep movsb
1120
 
1121
        mov     ebx, [ipc_tmp]
1122
        mov     edx, ebx
1123
        shr     ebx, 12
1124
        xor     eax, eax
1125
        mov     [page_tabs+ebx*4], eax
1126
        invlpg  [edx]
1127
 
1128
        mov     ebx, [ipc_pdir]
1129
        mov     edx, ebx
1130
        shr     ebx, 12
1131
        xor     eax, eax
1132
        mov     [page_tabs+ebx*4], eax
1133
        invlpg  [edx]
1134
 
1135
        mov     ebx, [ipc_ptab]
1136
        mov     edx, ebx
1137
        shr     ebx, 12
1138
        xor     eax, eax
1139
        mov     [page_tabs+ebx*4], eax
1140
        invlpg  [edx]
1141
 
1142
        mov     eax, [dst_slot]
1143
        shl     eax, 8
1144
        or      [eax+SLOT_BASE+0xA8], dword 0x40
1145
        push    0
1146
        jmp     .ret
1147
.no_pid:
1148
        popf
1149
        mov     eax, 4
1150
        ret
1151
.no_ipc_area:
1152
        popf
1153
        xor     eax, eax
1154
        inc     eax
1155
        ret
1156
.ipc_blocked:
1157
        push    2
1158
        jmp     .ret
1159
.buffer_overflow:
1160
        push    3
1161
.ret:
1162
        mov     eax, [used_buf]
1163
        cmp     eax, [ipc_tmp]
5130 serge 1164
        je      @f
2288 clevermous 1165
        stdcall free_kernel_space, eax
1166
@@:
1167
        pop     eax
1168
        popf
1169
        ret
1170
endp
1171
 
1172
align 4
1173
sysfn_meminfo:
1174
 
1175
        ;   add ecx, new_app_base
1176
        cmp     ecx, OS_BASE
1177
        jae     .fail
1178
 
1179
        mov     eax, [pg_data.pages_count]
1180
        mov     [ecx], eax
1181
        shl     eax, 12
1182
        mov     [esp+32], eax
1183
        mov     eax, [pg_data.pages_free]
1184
        mov     [ecx+4], eax
1185
        mov     eax, [pg_data.pages_faults]
1186
        mov     [ecx+8], eax
1187
        mov     eax, [heap_size]
1188
        mov     [ecx+12], eax
1189
        mov     eax, [heap_free]
1190
        mov     [ecx+16], eax
1191
        mov     eax, [heap_blocks]
1192
        mov     [ecx+20], eax
1193
        mov     eax, [free_blocks]
1194
        mov     [ecx+24], eax
1195
        ret
1196
.fail:
1197
        or      dword [esp+32], -1
1198
        ret
1199
 
1200
align 4
1201
f68:
1202
        cmp     ebx, 4
1203
        jbe     sys_sheduler
1204
 
1205
        cmp     ebx, 11
1206
        jb      .fail
1207
 
3786 Serge 1208
        cmp     ebx, 27
2288 clevermous 1209
        ja      .fail
1210
 
1211
        jmp     dword [f68call+ebx*4-11*4]
1212
.11:
1213
        call    init_heap
1214
        mov     [esp+32], eax
1215
        ret
1216
.12:
1217
        stdcall user_alloc, ecx
1218
        mov     [esp+32], eax
1219
        ret
1220
.13:
1221
        stdcall user_free, ecx
1222
        mov     [esp+32], eax
1223
        ret
1224
.14:
1225
        cmp     ecx, OS_BASE
1226
        jae     .fail
1227
        mov     edi, ecx
1228
        call    get_event_ex
1229
        mov     [esp+32], eax
1230
        ret
1231
.16:
1232
        test    ecx, ecx
1233
        jz      .fail
1234
        cmp     ecx, OS_BASE
1235
        jae     .fail
1236
        stdcall get_service, ecx
1237
        mov     [esp+32], eax
1238
        ret
1239
.17:
1240
        call    srv_handlerEx   ;ecx
1241
        mov     [esp+32], eax
1242
        ret
1243
.19:
1244
        cmp     ecx, OS_BASE
1245
        jae     .fail
1246
        stdcall load_library, ecx
1247
        mov     [esp+32], eax
1248
        ret
1249
.20:
1250
        mov     eax, edx
1251
        mov     ebx, ecx
1252
        call    user_realloc            ;in: eax = pointer, ebx = new size
1253
        mov     [esp+32], eax
1254
        ret
1255
.21:
1256
        cmp     ecx, OS_BASE
1257
        jae     .fail
1258
 
2647 mario79 1259
        cmp     edx, OS_BASE
2288 clevermous 1260
        jae     .fail
1261
 
4418 clevermous 1262
        stdcall load_pe_driver, ecx, edx
2288 clevermous 1263
        mov     [esp+32], eax
1264
        ret
1265
.22:
1266
        cmp     ecx, OS_BASE
1267
        jae     .fail
1268
 
1269
        stdcall shmem_open, ecx, edx, esi
1270
        mov     [esp+24], edx
1271
        mov     [esp+32], eax
1272
        ret
1273
 
1274
.23:
1275
        cmp     ecx, OS_BASE
1276
        jae     .fail
1277
 
1278
        stdcall shmem_close, ecx
1279
        mov     [esp+32], eax
1280
        ret
1281
.24:
1282
        mov     eax, [current_slot]
1283
        xchg    ecx, [eax+APPDATA.exc_handler]
1284
        xchg    edx, [eax+APPDATA.except_mask]
1285
        mov     [esp+32], ecx ; reg_eax+8
1286
        mov     [esp+20], edx ; reg_ebx+8
1287
        ret
1288
.25:
1289
        cmp     ecx, 32
1290
        jae     .fail
1291
        mov     eax, [current_slot]
1292
        btr     [eax+APPDATA.except_mask], ecx
1293
        setc    byte[esp+32]
1294
        jecxz   @f
1295
        bts     [eax+APPDATA.except_mask], ecx
1296
@@:
1297
        ret
1298
 
2400 Serge 1299
.26:
1300
        stdcall user_unmap, ecx, edx, esi
1301
        mov     [esp+32], eax
1302
        ret
1303
 
3786 Serge 1304
.27:
1305
        cmp     ecx, OS_BASE
1306
        jae     .fail
1307
 
1308
        stdcall load_file_umode, ecx
1309
        mov     [esp+24], edx
1310
        mov     [esp+32], eax
1311
        ret
1312
 
2288 clevermous 1313
.fail:
1314
        xor     eax, eax
1315
        mov     [esp+32], eax
1316
        ret
1317
 
1318
 
1319
align 4
1320
f68call:   ; keep this table closer to main code
1321
 
1322
           dd f68.11   ; init_heap
1323
           dd f68.12   ; user_alloc
1324
           dd f68.13   ; user_free
1325
           dd f68.14   ; get_event_ex
1326
           dd f68.fail ; moved to f68.24
1327
           dd f68.16   ; get_service
1328
           dd f68.17   ; call_service
1329
           dd f68.fail ; moved to f68.25
1330
           dd f68.19   ; load_dll
1331
           dd f68.20   ; user_realloc
1332
           dd f68.21   ; load_driver
1333
           dd f68.22   ; shmem_open
1334
           dd f68.23   ; shmem_close
2400 Serge 1335
           dd f68.24   ; set exception handler
1336
           dd f68.25   ; unmask exception
1337
           dd f68.26   ; user_unmap
3786 Serge 1338
           dd f68.27   ; load_file_umode
2288 clevermous 1339
 
1340
 
1341
align 4
4418 clevermous 1342
proc load_pe_driver stdcall, file:dword, cmdline:dword
1343
        push    esi
2288 clevermous 1344
 
1345
        stdcall load_PE, [file]
1346
        test    eax, eax
1347
        jz      .fail
1348
 
1349
        mov     esi, eax
4418 clevermous 1350
        push    [cmdline]
1351
        push    DRV_ENTRY
1352
        call    eax
1353
        pop     ecx
1354
        pop     ecx
2288 clevermous 1355
        test    eax, eax
1356
        jz      .fail
1357
 
1358
        mov     [eax+SRV.entry], esi
4418 clevermous 1359
        pop     esi
2288 clevermous 1360
        ret
1361
 
1362
.fail:
1363
        xor     eax, eax
4418 clevermous 1364
        pop     esi
2288 clevermous 1365
        ret
1366
endp
1367
 
1368
align 4
1369
proc create_ring_buffer stdcall, size:dword, flags:dword
1370
           locals
1371
             buf_ptr  dd ?
1372
           endl
1373
 
1374
        mov     eax, [size]
1375
        test    eax, eax
1376
        jz      .fail
1377
 
1378
        add     eax, eax
1379
        stdcall alloc_kernel_space, eax
1380
        test    eax, eax
1381
        jz      .fail
1382
 
1383
        push    ebx
1384
 
1385
        mov     [buf_ptr], eax
1386
 
1387
        mov     ebx, [size]
1388
        shr     ebx, 12
1389
        push    ebx
1390
 
1391
        stdcall alloc_pages, ebx
1392
        pop     ecx
1393
 
1394
        test    eax, eax
1395
        jz      .mm_fail
1396
 
1397
        push    edi
1398
 
1399
        or      eax, [flags]
1400
        mov     edi, [buf_ptr]
1401
        mov     ebx, [buf_ptr]
1402
        mov     edx, ecx
1403
        shl     edx, 2
1404
        shr     edi, 10
1405
@@:
1406
        mov     [page_tabs+edi], eax
1407
        mov     [page_tabs+edi+edx], eax
1408
        invlpg  [ebx]
1409
        invlpg  [ebx+0x10000]
1410
        add     eax, 0x1000
1411
        add     ebx, 0x1000
1412
        add     edi, 4
1413
        dec     ecx
1414
        jnz     @B
1415
 
1416
        mov     eax, [buf_ptr]
1417
        pop     edi
1418
        pop     ebx
1419
        ret
1420
.mm_fail:
1421
        stdcall free_kernel_space, [buf_ptr]
1422
        xor     eax, eax
1423
        pop     ebx
1424
.fail:
1425
        ret
1426
endp
2466 Serge 1427
 
1428
 
1429
align 4
1430
proc print_mem
1431
        mov     edi, BOOT_VAR + 0x9104
1432
        mov     ecx, [edi-4]
1433
        test    ecx, ecx
1434
        jz      .done
1435
 
1436
@@:
1437
        mov     eax, [edi]
1438
        mov     edx, [edi+4]
1439
        add     eax, [edi+8]
1440
        adc     edx, [edi+12]
1441
 
1442
        DEBUGF  1, "K : E820 %x%x - %x%x type %d\n", \
1443
                    [edi+4], [edi],\
1444
                    edx, eax, [edi+16]
1445
        add     edi, 20
1446
        dec     ecx
1447
        jnz     @b
1448
.done:
1449
        ret
1450
endp