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