Subversion Repositories Kolibri OS

Rev

Rev 996 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1.  
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;                                                              ;;
  4. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  5. ;; Distributed under terms of the GNU General Public License    ;;
  6. ;;                                                              ;;
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8.  
  9. include "../macros.inc"
  10. include "../proc32.inc"
  11. include "../const.inc"
  12.  
  13. $Revision: 847 $
  14.  
  15. sel_tss         equ  0x08
  16.  
  17. sel_os_code     equ  0x10
  18. sel_os_stack    equ  0x18
  19.  
  20. sel_app_code    equ  0x23
  21. sel_app_data    equ  0x2B
  22.  
  23. sel_srv_code    equ  0x31
  24. sel_srv_stack   equ  0x39
  25.  
  26. sel_code_16     equ  0x70
  27.  
  28. format MS COFF
  29.  
  30. use32
  31.  
  32. public __os_stack
  33. public _pg_balloc
  34.  
  35.  
  36. public high_code
  37.  
  38. public core_init
  39.  
  40. public  test_cpu
  41.  
  42. public cpu_vendor
  43. public cpu_sign
  44. public cpu_info
  45. public cpu_caps
  46.  
  47. extrn _parse_mbi
  48.  
  49. extrn _16bit_start
  50. extrn _16bit_end
  51. extrn _enter_bootscreen
  52.  
  53. extrn init_fpu
  54. extrn init_idt
  55. extrn _init_mm
  56. extrn _slab_cache_init
  57. extrn @init_heap@8
  58. extrn init_malloc
  59. extrn _init_core_dll
  60. extrn _init_threads
  61. extrn init_mtrr
  62. extrn system_init
  63.  
  64. extrn sysenter_entry
  65. extrn syscall_entry
  66.  
  67.  
  68. extrn  @create_systhread@4
  69.  
  70. extrn _sys_pdbr
  71. extrn _current_thread
  72. extrn _k_reenter:dword
  73.  
  74. extrn scr_mode:dword
  75. extrn LFBAddress:dword
  76. extrn LFBSize:dword
  77.  
  78. section '.text' code readable align 16
  79.  
  80. high_code:
  81.  
  82.            mov ax, sel_os_stack
  83.            mov dx, sel_app_data
  84.            mov ss, ax
  85.            mov esp, __os_stack
  86.  
  87.            mov ds, dx
  88.            mov es, dx
  89.            mov fs, dx
  90.            mov gs, dx
  91.  
  92.  
  93.          ;  bt [cpu_caps], CAPS_PGE
  94.          ;  jnc @F
  95.  
  96.          ;  or dword [sys_pgdir-OS_BASE+(OS_BASE shr 20)], PG_GLOBAL
  97.  
  98.          ;  mov ebx, cr4
  99.          ;  or ebx, CR4_PGE
  100.          ;  mov cr4, ebx
  101. @@:
  102.          ;  mov eax, cr3
  103.          ;  mov cr3, eax           ; flush TLB
  104.  
  105.            mov edx, 0x3fB
  106.            mov eax, 3
  107.            out dx, al
  108.  
  109.            call test_cpu
  110.            call _parse_mbi
  111.  
  112.         ;   mov eax, [_pg_balloc]
  113.         ;   mov [_copy_pg_balloc], eax
  114.  
  115. __core_restart:
  116.  
  117.            mov esi, _16bit_start
  118.            mov ecx, _16bit_end
  119.            shr ecx, 2
  120.            mov edi, _16BIT_BASE
  121.            cld
  122.            rep movsd
  123.  
  124.            jmp far sel_code_16:_enter_bootscreen;
  125.  
  126. align 16
  127. core_init:
  128.            cld
  129.  
  130.            mov ax, sel_os_stack
  131.            mov dx, sel_app_data
  132.            mov ss, ax
  133.            mov esp, __os_stack
  134.  
  135.            mov ds, dx
  136.            mov es, dx
  137.            mov fs, dx
  138.            mov gs, dx
  139.  
  140.            mov [tss._ss0], sel_os_stack
  141.            mov [tss._esp0], __os_stack
  142.            mov [tss._esp], __os_stack
  143.            mov [tss._cs], sel_os_code
  144.            mov [tss._ss], sel_os_stack
  145.            mov [tss._ds], sel_app_data
  146.            mov [tss._es], sel_app_data
  147.            mov [tss._fs], sel_app_data
  148.            mov [tss._gs], sel_app_data
  149.            mov [tss._io], 128
  150. ;Add IO access table - bit array of permitted ports
  151.            mov edi, tss._io_map_0
  152.            xor eax, eax
  153.        ;    not eax
  154.            mov ecx, 8192/4
  155.            rep stosd             ; access to 4096*8=65536 ports
  156.  
  157.            mov ax, sel_tss
  158.            ltr ax
  159.  
  160. ; -------- Fast System Call init ----------
  161. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  162.            bt [cpu_caps], CAPS_SEP
  163.            jnc .SEnP   ; SysEnter not Present
  164.  
  165.            xor edx, edx
  166.            mov ecx, MSR_SYSENTER_CS
  167.        mov eax, sel_os_code
  168.            wrmsr
  169.            mov ecx, MSR_SYSENTER_ESP
  170. ;           mov eax, sysenter_stack ; Check it
  171.            xor     eax, eax
  172.            wrmsr
  173.            mov ecx, MSR_SYSENTER_EIP
  174.            mov eax, sysenter_entry
  175.            wrmsr
  176.  
  177. .SEnP:
  178. ; AMD SYSCALL/SYSRET
  179.            cmp byte[cpu_vendor], 'A'
  180.            jne .noSYSCALL
  181.            mov eax, 0x80000001
  182.            cpuid
  183.            test edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  184.            jz .noSYSCALL
  185.            mov ecx, MSR_AMD_EFER
  186.            rdmsr
  187.            or eax, 1   ; bit_0 - System Call Extension (SCE)
  188.            wrmsr
  189.  
  190. ; Bits of EDX :
  191. ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  192. ;  and the contents of this field, plus 8, are copied into the SS register.
  193. ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  194. ;  and the contents of this field, plus 8, are copied into the SS register.
  195.  
  196.        mov   edx, ((sel_os_code + 16) shl 16) + sel_os_code
  197.  
  198.            mov eax, syscall_entry
  199.            mov ecx, MSR_AMD_STAR
  200.            wrmsr
  201. .noSYSCALL:
  202.  
  203.            call init_fpu
  204.  
  205.            call init_idt
  206.  
  207.            call _init_mm
  208.  
  209.            call init_malloc
  210.            call _slab_cache_init
  211.  
  212.            mov ecx, 0x80000000
  213.            mov edx, 0x40000000
  214.            call @init_heap@8
  215.  
  216.            call _init_core_dll
  217.        ;    call _init_threads
  218.  
  219. ; SAVE & CLEAR 0-0xffff
  220.  
  221.            mov dword [_sys_pdbr], eax
  222.            mov dword [_sys_pdbr+4], eax
  223.  
  224.            movzx eax,word [BOOT_VAR+0x9008]  ; screen mode
  225.            mov   [scr_mode],eax
  226.  
  227.            mov eax,[BOOT_VAR+0x9018]
  228.            call map_LFB
  229.  
  230.            mov eax, cr3
  231.            mov cr3, eax
  232.  
  233.  
  234.            jmp  system_init
  235.  
  236. if 0
  237.            mov ecx, system_init
  238.            call @create_systhread@4
  239.  
  240.            mov [_current_thread], eax
  241.  
  242.            mov ebx, [eax+THR.pdir]
  243.            mov ecx, cr3
  244.            cmp ebx, ecx
  245.            je .skip
  246.            mov cr3, ebx
  247. .skip:
  248.            mov esp, [_current_thread]
  249.         ;   lea eax, [esp+THR.pl0_stack]
  250.         ;   mov [tss._esp0], eax
  251. restart1:
  252.            dec [_k_reenter]
  253.            popad
  254.            add esp, 4                 ; skip return adr
  255.            iretd                      ; continue process
  256. end if
  257.  
  258. align 4
  259. map_LFB:
  260.            cmp eax, -1
  261.            jne @f
  262.  
  263.            ret
  264. @@:
  265.            test [scr_mode], 0100000000000000b
  266.            jnz @f
  267.            mov [BOOT_VAR+0x901c],byte 2
  268.            ret
  269. @@:
  270.            mov [LFBAddress], eax
  271.            mov [LFBSize], 0x800000
  272.            call init_mtrr
  273.  
  274.            mov eax, [LFBAddress]
  275.            or eax, PG_LARGE+PG_UW
  276.            mov [_sys_pdbr+(LFB_BASE shr 20)], eax
  277.            add eax, 0x00400000
  278.            mov [_sys_pdbr+4+(LFB_BASE shr 20)], eax
  279. if SHADOWFB
  280.            mov ecx, 1 shl 11
  281.            call @frame_alloc@4
  282.            or eax, PG_LARGE+PG_UW
  283.            mov [_sys_pdbr+(SHADOWFB shr 20)], eax
  284.            add eax, 0x00400000
  285.            mov [_sys_pdbr+4+(SHADOWFB shr 20)], eax
  286. end if
  287.  
  288.            bt [cpu_caps], CAPS_PGE
  289.            jnc @F
  290.            or dword [_sys_pdbr+(LFB_BASE shr 20)], PG_GLOBAL
  291. @@:
  292.            mov dword [LFBAddress], LFB_BASE
  293.            ret
  294.  
  295.  
  296. align 4
  297. proc test_cpu
  298.            locals
  299.               cpu_type   dd ?
  300.               cpu_id     dd ?
  301.               cpu_Intel  dd ?
  302.               cpu_AMD    dd ?
  303.            endl
  304.  
  305.            mov [cpu_type], 0
  306.            xor eax, eax
  307.            mov [cpu_caps], eax
  308.            mov [cpu_caps+4], eax
  309.  
  310.            xor eax, eax
  311.            cpuid
  312.  
  313.            mov [cpu_vendor], ebx
  314.            mov [cpu_vendor+4], edx
  315.            mov [cpu_vendor+8], ecx
  316.            cmp ebx, dword [intel_str]
  317.            jne .check_AMD
  318.            cmp edx, dword [intel_str+4]
  319.            jne .check_AMD
  320.            cmp ecx, dword [intel_str+8]
  321.            jne .check_AMD
  322.            mov [cpu_Intel], 1
  323.            cmp eax, 1
  324.            jl .end_cpuid
  325.            mov eax, 1
  326.            cpuid
  327.            mov [cpu_sign], eax
  328.            mov [cpu_info],  ebx
  329.            mov [cpu_caps],  edx
  330.            mov [cpu_caps+4],ecx
  331.  
  332.            shr eax, 8
  333.            and eax, 0x0f
  334.            ret
  335. .end_cpuid:
  336.            mov eax, [cpu_type]
  337.            ret
  338.  
  339. .check_AMD:
  340.            cmp ebx, dword [AMD_str]
  341.            jne .unknown
  342.            cmp edx, dword [AMD_str+4]
  343.            jne .unknown
  344.            cmp ecx, dword [AMD_str+8]
  345.            jne .unknown
  346.            mov [cpu_AMD], 1
  347.            cmp eax, 1
  348.            jl .unknown
  349.            mov eax, 1
  350.            cpuid
  351.            mov [cpu_sign], eax
  352.            mov [cpu_info],  ebx
  353.            mov [cpu_caps],  edx
  354.            mov [cpu_caps+4],ecx
  355.            shr eax, 8
  356.            and eax, 0x0f
  357.            ret
  358. .unknown:
  359.            mov eax, 1
  360.            cpuid
  361.            mov [cpu_sign], eax
  362.            mov [cpu_info],  ebx
  363.            mov [cpu_caps],  edx
  364.            mov [cpu_caps+4],ecx
  365.            shr eax, 8
  366.            and eax, 0x0f
  367.            ret
  368. endp
  369.  
  370. intel_str  db "GenuineIntel",0
  371. AMD_str    db "AuthenticAMD",0
  372.  
  373.  
  374.  
  375. if 0
  376. align 4
  377.  
  378. init_BIOS32:
  379.            mov edi, 0xE0000
  380. .pcibios_nxt:
  381.            cmp dword[edi], '_32_' ; "magic" word
  382.            je .BIOS32_found
  383. .pcibios_nxt2:
  384.            add edi, 0x10
  385.            cmp edi, 0xFFFF0
  386.            je .BIOS32_not_found
  387.            jmp .pcibios_nxt
  388. .BIOS32_found:                  ; magic word found, check control summ
  389.  
  390.            movzx ecx, byte[edi + 9]
  391.            shl ecx, 4
  392.            mov esi, edi
  393.            xor eax, eax
  394.            cld   ; paranoia
  395. @@:     lodsb
  396.            add ah, al
  397.            loop @b
  398.            jnz .pcibios_nxt2 ; control summ must be zero
  399.     ; BIOS32 service found !
  400.            mov ebp, [edi + 4]
  401.            mov [bios32_entry], ebp
  402.     ; check PCI BIOS present
  403.            mov eax, '$PCI'
  404.            xor ebx, ebx
  405.            push cs  ; special for 'ret far' from  BIOS
  406.            call ebp
  407.            test al, al
  408.            jnz .PCI_BIOS32_not_found
  409.  
  410.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  411.  
  412.            add ebx, OS_BASE
  413.            dec ecx
  414.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  415.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  416.  
  417.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  418.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  419.  
  420.            shr ebx, 16
  421.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  422.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  423.  
  424.            shr ecx, 16
  425.            and cl, 0x0F
  426.            mov ch, bh
  427.            add cx, D32
  428.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  429.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  430.  
  431.            mov [(pci_bios_entry-OS_BASE)], edx
  432.          ; jmp .end
  433. .PCI_BIOS32_not_found:
  434.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  435. .BIOS32_not_found:
  436. .end:
  437.            ret
  438.  
  439. end if
  440.  
  441. section '.data' data writeable align 16
  442.  
  443. _pg_balloc        dd LAST_PAGE
  444.  
  445. section '.bss' data writeable align 16
  446.  
  447.                   rb 8192-512
  448.  
  449. __os_stack        rb 512
  450.  
  451. ;CPUID information
  452.  
  453. cpu_vendor        rd 3
  454. cpu_sign          rd 1
  455. cpu_info          rd 1
  456. cpu_caps          rd 4
  457.  
  458.  
  459.  
  460.