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 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
| core_code_seg_sel equ 0x38 core_data_seg_sel equ 0x30 sys_routine_seg_sel equ 0x28 video_ram_seg_sel equ 0x20 core_stack_seg_sel equ 0x18 mem_0_4_gb_seg_sel equ 0x08
core_length dd core_end
sys_routine_seg dd section.sys_routine.start
core_data_seg dd section.core_data.start
core_code_seg dd section.core_code.start
core_entry dd start dw core_code_seg_sel
[bits 32]
SECTION sys_routine vstart=0
put_string: push ecx .getc: mov cl,[ebx] or cl,cl jz .exit call put_char inc ebx jmp .getc
.exit: pop ecx retf
put_char: pushad
mov dx,0x3d4 mov al,0x0e out dx,al inc dx in al,dx mov ah,al
dec dx mov al,0x0f out dx,al inc dx in al,dx mov bx,ax
cmp cl,0x0d jnz .put_0a mov ax,bx mov bl,80 div bl mul bl mov bx,ax jmp .set_cursor
.put_0a: cmp cl,0x0a jnz .put_other add bx,80 jmp .roll_screen
.put_other: push es mov eax,video_ram_seg_sel mov es,eax shl bx,1 mov [es:bx],cl pop es
shr bx,1 inc bx
.roll_screen: cmp bx,2000 jl .set_cursor
push ds push es mov eax,video_ram_seg_sel mov ds,eax mov es,eax cld mov esi,0xa0 mov edi,0x00 mov ecx,1920 rep movsd mov bx,3840 mov ecx,80 .cls: mov word[es:bx],0x0720 add bx,2 loop .cls
pop es pop ds
mov bx,1920
.set_cursor: mov dx,0x3d4 mov al,0x0e out dx,al inc dx mov al,bh out dx,al dec dx mov al,0x0f out dx,al inc dx mov al,bl out dx,al
popad ret
read_hard_disk_0: push eax push ecx push edx push eax mov dx,0x1f2 mov al,1 out dx,al
inc dx pop eax out dx,al
inc dx mov cl,8 shr eax,cl out dx,al
inc dx shr eax,cl out dx,al
inc dx shr eax,cl or al,0xe0 out dx,al
inc dx mov al,0x20 out dx,al
.waits: in al,dx and al,0x88 cmp al,0x08 jnz .waits
mov ecx,256 mov dx,0x1f0 .readw: in ax,dx mov [ebx],ax add ebx,2 loop .readw
pop edx pop ecx pop eax retf
put_hex_dword: pushad push ds mov ax,core_data_seg_sel mov ds,ax mov ebx,bin_hex mov ecx,8 .xlt: rol edx,4 mov eax,edx and eax,0x0000000f xlat push ecx mov cl,al call put_char pop ecx loop .xlt pop ds popad retf
allocate_memory: push ds push eax push ebx mov eax,core_data_seg_sel mov ds,eax mov eax,[ram_alloc] add eax,ecx mov ecx,[ram_alloc]
mov ebx,eax and ebx,0xfffffffc add ebx,4 test eax,0x00000003 cmovnz eax,ebx mov [ram_alloc],eax pop ebx pop eax pop ds
retf
set_up_gdt_descriptor: push eax push ebx push edx
push ds push es
mov ebx,core_data_seg_sel mov ds,ebx
sgdt [pgdt]
mov ebx,mem_0_4_gb_seg_sel mov es,ebx
movzx ebx,word [pgdt] inc bx add ebx,[pgdt+2]
mov [es:ebx],eax mov [es:ebx+4],edx
add word [pgdt],8
lgdt [pgdt]
mov ax,[pgdt] xor dx,dx mov bx,8 div bx mov cx,ax shl cx,3
pop es pop ds
pop edx pop ebx pop eax
retf
make_seg_descriptor: mov edx,eax shl eax,16 or ax,bx
and edx,0xffff0000 rol edx,8 bswap edx
xor bx,bx or edx,ebx
or edx,ecx
retf
make_gate_descriptor: push ebx push ecx mov edx,eax and edx,0xffff0000 or dx,cx and eax,0x0000ffff shl ebx,16 or eax,ebx pop ecx pop ebx retf
terminate_current_task: pushfd mov edx,[esp] add esp,4
mov eax,core_data_seg_sel mov ds,eax
test dx,0100_0000_0000_0000B jnz .b1 mov ebx,core_msg1 call sys_routine_seg_sel:put_string jmp far [prgman_tss] .b1: mov ebx,core_msg0 call sys_routine_seg_sel:put_string iretd sys_routine_end:
SECTION core_data vstart=0
pgdt dw 0 dd 0
ram_alloc dd 0x00100000
salt: salt_1 db '@PrintString' times 256-($-salt_1) db 0 dd put_string dw sys_routine_seg_sel
salt_2 db '@ReadDiskData' times 256-($-salt_2) db 0 dd read_hard_disk_0 dw sys_routine_seg_sel
salt_3 db '@PrintDwordAsHexString' times 256-($-salt_3) db 0 dd put_hex_dword dw sys_routine_seg_sel
salt_4 db '@TerminateProgram' times 256-($-salt_4) db 0 dd terminate_current_task dw sys_routine_seg_sel
salt_item_len equ $-salt_4 salt_items equ ($-salt)/salt_item_len
message_1 db ' If you seen this message,that means we ' db 'are now in protect mode,and the system ' db 'core is loaded,and the video display ' db 'routine works perfectly.',0x0d,0x0a,0
message_2 db ' System wide CALL-GATE mounted.',0x0d,0x0a,0 bin_hex db '0123456789ABCDEF'
core_buf times 2048 db 0
cpu_brnd0 db 0x0d,0x0a,' ',0 cpu_brand times 52 db 0 cpu_brnd1 db 0x0d,0x0a,0x0d,0x0a,0
tcb_chain dd 0
prgman_tss dd 0 dw 0
prgman_msg1 db 0x0d,0x0a db '[PROGRAM MANAGER]: Hello! I am Program Manager,' db 'run at CPL=0.Now,create user task and switch ' db 'to it by the CALL instruction...',0x0d,0x0a,0 prgman_msg2 db 0x0d,0x0a db '[PROGRAM MANAGER]: I am glad to regain control.' db 'Now,create another user task and switch to ' db 'it by the JMP instruction...',0x0d,0x0a,0 prgman_msg3 db 0x0d,0x0a db '[PROGRAM MANAGER]: I am gain control again,' db 'HALT...',0
core_msg0 db 0x0d,0x0a db '[SYSTEM CORE]: Uh...This task initiated with ' db 'CALL instruction or an exeception/ interrupt,' db 'should use IRETD instruction to switch back...' db 0x0d,0x0a,0
core_msg1 db 0x0d,0x0a db '[SYSTEM CORE]: Uh...This task initiated with ' db 'JMP instruction, should switch to Program ' db 'Manager directly by the JMP instruction...' db 0x0d,0x0a,0
core_data_end:
SECTION core_code vstart=0
fill_descriptor_in_ldt: push eax push edx push edi push ds
mov ecx,mem_0_4_gb_seg_sel mov ds,ecx
mov edi,[ebx+0x0c] xor ecx,ecx mov cx,[ebx+0x0a] inc cx mov [edi+ecx+0x00],eax mov [edi+ecx+0x04],edx
add cx,8 dec cx
mov [ebx+0x0a],cx
mov ax,cx xor dx,dx mov cx,8 div cx mov cx,ax shl cx,3 or cx,0000_0000_0000_0100B
pop ds pop edi pop edx pop eax ret
load_relocate_program: pushad push ds push es mov ebp,esp mov ecx,mem_0_4_gb_seg_sel mov es,ecx mov esi,[ebp+11*4]
mov ecx,160 call sys_routine_seg_sel:allocate_memory mov [es:esi+0x0c],ecx mov word [es:esi+0x0a],0xffff
mov eax,core_data_seg_sel mov ds,eax mov eax,[ebp+12*4] mov ebx,core_buf call sys_routine_seg_sel:read_hard_disk_0
mov eax,[core_buf] mov ebx,eax and ebx,0xfffffe00 add ebx,512 test eax,0x000001ff cmovnz eax,ebx mov ecx,eax call sys_routine_seg_sel:allocate_memory mov [es:esi+0x06],ecx mov ebx,ecx xor edx,edx mov ecx,512 div ecx mov ecx,eax mov eax,mem_0_4_gb_seg_sel mov ds,eax
mov eax,[ebp+12*4] .b1: call sys_routine_seg_sel:read_hard_disk_0 inc eax loop .b1
mov edi,[es:esi+0x06]
mov eax,edi mov ebx,[edi+0x04] dec ebx mov ecx,0x0040f200 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt
or cx,0000_0000_0000_0011B mov [es:esi+0x44],cx mov [edi+0x04],cx mov eax,edi add eax,[edi+0x14] mov ebx,[edi+0x18] dec ebx mov ecx,0x0040f800 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt or cx,0000_0000_0000_0011B mov [edi+0x14],cx
mov eax,edi add eax,[edi+0x1c] mov ebx,[edi+0x20] dec ebx mov ecx,0x0040f200 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt or cx,0000_0000_0000_0011B mov [edi+0x1c],cx
mov ecx,[edi+0x0c] mov ebx,0x000fffff sub ebx,ecx mov eax,4096 mul ecx mov ecx,eax call sys_routine_seg_sel:allocate_memory add eax,ecx mov ecx,0x00c0f600 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt or cx,0000_0000_0000_0011B mov [edi+0x08],cx
mov eax,mem_0_4_gb_seg_sel mov es,eax mov eax,core_data_seg_sel mov ds,eax cld
mov ecx,[es:edi+0x24] add edi,0x28 .b2: push ecx push edi mov ecx,salt_items mov esi,salt .b3: push edi push esi push ecx
mov ecx,64 repe cmpsd jnz .b4 mov eax,[esi] mov [es:edi-256],eax mov ax,[esi+4] or ax,0000000000000011B mov [es:edi-252],ax .b4: pop ecx pop esi add esi,salt_item_len pop edi loop .b3 pop edi add edi,256 pop ecx loop .b2
mov esi,[ebp+11*4]
mov ecx,4096 mov eax,ecx mov [es:esi+0x1a],ecx shr dword [es:esi+0x1a],12 call sys_routine_seg_sel:allocate_memory add eax,ecx mov [es:esi+0x1e],eax mov ebx,0xffffe mov ecx,0x00c09600 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt mov [es:esi+0x22],cx mov dword [es:esi+0x24],0 mov ecx,4096 mov eax,ecx mov [es:esi+0x28],ecx shr [es:esi+0x28],12 call sys_routine_seg_sel:allocate_memory add eax,ecx mov [es:esi+0x2c],eax mov ebx,0xffffe mov ecx,0x00c0b600 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt or cx,0000_0000_0000_0001 mov [es:esi+0x30],cx mov dword [es:esi+0x32],0
mov ecx,4096 mov eax,ecx mov [es:esi+0x36],ecx shr [es:esi+0x36],12 call sys_routine_seg_sel:allocate_memory add eax,ecx mov [es:esi+0x3a],ecx mov ebx,0xffffe mov ecx,0x00c0d600 call sys_routine_seg_sel:make_seg_descriptor mov ebx,esi call fill_descriptor_in_ldt or cx,0000_0000_0000_0010 mov [es:esi+0x3e],cx mov dword [es:esi+0x40],0 mov eax,[es:esi+0x0c] movzx ebx,word [es:esi+0x0a] mov ecx,0x00408200 call sys_routine_seg_sel:make_seg_descriptor call sys_routine_seg_sel:set_up_gdt_descriptor mov [es:esi+0x10],cx mov ecx,104 mov [es:esi+0x12],cx dec word [es:esi+0x12] call sys_routine_seg_sel:allocate_memory mov [es:esi+0x14],ecx mov word [es:ecx+0],0 mov edx,[es:esi+0x24] mov [es:ecx+4],edx mov dx,[es:esi+0x22] mov [es:ecx+8],dx mov edx,[es:esi+0x32] mov [es:ecx+12],edx
mov dx,[es:esi+0x30] mov [es:ecx+16],dx
mov edx,[es:esi+0x40] mov [es:ecx+20],edx
mov dx,[es:esi+0x3e] mov [es:ecx+24],dx
mov dx,[es:esi+0x10] mov [es:ecx+96],dx mov dx,[es:esi+0x12] mov [es:ecx+102],dx mov word [es:ecx+100],0 mov dword [es:ecx+28],0 mov ebx,[ebp+11*4] mov edi,[es:ebx+0x06]
mov edx,[es:edi+0x10] mov [es:ecx+32],edx
mov dx,[es:edi+0x14] mov [es:ecx+76],dx
mov dx,[es:edi+0x08] mov [es:ecx+80],dx
mov dx,[es:edi+0x04] mov word [es:ecx+84],dx mov word [es:ecx+72],0
mov word [es:ecx+88],0
mov word [es:ecx+92],0
pushfd pop edx mov dword [es:ecx+36],edx
mov eax,[es:esi+0x14] movzx ebx,word [es:esi+0x12] mov ecx,0x00408900 call sys_routine_seg_sel:make_seg_descriptor call sys_routine_seg_sel:set_up_gdt_descriptor mov [es:esi+0x18],cx
pop es pop ds popad ret 8
append_to_tcb_link: push eax push edx push ds push es mov eax,core_data_seg_sel mov ds,eax mov eax,mem_0_4_gb_seg_sel mov es,eax mov dword [es: ecx+0x00],0 mov eax,[tcb_chain] or eax,eax jz .notcb .searc: mov edx,eax mov eax,[es: edx+0x00] or eax,eax jnz .searc mov [es: edx+0x00],ecx jmp .retpc .notcb: mov [tcb_chain],ecx .retpc: pop es pop ds pop edx pop eax ret
start: mov ecx,core_data_seg_sel mov ds,ecx
mov ecx,mem_0_4_gb_seg_sel mov es,ecx
mov ebx,message_1 call sys_routine_seg_sel:put_string mov eax,0x80000002 cpuid mov [cpu_brand + 0x00],eax mov [cpu_brand + 0x04],ebx mov [cpu_brand + 0x08],ecx mov [cpu_brand + 0x0c],edx mov eax,0x80000003 cpuid mov [cpu_brand + 0x10],eax mov [cpu_brand + 0x14],ebx mov [cpu_brand + 0x18],ecx mov [cpu_brand + 0x1c],edx
mov eax,0x80000004 cpuid mov [cpu_brand + 0x20],eax mov [cpu_brand + 0x24],ebx mov [cpu_brand + 0x28],ecx mov [cpu_brand + 0x2c],edx
mov ebx,cpu_brnd0 call sys_routine_seg_sel:put_string mov ebx,cpu_brand call sys_routine_seg_sel:put_string mov ebx,cpu_brnd1 call sys_routine_seg_sel:put_string
mov edi,salt mov ecx,salt_items .b3: push ecx mov eax,[edi+256] mov bx,[edi+260] mov cx,1_11_0_1100_000_00000B call sys_routine_seg_sel:make_gate_descriptor call sys_routine_seg_sel:set_up_gdt_descriptor mov [edi+260],cx add edi,salt_item_len pop ecx loop .b3
mov ebx,message_2 call far [salt_1+256] mov ecx,104 call sys_routine_seg_sel:allocate_memory mov [prgman_tss+0x00],ecx mov word [es:ecx+96],0 mov word [es:ecx+102],103 mov word [es:ecx+0],0 mov dword [es:ecx+28],0 mov word [es:ecx+100],0 mov eax,ecx mov ebx,103 mov ecx,0x00408900 call sys_routine_seg_sel:make_seg_descriptor call sys_routine_seg_sel:set_up_gdt_descriptor mov [prgman_tss+0x04],cx
ltr cx
mov ebx,prgman_msg1 call sys_routine_seg_sel:put_string
mov ecx,0x46 call sys_routine_seg_sel:allocate_memory call append_to_tcb_link push dword 50 push ecx call load_relocate_program call far [es:ecx+0x14] mov ebx,prgman_msg2 call sys_routine_seg_sel:put_string
mov ecx,0x46 call sys_routine_seg_sel:allocate_memory call append_to_tcb_link
push dword 50 push ecx
call load_relocate_program
jmp far [es:ecx+0x14]
mov ebx,prgman_msg3 call sys_routine_seg_sel:put_string
hlt core_code_end:
SECTION core_trail
core_end:
|