2004-09-13 14:19:05

by Constantine Gavrilov

[permalink] [raw]
Subject: Re: Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines

#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


Attachments:
gsyscall.h (2.15 kB)

2004-09-13 14:51:52

by Richard B. Johnson

[permalink] [raw]
Subject: Re: Fwd: Calling syscalls from x86-64 kernel results in a crash on Opteron machines

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 <[email protected]>
> >To: [email protected], [email protected]
> >
> >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.