xencomm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright (C) 2006 Hollis Blanchard <hollisb@us.ibm.com>, IBM Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/mm.h>
  19. static unsigned long kernel_virtual_offset;
  20. static int is_xencomm_initialized;
  21. /* for xen early printk. It uses console io hypercall which uses xencomm.
  22. * However early printk may use it before xencomm initialization.
  23. */
  24. int
  25. xencomm_is_initialized(void)
  26. {
  27. return is_xencomm_initialized;
  28. }
  29. void
  30. xencomm_initialize(void)
  31. {
  32. kernel_virtual_offset = KERNEL_START - ia64_tpa(KERNEL_START);
  33. is_xencomm_initialized = 1;
  34. }
  35. /* Translate virtual address to physical address. */
  36. unsigned long
  37. xencomm_vtop(unsigned long vaddr)
  38. {
  39. struct page *page;
  40. struct vm_area_struct *vma;
  41. if (vaddr == 0)
  42. return 0UL;
  43. if (REGION_NUMBER(vaddr) == 5) {
  44. pgd_t *pgd;
  45. pud_t *pud;
  46. pmd_t *pmd;
  47. pte_t *ptep;
  48. /* On ia64, TASK_SIZE refers to current. It is not initialized
  49. during boot.
  50. Furthermore the kernel is relocatable and __pa() doesn't
  51. work on addresses. */
  52. if (vaddr >= KERNEL_START
  53. && vaddr < (KERNEL_START + KERNEL_TR_PAGE_SIZE))
  54. return vaddr - kernel_virtual_offset;
  55. /* In kernel area -- virtually mapped. */
  56. pgd = pgd_offset_k(vaddr);
  57. if (pgd_none(*pgd) || pgd_bad(*pgd))
  58. return ~0UL;
  59. pud = pud_offset(pgd, vaddr);
  60. if (pud_none(*pud) || pud_bad(*pud))
  61. return ~0UL;
  62. pmd = pmd_offset(pud, vaddr);
  63. if (pmd_none(*pmd) || pmd_bad(*pmd))
  64. return ~0UL;
  65. ptep = pte_offset_kernel(pmd, vaddr);
  66. if (!ptep)
  67. return ~0UL;
  68. return (pte_val(*ptep) & _PFN_MASK) | (vaddr & ~PAGE_MASK);
  69. }
  70. if (vaddr > TASK_SIZE) {
  71. /* percpu variables */
  72. if (REGION_NUMBER(vaddr) == 7 &&
  73. REGION_OFFSET(vaddr) >= (1ULL << IA64_MAX_PHYS_BITS))
  74. ia64_tpa(vaddr);
  75. /* kernel address */
  76. return __pa(vaddr);
  77. }
  78. /* XXX double-check (lack of) locking */
  79. vma = find_extend_vma(current->mm, vaddr);
  80. if (!vma)
  81. return ~0UL;
  82. /* We assume the page is modified. */
  83. page = follow_page(vma, vaddr, FOLL_WRITE | FOLL_TOUCH);
  84. if (!page)
  85. return ~0UL;
  86. return (page_to_pfn(page) << PAGE_SHIFT) | (vaddr & ~PAGE_MASK);
  87. }