windows内核技术介绍

40
Windows Kernel Technologies 潘爱民 2010-9-11

Upload: jeffz

Post on 10-May-2015

3.536 views

Category:

Technology


9 download

TRANSCRIPT

Page 1: Windows内核技术介绍

Windows Kernel Technologies

潘爱民2010-9-11

Page 2: Windows内核技术介绍

Outline

• Windows Architecture Overview

• System Trap

• Windows I/O Model & Device Drivers

• Windows Subsystem

Page 3: Windows内核技术介绍

NT Timeline: first 20 years

2/1989 Design and Coding Begins

7/1993 NT 3.1

9/1994 NT 3.5

5/1995 NT 3.51

7/1996 NT 4.0

12/1999 NT 5.0 Windows 2000

8/2001 NT 5.1 Windows XP

3/2003 NT 5.2 Windows Server 2003

8/2004 NT 5.2 Windows XP SP2

4/2005 NT 5.2 Windows XP 64 Bit Edition (WS03SP1)

10/2006 NT 6.0 Windows Vista (client)

2/2008 NT 6.0 Windows Server 2008 (Vista SP1)

10/2009 NT 6.1 Win7 & Server 2008 R2

Page 4: Windows内核技术介绍

Windows Architecture

系统和服

务进程 系统和服

务进程 系统和服

务进程

系统和服

务进程 系统和服

务进程

Windows 子

系统进程

NTDLL.DLL

用户模式

内核模式 执行体(Executive)

内核(或微内核)

硬件抽象层(HAL)

设备驱动

程序

Windows 子系

统内核模块

系统 DLL

应用程序 .NET

Page 5: Windows内核技术介绍

About Windows APIs

• Provided by Windows Subsystem DLLs

• Three types: ① No kernel-mode interactions are needed. (e.g. PtInRect,

IsRectEmpty, GetCurrentProcess);

② Call into kernel once or more (e.g. CreateFile,

PostMessage and BitBlt);

③ Communications with the Windows subsystem process

(csrss) are needed. (e.g. CreateProcess)

应用程序

Csrss进程用户模式

内核模式

系统DLL

ntoskrnl win32k

Page 6: Windows内核技术介绍

Call into Kernel 应用程序

Ntdll.dll

用户模式

内核模式

系统 DLL,如 Kernel32.dll、User32.dll 等

KiSystemService

服务分发

执行体系统服务

int 2e/sysenter

iretd/sysexit

KiServiceExit

KiXxxSystemCall

系统服务存根函数

Page 7: Windows内核技术介绍

Example System Service: NtCreateFile

• The Call stack of creating a file in a user thread

f5484c94 808e3375 nt!IopCreateFile

f5484cf0 808e50ec nt!IoCreateFile

f5484d30 80882a2c nt!NtCreateFile

007deed8 7c8211f4 ntdll!KiFastSystemCallRet

007deedc 76cf2707 ntdll!ZwCreateFile+0xc

007def54 76cf276b iphlpapi!OpenTCPDriver+0xad

007def64 76cf278f iphlpapi!CheckTcpipState+0x62

007def98 76cf299a iphlpapi!GetIpStatsFromStack+0xd

007df00c 76cf32de iphlpapi!GetInterfaceInfo+0x38

007df028 76cf3230 iphlpapi!GetAdapterNameToIndexInfo+0x1e

007df060 76cf6a6b iphlpapi!GetAdapterInfo+0x18

007df0b4 4e7fdf36 iphlpapi!GetAdapterInfoEx+0x1c

007df504 4e7fe2f9 WINHTTP!CIpConfig::GetAdapterListOnNT5+0x6e

007dfac4 4e7fe938 WINHTTP!CIpConfig::GetAdapterList+0x58

007dfad0 4e7fd3ee WINHTTP!CIpConfig::CIpConfig+0x23

007dfaf0 4e7cd1cc WINHTTP!DetectAutoProxyUrl+0x26

007dfb28 4e7cd9b5 WINHTTP!CAutoProxy::DetectAutoProxyUrl+0xa4

007dfb58 4e7cdafb WINHTTP!CAutoProxy::GetProxyForURL+0x33

007dfb6c 4e7cdcd8 WINHTTP!InProcGetProxyForUrl+0x20

内核模式

用户模式

Page 8: Windows内核技术介绍

Example Call Stack with .NET

0012f2f4 77d19418 ntdll!KiFastSystemCallRet

0012f38c 7b1d8997 user32!NtUserWaitMessage+0xc

0012f3e4 7b1d87e1 System_Windows_Forms_ni+0x208997

0012f414 7b6eddc6 System_Windows_Forms_ni+0x2087e1

0012f460 79e71b4c System_Windows_Forms_ni+0x71ddc6

0012f470 79e821f9 mscorwks!CallDescrWorker+0x33

0012f4f0 79e96571 mscorwks!CallDescrWorkerWithHandler+0xa3

0012f634 79e965a4 mscorwks!MethodDesc::CallDescr+0x19c

0012f650 79e965c2 mscorwks!MethodDesc::CallTargetWorker+0x1f

0012f668 79f0788d mscorwks!MethodDescCallSite::Call+0x1a

0012f7cc 79f077ad mscorwks!ClassLoader::RunMain+0x223

0012fa34 79f07cfd mscorwks!Assembly::ExecuteMainMethod+0xa6

0012ff04 79f07ee7 mscorwks!SystemDomain::ExecuteMainMethod+0x456

0012ff54 79f07e17 mscorwks!ExecuteEXE+0x59

0012ff9c 7900b77b mscorwks!_CorExeMain+0x15c

0012ffac 7900b73d mscoree!_CorExeMain+0x2e

0012ffb8 79004de3 mscoree!ShellShim__CorExeMain+0x29

0012ffc0 7c817077 mscoree!_CorExeMain_Exported+0x8

0012fff0 00000000 kernel32!BaseProcessStart+0x23

用户模式

.NET Runtime Execution Engine

.NET Runtime Common Langueage

Runtime

Page 9: Windows内核技术介绍

Windows I/O Model• Asychronous, Packet-based, Extensible

• Device discovery supports plug-and-play (PnP)

volumes automatically detected and mounted

power management support (ACPI)

• Drivers attach to per device driver stacks

Drivers can filter actions of other drivers in each stack

• Integrated kernel support

memory manager provides DMA support

HAL provides device access, PnP manages device resources

Cache manager provides file-level caching via MM file-mapping

• Multiple I/O completion mechanisms:

synchronous

update user-mode memory status

signal events

callbacks within initiating thread

I/O Completion Port

Page 10: Windows内核技术介绍

I/O Architecture

I/O 管理器

内核模式

用户模式

PnP 管理器 电源管理器 WMI支持

驱动程序 驱动程序 驱动程序 ……

I/O

系统

HAL

配置管理器

注册表

硬件控制器 硬件控制器 ……

Page 11: Windows内核技术介绍

I/O Manager Objects

• Driver objects represent loaded drivers.

NtLoadDriver/NtUnloadDriver

IoCreateDriver

• Drivers create device objects to represent devices.

IoCreateDevice

• All I/O requests are made to device objects.

• File objects represent open instances of device objects.

Page 12: Windows内核技术介绍

Object Relationships

驱动程序 2 设备对象 2a

设备对象 3a

设备对象 2b

设备对象 1a 设备对象 1c 驱动程序 1

驱动程序 3

文件对象

#1

设备对象 1b

文件对象

#2

Page 13: Windows内核技术介绍

Structure of Windows Drivers

设备驱动程序 初始化例程 I/O 系统

I/O 管理器

PnP 管理器

电源管理器

增加设备

分发例程

卸载例程

可选例程 ……

WMI

Page 14: Windows内核技术介绍

Loading Device Drivers

• Drivers can be loaded by,

The boot loader at boot time.

The I/O manager at system initialization.

The service control manager or PnP manager.

• Driver details are obtained from the registry.

• Driver object is created and DriverEntry for the driver is invoked.

• Drivers provide dispatch routines for various I/O operations. (Create, Read, Write, „).

• Drivers can optionally provide fast path entry points.

Page 15: Windows内核技术介绍

Layering Drivers

• Device objects can be attached one on top of another using IoAttachDevice* APIs to create device stacks.

• I/O manager sends IRP to the top of the stack.

• Drivers store next lower device object in their private data structure.

• Stack tear down done using IoDetachDevice and IoDeleteDevice.

Page 16: Windows内核技术介绍

Device Stack Example

AttachedDevice = null

StackSize = 6

AttachedTo = 81682a58

81682870

AttachedDevice = 81682870

StackSize = 5

AttachedTo = 817ad030

81682a58

AttachedDevice = 81682a58

StackSize = 4

AttachedTo = null

817ad030 DriverObject -> “\Driver\ACPI”

DriverObject -> “\Driver\i8042prt”

DriverObject -> “\Driver\Kbdclass”

设备名为“\Device\0000003b”

设备名为“\Device\KeyboardClass0”

Page 17: Windows内核技术介绍

Device Deletion and Driver Unload• Drivers delete devices using IoDeleteDevice.

• Drivers are unloaded by calling NtUnloadDriver or by PnP.

• No further opens/attaches allowed after a device is marked for deletion or unload.

• Driver unload function is invoked when all its device objects have no handles/attaches.

• Driver is unloaded when the last reference to driver object goes away.

Page 18: Windows内核技术介绍

File Objects

• Also managed by Windows Object Manager, its type is IoFileObjectType

Applications and drivers “open” devices by name

The name is parsed by the Object Manager

• Representation of an open instance of a device object

files on a volume are virtual devices

• Created by IoCreateFile function, which is invoked by NtCreateFile

When an open succeeds a file handle is added to

the process handle table

Page 19: Windows内核技术介绍

I/O Processing

• IRP

• Issues in I/O processing

Fast I/O

Buffer management

I/O completion

• I/O Completion port

Page 20: Windows内核技术介绍

I/O Request Packet (IRP)

• I/O operations are encapsulated in IRPs.

• I/O requests travel down a device stack in an IRP.

• Each driver gets a stack location which contains parameters for that I/O request.

• IRP has major and minor codes to describe I/O operations.

• Major codes include create, read, write, PnP, devioctl, cleanup and close.

• Irps are associated with a thread that made the I/O request.

Page 21: Windows内核技术介绍

I/O Requests from Applications

用户模式

内核模式

系统服务 NtCreateFile, NtClose, NtReadFile, NtWriteFile, NtDeviceIoControlFile …

I/O 管理器

IoCallDriver

驱动程序 驱动程序 驱动程序 ……

系统 DLL

Page 22: Windows内核技术介绍

IRP Major Codes

#define IRP_MJ_CREATE 0x00

#define IRP_MJ_CREATE_NAMED_PIPE 0x01

#define IRP_MJ_CLOSE 0x02

#define IRP_MJ_READ 0x03

#define IRP_MJ_WRITE 0x04

#define IRP_MJ_QUERY_INFORMATION 0x05

#define IRP_MJ_SET_INFORMATION 0x06

#define IRP_MJ_QUERY_EA 0x07

#define IRP_MJ_SET_EA 0x08

#define IRP_MJ_FLUSH_BUFFERS 0x09

#define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a

#define IRP_MJ_SET_VOLUME_INFORMATION 0x0b

#define IRP_MJ_DIRECTORY_CONTROL 0x0c

#define IRP_MJ_FILE_SYSTEM_CONTROL 0x0d

#define IRP_MJ_DEVICE_CONTROL 0x0e

#define IRP_MJ_INTERNAL_DEVICE_CONTROL 0x0f

Page 23: Windows内核技术介绍

IRP Major Codes (cont.)

#define IRP_MJ_SHUTDOWN 0x10

#define IRP_MJ_LOCK_CONTROL 0x11

#define IRP_MJ_CLEANUP 0x12

#define IRP_MJ_CREATE_MAILSLOT 0x13

#define IRP_MJ_QUERY_SECURITY 0x14

#define IRP_MJ_SET_SECURITY 0x15

#define IRP_MJ_POWER 0x16

#define IRP_MJ_SYSTEM_CONTROL 0x17

#define IRP_MJ_DEVICE_CHANGE 0x18

#define IRP_MJ_QUERY_QUOTA 0x19

#define IRP_MJ_SET_QUOTA 0x1a

#define IRP_MJ_PNP 0x1b

#define IRP_MJ_PNP_POWER IRP_MJ_PNP // Obsolete....

#define IRP_MJ_MAXIMUM_FUNCTION 0x1b

Page 24: Windows内核技术介绍

IO Stack Locations in IRPs

IRP 对象

栈单元[0]

栈单元[1]

栈单元[n-1]

……

原始文件对象

(OriginalFileObject) 设备对象

一组分发例程 设备对象 文件对象 驱动程序

对象

一组分发例程 设备对象 文件对象 驱动程序

对象

一组分发例程 设备对象 文件对象 驱动程序

对象

Page 25: Windows内核技术介绍

Completing an I/O Request

• Servicing an interrupt:

ISR schedules Deferred Procedure Call (DPC); dismisses int.

DPC routine starts next I/O request and completes interrupt

servicing

May call completion routine of higher-level driver

• I/O completion:

Record the outcome of the operation in an I/O status block

Return data to the calling thread – by queuing a kernel-mode

Asynchronous Procedure Call (APC)

APC executes in context of calling thread; copies data; frees

IRP;

sets calling thread to signaled state

I/O is now considered complete; waiting threads are released

Page 26: Windows内核技术介绍

Flow of I/O Processing

驱动程序

硬件设备

分发例程

IoCallDriver

同步

I/O

等待 执行其

他任务

是 否 ISR

DPC

中断

IoCompleteRequest

在发起者线程中插入 APC 例程 IopCompleteRequest,

或者,直接返回发起者线程

通知 I/O

管理器,

I/O 请求

已完成

插入 DPC

发送 I/O

命令,并

传输数据

调用者线程

IopCompleteRequest

Page 27: Windows内核技术介绍

I/O Processing in a Device Stack

(a) I/O 请求在设备栈中的传递流程

栈顶

设备对象

驱动程序 1

(分发例程)

I/O 管理器

IoCallDriver

I/O 请求

IRP

CurrentLocation

中间

设备对象

驱动程序 2

(分发例程)

IRP

CurrentLocation

栈底

设备对象

驱动程序 3

(分发例程)

IRP

CurrentLocation

栈顶

设备对象

驱动程序 1

(完成例程)

IRP

CurrentLocation

中间

设备对象

驱动程序 2

(完成例程)

IRP

CurrentLocation

栈底

设备对象

驱动程序 3

(完成例程)

IRP

CurrentLocation

(b) I/O 请求的完成流程

I/O 管理器

IoCompleteRequest

I/O 管理器

IopCompleteRequest

I/O 完成

APC

Page 28: Windows内核技术介绍

I/O Completion Port

• A kind of executive objects called I/O Completion Port or IoCompletion, its type is IoCompletionObjectType

• For balancing the I/O throughput and thread-trashing

• The completion port is essential a queue object

• A file can be associated with an IoCompletion object, so I/O manager will queue a completion packet to the completion port

• The number of active threads which process I/O completion are controlled by the queue object.

Page 29: Windows内核技术介绍

How does IOCompletion work

I/O 请求

文件对象

IopCompleteRequest

I/O 完成

I/O 完成端口

队列 KQUEUE

线程对象 线程对象

KeInsertQueue

CompletionContext KeRemovetQueue

NtCreateIoCompletion

KeInitializeQueue NtSetInformationFile

关联 NtRemoveIoCompletion

KeInsertQueue NtSetIoCompletion

Page 30: Windows内核技术介绍

Windows Subsystem

Win32k.sys

(窗口管理、图形接

口等)

用户模式

内核模式

图形设备驱动程序

Windows 子系统进程

(csrss.exe)

子系统 DLL

Windows 应用程序

Windows 内核

NTDLL.DLL

Page 31: Windows内核技术介绍

Windows Subsystem Initialization

• DriverEntry in Win32k.sys

KeAddSystemServiceTable

PsEstablishWin32Callouts

MmPageEntireDriver

InitializeGre

Win32UserInitialize

Returns Win32KDriverUnload

• Smss

• Winlogon

• Csrss

Smss 进程

创建“\SmApiPort”LPC端口对象,以接收“创建会话”请求

加载 Windows子系统内核模块 win32k.sys

启动 csrss进程

启动 winlogon进程

创建窗口站

创建登录桌面和默认桌面

启动 SCM进程

启动 lsass进程

加载自动-启动的服务和驱动程序

等待用户按下 Ctrl+Alt+Del

Page 32: Windows内核技术介绍

Convert to a GUI thread• What is a GUI thread?

Thread are non-GUI when created

Converted on first call to win32k.sys

• Bigger Stack

• Win32k.sys notified of creation and destruction

• Converts process to GUI

• PsConvertToGuiThread MmCreateKernelStack & KeSwitchKernelStack

KTHREAD->ServiceTable initialized to

ntkrnlmp!KeServiceDescriptorTable, replaced with

ntkrnlmp!KeServiceDescriptorTableShadow

Call PspW32ProcessCallout

Call PspW32ThreadCallout内核模式

ntoskrnl win32k

普通线程 GUI线程

Page 33: Windows内核技术介绍

Terminal Services/Multiple Sessions

• Multiple Sessions

Console Session

Session 0 in Vista/Win7

Terminal Sessions

by FUS (Fast User Switching)

• For each session

Created by smss (session manager process)

Session space in system space

Its own copies of Win32k.sys, csrss.exe,

Winlogon.exe, video driver, print driver, etc.

Page 34: Windows内核技术介绍

Session Spaces

Session Image(8MB)

SESSION_WS(4MB)

Session View

(20MB)

Session Pool(16MB)

Session Space

0x80000000

0xFFFFFFFF

0xC0000000

0xBC000000

System Space

Session Space

Page 35: Windows内核技术介绍

Window Management:Window Stations and Desktops

交互式窗口站

(WinSta0)

剪贴板

键盘

鼠标

显示器

登录桌面 交互桌面 屏幕保护桌面

非交互式窗口站

不可见桌面 ……

剪贴板

……

Page 36: Windows内核技术介绍

Window Hierarchy

交互式窗口站

(WinSta0)

非交互式窗口站

子系统会话

默认桌面

屏幕保护桌面

不可见桌面

顶级窗口列表

子窗口

登录桌面

Page 37: Windows内核技术介绍

Desktop

• An surface for output

• In a WindowStation

• Have a Desktop Heap (assigned from the session view)

Page 38: Windows内核技术介绍

Message Path in Win32

GUI 线程 输入(input)

消息队列

寄入(post)

消息队列

其他线程

RIT 线程

其他 HID

桌面线程

鼠标事件

Page 39: Windows内核技术介绍

References

• Mark E. Russinovich and David A. Solomon, Windows Internals (4th/5th Edition), Microsoft Press, 2004/2009.

• 潘爱民,Windows内核原理与实现,电子工业出版社,2010.4

• WRK, Windows Research Kernel(by Microsoft)

• Microsoft WDK Documents & Samples

Page 40: Windows内核技术介绍

Thanks!

Q&A