xmon.c 61 KB

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