【问题标题】:what is the right way to update MMU translation table更新 MMU 转换表的正确方法是什么
【发布时间】:2013-05-05 09:07:38
【问题描述】:

我在我的 s3c2440 板上启用了 MMU(3G - 4G 内存 :: 故障属性),当我没有读/写 3G - 4G 内存时一切都很好。所以为了测试页面故障向量,我写信给3G地址的0xFF,正如我所料,我从FSR得到了正确的值,所以我在_do_page_fault()中做了这个,步骤是这样的:

.....                 // set new page to translation table 
.....
invlidate_icache ();  // clear icache 
clr_dcache ();        // wb is used ,clear dcache 
invalidate_ttb ();    // invalidate translation table 

然后ISR_dataabort返回,我读取3G地址得到我之前写的0xFF。 不幸的是,我再次中止了数据。(我确定我设置的转换表值没问题)

那么更新 MMU 转换表的正确方法是什么。感谢您的帮助!谢谢

这是我使用的主要代码(仅用于一些测试), (我对 ARM ARCH 有点陌生,所以这段代码可能很紧急)

/* MMU TTB 0 BASE ATTR */
#define TTB0_FAULT          (0|(1<<4))          /* TTB FAULT */
#define TTB0_COARSE         (1|(1<<4))          /* COARSE PAGE BASE ADDR */
#define TTB0_SEG            (2|(1<<4))          /* SEG BASE ADDR */
#define TTB0_FINE           (3|(1<<4))          /* FINE PAGE BASE ADDR */

/* MMU TTB 1 BASE ATTR */ 
#define TTB1_FAULT          (0)
#define TTB1_LPG            (1)                 /* Large page */
#define TTB1_SPG            (2)                 /* small page */
#define TTB1_TPG            (3)                 /* tiny page */

/* domain access priority level */
#define FAULT_PL            (0x0)               /* domain fault */
#define USR_PL              (0x1)               /* usr mode */
#define RSV_PL              (0x2)               /* reserved */
#define SYS_PL              (0x3)               /* sys mode */

#define DOMAIN_FAULT        (0x0<<5)            /* fault 0*/
#define DOMAIN_SYS          (0x1<<5)            /* sys 1*/
#define DOMAIN_USR          (0x2<<5)            /* usr 2*/

/* C,B bit */
#define CB                  (3<<2)              /* cache_on, write_back */
#define CNB                 (2<<2)              /* cache_on, write_through */ 
#define NCB                 (1<<2)              /* cache_off,WR_BUF on */
#define NCNB                (0<<2)              /* cache_off,WR_BUF off */

/* ap 2 bits */
#define AP_FAULT            (0<<10)             /* access deny */
#define AP_SU_ONLY          (1<<10)             /* rw su only */
#define AP_USR_RO           (2<<10)             /* sup=RW, user=RO */
#define AP_RW               (3<<10)             /* su=RW, user=RW */

/* page dir 1 ap0 */
#define AP0_SU_ONLY         (1<<4)             /* rw su only */
#define AP0_USR_RO          (2<<4)             /* sup=RW, user=RO */
#define AP0_RW              (3<<4)             /* su=RW, user=RW */

/* page dir 1 ap1 */
#define AP1_SU_ONLY         (1<<6)             /* rw su only */
#define AP1_USR_RO          (2<<6)             /* sup=RW, user=RO */
#define AP1_RW              (3<<6)             /* su=RW, user=RW */


/* page dir 1 ap2 */
#define AP2_SU_ONLY         (1<<8)             /* rw su only */
#define AP2_USR_RO          (2<<8)             /* sup=RW, user=RO */
#define AP2_RW              (3<<8)             /* su=RW, user=RW */

/* page dir 1 ap3 */
#define AP3_SU_ONLY         (1<<10)             /* rw su only */
#define AP3_USR_RO          (2<<10)             /* sup=RW, user=RO */
#define AP3_RW              (3<<10)             /* su=RW, user=RW */


#define RAM_START           (0x30000000)
#define KERNEL_ENTRY        (0x30300000)        /* BANK 6 (3M) */
#define KERNEL_STACK        (0x3001A000)        /* BANK 6 (16K + 64K + 16K + 8K) (8k kernel stack) */
#define IRQ_STACK           (0x3001B000)        /* 4K IRQ STACK */
#define KERNEL_IMG_SIZE     (0x20000)            

#define IRQ_STACK           (0x3001B000)

/* 16K aignment */
#define TTB_BASE            (0x30000000)        
#define PAGE_DIR0           (TTB_BASE)
#define TTB_FULL_SIZE       (0x4000)        
#define PAGE_DIR1           (TTB_BASE+TTB_FULL_SIZE)                 

#define PAGE_DIR0_SIZE      (0x4000)            /* 16k */


    void _do_page_fault (void)
    {
        //
        ...........
        // 
        // read the FSR && get the vaddr && type here 
    volatile unsigned *page_dir = (volatile unsigned*)(TTB_BASE);  
    unsigned index = vaddr >> 20,i = 0, j = 0;
    unsigned page = 0;

        if (!(page_dir[index] & ~(0x3FF) && (type == 0x0B))) {          /* page_dir empty */
            i = index & ~0x03;                                          
            if ( (page_dir[i+0] & ~(0x3FF)) || (page_dir [i+1] & ~(0x3FF))         
              || (page_dir[i+2] & ~(0x3FF)) || (page_dir [i+3] & ~(0x3FF)) )
            {
                panic ( "page dir is bad !\n" );                        /* 4 continuous page_dir must be 0 */
            }

            if (!(page = find_free_page ())) 
                panic ( "no more free page !\n" );                      /* alloc a page page dir*/

            page_dir[i+0] = (page + 0x000) | DOMAIN_USR | TTB0_COARSE ; /* small page 1st 1KB */
            page_dir[i+1] = (page + 0x400) | DOMAIN_USR | TTB0_COARSE ; /* small page 2nd 1KB */
            page_dir[i+2] = (page + 0x800) | DOMAIN_USR | TTB0_COARSE ; /* small page 3rd 1KB */
            page_dir[i+3] = (page + 0xC00) | DOMAIN_USR | TTB0_COARSE ; /* small page 4th 1KB */

            if (!(page = find_free_page ())) 
                panic ( "no more free page !\n" );                      /* alloc a page page table*/

            volatile unsigned *page_tbl = (volatile unsigned*) (page_dir[index] & ~(0x3FF));

            *page_tbl = page|AP0_RW|AP1_RW|AP2_RW|AP3_RW| NCNB|TTB1_SPG;/* small page is used */


            invalidate_icache ();

            for (i = 0; i < 64; i++)
            {
                for (j = 0;j < 8;j ++)
                    clr_invalidate_dcache ( (i<<26)|(j<<5) );
            }


            invalidate_tlb ();
        }
        ........
        //

    }

    /* here is the macros */

    #define invalidate_tlb()    \
    {\
         __asm__  __volatile__ (\
            "mov    r0,#0\n"\
            "mcr    p15,0,r0,c8,c7,0\n"\
            :::"r0" \
        );\
    }

    #define clr_invalidate_dcache(index)    \
    {\
        __asm__  __volatile__ (\
            "mcr    p15,0,%[i],c7,c14,2\n"\
            :: [i]"r"(index)\
        );\
    }


    #define invalidate_icache() \
    {\
        __asm__  __volatile__ (\
            "mov    r0,#0\n"\
            "mcr    p15,0,r0,c7,c5,0\n"\
            ::: "r0"\
        );\
    }

    #define invalidate_dcache() \
    {\
        __asm__  __volatile__ (\
            "mov    r0,#0\n"\
            "mcr    p15,0,r0,c7,c6,0\n"\
            ::: "r0"\
        );\
    }



    #define invalidate_idcache()    \
    {\
        __asm__  __volatile__ (\
            "mov    r0,#0\n"\
            "mcr    p15,0,r0,c7,c7,0\n"\
            :::"r0"\
        );\
    }\

【问题讨论】:

  • 请提供实际使用的代码。一小段拼写错误的伪代码没有任何关于您正在使用的操作系统的信息,并不能提供关于您的问题可能是什么的最微弱的线索。此外,您确定您的翻译表条目这一事实并不意味着它是正确的,因此请也包括在内。
  • 上面贴了我使用的代码,不知道为什么我的s3c2440在调用invalidate_tbl()时会死机,能帮帮我吗,谢谢!
  • 如果你不想显示你的定义,你可以显示 L1L2hex 转储中止之前的表条目。请务必提供与 TTB_BASEL2 基础的偏移量。如果你这样做,你可以回答你自己的问题。
  • 所有#defines 都已发布。感谢您的帮助^_^

标签: arm mmu


【解决方案1】:

注意:我假设 TTB_BASE 是主要的 ARM L1 页表。如果是一些阴影,您需要根据unixsmurf 显示更多代码。这是我最好的猜测...

您的page_dir 既可用作主要的L1 条目,也可用作L2 精细页表。TTB_BASE 应该只包含节、超节或指向子页表的指针。您需要为 L2 页表分配更多的物理内存。

我猜你的 page_dir[i+0]page_dir[i+1] 等正在覆盖其他 L1 部分条目和你的invalidate_tlb() 使这个具体到 CPU。您应该使用 L2 page_tbl 指针来设置/索引小/细页面。也许您的内核代码位于 3G-4G 空间中,而您正在那里重写一些关键的 L1 映射;任何数量的奇怪的事情都可能发生。 page_tbl 的当前用途尚不清楚。

您不能双重使用L1表,因为它们对硬件有意义。我猜这只是一个错误?

L1页表中只有三种类型的条目,

  1. Sections - 1MB 内存直接映射 virt 到 phy,没有 2nd 页表。
  2. Super-sections - 每个部分都有 4MB 内存映射,但最大限度地减少 TLB 压力。
  3. 第 2nd 页表。条目可以是 1k、4k 和 64k。 细页表是 4k 的表大小,不在现代 ARM 设计中。每个条目是精细页表中 1k 的地址。

主页表必须位于16k对齐的物理地址上,这也是它的大小。 16k/(4bytes/entry)*1MB 给出 L1 表的 4GB 地址范围。所以主 L1 页表中的每个条目总是引用一个 1MB 条目。 粗略页表是较新 ARM 上的唯一选项,它们指的是 1K L2 表。

1K_L2_size * 4K_entry / (4bytes_per_entry) gives 1MB address space.
1K_L2_size * 64K_entry / (16bytes_per_entry) gives 1MB address space.

L1 中有四个1MB 条目,用于一个超级部分。 L2 表中每个 64k 大页面 有四个条目。如果您使用 super-sections,则您没有 L2 条目。

我认为您可能将超级部分大页面混淆了?对于某些格式不正确的页表只会在 TLB 无效 上表现出来,因此 MMU 映射会通过遍历从表中重新获取。

最后,你应该刷新D缓存drain写缓冲区以确保与内存的一致性。您可能还希望有一个内存屏障

static inline void dcache_clean(void)
{
    const int zero = 0;
    asm volatile ("" ::: "memory"); /* barrier */
    /* clean entire D cache -> push to external memory. */
    asm volatile ("1: mrc p15, 0, r15, c7, c10, 3\n"
                    " bne 1b\n" ::: "cc");
    /* drain the write buffer */
    asm volatile ("mcr 15, 0, %0, c7, c10, 4"::"r" (zero));
}

还有协处理器命令使单个 TLB 条目无效,因为您只更改数据故障中的 vaddr/paddr 映射的一部分。

另请参阅:ARM MMU tutorialVirtual Memory structures 和您的 ARM 架构参考手册

【讨论】:

  • 是的,TTB_BASE 是 Level 1 page_table ,它花费 16KB 物理地址。情况是我想使用部分映射 0x0000 - 0x30000000 && 0x40000000 - 0xA0000000,并使用粗页面映射(成本 1KB),0x30000000 - 0x34000000 (SDRAM = 64M)。由于 L2(粗页表占用 1KB),我让 4 个连续的 4 个 L2 页表使用同一页。这就是我使用 'i = index & ~0x03' 的原因。
  • 在L1表中,i = index & ~3得到4M对齐索引,在L1成本4 * 4Byte,当4M的page dir之一为空时,“index”中的所有L1 page dir & ~3" 实体应该指向同一页面 *page_tbl 表示我使用了 page_tbl[0],可能是 vaddr 不是 4M 对齐时出错,但 3G 地址就好了。
  • 一个 L1 page_table 需要 4 个字节,无论类型是什么,它都指向 1MB。当我使用粗页面时,L1 page_table ptr 指向 1KB 物理内存,但 find_free_page () 返回 4KB 页面,所以我让L1 4-continuous entries ptr 指向同一页,如果 vaddr 为 3M,则 3M(4K 页)的 L1 条目与 0M,1M,2M 相同。 "i = index & ~3" 获取 4M 对齐条目。
  • 我的意思是 3M vaddr 的 L1 条目与 0M、1M、2M 采用相同的物理页面......但它们的偏移量不同,0M 有 0 - (0x3FF),1M 有 0X400 - 7ff ) ......
  • 现在,我不需要关闭mmu,我只需输入代码:clr_all_dcache ();//dcache_clean ();invalidate_dcache ();__asm__ __volatile__ ("mcr p15,0,%0,c8,c6,1"::"r"(vaddr));
猜你喜欢
  • 2012-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多