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>
29 #ifndef EXPORTED_SYS_CALL_TABLE
30 #include <linux/sched.h>
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 int (*sys_killp)(int pid, int signal);
42 asmlinkage long (*sys_setgroupsp)(int gidsetsize, gid_t *grouplist);
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 */
48 extern void * sys_call_table[]; /* safer for other linuces */
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 */
54 static void ** sys_call_table; /* safer for other linuces */
57 extern struct file_system_type afs_file_system;
59 static long get_page_offset(void);
61 #if defined(AFS_LINUX24_ENV)
62 DECLARE_MUTEX(afs_global_lock);
64 struct semaphore afs_global_lock = MUTEX;
66 int afs_global_owner = 0;
67 unsigned long afs_linux_page_offset = 0; /* contains the PAGE_OFFSET value */
69 /* Since sys_ni_syscall is not exported, I need to cache it in order to restore
72 #ifdef AFS_SPARC64_LINUX20_ENV
73 static unsigned int afs_ni_syscall = 0;
75 static void* afs_ni_syscall = 0;
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);
84 #ifdef EXPORTED_SYS_CALL_TABLE
85 extern unsigned int sys_call_table32[];
87 static unsigned int *sys_call_table32;
90 asmlinkage int afs_syscall32(long syscall, long parm1, long parm2, long parm3,
91 long parm4, long parm5)
93 __asm__ __volatile__ ("
104 #ifdef AFS_IA64_LINUX20_ENV
107 afs_syscall_stub(int r0, int r1, long r2, long r3, long r4, long gp)
109 __asm__ __volatile__ ("
110 alloc r42 = ar.pfs, 8, 3, 6, 0
111 mov r41 = b0 /* save rp */
117 mov out5 = gp /* save gp */
121 addl r15=.fptr_afs_syscall-.L1,r3
129 br.call.sptk.many b0 = b6
133 mov gp = r48 /* restore gp */
136 data8 @fptr(afs_syscall)
141 afs_xsetgroups_stub(int r0, int r1, long r2, long r3, long r4, long gp)
143 __asm__ __volatile__ ("
144 alloc r42 = ar.pfs, 8, 3, 6, 0
145 mov r41 = b0 /* save rp */
151 mov out5 = gp /* save gp */
155 addl r15=.fptr_afs_xsetgroups - .L2,r3
163 br.call.sptk.many b0 = b6
167 mov gp = r48 /* restore gp */
169 .fptr_afs_xsetgroups:
170 data8 @fptr(afs_xsetgroups)
180 #endif /* AFS_IA64_LINUX20_ENV */
182 #ifdef AFS_LINUX24_ENV
183 asmlinkage int (*sys_setgroups32p)(int gidsetsize, __kernel_gid32_t *grouplist);
186 #ifdef AFS_SPARC64_LINUX20_ENV
187 #define POINTER2SYSCALL (unsigned int)(unsigned long)
188 #define SYSCALL2POINTER (void *)(long)
190 #define POINTER2SYSCALL (void *)
191 #define SYSCALL2POINTER (void *)
194 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
195 int __init afs_init(void)
197 int init_module(void)
200 #if defined(AFS_IA64_LINUX20_ENV)
201 unsigned long kernel_gp;
202 static struct fptr sys_kill, sys_settimeofday, sys_setgroups;
204 extern int afs_syscall();
205 extern long afs_xsetgroups();
206 #if defined(__NR_setgroups32)
207 extern int afs_xsetgroups32();
209 #ifdef AFS_SPARC64_LINUX20_ENV
210 extern int afs32_xsetgroups();
211 #if defined(__NR_setgroups32)
212 extern int afs32_xsetgroups32();
216 #ifndef EXPORTED_SYS_CALL_TABLE
218 unsigned long offset;
219 unsigned long datalen;
223 unsigned long mod_start;
224 unsigned long mod_end;
226 unsigned long sec_start;
227 unsigned long sec_end;
229 unsigned long sym_start;
230 unsigned long sym_end;
233 RWLOCK_INIT(&afs_xosi, "afs_xosi");
235 /* obtain PAGE_OFFSET value */
236 afs_linux_page_offset = get_page_offset();
238 #ifndef AFS_S390_LINUX22_ENV
239 if (afs_linux_page_offset == 0) {
240 /* couldn't obtain page offset so can't continue */
241 printf("afs: Unable to obtain PAGE_OFFSET. Exiting..");
245 #ifndef EXPORTED_SYS_CALL_TABLE
248 #ifdef EXPORTED_KALLSYMS_SYMBOL
253 ret=kallsyms_symbol_to_address("sys_call_table", &token, &mod_name,
254 &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
255 &sym_name, &sym_start, &sym_end);
256 if (ret && !strcmp(mod_name, "kernel"))
259 if (ret && sym_start) {
260 sys_call_table=sym_start;
263 #ifdef EXPORTED_KALLSYMS_ADDRESS
264 ret=kallsyms_address_to_symbol((unsigned long)&init_mm, &mod_name,
265 &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
266 &sym_name, &sym_start, &sym_end);
267 ptr=(unsigned long *)sec_start;
268 datalen=(sec_end-sec_start)/sizeof(unsigned long);
270 #if defined(AFS_IA64_LINUX20_ENV)
271 ptr = (unsigned long *) (&sys_close - 0x180000);
272 datalen=0x180000/sizeof(ptr);
274 ptr=(unsigned long *)&init_mm;
278 for (offset=0;offset <datalen;ptr++,offset++) {
279 #if defined(AFS_IA64_LINUX20_ENV)
280 unsigned long close_ip=(unsigned long) ((struct fptr *)&sys_close)->ip;
281 unsigned long chdir_ip=(unsigned long) ((struct fptr *)&sys_chdir)->ip;
282 unsigned long write_ip=(unsigned long) ((struct fptr *)&sys_write)->ip;
283 if (ptr[0] == close_ip &&
284 ptr[__NR_chdir - __NR_close] == chdir_ip &&
285 ptr[__NR_write - __NR_close] == write_ip) {
286 sys_call_table=(void *) &(ptr[ -1 * (__NR_close-1024)]);
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;
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;
305 #ifdef EXPORTED_KALLSYMS_ADDRESS
306 ret=kallsyms_address_to_symbol((unsigned long)sys_call_table, &mod_name,
307 &mod_start, &mod_end, &sec_name, &sec_start, &sec_end,
308 &sym_name, &sym_start, &sym_end);
309 if (ret && strcmp(sym_name, "sys_call_table"))
313 if (!sys_call_table) {
314 printf("Failed to find address of sys_call_table\n");
317 printf("Found sys_call_table at %x\n", sys_call_table);
318 # ifdef AFS_SPARC64_LINUX20_ENV
319 error cant support this yet.
321 #endif /* EXPORTED_SYS_CALL_TABLE */
323 /* Initialize pointers to kernel syscalls. */
324 #if defined(AFS_IA64_LINUX20_ENV)
325 kernel_gp = ((struct fptr *)printk)->gp;
327 sys_settimeofdayp = (void *) &sys_settimeofday;
328 sys_killp = (void *) &sys_kill;
330 ((struct fptr *)sys_settimeofdayp)->ip =
331 SYSCALL2POINTER sys_call_table[__NR_settimeofday - 1024];
332 ((struct fptr *)sys_settimeofdayp)->gp = kernel_gp;
334 ((struct fptr *)sys_killp)->ip =
335 SYSCALL2POINTER sys_call_table[__NR_kill - 1024];
336 ((struct fptr *)sys_killp)->gp = kernel_gp;
337 #else /* !AFS_IA64_LINUX20_ENV */
338 sys_settimeofdayp = SYSCALL2POINTER sys_call_table[__NR_settimeofday];
339 sys_killp = SYSCALL2POINTER sys_call_table[__NR_kill];
340 #endif /* AFS_IA64_LINUX20_ENV */
342 /* setup AFS entry point. */
344 #if defined(AFS_IA64_LINUX20_ENV)
345 SYSCALL2POINTER sys_call_table[__NR_afs_syscall - 1024]
347 SYSCALL2POINTER sys_call_table[__NR_afs_syscall]
350 printf("AFS syscall entry point already in use!\n");
354 #if defined(AFS_IA64_LINUX20_ENV)
355 afs_ni_syscall = sys_call_table[__NR_afs_syscall - 1024];
356 sys_call_table[__NR_afs_syscall - 1024] = POINTER2SYSCALL ((struct fptr *)afs_syscall_stub)->ip;
357 #else /* AFS_IA64_LINUX20_ENV */
358 afs_ni_syscall = sys_call_table[__NR_afs_syscall];
359 sys_call_table[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall;
360 # ifdef AFS_SPARC64_LINUX20_ENV
361 afs_ni_syscall32 = sys_call_table32[__NR_afs_syscall];
362 sys_call_table32[__NR_afs_syscall] = POINTER2SYSCALL afs_syscall32;
364 #endif /* AFS_IA64_LINUX20_ENV */
367 register_filesystem(&afs_file_system);
369 /* Intercept setgroups calls */
370 #if defined(AFS_IA64_LINUX20_ENV)
371 sys_setgroupsp = (void *) &sys_setgroups;
373 ((struct fptr *)sys_setgroupsp)->ip =
374 SYSCALL2POINTER sys_call_table[__NR_setgroups - 1024];
375 ((struct fptr *)sys_setgroupsp)->gp = kernel_gp;
377 sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *)afs_xsetgroups_stub)->ip;
378 #else /* AFS_IA64_LINUX20_ENV */
379 sys_setgroupsp = SYSCALL2POINTER sys_call_table[__NR_setgroups];
380 sys_call_table[__NR_setgroups] = POINTER2SYSCALL afs_xsetgroups;
381 # ifdef AFS_SPARC64_LINUX20_ENV
382 sys32_setgroupsp = SYSCALL2POINTER sys_call_table32[__NR_setgroups];
383 sys_call_table32[__NR_setgroups] = POINTER2SYSCALL afs32_xsetgroups;
385 # if defined(__NR_setgroups32)
386 sys_setgroups32p = SYSCALL2POINTER sys_call_table[__NR_setgroups32];
387 sys_call_table[__NR_setgroups32] = POINTER2SYSCALL afs_xsetgroups32;
388 # ifdef AFS_SPARC64_LINUX20_ENV
389 sys32_setgroups32p = SYSCALL2POINTER sys_call_table32[__NR_setgroups32];
390 sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL afs32_xsetgroups32;
393 #endif /* AFS_IA64_LINUX20_ENV */
400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
401 void __exit afs_cleanup(void)
403 void cleanup_module(void)
406 struct task_struct *t;
410 #if defined(AFS_IA64_LINUX20_ENV)
411 sys_call_table[__NR_setgroups - 1024] = POINTER2SYSCALL ((struct fptr *) sys_setgroupsp)->ip;
412 sys_call_table[__NR_afs_syscall - 1024] = afs_ni_syscall;
413 #else /* AFS_IA64_LINUX20_ENV */
414 sys_call_table[__NR_setgroups] = POINTER2SYSCALL sys_setgroupsp;
415 sys_call_table[__NR_afs_syscall] = afs_ni_syscall;
416 # ifdef AFS_SPARC64_LINUX20_ENV
417 sys_call_table32[__NR_setgroups] = POINTER2SYSCALL sys32_setgroupsp;
418 sys_call_table32[__NR_afs_syscall] = afs_ni_syscall32;
420 # if defined(__NR_setgroups32)
421 sys_call_table[__NR_setgroups32] = POINTER2SYSCALL sys_setgroups32p;
422 # ifdef AFS_SPARC64_LINUX20_ENV
423 sys_call_table32[__NR_setgroups32] = POINTER2SYSCALL sys32_setgroups32p;
426 #endif /* AFS_IA64_LINUX20_ENV */
427 unregister_filesystem(&afs_file_system);
429 osi_linux_free_inode_pages(); /* Invalidate all pages using AFS inodes. */
430 osi_linux_free_afs_memory();
435 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
436 module_init(afs_init);
437 module_exit(afs_cleanup);
441 static long get_page_offset(void)
443 #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)
446 struct task_struct *p, *q;
448 /* search backward thru the circular list */
449 #ifdef DEFINED_PREV_TASK
450 for(q = current; p = q; q = prev_task(p))
452 for(p = current; p; p = p->prev_task)
455 return p->addr_limit.seg;