/*1* Copyright (C) 2006 Hollis Blanchard <[email protected]>, IBM Corporation2*3* This program is free software; you can redistribute it and/or modify4* it under the terms of the GNU General Public License as published by5* the Free Software Foundation; either version 2 of the License, or6* (at your option) any later version.7*8* This program is distributed in the hope that it will be useful,9* but WITHOUT ANY WARRANTY; without even the implied warranty of10* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the11* GNU General Public License for more details.12*13* You should have received a copy of the GNU General Public License14* along with this program; if not, write to the Free Software15* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA16*/1718#include <linux/mm.h>1920static unsigned long kernel_virtual_offset;21static int is_xencomm_initialized;2223/* for xen early printk. It uses console io hypercall which uses xencomm.24* However early printk may use it before xencomm initialization.25*/26int27xencomm_is_initialized(void)28{29return is_xencomm_initialized;30}3132void33xencomm_initialize(void)34{35kernel_virtual_offset = KERNEL_START - ia64_tpa(KERNEL_START);36is_xencomm_initialized = 1;37}3839/* Translate virtual address to physical address. */40unsigned long41xencomm_vtop(unsigned long vaddr)42{43struct page *page;44struct vm_area_struct *vma;4546if (vaddr == 0)47return 0UL;4849if (REGION_NUMBER(vaddr) == 5) {50pgd_t *pgd;51pud_t *pud;52pmd_t *pmd;53pte_t *ptep;5455/* On ia64, TASK_SIZE refers to current. It is not initialized56during boot.57Furthermore the kernel is relocatable and __pa() doesn't58work on addresses. */59if (vaddr >= KERNEL_START60&& vaddr < (KERNEL_START + KERNEL_TR_PAGE_SIZE))61return vaddr - kernel_virtual_offset;6263/* In kernel area -- virtually mapped. */64pgd = pgd_offset_k(vaddr);65if (pgd_none(*pgd) || pgd_bad(*pgd))66return ~0UL;6768pud = pud_offset(pgd, vaddr);69if (pud_none(*pud) || pud_bad(*pud))70return ~0UL;7172pmd = pmd_offset(pud, vaddr);73if (pmd_none(*pmd) || pmd_bad(*pmd))74return ~0UL;7576ptep = pte_offset_kernel(pmd, vaddr);77if (!ptep)78return ~0UL;7980return (pte_val(*ptep) & _PFN_MASK) | (vaddr & ~PAGE_MASK);81}8283if (vaddr > TASK_SIZE) {84/* percpu variables */85if (REGION_NUMBER(vaddr) == 7 &&86REGION_OFFSET(vaddr) >= (1ULL << IA64_MAX_PHYS_BITS))87ia64_tpa(vaddr);8889/* kernel address */90return __pa(vaddr);91}9293/* XXX double-check (lack of) locking */94vma = find_extend_vma(current->mm, vaddr);95if (!vma)96return ~0UL;9798/* We assume the page is modified. */99page = follow_page(vma, vaddr, FOLL_WRITE | FOLL_TOUCH);100if (!page)101return ~0UL;102103return (page_to_pfn(page) << PAGE_SHIFT) | (vaddr & ~PAGE_MASK);104}105106107