* Need help about how linux to handle translation fault
@ 2011-08-12 7:37 Pecker Hu
2011-08-12 11:27 ` Clemens Ladisch
0 siblings, 1 reply; 2+ messages in thread
From: Pecker Hu @ 2011-08-12 7:37 UTC (permalink / raw)
To: linux-kernel
Dear all:
I am a newer,I hope someone can help me about how linux to handle
translation fault(ARM architecture)
I define a string arrary in user space code:
unsignd char str[20] = "abcdefg";
And I send the address of str to kernel space using IOCTL interface.
ioctl(fd, VM_TEST_GET_VADDR, str);
When I try to use pgd to find the physical address, I don't find it
.But after calling copy_from_user,I can find physical address through
pgd.I don' know why?
/* user space address convert to physical address , if pte is NULL ,
it will return zero */
static unsigned long uva_to_pa(struct mm_struct *mm, unsigned long addr)
{
unsigned long ret = 0UL;
pgd_t *pgd;
pud_t *pud;
pmd_t *pmd;
pte_t *pte;
pgd = pgd_offset(mm, addr);
if (!pgd_none(*pgd)) {
pud = pud_offset(pgd, addr);
if (!pud_none(*pud)) {
pmd = pmd_offset(pud, addr);
if (!pmd_none(*pmd)) {
pte = pte_offset_map(pmd, addr);
if (!pte_none(*pte) && pte_present(*pte)) {
/* Use hard PTE */
pte = (pte_t *)((u32)pte - 2048);
if(pte)
ret = (*pte & 0xfffff000) | (addr & 0xfff);
} else {
printk("pet is not present:0x%08x\n", (*pte & 0xfffff000) | (addr
& 0xfff));
}
}
}
}
return ret;
}
So I think string array is located in data cache not in physical
memory, after I call copy_from_user, kernel will allocate page for it,
then we can access it, is it right?
Thanks
Best regards.
Pecker Hu
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: Need help about how linux to handle translation fault
2011-08-12 7:37 Need help about how linux to handle translation fault Pecker Hu
@ 2011-08-12 11:27 ` Clemens Ladisch
0 siblings, 0 replies; 2+ messages in thread
From: Clemens Ladisch @ 2011-08-12 11:27 UTC (permalink / raw)
To: Pecker Hu; +Cc: linux-kernel
Pecker Hu wrote:
> ...
> And I send the address of str to kernel space using IOCTL interface.
> ioctl(fd, VM_TEST_GET_VADDR, str);
>
> When I try to use pgd to find the physical address, I don't find it
> .
You must not directly access user space memory from kernel space because
that memory can be moved or swapped out at any time. Use only functions
like copy_from_user() which take appropriate precautions; and don't
forget to check for errors.
Furthermore, the physical address is useless. If you want to access
the memory from a device, you have to use the DMA mapping API because
the CPU's physical address it not necessarily the same address as that
used on the device's bus, and sometimes you have to program an IOMMU
before devices are allowed to access that memory at all.
> So I think string array is located in data cache not in physical
> memory, after I call copy_from_user, kernel will allocate page for it,
> then we can access it, is it right?
As far as any code running on the CPU is concerned, caches are
completely transparent. (This is not always true for devices, but the
DMA mapping API takes care of that.)
Regards,
Clemens
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2011-08-12 11:23 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-08-12 7:37 Need help about how linux to handle translation fault Pecker Hu
2011-08-12 11:27 ` Clemens Ladisch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox