Paging problem & "assignment makes integer from pointer"

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
User avatar
wrozowski
Posts: 13
Joined: Mon Dec 30, 2013 6:41 am
Location: Poland
Contact:

Paging problem & "assignment makes integer from pointer"

Post 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
Last edited by wrozowski on Sat Aug 23, 2014 3:34 am, edited 1 time in total.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Paging problem & "assignment makes integer from pointer

Post 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

Code: Select all

void* p;
unsigned n;
n = p;
to

Code: Select all

void* p;
unsigned n;
n = (unsigned)p;
User avatar
wrozowski
Posts: 13
Joined: Mon Dec 30, 2013 6:41 am
Location: Poland
Contact:

Re: Paging problem & "assignment makes integer from pointer

Post by wrozowski »

Hello,

Thank you very much alexfru. It worked for me :D - 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)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Paging problem & "assignment makes integer from pointer

Post 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.
User avatar
wrozowski
Posts: 13
Joined: Mon Dec 30, 2013 6:41 am
Location: Poland
Contact:

Re: Paging problem & "assignment makes integer from pointer

Post 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
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Paging problem & "assignment makes integer from pointer

Post 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

Code: Select all

asm volatile
all the time. Fix it. The proper way to do it is to always use

Code: Select all

__asm__
(take a look into the C standard) and leaving out

Code: Select all

volatile
where possible.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: Paging problem & "assignment makes integer from pointer

Post by alexfru »

no92 wrote: The proper way to do it is to always use

Code: Select all

__asm__
(take a look into the C standard) and leaving out

Code: Select all

volatile
where possible.
Since when is __asm__ defined in the C standard?
no92
Member
Member
Posts: 307
Joined: Wed Oct 30, 2013 1:57 pm
Libera.chat IRC: no92
Location: Germany
Contact:

Re: Paging problem & "assignment makes integer from pointer

Post 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).
Post Reply