Subject: Re: Question about modules
To: None <tech-kern@netbsd.org>
From: Hans Petter Selasky <hselasky@c2i.net>
List: tech-kern
Date: 08/28/2005 19:43:45
On Sunday 28 August 2005 17:26, Quentin Garnier wrote:
> On Sun, Aug 28, 2005 at 05:17:10PM +0200, Julio M. Merino Vidal wrote:
> > On 8/28/05, Hans Petter Selasky <hselasky@c2i.net> wrote:
> > > Hi,
> > >
> > > On FreeBSD they use the "__section__" attribute to do linking magic.
> > > But on NetBSD I am completely lost :-)
> > >
> > > 1) What is the logic behind linking modules?
> > >
> > > # pwd
> > > /usr/src/sys/lkm/vfs/adosfs
> > >
> > > # ls
> > > CVS             Makefile        lkminit_vfs.c
> > >                                 ^^^ is there some
> > >                        magic hidden in this filename?
> > >
> > > From where is the following function called:
> > >
> > > adosfs_lkmentry
> >
> > I guess this is because the structure defined by the MOD_VFS
> > macro, whose name is _module.  See sys/lkm.h.
> >
> > After the structure is defined with a known name, the kernel
> > can access it, get the vfs operations structure and call the
> > vfs_init function from there.
>
> Not really.  That part is done through DISPATCH().
>
> > > Is this function also called when the module is
> > > in the kernel ?
> >
> > If "this function" means vfs_init, then yes, it is called during
> > system startup as a result of the VFS_ATTACH macro.
>
> "This function" is adofs_lkmentry.
>
> The entry point address is passed to the kernel by modload(8) as
> part of the LKM API.
>
> > > 2) Where is PSEUDO_SET defined?
> >
> > Dunno; is that something FreeBSD specific?  The comment in
> > net/net_osdep.h that includes PSEUDO_SET in it may be of
> > help.
>
> That's very likely to be FreeBSD stuff.  Definitely not NetBSD stuff
> anyway.

Yes, you are right, there was an "ifdef" there for FreeBSD.

>
> Remember that the NetBSD kernel doesn't include a linker, so modload(8)
> spawns ld(1) which does the actual linking and then modload(8) copies
> the resulting binary to the kernel.

Ok, so any function which name ends with "_lkmentry" will be called 
automatically from the kernel during boot or module load ? I think I 
understand now.

What I am about to do is to write a small FreeBSD emulator for NetBSD. When a 
module is loaded, my "_lkmentry" function is called, which will then scan for 
special data-sections like on FreeBSD, so that kernel drivers can be ported 
without having to change so much code.

This is how one declares a data-section:

extern u_int32_t my_data[];

static u_int32_t __attribute__((__section__("my_data"),__aligned__(1))) test1 
= 0;
static u_int32_t other_value = 1;

static u_int32_t __attribute__((__section__("my_data"),__aligned__(1))) test2 
= 2;

After linking "my_data[1] == 2". This is used everywhere in the FreeBSD 
kernel, and is totally absent in the NetBSD kernel.


Currently I am almost finished implementing FreeBSD's /dev on NetBSD. If I am 
into luck I will probably be finished by some weeks.



I think it will take much less time to write a FreeBSD emulator, than having 
two branches for all my drivers. That is why I am doing this.



--HPS