Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/19/2007TTI-C PL-Lunch Certified Concurrent Code with Interrupts Xinyu Feng Joint work with Zhong Shao (Yale University) and Yuan Dong (Tsinghua University)

Similar presentations


Presentation on theme: "10/19/2007TTI-C PL-Lunch Certified Concurrent Code with Interrupts Xinyu Feng Joint work with Zhong Shao (Yale University) and Yuan Dong (Tsinghua University)"— Presentation transcript:

1 10/19/2007TTI-C PL-Lunch Certified Concurrent Code with Interrupts Xinyu Feng Joint work with Zhong Shao (Yale University) and Yuan Dong (Tsinghua University)

2 How to Guarantee Software Quality? Hardware We need a “certified” computing platform! Buggy? Bootloader + OS + Device DriverCertified Bootloader + OS + Device Driver Buggy? Runtime Services & LibrariesCertified Runtime Services & Libraries Buggy? Security InfrastructureCertified Security Infrastructure Uncertified legacy code becomes second-class citizen! We need firm control of the lowest-level software!

3 Components of a certified framework certified code (proof + machine code) machine model specifications mechanized meta-logic proof checker Proof Checker Yes CPU Specifications Proof machine code No

4 timer interrupt handler A mini- OS 1300 lines of 16-bit x86 asm. bootloader keyboard driver keyboard interrupt handler preemptive thread lib: spawn, exit, yield, scheduler, … sync. lib: locks and monitors … a multi-threaded user application Need a precise definition of abstraction layers! [Chuck Moor FCRC’07]

5 Layering of miniOS Code

6 C: low-level sequential code, might be tricky (e.g. context switching) [Feng et al. PLDI’06, Feng et al. TLDI’07, Ni et al. TPHOLs’07]

7 Layering of miniOS Code A: concurrent code, Rely-Guarantee, CSL, … PCC: [Yu&Shao ICFP’04, Feng&Shao ICFP’05, Feng et al. ESOP’07]

8 Layering of miniOS Code B: concurrent code with explicit interrupts How to certify ???

9 Concurrency with Interrupts: Challenges IR0 IR1 (1) (2) (3) (4) (5) Asymmetric preemption between handlers and non-handler code Intertwining between threads and handlers Asymmetric synchronization: cli/sti are different from locks Nesting of handlers: may not respect priorities

10 Contributions of Our Work An abstract machine and operational semantics –Interrupts, switch, block/unblock Program logics –Modeling concurrency/interrupt primitives in terms of memory ownership transfer Certify implementation of sync. primitives –Locks –Condition variables Hoare style, Brinch-Hanson style, Mesa style

11 Outline of This Talk Background: Separation Logic and Concurrency Abstract Interrupt Machine (AIM) – I –Interrupts with sequential non-handler code AIM – II –Interrupts with multi-threaded non-handler code AIM – III –Adding block/unblock primitives

12 Separation Logic Assertions l  n n l p  qp  q pq emp empty heap p  qp  q p  q

13 Separation Logic for Concurrency Key ideas: There is always a partition of memory among threads, and each thread only access its own part. The partition is dynamic: ownership of memory can be dynamically transferred. When there is ownership transfer, certain agreement is followed. Synchronization primitives can be modeled as operations that trigger memory ownership transfer.

14 Outline of This Talk Background: Separation Logic and Concurrency Abstract Interrupt Machine (AIM) – I –Interrupts with sequential non-handler code AIM – II –Interrupts with multi-threaded non-handler code AIM – III –Adding block/unblock primitives

15 AIM – I : The Machine I1I1 f1:f1: I2I2 f2:f2: ISR ih: … (code heap) C 0 r1r1 12… r2r2 r3r3 …rnrn (data heap) H (register file) R (state) S addu … cli sti iret … j f (program) P::=(C,S,pc) ::=(H,R,ie) ::={f  I} * pc ie

16 AIM – I : Operational Semantics Using non-determinism to model interrupts: (C, S, pc)  (C, S', pc') NextS C(pc) (S, S')NextPC C(pc) (pc, pc') (seq) (C, S, pc)  (C, S', ih) S.ie = 1S' = S| ie=0 (irq) P  P' (step) P  P'P  P'

17 Example: Teeter-Totter while(true){ cli; if([right] == 0){ sti; break; } [right] := [right]-1; [left] := [left]+1; sti; } print(“left wins!”); timer: if([left] == 0){ print(“right wins!”); iret; } [left] := [left]-1; [right] := [right]+1; iret; 50 left right

18 - { P h }INV AIM – I : The Memory Model Non-handler sti iret Handler BA cli … Memory … - { P h }INV

19 AIM – I : cli/sti BA INV BB B BA B B cli sti ie = 1ie = 0

20 AIM – I : handler BA INV irq iret ie = 1ie = 0 BA INV BA BA

21 AIM – I : cli/sti in handler sti cli ie = 0ie = 1 BA INV B BA B...

22 Example: Teeter-Totter while(true){ -{emp} cli; -{emp * INV}... [right] := [right]-1; [left] := [left]+1; -{INV} sti; -{emp} } timer: -{INV} if([left] == 0){ print(“right wins!”); -{INV} iret; } [left] := [left]-1; [right] := [right]+1; -{INV} iret; INV:  m, n. (left  m)  (right  n)  (m+n = 100)

23 Soundness Safety: –“Certified non-handler and hander would not get stuck”; Partial correctness: –Inserted assertions as specifications always hold whenever the specified program points are reached. Proof based on progress/preservation lemmas

24 Outline of This Talk Background: Separation Logic and Concurrency Abstract Interrupt Machine (AIM) – I –Interrupts with sequential non-handler code AIM – II –Interrupts with multi-threaded non-handler code AIM – III –Adding block/unblock primitives

25 AIM – II : The Machine I1I1 f1:f1: I2I2 f2:f2: ISR ih: … (code heap) C 0 r1r1 12… r2r2 r3r3 …rnrn (data heap) H (register file) R (state) S cli sti … switch j f (program) P::=(C,S,Q,pc) ::=(H,R,ie) ::={f  I} * pc ie pc R R R … (ready. queue) Q

26 AIM – II : switch A primitive implemented at layer C –Interrupt must be disabled before executing switch Operational semantics: –Put the current thread into ready Q –Resume a thread in Q (non-deterministic op)

27 Examples timer_0:... switch... iret timer_1: iret yield: cli switch sti ret Layer A: ie = 0: non-preemptive threads ie = 1 & timer_1: non-preemptive threads ie = 1 & timer_0: preemptive threads

28 Example: spin locks acquire (l): cli; while([l] == 0){ sti; cli; } [l] := 0; sti; return; release (l): cli; [l] := 1; sti; return; switch

29 AIM – II : Memory Model BA INV0 A C T1T1 T2T2 INV1

30 AIM – II : cli/sti T 1 : cli CA INV0 T1T1 T2T2 INV1 T1T1 T1T1 INV0 T1T1 T2T2 INV1 T 1 : sti ie = 1ie = 0

31 AIM – II : switch T1T1 T1T1 INV0 T1T1 T2T2 INV1 T 1 : switch T2T2 T2T2 INV0 T1T1 T2T2 INV1 ie = 0

32 Example: spin locks acquire (l): cli; while([l] == 0){ sti; cli; } [l] := 0; sti; return; release (l): cli; [l] := 1; sti; return; l 0/1 R  INV1:  b. ( l  b)  (b=0  emp  b=1  R) -{ emp } -{ R } -{ emp } -{ R }

33 Example: spin locks acquire (l): cli; while([l] == 0){ sti; cli; } [l] := 0; sti; return; l 0/1 R  INV1:  b. ( l  b)  (b=0  emp  b=1  R) -{ emp } -{ R } -{ emp  INV1  INV0} -{ emp } -{ emp  INV1  INV0}-{ (l  1)  R  INV0}-{ (l  0)  R  INV0} -{ INV1  INV0  R} -{ R}

34 Outline of This Talk Background: Separation Logic and Concurrency Abstract Interrupt Machine (AIM) – I –Interrupts with sequential non-handler code AIM – II –Interrupts with multi-threaded non-handler code AIM – III –Adding block/unblock primitives

35 AIM – III : The Machine f1:f1: f2:f2: ih: (code heap) C (state) S (program) P::=(C,S,B,Q,pc) ::=(H,R,ie) block unblock … pc 0 r1r1 1 2 … r2r2 r3r3 …rnrn (data heap) H (register file) R ie pc R R R … (ready. queue) Q … w1w1 w2w2 wnwn B

36 AIM – III : block and unblock block rs –put current thread into the block queue B(rs) –pick a thread from ready queue to execute unblock rs, rd –move a thread from B(rs) to the ready queue –put the thread id into rd, put 0 if B(rs) empty –no context switching!

37 Examples: locks acquire_0(l): cli; if ([l] == 0) block l; else [l] := 0; sti; return; l B(l) = Q l release_0(l): local x; cli; unblock l x; if (x == 0) [l] := 1; sti; return; 0/1 acquire_1(l): cli; while ([l] == 0) block l; [l] := 0; sti; return; release_1(l): local x; cli; unblock l x; [l] := 1; sti; return; R 

38 How to interpret block/unblock Threads block themselves to wait for resources. … w1w1 w2w2 wnwn B locks: wait for resources protected by locks condition variables: wait for resources over which the condition holds R1R1 R2R2 RnRn  ::= {w 0  R 0, …, w n  R n } R i can be emp !

39 block switch T1T1 T1T1 INV0 T1T1 T2T2 INV1 ? ! T2T2 T2T2 INV0 T1T1 T2T2 INV1 ! T2T2 T2T2 INV0 INV1 T1T1 T2T2 T1T1 T1T1 INV0 T1T1 INV1 T2T2 ! unblock Thread 1 T2T2 ! How to interpret block/unblock Thread 2 block

40 Examples: locks acquire_0(l): cli; if ([l] == 0) block l; else [l] := 0; sti; return; l B(l) = Q l 0/1 R   (l) = R INV1:  b, ( l  b)  (b=0  emp  b=1  R) -{ emp } -{ R } -{ emp } -{ emp  INV1  INV0} -{ emp  (l  1)  R  INV0} -{ emp  INV1  INV0   (l) } -{ emp  (l  0)  R  INV0} -{ emp  INV1  INV0  R}-{ emp  R}

41 Examples: locks l B(l) = Q l 0/1 R   (l) = R INV1:  b, ( l  b)  (b=0  emp  b=1  R) release_0(l): local x; cli; unblock l x; if (x == 0) [l] := 1; sti; return; -{ R } -{ emp } -{ R } -{ R  INV1  INV0}-{(x=0  R  x  0  emp)  INV1  INV0}-{ R  INV1  INV0}-{ (l  1)  R  INV0} -{ INV1  INV0} -{ emp } -{ (l  0)  R  INV0}

42 Examples: locks l B(l) = Q l 0/1 R   (l) = R INV1:  b, ( l  b)  (b=0  emp  b=1  R) acquire_1(l): cli; while ([l] == 0) block l; [l] := 0; sti; return; release_1(l): local x; cli; unblock l x; [l] := 1; sti; return; -{ emp } -{ R } -{ emp }  (l) = emp

43 Examples : Condition Variables wait()/notify() Hoare Style / Brinch-Hanson Style Mesa style See the technical report: http://flint.cs.yale.edu/publications/aim.html

44 Conclusion AIM machine –low-level –can implement interrupt handlers and thread libraries A program logic –following local reasoning in separation logic –modeling cli/sti, switch, block/unblock in terms of memory ownership transfer –can certify different implementation of locks and C.V.s

45 Future Work Linking layers A, B and C –operational semantics of switch and block/unblock can be used as spec. for layer C –may derive a logic for layer A as a specialization of B –expose spec. of locks and CVs to A

46 Future Work Linking layers A, B and C –operational semantics of switch and block/unblock can be used as spec. for layer C –may derive a logic for layer A as a specialization of B –expose spec. of locks and CVs to A Multiple interrupts Dynamic creation of block queues in B Device drivers and I/O BAAnAn A0A0 A1A1 …

47 Thank you! Any Questions?

48 A Simple Example if ([ l ] = 1) [ l ] := 0; 11 l R l R INV:  b, ( l  b)  (b=0  emp  b=1  R) 0 l R 0 l R INV


Download ppt "10/19/2007TTI-C PL-Lunch Certified Concurrent Code with Interrupts Xinyu Feng Joint work with Zhong Shao (Yale University) and Yuan Dong (Tsinghua University)"

Similar presentations


Ads by Google