2 * Copyright 2000, International Business Machines Corporation and others.
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
11 * Linux module support routines.
14 #include <afsconfig.h>
15 #include "afs/param.h"
20 #include "afs/sysincludes.h"
21 #include "afsincludes.h"
22 #include "h/unistd.h" /* For syscall numbers. */
25 #ifdef AFS_AMD64_LINUX20_ENV
26 #include "../asm/ia32_unistd.h"
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
32 #include <linux/init.h>
33 #include <linux/sched.h>
35 #if !defined(EXPORTED_SYS_CALL_TABLE) && defined(HAVE_KERNEL_LINUX_SYSCALL_H)
36 #include <linux/syscall.h>
39 #ifdef AFS_SPARC64_LINUX24_ENV
40 #define __NR_setgroups32 82 /* This number is not exported for some bizarre reason. */
43 asmlinkage int (*sys_settimeofdayp) (struct timeval * tv,
44 struct timezone * tz);
45 asmlinkage long (*sys_setgroupsp) (int gidsetsize, gid_t * grouplist);
47 #ifdef EXPORTED_SYS_CALL_TABLE
48 #ifdef AFS_SPARC64_LINUX20_ENV
49 extern unsigned int sys_call_table[]; /* changed to uint because SPARC64 has syscaltable of 32bit items */
51 extern void *sys_call_table[]; /* safer for other linuces */
53 #else /* EXPORTED_SYS_CALL_TABLE */
54 #ifdef AFS_SPARC64_LINUX20_ENV
55 static unsigned int *sys_call_table; /* changed to uint because SPARC64 has syscaltable of 32bit items */
57 static void **sys_call_table; /* safer for other linuces */
60 extern struct file_system_type afs_file_system;
62 static long get_page_offset(void);
64 #if defined(AFS_LINUX24_ENV)
65 DECLARE_MUTEX(afs_global_lock);
67 struct semaphore afs_global_lock = MUTEX;
69 int afs_global_owner = 0;
70 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
72 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
75 #ifdef AFS_SPARC64_LINUX20_ENV
76 static unsigned int afs_ni_syscall = 0;
78 static void *afs_ni_syscall = 0;
81 #ifdef AFS_AMD64_LINUX20_ENV
82 #ifdef EXPORTED_IA32_SYS_CALL_TABLE
83 extern void *ia32_sys_call_table[];
85 static void **ia32_sys_call_table;
88 static void *ia32_ni_syscall = 0;
89 asmlinkage long (*sys32_setgroupsp) (int gidsetsize, u16 * grouplist);
90 #if defined(__NR_ia32_setgroups32)
91 asmlinkage long (*sys32_setgroups32p) (int gidsetsize, gid_t * grouplist);
92 #endif /* __NR_ia32_setgroups32 */
93 #endif /* AFS_AMD64_LINUX20_ENV */
95 #ifdef AFS_SPARC64_LINUX20_ENV
96 static unsigned int afs_ni_syscall32 = 0;
97 asmlinkage int (*sys32_setgroupsp) (int gidsetsize,
98 __kernel_gid_t32 * grouplist);
99 #if defined(__NR_setgroups32)
100 asmlinkage int (*sys32_setgroups32p) (int gidsetsize,
101 __kernel_gid_t32 * grouplist);
102 #endif /* __NR_setgroups32 */
103 #ifdef EXPORTED_SYS_CALL_TABLE
104 extern unsigned int sys_call_table32[];
105 #else /* EXPORTED_SYS_CALL_TABLE */
106 static unsigned int *sys_call_table32;
107 #endif /* EXPORTED_SYS_CALL_TABLE */
110 afs_syscall32(long syscall, long parm1, long parm2, long parm3, long parm4,
113 __asm__ __volatile__("srl %o4, 0, %o4\n\t" "mov %o7, %i7\n\t"
114 "call afs_syscall\n\t" "srl %o5, 0, %o5\n\t"
117 #endif /* AFS_SPARC64_LINUX20_ENV */
119 #ifdef AFS_IA64_LINUX20_ENV
122 afs_syscall_stub(int r0, int r1, long r2, long r3, long r4, long gp)
124 __asm__ __volatile__("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t" "mov r41 = b0\n\t" /* save rp */
125 "mov out0 = in0\n\t" "mov out1 = in1\n\t" "mov out2 = in2\n\t" "mov out3 = in3\n\t" "mov out4 = in4\n\t" "mov out5 = gp\n\t" /* save gp */
126 ";;\n" ".L1: mov r3 = ip\n\t" ";;\n\t" "addl r15=.fptr_afs_syscall-.L1,r3\n\t" ";;\n\t" "ld8 r15=[r15]\n\t" ";;\n\t" "ld8 r16=[r15],8\n\t" ";;\n\t" "ld8 gp=[r15]\n\t" "mov b6=r16\n\t" "br.call.sptk.many b0 = b6\n\t" ";;\n\t" "mov ar.pfs = r42\n\t" "mov b0 = r41\n\t" "mov gp = r48\n\t" /* restore gp */
127 "br.ret.sptk.many b0\n" ".fptr_afs_syscall:\n\t"
128 "data8 @fptr(afs_syscall)");
132 afs_xsetgroups_stub(int r0, int r1, long r2, long r3, long r4, long gp)
134 __asm__ __volatile__("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t" "mov r41 = b0\n\t" /* save rp */
135 "mov out0 = in0\n\t" "mov out1 = in1\n\t" "mov out2 = in2\n\t" "mov out3 = in3\n\t" "mov out4 = in4\n\t" "mov out5 = gp\n\t" /* save gp */
136 ";;\n" ".L2: mov r3 = ip\n\t" ";;\n\t" "addl r15=.fptr_afs_xsetgroups - .L2,r3\n\t" ";;\n\t" "ld8 r15=[r15]\n\t" ";;\n\t" "ld8 r16=[r15],8\n\t" ";;\n\t" "ld8 gp=[r15]\n\t" "mov b6=r16\n\t" "br.call.sptk.many b0 = b6\n\t" ";;\n\t" "mov ar.pfs = r42\n\t" "mov b0 = r41\n\t" "mov gp = r48\n\t" /* restore gp */
137 "br.ret.sptk.many b0\n" ".fptr_afs_xsetgroups:\n\t"
138 "data8 @fptr(afs_xsetgroups)");
146 #endif /* AFS_IA64_LINUX20_ENV */
148 #ifdef AFS_LINUX24_ENV
149 asmlinkage int (*sys_setgroups32p) (int gidsetsize,
150 __kernel_gid32_t * grouplist);
151 #endif /* AFS_LINUX24_ENV */
153 #ifdef AFS_SPARC64_LINUX20_ENV
154 #define POINTER2SYSCALL (unsigned int)(unsigned long)
155 #define SYSCALL2POINTER (void *)(long)
157 #define POINTER2SYSCALL (void *)
158 #define SYSCALL2POINTER (void *)
161 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
169 #if defined(AFS_IA64_LINUX20_ENV)
170 unsigned long kernel_gp = 0;
171 static struct fptr sys_settimeofday, sys_setgroups;
173 extern int afs_syscall();
174 extern long afs_xsetgroups();
175 #if defined(__NR_setgroups32)
176 extern int afs_xsetgroups32();
177 #endif /* __NR_setgroups32 */
178 #if defined(AFS_SPARC64_LINUX20_ENV) || defined (AFS_AMD64_LINUX20_ENV)
179 extern int afs32_xsetgroups();
180 #if (defined(__NR_setgroups32) && defined(AFS_SPARC64_LINUX20_ENV))
181 extern int afs32_xsetgroups32();
183 #if (defined(__NR_ia32_setgroups32) && defined(AFS_AMD64_LINUX20_ENV))
184 extern int afs32_xsetgroups32();
186 #endif /* AFS_SPARC64_LINUX20_ENV || AFS_AMD64_LINUX20_ENV */
188 #if !defined(EXPORTED_SYS_CALL_TABLE) || (defined(AFS_AMD64_LINUX20_ENV) && !defined(EXPORTED_IA32_SYS_CALL_TABLE))
190 unsigned long offset=0;
191 unsigned long datalen=0;
193 unsigned long token=0;
195 unsigned long mod_start=0;
196 unsigned long mod_end=0;
198 unsigned long sec_start=0;
199 unsigned long sec_end=0;
201 unsigned long sym_start=0;
202 unsigned long sym_end=0;
203 #endif /* EXPORTED_SYS_CALL_TABLE */
205 RWLOCK_INIT(&afs_xosi, "afs_xosi");
207 /* obtain PAGE_OFFSET value */
208 afs_linux_page_offset = get_page_offset();
210 #ifndef AFS_S390_LINUX22_ENV
211 if (afs_linux_page_offset == 0) {
212 /* couldn't obtain page offset so can't continue */
213 printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
217 #ifndef EXPORTED_SYS_CALL_TABLE
220 #ifdef EXPORTED_KALLSYMS_SYMBOL
226 kallsyms_symbol_to_address("sys_call_table", &token, &mod_name,
227 &mod_start, &mod_end, &sec_name,
228 &sec_start, &sec_end, &sym_name,
229 &sym_start, &sym_end);
230 if (ret && !strcmp(mod_name, "kernel"))
233 if (ret && sym_start) {
234 sys_call_table = sym_start;
237 #ifdef EXPORTED_KALLSYMS_ADDRESS
239 kallsyms_address_to_symbol((unsigned long)&init_mm, &mod_name,
240 &mod_start, &mod_end, &sec_name,
241 &sec_start, &sec_end, &sym_name,
242 &sym_start, &sym_end);
243 ptr = (unsigned long *)sec_start;
244 datalen = (sec_end - sec_start) / sizeof(unsigned long);
246 #if defined(AFS_IA64_LINUX20_ENV)
247 ptr = (unsigned long *)(&sys_close - 0x180000);
248 datalen = 0x180000 / sizeof(ptr);
250 #if defined(AFS_AMD64_LINUX20_ENV)
251 ptr = (unsigned long *)&init_mm;
252 datalen = 0x360000 / sizeof(ptr);
254 ptr = (unsigned long *)&init_mm;
259 for (offset = 0; offset < datalen; ptr++, offset++) {
260 #if defined(AFS_IA64_LINUX20_ENV)
261 unsigned long close_ip =
262 (unsigned long)((struct fptr *)&sys_close)->ip;
263 unsigned long chdir_ip =
264 (unsigned long)((struct fptr *)&sys_chdir)->ip;
265 unsigned long write_ip =
266 (unsigned long)((struct fptr *)&sys_write)->ip;
267 if (ptr[0] == close_ip && ptr[__NR_chdir - __NR_close] == chdir_ip
268 && ptr[__NR_write - __NR_close] == write_ip) {
269 sys_call_table = (void *)&(ptr[-1 * (__NR_close - 1024)]);
273 #if defined(EXPORTED_SYS_WAIT4) && defined(EXPORTED_SYS_CLOSE)
274 if (ptr[0] == (unsigned long)&sys_close
275 && ptr[__NR_wait4 - __NR_close] == (unsigned long)&sys_wait4) {
276 sys_call_table = ptr - __NR_close;
280 #if defined(EXPORTED_SYS_CHDIR) && defined(EXPORTED_SYS_CLOSE)
281 if (ptr[0] == (unsigned long)&sys_close
282 && ptr[__NR_chdir - __NR_close] == (unsigned long)&sys_chdir) {
283 sys_call_table = ptr - __NR_close;
287 if (ptr[0] == (unsigned long)&sys_exit
288 && ptr[__NR_open - __NR_exit] == (unsigned long)&sys_open) {
289 sys_call_table = ptr - __NR_exit;
296 #ifdef EXPORTED_KALLSYMS_ADDRESS
298 kallsyms_address_to_symbol((unsigned long)sys_call_table, &mod_name,
299 &mod_start, &mod_end, &sec_name,
300 &sec_start, &sec_end, &sym_name,
301 &sym_start, &sym_end);
302 if (ret && strcmp(sym_name, "sys_call_table"))
306 if (!sys_call_table) {
307 printf("Failed to find address of sys_call_table\n");
310 printf("Found sys_call_table at %x\n", sys_call_table);
311 # ifdef AFS_SPARC64_LINUX20_ENV
312 error cant support this yet.
314 #endif /* EXPORTED_SYS_CALL_TABLE */
315 #ifdef AFS_AMD64_LINUX20_ENV
316 #ifndef EXPORTED_IA32_SYS_CALL_TABLE
317 ia32_sys_call_table = 0;
318 #ifdef EXPORTED_KALLSYMS_SYMBOL
324 kallsyms_symbol_to_address("ia32_sys_call_table", &token,
325 &mod_name, &mod_start, &mod_end,
326 &sec_name, &sec_start, &sec_end,
327 &sym_name, &sym_start, &sym_end);
328 if (ret && !strcmp(mod_name, "kernel"))
331 if (ret && sym_start) {
332 ia32_sys_call_table = sym_start;
334 #else /* EXPORTED_KALLSYMS_SYMBOL */
335 #ifdef EXPORTED_KALLSYMS_ADDRESS
337 kallsyms_address_to_symbol((unsigned long)&interruptible_sleep_on,
338 &mod_name, &mod_start, &mod_end, &sec_name,
339 &sec_start, &sec_end, &sym_name,
340 &sym_start, &sym_end);
341 ptr = (unsigned long *)sec_start;
342 datalen = (sec_end - sec_start) / sizeof(unsigned long);
343 #else /* EXPORTED_KALLSYMS_ADDRESS */
344 #if defined(AFS_AMD64_LINUX20_ENV)
345 ptr = (unsigned long *)&interruptible_sleep_on;
346 datalen = 0x180000 / sizeof(ptr);
347 #else /* AFS_AMD64_LINUX20_ENV */
348 ptr = (unsigned long *)&interruptible_sleep_on;
350 #endif /* AFS_AMD64_LINUX20_ENV */
351 #endif /* EXPORTED_KALLSYMS_ADDRESS */
352 for (offset = 0; offset < datalen; ptr++, offset++) {
353 if (ptr[0] == (unsigned long)&sys_exit
354 && ptr[__NR_ia32_open - __NR_ia32_exit] ==
355 (unsigned long)&sys_open) {
356 ia32_sys_call_table = ptr - __NR_ia32_exit;
360 #ifdef EXPORTED_KALLSYMS_ADDRESS
362 kallsyms_address_to_symbol((unsigned long)ia32_sys_call_table,
363 &mod_name, &mod_start, &mod_end, &sec_name,
364 &sec_start, &sec_end, &sym_name,
365 &sym_start, &sym_end);
366 if (ret && strcmp(sym_name, "ia32_sys_call_table"))
367 ia32_sys_call_table = 0;
368 #endif /* EXPORTED_KALLSYMS_ADDRESS */
369 #endif /* EXPORTED_KALLSYMS_SYMBOL */
370 if (!ia32_sys_call_table) {
371 printf("Warning: Failed to find address of ia32_sys_call_table\n");
373 printf("Found ia32_sys_call_table at %x\n", ia32_sys_call_table);
376 printf("Found ia32_sys_call_table at %x\n", ia32_sys_call_table);
377 #endif /* IA32_SYS_CALL_TABLE */
380 /* Initialize pointers to kernel syscalls. */
381 #if defined(AFS_IA64_LINUX20_ENV)
382 kernel_gp = ((struct fptr *)printk)->gp;
384 sys_settimeofdayp = (void *)&sys_settimeofday;
386 ((struct fptr *)sys_settimeofdayp)->ip =
387 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
388 ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
390 #else /* !AFS_IA64_LINUX20_ENV */
391 sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
392 #endif /* AFS_IA64_LINUX20_ENV */
394 /* setup AFS entry point. */
396 #if defined(AFS_IA64_LINUX20_ENV)
397 SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
399 SYSCALL2POINTER sys_call_table[__NR_afs_syscall]
402 printf("AFS syscall entry point already in use!\n");
405 #if defined(AFS_IA64_LINUX20_ENV)
406 afs_ni_syscall = sys_call_table[__NR_afs_syscall - 1024];
407 sys_call_table[__NR_afs_syscall - 1024] =
408 POINTER2SYSCALL((struct fptr *)afs_syscall_stub)->ip;
409 #else /* AFS_IA64_LINUX20_ENV */
410 afs_ni_syscall = sys_call_table[__NR_afs_syscall];
411 sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
412 # ifdef AFS_SPARC64_LINUX20_ENV
413 afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
414 sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
416 #endif /* AFS_IA64_LINUX20_ENV */
417 #ifdef AFS_AMD64_LINUX20_ENV
418 if (ia32_sys_call_table) {
419 ia32_ni_syscall = ia32_sys_call_table[__NR_ia32_afs_syscall];
420 ia32_sys_call_table[__NR_ia32_afs_syscall] =
421 POINTER2SYSCALL afs_syscall;
423 #endif /* AFS_S390_LINUX22_ENV */
426 register_filesystem(&afs_file_system);
428 /* Intercept setgroups calls */
429 #if defined(AFS_IA64_LINUX20_ENV)
430 sys_setgroupsp = (void *)&sys_setgroups;
432 ((struct fptr *)sys_setgroupsp)->ip =
433 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
434 ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
436 sys_call_table[__NR_setgroups - 1024] =
437 POINTER2SYSCALL((struct fptr *)afs_xsetgroups_stub)->ip;
438 #else /* AFS_IA64_LINUX20_ENV */
439 sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
440 sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
441 #ifdef AFS_SPARC64_LINUX20_ENV
442 sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
443 sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
444 #endif /* AFS_SPARC64_LINUX20_ENV */
445 #if defined(__NR_setgroups32)
446 sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
447 sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
448 #ifdef AFS_SPARC64_LINUX20_ENV
449 sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
450 sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
451 #endif /* AFS_SPARC64_LINUX20_ENV */
452 #endif /* __NR_setgroups32 */
453 #ifdef AFS_AMD64_LINUX20_ENV
454 if (ia32_sys_call_table) {
456 SYSCALL2POINTER ia32_sys_call_table[__NR_ia32_setgroups];
457 ia32_sys_call_table[__NR_ia32_setgroups] =
458 POINTER2SYSCALL afs32_xsetgroups;
459 #if defined(__NR_ia32_setgroups32)
461 SYSCALL2POINTER ia32_sys_call_table[__NR_ia32_setgroups32];
462 ia32_sys_call_table[__NR_ia32_setgroups32] =
463 POINTER2SYSCALL afs32_xsetgroups32;
464 #endif /* __NR_ia32_setgroups32 */
466 #endif /* AFS_AMD64_LINUX20_ENV */
467 #endif /* AFS_IA64_LINUX20_ENV */
474 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
482 struct task_struct *t;
486 #if defined(AFS_IA64_LINUX20_ENV)
487 sys_call_table[__NR_setgroups - 1024] =
488 POINTER2SYSCALL((struct fptr *)sys_setgroupsp)->ip;
489 sys_call_table[__NR_afs_syscall - 1024] = afs_ni_syscall;
490 #else /* AFS_IA64_LINUX20_ENV */
491 sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
492 sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
493 # ifdef AFS_SPARC64_LINUX20_ENV
494 sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
495 sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
497 # if defined(__NR_setgroups32)
498 sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
499 # ifdef AFS_SPARC64_LINUX20_ENV
500 sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
503 #endif /* AFS_IA64_LINUX20_ENV */
504 #ifdef AFS_AMD64_LINUX20_ENV
505 if (ia32_sys_call_table) {
506 ia32_sys_call_table[__NR_ia32_setgroups] =
507 POINTER2SYSCALL sys32_setgroupsp;
508 ia32_sys_call_table[__NR_ia32_afs_syscall] =
509 POINTER2SYSCALL ia32_ni_syscall;
510 # if defined(__NR_setgroups32)
511 ia32_sys_call_table[__NR_ia32_setgroups32] =
512 POINTER2SYSCALL sys32_setgroups32p;
516 unregister_filesystem(&afs_file_system);
518 osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
519 osi_linux_free_afs_memory();
524 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
525 module_init(afs_init);
526 module_exit(afs_cleanup);
531 get_page_offset(void)
533 #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)
536 struct task_struct *p, *q;
538 /* search backward thru the circular list */
539 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
540 read_lock(&tasklist_lock);
542 /* search backward thru the circular list */
543 #ifdef DEFINED_PREV_TASK
544 for (q = current; p = q; q = prev_task(p)) {
546 for (p = current; p; p = p->prev_task) {
549 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
550 read_unlock(&tasklist_lock);
552 return p->addr_limit.seg;
556 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
557 read_unlock(&tasklist_lock);