Page 1 of 1

Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 8:02 am
by JamieEdwards
Hi guys,

First post on these forums!

I'm following JamesM's kerndev tutorial, and I managed to get to the Paging part. Only problem is. When it comes to compiling all the source files into their obj files, and the kernel file my compiler spits out the following error:

paging.o: In function `page_fault':
paging.c:(.text+0x4d0): undefined reference to `PANIC'
make: *** [link] Error 1

Am I having to make another file that does define PANIC, or have I done something wrong? :L Here's the paging.c and paging.h code:

Paging.c

Code: Select all

// paging.c -- Defines the interface for and structures relating to paging.
//             Written for JamesM's kernel development tutorials.

#include "paging.h"
#include "kheap.h"

// The kernel's page directory
page_directory_t *kernel_directory=0;

// The current page directory;
page_directory_t *current_directory=0;

// A bitset of frames - used or free.
u32int *frames;
u32int nframes;

// Defined in kheap.c
extern u32int placement_address;

// Macros used in the bitset algorithms.
#define INDEX_FROM_BIT(a) (a/(8*4))
#define OFFSET_FROM_BIT(a) (a%(8*4))

// Static function to set a bit in the frames bitset
static void set_frame(u32int frame_addr)
{
    u32int frame = frame_addr/0x1000;
    u32int idx = INDEX_FROM_BIT(frame);
    u32int off = OFFSET_FROM_BIT(frame);
    frames[idx] |= (0x1 << off);
}

// Static function to clear a bit in the frames bitset
static void clear_frame(u32int frame_addr)
{
    u32int frame = frame_addr/0x1000;
    u32int idx = INDEX_FROM_BIT(frame);
    u32int off = OFFSET_FROM_BIT(frame);
    frames[idx] &= ~(0x1 << off);
}

// Static function to test if a bit is set.
static u32int test_frame(u32int frame_addr)
{
    u32int frame = frame_addr/0x1000;
    u32int idx = INDEX_FROM_BIT(frame);
    u32int off = OFFSET_FROM_BIT(frame);
    return (frames[idx] & (0x1 << off));
}

// Static function to find the first free frame.
static u32int first_frame()
{
    u32int i, j;
    for (i = 0; i < INDEX_FROM_BIT(nframes); i++)
    {
        if (frames[i] != 0xFFFFFFFF) // nothing free, exit early.
        {
            // at least one bit is free here.
            for (j = 0; j < 32; j++)
            {
                u32int toTest = 0x1 << j;
                if ( !(frames[i]&toTest) )
                {
                    return i*4*8+j;
                }
            }
        }
    }
}

// Function to allocate a frame.
void alloc_frame(page_t *page, int is_kernel, int is_writeable)
{
    if (page->frame != 0)
    {
        return;
    }
    else
    {
        u32int idx = first_frame(); // idx is now the index of the first free frame.
        if (idx == (u32int)-1)
        {
            // PANIC is just a macro that prints a message to the screen then hits an infinate loop.
		PANIC ( "No free frames!" );
        }
        set_frame(idx*0x1000);
        page->present = 1;
        page->rw = (is_writeable)?1:0;
        page->user = (is_kernel)?0:1;
        page->frame = idx;
    }
}

// Function to deallocate a frame.
void free_frame(page_t *page)
{
    u32int frame;
    if (!(frame=page->frame))
    {
        return;
    }
    else
    {
        clear_frame(frame);
        page->frame = 0x0;
    }
}

void initialise_paging()
{
    // The size of physical memory. For the moment we 
    // assume it is 16MB big.
    u32int mem_end_page = 0x1000000;
    
    nframes = mem_end_page / 0x1000;
    frames = (u32int*)kmalloc(INDEX_FROM_BIT(nframes));
    memset(frames, 0, INDEX_FROM_BIT(nframes));
    
    // Let's make a page directory.
    kernel_directory = (page_directory_t*)kmalloc_a(sizeof(page_directory_t));
    current_directory = kernel_directory;

    // We need to identity map (phys addr = virt addr) from
    // 0x0 to the end of used memory, so we can access this
    // transparently, as if paging wasn't enabled.
    // NOTE that we use a while loop here deliberately.
    // inside the loop body we actually change placement_address
    // by calling kmalloc(). A while loop causes this to be
    // computed on-the-fly rather than once at the start.
    int i = 0;
    while (i < placement_address)
    {
        // Kernel code is readable but not writeable from userspace.
        alloc_frame( get_page(i, 1, kernel_directory), 0, 0);
        i += 0x1000;
    }
    // Before we enable paging, we must register our page fault handler.
    register_interrupt_handler(14, page_fault);

    // Now, enable paging!
    switch_page_directory(kernel_directory);
}

void switch_page_directory(page_directory_t *dir)
{
    current_directory = dir;
    asm volatile("mov %0, %%cr3":: "r"(&dir->tablesPhysical));
    u32int cr0;
    asm volatile("mov %%cr0, %0": "=r"(cr0));
    cr0 |= 0x80000000; // Enable paging!
    asm volatile("mov %0, %%cr0":: "r"(cr0));
}

page_t *get_page(u32int address, int make, page_directory_t *dir)
{
    // Turn the address into an index.
    address /= 0x1000;
    // Find the page table containing this address.
    u32int table_idx = address / 1024;
    if (dir->tables[table_idx]) // If this table is already assigned
    {
        return &dir->tables[table_idx]->pages[address%1024];
    }
    else if(make)
    {
        u32int tmp;
        dir->tables[table_idx] = (page_table_t*)kmalloc_ap(sizeof(page_table_t), &tmp);
        dir->tablesPhysical[table_idx] = tmp | 0x7; // PRESENT, RW, US.
        return &dir->tables[table_idx]->pages[address%1024];
    }
    else
    {
        return 0;
    }
}


void page_fault(registers_t regs)
{
    // A page fault has occurred.
    // The faulting address is stored in the CR2 register.
    u32int faulting_address;
    asm volatile("mov %%cr2, %0" : "=r" (faulting_address));
    
    // The error code gives us details of what happened.
    int present   = !(regs.err_code & 0x1); // Page not present
    int rw = regs.err_code & 0x2;           // Write operation?
    int us = regs.err_code & 0x4;           // Processor was in user-mode?
    int reserved = regs.err_code & 0x8;     // Overwritten CPU-reserved bits of page entry?
    int id = regs.err_code & 0x10;          // Caused by an instruction fetch?

    // Output an error message.
    monitor_write("Page fault! ( ");
    if (present) {monitor_write("present ");}
    if (rw) {monitor_write("read-only ");}
    if (us) {monitor_write("user-mode ");}
    if (reserved) {monitor_write("reserved ");}
    monitor_write(") at 0x");
    monitor_write_hex(faulting_address);
    monitor_write("\n");
    PANIC("Page fault");
}
Paging.h

Code: Select all

// paging.h -- Defines the interface for and structures relating to paging.
//             Written for JamesM's kernel development tutorials.

#ifndef PAGING_H
#define PAGING_H

#include "common.h"
#include "isr.h"

typedef struct page
{
    u32int present    : 1;   // Page present in memory
    u32int rw         : 1;   // Read-only if clear, readwrite if set
    u32int user       : 1;   // Supervisor level only if clear
    u32int accessed   : 1;   // Has the page been accessed since last refresh?
    u32int dirty      : 1;   // Has the page been written to since last refresh?
    u32int unused     : 7;   // Amalgamation of unused and reserved bits
    u32int frame      : 20;  // Frame address (shifted right 12 bits)
} page_t;

typedef struct page_table
{
    page_t pages[1024];
} page_table_t;

typedef struct page_directory
{
    /**
       Array of pointers to pagetables.
    **/
    page_table_t *tables[1024];
    /**
       Array of pointers to the pagetables above, but gives their *physical*
       location, for loading into the CR3 register.
    **/
    u32int tablesPhysical[1024];

    /**
       The physical address of tablesPhysical. This comes into play
       when we get our kernel heap allocated and the directory
       may be in a different location in virtual memory.
    **/
    u32int physicalAddr;
} page_directory_t;

/**
   Sets up the environment, page directories etc and
   enables paging.
**/
void initialise_paging();

/**
   Causes the specified page directory to be loaded into the
   CR3 register.
**/
void switch_page_directory(page_directory_t *new);

/**
   Retrieves a pointer to the page required.
   If make == 1, if the page-table in which this page should
   reside isn't created, create it!
**/
page_t *get_page(u32int address, int make, page_directory_t *dir);

/**
   Handler for page faults.
**/
void page_fault(registers_t regs);

#endif
Thanks for any help in advance.

Jamie

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 8:21 am
by JamieEdwards
berkus wrote:Your (copypasted) code (that you didn't even bother to read) says:

Code: Select all

       if (idx == (u32int)-1)
        {
            // PANIC is just a macro that prints a message to the screen then hits an infinate loop.
      PANIC ( "No free frames!" );
        }
Conclusions?
I had read it, but I thought it was the defining function. And if it IS the defining function, why isn't it being called?

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 8:37 am
by JamieEdwards
berkus wrote:What is the defining function?

Have you started in the middle of the tutorial? Have you read the text surrounding the code snippets?
I haven't started in the middle of the tutorial, and I have read the text surrounding the code, the only thing the tut says about PANIC is that it's a global function. But if it's a global function then it should be called. Only thing is... It isn't

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 8:42 am
by Combuster
paging.c:(.text+0x4d0): undefined reference to `PANIC'
If you comply to the forum rules, you will know what that means, no?

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 9:03 am
by JamieEdwards
Combuster wrote:
paging.c:(.text+0x4d0): undefined reference to `PANIC'
If you comply to the forum rules, you will know what that means, no?
I have never seen an error like this before #-o

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 9:04 am
by Synon
JamieEdwards wrote:
berkus wrote:What is the defining function?

Have you started in the middle of the tutorial? Have you read the text surrounding the code snippets?
I haven't started in the middle of the tutorial, and I have read the text surrounding the code, the only thing the tut says about PANIC is that it's a global function. But if it's a global function then it should be called. Only thing is... It isn't
You have to write it. When he says "it's a global function" he means it's a global function that you should write somewhere. The law of the conservation of code (also known as the assembly-machine code equivalency principle) states that code cannot be created or destroyed, only converted between forms (in this case, C, assembly code and object code).

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 9:11 am
by JamieEdwards
Synon wrote:
JamieEdwards wrote:
berkus wrote:What is the defining function?

Have you started in the middle of the tutorial? Have you read the text surrounding the code snippets?
I haven't started in the middle of the tutorial, and I have read the text surrounding the code, the only thing the tut says about PANIC is that it's a global function. But if it's a global function then it should be called. Only thing is... It isn't
You have to write it. When he says "it's a global function" he means it's a global function that you should write somewhere. The law of the conservation of code (also known as the assembly-machine code equivalency principle) states that code cannot be created or destroyed, only converted between forms (in this case, C, assembly code and object code).
#-o I'm such an idiot!!! Lack of patience sometimes gets the better of me, which I think isn't the best thing in the world when it comes to programming. But I digress...

Thank you XD

Re: Undefined Reference to 'PANIC'

Posted: Fri Apr 06, 2012 9:35 am
by bluemoon
JamieEdwards wrote:
Combuster wrote:
paging.c:(.text+0x4d0): undefined reference to `PANIC'
If you comply to the forum rules, you will know what that means, no?
I have never seen an error like this before #-o
Good luck with os development...