xmon.c 62 KB

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