Subversion Repositories Kolibri OS

Rev

Rev 2350 | Rev 3168 | 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: 2425 $
  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.  
  55. init_mem:
  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.            shr esi, 12
  82.            mov [pg_data.pages_count-OS_BASE], esi       ; max number of PTEs   ?
  83.  
  84.            mov edx, esi                                 ; edx will hold maximum allocatable address
  85.            shr edx, 3
  86.            mov [pg_data.pagemap_size-OS_BASE], edx      ; size of sys_pgmap structure
  87.  
  88.            add edx, (sys_pgmap-OS_BASE)+4095
  89.            and edx, not 4095
  90.            mov [tmp_page_tabs], edx                     ; free zone to build PTEs for all available memory
  91.  
  92.            mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096
  93.            mov [pg_data.kernel_pages -OS_BASE], edx
  94.            shr edx, 10
  95.            mov [pg_data.kernel_tables-OS_BASE], edx     ; number of Kernel PDEs needed
  96.  
  97.            mov edx, (sys_pgdir-OS_BASE)+ 0x800          ; (0x800 = OS_BASE shr 20)
  98.  
  99.            mov ebx, cr4
  100.            or  ebx, CR4_PSE
  101.            mov eax, PG_LARGE+PG_SW
  102.            mov cr4, ebx
  103.            dec [pg_data.kernel_tables-OS_BASE]
  104.  
  105.            mov [edx], eax                               ; map first (physical) 4M bytes
  106.            add edx, 4
  107.  
  108.            mov edi, [tmp_page_tabs]
  109.            mov ecx, [pg_data.kernel_pages -OS_BASE]     ; safety cleaning of already-zeroed space
  110.            xor eax, eax
  111.            rep stosd
  112.  
  113.            mov ecx, [pg_data.kernel_tables-OS_BASE]     ; build some PDEs to hold empty PTEs
  114.            mov eax, [tmp_page_tabs]
  115.            or  eax, PG_SW
  116.            mov edi, edx                 ; edi = sys_pgdir+0x804
  117.  
  118. .map_kernel_tabs:
  119.            stosd
  120.            add eax, 0x1000
  121.            dec ecx
  122.            jnz .map_kernel_tabs
  123.  
  124. ; map pagetables to linear space
  125.            mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE
  126.  
  127.            mov edi, (sys_pgdir-OS_BASE)
  128.            lea esi, [edi+(OS_BASE shr 20)]
  129.            movsd
  130.            movsd
  131.  
  132. init_page_map:
  133. ; mark all memory as available
  134.            mov edi, sys_pgmap-OS_BASE
  135.            mov ecx, [pg_data.pagemap_size-OS_BASE]
  136.            shr ecx, 2
  137.            mov eax, -1
  138.            cld
  139.            rep stosd
  140.  
  141.  
  142. ; mark kernel memory as allocated (unavailable)
  143.            mov ecx, [tmp_page_tabs]
  144.            mov edx, [pg_data.pages_count-OS_BASE]
  145.            shr ecx, 12
  146.            add ecx, [pg_data.kernel_tables-OS_BASE]
  147.            sub edx, ecx
  148.            mov [pg_data.pages_free-OS_BASE], edx
  149.  
  150.            mov edi, sys_pgmap-OS_BASE
  151.            mov ebx, ecx
  152.            shr ecx, 5
  153.            xor eax, eax
  154.            rep stosd
  155.  
  156.            not eax
  157.            mov ecx, ebx
  158.            and ecx, 31
  159.            shl eax, cl
  160.            and [edi], eax
  161.            add edi, OS_BASE
  162.            mov [page_start-OS_BASE], edi;
  163.  
  164.            mov ebx, sys_pgmap
  165.            add ebx, [pg_data.pagemap_size-OS_BASE]
  166.            mov [page_end-OS_BASE], ebx
  167.  
  168.            mov [pg_data.pg_mutex-OS_BASE], 0
  169.            ret
  170.  
  171.  
  172. align 4
  173.  
  174. init_BIOS32:
  175.            mov edi, 0xE0000
  176. .pcibios_nxt:
  177.            cmp dword[edi], '_32_' ; "magic" word
  178.            je .BIOS32_found
  179. .pcibios_nxt2:
  180.            add edi, 0x10
  181.            cmp edi, 0xFFFF0
  182.            je .BIOS32_not_found
  183.            jmp .pcibios_nxt
  184. .BIOS32_found:                  ; magic word found, check control summ
  185.  
  186.            movzx ecx, byte[edi + 9]
  187.            shl ecx, 4
  188.            mov esi, edi
  189.            xor eax, eax
  190.            cld   ; paranoia
  191. @@:     lodsb
  192.            add ah, al
  193.            loop @b
  194.            jnz .pcibios_nxt2 ; control summ must be zero
  195.     ; BIOS32 service found !
  196.            mov ebp, [edi + 4]
  197.            mov [bios32_entry], ebp
  198.     ; check PCI BIOS present
  199.            mov eax, '$PCI'
  200.            xor ebx, ebx
  201.            push cs  ; special for 'ret far' from  BIOS
  202.            call ebp
  203.            test al, al
  204.            jnz .PCI_BIOS32_not_found
  205.  
  206.  ; çäåñü ñîçäàþòñÿ äèñêðèïòîðû äëÿ PCI BIOS
  207.  
  208.            add ebx, OS_BASE
  209.            dec ecx
  210.            mov [(pci_code_32-OS_BASE)], cx    ;limit 0-15
  211.            mov [(pci_data_32-OS_BASE)], cx    ;limit 0-15
  212.  
  213.            mov [(pci_code_32-OS_BASE)+2], bx  ;base  0-15
  214.            mov [(pci_data_32-OS_BASE)+2], bx  ;base  0-15
  215.  
  216.            shr ebx, 16
  217.            mov [(pci_code_32-OS_BASE)+4], bl  ;base  16-23
  218.            mov [(pci_data_32-OS_BASE)+4], bl  ;base  16-23
  219.  
  220.            shr ecx, 16
  221.            and cl, 0x0F
  222.            mov ch, bh
  223.            add cx, D32
  224.            mov [(pci_code_32-OS_BASE)+6], cx  ;lim   16-19 &
  225.            mov [(pci_data_32-OS_BASE)+6], cx  ;base  24-31
  226.  
  227.            mov [(pci_bios_entry-OS_BASE)], edx
  228.          ; jmp .end
  229. .PCI_BIOS32_not_found:
  230.         ; çäåñü äîëæíà çàïîëíÿòñÿ pci_emu_dat
  231. .BIOS32_not_found:
  232. .end:
  233.            ret
  234.  
  235. align 4
  236. test_cpu:       ; only AMD machines supported
  237.  
  238.            xor eax, eax
  239.            mov [cpu_caps-OS_BASE], eax
  240.            mov [cpu_caps+4-OS_BASE], eax
  241.  
  242.            pushfd
  243.            pop eax
  244.            mov ecx, eax
  245.            xor eax, 0x40000
  246.            push eax
  247.            popfd
  248.            pushfd
  249.            pop eax
  250.            xor eax, ecx
  251.            jz $                 ; 386
  252.            push ecx
  253.            popfd
  254.  
  255.            mov eax, ecx
  256.            xor eax, 0x200000
  257.            push eax
  258.            popfd
  259.            pushfd
  260.            pop eax
  261.            xor eax, ecx
  262.            je $                 ; 486
  263.  
  264.            xor eax, eax
  265.            cpuid
  266.  
  267.            mov [cpu_vendor-OS_BASE],   ebx
  268.            mov [cpu_vendor+4-OS_BASE], edx
  269.            mov [cpu_vendor+8-OS_BASE], ecx
  270.  
  271.            cmp ebx, dword [AMD_str-OS_BASE]
  272.            jne $
  273.            cmp edx, dword [AMD_str+4-OS_BASE]
  274.            jne $
  275.            cmp ecx, dword [AMD_str+8-OS_BASE]
  276.            jne $
  277.            cmp eax, 1
  278.            jl $
  279.            mov eax, 1
  280.            cpuid
  281.            mov [cpu_sign-OS_BASE],  eax
  282.            mov [cpu_info-OS_BASE],  ebx
  283.            mov [cpu_caps-OS_BASE],  edx
  284.            mov [cpu_caps+4-OS_BASE],ecx
  285.            shr eax, 8
  286.            and eax, 0x0f
  287.            ret
  288.  
  289.  
  290.  
  291.