1*4882a593Smuzhiyun// SPDX-License-Identifier: GPL-2.0+ 2*4882a593Smuzhiyun(* 3*4882a593Smuzhiyun * Copyright (C) 2016 Luc Maranget <luc.maranget@inria.fr> for Inria 4*4882a593Smuzhiyun * Copyright (C) 2017 Alan Stern <stern@rowland.harvard.edu> 5*4882a593Smuzhiyun *) 6*4882a593Smuzhiyun 7*4882a593Smuzhiyun(* 8*4882a593Smuzhiyun * Generate coherence orders and handle lock operations 9*4882a593Smuzhiyun *) 10*4882a593Smuzhiyun 11*4882a593Smuzhiyuninclude "cross.cat" 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun(* 14*4882a593Smuzhiyun * The lock-related events generated by herd7 are as follows: 15*4882a593Smuzhiyun * 16*4882a593Smuzhiyun * LKR Lock-Read: the read part of a spin_lock() or successful 17*4882a593Smuzhiyun * spin_trylock() read-modify-write event pair 18*4882a593Smuzhiyun * LKW Lock-Write: the write part of a spin_lock() or successful 19*4882a593Smuzhiyun * spin_trylock() RMW event pair 20*4882a593Smuzhiyun * UL Unlock: a spin_unlock() event 21*4882a593Smuzhiyun * LF Lock-Fail: a failed spin_trylock() event 22*4882a593Smuzhiyun * RL Read-Locked: a spin_is_locked() event which returns True 23*4882a593Smuzhiyun * RU Read-Unlocked: a spin_is_locked() event which returns False 24*4882a593Smuzhiyun * 25*4882a593Smuzhiyun * LKR and LKW events always come paired, like all RMW event sequences. 26*4882a593Smuzhiyun * 27*4882a593Smuzhiyun * LKR, LF, RL, and RU are read events; LKR has Acquire ordering. 28*4882a593Smuzhiyun * LKW and UL are write events; UL has Release ordering. 29*4882a593Smuzhiyun * LKW, LF, RL, and RU have no ordering properties. 30*4882a593Smuzhiyun *) 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun(* Backward compatibility *) 33*4882a593Smuzhiyunlet RL = try RL with emptyset 34*4882a593Smuzhiyunlet RU = try RU with emptyset 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun(* Treat RL as a kind of LF: a read with no ordering properties *) 37*4882a593Smuzhiyunlet LF = LF | RL 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun(* There should be no ordinary R or W accesses to spinlocks *) 40*4882a593Smuzhiyunlet ALL-LOCKS = LKR | LKW | UL | LF | RU 41*4882a593Smuzhiyunflag ~empty [M \ IW] ; loc ; [ALL-LOCKS] as mixed-lock-accesses 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun(* Link Lock-Reads to their RMW-partner Lock-Writes *) 44*4882a593Smuzhiyunlet lk-rmw = ([LKR] ; po-loc ; [LKW]) \ (po ; po) 45*4882a593Smuzhiyunlet rmw = rmw | lk-rmw 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun(* The litmus test is invalid if an LKR/LKW event is not part of an RMW pair *) 48*4882a593Smuzhiyunflag ~empty LKW \ range(lk-rmw) as unpaired-LKW 49*4882a593Smuzhiyunflag ~empty LKR \ domain(lk-rmw) as unpaired-LKR 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun(* 52*4882a593Smuzhiyun * An LKR must always see an unlocked value; spin_lock() calls nested 53*4882a593Smuzhiyun * inside a critical section (for the same lock) always deadlock. 54*4882a593Smuzhiyun *) 55*4882a593Smuzhiyunempty ([LKW] ; po-loc ; [LKR]) \ (po-loc ; [UL] ; po-loc) as lock-nest 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun(* The final value of a spinlock should not be tested *) 58*4882a593Smuzhiyunflag ~empty [FW] ; loc ; [ALL-LOCKS] as lock-final 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun(* 61*4882a593Smuzhiyun * Put lock operations in their appropriate classes, but leave UL out of W 62*4882a593Smuzhiyun * until after the co relation has been generated. 63*4882a593Smuzhiyun *) 64*4882a593Smuzhiyunlet R = R | LKR | LF | RU 65*4882a593Smuzhiyunlet W = W | LKW 66*4882a593Smuzhiyun 67*4882a593Smuzhiyunlet Release = Release | UL 68*4882a593Smuzhiyunlet Acquire = Acquire | LKR 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun(* Match LKW events to their corresponding UL events *) 71*4882a593Smuzhiyunlet critical = ([LKW] ; po-loc ; [UL]) \ (po-loc ; [LKW | UL] ; po-loc) 72*4882a593Smuzhiyun 73*4882a593Smuzhiyunflag ~empty UL \ range(critical) as unmatched-unlock 74*4882a593Smuzhiyun 75*4882a593Smuzhiyun(* Allow up to one unmatched LKW per location; more must deadlock *) 76*4882a593Smuzhiyunlet UNMATCHED-LKW = LKW \ domain(critical) 77*4882a593Smuzhiyunempty ([UNMATCHED-LKW] ; loc ; [UNMATCHED-LKW]) \ id as unmatched-locks 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun(* rfi for LF events: link each LKW to the LF events in its critical section *) 80*4882a593Smuzhiyunlet rfi-lf = ([LKW] ; po-loc ; [LF]) \ ([LKW] ; po-loc ; [UL] ; po-loc) 81*4882a593Smuzhiyun 82*4882a593Smuzhiyun(* rfe for LF events *) 83*4882a593Smuzhiyunlet all-possible-rfe-lf = 84*4882a593Smuzhiyun (* 85*4882a593Smuzhiyun * Given an LF event r, compute the possible rfe edges for that event 86*4882a593Smuzhiyun * (all those starting from LKW events in other threads), 87*4882a593Smuzhiyun * and then convert that relation to a set of single-edge relations. 88*4882a593Smuzhiyun *) 89*4882a593Smuzhiyun let possible-rfe-lf r = 90*4882a593Smuzhiyun let pair-to-relation p = p ++ 0 91*4882a593Smuzhiyun in map pair-to-relation ((LKW * {r}) & loc & ext) 92*4882a593Smuzhiyun (* Do this for each LF event r that isn't in rfi-lf *) 93*4882a593Smuzhiyun in map possible-rfe-lf (LF \ range(rfi-lf)) 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun(* Generate all rf relations for LF events *) 96*4882a593Smuzhiyunwith rfe-lf from cross(all-possible-rfe-lf) 97*4882a593Smuzhiyunlet rf-lf = rfe-lf | rfi-lf 98*4882a593Smuzhiyun 99*4882a593Smuzhiyun(* 100*4882a593Smuzhiyun * RU, i.e., spin_is_locked() returning False, is slightly different. 101*4882a593Smuzhiyun * We rely on the memory model to rule out cases where spin_is_locked() 102*4882a593Smuzhiyun * within one of the lock's critical sections returns False. 103*4882a593Smuzhiyun *) 104*4882a593Smuzhiyun 105*4882a593Smuzhiyun(* rfi for RU events: an RU may read from the last po-previous UL *) 106*4882a593Smuzhiyunlet rfi-ru = ([UL] ; po-loc ; [RU]) \ ([UL] ; po-loc ; [LKW] ; po-loc) 107*4882a593Smuzhiyun 108*4882a593Smuzhiyun(* rfe for RU events: an RU may read from an external UL or the initial write *) 109*4882a593Smuzhiyunlet all-possible-rfe-ru = 110*4882a593Smuzhiyun let possible-rfe-ru r = 111*4882a593Smuzhiyun let pair-to-relation p = p ++ 0 112*4882a593Smuzhiyun in map pair-to-relation (((UL | IW) * {r}) & loc & ext) 113*4882a593Smuzhiyun in map possible-rfe-ru RU 114*4882a593Smuzhiyun 115*4882a593Smuzhiyun(* Generate all rf relations for RU events *) 116*4882a593Smuzhiyunwith rfe-ru from cross(all-possible-rfe-ru) 117*4882a593Smuzhiyunlet rf-ru = rfe-ru | rfi-ru 118*4882a593Smuzhiyun 119*4882a593Smuzhiyun(* Final rf relation *) 120*4882a593Smuzhiyunlet rf = rf | rf-lf | rf-ru 121*4882a593Smuzhiyun 122*4882a593Smuzhiyun(* Generate all co relations, including LKW events but not UL *) 123*4882a593Smuzhiyunlet co0 = co0 | ([IW] ; loc ; [LKW]) | 124*4882a593Smuzhiyun (([LKW] ; loc ; [UNMATCHED-LKW]) \ [UNMATCHED-LKW]) 125*4882a593Smuzhiyuninclude "cos-opt.cat" 126*4882a593Smuzhiyunlet W = W | UL 127*4882a593Smuzhiyunlet M = R | W 128*4882a593Smuzhiyun 129*4882a593Smuzhiyun(* Merge UL events into co *) 130*4882a593Smuzhiyunlet co = (co | critical | (critical^-1 ; co))+ 131*4882a593Smuzhiyunlet coe = co & ext 132*4882a593Smuzhiyunlet coi = co & int 133*4882a593Smuzhiyun 134*4882a593Smuzhiyun(* Merge LKR events into rf *) 135*4882a593Smuzhiyunlet rf = rf | ([IW | UL] ; singlestep(co) ; lk-rmw^-1) 136*4882a593Smuzhiyunlet rfe = rf & ext 137*4882a593Smuzhiyunlet rfi = rf & int 138*4882a593Smuzhiyun 139*4882a593Smuzhiyunlet fr = rf^-1 ; co 140*4882a593Smuzhiyunlet fre = fr & ext 141*4882a593Smuzhiyunlet fri = fr & int 142*4882a593Smuzhiyun 143*4882a593Smuzhiyunshow co,rf,fr 144