Subject: question regarding put data in the specified section

From: "Teresa Tao" <[email-masked]>
Date: Fri, 14 Nov 2003 09:42:34 -0800

Hi there,

Does anyone know how to put my userland program's data in a specified section?

I know there is an attribute "section" to put my data inside a specified section, for example, int data __attribute__ ((section("INITDAT"));
But how do I initialize/setup the INITDAT section? We use the commercial toolchain, and we don't have the source code for it, is there still a way to specify the postion of the INITDAT section?

Thanks in advance!

Teresa

---

From: Ralf Baechle <[email-masked]>
Date: Sat, 15 Nov 2003 13:53:29 +0100

On Fri, Nov 14, 2003 at 09:42:34AM -0800, Teresa Tao wrote:

> But how do I initialize/setup the INITDAT section? We use the
> commercial toolchain, and we don't have the source code for it, is
> there still a way to specify the postion of the INITDAT section?

If your commercial toolchain understands the __attribute__ syntax then
I suspect it's based on gcc which would mean you have a right to the
sourcecode.

No initialization needed;  Ld will use the flags of the first instance
of an input section for the output section which usually is right.  In
the rare case this isn't suitable you can use a .section pseudo-op
in inline assembler or assembler to setup the section with the right
flags.  Just make sure this section is linked first.

  Ralf

---

From: "durai" <[email-masked]>
Date: Sun, 16 Nov 2003 14:30:05 +0530

Hello,
I need to read a file from a device driver and i wrote a sample driver like this

This kernel mode code which try to read the file until end of file is reached. This code had been is working without any problems in RedHat linux and uClinux.
But the same code causes a General Protection Fault in my mips linux. I tested the same code in mips running on uClinux which runs well.
what is wrong with mips linux?

#define __KERNEL_SYSCALLS__

#include <linux/version.h>
#ifdef MODULE
#ifdef MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/module.h>
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif

#include <linux/types.h>
#include <linux/unistd.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/io.h>
int test_open(void );
int init_module(void)
{
        printk("\n init_module ");
        test_open();
        return 0;
}
void cleanup_module(void)
{
        printk("\n cleanup module");
}

int test_open(void )
{
    int ifp,bcount;
    mm_segment_t fs;
    char buffer[0x1000];

    // for file opening temporarily tell the kernel I am not a user for
    // memory management segment access
    fs = get_fs();
    set_fs(KERNEL_DS);
    // open the file with the firmware for uploading
    if (ifp = open( "/etc/hotplug/isl3890.arm", O_RDONLY, 0 ), ifp < 0)
    {
        // error opening the file
        printk( "ERROR: File opening did not success \n");
        set_fs(fs);
        return -1;
    }
    // enter a loop which reads data blocks from the file and writes them
    // to the Direct Memory Windows
    do
    {
            bcount = read(ifp, &buffer, sizeof(buffer));
            printk("\n bcount %x ",bcount);
    }
    while (bcount != 0);
    close(ifp);
    // switch back the segment setting
    set_fs(fs);

    return 0;
}

thanks & regards
durai

---

From: Ralf Baechle <[email-masked]>
Date: Sun, 16 Nov 2003 23:51:33 +0100

On Sun, Nov 16, 2003 at 02:30:05PM +0530, durai wrote:

> This kernel mode code which try to read the file until end of file is
> reached. This code had been is working without any problems in RedHat
> linux and uClinux.
> But the same code causes a General Protection Fault in my mips linux.
> I tested the same code in mips running on uClinux which runs well.

If the screen says general protection fault it's wasn't a MIPS box :-)

> #define MOD_DEC_USE_COUNT

Ouch.  You can replace that all with a single line:

> int init_module(void)

[...]

> void cleanup_module(void)

[...]

The use of init_module and cleanup_module is deprecated, use something
like this instead:

#include <linux/init.h>

static int __init foo_init_module(void)
{
	return 0;
}

static void __exit foo_cleanup_module(void)
{
}

module_init(foo_init_module);
module_exit(foo_cleanup_module);

>     char buffer[0x1000];
                  ^^^^^^

And this is the BIG no-no.  Never allocate large amounts of data on the
kernel stack which is only 8k which are used for various other stuff
also..

>     if (ifp = open( "/etc/hotplug/isl3890.arm", O_RDONLY, 0 ), ifp < 0)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^

Hard coding file names is generally considered bad design; this topic has
been discussed to death numerous times on linux-kernel.  The general
recommendation is to use a special such as /dev/foodevice; the firmware
would then simply be loader by cat bar > //dev/foodevice or similar.

For 2.4.23 (I'm going to merge with 2.4.23-rc1 tonight) and 2.6 the
recommendation is to use CONFIG_FW_LOADER, the new standard interface
for loading firmware.

  Ralf

---

From: Brad Parker <[email-masked]>
Date: Sun, 16 Nov 2003 19:46:35 -0500

>> what is wrong with mips linux?

Shouldn't someone point out that having a driver read a file is 
very, very wrong and a classic FAQ question?

Perhaps I'm mistaken but this seems to come up once a year on every port
list I'm on.

The "unix way" is to have something in userland feed the data to the
driver.  Things in the kernel should never reach back up into the file
system. It's just not done.

Drivers are passive objects which do the bidding of "managers" which
live in userland.  Add an ioctl to the driver which allows a userland
daemon to feed the microcode/firmware/fpga-data/whatever to the driver
in pieces.

Resist the temptation to put code in the driver to access the file
system.

ps: isn't hotplug already setup to notice when a device comes up and to
have a shell script run?  it's bad enough that the hotplug code runs a
shell script from the kernel.  I can't believe that got through...

(and if you have time, go read the plan 9 design docs.  then ask yourself
what those guys would do :-)

-brad

---

From: Ralf Baechle <[email-masked]>
Date: Mon, 17 Nov 2003 02:06:32 +0100

> very, very wrong and a classic FAQ question?

It is.

> Perhaps I'm mistaken but this seems to come up once a year on every port

I think it's the first time on this list.  In my previous posting I suggested
request_firmware for 2.4.23 / 2.6.  For kernels older than this I suggest
arch/i386/kernel/microcode.c as an example for how to easily implement
a character special file.

> Resist the temptation to put code in the driver to access the file

Amen.

> what those guys would do :-)

2.6 certainly is quite a bit more plan 9-ish.  What would you expect from
Al Viro :-)

  Ralf