Jumping into infinite loop when mapping virtual address

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Like the title, i followed Jose's instructions on enabling paging. Everything was fine until it mapped the virtual address to the physical address above KMEM_MAX. At that point it fell into an infinite loop. I don't know how to fix this error, does anyone know how to fix it?

Here is my source code (copy from Jose, cause this is the only way I know to fix it, but useless):
In "paging.h":

Code: Select all

#ifndef PAGING_H
#define PAGING_H

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

#include "../common/debug.h"
#include "../common/string.h"
#include "../interrupt/isr.h"

/** Assume 4KiB pages, not support any other sizes. */
#define PAGE_SIZE 4096

#define PTES_PER_PAGE 1024
#define PDES_PER_PAGE 1024


/** Number of physical frames available. Assume 128MiB physical memory. */
#define PHYS_MAX 0x08000000     /** 128MiB physical memory. */
#define NUM_FRAMES (PHYS_MAX / PAGE_SIZE)

/** Up to where is kernel memory, == the upper bound of kernel heap. */
#define KMEM_MAX 0x00800000     /** 8MiB reserved for the kernel. */


/**
 * Page table entry format, 32bits per entry. Order in struct
 * definition is from LSB -> MSB.
 * 
 * See https://wiki.osdev.org/Paging for the detailed definition.
 */
struct page_table_entry {
    uint32_t present  :  1;     /** Set -> present in memory. */
    uint32_t writable :  1;     /** Set -> user writable. (read/write bit) */
    uint32_t user     :  1;     /** Set -> user accessible. */
    uint32_t unused0  :  2;     /** Unused 2 caching bits. */
    uint32_t accessed :  1;     /** Set -> accessed sinced mapped. */
    uint32_t dirty    :  1;     /** Set -> page has been written to. */
    uint32_t unused1  :  5;     /** Unused 5 misc bits. */
    uint32_t frame    : 20;     /** Physical frame number of the page. */
} __attribute__((packed));
typedef struct page_table_entry pte_t;

/**
 * Page directory entry format, 32bits per entry. Order in struct
 * definition is from LSB -> MSB.
 * 
 * See https://wiki.osdev.org/Paging for the detailed definition.
 */
struct page_directory_entry {
    uint32_t present  :  1;     /** Set -> present in memory. */
    uint32_t writable :  1;     /** Set -> user writable. (read/write bit) */
    uint32_t user     :  1;     /** Set -> user accessible. */
    uint32_t unused0  :  2;     /** Unused 2 caching bits. */
    uint32_t accessed :  1;     /** Set -> accessed sinced mapped. */
    uint32_t unused1  :  1;     /** Unused bit. */
    uint32_t size     :  1;     /** 0 -> using 4KiB page size. */
    uint32_t unused2  :  4;     /** Unused 4 misc bits. */
    uint32_t frame    : 20;     /** Physical frame number of level-2 table. */
} __attribute__((packed));
typedef struct page_directory_entry pde_t;

/** Helper macros on addresses and page alignments. */
#define ADDR_PAGE_OFFSET(addr) ((addr) & 0x00000FFF)
#define ADDR_PAGE_NUMBER(addr) ((addr) >> 12)

#define ADDR_PDE_INDEX(addr) (ADDR_PAGE_NUMBER(addr) / 1024)
#define ADDR_PTE_INDEX(addr) (ADDR_PAGE_NUMBER(addr) % 1024)

#define ADDR_PAGE_ALIGNED(addr) (ADDR_PAGE_OFFSET(addr) == 0)

#define ADDR_PAGE_ROUND_DN(addr) ((addr) & 0xFFFFF000)
#define ADDR_PAGE_ROUND_UP(addr) (ADDR_PAGE_ROUND_DN((addr) + 0x00000FFF))


/** Helper macro on getting the pointed-to address stored in an entry. */
#define ENTRY_FRAME_ADDR(entry) ((uint32_t) (entry).frame << 12)


void paging_init();
void paging_switch_pgdir(pde_t *pgdir);

#endif
In "paging.c":

Code: Select all

#include "paging.h"

/** Kernel heap bottom address - should be above `elf_shstrtab_end`. */
uint32_t kheap_curr;


/**
 * Auxiliary function for allocating (page-aligned) chunks of memory in the
 * kernel heap region that never gets freed.
 * 
 * Should only be used to allocate the kernel's page directory/tables and
 * the frames bitmap and other things before our actual heap allocation
 * algorithm setup.
 */
static uint32_t
_kalloc_temp(size_t size, bool page_align)
{
    /** If `page_align` is set, return an aligned address. */
    if (page_align && !ADDR_PAGE_ALIGNED(kheap_curr))
        kheap_curr = ADDR_PAGE_ROUND_UP(kheap_curr);

    /** If exceeds the 8MiB kernel memory boundary, panic. */
    if (kheap_curr + size > KMEM_MAX)
        error("_kalloc_temp: kernel memory exceeds boundary");

    uint32_t temp = kheap_curr;
    kheap_curr += size;
    return temp;
}


/** Bitmap indicating free/used frames. */
static uint8_t *frame_bitmap;

/**
 * Helper functions for managing free physical frames, using a bitmap
 * data structure. Every bit indicates the free/used state of a corresponding
 * physical frame. Frame number one-one maps to bit index.
 */
#define BITMAP_OUTER_IDX(frame_num) ((frame_num) / 8)
#define BITMAP_INNER_IDX(frame_num) ((frame_num) % 8)

/** Set a frame as used. */
static inline void
frame_bitmap_set(uint32_t frame_num)
{
    size_t outer_idx = BITMAP_OUTER_IDX(frame_num);
    size_t inner_idx = BITMAP_INNER_IDX(frame_num);
    frame_bitmap[outer_idx] |= (1 << (7 - inner_idx));
}

/** Clear a frame as free. */
static inline void
frame_bitmap_clear(uint32_t frame_num)
{
    size_t outer_idx = BITMAP_OUTER_IDX(frame_num);
    size_t inner_idx = BITMAP_INNER_IDX(frame_num);
    frame_bitmap[outer_idx] &= ~(1 << (7 - inner_idx));
}

/** Returns true if a frame is in use, otherwise false. */
static inline bool
frame_bitmap_check(uint32_t frame_num)
{
    size_t outer_idx = BITMAP_OUTER_IDX(frame_num);
    size_t inner_idx = BITMAP_INNER_IDX(frame_num);
    return frame_bitmap[outer_idx] & (1 << (7 - inner_idx));
}

/**
 * Allocate a frame and mark as used. Returns the frame number of
 * the allocated frame, or panics if there is no free frame.
 */
static uint32_t
frame_bitmap_alloc(void)
{
    for (size_t i = 0; i < (NUM_FRAMES / 8); ++i) {
        if (frame_bitmap[i] == 0xFF)
            continue;
        for (size_t j = 0; j < 8; ++j) {
            if ((frame_bitmap[i] & (1 << (7 - j))) == 0) {
                /** Found a free frame. */
                uint32_t frame_num = i * 8 + j;
                frame_bitmap_set(frame_num);
                return frame_num;
            }
        }
    }

    return NUM_FRAMES;
}


/**
 * Walk a 2-level page table for a virtual address to locate its PTE.
 * If `alloc` is true, then when a level-2 table is needed but not
 * allocated yet, will perform the allocation.
 */
pte_t *
paging_walk_pgdir_at_boot(pde_t *pgdir, uint32_t vaddr, bool alloc)
{
    size_t pde_idx = ADDR_PDE_INDEX(vaddr);
    size_t pte_idx = ADDR_PTE_INDEX(vaddr);

    /** If already has the level-2 table, return the correct PTE. */
    if (pgdir[pde_idx].present != 0) {
        pte_t *pgtab = (pte_t *) ENTRY_FRAME_ADDR(pgdir[pde_idx]);
        return &pgtab[pte_idx];
    }

    /**
     * Else, the level-2 table is not allocated yet. Do the allocation if
     * the alloc argument is set, otherwise return a NULL.
     */
    if (!alloc)
        return NULL;

    pte_t *pgtab = (pte_t *) _kalloc_temp(sizeof(pte_t) * PTES_PER_PAGE, true);
    assert(pgtab != NULL);
    memset(pgtab, 0, sizeof(pte_t) * PTES_PER_PAGE);

    pgdir[pde_idx].present = 1;
    pgdir[pde_idx].writable = 0;
    pgdir[pde_idx].user = 1;    /** Just allow user access on all PDEs. */
    pgdir[pde_idx].frame = ADDR_PAGE_NUMBER((uint32_t) pgtab);

    return &pgtab[pte_idx];
}


/** kernel's identity-mapping page directory. */
pde_t *kernel_pgdir;    /** Allocated at paging init. */


/** Switch the current page directory to the given one. */
inline void
paging_switch_pgdir(pde_t *pgdir)
{
    assert(pgdir != NULL);
    asm volatile ( "movl %0, %%cr3" : : "r" (pgdir) );
}

/** Page fault (ISR # 14) handler. */
static void
page_fault_handler(interrupt_state_t *state)
{
    /** The CR2 register holds the faulty address. */
    uint32_t faulty_addr;
    asm ( "movl %%cr2, %0" : "=r" (faulty_addr) : );

    /**
     * Analyze the least significant 3 bits of error code to see what
     * triggered this page fault:
     *   - bit 0: page present -> 1, otherwise 0
     *   - bit 1: is a write operation -> 1, read -> 0
     *   - bit 2: is from user mode -> 1, kernel -> 0
     *
     * See https://wiki.osdev.org/Paging for more.
     */
    bool present = state->err_code & 0x1;
    bool write   = state->err_code & 0x2;
    bool user    = state->err_code & 0x4;

    /** Just prints an information message for now. */
    info("Caught page fault {\n"
         "  faulty addr = %p\n"
         "  present: %d\n"
         "  write:   %d\n"
         "  user:    %d\n"
         "}", faulty_addr, present, write, user);

    panic("page fault not handled!");
}


/** Initialize paging and switch to use paging. */
void
paging_init(void)
{
    /** Kernel heap starts above all ELF sections. */
    kheap_curr = ADDR_PAGE_ROUND_UP((uint32_t) elf_shstrtab_end);

    /**
     * The frame bitmap also needs space, so allocate space for it in
     * our kernel heap. Clear it to zeros.
     */
    frame_bitmap = (uint8_t *) _kalloc_temp(NUM_FRAMES / 8, false);
    memset(frame_bitmap, 0, NUM_FRAMES / 8);

    /**
     * Allocate the one-page space for the kernel's page directory in
     * the kernel heap. All pages of page directory/tables must be
     * page-aligned.
     */
    kernel_pgdir = (pde_t *) _kalloc_temp(sizeof(pde_t) * PDES_PER_PAGE, true);
    memset(kernel_pgdir, 0, sizeof(pde_t) * PDES_PER_PAGE);

    /**
     * Identity-map the kernel's virtual address space to the physical
     * memory. This means we need to map all the allowed kernel physical
     * frames (from 0 -> KMEM_MAX) as its identity virtual address in
     * the kernel page table, and reserve this entire physical memory region.
     *
     * Assumes that `frame_bitmap_alloc()` behaves sequentially.
     */
    uint32_t addr = 0;
    while (addr < KMEM_MAX) {
        uint32_t frame_num = frame_bitmap_alloc();
        assert(frame_num < NUM_FRAMES);
        pte_t *pte = paging_walk_pgdir_at_boot(kernel_pgdir, addr, true);
        assert(pte != NULL);

        /** Update the bits in this PTE. */
        pte->present = 1;
        pte->writable = 0;      /** Has no affect. */
        pte->user = 0;
        pte->frame = frame_num;

        addr += PAGE_SIZE;
    }

    /**
     * Also map the rest of physical memory into the scheduler page table,
     * so it could access any physical address directly.
     */
    while (addr < PHYS_MAX) {
        pte_t *pte = paging_walk_pgdir_at_boot(kernel_pgdir, addr, true);
        assert(pte != NULL);

        /** Update the bits in this PTE. */
        pte->present = 1;
        pte->writable = 0;      /** Has no affect. */
        pte->user = 0;
        pte->frame = ADDR_PAGE_NUMBER(addr);

        addr += PAGE_SIZE;
    }

    /**
     * Register the page fault handler. This acation must be done before
     * we do the acatual switch towards using paging.
     */
    isr_register(INT_NO_PAGE_FAULT, &page_fault_handler);
                 // 14, add macro definition in `src/interrupt/isr.h`

    /** Load the address of kernel page directory into CR3. */
    paging_switch_pgdir(kernel_pgdir);

    /**
     * Enable paging by setting the two proper bits of CR0:
     *   - PG bit (31): enable paging
     *   - PE bit (0): enable protected mode
     *   
     * We are not setting the WP bit, so the read/write bit of any PTE just
     * controls whether the page is user writable - in kernel priviledge any
     * page can be written.
     */
    uint32_t cr0;
    asm volatile ( "movl %%cr0, %0" : "=r" (cr0) : );
    cr0 |= 0x80000001;
    asm volatile ( "movl %0, %%cr0" : : "r" (cr0) );
}
When I use GDB to debug it, it will cause infinite loop in paging_init(): line "while(addr < PHYS_MAX) {"
When I can't use GDB for debugging, I use Qemu with the -no-shutdown -no-reboot -d int flag. It printed this:

Code: Select all

SMM: enter
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000f2c72 ESP=00006d98
EIP=000ebaef EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00cf9b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00cf9300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000080 CCD=00000001 CCO=LOGICB  
EFER=0000000000000000
SMM: after RSM
EAX=00000001 EBX=00000000 ECX=02000000 EDX=02000628
ESI=0000000b EDI=02000000 EBP=000f2c72 ESP=00006d98
EIP=000ebaef EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006dff
ESI=00006d3c EDI=07fbedc5 EBP=00006cfc ESP=00006cfc
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =0000 00000000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00006cfc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006dff
ESI=00006d3c EDI=07fbedc5 EBP=00006cfc ESP=00006cfc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=07fabb00
ESI=000ea600 EDI=07fbedc5 EBP=00006cfc ESP=00006cfc
EIP=000f7c15 EFL=00000016 [----AP-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000014 CCD=00006ce8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=07fabb00
ESI=000ea600 EDI=07fbedc5 EBP=00006cfc ESP=00006cfc
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =0000 00000000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 008f9300
CS =f000 000f0000 ffffffff 008f9b00
SS =0000 00000000 ffffffff 008f9300
DS =0000 00000000 ffffffff 008f9300
FS =0000 00000000 ffffffff 008f9300
GS =ca00 000ca000 ffffffff 008f9300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=000069e2 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7c15 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069ce CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=00007bfb EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069dc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=07f8cb00 EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=000f7c15 EFL=00000012 [----A--] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=000069c8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=07f8cb00 EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=00007c16 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=00007bfb EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=000069e2 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a22 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=000f7c15 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069ce CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000005
ESI=00000000 EDI=07fbedc5 EBP=000069e2 ESP=000069e2
EIP=00007c16 EFL=00000006 [-----P-] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000004 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=00007bfb EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=000069dc CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=000f7bfc ECX=00001234 EDX=00006aff
ESI=00006a1c EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=000f7bfc EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000000 CCO=EFLAGS  
EFER=0000000000000000
SMM: enter
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=07eccb00 EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=000f7c15 EFL=00000012 [----A--] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
CS =0008 00000000 ffffffff 00c09b00 DPL=0 CS32 [-RA]
SS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
DS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
FS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
GS =0010 00000000 ffffffff 00c09300 DPL=0 DS   [-WA]
LDT=0000 00000000 0000ffff 00008200 DPL=0 LDT
TR =0000 00000000 0000ffff 00008b00 DPL=0 TSS32-busy
GDT=     000f6280 00000037
IDT=     000f62be 00000000
CR0=00000011 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000010 CCD=000069c8 CCO=EFLAGS  
EFER=0000000000000000
SMM: after RSM
EAX=000000b5 EBX=00007c16 ECX=00005678 EDX=00000003
ESI=07eccb00 EDI=07fbedc5 EBP=000069dc ESP=000069dc
EIP=00007c16 EFL=00000002 [-------] CPL=0 II=0 A20=1 SMM=0 HLT=0
ES =db80 000db800 ffffffff 00809300
CS =f000 000f0000 ffffffff 00809b00
SS =0000 00000000 ffffffff 00809300
DS =0000 00000000 ffffffff 00809300
FS =0000 00000000 ffffffff 00809300
GS =ca00 000ca000 ffffffff 00809300
LDT=0000 00000000 0000ffff 00008200
TR =0000 00000000 0000ffff 00008b00
GDT=     00000000 00000000
IDT=     00000000 000003ff
CR0=00000010 CR2=00000000 CR3=00000000 CR4=00000000
DR0=00000000 DR1=00000000 DR2=00000000 DR3=00000000 
DR6=ffff0ff0 DR7=00000400
CCS=00000000 CCD=00000001 CCO=EFLAGS  
EFER=0000000000000000
Can anyone help me?
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Jumping into infinite loop when mapping virtual address

Post by Octocontrabass »

NeonLightions wrote:At that point it fell into an infinite loop.
I think we need more details about this infinite loop. Since you're using GDB, try stepping through instructions rather than lines of code to see what's going on.
NeonLightions wrote:When I can't use GDB for debugging, I use Qemu with the -no-shutdown -no-reboot -d int flag. It printed this:
Those are all SMM entry/exit events from when the BIOS was running. I don't see any interrupts or exceptions.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Octocontrabass wrote:
NeonLightions wrote:At that point it fell into an infinite loop.
I think we need more details about this infinite loop. Since you're using GDB, try stepping through instructions rather than lines of code to see what's going on.
Sure, what do you want to know more about? I can give you information about that
Octocontrabass wrote:
NeonLightions wrote:When I can't use GDB for debugging, I use Qemu with the -no-shutdown -no-reboot -d int flag. It printed this:
Those are all SMM entry/exit events from when the BIOS was running. I don't the see any interrupts or exceptions.
If I remove the line in paging_init():

Code: Select all

/**
     * Also map the rest of physical memory into the scheduler page table,
     * so it could access any physical address directly.
     */
    while (addr < PHYS_MAX) {
        pte_t *pte = paging_walk_pgdir_at_boot(kernel_pgdir, addr, true);
        assert(pte != NULL);

        /** Update the bits in this PTE. */
        pte->present = 1;
        pte->writable = 0;      /** Has no affect. */
        pte->user = 0;
        pte->frame = ADDR_PAGE_NUMBER(addr);

        addr += PAGE_SIZE;
    }
it will cause Double Fault. But if I keep this, it jump into infinite loop. I have stepped through every instruction, but everything is normal. I don't know what is happening
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Jumping into infinite loop when mapping virtual address

Post by Octocontrabass »

NeonLightions wrote:Sure, what do you want to know more about? I can give you information about that
Can you provide a disassembly of the code in the loop? I just can't see how that code could compile to an infinite loop.
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Octocontrabass wrote:
NeonLightions wrote:Sure, what do you want to know more about? I can give you information about that
Can you provide a disassembly of the code in the loop? I just can't see how that code could compile to an infinite loop.
Here is my disassembly code:

Code: Select all

00102f70 <paging_init>:
  102f70:	55                   	push   %ebp
  102f71:	89 e5                	mov    %esp,%ebp
  102f73:	57                   	push   %edi
  102f74:	56                   	push   %esi
  102f75:	53                   	push   %ebx
  102f76:	bb 07 00 00 00       	mov    $0x7,%ebx
  102f7b:	83 ec 1c             	sub    $0x1c,%esp
  102f7e:	a1 00 b0 10 00       	mov    0x10b000,%eax
  102f83:	05 ff 0f 00 00       	add    $0xfff,%eax
  102f88:	25 00 f0 ff ff       	and    $0xfffff000,%eax
  102f8d:	a3 b4 bc 10 00       	mov    %eax,0x10bcb4
  102f92:	31 c0                	xor    %eax,%eax
  102f94:	e8 07 fe ff ff       	call   102da0 <_kalloc_temp.constprop.0>
  102f99:	83 ec 04             	sub    $0x4,%esp
  102f9c:	68 00 10 00 00       	push   $0x1000
  102fa1:	6a 00                	push   $0x0
  102fa3:	50                   	push   %eax
  102fa4:	a3 b8 bc 10 00       	mov    %eax,0x10bcb8
  102fa9:	e8 b2 eb ff ff       	call   101b60 <memset>
  102fae:	b8 01 00 00 00       	mov    $0x1,%eax
  102fb3:	e8 e8 fd ff ff       	call   102da0 <_kalloc_temp.constprop.0>
  102fb8:	83 c4 0c             	add    $0xc,%esp
  102fbb:	68 00 10 00 00       	push   $0x1000
  102fc0:	6a 00                	push   $0x0
  102fc2:	50                   	push   %eax
  102fc3:	a3 b0 bc 10 00       	mov    %eax,0x10bcb0
  102fc8:	e8 93 eb ff ff       	call   101b60 <memset>
  102fcd:	c7 45 e4 00 00 00 00 	movl   $0x0,-0x1c(%ebp)
  102fd4:	83 c4 10             	add    $0x10,%esp
  102fd7:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  102fde:	66 90                	xchg   %ax,%ax
  102fe0:	8b 35 b8 bc 10 00    	mov    0x10bcb8,%esi
  102fe6:	31 ff                	xor    %edi,%edi
  102fe8:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  102fef:	90                   	nop
  102ff0:	0f b6 0c 3e          	movzbl (%esi,%edi,1),%ecx
  102ff4:	80 f9 ff             	cmp    $0xff,%cl
  102ff7:	74 18                	je     103011 <paging_init+0xa1>
  102ff9:	31 c0                	xor    %eax,%eax
  102ffb:	8d 74 26 00          	lea    0x0(%esi,%eiz,1),%esi
  102fff:	90                   	nop
  103000:	89 da                	mov    %ebx,%edx
  103002:	29 c2                	sub    %eax,%edx
  103004:	0f a3 d1             	bt     %edx,%ecx
  103007:	73 57                	jae    103060 <paging_init+0xf0>
  103009:	83 c0 01             	add    $0x1,%eax
  10300c:	83 f8 08             	cmp    $0x8,%eax
  10300f:	75 ef                	jne    103000 <paging_init+0x90>
  103011:	83 c7 01             	add    $0x1,%edi
  103014:	81 ff 00 10 00 00    	cmp    $0x1000,%edi
  10301a:	75 d4                	jne    102ff0 <paging_init+0x80>
  10301c:	fa                   	cli    
  10301d:	83 ec 0c             	sub    $0xc,%esp
  103020:	68 d1 00 00 00       	push   $0xd1
  103025:	68 2a 43 10 00       	push   $0x10432a
  10302a:	68 44 42 10 00       	push   $0x104244
  10302f:	68 40 43 10 00       	push   $0x104340
  103034:	6a 05                	push   $0x5
  103036:	e8 85 e3 ff ff       	call   1013c0 <cprintf>
  10303b:	83 c4 20             	add    $0x20,%esp
  10303e:	e8 ad d4 ff ff       	call   1004f0 <stack_trace>
  103043:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  10304a:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  103050:	f4                   	hlt    
  103051:	eb fd                	jmp    103050 <paging_init+0xe0>
  103053:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  10305a:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  103060:	8d 3c f8             	lea    (%eax,%edi,8),%edi
  103063:	b8 01 00 00 00       	mov    $0x1,%eax
  103068:	83 ec 04             	sub    $0x4,%esp
  10306b:	89 f9                	mov    %edi,%ecx
  10306d:	89 fa                	mov    %edi,%edx
  10306f:	f7 d1                	not    %ecx
  103071:	c1 ea 03             	shr    $0x3,%edx
  103074:	83 e1 07             	and    $0x7,%ecx
  103077:	d3 e0                	shl    %cl,%eax
  103079:	08 04 16             	or     %al,(%esi,%edx,1)
  10307c:	6a 01                	push   $0x1
  10307e:	ff 75 e4             	push   -0x1c(%ebp)
  103081:	ff 35 b0 bc 10 00    	push   0x10bcb0
  103087:	e8 a4 fd ff ff       	call   102e30 <paging_walk_pgdir_at_boot>
  10308c:	83 c4 10             	add    $0x10,%esp
  10308f:	85 c0                	test   %eax,%eax
  103091:	0f 84 ac 00 00 00    	je     103143 <paging_init+0x1d3>
  103097:	8b 10                	mov    (%eax),%edx
  103099:	c1 e7 0c             	shl    $0xc,%edi
  10309c:	81 45 e4 00 10 00 00 	addl   $0x1000,-0x1c(%ebp)
  1030a3:	83 cf 01             	or     $0x1,%edi
  1030a6:	81 e2 f8 0f 00 00    	and    $0xff8,%edx
  1030ac:	09 d7                	or     %edx,%edi
  1030ae:	89 38                	mov    %edi,(%eax)
  1030b0:	8b 45 e4             	mov    -0x1c(%ebp),%eax
  1030b3:	3d 00 00 80 00       	cmp    $0x800000,%eax
  1030b8:	0f 85 22 ff ff ff    	jne    102fe0 <paging_init+0x70>
  1030be:	8b 5d e4             	mov    -0x1c(%ebp),%ebx
  1030c1:	eb 32                	jmp    1030f5 <paging_init+0x185>
  1030c3:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  1030ca:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  1030d0:	8b 08                	mov    (%eax),%ecx
  1030d2:	89 da                	mov    %ebx,%edx
  1030d4:	81 c3 00 10 00 00    	add    $0x1000,%ebx
  1030da:	81 e2 00 f0 ff ff    	and    $0xfffff000,%edx
  1030e0:	83 ca 01             	or     $0x1,%edx
  1030e3:	81 e1 f8 0f 00 00    	and    $0xff8,%ecx
  1030e9:	09 ca                	or     %ecx,%edx
  1030eb:	89 10                	mov    %edx,(%eax)
  1030ed:	81 fb 00 00 00 08    	cmp    $0x8000000,%ebx
  1030f3:	74 7e                	je     103173 <paging_init+0x203>
  1030f5:	83 ec 04             	sub    $0x4,%esp
  1030f8:	6a 01                	push   $0x1
  1030fa:	53                   	push   %ebx
  1030fb:	ff 35 b0 bc 10 00    	push   0x10bcb0
  103101:	e8 2a fd ff ff       	call   102e30 <paging_walk_pgdir_at_boot>
  103106:	83 c4 10             	add    $0x10,%esp
  103109:	85 c0                	test   %eax,%eax
  10310b:	75 c3                	jne    1030d0 <paging_init+0x160>
  10310d:	fa                   	cli    
  10310e:	83 ec 0c             	sub    $0xc,%esp
  103111:	68 e4 00 00 00       	push   $0xe4
  103116:	68 2a 43 10 00       	push   $0x10432a
  10311b:	68 44 42 10 00       	push   $0x104244
  103120:	68 40 43 10 00       	push   $0x104340
  103125:	6a 05                	push   $0x5
  103127:	e8 94 e2 ff ff       	call   1013c0 <cprintf>
  10312c:	83 c4 20             	add    $0x20,%esp
  10312f:	e8 bc d3 ff ff       	call   1004f0 <stack_trace>
  103134:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  10313b:	8d 74 26 00          	lea    0x0(%esi,%eiz,1),%esi
  10313f:	90                   	nop
  103140:	f4                   	hlt    
  103141:	eb fd                	jmp    103140 <paging_init+0x1d0>
  103143:	fa                   	cli    
  103144:	83 ec 0c             	sub    $0xc,%esp
  103147:	68 d3 00 00 00       	push   $0xd3
  10314c:	68 2a 43 10 00       	push   $0x10432a
  103151:	68 44 42 10 00       	push   $0x104244
  103156:	68 40 43 10 00       	push   $0x104340
  10315b:	6a 05                	push   $0x5
  10315d:	e8 5e e2 ff ff       	call   1013c0 <cprintf>
  103162:	83 c4 20             	add    $0x20,%esp
  103165:	e8 86 d3 ff ff       	call   1004f0 <stack_trace>
  10316a:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  103170:	f4                   	hlt    
  103171:	eb fd                	jmp    103170 <paging_init+0x200>
  103173:	83 ec 08             	sub    $0x8,%esp
  103176:	68 40 2d 10 00       	push   $0x102d40
  10317b:	6a 0e                	push   $0xe
  10317d:	e8 2e f9 ff ff       	call   102ab0 <isr_register>
  103182:	a1 b0 bc 10 00       	mov    0x10bcb0,%eax
  103187:	83 c4 10             	add    $0x10,%esp
  10318a:	85 c0                	test   %eax,%eax
  10318c:	74 16                	je     1031a4 <paging_init+0x234>
  10318e:	0f 22 d8             	mov    %eax,%cr3
  103191:	0f 20 c0             	mov    %cr0,%eax
  103194:	0d 01 00 00 80       	or     $0x80000001,%eax
  103199:	0f 22 c0             	mov    %eax,%cr0
  10319c:	8d 65 f4             	lea    -0xc(%ebp),%esp
  10319f:	5b                   	pop    %ebx
  1031a0:	5e                   	pop    %esi
  1031a1:	5f                   	pop    %edi
  1031a2:	5d                   	pop    %ebp
  1031a3:	c3                   	ret    
  1031a4:	fa                   	cli    
  1031a5:	83 ec 0c             	sub    $0xc,%esp
  1031a8:	68 8b 00 00 00       	push   $0x8b
  1031ad:	68 2a 43 10 00       	push   $0x10432a
  1031b2:	68 50 42 10 00       	push   $0x104250
  1031b7:	68 40 43 10 00       	push   $0x104340
  1031bc:	6a 05                	push   $0x5
  1031be:	e8 fd e1 ff ff       	call   1013c0 <cprintf>
  1031c3:	83 c4 20             	add    $0x20,%esp
  1031c6:	e8 25 d3 ff ff       	call   1004f0 <stack_trace>
  1031cb:	f4                   	hlt    
  1031cc:	eb fd                	jmp    1031cb <paging_init+0x25b>
  1031ce:	47                   	inc    %edi
  1031cf:	43                   	inc    %ebx
  1031d0:	43                   	inc    %ebx
  1031d1:	3a 20                	cmp    (%eax),%ah
  1031d3:	28 47 4e             	sub    %al,0x4e(%edi)
  1031d6:	55                   	push   %ebp
  1031d7:	29 20                	sub    %esp,(%eax)
  1031d9:	31 31                	xor    %esi,(%ecx)
  1031db:	2e 31 2e             	xor    %ebp,%cs:(%esi)
  1031de:	30 00                	xor    %al,(%eax)
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Jumping into infinite loop when mapping virtual address

Post by Octocontrabass »

Which part of that is the loop it gets stuck in?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Octocontrabass wrote:Which part of that is the loop it gets stuck in?
Here is the part:

Code: Select all

  1031ad:	81 fb 00 00 00 08    	cmp    $0x8000000,%ebx
  1031b3:	74 7e                	je     103233 <paging_init+0x203>
  1031b5:	83 ec 04             	sub    $0x4,%esp
  1031b8:	6a 01                	push   $0x1
  1031ba:	53                   	push   %ebx
  1031bb:	ff 35 b0 bc 10 00    	push   0x10bcb0
  1031c1:	e8 2a fd ff ff       	call   102ef0 <paging_walk_pgdir_at_boot>
  1031c6:	83 c4 10             	add    $0x10,%esp
  1031c9:	85 c0                	test   %eax,%eax
  1031cb:	75 c3                	jne    103190 <paging_init+0x160>
  1031cd:	fa                   	cli    
  1031ce:	83 ec 0c             	sub    $0xc,%esp
  1031d1:	68 e4 00 00 00       	push   $0xe4
  1031d6:	68 45 43 10 00       	push   $0x104345
  1031db:	68 50 42 10 00       	push   $0x104250
  1031e0:	68 5c 43 10 00       	push   $0x10435c
  1031e5:	6a 05                	push   $0x5
  1031e7:	e8 d4 e1 ff ff       	call   1013c0 <cprintf>
  1031ec:	83 c4 20             	add    $0x20,%esp
  1031ef:	e8 fc d2 ff ff       	call   1004f0 <stack_trace>
  1031f4:	8d b4 26 00 00 00 00 	lea    0x0(%esi,%eiz,1),%esi
  1031fb:	8d 74 26 00          	lea    0x0(%esi,%eiz,1),%esi
  1031ff:	90                   	nop
  103200:	f4                   	hlt    
  103201:	eb fd                	jmp    103200 <paging_init+0x1d0>
  103203:	fa                   	cli    
  103204:	83 ec 0c             	sub    $0xc,%esp
  103207:	68 d3 00 00 00       	push   $0xd3
  10320c:	68 45 43 10 00       	push   $0x104345
  103211:	68 50 42 10 00       	push   $0x104250
  103216:	68 5c 43 10 00       	push   $0x10435c
  10321b:	6a 05                	push   $0x5
  10321d:	e8 9e e1 ff ff       	call   1013c0 <cprintf>
  103222:	83 c4 20             	add    $0x20,%esp
  103225:	e8 c6 d2 ff ff       	call   1004f0 <stack_trace>
  10322a:	8d b6 00 00 00 00    	lea    0x0(%esi),%esi
  103230:	f4                   	hlt    
  103231:	eb fd                	jmp    103230 <paging_init+0x200>
  103233:	83 ec 08             	sub    $0x8,%esp
And here is the full source code after using i686-elf-objdump: https://pastebin.com/pm4qy60W
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Jumping into infinite loop when mapping virtual address

Post by Octocontrabass »

I see more than one place where it could get stuck. Where exactly does it get stuck?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Octocontrabass wrote:I see more than one place where it could get stuck. Where exactly does it get stuck?
It gets stuck at this part in paging_init():

Code: Select all


    /**
     * Also map the rest of physical memory into the scheduler page table,
     * so it could access any physical address directly.
     */
    while (addr < PHYS_MAX) {
        pte_t *pte = paging_walk_pgdir_at_boot(kernel_pgdir, addr, true);
        assert(pte != NULL);

        /** Update the bits in this PTE. */
        pte->present = 1;
        pte->writable = 0;      /** Has no affect. */
        pte->user = 0;
        pte->frame = ADDR_PAGE_NUMBER(addr);

        addr += PAGE_SIZE;
    }
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Jumping into infinite loop when mapping virtual address

Post by Octocontrabass »

No, I mean... what are the addresses of the instructions in the loop when it gets stuck? You've stepped through it in a debugger, right? So which of those instructions are the ones in the loop?
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

Octocontrabass wrote:No, I mean... what are the addresses of the instructions in the loop when it gets stuck? You've stepped through it in a debugger, right? So which of those instructions are the ones in the loop?
Err... There is no bug in that loop. Only one weird thing that it will get stuck at loop 2048th, I don't know why.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Jumping into infinite loop when mapping virtual address

Post by iansjack »

That doesn't answer the question.

Have you single-stepped through the code to see where it is looping (and, how do you know it is looping)? Once you determine that it should be fairly easy to determine the cause.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Jumping into infinite loop when mapping virtual address

Post by neon »

Hi,
Only one weird thing that it will get stuck at loop 2048th
Do you mean the 2048th iteration? How have you confirmed this, how have you confirmed it gets "stuck" and what, precisely does "it will get stuck" mean? If you are indeed looking at the 2048'th iteration then what, precisely happens going into the next iteration? I do find the 2048th iteration interesting given 2048*4096=0x800000 so would be interested to see what "addr" becomes.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

iansjack wrote:That doesn't answer the question.

Have you single-stepped through the code to see where it is looping (and, how do you know it is looping)? Once you determine that it should be fairly easy to determine the cause.
Yes, I have single-steped the code and it is very.... normal. It doesn't do anything cause crash or something, it's just... getting stop right there and and do nothing more.


And one more thing, when I run again, it stops at loop 1024th, not 2048th, like this picture:
Image
... sorry
NeonLightions
Member
Member
Posts: 102
Joined: Wed Oct 20, 2021 6:00 pm
Location: Paraguay

Re: Jumping into infinite loop when mapping virtual address

Post by NeonLightions »

iansjack wrote:That doesn't answer the question.

Have you single-stepped through the code to see where it is looping (and, how do you know it is looping)? Once you determine that it should be fairly easy to determine the cause.
Yes, I have single-steped the code and it is very.... normal. It doesn't do anything cause crash or something, it's just... getting stop right there and and do nothing more.


And one more thing, when I run again, it stops at loop 1024th, not 2048th, like this picture:
Image
... and the addr = 00C00000, it reminds me about Higher Half Kernel :D
Last edited by NeonLightions on Mon Nov 22, 2021 1:16 am, edited 1 time in total.
Post Reply