xmon.c 62 KB

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