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