xmon.c 60 KB

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