Subversion Repositories Kolibri OS

Rev

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