Subversion Repositories Kolibri OS

Rev

Rev 1131 | 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: 1131 $
  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.            and edx, -1024
  112.            cmp edx, (OS_BASE/4096)
  113.            jbe @F
  114.            mov edx, (OS_BASE/4096)
  115.            jmp .set
  116. @@:
  117.            cmp edx, (HEAP_BASE+HEAP_MIN_SIZE)/4096
  118.            jae .set
  119.            mov edx, (HEAP_BASE+HEAP_MIN_SIZE)/4096
  120. .set:
  121.            mov [pg_data.kernel_pages-OS_BASE], edx
  122.            shr edx, 10
  123.            mov [pg_data.kernel_tables-OS_BASE], edx
  124.  
  125.            xor eax, eax
  126.            mov edi, sys_pgdir-OS_BASE
  127.            mov ecx, 4096/4
  128.            cld
  129.            rep stosd
  130.  
  131.            mov edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20)
  132.            bt [cpu_caps-OS_BASE], CAPS_PSE
  133.            jnc .no_PSE
  134.  
  135.            mov ebx, cr4
  136.            or ebx, CR4_PSE
  137.            mov eax, PG_LARGE+PG_SW
  138.            mov cr4, ebx
  139.            dec [pg_data.kernel_tables-OS_BASE]
  140.  
  141.            mov [edx], eax
  142.            add eax, 0x00400000
  143.            add edx, 4
  144.  
  145.            mov eax, 0x400000+PG_SW
  146.            mov ecx, [tmp_page_tabs]
  147.            sub ecx, 0x400000
  148.            shr ecx, 12          ;ecx/=4096
  149.            jmp .map_low
  150. .no_PSE:
  151.            mov eax, PG_SW
  152.            mov ecx, [tmp_page_tabs]
  153.            shr ecx, 12
  154. .map_low:
  155.            mov edi, [tmp_page_tabs]
  156. @@:                                   ;
  157.            stosd
  158.            add eax, 0x1000
  159.            dec ecx
  160.            jnz @B
  161.  
  162.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  163.            shl ecx, 10
  164.            xor eax, eax
  165.            rep stosd
  166.  
  167.            mov ecx, [pg_data.kernel_tables-OS_BASE]
  168.            mov eax, [tmp_page_tabs]
  169.            or eax, PG_SW
  170.            mov edi, edx
  171.  
  172. .map_kernel_tabs:
  173.  
  174.            stosd
  175.            add eax, 0x1000
  176.            dec ecx
  177.            jnz .map_kernel_tabs
  178.  
  179.            mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  180.  
  181.            mov edi, (sys_pgdir-OS_BASE)
  182.            lea esi, [edi+(OS_BASE shr 20)]
  183.            movsd
  184.            movsd
  185.            ret
  186. endp
  187.  
  188. align 4
  189. proc init_page_map
  190. ; mark all memory as unavailable
  191.            mov edi, sys_pgmap-OS_BASE
  192.            mov ecx, [pg_data.pagemap_size-OS_BASE]
  193.            shr ecx, 2
  194.            xor eax, eax
  195.            cld
  196.            rep stosd
  197.  
  198. ; scan through memory map and mark free areas as available
  199.            mov ebx, BOOT_VAR-OS_BASE + 0x9104
  200.            mov edx, [ebx-4]
  201. .scanmap:
  202.            mov ecx, [ebx+8]
  203.            shr ecx, 12 ; ecx = number of pages
  204.            jz .next
  205.            mov edi, [ebx]
  206.            shr edi, 12 ; edi = first page
  207.            mov eax, edi
  208.            neg eax
  209.            shr edi, 5
  210.            add edi, sys_pgmap-OS_BASE
  211.            and eax, 31
  212.            jz .startok
  213.            sub ecx, eax
  214.            jbe .onedword
  215.            push ecx
  216.            mov ecx, eax
  217.            xor eax, eax
  218.            inc eax
  219.            shl eax, cl
  220.            dec eax
  221.            or [edi], eax
  222.            add edi, 4
  223.            pop ecx
  224. .startok:
  225.            push ecx
  226.            shr ecx, 5
  227.            or eax, -1
  228.            rep stosd
  229.            pop ecx
  230.            and ecx, 31
  231.            not eax
  232.            shl eax, cl
  233.            or [edi], eax
  234.            jmp .next
  235. .onedword:
  236.            add ecx, eax
  237. @@:
  238.            dec eax
  239.            bts [edi], eax
  240.            loop @b
  241. .next:
  242.            add ebx, 20
  243.            dec edx
  244.            jnz .scanmap
  245.  
  246. ; mark kernel memory as allocated (unavailable)
  247.            mov ecx, [tmp_page_tabs]
  248.            mov edx, [pg_data.pages_count-OS_BASE]
  249.            shr ecx, 12
  250.            add ecx, [pg_data.kernel_tables-OS_BASE]
  251.            sub edx, ecx
  252.            mov [pg_data.pages_free-OS_BASE], edx
  253.  
  254.            mov edi, sys_pgmap-OS_BASE
  255.            mov ebx, ecx
  256.            shr ecx, 5
  257.            xor eax, eax
  258.            rep stosd
  259.  
  260.            not eax
  261.            mov ecx, ebx
  262.            and ecx, 31
  263.            shl eax, cl
  264.            and [edi], eax
  265.            add edi, OS_BASE
  266.            mov [page_start-OS_BASE], edi;
  267.  
  268.            mov ebx, sys_pgmap
  269.            add ebx, [pg_data.pagemap_size-OS_BASE]
  270.            mov [page_end-OS_BASE], ebx
  271.  
  272.            mov [pg_data.pg_mutex-OS_BASE], 0
  273.            ret
  274. endp
  275.  
  276. align 4
  277.  
  278. init_BIOS32:
  279.            mov edi, 0xE0000
  280. .pcibios_nxt:
  281.            cmp dword[edi], '_32_' ; "magic" word
  282.            je .BIOS32_found
  283. .pcibios_nxt2:
  284.            add edi, 0x10
  285.            cmp edi, 0xFFFF0
  286.            je .BIOS32_not_found
  287.            jmp .pcibios_nxt
  288. .BIOS32_found:                  ; magic word found, check control summ
  289.  
  290.            movzx ecx, byte[edi + 9]
  291.            shl ecx, 4
  292.            mov esi, edi
  293.            xor eax, eax
  294.            cld   ; paranoia
  295. @@:     lodsb
  296.            add ah, al
  297.            loop @b
  298.            jnz .pcibios_nxt2 ; control summ must be zero
  299.     ; BIOS32 service found !
  300.            mov ebp, [edi + 4]
  301.            mov [bios32_entry], ebp
  302.     ; check PCI BIOS present
  303.            mov eax, '$PCI'
  304.            xor ebx, ebx
  305.            push cs  ; special for 'ret far' from  BIOS
  306.            call ebp
  307.            test al, al
  308.            jnz .PCI_BIOS32_not_found
  309.  
  310.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  311.  
  312.            add ebx, OS_BASE
  313.            dec ecx
  314.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  315.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  316.  
  317.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  318.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  319.  
  320.            shr ebx, 16
  321.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  322.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  323.  
  324.            shr ecx, 16
  325.            and cl, 0x0F
  326.            mov ch, bh
  327.            add cx, D32
  328.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  329.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  330.  
  331.            mov [(pci_bios_entry-OS_BASE)], edx
  332.          ; jmp .end
  333. .PCI_BIOS32_not_found:
  334.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  335. .BIOS32_not_found:
  336. .end:
  337.            ret 
  338.  
  339. align 4
  340. proc test_cpu
  341.            locals
  342.               cpu_type   dd ?
  343.               cpu_id     dd ?
  344.               cpu_Intel  dd ?
  345.               cpu_AMD    dd ?
  346.            endl
  347.  
  348.            xor eax, eax
  349.            mov [cpu_type], eax
  350.            mov [cpu_caps-OS_BASE], eax
  351.            mov [cpu_caps+4-OS_BASE], eax
  352.  
  353.            pushfd
  354.            pop eax
  355.            mov ecx, eax
  356.            xor eax, 0x40000
  357.            push eax
  358.            popfd
  359.            pushfd
  360.            pop eax
  361.            xor eax, ecx
  362.            mov [cpu_type], CPU_386
  363.            jz .end_cpuid
  364.            push ecx
  365.            popfd
  366.  
  367.            mov [cpu_type], CPU_486
  368.            mov eax, ecx
  369.            xor eax, 0x200000
  370.            push eax
  371.            popfd
  372.            pushfd
  373.            pop eax
  374.            xor eax, ecx
  375.            je .end_cpuid
  376.            mov [cpu_id], 1
  377.  
  378.            xor eax, eax
  379.            cpuid
  380.  
  381.            mov [cpu_vendor-OS_BASE], ebx
  382.            mov [cpu_vendor+4-OS_BASE], edx
  383.            mov [cpu_vendor+8-OS_BASE], ecx
  384.            cmp ebx, dword [intel_str-OS_BASE]
  385.            jne .check_AMD
  386.            cmp edx, dword [intel_str+4-OS_BASE]
  387.            jne .check_AMD
  388.            cmp ecx, dword [intel_str+8-OS_BASE]
  389.            jne .check_AMD
  390.            mov [cpu_Intel], 1
  391.            cmp eax, 1
  392.            jl .end_cpuid
  393.            mov eax, 1
  394.            cpuid
  395.            mov [cpu_sign-OS_BASE], eax
  396.            mov [cpu_info-OS_BASE],  ebx
  397.            mov [cpu_caps-OS_BASE],  edx
  398.            mov [cpu_caps+4-OS_BASE],ecx
  399.  
  400.            shr eax, 8
  401.            and eax, 0x0f
  402.            ret
  403. .end_cpuid:
  404.            mov eax, [cpu_type]
  405.            ret
  406.  
  407. .check_AMD:
  408.            cmp ebx, dword [AMD_str-OS_BASE]
  409.            jne .unknown
  410.            cmp edx, dword [AMD_str+4-OS_BASE]
  411.            jne .unknown
  412.            cmp ecx, dword [AMD_str+8-OS_BASE]
  413.            jne .unknown
  414.            mov [cpu_AMD], 1
  415.            cmp eax, 1
  416.            jl .unknown
  417.            mov eax, 1
  418.            cpuid
  419.            mov [cpu_sign-OS_BASE], eax
  420.            mov [cpu_info-OS_BASE],  ebx
  421.            mov [cpu_caps-OS_BASE],  edx
  422.            mov [cpu_caps+4-OS_BASE],ecx
  423.            shr eax, 8
  424.            and eax, 0x0f
  425.            ret
  426. .unknown:
  427.            mov eax, 1
  428.            cpuid
  429.            mov [cpu_sign-OS_BASE], eax
  430.            mov [cpu_info-OS_BASE],  ebx
  431.            mov [cpu_caps-OS_BASE],  edx
  432.            mov [cpu_caps+4-OS_BASE],ecx
  433.            shr eax, 8
  434.            and eax, 0x0f
  435.            ret
  436. endp
  437.  
  438.