physaddr.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <linux/bootmem.h>
  2. #include <linux/mmdebug.h>
  3. #include <linux/export.h>
  4. #include <linux/mm.h>
  5. #include <asm/page.h>
  6. #include "physaddr.h"
  7. #ifdef CONFIG_X86_64
  8. #ifdef CONFIG_DEBUG_VIRTUAL
  9. unsigned long __phys_addr(unsigned long x)
  10. {
  11. unsigned long y = x - __START_KERNEL_map;
  12. /* use the carry flag to determine if x was < __START_KERNEL_map */
  13. if (unlikely(x > y)) {
  14. x = y + phys_base;
  15. VIRTUAL_BUG_ON(y >= KERNEL_IMAGE_SIZE);
  16. } else {
  17. x = y + (__START_KERNEL_map - PAGE_OFFSET);
  18. /* carry flag will be set if starting x was >= PAGE_OFFSET */
  19. VIRTUAL_BUG_ON((x > y) || !phys_addr_valid(x));
  20. }
  21. return x;
  22. }
  23. EXPORT_SYMBOL(__phys_addr);
  24. unsigned long __phys_addr_symbol(unsigned long x)
  25. {
  26. unsigned long y = x - __START_KERNEL_map;
  27. /* only check upper bounds since lower bounds will trigger carry */
  28. VIRTUAL_BUG_ON(y >= KERNEL_IMAGE_SIZE);
  29. return y + phys_base;
  30. }
  31. EXPORT_SYMBOL(__phys_addr_symbol);
  32. #endif
  33. bool __virt_addr_valid(unsigned long x)
  34. {
  35. unsigned long y = x - __START_KERNEL_map;
  36. /* use the carry flag to determine if x was < __START_KERNEL_map */
  37. if (unlikely(x > y)) {
  38. x = y + phys_base;
  39. if (y >= KERNEL_IMAGE_SIZE)
  40. return false;
  41. } else {
  42. x = y + (__START_KERNEL_map - PAGE_OFFSET);
  43. /* carry flag will be set if starting x was >= PAGE_OFFSET */
  44. if ((x > y) || !phys_addr_valid(x))
  45. return false;
  46. }
  47. return pfn_valid(x >> PAGE_SHIFT);
  48. }
  49. EXPORT_SYMBOL(__virt_addr_valid);
  50. #else
  51. #ifdef CONFIG_DEBUG_VIRTUAL
  52. unsigned long __phys_addr(unsigned long x)
  53. {
  54. unsigned long phys_addr = x - PAGE_OFFSET;
  55. /* VMALLOC_* aren't constants */
  56. VIRTUAL_BUG_ON(x < PAGE_OFFSET);
  57. VIRTUAL_BUG_ON(__vmalloc_start_set && is_vmalloc_addr((void *) x));
  58. /* max_low_pfn is set early, but not _that_ early */
  59. if (max_low_pfn) {
  60. VIRTUAL_BUG_ON((phys_addr >> PAGE_SHIFT) > max_low_pfn);
  61. BUG_ON(slow_virt_to_phys((void *)x) != phys_addr);
  62. }
  63. return phys_addr;
  64. }
  65. EXPORT_SYMBOL(__phys_addr);
  66. #endif
  67. bool __virt_addr_valid(unsigned long x)
  68. {
  69. if (x < PAGE_OFFSET)
  70. return false;
  71. if (__vmalloc_start_set && is_vmalloc_addr((void *) x))
  72. return false;
  73. if (x >= FIXADDR_START)
  74. return false;
  75. return pfn_valid((x - PAGE_OFFSET) >> PAGE_SHIFT);
  76. }
  77. EXPORT_SYMBOL(__virt_addr_valid);
  78. #endif /* CONFIG_X86_64 */