linux-ia64-no-asm-syscall-stubs-20020502
[openafs.git] / src / afs / LINUX / osi_module.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * Linux module support routines.
12  *
13  */
14 #include <afsconfig.h>
15 #include "../afs/param.h"
16
17 RCSID("$Header$");
18
19 #include "../afs/sysincludes.h"
20 #include "../afs/afsincludes.h"
21 #include "../h/unistd.h" /* For syscall numbers. */
22 #include "../h/mm.h"
23
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
27 #include <linux/init.h>
28 #endif
29
30
31
32 #ifdef AFS_SPARC64_LINUX24_ENV
33 #define __NR_setgroups32      82 /* This number is not exported for some bizarre reason. */
34 #endif
35
36 asmlinkage int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
37 #if !defined(AFS_ALPHA_LINUX20_ENV)
38 asmlinkage int (*sys_socketcallp)(int call, long *args);
39 #endif /* no socketcall on alpha */
40 asmlinkage int (*sys_killp)(int pid, int signal);
41 asmlinkage long (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
42
43 #ifdef AFS_SPARC64_LINUX20_ENV
44 extern unsigned int sys_call_table[];  /* changed to uint because SPARC64 has syscaltable of 32bit items */
45 #else
46 extern void * sys_call_table[]; /* safer for other linuces */
47 #endif
48 extern struct file_system_type afs_file_system;
49
50 static long get_page_offset(void);
51
52 #if defined(AFS_LINUX24_ENV)
53 DECLARE_MUTEX(afs_global_lock);
54 #else
55 struct semaphore afs_global_lock = MUTEX;
56 #endif
57 int afs_global_owner = 0;
58 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
59
60 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
61  * it.
62  */
63 #ifdef AFS_SPARC64_LINUX20_ENV
64 static unsigned int afs_ni_syscall = 0;
65 #else
66 static void* afs_ni_syscall = 0;
67 #endif
68  
69 #ifdef AFS_SPARC64_LINUX20_ENV
70 static unsigned int afs_ni_syscall32 = 0;
71 asmlinkage int (*sys32_setgroupsp)(int gidsetsize, __kernel_gid_t32 *grouplist);
72 #if defined(__NR_setgroups32)
73 asmlinkage int (*sys32_setgroups32p)(int gidsetsize, __kernel_gid_t32 *grouplist);
74 #endif
75 extern unsigned int sys_call_table32[];
76
77 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
78                              long parm4, long parm5)
79 {
80 __asm__ __volatile__ ("
81         srl %o4, 0, %o4
82         mov %o7, %i7
83         call afs_syscall
84         srl %o5, 0, %o5
85         ret
86         nop
87 ");
88 }
89 #endif
90
91 #ifdef AFS_IA64_LINUX20_ENV
92
93 asmlinkage long
94 afs_syscall_stub(int r0, int r1, long r2, long r3, long r4, long gp)
95 {
96 __asm__ __volatile__ ("
97         alloc r42 = ar.pfs, 8, 3, 6, 0
98         mov r41 = b0                    /* save rp */
99         mov out0 = in0
100         mov out1 = in1
101         mov out2 = in2
102         mov out3 = in3
103         mov out4 = in4
104         mov out5 = gp                   /* save gp */
105         ;;
106 .L1:    mov r3 = ip
107         ;;
108         addl r15=.fptr_afs_syscall-.L1,r3
109         ;;
110         ld8 r15=[r15]
111         ;;
112         ld8 r16=[r15],8
113         ;;
114         ld8 gp=[r15]
115         mov b6=r16
116         br.call.sptk.many b0 = b6
117         ;;
118         mov ar.pfs = r42
119         mov b0 = r41
120         mov gp = r48                    /* restore gp */
121         br.ret.sptk.many b0
122 .fptr_afs_syscall:
123         data8 @fptr(afs_syscall)
124 ");
125 }
126
127 asmlinkage long
128 afs_xsetgroups_stub(int r0, int r1, long r2, long r3, long r4, long gp)
129 {
130 __asm__ __volatile__ ("
131         alloc r42 = ar.pfs, 8, 3, 6, 0
132         mov r41 = b0                    /* save rp */
133         mov out0 = in0
134         mov out1 = in1
135         mov out2 = in2
136         mov out3 = in3
137         mov out4 = in4
138         mov out5 = gp                   /* save gp */
139         ;;
140 .L2:    mov r3 = ip
141         ;;
142         addl r15=.fptr_afs_xsetgroups - .L2,r3
143         ;;
144         ld8 r15=[r15]
145         ;;
146         ld8 r16=[r15],8
147         ;;
148         ld8 gp=[r15]
149         mov b6=r16
150         br.call.sptk.many b0 = b6
151         ;;
152         mov ar.pfs = r42
153         mov b0 = r41
154         mov gp = r48                    /* restore gp */
155         br.ret.sptk.many b0
156 .fptr_afs_xsetgroups:
157         data8 @fptr(afs_xsetgroups)
158 ");
159 }
160
161 struct fptr
162 {
163         void *ip;
164         unsigned long gp;
165 };
166
167 #endif /* AFS_IA64_LINUX20_ENV */
168
169 #ifdef AFS_LINUX24_ENV
170 asmlinkage int (*sys_setgroups32p)(int gidsetsize, __kernel_gid32_t *grouplist);
171 #endif 
172
173 #ifdef AFS_SPARC64_LINUX20_ENV
174 #define POINTER2SYSCALL (unsigned int)(unsigned long)
175 #define SYSCALL2POINTER (void *)(long)
176 #else
177 #define POINTER2SYSCALL (void *)
178 #define SYSCALL2POINTER (void *)
179 #endif
180
181 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
182 int __init afs_init(void)
183 #else
184 int init_module(void)
185 #endif
186 {
187 #if defined(AFS_IA64_LINUX20_ENV)
188     unsigned long kernel_gp;
189     static struct fptr sys_kill, sys_settimeofday, sys_setgroups;
190 #endif
191     extern int afs_syscall();
192     extern long afs_xsetgroups();
193 #if defined(__NR_setgroups32)
194     extern int afs_xsetgroups32();
195 #endif
196 #ifdef AFS_SPARC64_LINUX20_ENV
197     extern int afs32_xsetgroups();
198 #if defined(__NR_setgroups32)
199     extern int afs32_xsetgroups32();
200 #endif
201 #endif
202
203
204
205     /* obtain PAGE_OFFSET value */
206     afs_linux_page_offset = get_page_offset();
207
208 #ifndef AFS_S390_LINUX22_ENV
209     if (afs_linux_page_offset == 0) {
210         /* couldn't obtain page offset so can't continue */
211         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
212         return -EIO;
213     }
214 #endif
215
216     /* Initialize pointers to kernel syscalls. */
217 #if defined(AFS_IA64_LINUX20_ENV)
218     kernel_gp = ((struct fptr *)printk)->gp;
219
220     sys_settimeofdayp = (void *) &sys_settimeofday;
221     sys_killp = (void *) &sys_kill;
222
223     ((struct fptr *)sys_settimeofdayp)->ip =
224                 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
225     ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
226     
227     ((struct fptr *)sys_killp)->ip =
228                 SYSCALL2POINTER sys_call_table[__NR_kill - 1024];
229     ((struct fptr *)sys_killp)->gp = kernel_gp;
230 #else /* !AFS_IA64_LINUX20_ENV */
231     sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
232 #ifdef __NR_socketcall
233     sys_socketcallp = SYSCALL2POINTER sys_call_table[__NR_socketcall];
234 #endif /* no socketcall on alpha */
235     sys_killp = SYSCALL2POINTER sys_call_table[__NR_kill];
236 #endif /* AFS_IA64_LINUX20_ENV */
237
238     /* setup AFS entry point. */
239     if (
240 #if defined(AFS_IA64_LINUX20_ENV)
241         SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
242 #else
243         SYSCALL2POINTER sys_call_table[__NR_afs_syscall] 
244 #endif
245         == afs_syscall) {
246         printf("AFS syscall entry point already in use!\n");
247         return -EBUSY;
248     }
249
250
251 #if defined(AFS_IA64_LINUX20_ENV)
252     afs_ni_syscall = sys_call_table[__NR_afs_syscall - 1024];
253     sys_call_table[__NR_afs_syscall - 1024] = POINTER2SYSCALL ((struct fptr *)afs_syscall_stub)->ip;
254 #else /* AFS_IA64_LINUX20_ENV */
255     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
256     sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
257 # ifdef AFS_SPARC64_LINUX20_ENV
258     afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
259     sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
260 # endif
261 #endif /* AFS_IA64_LINUX20_ENV */
262
263     osi_Init();
264     register_filesystem(&afs_file_system);
265
266     /* Intercept setgroups calls */
267 #if defined(AFS_IA64_LINUX20_ENV)
268     sys_setgroupsp = (void *) &sys_setgroups;
269
270     ((struct fptr *)sys_setgroupsp)->ip =
271                 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
272     ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
273
274     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *)afs_xsetgroups_stub)->ip;
275 #else /* AFS_IA64_LINUX20_ENV */
276     sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
277     sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
278 # ifdef AFS_SPARC64_LINUX20_ENV
279     sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
280     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
281 # endif
282 # if defined(__NR_setgroups32)
283     sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
284     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
285 # ifdef AFS_SPARC64_LINUX20_ENV
286     sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
287     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
288 # endif
289 # endif
290 #endif /* AFS_IA64_LINUX20_ENV */
291
292     osi_sysctl_init();
293
294     return 0;
295 }
296
297 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
298 void __exit afs_cleanup(void)
299 #else
300 void cleanup_module(void)
301 #endif
302 {
303     struct task_struct *t;
304
305     osi_sysctl_clean();
306
307 #if defined(AFS_IA64_LINUX20_ENV)
308     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *) sys_setgroupsp)->ip;
309     sys_call_table[__NR_afs_syscall - 1024] = afs_ni_syscall;
310 #else /* AFS_IA64_LINUX20_ENV */
311     sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
312     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
313 # ifdef AFS_SPARC64_LINUX20_ENV
314     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
315     sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
316 # endif
317 # if defined(__NR_setgroups32)
318     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
319 # ifdef AFS_SPARC64_LINUX20_ENV
320     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
321 # endif
322 # endif
323 #endif /* AFS_IA64_LINUX20_ENV */
324     unregister_filesystem(&afs_file_system);
325
326     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
327     osi_linux_free_afs_memory();
328
329     return;
330 }
331
332 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
333 module_init(afs_init);
334 module_exit(afs_cleanup);
335 #endif
336
337
338 static long get_page_offset(void)
339 {
340 #if defined(AFS_PPC_LINUX22_ENV) || defined(AFS_SPARC64_LINUX20_ENV) || defined(AFS_SPARC_LINUX20_ENV) || defined(AFS_ALPHA_LINUX20_ENV) || defined(AFS_S390_LINUX22_ENV) || defined(AFS_IA64_LINUX20_ENV) || defined(AFS_PARISC_LINUX24_ENV)
341     return PAGE_OFFSET;
342 #else
343     struct task_struct *p;
344
345     /* search backward thru the circular list */
346     for(p = current; p; p = p->prev_task)
347         if (p->pid == 1)
348             return p->addr_limit.seg;
349
350     return 0;
351 #endif
352 }