Is memory mapped I/O only used internally by OS, not exposed to and used by programmers on top of Linux?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












In Operating System Concepts, memory mapped files and memory mapped I/O are two different things. See below about memory mapped I/O.



To use memory mapped files, we have mmap().



To use memory mapped I/O, what functions can we use? Is memory mapped I/O only used internally by OS, not exposed to and used by programmers on top of Linux?



Thanks.




In the case of I/O, as mentioned in Section 1.2.1, each I/O controller includes
registers to hold commands and the data being transferred. Usually, special I/O
instructions allow data transfers between these registers and system memory.
To allow more convenient access to I/O devices, many computer architectures
provide memory-mapped I/O. In this case, ranges of memory addresses are
set aside and are mapped to the device registers. Reads and writes to these
memory addresses cause the data to be transferred to and from the device
registers. This method is appropriate for devices that have fast response times,
such as video controllers. In the IBM PC, each location on the screen is mapped
to a memory location. Displaying text on the screen is almost as easy as writing
the text into the appropriate memory-mapped locations.




...




How can the processor give commands and data to a controller to
accomplish an I/O transfer?
The short answer is that the controller has one
or more registers for data and control signals. The processor communicates
with the controller by reading and writing bit patterns in these registers. One
way in which this communication can occur is through the use of special
I/O instructions
that specify the transfer of a byte or word to an I/O port
address. The I/O instruction triggers bus lines to select the proper device and
to move bits into or out of a device register. Alternatively, the device controller
can support memory-mapped I/O. In this case, the device-control registers
are mapped into the address space of the processor. The CPU executes I/O
requests using the standard data-transfer instructions to read and write the
device-control registers at their mapped locations in physical memory.











share|improve this question

























    up vote
    1
    down vote

    favorite












    In Operating System Concepts, memory mapped files and memory mapped I/O are two different things. See below about memory mapped I/O.



    To use memory mapped files, we have mmap().



    To use memory mapped I/O, what functions can we use? Is memory mapped I/O only used internally by OS, not exposed to and used by programmers on top of Linux?



    Thanks.




    In the case of I/O, as mentioned in Section 1.2.1, each I/O controller includes
    registers to hold commands and the data being transferred. Usually, special I/O
    instructions allow data transfers between these registers and system memory.
    To allow more convenient access to I/O devices, many computer architectures
    provide memory-mapped I/O. In this case, ranges of memory addresses are
    set aside and are mapped to the device registers. Reads and writes to these
    memory addresses cause the data to be transferred to and from the device
    registers. This method is appropriate for devices that have fast response times,
    such as video controllers. In the IBM PC, each location on the screen is mapped
    to a memory location. Displaying text on the screen is almost as easy as writing
    the text into the appropriate memory-mapped locations.




    ...




    How can the processor give commands and data to a controller to
    accomplish an I/O transfer?
    The short answer is that the controller has one
    or more registers for data and control signals. The processor communicates
    with the controller by reading and writing bit patterns in these registers. One
    way in which this communication can occur is through the use of special
    I/O instructions
    that specify the transfer of a byte or word to an I/O port
    address. The I/O instruction triggers bus lines to select the proper device and
    to move bits into or out of a device register. Alternatively, the device controller
    can support memory-mapped I/O. In this case, the device-control registers
    are mapped into the address space of the processor. The CPU executes I/O
    requests using the standard data-transfer instructions to read and write the
    device-control registers at their mapped locations in physical memory.











    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In Operating System Concepts, memory mapped files and memory mapped I/O are two different things. See below about memory mapped I/O.



      To use memory mapped files, we have mmap().



      To use memory mapped I/O, what functions can we use? Is memory mapped I/O only used internally by OS, not exposed to and used by programmers on top of Linux?



      Thanks.




      In the case of I/O, as mentioned in Section 1.2.1, each I/O controller includes
      registers to hold commands and the data being transferred. Usually, special I/O
      instructions allow data transfers between these registers and system memory.
      To allow more convenient access to I/O devices, many computer architectures
      provide memory-mapped I/O. In this case, ranges of memory addresses are
      set aside and are mapped to the device registers. Reads and writes to these
      memory addresses cause the data to be transferred to and from the device
      registers. This method is appropriate for devices that have fast response times,
      such as video controllers. In the IBM PC, each location on the screen is mapped
      to a memory location. Displaying text on the screen is almost as easy as writing
      the text into the appropriate memory-mapped locations.




      ...




      How can the processor give commands and data to a controller to
      accomplish an I/O transfer?
      The short answer is that the controller has one
      or more registers for data and control signals. The processor communicates
      with the controller by reading and writing bit patterns in these registers. One
      way in which this communication can occur is through the use of special
      I/O instructions
      that specify the transfer of a byte or word to an I/O port
      address. The I/O instruction triggers bus lines to select the proper device and
      to move bits into or out of a device register. Alternatively, the device controller
      can support memory-mapped I/O. In this case, the device-control registers
      are mapped into the address space of the processor. The CPU executes I/O
      requests using the standard data-transfer instructions to read and write the
      device-control registers at their mapped locations in physical memory.











      share|improve this question













      In Operating System Concepts, memory mapped files and memory mapped I/O are two different things. See below about memory mapped I/O.



      To use memory mapped files, we have mmap().



      To use memory mapped I/O, what functions can we use? Is memory mapped I/O only used internally by OS, not exposed to and used by programmers on top of Linux?



      Thanks.




      In the case of I/O, as mentioned in Section 1.2.1, each I/O controller includes
      registers to hold commands and the data being transferred. Usually, special I/O
      instructions allow data transfers between these registers and system memory.
      To allow more convenient access to I/O devices, many computer architectures
      provide memory-mapped I/O. In this case, ranges of memory addresses are
      set aside and are mapped to the device registers. Reads and writes to these
      memory addresses cause the data to be transferred to and from the device
      registers. This method is appropriate for devices that have fast response times,
      such as video controllers. In the IBM PC, each location on the screen is mapped
      to a memory location. Displaying text on the screen is almost as easy as writing
      the text into the appropriate memory-mapped locations.




      ...




      How can the processor give commands and data to a controller to
      accomplish an I/O transfer?
      The short answer is that the controller has one
      or more registers for data and control signals. The processor communicates
      with the controller by reading and writing bit patterns in these registers. One
      way in which this communication can occur is through the use of special
      I/O instructions
      that specify the transfer of a byte or word to an I/O port
      address. The I/O instruction triggers bus lines to select the proper device and
      to move bits into or out of a device register. Alternatively, the device controller
      can support memory-mapped I/O. In this case, the device-control registers
      are mapped into the address space of the processor. The CPU executes I/O
      requests using the standard data-transfer instructions to read and write the
      device-control registers at their mapped locations in physical memory.








      linux io memory-mapped-file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      Tim

      24.1k67233422




      24.1k67233422




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote













          Memory mapped IO is done by mmap()ing a region of a file and then using the mapped data.



          If you use a modern OS, the OS does most IO mmapped internally:



          • map parts of the file into a transient kernel area


          • copyout() the mapped data to the user address space. This causes the file content to be faulted in to the transient memory area of the kernel.


          • unmap the region


          BTW: if you are referring to accessing hardware from user space, this is usually done e.g. by the X Server that mmap()s the hardware from the graphics board and accesses it from user space.



          The device registers are accessible as memory addresses in that case and you just write code that looks as if you were inside the kernel. In other words: you do not use read() or write() but just dereference pointers that point to the addresses of the hardware registers.






          share|improve this answer






















          • I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
            – schily
            2 hours ago











          • @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
            – Tim
            1 hour ago











          • I added an explanation on how the device registers are accessed in such a case.
            – schily
            26 mins ago

















          up vote
          3
          down vote













          On Linux, MMIO is possible from user-space using mmap on /dev/mem. For example, the X server does



          fd = open("/dev/mem", O_RDWR);
          if (ioBase == NULL) PROT_WRITE,
          MAP_SHARED, fd, ioBase_phys);

          close(fd);


          in some cases. This is going out of fashion though and the kernel strictly controls what can be done using this type of access: accessing /dev/mem requires CAP_SYS_RAWIO, and distribution kernels nowadays tend to be built with STRICT_DEVMEM and IO_STRICT_DEVMEM which restrict access via /dev/mem to a few ranges in memory, either required for DOSEMU or X, or mapped to devices and otherwise unused (i.e. providing MMIO for a device which isn’t handled by a driver).






          share|improve this answer




















          • By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
            – JdeBP
            1 hour ago










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475805%2fis-memory-mapped-i-o-only-used-internally-by-os-not-exposed-to-and-used-by-prog%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote













          Memory mapped IO is done by mmap()ing a region of a file and then using the mapped data.



          If you use a modern OS, the OS does most IO mmapped internally:



          • map parts of the file into a transient kernel area


          • copyout() the mapped data to the user address space. This causes the file content to be faulted in to the transient memory area of the kernel.


          • unmap the region


          BTW: if you are referring to accessing hardware from user space, this is usually done e.g. by the X Server that mmap()s the hardware from the graphics board and accesses it from user space.



          The device registers are accessible as memory addresses in that case and you just write code that looks as if you were inside the kernel. In other words: you do not use read() or write() but just dereference pointers that point to the addresses of the hardware registers.






          share|improve this answer






















          • I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
            – schily
            2 hours ago











          • @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
            – Tim
            1 hour ago











          • I added an explanation on how the device registers are accessed in such a case.
            – schily
            26 mins ago














          up vote
          4
          down vote













          Memory mapped IO is done by mmap()ing a region of a file and then using the mapped data.



          If you use a modern OS, the OS does most IO mmapped internally:



          • map parts of the file into a transient kernel area


          • copyout() the mapped data to the user address space. This causes the file content to be faulted in to the transient memory area of the kernel.


          • unmap the region


          BTW: if you are referring to accessing hardware from user space, this is usually done e.g. by the X Server that mmap()s the hardware from the graphics board and accesses it from user space.



          The device registers are accessible as memory addresses in that case and you just write code that looks as if you were inside the kernel. In other words: you do not use read() or write() but just dereference pointers that point to the addresses of the hardware registers.






          share|improve this answer






















          • I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
            – schily
            2 hours ago











          • @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
            – Tim
            1 hour ago











          • I added an explanation on how the device registers are accessed in such a case.
            – schily
            26 mins ago












          up vote
          4
          down vote










          up vote
          4
          down vote









          Memory mapped IO is done by mmap()ing a region of a file and then using the mapped data.



          If you use a modern OS, the OS does most IO mmapped internally:



          • map parts of the file into a transient kernel area


          • copyout() the mapped data to the user address space. This causes the file content to be faulted in to the transient memory area of the kernel.


          • unmap the region


          BTW: if you are referring to accessing hardware from user space, this is usually done e.g. by the X Server that mmap()s the hardware from the graphics board and accesses it from user space.



          The device registers are accessible as memory addresses in that case and you just write code that looks as if you were inside the kernel. In other words: you do not use read() or write() but just dereference pointers that point to the addresses of the hardware registers.






          share|improve this answer














          Memory mapped IO is done by mmap()ing a region of a file and then using the mapped data.



          If you use a modern OS, the OS does most IO mmapped internally:



          • map parts of the file into a transient kernel area


          • copyout() the mapped data to the user address space. This causes the file content to be faulted in to the transient memory area of the kernel.


          • unmap the region


          BTW: if you are referring to accessing hardware from user space, this is usually done e.g. by the X Server that mmap()s the hardware from the graphics board and accesses it from user space.



          The device registers are accessible as memory addresses in that case and you just write code that looks as if you were inside the kernel. In other words: you do not use read() or write() but just dereference pointers that point to the addresses of the hardware registers.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 26 mins ago

























          answered 2 hours ago









          schily

          9,80031537




          9,80031537











          • I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
            – schily
            2 hours ago











          • @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
            – Tim
            1 hour ago











          • I added an explanation on how the device registers are accessed in such a case.
            – schily
            26 mins ago
















          • I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
            – schily
            2 hours ago











          • @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
            – Tim
            1 hour ago











          • I added an explanation on how the device registers are accessed in such a case.
            – schily
            26 mins ago















          I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
          – schily
          2 hours ago





          I would not use the term memory mapped IO in relation to UNIX while talking about hardware access. If you however like to access hardware from user space, you need special privileges to be allowed to mmap() the addresses that are used by interface hardware. Then you can use the hardware from user space as if you were in the kernel.
          – schily
          2 hours ago













          @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
          – Tim
          1 hour ago





          @StephenKitt In MMIO, are the device controllers' registers and memory exposed as device files, so that system calls including read(), write(), and mmap() can apply to the device files?
          – Tim
          1 hour ago













          I added an explanation on how the device registers are accessed in such a case.
          – schily
          26 mins ago




          I added an explanation on how the device registers are accessed in such a case.
          – schily
          26 mins ago












          up vote
          3
          down vote













          On Linux, MMIO is possible from user-space using mmap on /dev/mem. For example, the X server does



          fd = open("/dev/mem", O_RDWR);
          if (ioBase == NULL) PROT_WRITE,
          MAP_SHARED, fd, ioBase_phys);

          close(fd);


          in some cases. This is going out of fashion though and the kernel strictly controls what can be done using this type of access: accessing /dev/mem requires CAP_SYS_RAWIO, and distribution kernels nowadays tend to be built with STRICT_DEVMEM and IO_STRICT_DEVMEM which restrict access via /dev/mem to a few ranges in memory, either required for DOSEMU or X, or mapped to devices and otherwise unused (i.e. providing MMIO for a device which isn’t handled by a driver).






          share|improve this answer




















          • By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
            – JdeBP
            1 hour ago














          up vote
          3
          down vote













          On Linux, MMIO is possible from user-space using mmap on /dev/mem. For example, the X server does



          fd = open("/dev/mem", O_RDWR);
          if (ioBase == NULL) PROT_WRITE,
          MAP_SHARED, fd, ioBase_phys);

          close(fd);


          in some cases. This is going out of fashion though and the kernel strictly controls what can be done using this type of access: accessing /dev/mem requires CAP_SYS_RAWIO, and distribution kernels nowadays tend to be built with STRICT_DEVMEM and IO_STRICT_DEVMEM which restrict access via /dev/mem to a few ranges in memory, either required for DOSEMU or X, or mapped to devices and otherwise unused (i.e. providing MMIO for a device which isn’t handled by a driver).






          share|improve this answer




















          • By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
            – JdeBP
            1 hour ago












          up vote
          3
          down vote










          up vote
          3
          down vote









          On Linux, MMIO is possible from user-space using mmap on /dev/mem. For example, the X server does



          fd = open("/dev/mem", O_RDWR);
          if (ioBase == NULL) PROT_WRITE,
          MAP_SHARED, fd, ioBase_phys);

          close(fd);


          in some cases. This is going out of fashion though and the kernel strictly controls what can be done using this type of access: accessing /dev/mem requires CAP_SYS_RAWIO, and distribution kernels nowadays tend to be built with STRICT_DEVMEM and IO_STRICT_DEVMEM which restrict access via /dev/mem to a few ranges in memory, either required for DOSEMU or X, or mapped to devices and otherwise unused (i.e. providing MMIO for a device which isn’t handled by a driver).






          share|improve this answer












          On Linux, MMIO is possible from user-space using mmap on /dev/mem. For example, the X server does



          fd = open("/dev/mem", O_RDWR);
          if (ioBase == NULL) PROT_WRITE,
          MAP_SHARED, fd, ioBase_phys);

          close(fd);


          in some cases. This is going out of fashion though and the kernel strictly controls what can be done using this type of access: accessing /dev/mem requires CAP_SYS_RAWIO, and distribution kernels nowadays tend to be built with STRICT_DEVMEM and IO_STRICT_DEVMEM which restrict access via /dev/mem to a few ranges in memory, either required for DOSEMU or X, or mapped to devices and otherwise unused (i.e. providing MMIO for a device which isn’t handled by a driver).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Stephen Kitt

          151k23335402




          151k23335402











          • By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
            – JdeBP
            1 hour ago
















          • By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
            – JdeBP
            1 hour ago















          By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
          – JdeBP
          1 hour ago




          By the definition quoted in the question, the framebuffer counts as memory-mapped I/O space. So there's another mmap() possibility.
          – JdeBP
          1 hour ago

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475805%2fis-memory-mapped-i-o-only-used-internally-by-os-not-exposed-to-and-used-by-prog%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery