RISCV获取物理地址

define get_phyaddr
    printf "argc=%d\n", $argc
    set $ppn = $satp & 0xfffffffffff
    printf "satp: 0x%llx satp.ppn:0x%llx\n", $satp, $ppn

    set $vaddr = $arg0
    printf "vaddr: 0x%llx\n", $vaddr
    set $2 = ($vaddr >> 30) & 0x1ff
    set $1 = ($vaddr >> 21) & 0x1ff
    set $0 = ($vaddr >> 12) & 0x1ff
     printf "2: 0x%llx 1: 0x%llx 0: 0x%llx\n", $2, $1, $0

    set $pte_addr = (($ppn << 12)) + $2 * 8
    set $pte = *(unsigned long long*)$pte_addr
    set $pte1_ppn = (($pte >> 10) << 12) & 0xfffffffffff
    set $paddr1 = $pte1_ppn + ($vaddr &  ((1 << 30) - 1))
    printf "pte1:0x%llx phy addr1: 0x%llx pte1.ppn: 0x%llx \n", $pte, $paddr1, $pte1_ppn


    set $pte_addr = $pte1_ppn + $1 * 8
    set $pte = *(unsigned long long*)$pte_addr
    set $pte2_ppn = (($pte >> 10) << 12) & 0xfffffffffff
    set $paddr2 = $pte2_ppn + ($vaddr &  ((1 << 21) - 1))
    printf "pte2:0x%llx phy addr2: 0x%llx\n", $pte, $paddr2

    set $pte_addr = $pte2_ppn + $0 * 8
    set $pte = *(unsigned long long*)$pte_addr
    set $pte3_ppn = (($pte >> 10) << 12) & 0xfffffffffff
    set $paddr3 = $pte3_ppn + ($vaddr &  ((1 << 12) - 1))
    printf "pte3:0x%llx phy addr3: 0x%llx\n", $pte, $paddr3

你可能感兴趣的:(linux)