perfmon.c 169 KB

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