Subversion Repositories Kolibri OS

Rev

Rev 1066 | 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.            jmp  system_init
  234.  
  235. if 0
  236.            mov ecx, system_init
  237.            call @create_systhread@4
  238.  
  239.            mov [_current_thread], eax
  240.  
  241.            mov ebx, [eax+THR.pdir]
  242.            mov ecx, cr3
  243.            cmp ebx, ecx
  244.            je .skip
  245.            mov cr3, ebx
  246. .skip:
  247.            mov esp, [_current_thread]
  248.         ;   lea eax, [esp+THR.pl0_stack]
  249.         ;   mov [tss._esp0], eax
  250. restart1:
  251.            dec [_k_reenter]
  252.            popad
  253.            add esp, 4                 ; skip return adr
  254.            iretd                      ; continue process
  255. end if
  256.  
  257. align 4
  258. map_LFB:
  259.            cmp eax, -1
  260.            jne @f
  261.  
  262.            ret
  263. @@:
  264.            test [scr_mode], 0100000000000000b
  265.            jnz @f
  266.            mov [BOOT_VAR+0x901c],byte 2
  267.            ret
  268. @@:
  269.            mov [LFBAddress], eax
  270.            mov [LFBSize], 0x800000
  271.            call init_mtrr
  272.  
  273.            mov eax, [LFBAddress]
  274.            or eax, PG_LARGE+PG_UW
  275.            mov [_sys_pdbr+(LFB_BASE shr 20)], eax
  276.            add eax, 0x00400000
  277.            mov [_sys_pdbr+4+(LFB_BASE shr 20)], eax
  278. if SHADOWFB
  279.            mov ecx, 1 shl 11
  280.            call @frame_alloc@4
  281.            or eax, PG_LARGE+PG_UW
  282.            mov [_sys_pdbr+(SHADOWFB shr 20)], eax
  283.            add eax, 0x00400000
  284.            mov [_sys_pdbr+4+(SHADOWFB shr 20)], eax
  285. end if
  286.  
  287.            bt [cpu_caps], CAPS_PGE
  288.            jnc @F
  289.            or dword [_sys_pdbr+(LFB_BASE shr 20)], PG_GLOBAL
  290. @@:
  291.            mov dword [LFBAddress], LFB_BASE
  292.            ret
  293.  
  294.  
  295. align 4
  296. proc test_cpu
  297.            locals
  298.               cpu_type   dd ?
  299.               cpu_id     dd ?
  300.               cpu_Intel  dd ?
  301.               cpu_AMD    dd ?
  302.            endl
  303.  
  304.            mov [cpu_type], 0
  305.            xor eax, eax
  306.            mov [cpu_caps], eax
  307.            mov [cpu_caps+4], eax
  308.  
  309.            xor eax, eax
  310.            cpuid
  311.  
  312.            mov [cpu_vendor], ebx
  313.            mov [cpu_vendor+4], edx
  314.            mov [cpu_vendor+8], ecx
  315.            cmp ebx, dword [intel_str]
  316.            jne .check_AMD
  317.            cmp edx, dword [intel_str+4]
  318.            jne .check_AMD
  319.            cmp ecx, dword [intel_str+8]
  320.            jne .check_AMD
  321.            mov [cpu_Intel], 1
  322.            cmp eax, 1
  323.            jl .end_cpuid
  324.            mov eax, 1
  325.            cpuid
  326.            mov [cpu_sign], eax
  327.            mov [cpu_info],  ebx
  328.            mov [cpu_caps],  edx
  329.            mov [cpu_caps+4],ecx
  330.  
  331.            shr eax, 8
  332.            and eax, 0x0f
  333.            ret
  334. .end_cpuid:
  335.            mov eax, [cpu_type]
  336.            ret
  337.  
  338. .check_AMD:
  339.            cmp ebx, dword [AMD_str]
  340.            jne .unknown
  341.            cmp edx, dword [AMD_str+4]
  342.            jne .unknown
  343.            cmp ecx, dword [AMD_str+8]
  344.            jne .unknown
  345.            mov [cpu_AMD], 1
  346.            cmp eax, 1
  347.            jl .unknown
  348.            mov eax, 1
  349.            cpuid
  350.            mov [cpu_sign], eax
  351.            mov [cpu_info],  ebx
  352.            mov [cpu_caps],  edx
  353.            mov [cpu_caps+4],ecx
  354.            shr eax, 8
  355.            and eax, 0x0f
  356.            ret
  357. .unknown:
  358.            mov eax, 1
  359.            cpuid
  360.            mov [cpu_sign], eax
  361.            mov [cpu_info],  ebx
  362.            mov [cpu_caps],  edx
  363.            mov [cpu_caps+4],ecx
  364.            shr eax, 8
  365.            and eax, 0x0f
  366.            ret
  367. endp
  368.  
  369. intel_str  db "GenuineIntel",0
  370. AMD_str    db "AuthenticAMD",0
  371.  
  372.  
  373.  
  374. if 0
  375. align 4
  376.  
  377. init_BIOS32:
  378.            mov edi, 0xE0000
  379. .pcibios_nxt:
  380.            cmp dword[edi], '_32_' ; "magic" word
  381.            je .BIOS32_found
  382. .pcibios_nxt2:
  383.            add edi, 0x10
  384.            cmp edi, 0xFFFF0
  385.            je .BIOS32_not_found
  386.            jmp .pcibios_nxt
  387. .BIOS32_found:                  ; magic word found, check control summ
  388.  
  389.            movzx ecx, byte[edi + 9]
  390.            shl ecx, 4
  391.            mov esi, edi
  392.            xor eax, eax
  393.            cld   ; paranoia
  394. @@:     lodsb
  395.            add ah, al
  396.            loop @b
  397.            jnz .pcibios_nxt2 ; control summ must be zero
  398.     ; BIOS32 service found !
  399.            mov ebp, [edi + 4]
  400.            mov [bios32_entry], ebp
  401.     ; check PCI BIOS present
  402.            mov eax, '$PCI'
  403.            xor ebx, ebx
  404.            push cs  ; special for 'ret far' from  BIOS
  405.            call ebp
  406.            test al, al
  407.            jnz .PCI_BIOS32_not_found
  408.  
  409.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  410.  
  411.            add ebx, OS_BASE
  412.            dec ecx
  413.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  414.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  415.  
  416.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  417.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  418.  
  419.            shr ebx, 16
  420.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  421.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  422.  
  423.            shr ecx, 16
  424.            and cl, 0x0F
  425.            mov ch, bh
  426.            add cx, D32
  427.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  428.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  429.  
  430.            mov [(pci_bios_entry-OS_BASE)], edx
  431.          ; jmp .end
  432. .PCI_BIOS32_not_found:
  433.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  434. .BIOS32_not_found:
  435. .end:
  436.            ret
  437.  
  438. end if
  439.  
  440. section '.data' data writeable align 16
  441.  
  442. _pg_balloc        dd LAST_PAGE
  443.  
  444. section '.bss' data writeable align 16
  445.  
  446.                   rb 8192-512
  447.  
  448. __os_stack        rb 512
  449.  
  450. ;CPUID information
  451.  
  452. cpu_vendor        rd 3
  453. cpu_sign          rd 1
  454. cpu_info          rd 1
  455. cpu_caps          rd 4
  456.  
  457.  
  458.  
  459.