Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 28 Sep 2009 10:19:08 -0700 (PDT)
From:      Leunam Elebek <forensec@yahoo.de>
To:        freebsd-hackers@freebsd.org
Subject:   Trouble with copyout, memcpy.... Plain-Text version =)
Message-ID:  <389605.70197.qm@web28503.mail.ukl.yahoo.com>

next in thread | raw e-mail | index | archive | help
Hey list,

I currently code a driver under Current 8.0 for Current 8.0.
But there are some problems with kernel/user-space interaction.

I've the following structure:

struct daq_kitinfo {
=A0 =A0 =A0 =A0 uint32_t ki_maxdata;
=A0 =A0 =A0 =A0 uint32_t ki_flags;
=A0 =A0 =A0 =A0 uint32_t ki_rng_type;
=A0 =A0 =A0 =A0 int=A0 =A0 =A0 ki_type;
=A0 =A0 =A0 =A0 int=A0 =A0 =A0 ki_nchan;
=A0 =A0 =A0 =A0 int=A0 =A0 =A0 ki_len_chanl;
};

The above structure is used in my user-space app:

int main(void) {
=A0 =A0 =A0 =A0 struct daq_kitinfo *info;
=A0 =A0 =A0 =A0 struct daq_kit kit;=20
=A0 =A0 =A0 =A0 int fd, size;=20
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 /*=20
=A0 =A0 =A0 =A0=A0=A0* At this point I'll try to alloc memory. Notice that=
=20
=A0 =A0 =A0 =A0=A0=A0* the size i dependet from another struct entry.
=A0 =A0 =A0 =A0=A0=A0*/
=A0 =A0 =A0 =A0 size =3D sizeof(*info) * kit.k_nkits;
=A0 =A0 =A0 =A0 info =3D malloc(size);
=A0 =A0 =A0 =A0 if (info =3D=3D NULL)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 exit(ENOMEM);

=A0 =A0 =A0 =A0 /*
=A0 =A0 =A0 =A0=A0=A0* The next step is to call the drivers ioctl() interfa=
ce
=A0 =A0 =A0 =A0=A0=A0* (the reason for that is described below).
=A0 =A0 =A0 =A0=A0=A0*/
=A0 =A0 =A0 =A0 if (ioctl(fd, DAQ_KITINFO, info)) {
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 printf("errno: %d\n", errno);=20
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0=A0exit(errno);
=A0 =A0 =A0 =A0 }

=A0 =A0 =A0 =A0=A0=A0printf("[ki_nchan] %d\n", info.ki_nchan);
=A0 =A0 =A0 =A0=A0=A0...
=A0 =A0 =A0 =A0=A0=A0...
=A0 =A0 =A0 =A0=A0=A0return (0);
}


and inside the driver (put it simply):


static int
my_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flags,
=A0 =A0 struct thread *td)
{
=A0 =A0 =A0 =A0 struct daq_kitinfo *info;
=A0 =A0 =A0 =A0 struct daq_kit =3D dev->si_drv1;=20
=A0 =A0 =A0 =A0 int size;=20
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 /* Do something useful e.g mutex'ing... */
=A0 =A0 =A0 =A0 ...
=A0 =A0 =A0 =A0 /* The same as in user-space... */
=A0 =A0 =A0 =A0 size =3D sizeof(*info) * kit.k_nkits;
=A0 =A0 =A0 =A0 info =3D malloc(sz, M_DAQ, M_NOWAIT | M_ZERO);
=A0 =A0 =A0 =A0 if (info =3D=3D NULL)
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 ....
=A0 =A0 =A0 =A0 /*
=A0 =A0 =A0 =A0=A0=A0* Here I want to copy struct info from kernel to user-=
space.
=A0 =A0 =A0 =A0=A0=A0* If i use memcpy, the result is that the system hangs
=A0 =A0 =A0 =A0=A0=A0* and I need to reboot the machine. OK, I thought
=A0 =A0 =A0 =A0=A0=A0* copyout() should be able to do the job for me...
=A0 =A0 =A0 =A0=A0=A0*/
=A0 =A0 =A0 =A0=A0=A0if (copyout(info, arg, sz))
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=A0=A0/*
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 * Fuc[k-k] i still come inside this blo=
ck. I always
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 * get an EFAULT error.=20
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 */
}

I really don't know what I should do to let the driver working
properly. The driver should grap some informations/attributes,
and fill up the info structure, so we can copy the filled info struct
to the user's app.

I hope somebody can help me to resolve that problem.

Ah, the corresponding ioctl is:


#define GRP=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 'd'
#define DAQ_KITINFO=A0 =A0 =A0 =A0 _IOR(GRP, 3, struct daq_kitinfo)

Thanks for attention and greatz from germany

MG=0A=0A=0A      



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