Thread: Page fault despite doing as tutorial says?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18

    Page fault despite doing as tutorial says?

    Hi guys,

    I've been following a tutorial ( see below for link ) about making my own os. I got to the Multitasking section and compiling and linking goes fine throughout it all.

    But when it comes to firing up bochs, it spits out a page fault error. I've never had to deal with these before and I haven't got a clue how to fix it :L

    here's what I get from my compiler after I add the -Wall flag to my makefile:

    Code:
    nasm -felf asem/boot.s -o obj/boot.o
    nasm -felf asem/interrupt.s -o obj/interrupt.o
    nasm -felf asem/gdt.s -o obj/gdt.o
    nasm -felf asem/process.s -o obj/process.o
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/main.c -o obj/main.o
    source/main.c:19:5: warning: first argument of ‘main’ should be ‘int’
    source/main.c:19:5: warning: second argument of ‘main’ should be ‘char **’
    source/main.c: In function ‘main’:
    source/main.c:81:4: warning: pointer targets in passing argument 4 of ‘read_fs’ differ in signedness
    headers/fs.h:58:8: note: expected ‘u8int *’ but argument is of type ‘char *’
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/monitor.c -o obj/monitor.o
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/common.c -o obj/common.o
    source/common.c: In function ‘strcat’:
    source/common.c:80:22: warning: operation on ‘dest’ may be undefined
    source/common.c: In function ‘panic’:
    source/common.c:104:5: warning: implicit declaration of function ‘monitor_write’
    source/common.c:109:5: warning: implicit declaration of function ‘monitor_write_dec’
    source/common.c: In function ‘strcpy’:
    source/common.c:72:1: warning: control reaches end of non-void function
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/descriptor_tables.c -o obj/descriptor_tables.o
    source/descriptor_tables.c: In function ‘init_descriptor_tables’:
    source/descriptor_tables.c:43:5: warning: implicit declaration of function ‘memset’
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/isr.c -o obj/isr.o
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/timer.c -o obj/timer.o
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/kheap.c -o obj/kheap.o
    source/kheap.c: In function ‘expand’:
    source/kheap.c:76:5: warning: suggest parentheses around comparison in operand of ‘&’
    source/kheap.c:91:9: warning: implicit declaration of function ‘alloc_frame’
    source/kheap.c: In function ‘contract’:
    source/kheap.c:118:9: warning: implicit declaration of function ‘free_frame’
    source/kheap.c: In function ‘find_smallest_hole’:
    source/kheap.c:139:13: warning: suggest parentheses around comparison in operand of ‘&’
    source/kheap.c: In function ‘create_heap’:
    source/kheap.c:177:5: warning: suggest parentheses around comparison in operand of ‘&’
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/paging.c -o obj/paging.o
    source/paging.c: In function ‘initialise_paging’:
    source/paging.c:118:5: warning: implicit declaration of function ‘memset’
    source/paging.c:121:12: warning: unused variable ‘phys’
    source/paging.c: In function ‘page_fault’:
    source/paging.c:220:5: warning: implicit declaration of function ‘monitor_write’
    source/paging.c:226:5: warning: implicit declaration of function ‘monitor_write_hex’
    source/paging.c:217:9: warning: unused variable ‘id’
    source/paging.c: In function ‘clone_table’:
    source/paging.c:257:2: warning: implicit declaration of function ‘copy_page_physical’
    source/paging.c: At top level:
    source/paging.c:44:15: warning: ‘test_frame’ defined but not used
    source/paging.c: In function ‘first_frame’:
    source/paging.c:71:1: warning: control reaches end of non-void function
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/ordered_array.c -o obj/ordered_array.o
    source/ordered_array.c: In function ‘create_ordered_array’:
    source/ordered_array.c:15:5: warning: implicit declaration of function ‘kmalloc’
    source/ordered_array.c:16:5: warning: implicit declaration of function ‘memset’
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/fs.c -o obj/fs.o
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/initrd.c -o obj/initrd.o
    source/initrd.c: In function ‘initrd_read’:
    source/initrd.c:22:5: warning: implicit declaration of function ‘memcpy’
    source/initrd.c: In function ‘initrd_readdir’:
    source/initrd.c:30:7: warning: implicit declaration of function ‘strcpy’
    source/initrd.c:39:5: warning: implicit declaration of function ‘strlen’
    source/initrd.c: In function ‘initrd_finddir’:
    source/initrd.c:47:9: warning: implicit declaration of function ‘strcmp’
    source/initrd.c: In function ‘initialise_initrd’:
    source/initrd.c:64:5: warning: implicit declaration of function ‘kmalloc’
    gcc -c -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders -Wall source/task.c -o obj/task.o
    source/task.c: In function ‘initialise_tasking’:
    source/task.c:34:5: warning: implicit declaration of function ‘kmalloc’
    source/task.c:40:5: warning: implicit declaration of function ‘kmalloc_a’
    source/task.c: In function ‘move_stack’:
    source/task.c:75:3: warning: implicit declaration of function ‘memcpy’
    source/task.c: In function ‘switch_task’:
    source/task.c:142:5: warning: implicit declaration of function ‘set_kernel_stack’
    ld -Tlink.ld -melf_i386 --oformat=elf32-i386 -o kern/kernel obj/boot.o obj/interrupt.o obj/gdt.o obj/process.o obj/main.o obj/monitor.o obj/common.o obj/descriptor_tables.o obj/isr.o obj/timer.o obj/kheap.o obj/paging.o obj/ordered_array.o obj/fs.o obj/initrd.o obj/task.o
    NOTE: I did find out that the first two arguments in the function main ( int and char** ) are meant to be like that.

    here's my main.c file:

    Code:
    // main.c -- Defines the C-code kernel entry point, calls initialisation routines.
    //           Made for JamesM's tutorials <www.jamesmolloy.co.uk>
    
    
    char kern_ver[] = "0.01";
    
    
    #include "monitor.h"
    #include "descriptor_tables.h"
    #include "timer.h"
    #include "paging.h"
    #include "multiboot.h"
    #include "fs.h"
    #include "initrd.h"
    #include "task.h"
    
    
    extern u32int placement_address;
    u32int initial_esp;
    
    
    
    
    int main ( struct multiboot *mboot_ptr, u32int initial_stack )
    {
        initial_esp = initial_stack;
        
        // Initialise all the ISRs and segmentation
        init_descriptor_tables();
        
        // Initialise the screen ( by clearing it )
        monitor_clear();
        
        // Initialise the PIT to 100Hz
        asm volatile ( "sti" );
        init_timer ( 50 );
        
        // Find the location of our initial ramdisk.
        ASSERT ( mboot_ptr -> mods_count > 0 );
        u32int initrd_location = * ( ( u32int * ) mboot_ptr -> mods_addr );
        u32int initrd_end = * ( u32int * ) ( mboot_ptr -> mods_addr + 4 );
        // Don't trample our module with placement accesses, please!
        placement_address = initrd_end;
        
        // Initialise paging
        initialise_paging();
        
        // Initialise the initial ramdisk, and set it as the filesystem root.
        fs_root = initialise_initrd ( initrd_location );
        
        // Write to screen.
        monitor_write ( "urOS Version " );
        monitor_write ( kern_ver );
        monitor_write ( "\n" );
        
        // Create a new process in a new address space which is a clone of this.
        int ret = fork();
        
        monitor_write ( "\n" );
        monitor_write ( "fork() returned " );
        monitor_write_hex ( ret );
        monitor_write ( ", and getpid() returned " );
        monitor_write_hex ( getpid() );
        monitor_write ( "\n============================================================================\n" );
        
        
        // The next section of code is not reentrant so make sure we aren't interrupted during.
        asm volatile ( "cli" );
        // List contents of /
        int i = 0;
        struct dirent *node = 0;
        while ( ( node = readdir_fs ( fs_root, i ) ) != 0 )
        {
            monitor_write ( "Found file " );
            monitor_write ( node -> name );
            fs_node_t *fsnode = finddir_fs ( fs_root, node -> name );
            
            if ( ( fsnode -> flags & 0x7 ) == FS_DIRECTORY )
            {
                monitor_write ( "\n\t( directory )\n" );
            }
            else
            {
                monitor_write ( "\n\t contents: \"" );
                char buf[256];
                u32int sz = read_fs ( fsnode, 0, 256, buf );
                int j;
                for ( j = 0; j < sz; j++ )
                    monitor_put ( buf[j] );
                
                monitor_write ( "\"\n" );
            }
            i++;
        }
        monitor_write ( "\n" );
        
        asm volatile ( "sti" );
        
        return 0;
    }
    Any idea's in what could be going wrong and making a page fault?

    If you need any other file then just say and I will update the OP.

  2. #2
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Oh forgot here's the tutorial link:
    jamesmolloy.co.uk

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Just because a tutorial exists that uses C, doesn't mean the person who wrote it wrote in standards-compliant C that would work everywhere. You've just hit an instance of it.

    The main warning is perfectly correct - for standard C. You most probably don't want it here though, because you're bootstrapping code by a method other than the usual program entry. Why it pops up, I can't easily see but either you're compiling some header or linking some library that has defined a main() prototype different to yours.

    The pointer-signedness? Much more of a problem caused by people not writing compliant code or casting where they KNOW that could be a problem. You'll have to hunt down the fixes for that yourself.

    The implicit declarations - caused by you not having prototypes for those functions, probably because they are in the assembler. That may be a problem, especially if you assembler assumes a certain pointer / integer size and you haven't prototypes them properly.

    But all those warnings suggest that the guy who wrote the code, although clearly knowledgeable, didn't care about people with anything that was a different system to his (64-bit, or different compiler, or even just ignored the warnings himself).

    Did you follow the tutorials individually or did you just lump everything together and compile the end result? At which point IN THE TUTORIAL does the code get the page fault that wasn't there before, or can you not even boot the simplest example on the "Genesis" page?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Quote Originally Posted by ledow View Post
    Just because a tutorial exists that uses C, doesn't mean the person who wrote it wrote in standards-compliant C that would work everywhere. You've just hit an instance of it.

    The main warning is perfectly correct - for standard C. You most probably don't want it here though, because you're bootstrapping code by a method other than the usual program entry. Why it pops up, I can't easily see but either you're compiling some header or linking some library that has defined a main() prototype different to yours.

    The pointer-signedness? Much more of a problem caused by people not writing compliant code or casting where they KNOW that could be a problem. You'll have to hunt down the fixes for that yourself.

    The implicit declarations - caused by you not having prototypes for those functions, probably because they are in the assembler. That may be a problem, especially if you assembler assumes a certain pointer / integer size and you haven't prototypes them properly.

    But all those warnings suggest that the guy who wrote the code, although clearly knowledgeable, didn't care about people with anything that was a different system to his (64-bit, or different compiler, or even just ignored the warnings himself).

    Did you follow the tutorials individually or did you just lump everything together and compile the end result? At which point IN THE TUTORIAL does the code get the page fault that wasn't there before, or can you not even boot the simplest example on the "Genesis" page?
    I have been writing as I read ( it's the best way for me to learn as I'm actually more of a C++ guy XD ).

    As for the page fault it's during the Multitasking page everything before that is fine at does as said.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Jamie_Edwards View Post
    Oh forgot here's the tutorial link:
    jamesmolloy.co.uk
    JamesM's kernel development tutorials

    Added what I guess is the tutorial link.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    JamesM's kernel development tutorials

    Added what I guess is the tutorial link.

    Tim S.
    Yeah, that's the one XD sorry :L

  7. #7
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Oh, by the way I'm using 64-bit linux with the same gcc compiler as he did/does, so that shouldn't be a problem.

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Jamie_Edwards View Post
    Oh, by the way I'm using 64-bit linux with the same gcc compiler as he did/does, so that shouldn't be a problem.
    Why do you think he used 64bit instead of 32bit? What I read implied 32bit.

    Why do you think you are using the same version of GCC as he did?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Quote Originally Posted by stahta01 View Post
    Why do you think he used 64bit instead of 32bit? What I read implied 32bit.

    Why do you think you are using the same version of GCC as he did?

    Tim S.
    I guess you have a point :L , still as far as I know it shouldn't matter if your on a 64 or 32 bit system as long as you have the right way of compiling right ( i.e the right compiler flags ) and the version of gcc should matter too much, as long as it isn't older than that of the original writer?

    Mind you, it doesn't solve the problems with the page fault, I haven't got a clue where to find the fix for the signedness issue.

    NB - sorry if the above sounds cocky, I'm new to C, I just want to get user-mode running so I can start programming in C++ :L

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by Jamie_Edwards View Post
    I guess you have a point :L , still as far as I know it shouldn't matter if your on a 64 or 32 bit system as long as you have the right way of compiling right ( i.e the right compiler flags ) and the version of gcc should matter too much, as long as it isn't older than that of the original writer?

    Mind you, it doesn't solve the problems with the page fault, I haven't got a clue where to find the fix for the signedness issue.

    NB - sorry if the above sounds cocky, I'm new to C, I just want to get user-mode running so I can start programming in C++ :L
    I have no idea if it makes a difference; but, it might.

    GCC Compiler options related to sign/unsigned chars (only the ones I know about could be many more).

    -fsigned-char
    -funsigned-char This is the one you might need to use.
    Last edited by stahta01; 04-20-2012 at 04:31 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  11. #11
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    No good, I tried both and I'm still having the page fault, any other ideas? :L

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I would start by getting rid of all these warnings; because they can all cause issues. After that you need to look at the code for problems and fix them.

    Code:
    warning: implicit declaration of function
    I would also get rid of the char sign warnings if possible; but the warning above is a sign of a worse problem.

    In writing C/C++ programs you need to get rid of all the warning or document why they are not necessary to be fixed to write good code.

    NOTE: Turning off the warnings IS NOT a valid fix.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    It would mean having to include standard header files into the mix, which, as you may know, won't work as I'm trying to make an OS :L

  14. #14
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    How do I say this... NO!.

    C doesn't work that way. Actually, no languages works that way; programming isn't some magic interdynamic of prophecy.

    It has nothing to do with what you are developing. If the compiler doesn't see a prototype (declaration) before a function is used the compiler will assume that almost anything is valid so can't tell you what or where you may be messing up nearly as well as it might.

    If you are using a function, and the compiler clearly says that you are, the definition of the function must be available to the linker by object or by reference or the linker will refuse to do the job of linking.

    That's a fancy way of saying "Who cares what you are developing `memcpy' is used so `memcpy' must be available.". The declaration just makes the compiler happy so that it doesn't complain to you. Without the definition, which something must provide, the linker will not just complain but not do the only job a linker does.

    So, yea, when you say "I'm trying to make an OS." and the compiler says "You are using functions from the standard C library." the compiler wins. You don't have to include headers provided by your compiler's vendor. You almost certainly can't use the libraries provided by your compiler's vendor. However, you've used the function, the compiler said so, so you have to provide the function in some fashion.

    Soma

  15. #15
    Registered User
    Join Date
    Apr 2012
    Location
    Cwmbran, South Wales
    Posts
    18
    Quote Originally Posted by phantomotap View Post
    O_o

    How do I say this... NO!.

    C doesn't work that way. Actually, no languages works that way; programming isn't some magic interdynamic of prophecy.

    It has nothing to do with what you are developing. If the compiler doesn't see a prototype (declaration) before a function is used the compiler will assume that almost anything is valid so can't tell you what or where you may be messing up nearly as well as it might.

    If you are using a function, and the compiler clearly says that you are, the definition of the function must be available to the linker by object or by reference or the linker will refuse to do the job of linking.

    That's a fancy way of saying "Who cares what you are developing `memcpy' is used so `memcpy' must be available.". The declaration just makes the compiler happy so that it doesn't complain to you. Without the definition, which something must provide, the linker will not just complain but not do the only job a linker does.

    So, yea, when you say "I'm trying to make an OS." and the compiler says "You are using functions from the standard C library." the compiler wins. You don't have to include headers provided by your compiler's vendor. You almost certainly can't use the libraries provided by your compiler's vendor. However, you've used the function, the compiler said so, so you have to provide the function in some fashion.

    Soma
    Thanks XD. At least I now know how to get rid of the implicit declarations, could this solve the page fault too?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Re: Explorer Page Fault error.
    By Adock in forum Tech Board
    Replies: 2
    Last Post: 07-25-2009, 02:39 AM
  2. Explorer Error Page fault
    By Adock in forum Tech Board
    Replies: 12
    Last Post: 07-24-2009, 04:24 PM
  3. monitor page fault
    By George2 in forum Tech Board
    Replies: 0
    Last Post: 01-15-2008, 04:17 AM
  4. page fault
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:27 AM
  5. Page Fault in Visual C++
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 05-03-2002, 12:30 AM

Tags for this Thread