Subversion Repositories Kolibri OS

Rev

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