12,92 → 12,72 |
MEM_WC equ 1 ;write combined memory |
MEM_UC equ 0 ;uncached memory |
|
; ====================================================================== |
align 4 |
preinit_mem: |
|
; clear [0x280000..HEAP_BASE] |
xor eax,eax |
mov edi,0x280000 ; 0x280000 = ramdisk FAT ? |
mov ecx,(HEAP_BASE-OS_BASE-0x280000) / 4 |
cld |
rep stosd |
|
; clear [0x40000..0x90000] |
mov edi,0x50000 ; 0x50000 is somewhere inside kernel code? |
mov ecx,(0x90000-0x50000)/4 |
rep stosd |
|
; clear undefined kernel globals |
mov edi, endofcode-OS_BASE |
mov ecx, (uglobals_size/4)+4 |
rep stosd |
|
; save [0..0xffff] |
xor esi, esi |
mov edi,0x2F0000 ; low mem storage area |
mov ecx, 0x10000 / 4 |
rep movsd |
; clear [0x1000..0x0ffff] |
mov edi,0x1000 |
mov ecx,0xf000 / 4 |
rep stosd |
|
; clear <sys_pgdir> table |
mov edi, sys_pgdir-OS_BASE |
mov ecx, 4096/4 |
rep stosd |
ret |
|
; ====================================================================== |
align 4 |
proc init_mem |
; calculate maximum allocatable address and number of allocatable pages |
mov edi, BOOT_VAR-OS_BASE + 0x9104 |
mov ecx, [edi-4] |
xor esi, esi ; esi will hold total amount of memory |
xor edx, edx ; edx will hold maximum allocatable address |
.calcmax: |
; round all to pages |
mov eax, [edi] |
test eax, 0xFFF |
jz @f |
neg eax |
and eax, 0xFFF |
add [edi], eax |
adc dword [edi+4], 0 |
sub [edi+8], eax |
sbb dword [edi+12], 0 |
jc .unusable |
@@: |
and dword [edi+8], not 0xFFF |
jz .unusable |
; ignore memory after 4 Gb |
cmp dword [edi+4], 0 |
jnz .unusable |
mov eax, [edi] |
cmp dword [edi+12], 0 |
jnz .overflow |
add eax, [edi+8] |
jnc @f |
.overflow: |
mov eax, 0xFFFFF000 |
@@: |
cmp edx, eax |
jae @f |
mov edx, eax |
@@: |
sub eax, [edi] |
mov [edi+8], eax |
add esi, eax |
jmp .usable |
.unusable: |
and dword [edi+8], 0 |
.usable: |
add edi, 20 |
loop .calcmax |
.calculated: |
|
mov esi, (PCIe_CONFIG_SPACE-OS_BASE) ; esi will hold total amount of memory |
mov edx, esi ; edx will hold maximum allocatable address |
|
mov [MEM_AMOUNT-OS_BASE], esi |
mov [pg_data.mem_amount-OS_BASE], esi |
shr esi, 12 |
mov [pg_data.pages_count-OS_BASE], esi |
mov [pg_data.pages_count-OS_BASE], esi ; max number of PTEs ? |
|
shr edx, 12 |
add edx, 31 |
and edx, not 31 |
shr edx, 3 |
mov [pg_data.pagemap_size-OS_BASE], edx |
mov [pg_data.pagemap_size-OS_BASE], edx ; size of sys_pgmap structure |
|
add edx, (sys_pgmap-OS_BASE)+4095 |
and edx, not 4095 |
mov [tmp_page_tabs], edx |
mov [tmp_page_tabs], edx ; free zone to build PTEs |
|
mov edx, esi |
and edx, -1024 |
cmp edx, (OS_BASE/4096) |
jbe @F |
mov edx, (OS_BASE/4096) |
jmp .set |
@@: |
cmp edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096 |
jae .set |
mov edx, (HEAP_BASE-OS_BASE+HEAP_MIN_SIZE)/4096 |
.set: |
mov [pg_data.kernel_pages-OS_BASE], edx |
shr edx, 10 |
mov [pg_data.kernel_tables-OS_BASE], edx |
|
xor eax, eax |
mov edi, sys_pgdir-OS_BASE |
mov ecx, 4096/4 |
cld |
rep stosd |
mov edx, (sys_pgdir-OS_BASE)+ 0x800 ; (0x800 = OS_BASE shr 20) |
|
mov edx, (sys_pgdir-OS_BASE)+ 0x800; (OS_BASE shr 20) |
|
mov ebx, cr4 |
or ebx, CR4_PSE |
mov eax, PG_LARGE+PG_SW |
104,19 → 84,18 |
mov cr4, ebx |
dec [pg_data.kernel_tables-OS_BASE] |
|
mov [edx], eax |
mov [edx], eax ; map first (physical) 4M bytes |
add edx, 4 |
|
mov edi, [tmp_page_tabs] |
mov ecx, [pg_data.kernel_tables-OS_BASE] |
shl ecx, 10 |
mov ecx, [pg_data.kernel_pages -OS_BASE] ; safety cleaning of already-zeroed space |
xor eax, eax |
rep stosd |
|
mov ecx, [pg_data.kernel_tables-OS_BASE] |
mov ecx, [pg_data.kernel_tables-OS_BASE] ; build some PDEs to hold empty PTEs |
mov eax, [tmp_page_tabs] |
or eax, PG_SW |
mov edi, edx |
mov edi, edx ; edi = sys_pgdir+0x804 |
|
.map_kernel_tabs: |
stosd |
124,6 → 103,7 |
dec ecx |
jnz .map_kernel_tabs |
|
; map pagetables to linear space |
mov dword [sys_pgdir-OS_BASE+(page_tabs shr 20)], sys_pgdir+PG_SW-OS_BASE |
|
mov edi, (sys_pgdir-OS_BASE) |
286,16 → 266,9 |
ret |
|
align 4 |
proc test_cpu |
locals |
cpu_type dd ? |
cpu_id dd ? |
cpu_Intel dd ? |
cpu_AMD dd ? |
endl |
test_cpu: ; only AMD machines supported |
|
xor eax, eax |
mov [cpu_type], eax |
mov [cpu_caps-OS_BASE], eax |
mov [cpu_caps+4-OS_BASE], eax |
|
308,12 → 281,10 |
pushfd |
pop eax |
xor eax, ecx |
mov [cpu_type], CPU_386 |
jz .end_cpuid |
jz $ ; 386 |
push ecx |
popfd |
|
mov [cpu_type], CPU_486 |
mov eax, ecx |
xor eax, 0x200000 |
push eax |
321,8 → 292,7 |
pushfd |
pop eax |
xor eax, ecx |
je .end_cpuid |
mov [cpu_id], 1 |
je $ ; 486 |
|
xor eax, eax |
cpuid |
330,39 → 300,15 |
mov [cpu_vendor-OS_BASE], ebx |
mov [cpu_vendor+4-OS_BASE], edx |
mov [cpu_vendor+8-OS_BASE], ecx |
cmp ebx, dword [intel_str-OS_BASE] |
jne .check_AMD |
cmp edx, dword [intel_str+4-OS_BASE] |
jne .check_AMD |
cmp ecx, dword [intel_str+8-OS_BASE] |
jne .check_AMD |
mov [cpu_Intel], 1 |
cmp eax, 1 |
jl .end_cpuid |
mov eax, 1 |
cpuid |
mov [cpu_sign-OS_BASE], eax |
mov [cpu_info-OS_BASE], ebx |
mov [cpu_caps-OS_BASE], edx |
mov [cpu_caps+4-OS_BASE],ecx |
|
shr eax, 8 |
and eax, 0x0f |
ret |
.end_cpuid: |
mov eax, [cpu_type] |
ret |
|
.check_AMD: |
cmp ebx, dword [AMD_str-OS_BASE] |
jne .unknown |
jne $ |
cmp edx, dword [AMD_str+4-OS_BASE] |
jne .unknown |
jne $ |
cmp ecx, dword [AMD_str+8-OS_BASE] |
jne .unknown |
mov [cpu_AMD], 1 |
jne $ |
cmp eax, 1 |
jl .unknown |
jl $ |
mov eax, 1 |
cpuid |
mov [cpu_sign-OS_BASE], eax |
372,16 → 318,6 |
shr eax, 8 |
and eax, 0x0f |
ret |
.unknown: |
mov eax, 1 |
cpuid |
mov [cpu_sign-OS_BASE], eax |
mov [cpu_info-OS_BASE], ebx |
mov [cpu_caps-OS_BASE], edx |
mov [cpu_caps+4-OS_BASE],ecx |
shr eax, 8 |
and eax, 0x0f |
ret |
endp |
|
|
|