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