Subversion Repositories Kolibri OS

Rev

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