perfmon.c 165 KB

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