Driver Development for Windows 64-bit
Questions concerned with the release of drivers for 64-bit versions of Windows are considered in this article. Some typical problems and the ways of their solutions are listed in the article as well as tools which simplify the process of drivers developing.
The output of the first version of the Windows operating system for 64-bit AMD64 processors in 2005 posed a new problem for driver developers. This problem was the opportunity to release drivers for 32-bit and 64-bit systems. At that time many manufacturers turned out not to be ready for supporting of drivers for two systems, that's why many devices didn't get any renewed drivers. At that time this was excusable because 64-bit systems were a novelty and only few users could appreciate them. However, the situation changed greatly when Windows Vista x64 was released. Now even home users want to have a 64-bit system and the developers have to think about the release of 64-bit versions of drivers without fail.
First of all there arises a question. "Do the 64-bit drivers for 64-bit versions of Windows have to be developed?" And there comes a definite answer: "They do". The point is that it is impossible to use 32-bit drivers with a 64-bit system in the kernel mode.
While developing drivers for 64-bit system one should take into consideration some peculiarities. The neglect of them can possibly lead to vain attempts to understand why some things do not function as they used to. MSDN covers the question of 64-bit drivers development thoroughly enough, but sometimes the information turns out to be too uncoordinated. That's why this article gives a survey of main problems and directions for the further study of this question.
While developing drivers for Win64 it is desirable to use the same source code that was used for Win32. The differences in code should be brought to separate code sections under conditional compilation.
- Let's enumerate some important theses which are concerned with 64-bit drivers.
- The support of DMA (Direct Memory Access).The inclusion of support of 64-bit addressing in the driver allows to increase the productivity considerably.
- The support of 32-bit in-out. If a driver uses DMA procedures correctly with Windows 2000, it should also function normally with 64-bit systems.
- A driver must support both 32-bit and 64-bit versions of IOCTL commands.
It is necessary to mention the fact that Microsoft provides the developers with instructions of code migration for new versions of Windows DDK as much as possible.
Possible Code Problems
The list of possible problems of code migration is given in [1, 2]. But these problems are only named there. And here we shall consider variants of their solutions in detail.
So, MSDN recommends us to use:
- New data types, secure from the point of view of Win64. These are, first of all, types of fixed length as well as types meant especially for pointer storage.
- Correct format lines in printf-like functions. As the size of pointers with 32-bit and 64-bit is different, so we can't use one and the same format line. The perfect solution here is total refusal to use such functions instead of using less dangerous analogues.
- When it is necessary to cut off a pointer, one should use PtrToLong and PtrToUlong functions. Yet, you'd better alter the code in such a way that this operation becomes unnecessary. The matter is that the pointer becomes incorrect after such an operation.
- One should use FIELD_OFFSET and TYPE_ALIGNMENT macros instead of explicit evaluation of the position of variable in the structure or manual alignment calculation. This is concerned with different types of alignment and the size of pointers.
Special attention should be paid to:
- Operations with signed variables and unsigned variables. The questions of variable type conversion often cause misunderstanding; that's why it's better to avoid them.
- Function call, where one of the parameters is a pointer. The potential error may be located both inside the function and outside of it when the result of its work is used.
- The use of magic constants which may change depending upon the digit capacity of the system. Notice, that the number -1 with a 64-bit system is not equal to 0xffffffff as it used to be with a 32-bit one. What is this number equal to then? Naturally, it is equal to 0xffffffffffffffff, whereas 0xffffffff equals only to 0x00000000ffffffff.
- Structures which contain pointers. The changes of address space occurred in 64-bit systems may directly affect the functioning of drivers.
- The conversion of pointers to some data types (int, long).
To check the efficiency of drivers it is advisable to use appropriate tools. In addition to the maximum level of compiler diagnostics (/W4 or even /Wall) it is necessary to use other opportunities without fail.
First of all, take a note of a free tool created by Microsoft, PREfast analyzer if you are not aware of it yet. It's a static code analyzer; it discovers a great number of errors which can be found in common programs besides the errors we have considered above.
Another static code analyzer, Viva64, is meant for search of the errors in C++ programs which appear when the code migration from 32-bit systems to 64-bit ones is performed.
As you may see, the development of 64-bit drivers is quite easy and the tools which can facilitate this work are available. And this means that you don't have to put off the work.
- Designing for 64-bit Windows. https://msdn.microsoft.com/en-us/library/windows/hardware/dn613903(v=vs.85).aspx.
- Porting Issues Checklist. https://msdn.microsoft.com/en-us/library/ff559744.aspx.