Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the access to Linux IO memory

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly talks about "how to access Linux IO memory". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to access Linux IO memory.

IO memory access process

We know that in order to manage the most important system resources and make physical addresses transparent to processes, Linux uses a memory mapping mechanism, that is, if a process wants to access a physical memory address (eg.SFR address), it first maps it to a virtual address.

IO memory request / return

Linux provides a set of functions for applying for and releasing the scope of IO memory. These two API are not necessary when accessing IO memory, but they are recommended. They can check whether the requested resources are available and increase the security of IO access. If available, the application is successful and marked as used. Other drivers will fail before the process returns the resources.

The request_mem_region () macro function requests n memory addresses from memory, starting with first, where len is long, name represents the name of the device, and NULL is returned if it returns non-NULL successfully.

/ * request_mem_region-create a new busy resource region * @ start: resource start address * @ n: resource region size * @ name: reserving caller's ID string * / struct resource * request_mem_region (resource_size_t start, resource_size_t nMae Const char * name)

The release_mem_region () macro function, as its name implies, returns the IO memory resource requested by request_mem_region () to the kernel so that other processes can access the IO memory as well.

/ * release_mem_region-release a previously reserved resource region * @ start: resource start address * @ n: resource region size * / void release_mem_region (resource_size_t start, resource_size_t nmeme Const char * name)

IO memory mapping / de-mapping

After applying for the IO resource, the next step is to map the physical address to the virtual address. The API provided by the kernel is as follows

Static inline void _ _ iomem * ioremap (unsigned long port, unsigned long size) static inline void iounmap (volatile void _ _ iomem * addr)

IO memory access API

ARM's SFR is 32bit, and we can actually read the obtained virtual address directly through forced type conversion after ioremap, but this method is not safe enough, it will be misplaced if we are not careful. For this reason, the kernel also provides standard API to read and write IO memory, which not only has higher code security, but also improves readability.

Read IO

Unsigned int ioread8 (void * addr) unsigned int ioread16 (void * addr) unsigned int ioread32 (void * addr)

Write IO

Void iowrite8 (U8 val,void * addr) void iowrite16 (U8 val,void * addr) void iowrite32 (U8 val,void * addr)

Read a string of IO memory

Void ioread8_rep (void * addr,void * buf,unsigned long len) void ioread16_rep (void * addr,void * buf,unsigned long len) void ioread32_rep (void * addr,void * buf,unsigned long len)

Write a string of IO memory

Void iowrite8_rep (void * addr,const void * buf,unsigned long len) void iowrite16_rep (void * addr,const void * buf,unsigned long len) void iowrite32_rep (void * addr,const void * buf,unsigned long len)

Copy IO memory

Void memcpy_fromio (void * dest,void * source,unsigned long len) void memcpy_toio (void * dest,void * source,unsigned long len)

Set up IO memory

Void memset_io (void * addr,u8 value,unsigned int len) so far, I believe you have a deeper understanding of "how to access Linux IO memory". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report