karose
Posts: 6
Joined: Thu Oct 23, 2014 5:32 am

MMU initialization problem

Tue Nov 18, 2014 5:17 am

I want to write a sample MMU initialization code to map physical address 0x20000000~0x21000000 to virtual address 0xa0000000~0xa100000, but can not work.


I folloed that example from here "http://stackoverflow.com/questions/3439 ... en-physica".

Code: Select all

#define U32 unsigned int

void enable_mmu2(void)
{
    static U32 __attribute__((aligned(16384))) page_table[NUM_PAGE_TABLE_ENTRIES];
    int i;
    U32 reg;

    for (i = 0; i < NUM_PAGE_TABLE_ENTRIES; i++)
   {
		/* map 0x20000000~0x210000000 to 0xa0000000~0xa1000000 */
		if (i >= (0xa0000000 >> 20) && i <= ( 0xa1000000 >> 20))
		{
			page_table[i] = (i-2048) << 20 | (3 << 10) | CACHE_DISABLED;
			printk("page_table[%d]addr=%x, section=%x \n",i,&page_table[i],page_table[i]);
		}
		else
                       /*not mapping yet*/
			page_table[i]= 0;
   }
    printk("page_talble base=%x \n",page_table);
    /* Copy the page table address to cp15 */
    asm volatile("mcr p15, 0, %0, c2, c0, 0"
            : : "r" (page_table) : "memory");
    /* Set the access control to all-supervisor */
    asm volatile("mcr p15, 0, %0, c3, c0, 0" : : "r" (~0));

    /* Enable the MMU */
    asm("mrc p15, 0, %0, c1, c0, 0" : "=r" (reg) : : "cc");
    reg|=0x1;
    asm volatile("mcr p15, 0, %0, c1, c0, 0" : : "r" (reg) : "cc");
}
The printk debug message shows page_table[2562]addr=0x16808, section=0x20200412 , but my test_mmu print can not work.

Code: Select all

void test_mmu(void){
	const char *p="test_mmu\n";
	while(*p){
		*(volatile unsigned int *)0xa0201000=*p++;
	};
}
Thanks

karose
Posts: 6
Joined: Thu Oct 23, 2014 5:32 am

Re: MMU initialization problem

Tue Nov 18, 2014 8:34 am

I just found that if I change below part then it can work.

Code: Select all

/*not mapping yet*/
            from "page_table[i]=0" to "page_table[i]= i<<20| 0x0410|2" 
Can't I setup section 0xa0000000~0xa1000000 only and let all other sections translation fault?

dwelch67
Posts: 1006
Joined: Sat May 26, 2012 5:32 pm

Re: MMU initialization problem

Tue Nov 18, 2014 7:47 pm

Okay will I just added a bare metal mmu example using sections, it is fairly simple. github.com/dwelch67/raspberrypi mmu directory.

But you mention printk() so I assume you are running linux? This is a bare metal forum...

dwelch67
Posts: 1006
Joined: Sat May 26, 2012 5:32 pm

Re: MMU initialization problem

Tue Nov 18, 2014 7:50 pm

And yes the arm mmu in this chip will let you setup a 16MB space at 0xa0000000~0xa1000000. but even if you do a 16MB section you still have to put in all 16 entries. If you think about the picture, it takes the top 12 bits whatever they are and uses that to look at the first level descriptor, 0xa00... to 0xa10... is 16 different combinations so you need 16 entries. Not sure if that is your problem but the first time I tried to use a 16MB section I got burned with that....

David

karose
Posts: 6
Joined: Thu Oct 23, 2014 5:32 am

Re: MMU initialization problem

Wed Nov 19, 2014 2:15 am

dwelch67 wrote:Okay will I just added a bare metal mmu example using sections, it is fairly simple. github.com/dwelch67/raspberrypi mmu directory.

But you mention printk() so I assume you are running linux? This is a bare metal forum...
Thanks David, you always answer my question kindly , I just found that you've added a mmu example two days ago. I tried your extest example earlier but somehow it doesn't show any message in my Qemu serial console but can run on Raspberry Pi board correctly.

I'm study bare metal OS development, your examples are really nice.
I'm not running linux, printk is I added to debug. here is the MMU test full source code:
https://github.com/tzuCarlos/RaspberryP ... ter/labMMU

Hsu

Return to “Bare metal, Assembly language”