"Cannot access memory at address" during paging activation

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.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

"Cannot access memory at address" during paging activation

Post by costa90sm »

Hi,
during the paging activation, when I go to set a bit of CR4, I get the error "Cannot access memory...".

Code: Select all

.global attiva_paginazione
attiva_paginazione:
	pushl %eax
	pushl %ecx
	pushl %edx

	movl $0x0000080, %ecx	//efer msr
	rdmsr
	orl $0x00000100, %eax	//efer.lme
	wrmsr

	movl $0x00000020, %eax	//cr4.pae
	movl %eax, %cr4

	movl %cr0, %eax
	orl $0x80000000, %eax	//cr0.pg
	movl %eax, %cr0

	popl %edx
	popl %ecx
	popl %eax
	ret
Debugging with gdb, the routine is performed until "mov %eax, %cr4", and then gives the error "Cannot access memory at address 0x6ee0".
After, the function continue to running until the first pop istruction, then crashes.
The value 0x6ee0, analyzing registers, is in the esp.
what happens? How to solve?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

Obviously it's impossible to answer your question with seeing how you have set up your Page Table. But you must have done something wrong in doing so. Use a debugger to inspect your table and the cause should be obvious.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

My kernel is divided in two modules: "boot" and "sistema" (both files are ELF format).
Paging activation function must be in "boot" and after it there is a jump to the "sistema" module.
Can I activate paging with only the pml4 or I must map memory before?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

If you don't map memory then you're not going to be able to access it, are you? You need to create a full Page Table that at least covers any memory you are using before you activate paging.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

Of course. Sorry, mea culpa.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

I did it but it's the same thing: in the first pop instruction in paging activation function it crashes
The memory is mapped as following:

Code: Select all

.global finestra_FM
finestra_FM:
	pushl %ebp
	movl %esp, %ebp
	pushl %eax
	pushl %ecx
	pushl %edi

	movl 8(%ebp), %edi		// pulisco la memoria
	movl $0, %eax
	movl $0x2C00, %ecx
	rep
	stosl

	movl 8(%ebp), %edi
     leal    0x1000(%edi), %eax		// indirizzo del Page Directory Pointer Table
     orl     $BIT_P, %eax
     orl     $BIT_RW, %eax
     movl    %eax, (%edi)		// nella prima entrata del Page Map Level 4
					// (per ogni descrittore inserito vengono settati il bit P di presenza
					// ed il bit R/W di pagina scrivibile)

     leal    0x2000(%edi), %eax		// indirizzo del Page Directory
     orl     $BIT_P, %eax
     orl     $BIT_RW, %eax
     movl    %eax, 0x1000(%edi)		// nella prima entrata del Page Directory Pointer Table

     leal    0x3000(%edi), %eax		// indirizzo della prima Page Table
     orl     $BIT_P, %eax
     orl     $BIT_RW, %eax
     leal    0x2000(%edi), %edi		// indirizzo del Page Directory in EDI (serve per l'istruzione stosl)
     movl    $8, %ecx			// contatore (bastano 8 Page Tables per la finestra FM)
loop_fm_pt:
     stosl				
     addl    $4, %edi			// stosl incrementa di 4 ma i descrittori sono da 8
     addl    $0x1000, %eax
     loop    loop_fm_pt			// a questo punto sono state create le Page Tables necessarie


     movl    8(%ebp), %edi		// EDI è stato sporcato				
     leal    0x3000(%edi), %edi		// indirizzo della prima Page Table in EDI
     movl    $0, %eax
     orl     $BIT_P, %eax
     orl     $BIT_RW, %eax		// EAX ha per valore l'indirizzo 0 con i due bit settati
     movl    $4096, %ecx		// contatore (8 Page Tables hanno in totale 4096 entrate)
					// sfrutto il fatto che sono consecutive
loop_fm_p:
     stosl
     addl    $4, %edi			
     addl    $0x1000, %eax
     loop    loop_fm_p			// è stata creata la finestra FM
					// di dimensione MEM_TOT (16 MiB)
	popl %edi
	popl %ecx
	popl %eax
	leave
	ret
Are you sure that the problem is in the memory mapping and not in the stack?
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

Rather than expecting someone else to analyze your code and find your error, I think it would be more useful for you to debug it yourself. After all, you do have the failing code to hand. Set a breakpoint just before you enable paging and inspect the Page Table that you have created; ensure that every memory location that you wish to access (program, data, or stack) has a valid mapping. If all is well you can then single-step until you get a page fault; the error code and register cr2 will tell you what is going wrong at what memory location. It should then be trivial to locate the cause.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

The memory is mapped correctly, the kernel run if in protected mode.
If I load 0 in cr4 and set pag bit in cr0, there are not problems; the problem there is when I try to set pae bit in cr4 register.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: "Cannot access memory at address" during paging activati

Post by Combuster »

set pae bit in cr4 register.
Which can't possibly use the same page tables as non-pae paging. Each of the entries are 64 bits with PAE enabled.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

As a final resort, i think it may be time to read the documentation.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

I would not dare ask questions without having read the documentation, I've read it.
I've got two versions of the kernel, the 32-bit one and the 64-bit one (my work is the porting from the first version to the second).
I would just ask if the "cannot access memory..." error during set cr4 is given exclusively by the memory map or if there may be other causes. I think that the Page Table that i've done is correct.
I have little time and I would not lose too much with wrong errors.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

Yes, the fault must lie in your Page Table. My original advice still stands; inspect your Page Table and ensure that all the entries in it correspond with what you have read in the manual. The actual error message must be produced by the environment you are running under; without knowing what that is it's difficult to be more precise.
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

Because of the problems of QEMU to load 64-bit elf file, I divided my kernel in two parts (as suggested by the professor): "boot" module in 32 bit and "sistema" module in 64 bit.
So, in Makefile, I link "boot" and "sistema" as following:
ld -m elf_i386 -o boot ...
ld -m elf_x86_64 -o sistema ...

Is possible that I can not do PAE or 64-bit paging in a 32-bit elf file?
Should I activate 32-bit paging in "boot", jump to "sistema" and activate 64-bit paging there?
costa90sm
Posts: 13
Joined: Sat Jun 22, 2013 3:06 am

Re: "Cannot access memory at address" during paging activati

Post by costa90sm »

Sorry, only 64-bit paging, the PAE one is possible in 32-bit structure.
User avatar
iansjack
Member
Member
Posts: 4711
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: "Cannot access memory at address" during paging activati

Post by iansjack »

Have you inspected your Page Table yet or are you just assuming it's correct? Perhaps you'd like to post the entries relating to the memory region where the page fault is happening.
Post Reply