xmon.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Routines providing a simple monitor for use on the PowerMac.
  4. *
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. * Copyright (C) 2001 PPC64 Team, IBM Corp
  7. * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/sched/signal.h>
  12. #include <linux/smp.h>
  13. #include <linux/mm.h>
  14. #include <linux/reboot.h>
  15. #include <linux/delay.h>
  16. #include <linux/kallsyms.h>
  17. #include <linux/kmsg_dump.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/export.h>
  20. #include <linux/sysrq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/irq.h>
  23. #include <linux/bug.h>
  24. #include <linux/nmi.h>
  25. #include <linux/ctype.h>
  26. #include <linux/highmem.h>
  27. #include <linux/security.h>
  28. #include <linux/debugfs.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/smp.h>
  31. #include <asm/string.h>
  32. #include <asm/machdep.h>
  33. #include <asm/xmon.h>
  34. #include <asm/processor.h>
  35. #include <asm/mmu.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/plpar_wrappers.h>
  38. #include <asm/cputable.h>
  39. #include <asm/rtas.h>
  40. #include <asm/sstep.h>
  41. #include <asm/irq_regs.h>
  42. #include <asm/setjmp.h>
  43. #include <asm/reg.h>
  44. #include <asm/debug.h>
  45. #include <asm/hw_breakpoint.h>
  46. #include <asm/xive.h>
  47. #include <asm/opal.h>
  48. #include <asm/firmware.h>
  49. #include <asm/text-patching.h>
  50. #include <asm/sections.h>
  51. #include <asm/inst.h>
  52. #include <asm/interrupt.h>
  53. #ifdef CONFIG_PPC64
  54. #include <asm/hvcall.h>
  55. #include <asm/paca.h>
  56. #include <asm/lppaca.h>
  57. #endif
  58. #include "nonstdio.h"
  59. #include "dis-asm.h"
  60. #include "xmon_bpts.h"
  61. #ifdef CONFIG_SMP
  62. static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  63. static unsigned long xmon_taken = 1;
  64. static int xmon_owner;
  65. static int xmon_gate;
  66. static int xmon_batch;
  67. static unsigned long xmon_batch_start_cpu;
  68. static cpumask_t xmon_batch_cpus = CPU_MASK_NONE;
  69. #else
  70. #define xmon_owner 0
  71. #endif /* CONFIG_SMP */
  72. static unsigned long in_xmon __read_mostly = 0;
  73. static int xmon_on = IS_ENABLED(CONFIG_XMON_DEFAULT);
  74. static bool xmon_is_ro = IS_ENABLED(CONFIG_XMON_DEFAULT_RO_MODE);
  75. static unsigned long adrs;
  76. static int size = 1;
  77. #define MAX_DUMP (64 * 1024)
  78. static unsigned long ndump = 64;
  79. #define MAX_IDUMP (MAX_DUMP >> 2)
  80. static unsigned long nidump = 16;
  81. static unsigned long ncsum = 4096;
  82. static int termch;
  83. static char tmpstr[KSYM_NAME_LEN];
  84. static int tracing_enabled;
  85. static long bus_error_jmp[JMP_BUF_LEN];
  86. static int catch_memory_errors;
  87. static int catch_spr_faults;
  88. static long *xmon_fault_jmp[NR_CPUS];
  89. /* Breakpoint stuff */
  90. struct bpt {
  91. unsigned long address;
  92. u32 *instr;
  93. atomic_t ref_count;
  94. int enabled;
  95. unsigned long pad;
  96. };
  97. /* Bits in bpt.enabled */
  98. #define BP_CIABR 1
  99. #define BP_TRAP 2
  100. #define BP_DABR 4
  101. static struct bpt bpts[NBPTS];
  102. static struct bpt dabr[HBP_NUM_MAX];
  103. static struct bpt *iabr;
  104. static unsigned int bpinstr = PPC_RAW_TRAP();
  105. #define BP_NUM(bp) ((bp) - bpts + 1)
  106. /* Prototypes */
  107. static int cmds(struct pt_regs *);
  108. static int mread(unsigned long, void *, int);
  109. static int mwrite(unsigned long, void *, int);
  110. static int mread_instr(unsigned long, ppc_inst_t *);
  111. static int handle_fault(struct pt_regs *);
  112. static void byterev(unsigned char *, int);
  113. static void memex(void);
  114. static int bsesc(void);
  115. static void dump(void);
  116. static void show_pte(unsigned long);
  117. static void prdump(unsigned long, long);
  118. static int ppc_inst_dump(unsigned long, long, int);
  119. static void dump_log_buf(void);
  120. #ifdef CONFIG_SMP
  121. static int xmon_switch_cpu(unsigned long);
  122. static int xmon_batch_next_cpu(void);
  123. static int batch_cmds(struct pt_regs *);
  124. #endif
  125. #ifdef CONFIG_PPC_POWERNV
  126. static void dump_opal_msglog(void);
  127. #else
  128. static inline void dump_opal_msglog(void)
  129. {
  130. printf("Machine is not running OPAL firmware.\n");
  131. }
  132. #endif
  133. static void backtrace(struct pt_regs *);
  134. static void excprint(struct pt_regs *);
  135. static void prregs(struct pt_regs *);
  136. static void memops(int);
  137. static void memlocate(void);
  138. static void memzcan(void);
  139. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  140. int skipbl(void);
  141. int scanhex(unsigned long *valp);
  142. static void scannl(void);
  143. static int hexdigit(int);
  144. void getstring(char *, int);
  145. static void flush_input(void);
  146. static int inchar(void);
  147. static void take_input(char *);
  148. static int read_spr(int, unsigned long *);
  149. static void write_spr(int, unsigned long);
  150. static void super_regs(void);
  151. static void remove_bpts(void);
  152. static void insert_bpts(void);
  153. static void remove_cpu_bpts(void);
  154. static void insert_cpu_bpts(void);
  155. static struct bpt *at_breakpoint(unsigned long pc);
  156. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  157. static int do_step(struct pt_regs *);
  158. static void bpt_cmds(void);
  159. static void cacheflush(void);
  160. static int cpu_cmd(void);
  161. static void csum(void);
  162. static void bootcmds(void);
  163. static void proccall(void);
  164. static void show_tasks(void);
  165. void dump_segments(void);
  166. static void symbol_lookup(void);
  167. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  168. unsigned long pc);
  169. static void xmon_print_symbol(unsigned long address, const char *mid,
  170. const char *after);
  171. static const char *getvecname(unsigned long vec);
  172. #ifdef CONFIG_44x
  173. static void dump_tlb_44x(void);
  174. #endif
  175. #ifdef CONFIG_PPC_BOOK3E_64
  176. static void dump_tlb_book3e(void);
  177. #endif
  178. static void clear_all_bpt(void);
  179. #ifdef CONFIG_PPC64
  180. #define REG "%.16lx"
  181. #else
  182. #define REG "%.8lx"
  183. #endif
  184. #ifdef __LITTLE_ENDIAN__
  185. #define GETWORD(v) (((v)[3] << 24) + ((v)[2] << 16) + ((v)[1] << 8) + (v)[0])
  186. #else
  187. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  188. #endif
  189. static const char *xmon_ro_msg = "Operation disabled: xmon in read-only mode\n";
  190. static char *help_string = "\
  191. Commands:\n\
  192. b show breakpoints\n\
  193. bd set data breakpoint\n\
  194. bi set instruction breakpoint\n\
  195. bc clear breakpoint\n"
  196. #ifdef CONFIG_SMP
  197. "\
  198. c print cpus stopped in xmon\n\
  199. c# try to switch to cpu number h (in hex)\n\
  200. c# $ run command '$' (one of 'r','S' or 't') on all cpus in xmon\n"
  201. #endif
  202. "\
  203. C checksum\n\
  204. d dump bytes\n\
  205. d1 dump 1 byte values\n\
  206. d2 dump 2 byte values\n\
  207. d4 dump 4 byte values\n\
  208. d8 dump 8 byte values\n\
  209. di dump instructions\n\
  210. df dump float values\n\
  211. dd dump double values\n\
  212. dl dump the kernel log buffer\n"
  213. #ifdef CONFIG_PPC_POWERNV
  214. "\
  215. do dump the OPAL message log\n"
  216. #endif
  217. #ifdef CONFIG_PPC64
  218. "\
  219. dp[#] dump paca for current cpu, or cpu #\n\
  220. dpa dump paca for all possible cpus\n"
  221. #endif
  222. "\
  223. dr dump stream of raw bytes\n\
  224. dv dump virtual address translation \n\
  225. dt dump the tracing buffers (uses printk)\n\
  226. dtc dump the tracing buffers for current CPU (uses printk)\n\
  227. "
  228. #ifdef CONFIG_PPC_POWERNV
  229. " dx# dump xive on CPU #\n\
  230. dxi# dump xive irq state #\n\
  231. dxa dump xive on all CPUs\n"
  232. #endif
  233. " e print exception information\n\
  234. f flush cache\n\
  235. la lookup symbol+offset of specified address\n\
  236. ls lookup address of specified symbol\n\
  237. lp s [#] lookup address of percpu symbol s for current cpu, or cpu #\n\
  238. m examine/change memory\n\
  239. mm move a block of memory\n\
  240. ms set a block of memory\n\
  241. md compare two blocks of memory\n\
  242. ml locate a block of memory\n\
  243. mz zero a block of memory\n\
  244. mi show information about memory allocation\n\
  245. p call a procedure\n\
  246. P list processes/tasks\n\
  247. r print registers\n\
  248. s single step\n"
  249. " S print special registers\n\
  250. Sa print all SPRs\n\
  251. Sr # read SPR #\n\
  252. Sw #v write v to SPR #\n\
  253. t print backtrace\n\
  254. x exit monitor and recover\n\
  255. X exit monitor and don't recover\n"
  256. #if defined(CONFIG_PPC_BOOK3S_64)
  257. " u dump segment table or SLB\n"
  258. #elif defined(CONFIG_PPC_BOOK3S_32)
  259. " u dump segment registers\n"
  260. #elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E_64)
  261. " u dump TLB\n"
  262. #endif
  263. " U show uptime information\n"
  264. " ? help\n"
  265. " # n limit output to n lines per page (for dp, dpa, dl)\n"
  266. " zr reboot\n"
  267. " zh halt\n"
  268. ;
  269. #ifdef CONFIG_SECURITY
  270. static bool xmon_is_locked_down(void)
  271. {
  272. static bool lockdown;
  273. if (!lockdown) {
  274. lockdown = !!security_locked_down(LOCKDOWN_XMON_RW);
  275. if (lockdown) {
  276. printf("xmon: Disabled due to kernel lockdown\n");
  277. xmon_is_ro = true;
  278. }
  279. }
  280. if (!xmon_is_ro) {
  281. xmon_is_ro = !!security_locked_down(LOCKDOWN_XMON_WR);
  282. if (xmon_is_ro)
  283. printf("xmon: Read-only due to kernel lockdown\n");
  284. }
  285. return lockdown;
  286. }
  287. #else /* CONFIG_SECURITY */
  288. static inline bool xmon_is_locked_down(void)
  289. {
  290. return false;
  291. }
  292. #endif
  293. static struct pt_regs *xmon_regs;
  294. static inline void sync(void)
  295. {
  296. asm volatile("sync; isync");
  297. }
  298. static inline void cflush(void *p)
  299. {
  300. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  301. }
  302. static inline void cinval(void *p)
  303. {
  304. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  305. }
  306. /**
  307. * write_ciabr() - write the CIABR SPR
  308. * @ciabr: The value to write.
  309. *
  310. * This function writes a value to the CIARB register either directly
  311. * through mtspr instruction if the kernel is in HV privilege mode or
  312. * call a hypervisor function to achieve the same in case the kernel
  313. * is in supervisor privilege mode.
  314. */
  315. static void write_ciabr(unsigned long ciabr)
  316. {
  317. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  318. return;
  319. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  320. mtspr(SPRN_CIABR, ciabr);
  321. return;
  322. }
  323. plpar_set_ciabr(ciabr);
  324. }
  325. /**
  326. * set_ciabr() - set the CIABR
  327. * @addr: The value to set.
  328. *
  329. * This function sets the correct privilege value into the HW
  330. * breakpoint address before writing it up in the CIABR register.
  331. */
  332. static void set_ciabr(unsigned long addr)
  333. {
  334. addr &= ~CIABR_PRIV;
  335. if (cpu_has_feature(CPU_FTR_HVMODE))
  336. addr |= CIABR_PRIV_HYPER;
  337. else
  338. addr |= CIABR_PRIV_SUPER;
  339. write_ciabr(addr);
  340. }
  341. /*
  342. * Disable surveillance (the service processor watchdog function)
  343. * while we are in xmon.
  344. * XXX we should re-enable it when we leave. :)
  345. */
  346. #define SURVEILLANCE_TOKEN 9000
  347. static inline void disable_surveillance(void)
  348. {
  349. #ifdef CONFIG_PPC_PSERIES
  350. /* Since this can't be a module, args should end up below 4GB. */
  351. static struct rtas_args args;
  352. const s32 token = rtas_function_token(RTAS_FN_SET_INDICATOR);
  353. /*
  354. * At this point we have got all the cpus we can into
  355. * xmon, so there is hopefully no other cpu calling RTAS
  356. * at the moment, even though we don't take rtas.lock.
  357. * If we did try to take rtas.lock there would be a
  358. * real possibility of deadlock.
  359. */
  360. if (token == RTAS_UNKNOWN_SERVICE)
  361. return;
  362. rtas_call_unlocked(&args, token, 3, 1, NULL,
  363. SURVEILLANCE_TOKEN, 0, 0);
  364. #endif /* CONFIG_PPC_PSERIES */
  365. }
  366. #ifdef CONFIG_SMP
  367. static int xmon_speaker;
  368. static void get_output_lock(void)
  369. {
  370. int me = smp_processor_id() + 0x100;
  371. int last_speaker = 0, prev;
  372. long timeout;
  373. if (xmon_speaker == me)
  374. return;
  375. for (;;) {
  376. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  377. if (last_speaker == 0)
  378. return;
  379. /*
  380. * Wait a full second for the lock, we might be on a slow
  381. * console, but check every 100us.
  382. */
  383. timeout = 10000;
  384. while (xmon_speaker == last_speaker) {
  385. if (--timeout > 0) {
  386. udelay(100);
  387. continue;
  388. }
  389. /* hostile takeover */
  390. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  391. if (prev == last_speaker)
  392. return;
  393. break;
  394. }
  395. }
  396. }
  397. static void release_output_lock(void)
  398. {
  399. xmon_speaker = 0;
  400. }
  401. int cpus_are_in_xmon(void)
  402. {
  403. return !cpumask_empty(&cpus_in_xmon);
  404. }
  405. static bool wait_for_other_cpus(int ncpus)
  406. {
  407. unsigned long timeout;
  408. /* We wait for 2s, which is a metric "little while" */
  409. for (timeout = 20000; timeout != 0; --timeout) {
  410. if (cpumask_weight(&cpus_in_xmon) >= ncpus)
  411. return true;
  412. udelay(100);
  413. barrier();
  414. }
  415. return false;
  416. }
  417. #else /* CONFIG_SMP */
  418. static inline void get_output_lock(void) {}
  419. static inline void release_output_lock(void) {}
  420. #endif
  421. static void xmon_touch_watchdogs(void)
  422. {
  423. touch_softlockup_watchdog_sync();
  424. rcu_cpu_stall_reset();
  425. touch_nmi_watchdog();
  426. }
  427. static int xmon_core(struct pt_regs *regs, volatile int fromipi)
  428. {
  429. volatile int cmd = 0;
  430. struct bpt *volatile bp;
  431. long recurse_jmp[JMP_BUF_LEN];
  432. bool locked_down;
  433. unsigned long offset;
  434. unsigned long flags;
  435. #ifdef CONFIG_SMP
  436. int cpu;
  437. int secondary;
  438. #endif
  439. local_irq_save(flags);
  440. hard_irq_disable();
  441. locked_down = xmon_is_locked_down();
  442. if (!fromipi) {
  443. tracing_enabled = tracing_is_on();
  444. tracing_off();
  445. }
  446. bp = in_breakpoint_table(regs->nip, &offset);
  447. if (bp != NULL) {
  448. regs_set_return_ip(regs, bp->address + offset);
  449. atomic_dec(&bp->ref_count);
  450. }
  451. remove_cpu_bpts();
  452. #ifdef CONFIG_SMP
  453. cpu = smp_processor_id();
  454. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  455. /*
  456. * We catch SPR read/write faults here because the 0x700, 0xf60
  457. * etc. handlers don't call debugger_fault_handler().
  458. */
  459. if (catch_spr_faults)
  460. longjmp(bus_error_jmp, 1);
  461. get_output_lock();
  462. excprint(regs);
  463. printf("cpu 0x%x: Exception %lx %s in xmon, "
  464. "returning to main loop\n",
  465. cpu, regs->trap, getvecname(TRAP(regs)));
  466. release_output_lock();
  467. longjmp(xmon_fault_jmp[cpu], 1);
  468. }
  469. if (setjmp(recurse_jmp) != 0) {
  470. if (!in_xmon || !xmon_gate) {
  471. get_output_lock();
  472. printf("xmon: WARNING: bad recursive fault "
  473. "on cpu 0x%x\n", cpu);
  474. release_output_lock();
  475. goto waiting;
  476. }
  477. secondary = !(xmon_taken && cpu == xmon_owner);
  478. goto cmdloop;
  479. }
  480. xmon_fault_jmp[cpu] = recurse_jmp;
  481. bp = NULL;
  482. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
  483. bp = at_breakpoint(regs->nip);
  484. if (bp || regs_is_unrecoverable(regs))
  485. fromipi = 0;
  486. if (!fromipi) {
  487. get_output_lock();
  488. if (!locked_down)
  489. excprint(regs);
  490. if (bp) {
  491. printf("cpu 0x%x stopped at breakpoint 0x%tx (",
  492. cpu, BP_NUM(bp));
  493. xmon_print_symbol(regs->nip, " ", ")\n");
  494. }
  495. if (regs_is_unrecoverable(regs))
  496. printf("WARNING: exception is not recoverable, "
  497. "can't continue\n");
  498. release_output_lock();
  499. }
  500. cpumask_set_cpu(cpu, &cpus_in_xmon);
  501. waiting:
  502. secondary = 1;
  503. spin_begin();
  504. while (secondary && !xmon_gate) {
  505. if (in_xmon == 0) {
  506. if (fromipi) {
  507. spin_end();
  508. goto leave;
  509. }
  510. secondary = test_and_set_bit(0, &in_xmon);
  511. }
  512. spin_cpu_relax();
  513. touch_nmi_watchdog();
  514. }
  515. spin_end();
  516. if (!secondary && !xmon_gate) {
  517. /* we are the first cpu to come in */
  518. /* interrupt other cpu(s) */
  519. int ncpus = num_online_cpus();
  520. xmon_owner = cpu;
  521. mb();
  522. if (ncpus > 1) {
  523. /*
  524. * A system reset (trap == 0x100) can be triggered on
  525. * all CPUs, so when we come in via 0x100 try waiting
  526. * for the other CPUs to come in before we send the
  527. * debugger break (IPI). This is similar to
  528. * crash_kexec_secondary().
  529. */
  530. if (TRAP(regs) != INTERRUPT_SYSTEM_RESET || !wait_for_other_cpus(ncpus))
  531. smp_send_debugger_break();
  532. wait_for_other_cpus(ncpus);
  533. }
  534. remove_bpts();
  535. disable_surveillance();
  536. if (!locked_down) {
  537. /* for breakpoint or single step, print curr insn */
  538. if (bp || TRAP(regs) == INTERRUPT_TRACE)
  539. ppc_inst_dump(regs->nip, 1, 0);
  540. printf("enter ? for help\n");
  541. }
  542. mb();
  543. xmon_gate = 1;
  544. barrier();
  545. touch_nmi_watchdog();
  546. }
  547. cmdloop:
  548. while (in_xmon) {
  549. if (secondary) {
  550. spin_begin();
  551. if (cpu == xmon_owner) {
  552. if (!test_and_set_bit(0, &xmon_taken)) {
  553. secondary = 0;
  554. spin_end();
  555. continue;
  556. }
  557. /* missed it */
  558. while (cpu == xmon_owner)
  559. spin_cpu_relax();
  560. }
  561. spin_cpu_relax();
  562. touch_nmi_watchdog();
  563. } else {
  564. cmd = 1;
  565. if (xmon_batch)
  566. cmd = batch_cmds(regs);
  567. if (!locked_down && cmd)
  568. cmd = cmds(regs);
  569. if (locked_down || cmd != 0) {
  570. /* exiting xmon */
  571. insert_bpts();
  572. xmon_gate = 0;
  573. wmb();
  574. in_xmon = 0;
  575. break;
  576. }
  577. /* have switched to some other cpu */
  578. secondary = 1;
  579. }
  580. }
  581. leave:
  582. cpumask_clear_cpu(cpu, &cpus_in_xmon);
  583. xmon_fault_jmp[cpu] = NULL;
  584. #else
  585. /* UP is simple... */
  586. if (in_xmon) {
  587. printf("Exception %lx %s in xmon, returning to main loop\n",
  588. regs->trap, getvecname(TRAP(regs)));
  589. longjmp(xmon_fault_jmp[0], 1);
  590. }
  591. if (setjmp(recurse_jmp) == 0) {
  592. xmon_fault_jmp[0] = recurse_jmp;
  593. in_xmon = 1;
  594. excprint(regs);
  595. bp = at_breakpoint(regs->nip);
  596. if (bp) {
  597. printf("Stopped at breakpoint %tx (", BP_NUM(bp));
  598. xmon_print_symbol(regs->nip, " ", ")\n");
  599. }
  600. if (regs_is_unrecoverable(regs))
  601. printf("WARNING: exception is not recoverable, "
  602. "can't continue\n");
  603. remove_bpts();
  604. disable_surveillance();
  605. if (!locked_down) {
  606. /* for breakpoint or single step, print current insn */
  607. if (bp || TRAP(regs) == INTERRUPT_TRACE)
  608. ppc_inst_dump(regs->nip, 1, 0);
  609. printf("enter ? for help\n");
  610. }
  611. }
  612. if (!locked_down)
  613. cmd = cmds(regs);
  614. insert_bpts();
  615. in_xmon = 0;
  616. #endif
  617. #ifdef CONFIG_BOOKE
  618. if (regs->msr & MSR_DE) {
  619. bp = at_breakpoint(regs->nip);
  620. if (bp != NULL) {
  621. regs_set_return_ip(regs, (unsigned long) &bp->instr[0]);
  622. atomic_inc(&bp->ref_count);
  623. }
  624. }
  625. #else
  626. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  627. bp = at_breakpoint(regs->nip);
  628. if (bp != NULL) {
  629. int stepped = emulate_step(regs, ppc_inst_read(bp->instr));
  630. if (stepped == 0) {
  631. regs_set_return_ip(regs, (unsigned long) &bp->instr[0]);
  632. atomic_inc(&bp->ref_count);
  633. } else if (stepped < 0) {
  634. printf("Couldn't single-step %s instruction\n",
  635. IS_RFID(ppc_inst_read(bp->instr))? "rfid": "mtmsrd");
  636. }
  637. }
  638. }
  639. #endif
  640. if (locked_down)
  641. clear_all_bpt();
  642. else
  643. insert_cpu_bpts();
  644. xmon_touch_watchdogs();
  645. local_irq_restore(flags);
  646. return cmd != 'X' && cmd != EOF;
  647. }
  648. int xmon(struct pt_regs *excp)
  649. {
  650. struct pt_regs regs;
  651. if (excp == NULL) {
  652. ppc_save_regs(&regs);
  653. excp = &regs;
  654. }
  655. return xmon_core(excp, 0);
  656. }
  657. EXPORT_SYMBOL(xmon);
  658. irqreturn_t xmon_irq(int irq, void *d)
  659. {
  660. unsigned long flags;
  661. local_irq_save(flags);
  662. printf("Keyboard interrupt\n");
  663. xmon(get_irq_regs());
  664. local_irq_restore(flags);
  665. return IRQ_HANDLED;
  666. }
  667. static int xmon_bpt(struct pt_regs *regs)
  668. {
  669. struct bpt *bp;
  670. unsigned long offset;
  671. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  672. return 0;
  673. /* Are we at the trap at bp->instr[1] for some bp? */
  674. bp = in_breakpoint_table(regs->nip, &offset);
  675. if (bp != NULL && (offset == 4 || offset == 8)) {
  676. regs_set_return_ip(regs, bp->address + offset);
  677. atomic_dec(&bp->ref_count);
  678. return 1;
  679. }
  680. /* Are we at a breakpoint? */
  681. bp = at_breakpoint(regs->nip);
  682. if (!bp)
  683. return 0;
  684. xmon_core(regs, 0);
  685. return 1;
  686. }
  687. static int xmon_sstep(struct pt_regs *regs)
  688. {
  689. if (user_mode(regs))
  690. return 0;
  691. xmon_core(regs, 0);
  692. return 1;
  693. }
  694. static int xmon_break_match(struct pt_regs *regs)
  695. {
  696. int i;
  697. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  698. return 0;
  699. for (i = 0; i < nr_wp_slots(); i++) {
  700. if (dabr[i].enabled)
  701. goto found;
  702. }
  703. return 0;
  704. found:
  705. xmon_core(regs, 0);
  706. return 1;
  707. }
  708. static int xmon_iabr_match(struct pt_regs *regs)
  709. {
  710. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  711. return 0;
  712. if (iabr == NULL)
  713. return 0;
  714. xmon_core(regs, 0);
  715. return 1;
  716. }
  717. static int xmon_ipi(struct pt_regs *regs)
  718. {
  719. #ifdef CONFIG_SMP
  720. if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
  721. xmon_core(regs, 1);
  722. #endif
  723. return 0;
  724. }
  725. static int xmon_fault_handler(struct pt_regs *regs)
  726. {
  727. struct bpt *bp;
  728. unsigned long offset;
  729. if (in_xmon && catch_memory_errors)
  730. handle_fault(regs); /* doesn't return */
  731. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  732. bp = in_breakpoint_table(regs->nip, &offset);
  733. if (bp != NULL) {
  734. regs_set_return_ip(regs, bp->address + offset);
  735. atomic_dec(&bp->ref_count);
  736. }
  737. }
  738. return 0;
  739. }
  740. /* Force enable xmon if not already enabled */
  741. static inline void force_enable_xmon(void)
  742. {
  743. /* Enable xmon hooks if needed */
  744. if (!xmon_on) {
  745. printf("xmon: Enabling debugger hooks\n");
  746. xmon_on = 1;
  747. }
  748. }
  749. static struct bpt *at_breakpoint(unsigned long pc)
  750. {
  751. int i;
  752. struct bpt *volatile bp;
  753. bp = bpts;
  754. for (i = 0; i < NBPTS; ++i, ++bp)
  755. if (bp->enabled && pc == bp->address)
  756. return bp;
  757. return NULL;
  758. }
  759. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  760. {
  761. unsigned long off;
  762. off = nip - (unsigned long)bpt_table;
  763. if (off >= sizeof(bpt_table))
  764. return NULL;
  765. *offp = off & (BPT_SIZE - 1);
  766. if (off & 3)
  767. return NULL;
  768. return bpts + (off / BPT_SIZE);
  769. }
  770. static struct bpt *new_breakpoint(unsigned long a)
  771. {
  772. struct bpt *bp;
  773. a &= ~3UL;
  774. bp = at_breakpoint(a);
  775. if (bp)
  776. return bp;
  777. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  778. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  779. bp->address = a;
  780. bp->instr = (void *)(bpt_table + ((bp - bpts) * BPT_WORDS));
  781. return bp;
  782. }
  783. }
  784. printf("Sorry, no free breakpoints. Please clear one first.\n");
  785. return NULL;
  786. }
  787. static void insert_bpts(void)
  788. {
  789. int i;
  790. ppc_inst_t instr, instr2;
  791. struct bpt *bp, *bp2;
  792. bp = bpts;
  793. for (i = 0; i < NBPTS; ++i, ++bp) {
  794. if ((bp->enabled & (BP_TRAP|BP_CIABR)) == 0)
  795. continue;
  796. if (!mread_instr(bp->address, &instr)) {
  797. printf("Couldn't read instruction at %lx, "
  798. "disabling breakpoint there\n", bp->address);
  799. bp->enabled = 0;
  800. continue;
  801. }
  802. if (!can_single_step(ppc_inst_val(instr))) {
  803. printf("Breakpoint at %lx is on an instruction that can't be single stepped, disabling it\n",
  804. bp->address);
  805. bp->enabled = 0;
  806. continue;
  807. }
  808. /*
  809. * Check the address is not a suffix by looking for a prefix in
  810. * front of it.
  811. */
  812. if (mread_instr(bp->address - 4, &instr2) == 8) {
  813. printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
  814. bp->address);
  815. bp->enabled = 0;
  816. continue;
  817. }
  818. /*
  819. * We might still be a suffix - if the prefix has already been
  820. * replaced by a breakpoint we won't catch it with the above
  821. * test.
  822. */
  823. bp2 = at_breakpoint(bp->address - 4);
  824. if (bp2 && ppc_inst_prefixed(ppc_inst_read(bp2->instr))) {
  825. printf("Breakpoint at %lx is on the second word of a prefixed instruction, disabling it\n",
  826. bp->address);
  827. bp->enabled = 0;
  828. continue;
  829. }
  830. patch_instruction(bp->instr, instr);
  831. patch_instruction(ppc_inst_next(bp->instr, bp->instr),
  832. ppc_inst(bpinstr));
  833. if (bp->enabled & BP_CIABR)
  834. continue;
  835. if (patch_instruction((u32 *)bp->address,
  836. ppc_inst(bpinstr)) != 0) {
  837. printf("Couldn't write instruction at %lx, "
  838. "disabling breakpoint there\n", bp->address);
  839. bp->enabled &= ~BP_TRAP;
  840. continue;
  841. }
  842. }
  843. }
  844. static void insert_cpu_bpts(void)
  845. {
  846. int i;
  847. struct arch_hw_breakpoint brk;
  848. for (i = 0; i < nr_wp_slots(); i++) {
  849. if (dabr[i].enabled) {
  850. brk.address = dabr[i].address;
  851. brk.type = (dabr[i].enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
  852. brk.len = 8;
  853. brk.hw_len = 8;
  854. __set_breakpoint(i, &brk);
  855. }
  856. }
  857. if (iabr)
  858. set_ciabr(iabr->address);
  859. }
  860. static void remove_bpts(void)
  861. {
  862. int i;
  863. struct bpt *bp;
  864. ppc_inst_t instr;
  865. bp = bpts;
  866. for (i = 0; i < NBPTS; ++i, ++bp) {
  867. if ((bp->enabled & (BP_TRAP|BP_CIABR)) != BP_TRAP)
  868. continue;
  869. if (mread_instr(bp->address, &instr)
  870. && ppc_inst_equal(instr, ppc_inst(bpinstr))
  871. && patch_instruction(
  872. (u32 *)bp->address, ppc_inst_read(bp->instr)) != 0)
  873. printf("Couldn't remove breakpoint at %lx\n",
  874. bp->address);
  875. }
  876. }
  877. static void remove_cpu_bpts(void)
  878. {
  879. hw_breakpoint_disable();
  880. write_ciabr(0);
  881. }
  882. /* Based on uptime_proc_show(). */
  883. static void
  884. show_uptime(void)
  885. {
  886. struct timespec64 uptime;
  887. if (setjmp(bus_error_jmp) == 0) {
  888. catch_memory_errors = 1;
  889. sync();
  890. ktime_get_coarse_boottime_ts64(&uptime);
  891. printf("Uptime: %lu.%.2lu seconds\n", (unsigned long)uptime.tv_sec,
  892. ((unsigned long)uptime.tv_nsec / (NSEC_PER_SEC/100)));
  893. sync();
  894. __delay(200); \
  895. }
  896. catch_memory_errors = 0;
  897. }
  898. static void set_lpp_cmd(void)
  899. {
  900. unsigned long lpp;
  901. if (!scanhex(&lpp)) {
  902. printf("Invalid number.\n");
  903. lpp = 0;
  904. }
  905. xmon_set_pagination_lpp(lpp);
  906. }
  907. /* Command interpreting routine */
  908. static char *last_cmd;
  909. static int
  910. cmds(struct pt_regs *excp)
  911. {
  912. int cmd = 0;
  913. last_cmd = NULL;
  914. xmon_regs = excp;
  915. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  916. for(;;) {
  917. #ifdef CONFIG_SMP
  918. printf("%x:", smp_processor_id());
  919. #endif /* CONFIG_SMP */
  920. printf("mon> ");
  921. flush_input();
  922. termch = 0;
  923. cmd = skipbl();
  924. if( cmd == '\n' ) {
  925. if (last_cmd == NULL)
  926. continue;
  927. take_input(last_cmd);
  928. last_cmd = NULL;
  929. cmd = inchar();
  930. }
  931. switch (cmd) {
  932. case 'm':
  933. cmd = inchar();
  934. switch (cmd) {
  935. case 'm':
  936. case 's':
  937. case 'd':
  938. memops(cmd);
  939. break;
  940. case 'l':
  941. memlocate();
  942. break;
  943. case 'z':
  944. if (xmon_is_ro) {
  945. printf(xmon_ro_msg);
  946. break;
  947. }
  948. memzcan();
  949. break;
  950. case 'i':
  951. show_mem();
  952. break;
  953. default:
  954. termch = cmd;
  955. memex();
  956. }
  957. break;
  958. case 'd':
  959. dump();
  960. break;
  961. case 'l':
  962. symbol_lookup();
  963. break;
  964. case 'r':
  965. prregs(excp); /* print regs */
  966. break;
  967. case 'e':
  968. excprint(excp);
  969. break;
  970. case 'S':
  971. super_regs();
  972. break;
  973. case 't':
  974. backtrace(excp);
  975. break;
  976. case 'f':
  977. cacheflush();
  978. break;
  979. case 's':
  980. if (do_step(excp))
  981. return cmd;
  982. break;
  983. case 'x':
  984. case 'X':
  985. if (tracing_enabled)
  986. tracing_on();
  987. return cmd;
  988. case EOF:
  989. printf(" <no input ...>\n");
  990. mdelay(2000);
  991. return cmd;
  992. case '?':
  993. xmon_puts(help_string);
  994. break;
  995. case '#':
  996. set_lpp_cmd();
  997. break;
  998. case 'b':
  999. bpt_cmds();
  1000. break;
  1001. case 'C':
  1002. csum();
  1003. break;
  1004. case 'c':
  1005. if (cpu_cmd())
  1006. return 0;
  1007. break;
  1008. case 'z':
  1009. bootcmds();
  1010. break;
  1011. case 'p':
  1012. if (xmon_is_ro) {
  1013. printf(xmon_ro_msg);
  1014. break;
  1015. }
  1016. proccall();
  1017. break;
  1018. case 'P':
  1019. show_tasks();
  1020. break;
  1021. #if defined(CONFIG_PPC_BOOK3S_32) || defined(CONFIG_PPC_64S_HASH_MMU)
  1022. case 'u':
  1023. dump_segments();
  1024. break;
  1025. #elif defined(CONFIG_44x)
  1026. case 'u':
  1027. dump_tlb_44x();
  1028. break;
  1029. #elif defined(CONFIG_PPC_BOOK3E_64)
  1030. case 'u':
  1031. dump_tlb_book3e();
  1032. break;
  1033. #endif
  1034. case 'U':
  1035. show_uptime();
  1036. break;
  1037. default:
  1038. printf("Unrecognized command: ");
  1039. do {
  1040. if (' ' < cmd && cmd <= '~')
  1041. putchar(cmd);
  1042. else
  1043. printf("\\x%x", cmd);
  1044. cmd = inchar();
  1045. } while (cmd != '\n');
  1046. printf(" (type ? for help)\n");
  1047. break;
  1048. }
  1049. }
  1050. }
  1051. #ifdef CONFIG_BOOKE
  1052. static int do_step(struct pt_regs *regs)
  1053. {
  1054. regs_set_return_msr(regs, regs->msr | MSR_DE);
  1055. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
  1056. return 1;
  1057. }
  1058. #else
  1059. /*
  1060. * Step a single instruction.
  1061. * Some instructions we emulate, others we execute with MSR_SE set.
  1062. */
  1063. static int do_step(struct pt_regs *regs)
  1064. {
  1065. ppc_inst_t instr;
  1066. int stepped;
  1067. force_enable_xmon();
  1068. /* check we are in 64-bit kernel mode, translation enabled */
  1069. if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
  1070. if (mread_instr(regs->nip, &instr)) {
  1071. stepped = emulate_step(regs, instr);
  1072. if (stepped < 0) {
  1073. printf("Couldn't single-step %s instruction\n",
  1074. (IS_RFID(instr)? "rfid": "mtmsrd"));
  1075. return 0;
  1076. }
  1077. if (stepped > 0) {
  1078. set_trap(regs, 0xd00);
  1079. printf("stepped to ");
  1080. xmon_print_symbol(regs->nip, " ", "\n");
  1081. ppc_inst_dump(regs->nip, 1, 0);
  1082. return 0;
  1083. }
  1084. }
  1085. }
  1086. regs_set_return_msr(regs, regs->msr | MSR_SE);
  1087. return 1;
  1088. }
  1089. #endif
  1090. static void bootcmds(void)
  1091. {
  1092. char tmp[64];
  1093. int cmd;
  1094. cmd = inchar();
  1095. if (cmd == 'r') {
  1096. getstring(tmp, 64);
  1097. ppc_md.restart(tmp);
  1098. } else if (cmd == 'h') {
  1099. ppc_md.halt();
  1100. } else if (cmd == 'p') {
  1101. do_kernel_power_off();
  1102. }
  1103. }
  1104. #ifdef CONFIG_SMP
  1105. static int xmon_switch_cpu(unsigned long cpu)
  1106. {
  1107. int timeout;
  1108. xmon_taken = 0;
  1109. mb();
  1110. xmon_owner = cpu;
  1111. timeout = 10000000;
  1112. while (!xmon_taken) {
  1113. if (--timeout == 0) {
  1114. if (test_and_set_bit(0, &xmon_taken))
  1115. break;
  1116. /* take control back */
  1117. mb();
  1118. xmon_owner = smp_processor_id();
  1119. printf("cpu 0x%lx didn't take control\n", cpu);
  1120. return 0;
  1121. }
  1122. barrier();
  1123. }
  1124. return 1;
  1125. }
  1126. static int xmon_batch_next_cpu(void)
  1127. {
  1128. unsigned long cpu;
  1129. for_each_cpu_wrap(cpu, &xmon_batch_cpus, xmon_batch_start_cpu) {
  1130. if (xmon_batch_start_cpu == -1)
  1131. xmon_batch_start_cpu = cpu;
  1132. if (xmon_switch_cpu(cpu))
  1133. return 0;
  1134. cpumask_clear_cpu(cpu, &xmon_batch_cpus);
  1135. }
  1136. xmon_batch = 0;
  1137. printf("%x:mon> \n", smp_processor_id());
  1138. return 1;
  1139. }
  1140. static int batch_cmds(struct pt_regs *excp)
  1141. {
  1142. int cmd;
  1143. /* simulate command entry */
  1144. cmd = xmon_batch;
  1145. termch = '\n';
  1146. last_cmd = NULL;
  1147. xmon_regs = excp;
  1148. printf("%x:", smp_processor_id());
  1149. printf("mon> ");
  1150. printf("%c\n", (char)cmd);
  1151. switch (cmd) {
  1152. case 'r':
  1153. prregs(excp); /* print regs */
  1154. break;
  1155. case 'S':
  1156. super_regs();
  1157. break;
  1158. case 't':
  1159. backtrace(excp);
  1160. break;
  1161. }
  1162. cpumask_clear_cpu(smp_processor_id(), &xmon_batch_cpus);
  1163. return xmon_batch_next_cpu();
  1164. }
  1165. static int cpu_cmd(void)
  1166. {
  1167. unsigned long cpu, first_cpu, last_cpu;
  1168. cpu = skipbl();
  1169. if (cpu == '#') {
  1170. xmon_batch = skipbl();
  1171. if (xmon_batch) {
  1172. switch (xmon_batch) {
  1173. case 'r':
  1174. case 'S':
  1175. case 't':
  1176. cpumask_copy(&xmon_batch_cpus, &cpus_in_xmon);
  1177. if (cpumask_weight(&xmon_batch_cpus) <= 1) {
  1178. printf("There are no other cpus in xmon\n");
  1179. break;
  1180. }
  1181. xmon_batch_start_cpu = -1;
  1182. if (!xmon_batch_next_cpu())
  1183. return 1;
  1184. break;
  1185. default:
  1186. printf("c# only supports 'r', 'S' and 't' commands\n");
  1187. }
  1188. xmon_batch = 0;
  1189. return 0;
  1190. }
  1191. }
  1192. termch = cpu;
  1193. if (!scanhex(&cpu) || cpu >= num_possible_cpus()) {
  1194. /* print cpus waiting or in xmon */
  1195. printf("cpus stopped:");
  1196. last_cpu = first_cpu = NR_CPUS;
  1197. for_each_possible_cpu(cpu) {
  1198. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  1199. if (cpu == last_cpu + 1) {
  1200. last_cpu = cpu;
  1201. } else {
  1202. if (last_cpu != first_cpu)
  1203. printf("-0x%lx", last_cpu);
  1204. last_cpu = first_cpu = cpu;
  1205. printf(" 0x%lx", cpu);
  1206. }
  1207. }
  1208. }
  1209. if (last_cpu != first_cpu)
  1210. printf("-0x%lx", last_cpu);
  1211. printf("\n");
  1212. return 0;
  1213. }
  1214. /* try to switch to cpu specified */
  1215. if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  1216. printf("cpu 0x%lx isn't in xmon\n", cpu);
  1217. #ifdef CONFIG_PPC64
  1218. printf("backtrace of paca[0x%lx].saved_r1 (possibly stale):\n", cpu);
  1219. xmon_show_stack(paca_ptrs[cpu]->saved_r1, 0, 0);
  1220. #endif
  1221. return 0;
  1222. }
  1223. return xmon_switch_cpu(cpu);
  1224. }
  1225. #else
  1226. static int cpu_cmd(void)
  1227. {
  1228. return 0;
  1229. }
  1230. #endif /* CONFIG_SMP */
  1231. static unsigned short fcstab[256] = {
  1232. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  1233. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  1234. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  1235. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  1236. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  1237. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  1238. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  1239. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  1240. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  1241. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  1242. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  1243. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  1244. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  1245. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  1246. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  1247. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  1248. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  1249. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  1250. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  1251. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  1252. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  1253. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  1254. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  1255. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  1256. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  1257. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  1258. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  1259. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  1260. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  1261. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  1262. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  1263. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  1264. };
  1265. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  1266. static void
  1267. csum(void)
  1268. {
  1269. unsigned int i;
  1270. unsigned short fcs;
  1271. unsigned char v;
  1272. if (!scanhex(&adrs))
  1273. return;
  1274. if (!scanhex(&ncsum))
  1275. return;
  1276. fcs = 0xffff;
  1277. for (i = 0; i < ncsum; ++i) {
  1278. if (mread(adrs+i, &v, 1) == 0) {
  1279. printf("csum stopped at "REG"\n", adrs+i);
  1280. break;
  1281. }
  1282. fcs = FCS(fcs, v);
  1283. }
  1284. printf("%x\n", fcs);
  1285. }
  1286. /*
  1287. * Check if this is a suitable place to put a breakpoint.
  1288. */
  1289. static long check_bp_loc(unsigned long addr)
  1290. {
  1291. ppc_inst_t instr;
  1292. addr &= ~3;
  1293. if (!is_kernel_addr(addr)) {
  1294. printf("Breakpoints may only be placed at kernel addresses\n");
  1295. return 0;
  1296. }
  1297. if (!mread_instr(addr, &instr)) {
  1298. printf("Can't read instruction at address %lx\n", addr);
  1299. return 0;
  1300. }
  1301. if (!can_single_step(ppc_inst_val(instr))) {
  1302. printf("Breakpoints may not be placed on instructions that can't be single stepped\n");
  1303. return 0;
  1304. }
  1305. return 1;
  1306. }
  1307. static int find_free_data_bpt(void)
  1308. {
  1309. int i;
  1310. for (i = 0; i < nr_wp_slots(); i++) {
  1311. if (!dabr[i].enabled)
  1312. return i;
  1313. }
  1314. printf("Couldn't find free breakpoint register\n");
  1315. return -1;
  1316. }
  1317. static void print_data_bpts(void)
  1318. {
  1319. int i;
  1320. for (i = 0; i < nr_wp_slots(); i++) {
  1321. if (!dabr[i].enabled)
  1322. continue;
  1323. printf(" data "REG" [", dabr[i].address);
  1324. if (dabr[i].enabled & 1)
  1325. printf("r");
  1326. if (dabr[i].enabled & 2)
  1327. printf("w");
  1328. printf("]\n");
  1329. }
  1330. }
  1331. static char *breakpoint_help_string =
  1332. "Breakpoint command usage:\n"
  1333. "b show breakpoints\n"
  1334. "b <addr> [cnt] set breakpoint at given instr addr\n"
  1335. "bc clear all breakpoints\n"
  1336. "bc <n/addr> clear breakpoint number n or at addr\n"
  1337. "bi <addr> [cnt] set hardware instr breakpoint (POWER8 only)\n"
  1338. "bd <addr> [cnt] set hardware data breakpoint\n"
  1339. "";
  1340. static void
  1341. bpt_cmds(void)
  1342. {
  1343. int cmd;
  1344. unsigned long a;
  1345. int i;
  1346. struct bpt *bp;
  1347. cmd = inchar();
  1348. switch (cmd) {
  1349. case 'd': { /* bd - hardware data breakpoint */
  1350. static const char badaddr[] = "Only kernel addresses are permitted for breakpoints\n";
  1351. int mode;
  1352. if (xmon_is_ro) {
  1353. printf(xmon_ro_msg);
  1354. break;
  1355. }
  1356. if (!ppc_breakpoint_available()) {
  1357. printf("Hardware data breakpoint not supported on this cpu\n");
  1358. break;
  1359. }
  1360. i = find_free_data_bpt();
  1361. if (i < 0)
  1362. break;
  1363. mode = 7;
  1364. cmd = inchar();
  1365. if (cmd == 'r')
  1366. mode = 5;
  1367. else if (cmd == 'w')
  1368. mode = 6;
  1369. else
  1370. termch = cmd;
  1371. dabr[i].address = 0;
  1372. dabr[i].enabled = 0;
  1373. if (scanhex(&dabr[i].address)) {
  1374. if (!is_kernel_addr(dabr[i].address)) {
  1375. printf(badaddr);
  1376. break;
  1377. }
  1378. dabr[i].address &= ~HW_BRK_TYPE_DABR;
  1379. dabr[i].enabled = mode | BP_DABR;
  1380. }
  1381. force_enable_xmon();
  1382. break;
  1383. }
  1384. case 'i': /* bi - hardware instr breakpoint */
  1385. if (xmon_is_ro) {
  1386. printf(xmon_ro_msg);
  1387. break;
  1388. }
  1389. if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
  1390. printf("Hardware instruction breakpoint "
  1391. "not supported on this cpu\n");
  1392. break;
  1393. }
  1394. if (iabr) {
  1395. iabr->enabled &= ~BP_CIABR;
  1396. iabr = NULL;
  1397. }
  1398. if (!scanhex(&a))
  1399. break;
  1400. if (!check_bp_loc(a))
  1401. break;
  1402. bp = new_breakpoint(a);
  1403. if (bp != NULL) {
  1404. bp->enabled |= BP_CIABR;
  1405. iabr = bp;
  1406. force_enable_xmon();
  1407. }
  1408. break;
  1409. case 'c':
  1410. if (!scanhex(&a)) {
  1411. /* clear all breakpoints */
  1412. for (i = 0; i < NBPTS; ++i)
  1413. bpts[i].enabled = 0;
  1414. iabr = NULL;
  1415. for (i = 0; i < nr_wp_slots(); i++)
  1416. dabr[i].enabled = 0;
  1417. printf("All breakpoints cleared\n");
  1418. break;
  1419. }
  1420. if (a <= NBPTS && a >= 1) {
  1421. /* assume a breakpoint number */
  1422. bp = &bpts[a-1]; /* bp nums are 1 based */
  1423. } else {
  1424. /* assume a breakpoint address */
  1425. bp = at_breakpoint(a);
  1426. if (bp == NULL) {
  1427. printf("No breakpoint at %lx\n", a);
  1428. break;
  1429. }
  1430. }
  1431. printf("Cleared breakpoint %tx (", BP_NUM(bp));
  1432. xmon_print_symbol(bp->address, " ", ")\n");
  1433. bp->enabled = 0;
  1434. break;
  1435. default:
  1436. termch = cmd;
  1437. cmd = skipbl();
  1438. if (cmd == '?') {
  1439. printf(breakpoint_help_string);
  1440. break;
  1441. }
  1442. termch = cmd;
  1443. if (xmon_is_ro || !scanhex(&a)) {
  1444. /* print all breakpoints */
  1445. printf(" type address\n");
  1446. print_data_bpts();
  1447. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1448. if (!bp->enabled)
  1449. continue;
  1450. printf("%tx %s ", BP_NUM(bp),
  1451. (bp->enabled & BP_CIABR) ? "inst": "trap");
  1452. xmon_print_symbol(bp->address, " ", "\n");
  1453. }
  1454. break;
  1455. }
  1456. if (!check_bp_loc(a))
  1457. break;
  1458. bp = new_breakpoint(a);
  1459. if (bp != NULL) {
  1460. bp->enabled |= BP_TRAP;
  1461. force_enable_xmon();
  1462. }
  1463. break;
  1464. }
  1465. }
  1466. /* Very cheap human name for vector lookup. */
  1467. static
  1468. const char *getvecname(unsigned long vec)
  1469. {
  1470. char *ret;
  1471. switch (vec) {
  1472. case 0x100: ret = "(System Reset)"; break;
  1473. case 0x200: ret = "(Machine Check)"; break;
  1474. case 0x300: ret = "(Data Access)"; break;
  1475. case 0x380:
  1476. if (radix_enabled())
  1477. ret = "(Data Access Out of Range)";
  1478. else
  1479. ret = "(Data SLB Access)";
  1480. break;
  1481. case 0x400: ret = "(Instruction Access)"; break;
  1482. case 0x480:
  1483. if (radix_enabled())
  1484. ret = "(Instruction Access Out of Range)";
  1485. else
  1486. ret = "(Instruction SLB Access)";
  1487. break;
  1488. case 0x500: ret = "(Hardware Interrupt)"; break;
  1489. case 0x600: ret = "(Alignment)"; break;
  1490. case 0x700: ret = "(Program Check)"; break;
  1491. case 0x800: ret = "(FPU Unavailable)"; break;
  1492. case 0x900: ret = "(Decrementer)"; break;
  1493. case 0x980: ret = "(Hypervisor Decrementer)"; break;
  1494. case 0xa00: ret = "(Doorbell)"; break;
  1495. case 0xc00: ret = "(System Call)"; break;
  1496. case 0xd00: ret = "(Single Step)"; break;
  1497. case 0xe40: ret = "(Emulation Assist)"; break;
  1498. case 0xe60: ret = "(HMI)"; break;
  1499. case 0xe80: ret = "(Hypervisor Doorbell)"; break;
  1500. case 0xf00: ret = "(Performance Monitor)"; break;
  1501. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1502. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1503. case 0x1500: ret = "(Denormalisation)"; break;
  1504. case 0x1700: ret = "(Altivec Assist)"; break;
  1505. case 0x3000: ret = "(System Call Vectored)"; break;
  1506. default: ret = "";
  1507. }
  1508. return ret;
  1509. }
  1510. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1511. unsigned long *endp)
  1512. {
  1513. unsigned long size, offset;
  1514. const char *name;
  1515. *startp = *endp = 0;
  1516. if (pc == 0)
  1517. return;
  1518. if (setjmp(bus_error_jmp) == 0) {
  1519. catch_memory_errors = 1;
  1520. sync();
  1521. name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
  1522. if (name != NULL) {
  1523. *startp = pc - offset;
  1524. *endp = pc - offset + size;
  1525. }
  1526. sync();
  1527. }
  1528. catch_memory_errors = 0;
  1529. }
  1530. #define LRSAVE_OFFSET (STACK_FRAME_LR_SAVE * sizeof(unsigned long))
  1531. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1532. unsigned long pc)
  1533. {
  1534. int max_to_print = 64;
  1535. unsigned long ip;
  1536. unsigned long newsp;
  1537. unsigned long marker;
  1538. struct pt_regs regs;
  1539. while (max_to_print--) {
  1540. if (!is_kernel_addr(sp)) {
  1541. if (sp != 0)
  1542. printf("SP (%lx) is in userspace\n", sp);
  1543. break;
  1544. }
  1545. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1546. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1547. printf("Couldn't read stack frame at %lx\n", sp);
  1548. break;
  1549. }
  1550. /*
  1551. * For the first stack frame, try to work out if
  1552. * LR and/or the saved LR value in the bottommost
  1553. * stack frame are valid.
  1554. */
  1555. if ((pc | lr) != 0) {
  1556. unsigned long fnstart, fnend;
  1557. unsigned long nextip;
  1558. int printip = 1;
  1559. get_function_bounds(pc, &fnstart, &fnend);
  1560. nextip = 0;
  1561. if (newsp > sp)
  1562. mread(newsp + LRSAVE_OFFSET, &nextip,
  1563. sizeof(unsigned long));
  1564. if (lr == ip) {
  1565. if (!is_kernel_addr(lr)
  1566. || (fnstart <= lr && lr < fnend))
  1567. printip = 0;
  1568. } else if (lr == nextip) {
  1569. printip = 0;
  1570. } else if (is_kernel_addr(lr)
  1571. && !(fnstart <= lr && lr < fnend)) {
  1572. printf("[link register ] ");
  1573. xmon_print_symbol(lr, " ", "\n");
  1574. }
  1575. if (printip) {
  1576. printf("["REG"] ", sp);
  1577. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1578. }
  1579. pc = lr = 0;
  1580. } else {
  1581. printf("["REG"] ", sp);
  1582. xmon_print_symbol(ip, " ", "\n");
  1583. }
  1584. /* Look for "regs" marker to see if this is
  1585. an exception frame. */
  1586. if (mread(sp + STACK_INT_FRAME_MARKER, &marker, sizeof(unsigned long))
  1587. && marker == STACK_FRAME_REGS_MARKER) {
  1588. if (mread(sp + STACK_INT_FRAME_REGS, &regs, sizeof(regs)) != sizeof(regs)) {
  1589. printf("Couldn't read registers at %lx\n",
  1590. sp + STACK_INT_FRAME_REGS);
  1591. break;
  1592. }
  1593. printf("---- Exception: %lx %s at ", regs.trap,
  1594. getvecname(TRAP(&regs)));
  1595. pc = regs.nip;
  1596. lr = regs.link;
  1597. xmon_print_symbol(pc, " ", "\n");
  1598. }
  1599. if (newsp == 0)
  1600. break;
  1601. sp = newsp;
  1602. }
  1603. }
  1604. static void backtrace(struct pt_regs *excp)
  1605. {
  1606. unsigned long sp;
  1607. if (scanhex(&sp))
  1608. xmon_show_stack(sp, 0, 0);
  1609. else
  1610. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1611. scannl();
  1612. }
  1613. static void print_bug_trap(struct pt_regs *regs)
  1614. {
  1615. #ifdef CONFIG_BUG
  1616. const struct bug_entry *bug;
  1617. unsigned long addr;
  1618. if (user_mode(regs))
  1619. return;
  1620. addr = regs->nip; /* address of trap instruction */
  1621. if (!is_kernel_addr(addr))
  1622. return;
  1623. bug = find_bug(regs->nip);
  1624. if (bug == NULL)
  1625. return;
  1626. if (is_warning_bug(bug))
  1627. return;
  1628. #ifdef CONFIG_DEBUG_BUGVERBOSE
  1629. printf("kernel BUG at %s:%u!\n",
  1630. (char *)bug + bug->file_disp, bug->line);
  1631. #else
  1632. printf("kernel BUG at %px!\n", (void *)bug + bug->bug_addr_disp);
  1633. #endif
  1634. #endif /* CONFIG_BUG */
  1635. }
  1636. static void excprint(struct pt_regs *fp)
  1637. {
  1638. unsigned long trap;
  1639. #ifdef CONFIG_SMP
  1640. printf("cpu 0x%x: ", smp_processor_id());
  1641. #endif /* CONFIG_SMP */
  1642. trap = TRAP(fp);
  1643. printf("Vector: %lx %s at [%px]\n", fp->trap, getvecname(trap), fp);
  1644. printf(" pc: ");
  1645. xmon_print_symbol(fp->nip, ": ", "\n");
  1646. printf(" lr: ");
  1647. xmon_print_symbol(fp->link, ": ", "\n");
  1648. printf(" sp: %lx\n", fp->gpr[1]);
  1649. printf(" msr: %lx\n", fp->msr);
  1650. if (trap == INTERRUPT_DATA_STORAGE ||
  1651. trap == INTERRUPT_DATA_SEGMENT ||
  1652. trap == INTERRUPT_ALIGNMENT ||
  1653. trap == INTERRUPT_MACHINE_CHECK) {
  1654. printf(" dar: %lx\n", fp->dar);
  1655. if (trap != INTERRUPT_DATA_SEGMENT)
  1656. printf(" dsisr: %lx\n", fp->dsisr);
  1657. }
  1658. printf(" current = 0x%px\n", current);
  1659. #ifdef CONFIG_PPC64
  1660. printf(" paca = 0x%px\t irqmask: 0x%02x\t irq_happened: 0x%02x\n",
  1661. local_paca, local_paca->irq_soft_mask, local_paca->irq_happened);
  1662. #endif
  1663. if (current) {
  1664. printf(" pid = %d, comm = %s\n",
  1665. current->pid, current->comm);
  1666. }
  1667. if (trap == INTERRUPT_PROGRAM)
  1668. print_bug_trap(fp);
  1669. printf(linux_banner);
  1670. }
  1671. static void prregs(struct pt_regs *fp)
  1672. {
  1673. int n, trap;
  1674. unsigned long base;
  1675. struct pt_regs regs;
  1676. if (scanhex(&base)) {
  1677. if (setjmp(bus_error_jmp) == 0) {
  1678. catch_memory_errors = 1;
  1679. sync();
  1680. regs = *(struct pt_regs *)base;
  1681. sync();
  1682. __delay(200);
  1683. } else {
  1684. catch_memory_errors = 0;
  1685. printf("*** Error reading registers from "REG"\n",
  1686. base);
  1687. return;
  1688. }
  1689. catch_memory_errors = 0;
  1690. fp = &regs;
  1691. }
  1692. #ifdef CONFIG_PPC64
  1693. #define R_PER_LINE 2
  1694. #else
  1695. #define R_PER_LINE 4
  1696. #endif
  1697. for (n = 0; n < 32; ++n) {
  1698. printf("R%.2d = "REG"%s", n, fp->gpr[n],
  1699. (n % R_PER_LINE) == R_PER_LINE - 1 ? "\n" : " ");
  1700. }
  1701. printf("pc = ");
  1702. xmon_print_symbol(fp->nip, " ", "\n");
  1703. if (!trap_is_syscall(fp) && cpu_has_feature(CPU_FTR_CFAR)) {
  1704. printf("cfar= ");
  1705. xmon_print_symbol(fp->orig_gpr3, " ", "\n");
  1706. }
  1707. printf("lr = ");
  1708. xmon_print_symbol(fp->link, " ", "\n");
  1709. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1710. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1711. fp->ctr, fp->xer, fp->trap);
  1712. trap = TRAP(fp);
  1713. if (trap == INTERRUPT_DATA_STORAGE ||
  1714. trap == INTERRUPT_DATA_SEGMENT ||
  1715. trap == INTERRUPT_ALIGNMENT)
  1716. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1717. }
  1718. static void cacheflush(void)
  1719. {
  1720. int cmd;
  1721. unsigned long nflush;
  1722. cmd = inchar();
  1723. if (cmd != 'i')
  1724. termch = cmd;
  1725. scanhex((void *)&adrs);
  1726. if (termch != '\n')
  1727. termch = 0;
  1728. nflush = 1;
  1729. scanhex(&nflush);
  1730. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1731. if (setjmp(bus_error_jmp) == 0) {
  1732. catch_memory_errors = 1;
  1733. sync();
  1734. if (cmd != 'i' || IS_ENABLED(CONFIG_PPC_BOOK3S_64)) {
  1735. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1736. cflush((void *) adrs);
  1737. } else {
  1738. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1739. cinval((void *) adrs);
  1740. }
  1741. sync();
  1742. /* wait a little while to see if we get a machine check */
  1743. __delay(200);
  1744. }
  1745. catch_memory_errors = 0;
  1746. }
  1747. extern unsigned long xmon_mfspr(int spr, unsigned long default_value);
  1748. extern void xmon_mtspr(int spr, unsigned long value);
  1749. static int
  1750. read_spr(int n, unsigned long *vp)
  1751. {
  1752. unsigned long ret = -1UL;
  1753. int ok = 0;
  1754. if (setjmp(bus_error_jmp) == 0) {
  1755. catch_spr_faults = 1;
  1756. sync();
  1757. ret = xmon_mfspr(n, *vp);
  1758. sync();
  1759. *vp = ret;
  1760. ok = 1;
  1761. }
  1762. catch_spr_faults = 0;
  1763. return ok;
  1764. }
  1765. static void
  1766. write_spr(int n, unsigned long val)
  1767. {
  1768. if (xmon_is_ro) {
  1769. printf(xmon_ro_msg);
  1770. return;
  1771. }
  1772. if (setjmp(bus_error_jmp) == 0) {
  1773. catch_spr_faults = 1;
  1774. sync();
  1775. xmon_mtspr(n, val);
  1776. sync();
  1777. } else {
  1778. printf("SPR 0x%03x (%4d) Faulted during write\n", n, n);
  1779. }
  1780. catch_spr_faults = 0;
  1781. }
  1782. static void dump_206_sprs(void)
  1783. {
  1784. #ifdef CONFIG_PPC64
  1785. if (!cpu_has_feature(CPU_FTR_ARCH_206))
  1786. return;
  1787. /* Actually some of these pre-date 2.06, but whatever */
  1788. printf("srr0 = %.16lx srr1 = %.16lx dsisr = %.8lx\n",
  1789. mfspr(SPRN_SRR0), mfspr(SPRN_SRR1), mfspr(SPRN_DSISR));
  1790. printf("dscr = %.16lx ppr = %.16lx pir = %.8lx\n",
  1791. mfspr(SPRN_DSCR), mfspr(SPRN_PPR), mfspr(SPRN_PIR));
  1792. printf("amr = %.16lx uamor = %.16lx\n",
  1793. mfspr(SPRN_AMR), mfspr(SPRN_UAMOR));
  1794. if (!(mfmsr() & MSR_HV))
  1795. return;
  1796. printf("sdr1 = %.16lx hdar = %.16lx hdsisr = %.8lx\n",
  1797. mfspr(SPRN_SDR1), mfspr(SPRN_HDAR), mfspr(SPRN_HDSISR));
  1798. printf("hsrr0 = %.16lx hsrr1 = %.16lx hdec = %.16lx\n",
  1799. mfspr(SPRN_HSRR0), mfspr(SPRN_HSRR1), mfspr(SPRN_HDEC));
  1800. printf("lpcr = %.16lx pcr = %.16lx lpidr = %.8lx\n",
  1801. mfspr(SPRN_LPCR), mfspr(SPRN_PCR), mfspr(SPRN_LPID));
  1802. printf("hsprg0 = %.16lx hsprg1 = %.16lx amor = %.16lx\n",
  1803. mfspr(SPRN_HSPRG0), mfspr(SPRN_HSPRG1), mfspr(SPRN_AMOR));
  1804. printf("dabr = %.16lx dabrx = %.16lx\n",
  1805. mfspr(SPRN_DABR), mfspr(SPRN_DABRX));
  1806. #endif
  1807. }
  1808. static void dump_207_sprs(void)
  1809. {
  1810. #ifdef CONFIG_PPC64
  1811. unsigned long msr;
  1812. if (!cpu_has_feature(CPU_FTR_ARCH_207S))
  1813. return;
  1814. printf("dpdes = %.16lx tir = %.16lx cir = %.8lx\n",
  1815. mfspr(SPRN_DPDES), mfspr(SPRN_TIR), mfspr(SPRN_CIR));
  1816. printf("fscr = %.16lx tar = %.16lx pspb = %.8lx\n",
  1817. mfspr(SPRN_FSCR), mfspr(SPRN_TAR), mfspr(SPRN_PSPB));
  1818. msr = mfmsr();
  1819. if (msr & MSR_TM) {
  1820. /* Only if TM has been enabled in the kernel */
  1821. printf("tfhar = %.16lx tfiar = %.16lx texasr = %.16lx\n",
  1822. mfspr(SPRN_TFHAR), mfspr(SPRN_TFIAR),
  1823. mfspr(SPRN_TEXASR));
  1824. }
  1825. printf("mmcr0 = %.16lx mmcr1 = %.16lx mmcr2 = %.16lx\n",
  1826. mfspr(SPRN_MMCR0), mfspr(SPRN_MMCR1), mfspr(SPRN_MMCR2));
  1827. printf("pmc1 = %.8lx pmc2 = %.8lx pmc3 = %.8lx pmc4 = %.8lx\n",
  1828. mfspr(SPRN_PMC1), mfspr(SPRN_PMC2),
  1829. mfspr(SPRN_PMC3), mfspr(SPRN_PMC4));
  1830. printf("mmcra = %.16lx siar = %.16lx pmc5 = %.8lx\n",
  1831. mfspr(SPRN_MMCRA), mfspr(SPRN_SIAR), mfspr(SPRN_PMC5));
  1832. printf("sdar = %.16lx sier = %.16lx pmc6 = %.8lx\n",
  1833. mfspr(SPRN_SDAR), mfspr(SPRN_SIER), mfspr(SPRN_PMC6));
  1834. printf("ebbhr = %.16lx ebbrr = %.16lx bescr = %.16lx\n",
  1835. mfspr(SPRN_EBBHR), mfspr(SPRN_EBBRR), mfspr(SPRN_BESCR));
  1836. printf("iamr = %.16lx\n", mfspr(SPRN_IAMR));
  1837. if (!(msr & MSR_HV))
  1838. return;
  1839. printf("hfscr = %.16lx dhdes = %.16lx rpr = %.16lx\n",
  1840. mfspr(SPRN_HFSCR), mfspr(SPRN_DHDES), mfspr(SPRN_RPR));
  1841. printf("dawr0 = %.16lx dawrx0 = %.16lx\n",
  1842. mfspr(SPRN_DAWR0), mfspr(SPRN_DAWRX0));
  1843. if (nr_wp_slots() > 1) {
  1844. printf("dawr1 = %.16lx dawrx1 = %.16lx\n",
  1845. mfspr(SPRN_DAWR1), mfspr(SPRN_DAWRX1));
  1846. }
  1847. printf("ciabr = %.16lx\n", mfspr(SPRN_CIABR));
  1848. #endif
  1849. }
  1850. static void dump_300_sprs(void)
  1851. {
  1852. #ifdef CONFIG_PPC64
  1853. bool hv = mfmsr() & MSR_HV;
  1854. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  1855. return;
  1856. if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
  1857. printf("pidr = %.16lx tidr = %.16lx\n",
  1858. mfspr(SPRN_PID), mfspr(SPRN_TIDR));
  1859. } else {
  1860. printf("pidr = %.16lx\n",
  1861. mfspr(SPRN_PID));
  1862. }
  1863. printf("psscr = %.16lx\n",
  1864. hv ? mfspr(SPRN_PSSCR) : mfspr(SPRN_PSSCR_PR));
  1865. if (!hv)
  1866. return;
  1867. printf("ptcr = %.16lx asdr = %.16lx\n",
  1868. mfspr(SPRN_PTCR), mfspr(SPRN_ASDR));
  1869. #endif
  1870. }
  1871. static void dump_310_sprs(void)
  1872. {
  1873. #ifdef CONFIG_PPC64
  1874. if (!cpu_has_feature(CPU_FTR_ARCH_31))
  1875. return;
  1876. printf("mmcr3 = %.16lx, sier2 = %.16lx, sier3 = %.16lx\n",
  1877. mfspr(SPRN_MMCR3), mfspr(SPRN_SIER2), mfspr(SPRN_SIER3));
  1878. #endif
  1879. }
  1880. static void dump_one_spr(int spr, bool show_unimplemented)
  1881. {
  1882. unsigned long val;
  1883. val = 0xdeadbeef;
  1884. if (!read_spr(spr, &val)) {
  1885. printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
  1886. return;
  1887. }
  1888. if (val == 0xdeadbeef) {
  1889. /* Looks like read was a nop, confirm */
  1890. val = 0x0badcafe;
  1891. if (!read_spr(spr, &val)) {
  1892. printf("SPR 0x%03x (%4d) Faulted during read\n", spr, spr);
  1893. return;
  1894. }
  1895. if (val == 0x0badcafe) {
  1896. if (show_unimplemented)
  1897. printf("SPR 0x%03x (%4d) Unimplemented\n", spr, spr);
  1898. return;
  1899. }
  1900. }
  1901. printf("SPR 0x%03x (%4d) = 0x%lx\n", spr, spr, val);
  1902. }
  1903. static void super_regs(void)
  1904. {
  1905. static unsigned long regno;
  1906. int cmd;
  1907. int spr;
  1908. cmd = skipbl();
  1909. switch (cmd) {
  1910. case '\n': {
  1911. unsigned long sp, toc;
  1912. asm("mr %0,1" : "=r" (sp) :);
  1913. asm("mr %0,2" : "=r" (toc) :);
  1914. printf("msr = "REG" sprg0 = "REG"\n",
  1915. mfmsr(), mfspr(SPRN_SPRG0));
  1916. printf("pvr = "REG" sprg1 = "REG"\n",
  1917. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1918. printf("dec = "REG" sprg2 = "REG"\n",
  1919. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1920. printf("sp = "REG" sprg3 = "REG"\n", sp, mfspr(SPRN_SPRG3));
  1921. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1922. dump_206_sprs();
  1923. dump_207_sprs();
  1924. dump_300_sprs();
  1925. dump_310_sprs();
  1926. return;
  1927. }
  1928. case 'w': {
  1929. unsigned long val;
  1930. scanhex(&regno);
  1931. val = 0;
  1932. read_spr(regno, &val);
  1933. scanhex(&val);
  1934. write_spr(regno, val);
  1935. dump_one_spr(regno, true);
  1936. break;
  1937. }
  1938. case 'r':
  1939. scanhex(&regno);
  1940. dump_one_spr(regno, true);
  1941. break;
  1942. case 'a':
  1943. /* dump ALL SPRs */
  1944. for (spr = 1; spr < 1024; ++spr)
  1945. dump_one_spr(spr, false);
  1946. break;
  1947. }
  1948. scannl();
  1949. }
  1950. /*
  1951. * Stuff for reading and writing memory safely
  1952. */
  1953. static int
  1954. mread(unsigned long adrs, void *buf, int size)
  1955. {
  1956. volatile int n;
  1957. char *p, *q;
  1958. n = 0;
  1959. if (setjmp(bus_error_jmp) == 0) {
  1960. catch_memory_errors = 1;
  1961. sync();
  1962. p = (char *)adrs;
  1963. q = (char *)buf;
  1964. switch (size) {
  1965. case 2:
  1966. *(u16 *)q = *(u16 *)p;
  1967. break;
  1968. case 4:
  1969. *(u32 *)q = *(u32 *)p;
  1970. break;
  1971. case 8:
  1972. *(u64 *)q = *(u64 *)p;
  1973. break;
  1974. default:
  1975. for( ; n < size; ++n) {
  1976. *q++ = *p++;
  1977. sync();
  1978. }
  1979. }
  1980. sync();
  1981. /* wait a little while to see if we get a machine check */
  1982. __delay(200);
  1983. n = size;
  1984. }
  1985. catch_memory_errors = 0;
  1986. return n;
  1987. }
  1988. static int
  1989. mwrite(unsigned long adrs, void *buf, int size)
  1990. {
  1991. volatile int n;
  1992. char *p, *q;
  1993. n = 0;
  1994. if (xmon_is_ro) {
  1995. printf(xmon_ro_msg);
  1996. return n;
  1997. }
  1998. if (setjmp(bus_error_jmp) == 0) {
  1999. catch_memory_errors = 1;
  2000. sync();
  2001. p = (char *) adrs;
  2002. q = (char *) buf;
  2003. switch (size) {
  2004. case 2:
  2005. *(u16 *)p = *(u16 *)q;
  2006. break;
  2007. case 4:
  2008. *(u32 *)p = *(u32 *)q;
  2009. break;
  2010. case 8:
  2011. *(u64 *)p = *(u64 *)q;
  2012. break;
  2013. default:
  2014. for ( ; n < size; ++n) {
  2015. *p++ = *q++;
  2016. sync();
  2017. }
  2018. }
  2019. sync();
  2020. /* wait a little while to see if we get a machine check */
  2021. __delay(200);
  2022. n = size;
  2023. } else {
  2024. printf("*** Error writing address "REG"\n", adrs + n);
  2025. }
  2026. catch_memory_errors = 0;
  2027. return n;
  2028. }
  2029. static int
  2030. mread_instr(unsigned long adrs, ppc_inst_t *instr)
  2031. {
  2032. volatile int n;
  2033. n = 0;
  2034. if (setjmp(bus_error_jmp) == 0) {
  2035. catch_memory_errors = 1;
  2036. sync();
  2037. *instr = ppc_inst_read((u32 *)adrs);
  2038. sync();
  2039. /* wait a little while to see if we get a machine check */
  2040. __delay(200);
  2041. n = ppc_inst_len(*instr);
  2042. }
  2043. catch_memory_errors = 0;
  2044. return n;
  2045. }
  2046. static int fault_type;
  2047. static int fault_except;
  2048. static char *fault_chars[] = { "--", "**", "##" };
  2049. static int handle_fault(struct pt_regs *regs)
  2050. {
  2051. fault_except = TRAP(regs);
  2052. switch (TRAP(regs)) {
  2053. case 0x200:
  2054. fault_type = 0;
  2055. break;
  2056. case 0x300:
  2057. case 0x380:
  2058. fault_type = 1;
  2059. break;
  2060. default:
  2061. fault_type = 2;
  2062. }
  2063. longjmp(bus_error_jmp, 1);
  2064. return 0;
  2065. }
  2066. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  2067. static void
  2068. byterev(unsigned char *val, int size)
  2069. {
  2070. int t;
  2071. switch (size) {
  2072. case 2:
  2073. SWAP(val[0], val[1], t);
  2074. break;
  2075. case 4:
  2076. SWAP(val[0], val[3], t);
  2077. SWAP(val[1], val[2], t);
  2078. break;
  2079. case 8: /* is there really any use for this? */
  2080. SWAP(val[0], val[7], t);
  2081. SWAP(val[1], val[6], t);
  2082. SWAP(val[2], val[5], t);
  2083. SWAP(val[3], val[4], t);
  2084. break;
  2085. }
  2086. }
  2087. static int brev;
  2088. static int mnoread;
  2089. static char *memex_help_string =
  2090. "Memory examine command usage:\n"
  2091. "m [addr] [flags] examine/change memory\n"
  2092. " addr is optional. will start where left off.\n"
  2093. " flags may include chars from this set:\n"
  2094. " b modify by bytes (default)\n"
  2095. " w modify by words (2 byte)\n"
  2096. " l modify by longs (4 byte)\n"
  2097. " d modify by doubleword (8 byte)\n"
  2098. " r toggle reverse byte order mode\n"
  2099. " n do not read memory (for i/o spaces)\n"
  2100. " . ok to read (default)\n"
  2101. "NOTE: flags are saved as defaults\n"
  2102. "";
  2103. static char *memex_subcmd_help_string =
  2104. "Memory examine subcommands:\n"
  2105. " hexval write this val to current location\n"
  2106. " 'string' write chars from string to this location\n"
  2107. " ' increment address\n"
  2108. " ^ decrement address\n"
  2109. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  2110. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  2111. " ` clear no-read flag\n"
  2112. " ; stay at this addr\n"
  2113. " v change to byte mode\n"
  2114. " w change to word (2 byte) mode\n"
  2115. " l change to long (4 byte) mode\n"
  2116. " u change to doubleword (8 byte) mode\n"
  2117. " m addr change current addr\n"
  2118. " n toggle no-read flag\n"
  2119. " r toggle byte reverse flag\n"
  2120. " < count back up count bytes\n"
  2121. " > count skip forward count bytes\n"
  2122. " x exit this mode\n"
  2123. "";
  2124. static void
  2125. memex(void)
  2126. {
  2127. int cmd, inc, i, nslash;
  2128. unsigned long n;
  2129. unsigned char val[16];
  2130. scanhex((void *)&adrs);
  2131. cmd = skipbl();
  2132. if (cmd == '?') {
  2133. printf(memex_help_string);
  2134. return;
  2135. } else {
  2136. termch = cmd;
  2137. }
  2138. last_cmd = "m\n";
  2139. while ((cmd = skipbl()) != '\n') {
  2140. switch( cmd ){
  2141. case 'b': size = 1; break;
  2142. case 'w': size = 2; break;
  2143. case 'l': size = 4; break;
  2144. case 'd': size = 8; break;
  2145. case 'r': brev = !brev; break;
  2146. case 'n': mnoread = 1; break;
  2147. case '.': mnoread = 0; break;
  2148. }
  2149. }
  2150. if( size <= 0 )
  2151. size = 1;
  2152. else if( size > 8 )
  2153. size = 8;
  2154. for(;;){
  2155. if (!mnoread)
  2156. n = mread(adrs, val, size);
  2157. printf(REG"%c", adrs, brev? 'r': ' ');
  2158. if (!mnoread) {
  2159. if (brev)
  2160. byterev(val, size);
  2161. putchar(' ');
  2162. for (i = 0; i < n; ++i)
  2163. printf("%.2x", val[i]);
  2164. for (; i < size; ++i)
  2165. printf("%s", fault_chars[fault_type]);
  2166. }
  2167. putchar(' ');
  2168. inc = size;
  2169. nslash = 0;
  2170. for(;;){
  2171. if( scanhex(&n) ){
  2172. for (i = 0; i < size; ++i)
  2173. val[i] = n >> (i * 8);
  2174. if (!brev)
  2175. byterev(val, size);
  2176. mwrite(adrs, val, size);
  2177. inc = size;
  2178. }
  2179. cmd = skipbl();
  2180. if (cmd == '\n')
  2181. break;
  2182. inc = 0;
  2183. switch (cmd) {
  2184. case '\'':
  2185. for(;;){
  2186. n = inchar();
  2187. if( n == '\\' )
  2188. n = bsesc();
  2189. else if( n == '\'' )
  2190. break;
  2191. for (i = 0; i < size; ++i)
  2192. val[i] = n >> (i * 8);
  2193. if (!brev)
  2194. byterev(val, size);
  2195. mwrite(adrs, val, size);
  2196. adrs += size;
  2197. }
  2198. adrs -= size;
  2199. inc = size;
  2200. break;
  2201. case ',':
  2202. adrs += size;
  2203. break;
  2204. case '.':
  2205. mnoread = 0;
  2206. break;
  2207. case ';':
  2208. break;
  2209. case 'x':
  2210. case EOF:
  2211. scannl();
  2212. return;
  2213. case 'b':
  2214. case 'v':
  2215. size = 1;
  2216. break;
  2217. case 'w':
  2218. size = 2;
  2219. break;
  2220. case 'l':
  2221. size = 4;
  2222. break;
  2223. case 'u':
  2224. size = 8;
  2225. break;
  2226. case '^':
  2227. adrs -= size;
  2228. break;
  2229. case '/':
  2230. if (nslash > 0)
  2231. adrs -= 1 << nslash;
  2232. else
  2233. nslash = 0;
  2234. nslash += 4;
  2235. adrs += 1 << nslash;
  2236. break;
  2237. case '\\':
  2238. if (nslash < 0)
  2239. adrs += 1 << -nslash;
  2240. else
  2241. nslash = 0;
  2242. nslash -= 4;
  2243. adrs -= 1 << -nslash;
  2244. break;
  2245. case 'm':
  2246. scanhex((void *)&adrs);
  2247. break;
  2248. case 'n':
  2249. mnoread = 1;
  2250. break;
  2251. case 'r':
  2252. brev = !brev;
  2253. break;
  2254. case '<':
  2255. n = size;
  2256. scanhex(&n);
  2257. adrs -= n;
  2258. break;
  2259. case '>':
  2260. n = size;
  2261. scanhex(&n);
  2262. adrs += n;
  2263. break;
  2264. case '?':
  2265. printf(memex_subcmd_help_string);
  2266. break;
  2267. }
  2268. }
  2269. adrs += inc;
  2270. }
  2271. }
  2272. static int
  2273. bsesc(void)
  2274. {
  2275. int c;
  2276. c = inchar();
  2277. switch( c ){
  2278. case 'n': c = '\n'; break;
  2279. case 'r': c = '\r'; break;
  2280. case 'b': c = '\b'; break;
  2281. case 't': c = '\t'; break;
  2282. }
  2283. return c;
  2284. }
  2285. static void xmon_rawdump (unsigned long adrs, long ndump)
  2286. {
  2287. long n, m, r, nr;
  2288. unsigned char temp[16];
  2289. for (n = ndump; n > 0;) {
  2290. r = n < 16? n: 16;
  2291. nr = mread(adrs, temp, r);
  2292. adrs += nr;
  2293. for (m = 0; m < r; ++m) {
  2294. if (m < nr)
  2295. printf("%.2x", temp[m]);
  2296. else
  2297. printf("%s", fault_chars[fault_type]);
  2298. }
  2299. n -= r;
  2300. if (nr < r)
  2301. break;
  2302. }
  2303. printf("\n");
  2304. }
  2305. static void dump_tracing(void)
  2306. {
  2307. int c;
  2308. c = inchar();
  2309. if (c == 'c')
  2310. ftrace_dump(DUMP_ORIG);
  2311. else
  2312. ftrace_dump(DUMP_ALL);
  2313. }
  2314. #ifdef CONFIG_PPC64
  2315. static void dump_one_paca(int cpu)
  2316. {
  2317. struct paca_struct *p;
  2318. #ifdef CONFIG_PPC_64S_HASH_MMU
  2319. int i = 0;
  2320. #endif
  2321. if (setjmp(bus_error_jmp) != 0) {
  2322. printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
  2323. return;
  2324. }
  2325. catch_memory_errors = 1;
  2326. sync();
  2327. p = paca_ptrs[cpu];
  2328. printf("paca for cpu 0x%x @ %px:\n", cpu, p);
  2329. printf(" %-*s = %s\n", 25, "possible", str_yes_no(cpu_possible(cpu)));
  2330. printf(" %-*s = %s\n", 25, "present", str_yes_no(cpu_present(cpu)));
  2331. printf(" %-*s = %s\n", 25, "online", str_yes_no(cpu_online(cpu)));
  2332. #define DUMP(paca, name, format) \
  2333. printf(" %-*s = "format"\t(0x%lx)\n", 25, #name, 18, paca->name, \
  2334. offsetof(struct paca_struct, name));
  2335. DUMP(p, lock_token, "%#-*x");
  2336. DUMP(p, paca_index, "%#-*x");
  2337. #ifndef CONFIG_PPC_KERNEL_PCREL
  2338. DUMP(p, kernel_toc, "%#-*llx");
  2339. #endif
  2340. DUMP(p, kernelbase, "%#-*llx");
  2341. DUMP(p, kernel_msr, "%#-*llx");
  2342. DUMP(p, emergency_sp, "%-*px");
  2343. #ifdef CONFIG_PPC_BOOK3S_64
  2344. DUMP(p, nmi_emergency_sp, "%-*px");
  2345. DUMP(p, mc_emergency_sp, "%-*px");
  2346. DUMP(p, in_nmi, "%#-*x");
  2347. DUMP(p, in_mce, "%#-*x");
  2348. DUMP(p, hmi_event_available, "%#-*x");
  2349. #endif
  2350. DUMP(p, data_offset, "%#-*llx");
  2351. DUMP(p, hw_cpu_id, "%#-*x");
  2352. DUMP(p, cpu_start, "%#-*x");
  2353. DUMP(p, kexec_state, "%#-*x");
  2354. #ifdef CONFIG_PPC_BOOK3S_64
  2355. #ifdef CONFIG_PPC_64S_HASH_MMU
  2356. if (!early_radix_enabled()) {
  2357. for (i = 0; i < SLB_NUM_BOLTED; i++) {
  2358. u64 esid, vsid;
  2359. if (!p->slb_shadow_ptr)
  2360. continue;
  2361. esid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].esid);
  2362. vsid = be64_to_cpu(p->slb_shadow_ptr->save_area[i].vsid);
  2363. if (esid || vsid) {
  2364. printf(" %-*s[%d] = 0x%016llx 0x%016llx\n",
  2365. 22, "slb_shadow", i, esid, vsid);
  2366. }
  2367. }
  2368. DUMP(p, vmalloc_sllp, "%#-*x");
  2369. DUMP(p, stab_rr, "%#-*x");
  2370. DUMP(p, slb_used_bitmap, "%#-*x");
  2371. DUMP(p, slb_kern_bitmap, "%#-*x");
  2372. if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
  2373. DUMP(p, slb_cache_ptr, "%#-*x");
  2374. for (i = 0; i < SLB_CACHE_ENTRIES; i++)
  2375. printf(" %-*s[%d] = 0x%016x\n",
  2376. 22, "slb_cache", i, p->slb_cache[i]);
  2377. }
  2378. }
  2379. #endif
  2380. DUMP(p, rfi_flush_fallback_area, "%-*px");
  2381. #endif
  2382. DUMP(p, dscr_default, "%#-*llx");
  2383. #ifdef CONFIG_PPC_BOOK3E_64
  2384. DUMP(p, pgd, "%-*px");
  2385. DUMP(p, kernel_pgd, "%-*px");
  2386. DUMP(p, tcd_ptr, "%-*px");
  2387. DUMP(p, mc_kstack, "%-*px");
  2388. DUMP(p, crit_kstack, "%-*px");
  2389. DUMP(p, dbg_kstack, "%-*px");
  2390. #endif
  2391. DUMP(p, __current, "%-*px");
  2392. DUMP(p, kstack, "%#-*llx");
  2393. printf(" %-*s = 0x%016llx\n", 25, "kstack_base", p->kstack & ~(THREAD_SIZE - 1));
  2394. #ifdef CONFIG_STACKPROTECTOR
  2395. DUMP(p, canary, "%#-*lx");
  2396. #endif
  2397. DUMP(p, saved_r1, "%#-*llx");
  2398. #ifdef CONFIG_PPC_BOOK3E_64
  2399. DUMP(p, trap_save, "%#-*x");
  2400. #endif
  2401. DUMP(p, irq_soft_mask, "%#-*x");
  2402. DUMP(p, irq_happened, "%#-*x");
  2403. #ifdef CONFIG_MMIOWB
  2404. DUMP(p, mmiowb_state.nesting_count, "%#-*x");
  2405. DUMP(p, mmiowb_state.mmiowb_pending, "%#-*x");
  2406. #endif
  2407. DUMP(p, irq_work_pending, "%#-*x");
  2408. DUMP(p, sprg_vdso, "%#-*llx");
  2409. #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
  2410. DUMP(p, tm_scratch, "%#-*llx");
  2411. #endif
  2412. #ifdef CONFIG_PPC_POWERNV
  2413. DUMP(p, idle_state, "%#-*lx");
  2414. if (!early_cpu_has_feature(CPU_FTR_ARCH_300)) {
  2415. DUMP(p, thread_idle_state, "%#-*x");
  2416. DUMP(p, subcore_sibling_mask, "%#-*x");
  2417. } else {
  2418. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  2419. DUMP(p, requested_psscr, "%#-*llx");
  2420. DUMP(p, dont_stop.counter, "%#-*x");
  2421. #endif
  2422. }
  2423. #endif
  2424. DUMP(p, accounting.utime, "%#-*lx");
  2425. DUMP(p, accounting.stime, "%#-*lx");
  2426. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  2427. DUMP(p, accounting.utime_scaled, "%#-*lx");
  2428. #endif
  2429. DUMP(p, accounting.starttime, "%#-*lx");
  2430. DUMP(p, accounting.starttime_user, "%#-*lx");
  2431. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  2432. DUMP(p, accounting.startspurr, "%#-*lx");
  2433. DUMP(p, accounting.utime_sspurr, "%#-*lx");
  2434. #endif
  2435. DUMP(p, accounting.steal_time, "%#-*lx");
  2436. #undef DUMP
  2437. catch_memory_errors = 0;
  2438. sync();
  2439. }
  2440. static void dump_all_pacas(void)
  2441. {
  2442. int cpu;
  2443. if (num_possible_cpus() == 0) {
  2444. printf("No possible cpus, use 'dp #' to dump individual cpus\n");
  2445. return;
  2446. }
  2447. for_each_possible_cpu(cpu)
  2448. dump_one_paca(cpu);
  2449. }
  2450. static void dump_pacas(void)
  2451. {
  2452. unsigned long num;
  2453. int c;
  2454. c = inchar();
  2455. if (c == 'a') {
  2456. dump_all_pacas();
  2457. return;
  2458. }
  2459. termch = c; /* Put c back, it wasn't 'a' */
  2460. if (scanhex(&num) && num < num_possible_cpus())
  2461. dump_one_paca(num);
  2462. else
  2463. dump_one_paca(xmon_owner);
  2464. }
  2465. #endif
  2466. #ifdef CONFIG_PPC_POWERNV
  2467. static void dump_one_xive(int cpu)
  2468. {
  2469. unsigned int hwid = get_hard_smp_processor_id(cpu);
  2470. bool hv = cpu_has_feature(CPU_FTR_HVMODE);
  2471. if (hv) {
  2472. opal_xive_dump(XIVE_DUMP_TM_HYP, hwid);
  2473. opal_xive_dump(XIVE_DUMP_TM_POOL, hwid);
  2474. opal_xive_dump(XIVE_DUMP_TM_OS, hwid);
  2475. opal_xive_dump(XIVE_DUMP_TM_USER, hwid);
  2476. opal_xive_dump(XIVE_DUMP_VP, hwid);
  2477. opal_xive_dump(XIVE_DUMP_EMU_STATE, hwid);
  2478. }
  2479. if (setjmp(bus_error_jmp) != 0) {
  2480. catch_memory_errors = 0;
  2481. printf("*** Error dumping xive on cpu %d\n", cpu);
  2482. return;
  2483. }
  2484. catch_memory_errors = 1;
  2485. sync();
  2486. xmon_xive_do_dump(cpu);
  2487. sync();
  2488. __delay(200);
  2489. catch_memory_errors = 0;
  2490. }
  2491. static void dump_all_xives(void)
  2492. {
  2493. int cpu;
  2494. if (num_online_cpus() == 0) {
  2495. printf("No possible cpus, use 'dx #' to dump individual cpus\n");
  2496. return;
  2497. }
  2498. for_each_online_cpu(cpu)
  2499. dump_one_xive(cpu);
  2500. }
  2501. static void dump_xives(void)
  2502. {
  2503. unsigned long num;
  2504. int c;
  2505. if (!xive_enabled()) {
  2506. printf("Xive disabled on this system\n");
  2507. return;
  2508. }
  2509. c = inchar();
  2510. if (c == 'a') {
  2511. dump_all_xives();
  2512. return;
  2513. } else if (c == 'i') {
  2514. if (scanhex(&num))
  2515. xmon_xive_get_irq_config(num, NULL);
  2516. else
  2517. xmon_xive_get_irq_all();
  2518. return;
  2519. }
  2520. termch = c; /* Put c back, it wasn't 'a' */
  2521. if (scanhex(&num) && num < num_possible_cpus())
  2522. dump_one_xive(num);
  2523. else
  2524. dump_one_xive(xmon_owner);
  2525. }
  2526. #endif /* CONFIG_PPC_POWERNV */
  2527. static void dump_by_size(unsigned long addr, long count, int size)
  2528. {
  2529. unsigned char temp[16];
  2530. int i, j;
  2531. u64 val;
  2532. count = ALIGN(count, 16);
  2533. for (i = 0; i < count; i += 16, addr += 16) {
  2534. printf(REG, addr);
  2535. if (mread(addr, temp, 16) != 16) {
  2536. printf("\nFaulted reading %d bytes from 0x"REG"\n", 16, addr);
  2537. return;
  2538. }
  2539. for (j = 0; j < 16; j += size) {
  2540. putchar(' ');
  2541. switch (size) {
  2542. case 1: val = temp[j]; break;
  2543. case 2: val = *(u16 *)&temp[j]; break;
  2544. case 4: val = *(u32 *)&temp[j]; break;
  2545. case 8: val = *(u64 *)&temp[j]; break;
  2546. default: val = 0;
  2547. }
  2548. printf("%0*llx", size * 2, val);
  2549. }
  2550. printf(" |");
  2551. for (j = 0; j < 16; ++j) {
  2552. val = temp[j];
  2553. putchar(' ' <= val && val <= '~' ? val : '.');
  2554. }
  2555. printf("|\n");
  2556. }
  2557. }
  2558. static void
  2559. dump(void)
  2560. {
  2561. static char last[] = { "d?\n" };
  2562. int c;
  2563. c = inchar();
  2564. #ifdef CONFIG_PPC64
  2565. if (c == 'p') {
  2566. xmon_start_pagination();
  2567. dump_pacas();
  2568. xmon_end_pagination();
  2569. return;
  2570. }
  2571. #endif
  2572. #ifdef CONFIG_PPC_POWERNV
  2573. if (c == 'x') {
  2574. xmon_start_pagination();
  2575. dump_xives();
  2576. xmon_end_pagination();
  2577. return;
  2578. }
  2579. #endif
  2580. if (c == 't') {
  2581. dump_tracing();
  2582. return;
  2583. }
  2584. if (c == '\n')
  2585. termch = c;
  2586. scanhex((void *)&adrs);
  2587. if (termch != '\n')
  2588. termch = 0;
  2589. if (c == 'i') {
  2590. scanhex(&nidump);
  2591. if (nidump == 0)
  2592. nidump = 16;
  2593. else if (nidump > MAX_IDUMP)
  2594. nidump = MAX_IDUMP;
  2595. adrs += ppc_inst_dump(adrs, nidump, 1);
  2596. last_cmd = "di\n";
  2597. } else if (c == 'l') {
  2598. dump_log_buf();
  2599. } else if (c == 'o') {
  2600. dump_opal_msglog();
  2601. } else if (c == 'v') {
  2602. /* dump virtual to physical translation */
  2603. show_pte(adrs);
  2604. } else if (c == 'r') {
  2605. scanhex(&ndump);
  2606. if (ndump == 0)
  2607. ndump = 64;
  2608. xmon_rawdump(adrs, ndump);
  2609. adrs += ndump;
  2610. last_cmd = "dr\n";
  2611. } else {
  2612. scanhex(&ndump);
  2613. if (ndump == 0)
  2614. ndump = 64;
  2615. else if (ndump > MAX_DUMP)
  2616. ndump = MAX_DUMP;
  2617. switch (c) {
  2618. case '8':
  2619. case '4':
  2620. case '2':
  2621. case '1':
  2622. ndump = ALIGN(ndump, 16);
  2623. dump_by_size(adrs, ndump, c - '0');
  2624. last[1] = c;
  2625. last_cmd = last;
  2626. break;
  2627. default:
  2628. prdump(adrs, ndump);
  2629. last_cmd = "d\n";
  2630. }
  2631. adrs += ndump;
  2632. }
  2633. }
  2634. static void
  2635. prdump(unsigned long adrs, long ndump)
  2636. {
  2637. long n, m, c, r, nr;
  2638. unsigned char temp[16];
  2639. for (n = ndump; n > 0;) {
  2640. printf(REG, adrs);
  2641. putchar(' ');
  2642. r = n < 16? n: 16;
  2643. nr = mread(adrs, temp, r);
  2644. adrs += nr;
  2645. for (m = 0; m < r; ++m) {
  2646. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  2647. putchar(' ');
  2648. if (m < nr)
  2649. printf("%.2x", temp[m]);
  2650. else
  2651. printf("%s", fault_chars[fault_type]);
  2652. }
  2653. for (; m < 16; ++m) {
  2654. if ((m & (sizeof(long) - 1)) == 0)
  2655. putchar(' ');
  2656. printf(" ");
  2657. }
  2658. printf(" |");
  2659. for (m = 0; m < r; ++m) {
  2660. if (m < nr) {
  2661. c = temp[m];
  2662. putchar(' ' <= c && c <= '~'? c: '.');
  2663. } else
  2664. putchar(' ');
  2665. }
  2666. n -= r;
  2667. for (; m < 16; ++m)
  2668. putchar(' ');
  2669. printf("|\n");
  2670. if (nr < r)
  2671. break;
  2672. }
  2673. }
  2674. typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
  2675. static int
  2676. generic_inst_dump(unsigned long adr, long count, int praddr,
  2677. instruction_dump_func dump_func)
  2678. {
  2679. int nr, dotted;
  2680. unsigned long first_adr;
  2681. ppc_inst_t inst, last_inst = ppc_inst(0);
  2682. dotted = 0;
  2683. for (first_adr = adr; count > 0; --count, adr += ppc_inst_len(inst)) {
  2684. nr = mread_instr(adr, &inst);
  2685. if (nr == 0) {
  2686. if (praddr) {
  2687. const char *x = fault_chars[fault_type];
  2688. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  2689. }
  2690. break;
  2691. }
  2692. if (adr > first_adr && ppc_inst_equal(inst, last_inst)) {
  2693. if (!dotted) {
  2694. printf(" ...\n");
  2695. dotted = 1;
  2696. }
  2697. continue;
  2698. }
  2699. dotted = 0;
  2700. last_inst = inst;
  2701. if (praddr)
  2702. printf(REG" %08lx", adr, ppc_inst_as_ulong(inst));
  2703. printf("\t");
  2704. if (!ppc_inst_prefixed(inst))
  2705. dump_func(ppc_inst_val(inst), adr);
  2706. else
  2707. dump_func(ppc_inst_as_ulong(inst), adr);
  2708. printf("\n");
  2709. }
  2710. return adr - first_adr;
  2711. }
  2712. static int
  2713. ppc_inst_dump(unsigned long adr, long count, int praddr)
  2714. {
  2715. return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
  2716. }
  2717. void
  2718. print_address(unsigned long addr)
  2719. {
  2720. xmon_print_symbol(addr, "\t# ", "");
  2721. }
  2722. static void
  2723. dump_log_buf(void)
  2724. {
  2725. struct kmsg_dump_iter iter;
  2726. static unsigned char buf[1024];
  2727. size_t len;
  2728. if (setjmp(bus_error_jmp) != 0) {
  2729. printf("Error dumping printk buffer!\n");
  2730. return;
  2731. }
  2732. catch_memory_errors = 1;
  2733. sync();
  2734. kmsg_dump_rewind(&iter);
  2735. xmon_start_pagination();
  2736. while (kmsg_dump_get_line(&iter, false, buf, sizeof(buf), &len)) {
  2737. buf[len] = '\0';
  2738. printf("%s", buf);
  2739. }
  2740. xmon_end_pagination();
  2741. sync();
  2742. /* wait a little while to see if we get a machine check */
  2743. __delay(200);
  2744. catch_memory_errors = 0;
  2745. }
  2746. #ifdef CONFIG_PPC_POWERNV
  2747. static void dump_opal_msglog(void)
  2748. {
  2749. unsigned char buf[128];
  2750. ssize_t res;
  2751. volatile loff_t pos = 0;
  2752. if (!firmware_has_feature(FW_FEATURE_OPAL)) {
  2753. printf("Machine is not running OPAL firmware.\n");
  2754. return;
  2755. }
  2756. if (setjmp(bus_error_jmp) != 0) {
  2757. printf("Error dumping OPAL msglog!\n");
  2758. return;
  2759. }
  2760. catch_memory_errors = 1;
  2761. sync();
  2762. xmon_start_pagination();
  2763. while ((res = opal_msglog_copy(buf, pos, sizeof(buf) - 1))) {
  2764. if (res < 0) {
  2765. printf("Error dumping OPAL msglog! Error: %zd\n", res);
  2766. break;
  2767. }
  2768. buf[res] = '\0';
  2769. printf("%s", buf);
  2770. pos += res;
  2771. }
  2772. xmon_end_pagination();
  2773. sync();
  2774. /* wait a little while to see if we get a machine check */
  2775. __delay(200);
  2776. catch_memory_errors = 0;
  2777. }
  2778. #endif
  2779. /*
  2780. * Memory operations - move, set, print differences
  2781. */
  2782. static unsigned long mdest; /* destination address */
  2783. static unsigned long msrc; /* source address */
  2784. static unsigned long mval; /* byte value to set memory to */
  2785. static unsigned long mcount; /* # bytes to affect */
  2786. static unsigned long mdiffs; /* max # differences to print */
  2787. static void
  2788. memops(int cmd)
  2789. {
  2790. scanhex((void *)&mdest);
  2791. if( termch != '\n' )
  2792. termch = 0;
  2793. scanhex((void *)(cmd == 's'? &mval: &msrc));
  2794. if( termch != '\n' )
  2795. termch = 0;
  2796. scanhex((void *)&mcount);
  2797. switch( cmd ){
  2798. case 'm':
  2799. if (xmon_is_ro) {
  2800. printf(xmon_ro_msg);
  2801. break;
  2802. }
  2803. memmove((void *)mdest, (void *)msrc, mcount);
  2804. break;
  2805. case 's':
  2806. if (xmon_is_ro) {
  2807. printf(xmon_ro_msg);
  2808. break;
  2809. }
  2810. memset((void *)mdest, mval, mcount);
  2811. break;
  2812. case 'd':
  2813. if( termch != '\n' )
  2814. termch = 0;
  2815. scanhex((void *)&mdiffs);
  2816. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  2817. break;
  2818. }
  2819. }
  2820. static void
  2821. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  2822. {
  2823. unsigned n, prt;
  2824. prt = 0;
  2825. for( n = nb; n > 0; --n )
  2826. if( *p1++ != *p2++ )
  2827. if( ++prt <= maxpr )
  2828. printf("%px %.2x # %px %.2x\n", p1 - 1,
  2829. p1[-1], p2 - 1, p2[-1]);
  2830. if( prt > maxpr )
  2831. printf("Total of %d differences\n", prt);
  2832. }
  2833. static unsigned mend;
  2834. static unsigned mask;
  2835. static void
  2836. memlocate(void)
  2837. {
  2838. unsigned a, n;
  2839. unsigned char val[4];
  2840. last_cmd = "ml";
  2841. scanhex((void *)&mdest);
  2842. if (termch != '\n') {
  2843. termch = 0;
  2844. scanhex((void *)&mend);
  2845. if (termch != '\n') {
  2846. termch = 0;
  2847. scanhex((void *)&mval);
  2848. mask = ~0;
  2849. if (termch != '\n') termch = 0;
  2850. scanhex((void *)&mask);
  2851. }
  2852. }
  2853. n = 0;
  2854. for (a = mdest; a < mend; a += 4) {
  2855. if (mread(a, val, 4) == 4
  2856. && ((GETWORD(val) ^ mval) & mask) == 0) {
  2857. printf("%.16x: %.16x\n", a, GETWORD(val));
  2858. if (++n >= 10)
  2859. break;
  2860. }
  2861. }
  2862. }
  2863. static unsigned long mskip = 0x1000;
  2864. static unsigned long mlim = 0xffffffff;
  2865. static void
  2866. memzcan(void)
  2867. {
  2868. unsigned char v;
  2869. unsigned a;
  2870. int ok, ook;
  2871. scanhex(&mdest);
  2872. if (termch != '\n') termch = 0;
  2873. scanhex(&mskip);
  2874. if (termch != '\n') termch = 0;
  2875. scanhex(&mlim);
  2876. ook = 0;
  2877. for (a = mdest; a < mlim; a += mskip) {
  2878. ok = mread(a, &v, 1);
  2879. if (ok && !ook) {
  2880. printf("%.8x .. ", a);
  2881. } else if (!ok && ook)
  2882. printf("%.8lx\n", a - mskip);
  2883. ook = ok;
  2884. if (a + mskip < a)
  2885. break;
  2886. }
  2887. if (ook)
  2888. printf("%.8lx\n", a - mskip);
  2889. }
  2890. static void show_task(struct task_struct *volatile tsk)
  2891. {
  2892. unsigned int p_state = READ_ONCE(tsk->__state);
  2893. char state;
  2894. /*
  2895. * Cloned from kdb_task_state_char(), which is not entirely
  2896. * appropriate for calling from xmon. This could be moved
  2897. * to a common, generic, routine used by both.
  2898. */
  2899. state = (p_state == TASK_RUNNING) ? 'R' :
  2900. (p_state & TASK_UNINTERRUPTIBLE) ? 'D' :
  2901. (p_state & TASK_STOPPED) ? 'T' :
  2902. (p_state & TASK_TRACED) ? 'C' :
  2903. (tsk->exit_state & EXIT_ZOMBIE) ? 'Z' :
  2904. (tsk->exit_state & EXIT_DEAD) ? 'E' :
  2905. (p_state & TASK_INTERRUPTIBLE) ? 'S' : '?';
  2906. printf("%16px %16lx %16px %6d %6d %c %2d %s\n", tsk,
  2907. tsk->thread.ksp, tsk->thread.regs,
  2908. tsk->pid, rcu_dereference(tsk->parent)->pid,
  2909. state, task_cpu(tsk),
  2910. tsk->comm);
  2911. }
  2912. #ifdef CONFIG_PPC_BOOK3S_64
  2913. static void format_pte(void *ptep, unsigned long pte)
  2914. {
  2915. pte_t entry = __pte(pte);
  2916. printf("ptep @ 0x%016lx = 0x%016lx\n", (unsigned long)ptep, pte);
  2917. printf("Maps physical address = 0x%016lx\n", pte & PTE_RPN_MASK);
  2918. printf("Flags = %s%s%s%s%s\n",
  2919. pte_young(entry) ? "Accessed " : "",
  2920. pte_dirty(entry) ? "Dirty " : "",
  2921. pte_read(entry) ? "Read " : "",
  2922. pte_write(entry) ? "Write " : "",
  2923. pte_exec(entry) ? "Exec " : "");
  2924. }
  2925. static void show_pte(unsigned long addr)
  2926. {
  2927. unsigned long tskv = 0;
  2928. struct task_struct *volatile tsk = NULL;
  2929. struct mm_struct *volatile mm;
  2930. pgd_t *pgdp;
  2931. p4d_t *p4dp;
  2932. pud_t *pudp;
  2933. pmd_t *pmdp;
  2934. pte_t *ptep;
  2935. if (!scanhex(&tskv))
  2936. mm = &init_mm;
  2937. else
  2938. tsk = (struct task_struct *)tskv;
  2939. if (tsk == NULL)
  2940. mm = &init_mm;
  2941. else
  2942. mm = tsk->active_mm;
  2943. if (setjmp(bus_error_jmp) != 0) {
  2944. catch_memory_errors = 0;
  2945. printf("*** Error dumping pte for task %px\n", tsk);
  2946. return;
  2947. }
  2948. catch_memory_errors = 1;
  2949. sync();
  2950. if (mm == &init_mm)
  2951. pgdp = pgd_offset_k(addr);
  2952. else
  2953. pgdp = pgd_offset(mm, addr);
  2954. p4dp = p4d_offset(pgdp, addr);
  2955. if (p4d_none(*p4dp)) {
  2956. printf("No valid P4D\n");
  2957. return;
  2958. }
  2959. if (p4d_leaf(*p4dp)) {
  2960. format_pte(p4dp, p4d_val(*p4dp));
  2961. return;
  2962. }
  2963. printf("p4dp @ 0x%px = 0x%016lx\n", p4dp, p4d_val(*p4dp));
  2964. pudp = pud_offset(p4dp, addr);
  2965. if (pud_none(*pudp)) {
  2966. printf("No valid PUD\n");
  2967. return;
  2968. }
  2969. if (pud_leaf(*pudp)) {
  2970. format_pte(pudp, pud_val(*pudp));
  2971. return;
  2972. }
  2973. printf("pudp @ 0x%px = 0x%016lx\n", pudp, pud_val(*pudp));
  2974. pmdp = pmd_offset(pudp, addr);
  2975. if (pmd_none(*pmdp)) {
  2976. printf("No valid PMD\n");
  2977. return;
  2978. }
  2979. if (pmd_leaf(*pmdp)) {
  2980. format_pte(pmdp, pmd_val(*pmdp));
  2981. return;
  2982. }
  2983. printf("pmdp @ 0x%px = 0x%016lx\n", pmdp, pmd_val(*pmdp));
  2984. ptep = pte_offset_map(pmdp, addr);
  2985. if (!ptep || pte_none(*ptep)) {
  2986. if (ptep)
  2987. pte_unmap(ptep);
  2988. printf("no valid PTE\n");
  2989. return;
  2990. }
  2991. format_pte(ptep, pte_val(*ptep));
  2992. pte_unmap(ptep);
  2993. sync();
  2994. __delay(200);
  2995. catch_memory_errors = 0;
  2996. }
  2997. #else
  2998. static void show_pte(unsigned long addr)
  2999. {
  3000. printf("show_pte not yet implemented\n");
  3001. }
  3002. #endif /* CONFIG_PPC_BOOK3S_64 */
  3003. static void show_tasks(void)
  3004. {
  3005. unsigned long tskv;
  3006. struct task_struct *volatile tsk = NULL;
  3007. printf(" task_struct ->thread.ksp ->thread.regs PID PPID S P CMD\n");
  3008. if (scanhex(&tskv))
  3009. tsk = (struct task_struct *)tskv;
  3010. if (setjmp(bus_error_jmp) != 0) {
  3011. catch_memory_errors = 0;
  3012. printf("*** Error dumping task %px\n", tsk);
  3013. return;
  3014. }
  3015. catch_memory_errors = 1;
  3016. sync();
  3017. if (tsk)
  3018. show_task(tsk);
  3019. else
  3020. for_each_process(tsk)
  3021. show_task(tsk);
  3022. sync();
  3023. __delay(200);
  3024. catch_memory_errors = 0;
  3025. }
  3026. static void proccall(void)
  3027. {
  3028. unsigned long args[8];
  3029. unsigned long ret;
  3030. int i;
  3031. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  3032. unsigned long, unsigned long, unsigned long,
  3033. unsigned long, unsigned long, unsigned long);
  3034. callfunc_t func;
  3035. if (!scanhex(&adrs))
  3036. return;
  3037. if (termch != '\n')
  3038. termch = 0;
  3039. for (i = 0; i < 8; ++i)
  3040. args[i] = 0;
  3041. for (i = 0; i < 8; ++i) {
  3042. if (!scanhex(&args[i]) || termch == '\n')
  3043. break;
  3044. termch = 0;
  3045. }
  3046. func = (callfunc_t) adrs;
  3047. ret = 0;
  3048. if (setjmp(bus_error_jmp) == 0) {
  3049. catch_memory_errors = 1;
  3050. sync();
  3051. ret = func(args[0], args[1], args[2], args[3],
  3052. args[4], args[5], args[6], args[7]);
  3053. sync();
  3054. printf("return value is 0x%lx\n", ret);
  3055. } else {
  3056. printf("*** %x exception occurred\n", fault_except);
  3057. }
  3058. catch_memory_errors = 0;
  3059. }
  3060. /* Input scanning routines */
  3061. int
  3062. skipbl(void)
  3063. {
  3064. int c;
  3065. if( termch != 0 ){
  3066. c = termch;
  3067. termch = 0;
  3068. } else
  3069. c = inchar();
  3070. while( c == ' ' || c == '\t' )
  3071. c = inchar();
  3072. return c;
  3073. }
  3074. #define N_PTREGS 44
  3075. static const char *regnames[N_PTREGS] = {
  3076. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  3077. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  3078. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  3079. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  3080. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  3081. #ifdef CONFIG_PPC64
  3082. "softe",
  3083. #else
  3084. "mq",
  3085. #endif
  3086. "trap", "dar", "dsisr", "res"
  3087. };
  3088. int
  3089. scanhex(unsigned long *vp)
  3090. {
  3091. int c, d;
  3092. unsigned long v;
  3093. c = skipbl();
  3094. if (c == '%') {
  3095. /* parse register name */
  3096. char regname[8];
  3097. int i;
  3098. for (i = 0; i < sizeof(regname) - 1; ++i) {
  3099. c = inchar();
  3100. if (!isalnum(c)) {
  3101. termch = c;
  3102. break;
  3103. }
  3104. regname[i] = c;
  3105. }
  3106. regname[i] = 0;
  3107. i = match_string(regnames, N_PTREGS, regname);
  3108. if (i < 0) {
  3109. printf("invalid register name '%%%s'\n", regname);
  3110. return 0;
  3111. }
  3112. if (xmon_regs == NULL) {
  3113. printf("regs not available\n");
  3114. return 0;
  3115. }
  3116. *vp = ((unsigned long *)xmon_regs)[i];
  3117. return 1;
  3118. }
  3119. /* skip leading "0x" if any */
  3120. if (c == '0') {
  3121. c = inchar();
  3122. if (c == 'x') {
  3123. c = inchar();
  3124. } else {
  3125. d = hexdigit(c);
  3126. if (d == EOF) {
  3127. termch = c;
  3128. *vp = 0;
  3129. return 1;
  3130. }
  3131. }
  3132. } else if (c == '$') {
  3133. int i;
  3134. for (i = 0; i < (KSYM_NAME_LEN - 1); i++) {
  3135. c = inchar();
  3136. if (isspace(c) || c == '\0') {
  3137. termch = c;
  3138. break;
  3139. }
  3140. tmpstr[i] = c;
  3141. }
  3142. tmpstr[i++] = 0;
  3143. *vp = 0;
  3144. if (setjmp(bus_error_jmp) == 0) {
  3145. catch_memory_errors = 1;
  3146. sync();
  3147. *vp = kallsyms_lookup_name(tmpstr);
  3148. sync();
  3149. }
  3150. catch_memory_errors = 0;
  3151. if (!(*vp)) {
  3152. printf("unknown symbol '%s'\n", tmpstr);
  3153. return 0;
  3154. }
  3155. return 1;
  3156. }
  3157. d = hexdigit(c);
  3158. if (d == EOF) {
  3159. termch = c;
  3160. return 0;
  3161. }
  3162. v = 0;
  3163. do {
  3164. v = (v << 4) + d;
  3165. c = inchar();
  3166. d = hexdigit(c);
  3167. } while (d != EOF);
  3168. termch = c;
  3169. *vp = v;
  3170. return 1;
  3171. }
  3172. static void
  3173. scannl(void)
  3174. {
  3175. int c;
  3176. c = termch;
  3177. termch = 0;
  3178. while( c != '\n' )
  3179. c = inchar();
  3180. }
  3181. static int hexdigit(int c)
  3182. {
  3183. if( '0' <= c && c <= '9' )
  3184. return c - '0';
  3185. if( 'A' <= c && c <= 'F' )
  3186. return c - ('A' - 10);
  3187. if( 'a' <= c && c <= 'f' )
  3188. return c - ('a' - 10);
  3189. return EOF;
  3190. }
  3191. void
  3192. getstring(char *s, int size)
  3193. {
  3194. int c;
  3195. c = skipbl();
  3196. if (c == '\n') {
  3197. *s = 0;
  3198. return;
  3199. }
  3200. do {
  3201. if( size > 1 ){
  3202. *s++ = c;
  3203. --size;
  3204. }
  3205. c = inchar();
  3206. } while( c != ' ' && c != '\t' && c != '\n' );
  3207. termch = c;
  3208. *s = 0;
  3209. }
  3210. static char line[256];
  3211. static char *lineptr;
  3212. static void
  3213. flush_input(void)
  3214. {
  3215. lineptr = NULL;
  3216. }
  3217. static int
  3218. inchar(void)
  3219. {
  3220. if (lineptr == NULL || *lineptr == 0) {
  3221. if (xmon_gets(line, sizeof(line)) == NULL) {
  3222. lineptr = NULL;
  3223. return EOF;
  3224. }
  3225. lineptr = line;
  3226. }
  3227. return *lineptr++;
  3228. }
  3229. static void
  3230. take_input(char *str)
  3231. {
  3232. lineptr = str;
  3233. }
  3234. static void
  3235. symbol_lookup(void)
  3236. {
  3237. int type = inchar();
  3238. unsigned long addr, cpu;
  3239. void __percpu *ptr = NULL;
  3240. static char tmp[KSYM_NAME_LEN];
  3241. switch (type) {
  3242. case 'a':
  3243. if (scanhex(&addr))
  3244. xmon_print_symbol(addr, ": ", "\n");
  3245. termch = 0;
  3246. break;
  3247. case 's':
  3248. getstring(tmp, KSYM_NAME_LEN);
  3249. if (setjmp(bus_error_jmp) == 0) {
  3250. catch_memory_errors = 1;
  3251. sync();
  3252. addr = kallsyms_lookup_name(tmp);
  3253. if (addr)
  3254. printf("%s: %lx\n", tmp, addr);
  3255. else
  3256. printf("Symbol '%s' not found.\n", tmp);
  3257. sync();
  3258. }
  3259. catch_memory_errors = 0;
  3260. termch = 0;
  3261. break;
  3262. case 'p':
  3263. getstring(tmp, KSYM_NAME_LEN);
  3264. if (setjmp(bus_error_jmp) == 0) {
  3265. catch_memory_errors = 1;
  3266. sync();
  3267. ptr = (void __percpu *)kallsyms_lookup_name(tmp);
  3268. sync();
  3269. }
  3270. if (ptr &&
  3271. ptr >= (void __percpu *)__per_cpu_start &&
  3272. ptr < (void __percpu *)__per_cpu_end)
  3273. {
  3274. if (scanhex(&cpu) && cpu < num_possible_cpus()) {
  3275. addr = (unsigned long)per_cpu_ptr(ptr, cpu);
  3276. } else {
  3277. cpu = raw_smp_processor_id();
  3278. addr = (unsigned long)this_cpu_ptr(ptr);
  3279. }
  3280. printf("%s for cpu 0x%lx: %lx\n", tmp, cpu, addr);
  3281. } else {
  3282. printf("Percpu symbol '%s' not found.\n", tmp);
  3283. }
  3284. catch_memory_errors = 0;
  3285. termch = 0;
  3286. break;
  3287. }
  3288. }
  3289. /* Print an address in numeric and symbolic form (if possible) */
  3290. static void xmon_print_symbol(unsigned long address, const char *mid,
  3291. const char *after)
  3292. {
  3293. char *modname;
  3294. const char *volatile name = NULL;
  3295. unsigned long offset, size;
  3296. printf(REG, address);
  3297. if (setjmp(bus_error_jmp) == 0) {
  3298. catch_memory_errors = 1;
  3299. sync();
  3300. name = kallsyms_lookup(address, &size, &offset, &modname,
  3301. tmpstr);
  3302. sync();
  3303. /* wait a little while to see if we get a machine check */
  3304. __delay(200);
  3305. }
  3306. catch_memory_errors = 0;
  3307. if (name) {
  3308. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  3309. if (modname)
  3310. printf(" [%s]", modname);
  3311. }
  3312. printf("%s", after);
  3313. }
  3314. #ifdef CONFIG_PPC_64S_HASH_MMU
  3315. void dump_segments(void)
  3316. {
  3317. int i;
  3318. unsigned long esid,vsid;
  3319. unsigned long llp;
  3320. printf("SLB contents of cpu 0x%x\n", smp_processor_id());
  3321. for (i = 0; i < mmu_slb_size; i++) {
  3322. asm volatile("slbmfee %0,%1" : "=r" (esid) : "r" (i));
  3323. asm volatile("slbmfev %0,%1" : "=r" (vsid) : "r" (i));
  3324. if (!esid && !vsid)
  3325. continue;
  3326. printf("%02d %016lx %016lx", i, esid, vsid);
  3327. if (!(esid & SLB_ESID_V)) {
  3328. printf("\n");
  3329. continue;
  3330. }
  3331. llp = vsid & SLB_VSID_LLP;
  3332. if (vsid & SLB_VSID_B_1T) {
  3333. printf(" 1T ESID=%9lx VSID=%13lx LLP:%3lx \n",
  3334. GET_ESID_1T(esid),
  3335. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
  3336. llp);
  3337. } else {
  3338. printf(" 256M ESID=%9lx VSID=%13lx LLP:%3lx \n",
  3339. GET_ESID(esid),
  3340. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
  3341. llp);
  3342. }
  3343. }
  3344. }
  3345. #endif
  3346. #ifdef CONFIG_PPC_BOOK3S_32
  3347. void dump_segments(void)
  3348. {
  3349. int i;
  3350. printf("sr0-15 =");
  3351. for (i = 0; i < 16; ++i)
  3352. printf(" %x", mfsr(i << 28));
  3353. printf("\n");
  3354. }
  3355. #endif
  3356. #ifdef CONFIG_44x
  3357. static void dump_tlb_44x(void)
  3358. {
  3359. int i;
  3360. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  3361. unsigned long w0,w1,w2;
  3362. asm volatile("tlbre %0,%1,0" : "=r" (w0) : "r" (i));
  3363. asm volatile("tlbre %0,%1,1" : "=r" (w1) : "r" (i));
  3364. asm volatile("tlbre %0,%1,2" : "=r" (w2) : "r" (i));
  3365. printf("[%02x] %08lx %08lx %08lx ", i, w0, w1, w2);
  3366. if (w0 & PPC44x_TLB_VALID) {
  3367. printf("V %08lx -> %01lx%08lx %c%c%c%c%c",
  3368. w0 & PPC44x_TLB_EPN_MASK,
  3369. w1 & PPC44x_TLB_ERPN_MASK,
  3370. w1 & PPC44x_TLB_RPN_MASK,
  3371. (w2 & PPC44x_TLB_W) ? 'W' : 'w',
  3372. (w2 & PPC44x_TLB_I) ? 'I' : 'i',
  3373. (w2 & PPC44x_TLB_M) ? 'M' : 'm',
  3374. (w2 & PPC44x_TLB_G) ? 'G' : 'g',
  3375. (w2 & PPC44x_TLB_E) ? 'E' : 'e');
  3376. }
  3377. printf("\n");
  3378. }
  3379. }
  3380. #endif /* CONFIG_44x */
  3381. #ifdef CONFIG_PPC_BOOK3E_64
  3382. static void dump_tlb_book3e(void)
  3383. {
  3384. u32 mmucfg;
  3385. u64 ramask;
  3386. int i, tlb, ntlbs, pidsz, lpidsz, rasz;
  3387. int mmu_version;
  3388. static const char *pgsz_names[] = {
  3389. " 1K",
  3390. " 2K",
  3391. " 4K",
  3392. " 8K",
  3393. " 16K",
  3394. " 32K",
  3395. " 64K",
  3396. "128K",
  3397. "256K",
  3398. "512K",
  3399. " 1M",
  3400. " 2M",
  3401. " 4M",
  3402. " 8M",
  3403. " 16M",
  3404. " 32M",
  3405. " 64M",
  3406. "128M",
  3407. "256M",
  3408. "512M",
  3409. " 1G",
  3410. " 2G",
  3411. " 4G",
  3412. " 8G",
  3413. " 16G",
  3414. " 32G",
  3415. " 64G",
  3416. "128G",
  3417. "256G",
  3418. "512G",
  3419. " 1T",
  3420. " 2T",
  3421. };
  3422. /* Gather some infos about the MMU */
  3423. mmucfg = mfspr(SPRN_MMUCFG);
  3424. mmu_version = (mmucfg & 3) + 1;
  3425. ntlbs = ((mmucfg >> 2) & 3) + 1;
  3426. pidsz = ((mmucfg >> 6) & 0x1f) + 1;
  3427. lpidsz = (mmucfg >> 24) & 0xf;
  3428. rasz = (mmucfg >> 16) & 0x7f;
  3429. printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
  3430. mmu_version, ntlbs, pidsz, lpidsz, rasz);
  3431. ramask = (1ull << rasz) - 1;
  3432. for (tlb = 0; tlb < ntlbs; tlb++) {
  3433. u32 tlbcfg;
  3434. int nent, assoc, new_cc = 1;
  3435. printf("TLB %d:\n------\n", tlb);
  3436. switch(tlb) {
  3437. case 0:
  3438. tlbcfg = mfspr(SPRN_TLB0CFG);
  3439. break;
  3440. case 1:
  3441. tlbcfg = mfspr(SPRN_TLB1CFG);
  3442. break;
  3443. case 2:
  3444. tlbcfg = mfspr(SPRN_TLB2CFG);
  3445. break;
  3446. case 3:
  3447. tlbcfg = mfspr(SPRN_TLB3CFG);
  3448. break;
  3449. default:
  3450. printf("Unsupported TLB number !\n");
  3451. continue;
  3452. }
  3453. nent = tlbcfg & 0xfff;
  3454. assoc = (tlbcfg >> 24) & 0xff;
  3455. for (i = 0; i < nent; i++) {
  3456. u32 mas0 = MAS0_TLBSEL(tlb);
  3457. u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  3458. u64 mas2 = 0;
  3459. u64 mas7_mas3;
  3460. int esel = i, cc = i;
  3461. if (assoc != 0) {
  3462. cc = i / assoc;
  3463. esel = i % assoc;
  3464. mas2 = cc * 0x1000;
  3465. }
  3466. mas0 |= MAS0_ESEL(esel);
  3467. mtspr(SPRN_MAS0, mas0);
  3468. mtspr(SPRN_MAS1, mas1);
  3469. mtspr(SPRN_MAS2, mas2);
  3470. asm volatile("tlbre 0,0,0" : : : "memory");
  3471. mas1 = mfspr(SPRN_MAS1);
  3472. mas2 = mfspr(SPRN_MAS2);
  3473. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  3474. if (assoc && (i % assoc) == 0)
  3475. new_cc = 1;
  3476. if (!(mas1 & MAS1_VALID))
  3477. continue;
  3478. if (assoc == 0)
  3479. printf("%04x- ", i);
  3480. else if (new_cc)
  3481. printf("%04x-%c", cc, 'A' + esel);
  3482. else
  3483. printf(" |%c", 'A' + esel);
  3484. new_cc = 0;
  3485. printf(" %016llx %04x %s %c%c AS%c",
  3486. mas2 & ~0x3ffull,
  3487. (mas1 >> 16) & 0x3fff,
  3488. pgsz_names[(mas1 >> 7) & 0x1f],
  3489. mas1 & MAS1_IND ? 'I' : ' ',
  3490. mas1 & MAS1_IPROT ? 'P' : ' ',
  3491. mas1 & MAS1_TS ? '1' : '0');
  3492. printf(" %c%c%c%c%c%c%c",
  3493. mas2 & MAS2_X0 ? 'a' : ' ',
  3494. mas2 & MAS2_X1 ? 'v' : ' ',
  3495. mas2 & MAS2_W ? 'w' : ' ',
  3496. mas2 & MAS2_I ? 'i' : ' ',
  3497. mas2 & MAS2_M ? 'm' : ' ',
  3498. mas2 & MAS2_G ? 'g' : ' ',
  3499. mas2 & MAS2_E ? 'e' : ' ');
  3500. printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
  3501. if (mas1 & MAS1_IND)
  3502. printf(" %s\n",
  3503. pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
  3504. else
  3505. printf(" U%c%c%c S%c%c%c\n",
  3506. mas7_mas3 & MAS3_UX ? 'x' : ' ',
  3507. mas7_mas3 & MAS3_UW ? 'w' : ' ',
  3508. mas7_mas3 & MAS3_UR ? 'r' : ' ',
  3509. mas7_mas3 & MAS3_SX ? 'x' : ' ',
  3510. mas7_mas3 & MAS3_SW ? 'w' : ' ',
  3511. mas7_mas3 & MAS3_SR ? 'r' : ' ');
  3512. }
  3513. }
  3514. }
  3515. #endif /* CONFIG_PPC_BOOK3E_64 */
  3516. static void xmon_init(int enable)
  3517. {
  3518. if (enable) {
  3519. __debugger = xmon;
  3520. __debugger_ipi = xmon_ipi;
  3521. __debugger_bpt = xmon_bpt;
  3522. __debugger_sstep = xmon_sstep;
  3523. __debugger_iabr_match = xmon_iabr_match;
  3524. __debugger_break_match = xmon_break_match;
  3525. __debugger_fault_handler = xmon_fault_handler;
  3526. } else {
  3527. __debugger = NULL;
  3528. __debugger_ipi = NULL;
  3529. __debugger_bpt = NULL;
  3530. __debugger_sstep = NULL;
  3531. __debugger_iabr_match = NULL;
  3532. __debugger_break_match = NULL;
  3533. __debugger_fault_handler = NULL;
  3534. }
  3535. }
  3536. #ifdef CONFIG_MAGIC_SYSRQ
  3537. static void sysrq_handle_xmon(u8 key)
  3538. {
  3539. if (xmon_is_locked_down()) {
  3540. clear_all_bpt();
  3541. xmon_init(0);
  3542. return;
  3543. }
  3544. /* ensure xmon is enabled */
  3545. xmon_init(1);
  3546. debugger(get_irq_regs());
  3547. if (!xmon_on)
  3548. xmon_init(0);
  3549. }
  3550. static const struct sysrq_key_op sysrq_xmon_op = {
  3551. .handler = sysrq_handle_xmon,
  3552. .help_msg = "xmon(x)",
  3553. .action_msg = "Entering xmon",
  3554. };
  3555. static int __init setup_xmon_sysrq(void)
  3556. {
  3557. register_sysrq_key('x', &sysrq_xmon_op);
  3558. return 0;
  3559. }
  3560. device_initcall(setup_xmon_sysrq);
  3561. #endif /* CONFIG_MAGIC_SYSRQ */
  3562. static void clear_all_bpt(void)
  3563. {
  3564. int i;
  3565. /* clear/unpatch all breakpoints */
  3566. remove_bpts();
  3567. remove_cpu_bpts();
  3568. /* Disable all breakpoints */
  3569. for (i = 0; i < NBPTS; ++i)
  3570. bpts[i].enabled = 0;
  3571. /* Clear any data or iabr breakpoints */
  3572. iabr = NULL;
  3573. for (i = 0; i < nr_wp_slots(); i++)
  3574. dabr[i].enabled = 0;
  3575. }
  3576. #ifdef CONFIG_DEBUG_FS
  3577. static int xmon_dbgfs_set(void *data, u64 val)
  3578. {
  3579. xmon_on = !!val;
  3580. xmon_init(xmon_on);
  3581. /* make sure all breakpoints removed when disabling */
  3582. if (!xmon_on) {
  3583. clear_all_bpt();
  3584. get_output_lock();
  3585. printf("xmon: All breakpoints cleared\n");
  3586. release_output_lock();
  3587. }
  3588. return 0;
  3589. }
  3590. static int xmon_dbgfs_get(void *data, u64 *val)
  3591. {
  3592. *val = xmon_on;
  3593. return 0;
  3594. }
  3595. DEFINE_SIMPLE_ATTRIBUTE(xmon_dbgfs_ops, xmon_dbgfs_get,
  3596. xmon_dbgfs_set, "%llu\n");
  3597. static int __init setup_xmon_dbgfs(void)
  3598. {
  3599. debugfs_create_file("xmon", 0600, arch_debugfs_dir, NULL,
  3600. &xmon_dbgfs_ops);
  3601. return 0;
  3602. }
  3603. device_initcall(setup_xmon_dbgfs);
  3604. #endif /* CONFIG_DEBUG_FS */
  3605. static int xmon_early __initdata;
  3606. static int __init early_parse_xmon(char *p)
  3607. {
  3608. if (xmon_is_locked_down()) {
  3609. xmon_init(0);
  3610. xmon_early = 0;
  3611. xmon_on = 0;
  3612. } else if (!p || strncmp(p, "early", 5) == 0) {
  3613. /* just "xmon" is equivalent to "xmon=early" */
  3614. xmon_init(1);
  3615. xmon_early = 1;
  3616. xmon_on = 1;
  3617. } else if (strncmp(p, "on", 2) == 0) {
  3618. xmon_init(1);
  3619. xmon_on = 1;
  3620. } else if (strncmp(p, "rw", 2) == 0) {
  3621. xmon_init(1);
  3622. xmon_on = 1;
  3623. xmon_is_ro = false;
  3624. } else if (strncmp(p, "ro", 2) == 0) {
  3625. xmon_init(1);
  3626. xmon_on = 1;
  3627. xmon_is_ro = true;
  3628. } else if (strncmp(p, "off", 3) == 0)
  3629. xmon_on = 0;
  3630. else
  3631. return 1;
  3632. return 0;
  3633. }
  3634. early_param("xmon", early_parse_xmon);
  3635. void __init xmon_setup(void)
  3636. {
  3637. if (xmon_on)
  3638. xmon_init(1);
  3639. if (xmon_early)
  3640. debugger(NULL);
  3641. }