Subversion Repositories Kolibri OS

Rev

Rev 756 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2007. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 837 $
  9.  
  10.  
  11. MEM_WB     equ 6               ;write-back memory
  12. MEM_WC     equ 1               ;write combined memory
  13. MEM_UC     equ 0               ;uncached memory
  14.  
  15. align 4
  16. proc mem_test
  17.  
  18.            mov eax, cr0
  19.            and eax, not (CR0_CD+CR0_NW)
  20.            or eax, CR0_CD         ;disable caching
  21.            mov cr0, eax
  22.            wbinvd                 ;invalidate cache
  23.  
  24.            xor edi, edi
  25.            mov ebx, 'TEST'
  26. @@:
  27.            add edi, 0x100000
  28.            xchg ebx, dword [edi]
  29.            cmp dword [edi], 'TEST'
  30.            xchg ebx, dword [edi]
  31.            je @b
  32.            mov [MEM_AMOUNT-OS_BASE], edi
  33.  
  34.            and eax, not (CR0_CD+CR0_NW)  ;enable caching
  35.            mov cr0, eax
  36.            mov eax, edi
  37.            ret
  38. endp
  39.  
  40. align 4
  41. proc init_mem
  42.  
  43.            mov ecx, [0x2F0000 + 0x9100]
  44.            test ecx, ecx
  45.            jz .nosmap
  46.  
  47.            xor eax, eax
  48.            mov esi, 0x2F0000 + 0x9104
  49. @@:
  50.            cmp dword [esi+16], 1
  51.            jne .next
  52.            mov edx, [esi+8]
  53.            cmp eax, [esi+8]
  54.            ja .next
  55.  
  56.            mov eax, [esi+8]
  57. .next:
  58.            add esi, 20
  59.            loop @B
  60.  
  61.            mov [MEM_AMOUNT-OS_BASE], eax
  62.            jmp @F
  63. .nosmap:
  64.            call mem_test
  65.  
  66.            mov eax, [MEM_AMOUNT-OS_BASE]
  67. @@:
  68.            mov [pg_data.mem_amount-OS_BASE], eax
  69.  
  70.            shr eax, 12
  71.            mov edx, eax
  72.            mov [pg_data.pages_count-OS_BASE], eax
  73.            shr eax, 3
  74.            mov [pg_data.pagemap_size-OS_BASE], eax
  75.  
  76.            add eax, (sys_pgmap-OS_BASE)+4095
  77.            and eax, not 4095
  78.            mov [tmp_page_tabs], eax
  79.  
  80.            cmp edx, (OS_BASE/4096)
  81.            jbe @F
  82.            mov edx, (OS_BASE/4096)
  83.            jmp .set
  84. @@:
  85.            cmp edx, (HEAP_MIN_SIZE/4096)
  86.            jae .set
  87.            mov edx, (HEAP_MIN_SIZE/4096)
  88. .set:
  89.            mov [pg_data.kernel_pages-OS_BASE], edx
  90.            shr edx, 10
  91.            mov [pg_data.kernel_tables-OS_BASE], edx
  92.  
  93.            xor eax, eax
  94.            mov edi, sys_pgdir-OS_BASE
  95.            mov ecx, 4096/4
  96.            cld
  97.            rep stosd
  98.  
  99.            mov edx, (sys_pgdir-OS_BASE)+ (OS_BASE shr 20)
  100.            bt [cpu_caps-OS_BASE], CAPS_PSE
  101.            jnc .no_PSE
  102.  
  103.            mov ebx, cr4
  104.            or ebx, CR4_PSE
  105.            mov eax, PG_LARGE+PG_SW
  106.            mov cr4, ebx
  107.            dec [pg_data.kernel_tables-OS_BASE]
  108.  
  109.            mov [edx], eax
  110.            add eax, 0x00400000
  111.            add edx, 4
  112.  
  113.            mov eax, 0x400000+PG_SW
  114.            mov ecx, [tmp_page_tabs]
  115.            sub ecx, 0x400000
  116.            shr ecx, 12          ;ecx/=4096
  117.            jmp .map_low
  118. .no_PSE:
  119.            mov eax, PG_SW
  120.            mov ecx, [tmp_page_tabs]
  121.            shr ecx, 12
  122. .map_low:
  123.            mov edi, [tmp_page_tabs]
  124. @@:                                   ;
  125.            stosd
  126.            add eax, 0x1000
  127.            dec ecx
  128.            jnz @B
  129.  
  130.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  131.            shl ecx, 10
  132.            xor eax, eax
  133.            rep stosd
  134.  
  135.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  136.            mov eax, [tmp_page_tabs]
  137.            or eax, PG_SW
  138.            mov edi, edx
  139.  
  140. .map_kernel_tabs:
  141.  
  142.            stosd
  143.            add eax, 0x1000
  144.            dec ecx
  145.            jnz .map_kernel_tabs
  146.  
  147.            mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  148.  
  149.            mov edi, (sys_pgdir-OS_BASE)
  150.            lea esi, [edi+(OS_BASE shr 20)]
  151.            movsd
  152.            movsd
  153.            ret
  154. endp
  155.  
  156. align 4
  157. proc init_page_map
  158.  
  159.            mov edi, sys_pgmap-OS_BASE
  160.            mov ecx, [pg_data.pagemap_size-OS_BASE]
  161.            shr ecx, 2
  162.            or eax, -1
  163.            cld
  164.            rep stosd
  165.  
  166.            mov ecx, [tmp_page_tabs]
  167.            mov edx, [pg_data.pages_count-OS_BASE]
  168.            shr ecx, 12
  169.            add ecx, [pg_data.kernel_tables-OS_BASE]
  170.            sub edx, ecx
  171.            mov [pg_data.pages_free-OS_BASE], edx
  172.  
  173.            mov edi, sys_pgmap-OS_BASE
  174.            mov ebx, ecx
  175.            shr ecx, 5
  176.            xor eax, eax
  177.            rep stosd
  178.  
  179.            not eax
  180.            mov ecx, ebx
  181.            and ecx, 31
  182.            shl eax, cl
  183.            mov [edi], eax
  184.            add edi, OS_BASE
  185.            mov [page_start-OS_BASE], edi;
  186.  
  187.            mov ebx, sys_pgmap
  188.            add ebx, [pg_data.pagemap_size-OS_BASE]
  189.            mov [page_end-OS_BASE], ebx
  190.  
  191.            mov [pg_data.pg_mutex-OS_BASE], 0
  192.            ret
  193. endp
  194.  
  195. align 4
  196.  
  197. init_BIOS32:
  198.            mov edi, 0xE0000
  199. .pcibios_nxt:
  200.            cmp dword[edi], '_32_' ; "magic" word
  201.            je .BIOS32_found
  202. .pcibios_nxt2:
  203.            add edi, 0x10
  204.            cmp edi, 0xFFFF0
  205.            je .BIOS32_not_found
  206.            jmp .pcibios_nxt
  207. .BIOS32_found:                  ; magic word found, check control summ
  208.  
  209.            movzx ecx, byte[edi + 9]
  210.            shl ecx, 4
  211.            mov esi, edi
  212.            xor eax, eax
  213.            cld   ; paranoia
  214. @@:     lodsb
  215.            add ah, al
  216.            loop @b
  217.            jnz .pcibios_nxt2 ; control summ must be zero
  218.     ; BIOS32 service found !
  219.            mov ebp, [edi + 4]
  220.            mov [bios32_entry], ebp
  221.     ; check PCI BIOS present
  222.            mov eax, '$PCI'
  223.            xor ebx, ebx
  224.            push cs  ; special for 'ret far' from  BIOS
  225.            call ebp
  226.            test al, al
  227.            jnz .PCI_BIOS32_not_found
  228.  
  229.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  230.  
  231.            add ebx, OS_BASE
  232.            dec ecx
  233.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  234.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  235.  
  236.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  237.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  238.  
  239.            shr ebx, 16
  240.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  241.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  242.  
  243.            shr ecx, 16
  244.            and cl, 0x0F
  245.            mov ch, bh
  246.            add cx, D32
  247.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  248.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  249.  
  250.            mov [(pci_bios_entry-OS_BASE)], edx
  251.          ; jmp .end
  252. .PCI_BIOS32_not_found:
  253.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  254. .BIOS32_not_found:
  255. .end:
  256.            ret
  257.  
  258. align 4
  259. proc test_cpu
  260.            locals
  261.               cpu_type   dd ?
  262.               cpu_id     dd ?
  263.               cpu_Intel  dd ?
  264.               cpu_AMD    dd ?
  265.            endl
  266.  
  267.            mov [cpu_type], 0
  268.            xor eax, eax
  269.            mov [cpu_caps-OS_BASE], eax
  270.            mov [cpu_caps+4-OS_BASE], eax
  271.  
  272.            pushfd
  273.            pop eax
  274.            mov ecx, eax
  275.            xor eax, 0x40000
  276.            push eax
  277.            popfd
  278.            pushfd
  279.            pop eax
  280.            xor eax, ecx
  281.            mov [cpu_type], CPU_386
  282.            jz .end_cpuid
  283.            push ecx
  284.            popfd
  285.  
  286.            mov [cpu_type], CPU_486
  287.            mov eax, ecx
  288.            xor eax, 0x200000
  289.            push eax
  290.            popfd
  291.            pushfd
  292.            pop eax
  293.            xor eax, ecx
  294.            je .end_cpuid
  295.            mov [cpu_id], 1
  296.  
  297.            xor eax, eax
  298.            cpuid
  299.  
  300.            mov [cpu_vendor-OS_BASE], ebx
  301.            mov [cpu_vendor+4-OS_BASE], edx
  302.            mov [cpu_vendor+8-OS_BASE], ecx
  303.            cmp ebx, dword [intel_str-OS_BASE]
  304.            jne .check_AMD
  305.            cmp edx, dword [intel_str+4-OS_BASE]
  306.            jne .check_AMD
  307.            cmp ecx, dword [intel_str+8-OS_BASE]
  308.            jne .check_AMD
  309.            mov [cpu_Intel], 1
  310.            cmp eax, 1
  311.            jl .end_cpuid
  312.            mov eax, 1
  313.            cpuid
  314.            mov [cpu_sign-OS_BASE], eax
  315.            mov [cpu_info-OS_BASE],  ebx
  316.            mov [cpu_caps-OS_BASE],  edx
  317.            mov [cpu_caps+4-OS_BASE],ecx
  318.  
  319.            shr eax, 8
  320.            and eax, 0x0f
  321.            ret
  322. .end_cpuid:
  323.            mov eax, [cpu_type]
  324.            ret
  325.  
  326. .check_AMD:
  327.            cmp ebx, dword [AMD_str-OS_BASE]
  328.            jne .unknown
  329.            cmp edx, dword [AMD_str+4-OS_BASE]
  330.            jne .unknown
  331.            cmp ecx, dword [AMD_str+8-OS_BASE]
  332.            jne .unknown
  333.            mov [cpu_AMD], 1
  334.            cmp eax, 1
  335.            jl .unknown
  336.            mov eax, 1
  337.            cpuid
  338.            mov [cpu_sign-OS_BASE], eax
  339.            mov [cpu_info-OS_BASE],  ebx
  340.            mov [cpu_caps-OS_BASE],  edx
  341.            mov [cpu_caps+4-OS_BASE],ecx
  342.            shr eax, 8
  343.            and eax, 0x0f
  344.            ret
  345. .unknown:
  346.            mov eax, 1
  347.            cpuid
  348.            mov [cpu_sign-OS_BASE], eax
  349.            mov [cpu_info-OS_BASE],  ebx
  350.            mov [cpu_caps-OS_BASE],  edx
  351.            mov [cpu_caps+4-OS_BASE],ecx
  352.            shr eax, 8
  353.            and eax, 0x0f
  354.            ret
  355. endp
  356.  
  357.