PCI device /dev/mem questions - Linux

This is a discussion on PCI device /dev/mem questions - Linux ; Hi everyone, Just some quick questions: I wrote a C userspace program that accesses base address 0 of a PCI device. In my C code, I have been able to read and write to the base address by opening "/dev/mem" ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: PCI device /dev/mem questions

  1. PCI device /dev/mem questions

    Hi everyone,

    Just some quick questions:

    I wrote a C userspace program that accesses base address 0 of a PCI
    device. In my C code, I have been able to read and write to the base
    address by opening "/dev/mem" and mmap-ing the memory range I want
    based off of base address 0. Afterwards I can read/write to the base
    address and to any offset from the base address.

    Question #1) Sometimes, I have to run lspci -v in order to check the
    base address 0 of the device in order to make sure that the base
    address has not changed. Is there anyway to check the base address of
    the device in the userspace code instead? Or do I need to write a linux
    device driver that will return to me the base address zero?

    Question #2) In order to run the code, I have to run as root because I
    am operating on /dev/mem. What are the reasons that I need to become
    root? Is there anyway to get around being root? When I tried to chmod
    666 /dev/mem, it still will not run the program. Why is that?


  2. Re: PCI device /dev/mem questions

    "elliotng.ee@gmail.com" writes:

    > Hi everyone,
    >
    > Just some quick questions:
    >
    > I wrote a C userspace program that accesses base address 0 of a PCI
    > device. In my C code, I have been able to read and write to the base
    > address by opening "/dev/mem" and mmap-ing the memory range I want
    > based off of base address 0. Afterwards I can read/write to the base
    > address and to any offset from the base address.
    >
    > Question #1) Sometimes, I have to run lspci -v in order to check the
    > base address 0 of the device in order to make sure that the base
    > address has not changed. Is there anyway to check the base address of
    > the device in the userspace code instead? Or do I need to write a linux
    > device driver that will return to me the base address zero?


    There is: http://mj.ucw.cz/pciutils.shtml (libpci)

    >
    > Question #2) In order to run the code, I have to run as root because I
    > am operating on /dev/mem. What are the reasons that I need to become
    > root? Is there anyway to get around being root? When I tried to chmod
    > 666 /dev/mem, it still will not run the program. Why is that?
    >


    Read access to `/dev/mem' means that you can access all the memory
    (including privileged information)

    Write access means you can even manipulate it.

    Providing either of these to non-privileged process is bad from
    security perspective.

    Read/write file operations on `/dev/mem' can (and probably do) check
    the capabilities of the process, this is not a regular file.

    --
    vale

+ Reply to Thread