perfmon.c 169 KB

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