perfmon.c 169 KB

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