Subversion Repositories Kolibri OS

Rev

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