เกี่ยวกับ hardware port ใน windows
Size:2408 Byte Create: 28/2/2548 Time: 19:35:19

http://www.cpe.mut.ac.th/Question.asp?GID=1254
Port Access under Windows

Direct Port Access is possible under Windows 98,95 & ME but versions of Windows based on the NT Architecture do not support Direct Port Access. These include Window NT, 2000 and XP. The main reason being their increased security. Any program attempting to do so will cause an Error. But this can be o
vercome by means of Kernel Mode Drivers. The Concept is explained below.

The Operating system identifies four privilege levels, numbered from 0 to 3, where greater number means lesser privilege. The primary reason to use these privileges is to improve the reliability of operating systems.


Program or tasks running at Level 1 and 2 can have direct access to system hardware but Level 3 program will not have such privileges. If however it attempts to do so it will cause Privileged Instruction Fault. Level 3 is where most of normal user programs run. In order to overcome this, the program
in Level 3 should communicate to a driver called a Kernel mode Driver running in either Level 2 or 1 by means of Gates to perform port operations on behalf of the Level 3 Program. Level 1/2 driver will do this by modifying the x86-processors IOPM (Input Output Permission Map).

Note: The above theory applies only to programs based on Windows 32 Architecture. Surprisingly 16-Bit MS-DOS programs running under Windows NT, 2000 or XP have direct port access, may be because they run under NTVDM (NT Virtual Dos Mode) which emulates the actual DOS environment. How ever that too i
s achieved by a trick! And the trick is to place the OUT command under loop for at least 20 times under Windows XP and 2 times under 2000. This following Assembly code fragment describes exactly that:

MOV DX,03BcH ;Port Address
MOV AL,255 ;Data
MOV CX,20 ;loop 20 times

INLOOP:
OUT DX,AL
LOOP INLOOP ;loop 20 times


Note that the previous code fragments will not Run under windows NT, 2000 or XP.

To test the privilege level theory the following Visual C Code with inline Assembly code can be tested. (It will give unhandled exception: PI Fault on Windows NT, 2000 or XP )

#include
#include

void main()
{
printf("Press any key and this program will give an error on Win NT,2K or XP");
getch();


__asm{
mov al,182
mov dx,0x03bc
out dx,al
}


}