linux-look-for-syscall-h-in-kernel-20030519
[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 "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 #include <linux/sched.h>
29 #endif
30 #if !defined(EXPORTED_SYS_CALL_TABLE) && defined(HAVE_KERNEL_LINUX_SYSCALL_H)
31 #include <linux/syscall.h>
32 #endif
33
34
35 #ifdef AFS_SPARC64_LINUX24_ENV
36 #define __NR_setgroups32      82 /* This number is not exported for some bizarre reason. */
37 #endif
38
39 asmlinkage int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
40 asmlinkage long (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
41
42 #ifdef EXPORTED_SYS_CALL_TABLE
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 #else /* EXPORTED_SYS_CALL_TABLE */
49 #ifdef AFS_SPARC64_LINUX20_ENV
50 static unsigned int *sys_call_table;  /* changed to uint because SPARC64 has syscaltable of 32bit items */
51 #else
52 static void ** sys_call_table; /* safer for other linuces */
53 #endif
54 #endif
55 extern struct file_system_type afs_file_system;
56
57 static long get_page_offset(void);
58
59 #if defined(AFS_LINUX24_ENV)
60 DECLARE_MUTEX(afs_global_lock);
61 #else
62 struct semaphore afs_global_lock = MUTEX;
63 #endif
64 int afs_global_owner = 0;
65 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
66
67 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
68  * it.
69  */
70 #ifdef AFS_SPARC64_LINUX20_ENV
71 static unsigned int afs_ni_syscall = 0;
72 #else
73 static void* afs_ni_syscall = 0;
74 #endif
75  
76 #ifdef AFS_SPARC64_LINUX20_ENV
77 static unsigned int afs_ni_syscall32 = 0;
78 asmlinkage int (*sys32_setgroupsp)(int gidsetsize, __kernel_gid_t32 *grouplist);
79 #if defined(__NR_setgroups32)
80 asmlinkage int (*sys32_setgroups32p)(int gidsetsize, __kernel_gid_t32 *grouplist);
81 #endif
82 #ifdef EXPORTED_SYS_CALL_TABLE
83 extern unsigned int sys_call_table32[];
84 #else
85 static unsigned int *sys_call_table32;
86 #endif
87
88 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
89                              long parm4, long parm5)
90 {
91 __asm__ __volatile__ ("srl %o4, 0, %o4\n\t"
92                       "mov %o7, %i7\n\t"
93                       "call afs_syscall\n\t"
94                       "srl %o5, 0, %o5\n\t"
95                       "ret\n\t"
96                       "nop");
97 }
98 #endif
99
100 #ifdef AFS_IA64_LINUX20_ENV
101
102 asmlinkage long
103 afs_syscall_stub(int r0, int r1, long r2, long r3, long r4, long gp)
104 {
105 __asm__ __volatile__ ("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t"
106                       "mov r41 = b0\n\t"                /* save rp */
107                       "mov out0 = in0\n\t"
108                       "mov out1 = in1\n\t"
109                       "mov out2 = in2\n\t"
110                       "mov out3 = in3\n\t"
111                       "mov out4 = in4\n\t"
112                       "mov out5 = gp\n\t"                       /* save gp */
113                       ";;\n"
114                       ".L1:    mov r3 = ip\n\t"
115                       ";;\n\t"
116                       "addl r15=.fptr_afs_syscall-.L1,r3\n\t"
117                       ";;\n\t"
118                       "ld8 r15=[r15]\n\t"
119                       ";;\n\t"
120                       "ld8 r16=[r15],8\n\t"
121                       ";;\n\t"
122                       "ld8 gp=[r15]\n\t"
123                       "mov b6=r16\n\t"
124                       "br.call.sptk.many b0 = b6\n\t"
125                       ";;\n\t"
126                       "mov ar.pfs = r42\n\t"
127                       "mov b0 = r41\n\t"
128                       "mov gp = r48\n\t"                /* restore gp */
129                       "br.ret.sptk.many b0\n"
130                       ".fptr_afs_syscall:\n\t"
131                       "data8 @fptr(afs_syscall)");
132 }
133
134 asmlinkage long
135 afs_xsetgroups_stub(int r0, int r1, long r2, long r3, long r4, long gp)
136 {
137 __asm__ __volatile__ ("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t"
138                       "mov r41 = b0\n\t"                /* save rp */
139                       "mov out0 = in0\n\t"
140                       "mov out1 = in1\n\t"
141                       "mov out2 = in2\n\t"
142                       "mov out3 = in3\n\t"
143                       "mov out4 = in4\n\t"
144                       "mov out5 = gp\n\t"                       /* save gp */
145                       ";;\n"
146                       ".L2:    mov r3 = ip\n\t"
147                       ";;\n\t"
148                       "addl r15=.fptr_afs_xsetgroups - .L2,r3\n\t"
149                       ";;\n\t"
150                       "ld8 r15=[r15]\n\t"
151                       ";;\n\t"
152                       "ld8 r16=[r15],8\n\t"
153                       ";;\n\t"
154                       "ld8 gp=[r15]\n\t"
155                       "mov b6=r16\n\t"
156                       "br.call.sptk.many b0 = b6\n\t"
157                       ";;\n\t"
158                       "mov ar.pfs = r42\n\t"
159                       "mov b0 = r41\n\t"
160                       "mov gp = r48\n\t"                /* restore gp */
161                       "br.ret.sptk.many b0\n"
162                       ".fptr_afs_xsetgroups:\n\t"
163                       "data8 @fptr(afs_xsetgroups)");
164 }
165
166 struct fptr
167 {
168         void *ip;
169         unsigned long gp;
170 };
171
172 #endif /* AFS_IA64_LINUX20_ENV */
173
174 #ifdef AFS_LINUX24_ENV
175 asmlinkage int (*sys_setgroups32p)(int gidsetsize, __kernel_gid32_t *grouplist);
176 #endif 
177
178 #ifdef AFS_SPARC64_LINUX20_ENV
179 #define POINTER2SYSCALL (unsigned int)(unsigned long)
180 #define SYSCALL2POINTER (void *)(long)
181 #else
182 #define POINTER2SYSCALL (void *)
183 #define SYSCALL2POINTER (void *)
184 #endif
185
186 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
187 int __init afs_init(void)
188 #else
189 int init_module(void)
190 #endif
191 {
192 #if defined(AFS_IA64_LINUX20_ENV)
193     unsigned long kernel_gp;
194     static struct fptr sys_settimeofday, sys_setgroups;
195 #endif
196     extern int afs_syscall();
197     extern long afs_xsetgroups();
198 #if defined(__NR_setgroups32)
199     extern int afs_xsetgroups32();
200 #endif
201 #ifdef AFS_SPARC64_LINUX20_ENV
202     extern int afs32_xsetgroups();
203 #if defined(__NR_setgroups32)
204     extern int afs32_xsetgroups32();
205 #endif
206 #endif
207
208 #ifndef EXPORTED_SYS_CALL_TABLE
209     unsigned long *ptr;
210     unsigned long offset;
211     unsigned long datalen;
212     int ret;
213     unsigned long token;
214     char      *mod_name;
215     unsigned long    mod_start;
216     unsigned long    mod_end;
217     char      *sec_name;
218     unsigned long    sec_start;
219     unsigned long    sec_end;
220     char      *sym_name;
221     unsigned long    sym_start;
222     unsigned long    sym_end;
223 #endif
224
225     RWLOCK_INIT(&afs_xosi, "afs_xosi");
226
227     /* obtain PAGE_OFFSET value */
228     afs_linux_page_offset = get_page_offset();
229
230 #ifndef AFS_S390_LINUX22_ENV
231     if (afs_linux_page_offset == 0) {
232         /* couldn't obtain page offset so can't continue */
233         printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
234         return -EIO;
235     }
236 #endif
237 #ifndef EXPORTED_SYS_CALL_TABLE
238     sys_call_table=0;
239
240 #ifdef EXPORTED_KALLSYMS_SYMBOL
241     ret=1;
242     token=0;
243     while (ret) {
244       sym_start=0;
245       ret=kallsyms_symbol_to_address("sys_call_table", &token, &mod_name,
246                                      &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
247                                      &sym_name, &sym_start, &sym_end);
248       if (ret && !strcmp(mod_name, "kernel"))
249         break;
250     }
251     if (ret && sym_start) {
252       sys_call_table=sym_start;
253     }
254 #else
255 #ifdef EXPORTED_KALLSYMS_ADDRESS
256     ret=kallsyms_address_to_symbol((unsigned long)&init_mm, &mod_name,
257                                    &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
258                                    &sym_name, &sym_start, &sym_end);
259     ptr=(unsigned long *)sec_start;
260     datalen=(sec_end-sec_start)/sizeof(unsigned long);
261 #else
262 #if defined(AFS_IA64_LINUX20_ENV)
263     ptr = (unsigned long *) (&sys_close - 0x180000);
264     datalen=0x180000/sizeof(ptr);
265 #else
266     ptr=(unsigned long *)&init_mm;
267     datalen=16384;
268 #endif
269 #endif
270     for (offset=0;offset <datalen;ptr++,offset++) {
271 #if defined(AFS_IA64_LINUX20_ENV)
272         unsigned long close_ip=(unsigned long) ((struct fptr *)&sys_close)->ip;
273         unsigned long chdir_ip=(unsigned long) ((struct fptr *)&sys_chdir)->ip;
274         unsigned long write_ip=(unsigned long) ((struct fptr *)&sys_write)->ip;
275         if (ptr[0] == close_ip &&
276             ptr[__NR_chdir - __NR_close] == chdir_ip &&
277             ptr[__NR_write - __NR_close] == write_ip) {
278             sys_call_table=(void *) &(ptr[ -1 * (__NR_close-1024)]);
279             break;
280         }
281 #else
282 #if defined(EXPORTED_SYS_WAIT4) && defined(EXPORTED_SYS_CLOSE)
283         if (ptr[0] == (unsigned long)&sys_close &&
284             ptr[__NR_wait4 - __NR_close] == (unsigned long)&sys_wait4) {
285             sys_call_table=ptr - __NR_close;
286             break;
287         }
288 #else
289 #if defined(EXPORTED_SYS_CHDIR) && defined(EXPORTED_SYS_CLOSE)
290         if (ptr[0] == (unsigned long)&sys_close &&
291             ptr[__NR_chdir - __NR_close] == (unsigned long)&sys_chdir) {
292             sys_call_table=ptr - __NR_close;
293             break;
294         }
295 #else
296         if (ptr[0] == (unsigned long)&sys_exit &&
297             ptr[__NR_open - __NR_exit] == (unsigned long)&sys_open) {
298             sys_call_table=ptr - __NR_exit;
299             break;
300         }
301 #endif
302 #endif
303 #endif
304     }
305 #ifdef EXPORTED_KALLSYMS_ADDRESS
306     ret=kallsyms_address_to_symbol((unsigned long)sys_call_table, &mod_name,
307                                    &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
308                                    &sym_name, &sym_start, &sym_end);
309     if (ret && strcmp(sym_name, "sys_call_table"))
310       sys_call_table=0;
311 #endif
312 #endif
313     if (!sys_call_table) {
314       printf("Failed to find address of sys_call_table\n");
315       return -EIO;
316     }
317     printf("Found sys_call_table at %x\n", sys_call_table);
318 # ifdef AFS_SPARC64_LINUX20_ENV
319     error cant support this yet.
320 #endif
321 #endif /* EXPORTED_SYS_CALL_TABLE */
322       
323     /* Initialize pointers to kernel syscalls. */
324 #if defined(AFS_IA64_LINUX20_ENV)
325     kernel_gp = ((struct fptr *)printk)->gp;
326
327     sys_settimeofdayp = (void *) &sys_settimeofday;
328
329     ((struct fptr *)sys_settimeofdayp)->ip =
330                 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
331     ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
332     
333 #else /* !AFS_IA64_LINUX20_ENV */
334     sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
335 #endif /* AFS_IA64_LINUX20_ENV */
336
337     /* setup AFS entry point. */
338     if (
339 #if defined(AFS_IA64_LINUX20_ENV)
340         SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
341 #else
342         SYSCALL2POINTER sys_call_table[__NR_afs_syscall] 
343 #endif
344         == afs_syscall) {
345         printf("AFS syscall entry point already in use!\n");
346         return -EBUSY;
347     }
348
349 #if defined(AFS_IA64_LINUX20_ENV)
350     afs_ni_syscall = sys_call_table[__NR_afs_syscall - 1024];
351     sys_call_table[__NR_afs_syscall - 1024] = POINTER2SYSCALL ((struct fptr *)afs_syscall_stub)->ip;
352 #else /* AFS_IA64_LINUX20_ENV */
353     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
354     sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
355 # ifdef AFS_SPARC64_LINUX20_ENV
356     afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
357     sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
358 # endif
359 #endif /* AFS_IA64_LINUX20_ENV */
360
361     osi_Init();
362     register_filesystem(&afs_file_system);
363
364     /* Intercept setgroups calls */
365 #if defined(AFS_IA64_LINUX20_ENV)
366     sys_setgroupsp = (void *) &sys_setgroups;
367
368     ((struct fptr *)sys_setgroupsp)->ip =
369                 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
370     ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
371
372     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *)afs_xsetgroups_stub)->ip;
373 #else /* AFS_IA64_LINUX20_ENV */
374     sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
375     sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
376 # ifdef AFS_SPARC64_LINUX20_ENV
377     sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
378     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
379 # endif
380 # if defined(__NR_setgroups32)
381     sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
382     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
383 # ifdef AFS_SPARC64_LINUX20_ENV
384     sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
385     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
386 # endif
387 # endif
388 #endif /* AFS_IA64_LINUX20_ENV */
389
390     osi_sysctl_init();
391
392     return 0;
393 }
394
395 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
396 void __exit afs_cleanup(void)
397 #else
398 void cleanup_module(void)
399 #endif
400 {
401     struct task_struct *t;
402
403     osi_sysctl_clean();
404
405 #if defined(AFS_IA64_LINUX20_ENV)
406     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *) sys_setgroupsp)->ip;
407     sys_call_table[__NR_afs_syscall - 1024] = afs_ni_syscall;
408 #else /* AFS_IA64_LINUX20_ENV */
409     sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
410     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
411 # ifdef AFS_SPARC64_LINUX20_ENV
412     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
413     sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
414 # endif
415 # if defined(__NR_setgroups32)
416     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
417 # ifdef AFS_SPARC64_LINUX20_ENV
418     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
419 # endif
420 # endif
421 #endif /* AFS_IA64_LINUX20_ENV */
422     unregister_filesystem(&afs_file_system);
423
424     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
425     osi_linux_free_afs_memory();
426
427     return;
428 }
429
430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
431 module_init(afs_init);
432 module_exit(afs_cleanup);
433 #endif
434
435
436 static long get_page_offset(void)
437 {
438 #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) || defined(AFS_AMD64_LINUX20_ENV)
439     return PAGE_OFFSET;
440 #else
441     struct task_struct *p, *q;
442
443     /* search backward thru the circular list */
444 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
445     read_lock(&tasklist_lock);
446 #endif
447     /* search backward thru the circular list */
448 #ifdef DEFINED_PREV_TASK
449     for(q = current; p = q; q = prev_task(p)) {
450 #else
451     for(p = current; p; p = p->prev_task) {
452 #endif
453             if (p->pid == 1) {
454 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
455                     read_unlock(&tasklist_lock);
456 #endif
457                     return p->addr_limit.seg;
458             }
459     }
460   
461 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
462     read_unlock(&tasklist_lock);
463 #endif
464   return 0;
465 #endif
466 }