1 #include <linux/version.h>
2 
3 #include <linux/mm.h>
4 #include <linux/module.h>
5 #include <linux/sched.h>
6 #include <linux/slab.h>
7 
8 #include <xen/platform-compat.h>
9 
10 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
11 static int system_state = 1;
12 EXPORT_SYMBOL(system_state);
13 #endif
14 
ctrl_alt_del(void)15 void ctrl_alt_del(void)
16 {
17 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27)
18 	kill_proc(1, SIGINT, 1); /* interrupt init */
19 #else
20 	kill_cad_pid(SIGINT, 1);
21 #endif
22 }
23 
24 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
strcspn(const char * s,const char * reject)25 size_t strcspn(const char *s, const char *reject)
26 {
27         const char *p;
28         const char *r;
29         size_t count = 0;
30 
31         for (p = s; *p != '\0'; ++p) {
32                 for (r = reject; *r != '\0'; ++r) {
33                         if (*p == *r)
34                                 return count;
35                 }
36                 ++count;
37         }
38 
39         return count;
40 }
41 EXPORT_SYMBOL(strcspn);
42 #endif
43 
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
45 /*
46  * Map a vmalloc()-space virtual address to the physical page frame number.
47  */
vmalloc_to_pfn(void * vmalloc_addr)48 unsigned long vmalloc_to_pfn(void * vmalloc_addr)
49 {
50         return page_to_pfn(vmalloc_to_page(vmalloc_addr));
51 }
52 EXPORT_SYMBOL(vmalloc_to_pfn);
53 #endif
54 
55 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,11)
wait_for_completion_timeout(struct completion * x,unsigned long timeout)56 unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout)
57 {
58         might_sleep();
59 
60         spin_lock_irq(&x->wait.lock);
61         if (!x->done) {
62                 DECLARE_WAITQUEUE(wait, current);
63 
64                 wait.flags |= WQ_FLAG_EXCLUSIVE;
65                 __add_wait_queue_tail(&x->wait, &wait);
66                 do {
67                         __set_current_state(TASK_UNINTERRUPTIBLE);
68                         spin_unlock_irq(&x->wait.lock);
69                         timeout = schedule_timeout(timeout);
70                         spin_lock_irq(&x->wait.lock);
71                         if (!timeout) {
72                                 __remove_wait_queue(&x->wait, &wait);
73                                 goto out;
74                         }
75                 } while (!x->done);
76                 __remove_wait_queue(&x->wait, &wait);
77         }
78         x->done--;
79 out:
80         spin_unlock_irq(&x->wait.lock);
81         return timeout;
82 }
83 EXPORT_SYMBOL(wait_for_completion_timeout);
84 #endif
85 
86 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,12)
87 /*
88     fake do_exit using complete_and_exit
89  */
90 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,10)
do_exit(long code)91 asmlinkage NORET_TYPE void do_exit(long code)
92 #else
93 fastcall NORET_TYPE void do_exit(long code)
94 #endif
95 {
96     complete_and_exit(NULL, code);
97 }
98 EXPORT_SYMBOL_GPL(do_exit);
99 #endif
100 
101 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
schedule_timeout_interruptible(signed long timeout)102 signed long schedule_timeout_interruptible(signed long timeout)
103 {
104 	__set_current_state(TASK_INTERRUPTIBLE);
105 	return schedule_timeout(timeout);
106 }
107 EXPORT_SYMBOL(schedule_timeout_interruptible);
108 #endif
109 
110 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,14)
111 /**
112  * kzalloc - allocate memory. The memory is set to zero.
113  * @size: how many bytes of memory are required.
114  * @flags: the type of memory to allocate.
115  */
kzalloc(size_t size,int flags)116 void *kzalloc(size_t size, int flags)
117 {
118 	void *ret = kmalloc(size, flags);
119 	if (ret)
120 		memset(ret, 0, size);
121 	return ret;
122 }
123 EXPORT_SYMBOL(kzalloc);
124 #endif
125 
126 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
127 /* Simplified asprintf. */
kasprintf(gfp_t gfp,const char * fmt,...)128 char *kasprintf(gfp_t gfp, const char *fmt, ...)
129 {
130 	va_list ap;
131 	unsigned int len;
132 	char *p, dummy[1];
133 
134 	va_start(ap, fmt);
135 	len = vsnprintf(dummy, 0, fmt, ap);
136 	va_end(ap);
137 
138 	p = kmalloc(len + 1, gfp);
139 	if (!p)
140 		return NULL;
141 	va_start(ap, fmt);
142 	vsprintf(p, fmt, ap);
143 	va_end(ap);
144 	return p;
145 }
146 EXPORT_SYMBOL(kasprintf);
147 #endif
148