Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                              ;;
  3. ;; Copyright (C) KolibriOS team 2004-2008. All rights reserved. ;;
  4. ;; Distributed under terms of the GNU General Public License    ;;
  5. ;;                                                              ;;
  6. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  7.  
  8. $Revision: 1130 $
  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. ; if we have BIOS with fn E820, skip the test
  18.            cmp dword [BOOT_VAR-OS_BASE + 0x9100], 0
  19.            jnz .ret
  20.  
  21.            mov eax, cr0
  22.            and eax, not (CR0_CD+CR0_NW)
  23.            or eax, CR0_CD         ;disable caching
  24.            mov cr0, eax
  25.            wbinvd                 ;invalidate cache
  26.  
  27.            xor edi, edi
  28.            mov ebx, 'TEST'
  29. @@:
  30.            add edi, 0x100000
  31.            xchg ebx, dword [edi]
  32.            cmp dword [edi], 'TEST'
  33.            xchg ebx, dword [edi]
  34.            je @b
  35.  
  36.            and eax, not (CR0_CD+CR0_NW)  ;enable caching
  37.            mov cr0, eax
  38.            inc dword [BOOT_VAR-OS_BASE + 0x9100]
  39.            xor eax, eax
  40.            mov [BOOT_VAR-OS_BASE + 0x9104], eax
  41.            mov [BOOT_VAR-OS_BASE + 0x9108], eax
  42.            mov [BOOT_VAR-OS_BASE + 0x910C], edi
  43.            mov [BOOT_VAR-OS_BASE + 0x9110], eax
  44. .ret:
  45.            ret
  46. endp
  47.  
  48. align 4
  49. proc init_mem
  50. ; calculate maximum allocatable address and number of allocatable pages
  51.            mov edi, BOOT_VAR-OS_BASE + 0x9104
  52.            mov ecx, [edi-4]
  53.            xor esi, esi ; esi will hold total amount of memory
  54.            xor edx, edx ; edx will hold maximum allocatable address
  55. .calcmax:
  56. ; round all to pages
  57.            mov eax, [edi]
  58.            test eax, 0xFFF
  59.            jz @f
  60.            neg eax
  61.            and eax, 0xFFF
  62.            add [edi], eax
  63.            adc dword [edi+4], 0
  64.            sub [edi+8], eax
  65.            sbb dword [edi+12], 0
  66.            jc .unusable
  67. @@:
  68.            and dword [edi+8], not 0xFFF
  69.            jz .unusable
  70. ; ignore memory after 4 Gb
  71.            cmp dword [edi+4], 0
  72.            jnz .unusable
  73.            mov eax, [edi]
  74.            cmp dword [edi+12], 0
  75.            jnz .overflow
  76.            add eax, [edi+8]
  77.            jnc @f
  78. .overflow:
  79.            mov eax, 0xFFFFF000
  80. @@:
  81.            cmp edx, eax
  82.            jae @f
  83.            mov edx, eax
  84. @@:
  85.            sub eax, [edi]
  86.            mov [edi+8], eax
  87.            add esi, eax
  88.            jmp .usable
  89. .unusable:
  90.            and dword [edi+8], 0
  91. .usable:
  92.            add edi, 20
  93.            loop .calcmax
  94. .calculated:
  95.            mov [MEM_AMOUNT-OS_BASE], esi
  96.            mov [pg_data.mem_amount-OS_BASE], esi
  97.            shr esi, 12
  98.            mov [pg_data.pages_count-OS_BASE], esi
  99.  
  100.            shr edx, 12
  101.            add edx, 31
  102.            and edx, not 31
  103.            shr edx, 3
  104.            mov [pg_data.pagemap_size-OS_BASE], edx
  105.  
  106.            add edx, (sys_pgmap-OS_BASE)+4095
  107.            and edx, not 4095
  108.            mov [tmp_page_tabs], edx
  109.  
  110.            mov edx, esi
  111.            cmp edx, (OS_BASE/4096)
  112.            jbe @F
  113.            mov edx, (OS_BASE/4096)
  114.            jmp .set
  115. @@:
  116.            cmp edx, (HEAP_BASE+HEAP_MIN_SIZE)/4096
  117.            jae .set
  118.            mov edx, (HEAP_BASE+HEAP_MIN_SIZE)/4096
  119. .set:
  120.            mov [pg_data.kernel_pages-OS_BASE], edx
  121.            shr edx, 10
  122.            mov [pg_data.kernel_tables-OS_BASE], edx
  123.  
  124.            xor eax, eax
  125.            mov edi, sys_pgdir-OS_BASE
  126.            mov ecx, 4096/4
  127.            cld
  128.            rep stosd
  129.  
  130.            mov edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20)
  131.            bt [cpu_caps-OS_BASE], CAPS_PSE
  132.            jnc .no_PSE
  133.  
  134.            mov ebx, cr4
  135.            or ebx, CR4_PSE
  136.            mov eax, PG_LARGE+PG_SW
  137.            mov cr4, ebx
  138.            dec [pg_data.kernel_tables-OS_BASE]
  139.  
  140.            mov [edx], eax
  141.            add eax, 0x00400000
  142.            add edx, 4
  143.  
  144.            mov eax, 0x400000+PG_SW
  145.            mov ecx, [tmp_page_tabs]
  146.            sub ecx, 0x400000
  147.            shr ecx, 12          ;ecx/=4096
  148.            jmp .map_low
  149. .no_PSE:
  150.            mov eax, PG_SW
  151.            mov ecx, [tmp_page_tabs]
  152.            shr ecx, 12
  153. .map_low:
  154.            mov edi, [tmp_page_tabs]
  155. @@:                                   ;
  156.            stosd
  157.            add eax, 0x1000
  158.            dec ecx
  159.            jnz @B
  160.  
  161.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  162.            shl ecx, 10
  163.            xor eax, eax
  164.            rep stosd
  165.  
  166.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  167.            mov eax, [tmp_page_tabs]
  168.            or eax, PG_SW
  169.            mov edi, edx
  170.  
  171. .map_kernel_tabs:
  172.  
  173.            stosd
  174.            add eax, 0x1000
  175.            dec ecx
  176.            jnz .map_kernel_tabs
  177.  
  178.            mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  179.  
  180.            mov edi, (sys_pgdir-OS_BASE)
  181.            lea esi, [edi+(OS_BASE shr 20)]
  182.            movsd
  183.            movsd
  184.            ret
  185. endp
  186.  
  187. align 4
  188. proc init_page_map
  189. ; mark all memory as unavailable
  190.            mov edi, sys_pgmap-OS_BASE
  191.            mov ecx, [pg_data.pagemap_size-OS_BASE]
  192.            shr ecx, 2
  193.            xor eax, eax
  194.            cld
  195.            rep stosd
  196.  
  197. ; scan through memory map and mark free areas as available
  198.            mov ebx, BOOT_VAR-OS_BASE + 0x9104
  199.            mov edx, [ebx-4]
  200. .scanmap:
  201.            mov ecx, [ebx+8]
  202.            shr ecx, 12 ; ecx = number of pages
  203.            jz .next
  204.            mov edi, [ebx]
  205.            shr edi, 12 ; edi = first page
  206.            mov eax, edi
  207.            neg eax
  208.            shr edi, 5
  209.            add edi, sys_pgmap-OS_BASE
  210.            and eax, 31
  211.            jz .startok
  212.            sub ecx, eax
  213.            jbe .onedword
  214.            push ecx
  215.            mov ecx, eax
  216.            xor eax, eax
  217.            inc eax
  218.            shl eax, cl
  219.            dec eax
  220.            or [edi], eax
  221.            add edi, 4
  222.            pop ecx
  223. .startok:
  224.            push ecx
  225.            shr ecx, 5
  226.            or eax, -1
  227.            rep stosd
  228.            pop ecx
  229.            and ecx, 31
  230.            not eax
  231.            shl eax, cl
  232.            or [edi], eax
  233.            jmp .next
  234. .onedword:
  235.            add ecx, eax
  236. @@:
  237.            dec eax
  238.            bts [edi], eax
  239.            loop @b
  240. .next:
  241.            add ebx, 20
  242.            dec edx
  243.            jnz .scanmap
  244.  
  245. ; mark kernel memory as allocated (unavailable)
  246.            mov ecx, [tmp_page_tabs]
  247.            mov edx, [pg_data.pages_count-OS_BASE]
  248.            shr ecx, 12
  249.            add ecx, [pg_data.kernel_tables-OS_BASE]
  250.            sub edx, ecx
  251.            mov [pg_data.pages_free-OS_BASE], edx
  252.  
  253.            mov edi, sys_pgmap-OS_BASE
  254.            mov ebx, ecx
  255.            shr ecx, 5
  256.            xor eax, eax
  257.            rep stosd
  258.  
  259.            not eax
  260.            mov ecx, ebx
  261.            and ecx, 31
  262.            shl eax, cl
  263.            and [edi], eax
  264.            add edi, OS_BASE
  265.            mov [page_start-OS_BASE], edi;
  266.  
  267.            mov ebx, sys_pgmap
  268.            add ebx, [pg_data.pagemap_size-OS_BASE]
  269.            mov [page_end-OS_BASE], ebx
  270.  
  271.            mov [pg_data.pg_mutex-OS_BASE], 0
  272.            ret
  273. endp
  274.  
  275. align 4
  276.  
  277. init_BIOS32:
  278.            mov edi, 0xE0000
  279. .pcibios_nxt:
  280.            cmp dword[edi], '_32_' ; "magic" word
  281.            je .BIOS32_found
  282. .pcibios_nxt2:
  283.            add edi, 0x10
  284.            cmp edi, 0xFFFF0
  285.            je .BIOS32_not_found
  286.            jmp .pcibios_nxt
  287. .BIOS32_found:                  ; magic word found, check control summ
  288.  
  289.            movzx ecx, byte[edi + 9]
  290.            shl ecx, 4
  291.            mov esi, edi
  292.            xor eax, eax
  293.            cld   ; paranoia
  294. @@:     lodsb
  295.            add ah, al
  296.            loop @b
  297.            jnz .pcibios_nxt2 ; control summ must be zero
  298.     ; BIOS32 service found !
  299.            mov ebp, [edi + 4]
  300.            mov [bios32_entry], ebp
  301.     ; check PCI BIOS present
  302.            mov eax, '$PCI'
  303.            xor ebx, ebx
  304.            push cs  ; special for 'ret far' from  BIOS
  305.            call ebp
  306.            test al, al
  307.            jnz .PCI_BIOS32_not_found
  308.  
  309.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  310.  
  311.            add ebx, OS_BASE
  312.            dec ecx
  313.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  314.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  315.  
  316.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  317.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  318.  
  319.            shr ebx, 16
  320.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  321.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  322.  
  323.            shr ecx, 16
  324.            and cl, 0x0F
  325.            mov ch, bh
  326.            add cx, D32
  327.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  328.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  329.  
  330.            mov [(pci_bios_entry-OS_BASE)], edx
  331.          ; jmp .end
  332. .PCI_BIOS32_not_found:
  333.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  334. .BIOS32_not_found:
  335. .end:
  336.            ret 
  337.  
  338. align 4
  339. proc test_cpu
  340.            locals
  341.               cpu_type   dd ?
  342.               cpu_id     dd ?
  343.               cpu_Intel  dd ?
  344.               cpu_AMD    dd ?
  345.            endl
  346.  
  347.            xor eax, eax
  348.            mov [cpu_type], eax
  349.            mov [cpu_caps-OS_BASE], eax
  350.            mov [cpu_caps+4-OS_BASE], eax
  351.  
  352.            pushfd
  353.            pop eax
  354.            mov ecx, eax
  355.            xor eax, 0x40000
  356.            push eax
  357.            popfd
  358.            pushfd
  359.            pop eax
  360.            xor eax, ecx
  361.            mov [cpu_type], CPU_386
  362.            jz .end_cpuid
  363.            push ecx
  364.            popfd
  365.  
  366.            mov [cpu_type], CPU_486
  367.            mov eax, ecx
  368.            xor eax, 0x200000
  369.            push eax
  370.            popfd
  371.            pushfd
  372.            pop eax
  373.            xor eax, ecx
  374.            je .end_cpuid
  375.            mov [cpu_id], 1
  376.  
  377.            xor eax, eax
  378.            cpuid
  379.  
  380.            mov [cpu_vendor-OS_BASE], ebx
  381.            mov [cpu_vendor+4-OS_BASE], edx
  382.            mov [cpu_vendor+8-OS_BASE], ecx
  383.            cmp ebx, dword [intel_str-OS_BASE]
  384.            jne .check_AMD
  385.            cmp edx, dword [intel_str+4-OS_BASE]
  386.            jne .check_AMD
  387.            cmp ecx, dword [intel_str+8-OS_BASE]
  388.            jne .check_AMD
  389.            mov [cpu_Intel], 1
  390.            cmp eax, 1
  391.            jl .end_cpuid
  392.            mov eax, 1
  393.            cpuid
  394.            mov [cpu_sign-OS_BASE], eax
  395.            mov [cpu_info-OS_BASE],  ebx
  396.            mov [cpu_caps-OS_BASE],  edx
  397.            mov [cpu_caps+4-OS_BASE],ecx
  398.  
  399.            shr eax, 8
  400.            and eax, 0x0f
  401.            ret
  402. .end_cpuid:
  403.            mov eax, [cpu_type]
  404.            ret
  405.  
  406. .check_AMD:
  407.            cmp ebx, dword [AMD_str-OS_BASE]
  408.            jne .unknown
  409.            cmp edx, dword [AMD_str+4-OS_BASE]
  410.            jne .unknown
  411.            cmp ecx, dword [AMD_str+8-OS_BASE]
  412.            jne .unknown
  413.            mov [cpu_AMD], 1
  414.            cmp eax, 1
  415.            jl .unknown
  416.            mov eax, 1
  417.            cpuid
  418.            mov [cpu_sign-OS_BASE], eax
  419.            mov [cpu_info-OS_BASE],  ebx
  420.            mov [cpu_caps-OS_BASE],  edx
  421.            mov [cpu_caps+4-OS_BASE],ecx
  422.            shr eax, 8
  423.            and eax, 0x0f
  424.            ret
  425. .unknown:
  426.            mov eax, 1
  427.            cpuid
  428.            mov [cpu_sign-OS_BASE], eax
  429.            mov [cpu_info-OS_BASE],  ebx
  430.            mov [cpu_caps-OS_BASE],  edx
  431.            mov [cpu_caps+4-OS_BASE],ecx
  432.            shr eax, 8
  433.            and eax, 0x0f
  434.            ret
  435. endp
  436.  
  437.