Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
cs1ime committed Jul 19, 2023
1 parent 9c52585 commit 4c799d7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 62 deletions.
1 change: 1 addition & 0 deletions dma/include/dma_mmu.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ class mmu_tlb : public mmu_initializer
}

private:
bool rw_virt_from_tlb(uptr virt, u8* pb, u32 cb,bool iswrite);
mmu_pagetype entryexist(uptr virt);
std::shared_mutex smtx;
std::unordered_map <u64, u64>mmu_tlb_4k;
Expand Down
85 changes: 23 additions & 62 deletions dma/src/dma_mmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,20 @@ physaddr mmu_tlb::virt2phys(uptr virt, tlb_mode mode)
return entryget(virt);
}

bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
bool mmu_tlb::rw_virt_from_tlb(uptr virt, u8 *pb, u32 cb, bool iswrite)
{
auto rwphys =
[&](physaddr pa, u8* pb, u32 cb)->bool
{
if(iswrite)
{
return this->write_phys(pa,pb,cb);
}
else
{
return this->read_phys(pa,pb,cb);
}
};
if (cb == 0)
return false;
// DbgBreakPoint();
Expand All @@ -224,7 +236,7 @@ bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
u64 phy = virt2phys((uptr)virt);
if (phy != badphysaddr)
{
if (!read_phys(phy, pb, cb))
if (!rwphys(phy, pb, cb))
return false;
}
else
Expand All @@ -241,7 +253,7 @@ bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
u64 phy = virt2phys((uptr)virt);
if (phy != badphysaddr)
{
if (!read_phys(phy, pb, FirstReadSize))
if (!rwphys(phy, pb, FirstReadSize))
return false;
}
else
Expand All @@ -260,7 +272,7 @@ bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
phy = virt2phys((uptr)FullPageAddress);
if (phy != badphysaddr)
{
if (!read_phys(phy, pDst, PAGE_SIZE))
if (!rwphys(phy, pDst, PAGE_SIZE))
return false;
}
else
Expand All @@ -278,7 +290,7 @@ bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
phy = virt2phys((uptr)EndReadPage);
if (phy != badphysaddr)
{
if (!read_phys(phy, ((pbyte)pb) + EndReadDstOffset, EndReadSize))
if (!rwphys(phy, ((pbyte)pb) + EndReadDstOffset, EndReadSize))
return false;
}
else
Expand All @@ -289,65 +301,13 @@ bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
}
return true;
}
bool mmu_tlb::read_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
{
return rw_virt_from_tlb(virt,pb,cb,false);
}
bool mmu_tlb::write_virt_from_tlb(uptr virt, u8 *pb, u32 cb)
{
if (cb == 0)
return false;
// DbgBreakPoint();

u64 StartPhysicalAddress = (u64)virt;
u64 EndPhysicalAddress = StartPhysicalAddress + cb - 1;
u64 StartPhysicalAddressPage = StartPhysicalAddress & 0xFFFFFFFFFFFFF000;
u64 EndPhysicalAddressPage = EndPhysicalAddress & 0xFFFFFFFFFFFFF000;
u64 StartPFN = MmiGetPhysicalPFN(StartPhysicalAddress);
u64 EndPFN = MmiGetPhysicalPFN(EndPhysicalAddress);
u32 ThroughPages = EndPFN - StartPFN;
if (ThroughPages == 0)
{
u64 phy = virt2phys((uptr)virt);
if (phy == badphysaddr)
return false;
// memcpy(Dst, ((PUCHAR)Pending) + offset, Lenth);
if (!write_phys(phy, pb, cb))
return false;
}
else
{
u64 FirstReadPage = StartPhysicalAddressPage;
u32 FirstReadOffset = StartPhysicalAddress & 0x0000000000000FFF;
u32 FirstReadSize = PAGE_SIZE - FirstReadOffset;
u64 phy = virt2phys((uptr)virt);
if (phy == badphysaddr)
return false;
if (!write_phys(phy, pb, FirstReadSize))
return false;

u64 EndReadPage = EndPhysicalAddressPage;
u32 EndReadSize = (EndPhysicalAddress & 0x0000000000000FFF) + 1;
u32 EndReadDstOffset = (ThroughPages - 1) * PAGE_SIZE + FirstReadSize;

phy = virt2phys((uptr)EndReadPage);
if (phy == badphysaddr)
return false;
if (!write_phys(phy, ((pbyte)pb) + EndReadDstOffset, EndReadSize))
return false;

u32 FullPageNum = ThroughPages - 1;
if (FullPageNum)
{
for (u32 i = 0; i < FullPageNum; ++i)
{
u64 FullPageAddress = FirstReadPage + PAGE_SIZE + i * PAGE_SIZE;
pbyte pDst = (pbyte)pb + FirstReadSize + (i * PAGE_SIZE);
phy = virt2phys((uptr)FullPageAddress);
if (phy == badphysaddr)
return false;
if (!write_phys(phy, pDst, PAGE_SIZE))
return false;
}
}
}
return true;
return rw_virt_from_tlb(virt,pb,cb,true);
}

#define rr this->r
Expand Down Expand Up @@ -533,6 +493,7 @@ u64 mmu::enummem(uptr start_va, uptr end_va, std::function<bool(u64 start, u64 s
if (!read_phys(this->get_dtb(), (pbyte)pml4, 0x1000))
return 0;


for (int pml4e_idx = stva.pml4e_index; pml4e_idx <= edva.pml4e_index; pml4e_idx++)
{
va_t pml4_curva(0, pml4e_idx);
Expand Down

0 comments on commit 4c799d7

Please sign in to comment.