[ILUG] using map_user_kiobuf

Dave Airlie airlied at csn.ul.ie
Mon Jul 23 12:03:34 IST 2001


I don't think you've read up on the kernel/user divide how system calls
and ioctls work have you?

Ioctl and system calls are two ways of calling functions in the kernel
from user space,

ioctls operate on a /dev device, and can be passed in a structure, the
device driver for the /dev device uses the ioctl number (different for
every device) to decide what user space wants to do and uses the contents
of the structure as the input ...

systems calls use the CPU trap functionality, they set up the registers or
stack with the inputs to the system call, sticks the system call number
from unistd.h in a registers and calls a trap handler which makes the CPU
hop into kernel space, the kernel then takes the system call number from
the register, and calls the kernel routine which takes the stuff off the
stack or wherever it is ..

Dave.


On Mon, 23 Jul 2001, Oliver Ryan wrote:

> Hi all,
>
> I found the following piece of code below (from
> http://www.linuxarkivet.nu/mlists/linux-kernel/0011/msg05179.html) and
> would like to use it but I don't understand all of it:
>
> 1.  if test_kiobuf(char *buf) is a kernel module, how is it compiled and
> linked with the user space program?  How can buf be passed into it?  Where
> would init_module come into it?
>
> 2.  In the user space program, what is the ioctl command number 99 stand
> for?  I haven't found it anywhere.  What is that line (ioctl(fh, 99, buf))
> doing?
>
> That's it.  Thanks in advance for any help.
>
> Oliver.
>
> int test_kiobuf(char* buf)
> {
> struct kiobuf *iobuf;
> int i;
> alloc_kiovec(1, &iobuf);
>  map_user_kiobuf(WRITE, iobuf, buf, TEST_SIZE);
>  for (i = 0 ; i < iobuf->length; i++)
> {
> int off = iobuf->offset + i;
> int page = off / 4096;
> unsigned char* buf = page_address(iobuf->maplist[page]);
> buf[off % 4096] = (i & 0xFF); }
>  unmap_kiobuf(iobuf);
> free_kiovec(1, &iobuf);
>  return 0; }
>
> The user space programme is:
> #include <stdio.h>
> #include <fcntl.h>
> #include <sys/ioctl.h>
> #define TEST_SIZE 1000000
> int main(int argc, char *argv[])
> {
>  int fh;
> int i;
> unsigned char * buf;
> buf = (unsigned char *)malloc(TEST_SIZE);
> fh = open("/tmp/test", O_CREAT);
> if (ioctl(fh, 99, buf) != 0)
>   perror("ioctl failed");
> else
>   {
>     for (i = 0; i < TEST_SIZE; i++)
>      {
>         if (buf[i] != (i & 0xFF))
>           printf("%8.8X: %2.2X %2.2X\n", i, i & 0xFF, buf[i]);
>        }
>    }
>    close(fh);
>   free(buf);
>   return 0; }
>
>  .
>
>
>
>
> Dept. of Physics,
> National University of Ireland, Galway,
> Galway,
> Ireland.
>
> Tel: +353 (0)91 524411 ext. 2716
> Fax: +353 (0)91 750584
>
>
>

-- 
David Airlie, Software Engineer
http://www.skynet.ie/~airlied / airlied at skynet.ie
pam_smb / Linux DecStation / Linux VAX / ILUG person






More information about the ILUG mailing list