amd64-linux-port-20030428
[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_CHDIR) && defined(EXPORTED_SYS_CLOSE)
284         if (ptr[0] == (unsigned long)&sys_close &&
285             ptr[__NR_chdir - __NR_close] == (unsigned long)&sys_chdir) {
286             sys_call_table=ptr - __NR_close;
287             break;
288         }
289 #else
290         if (ptr[0] == (unsigned long)&sys_exit &&
291             ptr[__NR_open - __NR_exit] == (unsigned long)&sys_open) {
292             sys_call_table=ptr - __NR_exit;
293             break;
294         }
295 #endif
296 #endif
297     }
298 #ifdef EXPORTED_KALLSYMS_ADDRESS
299     ret=kallsyms_address_to_symbol((unsigned long)sys_call_table, &mod_name,
300                                    &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
301                                    &sym_name, &sym_start, &sym_end);
302     if (ret && strcmp(sym_name, "sys_call_table"))
303       sys_call_table=0;
304 #endif
305 #endif
306     if (!sys_call_table) {
307       printf("Failed to find address of sys_call_table\n");
308       return -EIO;
309     }
310     printf("Found sys_call_table at %x\n", sys_call_table);
311 # ifdef AFS_SPARC64_LINUX20_ENV
312     error cant support this yet.
313 #endif
314 #endif /* EXPORTED_SYS_CALL_TABLE */
315       
316     /* Initialize pointers to kernel syscalls. */
317 #if defined(AFS_IA64_LINUX20_ENV)
318     kernel_gp = ((struct fptr *)printk)->gp;
319
320     sys_settimeofdayp = (void *) &sys_settimeofday;
321
322     ((struct fptr *)sys_settimeofdayp)->ip =
323                 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
324     ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
325     
326 #else /* !AFS_IA64_LINUX20_ENV */
327     sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
328 #endif /* AFS_IA64_LINUX20_ENV */
329
330     /* setup AFS entry point. */
331     if (
332 #if defined(AFS_IA64_LINUX20_ENV)
333         SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
334 #else
335         SYSCALL2POINTER sys_call_table[__NR_afs_syscall] 
336 #endif
337         == afs_syscall) {
338         printf("AFS syscall entry point already in use!\n");
339         return -EBUSY;
340     }
341
342 #if defined(AFS_IA64_LINUX20_ENV)
343     afs_ni_syscall = sys_call_table[__NR_afs_syscall - 1024];
344     sys_call_table[__NR_afs_syscall - 1024] = POINTER2SYSCALL ((struct fptr *)afs_syscall_stub)->ip;
345 #else /* AFS_IA64_LINUX20_ENV */
346     afs_ni_syscall = sys_call_table[__NR_afs_syscall];
347     sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
348 # ifdef AFS_SPARC64_LINUX20_ENV
349     afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
350     sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
351 # endif
352 #endif /* AFS_IA64_LINUX20_ENV */
353
354     osi_Init();
355     register_filesystem(&afs_file_system);
356
357     /* Intercept setgroups calls */
358 #if defined(AFS_IA64_LINUX20_ENV)
359     sys_setgroupsp = (void *) &sys_setgroups;
360
361     ((struct fptr *)sys_setgroupsp)->ip =
362                 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
363     ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
364
365     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *)afs_xsetgroups_stub)->ip;
366 #else /* AFS_IA64_LINUX20_ENV */
367     sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
368     sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
369 # ifdef AFS_SPARC64_LINUX20_ENV
370     sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
371     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
372 # endif
373 # if defined(__NR_setgroups32)
374     sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
375     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
376 # ifdef AFS_SPARC64_LINUX20_ENV
377     sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
378     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
379 # endif
380 # endif
381 #endif /* AFS_IA64_LINUX20_ENV */
382
383     osi_sysctl_init();
384
385     return 0;
386 }
387
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
389 void __exit afs_cleanup(void)
390 #else
391 void cleanup_module(void)
392 #endif
393 {
394     struct task_struct *t;
395
396     osi_sysctl_clean();
397
398 #if defined(AFS_IA64_LINUX20_ENV)
399     sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *) sys_setgroupsp)->ip;
400     sys_call_table[__NR_afs_syscall - 1024] = afs_ni_syscall;
401 #else /* AFS_IA64_LINUX20_ENV */
402     sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
403     sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
404 # ifdef AFS_SPARC64_LINUX20_ENV
405     sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
406     sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
407 # endif
408 # if defined(__NR_setgroups32)
409     sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
410 # ifdef AFS_SPARC64_LINUX20_ENV
411     sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
412 # endif
413 # endif
414 #endif /* AFS_IA64_LINUX20_ENV */
415     unregister_filesystem(&afs_file_system);
416
417     osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
418     osi_linux_free_afs_memory();
419
420     return;
421 }
422
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
424 module_init(afs_init);
425 module_exit(afs_cleanup);
426 #endif
427
428
429 static long get_page_offset(void)
430 {
431 #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)
432     return PAGE_OFFSET;
433 #else
434     struct task_struct *p, *q;
435
436     /* search backward thru the circular list */
437 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
438     read_lock(&tasklist_lock);
439 #endif
440     /* search backward thru the circular list */
441 #ifdef DEFINED_PREV_TASK
442     for(q = current; p = q; q = prev_task(p)) {
443 #else
444     for(p = current; p; p = p->prev_task) {
445 #endif
446             if (p->pid == 1) {
447 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
448                     read_unlock(&tasklist_lock);
449 #endif
450                     return p->addr_limit.seg;
451             }
452     }
453   
454 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
455     read_unlock(&tasklist_lock);
456 #endif
457   return 0;
458 #endif
459 }