eboot彙編Startup.s中MMU設置流程詳細分析(轉)

CE5.0 - eboot彙編Startup.s中MMU設置流程詳細分析

以下爲SMDK開發板startup.s部分啓動代碼.
;-------------------------------------------------------------------------------

MemoryMap EQU 0x2a4
BANK_SIZE EQU 0x00100000 ; 1MB per bank in MemoryMap array
BANK_SHIFT EQU 20


; Define RAM space for the Page Tables:
;
PHYBASE EQU 0x30000000 ; physical start
PTs EQU 0x30010000 ; 1st level page table address (PHYBASE + 0x10000)
                                        ; save room for interrupt vectors.
;------------------------------------------------------------------------------
; Copy boot loader to memory

        ands r9, pc, #0xFF000000 ; see if we are in flash or in ram
//如果高8字節非0,那麼說明pc已經在ram,否則說明現在pc位於reset之後的0x00000000地址區間[luther.gliethttp]

        bne %f20 ; go ahead if we are already in ram//高8字節非0,說明eboot已在DDR中,跳到下面的20標號


        ; This is the loop that perform copying.
        ldr r0, = 0x38000 ; offset into the RAM
        add r0, r0, #PHYBASE ; add physical base //eboot最終的物理地址爲0x30038000,這也就是PLATFORM/SMDK2440A/Src/Bootloader/Eboot_usb/boot.bib中定義的

//eboot鏈接地址EBOOT 8c038000 00016800 RAMIMAGE //一個.bib允許由一個RAMIMAGE類型段,生成一個對應的.bin文件,之後由romimage.exe生成.nb0同時填充eboot的pTOC全局變量指針[luther.gliethttp]

        mov r1, r0 ; (r1) copy destination
//因爲s3c2440在nand啓動時,會將nand中前4k數據讀取到s3c2440內部的4k ram中,同時ram映射成0地址,cpu

//跳轉到ram開始執行從nand讀取的前4k代碼數據,於是讀取位於ram中0地址之後的4k數據就等同於讀取了nand的前4k數據[luther.gliethttp].

        ldr r2, =0x0 ; (r2) flash started at physical address 0
        ldr r3, =0x10000 ; counter (0x40000/4)//循環讀取4k字節

10 ldr r4, [r2], #4
        str r4, [r1], #4
        subs r3, r3, #1
        bne %b10

        ; Restart from the RAM position after copying.
        mov pc, r0 //跳轉到物理地址0x30038000重新執行上面的reset代碼,再次執行時因爲已經位於ram,所以將執行上面的bne %f20語句[luther.gliethttp]

        nop //爲流水線添加可能存在的指令最大空取次數

        nop
        nop

        ; Shouldn't get here.
        b       .

        INCLUDE oemaddrtab_cfg.inc  //這就是eboot的mmu映射表,位於PLATFORM/SMDK2440A/Src/Inc/oemaddrtab_cfg.inc
/*
        EXPORT  g_oalAddressTable[DATA]
g_oalAddressTable
        DCD     0x80000000, 0x32000000, 32      ; 32 MB DRAM BANK 6
        DCD     0x82000000, 0x08000000, 32      ; 32 MB SROM(SRAM/ROM) BANK 1
        DCD     0x84000000, 0x10000000, 32      ; nGCS2: PCMCIA/PCCARD
        DCD     0x86000000, 0x18000000, 32      ; 32 MB SROM(SRAM/ROM) BANK 3
        DCD     0x88000000, 0x20000000, 32      ; 32 MB SROM(SRAM/ROM) BANK 4
        DCD     0x8A000000, 0x28000000, 32      ; 32 MB SROM(SRAM/ROM) BANK 5
        DCD     0x8C000000, 0x30000000, 32      ; 32 MB DRAM BANK 6
        DCD     0x90800000, 0x48000000,  1      ; Memory control register
        DCD     0x90900000, 0x49000000,  1      ; USB Host register
        DCD     0x90A00000, 0x4A000000,  1      ; Interrupt Control register
        DCD     0x90B00000, 0x4B000000,  1      ; DMA control register
        DCD     0x90C00000, 0x4C000000,  1      ; Clock & Power register
        DCD     0x90D00000, 0x4D000000,  1      ; LCD control register
        DCD     0x90E00000, 0x4E000000,  1      ; NAND flash control register
        DCD     0x90F00000, 0x4F000000,  1      ; Camera control register
        DCD     0x91000000, 0x50000000,  1      ; UART control register
        DCD     0x91100000, 0x51000000,  1      ; PWM timer register
        DCD     0x91200000, 0x52000000,  1      ; USB device register
        DCD     0x91300000, 0x53000000,  1      ; Watchdog Timer register
        DCD     0x91400000, 0x54000000,  1      ; IIC control register
        DCD     0x91500000, 0x55000000,  1      ; IIS control register
        DCD     0x91600000, 0x56000000,  1      ; I/O Port register
        DCD     0x91700000, 0x57000000,  1      ; RTC control register
        DCD     0x91800000, 0x58000000,  1      ; A/D convert register
        DCD     0x91900000, 0x59000000,  1      ; SPI register
        DCD     0x91A00000, 0x5A000000,  1      ; SD Interface register
        DCD     0x92000000, 0x00000000, 32      ; 32 MB SROM(SRAM/ROM) BANK 0
        DCD     0x00000000, 0x00000000,  0      ; end of table
*/

        ; Compute physical address of the OEMAddressTable.
20      add     r11, pc, #g_oalAddressTable - (. + 8)
        ldr     r10, =PTs                ; (r10) = 1st level page table
0x20000000

        ; Setup 1st level page table (using section descriptor)     
        ; Fill in first level page table entries to create "un-mapped" regions
        ; from the contents of the MemoryMap array.
        ;
        ;   (r10) = 1st level page table
        ;   (r11) = ptr to MemoryMap array
//PTR的地址大家都選在了DDR開始的offset處,ce爲64k偏移,linux也是這樣做的
//然後第1級MMU入口爲pc地址的高14位(過濾掉低18位)[luther.gliethttp]
//比如0x80000000地址對應的第1級MMU入口偏移爲0x80000000 >> 18 = 0x2000
        add     r10, r10, #0x2000       ; (r10) = ptr to 1st PTE for "unmapped space"
//r10存放0x80000000虛擬地址在PTR中的對應地址
        mov     r0, #0x0E               ; (r0) = PTE for 0: 1MB cachable bufferable //有高速緩存C和寫緩衝B
        orr     r0, r0, #0x400          ; set kernel r/w permission
25      mov     r1, r11                 ; (r1) = ptr to MemoryMap array

        
30      ldr     r2, [r1], #4            ; (r2) = virtual address to map Bank at
        ldr     r3, [r1], #4            ; (r3) = physical address to map from
        ldr     r4, [r1], #4            ; (r4) = num MB to map

        cmp     r4, #0                  ; End of table?
        beq     %f40    //表示cachable地址0x80000000~0xa0000000映射MMU創建完畢,表結尾,前跳到40標號,創建對應的uncached映射MMU[luther.gliethttp]

        ldr     r5, =0x1FF00000
        and     r2, r2, r5              ; VA needs 512MB, 1MB aligned.  //對va虛擬地址進行512M對齊[luther.gliethttp]
//也就是r2的上限值不能超過0x20000000即512M,因爲r10現在指向1級映射表的0x80000000虛擬地址,
//又因爲0x80000000+0x20000000=0xa0000000開始地址存放的是uncached地址,所以這裏要做512M數據限制[luther.gliethttp]

        ldr     r5, =0xFFF00000
        and     r3, r3, r5              ; PA needs 4GB, 1MB aligned.    //對pa物理地址進行4G對齊

        add     r2, r10, r2, LSR #18
        add     r0, r0, r3              ; (r0) = PTE for next physical page //在MMU控制flag基礎上追加映射到的pa物理地址[luther.gliethttp]

35      str     r0, [r2], #4            //存到r2對應的PTR偏移中
        add     r0, r0, #0x00100000     ; (r0) = PTE for next physical page //1M遞增,因爲1<<18=256K,而每一個pte描述4k頁,所以最終描述256k*4k=1M地址空間[luther.gliethttp]
        sub     r4, r4, #1              ; Decrement number of MB left
        cmp     r4, #0
        bne     %b35                    ; Map next MB   //遍歷到該region結束

        bic     r0, r0, #0xF0000000     ; Clear Section Base Address Field
        bic     r0, r0, #0x0FF00000     ; Clear Section Base Address Field//清空r0中的地址信息[lutehr.gliethttp]
        b       %b30                    ; Get next element  //繼續創建oemaddrtab_cfg.inc描述的下一region[luther.gliethttp]
//創建對應的0xa0000000開始的uncached映射MMU表
40      tst     r0, #8      
//比較C高速緩存是否仍然置位,如果仍然置位了,那麼說明還沒有執行uncached創建,如果C位已經清0,那麼說明,uncached循環也執行完畢了,所以跳回到25標號繼續創建[luther.gliethttp]
        bic     r0, r0, #0x0C           ; clear cachable & bufferable bits in PTE//清除B寫緩衝和C高速緩存[luther.gliethttp]
        add     r10, r10, #0x0800       ; (r10) = ptr to 1st PTE for "unmapped uncached space"//r10現在對應0x80000000虛擬地址的PTR起始地址,hex(0x20000000>>18)爲0x800,
//所以r10 = r10 + 0x800;之後r10指向了0xa0000000虛擬地址對應的PTR起始地址[luther.gliethtp]
        bne     %b25                    ; go setup PTEs for uncached space
//ok,現在0x80000000 cachable空間和0xa0000000 uncached空間都已經創建完畢了[luther.gliethttp]
        sub     r10, r10, #0x3000       ; (r10) = restore address of 1st level page table//該句好像有問題,不過還好後面用到r10之前,又對r10進行了重新賦值,所以也更說明,該句操作無意義[luther.gliethttp]

//接下來爲虛擬0地址建立cachable和uncached映射[lutehr.gliethttp]
        ; Setup mmu to map (VA == 0) to (PA == 0x30000000).
        ldr     r0, =PTs                ; PTE entry for VA = 0
        ldr     r1, =0x3000040E         ; uncache/unbuffer/rw, PA base == 0x30000000//以C,B均開啓的方式映射虛擬地址0到pa物理地址0x30000000
        str     r1, [r0]

        ; uncached area.
        add     r0, r0, #0x0800         ; PTE entry for VA = 0x0200.0000 , uncached//接下來的0x2000000虛擬地址被映射爲虛擬0地址的uncached起始地址[luther.gliethttp]
        ldr     r1, =0x30000402         ; uncache/unbuffer/rw, base == 0x30000000//以C,B均不開啓的方式映射虛擬地址0到pa物理地址0x30000000
        str     r1, [r0]
//經過上面設置可以看到
//虛擬地址0對應0x30000000的cachable
//虛擬地址0x2000000對應0x30000000的uncached.[luther.gliethttp]
        ; Comment:
        ; The following loop is to direct map RAM VA == PA. i.e.
        ;   VA == 0x30XXXXXX => PA == 0x30XXXXXX for S3C2400
        ; Fill in 8 entries to have a direct mapping for DRAM
        ;
        ldr     r10, =PTs               ; restore address of 1st level page table
        ldr     r0,  =PHYBASE           //PHYBASE爲0x30000000,將虛擬地址映射到相同的物理地址

        add     r10, r10, #(0x3000 / 4) ; (r10) = ptr to 1st PTE for 0x30000000 //就是(0x30000000 >> 16) >> 2即(0x30000000 >> 16) / 4 [luther.gliethttp]
//現在r10指向了和DDR地址相等的PA地址對應的PTR地址處
        add     r0, r0, #0x1E           ; 1MB cachable bufferable
        orr     r0, r0, #0x400          ; set kernel r/w permission
        mov     r1, #0
        mov     r3, #64                 //映射64M空間,因爲我們的物理DDR只有64M(見上面的oemaddrtab_cfg.inc表圖),不能大於512,因爲下面只支持到512次循環(r1爲循環次數,每循環1次,創建1M空間映射表)[luther.gliethtp]
45      mov     r2, r1                  ; (r2) = virtual address to map Bank at
        cmp     r2, #0x20000000:SHR:BANK_SHIFT  //0x20000000>>20=512M,
        add     r2, r10, r2, LSL #BANK_SHIFT-18 //add r2, r10, r2, 4即r2 = r10 + r2*4;正好爲PTR操作時的4字節對齊邊界[luther.gliethttp]
        strlo   r0, [r2]                        //如果還沒有操作到512M,那麼填充PTR,如果現在映射操作已經操作過了512M大小,那麼忽略
        add     r0, r0, #0x00100000     ; (r0) = PTE for next physical page
        subs    r3, r3, #1
        add     r1, r1, #1
        bgt     %b45

        ldr     r10, =PTs               ; (r10) = restore address of 1st level page table

        ; The page tables and exception vectors are setup.
        ; Initialize the MMU and turn it on.
        mov     r1, #1
        mcr     p15, 0, r1, c3, c0, 0   ; setup access to domain 0
        mcr     p15, 0, r10, c2, c0, 0

        mcr     p15, 0, r0, c8, c7, 0   ; flush I+D TLBs    //flush所有MMU相關空間
        mov     r1, #0x0071             ; Enable: MMU       //設置打開MMU標誌到r1
        orr     r1, r1, #0x0004         ; Enable the cache  //設置打開cache標誌到r1

        ldr     r0, =VirtualStart       //取出VirtualStart編譯期間得到的虛擬地址值,在0x30038000~0x30038000+eboot_size之間[luther.gliethtp]

        cmp     r0, #0                  ; make sure no stall on "mov pc,r0" below //執行該比較,使r0踏實的讀入VirtualStart數值
        mcr     p15, 0, r1, c1, c0, 0   //將MMU設置標誌寫入p15協處理器,生效設置[luther.gliethttp]
        mov     pc, r0                  ;  & jump to new virtual address //跳轉過去,此刻之後MMU已經打開,所有允許操作的虛擬地址都在PLATFORM/SMDK2440A/Src/Inc/oemaddrtab_cfg.inc中定義[luther.gliethttp]
        nop

        ; MMU & caches now enabled.
        ;   (r10) = physcial address of 1st level page table
        ;

VirtualStart

        mov     sp, #0x8C000000         //設置棧空間,從這裏來看,sp棧頂對應的DDR物理地址爲0x30030000,然後向下生長,從boot.bib中

/*

MEMORY
;   Name     Start     Size      Type
;   -------  --------  --------  ----
    ARGS     8c020800  00000800  RESERVED
    BINFS    8c021000  00005000  RESERVED
    RAM      8c026000  00006000  RAM   
    STACK    8c02c000  00004000  RESERVED  //0x2c000+0x4000=0x30000 保留該16K地址空間用作eboot的sp棧空間
    EBOOT    8c038000  00016800  RAMIMAGE

*/

可以看到sp棧空間由.bib保留,位於0x30030000~0x3002c000,棧sp從0x30030000開始向下生長[luther.gliethttp]
        add     sp, sp, #0x30000        ; arbitrary initial super-page stack pointer
        b       main //跳轉到main主函數

原帖地址:http://blog.chinaunix.net/u1/38994/showart_1881778.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章