xmon.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258
  1. /*
  2. * Routines providing a simple monitor for use on the PowerMac.
  3. *
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. * Copyright (C) 2001 PPC64 Team, IBM Corp
  6. * Copyrignt (C) 2006 Michael Ellerman, IBM Corp
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/smp.h>
  16. #include <linux/mm.h>
  17. #include <linux/reboot.h>
  18. #include <linux/delay.h>
  19. #include <linux/kallsyms.h>
  20. #include <linux/kmsg_dump.h>
  21. #include <linux/cpumask.h>
  22. #include <linux/export.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irq.h>
  26. #include <linux/bug.h>
  27. #include <asm/ptrace.h>
  28. #include <asm/string.h>
  29. #include <asm/prom.h>
  30. #include <asm/machdep.h>
  31. #include <asm/xmon.h>
  32. #include <asm/processor.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/mmu.h>
  35. #include <asm/mmu_context.h>
  36. #include <asm/cputable.h>
  37. #include <asm/rtas.h>
  38. #include <asm/sstep.h>
  39. #include <asm/irq_regs.h>
  40. #include <asm/spu.h>
  41. #include <asm/spu_priv1.h>
  42. #include <asm/setjmp.h>
  43. #include <asm/reg.h>
  44. #include <asm/debug.h>
  45. #include <asm/hw_breakpoint.h>
  46. #ifdef CONFIG_PPC64
  47. #include <asm/hvcall.h>
  48. #include <asm/paca.h>
  49. #endif
  50. #include "nonstdio.h"
  51. #include "dis-asm.h"
  52. #ifdef CONFIG_SMP
  53. static cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  54. static unsigned long xmon_taken = 1;
  55. static int xmon_owner;
  56. static int xmon_gate;
  57. #else
  58. #define xmon_owner 0
  59. #endif /* CONFIG_SMP */
  60. static unsigned long in_xmon __read_mostly = 0;
  61. static unsigned long adrs;
  62. static int size = 1;
  63. #define MAX_DUMP (128 * 1024)
  64. static unsigned long ndump = 64;
  65. static unsigned long nidump = 16;
  66. static unsigned long ncsum = 4096;
  67. static int termch;
  68. static char tmpstr[128];
  69. static long bus_error_jmp[JMP_BUF_LEN];
  70. static int catch_memory_errors;
  71. static long *xmon_fault_jmp[NR_CPUS];
  72. /* Breakpoint stuff */
  73. struct bpt {
  74. unsigned long address;
  75. unsigned int instr[2];
  76. atomic_t ref_count;
  77. int enabled;
  78. unsigned long pad;
  79. };
  80. /* Bits in bpt.enabled */
  81. #define BP_IABR_TE 1 /* IABR translation enabled */
  82. #define BP_IABR 2
  83. #define BP_TRAP 8
  84. #define BP_DABR 0x10
  85. #define NBPTS 256
  86. static struct bpt bpts[NBPTS];
  87. static struct bpt dabr;
  88. static struct bpt *iabr;
  89. static unsigned bpinstr = 0x7fe00008; /* trap */
  90. #define BP_NUM(bp) ((bp) - bpts + 1)
  91. /* Prototypes */
  92. static int cmds(struct pt_regs *);
  93. static int mread(unsigned long, void *, int);
  94. static int mwrite(unsigned long, void *, int);
  95. static int handle_fault(struct pt_regs *);
  96. static void byterev(unsigned char *, int);
  97. static void memex(void);
  98. static int bsesc(void);
  99. static void dump(void);
  100. static void prdump(unsigned long, long);
  101. static int ppc_inst_dump(unsigned long, long, int);
  102. static void dump_log_buf(void);
  103. static void backtrace(struct pt_regs *);
  104. static void excprint(struct pt_regs *);
  105. static void prregs(struct pt_regs *);
  106. static void memops(int);
  107. static void memlocate(void);
  108. static void memzcan(void);
  109. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  110. int skipbl(void);
  111. int scanhex(unsigned long *valp);
  112. static void scannl(void);
  113. static int hexdigit(int);
  114. void getstring(char *, int);
  115. static void flush_input(void);
  116. static int inchar(void);
  117. static void take_input(char *);
  118. static unsigned long read_spr(int);
  119. static void write_spr(int, unsigned long);
  120. static void super_regs(void);
  121. static void remove_bpts(void);
  122. static void insert_bpts(void);
  123. static void remove_cpu_bpts(void);
  124. static void insert_cpu_bpts(void);
  125. static struct bpt *at_breakpoint(unsigned long pc);
  126. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  127. static int do_step(struct pt_regs *);
  128. static void bpt_cmds(void);
  129. static void cacheflush(void);
  130. static int cpu_cmd(void);
  131. static void csum(void);
  132. static void bootcmds(void);
  133. static void proccall(void);
  134. void dump_segments(void);
  135. static void symbol_lookup(void);
  136. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  137. unsigned long pc);
  138. static void xmon_print_symbol(unsigned long address, const char *mid,
  139. const char *after);
  140. static const char *getvecname(unsigned long vec);
  141. static int do_spu_cmd(void);
  142. #ifdef CONFIG_44x
  143. static void dump_tlb_44x(void);
  144. #endif
  145. #ifdef CONFIG_PPC_BOOK3E
  146. static void dump_tlb_book3e(void);
  147. #endif
  148. static int xmon_no_auto_backtrace;
  149. extern void xmon_enter(void);
  150. extern void xmon_leave(void);
  151. #ifdef CONFIG_PPC64
  152. #define REG "%.16lx"
  153. #else
  154. #define REG "%.8lx"
  155. #endif
  156. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  157. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  158. || ('a' <= (c) && (c) <= 'f') \
  159. || ('A' <= (c) && (c) <= 'F'))
  160. #define isalnum(c) (('0' <= (c) && (c) <= '9') \
  161. || ('a' <= (c) && (c) <= 'z') \
  162. || ('A' <= (c) && (c) <= 'Z'))
  163. #define isspace(c) (c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
  164. static char *help_string = "\
  165. Commands:\n\
  166. b show breakpoints\n\
  167. bd set data breakpoint\n\
  168. bi set instruction breakpoint\n\
  169. bc clear breakpoint\n"
  170. #ifdef CONFIG_SMP
  171. "\
  172. c print cpus stopped in xmon\n\
  173. c# try to switch to cpu number h (in hex)\n"
  174. #endif
  175. "\
  176. C checksum\n\
  177. d dump bytes\n\
  178. di dump instructions\n\
  179. df dump float values\n\
  180. dd dump double values\n\
  181. dl dump the kernel log buffer\n"
  182. #ifdef CONFIG_PPC64
  183. "\
  184. dp[#] dump paca for current cpu, or cpu #\n\
  185. dpa dump paca for all possible cpus\n"
  186. #endif
  187. "\
  188. dr dump stream of raw bytes\n\
  189. e print exception information\n\
  190. f flush cache\n\
  191. la lookup symbol+offset of specified address\n\
  192. ls lookup address of specified symbol\n\
  193. m examine/change memory\n\
  194. mm move a block of memory\n\
  195. ms set a block of memory\n\
  196. md compare two blocks of memory\n\
  197. ml locate a block of memory\n\
  198. mz zero a block of memory\n\
  199. mi show information about memory allocation\n\
  200. p call a procedure\n\
  201. r print registers\n\
  202. s single step\n"
  203. #ifdef CONFIG_SPU_BASE
  204. " ss stop execution on all spus\n\
  205. sr restore execution on stopped spus\n\
  206. sf # dump spu fields for spu # (in hex)\n\
  207. sd # dump spu local store for spu # (in hex)\n\
  208. sdi # disassemble spu local store for spu # (in hex)\n"
  209. #endif
  210. " S print special registers\n\
  211. t print backtrace\n\
  212. x exit monitor and recover\n\
  213. X exit monitor and dont recover\n"
  214. #if defined(CONFIG_PPC64) && !defined(CONFIG_PPC_BOOK3E)
  215. " u dump segment table or SLB\n"
  216. #elif defined(CONFIG_PPC_STD_MMU_32)
  217. " u dump segment registers\n"
  218. #elif defined(CONFIG_44x) || defined(CONFIG_PPC_BOOK3E)
  219. " u dump TLB\n"
  220. #endif
  221. " ? help\n"
  222. " zr reboot\n\
  223. zh halt\n"
  224. ;
  225. static struct pt_regs *xmon_regs;
  226. static inline void sync(void)
  227. {
  228. asm volatile("sync; isync");
  229. }
  230. static inline void store_inst(void *p)
  231. {
  232. asm volatile ("dcbst 0,%0; sync; icbi 0,%0; isync" : : "r" (p));
  233. }
  234. static inline void cflush(void *p)
  235. {
  236. asm volatile ("dcbf 0,%0; icbi 0,%0" : : "r" (p));
  237. }
  238. static inline void cinval(void *p)
  239. {
  240. asm volatile ("dcbi 0,%0; icbi 0,%0" : : "r" (p));
  241. }
  242. /*
  243. * Disable surveillance (the service processor watchdog function)
  244. * while we are in xmon.
  245. * XXX we should re-enable it when we leave. :)
  246. */
  247. #define SURVEILLANCE_TOKEN 9000
  248. static inline void disable_surveillance(void)
  249. {
  250. #ifdef CONFIG_PPC_PSERIES
  251. /* Since this can't be a module, args should end up below 4GB. */
  252. static struct rtas_args args;
  253. /*
  254. * At this point we have got all the cpus we can into
  255. * xmon, so there is hopefully no other cpu calling RTAS
  256. * at the moment, even though we don't take rtas.lock.
  257. * If we did try to take rtas.lock there would be a
  258. * real possibility of deadlock.
  259. */
  260. args.token = rtas_token("set-indicator");
  261. if (args.token == RTAS_UNKNOWN_SERVICE)
  262. return;
  263. args.nargs = 3;
  264. args.nret = 1;
  265. args.rets = &args.args[3];
  266. args.args[0] = SURVEILLANCE_TOKEN;
  267. args.args[1] = 0;
  268. args.args[2] = 0;
  269. enter_rtas(__pa(&args));
  270. #endif /* CONFIG_PPC_PSERIES */
  271. }
  272. #ifdef CONFIG_SMP
  273. static int xmon_speaker;
  274. static void get_output_lock(void)
  275. {
  276. int me = smp_processor_id() + 0x100;
  277. int last_speaker = 0, prev;
  278. long timeout;
  279. if (xmon_speaker == me)
  280. return;
  281. for (;;) {
  282. if (xmon_speaker == 0) {
  283. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  284. if (last_speaker == 0)
  285. return;
  286. }
  287. timeout = 10000000;
  288. while (xmon_speaker == last_speaker) {
  289. if (--timeout > 0)
  290. continue;
  291. /* hostile takeover */
  292. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  293. if (prev == last_speaker)
  294. return;
  295. break;
  296. }
  297. }
  298. }
  299. static void release_output_lock(void)
  300. {
  301. xmon_speaker = 0;
  302. }
  303. int cpus_are_in_xmon(void)
  304. {
  305. return !cpumask_empty(&cpus_in_xmon);
  306. }
  307. #endif
  308. static inline int unrecoverable_excp(struct pt_regs *regs)
  309. {
  310. #if defined(CONFIG_4xx) || defined(CONFIG_PPC_BOOK3E)
  311. /* We have no MSR_RI bit on 4xx or Book3e, so we simply return false */
  312. return 0;
  313. #else
  314. return ((regs->msr & MSR_RI) == 0);
  315. #endif
  316. }
  317. static int xmon_core(struct pt_regs *regs, int fromipi)
  318. {
  319. int cmd = 0;
  320. struct bpt *bp;
  321. long recurse_jmp[JMP_BUF_LEN];
  322. unsigned long offset;
  323. unsigned long flags;
  324. #ifdef CONFIG_SMP
  325. int cpu;
  326. int secondary;
  327. unsigned long timeout;
  328. #endif
  329. local_irq_save(flags);
  330. bp = in_breakpoint_table(regs->nip, &offset);
  331. if (bp != NULL) {
  332. regs->nip = bp->address + offset;
  333. atomic_dec(&bp->ref_count);
  334. }
  335. remove_cpu_bpts();
  336. #ifdef CONFIG_SMP
  337. cpu = smp_processor_id();
  338. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  339. get_output_lock();
  340. excprint(regs);
  341. printf("cpu 0x%x: Exception %lx %s in xmon, "
  342. "returning to main loop\n",
  343. cpu, regs->trap, getvecname(TRAP(regs)));
  344. release_output_lock();
  345. longjmp(xmon_fault_jmp[cpu], 1);
  346. }
  347. if (setjmp(recurse_jmp) != 0) {
  348. if (!in_xmon || !xmon_gate) {
  349. get_output_lock();
  350. printf("xmon: WARNING: bad recursive fault "
  351. "on cpu 0x%x\n", cpu);
  352. release_output_lock();
  353. goto waiting;
  354. }
  355. secondary = !(xmon_taken && cpu == xmon_owner);
  356. goto cmdloop;
  357. }
  358. xmon_fault_jmp[cpu] = recurse_jmp;
  359. cpumask_set_cpu(cpu, &cpus_in_xmon);
  360. bp = NULL;
  361. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT))
  362. bp = at_breakpoint(regs->nip);
  363. if (bp || unrecoverable_excp(regs))
  364. fromipi = 0;
  365. if (!fromipi) {
  366. get_output_lock();
  367. excprint(regs);
  368. if (bp) {
  369. printf("cpu 0x%x stopped at breakpoint 0x%x (",
  370. cpu, BP_NUM(bp));
  371. xmon_print_symbol(regs->nip, " ", ")\n");
  372. }
  373. if (unrecoverable_excp(regs))
  374. printf("WARNING: exception is not recoverable, "
  375. "can't continue\n");
  376. release_output_lock();
  377. }
  378. waiting:
  379. secondary = 1;
  380. while (secondary && !xmon_gate) {
  381. if (in_xmon == 0) {
  382. if (fromipi)
  383. goto leave;
  384. secondary = test_and_set_bit(0, &in_xmon);
  385. }
  386. barrier();
  387. }
  388. if (!secondary && !xmon_gate) {
  389. /* we are the first cpu to come in */
  390. /* interrupt other cpu(s) */
  391. int ncpus = num_online_cpus();
  392. xmon_owner = cpu;
  393. mb();
  394. if (ncpus > 1) {
  395. smp_send_debugger_break();
  396. /* wait for other cpus to come in */
  397. for (timeout = 100000000; timeout != 0; --timeout) {
  398. if (cpumask_weight(&cpus_in_xmon) >= ncpus)
  399. break;
  400. barrier();
  401. }
  402. }
  403. remove_bpts();
  404. disable_surveillance();
  405. /* for breakpoint or single step, print the current instr. */
  406. if (bp || TRAP(regs) == 0xd00)
  407. ppc_inst_dump(regs->nip, 1, 0);
  408. printf("enter ? for help\n");
  409. mb();
  410. xmon_gate = 1;
  411. barrier();
  412. }
  413. cmdloop:
  414. while (in_xmon) {
  415. if (secondary) {
  416. if (cpu == xmon_owner) {
  417. if (!test_and_set_bit(0, &xmon_taken)) {
  418. secondary = 0;
  419. continue;
  420. }
  421. /* missed it */
  422. while (cpu == xmon_owner)
  423. barrier();
  424. }
  425. barrier();
  426. } else {
  427. cmd = cmds(regs);
  428. if (cmd != 0) {
  429. /* exiting xmon */
  430. insert_bpts();
  431. xmon_gate = 0;
  432. wmb();
  433. in_xmon = 0;
  434. break;
  435. }
  436. /* have switched to some other cpu */
  437. secondary = 1;
  438. }
  439. }
  440. leave:
  441. cpumask_clear_cpu(cpu, &cpus_in_xmon);
  442. xmon_fault_jmp[cpu] = NULL;
  443. #else
  444. /* UP is simple... */
  445. if (in_xmon) {
  446. printf("Exception %lx %s in xmon, returning to main loop\n",
  447. regs->trap, getvecname(TRAP(regs)));
  448. longjmp(xmon_fault_jmp[0], 1);
  449. }
  450. if (setjmp(recurse_jmp) == 0) {
  451. xmon_fault_jmp[0] = recurse_jmp;
  452. in_xmon = 1;
  453. excprint(regs);
  454. bp = at_breakpoint(regs->nip);
  455. if (bp) {
  456. printf("Stopped at breakpoint %x (", BP_NUM(bp));
  457. xmon_print_symbol(regs->nip, " ", ")\n");
  458. }
  459. if (unrecoverable_excp(regs))
  460. printf("WARNING: exception is not recoverable, "
  461. "can't continue\n");
  462. remove_bpts();
  463. disable_surveillance();
  464. /* for breakpoint or single step, print the current instr. */
  465. if (bp || TRAP(regs) == 0xd00)
  466. ppc_inst_dump(regs->nip, 1, 0);
  467. printf("enter ? for help\n");
  468. }
  469. cmd = cmds(regs);
  470. insert_bpts();
  471. in_xmon = 0;
  472. #endif
  473. #ifdef CONFIG_BOOKE
  474. if (regs->msr & MSR_DE) {
  475. bp = at_breakpoint(regs->nip);
  476. if (bp != NULL) {
  477. regs->nip = (unsigned long) &bp->instr[0];
  478. atomic_inc(&bp->ref_count);
  479. }
  480. }
  481. #else
  482. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  483. bp = at_breakpoint(regs->nip);
  484. if (bp != NULL) {
  485. int stepped = emulate_step(regs, bp->instr[0]);
  486. if (stepped == 0) {
  487. regs->nip = (unsigned long) &bp->instr[0];
  488. atomic_inc(&bp->ref_count);
  489. } else if (stepped < 0) {
  490. printf("Couldn't single-step %s instruction\n",
  491. (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
  492. }
  493. }
  494. }
  495. #endif
  496. insert_cpu_bpts();
  497. local_irq_restore(flags);
  498. return cmd != 'X' && cmd != EOF;
  499. }
  500. int xmon(struct pt_regs *excp)
  501. {
  502. struct pt_regs regs;
  503. if (excp == NULL) {
  504. ppc_save_regs(&regs);
  505. excp = &regs;
  506. }
  507. return xmon_core(excp, 0);
  508. }
  509. EXPORT_SYMBOL(xmon);
  510. irqreturn_t xmon_irq(int irq, void *d)
  511. {
  512. unsigned long flags;
  513. local_irq_save(flags);
  514. printf("Keyboard interrupt\n");
  515. xmon(get_irq_regs());
  516. local_irq_restore(flags);
  517. return IRQ_HANDLED;
  518. }
  519. static int xmon_bpt(struct pt_regs *regs)
  520. {
  521. struct bpt *bp;
  522. unsigned long offset;
  523. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  524. return 0;
  525. /* Are we at the trap at bp->instr[1] for some bp? */
  526. bp = in_breakpoint_table(regs->nip, &offset);
  527. if (bp != NULL && offset == 4) {
  528. regs->nip = bp->address + 4;
  529. atomic_dec(&bp->ref_count);
  530. return 1;
  531. }
  532. /* Are we at a breakpoint? */
  533. bp = at_breakpoint(regs->nip);
  534. if (!bp)
  535. return 0;
  536. xmon_core(regs, 0);
  537. return 1;
  538. }
  539. static int xmon_sstep(struct pt_regs *regs)
  540. {
  541. if (user_mode(regs))
  542. return 0;
  543. xmon_core(regs, 0);
  544. return 1;
  545. }
  546. static int xmon_break_match(struct pt_regs *regs)
  547. {
  548. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  549. return 0;
  550. if (dabr.enabled == 0)
  551. return 0;
  552. xmon_core(regs, 0);
  553. return 1;
  554. }
  555. static int xmon_iabr_match(struct pt_regs *regs)
  556. {
  557. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) != (MSR_IR|MSR_64BIT))
  558. return 0;
  559. if (iabr == NULL)
  560. return 0;
  561. xmon_core(regs, 0);
  562. return 1;
  563. }
  564. static int xmon_ipi(struct pt_regs *regs)
  565. {
  566. #ifdef CONFIG_SMP
  567. if (in_xmon && !cpumask_test_cpu(smp_processor_id(), &cpus_in_xmon))
  568. xmon_core(regs, 1);
  569. #endif
  570. return 0;
  571. }
  572. static int xmon_fault_handler(struct pt_regs *regs)
  573. {
  574. struct bpt *bp;
  575. unsigned long offset;
  576. if (in_xmon && catch_memory_errors)
  577. handle_fault(regs); /* doesn't return */
  578. if ((regs->msr & (MSR_IR|MSR_PR|MSR_64BIT)) == (MSR_IR|MSR_64BIT)) {
  579. bp = in_breakpoint_table(regs->nip, &offset);
  580. if (bp != NULL) {
  581. regs->nip = bp->address + offset;
  582. atomic_dec(&bp->ref_count);
  583. }
  584. }
  585. return 0;
  586. }
  587. static struct bpt *at_breakpoint(unsigned long pc)
  588. {
  589. int i;
  590. struct bpt *bp;
  591. bp = bpts;
  592. for (i = 0; i < NBPTS; ++i, ++bp)
  593. if (bp->enabled && pc == bp->address)
  594. return bp;
  595. return NULL;
  596. }
  597. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  598. {
  599. unsigned long off;
  600. off = nip - (unsigned long) bpts;
  601. if (off >= sizeof(bpts))
  602. return NULL;
  603. off %= sizeof(struct bpt);
  604. if (off != offsetof(struct bpt, instr[0])
  605. && off != offsetof(struct bpt, instr[1]))
  606. return NULL;
  607. *offp = off - offsetof(struct bpt, instr[0]);
  608. return (struct bpt *) (nip - off);
  609. }
  610. static struct bpt *new_breakpoint(unsigned long a)
  611. {
  612. struct bpt *bp;
  613. a &= ~3UL;
  614. bp = at_breakpoint(a);
  615. if (bp)
  616. return bp;
  617. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  618. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  619. bp->address = a;
  620. bp->instr[1] = bpinstr;
  621. store_inst(&bp->instr[1]);
  622. return bp;
  623. }
  624. }
  625. printf("Sorry, no free breakpoints. Please clear one first.\n");
  626. return NULL;
  627. }
  628. static void insert_bpts(void)
  629. {
  630. int i;
  631. struct bpt *bp;
  632. bp = bpts;
  633. for (i = 0; i < NBPTS; ++i, ++bp) {
  634. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  635. continue;
  636. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  637. printf("Couldn't read instruction at %lx, "
  638. "disabling breakpoint there\n", bp->address);
  639. bp->enabled = 0;
  640. continue;
  641. }
  642. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  643. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  644. "instruction, disabling it\n", bp->address);
  645. bp->enabled = 0;
  646. continue;
  647. }
  648. store_inst(&bp->instr[0]);
  649. if (bp->enabled & BP_IABR)
  650. continue;
  651. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  652. printf("Couldn't write instruction at %lx, "
  653. "disabling breakpoint there\n", bp->address);
  654. bp->enabled &= ~BP_TRAP;
  655. continue;
  656. }
  657. store_inst((void *)bp->address);
  658. }
  659. }
  660. static void insert_cpu_bpts(void)
  661. {
  662. struct arch_hw_breakpoint brk;
  663. if (dabr.enabled) {
  664. brk.address = dabr.address;
  665. brk.type = (dabr.enabled & HW_BRK_TYPE_DABR) | HW_BRK_TYPE_PRIV_ALL;
  666. brk.len = 8;
  667. set_breakpoint(&brk);
  668. }
  669. if (iabr && cpu_has_feature(CPU_FTR_IABR))
  670. mtspr(SPRN_IABR, iabr->address
  671. | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
  672. }
  673. static void remove_bpts(void)
  674. {
  675. int i;
  676. struct bpt *bp;
  677. unsigned instr;
  678. bp = bpts;
  679. for (i = 0; i < NBPTS; ++i, ++bp) {
  680. if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
  681. continue;
  682. if (mread(bp->address, &instr, 4) == 4
  683. && instr == bpinstr
  684. && mwrite(bp->address, &bp->instr, 4) != 4)
  685. printf("Couldn't remove breakpoint at %lx\n",
  686. bp->address);
  687. else
  688. store_inst((void *)bp->address);
  689. }
  690. }
  691. static void remove_cpu_bpts(void)
  692. {
  693. hw_breakpoint_disable();
  694. if (cpu_has_feature(CPU_FTR_IABR))
  695. mtspr(SPRN_IABR, 0);
  696. }
  697. /* Command interpreting routine */
  698. static char *last_cmd;
  699. static int
  700. cmds(struct pt_regs *excp)
  701. {
  702. int cmd = 0;
  703. last_cmd = NULL;
  704. xmon_regs = excp;
  705. if (!xmon_no_auto_backtrace) {
  706. xmon_no_auto_backtrace = 1;
  707. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  708. }
  709. for(;;) {
  710. #ifdef CONFIG_SMP
  711. printf("%x:", smp_processor_id());
  712. #endif /* CONFIG_SMP */
  713. printf("mon> ");
  714. flush_input();
  715. termch = 0;
  716. cmd = skipbl();
  717. if( cmd == '\n' ) {
  718. if (last_cmd == NULL)
  719. continue;
  720. take_input(last_cmd);
  721. last_cmd = NULL;
  722. cmd = inchar();
  723. }
  724. switch (cmd) {
  725. case 'm':
  726. cmd = inchar();
  727. switch (cmd) {
  728. case 'm':
  729. case 's':
  730. case 'd':
  731. memops(cmd);
  732. break;
  733. case 'l':
  734. memlocate();
  735. break;
  736. case 'z':
  737. memzcan();
  738. break;
  739. case 'i':
  740. show_mem(0);
  741. break;
  742. default:
  743. termch = cmd;
  744. memex();
  745. }
  746. break;
  747. case 'd':
  748. dump();
  749. break;
  750. case 'l':
  751. symbol_lookup();
  752. break;
  753. case 'r':
  754. prregs(excp); /* print regs */
  755. break;
  756. case 'e':
  757. excprint(excp);
  758. break;
  759. case 'S':
  760. super_regs();
  761. break;
  762. case 't':
  763. backtrace(excp);
  764. break;
  765. case 'f':
  766. cacheflush();
  767. break;
  768. case 's':
  769. if (do_spu_cmd() == 0)
  770. break;
  771. if (do_step(excp))
  772. return cmd;
  773. break;
  774. case 'x':
  775. case 'X':
  776. return cmd;
  777. case EOF:
  778. printf(" <no input ...>\n");
  779. mdelay(2000);
  780. return cmd;
  781. case '?':
  782. xmon_puts(help_string);
  783. break;
  784. case 'b':
  785. bpt_cmds();
  786. break;
  787. case 'C':
  788. csum();
  789. break;
  790. case 'c':
  791. if (cpu_cmd())
  792. return 0;
  793. break;
  794. case 'z':
  795. bootcmds();
  796. break;
  797. case 'p':
  798. proccall();
  799. break;
  800. #ifdef CONFIG_PPC_STD_MMU
  801. case 'u':
  802. dump_segments();
  803. break;
  804. #elif defined(CONFIG_4xx)
  805. case 'u':
  806. dump_tlb_44x();
  807. break;
  808. #elif defined(CONFIG_PPC_BOOK3E)
  809. case 'u':
  810. dump_tlb_book3e();
  811. break;
  812. #endif
  813. default:
  814. printf("Unrecognized command: ");
  815. do {
  816. if (' ' < cmd && cmd <= '~')
  817. putchar(cmd);
  818. else
  819. printf("\\x%x", cmd);
  820. cmd = inchar();
  821. } while (cmd != '\n');
  822. printf(" (type ? for help)\n");
  823. break;
  824. }
  825. }
  826. }
  827. #ifdef CONFIG_BOOKE
  828. static int do_step(struct pt_regs *regs)
  829. {
  830. regs->msr |= MSR_DE;
  831. mtspr(SPRN_DBCR0, mfspr(SPRN_DBCR0) | DBCR0_IC | DBCR0_IDM);
  832. return 1;
  833. }
  834. #else
  835. /*
  836. * Step a single instruction.
  837. * Some instructions we emulate, others we execute with MSR_SE set.
  838. */
  839. static int do_step(struct pt_regs *regs)
  840. {
  841. unsigned int instr;
  842. int stepped;
  843. /* check we are in 64-bit kernel mode, translation enabled */
  844. if ((regs->msr & (MSR_64BIT|MSR_PR|MSR_IR)) == (MSR_64BIT|MSR_IR)) {
  845. if (mread(regs->nip, &instr, 4) == 4) {
  846. stepped = emulate_step(regs, instr);
  847. if (stepped < 0) {
  848. printf("Couldn't single-step %s instruction\n",
  849. (IS_RFID(instr)? "rfid": "mtmsrd"));
  850. return 0;
  851. }
  852. if (stepped > 0) {
  853. regs->trap = 0xd00 | (regs->trap & 1);
  854. printf("stepped to ");
  855. xmon_print_symbol(regs->nip, " ", "\n");
  856. ppc_inst_dump(regs->nip, 1, 0);
  857. return 0;
  858. }
  859. }
  860. }
  861. regs->msr |= MSR_SE;
  862. return 1;
  863. }
  864. #endif
  865. static void bootcmds(void)
  866. {
  867. int cmd;
  868. cmd = inchar();
  869. if (cmd == 'r')
  870. ppc_md.restart(NULL);
  871. else if (cmd == 'h')
  872. ppc_md.halt();
  873. else if (cmd == 'p')
  874. ppc_md.power_off();
  875. }
  876. static int cpu_cmd(void)
  877. {
  878. #ifdef CONFIG_SMP
  879. unsigned long cpu, first_cpu, last_cpu;
  880. int timeout;
  881. if (!scanhex(&cpu)) {
  882. /* print cpus waiting or in xmon */
  883. printf("cpus stopped:");
  884. last_cpu = first_cpu = NR_CPUS;
  885. for_each_possible_cpu(cpu) {
  886. if (cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  887. if (cpu == last_cpu + 1) {
  888. last_cpu = cpu;
  889. } else {
  890. if (last_cpu != first_cpu)
  891. printf("-%lx", last_cpu);
  892. last_cpu = first_cpu = cpu;
  893. printf(" %lx", cpu);
  894. }
  895. }
  896. }
  897. if (last_cpu != first_cpu)
  898. printf("-%lx", last_cpu);
  899. printf("\n");
  900. return 0;
  901. }
  902. /* try to switch to cpu specified */
  903. if (!cpumask_test_cpu(cpu, &cpus_in_xmon)) {
  904. printf("cpu 0x%x isn't in xmon\n", cpu);
  905. return 0;
  906. }
  907. xmon_taken = 0;
  908. mb();
  909. xmon_owner = cpu;
  910. timeout = 10000000;
  911. while (!xmon_taken) {
  912. if (--timeout == 0) {
  913. if (test_and_set_bit(0, &xmon_taken))
  914. break;
  915. /* take control back */
  916. mb();
  917. xmon_owner = smp_processor_id();
  918. printf("cpu %u didn't take control\n", cpu);
  919. return 0;
  920. }
  921. barrier();
  922. }
  923. return 1;
  924. #else
  925. return 0;
  926. #endif /* CONFIG_SMP */
  927. }
  928. static unsigned short fcstab[256] = {
  929. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  930. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  931. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  932. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  933. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  934. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  935. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  936. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  937. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  938. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  939. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  940. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  941. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  942. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  943. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  944. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  945. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  946. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  947. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  948. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  949. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  950. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  951. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  952. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  953. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  954. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  955. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  956. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  957. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  958. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  959. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  960. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  961. };
  962. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  963. static void
  964. csum(void)
  965. {
  966. unsigned int i;
  967. unsigned short fcs;
  968. unsigned char v;
  969. if (!scanhex(&adrs))
  970. return;
  971. if (!scanhex(&ncsum))
  972. return;
  973. fcs = 0xffff;
  974. for (i = 0; i < ncsum; ++i) {
  975. if (mread(adrs+i, &v, 1) == 0) {
  976. printf("csum stopped at %x\n", adrs+i);
  977. break;
  978. }
  979. fcs = FCS(fcs, v);
  980. }
  981. printf("%x\n", fcs);
  982. }
  983. /*
  984. * Check if this is a suitable place to put a breakpoint.
  985. */
  986. static long check_bp_loc(unsigned long addr)
  987. {
  988. unsigned int instr;
  989. addr &= ~3;
  990. if (!is_kernel_addr(addr)) {
  991. printf("Breakpoints may only be placed at kernel addresses\n");
  992. return 0;
  993. }
  994. if (!mread(addr, &instr, sizeof(instr))) {
  995. printf("Can't read instruction at address %lx\n", addr);
  996. return 0;
  997. }
  998. if (IS_MTMSRD(instr) || IS_RFID(instr)) {
  999. printf("Breakpoints may not be placed on mtmsrd or rfid "
  1000. "instructions\n");
  1001. return 0;
  1002. }
  1003. return 1;
  1004. }
  1005. static char *breakpoint_help_string =
  1006. "Breakpoint command usage:\n"
  1007. "b show breakpoints\n"
  1008. "b <addr> [cnt] set breakpoint at given instr addr\n"
  1009. "bc clear all breakpoints\n"
  1010. "bc <n/addr> clear breakpoint number n or at addr\n"
  1011. "bi <addr> [cnt] set hardware instr breakpoint (POWER3/RS64 only)\n"
  1012. "bd <addr> [cnt] set hardware data breakpoint\n"
  1013. "";
  1014. static void
  1015. bpt_cmds(void)
  1016. {
  1017. int cmd;
  1018. unsigned long a;
  1019. int mode, i;
  1020. struct bpt *bp;
  1021. const char badaddr[] = "Only kernel addresses are permitted "
  1022. "for breakpoints\n";
  1023. cmd = inchar();
  1024. switch (cmd) {
  1025. #ifndef CONFIG_8xx
  1026. case 'd': /* bd - hardware data breakpoint */
  1027. mode = 7;
  1028. cmd = inchar();
  1029. if (cmd == 'r')
  1030. mode = 5;
  1031. else if (cmd == 'w')
  1032. mode = 6;
  1033. else
  1034. termch = cmd;
  1035. dabr.address = 0;
  1036. dabr.enabled = 0;
  1037. if (scanhex(&dabr.address)) {
  1038. if (!is_kernel_addr(dabr.address)) {
  1039. printf(badaddr);
  1040. break;
  1041. }
  1042. dabr.address &= ~HW_BRK_TYPE_DABR;
  1043. dabr.enabled = mode | BP_DABR;
  1044. }
  1045. break;
  1046. case 'i': /* bi - hardware instr breakpoint */
  1047. if (!cpu_has_feature(CPU_FTR_IABR)) {
  1048. printf("Hardware instruction breakpoint "
  1049. "not supported on this cpu\n");
  1050. break;
  1051. }
  1052. if (iabr) {
  1053. iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
  1054. iabr = NULL;
  1055. }
  1056. if (!scanhex(&a))
  1057. break;
  1058. if (!check_bp_loc(a))
  1059. break;
  1060. bp = new_breakpoint(a);
  1061. if (bp != NULL) {
  1062. bp->enabled |= BP_IABR | BP_IABR_TE;
  1063. iabr = bp;
  1064. }
  1065. break;
  1066. #endif
  1067. case 'c':
  1068. if (!scanhex(&a)) {
  1069. /* clear all breakpoints */
  1070. for (i = 0; i < NBPTS; ++i)
  1071. bpts[i].enabled = 0;
  1072. iabr = NULL;
  1073. dabr.enabled = 0;
  1074. printf("All breakpoints cleared\n");
  1075. break;
  1076. }
  1077. if (a <= NBPTS && a >= 1) {
  1078. /* assume a breakpoint number */
  1079. bp = &bpts[a-1]; /* bp nums are 1 based */
  1080. } else {
  1081. /* assume a breakpoint address */
  1082. bp = at_breakpoint(a);
  1083. if (bp == NULL) {
  1084. printf("No breakpoint at %x\n", a);
  1085. break;
  1086. }
  1087. }
  1088. printf("Cleared breakpoint %x (", BP_NUM(bp));
  1089. xmon_print_symbol(bp->address, " ", ")\n");
  1090. bp->enabled = 0;
  1091. break;
  1092. default:
  1093. termch = cmd;
  1094. cmd = skipbl();
  1095. if (cmd == '?') {
  1096. printf(breakpoint_help_string);
  1097. break;
  1098. }
  1099. termch = cmd;
  1100. if (!scanhex(&a)) {
  1101. /* print all breakpoints */
  1102. printf(" type address\n");
  1103. if (dabr.enabled) {
  1104. printf(" data "REG" [", dabr.address);
  1105. if (dabr.enabled & 1)
  1106. printf("r");
  1107. if (dabr.enabled & 2)
  1108. printf("w");
  1109. printf("]\n");
  1110. }
  1111. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1112. if (!bp->enabled)
  1113. continue;
  1114. printf("%2x %s ", BP_NUM(bp),
  1115. (bp->enabled & BP_IABR)? "inst": "trap");
  1116. xmon_print_symbol(bp->address, " ", "\n");
  1117. }
  1118. break;
  1119. }
  1120. if (!check_bp_loc(a))
  1121. break;
  1122. bp = new_breakpoint(a);
  1123. if (bp != NULL)
  1124. bp->enabled |= BP_TRAP;
  1125. break;
  1126. }
  1127. }
  1128. /* Very cheap human name for vector lookup. */
  1129. static
  1130. const char *getvecname(unsigned long vec)
  1131. {
  1132. char *ret;
  1133. switch (vec) {
  1134. case 0x100: ret = "(System Reset)"; break;
  1135. case 0x200: ret = "(Machine Check)"; break;
  1136. case 0x300: ret = "(Data Access)"; break;
  1137. case 0x380: ret = "(Data SLB Access)"; break;
  1138. case 0x400: ret = "(Instruction Access)"; break;
  1139. case 0x480: ret = "(Instruction SLB Access)"; break;
  1140. case 0x500: ret = "(Hardware Interrupt)"; break;
  1141. case 0x600: ret = "(Alignment)"; break;
  1142. case 0x700: ret = "(Program Check)"; break;
  1143. case 0x800: ret = "(FPU Unavailable)"; break;
  1144. case 0x900: ret = "(Decrementer)"; break;
  1145. case 0x980: ret = "(Hypervisor Decrementer)"; break;
  1146. case 0xa00: ret = "(Doorbell)"; break;
  1147. case 0xc00: ret = "(System Call)"; break;
  1148. case 0xd00: ret = "(Single Step)"; break;
  1149. case 0xe40: ret = "(Emulation Assist)"; break;
  1150. case 0xe60: ret = "(HMI)"; break;
  1151. case 0xe80: ret = "(Hypervisor Doorbell)"; break;
  1152. case 0xf00: ret = "(Performance Monitor)"; break;
  1153. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1154. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1155. case 0x1500: ret = "(Denormalisation)"; break;
  1156. case 0x1700: ret = "(Altivec Assist)"; break;
  1157. default: ret = "";
  1158. }
  1159. return ret;
  1160. }
  1161. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1162. unsigned long *endp)
  1163. {
  1164. unsigned long size, offset;
  1165. const char *name;
  1166. *startp = *endp = 0;
  1167. if (pc == 0)
  1168. return;
  1169. if (setjmp(bus_error_jmp) == 0) {
  1170. catch_memory_errors = 1;
  1171. sync();
  1172. name = kallsyms_lookup(pc, &size, &offset, NULL, tmpstr);
  1173. if (name != NULL) {
  1174. *startp = pc - offset;
  1175. *endp = pc - offset + size;
  1176. }
  1177. sync();
  1178. }
  1179. catch_memory_errors = 0;
  1180. }
  1181. #define LRSAVE_OFFSET (STACK_FRAME_LR_SAVE * sizeof(unsigned long))
  1182. #define MARKER_OFFSET (STACK_FRAME_MARKER * sizeof(unsigned long))
  1183. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1184. unsigned long pc)
  1185. {
  1186. int max_to_print = 64;
  1187. unsigned long ip;
  1188. unsigned long newsp;
  1189. unsigned long marker;
  1190. struct pt_regs regs;
  1191. while (max_to_print--) {
  1192. if (sp < PAGE_OFFSET) {
  1193. if (sp != 0)
  1194. printf("SP (%lx) is in userspace\n", sp);
  1195. break;
  1196. }
  1197. if (!mread(sp + LRSAVE_OFFSET, &ip, sizeof(unsigned long))
  1198. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1199. printf("Couldn't read stack frame at %lx\n", sp);
  1200. break;
  1201. }
  1202. /*
  1203. * For the first stack frame, try to work out if
  1204. * LR and/or the saved LR value in the bottommost
  1205. * stack frame are valid.
  1206. */
  1207. if ((pc | lr) != 0) {
  1208. unsigned long fnstart, fnend;
  1209. unsigned long nextip;
  1210. int printip = 1;
  1211. get_function_bounds(pc, &fnstart, &fnend);
  1212. nextip = 0;
  1213. if (newsp > sp)
  1214. mread(newsp + LRSAVE_OFFSET, &nextip,
  1215. sizeof(unsigned long));
  1216. if (lr == ip) {
  1217. if (lr < PAGE_OFFSET
  1218. || (fnstart <= lr && lr < fnend))
  1219. printip = 0;
  1220. } else if (lr == nextip) {
  1221. printip = 0;
  1222. } else if (lr >= PAGE_OFFSET
  1223. && !(fnstart <= lr && lr < fnend)) {
  1224. printf("[link register ] ");
  1225. xmon_print_symbol(lr, " ", "\n");
  1226. }
  1227. if (printip) {
  1228. printf("["REG"] ", sp);
  1229. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1230. }
  1231. pc = lr = 0;
  1232. } else {
  1233. printf("["REG"] ", sp);
  1234. xmon_print_symbol(ip, " ", "\n");
  1235. }
  1236. /* Look for "regshere" marker to see if this is
  1237. an exception frame. */
  1238. if (mread(sp + MARKER_OFFSET, &marker, sizeof(unsigned long))
  1239. && marker == STACK_FRAME_REGS_MARKER) {
  1240. if (mread(sp + STACK_FRAME_OVERHEAD, &regs, sizeof(regs))
  1241. != sizeof(regs)) {
  1242. printf("Couldn't read registers at %lx\n",
  1243. sp + STACK_FRAME_OVERHEAD);
  1244. break;
  1245. }
  1246. printf("--- Exception: %lx %s at ", regs.trap,
  1247. getvecname(TRAP(&regs)));
  1248. pc = regs.nip;
  1249. lr = regs.link;
  1250. xmon_print_symbol(pc, " ", "\n");
  1251. }
  1252. if (newsp == 0)
  1253. break;
  1254. sp = newsp;
  1255. }
  1256. }
  1257. static void backtrace(struct pt_regs *excp)
  1258. {
  1259. unsigned long sp;
  1260. if (scanhex(&sp))
  1261. xmon_show_stack(sp, 0, 0);
  1262. else
  1263. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1264. scannl();
  1265. }
  1266. static void print_bug_trap(struct pt_regs *regs)
  1267. {
  1268. #ifdef CONFIG_BUG
  1269. const struct bug_entry *bug;
  1270. unsigned long addr;
  1271. if (regs->msr & MSR_PR)
  1272. return; /* not in kernel */
  1273. addr = regs->nip; /* address of trap instruction */
  1274. if (addr < PAGE_OFFSET)
  1275. return;
  1276. bug = find_bug(regs->nip);
  1277. if (bug == NULL)
  1278. return;
  1279. if (is_warning_bug(bug))
  1280. return;
  1281. #ifdef CONFIG_DEBUG_BUGVERBOSE
  1282. printf("kernel BUG at %s:%u!\n",
  1283. bug->file, bug->line);
  1284. #else
  1285. printf("kernel BUG at %p!\n", (void *)bug->bug_addr);
  1286. #endif
  1287. #endif /* CONFIG_BUG */
  1288. }
  1289. static void excprint(struct pt_regs *fp)
  1290. {
  1291. unsigned long trap;
  1292. #ifdef CONFIG_SMP
  1293. printf("cpu 0x%x: ", smp_processor_id());
  1294. #endif /* CONFIG_SMP */
  1295. trap = TRAP(fp);
  1296. printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
  1297. printf(" pc: ");
  1298. xmon_print_symbol(fp->nip, ": ", "\n");
  1299. printf(" lr: ", fp->link);
  1300. xmon_print_symbol(fp->link, ": ", "\n");
  1301. printf(" sp: %lx\n", fp->gpr[1]);
  1302. printf(" msr: %lx\n", fp->msr);
  1303. if (trap == 0x300 || trap == 0x380 || trap == 0x600 || trap == 0x200) {
  1304. printf(" dar: %lx\n", fp->dar);
  1305. if (trap != 0x380)
  1306. printf(" dsisr: %lx\n", fp->dsisr);
  1307. }
  1308. printf(" current = 0x%lx\n", current);
  1309. #ifdef CONFIG_PPC64
  1310. printf(" paca = 0x%lx\t softe: %d\t irq_happened: 0x%02x\n",
  1311. local_paca, local_paca->soft_enabled, local_paca->irq_happened);
  1312. #endif
  1313. if (current) {
  1314. printf(" pid = %ld, comm = %s\n",
  1315. current->pid, current->comm);
  1316. }
  1317. if (trap == 0x700)
  1318. print_bug_trap(fp);
  1319. }
  1320. static void prregs(struct pt_regs *fp)
  1321. {
  1322. int n, trap;
  1323. unsigned long base;
  1324. struct pt_regs regs;
  1325. if (scanhex(&base)) {
  1326. if (setjmp(bus_error_jmp) == 0) {
  1327. catch_memory_errors = 1;
  1328. sync();
  1329. regs = *(struct pt_regs *)base;
  1330. sync();
  1331. __delay(200);
  1332. } else {
  1333. catch_memory_errors = 0;
  1334. printf("*** Error reading registers from "REG"\n",
  1335. base);
  1336. return;
  1337. }
  1338. catch_memory_errors = 0;
  1339. fp = &regs;
  1340. }
  1341. #ifdef CONFIG_PPC64
  1342. if (FULL_REGS(fp)) {
  1343. for (n = 0; n < 16; ++n)
  1344. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1345. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1346. } else {
  1347. for (n = 0; n < 7; ++n)
  1348. printf("R%.2ld = "REG" R%.2ld = "REG"\n",
  1349. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1350. }
  1351. #else
  1352. for (n = 0; n < 32; ++n) {
  1353. printf("R%.2d = %.8x%s", n, fp->gpr[n],
  1354. (n & 3) == 3? "\n": " ");
  1355. if (n == 12 && !FULL_REGS(fp)) {
  1356. printf("\n");
  1357. break;
  1358. }
  1359. }
  1360. #endif
  1361. printf("pc = ");
  1362. xmon_print_symbol(fp->nip, " ", "\n");
  1363. if (TRAP(fp) != 0xc00 && cpu_has_feature(CPU_FTR_CFAR)) {
  1364. printf("cfar= ");
  1365. xmon_print_symbol(fp->orig_gpr3, " ", "\n");
  1366. }
  1367. printf("lr = ");
  1368. xmon_print_symbol(fp->link, " ", "\n");
  1369. printf("msr = "REG" cr = %.8lx\n", fp->msr, fp->ccr);
  1370. printf("ctr = "REG" xer = "REG" trap = %4lx\n",
  1371. fp->ctr, fp->xer, fp->trap);
  1372. trap = TRAP(fp);
  1373. if (trap == 0x300 || trap == 0x380 || trap == 0x600)
  1374. printf("dar = "REG" dsisr = %.8lx\n", fp->dar, fp->dsisr);
  1375. }
  1376. static void cacheflush(void)
  1377. {
  1378. int cmd;
  1379. unsigned long nflush;
  1380. cmd = inchar();
  1381. if (cmd != 'i')
  1382. termch = cmd;
  1383. scanhex((void *)&adrs);
  1384. if (termch != '\n')
  1385. termch = 0;
  1386. nflush = 1;
  1387. scanhex(&nflush);
  1388. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1389. if (setjmp(bus_error_jmp) == 0) {
  1390. catch_memory_errors = 1;
  1391. sync();
  1392. if (cmd != 'i') {
  1393. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1394. cflush((void *) adrs);
  1395. } else {
  1396. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1397. cinval((void *) adrs);
  1398. }
  1399. sync();
  1400. /* wait a little while to see if we get a machine check */
  1401. __delay(200);
  1402. }
  1403. catch_memory_errors = 0;
  1404. }
  1405. static unsigned long
  1406. read_spr(int n)
  1407. {
  1408. unsigned int instrs[2];
  1409. unsigned long (*code)(void);
  1410. unsigned long ret = -1UL;
  1411. #ifdef CONFIG_PPC64
  1412. unsigned long opd[3];
  1413. opd[0] = (unsigned long)instrs;
  1414. opd[1] = 0;
  1415. opd[2] = 0;
  1416. code = (unsigned long (*)(void)) opd;
  1417. #else
  1418. code = (unsigned long (*)(void)) instrs;
  1419. #endif
  1420. /* mfspr r3,n; blr */
  1421. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1422. instrs[1] = 0x4e800020;
  1423. store_inst(instrs);
  1424. store_inst(instrs+1);
  1425. if (setjmp(bus_error_jmp) == 0) {
  1426. catch_memory_errors = 1;
  1427. sync();
  1428. ret = code();
  1429. sync();
  1430. /* wait a little while to see if we get a machine check */
  1431. __delay(200);
  1432. n = size;
  1433. }
  1434. return ret;
  1435. }
  1436. static void
  1437. write_spr(int n, unsigned long val)
  1438. {
  1439. unsigned int instrs[2];
  1440. unsigned long (*code)(unsigned long);
  1441. #ifdef CONFIG_PPC64
  1442. unsigned long opd[3];
  1443. opd[0] = (unsigned long)instrs;
  1444. opd[1] = 0;
  1445. opd[2] = 0;
  1446. code = (unsigned long (*)(unsigned long)) opd;
  1447. #else
  1448. code = (unsigned long (*)(unsigned long)) instrs;
  1449. #endif
  1450. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1451. instrs[1] = 0x4e800020;
  1452. store_inst(instrs);
  1453. store_inst(instrs+1);
  1454. if (setjmp(bus_error_jmp) == 0) {
  1455. catch_memory_errors = 1;
  1456. sync();
  1457. code(val);
  1458. sync();
  1459. /* wait a little while to see if we get a machine check */
  1460. __delay(200);
  1461. n = size;
  1462. }
  1463. }
  1464. static unsigned long regno;
  1465. extern char exc_prolog;
  1466. extern char dec_exc;
  1467. static void super_regs(void)
  1468. {
  1469. int cmd;
  1470. unsigned long val;
  1471. cmd = skipbl();
  1472. if (cmd == '\n') {
  1473. unsigned long sp, toc;
  1474. asm("mr %0,1" : "=r" (sp) :);
  1475. asm("mr %0,2" : "=r" (toc) :);
  1476. printf("msr = "REG" sprg0= "REG"\n",
  1477. mfmsr(), mfspr(SPRN_SPRG0));
  1478. printf("pvr = "REG" sprg1= "REG"\n",
  1479. mfspr(SPRN_PVR), mfspr(SPRN_SPRG1));
  1480. printf("dec = "REG" sprg2= "REG"\n",
  1481. mfspr(SPRN_DEC), mfspr(SPRN_SPRG2));
  1482. printf("sp = "REG" sprg3= "REG"\n", sp, mfspr(SPRN_SPRG3));
  1483. printf("toc = "REG" dar = "REG"\n", toc, mfspr(SPRN_DAR));
  1484. return;
  1485. }
  1486. scanhex(&regno);
  1487. switch (cmd) {
  1488. case 'w':
  1489. val = read_spr(regno);
  1490. scanhex(&val);
  1491. write_spr(regno, val);
  1492. /* fall through */
  1493. case 'r':
  1494. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1495. break;
  1496. }
  1497. scannl();
  1498. }
  1499. /*
  1500. * Stuff for reading and writing memory safely
  1501. */
  1502. static int
  1503. mread(unsigned long adrs, void *buf, int size)
  1504. {
  1505. volatile int n;
  1506. char *p, *q;
  1507. n = 0;
  1508. if (setjmp(bus_error_jmp) == 0) {
  1509. catch_memory_errors = 1;
  1510. sync();
  1511. p = (char *)adrs;
  1512. q = (char *)buf;
  1513. switch (size) {
  1514. case 2:
  1515. *(u16 *)q = *(u16 *)p;
  1516. break;
  1517. case 4:
  1518. *(u32 *)q = *(u32 *)p;
  1519. break;
  1520. case 8:
  1521. *(u64 *)q = *(u64 *)p;
  1522. break;
  1523. default:
  1524. for( ; n < size; ++n) {
  1525. *q++ = *p++;
  1526. sync();
  1527. }
  1528. }
  1529. sync();
  1530. /* wait a little while to see if we get a machine check */
  1531. __delay(200);
  1532. n = size;
  1533. }
  1534. catch_memory_errors = 0;
  1535. return n;
  1536. }
  1537. static int
  1538. mwrite(unsigned long adrs, void *buf, int size)
  1539. {
  1540. volatile int n;
  1541. char *p, *q;
  1542. n = 0;
  1543. if (setjmp(bus_error_jmp) == 0) {
  1544. catch_memory_errors = 1;
  1545. sync();
  1546. p = (char *) adrs;
  1547. q = (char *) buf;
  1548. switch (size) {
  1549. case 2:
  1550. *(u16 *)p = *(u16 *)q;
  1551. break;
  1552. case 4:
  1553. *(u32 *)p = *(u32 *)q;
  1554. break;
  1555. case 8:
  1556. *(u64 *)p = *(u64 *)q;
  1557. break;
  1558. default:
  1559. for ( ; n < size; ++n) {
  1560. *p++ = *q++;
  1561. sync();
  1562. }
  1563. }
  1564. sync();
  1565. /* wait a little while to see if we get a machine check */
  1566. __delay(200);
  1567. n = size;
  1568. } else {
  1569. printf("*** Error writing address %x\n", adrs + n);
  1570. }
  1571. catch_memory_errors = 0;
  1572. return n;
  1573. }
  1574. static int fault_type;
  1575. static int fault_except;
  1576. static char *fault_chars[] = { "--", "**", "##" };
  1577. static int handle_fault(struct pt_regs *regs)
  1578. {
  1579. fault_except = TRAP(regs);
  1580. switch (TRAP(regs)) {
  1581. case 0x200:
  1582. fault_type = 0;
  1583. break;
  1584. case 0x300:
  1585. case 0x380:
  1586. fault_type = 1;
  1587. break;
  1588. default:
  1589. fault_type = 2;
  1590. }
  1591. longjmp(bus_error_jmp, 1);
  1592. return 0;
  1593. }
  1594. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1595. static void
  1596. byterev(unsigned char *val, int size)
  1597. {
  1598. int t;
  1599. switch (size) {
  1600. case 2:
  1601. SWAP(val[0], val[1], t);
  1602. break;
  1603. case 4:
  1604. SWAP(val[0], val[3], t);
  1605. SWAP(val[1], val[2], t);
  1606. break;
  1607. case 8: /* is there really any use for this? */
  1608. SWAP(val[0], val[7], t);
  1609. SWAP(val[1], val[6], t);
  1610. SWAP(val[2], val[5], t);
  1611. SWAP(val[3], val[4], t);
  1612. break;
  1613. }
  1614. }
  1615. static int brev;
  1616. static int mnoread;
  1617. static char *memex_help_string =
  1618. "Memory examine command usage:\n"
  1619. "m [addr] [flags] examine/change memory\n"
  1620. " addr is optional. will start where left off.\n"
  1621. " flags may include chars from this set:\n"
  1622. " b modify by bytes (default)\n"
  1623. " w modify by words (2 byte)\n"
  1624. " l modify by longs (4 byte)\n"
  1625. " d modify by doubleword (8 byte)\n"
  1626. " r toggle reverse byte order mode\n"
  1627. " n do not read memory (for i/o spaces)\n"
  1628. " . ok to read (default)\n"
  1629. "NOTE: flags are saved as defaults\n"
  1630. "";
  1631. static char *memex_subcmd_help_string =
  1632. "Memory examine subcommands:\n"
  1633. " hexval write this val to current location\n"
  1634. " 'string' write chars from string to this location\n"
  1635. " ' increment address\n"
  1636. " ^ decrement address\n"
  1637. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1638. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1639. " ` clear no-read flag\n"
  1640. " ; stay at this addr\n"
  1641. " v change to byte mode\n"
  1642. " w change to word (2 byte) mode\n"
  1643. " l change to long (4 byte) mode\n"
  1644. " u change to doubleword (8 byte) mode\n"
  1645. " m addr change current addr\n"
  1646. " n toggle no-read flag\n"
  1647. " r toggle byte reverse flag\n"
  1648. " < count back up count bytes\n"
  1649. " > count skip forward count bytes\n"
  1650. " x exit this mode\n"
  1651. "";
  1652. static void
  1653. memex(void)
  1654. {
  1655. int cmd, inc, i, nslash;
  1656. unsigned long n;
  1657. unsigned char val[16];
  1658. scanhex((void *)&adrs);
  1659. cmd = skipbl();
  1660. if (cmd == '?') {
  1661. printf(memex_help_string);
  1662. return;
  1663. } else {
  1664. termch = cmd;
  1665. }
  1666. last_cmd = "m\n";
  1667. while ((cmd = skipbl()) != '\n') {
  1668. switch( cmd ){
  1669. case 'b': size = 1; break;
  1670. case 'w': size = 2; break;
  1671. case 'l': size = 4; break;
  1672. case 'd': size = 8; break;
  1673. case 'r': brev = !brev; break;
  1674. case 'n': mnoread = 1; break;
  1675. case '.': mnoread = 0; break;
  1676. }
  1677. }
  1678. if( size <= 0 )
  1679. size = 1;
  1680. else if( size > 8 )
  1681. size = 8;
  1682. for(;;){
  1683. if (!mnoread)
  1684. n = mread(adrs, val, size);
  1685. printf(REG"%c", adrs, brev? 'r': ' ');
  1686. if (!mnoread) {
  1687. if (brev)
  1688. byterev(val, size);
  1689. putchar(' ');
  1690. for (i = 0; i < n; ++i)
  1691. printf("%.2x", val[i]);
  1692. for (; i < size; ++i)
  1693. printf("%s", fault_chars[fault_type]);
  1694. }
  1695. putchar(' ');
  1696. inc = size;
  1697. nslash = 0;
  1698. for(;;){
  1699. if( scanhex(&n) ){
  1700. for (i = 0; i < size; ++i)
  1701. val[i] = n >> (i * 8);
  1702. if (!brev)
  1703. byterev(val, size);
  1704. mwrite(adrs, val, size);
  1705. inc = size;
  1706. }
  1707. cmd = skipbl();
  1708. if (cmd == '\n')
  1709. break;
  1710. inc = 0;
  1711. switch (cmd) {
  1712. case '\'':
  1713. for(;;){
  1714. n = inchar();
  1715. if( n == '\\' )
  1716. n = bsesc();
  1717. else if( n == '\'' )
  1718. break;
  1719. for (i = 0; i < size; ++i)
  1720. val[i] = n >> (i * 8);
  1721. if (!brev)
  1722. byterev(val, size);
  1723. mwrite(adrs, val, size);
  1724. adrs += size;
  1725. }
  1726. adrs -= size;
  1727. inc = size;
  1728. break;
  1729. case ',':
  1730. adrs += size;
  1731. break;
  1732. case '.':
  1733. mnoread = 0;
  1734. break;
  1735. case ';':
  1736. break;
  1737. case 'x':
  1738. case EOF:
  1739. scannl();
  1740. return;
  1741. case 'b':
  1742. case 'v':
  1743. size = 1;
  1744. break;
  1745. case 'w':
  1746. size = 2;
  1747. break;
  1748. case 'l':
  1749. size = 4;
  1750. break;
  1751. case 'u':
  1752. size = 8;
  1753. break;
  1754. case '^':
  1755. adrs -= size;
  1756. break;
  1757. break;
  1758. case '/':
  1759. if (nslash > 0)
  1760. adrs -= 1 << nslash;
  1761. else
  1762. nslash = 0;
  1763. nslash += 4;
  1764. adrs += 1 << nslash;
  1765. break;
  1766. case '\\':
  1767. if (nslash < 0)
  1768. adrs += 1 << -nslash;
  1769. else
  1770. nslash = 0;
  1771. nslash -= 4;
  1772. adrs -= 1 << -nslash;
  1773. break;
  1774. case 'm':
  1775. scanhex((void *)&adrs);
  1776. break;
  1777. case 'n':
  1778. mnoread = 1;
  1779. break;
  1780. case 'r':
  1781. brev = !brev;
  1782. break;
  1783. case '<':
  1784. n = size;
  1785. scanhex(&n);
  1786. adrs -= n;
  1787. break;
  1788. case '>':
  1789. n = size;
  1790. scanhex(&n);
  1791. adrs += n;
  1792. break;
  1793. case '?':
  1794. printf(memex_subcmd_help_string);
  1795. break;
  1796. }
  1797. }
  1798. adrs += inc;
  1799. }
  1800. }
  1801. static int
  1802. bsesc(void)
  1803. {
  1804. int c;
  1805. c = inchar();
  1806. switch( c ){
  1807. case 'n': c = '\n'; break;
  1808. case 'r': c = '\r'; break;
  1809. case 'b': c = '\b'; break;
  1810. case 't': c = '\t'; break;
  1811. }
  1812. return c;
  1813. }
  1814. static void xmon_rawdump (unsigned long adrs, long ndump)
  1815. {
  1816. long n, m, r, nr;
  1817. unsigned char temp[16];
  1818. for (n = ndump; n > 0;) {
  1819. r = n < 16? n: 16;
  1820. nr = mread(adrs, temp, r);
  1821. adrs += nr;
  1822. for (m = 0; m < r; ++m) {
  1823. if (m < nr)
  1824. printf("%.2x", temp[m]);
  1825. else
  1826. printf("%s", fault_chars[fault_type]);
  1827. }
  1828. n -= r;
  1829. if (nr < r)
  1830. break;
  1831. }
  1832. printf("\n");
  1833. }
  1834. #ifdef CONFIG_PPC64
  1835. static void dump_one_paca(int cpu)
  1836. {
  1837. struct paca_struct *p;
  1838. if (setjmp(bus_error_jmp) != 0) {
  1839. printf("*** Error dumping paca for cpu 0x%x!\n", cpu);
  1840. return;
  1841. }
  1842. catch_memory_errors = 1;
  1843. sync();
  1844. p = &paca[cpu];
  1845. printf("paca for cpu 0x%x @ %p:\n", cpu, p);
  1846. printf(" %-*s = %s\n", 16, "possible", cpu_possible(cpu) ? "yes" : "no");
  1847. printf(" %-*s = %s\n", 16, "present", cpu_present(cpu) ? "yes" : "no");
  1848. printf(" %-*s = %s\n", 16, "online", cpu_online(cpu) ? "yes" : "no");
  1849. #define DUMP(paca, name, format) \
  1850. printf(" %-*s = %#-*"format"\t(0x%lx)\n", 16, #name, 18, paca->name, \
  1851. offsetof(struct paca_struct, name));
  1852. DUMP(p, lock_token, "x");
  1853. DUMP(p, paca_index, "x");
  1854. DUMP(p, kernel_toc, "lx");
  1855. DUMP(p, kernelbase, "lx");
  1856. DUMP(p, kernel_msr, "lx");
  1857. #ifdef CONFIG_PPC_STD_MMU_64
  1858. DUMP(p, stab_real, "lx");
  1859. DUMP(p, stab_addr, "lx");
  1860. #endif
  1861. DUMP(p, emergency_sp, "p");
  1862. DUMP(p, data_offset, "lx");
  1863. DUMP(p, hw_cpu_id, "x");
  1864. DUMP(p, cpu_start, "x");
  1865. DUMP(p, kexec_state, "x");
  1866. DUMP(p, __current, "p");
  1867. DUMP(p, kstack, "lx");
  1868. DUMP(p, stab_rr, "lx");
  1869. DUMP(p, saved_r1, "lx");
  1870. DUMP(p, trap_save, "x");
  1871. DUMP(p, soft_enabled, "x");
  1872. DUMP(p, irq_happened, "x");
  1873. DUMP(p, io_sync, "x");
  1874. DUMP(p, irq_work_pending, "x");
  1875. DUMP(p, nap_state_lost, "x");
  1876. #undef DUMP
  1877. catch_memory_errors = 0;
  1878. sync();
  1879. }
  1880. static void dump_all_pacas(void)
  1881. {
  1882. int cpu;
  1883. if (num_possible_cpus() == 0) {
  1884. printf("No possible cpus, use 'dp #' to dump individual cpus\n");
  1885. return;
  1886. }
  1887. for_each_possible_cpu(cpu)
  1888. dump_one_paca(cpu);
  1889. }
  1890. static void dump_pacas(void)
  1891. {
  1892. unsigned long num;
  1893. int c;
  1894. c = inchar();
  1895. if (c == 'a') {
  1896. dump_all_pacas();
  1897. return;
  1898. }
  1899. termch = c; /* Put c back, it wasn't 'a' */
  1900. if (scanhex(&num))
  1901. dump_one_paca(num);
  1902. else
  1903. dump_one_paca(xmon_owner);
  1904. }
  1905. #endif
  1906. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1907. || ('a' <= (c) && (c) <= 'f') \
  1908. || ('A' <= (c) && (c) <= 'F'))
  1909. static void
  1910. dump(void)
  1911. {
  1912. int c;
  1913. c = inchar();
  1914. #ifdef CONFIG_PPC64
  1915. if (c == 'p') {
  1916. dump_pacas();
  1917. return;
  1918. }
  1919. #endif
  1920. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1921. termch = c;
  1922. scanhex((void *)&adrs);
  1923. if (termch != '\n')
  1924. termch = 0;
  1925. if (c == 'i') {
  1926. scanhex(&nidump);
  1927. if (nidump == 0)
  1928. nidump = 16;
  1929. else if (nidump > MAX_DUMP)
  1930. nidump = MAX_DUMP;
  1931. adrs += ppc_inst_dump(adrs, nidump, 1);
  1932. last_cmd = "di\n";
  1933. } else if (c == 'l') {
  1934. dump_log_buf();
  1935. } else if (c == 'r') {
  1936. scanhex(&ndump);
  1937. if (ndump == 0)
  1938. ndump = 64;
  1939. xmon_rawdump(adrs, ndump);
  1940. adrs += ndump;
  1941. last_cmd = "dr\n";
  1942. } else {
  1943. scanhex(&ndump);
  1944. if (ndump == 0)
  1945. ndump = 64;
  1946. else if (ndump > MAX_DUMP)
  1947. ndump = MAX_DUMP;
  1948. prdump(adrs, ndump);
  1949. adrs += ndump;
  1950. last_cmd = "d\n";
  1951. }
  1952. }
  1953. static void
  1954. prdump(unsigned long adrs, long ndump)
  1955. {
  1956. long n, m, c, r, nr;
  1957. unsigned char temp[16];
  1958. for (n = ndump; n > 0;) {
  1959. printf(REG, adrs);
  1960. putchar(' ');
  1961. r = n < 16? n: 16;
  1962. nr = mread(adrs, temp, r);
  1963. adrs += nr;
  1964. for (m = 0; m < r; ++m) {
  1965. if ((m & (sizeof(long) - 1)) == 0 && m > 0)
  1966. putchar(' ');
  1967. if (m < nr)
  1968. printf("%.2x", temp[m]);
  1969. else
  1970. printf("%s", fault_chars[fault_type]);
  1971. }
  1972. for (; m < 16; ++m) {
  1973. if ((m & (sizeof(long) - 1)) == 0)
  1974. putchar(' ');
  1975. printf(" ");
  1976. }
  1977. printf(" |");
  1978. for (m = 0; m < r; ++m) {
  1979. if (m < nr) {
  1980. c = temp[m];
  1981. putchar(' ' <= c && c <= '~'? c: '.');
  1982. } else
  1983. putchar(' ');
  1984. }
  1985. n -= r;
  1986. for (; m < 16; ++m)
  1987. putchar(' ');
  1988. printf("|\n");
  1989. if (nr < r)
  1990. break;
  1991. }
  1992. }
  1993. typedef int (*instruction_dump_func)(unsigned long inst, unsigned long addr);
  1994. static int
  1995. generic_inst_dump(unsigned long adr, long count, int praddr,
  1996. instruction_dump_func dump_func)
  1997. {
  1998. int nr, dotted;
  1999. unsigned long first_adr;
  2000. unsigned long inst, last_inst = 0;
  2001. unsigned char val[4];
  2002. dotted = 0;
  2003. for (first_adr = adr; count > 0; --count, adr += 4) {
  2004. nr = mread(adr, val, 4);
  2005. if (nr == 0) {
  2006. if (praddr) {
  2007. const char *x = fault_chars[fault_type];
  2008. printf(REG" %s%s%s%s\n", adr, x, x, x, x);
  2009. }
  2010. break;
  2011. }
  2012. inst = GETWORD(val);
  2013. if (adr > first_adr && inst == last_inst) {
  2014. if (!dotted) {
  2015. printf(" ...\n");
  2016. dotted = 1;
  2017. }
  2018. continue;
  2019. }
  2020. dotted = 0;
  2021. last_inst = inst;
  2022. if (praddr)
  2023. printf(REG" %.8x", adr, inst);
  2024. printf("\t");
  2025. dump_func(inst, adr);
  2026. printf("\n");
  2027. }
  2028. return adr - first_adr;
  2029. }
  2030. static int
  2031. ppc_inst_dump(unsigned long adr, long count, int praddr)
  2032. {
  2033. return generic_inst_dump(adr, count, praddr, print_insn_powerpc);
  2034. }
  2035. void
  2036. print_address(unsigned long addr)
  2037. {
  2038. xmon_print_symbol(addr, "\t# ", "");
  2039. }
  2040. void
  2041. dump_log_buf(void)
  2042. {
  2043. struct kmsg_dumper dumper = { .active = 1 };
  2044. unsigned char buf[128];
  2045. size_t len;
  2046. if (setjmp(bus_error_jmp) != 0) {
  2047. printf("Error dumping printk buffer!\n");
  2048. return;
  2049. }
  2050. catch_memory_errors = 1;
  2051. sync();
  2052. kmsg_dump_rewind_nolock(&dumper);
  2053. while (kmsg_dump_get_line_nolock(&dumper, false, buf, sizeof(buf), &len)) {
  2054. buf[len] = '\0';
  2055. printf("%s", buf);
  2056. }
  2057. sync();
  2058. /* wait a little while to see if we get a machine check */
  2059. __delay(200);
  2060. catch_memory_errors = 0;
  2061. }
  2062. /*
  2063. * Memory operations - move, set, print differences
  2064. */
  2065. static unsigned long mdest; /* destination address */
  2066. static unsigned long msrc; /* source address */
  2067. static unsigned long mval; /* byte value to set memory to */
  2068. static unsigned long mcount; /* # bytes to affect */
  2069. static unsigned long mdiffs; /* max # differences to print */
  2070. static void
  2071. memops(int cmd)
  2072. {
  2073. scanhex((void *)&mdest);
  2074. if( termch != '\n' )
  2075. termch = 0;
  2076. scanhex((void *)(cmd == 's'? &mval: &msrc));
  2077. if( termch != '\n' )
  2078. termch = 0;
  2079. scanhex((void *)&mcount);
  2080. switch( cmd ){
  2081. case 'm':
  2082. memmove((void *)mdest, (void *)msrc, mcount);
  2083. break;
  2084. case 's':
  2085. memset((void *)mdest, mval, mcount);
  2086. break;
  2087. case 'd':
  2088. if( termch != '\n' )
  2089. termch = 0;
  2090. scanhex((void *)&mdiffs);
  2091. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  2092. break;
  2093. }
  2094. }
  2095. static void
  2096. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  2097. {
  2098. unsigned n, prt;
  2099. prt = 0;
  2100. for( n = nb; n > 0; --n )
  2101. if( *p1++ != *p2++ )
  2102. if( ++prt <= maxpr )
  2103. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  2104. p1[-1], p2 - 1, p2[-1]);
  2105. if( prt > maxpr )
  2106. printf("Total of %d differences\n", prt);
  2107. }
  2108. static unsigned mend;
  2109. static unsigned mask;
  2110. static void
  2111. memlocate(void)
  2112. {
  2113. unsigned a, n;
  2114. unsigned char val[4];
  2115. last_cmd = "ml";
  2116. scanhex((void *)&mdest);
  2117. if (termch != '\n') {
  2118. termch = 0;
  2119. scanhex((void *)&mend);
  2120. if (termch != '\n') {
  2121. termch = 0;
  2122. scanhex((void *)&mval);
  2123. mask = ~0;
  2124. if (termch != '\n') termch = 0;
  2125. scanhex((void *)&mask);
  2126. }
  2127. }
  2128. n = 0;
  2129. for (a = mdest; a < mend; a += 4) {
  2130. if (mread(a, val, 4) == 4
  2131. && ((GETWORD(val) ^ mval) & mask) == 0) {
  2132. printf("%.16x: %.16x\n", a, GETWORD(val));
  2133. if (++n >= 10)
  2134. break;
  2135. }
  2136. }
  2137. }
  2138. static unsigned long mskip = 0x1000;
  2139. static unsigned long mlim = 0xffffffff;
  2140. static void
  2141. memzcan(void)
  2142. {
  2143. unsigned char v;
  2144. unsigned a;
  2145. int ok, ook;
  2146. scanhex(&mdest);
  2147. if (termch != '\n') termch = 0;
  2148. scanhex(&mskip);
  2149. if (termch != '\n') termch = 0;
  2150. scanhex(&mlim);
  2151. ook = 0;
  2152. for (a = mdest; a < mlim; a += mskip) {
  2153. ok = mread(a, &v, 1);
  2154. if (ok && !ook) {
  2155. printf("%.8x .. ", a);
  2156. } else if (!ok && ook)
  2157. printf("%.8x\n", a - mskip);
  2158. ook = ok;
  2159. if (a + mskip < a)
  2160. break;
  2161. }
  2162. if (ook)
  2163. printf("%.8x\n", a - mskip);
  2164. }
  2165. static void proccall(void)
  2166. {
  2167. unsigned long args[8];
  2168. unsigned long ret;
  2169. int i;
  2170. typedef unsigned long (*callfunc_t)(unsigned long, unsigned long,
  2171. unsigned long, unsigned long, unsigned long,
  2172. unsigned long, unsigned long, unsigned long);
  2173. callfunc_t func;
  2174. if (!scanhex(&adrs))
  2175. return;
  2176. if (termch != '\n')
  2177. termch = 0;
  2178. for (i = 0; i < 8; ++i)
  2179. args[i] = 0;
  2180. for (i = 0; i < 8; ++i) {
  2181. if (!scanhex(&args[i]) || termch == '\n')
  2182. break;
  2183. termch = 0;
  2184. }
  2185. func = (callfunc_t) adrs;
  2186. ret = 0;
  2187. if (setjmp(bus_error_jmp) == 0) {
  2188. catch_memory_errors = 1;
  2189. sync();
  2190. ret = func(args[0], args[1], args[2], args[3],
  2191. args[4], args[5], args[6], args[7]);
  2192. sync();
  2193. printf("return value is %x\n", ret);
  2194. } else {
  2195. printf("*** %x exception occurred\n", fault_except);
  2196. }
  2197. catch_memory_errors = 0;
  2198. }
  2199. /* Input scanning routines */
  2200. int
  2201. skipbl(void)
  2202. {
  2203. int c;
  2204. if( termch != 0 ){
  2205. c = termch;
  2206. termch = 0;
  2207. } else
  2208. c = inchar();
  2209. while( c == ' ' || c == '\t' )
  2210. c = inchar();
  2211. return c;
  2212. }
  2213. #define N_PTREGS 44
  2214. static char *regnames[N_PTREGS] = {
  2215. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  2216. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  2217. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  2218. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  2219. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr",
  2220. #ifdef CONFIG_PPC64
  2221. "softe",
  2222. #else
  2223. "mq",
  2224. #endif
  2225. "trap", "dar", "dsisr", "res"
  2226. };
  2227. int
  2228. scanhex(unsigned long *vp)
  2229. {
  2230. int c, d;
  2231. unsigned long v;
  2232. c = skipbl();
  2233. if (c == '%') {
  2234. /* parse register name */
  2235. char regname[8];
  2236. int i;
  2237. for (i = 0; i < sizeof(regname) - 1; ++i) {
  2238. c = inchar();
  2239. if (!isalnum(c)) {
  2240. termch = c;
  2241. break;
  2242. }
  2243. regname[i] = c;
  2244. }
  2245. regname[i] = 0;
  2246. for (i = 0; i < N_PTREGS; ++i) {
  2247. if (strcmp(regnames[i], regname) == 0) {
  2248. if (xmon_regs == NULL) {
  2249. printf("regs not available\n");
  2250. return 0;
  2251. }
  2252. *vp = ((unsigned long *)xmon_regs)[i];
  2253. return 1;
  2254. }
  2255. }
  2256. printf("invalid register name '%%%s'\n", regname);
  2257. return 0;
  2258. }
  2259. /* skip leading "0x" if any */
  2260. if (c == '0') {
  2261. c = inchar();
  2262. if (c == 'x') {
  2263. c = inchar();
  2264. } else {
  2265. d = hexdigit(c);
  2266. if (d == EOF) {
  2267. termch = c;
  2268. *vp = 0;
  2269. return 1;
  2270. }
  2271. }
  2272. } else if (c == '$') {
  2273. int i;
  2274. for (i=0; i<63; i++) {
  2275. c = inchar();
  2276. if (isspace(c)) {
  2277. termch = c;
  2278. break;
  2279. }
  2280. tmpstr[i] = c;
  2281. }
  2282. tmpstr[i++] = 0;
  2283. *vp = 0;
  2284. if (setjmp(bus_error_jmp) == 0) {
  2285. catch_memory_errors = 1;
  2286. sync();
  2287. *vp = kallsyms_lookup_name(tmpstr);
  2288. sync();
  2289. }
  2290. catch_memory_errors = 0;
  2291. if (!(*vp)) {
  2292. printf("unknown symbol '%s'\n", tmpstr);
  2293. return 0;
  2294. }
  2295. return 1;
  2296. }
  2297. d = hexdigit(c);
  2298. if (d == EOF) {
  2299. termch = c;
  2300. return 0;
  2301. }
  2302. v = 0;
  2303. do {
  2304. v = (v << 4) + d;
  2305. c = inchar();
  2306. d = hexdigit(c);
  2307. } while (d != EOF);
  2308. termch = c;
  2309. *vp = v;
  2310. return 1;
  2311. }
  2312. static void
  2313. scannl(void)
  2314. {
  2315. int c;
  2316. c = termch;
  2317. termch = 0;
  2318. while( c != '\n' )
  2319. c = inchar();
  2320. }
  2321. static int hexdigit(int c)
  2322. {
  2323. if( '0' <= c && c <= '9' )
  2324. return c - '0';
  2325. if( 'A' <= c && c <= 'F' )
  2326. return c - ('A' - 10);
  2327. if( 'a' <= c && c <= 'f' )
  2328. return c - ('a' - 10);
  2329. return EOF;
  2330. }
  2331. void
  2332. getstring(char *s, int size)
  2333. {
  2334. int c;
  2335. c = skipbl();
  2336. do {
  2337. if( size > 1 ){
  2338. *s++ = c;
  2339. --size;
  2340. }
  2341. c = inchar();
  2342. } while( c != ' ' && c != '\t' && c != '\n' );
  2343. termch = c;
  2344. *s = 0;
  2345. }
  2346. static char line[256];
  2347. static char *lineptr;
  2348. static void
  2349. flush_input(void)
  2350. {
  2351. lineptr = NULL;
  2352. }
  2353. static int
  2354. inchar(void)
  2355. {
  2356. if (lineptr == NULL || *lineptr == 0) {
  2357. if (xmon_gets(line, sizeof(line)) == NULL) {
  2358. lineptr = NULL;
  2359. return EOF;
  2360. }
  2361. lineptr = line;
  2362. }
  2363. return *lineptr++;
  2364. }
  2365. static void
  2366. take_input(char *str)
  2367. {
  2368. lineptr = str;
  2369. }
  2370. static void
  2371. symbol_lookup(void)
  2372. {
  2373. int type = inchar();
  2374. unsigned long addr;
  2375. static char tmp[64];
  2376. switch (type) {
  2377. case 'a':
  2378. if (scanhex(&addr))
  2379. xmon_print_symbol(addr, ": ", "\n");
  2380. termch = 0;
  2381. break;
  2382. case 's':
  2383. getstring(tmp, 64);
  2384. if (setjmp(bus_error_jmp) == 0) {
  2385. catch_memory_errors = 1;
  2386. sync();
  2387. addr = kallsyms_lookup_name(tmp);
  2388. if (addr)
  2389. printf("%s: %lx\n", tmp, addr);
  2390. else
  2391. printf("Symbol '%s' not found.\n", tmp);
  2392. sync();
  2393. }
  2394. catch_memory_errors = 0;
  2395. termch = 0;
  2396. break;
  2397. }
  2398. }
  2399. /* Print an address in numeric and symbolic form (if possible) */
  2400. static void xmon_print_symbol(unsigned long address, const char *mid,
  2401. const char *after)
  2402. {
  2403. char *modname;
  2404. const char *name = NULL;
  2405. unsigned long offset, size;
  2406. printf(REG, address);
  2407. if (setjmp(bus_error_jmp) == 0) {
  2408. catch_memory_errors = 1;
  2409. sync();
  2410. name = kallsyms_lookup(address, &size, &offset, &modname,
  2411. tmpstr);
  2412. sync();
  2413. /* wait a little while to see if we get a machine check */
  2414. __delay(200);
  2415. }
  2416. catch_memory_errors = 0;
  2417. if (name) {
  2418. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2419. if (modname)
  2420. printf(" [%s]", modname);
  2421. }
  2422. printf("%s", after);
  2423. }
  2424. #ifdef CONFIG_PPC_BOOK3S_64
  2425. static void dump_slb(void)
  2426. {
  2427. int i;
  2428. unsigned long esid,vsid,valid;
  2429. unsigned long llp;
  2430. printf("SLB contents of cpu %x\n", smp_processor_id());
  2431. for (i = 0; i < mmu_slb_size; i++) {
  2432. asm volatile("slbmfee %0,%1" : "=r" (esid) : "r" (i));
  2433. asm volatile("slbmfev %0,%1" : "=r" (vsid) : "r" (i));
  2434. valid = (esid & SLB_ESID_V);
  2435. if (valid | esid | vsid) {
  2436. printf("%02d %016lx %016lx", i, esid, vsid);
  2437. if (valid) {
  2438. llp = vsid & SLB_VSID_LLP;
  2439. if (vsid & SLB_VSID_B_1T) {
  2440. printf(" 1T ESID=%9lx VSID=%13lx LLP:%3lx \n",
  2441. GET_ESID_1T(esid),
  2442. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT_1T,
  2443. llp);
  2444. } else {
  2445. printf(" 256M ESID=%9lx VSID=%13lx LLP:%3lx \n",
  2446. GET_ESID(esid),
  2447. (vsid & ~SLB_VSID_B) >> SLB_VSID_SHIFT,
  2448. llp);
  2449. }
  2450. } else
  2451. printf("\n");
  2452. }
  2453. }
  2454. }
  2455. static void dump_stab(void)
  2456. {
  2457. int i;
  2458. unsigned long *tmp = (unsigned long *)local_paca->stab_addr;
  2459. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2460. for (i = 0; i < PAGE_SIZE/16; i++) {
  2461. unsigned long a, b;
  2462. a = *tmp++;
  2463. b = *tmp++;
  2464. if (a || b) {
  2465. printf("%03d %016lx ", i, a);
  2466. printf("%016lx\n", b);
  2467. }
  2468. }
  2469. }
  2470. void dump_segments(void)
  2471. {
  2472. if (mmu_has_feature(MMU_FTR_SLB))
  2473. dump_slb();
  2474. else
  2475. dump_stab();
  2476. }
  2477. #endif
  2478. #ifdef CONFIG_PPC_STD_MMU_32
  2479. void dump_segments(void)
  2480. {
  2481. int i;
  2482. printf("sr0-15 =");
  2483. for (i = 0; i < 16; ++i)
  2484. printf(" %x", mfsrin(i));
  2485. printf("\n");
  2486. }
  2487. #endif
  2488. #ifdef CONFIG_44x
  2489. static void dump_tlb_44x(void)
  2490. {
  2491. int i;
  2492. for (i = 0; i < PPC44x_TLB_SIZE; i++) {
  2493. unsigned long w0,w1,w2;
  2494. asm volatile("tlbre %0,%1,0" : "=r" (w0) : "r" (i));
  2495. asm volatile("tlbre %0,%1,1" : "=r" (w1) : "r" (i));
  2496. asm volatile("tlbre %0,%1,2" : "=r" (w2) : "r" (i));
  2497. printf("[%02x] %08x %08x %08x ", i, w0, w1, w2);
  2498. if (w0 & PPC44x_TLB_VALID) {
  2499. printf("V %08x -> %01x%08x %c%c%c%c%c",
  2500. w0 & PPC44x_TLB_EPN_MASK,
  2501. w1 & PPC44x_TLB_ERPN_MASK,
  2502. w1 & PPC44x_TLB_RPN_MASK,
  2503. (w2 & PPC44x_TLB_W) ? 'W' : 'w',
  2504. (w2 & PPC44x_TLB_I) ? 'I' : 'i',
  2505. (w2 & PPC44x_TLB_M) ? 'M' : 'm',
  2506. (w2 & PPC44x_TLB_G) ? 'G' : 'g',
  2507. (w2 & PPC44x_TLB_E) ? 'E' : 'e');
  2508. }
  2509. printf("\n");
  2510. }
  2511. }
  2512. #endif /* CONFIG_44x */
  2513. #ifdef CONFIG_PPC_BOOK3E
  2514. static void dump_tlb_book3e(void)
  2515. {
  2516. u32 mmucfg, pidmask, lpidmask;
  2517. u64 ramask;
  2518. int i, tlb, ntlbs, pidsz, lpidsz, rasz, lrat = 0;
  2519. int mmu_version;
  2520. static const char *pgsz_names[] = {
  2521. " 1K",
  2522. " 2K",
  2523. " 4K",
  2524. " 8K",
  2525. " 16K",
  2526. " 32K",
  2527. " 64K",
  2528. "128K",
  2529. "256K",
  2530. "512K",
  2531. " 1M",
  2532. " 2M",
  2533. " 4M",
  2534. " 8M",
  2535. " 16M",
  2536. " 32M",
  2537. " 64M",
  2538. "128M",
  2539. "256M",
  2540. "512M",
  2541. " 1G",
  2542. " 2G",
  2543. " 4G",
  2544. " 8G",
  2545. " 16G",
  2546. " 32G",
  2547. " 64G",
  2548. "128G",
  2549. "256G",
  2550. "512G",
  2551. " 1T",
  2552. " 2T",
  2553. };
  2554. /* Gather some infos about the MMU */
  2555. mmucfg = mfspr(SPRN_MMUCFG);
  2556. mmu_version = (mmucfg & 3) + 1;
  2557. ntlbs = ((mmucfg >> 2) & 3) + 1;
  2558. pidsz = ((mmucfg >> 6) & 0x1f) + 1;
  2559. lpidsz = (mmucfg >> 24) & 0xf;
  2560. rasz = (mmucfg >> 16) & 0x7f;
  2561. if ((mmu_version > 1) && (mmucfg & 0x10000))
  2562. lrat = 1;
  2563. printf("Book3E MMU MAV=%d.0,%d TLBs,%d-bit PID,%d-bit LPID,%d-bit RA\n",
  2564. mmu_version, ntlbs, pidsz, lpidsz, rasz);
  2565. pidmask = (1ul << pidsz) - 1;
  2566. lpidmask = (1ul << lpidsz) - 1;
  2567. ramask = (1ull << rasz) - 1;
  2568. for (tlb = 0; tlb < ntlbs; tlb++) {
  2569. u32 tlbcfg;
  2570. int nent, assoc, new_cc = 1;
  2571. printf("TLB %d:\n------\n", tlb);
  2572. switch(tlb) {
  2573. case 0:
  2574. tlbcfg = mfspr(SPRN_TLB0CFG);
  2575. break;
  2576. case 1:
  2577. tlbcfg = mfspr(SPRN_TLB1CFG);
  2578. break;
  2579. case 2:
  2580. tlbcfg = mfspr(SPRN_TLB2CFG);
  2581. break;
  2582. case 3:
  2583. tlbcfg = mfspr(SPRN_TLB3CFG);
  2584. break;
  2585. default:
  2586. printf("Unsupported TLB number !\n");
  2587. continue;
  2588. }
  2589. nent = tlbcfg & 0xfff;
  2590. assoc = (tlbcfg >> 24) & 0xff;
  2591. for (i = 0; i < nent; i++) {
  2592. u32 mas0 = MAS0_TLBSEL(tlb);
  2593. u32 mas1 = MAS1_TSIZE(BOOK3E_PAGESZ_4K);
  2594. u64 mas2 = 0;
  2595. u64 mas7_mas3;
  2596. int esel = i, cc = i;
  2597. if (assoc != 0) {
  2598. cc = i / assoc;
  2599. esel = i % assoc;
  2600. mas2 = cc * 0x1000;
  2601. }
  2602. mas0 |= MAS0_ESEL(esel);
  2603. mtspr(SPRN_MAS0, mas0);
  2604. mtspr(SPRN_MAS1, mas1);
  2605. mtspr(SPRN_MAS2, mas2);
  2606. asm volatile("tlbre 0,0,0" : : : "memory");
  2607. mas1 = mfspr(SPRN_MAS1);
  2608. mas2 = mfspr(SPRN_MAS2);
  2609. mas7_mas3 = mfspr(SPRN_MAS7_MAS3);
  2610. if (assoc && (i % assoc) == 0)
  2611. new_cc = 1;
  2612. if (!(mas1 & MAS1_VALID))
  2613. continue;
  2614. if (assoc == 0)
  2615. printf("%04x- ", i);
  2616. else if (new_cc)
  2617. printf("%04x-%c", cc, 'A' + esel);
  2618. else
  2619. printf(" |%c", 'A' + esel);
  2620. new_cc = 0;
  2621. printf(" %016llx %04x %s %c%c AS%c",
  2622. mas2 & ~0x3ffull,
  2623. (mas1 >> 16) & 0x3fff,
  2624. pgsz_names[(mas1 >> 7) & 0x1f],
  2625. mas1 & MAS1_IND ? 'I' : ' ',
  2626. mas1 & MAS1_IPROT ? 'P' : ' ',
  2627. mas1 & MAS1_TS ? '1' : '0');
  2628. printf(" %c%c%c%c%c%c%c",
  2629. mas2 & MAS2_X0 ? 'a' : ' ',
  2630. mas2 & MAS2_X1 ? 'v' : ' ',
  2631. mas2 & MAS2_W ? 'w' : ' ',
  2632. mas2 & MAS2_I ? 'i' : ' ',
  2633. mas2 & MAS2_M ? 'm' : ' ',
  2634. mas2 & MAS2_G ? 'g' : ' ',
  2635. mas2 & MAS2_E ? 'e' : ' ');
  2636. printf(" %016llx", mas7_mas3 & ramask & ~0x7ffull);
  2637. if (mas1 & MAS1_IND)
  2638. printf(" %s\n",
  2639. pgsz_names[(mas7_mas3 >> 1) & 0x1f]);
  2640. else
  2641. printf(" U%c%c%c S%c%c%c\n",
  2642. mas7_mas3 & MAS3_UX ? 'x' : ' ',
  2643. mas7_mas3 & MAS3_UW ? 'w' : ' ',
  2644. mas7_mas3 & MAS3_UR ? 'r' : ' ',
  2645. mas7_mas3 & MAS3_SX ? 'x' : ' ',
  2646. mas7_mas3 & MAS3_SW ? 'w' : ' ',
  2647. mas7_mas3 & MAS3_SR ? 'r' : ' ');
  2648. }
  2649. }
  2650. }
  2651. #endif /* CONFIG_PPC_BOOK3E */
  2652. static void xmon_init(int enable)
  2653. {
  2654. if (enable) {
  2655. __debugger = xmon;
  2656. __debugger_ipi = xmon_ipi;
  2657. __debugger_bpt = xmon_bpt;
  2658. __debugger_sstep = xmon_sstep;
  2659. __debugger_iabr_match = xmon_iabr_match;
  2660. __debugger_break_match = xmon_break_match;
  2661. __debugger_fault_handler = xmon_fault_handler;
  2662. } else {
  2663. __debugger = NULL;
  2664. __debugger_ipi = NULL;
  2665. __debugger_bpt = NULL;
  2666. __debugger_sstep = NULL;
  2667. __debugger_iabr_match = NULL;
  2668. __debugger_break_match = NULL;
  2669. __debugger_fault_handler = NULL;
  2670. }
  2671. }
  2672. #ifdef CONFIG_MAGIC_SYSRQ
  2673. static void sysrq_handle_xmon(int key)
  2674. {
  2675. /* ensure xmon is enabled */
  2676. xmon_init(1);
  2677. debugger(get_irq_regs());
  2678. }
  2679. static struct sysrq_key_op sysrq_xmon_op = {
  2680. .handler = sysrq_handle_xmon,
  2681. .help_msg = "xmon(x)",
  2682. .action_msg = "Entering xmon",
  2683. };
  2684. static int __init setup_xmon_sysrq(void)
  2685. {
  2686. register_sysrq_key('x', &sysrq_xmon_op);
  2687. return 0;
  2688. }
  2689. __initcall(setup_xmon_sysrq);
  2690. #endif /* CONFIG_MAGIC_SYSRQ */
  2691. static int __initdata xmon_early, xmon_off;
  2692. static int __init early_parse_xmon(char *p)
  2693. {
  2694. if (!p || strncmp(p, "early", 5) == 0) {
  2695. /* just "xmon" is equivalent to "xmon=early" */
  2696. xmon_init(1);
  2697. xmon_early = 1;
  2698. } else if (strncmp(p, "on", 2) == 0)
  2699. xmon_init(1);
  2700. else if (strncmp(p, "off", 3) == 0)
  2701. xmon_off = 1;
  2702. else if (strncmp(p, "nobt", 4) == 0)
  2703. xmon_no_auto_backtrace = 1;
  2704. else
  2705. return 1;
  2706. return 0;
  2707. }
  2708. early_param("xmon", early_parse_xmon);
  2709. void __init xmon_setup(void)
  2710. {
  2711. #ifdef CONFIG_XMON_DEFAULT
  2712. if (!xmon_off)
  2713. xmon_init(1);
  2714. #endif
  2715. if (xmon_early)
  2716. debugger(NULL);
  2717. }
  2718. #ifdef CONFIG_SPU_BASE
  2719. struct spu_info {
  2720. struct spu *spu;
  2721. u64 saved_mfc_sr1_RW;
  2722. u32 saved_spu_runcntl_RW;
  2723. unsigned long dump_addr;
  2724. u8 stopped_ok;
  2725. };
  2726. #define XMON_NUM_SPUS 16 /* Enough for current hardware */
  2727. static struct spu_info spu_info[XMON_NUM_SPUS];
  2728. void xmon_register_spus(struct list_head *list)
  2729. {
  2730. struct spu *spu;
  2731. list_for_each_entry(spu, list, full_list) {
  2732. if (spu->number >= XMON_NUM_SPUS) {
  2733. WARN_ON(1);
  2734. continue;
  2735. }
  2736. spu_info[spu->number].spu = spu;
  2737. spu_info[spu->number].stopped_ok = 0;
  2738. spu_info[spu->number].dump_addr = (unsigned long)
  2739. spu_info[spu->number].spu->local_store;
  2740. }
  2741. }
  2742. static void stop_spus(void)
  2743. {
  2744. struct spu *spu;
  2745. int i;
  2746. u64 tmp;
  2747. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2748. if (!spu_info[i].spu)
  2749. continue;
  2750. if (setjmp(bus_error_jmp) == 0) {
  2751. catch_memory_errors = 1;
  2752. sync();
  2753. spu = spu_info[i].spu;
  2754. spu_info[i].saved_spu_runcntl_RW =
  2755. in_be32(&spu->problem->spu_runcntl_RW);
  2756. tmp = spu_mfc_sr1_get(spu);
  2757. spu_info[i].saved_mfc_sr1_RW = tmp;
  2758. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  2759. spu_mfc_sr1_set(spu, tmp);
  2760. sync();
  2761. __delay(200);
  2762. spu_info[i].stopped_ok = 1;
  2763. printf("Stopped spu %.2d (was %s)\n", i,
  2764. spu_info[i].saved_spu_runcntl_RW ?
  2765. "running" : "stopped");
  2766. } else {
  2767. catch_memory_errors = 0;
  2768. printf("*** Error stopping spu %.2d\n", i);
  2769. }
  2770. catch_memory_errors = 0;
  2771. }
  2772. }
  2773. static void restart_spus(void)
  2774. {
  2775. struct spu *spu;
  2776. int i;
  2777. for (i = 0; i < XMON_NUM_SPUS; i++) {
  2778. if (!spu_info[i].spu)
  2779. continue;
  2780. if (!spu_info[i].stopped_ok) {
  2781. printf("*** Error, spu %d was not successfully stopped"
  2782. ", not restarting\n", i);
  2783. continue;
  2784. }
  2785. if (setjmp(bus_error_jmp) == 0) {
  2786. catch_memory_errors = 1;
  2787. sync();
  2788. spu = spu_info[i].spu;
  2789. spu_mfc_sr1_set(spu, spu_info[i].saved_mfc_sr1_RW);
  2790. out_be32(&spu->problem->spu_runcntl_RW,
  2791. spu_info[i].saved_spu_runcntl_RW);
  2792. sync();
  2793. __delay(200);
  2794. printf("Restarted spu %.2d\n", i);
  2795. } else {
  2796. catch_memory_errors = 0;
  2797. printf("*** Error restarting spu %.2d\n", i);
  2798. }
  2799. catch_memory_errors = 0;
  2800. }
  2801. }
  2802. #define DUMP_WIDTH 23
  2803. #define DUMP_VALUE(format, field, value) \
  2804. do { \
  2805. if (setjmp(bus_error_jmp) == 0) { \
  2806. catch_memory_errors = 1; \
  2807. sync(); \
  2808. printf(" %-*s = "format"\n", DUMP_WIDTH, \
  2809. #field, value); \
  2810. sync(); \
  2811. __delay(200); \
  2812. } else { \
  2813. catch_memory_errors = 0; \
  2814. printf(" %-*s = *** Error reading field.\n", \
  2815. DUMP_WIDTH, #field); \
  2816. } \
  2817. catch_memory_errors = 0; \
  2818. } while (0)
  2819. #define DUMP_FIELD(obj, format, field) \
  2820. DUMP_VALUE(format, field, obj->field)
  2821. static void dump_spu_fields(struct spu *spu)
  2822. {
  2823. printf("Dumping spu fields at address %p:\n", spu);
  2824. DUMP_FIELD(spu, "0x%x", number);
  2825. DUMP_FIELD(spu, "%s", name);
  2826. DUMP_FIELD(spu, "0x%lx", local_store_phys);
  2827. DUMP_FIELD(spu, "0x%p", local_store);
  2828. DUMP_FIELD(spu, "0x%lx", ls_size);
  2829. DUMP_FIELD(spu, "0x%x", node);
  2830. DUMP_FIELD(spu, "0x%lx", flags);
  2831. DUMP_FIELD(spu, "%d", class_0_pending);
  2832. DUMP_FIELD(spu, "0x%lx", class_0_dar);
  2833. DUMP_FIELD(spu, "0x%lx", class_1_dar);
  2834. DUMP_FIELD(spu, "0x%lx", class_1_dsisr);
  2835. DUMP_FIELD(spu, "0x%lx", irqs[0]);
  2836. DUMP_FIELD(spu, "0x%lx", irqs[1]);
  2837. DUMP_FIELD(spu, "0x%lx", irqs[2]);
  2838. DUMP_FIELD(spu, "0x%x", slb_replace);
  2839. DUMP_FIELD(spu, "%d", pid);
  2840. DUMP_FIELD(spu, "0x%p", mm);
  2841. DUMP_FIELD(spu, "0x%p", ctx);
  2842. DUMP_FIELD(spu, "0x%p", rq);
  2843. DUMP_FIELD(spu, "0x%p", timestamp);
  2844. DUMP_FIELD(spu, "0x%lx", problem_phys);
  2845. DUMP_FIELD(spu, "0x%p", problem);
  2846. DUMP_VALUE("0x%x", problem->spu_runcntl_RW,
  2847. in_be32(&spu->problem->spu_runcntl_RW));
  2848. DUMP_VALUE("0x%x", problem->spu_status_R,
  2849. in_be32(&spu->problem->spu_status_R));
  2850. DUMP_VALUE("0x%x", problem->spu_npc_RW,
  2851. in_be32(&spu->problem->spu_npc_RW));
  2852. DUMP_FIELD(spu, "0x%p", priv2);
  2853. DUMP_FIELD(spu, "0x%p", pdata);
  2854. }
  2855. int
  2856. spu_inst_dump(unsigned long adr, long count, int praddr)
  2857. {
  2858. return generic_inst_dump(adr, count, praddr, print_insn_spu);
  2859. }
  2860. static void dump_spu_ls(unsigned long num, int subcmd)
  2861. {
  2862. unsigned long offset, addr, ls_addr;
  2863. if (setjmp(bus_error_jmp) == 0) {
  2864. catch_memory_errors = 1;
  2865. sync();
  2866. ls_addr = (unsigned long)spu_info[num].spu->local_store;
  2867. sync();
  2868. __delay(200);
  2869. } else {
  2870. catch_memory_errors = 0;
  2871. printf("*** Error: accessing spu info for spu %d\n", num);
  2872. return;
  2873. }
  2874. catch_memory_errors = 0;
  2875. if (scanhex(&offset))
  2876. addr = ls_addr + offset;
  2877. else
  2878. addr = spu_info[num].dump_addr;
  2879. if (addr >= ls_addr + LS_SIZE) {
  2880. printf("*** Error: address outside of local store\n");
  2881. return;
  2882. }
  2883. switch (subcmd) {
  2884. case 'i':
  2885. addr += spu_inst_dump(addr, 16, 1);
  2886. last_cmd = "sdi\n";
  2887. break;
  2888. default:
  2889. prdump(addr, 64);
  2890. addr += 64;
  2891. last_cmd = "sd\n";
  2892. break;
  2893. }
  2894. spu_info[num].dump_addr = addr;
  2895. }
  2896. static int do_spu_cmd(void)
  2897. {
  2898. static unsigned long num = 0;
  2899. int cmd, subcmd = 0;
  2900. cmd = inchar();
  2901. switch (cmd) {
  2902. case 's':
  2903. stop_spus();
  2904. break;
  2905. case 'r':
  2906. restart_spus();
  2907. break;
  2908. case 'd':
  2909. subcmd = inchar();
  2910. if (isxdigit(subcmd) || subcmd == '\n')
  2911. termch = subcmd;
  2912. case 'f':
  2913. scanhex(&num);
  2914. if (num >= XMON_NUM_SPUS || !spu_info[num].spu) {
  2915. printf("*** Error: invalid spu number\n");
  2916. return 0;
  2917. }
  2918. switch (cmd) {
  2919. case 'f':
  2920. dump_spu_fields(spu_info[num].spu);
  2921. break;
  2922. default:
  2923. dump_spu_ls(num, subcmd);
  2924. break;
  2925. }
  2926. break;
  2927. default:
  2928. return -1;
  2929. }
  2930. return 0;
  2931. }
  2932. #else /* ! CONFIG_SPU_BASE */
  2933. static int do_spu_cmd(void)
  2934. {
  2935. return -1;
  2936. }
  2937. #endif