-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel32.inc
808 lines (777 loc) · 14.8 KB
/
kernel32.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
;============================================================
; Здесь размещены все 32-разрядные функции
;============================================================
;============================================================
; 1. Процедура выводит символ
; На входе: bl - номер символа ASCII
; ds:esi - адрес таблицы символов
; gs:edi - адрес видеобуфера
; cl - цвет символа, ch - цвет фона
; На выходе: в видеобуфере содержится символ
; согласно таблице символов
;------------------------------------------------------------
char:
pushad
xor eax, eax
mov al, 12
mul bl
add esi, eax
mov al, 12
.for_y:
mov dl, [ds:esi]
mov dh, 1,
.for_x:
test dl, dh
jz .emppix
mov byte [gs:edi], cl
mov byte [gs:edi+1], cl
mov byte [gs:edi+2], cl
.return:
add edi, 3
shl dh, 1
cmp dh, 10000000b
jnz .for_x
inc esi
add edi, 3051
dec al
jnz .for_y
popad
iretd
.emppix:
mov byte [gs:edi], ch
mov byte [gs:edi+1], ch
mov byte [gs:edi+2], ch
jmp .return
;============================================================
;============================================================
; 2. Процедура выводит ASCIIZ-строку
; gs:edi - указатель на видеобуфер
; fs:ebp - указатель на ASCIIZ-строку
;------------------------------------------------------------
string:
pushad
push es
push ds
mov ax, gdt.chars-gdt
mov ds, ax
mov esi, 0
mov ax, gdt.video-gdt
mov gs, ax
mov bl, [fs:ebp]
mov cl, 0x0
mov ch, 0xFF
.for:
int 0x31
inc ebp
add edi, 21
mov bl, [fs:ebp]
cmp bl, 0
jnz .for
pop ds
pop es
popad
iretd
;============================================================
;============================================================
; 3. Процедура создает сегмент TSS процесса
; На входе: eax - начало кода, ebx - начало данных
; ecx - предел кода, edx - предел данных
;------------------------------------------------------------
addtask:
pushad
cmp byte [cs:counttask], 10
jae near .end
push fs
push ds
push es
push eax
mov ax, gdt.tssbuf-gdt
mov ds, ax
mov ax, gdt.cswr-gdt
mov fs, ax
mov esi, [cs:tsswrite]
mov dword [ds:esi], 0 ; LINK
add esi, 4
mov dword [ds:esi], ss0size-1 ; ESP0
add esi, 4
mov word [ds:esi], 11100b ; SS0
mov word [ds:esi+2], 0
add esi, 4
mov dword [ds:esi], ss1size-1 ; ESP1
add esi, 4
mov word [ds:esi], 100101b ; SS1
mov word [ds:esi+2], 0
add esi, 4
mov dword [ds:esi], ss2size-1 ; ESP2
add esi, 4
mov word [ds:esi], 101110b ; SS2
mov word [ds:esi+2], 0
add esi, 4
mov dword [ds:esi], 0 ; CR3
add esi, 4
mov dword [ds:esi], 0 ; EIP
add esi, 4
mov dword [ds:esi], 1000000010b ; EFLAGS
add esi, 4
mov dword [ds:esi], 0 ; EAX
add esi, 4
mov dword [ds:esi], 0 ; ECX
add esi, 4
mov dword [ds:esi], 0 ; EDX
add esi, 4
mov dword [ds:esi], 0 ; EBX
add esi, 4
mov dword [ds:esi], sssize-1 ; ESP
add esi, 4
mov dword [ds:esi], 0 ; EBP
add esi, 4
mov dword [ds:esi], 0 ; ESI
add esi, 4
mov dword [ds:esi], 0 ; EDI
add esi, 4
mov dword [ds:esi], 0 ; ES
add esi, 4
mov word [ds:esi], 1111b ; CS
mov word [ds:esi+2], 0
add esi, 4
mov word [ds:esi], 10111b ; SS
mov word [ds:esi+2], 0
add esi, 4
mov word [ds:esi], 0 ; DS
mov word [ds:esi+2], 0
add esi, 4
mov word [ds:esi], 0 ; FS
mov word [ds:esi+2], 0
add esi, 4
mov word [ds:esi], 0 ; GS
mov word [ds:esi+2], 0
add esi, 4
mov ah, 0
mov al, [cs:counttask]
shl ax, 4
add ax, gdt.task0_ldt-gdt
mov word [ds:esi], ax ; LDTR
mov word [ds:esi+2], 0
add esi, 4
mov word [ds:esi], 0 ; I/O
mov word [ds:esi+2], 0xFFFF
add esi, 4
mov dword [ds:esi], 0 ; LDT_0
mov dword [ds:esi+4], 0
add esi, 8
pop eax
mov word [ds:esi], cx ; LDT_CS
shr ecx, 16
and cl, 00001111b
or cl, 01000000b
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov ch, ah
mov word [ds:esi+6], cx
mov byte [ds:esi+5], 11111010b
add esi, 8
mov word [ds:esi], sssize-1 ; LDT_SS
mov eax, [cs:tsswrite]
add eax, ldtsize+tsssize
mov cx, gdt.gdt-gdt
mov es, cx
mov ch, [es:gdt.tssbuf-gdt+7]
mov cl, [es:gdt.tssbuf-gdt+4]
shl ecx, 16
mov cx, [es:gdt.tssbuf-gdt+2]
add eax, ecx
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov al, 0
mov word [ds:esi+6], ax
mov byte [ds:esi+5], 11110010b
add esi, 8
mov word [ds:esi], ss0size-1 ; LDT_SS0
mov eax, [cs:tsswrite]
add eax, ldtsize+tsssize+sssize
mov cx, gdt.gdt-gdt
mov es, cx
mov ch, [es:gdt.tss-gdt+7]
mov cl, [es:gdt.tss-gdt+4]
shl ecx, 16
mov cx, [es:gdt.tss-gdt+2]
add eax, ecx
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov al, 0
mov word [ds:esi+6], ax
mov byte [ds:esi+5], 10010010b
add esi, 8
mov word [ds:esi], ss1size-1 ; LDT_SS1
mov eax, [cs:tsswrite]
add eax, ldtsize+tsssize+sssize+ss0size
mov cx, gdt.gdt-gdt
mov es, cx
mov ch, [es:gdt.tss-gdt+7]
mov cl, [es:gdt.tss-gdt+4]
shl ecx, 16
mov cx, [es:gdt.tss-gdt+2]
add eax, ecx
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov al, 0
mov word [ds:esi+6], ax
mov byte [ds:esi+5], 10110010b
add esi, 8
mov word [ds:esi], ss2size-1 ; LDT_SS2
mov eax, [cs:tsswrite]
add eax, ldtsize+tsssize+sssize+ss0size+ss1size
mov cx, gdt.gdt-gdt
mov es, cx
mov ch, [es:gdt.tss-gdt+7]
mov cl, [es:gdt.tss-gdt+4]
shl ecx, 16
mov cx, [es:gdt.tss-gdt+2]
add eax, ecx
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov al, 0
mov word [ds:esi+6], ax
mov byte [ds:esi+5], 11010010b
add esi, 8
; LDT_video
mov word [ds:esi], vibuffersize/4096
mov eax, [cs:tsswrite]
add eax, ldtsize+tsssize+sssize+ss0size+ss1size+ss2size
mov cx, gdt.gdt-gdt
mov es, cx
mov ch, [es:gdt.tss-gdt+7]
mov cl, [es:gdt.tss-gdt+4]
shl ecx, 16
mov cx, [es:gdt.tss-gdt+2]
add eax, ecx
mov word [ds:esi+2], ax
shr eax, 16
mov byte [ds:esi+4], al
mov al, 10000000b
mov word [ds:esi+6], ax
mov byte [ds:esi+5], 11110010b
add esi, 8
mov word [ds:esi], dx ; LDT_DS
shr edx, 16
and dl, 00001111b
mov word [ds:esi+2], bx
shr ebx, 16
mov byte [ds:esi+4], bl
mov dh, bh
mov word [ds:esi+6], dx
mov byte [ds:esi+5], 11110010b
add dword [fs:tsswrite], sumsize
inc byte [fs:counttask]
pop es
pop ds
pop fs
popad
iretd
.end:
push fs
mov ax, gdt.cswr-gdt
mov fs, ax
mov ebp, taskerror
mov edi, visizew*vibytes*300
int 0x32
pop fs
popad
iretd
;============================================================
;============================================================
; 4. Функция преобразует число в строку:
; На входе: eax - число
; ebx - основание системы счисления
; es:edi - указатель на строку - результат
; На выходе - заполненный буфер
;------------------------------------------------------------
NumToASCII:
pushad
xor esi, esi
convert_loop:
xor edx, edx
div ebx
call HexDigit
push edx
inc esi
test eax, eax
jnz convert_loop
cld
write_loop:
pop eax
stosb
dec esi
test esi, esi
jnz write_loop
popad
iretd
HexDigit:
cmp dl, 10
jb .less
add dl, 'A'-10
ret
.less:
add dl, '0'
ret
;============================================================
;============================================================
; 5. Функция преобразует строку в число:
; На входе:
; ecx - основание системы счисления
; es:esi - указатель на ASCIIZ-строку
; На выходе: eax - число
;------------------------------------------------------------
ASCIIToNum:
push esi
xor eax, eax
mov ebx, eax
.next:
lodsb
test al, al
jz .done
call convert_char
imul ebx, ecx
add ebx, eax
jmp short .next
.done:
xchg ebx, eax
pop esi
iretd
convert_char:
sub al, '0'
cmp al, 10
jb done
add al, '0'
and al, 0x5F
sub al, 'A'-10
and al, 0x0F
done:
ret
;============================================================
;============================================================
; 6. Процедура читает данные с диска IDE
; На входе: esi - LBA-адрес
; es:edi - адрес буфера
; dx - базовый порт контроллера (0x1F0, 0x170)
; cl - количество секторов для чтения
; На выходе: в буфере будут находиться данные с диска
;------------------------------------------------------------
readsectors:
push ds
pushad
mov ax, es
mov ds, ax
or esi, 0xE0000000
mov bx, dx
add dx, 0x206
mov al, 10b
out dx, al
mov dx, bx
call bsywait
add dx, 6
mov eax, esi
shr eax, 24
out dx, al
mov dx, bx
call freewait
add dx, 3
mov eax, esi
out dx, al
shr eax, 8
inc dx
out dx, al
shr eax, 8
inc dx
out dx, al
mov al, cl
mov dx, bx
add dx, 2
out dx, al
add dx, 5
mov al, 0x20
out dx, al
mov dx, bx
call bsywait
call drqwait
cld
and ecx, 0xFF
xchg cl, ch
mov esi, edi
rep insw
mov dx, bx
add dx, 0x206
mov al, 0
out dx, al
popad
pop ds
iretd
bsywait:
push ax
push bx
push dx
add dx, 7
mov bl, 10000000b
.wait:
in al, dx
test al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
freewait:
push ax
push bx
push dx
add dx, 7
mov bl, 01000000b
mov bh, 11000000b
.wait:
in al, dx
and al, bh
cmp al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
drqwait:
push ax
push bx
push dx
add dx, 7
mov bl, 1000b
.wait:
in al, dx
test al, bl
jz .wait
pop dx
pop bx
pop ax
ret
;============================================================
;============================================================
; FIXME: possibly wrong
; 7. Процедура записывает данные на диск IDE
; На входе: esi - LBA-адрес
; es:edi - адрес буфера
; dx - базовый порт контроллера (0x1F0, 0x170)
; cl - количество секторов для записи
; На выходе: на диске будут находиться данные из буфера
;------------------------------------------------------------
writesectors:
iretd
push ds
pushad
mov ax, es
mov ds, ax
or esi, 0xE0000000
mov bx, dx
add dx, 0x206
mov al, 10b
out dx, al
mov dx, bx
call bsywait1
add dx, 6
mov eax, esi
shr eax, 24
out dx, al
mov dx, bx
call freewait1
add dx, 3
mov eax, esi
out dx, al
shr eax, 8
inc dx
out dx, al
shr eax, 8
inc dx
out dx, al
mov al, cl
mov dx, bx
add dx, 2
out dx, al
add dx, 5
mov al, 0x20
out dx, al
mov dx, bx
call bsywait1
call drqwait1
cld
and ecx, 0xFF
xchg cl, ch
mov esi, edi
rep outsw
mov dx, bx
add dx, 0x206
mov al, 0
out dx, al
popad
pop ds
iretd
bsywait1:
push ax
push bx
push dx
add dx, 7
mov bl, 10000000b
.wait:
in al, dx
test al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
freewait1:
push ax
push bx
push dx
add dx, 7
mov bl, 01000000b
mov bh, 11000000b
.wait:
in al, dx
and al, bh
cmp al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
drqwait1:
push ax
push bx
push dx
add dx, 7
mov bl, 1000b
.wait:
in al, dx
test al, bl
jz .wait
pop dx
pop bx
pop ax
ret
;============================================================
;============================================================
; 8. Процедура читает данные из файла
; На входе: es:edi - адрес буфера
; ebx:esi - адрес файла
; eax - кол-во секторов для операции
; На выходе: в буфере будут находится данные из файла
; или в файле будут данные из буфера
;------------------------------------------------------------
readfile:
pushad
push eax
push edi
push es
mov esi, ebx
shr esi, 6
inc esi
or esi, 0xF0000000
mov ax, gdt.cswr-gdt
mov es, ax
mov edi, selbuf
mov dx, 0x170
mov cl, 1
int 0x35
mov esi, ebx
shr esi, 4
and esi, 11b
shl esi, 7
mov esi, [es:esi+selbuf+84]
or esi, 0xF0000000
mov dx, 0x170
mov cl, 1
pop es
pop edi
pop eax
.for:
int 0x35
inc esi
add edi, 512
dec eax
jnz .for
popad
iretd
selbuf: TIMES 512 db 0
;============================================================
;============================================================
; 9. Процедура читает имя файла
; На входе: ds:edi - адрес буфера (81 байт)
; ebx - файловый селектор
; На выходе: в буфере будет содержаться имя файла
;------------------------------------------------------------
namefile:
pushad
push es
push edi
mov esi, ebx
shr esi, 6
inc esi
or esi, 0xF0000000
mov ax, gdt.cswr-gdt
mov es, ax
mov edi, selbuf2
mov dx, 0x170
mov cl, 1
int 0x35
mov esi, ebx
shr esi, 4
and esi, 11b
shl esi, 7
add esi, selbuf2
mov ecx, 80
pop edi
.for:
mov al, [es:esi]
mov byte [ds:edi], al
inc esi
inc edi
loop .for
mov byte [ds:edi], 0
pop es
popad
iretd
selbuf2: TIMES 512 db 0
;============================================================
;============================================================
;********************** Разработка **************************
; 10. Процедура читает данные с устройства IDE-ATAPI
; На входе: esi - LBA-адрес
; es:edi - адрес буфера
; dx - базовый порт контроллера (primary: 0x1F0, secondary: 0x170)
; ecx - количество секторов для чтения
; На выходе: в буфере будут находиться данные с диска
;------------------------------------------------------------
readsectorscd:
push ds
pushad
mov ax, es
mov ds, ax
or esi, 0xE0000000
mov bx, dx
add dx, 0x206
mov al, 10b
out dx, al
mov dx, bx
call bsywaitcd
add dx, 6
mov eax, esi
shr eax, 24
out dx, al
mov dx, bx
call freewaitcd
add dx, 3
mov eax, esi
out dx, al
shr eax, 8
inc dx
out dx, al
shr eax, 8
inc dx
out dx, al
mov al, cl
mov dx, bx
add dx, 2
out dx, al
add dx, 5
mov al, 0xA0
out dx, al
mov dx, bx
call bsywaitcd
call drqwaitcd
mov ax, gdt.cswr-gdt
mov fs, ax
mov eax, esi
mov byte [fs:packet+5], al
mov byte [fs:packet+4], ah
shr eax, 16
mov byte [fs:packet+3], al
xor ah, ah
mov byte [fs:packet+2], ah
mov eax, ecx
mov byte [fs:packet+9], al
mov byte [fs:packet+8], ah
shr eax, 16
mov byte [fs:packet+7], al
mov byte [fs:packet+6], ah
mov ax, [cs:packet+0]
out dx, ax
mov ax, [cs:packet+2]
out dx, ax
mov ax, [cs:packet+4]
out dx, ax
mov ax, [cs:packet+6]
out dx, ax
mov ax, [cs:packet+8]
out dx, ax
mov ax, [cs:packet+10]
out dx, ax
cld
shl ecx, 10
mov esi, edi
rep insw
mov dx, bx
add dx, 0x206
mov al, 0
out dx, al
popad
pop ds
iretd
packet:
db 0xA8
TIMES 11 db 0
bsywaitcd:
push ax
push bx
push dx
add dx, 7
mov bl, 10000000b
.wait:
in al, dx
test al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
freewaitcd:
push ax
push bx
push dx
add dx, 7
mov bl, 01000000b
mov bh, 11000000b
.wait:
in al, dx
and al, bh
cmp al, bl
jnz .wait
pop dx
pop bx
pop ax
ret
drqwaitcd:
push ax
push bx
push dx
add dx, 7
mov bl, 1000b
.wait:
in al, dx
test al, bl
jz .wait
pop dx
pop bx
pop ax
ret
;============================================================