Can hardware be accessed directly from user space?

6,446

Solution 1

Your guess is correct. The kernel is the only software that can send hardware requests. That does not only hold true for Linux. Virtually no operating system newer than DOS lets you access hardware directly, because, as you also suspect, it's quite quite dangerous.

However, there is no problem in writing your own driver and plenty of documentation is available. For example, this tutorial at xatlantis seems a recent (that's important!) and good source.

Solution 2

Yes, but it is rarely useful. On 32-bit x86 systems, you can use the ioperm(2) system call to set up the current process to allow it to perform port I/O directly. I believe this does not work on 64-bit systems. You can also do lseek/read/write combinations against /dev/port and I think this likewise is not very portable. See http://tldp.org/HOWTO/IO-Port-Programming-2.html for some more details. Both of these approaches are much slower and less flexible than writing a device driver of course (/dev/port slowest of all). None of tese userspace mechanisms allow you to handle interrupts or anything like that, of course. If performance is an issue you're going to end up writing a device driver.

Solution 3

It is definitely possible to access hardware from user space. Especially if the hardware registers are memory mapped. Se e.g., UIO.

Share:
6,446

Related videos on Youtube

Lewis Wilcock
Author by

Lewis Wilcock

Updated on September 18, 2022

Comments

  • Lewis Wilcock
    Lewis Wilcock almost 2 years

    At work we have some embedded devices controlled by DOS software. I have been tasked with the responsibility of assessing the use of Linux as a replacement OS for DOS on a next generation of hardware. My research leads me to the conclusion that DOS is simply a totally different type of operating system that allows you to do potentially dangerous things if you want to. From my understanding if you wish to access hardware directly you would have to write a custom driver. What I need to know to satisfy the curiosity of my boss is the following: is it at all possible to access hardware directly from user space??

    My guess is no, but I would like to request the opinion of those with knowledge far greater than mine.

    • bnikhil
      bnikhil almost 12 years
      How are the devices accessed from the OS? E.g. are they PCI cards?
    • Lewis Wilcock
      Lewis Wilcock almost 12 years
      I believe so yes. Most hardware is wired into the board directly (like serial ports and ethernet adaptor). There are devices attached also - I am a simple desktop application developer so my in depth knowledge of hardware is very limited. Hence my asking here.
    • Bananguin
      Bananguin almost 12 years
      If you are not using specialized hardware and are only looking to get serial-ports and ethernet working, you probably don't need to access the hardware directly ...
    • Lewis Wilcock
      Lewis Wilcock almost 12 years
      This was my thought to be honest. However, I think because the old code uses stuff like hardware interrupts to access incoming TCP and serial transmissions my boss wants the same functionality. It's probably a real-time issue. I personally cant see the problem with using things like poll() or select() to get data.
  • Netch
    Netch almost 12 years
    But work with interrupts and DMA is still impossible in userspace (AFAIK) So device access from userspace is limited to communicate via ports or device mapped memory.
  • Batfan
    Batfan almost 12 years
    Yes, which is why I mentioned that in my answer.