public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines
       [not found] <200409131715.27584.anatolya@qlusters.com>
@ 2004-09-13 14:17 ` Constantine Gavrilov
  2004-09-13 14:42   ` Richard B. Johnson
  0 siblings, 1 reply; 2+ messages in thread
From: Constantine Gavrilov @ 2004-09-13 14:17 UTC (permalink / raw)
  To: bugs, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 981 bytes --]

>
>
>Subject: Calling syscalls from x86-64 kernel results in a crash on Opteron machines
>Date: Mon, 13 Sep 2004 17:04:17 +0300
>From: Constantine Gavrilov <constg@qlusters.com>
>To: bugs@x86-64.org, linux-kernel@vger.kernel.org
>
>Hello:
>
>We have a piece of kernel code that calls some system calls in kernel
>context (from a process with mm and a daemonized kernel thread that does
>not have mm). This works fine on IA64 and i386 architectures.
>
..............

>Attached please find a test module that tries to call the umask() (JUST
>TO DEMONSTRATE a problem) via the syscall machanism. Both methods (the
>_syscall1() marco and GLIBC INLINE_SYCALL() were used.
>  
>

I forgot to attach a header file with glibc version of syscall inline 
implementation.

-- 
----------------------------------------
Constantine Gavrilov
Kernel Developer
Qlusters Software Ltd
1 Azrieli Center, Tel-Aviv
Phone: +972-3-6081977
Fax:   +972-3-6081841
----------------------------------------


[-- Attachment #2: gsyscall.h --]
[-- Type: text/plain, Size: 2204 bytes --]

#ifndef _GSYSCALL_H_
#define _GSYSCALL_H_

#define __set_errno(Val) errno = (Val)

#undef INLINE_SYSCALL
#define INLINE_SYSCALL(name, nr, args...) \
  ({									      \
    unsigned long resultvar = INTERNAL_SYSCALL (name, , nr, args);	      \
    if (__builtin_expect (INTERNAL_SYSCALL_ERROR_P (resultvar, ), 0))	      \
      {									      \
	__set_errno (INTERNAL_SYSCALL_ERRNO (resultvar, ));		      \
	resultvar = (unsigned long) -1;					      \
      }									      \
    (long) resultvar; })

#undef INTERNAL_SYSCALL_DECL
#define INTERNAL_SYSCALL_DECL(err) do { } while (0)



#undef INTERNAL_SYSCALL
#define INTERNAL_SYSCALL(name, err, nr, args...) \
  ({									      \
    unsigned long resultvar;						      \
    LOAD_ARGS_##nr (args)						      \
    asm volatile (							      \
    "movq %1, %%rax\n\t"						      \
    "syscall\n\t"							      \
    : "=a" (resultvar)							      \
    : "i" (__NR_##name) ASM_ARGS_##nr : "memory", "cc", "r11", "cx");	      \
    (long) resultvar; })

#undef INTERNAL_SYSCALL_ERROR_P
#define INTERNAL_SYSCALL_ERROR_P(val, err) \
  ((unsigned long) (val) >= -4095L)

#undef INTERNAL_SYSCALL_ERRNO
#define INTERNAL_SYSCALL_ERRNO(val, err)	(-(val))

#define LOAD_ARGS_0()
#define ASM_ARGS_0

#define LOAD_ARGS_1(a1)					\
  register long int _a1 asm ("rdi") = (long) (a1);	\
  LOAD_ARGS_0 ()
#define ASM_ARGS_1	ASM_ARGS_0, "r" (_a1)

#define LOAD_ARGS_2(a1, a2)				\
  register long int _a2 asm ("rsi") = (long) (a2);	\
  LOAD_ARGS_1 (a1)
#define ASM_ARGS_2	ASM_ARGS_1, "r" (_a2)

#define LOAD_ARGS_3(a1, a2, a3)				\
  register long int _a3 asm ("rdx") = (long) (a3);	\
  LOAD_ARGS_2 (a1, a2)
#define ASM_ARGS_3	ASM_ARGS_2, "r" (_a3)

#define LOAD_ARGS_4(a1, a2, a3, a4)			\
  register long int _a4 asm ("r10") = (long) (a4);	\
  LOAD_ARGS_3 (a1, a2, a3)
#define ASM_ARGS_4	ASM_ARGS_3, "r" (_a4)

#define LOAD_ARGS_5(a1, a2, a3, a4, a5)			\
  register long int _a5 asm ("r8") = (long) (a5);	\
  LOAD_ARGS_4 (a1, a2, a3, a4)
#define ASM_ARGS_5	ASM_ARGS_4, "r" (_a5)

#define LOAD_ARGS_6(a1, a2, a3, a4, a5, a6)		\
  register long int _a6 asm ("r9") = (long) (a6);	\
  LOAD_ARGS_5 (a1, a2, a3, a4, a5)
#define ASM_ARGS_6	ASM_ARGS_5, "r" (_a6)

#endif

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines
  2004-09-13 14:17 ` Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines Constantine Gavrilov
@ 2004-09-13 14:42   ` Richard B. Johnson
  0 siblings, 0 replies; 2+ messages in thread
From: Richard B. Johnson @ 2004-09-13 14:42 UTC (permalink / raw)
  To: Constantine Gavrilov; +Cc: bugs, linux-kernel

On Mon, 13 Sep 2004, Constantine Gavrilov wrote:

> >
> >
> >Subject: Calling syscalls from x86-64 kernel results in a crash on Opteron machines
> >Date: Mon, 13 Sep 2004 17:04:17 +0300
> >From: Constantine Gavrilov <constg@qlusters.com>
> >To: bugs@x86-64.org, linux-kernel@vger.kernel.org
> >
> >Hello:
> >
> >We have a piece of kernel code that calls some system calls in kernel
> >context (from a process with mm and a daemonized kernel thread that does
> >not have mm). This works fine on IA64 and i386 architectures.
> >
> ..............

Okay, It's a real process that has its own context.

>
> >Attached please find a test module that tries to call the umask() (JUST
> >TO DEMONSTRATE a problem) via the syscall machanism. Both methods (the
> >_syscall1() marco and GLIBC INLINE_SYCALL() were used.
> >
> >
>

You can't use the user-mode syscalls! You need to use the kernel
procedures to which they trap (like sys_open(), etc.). The reason
is that you are operating on the kernel stack, you then generate a
trap for the system call, which starts over again on the kernel
stack (overwriting your previous return addresses, etc.).

A kernel-mode daemon has a context of its own, but it shares
kernel data and stack.

Cheers,
Dick Johnson
Penguin : Linux version 2.4.26 on an i686 machine (5570.56 BogoMips).
            Note 96.31% of all statistics are fiction.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2004-09-13 14:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <200409131715.27584.anatolya@qlusters.com>
2004-09-13 14:17 ` Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines Constantine Gavrilov
2004-09-13 14:42   ` Richard B. Johnson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox