Page fault

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.
Post Reply
Shvets04
Member
Member
Posts: 28
Joined: Wed Feb 13, 2019 3:07 pm

Page fault

Post by Shvets04 »

I have the function :

Code: Select all

static s32 find_smallest_hole(u32 size, u8 page_align, heap_t *heap)
{
    // Find the smallest hole that will fit.
    u32 iterator = 0;
    while (iterator < heap->index.size)
    {
        header_t *header = (header_t *)lookup_ordered_array(iterator, &heap->index);
        // If the user has requested the memory be page-aligned
        if (page_align > 0)
        {
            // Page-align the starting point of this header.
            u32 location = (u32)header;
            u32 offset = 0;
            if ((location+sizeof(header_t)) & 0xFFFFF000 != 0)
                offset = 0x1000 /* page size */  - (location+sizeof(header_t))%0x1000;
            s32 hole_size = (u32)header->size - offset;
            // Can we fit now?
            if (hole_size >= (u32)size)
                break;
        }
        else if (header->size >= size)
            break;
        iterator++;
    }
    // Why did the loop exit?
    if (iterator == heap->index.size)
        return -1; // We got to the end and didn't find anything.
    else
        return iterator;
}

And

Code: Select all

type_t lookup_ordered_array(u32 i, ordered_array_t *array)
{
   ASSERT(i < array->size);
   return array->array[i];
}

After returning lookup_ordered_array, %eax is assigned to 0xe94aaa55 at 0x00003a13 <+34> line(see assembler code)
Dump of assembler code for function lookup_ordered_array:

Code: Select all

0x000039f1 <+0>:	push   %ebp
   0x000039f2 <+1>:	mov    %esp,%ebp
=> 0x000039f4 <+3>:	mov    0xc(%ebp),%eax
   0x000039f7 <+6>:	mov    0x4(%eax),%eax
   0x000039fa <+9>:	cmp    0x8(%ebp),%eax
   0x000039fd <+12>:	ja     0x3a06 <lookup_ordered_array+21>
   0x000039ff <+14>:	mov    $0xffffffff,%eax
   0x00003a04 <+19>:	jmp    0x3a15 <lookup_ordered_array+36>
   0x00003a06 <+21>:	mov    0xc(%ebp),%eax
   0x00003a09 <+24>:	mov    (%eax),%eax
   0x00003a0b <+26>:	mov    0x8(%ebp),%edx
   0x00003a0e <+29>:	shl    $0x2,%edx
   0x00003a11 <+32>:	add    %edx,%eax
   0x00003a13 <+34>:	mov    (%eax),%eax
   0x00003a15 <+36>:	pop    %ebp
   0x00003a16 <+37>:	ret    
It cause a page fault in find_smallest_hole(), at 0x00002b9a <+121> line.
Dump of assembler code for function find_smallest_hole:

Code: Select all

   0x00002b21 <+0>:	push   %ebp
   0x00002b22 <+1>:	mov    %esp,%ebp
   0x00002b24 <+3>:	sub    $0x38,%esp
   0x00002b27 <+6>:	mov    0xc(%ebp),%eax
   0x00002b2a <+9>:	mov    %al,-0x2c(%ebp)
   0x00002b2d <+12>:	movl   $0x0,-0xc(%ebp)
   0x00002b34 <+19>:	jmp    0x2ba8 <find_smallest_hole+135>
   0x00002b36 <+21>:	mov    0x10(%ebp),%eax
   0x00002b39 <+24>:	mov    %eax,0x4(%esp)
   0x00002b3d <+28>:	mov    -0xc(%ebp),%eax
   0x00002b40 <+31>:	mov    %eax,(%esp)
   0x00002b43 <+34>:	call   0x39f1 <lookup_ordered_array>
   0x00002b48 <+39>:	mov    %eax,-0x14(%ebp)
=> 0x00002b4b <+42>:	cmpb   $0x0,-0x2c(%ebp)
   0x00002b4f <+46>:	je     0x2b97 <find_smallest_hole+118>
   0x00002b51 <+48>:	mov    -0x14(%ebp),%eax
   0x00002b54 <+51>:	mov    %eax,-0x18(%ebp)
   0x00002b57 <+54>:	movl   $0x0,-0x10(%ebp)
   0x00002b5e <+61>:	mov    -0x18(%ebp),%eax
   0x00002b61 <+64>:	and    $0x1,%eax
   0x00002b64 <+67>:	test   %eax,%eax
   0x00002b66 <+69>:	je     0x2b7f <find_smallest_hole+94>
   0x00002b68 <+71>:	mov    -0x18(%ebp),%eax
   0x00002b6b <+74>:	add    $0xc,%eax
   0x00002b6e <+77>:	and    $0xfff,%eax
   0x00002b73 <+82>:	mov    %eax,%edx
   0x00002b75 <+84>:	mov    $0x1000,%eax
   0x00002b7a <+89>:	sub    %edx,%eax
   0x00002b7c <+91>:	mov    %eax,-0x10(%ebp)
   0x00002b7f <+94>:	mov    -0x14(%ebp),%eax
   0x00002b82 <+97>:	mov    0x8(%eax),%eax
   0x00002b85 <+100>:	sub    -0x10(%ebp),%eax
   0x00002b88 <+103>:	mov    %eax,-0x1c(%ebp)
   0x00002b8b <+106>:	mov    -0x1c(%ebp),%eax
   0x00002b8e <+109>:	cmp    0x8(%ebp),%eax
   0x00002b91 <+112>:	jb     0x2b95 <find_smallest_hole+116>
   0x00002b93 <+114>:	jmp    0x2bb3 <find_smallest_hole+146>
   0x00002b95 <+116>:	jmp    0x2ba4 <find_smallest_hole+131>
   0x00002b97 <+118>:	mov    -0x14(%ebp),%eax
   0x00002b9a <+121>:	mov    0x8(%eax),%eax
   0x00002b9d <+124>:	cmp    0x8(%ebp),%eax
   0x00002ba0 <+127>:	jb     0x2ba4 <find_smallest_hole+131>
   0x00002ba2 <+129>:	jmp    0x2bb3 <find_smallest_hole+146>
   0x00002ba4 <+131>:	addl   $0x1,-0xc(%ebp)
   0x00002ba8 <+135>:	mov    0x10(%ebp),%eax
   0x00002bab <+138>:	mov    0x4(%eax),%eax
   0x00002bae <+141>:	cmp    -0xc(%ebp),%eax
   0x00002bb1 <+144>:	ja     0x2b36 <find_smallest_hole+21>
   0x00002bb3 <+146>:	mov    0x10(%ebp),%eax
   0x00002bb6 <+149>:	mov    0x4(%eax),%eax
   0x00002bb9 <+152>:	cmp    -0xc(%ebp),%eax
   0x00002bbc <+155>:	jne    0x2bc5 <find_smallest_hole+164>
   0x00002bbe <+157>:	mov    $0xffffffff,%eax
   0x00002bc3 <+162>:	jmp    0x2bc8 <find_smallest_hole+167>
   0x00002bc5 <+164>:	mov    -0xc(%ebp),%eax
   0x00002bc8 <+167>:	leave  
   0x00002bc9 <+168>:	ret  
What should I to do for fix it?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Page fault

Post by Octacone »

Shvets04 wrote:What should I to do for fix it?
Well you should not use tutorials for something as important as memory management. It's one of the most crucial parts of the OS, you should design it to fit your needs.
Also don't use James Molloy's tutorials, you may read them but don't use the code:
https://wiki.osdev.org/James_Molloy%27s ... Known_Bugs

Memory management can be hard to understand sometimes, but learning it by yourself will pay of in a long run. Otherwise you'll just be hunting someone else's bugs not knowing how it all works.
If you don't want to deal with it right now, you could just use LibAlloc. https://github.com/blanham/liballoc
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Shvets04
Member
Member
Posts: 28
Joined: Wed Feb 13, 2019 3:07 pm

Re: Page fault

Post by Shvets04 »

Octacone wrote:
Shvets04 wrote:What should I to do for fix it?
Well you should not use tutorials for something as important as memory management. It's one of the most crucial parts of the OS, you should design it to fit your needs.
Also don't use James Molloy's tutorials, you may read them but don't use the code:
https://wiki.osdev.org/James_Molloy%27s ... Known_Bugs

Memory management can be hard to understand sometimes, but learning it by yourself will pay of in a long run. Otherwise you'll just be hunting someone else's bugs not knowing how it all works.
If you don't want to deal with it right now, you could just use LibAlloc. https://github.com/blanham/liballoc
Ok. Than where should i learn mm for implementing it in my project myself?
User avatar
Octacone
Member
Member
Posts: 1138
Joined: Fri Aug 07, 2015 6:13 am

Re: Page fault

Post by Octacone »

Shvets04 wrote:
Octacone wrote:
Shvets04 wrote:What should I to do for fix it?
Well you should not use tutorials for something as important as memory management. It's one of the most crucial parts of the OS, you should design it to fit your needs.
Also don't use James Molloy's tutorials, you may read them but don't use the code:
https://wiki.osdev.org/James_Molloy%27s ... Known_Bugs

Memory management can be hard to understand sometimes, but learning it by yourself will pay of in a long run. Otherwise you'll just be hunting someone else's bugs not knowing how it all works.
If you don't want to deal with it right now, you could just use LibAlloc. https://github.com/blanham/liballoc
Ok. Than where should i learn mm for implementing it in my project myself?
Well, that is a broad question.
First you need to know that there are typically 3 types of memory you need to manager: physical, virtual and the heap (Malloc etc...).
Start by writing a simple bitmap based physical memory manager.
Then write a simple paging implementation, nothing fancy. I would suggest going for legacy 4 KiB pages, 32 bit, PAE (if needed) to keep it simple. For this you will need a copy of Intel's manual.
Then you can focus on so called Dynamic Memory or the heap, this is the memory your apps would use through Malloc, New... IMHO this "type" of memory management is the most complex, hard to get right one.
You could write a simple linked list implementation just to see how it all works. Note, this is where you have to think about different algorithms and the overall speed of your implementation. Also there are typically two allocators of this type, the one you use for your kernel and the one you expose to userspace. You can also use are third party allocator for this.

Have you ever taken a look at these pages?
https://wiki.osdev.org/Memory_management
https://wiki.osdev.org/Memory_Map_(x86)
https://wiki.osdev.org/Brendan%27s_Memo ... ment_Guide
https://wiki.osdev.org/Paging
https://wiki.osdev.org/Page_Frame_Allocation
https://software.intel.com/en-us/articles/intel-sdm

This is just a short overview. There is a lot of things to talk about, that I didn't mention.
OS: Basic OS
About: 32 Bit Monolithic Kernel Written in C++ and Assembly, Custom FAT 32 Bootloader
Post Reply