perfmon.c 169 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845
  1. /*
  2. * This file implements the perfmon-2 subsystem which is used
  3. * to program the IA-64 Performance Monitoring Unit (PMU).
  4. *
  5. * The initial version of perfmon.c was written by
  6. * Ganesh Venkitachalam, IBM Corp.
  7. *
  8. * Then it was modified for perfmon-1.x by Stephane Eranian and
  9. * David Mosberger, Hewlett Packard Co.
  10. *
  11. * Version Perfmon-2.x is a rewrite of perfmon-1.x
  12. * by Stephane Eranian, Hewlett Packard Co.
  13. *
  14. * Copyright (C) 1999-2005 Hewlett Packard Co
  15. * Stephane Eranian <eranian@hpl.hp.com>
  16. * David Mosberger-Tang <davidm@hpl.hp.com>
  17. *
  18. * More information about perfmon available at:
  19. * http://www.hpl.hp.com/research/linux/perfmon
  20. */
  21. #include <linux/config.h>
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/sched.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/smp_lock.h>
  27. #include <linux/proc_fs.h>
  28. #include <linux/seq_file.h>
  29. #include <linux/init.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/mm.h>
  32. #include <linux/sysctl.h>
  33. #include <linux/list.h>
  34. #include <linux/file.h>
  35. #include <linux/poll.h>
  36. #include <linux/vfs.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/mount.h>
  39. #include <linux/bitops.h>
  40. #include <linux/rcupdate.h>
  41. #include <asm/errno.h>
  42. #include <asm/intrinsics.h>
  43. #include <asm/page.h>
  44. #include <asm/perfmon.h>
  45. #include <asm/processor.h>
  46. #include <asm/signal.h>
  47. #include <asm/system.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/delay.h>
  50. #ifdef CONFIG_PERFMON
  51. /*
  52. * perfmon context state
  53. */
  54. #define PFM_CTX_UNLOADED 1 /* context is not loaded onto any task */
  55. #define PFM_CTX_LOADED 2 /* context is loaded onto a task */
  56. #define PFM_CTX_MASKED 3 /* context is loaded but monitoring is masked due to overflow */
  57. #define PFM_CTX_ZOMBIE 4 /* owner of the context is closing it */
  58. #define PFM_INVALID_ACTIVATION (~0UL)
  59. /*
  60. * depth of message queue
  61. */
  62. #define PFM_MAX_MSGS 32
  63. #define PFM_CTXQ_EMPTY(g) ((g)->ctx_msgq_head == (g)->ctx_msgq_tail)
  64. /*
  65. * type of a PMU register (bitmask).
  66. * bitmask structure:
  67. * bit0 : register implemented
  68. * bit1 : end marker
  69. * bit2-3 : reserved
  70. * bit4 : pmc has pmc.pm
  71. * bit5 : pmc controls a counter (has pmc.oi), pmd is used as counter
  72. * bit6-7 : register type
  73. * bit8-31: reserved
  74. */
  75. #define PFM_REG_NOTIMPL 0x0 /* not implemented at all */
  76. #define PFM_REG_IMPL 0x1 /* register implemented */
  77. #define PFM_REG_END 0x2 /* end marker */
  78. #define PFM_REG_MONITOR (0x1<<4|PFM_REG_IMPL) /* a PMC with a pmc.pm field only */
  79. #define PFM_REG_COUNTING (0x2<<4|PFM_REG_MONITOR) /* a monitor + pmc.oi+ PMD used as a counter */
  80. #define PFM_REG_CONTROL (0x4<<4|PFM_REG_IMPL) /* PMU control register */
  81. #define PFM_REG_CONFIG (0x8<<4|PFM_REG_IMPL) /* configuration register */
  82. #define PFM_REG_BUFFER (0xc<<4|PFM_REG_IMPL) /* PMD used as buffer */
  83. #define PMC_IS_LAST(i) (pmu_conf->pmc_desc[i].type & PFM_REG_END)
  84. #define PMD_IS_LAST(i) (pmu_conf->pmd_desc[i].type & PFM_REG_END)
  85. #define PMC_OVFL_NOTIFY(ctx, i) ((ctx)->ctx_pmds[i].flags & PFM_REGFL_OVFL_NOTIFY)
  86. /* i assumed unsigned */
  87. #define PMC_IS_IMPL(i) (i< PMU_MAX_PMCS && (pmu_conf->pmc_desc[i].type & PFM_REG_IMPL))
  88. #define PMD_IS_IMPL(i) (i< PMU_MAX_PMDS && (pmu_conf->pmd_desc[i].type & PFM_REG_IMPL))
  89. /* XXX: these assume that register i is implemented */
  90. #define PMD_IS_COUNTING(i) ((pmu_conf->pmd_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
  91. #define PMC_IS_COUNTING(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_COUNTING) == PFM_REG_COUNTING)
  92. #define PMC_IS_MONITOR(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_MONITOR) == PFM_REG_MONITOR)
  93. #define PMC_IS_CONTROL(i) ((pmu_conf->pmc_desc[i].type & PFM_REG_CONTROL) == PFM_REG_CONTROL)
  94. #define PMC_DFL_VAL(i) pmu_conf->pmc_desc[i].default_value
  95. #define PMC_RSVD_MASK(i) pmu_conf->pmc_desc[i].reserved_mask
  96. #define PMD_PMD_DEP(i) pmu_conf->pmd_desc[i].dep_pmd[0]
  97. #define PMC_PMD_DEP(i) pmu_conf->pmc_desc[i].dep_pmd[0]
  98. #define PFM_NUM_IBRS IA64_NUM_DBG_REGS
  99. #define PFM_NUM_DBRS IA64_NUM_DBG_REGS
  100. #define CTX_OVFL_NOBLOCK(c) ((c)->ctx_fl_block == 0)
  101. #define CTX_HAS_SMPL(c) ((c)->ctx_fl_is_sampling)
  102. #define PFM_CTX_TASK(h) (h)->ctx_task
  103. #define PMU_PMC_OI 5 /* position of pmc.oi bit */
  104. /* XXX: does not support more than 64 PMDs */
  105. #define CTX_USED_PMD(ctx, mask) (ctx)->ctx_used_pmds[0] |= (mask)
  106. #define CTX_IS_USED_PMD(ctx, c) (((ctx)->ctx_used_pmds[0] & (1UL << (c))) != 0UL)
  107. #define CTX_USED_MONITOR(ctx, mask) (ctx)->ctx_used_monitors[0] |= (mask)
  108. #define CTX_USED_IBR(ctx,n) (ctx)->ctx_used_ibrs[(n)>>6] |= 1UL<< ((n) % 64)
  109. #define CTX_USED_DBR(ctx,n) (ctx)->ctx_used_dbrs[(n)>>6] |= 1UL<< ((n) % 64)
  110. #define CTX_USES_DBREGS(ctx) (((pfm_context_t *)(ctx))->ctx_fl_using_dbreg==1)
  111. #define PFM_CODE_RR 0 /* requesting code range restriction */
  112. #define PFM_DATA_RR 1 /* requestion data range restriction */
  113. #define PFM_CPUINFO_CLEAR(v) pfm_get_cpu_var(pfm_syst_info) &= ~(v)
  114. #define PFM_CPUINFO_SET(v) pfm_get_cpu_var(pfm_syst_info) |= (v)
  115. #define PFM_CPUINFO_GET() pfm_get_cpu_var(pfm_syst_info)
  116. #define RDEP(x) (1UL<<(x))
  117. /*
  118. * context protection macros
  119. * in SMP:
  120. * - we need to protect against CPU concurrency (spin_lock)
  121. * - we need to protect against PMU overflow interrupts (local_irq_disable)
  122. * in UP:
  123. * - we need to protect against PMU overflow interrupts (local_irq_disable)
  124. *
  125. * spin_lock_irqsave()/spin_lock_irqrestore():
  126. * in SMP: local_irq_disable + spin_lock
  127. * in UP : local_irq_disable
  128. *
  129. * spin_lock()/spin_lock():
  130. * in UP : removed automatically
  131. * in SMP: protect against context accesses from other CPU. interrupts
  132. * are not masked. This is useful for the PMU interrupt handler
  133. * because we know we will not get PMU concurrency in that code.
  134. */
  135. #define PROTECT_CTX(c, f) \
  136. do { \
  137. DPRINT(("spinlock_irq_save ctx %p by [%d]\n", c, current->pid)); \
  138. spin_lock_irqsave(&(c)->ctx_lock, f); \
  139. DPRINT(("spinlocked ctx %p by [%d]\n", c, current->pid)); \
  140. } while(0)
  141. #define UNPROTECT_CTX(c, f) \
  142. do { \
  143. DPRINT(("spinlock_irq_restore ctx %p by [%d]\n", c, current->pid)); \
  144. spin_unlock_irqrestore(&(c)->ctx_lock, f); \
  145. } while(0)
  146. #define PROTECT_CTX_NOPRINT(c, f) \
  147. do { \
  148. spin_lock_irqsave(&(c)->ctx_lock, f); \
  149. } while(0)
  150. #define UNPROTECT_CTX_NOPRINT(c, f) \
  151. do { \
  152. spin_unlock_irqrestore(&(c)->ctx_lock, f); \
  153. } while(0)
  154. #define PROTECT_CTX_NOIRQ(c) \
  155. do { \
  156. spin_lock(&(c)->ctx_lock); \
  157. } while(0)
  158. #define UNPROTECT_CTX_NOIRQ(c) \
  159. do { \
  160. spin_unlock(&(c)->ctx_lock); \
  161. } while(0)
  162. #ifdef CONFIG_SMP
  163. #define GET_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)
  164. #define INC_ACTIVATION() pfm_get_cpu_var(pmu_activation_number)++
  165. #define SET_ACTIVATION(c) (c)->ctx_last_activation = GET_ACTIVATION()
  166. #else /* !CONFIG_SMP */
  167. #define SET_ACTIVATION(t) do {} while(0)
  168. #define GET_ACTIVATION(t) do {} while(0)
  169. #define INC_ACTIVATION(t) do {} while(0)
  170. #endif /* CONFIG_SMP */
  171. #define SET_PMU_OWNER(t, c) do { pfm_get_cpu_var(pmu_owner) = (t); pfm_get_cpu_var(pmu_ctx) = (c); } while(0)
  172. #define GET_PMU_OWNER() pfm_get_cpu_var(pmu_owner)
  173. #define GET_PMU_CTX() pfm_get_cpu_var(pmu_ctx)
  174. #define LOCK_PFS(g) spin_lock_irqsave(&pfm_sessions.pfs_lock, g)
  175. #define UNLOCK_PFS(g) spin_unlock_irqrestore(&pfm_sessions.pfs_lock, g)
  176. #define PFM_REG_RETFLAG_SET(flags, val) do { flags &= ~PFM_REG_RETFL_MASK; flags |= (val); } while(0)
  177. /*
  178. * cmp0 must be the value of pmc0
  179. */
  180. #define PMC0_HAS_OVFL(cmp0) (cmp0 & ~0x1UL)
  181. #define PFMFS_MAGIC 0xa0b4d889
  182. /*
  183. * debugging
  184. */
  185. #define PFM_DEBUGGING 1
  186. #ifdef PFM_DEBUGGING
  187. #define DPRINT(a) \
  188. do { \
  189. if (unlikely(pfm_sysctl.debug >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
  190. } while (0)
  191. #define DPRINT_ovfl(a) \
  192. do { \
  193. if (unlikely(pfm_sysctl.debug > 0 && pfm_sysctl.debug_ovfl >0)) { printk("%s.%d: CPU%d [%d] ", __FUNCTION__, __LINE__, smp_processor_id(), current->pid); printk a; } \
  194. } while (0)
  195. #endif
  196. /*
  197. * 64-bit software counter structure
  198. *
  199. * the next_reset_type is applied to the next call to pfm_reset_regs()
  200. */
  201. typedef struct {
  202. unsigned long val; /* virtual 64bit counter value */
  203. unsigned long lval; /* last reset value */
  204. unsigned long long_reset; /* reset value on sampling overflow */
  205. unsigned long short_reset; /* reset value on overflow */
  206. unsigned long reset_pmds[4]; /* which other pmds to reset when this counter overflows */
  207. unsigned long smpl_pmds[4]; /* which pmds are accessed when counter overflow */
  208. unsigned long seed; /* seed for random-number generator */
  209. unsigned long mask; /* mask for random-number generator */
  210. unsigned int flags; /* notify/do not notify */
  211. unsigned long eventid; /* overflow event identifier */
  212. } pfm_counter_t;
  213. /*
  214. * context flags
  215. */
  216. typedef struct {
  217. unsigned int block:1; /* when 1, task will blocked on user notifications */
  218. unsigned int system:1; /* do system wide monitoring */
  219. unsigned int using_dbreg:1; /* using range restrictions (debug registers) */
  220. unsigned int is_sampling:1; /* true if using a custom format */
  221. unsigned int excl_idle:1; /* exclude idle task in system wide session */
  222. unsigned int going_zombie:1; /* context is zombie (MASKED+blocking) */
  223. unsigned int trap_reason:2; /* reason for going into pfm_handle_work() */
  224. unsigned int no_msg:1; /* no message sent on overflow */
  225. unsigned int can_restart:1; /* allowed to issue a PFM_RESTART */
  226. unsigned int reserved:22;
  227. } pfm_context_flags_t;
  228. #define PFM_TRAP_REASON_NONE 0x0 /* default value */
  229. #define PFM_TRAP_REASON_BLOCK 0x1 /* we need to block on overflow */
  230. #define PFM_TRAP_REASON_RESET 0x2 /* we need to reset PMDs */
  231. /*
  232. * perfmon context: encapsulates all the state of a monitoring session
  233. */
  234. typedef struct pfm_context {
  235. spinlock_t ctx_lock; /* context protection */
  236. pfm_context_flags_t ctx_flags; /* bitmask of flags (block reason incl.) */
  237. unsigned int ctx_state; /* state: active/inactive (no bitfield) */
  238. struct task_struct *ctx_task; /* task to which context is attached */
  239. unsigned long ctx_ovfl_regs[4]; /* which registers overflowed (notification) */
  240. struct semaphore ctx_restart_sem; /* use for blocking notification mode */
  241. unsigned long ctx_used_pmds[4]; /* bitmask of PMD used */
  242. unsigned long ctx_all_pmds[4]; /* bitmask of all accessible PMDs */
  243. unsigned long ctx_reload_pmds[4]; /* bitmask of force reload PMD on ctxsw in */
  244. unsigned long ctx_all_pmcs[4]; /* bitmask of all accessible PMCs */
  245. unsigned long ctx_reload_pmcs[4]; /* bitmask of force reload PMC on ctxsw in */
  246. unsigned long ctx_used_monitors[4]; /* bitmask of monitor PMC being used */
  247. unsigned long ctx_pmcs[IA64_NUM_PMC_REGS]; /* saved copies of PMC values */
  248. unsigned int ctx_used_ibrs[1]; /* bitmask of used IBR (speedup ctxsw in) */
  249. unsigned int ctx_used_dbrs[1]; /* bitmask of used DBR (speedup ctxsw in) */
  250. unsigned long ctx_dbrs[IA64_NUM_DBG_REGS]; /* DBR values (cache) when not loaded */
  251. unsigned long ctx_ibrs[IA64_NUM_DBG_REGS]; /* IBR values (cache) when not loaded */
  252. pfm_counter_t ctx_pmds[IA64_NUM_PMD_REGS]; /* software state for PMDS */
  253. u64 ctx_saved_psr_up; /* only contains psr.up value */
  254. unsigned long ctx_last_activation; /* context last activation number for last_cpu */
  255. unsigned int ctx_last_cpu; /* CPU id of current or last CPU used (SMP only) */
  256. unsigned int ctx_cpu; /* cpu to which perfmon is applied (system wide) */
  257. int ctx_fd; /* file descriptor used my this context */
  258. pfm_ovfl_arg_t ctx_ovfl_arg; /* argument to custom buffer format handler */
  259. pfm_buffer_fmt_t *ctx_buf_fmt; /* buffer format callbacks */
  260. void *ctx_smpl_hdr; /* points to sampling buffer header kernel vaddr */
  261. unsigned long ctx_smpl_size; /* size of sampling buffer */
  262. void *ctx_smpl_vaddr; /* user level virtual address of smpl buffer */
  263. wait_queue_head_t ctx_msgq_wait;
  264. pfm_msg_t ctx_msgq[PFM_MAX_MSGS];
  265. int ctx_msgq_head;
  266. int ctx_msgq_tail;
  267. struct fasync_struct *ctx_async_queue;
  268. wait_queue_head_t ctx_zombieq; /* termination cleanup wait queue */
  269. } pfm_context_t;
  270. /*
  271. * magic number used to verify that structure is really
  272. * a perfmon context
  273. */
  274. #define PFM_IS_FILE(f) ((f)->f_op == &pfm_file_ops)
  275. #define PFM_GET_CTX(t) ((pfm_context_t *)(t)->thread.pfm_context)
  276. #ifdef CONFIG_SMP
  277. #define SET_LAST_CPU(ctx, v) (ctx)->ctx_last_cpu = (v)
  278. #define GET_LAST_CPU(ctx) (ctx)->ctx_last_cpu
  279. #else
  280. #define SET_LAST_CPU(ctx, v) do {} while(0)
  281. #define GET_LAST_CPU(ctx) do {} while(0)
  282. #endif
  283. #define ctx_fl_block ctx_flags.block
  284. #define ctx_fl_system ctx_flags.system
  285. #define ctx_fl_using_dbreg ctx_flags.using_dbreg
  286. #define ctx_fl_is_sampling ctx_flags.is_sampling
  287. #define ctx_fl_excl_idle ctx_flags.excl_idle
  288. #define ctx_fl_going_zombie ctx_flags.going_zombie
  289. #define ctx_fl_trap_reason ctx_flags.trap_reason
  290. #define ctx_fl_no_msg ctx_flags.no_msg
  291. #define ctx_fl_can_restart ctx_flags.can_restart
  292. #define PFM_SET_WORK_PENDING(t, v) do { (t)->thread.pfm_needs_checking = v; } while(0);
  293. #define PFM_GET_WORK_PENDING(t) (t)->thread.pfm_needs_checking
  294. /*
  295. * global information about all sessions
  296. * mostly used to synchronize between system wide and per-process
  297. */
  298. typedef struct {
  299. spinlock_t pfs_lock; /* lock the structure */
  300. unsigned int pfs_task_sessions; /* number of per task sessions */
  301. unsigned int pfs_sys_sessions; /* number of per system wide sessions */
  302. unsigned int pfs_sys_use_dbregs; /* incremented when a system wide session uses debug regs */
  303. unsigned int pfs_ptrace_use_dbregs; /* incremented when a process uses debug regs */
  304. struct task_struct *pfs_sys_session[NR_CPUS]; /* point to task owning a system-wide session */
  305. } pfm_session_t;
  306. /*
  307. * information about a PMC or PMD.
  308. * dep_pmd[]: a bitmask of dependent PMD registers
  309. * dep_pmc[]: a bitmask of dependent PMC registers
  310. */
  311. typedef int (*pfm_reg_check_t)(struct task_struct *task, pfm_context_t *ctx, unsigned int cnum, unsigned long *val, struct pt_regs *regs);
  312. typedef struct {
  313. unsigned int type;
  314. int pm_pos;
  315. unsigned long default_value; /* power-on default value */
  316. unsigned long reserved_mask; /* bitmask of reserved bits */
  317. pfm_reg_check_t read_check;
  318. pfm_reg_check_t write_check;
  319. unsigned long dep_pmd[4];
  320. unsigned long dep_pmc[4];
  321. } pfm_reg_desc_t;
  322. /* assume cnum is a valid monitor */
  323. #define PMC_PM(cnum, val) (((val) >> (pmu_conf->pmc_desc[cnum].pm_pos)) & 0x1)
  324. /*
  325. * This structure is initialized at boot time and contains
  326. * a description of the PMU main characteristics.
  327. *
  328. * If the probe function is defined, detection is based
  329. * on its return value:
  330. * - 0 means recognized PMU
  331. * - anything else means not supported
  332. * When the probe function is not defined, then the pmu_family field
  333. * is used and it must match the host CPU family such that:
  334. * - cpu->family & config->pmu_family != 0
  335. */
  336. typedef struct {
  337. unsigned long ovfl_val; /* overflow value for counters */
  338. pfm_reg_desc_t *pmc_desc; /* detailed PMC register dependencies descriptions */
  339. pfm_reg_desc_t *pmd_desc; /* detailed PMD register dependencies descriptions */
  340. unsigned int num_pmcs; /* number of PMCS: computed at init time */
  341. unsigned int num_pmds; /* number of PMDS: computed at init time */
  342. unsigned long impl_pmcs[4]; /* bitmask of implemented PMCS */
  343. unsigned long impl_pmds[4]; /* bitmask of implemented PMDS */
  344. char *pmu_name; /* PMU family name */
  345. unsigned int pmu_family; /* cpuid family pattern used to identify pmu */
  346. unsigned int flags; /* pmu specific flags */
  347. unsigned int num_ibrs; /* number of IBRS: computed at init time */
  348. unsigned int num_dbrs; /* number of DBRS: computed at init time */
  349. unsigned int num_counters; /* PMC/PMD counting pairs : computed at init time */
  350. int (*probe)(void); /* customized probe routine */
  351. unsigned int use_rr_dbregs:1; /* set if debug registers used for range restriction */
  352. } pmu_config_t;
  353. /*
  354. * PMU specific flags
  355. */
  356. #define PFM_PMU_IRQ_RESEND 1 /* PMU needs explicit IRQ resend */
  357. /*
  358. * debug register related type definitions
  359. */
  360. typedef struct {
  361. unsigned long ibr_mask:56;
  362. unsigned long ibr_plm:4;
  363. unsigned long ibr_ig:3;
  364. unsigned long ibr_x:1;
  365. } ibr_mask_reg_t;
  366. typedef struct {
  367. unsigned long dbr_mask:56;
  368. unsigned long dbr_plm:4;
  369. unsigned long dbr_ig:2;
  370. unsigned long dbr_w:1;
  371. unsigned long dbr_r:1;
  372. } dbr_mask_reg_t;
  373. typedef union {
  374. unsigned long val;
  375. ibr_mask_reg_t ibr;
  376. dbr_mask_reg_t dbr;
  377. } dbreg_t;
  378. /*
  379. * perfmon command descriptions
  380. */
  381. typedef struct {
  382. int (*cmd_func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  383. char *cmd_name;
  384. int cmd_flags;
  385. unsigned int cmd_narg;
  386. size_t cmd_argsize;
  387. int (*cmd_getsize)(void *arg, size_t *sz);
  388. } pfm_cmd_desc_t;
  389. #define PFM_CMD_FD 0x01 /* command requires a file descriptor */
  390. #define PFM_CMD_ARG_READ 0x02 /* command must read argument(s) */
  391. #define PFM_CMD_ARG_RW 0x04 /* command must read/write argument(s) */
  392. #define PFM_CMD_STOP 0x08 /* command does not work on zombie context */
  393. #define PFM_CMD_NAME(cmd) pfm_cmd_tab[(cmd)].cmd_name
  394. #define PFM_CMD_READ_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_READ)
  395. #define PFM_CMD_RW_ARG(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_ARG_RW)
  396. #define PFM_CMD_USE_FD(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_FD)
  397. #define PFM_CMD_STOPPED(cmd) (pfm_cmd_tab[(cmd)].cmd_flags & PFM_CMD_STOP)
  398. #define PFM_CMD_ARG_MANY -1 /* cannot be zero */
  399. typedef struct {
  400. unsigned long pfm_spurious_ovfl_intr_count; /* keep track of spurious ovfl interrupts */
  401. unsigned long pfm_replay_ovfl_intr_count; /* keep track of replayed ovfl interrupts */
  402. unsigned long pfm_ovfl_intr_count; /* keep track of ovfl interrupts */
  403. unsigned long pfm_ovfl_intr_cycles; /* cycles spent processing ovfl interrupts */
  404. unsigned long pfm_ovfl_intr_cycles_min; /* min cycles spent processing ovfl interrupts */
  405. unsigned long pfm_ovfl_intr_cycles_max; /* max cycles spent processing ovfl interrupts */
  406. unsigned long pfm_smpl_handler_calls;
  407. unsigned long pfm_smpl_handler_cycles;
  408. char pad[SMP_CACHE_BYTES] ____cacheline_aligned;
  409. } pfm_stats_t;
  410. /*
  411. * perfmon internal variables
  412. */
  413. static pfm_stats_t pfm_stats[NR_CPUS];
  414. static pfm_session_t pfm_sessions; /* global sessions information */
  415. static DEFINE_SPINLOCK(pfm_alt_install_check);
  416. static pfm_intr_handler_desc_t *pfm_alt_intr_handler;
  417. static struct proc_dir_entry *perfmon_dir;
  418. static pfm_uuid_t pfm_null_uuid = {0,};
  419. static spinlock_t pfm_buffer_fmt_lock;
  420. static LIST_HEAD(pfm_buffer_fmt_list);
  421. static pmu_config_t *pmu_conf;
  422. /* sysctl() controls */
  423. pfm_sysctl_t pfm_sysctl;
  424. EXPORT_SYMBOL(pfm_sysctl);
  425. static ctl_table pfm_ctl_table[]={
  426. {1, "debug", &pfm_sysctl.debug, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
  427. {2, "debug_ovfl", &pfm_sysctl.debug_ovfl, sizeof(int), 0666, NULL, &proc_dointvec, NULL,},
  428. {3, "fastctxsw", &pfm_sysctl.fastctxsw, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
  429. {4, "expert_mode", &pfm_sysctl.expert_mode, sizeof(int), 0600, NULL, &proc_dointvec, NULL,},
  430. { 0, },
  431. };
  432. static ctl_table pfm_sysctl_dir[] = {
  433. {1, "perfmon", NULL, 0, 0755, pfm_ctl_table, },
  434. {0,},
  435. };
  436. static ctl_table pfm_sysctl_root[] = {
  437. {1, "kernel", NULL, 0, 0755, pfm_sysctl_dir, },
  438. {0,},
  439. };
  440. static struct ctl_table_header *pfm_sysctl_header;
  441. static int pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  442. static int pfm_flush(struct file *filp);
  443. #define pfm_get_cpu_var(v) __ia64_per_cpu_var(v)
  444. #define pfm_get_cpu_data(a,b) per_cpu(a, b)
  445. static inline void
  446. pfm_put_task(struct task_struct *task)
  447. {
  448. if (task != current) put_task_struct(task);
  449. }
  450. static inline void
  451. pfm_set_task_notify(struct task_struct *task)
  452. {
  453. struct thread_info *info;
  454. info = (struct thread_info *) ((char *) task + IA64_TASK_SIZE);
  455. set_bit(TIF_NOTIFY_RESUME, &info->flags);
  456. }
  457. static inline void
  458. pfm_clear_task_notify(void)
  459. {
  460. clear_thread_flag(TIF_NOTIFY_RESUME);
  461. }
  462. static inline void
  463. pfm_reserve_page(unsigned long a)
  464. {
  465. SetPageReserved(vmalloc_to_page((void *)a));
  466. }
  467. static inline void
  468. pfm_unreserve_page(unsigned long a)
  469. {
  470. ClearPageReserved(vmalloc_to_page((void*)a));
  471. }
  472. static inline unsigned long
  473. pfm_protect_ctx_ctxsw(pfm_context_t *x)
  474. {
  475. spin_lock(&(x)->ctx_lock);
  476. return 0UL;
  477. }
  478. static inline void
  479. pfm_unprotect_ctx_ctxsw(pfm_context_t *x, unsigned long f)
  480. {
  481. spin_unlock(&(x)->ctx_lock);
  482. }
  483. static inline unsigned int
  484. pfm_do_munmap(struct mm_struct *mm, unsigned long addr, size_t len, int acct)
  485. {
  486. return do_munmap(mm, addr, len);
  487. }
  488. static inline unsigned long
  489. pfm_get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags, unsigned long exec)
  490. {
  491. return get_unmapped_area(file, addr, len, pgoff, flags);
  492. }
  493. static struct super_block *
  494. pfmfs_get_sb(struct file_system_type *fs_type, int flags, const char *dev_name, void *data)
  495. {
  496. return get_sb_pseudo(fs_type, "pfm:", NULL, PFMFS_MAGIC);
  497. }
  498. static struct file_system_type pfm_fs_type = {
  499. .name = "pfmfs",
  500. .get_sb = pfmfs_get_sb,
  501. .kill_sb = kill_anon_super,
  502. };
  503. DEFINE_PER_CPU(unsigned long, pfm_syst_info);
  504. DEFINE_PER_CPU(struct task_struct *, pmu_owner);
  505. DEFINE_PER_CPU(pfm_context_t *, pmu_ctx);
  506. DEFINE_PER_CPU(unsigned long, pmu_activation_number);
  507. EXPORT_PER_CPU_SYMBOL_GPL(pfm_syst_info);
  508. /* forward declaration */
  509. static struct file_operations pfm_file_ops;
  510. /*
  511. * forward declarations
  512. */
  513. #ifndef CONFIG_SMP
  514. static void pfm_lazy_save_regs (struct task_struct *ta);
  515. #endif
  516. void dump_pmu_state(const char *);
  517. static int pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  518. #include "perfmon_itanium.h"
  519. #include "perfmon_mckinley.h"
  520. #include "perfmon_generic.h"
  521. static pmu_config_t *pmu_confs[]={
  522. &pmu_conf_mck,
  523. &pmu_conf_ita,
  524. &pmu_conf_gen, /* must be last */
  525. NULL
  526. };
  527. static int pfm_end_notify_user(pfm_context_t *ctx);
  528. static inline void
  529. pfm_clear_psr_pp(void)
  530. {
  531. ia64_rsm(IA64_PSR_PP);
  532. ia64_srlz_i();
  533. }
  534. static inline void
  535. pfm_set_psr_pp(void)
  536. {
  537. ia64_ssm(IA64_PSR_PP);
  538. ia64_srlz_i();
  539. }
  540. static inline void
  541. pfm_clear_psr_up(void)
  542. {
  543. ia64_rsm(IA64_PSR_UP);
  544. ia64_srlz_i();
  545. }
  546. static inline void
  547. pfm_set_psr_up(void)
  548. {
  549. ia64_ssm(IA64_PSR_UP);
  550. ia64_srlz_i();
  551. }
  552. static inline unsigned long
  553. pfm_get_psr(void)
  554. {
  555. unsigned long tmp;
  556. tmp = ia64_getreg(_IA64_REG_PSR);
  557. ia64_srlz_i();
  558. return tmp;
  559. }
  560. static inline void
  561. pfm_set_psr_l(unsigned long val)
  562. {
  563. ia64_setreg(_IA64_REG_PSR_L, val);
  564. ia64_srlz_i();
  565. }
  566. static inline void
  567. pfm_freeze_pmu(void)
  568. {
  569. ia64_set_pmc(0,1UL);
  570. ia64_srlz_d();
  571. }
  572. static inline void
  573. pfm_unfreeze_pmu(void)
  574. {
  575. ia64_set_pmc(0,0UL);
  576. ia64_srlz_d();
  577. }
  578. static inline void
  579. pfm_restore_ibrs(unsigned long *ibrs, unsigned int nibrs)
  580. {
  581. int i;
  582. for (i=0; i < nibrs; i++) {
  583. ia64_set_ibr(i, ibrs[i]);
  584. ia64_dv_serialize_instruction();
  585. }
  586. ia64_srlz_i();
  587. }
  588. static inline void
  589. pfm_restore_dbrs(unsigned long *dbrs, unsigned int ndbrs)
  590. {
  591. int i;
  592. for (i=0; i < ndbrs; i++) {
  593. ia64_set_dbr(i, dbrs[i]);
  594. ia64_dv_serialize_data();
  595. }
  596. ia64_srlz_d();
  597. }
  598. /*
  599. * PMD[i] must be a counter. no check is made
  600. */
  601. static inline unsigned long
  602. pfm_read_soft_counter(pfm_context_t *ctx, int i)
  603. {
  604. return ctx->ctx_pmds[i].val + (ia64_get_pmd(i) & pmu_conf->ovfl_val);
  605. }
  606. /*
  607. * PMD[i] must be a counter. no check is made
  608. */
  609. static inline void
  610. pfm_write_soft_counter(pfm_context_t *ctx, int i, unsigned long val)
  611. {
  612. unsigned long ovfl_val = pmu_conf->ovfl_val;
  613. ctx->ctx_pmds[i].val = val & ~ovfl_val;
  614. /*
  615. * writing to unimplemented part is ignore, so we do not need to
  616. * mask off top part
  617. */
  618. ia64_set_pmd(i, val & ovfl_val);
  619. }
  620. static pfm_msg_t *
  621. pfm_get_new_msg(pfm_context_t *ctx)
  622. {
  623. int idx, next;
  624. next = (ctx->ctx_msgq_tail+1) % PFM_MAX_MSGS;
  625. DPRINT(("ctx_fd=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  626. if (next == ctx->ctx_msgq_head) return NULL;
  627. idx = ctx->ctx_msgq_tail;
  628. ctx->ctx_msgq_tail = next;
  629. DPRINT(("ctx=%p head=%d tail=%d msg=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, idx));
  630. return ctx->ctx_msgq+idx;
  631. }
  632. static pfm_msg_t *
  633. pfm_get_next_msg(pfm_context_t *ctx)
  634. {
  635. pfm_msg_t *msg;
  636. DPRINT(("ctx=%p head=%d tail=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  637. if (PFM_CTXQ_EMPTY(ctx)) return NULL;
  638. /*
  639. * get oldest message
  640. */
  641. msg = ctx->ctx_msgq+ctx->ctx_msgq_head;
  642. /*
  643. * and move forward
  644. */
  645. ctx->ctx_msgq_head = (ctx->ctx_msgq_head+1) % PFM_MAX_MSGS;
  646. DPRINT(("ctx=%p head=%d tail=%d type=%d\n", ctx, ctx->ctx_msgq_head, ctx->ctx_msgq_tail, msg->pfm_gen_msg.msg_type));
  647. return msg;
  648. }
  649. static void
  650. pfm_reset_msgq(pfm_context_t *ctx)
  651. {
  652. ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
  653. DPRINT(("ctx=%p msgq reset\n", ctx));
  654. }
  655. static void *
  656. pfm_rvmalloc(unsigned long size)
  657. {
  658. void *mem;
  659. unsigned long addr;
  660. size = PAGE_ALIGN(size);
  661. mem = vmalloc(size);
  662. if (mem) {
  663. //printk("perfmon: CPU%d pfm_rvmalloc(%ld)=%p\n", smp_processor_id(), size, mem);
  664. memset(mem, 0, size);
  665. addr = (unsigned long)mem;
  666. while (size > 0) {
  667. pfm_reserve_page(addr);
  668. addr+=PAGE_SIZE;
  669. size-=PAGE_SIZE;
  670. }
  671. }
  672. return mem;
  673. }
  674. static void
  675. pfm_rvfree(void *mem, unsigned long size)
  676. {
  677. unsigned long addr;
  678. if (mem) {
  679. DPRINT(("freeing physical buffer @%p size=%lu\n", mem, size));
  680. addr = (unsigned long) mem;
  681. while ((long) size > 0) {
  682. pfm_unreserve_page(addr);
  683. addr+=PAGE_SIZE;
  684. size-=PAGE_SIZE;
  685. }
  686. vfree(mem);
  687. }
  688. return;
  689. }
  690. static pfm_context_t *
  691. pfm_context_alloc(void)
  692. {
  693. pfm_context_t *ctx;
  694. /*
  695. * allocate context descriptor
  696. * must be able to free with interrupts disabled
  697. */
  698. ctx = kmalloc(sizeof(pfm_context_t), GFP_KERNEL);
  699. if (ctx) {
  700. memset(ctx, 0, sizeof(pfm_context_t));
  701. DPRINT(("alloc ctx @%p\n", ctx));
  702. }
  703. return ctx;
  704. }
  705. static void
  706. pfm_context_free(pfm_context_t *ctx)
  707. {
  708. if (ctx) {
  709. DPRINT(("free ctx @%p\n", ctx));
  710. kfree(ctx);
  711. }
  712. }
  713. static void
  714. pfm_mask_monitoring(struct task_struct *task)
  715. {
  716. pfm_context_t *ctx = PFM_GET_CTX(task);
  717. struct thread_struct *th = &task->thread;
  718. unsigned long mask, val, ovfl_mask;
  719. int i;
  720. DPRINT_ovfl(("masking monitoring for [%d]\n", task->pid));
  721. ovfl_mask = pmu_conf->ovfl_val;
  722. /*
  723. * monitoring can only be masked as a result of a valid
  724. * counter overflow. In UP, it means that the PMU still
  725. * has an owner. Note that the owner can be different
  726. * from the current task. However the PMU state belongs
  727. * to the owner.
  728. * In SMP, a valid overflow only happens when task is
  729. * current. Therefore if we come here, we know that
  730. * the PMU state belongs to the current task, therefore
  731. * we can access the live registers.
  732. *
  733. * So in both cases, the live register contains the owner's
  734. * state. We can ONLY touch the PMU registers and NOT the PSR.
  735. *
  736. * As a consequence to this call, the thread->pmds[] array
  737. * contains stale information which must be ignored
  738. * when context is reloaded AND monitoring is active (see
  739. * pfm_restart).
  740. */
  741. mask = ctx->ctx_used_pmds[0];
  742. for (i = 0; mask; i++, mask>>=1) {
  743. /* skip non used pmds */
  744. if ((mask & 0x1) == 0) continue;
  745. val = ia64_get_pmd(i);
  746. if (PMD_IS_COUNTING(i)) {
  747. /*
  748. * we rebuild the full 64 bit value of the counter
  749. */
  750. ctx->ctx_pmds[i].val += (val & ovfl_mask);
  751. } else {
  752. ctx->ctx_pmds[i].val = val;
  753. }
  754. DPRINT_ovfl(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
  755. i,
  756. ctx->ctx_pmds[i].val,
  757. val & ovfl_mask));
  758. }
  759. /*
  760. * mask monitoring by setting the privilege level to 0
  761. * we cannot use psr.pp/psr.up for this, it is controlled by
  762. * the user
  763. *
  764. * if task is current, modify actual registers, otherwise modify
  765. * thread save state, i.e., what will be restored in pfm_load_regs()
  766. */
  767. mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
  768. for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
  769. if ((mask & 0x1) == 0UL) continue;
  770. ia64_set_pmc(i, th->pmcs[i] & ~0xfUL);
  771. th->pmcs[i] &= ~0xfUL;
  772. DPRINT_ovfl(("pmc[%d]=0x%lx\n", i, th->pmcs[i]));
  773. }
  774. /*
  775. * make all of this visible
  776. */
  777. ia64_srlz_d();
  778. }
  779. /*
  780. * must always be done with task == current
  781. *
  782. * context must be in MASKED state when calling
  783. */
  784. static void
  785. pfm_restore_monitoring(struct task_struct *task)
  786. {
  787. pfm_context_t *ctx = PFM_GET_CTX(task);
  788. struct thread_struct *th = &task->thread;
  789. unsigned long mask, ovfl_mask;
  790. unsigned long psr, val;
  791. int i, is_system;
  792. is_system = ctx->ctx_fl_system;
  793. ovfl_mask = pmu_conf->ovfl_val;
  794. if (task != current) {
  795. printk(KERN_ERR "perfmon.%d: invalid task[%d] current[%d]\n", __LINE__, task->pid, current->pid);
  796. return;
  797. }
  798. if (ctx->ctx_state != PFM_CTX_MASKED) {
  799. printk(KERN_ERR "perfmon.%d: task[%d] current[%d] invalid state=%d\n", __LINE__,
  800. task->pid, current->pid, ctx->ctx_state);
  801. return;
  802. }
  803. psr = pfm_get_psr();
  804. /*
  805. * monitoring is masked via the PMC.
  806. * As we restore their value, we do not want each counter to
  807. * restart right away. We stop monitoring using the PSR,
  808. * restore the PMC (and PMD) and then re-establish the psr
  809. * as it was. Note that there can be no pending overflow at
  810. * this point, because monitoring was MASKED.
  811. *
  812. * system-wide session are pinned and self-monitoring
  813. */
  814. if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
  815. /* disable dcr pp */
  816. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
  817. pfm_clear_psr_pp();
  818. } else {
  819. pfm_clear_psr_up();
  820. }
  821. /*
  822. * first, we restore the PMD
  823. */
  824. mask = ctx->ctx_used_pmds[0];
  825. for (i = 0; mask; i++, mask>>=1) {
  826. /* skip non used pmds */
  827. if ((mask & 0x1) == 0) continue;
  828. if (PMD_IS_COUNTING(i)) {
  829. /*
  830. * we split the 64bit value according to
  831. * counter width
  832. */
  833. val = ctx->ctx_pmds[i].val & ovfl_mask;
  834. ctx->ctx_pmds[i].val &= ~ovfl_mask;
  835. } else {
  836. val = ctx->ctx_pmds[i].val;
  837. }
  838. ia64_set_pmd(i, val);
  839. DPRINT(("pmd[%d]=0x%lx hw_pmd=0x%lx\n",
  840. i,
  841. ctx->ctx_pmds[i].val,
  842. val));
  843. }
  844. /*
  845. * restore the PMCs
  846. */
  847. mask = ctx->ctx_used_monitors[0] >> PMU_FIRST_COUNTER;
  848. for(i= PMU_FIRST_COUNTER; mask; i++, mask>>=1) {
  849. if ((mask & 0x1) == 0UL) continue;
  850. th->pmcs[i] = ctx->ctx_pmcs[i];
  851. ia64_set_pmc(i, th->pmcs[i]);
  852. DPRINT(("[%d] pmc[%d]=0x%lx\n", task->pid, i, th->pmcs[i]));
  853. }
  854. ia64_srlz_d();
  855. /*
  856. * must restore DBR/IBR because could be modified while masked
  857. * XXX: need to optimize
  858. */
  859. if (ctx->ctx_fl_using_dbreg) {
  860. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  861. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  862. }
  863. /*
  864. * now restore PSR
  865. */
  866. if (is_system && (PFM_CPUINFO_GET() & PFM_CPUINFO_DCR_PP)) {
  867. /* enable dcr pp */
  868. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
  869. ia64_srlz_i();
  870. }
  871. pfm_set_psr_l(psr);
  872. }
  873. static inline void
  874. pfm_save_pmds(unsigned long *pmds, unsigned long mask)
  875. {
  876. int i;
  877. ia64_srlz_d();
  878. for (i=0; mask; i++, mask>>=1) {
  879. if (mask & 0x1) pmds[i] = ia64_get_pmd(i);
  880. }
  881. }
  882. /*
  883. * reload from thread state (used for ctxw only)
  884. */
  885. static inline void
  886. pfm_restore_pmds(unsigned long *pmds, unsigned long mask)
  887. {
  888. int i;
  889. unsigned long val, ovfl_val = pmu_conf->ovfl_val;
  890. for (i=0; mask; i++, mask>>=1) {
  891. if ((mask & 0x1) == 0) continue;
  892. val = PMD_IS_COUNTING(i) ? pmds[i] & ovfl_val : pmds[i];
  893. ia64_set_pmd(i, val);
  894. }
  895. ia64_srlz_d();
  896. }
  897. /*
  898. * propagate PMD from context to thread-state
  899. */
  900. static inline void
  901. pfm_copy_pmds(struct task_struct *task, pfm_context_t *ctx)
  902. {
  903. struct thread_struct *thread = &task->thread;
  904. unsigned long ovfl_val = pmu_conf->ovfl_val;
  905. unsigned long mask = ctx->ctx_all_pmds[0];
  906. unsigned long val;
  907. int i;
  908. DPRINT(("mask=0x%lx\n", mask));
  909. for (i=0; mask; i++, mask>>=1) {
  910. val = ctx->ctx_pmds[i].val;
  911. /*
  912. * We break up the 64 bit value into 2 pieces
  913. * the lower bits go to the machine state in the
  914. * thread (will be reloaded on ctxsw in).
  915. * The upper part stays in the soft-counter.
  916. */
  917. if (PMD_IS_COUNTING(i)) {
  918. ctx->ctx_pmds[i].val = val & ~ovfl_val;
  919. val &= ovfl_val;
  920. }
  921. thread->pmds[i] = val;
  922. DPRINT(("pmd[%d]=0x%lx soft_val=0x%lx\n",
  923. i,
  924. thread->pmds[i],
  925. ctx->ctx_pmds[i].val));
  926. }
  927. }
  928. /*
  929. * propagate PMC from context to thread-state
  930. */
  931. static inline void
  932. pfm_copy_pmcs(struct task_struct *task, pfm_context_t *ctx)
  933. {
  934. struct thread_struct *thread = &task->thread;
  935. unsigned long mask = ctx->ctx_all_pmcs[0];
  936. int i;
  937. DPRINT(("mask=0x%lx\n", mask));
  938. for (i=0; mask; i++, mask>>=1) {
  939. /* masking 0 with ovfl_val yields 0 */
  940. thread->pmcs[i] = ctx->ctx_pmcs[i];
  941. DPRINT(("pmc[%d]=0x%lx\n", i, thread->pmcs[i]));
  942. }
  943. }
  944. static inline void
  945. pfm_restore_pmcs(unsigned long *pmcs, unsigned long mask)
  946. {
  947. int i;
  948. for (i=0; mask; i++, mask>>=1) {
  949. if ((mask & 0x1) == 0) continue;
  950. ia64_set_pmc(i, pmcs[i]);
  951. }
  952. ia64_srlz_d();
  953. }
  954. static inline int
  955. pfm_uuid_cmp(pfm_uuid_t a, pfm_uuid_t b)
  956. {
  957. return memcmp(a, b, sizeof(pfm_uuid_t));
  958. }
  959. static inline int
  960. pfm_buf_fmt_exit(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, struct pt_regs *regs)
  961. {
  962. int ret = 0;
  963. if (fmt->fmt_exit) ret = (*fmt->fmt_exit)(task, buf, regs);
  964. return ret;
  965. }
  966. static inline int
  967. pfm_buf_fmt_getsize(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags, int cpu, void *arg, unsigned long *size)
  968. {
  969. int ret = 0;
  970. if (fmt->fmt_getsize) ret = (*fmt->fmt_getsize)(task, flags, cpu, arg, size);
  971. return ret;
  972. }
  973. static inline int
  974. pfm_buf_fmt_validate(pfm_buffer_fmt_t *fmt, struct task_struct *task, unsigned int flags,
  975. int cpu, void *arg)
  976. {
  977. int ret = 0;
  978. if (fmt->fmt_validate) ret = (*fmt->fmt_validate)(task, flags, cpu, arg);
  979. return ret;
  980. }
  981. static inline int
  982. pfm_buf_fmt_init(pfm_buffer_fmt_t *fmt, struct task_struct *task, void *buf, unsigned int flags,
  983. int cpu, void *arg)
  984. {
  985. int ret = 0;
  986. if (fmt->fmt_init) ret = (*fmt->fmt_init)(task, buf, flags, cpu, arg);
  987. return ret;
  988. }
  989. static inline int
  990. pfm_buf_fmt_restart(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
  991. {
  992. int ret = 0;
  993. if (fmt->fmt_restart) ret = (*fmt->fmt_restart)(task, ctrl, buf, regs);
  994. return ret;
  995. }
  996. static inline int
  997. pfm_buf_fmt_restart_active(pfm_buffer_fmt_t *fmt, struct task_struct *task, pfm_ovfl_ctrl_t *ctrl, void *buf, struct pt_regs *regs)
  998. {
  999. int ret = 0;
  1000. if (fmt->fmt_restart_active) ret = (*fmt->fmt_restart_active)(task, ctrl, buf, regs);
  1001. return ret;
  1002. }
  1003. static pfm_buffer_fmt_t *
  1004. __pfm_find_buffer_fmt(pfm_uuid_t uuid)
  1005. {
  1006. struct list_head * pos;
  1007. pfm_buffer_fmt_t * entry;
  1008. list_for_each(pos, &pfm_buffer_fmt_list) {
  1009. entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
  1010. if (pfm_uuid_cmp(uuid, entry->fmt_uuid) == 0)
  1011. return entry;
  1012. }
  1013. return NULL;
  1014. }
  1015. /*
  1016. * find a buffer format based on its uuid
  1017. */
  1018. static pfm_buffer_fmt_t *
  1019. pfm_find_buffer_fmt(pfm_uuid_t uuid)
  1020. {
  1021. pfm_buffer_fmt_t * fmt;
  1022. spin_lock(&pfm_buffer_fmt_lock);
  1023. fmt = __pfm_find_buffer_fmt(uuid);
  1024. spin_unlock(&pfm_buffer_fmt_lock);
  1025. return fmt;
  1026. }
  1027. int
  1028. pfm_register_buffer_fmt(pfm_buffer_fmt_t *fmt)
  1029. {
  1030. int ret = 0;
  1031. /* some sanity checks */
  1032. if (fmt == NULL || fmt->fmt_name == NULL) return -EINVAL;
  1033. /* we need at least a handler */
  1034. if (fmt->fmt_handler == NULL) return -EINVAL;
  1035. /*
  1036. * XXX: need check validity of fmt_arg_size
  1037. */
  1038. spin_lock(&pfm_buffer_fmt_lock);
  1039. if (__pfm_find_buffer_fmt(fmt->fmt_uuid)) {
  1040. printk(KERN_ERR "perfmon: duplicate sampling format: %s\n", fmt->fmt_name);
  1041. ret = -EBUSY;
  1042. goto out;
  1043. }
  1044. list_add(&fmt->fmt_list, &pfm_buffer_fmt_list);
  1045. printk(KERN_INFO "perfmon: added sampling format %s\n", fmt->fmt_name);
  1046. out:
  1047. spin_unlock(&pfm_buffer_fmt_lock);
  1048. return ret;
  1049. }
  1050. EXPORT_SYMBOL(pfm_register_buffer_fmt);
  1051. int
  1052. pfm_unregister_buffer_fmt(pfm_uuid_t uuid)
  1053. {
  1054. pfm_buffer_fmt_t *fmt;
  1055. int ret = 0;
  1056. spin_lock(&pfm_buffer_fmt_lock);
  1057. fmt = __pfm_find_buffer_fmt(uuid);
  1058. if (!fmt) {
  1059. printk(KERN_ERR "perfmon: cannot unregister format, not found\n");
  1060. ret = -EINVAL;
  1061. goto out;
  1062. }
  1063. list_del_init(&fmt->fmt_list);
  1064. printk(KERN_INFO "perfmon: removed sampling format: %s\n", fmt->fmt_name);
  1065. out:
  1066. spin_unlock(&pfm_buffer_fmt_lock);
  1067. return ret;
  1068. }
  1069. EXPORT_SYMBOL(pfm_unregister_buffer_fmt);
  1070. extern void update_pal_halt_status(int);
  1071. static int
  1072. pfm_reserve_session(struct task_struct *task, int is_syswide, unsigned int cpu)
  1073. {
  1074. unsigned long flags;
  1075. /*
  1076. * validy checks on cpu_mask have been done upstream
  1077. */
  1078. LOCK_PFS(flags);
  1079. DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1080. pfm_sessions.pfs_sys_sessions,
  1081. pfm_sessions.pfs_task_sessions,
  1082. pfm_sessions.pfs_sys_use_dbregs,
  1083. is_syswide,
  1084. cpu));
  1085. if (is_syswide) {
  1086. /*
  1087. * cannot mix system wide and per-task sessions
  1088. */
  1089. if (pfm_sessions.pfs_task_sessions > 0UL) {
  1090. DPRINT(("system wide not possible, %u conflicting task_sessions\n",
  1091. pfm_sessions.pfs_task_sessions));
  1092. goto abort;
  1093. }
  1094. if (pfm_sessions.pfs_sys_session[cpu]) goto error_conflict;
  1095. DPRINT(("reserving system wide session on CPU%u currently on CPU%u\n", cpu, smp_processor_id()));
  1096. pfm_sessions.pfs_sys_session[cpu] = task;
  1097. pfm_sessions.pfs_sys_sessions++ ;
  1098. } else {
  1099. if (pfm_sessions.pfs_sys_sessions) goto abort;
  1100. pfm_sessions.pfs_task_sessions++;
  1101. }
  1102. DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1103. pfm_sessions.pfs_sys_sessions,
  1104. pfm_sessions.pfs_task_sessions,
  1105. pfm_sessions.pfs_sys_use_dbregs,
  1106. is_syswide,
  1107. cpu));
  1108. /*
  1109. * disable default_idle() to go to PAL_HALT
  1110. */
  1111. update_pal_halt_status(0);
  1112. UNLOCK_PFS(flags);
  1113. return 0;
  1114. error_conflict:
  1115. DPRINT(("system wide not possible, conflicting session [%d] on CPU%d\n",
  1116. pfm_sessions.pfs_sys_session[cpu]->pid,
  1117. cpu));
  1118. abort:
  1119. UNLOCK_PFS(flags);
  1120. return -EBUSY;
  1121. }
  1122. static int
  1123. pfm_unreserve_session(pfm_context_t *ctx, int is_syswide, unsigned int cpu)
  1124. {
  1125. unsigned long flags;
  1126. /*
  1127. * validy checks on cpu_mask have been done upstream
  1128. */
  1129. LOCK_PFS(flags);
  1130. DPRINT(("in sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1131. pfm_sessions.pfs_sys_sessions,
  1132. pfm_sessions.pfs_task_sessions,
  1133. pfm_sessions.pfs_sys_use_dbregs,
  1134. is_syswide,
  1135. cpu));
  1136. if (is_syswide) {
  1137. pfm_sessions.pfs_sys_session[cpu] = NULL;
  1138. /*
  1139. * would not work with perfmon+more than one bit in cpu_mask
  1140. */
  1141. if (ctx && ctx->ctx_fl_using_dbreg) {
  1142. if (pfm_sessions.pfs_sys_use_dbregs == 0) {
  1143. printk(KERN_ERR "perfmon: invalid release for ctx %p sys_use_dbregs=0\n", ctx);
  1144. } else {
  1145. pfm_sessions.pfs_sys_use_dbregs--;
  1146. }
  1147. }
  1148. pfm_sessions.pfs_sys_sessions--;
  1149. } else {
  1150. pfm_sessions.pfs_task_sessions--;
  1151. }
  1152. DPRINT(("out sys_sessions=%u task_sessions=%u dbregs=%u syswide=%d cpu=%u\n",
  1153. pfm_sessions.pfs_sys_sessions,
  1154. pfm_sessions.pfs_task_sessions,
  1155. pfm_sessions.pfs_sys_use_dbregs,
  1156. is_syswide,
  1157. cpu));
  1158. /*
  1159. * if possible, enable default_idle() to go into PAL_HALT
  1160. */
  1161. if (pfm_sessions.pfs_task_sessions == 0 && pfm_sessions.pfs_sys_sessions == 0)
  1162. update_pal_halt_status(1);
  1163. UNLOCK_PFS(flags);
  1164. return 0;
  1165. }
  1166. /*
  1167. * removes virtual mapping of the sampling buffer.
  1168. * IMPORTANT: cannot be called with interrupts disable, e.g. inside
  1169. * a PROTECT_CTX() section.
  1170. */
  1171. static int
  1172. pfm_remove_smpl_mapping(struct task_struct *task, void *vaddr, unsigned long size)
  1173. {
  1174. int r;
  1175. /* sanity checks */
  1176. if (task->mm == NULL || size == 0UL || vaddr == NULL) {
  1177. printk(KERN_ERR "perfmon: pfm_remove_smpl_mapping [%d] invalid context mm=%p\n", task->pid, task->mm);
  1178. return -EINVAL;
  1179. }
  1180. DPRINT(("smpl_vaddr=%p size=%lu\n", vaddr, size));
  1181. /*
  1182. * does the actual unmapping
  1183. */
  1184. down_write(&task->mm->mmap_sem);
  1185. DPRINT(("down_write done smpl_vaddr=%p size=%lu\n", vaddr, size));
  1186. r = pfm_do_munmap(task->mm, (unsigned long)vaddr, size, 0);
  1187. up_write(&task->mm->mmap_sem);
  1188. if (r !=0) {
  1189. printk(KERN_ERR "perfmon: [%d] unable to unmap sampling buffer @%p size=%lu\n", task->pid, vaddr, size);
  1190. }
  1191. DPRINT(("do_unmap(%p, %lu)=%d\n", vaddr, size, r));
  1192. return 0;
  1193. }
  1194. /*
  1195. * free actual physical storage used by sampling buffer
  1196. */
  1197. #if 0
  1198. static int
  1199. pfm_free_smpl_buffer(pfm_context_t *ctx)
  1200. {
  1201. pfm_buffer_fmt_t *fmt;
  1202. if (ctx->ctx_smpl_hdr == NULL) goto invalid_free;
  1203. /*
  1204. * we won't use the buffer format anymore
  1205. */
  1206. fmt = ctx->ctx_buf_fmt;
  1207. DPRINT(("sampling buffer @%p size %lu vaddr=%p\n",
  1208. ctx->ctx_smpl_hdr,
  1209. ctx->ctx_smpl_size,
  1210. ctx->ctx_smpl_vaddr));
  1211. pfm_buf_fmt_exit(fmt, current, NULL, NULL);
  1212. /*
  1213. * free the buffer
  1214. */
  1215. pfm_rvfree(ctx->ctx_smpl_hdr, ctx->ctx_smpl_size);
  1216. ctx->ctx_smpl_hdr = NULL;
  1217. ctx->ctx_smpl_size = 0UL;
  1218. return 0;
  1219. invalid_free:
  1220. printk(KERN_ERR "perfmon: pfm_free_smpl_buffer [%d] no buffer\n", current->pid);
  1221. return -EINVAL;
  1222. }
  1223. #endif
  1224. static inline void
  1225. pfm_exit_smpl_buffer(pfm_buffer_fmt_t *fmt)
  1226. {
  1227. if (fmt == NULL) return;
  1228. pfm_buf_fmt_exit(fmt, current, NULL, NULL);
  1229. }
  1230. /*
  1231. * pfmfs should _never_ be mounted by userland - too much of security hassle,
  1232. * no real gain from having the whole whorehouse mounted. So we don't need
  1233. * any operations on the root directory. However, we need a non-trivial
  1234. * d_name - pfm: will go nicely and kill the special-casing in procfs.
  1235. */
  1236. static struct vfsmount *pfmfs_mnt;
  1237. static int __init
  1238. init_pfm_fs(void)
  1239. {
  1240. int err = register_filesystem(&pfm_fs_type);
  1241. if (!err) {
  1242. pfmfs_mnt = kern_mount(&pfm_fs_type);
  1243. err = PTR_ERR(pfmfs_mnt);
  1244. if (IS_ERR(pfmfs_mnt))
  1245. unregister_filesystem(&pfm_fs_type);
  1246. else
  1247. err = 0;
  1248. }
  1249. return err;
  1250. }
  1251. static void __exit
  1252. exit_pfm_fs(void)
  1253. {
  1254. unregister_filesystem(&pfm_fs_type);
  1255. mntput(pfmfs_mnt);
  1256. }
  1257. static ssize_t
  1258. pfm_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
  1259. {
  1260. pfm_context_t *ctx;
  1261. pfm_msg_t *msg;
  1262. ssize_t ret;
  1263. unsigned long flags;
  1264. DECLARE_WAITQUEUE(wait, current);
  1265. if (PFM_IS_FILE(filp) == 0) {
  1266. printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
  1267. return -EINVAL;
  1268. }
  1269. ctx = (pfm_context_t *)filp->private_data;
  1270. if (ctx == NULL) {
  1271. printk(KERN_ERR "perfmon: pfm_read: NULL ctx [%d]\n", current->pid);
  1272. return -EINVAL;
  1273. }
  1274. /*
  1275. * check even when there is no message
  1276. */
  1277. if (size < sizeof(pfm_msg_t)) {
  1278. DPRINT(("message is too small ctx=%p (>=%ld)\n", ctx, sizeof(pfm_msg_t)));
  1279. return -EINVAL;
  1280. }
  1281. PROTECT_CTX(ctx, flags);
  1282. /*
  1283. * put ourselves on the wait queue
  1284. */
  1285. add_wait_queue(&ctx->ctx_msgq_wait, &wait);
  1286. for(;;) {
  1287. /*
  1288. * check wait queue
  1289. */
  1290. set_current_state(TASK_INTERRUPTIBLE);
  1291. DPRINT(("head=%d tail=%d\n", ctx->ctx_msgq_head, ctx->ctx_msgq_tail));
  1292. ret = 0;
  1293. if(PFM_CTXQ_EMPTY(ctx) == 0) break;
  1294. UNPROTECT_CTX(ctx, flags);
  1295. /*
  1296. * check non-blocking read
  1297. */
  1298. ret = -EAGAIN;
  1299. if(filp->f_flags & O_NONBLOCK) break;
  1300. /*
  1301. * check pending signals
  1302. */
  1303. if(signal_pending(current)) {
  1304. ret = -EINTR;
  1305. break;
  1306. }
  1307. /*
  1308. * no message, so wait
  1309. */
  1310. schedule();
  1311. PROTECT_CTX(ctx, flags);
  1312. }
  1313. DPRINT(("[%d] back to running ret=%ld\n", current->pid, ret));
  1314. set_current_state(TASK_RUNNING);
  1315. remove_wait_queue(&ctx->ctx_msgq_wait, &wait);
  1316. if (ret < 0) goto abort;
  1317. ret = -EINVAL;
  1318. msg = pfm_get_next_msg(ctx);
  1319. if (msg == NULL) {
  1320. printk(KERN_ERR "perfmon: pfm_read no msg for ctx=%p [%d]\n", ctx, current->pid);
  1321. goto abort_locked;
  1322. }
  1323. DPRINT(("fd=%d type=%d\n", msg->pfm_gen_msg.msg_ctx_fd, msg->pfm_gen_msg.msg_type));
  1324. ret = -EFAULT;
  1325. if(copy_to_user(buf, msg, sizeof(pfm_msg_t)) == 0) ret = sizeof(pfm_msg_t);
  1326. abort_locked:
  1327. UNPROTECT_CTX(ctx, flags);
  1328. abort:
  1329. return ret;
  1330. }
  1331. static ssize_t
  1332. pfm_write(struct file *file, const char __user *ubuf,
  1333. size_t size, loff_t *ppos)
  1334. {
  1335. DPRINT(("pfm_write called\n"));
  1336. return -EINVAL;
  1337. }
  1338. static unsigned int
  1339. pfm_poll(struct file *filp, poll_table * wait)
  1340. {
  1341. pfm_context_t *ctx;
  1342. unsigned long flags;
  1343. unsigned int mask = 0;
  1344. if (PFM_IS_FILE(filp) == 0) {
  1345. printk(KERN_ERR "perfmon: pfm_poll: bad magic [%d]\n", current->pid);
  1346. return 0;
  1347. }
  1348. ctx = (pfm_context_t *)filp->private_data;
  1349. if (ctx == NULL) {
  1350. printk(KERN_ERR "perfmon: pfm_poll: NULL ctx [%d]\n", current->pid);
  1351. return 0;
  1352. }
  1353. DPRINT(("pfm_poll ctx_fd=%d before poll_wait\n", ctx->ctx_fd));
  1354. poll_wait(filp, &ctx->ctx_msgq_wait, wait);
  1355. PROTECT_CTX(ctx, flags);
  1356. if (PFM_CTXQ_EMPTY(ctx) == 0)
  1357. mask = POLLIN | POLLRDNORM;
  1358. UNPROTECT_CTX(ctx, flags);
  1359. DPRINT(("pfm_poll ctx_fd=%d mask=0x%x\n", ctx->ctx_fd, mask));
  1360. return mask;
  1361. }
  1362. static int
  1363. pfm_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  1364. {
  1365. DPRINT(("pfm_ioctl called\n"));
  1366. return -EINVAL;
  1367. }
  1368. /*
  1369. * interrupt cannot be masked when coming here
  1370. */
  1371. static inline int
  1372. pfm_do_fasync(int fd, struct file *filp, pfm_context_t *ctx, int on)
  1373. {
  1374. int ret;
  1375. ret = fasync_helper (fd, filp, on, &ctx->ctx_async_queue);
  1376. DPRINT(("pfm_fasync called by [%d] on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
  1377. current->pid,
  1378. fd,
  1379. on,
  1380. ctx->ctx_async_queue, ret));
  1381. return ret;
  1382. }
  1383. static int
  1384. pfm_fasync(int fd, struct file *filp, int on)
  1385. {
  1386. pfm_context_t *ctx;
  1387. int ret;
  1388. if (PFM_IS_FILE(filp) == 0) {
  1389. printk(KERN_ERR "perfmon: pfm_fasync bad magic [%d]\n", current->pid);
  1390. return -EBADF;
  1391. }
  1392. ctx = (pfm_context_t *)filp->private_data;
  1393. if (ctx == NULL) {
  1394. printk(KERN_ERR "perfmon: pfm_fasync NULL ctx [%d]\n", current->pid);
  1395. return -EBADF;
  1396. }
  1397. /*
  1398. * we cannot mask interrupts during this call because this may
  1399. * may go to sleep if memory is not readily avalaible.
  1400. *
  1401. * We are protected from the conetxt disappearing by the get_fd()/put_fd()
  1402. * done in caller. Serialization of this function is ensured by caller.
  1403. */
  1404. ret = pfm_do_fasync(fd, filp, ctx, on);
  1405. DPRINT(("pfm_fasync called on ctx_fd=%d on=%d async_queue=%p ret=%d\n",
  1406. fd,
  1407. on,
  1408. ctx->ctx_async_queue, ret));
  1409. return ret;
  1410. }
  1411. #ifdef CONFIG_SMP
  1412. /*
  1413. * this function is exclusively called from pfm_close().
  1414. * The context is not protected at that time, nor are interrupts
  1415. * on the remote CPU. That's necessary to avoid deadlocks.
  1416. */
  1417. static void
  1418. pfm_syswide_force_stop(void *info)
  1419. {
  1420. pfm_context_t *ctx = (pfm_context_t *)info;
  1421. struct pt_regs *regs = ia64_task_regs(current);
  1422. struct task_struct *owner;
  1423. unsigned long flags;
  1424. int ret;
  1425. if (ctx->ctx_cpu != smp_processor_id()) {
  1426. printk(KERN_ERR "perfmon: pfm_syswide_force_stop for CPU%d but on CPU%d\n",
  1427. ctx->ctx_cpu,
  1428. smp_processor_id());
  1429. return;
  1430. }
  1431. owner = GET_PMU_OWNER();
  1432. if (owner != ctx->ctx_task) {
  1433. printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected owner [%d] instead of [%d]\n",
  1434. smp_processor_id(),
  1435. owner->pid, ctx->ctx_task->pid);
  1436. return;
  1437. }
  1438. if (GET_PMU_CTX() != ctx) {
  1439. printk(KERN_ERR "perfmon: pfm_syswide_force_stop CPU%d unexpected ctx %p instead of %p\n",
  1440. smp_processor_id(),
  1441. GET_PMU_CTX(), ctx);
  1442. return;
  1443. }
  1444. DPRINT(("on CPU%d forcing system wide stop for [%d]\n", smp_processor_id(), ctx->ctx_task->pid));
  1445. /*
  1446. * the context is already protected in pfm_close(), we simply
  1447. * need to mask interrupts to avoid a PMU interrupt race on
  1448. * this CPU
  1449. */
  1450. local_irq_save(flags);
  1451. ret = pfm_context_unload(ctx, NULL, 0, regs);
  1452. if (ret) {
  1453. DPRINT(("context_unload returned %d\n", ret));
  1454. }
  1455. /*
  1456. * unmask interrupts, PMU interrupts are now spurious here
  1457. */
  1458. local_irq_restore(flags);
  1459. }
  1460. static void
  1461. pfm_syswide_cleanup_other_cpu(pfm_context_t *ctx)
  1462. {
  1463. int ret;
  1464. DPRINT(("calling CPU%d for cleanup\n", ctx->ctx_cpu));
  1465. ret = smp_call_function_single(ctx->ctx_cpu, pfm_syswide_force_stop, ctx, 0, 1);
  1466. DPRINT(("called CPU%d for cleanup ret=%d\n", ctx->ctx_cpu, ret));
  1467. }
  1468. #endif /* CONFIG_SMP */
  1469. /*
  1470. * called for each close(). Partially free resources.
  1471. * When caller is self-monitoring, the context is unloaded.
  1472. */
  1473. static int
  1474. pfm_flush(struct file *filp)
  1475. {
  1476. pfm_context_t *ctx;
  1477. struct task_struct *task;
  1478. struct pt_regs *regs;
  1479. unsigned long flags;
  1480. unsigned long smpl_buf_size = 0UL;
  1481. void *smpl_buf_vaddr = NULL;
  1482. int state, is_system;
  1483. if (PFM_IS_FILE(filp) == 0) {
  1484. DPRINT(("bad magic for\n"));
  1485. return -EBADF;
  1486. }
  1487. ctx = (pfm_context_t *)filp->private_data;
  1488. if (ctx == NULL) {
  1489. printk(KERN_ERR "perfmon: pfm_flush: NULL ctx [%d]\n", current->pid);
  1490. return -EBADF;
  1491. }
  1492. /*
  1493. * remove our file from the async queue, if we use this mode.
  1494. * This can be done without the context being protected. We come
  1495. * here when the context has become unreacheable by other tasks.
  1496. *
  1497. * We may still have active monitoring at this point and we may
  1498. * end up in pfm_overflow_handler(). However, fasync_helper()
  1499. * operates with interrupts disabled and it cleans up the
  1500. * queue. If the PMU handler is called prior to entering
  1501. * fasync_helper() then it will send a signal. If it is
  1502. * invoked after, it will find an empty queue and no
  1503. * signal will be sent. In both case, we are safe
  1504. */
  1505. if (filp->f_flags & FASYNC) {
  1506. DPRINT(("cleaning up async_queue=%p\n", ctx->ctx_async_queue));
  1507. pfm_do_fasync (-1, filp, ctx, 0);
  1508. }
  1509. PROTECT_CTX(ctx, flags);
  1510. state = ctx->ctx_state;
  1511. is_system = ctx->ctx_fl_system;
  1512. task = PFM_CTX_TASK(ctx);
  1513. regs = ia64_task_regs(task);
  1514. DPRINT(("ctx_state=%d is_current=%d\n",
  1515. state,
  1516. task == current ? 1 : 0));
  1517. /*
  1518. * if state == UNLOADED, then task is NULL
  1519. */
  1520. /*
  1521. * we must stop and unload because we are losing access to the context.
  1522. */
  1523. if (task == current) {
  1524. #ifdef CONFIG_SMP
  1525. /*
  1526. * the task IS the owner but it migrated to another CPU: that's bad
  1527. * but we must handle this cleanly. Unfortunately, the kernel does
  1528. * not provide a mechanism to block migration (while the context is loaded).
  1529. *
  1530. * We need to release the resource on the ORIGINAL cpu.
  1531. */
  1532. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  1533. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  1534. /*
  1535. * keep context protected but unmask interrupt for IPI
  1536. */
  1537. local_irq_restore(flags);
  1538. pfm_syswide_cleanup_other_cpu(ctx);
  1539. /*
  1540. * restore interrupt masking
  1541. */
  1542. local_irq_save(flags);
  1543. /*
  1544. * context is unloaded at this point
  1545. */
  1546. } else
  1547. #endif /* CONFIG_SMP */
  1548. {
  1549. DPRINT(("forcing unload\n"));
  1550. /*
  1551. * stop and unload, returning with state UNLOADED
  1552. * and session unreserved.
  1553. */
  1554. pfm_context_unload(ctx, NULL, 0, regs);
  1555. DPRINT(("ctx_state=%d\n", ctx->ctx_state));
  1556. }
  1557. }
  1558. /*
  1559. * remove virtual mapping, if any, for the calling task.
  1560. * cannot reset ctx field until last user is calling close().
  1561. *
  1562. * ctx_smpl_vaddr must never be cleared because it is needed
  1563. * by every task with access to the context
  1564. *
  1565. * When called from do_exit(), the mm context is gone already, therefore
  1566. * mm is NULL, i.e., the VMA is already gone and we do not have to
  1567. * do anything here
  1568. */
  1569. if (ctx->ctx_smpl_vaddr && current->mm) {
  1570. smpl_buf_vaddr = ctx->ctx_smpl_vaddr;
  1571. smpl_buf_size = ctx->ctx_smpl_size;
  1572. }
  1573. UNPROTECT_CTX(ctx, flags);
  1574. /*
  1575. * if there was a mapping, then we systematically remove it
  1576. * at this point. Cannot be done inside critical section
  1577. * because some VM function reenables interrupts.
  1578. *
  1579. */
  1580. if (smpl_buf_vaddr) pfm_remove_smpl_mapping(current, smpl_buf_vaddr, smpl_buf_size);
  1581. return 0;
  1582. }
  1583. /*
  1584. * called either on explicit close() or from exit_files().
  1585. * Only the LAST user of the file gets to this point, i.e., it is
  1586. * called only ONCE.
  1587. *
  1588. * IMPORTANT: we get called ONLY when the refcnt on the file gets to zero
  1589. * (fput()),i.e, last task to access the file. Nobody else can access the
  1590. * file at this point.
  1591. *
  1592. * When called from exit_files(), the VMA has been freed because exit_mm()
  1593. * is executed before exit_files().
  1594. *
  1595. * When called from exit_files(), the current task is not yet ZOMBIE but we
  1596. * flush the PMU state to the context.
  1597. */
  1598. static int
  1599. pfm_close(struct inode *inode, struct file *filp)
  1600. {
  1601. pfm_context_t *ctx;
  1602. struct task_struct *task;
  1603. struct pt_regs *regs;
  1604. DECLARE_WAITQUEUE(wait, current);
  1605. unsigned long flags;
  1606. unsigned long smpl_buf_size = 0UL;
  1607. void *smpl_buf_addr = NULL;
  1608. int free_possible = 1;
  1609. int state, is_system;
  1610. DPRINT(("pfm_close called private=%p\n", filp->private_data));
  1611. if (PFM_IS_FILE(filp) == 0) {
  1612. DPRINT(("bad magic\n"));
  1613. return -EBADF;
  1614. }
  1615. ctx = (pfm_context_t *)filp->private_data;
  1616. if (ctx == NULL) {
  1617. printk(KERN_ERR "perfmon: pfm_close: NULL ctx [%d]\n", current->pid);
  1618. return -EBADF;
  1619. }
  1620. PROTECT_CTX(ctx, flags);
  1621. state = ctx->ctx_state;
  1622. is_system = ctx->ctx_fl_system;
  1623. task = PFM_CTX_TASK(ctx);
  1624. regs = ia64_task_regs(task);
  1625. DPRINT(("ctx_state=%d is_current=%d\n",
  1626. state,
  1627. task == current ? 1 : 0));
  1628. /*
  1629. * if task == current, then pfm_flush() unloaded the context
  1630. */
  1631. if (state == PFM_CTX_UNLOADED) goto doit;
  1632. /*
  1633. * context is loaded/masked and task != current, we need to
  1634. * either force an unload or go zombie
  1635. */
  1636. /*
  1637. * The task is currently blocked or will block after an overflow.
  1638. * we must force it to wakeup to get out of the
  1639. * MASKED state and transition to the unloaded state by itself.
  1640. *
  1641. * This situation is only possible for per-task mode
  1642. */
  1643. if (state == PFM_CTX_MASKED && CTX_OVFL_NOBLOCK(ctx) == 0) {
  1644. /*
  1645. * set a "partial" zombie state to be checked
  1646. * upon return from down() in pfm_handle_work().
  1647. *
  1648. * We cannot use the ZOMBIE state, because it is checked
  1649. * by pfm_load_regs() which is called upon wakeup from down().
  1650. * In such case, it would free the context and then we would
  1651. * return to pfm_handle_work() which would access the
  1652. * stale context. Instead, we set a flag invisible to pfm_load_regs()
  1653. * but visible to pfm_handle_work().
  1654. *
  1655. * For some window of time, we have a zombie context with
  1656. * ctx_state = MASKED and not ZOMBIE
  1657. */
  1658. ctx->ctx_fl_going_zombie = 1;
  1659. /*
  1660. * force task to wake up from MASKED state
  1661. */
  1662. up(&ctx->ctx_restart_sem);
  1663. DPRINT(("waking up ctx_state=%d\n", state));
  1664. /*
  1665. * put ourself to sleep waiting for the other
  1666. * task to report completion
  1667. *
  1668. * the context is protected by mutex, therefore there
  1669. * is no risk of being notified of completion before
  1670. * begin actually on the waitq.
  1671. */
  1672. set_current_state(TASK_INTERRUPTIBLE);
  1673. add_wait_queue(&ctx->ctx_zombieq, &wait);
  1674. UNPROTECT_CTX(ctx, flags);
  1675. /*
  1676. * XXX: check for signals :
  1677. * - ok for explicit close
  1678. * - not ok when coming from exit_files()
  1679. */
  1680. schedule();
  1681. PROTECT_CTX(ctx, flags);
  1682. remove_wait_queue(&ctx->ctx_zombieq, &wait);
  1683. set_current_state(TASK_RUNNING);
  1684. /*
  1685. * context is unloaded at this point
  1686. */
  1687. DPRINT(("after zombie wakeup ctx_state=%d for\n", state));
  1688. }
  1689. else if (task != current) {
  1690. #ifdef CONFIG_SMP
  1691. /*
  1692. * switch context to zombie state
  1693. */
  1694. ctx->ctx_state = PFM_CTX_ZOMBIE;
  1695. DPRINT(("zombie ctx for [%d]\n", task->pid));
  1696. /*
  1697. * cannot free the context on the spot. deferred until
  1698. * the task notices the ZOMBIE state
  1699. */
  1700. free_possible = 0;
  1701. #else
  1702. pfm_context_unload(ctx, NULL, 0, regs);
  1703. #endif
  1704. }
  1705. doit:
  1706. /* reload state, may have changed during opening of critical section */
  1707. state = ctx->ctx_state;
  1708. /*
  1709. * the context is still attached to a task (possibly current)
  1710. * we cannot destroy it right now
  1711. */
  1712. /*
  1713. * we must free the sampling buffer right here because
  1714. * we cannot rely on it being cleaned up later by the
  1715. * monitored task. It is not possible to free vmalloc'ed
  1716. * memory in pfm_load_regs(). Instead, we remove the buffer
  1717. * now. should there be subsequent PMU overflow originally
  1718. * meant for sampling, the will be converted to spurious
  1719. * and that's fine because the monitoring tools is gone anyway.
  1720. */
  1721. if (ctx->ctx_smpl_hdr) {
  1722. smpl_buf_addr = ctx->ctx_smpl_hdr;
  1723. smpl_buf_size = ctx->ctx_smpl_size;
  1724. /* no more sampling */
  1725. ctx->ctx_smpl_hdr = NULL;
  1726. ctx->ctx_fl_is_sampling = 0;
  1727. }
  1728. DPRINT(("ctx_state=%d free_possible=%d addr=%p size=%lu\n",
  1729. state,
  1730. free_possible,
  1731. smpl_buf_addr,
  1732. smpl_buf_size));
  1733. if (smpl_buf_addr) pfm_exit_smpl_buffer(ctx->ctx_buf_fmt);
  1734. /*
  1735. * UNLOADED that the session has already been unreserved.
  1736. */
  1737. if (state == PFM_CTX_ZOMBIE) {
  1738. pfm_unreserve_session(ctx, ctx->ctx_fl_system , ctx->ctx_cpu);
  1739. }
  1740. /*
  1741. * disconnect file descriptor from context must be done
  1742. * before we unlock.
  1743. */
  1744. filp->private_data = NULL;
  1745. /*
  1746. * if we free on the spot, the context is now completely unreacheable
  1747. * from the callers side. The monitored task side is also cut, so we
  1748. * can freely cut.
  1749. *
  1750. * If we have a deferred free, only the caller side is disconnected.
  1751. */
  1752. UNPROTECT_CTX(ctx, flags);
  1753. /*
  1754. * All memory free operations (especially for vmalloc'ed memory)
  1755. * MUST be done with interrupts ENABLED.
  1756. */
  1757. if (smpl_buf_addr) pfm_rvfree(smpl_buf_addr, smpl_buf_size);
  1758. /*
  1759. * return the memory used by the context
  1760. */
  1761. if (free_possible) pfm_context_free(ctx);
  1762. return 0;
  1763. }
  1764. static int
  1765. pfm_no_open(struct inode *irrelevant, struct file *dontcare)
  1766. {
  1767. DPRINT(("pfm_no_open called\n"));
  1768. return -ENXIO;
  1769. }
  1770. static struct file_operations pfm_file_ops = {
  1771. .llseek = no_llseek,
  1772. .read = pfm_read,
  1773. .write = pfm_write,
  1774. .poll = pfm_poll,
  1775. .ioctl = pfm_ioctl,
  1776. .open = pfm_no_open, /* special open code to disallow open via /proc */
  1777. .fasync = pfm_fasync,
  1778. .release = pfm_close,
  1779. .flush = pfm_flush
  1780. };
  1781. static int
  1782. pfmfs_delete_dentry(struct dentry *dentry)
  1783. {
  1784. return 1;
  1785. }
  1786. static struct dentry_operations pfmfs_dentry_operations = {
  1787. .d_delete = pfmfs_delete_dentry,
  1788. };
  1789. static int
  1790. pfm_alloc_fd(struct file **cfile)
  1791. {
  1792. int fd, ret = 0;
  1793. struct file *file = NULL;
  1794. struct inode * inode;
  1795. char name[32];
  1796. struct qstr this;
  1797. fd = get_unused_fd();
  1798. if (fd < 0) return -ENFILE;
  1799. ret = -ENFILE;
  1800. file = get_empty_filp();
  1801. if (!file) goto out;
  1802. /*
  1803. * allocate a new inode
  1804. */
  1805. inode = new_inode(pfmfs_mnt->mnt_sb);
  1806. if (!inode) goto out;
  1807. DPRINT(("new inode ino=%ld @%p\n", inode->i_ino, inode));
  1808. inode->i_mode = S_IFCHR|S_IRUGO;
  1809. inode->i_uid = current->fsuid;
  1810. inode->i_gid = current->fsgid;
  1811. sprintf(name, "[%lu]", inode->i_ino);
  1812. this.name = name;
  1813. this.len = strlen(name);
  1814. this.hash = inode->i_ino;
  1815. ret = -ENOMEM;
  1816. /*
  1817. * allocate a new dcache entry
  1818. */
  1819. file->f_dentry = d_alloc(pfmfs_mnt->mnt_sb->s_root, &this);
  1820. if (!file->f_dentry) goto out;
  1821. file->f_dentry->d_op = &pfmfs_dentry_operations;
  1822. d_add(file->f_dentry, inode);
  1823. file->f_vfsmnt = mntget(pfmfs_mnt);
  1824. file->f_mapping = inode->i_mapping;
  1825. file->f_op = &pfm_file_ops;
  1826. file->f_mode = FMODE_READ;
  1827. file->f_flags = O_RDONLY;
  1828. file->f_pos = 0;
  1829. /*
  1830. * may have to delay until context is attached?
  1831. */
  1832. fd_install(fd, file);
  1833. /*
  1834. * the file structure we will use
  1835. */
  1836. *cfile = file;
  1837. return fd;
  1838. out:
  1839. if (file) put_filp(file);
  1840. put_unused_fd(fd);
  1841. return ret;
  1842. }
  1843. static void
  1844. pfm_free_fd(int fd, struct file *file)
  1845. {
  1846. struct files_struct *files = current->files;
  1847. struct fdtable *fdt;
  1848. /*
  1849. * there ie no fd_uninstall(), so we do it here
  1850. */
  1851. spin_lock(&files->file_lock);
  1852. fdt = files_fdtable(files);
  1853. rcu_assign_pointer(fdt->fd[fd], NULL);
  1854. spin_unlock(&files->file_lock);
  1855. if (file)
  1856. put_filp(file);
  1857. put_unused_fd(fd);
  1858. }
  1859. static int
  1860. pfm_remap_buffer(struct vm_area_struct *vma, unsigned long buf, unsigned long addr, unsigned long size)
  1861. {
  1862. DPRINT(("CPU%d buf=0x%lx addr=0x%lx size=%ld\n", smp_processor_id(), buf, addr, size));
  1863. while (size > 0) {
  1864. unsigned long pfn = ia64_tpa(buf) >> PAGE_SHIFT;
  1865. if (remap_pfn_range(vma, addr, pfn, PAGE_SIZE, PAGE_READONLY))
  1866. return -ENOMEM;
  1867. addr += PAGE_SIZE;
  1868. buf += PAGE_SIZE;
  1869. size -= PAGE_SIZE;
  1870. }
  1871. return 0;
  1872. }
  1873. /*
  1874. * allocate a sampling buffer and remaps it into the user address space of the task
  1875. */
  1876. static int
  1877. pfm_smpl_buffer_alloc(struct task_struct *task, pfm_context_t *ctx, unsigned long rsize, void **user_vaddr)
  1878. {
  1879. struct mm_struct *mm = task->mm;
  1880. struct vm_area_struct *vma = NULL;
  1881. unsigned long size;
  1882. void *smpl_buf;
  1883. /*
  1884. * the fixed header + requested size and align to page boundary
  1885. */
  1886. size = PAGE_ALIGN(rsize);
  1887. DPRINT(("sampling buffer rsize=%lu size=%lu bytes\n", rsize, size));
  1888. /*
  1889. * check requested size to avoid Denial-of-service attacks
  1890. * XXX: may have to refine this test
  1891. * Check against address space limit.
  1892. *
  1893. * if ((mm->total_vm << PAGE_SHIFT) + len> task->rlim[RLIMIT_AS].rlim_cur)
  1894. * return -ENOMEM;
  1895. */
  1896. if (size > task->signal->rlim[RLIMIT_MEMLOCK].rlim_cur)
  1897. return -ENOMEM;
  1898. /*
  1899. * We do the easy to undo allocations first.
  1900. *
  1901. * pfm_rvmalloc(), clears the buffer, so there is no leak
  1902. */
  1903. smpl_buf = pfm_rvmalloc(size);
  1904. if (smpl_buf == NULL) {
  1905. DPRINT(("Can't allocate sampling buffer\n"));
  1906. return -ENOMEM;
  1907. }
  1908. DPRINT(("smpl_buf @%p\n", smpl_buf));
  1909. /* allocate vma */
  1910. vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
  1911. if (!vma) {
  1912. DPRINT(("Cannot allocate vma\n"));
  1913. goto error_kmem;
  1914. }
  1915. memset(vma, 0, sizeof(*vma));
  1916. /*
  1917. * partially initialize the vma for the sampling buffer
  1918. */
  1919. vma->vm_mm = mm;
  1920. vma->vm_flags = VM_READ| VM_MAYREAD |VM_RESERVED;
  1921. vma->vm_page_prot = PAGE_READONLY; /* XXX may need to change */
  1922. /*
  1923. * Now we have everything we need and we can initialize
  1924. * and connect all the data structures
  1925. */
  1926. ctx->ctx_smpl_hdr = smpl_buf;
  1927. ctx->ctx_smpl_size = size; /* aligned size */
  1928. /*
  1929. * Let's do the difficult operations next.
  1930. *
  1931. * now we atomically find some area in the address space and
  1932. * remap the buffer in it.
  1933. */
  1934. down_write(&task->mm->mmap_sem);
  1935. /* find some free area in address space, must have mmap sem held */
  1936. vma->vm_start = pfm_get_unmapped_area(NULL, 0, size, 0, MAP_PRIVATE|MAP_ANONYMOUS, 0);
  1937. if (vma->vm_start == 0UL) {
  1938. DPRINT(("Cannot find unmapped area for size %ld\n", size));
  1939. up_write(&task->mm->mmap_sem);
  1940. goto error;
  1941. }
  1942. vma->vm_end = vma->vm_start + size;
  1943. vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
  1944. DPRINT(("aligned size=%ld, hdr=%p mapped @0x%lx\n", size, ctx->ctx_smpl_hdr, vma->vm_start));
  1945. /* can only be applied to current task, need to have the mm semaphore held when called */
  1946. if (pfm_remap_buffer(vma, (unsigned long)smpl_buf, vma->vm_start, size)) {
  1947. DPRINT(("Can't remap buffer\n"));
  1948. up_write(&task->mm->mmap_sem);
  1949. goto error;
  1950. }
  1951. /*
  1952. * now insert the vma in the vm list for the process, must be
  1953. * done with mmap lock held
  1954. */
  1955. insert_vm_struct(mm, vma);
  1956. mm->total_vm += size >> PAGE_SHIFT;
  1957. vm_stat_account(vma);
  1958. up_write(&task->mm->mmap_sem);
  1959. /*
  1960. * keep track of user level virtual address
  1961. */
  1962. ctx->ctx_smpl_vaddr = (void *)vma->vm_start;
  1963. *(unsigned long *)user_vaddr = vma->vm_start;
  1964. return 0;
  1965. error:
  1966. kmem_cache_free(vm_area_cachep, vma);
  1967. error_kmem:
  1968. pfm_rvfree(smpl_buf, size);
  1969. return -ENOMEM;
  1970. }
  1971. /*
  1972. * XXX: do something better here
  1973. */
  1974. static int
  1975. pfm_bad_permissions(struct task_struct *task)
  1976. {
  1977. /* inspired by ptrace_attach() */
  1978. DPRINT(("cur: uid=%d gid=%d task: euid=%d suid=%d uid=%d egid=%d sgid=%d\n",
  1979. current->uid,
  1980. current->gid,
  1981. task->euid,
  1982. task->suid,
  1983. task->uid,
  1984. task->egid,
  1985. task->sgid));
  1986. return ((current->uid != task->euid)
  1987. || (current->uid != task->suid)
  1988. || (current->uid != task->uid)
  1989. || (current->gid != task->egid)
  1990. || (current->gid != task->sgid)
  1991. || (current->gid != task->gid)) && !capable(CAP_SYS_PTRACE);
  1992. }
  1993. static int
  1994. pfarg_is_sane(struct task_struct *task, pfarg_context_t *pfx)
  1995. {
  1996. int ctx_flags;
  1997. /* valid signal */
  1998. ctx_flags = pfx->ctx_flags;
  1999. if (ctx_flags & PFM_FL_SYSTEM_WIDE) {
  2000. /*
  2001. * cannot block in this mode
  2002. */
  2003. if (ctx_flags & PFM_FL_NOTIFY_BLOCK) {
  2004. DPRINT(("cannot use blocking mode when in system wide monitoring\n"));
  2005. return -EINVAL;
  2006. }
  2007. } else {
  2008. }
  2009. /* probably more to add here */
  2010. return 0;
  2011. }
  2012. static int
  2013. pfm_setup_buffer_fmt(struct task_struct *task, pfm_context_t *ctx, unsigned int ctx_flags,
  2014. unsigned int cpu, pfarg_context_t *arg)
  2015. {
  2016. pfm_buffer_fmt_t *fmt = NULL;
  2017. unsigned long size = 0UL;
  2018. void *uaddr = NULL;
  2019. void *fmt_arg = NULL;
  2020. int ret = 0;
  2021. #define PFM_CTXARG_BUF_ARG(a) (pfm_buffer_fmt_t *)(a+1)
  2022. /* invoke and lock buffer format, if found */
  2023. fmt = pfm_find_buffer_fmt(arg->ctx_smpl_buf_id);
  2024. if (fmt == NULL) {
  2025. DPRINT(("[%d] cannot find buffer format\n", task->pid));
  2026. return -EINVAL;
  2027. }
  2028. /*
  2029. * buffer argument MUST be contiguous to pfarg_context_t
  2030. */
  2031. if (fmt->fmt_arg_size) fmt_arg = PFM_CTXARG_BUF_ARG(arg);
  2032. ret = pfm_buf_fmt_validate(fmt, task, ctx_flags, cpu, fmt_arg);
  2033. DPRINT(("[%d] after validate(0x%x,%d,%p)=%d\n", task->pid, ctx_flags, cpu, fmt_arg, ret));
  2034. if (ret) goto error;
  2035. /* link buffer format and context */
  2036. ctx->ctx_buf_fmt = fmt;
  2037. /*
  2038. * check if buffer format wants to use perfmon buffer allocation/mapping service
  2039. */
  2040. ret = pfm_buf_fmt_getsize(fmt, task, ctx_flags, cpu, fmt_arg, &size);
  2041. if (ret) goto error;
  2042. if (size) {
  2043. /*
  2044. * buffer is always remapped into the caller's address space
  2045. */
  2046. ret = pfm_smpl_buffer_alloc(current, ctx, size, &uaddr);
  2047. if (ret) goto error;
  2048. /* keep track of user address of buffer */
  2049. arg->ctx_smpl_vaddr = uaddr;
  2050. }
  2051. ret = pfm_buf_fmt_init(fmt, task, ctx->ctx_smpl_hdr, ctx_flags, cpu, fmt_arg);
  2052. error:
  2053. return ret;
  2054. }
  2055. static void
  2056. pfm_reset_pmu_state(pfm_context_t *ctx)
  2057. {
  2058. int i;
  2059. /*
  2060. * install reset values for PMC.
  2061. */
  2062. for (i=1; PMC_IS_LAST(i) == 0; i++) {
  2063. if (PMC_IS_IMPL(i) == 0) continue;
  2064. ctx->ctx_pmcs[i] = PMC_DFL_VAL(i);
  2065. DPRINT(("pmc[%d]=0x%lx\n", i, ctx->ctx_pmcs[i]));
  2066. }
  2067. /*
  2068. * PMD registers are set to 0UL when the context in memset()
  2069. */
  2070. /*
  2071. * On context switched restore, we must restore ALL pmc and ALL pmd even
  2072. * when they are not actively used by the task. In UP, the incoming process
  2073. * may otherwise pick up left over PMC, PMD state from the previous process.
  2074. * As opposed to PMD, stale PMC can cause harm to the incoming
  2075. * process because they may change what is being measured.
  2076. * Therefore, we must systematically reinstall the entire
  2077. * PMC state. In SMP, the same thing is possible on the
  2078. * same CPU but also on between 2 CPUs.
  2079. *
  2080. * The problem with PMD is information leaking especially
  2081. * to user level when psr.sp=0
  2082. *
  2083. * There is unfortunately no easy way to avoid this problem
  2084. * on either UP or SMP. This definitively slows down the
  2085. * pfm_load_regs() function.
  2086. */
  2087. /*
  2088. * bitmask of all PMCs accessible to this context
  2089. *
  2090. * PMC0 is treated differently.
  2091. */
  2092. ctx->ctx_all_pmcs[0] = pmu_conf->impl_pmcs[0] & ~0x1;
  2093. /*
  2094. * bitmask of all PMDs that are accesible to this context
  2095. */
  2096. ctx->ctx_all_pmds[0] = pmu_conf->impl_pmds[0];
  2097. DPRINT(("<%d> all_pmcs=0x%lx all_pmds=0x%lx\n", ctx->ctx_fd, ctx->ctx_all_pmcs[0],ctx->ctx_all_pmds[0]));
  2098. /*
  2099. * useful in case of re-enable after disable
  2100. */
  2101. ctx->ctx_used_ibrs[0] = 0UL;
  2102. ctx->ctx_used_dbrs[0] = 0UL;
  2103. }
  2104. static int
  2105. pfm_ctx_getsize(void *arg, size_t *sz)
  2106. {
  2107. pfarg_context_t *req = (pfarg_context_t *)arg;
  2108. pfm_buffer_fmt_t *fmt;
  2109. *sz = 0;
  2110. if (!pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) return 0;
  2111. fmt = pfm_find_buffer_fmt(req->ctx_smpl_buf_id);
  2112. if (fmt == NULL) {
  2113. DPRINT(("cannot find buffer format\n"));
  2114. return -EINVAL;
  2115. }
  2116. /* get just enough to copy in user parameters */
  2117. *sz = fmt->fmt_arg_size;
  2118. DPRINT(("arg_size=%lu\n", *sz));
  2119. return 0;
  2120. }
  2121. /*
  2122. * cannot attach if :
  2123. * - kernel task
  2124. * - task not owned by caller
  2125. * - task incompatible with context mode
  2126. */
  2127. static int
  2128. pfm_task_incompatible(pfm_context_t *ctx, struct task_struct *task)
  2129. {
  2130. /*
  2131. * no kernel task or task not owner by caller
  2132. */
  2133. if (task->mm == NULL) {
  2134. DPRINT(("task [%d] has not memory context (kernel thread)\n", task->pid));
  2135. return -EPERM;
  2136. }
  2137. if (pfm_bad_permissions(task)) {
  2138. DPRINT(("no permission to attach to [%d]\n", task->pid));
  2139. return -EPERM;
  2140. }
  2141. /*
  2142. * cannot block in self-monitoring mode
  2143. */
  2144. if (CTX_OVFL_NOBLOCK(ctx) == 0 && task == current) {
  2145. DPRINT(("cannot load a blocking context on self for [%d]\n", task->pid));
  2146. return -EINVAL;
  2147. }
  2148. if (task->exit_state == EXIT_ZOMBIE) {
  2149. DPRINT(("cannot attach to zombie task [%d]\n", task->pid));
  2150. return -EBUSY;
  2151. }
  2152. /*
  2153. * always ok for self
  2154. */
  2155. if (task == current) return 0;
  2156. if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
  2157. DPRINT(("cannot attach to non-stopped task [%d] state=%ld\n", task->pid, task->state));
  2158. return -EBUSY;
  2159. }
  2160. /*
  2161. * make sure the task is off any CPU
  2162. */
  2163. wait_task_inactive(task);
  2164. /* more to come... */
  2165. return 0;
  2166. }
  2167. static int
  2168. pfm_get_task(pfm_context_t *ctx, pid_t pid, struct task_struct **task)
  2169. {
  2170. struct task_struct *p = current;
  2171. int ret;
  2172. /* XXX: need to add more checks here */
  2173. if (pid < 2) return -EPERM;
  2174. if (pid != current->pid) {
  2175. read_lock(&tasklist_lock);
  2176. p = find_task_by_pid(pid);
  2177. /* make sure task cannot go away while we operate on it */
  2178. if (p) get_task_struct(p);
  2179. read_unlock(&tasklist_lock);
  2180. if (p == NULL) return -ESRCH;
  2181. }
  2182. ret = pfm_task_incompatible(ctx, p);
  2183. if (ret == 0) {
  2184. *task = p;
  2185. } else if (p != current) {
  2186. pfm_put_task(p);
  2187. }
  2188. return ret;
  2189. }
  2190. static int
  2191. pfm_context_create(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2192. {
  2193. pfarg_context_t *req = (pfarg_context_t *)arg;
  2194. struct file *filp;
  2195. int ctx_flags;
  2196. int ret;
  2197. /* let's check the arguments first */
  2198. ret = pfarg_is_sane(current, req);
  2199. if (ret < 0) return ret;
  2200. ctx_flags = req->ctx_flags;
  2201. ret = -ENOMEM;
  2202. ctx = pfm_context_alloc();
  2203. if (!ctx) goto error;
  2204. ret = pfm_alloc_fd(&filp);
  2205. if (ret < 0) goto error_file;
  2206. req->ctx_fd = ctx->ctx_fd = ret;
  2207. /*
  2208. * attach context to file
  2209. */
  2210. filp->private_data = ctx;
  2211. /*
  2212. * does the user want to sample?
  2213. */
  2214. if (pfm_uuid_cmp(req->ctx_smpl_buf_id, pfm_null_uuid)) {
  2215. ret = pfm_setup_buffer_fmt(current, ctx, ctx_flags, 0, req);
  2216. if (ret) goto buffer_error;
  2217. }
  2218. /*
  2219. * init context protection lock
  2220. */
  2221. spin_lock_init(&ctx->ctx_lock);
  2222. /*
  2223. * context is unloaded
  2224. */
  2225. ctx->ctx_state = PFM_CTX_UNLOADED;
  2226. /*
  2227. * initialization of context's flags
  2228. */
  2229. ctx->ctx_fl_block = (ctx_flags & PFM_FL_NOTIFY_BLOCK) ? 1 : 0;
  2230. ctx->ctx_fl_system = (ctx_flags & PFM_FL_SYSTEM_WIDE) ? 1: 0;
  2231. ctx->ctx_fl_is_sampling = ctx->ctx_buf_fmt ? 1 : 0; /* assume record() is defined */
  2232. ctx->ctx_fl_no_msg = (ctx_flags & PFM_FL_OVFL_NO_MSG) ? 1: 0;
  2233. /*
  2234. * will move to set properties
  2235. * ctx->ctx_fl_excl_idle = (ctx_flags & PFM_FL_EXCL_IDLE) ? 1: 0;
  2236. */
  2237. /*
  2238. * init restart semaphore to locked
  2239. */
  2240. sema_init(&ctx->ctx_restart_sem, 0);
  2241. /*
  2242. * activation is used in SMP only
  2243. */
  2244. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  2245. SET_LAST_CPU(ctx, -1);
  2246. /*
  2247. * initialize notification message queue
  2248. */
  2249. ctx->ctx_msgq_head = ctx->ctx_msgq_tail = 0;
  2250. init_waitqueue_head(&ctx->ctx_msgq_wait);
  2251. init_waitqueue_head(&ctx->ctx_zombieq);
  2252. DPRINT(("ctx=%p flags=0x%x system=%d notify_block=%d excl_idle=%d no_msg=%d ctx_fd=%d \n",
  2253. ctx,
  2254. ctx_flags,
  2255. ctx->ctx_fl_system,
  2256. ctx->ctx_fl_block,
  2257. ctx->ctx_fl_excl_idle,
  2258. ctx->ctx_fl_no_msg,
  2259. ctx->ctx_fd));
  2260. /*
  2261. * initialize soft PMU state
  2262. */
  2263. pfm_reset_pmu_state(ctx);
  2264. return 0;
  2265. buffer_error:
  2266. pfm_free_fd(ctx->ctx_fd, filp);
  2267. if (ctx->ctx_buf_fmt) {
  2268. pfm_buf_fmt_exit(ctx->ctx_buf_fmt, current, NULL, regs);
  2269. }
  2270. error_file:
  2271. pfm_context_free(ctx);
  2272. error:
  2273. return ret;
  2274. }
  2275. static inline unsigned long
  2276. pfm_new_counter_value (pfm_counter_t *reg, int is_long_reset)
  2277. {
  2278. unsigned long val = is_long_reset ? reg->long_reset : reg->short_reset;
  2279. unsigned long new_seed, old_seed = reg->seed, mask = reg->mask;
  2280. extern unsigned long carta_random32 (unsigned long seed);
  2281. if (reg->flags & PFM_REGFL_RANDOM) {
  2282. new_seed = carta_random32(old_seed);
  2283. val -= (old_seed & mask); /* counter values are negative numbers! */
  2284. if ((mask >> 32) != 0)
  2285. /* construct a full 64-bit random value: */
  2286. new_seed |= carta_random32(old_seed >> 32) << 32;
  2287. reg->seed = new_seed;
  2288. }
  2289. reg->lval = val;
  2290. return val;
  2291. }
  2292. static void
  2293. pfm_reset_regs_masked(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
  2294. {
  2295. unsigned long mask = ovfl_regs[0];
  2296. unsigned long reset_others = 0UL;
  2297. unsigned long val;
  2298. int i;
  2299. /*
  2300. * now restore reset value on sampling overflowed counters
  2301. */
  2302. mask >>= PMU_FIRST_COUNTER;
  2303. for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
  2304. if ((mask & 0x1UL) == 0UL) continue;
  2305. ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
  2306. reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
  2307. DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
  2308. }
  2309. /*
  2310. * Now take care of resetting the other registers
  2311. */
  2312. for(i = 0; reset_others; i++, reset_others >>= 1) {
  2313. if ((reset_others & 0x1) == 0) continue;
  2314. ctx->ctx_pmds[i].val = val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
  2315. DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
  2316. is_long_reset ? "long" : "short", i, val));
  2317. }
  2318. }
  2319. static void
  2320. pfm_reset_regs(pfm_context_t *ctx, unsigned long *ovfl_regs, int is_long_reset)
  2321. {
  2322. unsigned long mask = ovfl_regs[0];
  2323. unsigned long reset_others = 0UL;
  2324. unsigned long val;
  2325. int i;
  2326. DPRINT_ovfl(("ovfl_regs=0x%lx is_long_reset=%d\n", ovfl_regs[0], is_long_reset));
  2327. if (ctx->ctx_state == PFM_CTX_MASKED) {
  2328. pfm_reset_regs_masked(ctx, ovfl_regs, is_long_reset);
  2329. return;
  2330. }
  2331. /*
  2332. * now restore reset value on sampling overflowed counters
  2333. */
  2334. mask >>= PMU_FIRST_COUNTER;
  2335. for(i = PMU_FIRST_COUNTER; mask; i++, mask >>= 1) {
  2336. if ((mask & 0x1UL) == 0UL) continue;
  2337. val = pfm_new_counter_value(ctx->ctx_pmds+ i, is_long_reset);
  2338. reset_others |= ctx->ctx_pmds[i].reset_pmds[0];
  2339. DPRINT_ovfl((" %s reset ctx_pmds[%d]=%lx\n", is_long_reset ? "long" : "short", i, val));
  2340. pfm_write_soft_counter(ctx, i, val);
  2341. }
  2342. /*
  2343. * Now take care of resetting the other registers
  2344. */
  2345. for(i = 0; reset_others; i++, reset_others >>= 1) {
  2346. if ((reset_others & 0x1) == 0) continue;
  2347. val = pfm_new_counter_value(ctx->ctx_pmds + i, is_long_reset);
  2348. if (PMD_IS_COUNTING(i)) {
  2349. pfm_write_soft_counter(ctx, i, val);
  2350. } else {
  2351. ia64_set_pmd(i, val);
  2352. }
  2353. DPRINT_ovfl(("%s reset_others pmd[%d]=%lx\n",
  2354. is_long_reset ? "long" : "short", i, val));
  2355. }
  2356. ia64_srlz_d();
  2357. }
  2358. static int
  2359. pfm_write_pmcs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2360. {
  2361. struct thread_struct *thread = NULL;
  2362. struct task_struct *task;
  2363. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2364. unsigned long value, pmc_pm;
  2365. unsigned long smpl_pmds, reset_pmds, impl_pmds;
  2366. unsigned int cnum, reg_flags, flags, pmc_type;
  2367. int i, can_access_pmu = 0, is_loaded, is_system, expert_mode;
  2368. int is_monitor, is_counting, state;
  2369. int ret = -EINVAL;
  2370. pfm_reg_check_t wr_func;
  2371. #define PFM_CHECK_PMC_PM(x, y, z) ((x)->ctx_fl_system ^ PMC_PM(y, z))
  2372. state = ctx->ctx_state;
  2373. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2374. is_system = ctx->ctx_fl_system;
  2375. task = ctx->ctx_task;
  2376. impl_pmds = pmu_conf->impl_pmds[0];
  2377. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  2378. if (is_loaded) {
  2379. thread = &task->thread;
  2380. /*
  2381. * In system wide and when the context is loaded, access can only happen
  2382. * when the caller is running on the CPU being monitored by the session.
  2383. * It does not have to be the owner (ctx_task) of the context per se.
  2384. */
  2385. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  2386. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2387. return -EBUSY;
  2388. }
  2389. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2390. }
  2391. expert_mode = pfm_sysctl.expert_mode;
  2392. for (i = 0; i < count; i++, req++) {
  2393. cnum = req->reg_num;
  2394. reg_flags = req->reg_flags;
  2395. value = req->reg_value;
  2396. smpl_pmds = req->reg_smpl_pmds[0];
  2397. reset_pmds = req->reg_reset_pmds[0];
  2398. flags = 0;
  2399. if (cnum >= PMU_MAX_PMCS) {
  2400. DPRINT(("pmc%u is invalid\n", cnum));
  2401. goto error;
  2402. }
  2403. pmc_type = pmu_conf->pmc_desc[cnum].type;
  2404. pmc_pm = (value >> pmu_conf->pmc_desc[cnum].pm_pos) & 0x1;
  2405. is_counting = (pmc_type & PFM_REG_COUNTING) == PFM_REG_COUNTING ? 1 : 0;
  2406. is_monitor = (pmc_type & PFM_REG_MONITOR) == PFM_REG_MONITOR ? 1 : 0;
  2407. /*
  2408. * we reject all non implemented PMC as well
  2409. * as attempts to modify PMC[0-3] which are used
  2410. * as status registers by the PMU
  2411. */
  2412. if ((pmc_type & PFM_REG_IMPL) == 0 || (pmc_type & PFM_REG_CONTROL) == PFM_REG_CONTROL) {
  2413. DPRINT(("pmc%u is unimplemented or no-access pmc_type=%x\n", cnum, pmc_type));
  2414. goto error;
  2415. }
  2416. wr_func = pmu_conf->pmc_desc[cnum].write_check;
  2417. /*
  2418. * If the PMC is a monitor, then if the value is not the default:
  2419. * - system-wide session: PMCx.pm=1 (privileged monitor)
  2420. * - per-task : PMCx.pm=0 (user monitor)
  2421. */
  2422. if (is_monitor && value != PMC_DFL_VAL(cnum) && is_system ^ pmc_pm) {
  2423. DPRINT(("pmc%u pmc_pm=%lu is_system=%d\n",
  2424. cnum,
  2425. pmc_pm,
  2426. is_system));
  2427. goto error;
  2428. }
  2429. if (is_counting) {
  2430. /*
  2431. * enforce generation of overflow interrupt. Necessary on all
  2432. * CPUs.
  2433. */
  2434. value |= 1 << PMU_PMC_OI;
  2435. if (reg_flags & PFM_REGFL_OVFL_NOTIFY) {
  2436. flags |= PFM_REGFL_OVFL_NOTIFY;
  2437. }
  2438. if (reg_flags & PFM_REGFL_RANDOM) flags |= PFM_REGFL_RANDOM;
  2439. /* verify validity of smpl_pmds */
  2440. if ((smpl_pmds & impl_pmds) != smpl_pmds) {
  2441. DPRINT(("invalid smpl_pmds 0x%lx for pmc%u\n", smpl_pmds, cnum));
  2442. goto error;
  2443. }
  2444. /* verify validity of reset_pmds */
  2445. if ((reset_pmds & impl_pmds) != reset_pmds) {
  2446. DPRINT(("invalid reset_pmds 0x%lx for pmc%u\n", reset_pmds, cnum));
  2447. goto error;
  2448. }
  2449. } else {
  2450. if (reg_flags & (PFM_REGFL_OVFL_NOTIFY|PFM_REGFL_RANDOM)) {
  2451. DPRINT(("cannot set ovfl_notify or random on pmc%u\n", cnum));
  2452. goto error;
  2453. }
  2454. /* eventid on non-counting monitors are ignored */
  2455. }
  2456. /*
  2457. * execute write checker, if any
  2458. */
  2459. if (likely(expert_mode == 0 && wr_func)) {
  2460. ret = (*wr_func)(task, ctx, cnum, &value, regs);
  2461. if (ret) goto error;
  2462. ret = -EINVAL;
  2463. }
  2464. /*
  2465. * no error on this register
  2466. */
  2467. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  2468. /*
  2469. * Now we commit the changes to the software state
  2470. */
  2471. /*
  2472. * update overflow information
  2473. */
  2474. if (is_counting) {
  2475. /*
  2476. * full flag update each time a register is programmed
  2477. */
  2478. ctx->ctx_pmds[cnum].flags = flags;
  2479. ctx->ctx_pmds[cnum].reset_pmds[0] = reset_pmds;
  2480. ctx->ctx_pmds[cnum].smpl_pmds[0] = smpl_pmds;
  2481. ctx->ctx_pmds[cnum].eventid = req->reg_smpl_eventid;
  2482. /*
  2483. * Mark all PMDS to be accessed as used.
  2484. *
  2485. * We do not keep track of PMC because we have to
  2486. * systematically restore ALL of them.
  2487. *
  2488. * We do not update the used_monitors mask, because
  2489. * if we have not programmed them, then will be in
  2490. * a quiescent state, therefore we will not need to
  2491. * mask/restore then when context is MASKED.
  2492. */
  2493. CTX_USED_PMD(ctx, reset_pmds);
  2494. CTX_USED_PMD(ctx, smpl_pmds);
  2495. /*
  2496. * make sure we do not try to reset on
  2497. * restart because we have established new values
  2498. */
  2499. if (state == PFM_CTX_MASKED) ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
  2500. }
  2501. /*
  2502. * Needed in case the user does not initialize the equivalent
  2503. * PMD. Clearing is done indirectly via pfm_reset_pmu_state() so there is no
  2504. * possible leak here.
  2505. */
  2506. CTX_USED_PMD(ctx, pmu_conf->pmc_desc[cnum].dep_pmd[0]);
  2507. /*
  2508. * keep track of the monitor PMC that we are using.
  2509. * we save the value of the pmc in ctx_pmcs[] and if
  2510. * the monitoring is not stopped for the context we also
  2511. * place it in the saved state area so that it will be
  2512. * picked up later by the context switch code.
  2513. *
  2514. * The value in ctx_pmcs[] can only be changed in pfm_write_pmcs().
  2515. *
  2516. * The value in thread->pmcs[] may be modified on overflow, i.e., when
  2517. * monitoring needs to be stopped.
  2518. */
  2519. if (is_monitor) CTX_USED_MONITOR(ctx, 1UL << cnum);
  2520. /*
  2521. * update context state
  2522. */
  2523. ctx->ctx_pmcs[cnum] = value;
  2524. if (is_loaded) {
  2525. /*
  2526. * write thread state
  2527. */
  2528. if (is_system == 0) thread->pmcs[cnum] = value;
  2529. /*
  2530. * write hardware register if we can
  2531. */
  2532. if (can_access_pmu) {
  2533. ia64_set_pmc(cnum, value);
  2534. }
  2535. #ifdef CONFIG_SMP
  2536. else {
  2537. /*
  2538. * per-task SMP only here
  2539. *
  2540. * we are guaranteed that the task is not running on the other CPU,
  2541. * we indicate that this PMD will need to be reloaded if the task
  2542. * is rescheduled on the CPU it ran last on.
  2543. */
  2544. ctx->ctx_reload_pmcs[0] |= 1UL << cnum;
  2545. }
  2546. #endif
  2547. }
  2548. DPRINT(("pmc[%u]=0x%lx ld=%d apmu=%d flags=0x%x all_pmcs=0x%lx used_pmds=0x%lx eventid=%ld smpl_pmds=0x%lx reset_pmds=0x%lx reloads_pmcs=0x%lx used_monitors=0x%lx ovfl_regs=0x%lx\n",
  2549. cnum,
  2550. value,
  2551. is_loaded,
  2552. can_access_pmu,
  2553. flags,
  2554. ctx->ctx_all_pmcs[0],
  2555. ctx->ctx_used_pmds[0],
  2556. ctx->ctx_pmds[cnum].eventid,
  2557. smpl_pmds,
  2558. reset_pmds,
  2559. ctx->ctx_reload_pmcs[0],
  2560. ctx->ctx_used_monitors[0],
  2561. ctx->ctx_ovfl_regs[0]));
  2562. }
  2563. /*
  2564. * make sure the changes are visible
  2565. */
  2566. if (can_access_pmu) ia64_srlz_d();
  2567. return 0;
  2568. error:
  2569. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2570. return ret;
  2571. }
  2572. static int
  2573. pfm_write_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2574. {
  2575. struct thread_struct *thread = NULL;
  2576. struct task_struct *task;
  2577. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2578. unsigned long value, hw_value, ovfl_mask;
  2579. unsigned int cnum;
  2580. int i, can_access_pmu = 0, state;
  2581. int is_counting, is_loaded, is_system, expert_mode;
  2582. int ret = -EINVAL;
  2583. pfm_reg_check_t wr_func;
  2584. state = ctx->ctx_state;
  2585. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2586. is_system = ctx->ctx_fl_system;
  2587. ovfl_mask = pmu_conf->ovfl_val;
  2588. task = ctx->ctx_task;
  2589. if (unlikely(state == PFM_CTX_ZOMBIE)) return -EINVAL;
  2590. /*
  2591. * on both UP and SMP, we can only write to the PMC when the task is
  2592. * the owner of the local PMU.
  2593. */
  2594. if (likely(is_loaded)) {
  2595. thread = &task->thread;
  2596. /*
  2597. * In system wide and when the context is loaded, access can only happen
  2598. * when the caller is running on the CPU being monitored by the session.
  2599. * It does not have to be the owner (ctx_task) of the context per se.
  2600. */
  2601. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  2602. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2603. return -EBUSY;
  2604. }
  2605. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2606. }
  2607. expert_mode = pfm_sysctl.expert_mode;
  2608. for (i = 0; i < count; i++, req++) {
  2609. cnum = req->reg_num;
  2610. value = req->reg_value;
  2611. if (!PMD_IS_IMPL(cnum)) {
  2612. DPRINT(("pmd[%u] is unimplemented or invalid\n", cnum));
  2613. goto abort_mission;
  2614. }
  2615. is_counting = PMD_IS_COUNTING(cnum);
  2616. wr_func = pmu_conf->pmd_desc[cnum].write_check;
  2617. /*
  2618. * execute write checker, if any
  2619. */
  2620. if (unlikely(expert_mode == 0 && wr_func)) {
  2621. unsigned long v = value;
  2622. ret = (*wr_func)(task, ctx, cnum, &v, regs);
  2623. if (ret) goto abort_mission;
  2624. value = v;
  2625. ret = -EINVAL;
  2626. }
  2627. /*
  2628. * no error on this register
  2629. */
  2630. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  2631. /*
  2632. * now commit changes to software state
  2633. */
  2634. hw_value = value;
  2635. /*
  2636. * update virtualized (64bits) counter
  2637. */
  2638. if (is_counting) {
  2639. /*
  2640. * write context state
  2641. */
  2642. ctx->ctx_pmds[cnum].lval = value;
  2643. /*
  2644. * when context is load we use the split value
  2645. */
  2646. if (is_loaded) {
  2647. hw_value = value & ovfl_mask;
  2648. value = value & ~ovfl_mask;
  2649. }
  2650. }
  2651. /*
  2652. * update reset values (not just for counters)
  2653. */
  2654. ctx->ctx_pmds[cnum].long_reset = req->reg_long_reset;
  2655. ctx->ctx_pmds[cnum].short_reset = req->reg_short_reset;
  2656. /*
  2657. * update randomization parameters (not just for counters)
  2658. */
  2659. ctx->ctx_pmds[cnum].seed = req->reg_random_seed;
  2660. ctx->ctx_pmds[cnum].mask = req->reg_random_mask;
  2661. /*
  2662. * update context value
  2663. */
  2664. ctx->ctx_pmds[cnum].val = value;
  2665. /*
  2666. * Keep track of what we use
  2667. *
  2668. * We do not keep track of PMC because we have to
  2669. * systematically restore ALL of them.
  2670. */
  2671. CTX_USED_PMD(ctx, PMD_PMD_DEP(cnum));
  2672. /*
  2673. * mark this PMD register used as well
  2674. */
  2675. CTX_USED_PMD(ctx, RDEP(cnum));
  2676. /*
  2677. * make sure we do not try to reset on
  2678. * restart because we have established new values
  2679. */
  2680. if (is_counting && state == PFM_CTX_MASKED) {
  2681. ctx->ctx_ovfl_regs[0] &= ~1UL << cnum;
  2682. }
  2683. if (is_loaded) {
  2684. /*
  2685. * write thread state
  2686. */
  2687. if (is_system == 0) thread->pmds[cnum] = hw_value;
  2688. /*
  2689. * write hardware register if we can
  2690. */
  2691. if (can_access_pmu) {
  2692. ia64_set_pmd(cnum, hw_value);
  2693. } else {
  2694. #ifdef CONFIG_SMP
  2695. /*
  2696. * we are guaranteed that the task is not running on the other CPU,
  2697. * we indicate that this PMD will need to be reloaded if the task
  2698. * is rescheduled on the CPU it ran last on.
  2699. */
  2700. ctx->ctx_reload_pmds[0] |= 1UL << cnum;
  2701. #endif
  2702. }
  2703. }
  2704. DPRINT(("pmd[%u]=0x%lx ld=%d apmu=%d, hw_value=0x%lx ctx_pmd=0x%lx short_reset=0x%lx "
  2705. "long_reset=0x%lx notify=%c seed=0x%lx mask=0x%lx used_pmds=0x%lx reset_pmds=0x%lx reload_pmds=0x%lx all_pmds=0x%lx ovfl_regs=0x%lx\n",
  2706. cnum,
  2707. value,
  2708. is_loaded,
  2709. can_access_pmu,
  2710. hw_value,
  2711. ctx->ctx_pmds[cnum].val,
  2712. ctx->ctx_pmds[cnum].short_reset,
  2713. ctx->ctx_pmds[cnum].long_reset,
  2714. PMC_OVFL_NOTIFY(ctx, cnum) ? 'Y':'N',
  2715. ctx->ctx_pmds[cnum].seed,
  2716. ctx->ctx_pmds[cnum].mask,
  2717. ctx->ctx_used_pmds[0],
  2718. ctx->ctx_pmds[cnum].reset_pmds[0],
  2719. ctx->ctx_reload_pmds[0],
  2720. ctx->ctx_all_pmds[0],
  2721. ctx->ctx_ovfl_regs[0]));
  2722. }
  2723. /*
  2724. * make changes visible
  2725. */
  2726. if (can_access_pmu) ia64_srlz_d();
  2727. return 0;
  2728. abort_mission:
  2729. /*
  2730. * for now, we have only one possibility for error
  2731. */
  2732. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2733. return ret;
  2734. }
  2735. /*
  2736. * By the way of PROTECT_CONTEXT(), interrupts are masked while we are in this function.
  2737. * Therefore we know, we do not have to worry about the PMU overflow interrupt. If an
  2738. * interrupt is delivered during the call, it will be kept pending until we leave, making
  2739. * it appears as if it had been generated at the UNPROTECT_CONTEXT(). At least we are
  2740. * guaranteed to return consistent data to the user, it may simply be old. It is not
  2741. * trivial to treat the overflow while inside the call because you may end up in
  2742. * some module sampling buffer code causing deadlocks.
  2743. */
  2744. static int
  2745. pfm_read_pmds(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2746. {
  2747. struct thread_struct *thread = NULL;
  2748. struct task_struct *task;
  2749. unsigned long val = 0UL, lval, ovfl_mask, sval;
  2750. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  2751. unsigned int cnum, reg_flags = 0;
  2752. int i, can_access_pmu = 0, state;
  2753. int is_loaded, is_system, is_counting, expert_mode;
  2754. int ret = -EINVAL;
  2755. pfm_reg_check_t rd_func;
  2756. /*
  2757. * access is possible when loaded only for
  2758. * self-monitoring tasks or in UP mode
  2759. */
  2760. state = ctx->ctx_state;
  2761. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  2762. is_system = ctx->ctx_fl_system;
  2763. ovfl_mask = pmu_conf->ovfl_val;
  2764. task = ctx->ctx_task;
  2765. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  2766. if (likely(is_loaded)) {
  2767. thread = &task->thread;
  2768. /*
  2769. * In system wide and when the context is loaded, access can only happen
  2770. * when the caller is running on the CPU being monitored by the session.
  2771. * It does not have to be the owner (ctx_task) of the context per se.
  2772. */
  2773. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  2774. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2775. return -EBUSY;
  2776. }
  2777. /*
  2778. * this can be true when not self-monitoring only in UP
  2779. */
  2780. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  2781. if (can_access_pmu) ia64_srlz_d();
  2782. }
  2783. expert_mode = pfm_sysctl.expert_mode;
  2784. DPRINT(("ld=%d apmu=%d ctx_state=%d\n",
  2785. is_loaded,
  2786. can_access_pmu,
  2787. state));
  2788. /*
  2789. * on both UP and SMP, we can only read the PMD from the hardware register when
  2790. * the task is the owner of the local PMU.
  2791. */
  2792. for (i = 0; i < count; i++, req++) {
  2793. cnum = req->reg_num;
  2794. reg_flags = req->reg_flags;
  2795. if (unlikely(!PMD_IS_IMPL(cnum))) goto error;
  2796. /*
  2797. * we can only read the register that we use. That includes
  2798. * the one we explicitely initialize AND the one we want included
  2799. * in the sampling buffer (smpl_regs).
  2800. *
  2801. * Having this restriction allows optimization in the ctxsw routine
  2802. * without compromising security (leaks)
  2803. */
  2804. if (unlikely(!CTX_IS_USED_PMD(ctx, cnum))) goto error;
  2805. sval = ctx->ctx_pmds[cnum].val;
  2806. lval = ctx->ctx_pmds[cnum].lval;
  2807. is_counting = PMD_IS_COUNTING(cnum);
  2808. /*
  2809. * If the task is not the current one, then we check if the
  2810. * PMU state is still in the local live register due to lazy ctxsw.
  2811. * If true, then we read directly from the registers.
  2812. */
  2813. if (can_access_pmu){
  2814. val = ia64_get_pmd(cnum);
  2815. } else {
  2816. /*
  2817. * context has been saved
  2818. * if context is zombie, then task does not exist anymore.
  2819. * In this case, we use the full value saved in the context (pfm_flush_regs()).
  2820. */
  2821. val = is_loaded ? thread->pmds[cnum] : 0UL;
  2822. }
  2823. rd_func = pmu_conf->pmd_desc[cnum].read_check;
  2824. if (is_counting) {
  2825. /*
  2826. * XXX: need to check for overflow when loaded
  2827. */
  2828. val &= ovfl_mask;
  2829. val += sval;
  2830. }
  2831. /*
  2832. * execute read checker, if any
  2833. */
  2834. if (unlikely(expert_mode == 0 && rd_func)) {
  2835. unsigned long v = val;
  2836. ret = (*rd_func)(ctx->ctx_task, ctx, cnum, &v, regs);
  2837. if (ret) goto error;
  2838. val = v;
  2839. ret = -EINVAL;
  2840. }
  2841. PFM_REG_RETFLAG_SET(reg_flags, 0);
  2842. DPRINT(("pmd[%u]=0x%lx\n", cnum, val));
  2843. /*
  2844. * update register return value, abort all if problem during copy.
  2845. * we only modify the reg_flags field. no check mode is fine because
  2846. * access has been verified upfront in sys_perfmonctl().
  2847. */
  2848. req->reg_value = val;
  2849. req->reg_flags = reg_flags;
  2850. req->reg_last_reset_val = lval;
  2851. }
  2852. return 0;
  2853. error:
  2854. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  2855. return ret;
  2856. }
  2857. int
  2858. pfm_mod_write_pmcs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  2859. {
  2860. pfm_context_t *ctx;
  2861. if (req == NULL) return -EINVAL;
  2862. ctx = GET_PMU_CTX();
  2863. if (ctx == NULL) return -EINVAL;
  2864. /*
  2865. * for now limit to current task, which is enough when calling
  2866. * from overflow handler
  2867. */
  2868. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  2869. return pfm_write_pmcs(ctx, req, nreq, regs);
  2870. }
  2871. EXPORT_SYMBOL(pfm_mod_write_pmcs);
  2872. int
  2873. pfm_mod_read_pmds(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  2874. {
  2875. pfm_context_t *ctx;
  2876. if (req == NULL) return -EINVAL;
  2877. ctx = GET_PMU_CTX();
  2878. if (ctx == NULL) return -EINVAL;
  2879. /*
  2880. * for now limit to current task, which is enough when calling
  2881. * from overflow handler
  2882. */
  2883. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  2884. return pfm_read_pmds(ctx, req, nreq, regs);
  2885. }
  2886. EXPORT_SYMBOL(pfm_mod_read_pmds);
  2887. /*
  2888. * Only call this function when a process it trying to
  2889. * write the debug registers (reading is always allowed)
  2890. */
  2891. int
  2892. pfm_use_debug_registers(struct task_struct *task)
  2893. {
  2894. pfm_context_t *ctx = task->thread.pfm_context;
  2895. unsigned long flags;
  2896. int ret = 0;
  2897. if (pmu_conf->use_rr_dbregs == 0) return 0;
  2898. DPRINT(("called for [%d]\n", task->pid));
  2899. /*
  2900. * do it only once
  2901. */
  2902. if (task->thread.flags & IA64_THREAD_DBG_VALID) return 0;
  2903. /*
  2904. * Even on SMP, we do not need to use an atomic here because
  2905. * the only way in is via ptrace() and this is possible only when the
  2906. * process is stopped. Even in the case where the ctxsw out is not totally
  2907. * completed by the time we come here, there is no way the 'stopped' process
  2908. * could be in the middle of fiddling with the pfm_write_ibr_dbr() routine.
  2909. * So this is always safe.
  2910. */
  2911. if (ctx && ctx->ctx_fl_using_dbreg == 1) return -1;
  2912. LOCK_PFS(flags);
  2913. /*
  2914. * We cannot allow setting breakpoints when system wide monitoring
  2915. * sessions are using the debug registers.
  2916. */
  2917. if (pfm_sessions.pfs_sys_use_dbregs> 0)
  2918. ret = -1;
  2919. else
  2920. pfm_sessions.pfs_ptrace_use_dbregs++;
  2921. DPRINT(("ptrace_use_dbregs=%u sys_use_dbregs=%u by [%d] ret = %d\n",
  2922. pfm_sessions.pfs_ptrace_use_dbregs,
  2923. pfm_sessions.pfs_sys_use_dbregs,
  2924. task->pid, ret));
  2925. UNLOCK_PFS(flags);
  2926. return ret;
  2927. }
  2928. /*
  2929. * This function is called for every task that exits with the
  2930. * IA64_THREAD_DBG_VALID set. This indicates a task which was
  2931. * able to use the debug registers for debugging purposes via
  2932. * ptrace(). Therefore we know it was not using them for
  2933. * perfmormance monitoring, so we only decrement the number
  2934. * of "ptraced" debug register users to keep the count up to date
  2935. */
  2936. int
  2937. pfm_release_debug_registers(struct task_struct *task)
  2938. {
  2939. unsigned long flags;
  2940. int ret;
  2941. if (pmu_conf->use_rr_dbregs == 0) return 0;
  2942. LOCK_PFS(flags);
  2943. if (pfm_sessions.pfs_ptrace_use_dbregs == 0) {
  2944. printk(KERN_ERR "perfmon: invalid release for [%d] ptrace_use_dbregs=0\n", task->pid);
  2945. ret = -1;
  2946. } else {
  2947. pfm_sessions.pfs_ptrace_use_dbregs--;
  2948. ret = 0;
  2949. }
  2950. UNLOCK_PFS(flags);
  2951. return ret;
  2952. }
  2953. static int
  2954. pfm_restart(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  2955. {
  2956. struct task_struct *task;
  2957. pfm_buffer_fmt_t *fmt;
  2958. pfm_ovfl_ctrl_t rst_ctrl;
  2959. int state, is_system;
  2960. int ret = 0;
  2961. state = ctx->ctx_state;
  2962. fmt = ctx->ctx_buf_fmt;
  2963. is_system = ctx->ctx_fl_system;
  2964. task = PFM_CTX_TASK(ctx);
  2965. switch(state) {
  2966. case PFM_CTX_MASKED:
  2967. break;
  2968. case PFM_CTX_LOADED:
  2969. if (CTX_HAS_SMPL(ctx) && fmt->fmt_restart_active) break;
  2970. /* fall through */
  2971. case PFM_CTX_UNLOADED:
  2972. case PFM_CTX_ZOMBIE:
  2973. DPRINT(("invalid state=%d\n", state));
  2974. return -EBUSY;
  2975. default:
  2976. DPRINT(("state=%d, cannot operate (no active_restart handler)\n", state));
  2977. return -EINVAL;
  2978. }
  2979. /*
  2980. * In system wide and when the context is loaded, access can only happen
  2981. * when the caller is running on the CPU being monitored by the session.
  2982. * It does not have to be the owner (ctx_task) of the context per se.
  2983. */
  2984. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  2985. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  2986. return -EBUSY;
  2987. }
  2988. /* sanity check */
  2989. if (unlikely(task == NULL)) {
  2990. printk(KERN_ERR "perfmon: [%d] pfm_restart no task\n", current->pid);
  2991. return -EINVAL;
  2992. }
  2993. if (task == current || is_system) {
  2994. fmt = ctx->ctx_buf_fmt;
  2995. DPRINT(("restarting self %d ovfl=0x%lx\n",
  2996. task->pid,
  2997. ctx->ctx_ovfl_regs[0]));
  2998. if (CTX_HAS_SMPL(ctx)) {
  2999. prefetch(ctx->ctx_smpl_hdr);
  3000. rst_ctrl.bits.mask_monitoring = 0;
  3001. rst_ctrl.bits.reset_ovfl_pmds = 0;
  3002. if (state == PFM_CTX_LOADED)
  3003. ret = pfm_buf_fmt_restart_active(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  3004. else
  3005. ret = pfm_buf_fmt_restart(fmt, task, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  3006. } else {
  3007. rst_ctrl.bits.mask_monitoring = 0;
  3008. rst_ctrl.bits.reset_ovfl_pmds = 1;
  3009. }
  3010. if (ret == 0) {
  3011. if (rst_ctrl.bits.reset_ovfl_pmds)
  3012. pfm_reset_regs(ctx, ctx->ctx_ovfl_regs, PFM_PMD_LONG_RESET);
  3013. if (rst_ctrl.bits.mask_monitoring == 0) {
  3014. DPRINT(("resuming monitoring for [%d]\n", task->pid));
  3015. if (state == PFM_CTX_MASKED) pfm_restore_monitoring(task);
  3016. } else {
  3017. DPRINT(("keeping monitoring stopped for [%d]\n", task->pid));
  3018. // cannot use pfm_stop_monitoring(task, regs);
  3019. }
  3020. }
  3021. /*
  3022. * clear overflowed PMD mask to remove any stale information
  3023. */
  3024. ctx->ctx_ovfl_regs[0] = 0UL;
  3025. /*
  3026. * back to LOADED state
  3027. */
  3028. ctx->ctx_state = PFM_CTX_LOADED;
  3029. /*
  3030. * XXX: not really useful for self monitoring
  3031. */
  3032. ctx->ctx_fl_can_restart = 0;
  3033. return 0;
  3034. }
  3035. /*
  3036. * restart another task
  3037. */
  3038. /*
  3039. * When PFM_CTX_MASKED, we cannot issue a restart before the previous
  3040. * one is seen by the task.
  3041. */
  3042. if (state == PFM_CTX_MASKED) {
  3043. if (ctx->ctx_fl_can_restart == 0) return -EINVAL;
  3044. /*
  3045. * will prevent subsequent restart before this one is
  3046. * seen by other task
  3047. */
  3048. ctx->ctx_fl_can_restart = 0;
  3049. }
  3050. /*
  3051. * if blocking, then post the semaphore is PFM_CTX_MASKED, i.e.
  3052. * the task is blocked or on its way to block. That's the normal
  3053. * restart path. If the monitoring is not masked, then the task
  3054. * can be actively monitoring and we cannot directly intervene.
  3055. * Therefore we use the trap mechanism to catch the task and
  3056. * force it to reset the buffer/reset PMDs.
  3057. *
  3058. * if non-blocking, then we ensure that the task will go into
  3059. * pfm_handle_work() before returning to user mode.
  3060. *
  3061. * We cannot explicitely reset another task, it MUST always
  3062. * be done by the task itself. This works for system wide because
  3063. * the tool that is controlling the session is logically doing
  3064. * "self-monitoring".
  3065. */
  3066. if (CTX_OVFL_NOBLOCK(ctx) == 0 && state == PFM_CTX_MASKED) {
  3067. DPRINT(("unblocking [%d] \n", task->pid));
  3068. up(&ctx->ctx_restart_sem);
  3069. } else {
  3070. DPRINT(("[%d] armed exit trap\n", task->pid));
  3071. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_RESET;
  3072. PFM_SET_WORK_PENDING(task, 1);
  3073. pfm_set_task_notify(task);
  3074. /*
  3075. * XXX: send reschedule if task runs on another CPU
  3076. */
  3077. }
  3078. return 0;
  3079. }
  3080. static int
  3081. pfm_debug(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3082. {
  3083. unsigned int m = *(unsigned int *)arg;
  3084. pfm_sysctl.debug = m == 0 ? 0 : 1;
  3085. printk(KERN_INFO "perfmon debugging %s (timing reset)\n", pfm_sysctl.debug ? "on" : "off");
  3086. if (m == 0) {
  3087. memset(pfm_stats, 0, sizeof(pfm_stats));
  3088. for(m=0; m < NR_CPUS; m++) pfm_stats[m].pfm_ovfl_intr_cycles_min = ~0UL;
  3089. }
  3090. return 0;
  3091. }
  3092. /*
  3093. * arg can be NULL and count can be zero for this function
  3094. */
  3095. static int
  3096. pfm_write_ibr_dbr(int mode, pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3097. {
  3098. struct thread_struct *thread = NULL;
  3099. struct task_struct *task;
  3100. pfarg_dbreg_t *req = (pfarg_dbreg_t *)arg;
  3101. unsigned long flags;
  3102. dbreg_t dbreg;
  3103. unsigned int rnum;
  3104. int first_time;
  3105. int ret = 0, state;
  3106. int i, can_access_pmu = 0;
  3107. int is_system, is_loaded;
  3108. if (pmu_conf->use_rr_dbregs == 0) return -EINVAL;
  3109. state = ctx->ctx_state;
  3110. is_loaded = state == PFM_CTX_LOADED ? 1 : 0;
  3111. is_system = ctx->ctx_fl_system;
  3112. task = ctx->ctx_task;
  3113. if (state == PFM_CTX_ZOMBIE) return -EINVAL;
  3114. /*
  3115. * on both UP and SMP, we can only write to the PMC when the task is
  3116. * the owner of the local PMU.
  3117. */
  3118. if (is_loaded) {
  3119. thread = &task->thread;
  3120. /*
  3121. * In system wide and when the context is loaded, access can only happen
  3122. * when the caller is running on the CPU being monitored by the session.
  3123. * It does not have to be the owner (ctx_task) of the context per se.
  3124. */
  3125. if (unlikely(is_system && ctx->ctx_cpu != smp_processor_id())) {
  3126. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3127. return -EBUSY;
  3128. }
  3129. can_access_pmu = GET_PMU_OWNER() == task || is_system ? 1 : 0;
  3130. }
  3131. /*
  3132. * we do not need to check for ipsr.db because we do clear ibr.x, dbr.r, and dbr.w
  3133. * ensuring that no real breakpoint can be installed via this call.
  3134. *
  3135. * IMPORTANT: regs can be NULL in this function
  3136. */
  3137. first_time = ctx->ctx_fl_using_dbreg == 0;
  3138. /*
  3139. * don't bother if we are loaded and task is being debugged
  3140. */
  3141. if (is_loaded && (thread->flags & IA64_THREAD_DBG_VALID) != 0) {
  3142. DPRINT(("debug registers already in use for [%d]\n", task->pid));
  3143. return -EBUSY;
  3144. }
  3145. /*
  3146. * check for debug registers in system wide mode
  3147. *
  3148. * If though a check is done in pfm_context_load(),
  3149. * we must repeat it here, in case the registers are
  3150. * written after the context is loaded
  3151. */
  3152. if (is_loaded) {
  3153. LOCK_PFS(flags);
  3154. if (first_time && is_system) {
  3155. if (pfm_sessions.pfs_ptrace_use_dbregs)
  3156. ret = -EBUSY;
  3157. else
  3158. pfm_sessions.pfs_sys_use_dbregs++;
  3159. }
  3160. UNLOCK_PFS(flags);
  3161. }
  3162. if (ret != 0) return ret;
  3163. /*
  3164. * mark ourself as user of the debug registers for
  3165. * perfmon purposes.
  3166. */
  3167. ctx->ctx_fl_using_dbreg = 1;
  3168. /*
  3169. * clear hardware registers to make sure we don't
  3170. * pick up stale state.
  3171. *
  3172. * for a system wide session, we do not use
  3173. * thread.dbr, thread.ibr because this process
  3174. * never leaves the current CPU and the state
  3175. * is shared by all processes running on it
  3176. */
  3177. if (first_time && can_access_pmu) {
  3178. DPRINT(("[%d] clearing ibrs, dbrs\n", task->pid));
  3179. for (i=0; i < pmu_conf->num_ibrs; i++) {
  3180. ia64_set_ibr(i, 0UL);
  3181. ia64_dv_serialize_instruction();
  3182. }
  3183. ia64_srlz_i();
  3184. for (i=0; i < pmu_conf->num_dbrs; i++) {
  3185. ia64_set_dbr(i, 0UL);
  3186. ia64_dv_serialize_data();
  3187. }
  3188. ia64_srlz_d();
  3189. }
  3190. /*
  3191. * Now install the values into the registers
  3192. */
  3193. for (i = 0; i < count; i++, req++) {
  3194. rnum = req->dbreg_num;
  3195. dbreg.val = req->dbreg_value;
  3196. ret = -EINVAL;
  3197. if ((mode == PFM_CODE_RR && rnum >= PFM_NUM_IBRS) || ((mode == PFM_DATA_RR) && rnum >= PFM_NUM_DBRS)) {
  3198. DPRINT(("invalid register %u val=0x%lx mode=%d i=%d count=%d\n",
  3199. rnum, dbreg.val, mode, i, count));
  3200. goto abort_mission;
  3201. }
  3202. /*
  3203. * make sure we do not install enabled breakpoint
  3204. */
  3205. if (rnum & 0x1) {
  3206. if (mode == PFM_CODE_RR)
  3207. dbreg.ibr.ibr_x = 0;
  3208. else
  3209. dbreg.dbr.dbr_r = dbreg.dbr.dbr_w = 0;
  3210. }
  3211. PFM_REG_RETFLAG_SET(req->dbreg_flags, 0);
  3212. /*
  3213. * Debug registers, just like PMC, can only be modified
  3214. * by a kernel call. Moreover, perfmon() access to those
  3215. * registers are centralized in this routine. The hardware
  3216. * does not modify the value of these registers, therefore,
  3217. * if we save them as they are written, we can avoid having
  3218. * to save them on context switch out. This is made possible
  3219. * by the fact that when perfmon uses debug registers, ptrace()
  3220. * won't be able to modify them concurrently.
  3221. */
  3222. if (mode == PFM_CODE_RR) {
  3223. CTX_USED_IBR(ctx, rnum);
  3224. if (can_access_pmu) {
  3225. ia64_set_ibr(rnum, dbreg.val);
  3226. ia64_dv_serialize_instruction();
  3227. }
  3228. ctx->ctx_ibrs[rnum] = dbreg.val;
  3229. DPRINT(("write ibr%u=0x%lx used_ibrs=0x%x ld=%d apmu=%d\n",
  3230. rnum, dbreg.val, ctx->ctx_used_ibrs[0], is_loaded, can_access_pmu));
  3231. } else {
  3232. CTX_USED_DBR(ctx, rnum);
  3233. if (can_access_pmu) {
  3234. ia64_set_dbr(rnum, dbreg.val);
  3235. ia64_dv_serialize_data();
  3236. }
  3237. ctx->ctx_dbrs[rnum] = dbreg.val;
  3238. DPRINT(("write dbr%u=0x%lx used_dbrs=0x%x ld=%d apmu=%d\n",
  3239. rnum, dbreg.val, ctx->ctx_used_dbrs[0], is_loaded, can_access_pmu));
  3240. }
  3241. }
  3242. return 0;
  3243. abort_mission:
  3244. /*
  3245. * in case it was our first attempt, we undo the global modifications
  3246. */
  3247. if (first_time) {
  3248. LOCK_PFS(flags);
  3249. if (ctx->ctx_fl_system) {
  3250. pfm_sessions.pfs_sys_use_dbregs--;
  3251. }
  3252. UNLOCK_PFS(flags);
  3253. ctx->ctx_fl_using_dbreg = 0;
  3254. }
  3255. /*
  3256. * install error return flag
  3257. */
  3258. PFM_REG_RETFLAG_SET(req->dbreg_flags, PFM_REG_RETFL_EINVAL);
  3259. return ret;
  3260. }
  3261. static int
  3262. pfm_write_ibrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3263. {
  3264. return pfm_write_ibr_dbr(PFM_CODE_RR, ctx, arg, count, regs);
  3265. }
  3266. static int
  3267. pfm_write_dbrs(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3268. {
  3269. return pfm_write_ibr_dbr(PFM_DATA_RR, ctx, arg, count, regs);
  3270. }
  3271. int
  3272. pfm_mod_write_ibrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  3273. {
  3274. pfm_context_t *ctx;
  3275. if (req == NULL) return -EINVAL;
  3276. ctx = GET_PMU_CTX();
  3277. if (ctx == NULL) return -EINVAL;
  3278. /*
  3279. * for now limit to current task, which is enough when calling
  3280. * from overflow handler
  3281. */
  3282. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  3283. return pfm_write_ibrs(ctx, req, nreq, regs);
  3284. }
  3285. EXPORT_SYMBOL(pfm_mod_write_ibrs);
  3286. int
  3287. pfm_mod_write_dbrs(struct task_struct *task, void *req, unsigned int nreq, struct pt_regs *regs)
  3288. {
  3289. pfm_context_t *ctx;
  3290. if (req == NULL) return -EINVAL;
  3291. ctx = GET_PMU_CTX();
  3292. if (ctx == NULL) return -EINVAL;
  3293. /*
  3294. * for now limit to current task, which is enough when calling
  3295. * from overflow handler
  3296. */
  3297. if (task != current && ctx->ctx_fl_system == 0) return -EBUSY;
  3298. return pfm_write_dbrs(ctx, req, nreq, regs);
  3299. }
  3300. EXPORT_SYMBOL(pfm_mod_write_dbrs);
  3301. static int
  3302. pfm_get_features(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3303. {
  3304. pfarg_features_t *req = (pfarg_features_t *)arg;
  3305. req->ft_version = PFM_VERSION;
  3306. return 0;
  3307. }
  3308. static int
  3309. pfm_stop(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3310. {
  3311. struct pt_regs *tregs;
  3312. struct task_struct *task = PFM_CTX_TASK(ctx);
  3313. int state, is_system;
  3314. state = ctx->ctx_state;
  3315. is_system = ctx->ctx_fl_system;
  3316. /*
  3317. * context must be attached to issue the stop command (includes LOADED,MASKED,ZOMBIE)
  3318. */
  3319. if (state == PFM_CTX_UNLOADED) return -EINVAL;
  3320. /*
  3321. * In system wide and when the context is loaded, access can only happen
  3322. * when the caller is running on the CPU being monitored by the session.
  3323. * It does not have to be the owner (ctx_task) of the context per se.
  3324. */
  3325. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  3326. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3327. return -EBUSY;
  3328. }
  3329. DPRINT(("task [%d] ctx_state=%d is_system=%d\n",
  3330. PFM_CTX_TASK(ctx)->pid,
  3331. state,
  3332. is_system));
  3333. /*
  3334. * in system mode, we need to update the PMU directly
  3335. * and the user level state of the caller, which may not
  3336. * necessarily be the creator of the context.
  3337. */
  3338. if (is_system) {
  3339. /*
  3340. * Update local PMU first
  3341. *
  3342. * disable dcr pp
  3343. */
  3344. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) & ~IA64_DCR_PP);
  3345. ia64_srlz_i();
  3346. /*
  3347. * update local cpuinfo
  3348. */
  3349. PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
  3350. /*
  3351. * stop monitoring, does srlz.i
  3352. */
  3353. pfm_clear_psr_pp();
  3354. /*
  3355. * stop monitoring in the caller
  3356. */
  3357. ia64_psr(regs)->pp = 0;
  3358. return 0;
  3359. }
  3360. /*
  3361. * per-task mode
  3362. */
  3363. if (task == current) {
  3364. /* stop monitoring at kernel level */
  3365. pfm_clear_psr_up();
  3366. /*
  3367. * stop monitoring at the user level
  3368. */
  3369. ia64_psr(regs)->up = 0;
  3370. } else {
  3371. tregs = ia64_task_regs(task);
  3372. /*
  3373. * stop monitoring at the user level
  3374. */
  3375. ia64_psr(tregs)->up = 0;
  3376. /*
  3377. * monitoring disabled in kernel at next reschedule
  3378. */
  3379. ctx->ctx_saved_psr_up = 0;
  3380. DPRINT(("task=[%d]\n", task->pid));
  3381. }
  3382. return 0;
  3383. }
  3384. static int
  3385. pfm_start(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3386. {
  3387. struct pt_regs *tregs;
  3388. int state, is_system;
  3389. state = ctx->ctx_state;
  3390. is_system = ctx->ctx_fl_system;
  3391. if (state != PFM_CTX_LOADED) return -EINVAL;
  3392. /*
  3393. * In system wide and when the context is loaded, access can only happen
  3394. * when the caller is running on the CPU being monitored by the session.
  3395. * It does not have to be the owner (ctx_task) of the context per se.
  3396. */
  3397. if (is_system && ctx->ctx_cpu != smp_processor_id()) {
  3398. DPRINT(("should be running on CPU%d\n", ctx->ctx_cpu));
  3399. return -EBUSY;
  3400. }
  3401. /*
  3402. * in system mode, we need to update the PMU directly
  3403. * and the user level state of the caller, which may not
  3404. * necessarily be the creator of the context.
  3405. */
  3406. if (is_system) {
  3407. /*
  3408. * set user level psr.pp for the caller
  3409. */
  3410. ia64_psr(regs)->pp = 1;
  3411. /*
  3412. * now update the local PMU and cpuinfo
  3413. */
  3414. PFM_CPUINFO_SET(PFM_CPUINFO_DCR_PP);
  3415. /*
  3416. * start monitoring at kernel level
  3417. */
  3418. pfm_set_psr_pp();
  3419. /* enable dcr pp */
  3420. ia64_setreg(_IA64_REG_CR_DCR, ia64_getreg(_IA64_REG_CR_DCR) | IA64_DCR_PP);
  3421. ia64_srlz_i();
  3422. return 0;
  3423. }
  3424. /*
  3425. * per-process mode
  3426. */
  3427. if (ctx->ctx_task == current) {
  3428. /* start monitoring at kernel level */
  3429. pfm_set_psr_up();
  3430. /*
  3431. * activate monitoring at user level
  3432. */
  3433. ia64_psr(regs)->up = 1;
  3434. } else {
  3435. tregs = ia64_task_regs(ctx->ctx_task);
  3436. /*
  3437. * start monitoring at the kernel level the next
  3438. * time the task is scheduled
  3439. */
  3440. ctx->ctx_saved_psr_up = IA64_PSR_UP;
  3441. /*
  3442. * activate monitoring at user level
  3443. */
  3444. ia64_psr(tregs)->up = 1;
  3445. }
  3446. return 0;
  3447. }
  3448. static int
  3449. pfm_get_pmc_reset(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3450. {
  3451. pfarg_reg_t *req = (pfarg_reg_t *)arg;
  3452. unsigned int cnum;
  3453. int i;
  3454. int ret = -EINVAL;
  3455. for (i = 0; i < count; i++, req++) {
  3456. cnum = req->reg_num;
  3457. if (!PMC_IS_IMPL(cnum)) goto abort_mission;
  3458. req->reg_value = PMC_DFL_VAL(cnum);
  3459. PFM_REG_RETFLAG_SET(req->reg_flags, 0);
  3460. DPRINT(("pmc_reset_val pmc[%u]=0x%lx\n", cnum, req->reg_value));
  3461. }
  3462. return 0;
  3463. abort_mission:
  3464. PFM_REG_RETFLAG_SET(req->reg_flags, PFM_REG_RETFL_EINVAL);
  3465. return ret;
  3466. }
  3467. static int
  3468. pfm_check_task_exist(pfm_context_t *ctx)
  3469. {
  3470. struct task_struct *g, *t;
  3471. int ret = -ESRCH;
  3472. read_lock(&tasklist_lock);
  3473. do_each_thread (g, t) {
  3474. if (t->thread.pfm_context == ctx) {
  3475. ret = 0;
  3476. break;
  3477. }
  3478. } while_each_thread (g, t);
  3479. read_unlock(&tasklist_lock);
  3480. DPRINT(("pfm_check_task_exist: ret=%d ctx=%p\n", ret, ctx));
  3481. return ret;
  3482. }
  3483. static int
  3484. pfm_context_load(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3485. {
  3486. struct task_struct *task;
  3487. struct thread_struct *thread;
  3488. struct pfm_context_t *old;
  3489. unsigned long flags;
  3490. #ifndef CONFIG_SMP
  3491. struct task_struct *owner_task = NULL;
  3492. #endif
  3493. pfarg_load_t *req = (pfarg_load_t *)arg;
  3494. unsigned long *pmcs_source, *pmds_source;
  3495. int the_cpu;
  3496. int ret = 0;
  3497. int state, is_system, set_dbregs = 0;
  3498. state = ctx->ctx_state;
  3499. is_system = ctx->ctx_fl_system;
  3500. /*
  3501. * can only load from unloaded or terminated state
  3502. */
  3503. if (state != PFM_CTX_UNLOADED) {
  3504. DPRINT(("cannot load to [%d], invalid ctx_state=%d\n",
  3505. req->load_pid,
  3506. ctx->ctx_state));
  3507. return -EBUSY;
  3508. }
  3509. DPRINT(("load_pid [%d] using_dbreg=%d\n", req->load_pid, ctx->ctx_fl_using_dbreg));
  3510. if (CTX_OVFL_NOBLOCK(ctx) == 0 && req->load_pid == current->pid) {
  3511. DPRINT(("cannot use blocking mode on self\n"));
  3512. return -EINVAL;
  3513. }
  3514. ret = pfm_get_task(ctx, req->load_pid, &task);
  3515. if (ret) {
  3516. DPRINT(("load_pid [%d] get_task=%d\n", req->load_pid, ret));
  3517. return ret;
  3518. }
  3519. ret = -EINVAL;
  3520. /*
  3521. * system wide is self monitoring only
  3522. */
  3523. if (is_system && task != current) {
  3524. DPRINT(("system wide is self monitoring only load_pid=%d\n",
  3525. req->load_pid));
  3526. goto error;
  3527. }
  3528. thread = &task->thread;
  3529. ret = 0;
  3530. /*
  3531. * cannot load a context which is using range restrictions,
  3532. * into a task that is being debugged.
  3533. */
  3534. if (ctx->ctx_fl_using_dbreg) {
  3535. if (thread->flags & IA64_THREAD_DBG_VALID) {
  3536. ret = -EBUSY;
  3537. DPRINT(("load_pid [%d] task is debugged, cannot load range restrictions\n", req->load_pid));
  3538. goto error;
  3539. }
  3540. LOCK_PFS(flags);
  3541. if (is_system) {
  3542. if (pfm_sessions.pfs_ptrace_use_dbregs) {
  3543. DPRINT(("cannot load [%d] dbregs in use\n", task->pid));
  3544. ret = -EBUSY;
  3545. } else {
  3546. pfm_sessions.pfs_sys_use_dbregs++;
  3547. DPRINT(("load [%d] increased sys_use_dbreg=%u\n", task->pid, pfm_sessions.pfs_sys_use_dbregs));
  3548. set_dbregs = 1;
  3549. }
  3550. }
  3551. UNLOCK_PFS(flags);
  3552. if (ret) goto error;
  3553. }
  3554. /*
  3555. * SMP system-wide monitoring implies self-monitoring.
  3556. *
  3557. * The programming model expects the task to
  3558. * be pinned on a CPU throughout the session.
  3559. * Here we take note of the current CPU at the
  3560. * time the context is loaded. No call from
  3561. * another CPU will be allowed.
  3562. *
  3563. * The pinning via shed_setaffinity()
  3564. * must be done by the calling task prior
  3565. * to this call.
  3566. *
  3567. * systemwide: keep track of CPU this session is supposed to run on
  3568. */
  3569. the_cpu = ctx->ctx_cpu = smp_processor_id();
  3570. ret = -EBUSY;
  3571. /*
  3572. * now reserve the session
  3573. */
  3574. ret = pfm_reserve_session(current, is_system, the_cpu);
  3575. if (ret) goto error;
  3576. /*
  3577. * task is necessarily stopped at this point.
  3578. *
  3579. * If the previous context was zombie, then it got removed in
  3580. * pfm_save_regs(). Therefore we should not see it here.
  3581. * If we see a context, then this is an active context
  3582. *
  3583. * XXX: needs to be atomic
  3584. */
  3585. DPRINT(("before cmpxchg() old_ctx=%p new_ctx=%p\n",
  3586. thread->pfm_context, ctx));
  3587. ret = -EBUSY;
  3588. old = ia64_cmpxchg(acq, &thread->pfm_context, NULL, ctx, sizeof(pfm_context_t *));
  3589. if (old != NULL) {
  3590. DPRINT(("load_pid [%d] already has a context\n", req->load_pid));
  3591. goto error_unres;
  3592. }
  3593. pfm_reset_msgq(ctx);
  3594. ctx->ctx_state = PFM_CTX_LOADED;
  3595. /*
  3596. * link context to task
  3597. */
  3598. ctx->ctx_task = task;
  3599. if (is_system) {
  3600. /*
  3601. * we load as stopped
  3602. */
  3603. PFM_CPUINFO_SET(PFM_CPUINFO_SYST_WIDE);
  3604. PFM_CPUINFO_CLEAR(PFM_CPUINFO_DCR_PP);
  3605. if (ctx->ctx_fl_excl_idle) PFM_CPUINFO_SET(PFM_CPUINFO_EXCL_IDLE);
  3606. } else {
  3607. thread->flags |= IA64_THREAD_PM_VALID;
  3608. }
  3609. /*
  3610. * propagate into thread-state
  3611. */
  3612. pfm_copy_pmds(task, ctx);
  3613. pfm_copy_pmcs(task, ctx);
  3614. pmcs_source = thread->pmcs;
  3615. pmds_source = thread->pmds;
  3616. /*
  3617. * always the case for system-wide
  3618. */
  3619. if (task == current) {
  3620. if (is_system == 0) {
  3621. /* allow user level control */
  3622. ia64_psr(regs)->sp = 0;
  3623. DPRINT(("clearing psr.sp for [%d]\n", task->pid));
  3624. SET_LAST_CPU(ctx, smp_processor_id());
  3625. INC_ACTIVATION();
  3626. SET_ACTIVATION(ctx);
  3627. #ifndef CONFIG_SMP
  3628. /*
  3629. * push the other task out, if any
  3630. */
  3631. owner_task = GET_PMU_OWNER();
  3632. if (owner_task) pfm_lazy_save_regs(owner_task);
  3633. #endif
  3634. }
  3635. /*
  3636. * load all PMD from ctx to PMU (as opposed to thread state)
  3637. * restore all PMC from ctx to PMU
  3638. */
  3639. pfm_restore_pmds(pmds_source, ctx->ctx_all_pmds[0]);
  3640. pfm_restore_pmcs(pmcs_source, ctx->ctx_all_pmcs[0]);
  3641. ctx->ctx_reload_pmcs[0] = 0UL;
  3642. ctx->ctx_reload_pmds[0] = 0UL;
  3643. /*
  3644. * guaranteed safe by earlier check against DBG_VALID
  3645. */
  3646. if (ctx->ctx_fl_using_dbreg) {
  3647. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  3648. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  3649. }
  3650. /*
  3651. * set new ownership
  3652. */
  3653. SET_PMU_OWNER(task, ctx);
  3654. DPRINT(("context loaded on PMU for [%d]\n", task->pid));
  3655. } else {
  3656. /*
  3657. * when not current, task MUST be stopped, so this is safe
  3658. */
  3659. regs = ia64_task_regs(task);
  3660. /* force a full reload */
  3661. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  3662. SET_LAST_CPU(ctx, -1);
  3663. /* initial saved psr (stopped) */
  3664. ctx->ctx_saved_psr_up = 0UL;
  3665. ia64_psr(regs)->up = ia64_psr(regs)->pp = 0;
  3666. }
  3667. ret = 0;
  3668. error_unres:
  3669. if (ret) pfm_unreserve_session(ctx, ctx->ctx_fl_system, the_cpu);
  3670. error:
  3671. /*
  3672. * we must undo the dbregs setting (for system-wide)
  3673. */
  3674. if (ret && set_dbregs) {
  3675. LOCK_PFS(flags);
  3676. pfm_sessions.pfs_sys_use_dbregs--;
  3677. UNLOCK_PFS(flags);
  3678. }
  3679. /*
  3680. * release task, there is now a link with the context
  3681. */
  3682. if (is_system == 0 && task != current) {
  3683. pfm_put_task(task);
  3684. if (ret == 0) {
  3685. ret = pfm_check_task_exist(ctx);
  3686. if (ret) {
  3687. ctx->ctx_state = PFM_CTX_UNLOADED;
  3688. ctx->ctx_task = NULL;
  3689. }
  3690. }
  3691. }
  3692. return ret;
  3693. }
  3694. /*
  3695. * in this function, we do not need to increase the use count
  3696. * for the task via get_task_struct(), because we hold the
  3697. * context lock. If the task were to disappear while having
  3698. * a context attached, it would go through pfm_exit_thread()
  3699. * which also grabs the context lock and would therefore be blocked
  3700. * until we are here.
  3701. */
  3702. static void pfm_flush_pmds(struct task_struct *, pfm_context_t *ctx);
  3703. static int
  3704. pfm_context_unload(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs)
  3705. {
  3706. struct task_struct *task = PFM_CTX_TASK(ctx);
  3707. struct pt_regs *tregs;
  3708. int prev_state, is_system;
  3709. int ret;
  3710. DPRINT(("ctx_state=%d task [%d]\n", ctx->ctx_state, task ? task->pid : -1));
  3711. prev_state = ctx->ctx_state;
  3712. is_system = ctx->ctx_fl_system;
  3713. /*
  3714. * unload only when necessary
  3715. */
  3716. if (prev_state == PFM_CTX_UNLOADED) {
  3717. DPRINT(("ctx_state=%d, nothing to do\n", prev_state));
  3718. return 0;
  3719. }
  3720. /*
  3721. * clear psr and dcr bits
  3722. */
  3723. ret = pfm_stop(ctx, NULL, 0, regs);
  3724. if (ret) return ret;
  3725. ctx->ctx_state = PFM_CTX_UNLOADED;
  3726. /*
  3727. * in system mode, we need to update the PMU directly
  3728. * and the user level state of the caller, which may not
  3729. * necessarily be the creator of the context.
  3730. */
  3731. if (is_system) {
  3732. /*
  3733. * Update cpuinfo
  3734. *
  3735. * local PMU is taken care of in pfm_stop()
  3736. */
  3737. PFM_CPUINFO_CLEAR(PFM_CPUINFO_SYST_WIDE);
  3738. PFM_CPUINFO_CLEAR(PFM_CPUINFO_EXCL_IDLE);
  3739. /*
  3740. * save PMDs in context
  3741. * release ownership
  3742. */
  3743. pfm_flush_pmds(current, ctx);
  3744. /*
  3745. * at this point we are done with the PMU
  3746. * so we can unreserve the resource.
  3747. */
  3748. if (prev_state != PFM_CTX_ZOMBIE)
  3749. pfm_unreserve_session(ctx, 1 , ctx->ctx_cpu);
  3750. /*
  3751. * disconnect context from task
  3752. */
  3753. task->thread.pfm_context = NULL;
  3754. /*
  3755. * disconnect task from context
  3756. */
  3757. ctx->ctx_task = NULL;
  3758. /*
  3759. * There is nothing more to cleanup here.
  3760. */
  3761. return 0;
  3762. }
  3763. /*
  3764. * per-task mode
  3765. */
  3766. tregs = task == current ? regs : ia64_task_regs(task);
  3767. if (task == current) {
  3768. /*
  3769. * cancel user level control
  3770. */
  3771. ia64_psr(regs)->sp = 1;
  3772. DPRINT(("setting psr.sp for [%d]\n", task->pid));
  3773. }
  3774. /*
  3775. * save PMDs to context
  3776. * release ownership
  3777. */
  3778. pfm_flush_pmds(task, ctx);
  3779. /*
  3780. * at this point we are done with the PMU
  3781. * so we can unreserve the resource.
  3782. *
  3783. * when state was ZOMBIE, we have already unreserved.
  3784. */
  3785. if (prev_state != PFM_CTX_ZOMBIE)
  3786. pfm_unreserve_session(ctx, 0 , ctx->ctx_cpu);
  3787. /*
  3788. * reset activation counter and psr
  3789. */
  3790. ctx->ctx_last_activation = PFM_INVALID_ACTIVATION;
  3791. SET_LAST_CPU(ctx, -1);
  3792. /*
  3793. * PMU state will not be restored
  3794. */
  3795. task->thread.flags &= ~IA64_THREAD_PM_VALID;
  3796. /*
  3797. * break links between context and task
  3798. */
  3799. task->thread.pfm_context = NULL;
  3800. ctx->ctx_task = NULL;
  3801. PFM_SET_WORK_PENDING(task, 0);
  3802. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
  3803. ctx->ctx_fl_can_restart = 0;
  3804. ctx->ctx_fl_going_zombie = 0;
  3805. DPRINT(("disconnected [%d] from context\n", task->pid));
  3806. return 0;
  3807. }
  3808. /*
  3809. * called only from exit_thread(): task == current
  3810. * we come here only if current has a context attached (loaded or masked)
  3811. */
  3812. void
  3813. pfm_exit_thread(struct task_struct *task)
  3814. {
  3815. pfm_context_t *ctx;
  3816. unsigned long flags;
  3817. struct pt_regs *regs = ia64_task_regs(task);
  3818. int ret, state;
  3819. int free_ok = 0;
  3820. ctx = PFM_GET_CTX(task);
  3821. PROTECT_CTX(ctx, flags);
  3822. DPRINT(("state=%d task [%d]\n", ctx->ctx_state, task->pid));
  3823. state = ctx->ctx_state;
  3824. switch(state) {
  3825. case PFM_CTX_UNLOADED:
  3826. /*
  3827. * only comes to thios function if pfm_context is not NULL, i.e., cannot
  3828. * be in unloaded state
  3829. */
  3830. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] ctx unloaded\n", task->pid);
  3831. break;
  3832. case PFM_CTX_LOADED:
  3833. case PFM_CTX_MASKED:
  3834. ret = pfm_context_unload(ctx, NULL, 0, regs);
  3835. if (ret) {
  3836. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
  3837. }
  3838. DPRINT(("ctx unloaded for current state was %d\n", state));
  3839. pfm_end_notify_user(ctx);
  3840. break;
  3841. case PFM_CTX_ZOMBIE:
  3842. ret = pfm_context_unload(ctx, NULL, 0, regs);
  3843. if (ret) {
  3844. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] state=%d unload failed %d\n", task->pid, state, ret);
  3845. }
  3846. free_ok = 1;
  3847. break;
  3848. default:
  3849. printk(KERN_ERR "perfmon: pfm_exit_thread [%d] unexpected state=%d\n", task->pid, state);
  3850. break;
  3851. }
  3852. UNPROTECT_CTX(ctx, flags);
  3853. { u64 psr = pfm_get_psr();
  3854. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  3855. BUG_ON(GET_PMU_OWNER());
  3856. BUG_ON(ia64_psr(regs)->up);
  3857. BUG_ON(ia64_psr(regs)->pp);
  3858. }
  3859. /*
  3860. * All memory free operations (especially for vmalloc'ed memory)
  3861. * MUST be done with interrupts ENABLED.
  3862. */
  3863. if (free_ok) pfm_context_free(ctx);
  3864. }
  3865. /*
  3866. * functions MUST be listed in the increasing order of their index (see permfon.h)
  3867. */
  3868. #define PFM_CMD(name, flags, arg_count, arg_type, getsz) { name, #name, flags, arg_count, sizeof(arg_type), getsz }
  3869. #define PFM_CMD_S(name, flags) { name, #name, flags, 0, 0, NULL }
  3870. #define PFM_CMD_PCLRWS (PFM_CMD_FD|PFM_CMD_ARG_RW|PFM_CMD_STOP)
  3871. #define PFM_CMD_PCLRW (PFM_CMD_FD|PFM_CMD_ARG_RW)
  3872. #define PFM_CMD_NONE { NULL, "no-cmd", 0, 0, 0, NULL}
  3873. static pfm_cmd_desc_t pfm_cmd_tab[]={
  3874. /* 0 */PFM_CMD_NONE,
  3875. /* 1 */PFM_CMD(pfm_write_pmcs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3876. /* 2 */PFM_CMD(pfm_write_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3877. /* 3 */PFM_CMD(pfm_read_pmds, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3878. /* 4 */PFM_CMD_S(pfm_stop, PFM_CMD_PCLRWS),
  3879. /* 5 */PFM_CMD_S(pfm_start, PFM_CMD_PCLRWS),
  3880. /* 6 */PFM_CMD_NONE,
  3881. /* 7 */PFM_CMD_NONE,
  3882. /* 8 */PFM_CMD(pfm_context_create, PFM_CMD_ARG_RW, 1, pfarg_context_t, pfm_ctx_getsize),
  3883. /* 9 */PFM_CMD_NONE,
  3884. /* 10 */PFM_CMD_S(pfm_restart, PFM_CMD_PCLRW),
  3885. /* 11 */PFM_CMD_NONE,
  3886. /* 12 */PFM_CMD(pfm_get_features, PFM_CMD_ARG_RW, 1, pfarg_features_t, NULL),
  3887. /* 13 */PFM_CMD(pfm_debug, 0, 1, unsigned int, NULL),
  3888. /* 14 */PFM_CMD_NONE,
  3889. /* 15 */PFM_CMD(pfm_get_pmc_reset, PFM_CMD_ARG_RW, PFM_CMD_ARG_MANY, pfarg_reg_t, NULL),
  3890. /* 16 */PFM_CMD(pfm_context_load, PFM_CMD_PCLRWS, 1, pfarg_load_t, NULL),
  3891. /* 17 */PFM_CMD_S(pfm_context_unload, PFM_CMD_PCLRWS),
  3892. /* 18 */PFM_CMD_NONE,
  3893. /* 19 */PFM_CMD_NONE,
  3894. /* 20 */PFM_CMD_NONE,
  3895. /* 21 */PFM_CMD_NONE,
  3896. /* 22 */PFM_CMD_NONE,
  3897. /* 23 */PFM_CMD_NONE,
  3898. /* 24 */PFM_CMD_NONE,
  3899. /* 25 */PFM_CMD_NONE,
  3900. /* 26 */PFM_CMD_NONE,
  3901. /* 27 */PFM_CMD_NONE,
  3902. /* 28 */PFM_CMD_NONE,
  3903. /* 29 */PFM_CMD_NONE,
  3904. /* 30 */PFM_CMD_NONE,
  3905. /* 31 */PFM_CMD_NONE,
  3906. /* 32 */PFM_CMD(pfm_write_ibrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL),
  3907. /* 33 */PFM_CMD(pfm_write_dbrs, PFM_CMD_PCLRWS, PFM_CMD_ARG_MANY, pfarg_dbreg_t, NULL)
  3908. };
  3909. #define PFM_CMD_COUNT (sizeof(pfm_cmd_tab)/sizeof(pfm_cmd_desc_t))
  3910. static int
  3911. pfm_check_task_state(pfm_context_t *ctx, int cmd, unsigned long flags)
  3912. {
  3913. struct task_struct *task;
  3914. int state, old_state;
  3915. recheck:
  3916. state = ctx->ctx_state;
  3917. task = ctx->ctx_task;
  3918. if (task == NULL) {
  3919. DPRINT(("context %d no task, state=%d\n", ctx->ctx_fd, state));
  3920. return 0;
  3921. }
  3922. DPRINT(("context %d state=%d [%d] task_state=%ld must_stop=%d\n",
  3923. ctx->ctx_fd,
  3924. state,
  3925. task->pid,
  3926. task->state, PFM_CMD_STOPPED(cmd)));
  3927. /*
  3928. * self-monitoring always ok.
  3929. *
  3930. * for system-wide the caller can either be the creator of the
  3931. * context (to one to which the context is attached to) OR
  3932. * a task running on the same CPU as the session.
  3933. */
  3934. if (task == current || ctx->ctx_fl_system) return 0;
  3935. /*
  3936. * we are monitoring another thread
  3937. */
  3938. switch(state) {
  3939. case PFM_CTX_UNLOADED:
  3940. /*
  3941. * if context is UNLOADED we are safe to go
  3942. */
  3943. return 0;
  3944. case PFM_CTX_ZOMBIE:
  3945. /*
  3946. * no command can operate on a zombie context
  3947. */
  3948. DPRINT(("cmd %d state zombie cannot operate on context\n", cmd));
  3949. return -EINVAL;
  3950. case PFM_CTX_MASKED:
  3951. /*
  3952. * PMU state has been saved to software even though
  3953. * the thread may still be running.
  3954. */
  3955. if (cmd != PFM_UNLOAD_CONTEXT) return 0;
  3956. }
  3957. /*
  3958. * context is LOADED or MASKED. Some commands may need to have
  3959. * the task stopped.
  3960. *
  3961. * We could lift this restriction for UP but it would mean that
  3962. * the user has no guarantee the task would not run between
  3963. * two successive calls to perfmonctl(). That's probably OK.
  3964. * If this user wants to ensure the task does not run, then
  3965. * the task must be stopped.
  3966. */
  3967. if (PFM_CMD_STOPPED(cmd)) {
  3968. if ((task->state != TASK_STOPPED) && (task->state != TASK_TRACED)) {
  3969. DPRINT(("[%d] task not in stopped state\n", task->pid));
  3970. return -EBUSY;
  3971. }
  3972. /*
  3973. * task is now stopped, wait for ctxsw out
  3974. *
  3975. * This is an interesting point in the code.
  3976. * We need to unprotect the context because
  3977. * the pfm_save_regs() routines needs to grab
  3978. * the same lock. There are danger in doing
  3979. * this because it leaves a window open for
  3980. * another task to get access to the context
  3981. * and possibly change its state. The one thing
  3982. * that is not possible is for the context to disappear
  3983. * because we are protected by the VFS layer, i.e.,
  3984. * get_fd()/put_fd().
  3985. */
  3986. old_state = state;
  3987. UNPROTECT_CTX(ctx, flags);
  3988. wait_task_inactive(task);
  3989. PROTECT_CTX(ctx, flags);
  3990. /*
  3991. * we must recheck to verify if state has changed
  3992. */
  3993. if (ctx->ctx_state != old_state) {
  3994. DPRINT(("old_state=%d new_state=%d\n", old_state, ctx->ctx_state));
  3995. goto recheck;
  3996. }
  3997. }
  3998. return 0;
  3999. }
  4000. /*
  4001. * system-call entry point (must return long)
  4002. */
  4003. asmlinkage long
  4004. sys_perfmonctl (int fd, int cmd, void __user *arg, int count)
  4005. {
  4006. struct file *file = NULL;
  4007. pfm_context_t *ctx = NULL;
  4008. unsigned long flags = 0UL;
  4009. void *args_k = NULL;
  4010. long ret; /* will expand int return types */
  4011. size_t base_sz, sz, xtra_sz = 0;
  4012. int narg, completed_args = 0, call_made = 0, cmd_flags;
  4013. int (*func)(pfm_context_t *ctx, void *arg, int count, struct pt_regs *regs);
  4014. int (*getsize)(void *arg, size_t *sz);
  4015. #define PFM_MAX_ARGSIZE 4096
  4016. /*
  4017. * reject any call if perfmon was disabled at initialization
  4018. */
  4019. if (unlikely(pmu_conf == NULL)) return -ENOSYS;
  4020. if (unlikely(cmd < 0 || cmd >= PFM_CMD_COUNT)) {
  4021. DPRINT(("invalid cmd=%d\n", cmd));
  4022. return -EINVAL;
  4023. }
  4024. func = pfm_cmd_tab[cmd].cmd_func;
  4025. narg = pfm_cmd_tab[cmd].cmd_narg;
  4026. base_sz = pfm_cmd_tab[cmd].cmd_argsize;
  4027. getsize = pfm_cmd_tab[cmd].cmd_getsize;
  4028. cmd_flags = pfm_cmd_tab[cmd].cmd_flags;
  4029. if (unlikely(func == NULL)) {
  4030. DPRINT(("invalid cmd=%d\n", cmd));
  4031. return -EINVAL;
  4032. }
  4033. DPRINT(("cmd=%s idx=%d narg=0x%x argsz=%lu count=%d\n",
  4034. PFM_CMD_NAME(cmd),
  4035. cmd,
  4036. narg,
  4037. base_sz,
  4038. count));
  4039. /*
  4040. * check if number of arguments matches what the command expects
  4041. */
  4042. if (unlikely((narg == PFM_CMD_ARG_MANY && count <= 0) || (narg > 0 && narg != count)))
  4043. return -EINVAL;
  4044. restart_args:
  4045. sz = xtra_sz + base_sz*count;
  4046. /*
  4047. * limit abuse to min page size
  4048. */
  4049. if (unlikely(sz > PFM_MAX_ARGSIZE)) {
  4050. printk(KERN_ERR "perfmon: [%d] argument too big %lu\n", current->pid, sz);
  4051. return -E2BIG;
  4052. }
  4053. /*
  4054. * allocate default-sized argument buffer
  4055. */
  4056. if (likely(count && args_k == NULL)) {
  4057. args_k = kmalloc(PFM_MAX_ARGSIZE, GFP_KERNEL);
  4058. if (args_k == NULL) return -ENOMEM;
  4059. }
  4060. ret = -EFAULT;
  4061. /*
  4062. * copy arguments
  4063. *
  4064. * assume sz = 0 for command without parameters
  4065. */
  4066. if (sz && copy_from_user(args_k, arg, sz)) {
  4067. DPRINT(("cannot copy_from_user %lu bytes @%p\n", sz, arg));
  4068. goto error_args;
  4069. }
  4070. /*
  4071. * check if command supports extra parameters
  4072. */
  4073. if (completed_args == 0 && getsize) {
  4074. /*
  4075. * get extra parameters size (based on main argument)
  4076. */
  4077. ret = (*getsize)(args_k, &xtra_sz);
  4078. if (ret) goto error_args;
  4079. completed_args = 1;
  4080. DPRINT(("restart_args sz=%lu xtra_sz=%lu\n", sz, xtra_sz));
  4081. /* retry if necessary */
  4082. if (likely(xtra_sz)) goto restart_args;
  4083. }
  4084. if (unlikely((cmd_flags & PFM_CMD_FD) == 0)) goto skip_fd;
  4085. ret = -EBADF;
  4086. file = fget(fd);
  4087. if (unlikely(file == NULL)) {
  4088. DPRINT(("invalid fd %d\n", fd));
  4089. goto error_args;
  4090. }
  4091. if (unlikely(PFM_IS_FILE(file) == 0)) {
  4092. DPRINT(("fd %d not related to perfmon\n", fd));
  4093. goto error_args;
  4094. }
  4095. ctx = (pfm_context_t *)file->private_data;
  4096. if (unlikely(ctx == NULL)) {
  4097. DPRINT(("no context for fd %d\n", fd));
  4098. goto error_args;
  4099. }
  4100. prefetch(&ctx->ctx_state);
  4101. PROTECT_CTX(ctx, flags);
  4102. /*
  4103. * check task is stopped
  4104. */
  4105. ret = pfm_check_task_state(ctx, cmd, flags);
  4106. if (unlikely(ret)) goto abort_locked;
  4107. skip_fd:
  4108. ret = (*func)(ctx, args_k, count, ia64_task_regs(current));
  4109. call_made = 1;
  4110. abort_locked:
  4111. if (likely(ctx)) {
  4112. DPRINT(("context unlocked\n"));
  4113. UNPROTECT_CTX(ctx, flags);
  4114. fput(file);
  4115. }
  4116. /* copy argument back to user, if needed */
  4117. if (call_made && PFM_CMD_RW_ARG(cmd) && copy_to_user(arg, args_k, base_sz*count)) ret = -EFAULT;
  4118. error_args:
  4119. if (args_k) kfree(args_k);
  4120. DPRINT(("cmd=%s ret=%ld\n", PFM_CMD_NAME(cmd), ret));
  4121. return ret;
  4122. }
  4123. static void
  4124. pfm_resume_after_ovfl(pfm_context_t *ctx, unsigned long ovfl_regs, struct pt_regs *regs)
  4125. {
  4126. pfm_buffer_fmt_t *fmt = ctx->ctx_buf_fmt;
  4127. pfm_ovfl_ctrl_t rst_ctrl;
  4128. int state;
  4129. int ret = 0;
  4130. state = ctx->ctx_state;
  4131. /*
  4132. * Unlock sampling buffer and reset index atomically
  4133. * XXX: not really needed when blocking
  4134. */
  4135. if (CTX_HAS_SMPL(ctx)) {
  4136. rst_ctrl.bits.mask_monitoring = 0;
  4137. rst_ctrl.bits.reset_ovfl_pmds = 0;
  4138. if (state == PFM_CTX_LOADED)
  4139. ret = pfm_buf_fmt_restart_active(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  4140. else
  4141. ret = pfm_buf_fmt_restart(fmt, current, &rst_ctrl, ctx->ctx_smpl_hdr, regs);
  4142. } else {
  4143. rst_ctrl.bits.mask_monitoring = 0;
  4144. rst_ctrl.bits.reset_ovfl_pmds = 1;
  4145. }
  4146. if (ret == 0) {
  4147. if (rst_ctrl.bits.reset_ovfl_pmds) {
  4148. pfm_reset_regs(ctx, &ovfl_regs, PFM_PMD_LONG_RESET);
  4149. }
  4150. if (rst_ctrl.bits.mask_monitoring == 0) {
  4151. DPRINT(("resuming monitoring\n"));
  4152. if (ctx->ctx_state == PFM_CTX_MASKED) pfm_restore_monitoring(current);
  4153. } else {
  4154. DPRINT(("stopping monitoring\n"));
  4155. //pfm_stop_monitoring(current, regs);
  4156. }
  4157. ctx->ctx_state = PFM_CTX_LOADED;
  4158. }
  4159. }
  4160. /*
  4161. * context MUST BE LOCKED when calling
  4162. * can only be called for current
  4163. */
  4164. static void
  4165. pfm_context_force_terminate(pfm_context_t *ctx, struct pt_regs *regs)
  4166. {
  4167. int ret;
  4168. DPRINT(("entering for [%d]\n", current->pid));
  4169. ret = pfm_context_unload(ctx, NULL, 0, regs);
  4170. if (ret) {
  4171. printk(KERN_ERR "pfm_context_force_terminate: [%d] unloaded failed with %d\n", current->pid, ret);
  4172. }
  4173. /*
  4174. * and wakeup controlling task, indicating we are now disconnected
  4175. */
  4176. wake_up_interruptible(&ctx->ctx_zombieq);
  4177. /*
  4178. * given that context is still locked, the controlling
  4179. * task will only get access when we return from
  4180. * pfm_handle_work().
  4181. */
  4182. }
  4183. static int pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds);
  4184. /*
  4185. * pfm_handle_work() can be called with interrupts enabled
  4186. * (TIF_NEED_RESCHED) or disabled. The down_interruptible
  4187. * call may sleep, therefore we must re-enable interrupts
  4188. * to avoid deadlocks. It is safe to do so because this function
  4189. * is called ONLY when returning to user level (PUStk=1), in which case
  4190. * there is no risk of kernel stack overflow due to deep
  4191. * interrupt nesting.
  4192. */
  4193. void
  4194. pfm_handle_work(void)
  4195. {
  4196. pfm_context_t *ctx;
  4197. struct pt_regs *regs;
  4198. unsigned long flags, dummy_flags;
  4199. unsigned long ovfl_regs;
  4200. unsigned int reason;
  4201. int ret;
  4202. ctx = PFM_GET_CTX(current);
  4203. if (ctx == NULL) {
  4204. printk(KERN_ERR "perfmon: [%d] has no PFM context\n", current->pid);
  4205. return;
  4206. }
  4207. PROTECT_CTX(ctx, flags);
  4208. PFM_SET_WORK_PENDING(current, 0);
  4209. pfm_clear_task_notify();
  4210. regs = ia64_task_regs(current);
  4211. /*
  4212. * extract reason for being here and clear
  4213. */
  4214. reason = ctx->ctx_fl_trap_reason;
  4215. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_NONE;
  4216. ovfl_regs = ctx->ctx_ovfl_regs[0];
  4217. DPRINT(("reason=%d state=%d\n", reason, ctx->ctx_state));
  4218. /*
  4219. * must be done before we check for simple-reset mode
  4220. */
  4221. if (ctx->ctx_fl_going_zombie || ctx->ctx_state == PFM_CTX_ZOMBIE) goto do_zombie;
  4222. //if (CTX_OVFL_NOBLOCK(ctx)) goto skip_blocking;
  4223. if (reason == PFM_TRAP_REASON_RESET) goto skip_blocking;
  4224. /*
  4225. * restore interrupt mask to what it was on entry.
  4226. * Could be enabled/diasbled.
  4227. */
  4228. UNPROTECT_CTX(ctx, flags);
  4229. /*
  4230. * force interrupt enable because of down_interruptible()
  4231. */
  4232. local_irq_enable();
  4233. DPRINT(("before block sleeping\n"));
  4234. /*
  4235. * may go through without blocking on SMP systems
  4236. * if restart has been received already by the time we call down()
  4237. */
  4238. ret = down_interruptible(&ctx->ctx_restart_sem);
  4239. DPRINT(("after block sleeping ret=%d\n", ret));
  4240. /*
  4241. * lock context and mask interrupts again
  4242. * We save flags into a dummy because we may have
  4243. * altered interrupts mask compared to entry in this
  4244. * function.
  4245. */
  4246. PROTECT_CTX(ctx, dummy_flags);
  4247. /*
  4248. * we need to read the ovfl_regs only after wake-up
  4249. * because we may have had pfm_write_pmds() in between
  4250. * and that can changed PMD values and therefore
  4251. * ovfl_regs is reset for these new PMD values.
  4252. */
  4253. ovfl_regs = ctx->ctx_ovfl_regs[0];
  4254. if (ctx->ctx_fl_going_zombie) {
  4255. do_zombie:
  4256. DPRINT(("context is zombie, bailing out\n"));
  4257. pfm_context_force_terminate(ctx, regs);
  4258. goto nothing_to_do;
  4259. }
  4260. /*
  4261. * in case of interruption of down() we don't restart anything
  4262. */
  4263. if (ret < 0) goto nothing_to_do;
  4264. skip_blocking:
  4265. pfm_resume_after_ovfl(ctx, ovfl_regs, regs);
  4266. ctx->ctx_ovfl_regs[0] = 0UL;
  4267. nothing_to_do:
  4268. /*
  4269. * restore flags as they were upon entry
  4270. */
  4271. UNPROTECT_CTX(ctx, flags);
  4272. }
  4273. static int
  4274. pfm_notify_user(pfm_context_t *ctx, pfm_msg_t *msg)
  4275. {
  4276. if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
  4277. DPRINT(("ignoring overflow notification, owner is zombie\n"));
  4278. return 0;
  4279. }
  4280. DPRINT(("waking up somebody\n"));
  4281. if (msg) wake_up_interruptible(&ctx->ctx_msgq_wait);
  4282. /*
  4283. * safe, we are not in intr handler, nor in ctxsw when
  4284. * we come here
  4285. */
  4286. kill_fasync (&ctx->ctx_async_queue, SIGIO, POLL_IN);
  4287. return 0;
  4288. }
  4289. static int
  4290. pfm_ovfl_notify_user(pfm_context_t *ctx, unsigned long ovfl_pmds)
  4291. {
  4292. pfm_msg_t *msg = NULL;
  4293. if (ctx->ctx_fl_no_msg == 0) {
  4294. msg = pfm_get_new_msg(ctx);
  4295. if (msg == NULL) {
  4296. printk(KERN_ERR "perfmon: pfm_ovfl_notify_user no more notification msgs\n");
  4297. return -1;
  4298. }
  4299. msg->pfm_ovfl_msg.msg_type = PFM_MSG_OVFL;
  4300. msg->pfm_ovfl_msg.msg_ctx_fd = ctx->ctx_fd;
  4301. msg->pfm_ovfl_msg.msg_active_set = 0;
  4302. msg->pfm_ovfl_msg.msg_ovfl_pmds[0] = ovfl_pmds;
  4303. msg->pfm_ovfl_msg.msg_ovfl_pmds[1] = 0UL;
  4304. msg->pfm_ovfl_msg.msg_ovfl_pmds[2] = 0UL;
  4305. msg->pfm_ovfl_msg.msg_ovfl_pmds[3] = 0UL;
  4306. msg->pfm_ovfl_msg.msg_tstamp = 0UL;
  4307. }
  4308. DPRINT(("ovfl msg: msg=%p no_msg=%d fd=%d ovfl_pmds=0x%lx\n",
  4309. msg,
  4310. ctx->ctx_fl_no_msg,
  4311. ctx->ctx_fd,
  4312. ovfl_pmds));
  4313. return pfm_notify_user(ctx, msg);
  4314. }
  4315. static int
  4316. pfm_end_notify_user(pfm_context_t *ctx)
  4317. {
  4318. pfm_msg_t *msg;
  4319. msg = pfm_get_new_msg(ctx);
  4320. if (msg == NULL) {
  4321. printk(KERN_ERR "perfmon: pfm_end_notify_user no more notification msgs\n");
  4322. return -1;
  4323. }
  4324. /* no leak */
  4325. memset(msg, 0, sizeof(*msg));
  4326. msg->pfm_end_msg.msg_type = PFM_MSG_END;
  4327. msg->pfm_end_msg.msg_ctx_fd = ctx->ctx_fd;
  4328. msg->pfm_ovfl_msg.msg_tstamp = 0UL;
  4329. DPRINT(("end msg: msg=%p no_msg=%d ctx_fd=%d\n",
  4330. msg,
  4331. ctx->ctx_fl_no_msg,
  4332. ctx->ctx_fd));
  4333. return pfm_notify_user(ctx, msg);
  4334. }
  4335. /*
  4336. * main overflow processing routine.
  4337. * it can be called from the interrupt path or explicitely during the context switch code
  4338. */
  4339. static void
  4340. pfm_overflow_handler(struct task_struct *task, pfm_context_t *ctx, u64 pmc0, struct pt_regs *regs)
  4341. {
  4342. pfm_ovfl_arg_t *ovfl_arg;
  4343. unsigned long mask;
  4344. unsigned long old_val, ovfl_val, new_val;
  4345. unsigned long ovfl_notify = 0UL, ovfl_pmds = 0UL, smpl_pmds = 0UL, reset_pmds;
  4346. unsigned long tstamp;
  4347. pfm_ovfl_ctrl_t ovfl_ctrl;
  4348. unsigned int i, has_smpl;
  4349. int must_notify = 0;
  4350. if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) goto stop_monitoring;
  4351. /*
  4352. * sanity test. Should never happen
  4353. */
  4354. if (unlikely((pmc0 & 0x1) == 0)) goto sanity_check;
  4355. tstamp = ia64_get_itc();
  4356. mask = pmc0 >> PMU_FIRST_COUNTER;
  4357. ovfl_val = pmu_conf->ovfl_val;
  4358. has_smpl = CTX_HAS_SMPL(ctx);
  4359. DPRINT_ovfl(("pmc0=0x%lx pid=%d iip=0x%lx, %s "
  4360. "used_pmds=0x%lx\n",
  4361. pmc0,
  4362. task ? task->pid: -1,
  4363. (regs ? regs->cr_iip : 0),
  4364. CTX_OVFL_NOBLOCK(ctx) ? "nonblocking" : "blocking",
  4365. ctx->ctx_used_pmds[0]));
  4366. /*
  4367. * first we update the virtual counters
  4368. * assume there was a prior ia64_srlz_d() issued
  4369. */
  4370. for (i = PMU_FIRST_COUNTER; mask ; i++, mask >>= 1) {
  4371. /* skip pmd which did not overflow */
  4372. if ((mask & 0x1) == 0) continue;
  4373. /*
  4374. * Note that the pmd is not necessarily 0 at this point as qualified events
  4375. * may have happened before the PMU was frozen. The residual count is not
  4376. * taken into consideration here but will be with any read of the pmd via
  4377. * pfm_read_pmds().
  4378. */
  4379. old_val = new_val = ctx->ctx_pmds[i].val;
  4380. new_val += 1 + ovfl_val;
  4381. ctx->ctx_pmds[i].val = new_val;
  4382. /*
  4383. * check for overflow condition
  4384. */
  4385. if (likely(old_val > new_val)) {
  4386. ovfl_pmds |= 1UL << i;
  4387. if (PMC_OVFL_NOTIFY(ctx, i)) ovfl_notify |= 1UL << i;
  4388. }
  4389. DPRINT_ovfl(("ctx_pmd[%d].val=0x%lx old_val=0x%lx pmd=0x%lx ovfl_pmds=0x%lx ovfl_notify=0x%lx\n",
  4390. i,
  4391. new_val,
  4392. old_val,
  4393. ia64_get_pmd(i) & ovfl_val,
  4394. ovfl_pmds,
  4395. ovfl_notify));
  4396. }
  4397. /*
  4398. * there was no 64-bit overflow, nothing else to do
  4399. */
  4400. if (ovfl_pmds == 0UL) return;
  4401. /*
  4402. * reset all control bits
  4403. */
  4404. ovfl_ctrl.val = 0;
  4405. reset_pmds = 0UL;
  4406. /*
  4407. * if a sampling format module exists, then we "cache" the overflow by
  4408. * calling the module's handler() routine.
  4409. */
  4410. if (has_smpl) {
  4411. unsigned long start_cycles, end_cycles;
  4412. unsigned long pmd_mask;
  4413. int j, k, ret = 0;
  4414. int this_cpu = smp_processor_id();
  4415. pmd_mask = ovfl_pmds >> PMU_FIRST_COUNTER;
  4416. ovfl_arg = &ctx->ctx_ovfl_arg;
  4417. prefetch(ctx->ctx_smpl_hdr);
  4418. for(i=PMU_FIRST_COUNTER; pmd_mask && ret == 0; i++, pmd_mask >>=1) {
  4419. mask = 1UL << i;
  4420. if ((pmd_mask & 0x1) == 0) continue;
  4421. ovfl_arg->ovfl_pmd = (unsigned char )i;
  4422. ovfl_arg->ovfl_notify = ovfl_notify & mask ? 1 : 0;
  4423. ovfl_arg->active_set = 0;
  4424. ovfl_arg->ovfl_ctrl.val = 0; /* module must fill in all fields */
  4425. ovfl_arg->smpl_pmds[0] = smpl_pmds = ctx->ctx_pmds[i].smpl_pmds[0];
  4426. ovfl_arg->pmd_value = ctx->ctx_pmds[i].val;
  4427. ovfl_arg->pmd_last_reset = ctx->ctx_pmds[i].lval;
  4428. ovfl_arg->pmd_eventid = ctx->ctx_pmds[i].eventid;
  4429. /*
  4430. * copy values of pmds of interest. Sampling format may copy them
  4431. * into sampling buffer.
  4432. */
  4433. if (smpl_pmds) {
  4434. for(j=0, k=0; smpl_pmds; j++, smpl_pmds >>=1) {
  4435. if ((smpl_pmds & 0x1) == 0) continue;
  4436. ovfl_arg->smpl_pmds_values[k++] = PMD_IS_COUNTING(j) ? pfm_read_soft_counter(ctx, j) : ia64_get_pmd(j);
  4437. DPRINT_ovfl(("smpl_pmd[%d]=pmd%u=0x%lx\n", k-1, j, ovfl_arg->smpl_pmds_values[k-1]));
  4438. }
  4439. }
  4440. pfm_stats[this_cpu].pfm_smpl_handler_calls++;
  4441. start_cycles = ia64_get_itc();
  4442. /*
  4443. * call custom buffer format record (handler) routine
  4444. */
  4445. ret = (*ctx->ctx_buf_fmt->fmt_handler)(task, ctx->ctx_smpl_hdr, ovfl_arg, regs, tstamp);
  4446. end_cycles = ia64_get_itc();
  4447. /*
  4448. * For those controls, we take the union because they have
  4449. * an all or nothing behavior.
  4450. */
  4451. ovfl_ctrl.bits.notify_user |= ovfl_arg->ovfl_ctrl.bits.notify_user;
  4452. ovfl_ctrl.bits.block_task |= ovfl_arg->ovfl_ctrl.bits.block_task;
  4453. ovfl_ctrl.bits.mask_monitoring |= ovfl_arg->ovfl_ctrl.bits.mask_monitoring;
  4454. /*
  4455. * build the bitmask of pmds to reset now
  4456. */
  4457. if (ovfl_arg->ovfl_ctrl.bits.reset_ovfl_pmds) reset_pmds |= mask;
  4458. pfm_stats[this_cpu].pfm_smpl_handler_cycles += end_cycles - start_cycles;
  4459. }
  4460. /*
  4461. * when the module cannot handle the rest of the overflows, we abort right here
  4462. */
  4463. if (ret && pmd_mask) {
  4464. DPRINT(("handler aborts leftover ovfl_pmds=0x%lx\n",
  4465. pmd_mask<<PMU_FIRST_COUNTER));
  4466. }
  4467. /*
  4468. * remove the pmds we reset now from the set of pmds to reset in pfm_restart()
  4469. */
  4470. ovfl_pmds &= ~reset_pmds;
  4471. } else {
  4472. /*
  4473. * when no sampling module is used, then the default
  4474. * is to notify on overflow if requested by user
  4475. */
  4476. ovfl_ctrl.bits.notify_user = ovfl_notify ? 1 : 0;
  4477. ovfl_ctrl.bits.block_task = ovfl_notify ? 1 : 0;
  4478. ovfl_ctrl.bits.mask_monitoring = ovfl_notify ? 1 : 0; /* XXX: change for saturation */
  4479. ovfl_ctrl.bits.reset_ovfl_pmds = ovfl_notify ? 0 : 1;
  4480. /*
  4481. * if needed, we reset all overflowed pmds
  4482. */
  4483. if (ovfl_notify == 0) reset_pmds = ovfl_pmds;
  4484. }
  4485. DPRINT_ovfl(("ovfl_pmds=0x%lx reset_pmds=0x%lx\n", ovfl_pmds, reset_pmds));
  4486. /*
  4487. * reset the requested PMD registers using the short reset values
  4488. */
  4489. if (reset_pmds) {
  4490. unsigned long bm = reset_pmds;
  4491. pfm_reset_regs(ctx, &bm, PFM_PMD_SHORT_RESET);
  4492. }
  4493. if (ovfl_notify && ovfl_ctrl.bits.notify_user) {
  4494. /*
  4495. * keep track of what to reset when unblocking
  4496. */
  4497. ctx->ctx_ovfl_regs[0] = ovfl_pmds;
  4498. /*
  4499. * check for blocking context
  4500. */
  4501. if (CTX_OVFL_NOBLOCK(ctx) == 0 && ovfl_ctrl.bits.block_task) {
  4502. ctx->ctx_fl_trap_reason = PFM_TRAP_REASON_BLOCK;
  4503. /*
  4504. * set the perfmon specific checking pending work for the task
  4505. */
  4506. PFM_SET_WORK_PENDING(task, 1);
  4507. /*
  4508. * when coming from ctxsw, current still points to the
  4509. * previous task, therefore we must work with task and not current.
  4510. */
  4511. pfm_set_task_notify(task);
  4512. }
  4513. /*
  4514. * defer until state is changed (shorten spin window). the context is locked
  4515. * anyway, so the signal receiver would come spin for nothing.
  4516. */
  4517. must_notify = 1;
  4518. }
  4519. DPRINT_ovfl(("owner [%d] pending=%ld reason=%u ovfl_pmds=0x%lx ovfl_notify=0x%lx masked=%d\n",
  4520. GET_PMU_OWNER() ? GET_PMU_OWNER()->pid : -1,
  4521. PFM_GET_WORK_PENDING(task),
  4522. ctx->ctx_fl_trap_reason,
  4523. ovfl_pmds,
  4524. ovfl_notify,
  4525. ovfl_ctrl.bits.mask_monitoring ? 1 : 0));
  4526. /*
  4527. * in case monitoring must be stopped, we toggle the psr bits
  4528. */
  4529. if (ovfl_ctrl.bits.mask_monitoring) {
  4530. pfm_mask_monitoring(task);
  4531. ctx->ctx_state = PFM_CTX_MASKED;
  4532. ctx->ctx_fl_can_restart = 1;
  4533. }
  4534. /*
  4535. * send notification now
  4536. */
  4537. if (must_notify) pfm_ovfl_notify_user(ctx, ovfl_notify);
  4538. return;
  4539. sanity_check:
  4540. printk(KERN_ERR "perfmon: CPU%d overflow handler [%d] pmc0=0x%lx\n",
  4541. smp_processor_id(),
  4542. task ? task->pid : -1,
  4543. pmc0);
  4544. return;
  4545. stop_monitoring:
  4546. /*
  4547. * in SMP, zombie context is never restored but reclaimed in pfm_load_regs().
  4548. * Moreover, zombies are also reclaimed in pfm_save_regs(). Therefore we can
  4549. * come here as zombie only if the task is the current task. In which case, we
  4550. * can access the PMU hardware directly.
  4551. *
  4552. * Note that zombies do have PM_VALID set. So here we do the minimal.
  4553. *
  4554. * In case the context was zombified it could not be reclaimed at the time
  4555. * the monitoring program exited. At this point, the PMU reservation has been
  4556. * returned, the sampiing buffer has been freed. We must convert this call
  4557. * into a spurious interrupt. However, we must also avoid infinite overflows
  4558. * by stopping monitoring for this task. We can only come here for a per-task
  4559. * context. All we need to do is to stop monitoring using the psr bits which
  4560. * are always task private. By re-enabling secure montioring, we ensure that
  4561. * the monitored task will not be able to re-activate monitoring.
  4562. * The task will eventually be context switched out, at which point the context
  4563. * will be reclaimed (that includes releasing ownership of the PMU).
  4564. *
  4565. * So there might be a window of time where the number of per-task session is zero
  4566. * yet one PMU might have a owner and get at most one overflow interrupt for a zombie
  4567. * context. This is safe because if a per-task session comes in, it will push this one
  4568. * out and by the virtue on pfm_save_regs(), this one will disappear. If a system wide
  4569. * session is force on that CPU, given that we use task pinning, pfm_save_regs() will
  4570. * also push our zombie context out.
  4571. *
  4572. * Overall pretty hairy stuff....
  4573. */
  4574. DPRINT(("ctx is zombie for [%d], converted to spurious\n", task ? task->pid: -1));
  4575. pfm_clear_psr_up();
  4576. ia64_psr(regs)->up = 0;
  4577. ia64_psr(regs)->sp = 1;
  4578. return;
  4579. }
  4580. static int
  4581. pfm_do_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
  4582. {
  4583. struct task_struct *task;
  4584. pfm_context_t *ctx;
  4585. unsigned long flags;
  4586. u64 pmc0;
  4587. int this_cpu = smp_processor_id();
  4588. int retval = 0;
  4589. pfm_stats[this_cpu].pfm_ovfl_intr_count++;
  4590. /*
  4591. * srlz.d done before arriving here
  4592. */
  4593. pmc0 = ia64_get_pmc(0);
  4594. task = GET_PMU_OWNER();
  4595. ctx = GET_PMU_CTX();
  4596. /*
  4597. * if we have some pending bits set
  4598. * assumes : if any PMC0.bit[63-1] is set, then PMC0.fr = 1
  4599. */
  4600. if (PMC0_HAS_OVFL(pmc0) && task) {
  4601. /*
  4602. * we assume that pmc0.fr is always set here
  4603. */
  4604. /* sanity check */
  4605. if (!ctx) goto report_spurious1;
  4606. if (ctx->ctx_fl_system == 0 && (task->thread.flags & IA64_THREAD_PM_VALID) == 0)
  4607. goto report_spurious2;
  4608. PROTECT_CTX_NOPRINT(ctx, flags);
  4609. pfm_overflow_handler(task, ctx, pmc0, regs);
  4610. UNPROTECT_CTX_NOPRINT(ctx, flags);
  4611. } else {
  4612. pfm_stats[this_cpu].pfm_spurious_ovfl_intr_count++;
  4613. retval = -1;
  4614. }
  4615. /*
  4616. * keep it unfrozen at all times
  4617. */
  4618. pfm_unfreeze_pmu();
  4619. return retval;
  4620. report_spurious1:
  4621. printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d has no PFM context\n",
  4622. this_cpu, task->pid);
  4623. pfm_unfreeze_pmu();
  4624. return -1;
  4625. report_spurious2:
  4626. printk(KERN_INFO "perfmon: spurious overflow interrupt on CPU%d: process %d, invalid flag\n",
  4627. this_cpu,
  4628. task->pid);
  4629. pfm_unfreeze_pmu();
  4630. return -1;
  4631. }
  4632. static irqreturn_t
  4633. pfm_interrupt_handler(int irq, void *arg, struct pt_regs *regs)
  4634. {
  4635. unsigned long start_cycles, total_cycles;
  4636. unsigned long min, max;
  4637. int this_cpu;
  4638. int ret;
  4639. this_cpu = get_cpu();
  4640. if (likely(!pfm_alt_intr_handler)) {
  4641. min = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min;
  4642. max = pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max;
  4643. start_cycles = ia64_get_itc();
  4644. ret = pfm_do_interrupt_handler(irq, arg, regs);
  4645. total_cycles = ia64_get_itc();
  4646. /*
  4647. * don't measure spurious interrupts
  4648. */
  4649. if (likely(ret == 0)) {
  4650. total_cycles -= start_cycles;
  4651. if (total_cycles < min) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_min = total_cycles;
  4652. if (total_cycles > max) pfm_stats[this_cpu].pfm_ovfl_intr_cycles_max = total_cycles;
  4653. pfm_stats[this_cpu].pfm_ovfl_intr_cycles += total_cycles;
  4654. }
  4655. }
  4656. else {
  4657. (*pfm_alt_intr_handler->handler)(irq, arg, regs);
  4658. }
  4659. put_cpu_no_resched();
  4660. return IRQ_HANDLED;
  4661. }
  4662. /*
  4663. * /proc/perfmon interface, for debug only
  4664. */
  4665. #define PFM_PROC_SHOW_HEADER ((void *)NR_CPUS+1)
  4666. static void *
  4667. pfm_proc_start(struct seq_file *m, loff_t *pos)
  4668. {
  4669. if (*pos == 0) {
  4670. return PFM_PROC_SHOW_HEADER;
  4671. }
  4672. while (*pos <= NR_CPUS) {
  4673. if (cpu_online(*pos - 1)) {
  4674. return (void *)*pos;
  4675. }
  4676. ++*pos;
  4677. }
  4678. return NULL;
  4679. }
  4680. static void *
  4681. pfm_proc_next(struct seq_file *m, void *v, loff_t *pos)
  4682. {
  4683. ++*pos;
  4684. return pfm_proc_start(m, pos);
  4685. }
  4686. static void
  4687. pfm_proc_stop(struct seq_file *m, void *v)
  4688. {
  4689. }
  4690. static void
  4691. pfm_proc_show_header(struct seq_file *m)
  4692. {
  4693. struct list_head * pos;
  4694. pfm_buffer_fmt_t * entry;
  4695. unsigned long flags;
  4696. seq_printf(m,
  4697. "perfmon version : %u.%u\n"
  4698. "model : %s\n"
  4699. "fastctxsw : %s\n"
  4700. "expert mode : %s\n"
  4701. "ovfl_mask : 0x%lx\n"
  4702. "PMU flags : 0x%x\n",
  4703. PFM_VERSION_MAJ, PFM_VERSION_MIN,
  4704. pmu_conf->pmu_name,
  4705. pfm_sysctl.fastctxsw > 0 ? "Yes": "No",
  4706. pfm_sysctl.expert_mode > 0 ? "Yes": "No",
  4707. pmu_conf->ovfl_val,
  4708. pmu_conf->flags);
  4709. LOCK_PFS(flags);
  4710. seq_printf(m,
  4711. "proc_sessions : %u\n"
  4712. "sys_sessions : %u\n"
  4713. "sys_use_dbregs : %u\n"
  4714. "ptrace_use_dbregs : %u\n",
  4715. pfm_sessions.pfs_task_sessions,
  4716. pfm_sessions.pfs_sys_sessions,
  4717. pfm_sessions.pfs_sys_use_dbregs,
  4718. pfm_sessions.pfs_ptrace_use_dbregs);
  4719. UNLOCK_PFS(flags);
  4720. spin_lock(&pfm_buffer_fmt_lock);
  4721. list_for_each(pos, &pfm_buffer_fmt_list) {
  4722. entry = list_entry(pos, pfm_buffer_fmt_t, fmt_list);
  4723. seq_printf(m, "format : %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x %s\n",
  4724. entry->fmt_uuid[0],
  4725. entry->fmt_uuid[1],
  4726. entry->fmt_uuid[2],
  4727. entry->fmt_uuid[3],
  4728. entry->fmt_uuid[4],
  4729. entry->fmt_uuid[5],
  4730. entry->fmt_uuid[6],
  4731. entry->fmt_uuid[7],
  4732. entry->fmt_uuid[8],
  4733. entry->fmt_uuid[9],
  4734. entry->fmt_uuid[10],
  4735. entry->fmt_uuid[11],
  4736. entry->fmt_uuid[12],
  4737. entry->fmt_uuid[13],
  4738. entry->fmt_uuid[14],
  4739. entry->fmt_uuid[15],
  4740. entry->fmt_name);
  4741. }
  4742. spin_unlock(&pfm_buffer_fmt_lock);
  4743. }
  4744. static int
  4745. pfm_proc_show(struct seq_file *m, void *v)
  4746. {
  4747. unsigned long psr;
  4748. unsigned int i;
  4749. int cpu;
  4750. if (v == PFM_PROC_SHOW_HEADER) {
  4751. pfm_proc_show_header(m);
  4752. return 0;
  4753. }
  4754. /* show info for CPU (v - 1) */
  4755. cpu = (long)v - 1;
  4756. seq_printf(m,
  4757. "CPU%-2d overflow intrs : %lu\n"
  4758. "CPU%-2d overflow cycles : %lu\n"
  4759. "CPU%-2d overflow min : %lu\n"
  4760. "CPU%-2d overflow max : %lu\n"
  4761. "CPU%-2d smpl handler calls : %lu\n"
  4762. "CPU%-2d smpl handler cycles : %lu\n"
  4763. "CPU%-2d spurious intrs : %lu\n"
  4764. "CPU%-2d replay intrs : %lu\n"
  4765. "CPU%-2d syst_wide : %d\n"
  4766. "CPU%-2d dcr_pp : %d\n"
  4767. "CPU%-2d exclude idle : %d\n"
  4768. "CPU%-2d owner : %d\n"
  4769. "CPU%-2d context : %p\n"
  4770. "CPU%-2d activations : %lu\n",
  4771. cpu, pfm_stats[cpu].pfm_ovfl_intr_count,
  4772. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles,
  4773. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_min,
  4774. cpu, pfm_stats[cpu].pfm_ovfl_intr_cycles_max,
  4775. cpu, pfm_stats[cpu].pfm_smpl_handler_calls,
  4776. cpu, pfm_stats[cpu].pfm_smpl_handler_cycles,
  4777. cpu, pfm_stats[cpu].pfm_spurious_ovfl_intr_count,
  4778. cpu, pfm_stats[cpu].pfm_replay_ovfl_intr_count,
  4779. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_SYST_WIDE ? 1 : 0,
  4780. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_DCR_PP ? 1 : 0,
  4781. cpu, pfm_get_cpu_data(pfm_syst_info, cpu) & PFM_CPUINFO_EXCL_IDLE ? 1 : 0,
  4782. cpu, pfm_get_cpu_data(pmu_owner, cpu) ? pfm_get_cpu_data(pmu_owner, cpu)->pid: -1,
  4783. cpu, pfm_get_cpu_data(pmu_ctx, cpu),
  4784. cpu, pfm_get_cpu_data(pmu_activation_number, cpu));
  4785. if (num_online_cpus() == 1 && pfm_sysctl.debug > 0) {
  4786. psr = pfm_get_psr();
  4787. ia64_srlz_d();
  4788. seq_printf(m,
  4789. "CPU%-2d psr : 0x%lx\n"
  4790. "CPU%-2d pmc0 : 0x%lx\n",
  4791. cpu, psr,
  4792. cpu, ia64_get_pmc(0));
  4793. for (i=0; PMC_IS_LAST(i) == 0; i++) {
  4794. if (PMC_IS_COUNTING(i) == 0) continue;
  4795. seq_printf(m,
  4796. "CPU%-2d pmc%u : 0x%lx\n"
  4797. "CPU%-2d pmd%u : 0x%lx\n",
  4798. cpu, i, ia64_get_pmc(i),
  4799. cpu, i, ia64_get_pmd(i));
  4800. }
  4801. }
  4802. return 0;
  4803. }
  4804. struct seq_operations pfm_seq_ops = {
  4805. .start = pfm_proc_start,
  4806. .next = pfm_proc_next,
  4807. .stop = pfm_proc_stop,
  4808. .show = pfm_proc_show
  4809. };
  4810. static int
  4811. pfm_proc_open(struct inode *inode, struct file *file)
  4812. {
  4813. return seq_open(file, &pfm_seq_ops);
  4814. }
  4815. /*
  4816. * we come here as soon as local_cpu_data->pfm_syst_wide is set. this happens
  4817. * during pfm_enable() hence before pfm_start(). We cannot assume monitoring
  4818. * is active or inactive based on mode. We must rely on the value in
  4819. * local_cpu_data->pfm_syst_info
  4820. */
  4821. void
  4822. pfm_syst_wide_update_task(struct task_struct *task, unsigned long info, int is_ctxswin)
  4823. {
  4824. struct pt_regs *regs;
  4825. unsigned long dcr;
  4826. unsigned long dcr_pp;
  4827. dcr_pp = info & PFM_CPUINFO_DCR_PP ? 1 : 0;
  4828. /*
  4829. * pid 0 is guaranteed to be the idle task. There is one such task with pid 0
  4830. * on every CPU, so we can rely on the pid to identify the idle task.
  4831. */
  4832. if ((info & PFM_CPUINFO_EXCL_IDLE) == 0 || task->pid) {
  4833. regs = ia64_task_regs(task);
  4834. ia64_psr(regs)->pp = is_ctxswin ? dcr_pp : 0;
  4835. return;
  4836. }
  4837. /*
  4838. * if monitoring has started
  4839. */
  4840. if (dcr_pp) {
  4841. dcr = ia64_getreg(_IA64_REG_CR_DCR);
  4842. /*
  4843. * context switching in?
  4844. */
  4845. if (is_ctxswin) {
  4846. /* mask monitoring for the idle task */
  4847. ia64_setreg(_IA64_REG_CR_DCR, dcr & ~IA64_DCR_PP);
  4848. pfm_clear_psr_pp();
  4849. ia64_srlz_i();
  4850. return;
  4851. }
  4852. /*
  4853. * context switching out
  4854. * restore monitoring for next task
  4855. *
  4856. * Due to inlining this odd if-then-else construction generates
  4857. * better code.
  4858. */
  4859. ia64_setreg(_IA64_REG_CR_DCR, dcr |IA64_DCR_PP);
  4860. pfm_set_psr_pp();
  4861. ia64_srlz_i();
  4862. }
  4863. }
  4864. #ifdef CONFIG_SMP
  4865. static void
  4866. pfm_force_cleanup(pfm_context_t *ctx, struct pt_regs *regs)
  4867. {
  4868. struct task_struct *task = ctx->ctx_task;
  4869. ia64_psr(regs)->up = 0;
  4870. ia64_psr(regs)->sp = 1;
  4871. if (GET_PMU_OWNER() == task) {
  4872. DPRINT(("cleared ownership for [%d]\n", ctx->ctx_task->pid));
  4873. SET_PMU_OWNER(NULL, NULL);
  4874. }
  4875. /*
  4876. * disconnect the task from the context and vice-versa
  4877. */
  4878. PFM_SET_WORK_PENDING(task, 0);
  4879. task->thread.pfm_context = NULL;
  4880. task->thread.flags &= ~IA64_THREAD_PM_VALID;
  4881. DPRINT(("force cleanup for [%d]\n", task->pid));
  4882. }
  4883. /*
  4884. * in 2.6, interrupts are masked when we come here and the runqueue lock is held
  4885. */
  4886. void
  4887. pfm_save_regs(struct task_struct *task)
  4888. {
  4889. pfm_context_t *ctx;
  4890. struct thread_struct *t;
  4891. unsigned long flags;
  4892. u64 psr;
  4893. ctx = PFM_GET_CTX(task);
  4894. if (ctx == NULL) return;
  4895. t = &task->thread;
  4896. /*
  4897. * we always come here with interrupts ALREADY disabled by
  4898. * the scheduler. So we simply need to protect against concurrent
  4899. * access, not CPU concurrency.
  4900. */
  4901. flags = pfm_protect_ctx_ctxsw(ctx);
  4902. if (ctx->ctx_state == PFM_CTX_ZOMBIE) {
  4903. struct pt_regs *regs = ia64_task_regs(task);
  4904. pfm_clear_psr_up();
  4905. pfm_force_cleanup(ctx, regs);
  4906. BUG_ON(ctx->ctx_smpl_hdr);
  4907. pfm_unprotect_ctx_ctxsw(ctx, flags);
  4908. pfm_context_free(ctx);
  4909. return;
  4910. }
  4911. /*
  4912. * save current PSR: needed because we modify it
  4913. */
  4914. ia64_srlz_d();
  4915. psr = pfm_get_psr();
  4916. BUG_ON(psr & (IA64_PSR_I));
  4917. /*
  4918. * stop monitoring:
  4919. * This is the last instruction which may generate an overflow
  4920. *
  4921. * We do not need to set psr.sp because, it is irrelevant in kernel.
  4922. * It will be restored from ipsr when going back to user level
  4923. */
  4924. pfm_clear_psr_up();
  4925. /*
  4926. * keep a copy of psr.up (for reload)
  4927. */
  4928. ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
  4929. /*
  4930. * release ownership of this PMU.
  4931. * PM interrupts are masked, so nothing
  4932. * can happen.
  4933. */
  4934. SET_PMU_OWNER(NULL, NULL);
  4935. /*
  4936. * we systematically save the PMD as we have no
  4937. * guarantee we will be schedule at that same
  4938. * CPU again.
  4939. */
  4940. pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
  4941. /*
  4942. * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
  4943. * we will need it on the restore path to check
  4944. * for pending overflow.
  4945. */
  4946. t->pmcs[0] = ia64_get_pmc(0);
  4947. /*
  4948. * unfreeze PMU if had pending overflows
  4949. */
  4950. if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
  4951. /*
  4952. * finally, allow context access.
  4953. * interrupts will still be masked after this call.
  4954. */
  4955. pfm_unprotect_ctx_ctxsw(ctx, flags);
  4956. }
  4957. #else /* !CONFIG_SMP */
  4958. void
  4959. pfm_save_regs(struct task_struct *task)
  4960. {
  4961. pfm_context_t *ctx;
  4962. u64 psr;
  4963. ctx = PFM_GET_CTX(task);
  4964. if (ctx == NULL) return;
  4965. /*
  4966. * save current PSR: needed because we modify it
  4967. */
  4968. psr = pfm_get_psr();
  4969. BUG_ON(psr & (IA64_PSR_I));
  4970. /*
  4971. * stop monitoring:
  4972. * This is the last instruction which may generate an overflow
  4973. *
  4974. * We do not need to set psr.sp because, it is irrelevant in kernel.
  4975. * It will be restored from ipsr when going back to user level
  4976. */
  4977. pfm_clear_psr_up();
  4978. /*
  4979. * keep a copy of psr.up (for reload)
  4980. */
  4981. ctx->ctx_saved_psr_up = psr & IA64_PSR_UP;
  4982. }
  4983. static void
  4984. pfm_lazy_save_regs (struct task_struct *task)
  4985. {
  4986. pfm_context_t *ctx;
  4987. struct thread_struct *t;
  4988. unsigned long flags;
  4989. { u64 psr = pfm_get_psr();
  4990. BUG_ON(psr & IA64_PSR_UP);
  4991. }
  4992. ctx = PFM_GET_CTX(task);
  4993. t = &task->thread;
  4994. /*
  4995. * we need to mask PMU overflow here to
  4996. * make sure that we maintain pmc0 until
  4997. * we save it. overflow interrupts are
  4998. * treated as spurious if there is no
  4999. * owner.
  5000. *
  5001. * XXX: I don't think this is necessary
  5002. */
  5003. PROTECT_CTX(ctx,flags);
  5004. /*
  5005. * release ownership of this PMU.
  5006. * must be done before we save the registers.
  5007. *
  5008. * after this call any PMU interrupt is treated
  5009. * as spurious.
  5010. */
  5011. SET_PMU_OWNER(NULL, NULL);
  5012. /*
  5013. * save all the pmds we use
  5014. */
  5015. pfm_save_pmds(t->pmds, ctx->ctx_used_pmds[0]);
  5016. /*
  5017. * save pmc0 ia64_srlz_d() done in pfm_save_pmds()
  5018. * it is needed to check for pended overflow
  5019. * on the restore path
  5020. */
  5021. t->pmcs[0] = ia64_get_pmc(0);
  5022. /*
  5023. * unfreeze PMU if had pending overflows
  5024. */
  5025. if (t->pmcs[0] & ~0x1UL) pfm_unfreeze_pmu();
  5026. /*
  5027. * now get can unmask PMU interrupts, they will
  5028. * be treated as purely spurious and we will not
  5029. * lose any information
  5030. */
  5031. UNPROTECT_CTX(ctx,flags);
  5032. }
  5033. #endif /* CONFIG_SMP */
  5034. #ifdef CONFIG_SMP
  5035. /*
  5036. * in 2.6, interrupts are masked when we come here and the runqueue lock is held
  5037. */
  5038. void
  5039. pfm_load_regs (struct task_struct *task)
  5040. {
  5041. pfm_context_t *ctx;
  5042. struct thread_struct *t;
  5043. unsigned long pmc_mask = 0UL, pmd_mask = 0UL;
  5044. unsigned long flags;
  5045. u64 psr, psr_up;
  5046. int need_irq_resend;
  5047. ctx = PFM_GET_CTX(task);
  5048. if (unlikely(ctx == NULL)) return;
  5049. BUG_ON(GET_PMU_OWNER());
  5050. t = &task->thread;
  5051. /*
  5052. * possible on unload
  5053. */
  5054. if (unlikely((t->flags & IA64_THREAD_PM_VALID) == 0)) return;
  5055. /*
  5056. * we always come here with interrupts ALREADY disabled by
  5057. * the scheduler. So we simply need to protect against concurrent
  5058. * access, not CPU concurrency.
  5059. */
  5060. flags = pfm_protect_ctx_ctxsw(ctx);
  5061. psr = pfm_get_psr();
  5062. need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
  5063. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  5064. BUG_ON(psr & IA64_PSR_I);
  5065. if (unlikely(ctx->ctx_state == PFM_CTX_ZOMBIE)) {
  5066. struct pt_regs *regs = ia64_task_regs(task);
  5067. BUG_ON(ctx->ctx_smpl_hdr);
  5068. pfm_force_cleanup(ctx, regs);
  5069. pfm_unprotect_ctx_ctxsw(ctx, flags);
  5070. /*
  5071. * this one (kmalloc'ed) is fine with interrupts disabled
  5072. */
  5073. pfm_context_free(ctx);
  5074. return;
  5075. }
  5076. /*
  5077. * we restore ALL the debug registers to avoid picking up
  5078. * stale state.
  5079. */
  5080. if (ctx->ctx_fl_using_dbreg) {
  5081. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  5082. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  5083. }
  5084. /*
  5085. * retrieve saved psr.up
  5086. */
  5087. psr_up = ctx->ctx_saved_psr_up;
  5088. /*
  5089. * if we were the last user of the PMU on that CPU,
  5090. * then nothing to do except restore psr
  5091. */
  5092. if (GET_LAST_CPU(ctx) == smp_processor_id() && ctx->ctx_last_activation == GET_ACTIVATION()) {
  5093. /*
  5094. * retrieve partial reload masks (due to user modifications)
  5095. */
  5096. pmc_mask = ctx->ctx_reload_pmcs[0];
  5097. pmd_mask = ctx->ctx_reload_pmds[0];
  5098. } else {
  5099. /*
  5100. * To avoid leaking information to the user level when psr.sp=0,
  5101. * we must reload ALL implemented pmds (even the ones we don't use).
  5102. * In the kernel we only allow PFM_READ_PMDS on registers which
  5103. * we initialized or requested (sampling) so there is no risk there.
  5104. */
  5105. pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
  5106. /*
  5107. * ALL accessible PMCs are systematically reloaded, unused registers
  5108. * get their default (from pfm_reset_pmu_state()) values to avoid picking
  5109. * up stale configuration.
  5110. *
  5111. * PMC0 is never in the mask. It is always restored separately.
  5112. */
  5113. pmc_mask = ctx->ctx_all_pmcs[0];
  5114. }
  5115. /*
  5116. * when context is MASKED, we will restore PMC with plm=0
  5117. * and PMD with stale information, but that's ok, nothing
  5118. * will be captured.
  5119. *
  5120. * XXX: optimize here
  5121. */
  5122. if (pmd_mask) pfm_restore_pmds(t->pmds, pmd_mask);
  5123. if (pmc_mask) pfm_restore_pmcs(t->pmcs, pmc_mask);
  5124. /*
  5125. * check for pending overflow at the time the state
  5126. * was saved.
  5127. */
  5128. if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
  5129. /*
  5130. * reload pmc0 with the overflow information
  5131. * On McKinley PMU, this will trigger a PMU interrupt
  5132. */
  5133. ia64_set_pmc(0, t->pmcs[0]);
  5134. ia64_srlz_d();
  5135. t->pmcs[0] = 0UL;
  5136. /*
  5137. * will replay the PMU interrupt
  5138. */
  5139. if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
  5140. pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
  5141. }
  5142. /*
  5143. * we just did a reload, so we reset the partial reload fields
  5144. */
  5145. ctx->ctx_reload_pmcs[0] = 0UL;
  5146. ctx->ctx_reload_pmds[0] = 0UL;
  5147. SET_LAST_CPU(ctx, smp_processor_id());
  5148. /*
  5149. * dump activation value for this PMU
  5150. */
  5151. INC_ACTIVATION();
  5152. /*
  5153. * record current activation for this context
  5154. */
  5155. SET_ACTIVATION(ctx);
  5156. /*
  5157. * establish new ownership.
  5158. */
  5159. SET_PMU_OWNER(task, ctx);
  5160. /*
  5161. * restore the psr.up bit. measurement
  5162. * is active again.
  5163. * no PMU interrupt can happen at this point
  5164. * because we still have interrupts disabled.
  5165. */
  5166. if (likely(psr_up)) pfm_set_psr_up();
  5167. /*
  5168. * allow concurrent access to context
  5169. */
  5170. pfm_unprotect_ctx_ctxsw(ctx, flags);
  5171. }
  5172. #else /* !CONFIG_SMP */
  5173. /*
  5174. * reload PMU state for UP kernels
  5175. * in 2.5 we come here with interrupts disabled
  5176. */
  5177. void
  5178. pfm_load_regs (struct task_struct *task)
  5179. {
  5180. struct thread_struct *t;
  5181. pfm_context_t *ctx;
  5182. struct task_struct *owner;
  5183. unsigned long pmd_mask, pmc_mask;
  5184. u64 psr, psr_up;
  5185. int need_irq_resend;
  5186. owner = GET_PMU_OWNER();
  5187. ctx = PFM_GET_CTX(task);
  5188. t = &task->thread;
  5189. psr = pfm_get_psr();
  5190. BUG_ON(psr & (IA64_PSR_UP|IA64_PSR_PP));
  5191. BUG_ON(psr & IA64_PSR_I);
  5192. /*
  5193. * we restore ALL the debug registers to avoid picking up
  5194. * stale state.
  5195. *
  5196. * This must be done even when the task is still the owner
  5197. * as the registers may have been modified via ptrace()
  5198. * (not perfmon) by the previous task.
  5199. */
  5200. if (ctx->ctx_fl_using_dbreg) {
  5201. pfm_restore_ibrs(ctx->ctx_ibrs, pmu_conf->num_ibrs);
  5202. pfm_restore_dbrs(ctx->ctx_dbrs, pmu_conf->num_dbrs);
  5203. }
  5204. /*
  5205. * retrieved saved psr.up
  5206. */
  5207. psr_up = ctx->ctx_saved_psr_up;
  5208. need_irq_resend = pmu_conf->flags & PFM_PMU_IRQ_RESEND;
  5209. /*
  5210. * short path, our state is still there, just
  5211. * need to restore psr and we go
  5212. *
  5213. * we do not touch either PMC nor PMD. the psr is not touched
  5214. * by the overflow_handler. So we are safe w.r.t. to interrupt
  5215. * concurrency even without interrupt masking.
  5216. */
  5217. if (likely(owner == task)) {
  5218. if (likely(psr_up)) pfm_set_psr_up();
  5219. return;
  5220. }
  5221. /*
  5222. * someone else is still using the PMU, first push it out and
  5223. * then we'll be able to install our stuff !
  5224. *
  5225. * Upon return, there will be no owner for the current PMU
  5226. */
  5227. if (owner) pfm_lazy_save_regs(owner);
  5228. /*
  5229. * To avoid leaking information to the user level when psr.sp=0,
  5230. * we must reload ALL implemented pmds (even the ones we don't use).
  5231. * In the kernel we only allow PFM_READ_PMDS on registers which
  5232. * we initialized or requested (sampling) so there is no risk there.
  5233. */
  5234. pmd_mask = pfm_sysctl.fastctxsw ? ctx->ctx_used_pmds[0] : ctx->ctx_all_pmds[0];
  5235. /*
  5236. * ALL accessible PMCs are systematically reloaded, unused registers
  5237. * get their default (from pfm_reset_pmu_state()) values to avoid picking
  5238. * up stale configuration.
  5239. *
  5240. * PMC0 is never in the mask. It is always restored separately
  5241. */
  5242. pmc_mask = ctx->ctx_all_pmcs[0];
  5243. pfm_restore_pmds(t->pmds, pmd_mask);
  5244. pfm_restore_pmcs(t->pmcs, pmc_mask);
  5245. /*
  5246. * check for pending overflow at the time the state
  5247. * was saved.
  5248. */
  5249. if (unlikely(PMC0_HAS_OVFL(t->pmcs[0]))) {
  5250. /*
  5251. * reload pmc0 with the overflow information
  5252. * On McKinley PMU, this will trigger a PMU interrupt
  5253. */
  5254. ia64_set_pmc(0, t->pmcs[0]);
  5255. ia64_srlz_d();
  5256. t->pmcs[0] = 0UL;
  5257. /*
  5258. * will replay the PMU interrupt
  5259. */
  5260. if (need_irq_resend) hw_resend_irq(NULL, IA64_PERFMON_VECTOR);
  5261. pfm_stats[smp_processor_id()].pfm_replay_ovfl_intr_count++;
  5262. }
  5263. /*
  5264. * establish new ownership.
  5265. */
  5266. SET_PMU_OWNER(task, ctx);
  5267. /*
  5268. * restore the psr.up bit. measurement
  5269. * is active again.
  5270. * no PMU interrupt can happen at this point
  5271. * because we still have interrupts disabled.
  5272. */
  5273. if (likely(psr_up)) pfm_set_psr_up();
  5274. }
  5275. #endif /* CONFIG_SMP */
  5276. /*
  5277. * this function assumes monitoring is stopped
  5278. */
  5279. static void
  5280. pfm_flush_pmds(struct task_struct *task, pfm_context_t *ctx)
  5281. {
  5282. u64 pmc0;
  5283. unsigned long mask2, val, pmd_val, ovfl_val;
  5284. int i, can_access_pmu = 0;
  5285. int is_self;
  5286. /*
  5287. * is the caller the task being monitored (or which initiated the
  5288. * session for system wide measurements)
  5289. */
  5290. is_self = ctx->ctx_task == task ? 1 : 0;
  5291. /*
  5292. * can access PMU is task is the owner of the PMU state on the current CPU
  5293. * or if we are running on the CPU bound to the context in system-wide mode
  5294. * (that is not necessarily the task the context is attached to in this mode).
  5295. * In system-wide we always have can_access_pmu true because a task running on an
  5296. * invalid processor is flagged earlier in the call stack (see pfm_stop).
  5297. */
  5298. can_access_pmu = (GET_PMU_OWNER() == task) || (ctx->ctx_fl_system && ctx->ctx_cpu == smp_processor_id());
  5299. if (can_access_pmu) {
  5300. /*
  5301. * Mark the PMU as not owned
  5302. * This will cause the interrupt handler to do nothing in case an overflow
  5303. * interrupt was in-flight
  5304. * This also guarantees that pmc0 will contain the final state
  5305. * It virtually gives us full control on overflow processing from that point
  5306. * on.
  5307. */
  5308. SET_PMU_OWNER(NULL, NULL);
  5309. DPRINT(("releasing ownership\n"));
  5310. /*
  5311. * read current overflow status:
  5312. *
  5313. * we are guaranteed to read the final stable state
  5314. */
  5315. ia64_srlz_d();
  5316. pmc0 = ia64_get_pmc(0); /* slow */
  5317. /*
  5318. * reset freeze bit, overflow status information destroyed
  5319. */
  5320. pfm_unfreeze_pmu();
  5321. } else {
  5322. pmc0 = task->thread.pmcs[0];
  5323. /*
  5324. * clear whatever overflow status bits there were
  5325. */
  5326. task->thread.pmcs[0] = 0;
  5327. }
  5328. ovfl_val = pmu_conf->ovfl_val;
  5329. /*
  5330. * we save all the used pmds
  5331. * we take care of overflows for counting PMDs
  5332. *
  5333. * XXX: sampling situation is not taken into account here
  5334. */
  5335. mask2 = ctx->ctx_used_pmds[0];
  5336. DPRINT(("is_self=%d ovfl_val=0x%lx mask2=0x%lx\n", is_self, ovfl_val, mask2));
  5337. for (i = 0; mask2; i++, mask2>>=1) {
  5338. /* skip non used pmds */
  5339. if ((mask2 & 0x1) == 0) continue;
  5340. /*
  5341. * can access PMU always true in system wide mode
  5342. */
  5343. val = pmd_val = can_access_pmu ? ia64_get_pmd(i) : task->thread.pmds[i];
  5344. if (PMD_IS_COUNTING(i)) {
  5345. DPRINT(("[%d] pmd[%d] ctx_pmd=0x%lx hw_pmd=0x%lx\n",
  5346. task->pid,
  5347. i,
  5348. ctx->ctx_pmds[i].val,
  5349. val & ovfl_val));
  5350. /*
  5351. * we rebuild the full 64 bit value of the counter
  5352. */
  5353. val = ctx->ctx_pmds[i].val + (val & ovfl_val);
  5354. /*
  5355. * now everything is in ctx_pmds[] and we need
  5356. * to clear the saved context from save_regs() such that
  5357. * pfm_read_pmds() gets the correct value
  5358. */
  5359. pmd_val = 0UL;
  5360. /*
  5361. * take care of overflow inline
  5362. */
  5363. if (pmc0 & (1UL << i)) {
  5364. val += 1 + ovfl_val;
  5365. DPRINT(("[%d] pmd[%d] overflowed\n", task->pid, i));
  5366. }
  5367. }
  5368. DPRINT(("[%d] ctx_pmd[%d]=0x%lx pmd_val=0x%lx\n", task->pid, i, val, pmd_val));
  5369. if (is_self) task->thread.pmds[i] = pmd_val;
  5370. ctx->ctx_pmds[i].val = val;
  5371. }
  5372. }
  5373. static struct irqaction perfmon_irqaction = {
  5374. .handler = pfm_interrupt_handler,
  5375. .flags = SA_INTERRUPT,
  5376. .name = "perfmon"
  5377. };
  5378. static void
  5379. pfm_alt_save_pmu_state(void *data)
  5380. {
  5381. struct pt_regs *regs;
  5382. regs = ia64_task_regs(current);
  5383. DPRINT(("called\n"));
  5384. /*
  5385. * should not be necessary but
  5386. * let's take not risk
  5387. */
  5388. pfm_clear_psr_up();
  5389. pfm_clear_psr_pp();
  5390. ia64_psr(regs)->pp = 0;
  5391. /*
  5392. * This call is required
  5393. * May cause a spurious interrupt on some processors
  5394. */
  5395. pfm_freeze_pmu();
  5396. ia64_srlz_d();
  5397. }
  5398. void
  5399. pfm_alt_restore_pmu_state(void *data)
  5400. {
  5401. struct pt_regs *regs;
  5402. regs = ia64_task_regs(current);
  5403. DPRINT(("called\n"));
  5404. /*
  5405. * put PMU back in state expected
  5406. * by perfmon
  5407. */
  5408. pfm_clear_psr_up();
  5409. pfm_clear_psr_pp();
  5410. ia64_psr(regs)->pp = 0;
  5411. /*
  5412. * perfmon runs with PMU unfrozen at all times
  5413. */
  5414. pfm_unfreeze_pmu();
  5415. ia64_srlz_d();
  5416. }
  5417. int
  5418. pfm_install_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
  5419. {
  5420. int ret, i;
  5421. int reserve_cpu;
  5422. /* some sanity checks */
  5423. if (hdl == NULL || hdl->handler == NULL) return -EINVAL;
  5424. /* do the easy test first */
  5425. if (pfm_alt_intr_handler) return -EBUSY;
  5426. /* one at a time in the install or remove, just fail the others */
  5427. if (!spin_trylock(&pfm_alt_install_check)) {
  5428. return -EBUSY;
  5429. }
  5430. /* reserve our session */
  5431. for_each_online_cpu(reserve_cpu) {
  5432. ret = pfm_reserve_session(NULL, 1, reserve_cpu);
  5433. if (ret) goto cleanup_reserve;
  5434. }
  5435. /* save the current system wide pmu states */
  5436. ret = on_each_cpu(pfm_alt_save_pmu_state, NULL, 0, 1);
  5437. if (ret) {
  5438. DPRINT(("on_each_cpu() failed: %d\n", ret));
  5439. goto cleanup_reserve;
  5440. }
  5441. /* officially change to the alternate interrupt handler */
  5442. pfm_alt_intr_handler = hdl;
  5443. spin_unlock(&pfm_alt_install_check);
  5444. return 0;
  5445. cleanup_reserve:
  5446. for_each_online_cpu(i) {
  5447. /* don't unreserve more than we reserved */
  5448. if (i >= reserve_cpu) break;
  5449. pfm_unreserve_session(NULL, 1, i);
  5450. }
  5451. spin_unlock(&pfm_alt_install_check);
  5452. return ret;
  5453. }
  5454. EXPORT_SYMBOL_GPL(pfm_install_alt_pmu_interrupt);
  5455. int
  5456. pfm_remove_alt_pmu_interrupt(pfm_intr_handler_desc_t *hdl)
  5457. {
  5458. int i;
  5459. int ret;
  5460. if (hdl == NULL) return -EINVAL;
  5461. /* cannot remove someone else's handler! */
  5462. if (pfm_alt_intr_handler != hdl) return -EINVAL;
  5463. /* one at a time in the install or remove, just fail the others */
  5464. if (!spin_trylock(&pfm_alt_install_check)) {
  5465. return -EBUSY;
  5466. }
  5467. pfm_alt_intr_handler = NULL;
  5468. ret = on_each_cpu(pfm_alt_restore_pmu_state, NULL, 0, 1);
  5469. if (ret) {
  5470. DPRINT(("on_each_cpu() failed: %d\n", ret));
  5471. }
  5472. for_each_online_cpu(i) {
  5473. pfm_unreserve_session(NULL, 1, i);
  5474. }
  5475. spin_unlock(&pfm_alt_install_check);
  5476. return 0;
  5477. }
  5478. EXPORT_SYMBOL_GPL(pfm_remove_alt_pmu_interrupt);
  5479. /*
  5480. * perfmon initialization routine, called from the initcall() table
  5481. */
  5482. static int init_pfm_fs(void);
  5483. static int __init
  5484. pfm_probe_pmu(void)
  5485. {
  5486. pmu_config_t **p;
  5487. int family;
  5488. family = local_cpu_data->family;
  5489. p = pmu_confs;
  5490. while(*p) {
  5491. if ((*p)->probe) {
  5492. if ((*p)->probe() == 0) goto found;
  5493. } else if ((*p)->pmu_family == family || (*p)->pmu_family == 0xff) {
  5494. goto found;
  5495. }
  5496. p++;
  5497. }
  5498. return -1;
  5499. found:
  5500. pmu_conf = *p;
  5501. return 0;
  5502. }
  5503. static struct file_operations pfm_proc_fops = {
  5504. .open = pfm_proc_open,
  5505. .read = seq_read,
  5506. .llseek = seq_lseek,
  5507. .release = seq_release,
  5508. };
  5509. int __init
  5510. pfm_init(void)
  5511. {
  5512. unsigned int n, n_counters, i;
  5513. printk("perfmon: version %u.%u IRQ %u\n",
  5514. PFM_VERSION_MAJ,
  5515. PFM_VERSION_MIN,
  5516. IA64_PERFMON_VECTOR);
  5517. if (pfm_probe_pmu()) {
  5518. printk(KERN_INFO "perfmon: disabled, there is no support for processor family %d\n",
  5519. local_cpu_data->family);
  5520. return -ENODEV;
  5521. }
  5522. /*
  5523. * compute the number of implemented PMD/PMC from the
  5524. * description tables
  5525. */
  5526. n = 0;
  5527. for (i=0; PMC_IS_LAST(i) == 0; i++) {
  5528. if (PMC_IS_IMPL(i) == 0) continue;
  5529. pmu_conf->impl_pmcs[i>>6] |= 1UL << (i&63);
  5530. n++;
  5531. }
  5532. pmu_conf->num_pmcs = n;
  5533. n = 0; n_counters = 0;
  5534. for (i=0; PMD_IS_LAST(i) == 0; i++) {
  5535. if (PMD_IS_IMPL(i) == 0) continue;
  5536. pmu_conf->impl_pmds[i>>6] |= 1UL << (i&63);
  5537. n++;
  5538. if (PMD_IS_COUNTING(i)) n_counters++;
  5539. }
  5540. pmu_conf->num_pmds = n;
  5541. pmu_conf->num_counters = n_counters;
  5542. /*
  5543. * sanity checks on the number of debug registers
  5544. */
  5545. if (pmu_conf->use_rr_dbregs) {
  5546. if (pmu_conf->num_ibrs > IA64_NUM_DBG_REGS) {
  5547. printk(KERN_INFO "perfmon: unsupported number of code debug registers (%u)\n", pmu_conf->num_ibrs);
  5548. pmu_conf = NULL;
  5549. return -1;
  5550. }
  5551. if (pmu_conf->num_dbrs > IA64_NUM_DBG_REGS) {
  5552. printk(KERN_INFO "perfmon: unsupported number of data debug registers (%u)\n", pmu_conf->num_ibrs);
  5553. pmu_conf = NULL;
  5554. return -1;
  5555. }
  5556. }
  5557. printk("perfmon: %s PMU detected, %u PMCs, %u PMDs, %u counters (%lu bits)\n",
  5558. pmu_conf->pmu_name,
  5559. pmu_conf->num_pmcs,
  5560. pmu_conf->num_pmds,
  5561. pmu_conf->num_counters,
  5562. ffz(pmu_conf->ovfl_val));
  5563. /* sanity check */
  5564. if (pmu_conf->num_pmds >= IA64_NUM_PMD_REGS || pmu_conf->num_pmcs >= IA64_NUM_PMC_REGS) {
  5565. printk(KERN_ERR "perfmon: not enough pmc/pmd, perfmon disabled\n");
  5566. pmu_conf = NULL;
  5567. return -1;
  5568. }
  5569. /*
  5570. * create /proc/perfmon (mostly for debugging purposes)
  5571. */
  5572. perfmon_dir = create_proc_entry("perfmon", S_IRUGO, NULL);
  5573. if (perfmon_dir == NULL) {
  5574. printk(KERN_ERR "perfmon: cannot create /proc entry, perfmon disabled\n");
  5575. pmu_conf = NULL;
  5576. return -1;
  5577. }
  5578. /*
  5579. * install customized file operations for /proc/perfmon entry
  5580. */
  5581. perfmon_dir->proc_fops = &pfm_proc_fops;
  5582. /*
  5583. * create /proc/sys/kernel/perfmon (for debugging purposes)
  5584. */
  5585. pfm_sysctl_header = register_sysctl_table(pfm_sysctl_root, 0);
  5586. /*
  5587. * initialize all our spinlocks
  5588. */
  5589. spin_lock_init(&pfm_sessions.pfs_lock);
  5590. spin_lock_init(&pfm_buffer_fmt_lock);
  5591. init_pfm_fs();
  5592. for(i=0; i < NR_CPUS; i++) pfm_stats[i].pfm_ovfl_intr_cycles_min = ~0UL;
  5593. return 0;
  5594. }
  5595. __initcall(pfm_init);
  5596. /*
  5597. * this function is called before pfm_init()
  5598. */
  5599. void
  5600. pfm_init_percpu (void)
  5601. {
  5602. /*
  5603. * make sure no measurement is active
  5604. * (may inherit programmed PMCs from EFI).
  5605. */
  5606. pfm_clear_psr_pp();
  5607. pfm_clear_psr_up();
  5608. /*
  5609. * we run with the PMU not frozen at all times
  5610. */
  5611. pfm_unfreeze_pmu();
  5612. if (smp_processor_id() == 0)
  5613. register_percpu_irq(IA64_PERFMON_VECTOR, &perfmon_irqaction);
  5614. ia64_setreg(_IA64_REG_CR_PMV, IA64_PERFMON_VECTOR);
  5615. ia64_srlz_d();
  5616. }
  5617. /*
  5618. * used for debug purposes only
  5619. */
  5620. void
  5621. dump_pmu_state(const char *from)
  5622. {
  5623. struct task_struct *task;
  5624. struct thread_struct *t;
  5625. struct pt_regs *regs;
  5626. pfm_context_t *ctx;
  5627. unsigned long psr, dcr, info, flags;
  5628. int i, this_cpu;
  5629. local_irq_save(flags);
  5630. this_cpu = smp_processor_id();
  5631. regs = ia64_task_regs(current);
  5632. info = PFM_CPUINFO_GET();
  5633. dcr = ia64_getreg(_IA64_REG_CR_DCR);
  5634. if (info == 0 && ia64_psr(regs)->pp == 0 && (dcr & IA64_DCR_PP) == 0) {
  5635. local_irq_restore(flags);
  5636. return;
  5637. }
  5638. printk("CPU%d from %s() current [%d] iip=0x%lx %s\n",
  5639. this_cpu,
  5640. from,
  5641. current->pid,
  5642. regs->cr_iip,
  5643. current->comm);
  5644. task = GET_PMU_OWNER();
  5645. ctx = GET_PMU_CTX();
  5646. printk("->CPU%d owner [%d] ctx=%p\n", this_cpu, task ? task->pid : -1, ctx);
  5647. psr = pfm_get_psr();
  5648. printk("->CPU%d pmc0=0x%lx psr.pp=%d psr.up=%d dcr.pp=%d syst_info=0x%lx user_psr.up=%d user_psr.pp=%d\n",
  5649. this_cpu,
  5650. ia64_get_pmc(0),
  5651. psr & IA64_PSR_PP ? 1 : 0,
  5652. psr & IA64_PSR_UP ? 1 : 0,
  5653. dcr & IA64_DCR_PP ? 1 : 0,
  5654. info,
  5655. ia64_psr(regs)->up,
  5656. ia64_psr(regs)->pp);
  5657. ia64_psr(regs)->up = 0;
  5658. ia64_psr(regs)->pp = 0;
  5659. t = &current->thread;
  5660. for (i=1; PMC_IS_LAST(i) == 0; i++) {
  5661. if (PMC_IS_IMPL(i) == 0) continue;
  5662. printk("->CPU%d pmc[%d]=0x%lx thread_pmc[%d]=0x%lx\n", this_cpu, i, ia64_get_pmc(i), i, t->pmcs[i]);
  5663. }
  5664. for (i=1; PMD_IS_LAST(i) == 0; i++) {
  5665. if (PMD_IS_IMPL(i) == 0) continue;
  5666. printk("->CPU%d pmd[%d]=0x%lx thread_pmd[%d]=0x%lx\n", this_cpu, i, ia64_get_pmd(i), i, t->pmds[i]);
  5667. }
  5668. if (ctx) {
  5669. printk("->CPU%d ctx_state=%d vaddr=%p addr=%p fd=%d ctx_task=[%d] saved_psr_up=0x%lx\n",
  5670. this_cpu,
  5671. ctx->ctx_state,
  5672. ctx->ctx_smpl_vaddr,
  5673. ctx->ctx_smpl_hdr,
  5674. ctx->ctx_msgq_head,
  5675. ctx->ctx_msgq_tail,
  5676. ctx->ctx_saved_psr_up);
  5677. }
  5678. local_irq_restore(flags);
  5679. }
  5680. /*
  5681. * called from process.c:copy_thread(). task is new child.
  5682. */
  5683. void
  5684. pfm_inherit(struct task_struct *task, struct pt_regs *regs)
  5685. {
  5686. struct thread_struct *thread;
  5687. DPRINT(("perfmon: pfm_inherit clearing state for [%d]\n", task->pid));
  5688. thread = &task->thread;
  5689. /*
  5690. * cut links inherited from parent (current)
  5691. */
  5692. thread->pfm_context = NULL;
  5693. PFM_SET_WORK_PENDING(task, 0);
  5694. /*
  5695. * the psr bits are already set properly in copy_threads()
  5696. */
  5697. }
  5698. #else /* !CONFIG_PERFMON */
  5699. asmlinkage long
  5700. sys_perfmonctl (int fd, int cmd, void *arg, int count)
  5701. {
  5702. return -ENOSYS;
  5703. }
  5704. #endif /* CONFIG_PERFMON */