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"
19 #include "afs/sysincludes.h"
20 #include "afsincludes.h"
21 #include "h/unistd.h" /* For syscall numbers. */
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>
30 #ifndef EXPORTED_SYS_CALL_TABLE
31 #include <linux/syscall.h>
36 #ifdef AFS_SPARC64_LINUX24_ENV
37 #define __NR_setgroups32 82 /* This number is not exported for some bizarre reason. */
40 asmlinkage int (*sys_settimeofdayp)(struct timeval *tv, struct timezone *tz);
41 asmlinkage long (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
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 */
47 extern void * sys_call_table[]; /* safer for other linuces */
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 */
53 static void ** sys_call_table; /* safer for other linuces */
56 extern struct file_system_type afs_file_system;
58 static long get_page_offset(void);
60 #if defined(AFS_LINUX24_ENV)
61 DECLARE_MUTEX(afs_global_lock);
63 struct semaphore afs_global_lock = MUTEX;
65 int afs_global_owner = 0;
66 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
68 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
71 #ifdef AFS_SPARC64_LINUX20_ENV
72 static unsigned int afs_ni_syscall = 0;
74 static void* afs_ni_syscall = 0;
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);
83 #ifdef EXPORTED_SYS_CALL_TABLE
84 extern unsigned int sys_call_table32[];
86 static unsigned int *sys_call_table32;
89 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
90 long parm4, long parm5)
92 __asm__ __volatile__ ("srl %o4, 0, %o4\n\t"
94 "call afs_syscall\n\t"
101 #ifdef AFS_IA64_LINUX20_ENV
104 afs_syscall_stub(int r0, int r1, long r2, long r3, long r4, long gp)
106 __asm__ __volatile__ ("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t"
107 "mov r41 = b0\n\t" /* save rp */
113 "mov out5 = gp\n\t" /* save gp */
115 ".L1: mov r3 = ip\n\t"
117 "addl r15=.fptr_afs_syscall-.L1,r3\n\t"
121 "ld8 r16=[r15],8\n\t"
125 "br.call.sptk.many b0 = b6\n\t"
127 "mov ar.pfs = r42\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)");
136 afs_xsetgroups_stub(int r0, int r1, long r2, long r3, long r4, long gp)
138 __asm__ __volatile__ ("alloc r42 = ar.pfs, 8, 3, 6, 0\n\t"
139 "mov r41 = b0\n\t" /* save rp */
145 "mov out5 = gp\n\t" /* save gp */
147 ".L2: mov r3 = ip\n\t"
149 "addl r15=.fptr_afs_xsetgroups - .L2,r3\n\t"
153 "ld8 r16=[r15],8\n\t"
157 "br.call.sptk.many b0 = b6\n\t"
159 "mov ar.pfs = r42\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)");
173 #endif /* AFS_IA64_LINUX20_ENV */
175 #ifdef AFS_LINUX24_ENV
176 asmlinkage int (*sys_setgroups32p)(int gidsetsize, __kernel_gid32_t *grouplist);
179 #ifdef AFS_SPARC64_LINUX20_ENV
180 #define POINTER2SYSCALL (unsigned int)(unsigned long)
181 #define SYSCALL2POINTER (void *)(long)
183 #define POINTER2SYSCALL (void *)
184 #define SYSCALL2POINTER (void *)
187 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
188 int __init afs_init(void)
190 int init_module(void)
193 #if defined(AFS_IA64_LINUX20_ENV)
194 unsigned long kernel_gp;
195 static struct fptr sys_settimeofday, sys_setgroups;
197 extern int afs_syscall();
198 extern long afs_xsetgroups();
199 #if defined(__NR_setgroups32)
200 extern int afs_xsetgroups32();
202 #ifdef AFS_SPARC64_LINUX20_ENV
203 extern int afs32_xsetgroups();
204 #if defined(__NR_setgroups32)
205 extern int afs32_xsetgroups32();
209 #ifndef EXPORTED_SYS_CALL_TABLE
211 unsigned long offset;
212 unsigned long datalen;
216 unsigned long mod_start;
217 unsigned long mod_end;
219 unsigned long sec_start;
220 unsigned long sec_end;
222 unsigned long sym_start;
223 unsigned long sym_end;
226 RWLOCK_INIT(&afs_xosi, "afs_xosi");
228 /* obtain PAGE_OFFSET value */
229 afs_linux_page_offset = get_page_offset();
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..");
238 #ifndef EXPORTED_SYS_CALL_TABLE
241 #ifdef EXPORTED_KALLSYMS_SYMBOL
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"))
252 if (ret && sym_start) {
253 sys_call_table=sym_start;
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);
263 #if defined(AFS_IA64_LINUX20_ENV)
264 ptr = (unsigned long *) (&sys_close - 0x180000);
265 datalen=0x180000/sizeof(ptr);
267 ptr=(unsigned long *)&init_mm;
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)]);
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;
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;
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"))
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 */
316 /* Initialize pointers to kernel syscalls. */
317 #if defined(AFS_IA64_LINUX20_ENV)
318 kernel_gp = ((struct fptr *)printk)->gp;
320 sys_settimeofdayp = (void *) &sys_settimeofday;
322 ((struct fptr *)sys_settimeofdayp)->ip =
323 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
324 ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
326 #else /* !AFS_IA64_LINUX20_ENV */
327 sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
328 #endif /* AFS_IA64_LINUX20_ENV */
330 /* setup AFS entry point. */
332 #if defined(AFS_IA64_LINUX20_ENV)
333 SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
335 SYSCALL2POINTER sys_call_table[__NR_afs_syscall]
338 printf("AFS syscall entry point already in use!\n");
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;
352 #endif /* AFS_IA64_LINUX20_ENV */
355 register_filesystem(&afs_file_system);
357 /* Intercept setgroups calls */
358 #if defined(AFS_IA64_LINUX20_ENV)
359 sys_setgroupsp = (void *) &sys_setgroups;
361 ((struct fptr *)sys_setgroupsp)->ip =
362 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
363 ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
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;
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;
381 #endif /* AFS_IA64_LINUX20_ENV */
388 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
389 void __exit afs_cleanup(void)
391 void cleanup_module(void)
394 struct task_struct *t;
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;
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;
414 #endif /* AFS_IA64_LINUX20_ENV */
415 unregister_filesystem(&afs_file_system);
417 osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
418 osi_linux_free_afs_memory();
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
424 module_init(afs_init);
425 module_exit(afs_cleanup);
429 static long get_page_offset(void)
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)
434 struct task_struct *p, *q;
436 /* search backward thru the circular list */
437 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
438 read_lock(&tasklist_lock);
440 /* search backward thru the circular list */
441 #ifdef DEFINED_PREV_TASK
442 for(q = current; p = q; q = prev_task(p)) {
444 for(p = current; p; p = p->prev_task) {
447 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
448 read_unlock(&tasklist_lock);
450 return p->addr_limit.seg;
454 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
455 read_unlock(&tasklist_lock);