perfmon.c 169 KB

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