1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
4 * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>
7 (* Generate coherence orders and handle lock operations *)
9 * Warning, crashes with herd7 versions strictly before 7.48.
10 * spin_islocked is functional from version 7.49.
15 (* From lock reads to their partner lock writes *)
16 let lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
17 let rmw = rmw | lk-rmw
20 * A paired LKR must always see an unlocked value; spin_lock() calls nested
21 * inside a critical section (for the same lock) always deadlock.
23 empty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
26 (* The litmus test is invalid if an LKW event is not part of an RMW pair *)
27 flag ~empty LKW \ range(lk-rmw) as unpaired-LKW
29 (* This will be allowed if we implement spin_is_locked() *)
30 flag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
32 (* There should be no R or W accesses to spinlocks *)
33 let ALL-LOCKS = LKR | LKW | UL | LF
34 flag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
36 (* The final value of a spinlock should not be tested *)
37 flag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
40 * Backward compatibility
42 let RL = try RL with emptyset (* defined herd7 >= 7.49 *)
43 let RU = try RU with emptyset (* defined herd7 >= 7.49 *)
45 (* Treat RL as a kind of LF: a read with no ordering properties *)
49 * Put lock operations in their appropriate classes, but leave UL out of W
50 * until after the co relation has been generated.
52 let R = R | LKR | LF | RU
55 let Release = Release | UL
56 let Acquire = Acquire | LKR
59 (* Match LKW events to their corresponding UL events *)
60 let critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
62 flag ~empty UL \ range(critical) as unmatched-unlock
64 (* Allow up to one unmatched LKW per location; more must deadlock *)
65 let UNMATCHED-LKW = LKW \ domain(critical)
66 empty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
69 (* rfi for LF events: link each LKW to the LF events in its critical section *)
70 let rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
72 (* rfe for LF events *)
73 let all-possible-rfe-lf =
75 * Given an LF event r, compute the possible rfe edges for that event
76 * (all those starting from LKW events in other threads),
77 * and then convert that relation to a set of single-edge relations.
79 let possible-rfe-lf r =
80 let pair-to-relation p = p ++ 0
81 in map pair-to-relation ((LKW * {r}) & loc & ext)
82 (* Do this for each LF event r that isn't in rfi-lf *)
83 in map possible-rfe-lf (LF \ range(rfi-lf))
85 (* Generate all rf relations for LF events *)
86 with rfe-lf from cross(all-possible-rfe-lf)
87 let rf-lf = rfe-lf | rfi-lf
89 (* Read from unlock, ie islocked returning false, slightly different *)
91 (* islocked returning false can read from the last po-previous unlock *)
92 let rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
94 (* any islocked returning false can read from any external unlock *)
95 let all-possible-rfe-ru =
96 let possible-rfe-ru r =
97 let pair-to-relation p = p ++ 0
98 in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
99 in map possible-rfe-ru RU
101 with rfe-ru from cross(all-possible-rfe-ru)
102 let rf-ru = rfe-ru | rfi-ru
104 (* Final rf relation *)
105 let rf = rf | rf-lf | rf-ru
107 (* Generate all co relations, including LKW events but not UL *)
108 let co0 = co0 | ([IW] ; loc ; [LKW]) |
109 (([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
110 include "cos-opt.cat"
114 (* Merge UL events into co *)
115 let co = (co | critical | (critical^-1 ; co))+
119 (* Merge LKR events into rf *)
120 let rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)