System calls parameter restrictions - Linux

This is a discussion on System calls parameter restrictions - Linux ; Hello Are there any restrictions on the parameters that are passed to a system call? Is it possible to pass a struct as a parameter to a system call? The _syscall1 macro tries to cast the parameter to a ``long''. ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: System calls parameter restrictions

  1. System calls parameter restrictions

    Hello

    Are there any restrictions on the parameters that are passed to a
    system call? Is it possible to pass a struct as a parameter to a system
    call?

    The _syscall1 macro tries to cast the parameter to a ``long''. Does it
    mean that a struct or union type cannot be passed by value? Only as a
    pointer?

    Thank you for your attention

  2. Re: System calls parameter restrictions


    Rafael Almeida wrote:

    > Are there any restrictions on the parameters that are passed to a
    > system call? Is it possible to pass a struct as a parameter to a system
    > call?


    Obviously, nobody would design a system that had a system call that
    required a parameter you couldn't pass the value for. You can always
    pass pointers, otherwise 'read' and 'write' wouldn't work. A struct
    would be passed by address.

    > The _syscall1 macro tries to cast the parameter to a ``long''. Does it
    > mean that a struct or union type cannot be passed by value? Only as a
    > pointer?


    Mine doesn't cast the parameter to a long. It looks like this:

    #define _syscall1(type,name,type1,arg1) \
    type name(type1 arg1) \
    {\
    return syscall(__NR_##name, arg1);\
    }

    And 'syscall' takes the number of the system call and then '...'.

    I believe structs and unions are never passed by value, but I'm not
    sure.

    DS


  3. Re: System calls parameter restrictions

    On 26 Oct 2006 01:54:12 -0700
    "David Schwartz" wrote:

    > Obviously, nobody would design a system that had a system call that
    > required a parameter you couldn't pass the value for. You can always
    > pass pointers, otherwise 'read' and 'write' wouldn't work. A struct
    > would be passed by address.
    >

    Obviously you can pass pretty much anything through a pointer, but what
    I didn't understand was whether you could pass a struct by value or not.

    > Mine doesn't cast the parameter to a long. It looks like this:
    >
    > #define _syscall1(type,name,type1,arg1) \
    > type name(type1 arg1) \
    > {\
    > return syscall(__NR_##name, arg1);\
    > }
    >
    > And 'syscall' takes the number of the system call and then '...'.
    >

    Hum. What's your kernel version? On linux 2.6.18.1 (i386 code) it goes
    like this:
    #define _syscall1(type,name,type1,arg1) \
    type name(type1 arg1) \
    { \
    long __res; \
    __asm__ volatile ("int $0x80" \
    : "=a" (__res) \
    : "0" (__NR_##name),"b" ((long)(arg1))); \
    __syscall_return(type,__res); \
    }
    So you can see from that that passing a struct (and not a pointer to a
    struct) as a parameter is going to fail.

    > I believe structs and unions are never passed by value, but I'm not
    > sure.

    Yeah, I couldn't find any system call that gets a struct as a parameter.
    That's why I thought it was a restriction on the possible parameter
    types. And I want to know the reason of that restriction. Not because I
    want to pass a struct as a parameter so bad, but because I want to learn
    what I'm missing that would make passing a struct by value a bad idea.

  4. Re: System calls parameter restrictions

    On Fri, 27 Oct 2006 16:54:36 -0300 Rafael Almeida wrote:
    | On 26 Oct 2006 01:54:12 -0700
    | "David Schwartz" wrote:
    |
    |> Obviously, nobody would design a system that had a system call that
    |> required a parameter you couldn't pass the value for. You can always
    |> pass pointers, otherwise 'read' and 'write' wouldn't work. A struct
    |> would be passed by address.
    |>
    | Obviously you can pass pretty much anything through a pointer, but what
    | I didn't understand was whether you could pass a struct by value or not.

    C is capable of passing a struct by value. But in the past it was not,
    and thus traditional Unix system calls would pass a struct by pointer.
    In cases where the struct might be changed and the called needs to see
    those changes, then either passing by pointer is needed or the syscall
    would need to return the struct value.

    I've never heard of any syscall that passes or returns a struct by value.
    That doesn't mean there couldn't be one in the future.


    | Yeah, I couldn't find any system call that gets a struct as a parameter.
    | That's why I thought it was a restriction on the possible parameter
    | types. And I want to know the reason of that restriction. Not because I
    | want to pass a struct as a parameter so bad, but because I want to learn
    | what I'm missing that would make passing a struct by value a bad idea.

    It's not that bad of an idea for application programs. I've done it many
    times. As for passing to and from a kernel, that might be another issue.
    Passing by value is actually a kind of abstraction, anyway. Literally,
    the struct is copied or loaded in registers, depending on architecture.
    The register starved i386 is probably going to have to copy.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-27-1704@ipal.net |
    |------------------------------------/-------------------------------------|

  5. Re: System calls parameter restrictions


    Rafael Almeida wrote:

    > Obviously you can pass pretty much anything through a pointer, but what
    > I didn't understand was whether you could pass a struct by value or not.


    I think it's an ABI restriction. As far as the ABI is concerned, a
    structure doesn't have a value, it's an arbitrarily-formatted chunk of
    memory. So the only way you could pass it by value would be to make a
    copy of it and pass a pointer to the copy.

    DS


  6. Re: System calls parameter restrictions

    On 27 Oct 2006 16:47:46 -0700 David Schwartz wrote:

    | Rafael Almeida wrote:
    |
    |> Obviously you can pass pretty much anything through a pointer, but what
    |> I didn't understand was whether you could pass a struct by value or not.
    |
    | I think it's an ABI restriction. As far as the ABI is concerned, a
    | structure doesn't have a value, it's an arbitrarily-formatted chunk of
    | memory. So the only way you could pass it by value would be to make a
    | copy of it and pass a pointer to the copy.

    Structs small enough to fit in available register space could be passed
    in much the same way many archtectures do pass values. It's just a big
    complex value. What if you were passing a 64 bit integer by value, on a
    32 bit machine, knowing that there is really no true value as cuh, but
    only a joining of a pair of 32 bit integers? It really doesn't matter.
    It can be dealt with one way or another. It can be loaded into a register
    or a copy can be made through use of pass by reference (pointer). It's
    just a matter of how the kernel syscall interface is designed and to the
    extent such a design depends on it, the ABI.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-27-2148@ipal.net |
    |------------------------------------/-------------------------------------|

  7. Re: System calls parameter restrictions


    phil-news-nospam@ipal.net wrote:

    > Structs small enough to fit in available register space could be passed
    > in much the same way many archtectures do pass values. It's just a big
    > complex value. What if you were passing a 64 bit integer by value, on a
    > 32 bit machine, knowing that there is really no true value as cuh, but
    > only a joining of a pair of 32 bit integers? It really doesn't matter.
    > It can be dealt with one way or another. It can be loaded into a register
    > or a copy can be made through use of pass by reference (pointer). It's
    > just a matter of how the kernel syscall interface is designed and to the
    > extent such a design depends on it, the ABI.


    But that would mean that changing a structure would break the ABI. That
    doesn't sound like it's worth it. Think about structures of unknown
    length, such as 'struct sockaddr'. (How do you tell the compiler a
    structure has unknown length?)

    DS


  8. Re: System calls parameter restrictions

    On 28 Oct 2006 05:03:19 -0700 David Schwartz wrote:
    |
    | phil-news-nospam@ipal.net wrote:
    |
    |> Structs small enough to fit in available register space could be passed
    |> in much the same way many archtectures do pass values. It's just a big
    |> complex value. What if you were passing a 64 bit integer by value, on a
    |> 32 bit machine, knowing that there is really no true value as cuh, but
    |> only a joining of a pair of 32 bit integers? It really doesn't matter.
    |> It can be dealt with one way or another. It can be loaded into a register
    |> or a copy can be made through use of pass by reference (pointer). It's
    |> just a matter of how the kernel syscall interface is designed and to the
    |> extent such a design depends on it, the ABI.
    |
    | But that would mean that changing a structure would break the ABI. That
    | doesn't sound like it's worth it. Think about structures of unknown
    | length, such as 'struct sockaddr'. (How do you tell the compiler a
    | structure has unknown length?)

    Are you saying change the way a struct is passed? Or changing the value
    in a struct?

    The compiler can pass structs by value. But I think that is only for
    structs with a fixed size.

    --
    |---------------------------------------/----------------------------------|
    | Phil Howard KA9WGN (ka9wgn.ham.org) / Do not send to the address below |
    | first name lower case at ipal.net / spamtrap-2006-10-28-1751@ipal.net |
    |------------------------------------/-------------------------------------|

  9. Re: System calls parameter restrictions


    > Are you saying change the way a struct is passed? Or changing the value
    > in a struct?


    > The compiler can pass structs by value. But I think that is only for
    > structs with a fixed size.


    To put it another way, allowing structures to be passed by value would
    only help if you actually did pass any structures by value. However,
    any ABI that actually did pass a structure by value would break if the
    size of that structure changed.

    DS


+ Reply to Thread