Operating Systems II Unit OS6: Device Management 6.3. Windows I/O Processing
- Prof. Dr. Andreas Polze,
Andreas Grapentin, Bernhard Rabe
Operating Systems II Unit OS6: Device Management 6.3. Windows I/O - - PowerPoint PPT Presentation
Operating Systems II Unit OS6: Device Management 6.3. Windows I/O Processing Prof. Dr. Andreas Polze, Andreas Grapentin, Bernhard Rabe Roadmap for Sec.on 6.3 Driver and Device Objects I/O Request
Andreas Grapentin, Bernhard Rabe
2 ¡
3 ¡
4 ¡
5 ¡
\TCPIP ¡ Driver ¡Object ¡
\Device\TCP ¡ \Device\UDP ¡ \Device\IP ¡
Dispatch ¡Table ¡ Open ¡ Write ¡ Read ¡ Loaded ¡Driver ¡Image ¡ Open(…) ¡ Read(…) ¡ Write(…) ¡
TCP/IP ¡Drivers ¡Driver ¡and ¡Device ¡Objects ¡
6 ¡
devices) ¡
– Applica.ons ¡and ¡drivers ¡“open” ¡devices ¡by ¡name ¡ – The ¡name ¡is ¡parsed ¡by ¡the ¡Object ¡Manager ¡ – When ¡an ¡open ¡succeeds ¡the ¡object ¡manager ¡creates ¡a ¡file ¡object ¡to ¡represent ¡ the ¡open ¡instance ¡of ¡the ¡device ¡and ¡a ¡file ¡handle ¡in ¡the ¡process ¡handle ¡table ¡
– File ¡offset ¡for ¡sequen.al ¡access ¡ – File ¡open ¡characteris.cs ¡(e.g. ¡delete-‑on-‑close) ¡ – File ¡name ¡
7 ¡
– File ¡object ¡at ¡which ¡I/O ¡is ¡directed ¡ – I/O ¡characteris.cs ¡(e.g. ¡synchronous, ¡non-‑buffered) ¡ – Byte ¡offset ¡ – Length ¡ – Buffer ¡loca.on ¡
File ¡Object ¡ Device ¡Object ¡ Driver ¡Object ¡
8 ¡
Process ¡ NtDeviceIoControlFile ¡
Handle ¡ ¡ Table ¡ File ¡ ¡ Object ¡ Device ¡ ¡ Object ¡ Driver ¡ ¡ Object ¡ Dispatch ¡ ¡ Table ¡
DispatchDeviceControl( ¡DeviceObject, ¡Irp ¡) ¡ Driver ¡Code ¡
User ¡Mode ¡ Kernel ¡Mode ¡
IRP ¡
DeviceIoControl ¡
9 ¡
Environment ¡ subsystem ¡or ¡ DLL ¡ Services ¡ I/O ¡manager ¡ IRP ¡header ¡ ¡ WRITE ¡ parameters ¡ File ¡
Device ¡
Driver ¡
IRP ¡stack ¡ loca.on ¡ Dispatch ¡ rou.ne(s) ¡ Start ¡I/O ¡ ISR ¡ DPC ¡ rou.ne ¡ Device ¡Driver ¡ 1) An application writes a file to the printer, passing a handle to the file object 2)The I/O manager creates an IRP and initializes first stack location 3)The I/O manager uses the driver object to locate the WRITE dispatch routine and calls it, passing the IRP User ¡mode ¡ Kernel ¡mode ¡
10 ¡
IRP ¡consists ¡of ¡two ¡parts: ¡
– Type ¡and ¡size ¡of ¡the ¡request ¡ – Whether ¡request ¡is ¡synchronous ¡or ¡asynchronous ¡ – Pointer ¡to ¡buffer ¡for ¡buffered ¡I/O ¡ – State ¡informa.on ¡(changes ¡with ¡progress ¡of ¡the ¡request) ¡
– Func.on ¡code ¡ – Func.on-‑specific ¡parameters ¡ – Pointer ¡to ¡caller‘s ¡file ¡object ¡
– I/O ¡system ¡may ¡free ¡any ¡outstanding ¡IRPs ¡if ¡thread ¡terminates ¡
11 ¡
1. The ¡I/O ¡request ¡passes ¡through ¡a ¡subsystem ¡DLL ¡ 2. The ¡subsystem ¡DLL ¡calls ¡the ¡I/O ¡manager‘s ¡NtWriteFile() ¡service ¡ 3. I/O ¡manager ¡sends ¡the ¡request ¡in ¡form ¡of ¡an ¡IRP ¡to ¡the ¡driver ¡(a ¡device ¡driver) ¡ 4. The ¡driver ¡starts ¡the ¡I/O ¡opera.on ¡ 5. When ¡the ¡device ¡completes ¡the ¡opera.on ¡and ¡interrupts ¡the ¡CPU, ¡the ¡device ¡ driver ¡services ¡the ¡int. ¡ 6. The ¡I/O ¡manager ¡completes ¡the ¡I/O ¡request ¡
12 ¡
Servicing ¡an ¡interrupt: ¡
– ISR ¡schedules ¡Deferred ¡Procedure ¡Call ¡(DPC); ¡dismisses ¡int. ¡ – DPC ¡rou.ne ¡starts ¡next ¡I/O ¡request ¡and ¡completes ¡interrupt ¡servicing ¡ – May ¡call ¡comple.on ¡rou.ne ¡of ¡higher-‑level ¡driver ¡
I/O ¡comple.on: ¡
– Record ¡the ¡outcome ¡of ¡the ¡opera.on ¡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; ¡wai.ng ¡threads ¡are ¡released ¡
13 ¡
Peripheral ¡Device ¡Controller ¡ CPU ¡Interrupt ¡ Controller ¡
CPU ¡Interrupt ¡ Service ¡Table ¡ 0 ¡ 2 ¡ 3 ¡ n ¡ ISR ¡Address ¡ Spin ¡Lock ¡ ¡ Dispatch ¡Code ¡
Interrupt ¡Object ¡
Read ¡from ¡device ¡ Acknowledge-‑ Interrupt ¡ Request ¡DPC ¡
Driver ¡ISR ¡
Raise ¡IRQL ¡ Lower ¡IRQL ¡
KiInterruptDispatch ¡
Grab ¡Spinlock ¡ Drop ¡Spinlock ¡
14 ¡
level ¡
– Also ¡used ¡for ¡quantum ¡end ¡and ¡.mer ¡expira.on ¡
– One ¡queue ¡per ¡CPU. ¡ ¡DPCs ¡are ¡normally ¡queued ¡to ¡the ¡current ¡processor, ¡but ¡can ¡be ¡ targeted ¡to ¡other ¡CPUs ¡ – Executes ¡specified ¡procedure ¡at ¡dispatch ¡IRQL ¡(or ¡“dispatch ¡level”, ¡also ¡“DPC ¡level”) ¡when ¡ all ¡higher-‑IRQL ¡work ¡(interrupts) ¡completed ¡ – Maximum ¡.mes ¡recommended: ¡ISR: ¡10 ¡µsec, ¡DPC: ¡25 ¡µsec ¡
queue ¡head ¡ DPC ¡object ¡ DPC ¡object ¡ DPC ¡object ¡
15 ¡ DPC ¡
DPC ¡rou.nes ¡can ¡call ¡kernel ¡func.ons ¡ but ¡can‘t ¡call ¡system ¡services, ¡generate ¡ page ¡faults, ¡or ¡create ¡or ¡wait ¡on ¡objects ¡ DPC ¡rou.nes ¡can‘t ¡ assume ¡what ¡ process ¡address ¡ space ¡is ¡currently ¡ mapped ¡
Interrupt ¡ dispatch ¡table ¡ high ¡ Power ¡failure ¡ Dispatch/DPC ¡ APC ¡ Low ¡ DPC ¡
queues ¡DPC ¡that ¡will ¡ release ¡all ¡wai.ng ¡threads ¡ Kernel ¡requests ¡SW ¡int. ¡ DPC ¡ DPC ¡ DPC ¡queue ¡
when ¡IRQL ¡drops ¡below ¡ dispatch/DPC ¡level ¡ dispatcher ¡
control ¡transfers ¡to ¡ thread ¡dispatcher ¡
rou.ne ¡in ¡DPC ¡queue ¡
16 ¡
– APC ¡rou.nes ¡can ¡acquire ¡resources ¡(objects), ¡incur ¡page ¡faults, ¡ call ¡system ¡services ¡
– Permission ¡required ¡for ¡user ¡mode ¡APCs ¡
– Wait ¡for ¡asynchronous ¡I/O ¡opera.on ¡ – Emulate ¡delivery ¡of ¡POSIX ¡signals ¡ – Make ¡threads ¡suspend/terminate ¡itself ¡(env. ¡subsystems) ¡
– WaitForMul.pleObjectsEx(), ¡SleepEx() ¡
17 ¡
– Run ¡in ¡kernel ¡mode, ¡at ¡IRQL ¡1 ¡ – Always ¡deliverable ¡unless ¡thread ¡is ¡already ¡at ¡IRQL ¡1 ¡or ¡above ¡ – Used ¡for ¡I/O ¡comple.on ¡repor.ng ¡from ¡“arbitrary ¡thread ¡context” ¡ – Kernel-‑mode ¡interface ¡is ¡linkable, ¡but ¡not ¡documented ¡
– Always ¡deliverable ¡if ¡at ¡IRQL ¡0, ¡unless ¡explicitly ¡disabled ¡ ¡ (disable ¡with ¡KeEnterCri.calRegion) ¡
– Used ¡for ¡I/O ¡comple.on ¡callback ¡rou.nes ¡(see ¡ReadFileEx, ¡WriteFileEx); ¡also, ¡ QueueUserApc ¡ – Only ¡deliverable ¡when ¡thread ¡is ¡in ¡“alertable ¡wait” ¡
Thread ¡ Object ¡ K ¡ U ¡ APC ¡objects ¡
18 ¡
provide ¡added ¡value, ¡etc. ¡ ¡
– Only ¡the ¡lowest ¡layer ¡talks ¡to ¡the ¡I/O ¡ hardware ¡
to ¡other ¡devices ¡
– They ¡see ¡all ¡requests ¡first ¡and ¡can ¡ manipulate ¡them ¡ – Example ¡filter ¡drivers: ¡
Process ¡
User ¡Mode ¡ Kernel ¡Mode ¡
System ¡Services ¡ I/O ¡Manager ¡ File ¡System ¡ ¡ Driver ¡ Volume ¡ ¡ Manager ¡ Driver ¡ Disk ¡Driver ¡
IRP ¡
19 ¡
– Addresses ¡the ¡“backup ¡open ¡files” ¡problem ¡
– Database ¡servers ¡flush ¡transac.ons ¡ – Windows ¡components ¡ ¡ such ¡as ¡the ¡Registry ¡ ¡ flush ¡data ¡files ¡
can ¡implement ¡different ¡snapshot ¡ ¡ mechanisms: ¡ – Copy-‑on-‑write ¡ – Mirroring ¡
Volsnap ¡is ¡the ¡built-‑in ¡provider: ¡
and ¡where ¡paging ¡file ¡is ¡to ¡avoid ¡tracking ¡their ¡ changes ¡
¡
20 ¡
Volume ¡Shadow ¡ ¡ Copy ¡Driver ¡ (volsnap.sys) ¡ Mirror ¡provider ¡ Oracle ¡ SQL ¡ Volume ¡Shadow ¡ Copy ¡Service ¡ Backup ¡ ¡ ApplicaZon ¡
1. Backup application requests shadow copy
to freeze activity
create volume shadow copies
to resume (“thaw”) activity
Writers ¡ Providers ¡
saves ¡data ¡from ¡volume ¡ ¡ Shadow ¡copies ¡
21 ¡
File System Driver Volsnap.sys a b c a d … b c Backup read of sector c
Snapshot
Backup Application Application Shadow Volume C: C: Application read of sector c All reads of sector d
22 ¡
23 ¡
– View ¡the ¡state ¡of ¡folders ¡and ¡files ¡ within ¡a ¡snapshot ¡ – Rollback ¡individual ¡folders ¡and ¡ files ¡to ¡a ¡snapshot ¡
24 ¡
25 ¡
the ¡PnP ¡Manager ¡determines ¡if ¡ ¡the ¡driver ¡is ¡signed ¡ – ¡If ¡the ¡driver ¡ ¡ is ¡not ¡signed, ¡the ¡system’s ¡driver ¡ ¡ signing ¡policy ¡determines ¡whether ¡ ¡
¡ ¡ ¡
– The ¡driver ¡informs ¡the ¡PnP ¡Manager ¡of ¡the ¡device’s ¡resource ¡requirements ¡ – The ¡PnP ¡Manager ¡reconfigures ¡other ¡devices ¡to ¡accommodate ¡the ¡new ¡device ¡
26 ¡
– Bus ¡drivers ¡iden.fy ¡device ¡on ¡a ¡bus ¡
filled ¡in ¡with ¡devices ¡
Root ACPI PCI USB Video Disk Key- board Battery
Device Tree
27 ¡
ports) ¡
Manager ¡tries ¡to ¡reassign ¡resources ¡in ¡order ¡to ¡accommodate ¡ ¡
28 ¡
no.fies ¡about ¡config. ¡changes ¡
Not ¡started ¡ Started ¡ Pending ¡stop ¡ Stopped ¡ Pending ¡ ¡ remove ¡ Surprise ¡ remove ¡ Removed ¡
Start-‑device ¡ command ¡ Start-‑device ¡ command ¡ Query-‑stop ¡ command ¡ Stop ¡ command ¡ Query-‑remove ¡ command ¡ Surprise-‑remove ¡ command ¡ Remove ¡ command ¡ Remove ¡ command ¡
29 ¡
limited ¡power ¡support) ¡
– System ¡ac.vity ¡level ¡ ¡ – System ¡balery ¡level ¡ ¡ – Shutdown, ¡hibernate, ¡or ¡sleep ¡requests ¡from ¡ ¡ applica.ons ¡ ¡ – User ¡ac.ons, ¡such ¡as ¡pressing ¡the ¡power ¡bulon ¡ ¡ – Control ¡Panel ¡power ¡se^ngs ¡
but ¡it ¡requires ¡the ¡coopera.on ¡of ¡every ¡ ¡ device ¡driver ¡-‑ ¡applica.ons ¡can ¡provide ¡their ¡input ¡as ¡well ¡
30 ¡
– On ¡
– Standby ¡
– Hiberna.ng ¡
system ¡volume ¡ – Off ¡
– Only ¡a ¡driver ¡knows ¡the ¡capabili.es ¡of ¡their ¡device ¡ – Some ¡devices ¡only ¡have ¡“on” ¡and ¡“off”, ¡others ¡have ¡intermediate ¡states ¡
– Display ¡can ¡dim, ¡disk ¡spin ¡down, ¡etc. ¡
31 ¡
State Power Consumption Software Resumption HW Latency S0 (fully on) Maximum Not applicable None S1 (sleeping) Less than S0, more than S2 System resumes where it left
Less than 2 sec. S2 (sleeping) Less than S1, more than S3 System resumes where it left
2 or more sec. S3 (sleeping) Less than S2, processor is off System resumes where it left
Same as S2 S4 (sleeping) Trickle current to power button and wake circuitry System restarts from hibernate file and resumes where it left off (returns to S0) Long and undefined S5 (fully off) Trickle current to power button System boot Long and undefined System ¡Power-‑State ¡Defini.ons ¡
32 ¡
– Go ¡to ¡end ¡of ¡log ¡and ¡look ¡backwards ¡to ¡where ¡problem ¡occurred ¡or ¡is ¡evident ¡and ¡ focused ¡on ¡the ¡last ¡things ¡done ¡ – Compare ¡a ¡good ¡log ¡with ¡a ¡bad ¡log ¡
point ¡to ¡the ¡problem ¡
– Have ¡to ¡first ¡massage ¡log ¡file ¡to ¡remove ¡data ¡that ¡differs ¡run ¡to ¡run ¡
– Easy ¡to ¡do ¡with ¡Excel ¡by ¡dele.ng ¡columns ¡
– Then ¡compare ¡with ¡FC ¡(built ¡in ¡tool) ¡or ¡Windiff ¡(Resource ¡Kit) ¡
33 ¡
34 ¡
35 ¡
36 ¡
– Exclude ¡overrides ¡include ¡filter ¡
– Capture ¡everything ¡and ¡save ¡the ¡log ¡ – Then ¡apply ¡filters ¡(you ¡can ¡always ¡reload ¡the ¡log) ¡
37 ¡
38 ¡
– Process ¡performance ¡counters ¡show ¡I/O ¡ac.vity, ¡but ¡not ¡to ¡where ¡ – System ¡performance ¡counters ¡show ¡which ¡disks ¡are ¡being ¡hit, ¡but ¡not ¡ which ¡files ¡or ¡which ¡process ¡ – Filemon ¡pinpoints ¡which ¡file(s) ¡are ¡being ¡accessed, ¡by ¡whom, ¡and ¡how ¡ frequently ¡
– Import ¡into ¡Excel ¡and ¡make ¡a ¡pie ¡chart ¡by ¡file ¡name ¡or ¡opera.on ¡type ¡ – Move ¡heavy-‑access ¡files ¡to ¡a ¡different ¡disk ¡on ¡a ¡different ¡controller ¡
39 ¡
– A ¡poorly ¡wrilen ¡applica.on ¡will ¡“poll” ¡for ¡changes ¡ – A ¡well-‑wrilen ¡applica.on ¡will ¡request ¡no.fica.on ¡by ¡the ¡system ¡of ¡ changes ¡
– Context ¡switches ¡including ¡TLB ¡flush ¡ – Cache ¡invalida.on ¡ – Physical ¡memory ¡usage ¡ – CPU ¡usage ¡
– Polling ¡is ¡visible ¡as ¡periodic ¡accesses ¡to ¡the ¡same ¡files ¡and ¡directories ¡ – File ¡change ¡no.fica.on ¡is ¡visible ¡as ¡directory ¡queries ¡that ¡have ¡no ¡ result ¡
40 ¡
41 ¡
42 ¡
43 ¡
44 ¡
45 ¡