Subversion Repositories Kolibri OS

Rev

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