Page 1 of 1
Paging problem & "assignment makes integer from pointer"
Posted: Fri Aug 22, 2014 2:33 pm
by wrozowski
Hello,
I'm currently working on paging code in my OS. I've read "setting up paging" & James'm "Paging" tutorials.
When I'm using code form these tutorials I have following problems:
1. When I write data to CR3 I get "assignment makes integer from pointer without a cast" error. (I use -Wextra - Werror flags) How can I fix that?
[solved]
2. When I set CR0 my emulator crashes (It happened on qemu, vmware and virtualbox. I've tried to run my OS (*.iso file) on bochs but I got "invalid image file" error).
[solved] Why is this happenning?
And I've got few questions:
1.I saw that many people use end symbol definied in the linker. My kernel ends at 0x1078c4 and I've got one module loaded at 0x109000 ending at 0x109257. According to mbi at 0x108000 there is kernel elf section table. When I use code form james'm tutorial my "heap" starts after kernel end. Is page_directory colliding with my module and elf section. Is this causing these errors? Where should I put my page_directory?
2. For what reason people use mmap in their mm? Is this used for skipping some importand data in page table?
Here is code based on "setting paging tutorial" which I use:
Code: Select all
void init_paging()
{
unsigned int *page_directory = (unsigned int*) 0x9C000;
unsigned int *first_page_table = page_directory + 1024;
int i = 0;
for(i = 0; i < 1024; i++)
{
page_directory[i] = 0 | 2;
}
unsigned int address = 0;
unsigned int j;
for(j = 0; j < 1024; j++)
{
first_page_table[j] = address | 3;
address = address + 4096;
}
page_directory[0] = first_page_table;
page_directory[0] |= 3;
asm ("mov %0, %%cr3":: "b"(page_directory));
unsigned int cr0;
asm ("mov %%cr0, %0": "=b"(cr0));
cr0 |= 0x80000000;
asm ("mov %0, %%cr0":: "b"(cr0));
}
The best greetings for you from Poland
Wojciech Różowski
Re: Paging problem & "assignment makes integer from pointer
Posted: Fri Aug 22, 2014 10:03 pm
by alexfru
wrozowski wrote:1. When I write data to CR3 I get "assignment makes integer from pointer without a cast" error. (I use -Wextra - Werror flags) How can I fix that?
Change
to
Code: Select all
void* p;
unsigned n;
n = (unsigned)p;
Re: Paging problem & "assignment makes integer from pointer
Posted: Sat Aug 23, 2014 3:36 am
by wrozowski
Hello,
Thank you very much alexfru. It worked for me
- evrything is compiling without the errors and my virtual machine is not crashing when i set paging (write to CR0).
Best regards
Wojciech Różowski (wrozowski)
Re: Paging problem & "assignment makes integer from pointer
Posted: Sat Aug 23, 2014 3:55 am
by bluemoon
wrozowski wrote:Where should I put my page_directory?
The easiest way is put in on BSS.
wrozowski wrote:2. For what reason people use mmap in their mm? Is this used for skipping some important data in page table?
mmap is so convenient that you can let driver access specific region of physical memory, used in manipulating processes, or share data across process, and let people fly to Mars.
Re: Paging problem & "assignment makes integer from pointer
Posted: Mon Aug 25, 2014 11:19 am
by wrozowski
Hi
,
Thank you blumoon for your answer.
bluemoon wrote:The easiest way is put in on BSS.
How can I put data in the bss? In asm? Something like".section bss"?
Here is my paging code:
paging.c
Code: Select all
#include <loki/system.h>
uint32_t placement_address = (uint32_t)0x10A000;
uint32_t kernel_end = (uint32_t) 0x10A000;
void init_paging()
{
uint32_t memory_end = 0x400000; //64 MB
uint32_t nframes = memory_end/0x1000;
printklog("Memory ends at 0x%x, and we need 0x%x frames to page it.\n", (uint32_t) memory_end, (uint32_t) nframes);
printklog("We need to map memory to 0x%x address using 0x%x frames.\n", (uint32_t) placement_address, (uint32_t) kernel_end/0x1000);
page_directory_t *kernel_directory = (page_directory_t*)kmalloc_a(1024);
printklog("Allocated memory for first kernel directory, at 0x%x.\n", (uint32_t *) kernel_directory);
page_table_t *page_tables = (page_table_t *)kmalloc_a(1024 * 1024);
printklog("Allocated memory for page_tables, at 0x%x.\n", (uint32_t *) page_tables);
unsigned int i = 0;
for(i = 0; i < 1024; i++)
{
kernel_directory[i] = 0 | 2;
}
unsigned int address = 0;
unsigned int j;
unsigned int directory_index = 0;
uint32_t frames = kernel_end / 0x1000;
printklog("We will write 0x%x frames.\n", (uint32_t) frames);
for(j = 0; j < frames; j++)
{
page_tables[j] = address | 3;
address = address + 4096;
if ((frames % 1024) == 0)
{
directory_index++;
}
}
printklog("Written 0x%x pages in 0x%x page tables.\n", (unsigned int) j, (unsigned int) directory_index);
for(unsigned int g = 0; g <= directory_index; g++)
{
kernel_directory[g] = (page_table_t) *(&page_tables + (g*1024));
kernel_directory[g] |= 3;
}
printklog("Loded page tables to page directory\n");
printklog("Writing data to CR3\n");
asm volatile("mov %0, %%cr3":: "b"(kernel_directory));
unsigned int cr0;
asm volatile("mov %%cr0, %0": "=b"(cr0));
printklog("CR0 = 0x%x\n", (unsigned int) cr0);
cr0 |= 0x80000000;
printklog("Writing CR0 = 0x%x\n", (unsigned int) cr0);
asm volatile("mov %0, %%cr0":: "b"(cr0));
printklog("CR0 has been loaded. Paging has been enabled!\n");
init_msg("init paging");
}
uint32_t kmalloc_universal(uint32_t sz, int align, uint32_t *phys)
{
if (align == 1 && (placement_address & 0xFFFFF000))
{
// Align it.
placement_address &= 0xFFFFF000;
placement_address += 0x1000;
}
if (phys)
{
*phys = placement_address;
}
uint32_t tmp = placement_address;
placement_address += sz;
return tmp;
}
uint32_t kmalloc_a(uint32_t sz) //page aligned
{
return kmalloc_universal(sz, 1, 0);
}
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys)//return physical adress
{
return kmalloc_universal(sz,0, phys);
}
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys) //page aligned and return physical adress
{
return kmalloc_universal(sz, 1, phys);
}
uint32_t kmalloc(uint32_t sz) //malloc
{
return kmalloc_universal(sz, 0, 0);
}
paging.h
Code: Select all
#include <loki/system.h>
#ifndef paging_h
#define paging_h
typedef unsigned int page_table_t;
typedef unsigned int page_directory_t;
void init_paging();
uint32_t kmalloc_universal(uint32_t sz, int align, uint32_t *phys);
uint32_t kmalloc_a(uint32_t sz);
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys);
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys);
uint32_t kmalloc(uint32_t sz);
#define INDEX_FROM_BIT(a) (a/(8*4))
#define OFFSET_FROM_BIT(a) (a%(8*4))
uint32_t *frames;
uint32_t nframes;
#endif
The rest of the code is
here. What do you think about it? What would you improve?
Greetings
wrozowski
Re: Paging problem & "assignment makes integer from pointer
Posted: Thu Aug 28, 2014 7:51 am
by no92
wrozowski, this is not a code review forum. Also, if you don't know what the BSS section is and how to use it, please take another look at
Beginner Mistakes and
Required Knowledge, as well as
James Molloy's Tutorial - Known Bugs.
Anyways, when quickly reading through your tutorial, I saw you use
all the time. Fix it. The proper way to do it is to always use
(take a look into the C standard) and leaving out
where possible.
Re: Paging problem & "assignment makes integer from pointer
Posted: Thu Aug 28, 2014 8:02 am
by alexfru
no92 wrote:
The proper way to do it is to always use
(take a look into the C standard) and leaving out
where possible.
Since when is __asm__ defined in the C standard?
Re: Paging problem & "assignment makes integer from pointer
Posted: Thu Aug 28, 2014 8:26 am
by no92
The inline assembly keyword asm is mentioned in the ANSI-C standard, but not required, just optional.
After looking it up, it looks like only asm is inside the standard, but __asm__ isn't. So, you were, in fact, right.
Still, gcc supports both of these (not sure about clang, but last time I checked it worked for me).