2007年7月20日 星期五

How to load a driver into user mode

在CE6.0中一般driver default都是run在kernel mode
要如何將driver load 到 user mode?
其實很簡單
只需要設定platform.reg中Flags的value為 DEVFLAGS_LOAD_AS_USERPROC(0x10)
n

及platform.bib即可。

以上次建出來的Driver為範例。

platform.reg
//===============================
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Cin]
"Dll" = "CIN.dll"
"Prefix" ="CIN"
"Index"= dword:1
"Order"= dword:0
"FriendlyName" = "CIN Demo Driver"
"Ioctl" = dword:0
"Flags" = dword:10
; Flags==0x10 is DEVFLAGS_LOAD_AS_USERPROC

//===============================

platform.bib
//===============================
CIN.dll $(_FLATRELEASEDIR)\CIN.dll NK SH
//===============================


run 出來的結果


千萬要注意哦
bib檔的SHK要改成SH哦,沒改的話會出現錯誤。
run出來的結果

How to create a CE 6.0 driver

其實建CE 6.0 的driver與5.0的雷同,先前也曾介紹過。
1.首先在BSP底下建你要的driver folder。

\WINCE600\PLATFORM\DEVICEEMULATOR\SRC\DRIVERS


2.接著建立所需的makefile、.def、sources、.c(or .cpp)檔。
內容設定如下:簡略說明,可參閱stream driver文章。
D:\WINCE600\PLATFORM\DEVICEEMULATOR\SRC\DRIVERS\CINTEST


.def檔

//=========================================

LIBRARY CINTEST // driver folder 名稱

EXPORTS
// 要export 的function
XXX_Init

XXX_Deinit
XXX_Open
XXX_Close
XXX_IOControl
XXX_PowerUp
XXX_PowerDown
XXX_Read
XXX_Write
XXX_Seek

//=========================================


source檔
//=========================================
SYNCHRONIZE_DRAIN=1

TARGETNAME=CIN //.dll的名稱
RELEASETYPE=PLATFORM
TARGETTYPE=DYNLINK

TARGETLIBS= \
$(_SYSGENSDKROOT)\lib\$(_CPUINDPATH)\coredll.lib \ //會呼叫到的library

SOURCES= \
cin.c \\用到的.c檔
//=========================================


3.修改platform.reg和platform.bib檔
\WINCE600\PLATFORM\DEVICEEMULATOR\FILES

platform.reg
//=========================================
[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\Cin]
"Dll" = "CIN.dll"
"Prefix" ="CIN"
"Index"= dword:1
"Order"= dword:0
"FriendlyName" = "CIN Demo Driver"
"Ioctl" = dword:0

//=========================================


platform.bib
//=========================================
CIN.dll $(_FLATRELEASEDIR)\CIN.dll NK SHK
//=========================================

2007年7月13日 星期五

system calls

In compupting, a system call is the mechanism used by an application program to request service from the operating system.
簡言之就是應用程式與作業系統的介面

2007年7月4日 星期三

User Mode Driver Framework

User Mode Driver Framework Development Concepts
  • allows an intermediate driver to load in user mode
  • cannot directly access hardware
  • some drivers will be more stable in user mode
  • reflector in kernel makes a user mode driver work as if it was a kernel mode driver
User Mode Driver Framework Architecture



The user mode driver framework is divided into two physical components.
  • the user mode driver reflector, which reside inside the device manager.
    • manages the user mode driver host processor.
    • forwards the requests that are issued by the device manager.
    • provides services for a user mode driver.
  • the user mode driver host, which is a user mode application that is launched and managed bye the user mode driver reflector.
    • it is used at the kernel's request to launch and to manage a user mode driver.
    • it is responsible for mounting the API of a volume in order to receive forwarded requests from the user mode driver reflector.
Registry
  • stores configuration information for applications, drivers, and the OS.
  • OS uses registry to locate and load device drivers.
  • Same basic setup as the desktop windows OS registry.
  • space is limited, so keep any new entries short.
Device Manager
  • manages devices and their interfaces.
  • decides which drivers to load.
  • uses registry to find drivers.
  • runs continuously.

Kernel Mode Drviers VS. User Mode Drivers

The development team at Microsoft has made a number of changes to the architecture of Windows CE in version 6.0. One of these is a new model for loading device drivers that can provide for increased security, robustness or performance.

Prior to CE6, device drivers primarily resided in the device.exe process.
The new kernel architecture provides more flexibility by providing two different driver models. The old device.exe functionality has been moved into the kernel, and a new user device process (udevice.exe) has been introduced. This change results in two different driver models, kernel mode drivers and user mode drivers. BSP developers must choose how each of their drivers will be implemented on a particular platform.

Kernel Mode Drivers

Kernel mode drivers are loaded by device.dll inside of the kernel. This is the default behavior, and most closely resembles the old device.exe model. The big win here is performance – the expensive interprocess calls (thunks) have been eliminated. There is no need for the user mode process initiating a driver access to be switched out, because the user mode process coexists with the kernel. The kernel includes the file system and device drivers so everything is resident in memory at the same time.

This new performance benefit comes at a price. Kernel mode drivers are now a part of the kernel, and they have full privileges for the entire kernel address space. A driver failure that results in memory corruption could easily bring down the kernel and with it the entire system. Therefore it’s critical that kernel mode drivers be robust.


User Mode Drivers

CE6 provides a new mechanism to load drivers into individual user mode processes (called udevice.exe) instead of into the kernel. A driver that loads in this fashion is called a user mode driver, and it gives up a level of performance in return for increased system robustness and security. In addition, since user mode drivers run inside a user mode process they are restricted in their use of certain APIs . This security feature restricts drivers loading in user mode from arbitrarily accessing any hardware resource in the system.

Microsoft has designed the user mode driver model to have a very high level of compatibility with kernel mode drivers. There are very few differences between the two models, and a driver written for user mode can be loaded into kernel mode with no changes. A new kernel component called the user mode driver reflector handles the interface between user mode applications and the user mode driver. This means that applications do not need to be aware of how a particular driver was loaded.

It’s a simple matter to load a driver into user mode instead of the default kernel mode. There is a new bit defined in the Flags registry key that if set tells the kernel to load the corresponding driver into user mode. That’s it – nothing more to it. The user mode driver will run in its own process isolated from the kernel and the rest of the system. If the driver fails for some reason only its copy of udevice.exe is affected, the rest of the system should remain intact.

BSP developers now have the option of choosing between a high performance kernel mode driver and a more protected user mode driver. Untrusted third party drivers can be loaded into user mode to increase system security and robustness. Unstable or otherwise questionable drivers can start life in user mode and then be moved to kernel mode as they become proven.


參考來源:http://www.bsquare.com/blog/default-aug_21.asp