Subversion Repositories Kolibri OS

Rev

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