Subversion Repositories Kolibri OS

Rev

Rev 1332 | Go to most recent revision | Blame | 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: 1376 $
  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-OS_BASE+HEAP_MIN_SIZE)/4096
  118.            jae .set
  119.            mov edx, (HEAP_BASE-OS_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.            shr edi, 5
  209.            shl edi, 2
  210.            add edi, sys_pgmap-OS_BASE
  211.            and eax, 31
  212.            jz .startok
  213.            add ecx, eax
  214.            sub ecx, 32
  215.            jbe .onedword
  216.            push ecx
  217.            mov ecx, eax
  218.            or eax, -1
  219.            shl eax, cl
  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.            neg eax
  231.            shl eax, cl
  232.            dec eax
  233.            or [edi], eax
  234.            jmp .next
  235. .onedword:
  236.            add ecx, 32
  237.            sub ecx, eax
  238. @@:
  239.            bts [edi], eax
  240.            inc eax
  241.            loop @b
  242. .next:
  243.            add ebx, 20
  244.            dec edx
  245.            jnz .scanmap
  246.  
  247. ; mark kernel memory as allocated (unavailable)
  248.            mov ecx, [tmp_page_tabs]
  249.            mov edx, [pg_data.pages_count-OS_BASE]
  250.            shr ecx, 12
  251.            add ecx, [pg_data.kernel_tables-OS_BASE]
  252.            sub edx, ecx
  253.            mov [pg_data.pages_free-OS_BASE], edx
  254.  
  255.            mov edi, sys_pgmap-OS_BASE
  256.            mov ebx, ecx
  257.            shr ecx, 5
  258.            xor eax, eax
  259.            rep stosd
  260.  
  261.            not eax
  262.            mov ecx, ebx
  263.            and ecx, 31
  264.            shl eax, cl
  265.            and [edi], eax
  266.            add edi, OS_BASE
  267.            mov [page_start-OS_BASE], edi;
  268.  
  269.            mov ebx, sys_pgmap
  270.            add ebx, [pg_data.pagemap_size-OS_BASE]
  271.            mov [page_end-OS_BASE], ebx
  272.  
  273.            mov [pg_data.pg_mutex-OS_BASE], 0
  274.            ret
  275. endp
  276.  
  277. align 4
  278.  
  279. init_BIOS32:
  280.            mov edi, 0xE0000
  281. .pcibios_nxt:
  282.            cmp dword[edi], '_32_' ; "magic" word
  283.            je .BIOS32_found
  284. .pcibios_nxt2:
  285.            add edi, 0x10
  286.            cmp edi, 0xFFFF0
  287.            je .BIOS32_not_found
  288.            jmp .pcibios_nxt
  289. .BIOS32_found:                  ; magic word found, check control summ
  290.  
  291.            movzx ecx, byte[edi + 9]
  292.            shl ecx, 4
  293.            mov esi, edi
  294.            xor eax, eax
  295.            cld   ; paranoia
  296. @@:     lodsb
  297.            add ah, al
  298.            loop @b
  299.            jnz .pcibios_nxt2 ; control summ must be zero
  300.     ; BIOS32 service found !
  301.            mov ebp, [edi + 4]
  302.            mov [bios32_entry], ebp
  303.     ; check PCI BIOS present
  304.            mov eax, '$PCI'
  305.            xor ebx, ebx
  306.            push cs  ; special for 'ret far' from  BIOS
  307.            call ebp
  308.            test al, al
  309.            jnz .PCI_BIOS32_not_found
  310.  
  311.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  312.  
  313.            add ebx, OS_BASE
  314.            dec ecx
  315.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  316.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  317.  
  318.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  319.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  320.  
  321.            shr ebx, 16
  322.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  323.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  324.  
  325.            shr ecx, 16
  326.            and cl, 0x0F
  327.            mov ch, bh
  328.            add cx, D32
  329.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  330.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  331.  
  332.            mov [(pci_bios_entry-OS_BASE)], edx
  333.          ; jmp .end
  334. .PCI_BIOS32_not_found:
  335.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  336. .BIOS32_not_found:
  337. .end:
  338.            ret 
  339.  
  340. align 4
  341. proc test_cpu
  342.            locals
  343.               cpu_type   dd ?
  344.               cpu_id     dd ?
  345.               cpu_Intel  dd ?
  346.               cpu_AMD    dd ?
  347.            endl
  348.  
  349.            xor eax, eax
  350.            mov [cpu_type], eax
  351.            mov [cpu_caps-OS_BASE], eax
  352.            mov [cpu_caps+4-OS_BASE], eax
  353.  
  354.            pushfd
  355.            pop eax
  356.            mov ecx, eax
  357.            xor eax, 0x40000
  358.            push eax
  359.            popfd
  360.            pushfd
  361.            pop eax
  362.            xor eax, ecx
  363.            mov [cpu_type], CPU_386
  364.            jz .end_cpuid
  365.            push ecx
  366.            popfd
  367.  
  368.            mov [cpu_type], CPU_486
  369.            mov eax, ecx
  370.            xor eax, 0x200000
  371.            push eax
  372.            popfd
  373.            pushfd
  374.            pop eax
  375.            xor eax, ecx
  376.            je .end_cpuid
  377.            mov [cpu_id], 1
  378.  
  379.            xor eax, eax
  380.            cpuid
  381.  
  382.            mov [cpu_vendor-OS_BASE], ebx
  383.            mov [cpu_vendor+4-OS_BASE], edx
  384.            mov [cpu_vendor+8-OS_BASE], ecx
  385.            cmp ebx, dword [intel_str-OS_BASE]
  386.            jne .check_AMD
  387.            cmp edx, dword [intel_str+4-OS_BASE]
  388.            jne .check_AMD
  389.            cmp ecx, dword [intel_str+8-OS_BASE]
  390.            jne .check_AMD
  391.            mov [cpu_Intel], 1
  392.            cmp eax, 1
  393.            jl .end_cpuid
  394.            mov eax, 1
  395.            cpuid
  396.            mov [cpu_sign-OS_BASE], eax
  397.            mov [cpu_info-OS_BASE],  ebx
  398.            mov [cpu_caps-OS_BASE],  edx
  399.            mov [cpu_caps+4-OS_BASE],ecx
  400.  
  401.            shr eax, 8
  402.            and eax, 0x0f
  403.            ret
  404. .end_cpuid:
  405.            mov eax, [cpu_type]
  406.            ret
  407.  
  408. .check_AMD:
  409.            cmp ebx, dword [AMD_str-OS_BASE]
  410.            jne .unknown
  411.            cmp edx, dword [AMD_str+4-OS_BASE]
  412.            jne .unknown
  413.            cmp ecx, dword [AMD_str+8-OS_BASE]
  414.            jne .unknown
  415.            mov [cpu_AMD], 1
  416.            cmp eax, 1
  417.            jl .unknown
  418.            mov eax, 1
  419.            cpuid
  420.            mov [cpu_sign-OS_BASE], eax
  421.            mov [cpu_info-OS_BASE],  ebx
  422.            mov [cpu_caps-OS_BASE],  edx
  423.            mov [cpu_caps+4-OS_BASE],ecx
  424.            shr eax, 8
  425.            and eax, 0x0f
  426.            ret
  427. .unknown:
  428.            mov eax, 1
  429.            cpuid
  430.            mov [cpu_sign-OS_BASE], eax
  431.            mov [cpu_info-OS_BASE],  ebx
  432.            mov [cpu_caps-OS_BASE],  edx
  433.            mov [cpu_caps+4-OS_BASE],ecx
  434.            shr eax, 8
  435.            and eax, 0x0f
  436.            ret
  437. endp
  438.  
  439.