Subversion Repositories Kolibri OS

Rev

Rev 1952 | Rev 2425 | 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: 2047 $
  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. ; ======================================================================
  16. align 4
  17. preinit_mem:
  18.  
  19. ; clear [CLEAN_ZONE..HEAP_BASE]
  20.            xor   eax,eax
  21.            mov   edi,CLEAN_ZONE                 ; 0x280000 = ramdisk FAT ?
  22.            mov   ecx,(HEAP_BASE-OS_BASE-CLEAN_ZONE) / 4
  23.            cld
  24.            rep   stosd
  25.  
  26. ; clear [0x40000..0x90000]
  27.            mov   edi,0x50000                    ; 0x50000 is somewhere inside kernel code?
  28.            mov   ecx,(0x90000-0x50000)/4
  29.            rep   stosd
  30.  
  31. ; clear undefined kernel globals
  32.            mov   edi, endofcode-OS_BASE
  33.            mov   ecx, (uglobals_size/4)+4
  34.            rep   stosd
  35.  
  36. ; save [0..0xffff]
  37.            xor   esi, esi
  38.            mov   edi, (BOOT_VAR - OS_BASE)              ; low mem storage area
  39.            mov   ecx, 0x10000 / 4
  40.            rep   movsd
  41. ; clear [0x1000..0x0ffff]
  42.            mov   edi,0x1000
  43.            mov   ecx,0xf000 / 4
  44.            rep   stosd
  45.  
  46. ; clear <sys_pgdir> table
  47.            mov edi, sys_pgdir-OS_BASE
  48.            mov ecx, 4096/4
  49.            rep stosd
  50.         ret
  51.  
  52. ; ======================================================================
  53. align 4
  54. proc init_mem
  55.  
  56.            mov  ecx, 0xC001001A                         ; Top of Memory MSR
  57.            xor  edi, edi
  58.            rdmsr
  59.            mov  esi, eax                                ; esi = total amount of memory
  60.            mov  ecx, 0x0200
  61. .read_mtrr:
  62.            rdmsr
  63.            and  eax, 0xFFF00000                         ; not just bitcleaning
  64.            jz   .next_mtrr                              ; ignore the main memory and free MTRRs
  65.            cmp  esi, eax
  66.            jb   .next_mtrr                              ; ignore MMIO blocks
  67.            mov  esi, eax
  68. .next_mtrr:
  69.            add  cl, 2
  70.            cmp  cl, 0x10
  71.            jb   .read_mtrr
  72.  
  73.            mov  eax, USER_DMA_SIZE
  74.            sub  esi, eax                                ; exclude the Global DMA block...
  75.            and  esi, 0xFF800000                         ; ...and the hole above it
  76.            mov  eax, esi
  77.            mov  [MEM_AMOUNT-OS_BASE], eax
  78.            mov  [pg_data.mem_amount-OS_BASE], eax       ; the true MEMTOP
  79.            mov  [UserDMAaddr-OS_BASE], eax
  80.  
  81.            mov edx, esi                                 ; edx will hold maximum allocatable address
  82.            shr esi, 12
  83.            mov [pg_data.pages_count-OS_BASE], esi       ; max number of PTEs   ?
  84.  
  85.            shr edx, 12
  86.            add edx, 31
  87.            and edx, not 31
  88.            shr edx, 3
  89.            mov [pg_data.pagemap_size-OS_BASE], edx      ; size of sys_pgmap structure
  90.  
  91.            add edx, (sys_pgmap-OS_BASE)+4095
  92.            and edx, not 4095
  93.            mov [tmp_page_tabs], edx                     ; free zone to build PTEs
  94.  
  95.            mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
  96.            mov [pg_data.kernel_pages -OS_BASE], edx
  97.            shr edx, 10
  98.            mov [pg_data.kernel_tables-OS_BASE], edx
  99.  
  100.            mov edx, (sys_pgdir-OS_BASE)+ 0x800          ; (0x800 = OS_BASE shr 20)
  101.  
  102.            mov ebx, cr4
  103.            or  ebx, CR4_PSE
  104.            mov eax, PG_LARGE+PG_SW
  105.            mov cr4, ebx
  106.            dec [pg_data.kernel_tables-OS_BASE]
  107.  
  108.            mov [edx], eax                               ; map first (physical) 4M bytes
  109.            add edx, 4
  110.  
  111.            mov edi, [tmp_page_tabs]
  112.            mov ecx, [pg_data.kernel_pages -OS_BASE]     ; safety cleaning of already-zeroed space
  113.            xor eax, eax
  114.            rep stosd
  115.  
  116.            mov ecx, [pg_data.kernel_tables-OS_BASE]     ; build some PDEs to hold empty PTEs
  117.            mov eax, [tmp_page_tabs]
  118.            or  eax, PG_SW
  119.            mov edi, edx                 ; edi = sys_pgdir+0x804
  120.  
  121. .map_kernel_tabs:
  122.            stosd
  123.            add eax, 0x1000
  124.            dec ecx
  125.            jnz .map_kernel_tabs
  126.  
  127. ; map pagetables to linear space
  128.            mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  129.  
  130.            mov edi, (sys_pgdir-OS_BASE)
  131.            lea esi, [edi+(OS_BASE shr 20)]
  132.            movsd
  133.            movsd
  134.            ret
  135. endp
  136.  
  137. align 4
  138. proc init_page_map
  139. ; mark all memory as unavailable
  140.            mov edi, sys_pgmap-OS_BASE
  141.            mov ecx, [pg_data.pagemap_size-OS_BASE]
  142.            shr ecx, 2
  143.            xor eax, eax
  144.            cld
  145.            rep stosd
  146.  
  147. ; scan through memory map and mark free areas as available
  148.            mov ebx, BOOT_VAR-OS_BASE + 0x9104
  149.            mov edx, [ebx-4]
  150. .scanmap:
  151.            mov ecx, [ebx+8]
  152.            shr ecx, 12 ; ecx = number of pages
  153.            jz .next
  154.            mov edi, [ebx]
  155.            shr edi, 12 ; edi = first page
  156.            mov eax, edi
  157.            shr edi, 5
  158.            shl edi, 2
  159.            add edi, sys_pgmap-OS_BASE
  160.            and eax, 31
  161.            jz .startok
  162.            add ecx, eax
  163.            sub ecx, 32
  164.            jbe .onedword
  165.            push ecx
  166.            mov ecx, eax
  167.            or eax, -1
  168.            shl eax, cl
  169.            or [edi], eax
  170.            add edi, 4
  171.            pop ecx
  172. .startok:
  173.            push ecx
  174.            shr ecx, 5
  175.            or eax, -1
  176.            rep stosd
  177.            pop ecx
  178.            and ecx, 31
  179.            neg eax
  180.            shl eax, cl
  181.            dec eax
  182.            or [edi], eax
  183.            jmp .next
  184. .onedword:
  185.            add ecx, 32
  186.            sub ecx, eax
  187. @@:
  188.            bts [edi], eax
  189.            inc eax
  190.            loop @b
  191. .next:
  192.            add ebx, 20
  193.            dec edx
  194.            jnz .scanmap
  195.  
  196. ; mark kernel memory as allocated (unavailable)
  197.            mov ecx, [tmp_page_tabs]
  198.            mov edx, [pg_data.pages_count-OS_BASE]
  199.            shr ecx, 12
  200.            add ecx, [pg_data.kernel_tables-OS_BASE]
  201.            sub edx, ecx
  202.            mov [pg_data.pages_free-OS_BASE], edx
  203.  
  204.            mov edi, sys_pgmap-OS_BASE
  205.            mov ebx, ecx
  206.            shr ecx, 5
  207.            xor eax, eax
  208.            rep stosd
  209.  
  210.            not eax
  211.            mov ecx, ebx
  212.            and ecx, 31
  213.            shl eax, cl
  214.            and [edi], eax
  215.            add edi, OS_BASE
  216.            mov [page_start-OS_BASE], edi;
  217.  
  218.            mov ebx, sys_pgmap
  219.            add ebx, [pg_data.pagemap_size-OS_BASE]
  220.            mov [page_end-OS_BASE], ebx
  221.  
  222.            mov [pg_data.pg_mutex-OS_BASE], 0
  223.            ret
  224. endp
  225.  
  226. align 4
  227.  
  228. init_BIOS32:
  229.            mov edi, 0xE0000
  230. .pcibios_nxt:
  231.            cmp dword[edi], '_32_' ; "magic" word
  232.            je .BIOS32_found
  233. .pcibios_nxt2:
  234.            add edi, 0x10
  235.            cmp edi, 0xFFFF0
  236.            je .BIOS32_not_found
  237.            jmp .pcibios_nxt
  238. .BIOS32_found:                  ; magic word found, check control summ
  239.  
  240.            movzx ecx, byte[edi + 9]
  241.            shl ecx, 4
  242.            mov esi, edi
  243.            xor eax, eax
  244.            cld   ; paranoia
  245. @@:     lodsb
  246.            add ah, al
  247.            loop @b
  248.            jnz .pcibios_nxt2 ; control summ must be zero
  249.     ; BIOS32 service found !
  250.            mov ebp, [edi + 4]
  251.            mov [bios32_entry], ebp
  252.     ; check PCI BIOS present
  253.            mov eax, '$PCI'
  254.            xor ebx, ebx
  255.            push cs  ; special for 'ret far' from  BIOS
  256.            call ebp
  257.            test al, al
  258.            jnz .PCI_BIOS32_not_found
  259.  
  260.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  261.  
  262.            add ebx, OS_BASE
  263.            dec ecx
  264.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  265.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  266.  
  267.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  268.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  269.  
  270.            shr ebx, 16
  271.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  272.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  273.  
  274.            shr ecx, 16
  275.            and cl, 0x0F
  276.            mov ch, bh
  277.            add cx, D32
  278.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  279.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  280.  
  281.            mov [(pci_bios_entry-OS_BASE)], edx
  282.          ; jmp .end
  283. .PCI_BIOS32_not_found:
  284.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  285. .BIOS32_not_found:
  286. .end:
  287.            ret
  288.  
  289. align 4
  290. test_cpu:       ; only AMD machines supported
  291.  
  292.            xor eax, eax
  293.            mov [cpu_caps-OS_BASE], eax
  294.            mov [cpu_caps+4-OS_BASE], eax
  295.  
  296.            pushfd
  297.            pop eax
  298.            mov ecx, eax
  299.            xor eax, 0x40000
  300.            push eax
  301.            popfd
  302.            pushfd
  303.            pop eax
  304.            xor eax, ecx
  305.            jz $                 ; 386
  306.            push ecx
  307.            popfd
  308.  
  309.            mov eax, ecx
  310.            xor eax, 0x200000
  311.            push eax
  312.            popfd
  313.            pushfd
  314.            pop eax
  315.            xor eax, ecx
  316.            je $                 ; 486
  317.  
  318.            xor eax, eax
  319.            cpuid
  320.  
  321.            mov [cpu_vendor-OS_BASE],   ebx
  322.            mov [cpu_vendor+4-OS_BASE], edx
  323.            mov [cpu_vendor+8-OS_BASE], ecx
  324.  
  325.            cmp ebx, dword [AMD_str-OS_BASE]
  326.            jne $
  327.            cmp edx, dword [AMD_str+4-OS_BASE]
  328.            jne $
  329.            cmp ecx, dword [AMD_str+8-OS_BASE]
  330.            jne $
  331.            cmp eax, 1
  332.            jl $
  333.            mov eax, 1
  334.            cpuid
  335.            mov [cpu_sign-OS_BASE],  eax
  336.            mov [cpu_info-OS_BASE],  ebx
  337.            mov [cpu_caps-OS_BASE],  edx
  338.            mov [cpu_caps+4-OS_BASE],ecx
  339.            shr eax, 8
  340.            and eax, 0x0f
  341.            ret
  342.  
  343.  
  344.  
  345.