Cant display Upper and Lower Memory

0

The OS Dosent display the upper and lower memory for some reason. As you can see here in Detecting Memory Wiki : http://wiki.osdev.org/Detecting_Memory_(x86)#Memory_Map_Via_GRUB , it says

Refer to mbd->mem_lower for conventional memory (e.g. physical addresses ranging between 0 and 640KB) and mbd->mem_upper for high memory (e.g. from 1MB). Both are given in kibibytes

Well i am exactly doing so here:

kernel.c++ :

#include "types.h"
#include "gdt.h"
#include "stdio.h"
#include "serial.h"
#include "mem.h"
#include "idt.h"
#include "timer.h"
#include "isr.h"
#include "kbd.h"
#include "mouse.h"
#include "irq.h"
#include "string.h"
#include "terminal.h"
#include "multiboot.h"
#include "pmm.h"
#include "heap.h"




//Call all class constructor
//for global objects before
//calling the kernel
typedef void (*constructor)();
extern "C" constructor start_ctors;
extern "C" constructor end_ctors;
extern "C" void callConstructors()
{
    for(constructor* i = &start_ctors; i != &end_ctors; i++)
       (*i)();
}




extern "C" void kernelMain(uint32_t kernel_virtual_end,
        uint32_t placeholder,
        uint32_t  kernel_physical_end,
        uint32_t kernel_physical_start, uint32_t  kernel_virtual_start,
        multiboot_info_t multiboot_structure,uint32_t magicnumber
        )
{


       cls();
       printf("******KERNEL INFO********\n");
       printf("KERNEL START VIRTUAL 0x%x\n" , kernel_virtual_start);
       printf("KERNEL START PHYSICAL 0x%x\n" , kernel_physical_start);
       printf("KERNEL END VIRTUAL 0x%x\n" , kernel_virtual_end);
       printf("KERNEL END PHYSICAL 0x%x\n" , kernel_physical_end);
       printf("*************************\n\n");
       printf("********RAM INFO*********\n");
       printf("LOWER MEMORY : %x \n" , (uint32_t)multiboot_structure.mem_lower);
       printf("UPPER MEMORY : %x \n" , (uint32_t)multiboot_structure.mem_upper);
       printf("*************************\n");
       gdt gt;
       IDT idt;
       ISR isr;
       IRQ irq;
       SerialPort sp;
       isr.install_isrs();
       irq.install_irqs();
        Timer timer;
        timer.install_timer();
        KBD kbd;
        kbd.install_kbd_driver();


        MOUSE mouse;
        mouse.install_mouse_driver();
        __asm__ __volatile__ ("sti");




   while(1);
   err:
       while(1);
}

boot.asm:

;Global MultiBoot Kernel Recongnzatio

; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0             ; align loaded modules on page boundaries
MEMINFO     equ  1<<1             ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002     ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)  ; checksum required


;Putting in object file
section .multiboot

    dd MAGIC
    dd FLAGS
    dd CHECKSUM

section .data

KERNEL_VIRTUAL_BASE equ 0xC0000000                  ; 3GB
KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22)  ; Page directory index of kernel's 4MB PTE.

align 0x1000
BootPageDirectory:
    ; This page directory entry identity-maps the first 4MB of the 32-bit physical address space.
    ; All bits are clear except the following:
    ; bit 7: PS The kernel page is 4MB.
    ; bit 1: RW The kernel page is read/write.
    ; bit 0: P  The kernel page is present.
    ; This entry must be here -- otherwise the kernel will crash immediately after paging is
    ; enabled because it can't fetch the next instruction! It's ok to unmap this page later.
    dd 0x00000083
    times (KERNEL_PAGE_NUMBER - 1) dd 0                 ; Pages before kernel space.
    ; This page directory entry defines a 4MB page containing the kernel.
    dd 0x00000083
    times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0  ; Pages after the kernel image.


section .text
    ; reserve initial kernel stack space -- that's 16k.
    STACKSIZE equ 0x4000
    global loader
    global BootPageDirectory

        loader:

                    ;Enable Paging START

                    ; NOTE: Until paging is set up, the code must be position-independent and use physical
                    ; addresses, not virtual ones!
                    mov ecx, (BootPageDirectory - KERNEL_VIRTUAL_BASE)
                    mov cr3, ecx                                        ; Load Page Directory Base Register.

                    mov ecx, cr4
                    or ecx, 0x00000010                          ; Set PSE bit in CR4 to enable 4MB pages.
                    mov cr4, ecx

                    mov ecx, cr0
                    or ecx, 0x80000000                          ; Set PG bit in CR0 to enable paging.
                    mov cr0, ecx


                    lea ebx, [higherhalf]
                    jmp ebx ; Absolute Jump

        higherhalf:
            extern kernelMain
            extern callConstructors

                ; Unmap the identity-mapped first 4MB of physical address space. It should not be needed
                ; anymore.
                mov dword [BootPageDirectory], 0
                invlpg [0]

                mov esp, stack + STACKSIZE            ; set up the stack
                call callConstructors



              extern kernel_virtual_start
              extern kernel_virtual_end
              extern kernel_physical_start
              extern kernel_physical_end


                push kernel_virtual_end ; 2
                push 5
                push kernel_virtual_start ; 1
                push kernel_physical_start ; 3
                push kernel_physical_end ; 4
                push eax ; 5
                push ebx ; 6
                call kernelMain


                jmp _eof

        _eof:
             cli
             hlt 
             jmp _eof


section .bss
align 32
stack:
    resb STACKSIZE      ; reserve 16k stack on a uint64_t boundary

When i complete above i get these wierd characters when i print low and high memory (which give you ), as you can see here: https://www.youtube.com/watch?v=nDxSOkKd_NI . You can see full source code here: https://raw.githubusercontent.com/amanuel2/OS_Mirror . Help Would Be Appreciated.

memory-management
x86
ram
osdev
asked on Stack Overflow Aug 22, 2016 by amanuel2 • edited Aug 22, 2016 by amanuel2

1 Answer

-1

Your code seems to have multiple errors -

1. Destruction of register values on calling static object constructors - When you execute the following code -

MOV ESP, stack + STACKSIZE
CALL callConstructors

then you need to save the register values that you are dependent on, and here EAX & EBX are crucial for holding the multiboot-related values. If any other registers are used before the call, they should be saved & you should read your ABI specification for caller/callee saved registers.

2. Parameters pushed in reverse order - This may seem awkward to you that your arguments to

push kernel_virtual_end ; 2
push 5
push kernel_virtual_start ; 1
push kernel_physical_start ; 3
push kernel_physical_end ; 4
push eax ; 5
push ebx ; 6
call kernelMain

call kernelMain are in reverse order, although you see them straight in code. You should have written your assembly-calling code as

push ebx ;6
push eax ; 5
push kernel_physical_end ; 4
push kernel_physical_start ; 3
push kernel_virtual_start ; 1
push 5
push kernel_virtual_end ; 2
call kernelMain

To understand why this is required, you should know that the IA32 stack grows downward. This means the value in ESP (address of processor stack) reduces by 4 (for 32-bit platform, otherwise 8 in 64-bit platform) on every

push <REG>

but in C, the first argument is the one at the lowest-address, or the one which is pushed last. Thus, you must push the arguments in a reverse-fashion in assembly code.

answered on Stack Overflow Nov 11, 2017 by Shukant Pal

User contributions licensed under CC BY-SA 3.0