Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 10 May 2013 20:31:24 +0100 (BST)
From:      Karl Dreger <k.dreger@yahoo.de>
To:        "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
Subject:   syscall to userland interface
Message-ID:  <1368214284.29611.YahooMailNeo@web171503.mail.ir2.yahoo.com>

next in thread | raw e-mail | index | archive | help
Hello,=0AI have been taking a look at a few syscalls in /usr/src/sys/kern/ =
and=0Aalways find that in their actuall c definition the function names are=
=0Apreprended by a sys_. Take for example the fork system call which=0Ais f=
ound in /usr/src/sys/kern/kern_fork.c=0A=0Aint=0Asys_fork(struct thread *td=
, struct fork_args *uap)=0A...=0A=0ANow when I write a program from userlan=
d, that makes use of the =0Afork system call, then if call it as:=0A=0Afork=
();=0A=0AAll the syscall are part of libc, which is usually defined in =0A/=
usr/src/lib/libc/=0A=0ASince the system calls are already defined in the ke=
rnel sources, they =0Ano longer need to be defined in /usr/src/lib/libc/. T=
his is the reason =0Awhy one can only find the manpages and no c files in =
=0A/usr/src/lib/libc/sys?=0AAt least this is how my thinking goes.=0A=0ANow=
, when the syscalls in the kernel sources are all defined as sys_xxx =0Abut=
 are invoked as xxx and the c headers also show syscall prototypes =0Awitho=
ut any prepended sys. How does the actual user-, kernelland =0Amove happen?=
 In other words, why do I invoke fork() as fork() and=0Anot as sys_fork()?=
=0A=0AOr is there something that I missed?=0A=0A=0AClarification on that po=
int is highly welcome.=0A=0AThanks=0A
From owner-freebsd-hackers@FreeBSD.ORG  Fri May 10 19:37:07 2013
Return-Path: <owner-freebsd-hackers@FreeBSD.ORG>
Delivered-To: freebsd-hackers@freebsd.org
Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115])
 by hub.freebsd.org (Postfix) with ESMTP id 9CF35B4C
 for <freebsd-hackers@freebsd.org>; Fri, 10 May 2013 19:37:07 +0000 (UTC)
 (envelope-from bright@mu.org)
Received: from elvis.mu.org (elvis.mu.org [192.203.228.196])
 by mx1.freebsd.org (Postfix) with ESMTP id 8EBCB31B
 for <freebsd-hackers@freebsd.org>; Fri, 10 May 2013 19:37:06 +0000 (UTC)
Received: from Alfreds-MacBook-Pro-9.local
 (c-67-180-208-218.hsd1.ca.comcast.net [67.180.208.218])
 by elvis.mu.org (Postfix) with ESMTPSA id DB7DB1A3D0F;
 Fri, 10 May 2013 12:37:05 -0700 (PDT)
Message-ID: <518D4C4F.90902@mu.org>
Date: Fri, 10 May 2013 12:36:47 -0700
From: Alfred Perlstein <bright@mu.org>
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7;
 rv:17.0) Gecko/20130328 Thunderbird/17.0.5
MIME-Version: 1.0
To: Karl Dreger <k.dreger@yahoo.de>
Subject: Re: syscall to userland interface
References: <1368214284.29611.YahooMailNeo@web171503.mail.ir2.yahoo.com>
In-Reply-To: <1368214284.29611.YahooMailNeo@web171503.mail.ir2.yahoo.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Cc: "freebsd-hackers@freebsd.org" <freebsd-hackers@freebsd.org>
X-BeenThere: freebsd-hackers@freebsd.org
X-Mailman-Version: 2.1.14
Precedence: list
List-Id: Technical Discussions relating to FreeBSD
 <freebsd-hackers.freebsd.org>
List-Unsubscribe: <http://lists.freebsd.org/mailman/options/freebsd-hackers>, 
 <mailto:freebsd-hackers-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/freebsd-hackers>;
List-Post: <mailto:freebsd-hackers@freebsd.org>
List-Help: <mailto:freebsd-hackers-request@freebsd.org?subject=help>
List-Subscribe: <http://lists.freebsd.org/mailman/listinfo/freebsd-hackers>,
 <mailto:freebsd-hackers-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Fri, 10 May 2013 19:37:07 -0000

On 5/10/13 12:31 PM, Karl Dreger wrote:
> Hello,
> I have been taking a look at a few syscalls in /usr/src/sys/kern/ and
> always find that in their actuall c definition the function names are
> preprended by a sys_. Take for example the fork system call which
> is found in /usr/src/sys/kern/kern_fork.c
>
> int
> sys_fork(struct thread *td, struct fork_args *uap)
> ...
>
> Now when I write a program from userland, that makes use of the
> fork system call, then if call it as:
>
> fork();
>
> All the syscall are part of libc, which is usually defined in
> /usr/src/lib/libc/
>
> Since the system calls are already defined in the kernel sources, they
> no longer need to be defined in /usr/src/lib/libc/. This is the reason
> why one can only find the manpages and no c files in
> /usr/src/lib/libc/sys?
> At least this is how my thinking goes.
>
> Now, when the syscalls in the kernel sources are all defined as sys_xxx
> but are invoked as xxx and the c headers also show syscall prototypes
> without any prepended sys. How does the actual user-, kernelland
> move happen? In other words, why do I invoke fork() as fork() and
> not as sys_fork()?
>
> Or is there something that I missed?
>
>
> Clarification on that point is highly welcome.

When you build the system a whole bunch of assembler files are 
automatically generated that define the functions you are looking for.

Look for .S files under the object directory.

Those assembler files have the magic to cause a system call to happen.

example: src/lib/libc/getauid.S  (note, this file is GENERATED, it's not 
part of src.)



-Alfred




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1368214284.29611.YahooMailNeo>