znsoft
管理员
管理员
  • 注册日期2001-03-23
  • 最后登录2023-10-25
  • 粉丝300
  • 关注6
  • 积分910分
  • 威望14796点
  • 贡献值7点
  • 好评度2410点
  • 原创分5分
  • 专家分100分
  • 社区居民
  • 最爱沙发
  • 社区明星
阅读:6615回复:2

64位驱动下使用汇编代码的技巧

楼主#
更多 发布于:2009-04-09 12:55
Programatically update the driver for a device
So you need to change the driver loaded for a given device from your application. You have a couple of options to do this.

The first method is to use the UpdateDriverForPlugAndPlayDevices API. This method is simple and straightforward to use. But the downside of using this API is that it will update drivers for all devices with the specified hardware id. So if you need to selectively update one of the devices with a new driver you will have to look for something else.

And that method is by using the InstalledSelectedDriver API.

Sample Code: How to use InstallSelectedDriver to update driver ? posted @ Wednesday, November 12, 2008 3:00 AM | Feedback (0)

Monday, April 28, 2008 #
USB Monitor for Windows
This post is about USBTrace,  a software based USB monitor / USB sniffer , which can be used to capture & analyze transactions happening in the USB bus. This tool is helpful for USB firmware/device driver developers while debugging and testing their device implementation. USB requests are captured and displayed in easly readable format to make analyzing easy.  USB requests passing though different layers of the USB device stack are captured and displayed. Helps developers to highly reduce their debug session's time. Supports USB 1.x and 2.0 host controllers, hubs and devices.

Download : http://www.sysnucleus.com/usbtrace_download.html

Keywords : USB Monitor , USB Analyzer ,  USB Protocol Analyzer , USB Sniffer posted @ Monday, April 28, 2008 10:50 PM | Feedback (1)

Friday, March 07, 2008 #
X64 Assembly Code in Windows Drivers
Writing asm code for 32 bit drivers is straightforward. You can embed the code in an __asm { } block.

void DemoFunction()
{
    __asm
    {
          mov eax, 0x01
          ; more assembly
    }
}


But writing assembly in 64 bit driver source code requires a bit more work. The 64 bit compiler will not allow inline assembly. The assembly code will have to be moved to a seperate assembly module (an .asm file).

Step 1 : Write necessary assembly routines in a seperate .asm file

Example : Test.asm
------------------------------------------

.data

; all data variables in your asm code goes here
myData1   dq   0   ; 64 bit data


.code

; all assembly routines go here

TestFunction PROC

    ; sample function/routine/procedure

    ; assembly code for the function goes here

    ret
    
TestFunction ENDP

END ; end of assembly file

Step2 :  Integrate assembly function with C

In one of your C header files declare the function:

extern void TestFunction(void);

Step 3 : Adding asm file to sources file

In the sources file of your driver you can add the .asm file along with other C files.

Example:

SOURCES = init. c \
ioctl.c \
pnp.c\
power.c\
Test.asm

You can add the same under AMD64_SOURCES or IA64_SOURCES if you required to include the same only in those specific architectures.


posted @ Friday, March 07, 2008 2:27 AM | Feedback (11)

Thursday, March 06, 2008 #
X64 Calling
Computers with 64 bit processors are becoming popular, at least in enterprise circles. Also the X64 version of Windows Vista is more popular than Windows XP 64 bit edition.

2 Types of 64 bit architecture

Yes, unlike 32 bit (aka X32) there are 2 64 bit architectures:

  1. AMD's X64 ; also known as X32-64 & AMD64
  2. Intel's IA-64; also known as Itanium
The subject of this post is AMD's 64 bit chip, which is commonly referred to as X64

So, whats the difference ?

The X64 architecture is a super set of  X32 architecture :

  • 64 bit versions of the the existing 32 bit registers
    • So X32's 32 bit registers EAX, EBX, ECX etc becomes 64 bit RAX, RBX, RCX etc in X64
  • 8 new 64 bit general purpose registers (R8, R9...R15)
  • 8 new 128 bit XMM registers
To know more about the architecture goto [url]http://www.amd.com/us-en/Processors/DevelopWithAMD/0,,30_2252_875_7044,00.html[/url]

And how is programing different ?

A lot has been written about porting existing 32 bit code to 64 bit. A lot of these deals with change in sizeof pointers (from 32 bit to 64 bit) and change in the sizeof some of the basic data types (this depends on the compiler which you are using)

Links:

Porting device drivers to AMD64
[http://www.amd.com/us-en/assets/content_type/DownloadableAssets/dwamd_Porting_Win_DD_to_AMD64_Sept24.pdf]

64 bit driver guidelines
[http://www.microsoft.com/whdc/driver/64bitguide.mspx]

20 issues of porting C++ code on the 64-bit platform
[http://www.viva64.com/articles/20_issues_of_porting_C++_code_on_the_64-bit_platform.html]

..assembly programming ?

Not many program in assembly languages these days. But if it occurs to you there are a few things to keep in mind.

Source Link : http://www.quequero.org/X64_Assembly

Win32 on X32 provided us with many calling conventions (function calling conventions : fastcall, stdcall etc). In X64 there is no choice. There is only one calling convention:

The first parameter is the rcx register, the second one rdx, the third r8 and the fourth r9. Saying that the parameters registers are part of the stack frame, makes it also clear that any function that calls another child function has to initialize the stack providing space for these four registers, even if the parameters passed to the child function are less than four.
The initialization of the stack pointer is done only in the prologue of a function, it has to be large enough to hold all the arguments passed to child functions and it's always a duty of the caller to clean the stack. Now, the most important thing to understand how the space is provided in the stack frame is that the stack has to be 16-byte aligned.
In fact, the return address has to be aligned to 16 bytes. So, the stack space will always be something like 16n + 8, where n depends on the number of parameters. Here's a small figure of a stack frame:

Stack Parameters (5th param onwards)
Register Parameters (Space for 4 Reg params)
Return IP address (RIP)
Local Variables of the function


If you see the disassembly of a 64 bit program, you can see that the stack pointer (RSP) is not messed with throughout the function body. Necessary stack is reserverd ( Sub RSP, 0x[ReqSize] ) in the function prolog.

Another important thing to note is that even though the first 4 parameters are passed via registerd (RCX, RDX, R8 and R9) they must be given scratch storage space in the stack (Register Parameters in the above figure/call stack).

So while porting asm from 32bit to 64bit, if you have a void routine

call MyRoutinemust be changed as:
sub rsp, 20h      ; Reserve space for register parameters

call MyRoutine
add rsp, 20h

Another difference found was in X64 the luxury of PUSHA/PUSHD POPA/POPD (Push/Pop all registers and flags) is not available.

Footnotes
  • MS Visual Studio 2005 lets you build 64 bit applications. The 64 bit compiler modules are not included in the installation by default.
  • VS 2005 has options to turn on 64bit compile warning (probable errors) for your 32 bit code; so you can check whether your 32bit code is 64bit ready
  • Windows Server 2003 DDK and above comes with MASM64 for writing asm modules.
  • 32 bit applications can run over 64 bit windows (WOW64 http://en.wikipedia.org/wiki/WOW64)
  • 32 bit drivers cannot be used to 64 bit windows.

http://www.zndev.com 免费源码交换网 ----------------------------- 软件创造价值,驱动提供力量! 淡泊以明志,宁静以致远。 ---------------------------------- 勤用搜索,多查资料,先搜再问。
fazwh
驱动牛犊
驱动牛犊
  • 注册日期2005-09-11
  • 最后登录2020-11-18
  • 粉丝0
  • 关注0
  • 积分32分
  • 威望303点
  • 贡献值0点
  • 好评度48点
  • 原创分0分
  • 专家分0分
  • 社区居民
沙发#
发布于:2010-06-29 12:24
好贴。受益匪浅!

请教另外一个问题:我在Intel CPU上安装win2k3 sp2 x64 ,检测wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64。也就是说安装在Intel CPU机器上的检查为AMDx64,这正常吗?
fazwh
驱动牛犊
驱动牛犊
  • 注册日期2005-09-11
  • 最后登录2020-11-18
  • 粉丝0
  • 关注0
  • 积分32分
  • 威望303点
  • 贡献值0点
  • 好评度48点
  • 原创分0分
  • 专家分0分
  • 社区居民
板凳#
发布于:2010-06-29 12:29
自问自答了。

查看了一下MSDN相关介绍,应该是正常的。

详情查看 http://msdn.microsoft.com/en-us/library/ms724958(VS.85).aspx
游客

返回顶部