xref: /openbmc/linux/tools/memory-model/lock.cat (revision 8559183ccaec97454b2515ac426f113967256cf9)
11c27b644SPaul E. McKenney// SPDX-License-Identifier: GPL-2.0+
21c27b644SPaul E. McKenney(*
31c27b644SPaul E. McKenney * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria
41c27b644SPaul E. McKenney * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu>
51c27b644SPaul E. McKenney *)
61c27b644SPaul E. McKenney
71c27b644SPaul E. McKenney(* Generate coherence orders and handle lock operations *)
815553dcbSLuc Maranget(*
915553dcbSLuc Maranget * Warning, crashes with herd7 versions strictly before 7.48.
1015553dcbSLuc Maranget * spin_islocked is functional from version 7.49.
1115553dcbSLuc Maranget *
1215553dcbSLuc Maranget *)
131c27b644SPaul E. McKenneyinclude "cross.cat"
141c27b644SPaul E. McKenney
151c27b644SPaul E. McKenney(* From lock reads to their partner lock writes *)
161c27b644SPaul E. McKenneylet lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po)
171c27b644SPaul E. McKenneylet rmw = rmw | lk-rmw
181c27b644SPaul E. McKenney
191c27b644SPaul E. McKenney(*
201c27b644SPaul E. McKenney * A paired LKR must always see an unlocked value; spin_lock() calls nested
211c27b644SPaul E. McKenney * inside a critical section (for the same lock) always deadlock.
221c27b644SPaul E. McKenney *)
231c27b644SPaul E. McKenneyempty ([LKW] ; po-loc ; [domain(lk-rmw)]) \ (po-loc ; [UL] ; po-loc)
241c27b644SPaul E. McKenney	as lock-nest
251c27b644SPaul E. McKenney
261c27b644SPaul E. McKenney(* The litmus test is invalid if an LKW event is not part of an RMW pair *)
271c27b644SPaul E. McKenneyflag ~empty LKW \ range(lk-rmw) as unpaired-LKW
281c27b644SPaul E. McKenney
291c27b644SPaul E. McKenney(* This will be allowed if we implement spin_is_locked() *)
301c27b644SPaul E. McKenneyflag ~empty LKR \ domain(lk-rmw) as unpaired-LKR
311c27b644SPaul E. McKenney
321c27b644SPaul E. McKenney(* There should be no R or W accesses to spinlocks *)
331c27b644SPaul E. McKenneylet ALL-LOCKS = LKR | LKW | UL | LF
341c27b644SPaul E. McKenneyflag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses
351c27b644SPaul E. McKenney
361c27b644SPaul E. McKenney(* The final value of a spinlock should not be tested *)
371c27b644SPaul E. McKenneyflag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final
381c27b644SPaul E. McKenney
3915553dcbSLuc Maranget(*
4015553dcbSLuc Maranget * Backward compatibility
4115553dcbSLuc Maranget *)
4215553dcbSLuc Marangetlet RL = try RL with emptyset (* defined herd7 >= 7.49 *)
4315553dcbSLuc Marangetlet RU = try RU with emptyset (* defined herd7 >= 7.49 *)
44*8559183cSAlan Stern
45*8559183cSAlan Stern(* Treat RL as a kind of LF: a read with no ordering properties *)
46*8559183cSAlan Sternlet LF = LF | RL
47*8559183cSAlan Stern
481c27b644SPaul E. McKenney(*
491c27b644SPaul E. McKenney * Put lock operations in their appropriate classes, but leave UL out of W
501c27b644SPaul E. McKenney * until after the co relation has been generated.
511c27b644SPaul E. McKenney *)
52*8559183cSAlan Sternlet R = R | LKR | LF | RU
531c27b644SPaul E. McKenneylet W = W | LKW
541c27b644SPaul E. McKenney
551c27b644SPaul E. McKenneylet Release = Release | UL
561c27b644SPaul E. McKenneylet Acquire = Acquire | LKR
571c27b644SPaul E. McKenney
581c27b644SPaul E. McKenney
591c27b644SPaul E. McKenney(* Match LKW events to their corresponding UL events *)
601c27b644SPaul E. McKenneylet critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc)
611c27b644SPaul E. McKenney
621c27b644SPaul E. McKenneyflag ~empty UL \ range(critical) as unmatched-unlock
631c27b644SPaul E. McKenney
641c27b644SPaul E. McKenney(* Allow up to one unmatched LKW per location; more must deadlock *)
651c27b644SPaul E. McKenneylet UNMATCHED-LKW = LKW \ domain(critical)
661c27b644SPaul E. McKenneyempty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks
671c27b644SPaul E. McKenney
681c27b644SPaul E. McKenney
691c27b644SPaul E. McKenney(* rfi for LF events: link each LKW to the LF events in its critical section *)
701c27b644SPaul E. McKenneylet rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc)
711c27b644SPaul E. McKenney
721c27b644SPaul E. McKenney(* rfe for LF events *)
731c27b644SPaul E. McKenneylet all-possible-rfe-lf =
741c27b644SPaul E. McKenney  (*
751c27b644SPaul E. McKenney   * Given an LF event r, compute the possible rfe edges for that event
761c27b644SPaul E. McKenney   * (all those starting from LKW events in other threads),
771c27b644SPaul E. McKenney   * and then convert that relation to a set of single-edge relations.
781c27b644SPaul E. McKenney   *)
791c27b644SPaul E. McKenney  let possible-rfe-lf r =
801c27b644SPaul E. McKenney    let pair-to-relation p = p ++ 0
811c27b644SPaul E. McKenney    in map pair-to-relation ((LKW * {r}) & loc & ext)
821c27b644SPaul E. McKenney  (* Do this for each LF event r that isn't in rfi-lf *)
831c27b644SPaul E. McKenney  in map possible-rfe-lf (LF \ range(rfi-lf))
841c27b644SPaul E. McKenney
851c27b644SPaul E. McKenney(* Generate all rf relations for LF events *)
861c27b644SPaul E. McKenneywith rfe-lf from cross(all-possible-rfe-lf)
8715553dcbSLuc Marangetlet rf-lf = rfe-lf | rfi-lf
8815553dcbSLuc Maranget
8915553dcbSLuc Maranget(* Read from unlock, ie islocked returning false, slightly different *)
9015553dcbSLuc Maranget
9115553dcbSLuc Maranget(* islocked returning false can read from the last po-previous unlock *)
9215553dcbSLuc Marangetlet rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc)
9315553dcbSLuc Maranget
9415553dcbSLuc Maranget(* any islocked returning false can read from any external unlock *)
9515553dcbSLuc Marangetlet all-possible-rfe-ru =
9615553dcbSLuc Maranget   let possible-rfe-ru r =
9715553dcbSLuc Maranget     let pair-to-relation p = p ++ 0
9815553dcbSLuc Maranget     in map pair-to-relation (((UL|IW) * {r}) & loc & ext)
9915553dcbSLuc Maranget  in map possible-rfe-ru RU
10015553dcbSLuc Maranget
10115553dcbSLuc Marangetwith rfe-ru from cross(all-possible-rfe-ru)
10215553dcbSLuc Marangetlet rf-ru = rfe-ru | rfi-ru
10315553dcbSLuc Maranget
10415553dcbSLuc Maranget(* Final rf relation *)
105*8559183cSAlan Sternlet rf = rf | rf-lf | rf-ru
1061c27b644SPaul E. McKenney
1071c27b644SPaul E. McKenney(* Generate all co relations, including LKW events but not UL *)
1081c27b644SPaul E. McKenneylet co0 = co0 | ([IW] ; loc ; [LKW]) |
1091c27b644SPaul E. McKenney	(([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW])
1101c27b644SPaul E. McKenneyinclude "cos-opt.cat"
1111c27b644SPaul E. McKenneylet W = W | UL
1121c27b644SPaul E. McKenneylet M = R | W
1131c27b644SPaul E. McKenney
1141c27b644SPaul E. McKenney(* Merge UL events into co *)
1151c27b644SPaul E. McKenneylet co = (co | critical | (critical^-1 ; co))+
1161c27b644SPaul E. McKenneylet coe = co & ext
1171c27b644SPaul E. McKenneylet coi = co & int
1181c27b644SPaul E. McKenney
1191c27b644SPaul E. McKenney(* Merge LKR events into rf *)
1201c27b644SPaul E. McKenneylet rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1)
1211c27b644SPaul E. McKenneylet rfe = rf & ext
1221c27b644SPaul E. McKenneylet rfi = rf & int
1231c27b644SPaul E. McKenney
1241c27b644SPaul E. McKenneylet fr = rf^-1 ; co
1251c27b644SPaul E. McKenneylet fre = fr & ext
1261c27b644SPaul E. McKenneylet fri = fr & int
1271c27b644SPaul E. McKenney
1281c27b644SPaul E. McKenneyshow co,rf,fr
129