开发者

Direct access to memory devices under Windows / Linux

开发者 https://www.devze.com 2023-01-21 06:28 出处:网络
Hello I\'m currently writing a software to test the integrity of flash-memory devices. (using c++ iostreams)

Hello

I'm currently writing a software to test the integrity of flash-memory devices. (using c++ iostreams)

To achieve this goal files with random content are written to the device, read back, copied, moved, compared ( I put the device under stress condition, and check if the data is valid from time to time)...

Everything looks great on the outside, but there is a problem behind the curtains.

When I analyzed the commands that are sent to the device over the ATA / SATA Cable (with a Bus Doctor), I discovered that (especially) read commands are often not executed. Nevertheless the data is availible to the program. So they have to come from a buffer, which is not acceptable in my case. Whenever I read Data it has to come straight from the disk, not from the cache. Read / Write Performance isn't important at all.

But I haven't found a way to tell Windows not to buffer anything (There is the option to deactivate write caching for a specific device in the device manager, but doesn't have the desired effect).

If anyone knows how to do this: Please tell me

( same goes for any advice concerning Linux. I think I will run into the same problems there )

Only solution I could come up with so far is Direct I/O.

What I found for Windows is this:

http://www.internals.com/utilities_main.htm

The WinIo library allows 32-bit Windows applications to directly access I/O ports and physical memory. It bypasses Windows protection mechanisms by using a combination of a kernel-mode device driver and several low-level programming techniques.

and http://www.direct-io.com/

Direct I/O is the world's first device driver for Microsoft Windows NT, Windows 2000, Windows XP and Windows Server 2003 which enables the direct hardware access for your existing software without any programming efforts on your side

Questions:

Has somebody ever had a similar problem, and can give me some advice?

Do I really have to go with D开发者_高级运维irect I/O or is there some OS option/setting I haven't found (Windows or Linux)

Do you know other libraries for Direct I/O (Windows or Linux or even better: both)


For Windows you can disable buffering using the Win32 API directly.

When opening or creating a file with the CreateFile function, the FILE_FLAG_NO_BUFFERING flag can be specified to disable system caching of data being read from or written to the file. Although this gives complete and direct control over data I/O buffering, in the case of files and similar devices there are data alignment requirements that must be considered.

Not sure if this can be done in the C++ standard library. You can disable buffering at the iostream level but that may not affect the O/S buffering.


Under Linux you can use the O_DIRECT flag when opening the device, and then all the accesses will bypass the page cache and go directly to the device.

Note that O_DIRECT reads and writes must be aligned to the device's block size (usually 512 bytes) in memory addresses, file offsets and read/write request sizes.


You can access any place in memory in Linux under /proc/kcore. So if you know which address you device's memory is mapped to (something you can figure out by looking more through the /proc directory), you can read/write directly to it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号