xmon.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  1. /*
  2. * Routines providing a simple monitor for use on the PowerMac.
  3. *
  4. * Copyright (C) 1996 Paul Mackerras.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/mm.h>
  16. #include <linux/reboot.h>
  17. #include <linux/delay.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/cpumask.h>
  20. #include <asm/ptrace.h>
  21. #include <asm/string.h>
  22. #include <asm/prom.h>
  23. #include <asm/machdep.h>
  24. #include <asm/processor.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/mmu.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/paca.h>
  29. #include <asm/ppcdebug.h>
  30. #include <asm/cputable.h>
  31. #include <asm/rtas.h>
  32. #include <asm/sstep.h>
  33. #include <asm/bug.h>
  34. #include <asm/hvcall.h>
  35. #include "nonstdio.h"
  36. #include "privinst.h"
  37. #define scanhex xmon_scanhex
  38. #define skipbl xmon_skipbl
  39. #ifdef CONFIG_SMP
  40. cpumask_t cpus_in_xmon = CPU_MASK_NONE;
  41. static unsigned long xmon_taken = 1;
  42. static int xmon_owner;
  43. static int xmon_gate;
  44. #endif /* CONFIG_SMP */
  45. static unsigned long in_xmon = 0;
  46. static unsigned long adrs;
  47. static int size = 1;
  48. #define MAX_DUMP (128 * 1024)
  49. static unsigned long ndump = 64;
  50. static unsigned long nidump = 16;
  51. static unsigned long ncsum = 4096;
  52. static int termch;
  53. static char tmpstr[128];
  54. #define JMP_BUF_LEN (184/sizeof(long))
  55. static long bus_error_jmp[JMP_BUF_LEN];
  56. static int catch_memory_errors;
  57. static long *xmon_fault_jmp[NR_CPUS];
  58. #define setjmp xmon_setjmp
  59. #define longjmp xmon_longjmp
  60. /* Breakpoint stuff */
  61. struct bpt {
  62. unsigned long address;
  63. unsigned int instr[2];
  64. atomic_t ref_count;
  65. int enabled;
  66. unsigned long pad;
  67. };
  68. /* Bits in bpt.enabled */
  69. #define BP_IABR_TE 1 /* IABR translation enabled */
  70. #define BP_IABR 2
  71. #define BP_TRAP 8
  72. #define BP_DABR 0x10
  73. #define NBPTS 256
  74. static struct bpt bpts[NBPTS];
  75. static struct bpt dabr;
  76. static struct bpt *iabr;
  77. static unsigned bpinstr = 0x7fe00008; /* trap */
  78. #define BP_NUM(bp) ((bp) - bpts + 1)
  79. /* Prototypes */
  80. static int cmds(struct pt_regs *);
  81. static int mread(unsigned long, void *, int);
  82. static int mwrite(unsigned long, void *, int);
  83. static int handle_fault(struct pt_regs *);
  84. static void byterev(unsigned char *, int);
  85. static void memex(void);
  86. static int bsesc(void);
  87. static void dump(void);
  88. static void prdump(unsigned long, long);
  89. static int ppc_inst_dump(unsigned long, long, int);
  90. void print_address(unsigned long);
  91. static void backtrace(struct pt_regs *);
  92. static void excprint(struct pt_regs *);
  93. static void prregs(struct pt_regs *);
  94. static void memops(int);
  95. static void memlocate(void);
  96. static void memzcan(void);
  97. static void memdiffs(unsigned char *, unsigned char *, unsigned, unsigned);
  98. int skipbl(void);
  99. int scanhex(unsigned long *valp);
  100. static void scannl(void);
  101. static int hexdigit(int);
  102. void getstring(char *, int);
  103. static void flush_input(void);
  104. static int inchar(void);
  105. static void take_input(char *);
  106. static unsigned long read_spr(int);
  107. static void write_spr(int, unsigned long);
  108. static void super_regs(void);
  109. static void remove_bpts(void);
  110. static void insert_bpts(void);
  111. static void remove_cpu_bpts(void);
  112. static void insert_cpu_bpts(void);
  113. static struct bpt *at_breakpoint(unsigned long pc);
  114. static struct bpt *in_breakpoint_table(unsigned long pc, unsigned long *offp);
  115. static int do_step(struct pt_regs *);
  116. static void bpt_cmds(void);
  117. static void cacheflush(void);
  118. static int cpu_cmd(void);
  119. static void csum(void);
  120. static void bootcmds(void);
  121. void dump_segments(void);
  122. static void symbol_lookup(void);
  123. static void xmon_print_symbol(unsigned long address, const char *mid,
  124. const char *after);
  125. static const char *getvecname(unsigned long vec);
  126. static void debug_trace(void);
  127. extern int print_insn_powerpc(unsigned long, unsigned long, int);
  128. extern void printf(const char *fmt, ...);
  129. extern void xmon_vfprintf(void *f, const char *fmt, va_list ap);
  130. extern int xmon_putc(int c, void *f);
  131. extern int putchar(int ch);
  132. extern int xmon_read_poll(void);
  133. extern int setjmp(long *);
  134. extern void longjmp(long *, int);
  135. extern unsigned long _ASR;
  136. #define GETWORD(v) (((v)[0] << 24) + ((v)[1] << 16) + ((v)[2] << 8) + (v)[3])
  137. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  138. || ('a' <= (c) && (c) <= 'f') \
  139. || ('A' <= (c) && (c) <= 'F'))
  140. #define isalnum(c) (('0' <= (c) && (c) <= '9') \
  141. || ('a' <= (c) && (c) <= 'z') \
  142. || ('A' <= (c) && (c) <= 'Z'))
  143. #define isspace(c) (c == ' ' || c == '\t' || c == 10 || c == 13 || c == 0)
  144. static char *help_string = "\
  145. Commands:\n\
  146. b show breakpoints\n\
  147. bd set data breakpoint\n\
  148. bi set instruction breakpoint\n\
  149. bc clear breakpoint\n"
  150. #ifdef CONFIG_SMP
  151. "\
  152. c print cpus stopped in xmon\n\
  153. c# try to switch to cpu number h (in hex)\n"
  154. #endif
  155. "\
  156. C checksum\n\
  157. d dump bytes\n\
  158. di dump instructions\n\
  159. df dump float values\n\
  160. dd dump double values\n\
  161. e print exception information\n\
  162. f flush cache\n\
  163. la lookup symbol+offset of specified address\n\
  164. ls lookup address of specified symbol\n\
  165. m examine/change memory\n\
  166. mm move a block of memory\n\
  167. ms set a block of memory\n\
  168. md compare two blocks of memory\n\
  169. ml locate a block of memory\n\
  170. mz zero a block of memory\n\
  171. mi show information about memory allocation\n\
  172. p show the task list\n\
  173. r print registers\n\
  174. s single step\n\
  175. S print special registers\n\
  176. t print backtrace\n\
  177. T Enable/Disable PPCDBG flags\n\
  178. x exit monitor and recover\n\
  179. X exit monitor and dont recover\n\
  180. u dump segment table or SLB\n\
  181. ? help\n"
  182. "\
  183. zr reboot\n\
  184. zh halt\n"
  185. ;
  186. static struct pt_regs *xmon_regs;
  187. extern inline void sync(void)
  188. {
  189. asm volatile("sync; isync");
  190. }
  191. /* (Ref: 64-bit PowerPC ELF ABI Spplement; Ian Lance Taylor, Zembu Labs).
  192. A PPC stack frame looks like this:
  193. High Address
  194. Back Chain
  195. FP reg save area
  196. GP reg save area
  197. Local var space
  198. Parameter save area (SP+48)
  199. TOC save area (SP+40)
  200. link editor doubleword (SP+32)
  201. compiler doubleword (SP+24)
  202. LR save (SP+16)
  203. CR save (SP+8)
  204. Back Chain (SP+0)
  205. Note that the LR (ret addr) may not be saved in the current frame if
  206. no functions have been called from the current function.
  207. */
  208. /*
  209. * Disable surveillance (the service processor watchdog function)
  210. * while we are in xmon.
  211. * XXX we should re-enable it when we leave. :)
  212. */
  213. #define SURVEILLANCE_TOKEN 9000
  214. static inline void disable_surveillance(void)
  215. {
  216. #ifdef CONFIG_PPC_PSERIES
  217. /* Since this can't be a module, args should end up below 4GB. */
  218. static struct rtas_args args;
  219. /*
  220. * At this point we have got all the cpus we can into
  221. * xmon, so there is hopefully no other cpu calling RTAS
  222. * at the moment, even though we don't take rtas.lock.
  223. * If we did try to take rtas.lock there would be a
  224. * real possibility of deadlock.
  225. */
  226. args.token = rtas_token("set-indicator");
  227. if (args.token == RTAS_UNKNOWN_SERVICE)
  228. return;
  229. args.nargs = 3;
  230. args.nret = 1;
  231. args.rets = &args.args[3];
  232. args.args[0] = SURVEILLANCE_TOKEN;
  233. args.args[1] = 0;
  234. args.args[2] = 0;
  235. enter_rtas(__pa(&args));
  236. #endif /* CONFIG_PPC_PSERIES */
  237. }
  238. #ifdef CONFIG_SMP
  239. static int xmon_speaker;
  240. static void get_output_lock(void)
  241. {
  242. int me = smp_processor_id() + 0x100;
  243. int last_speaker = 0, prev;
  244. long timeout;
  245. if (xmon_speaker == me)
  246. return;
  247. for (;;) {
  248. if (xmon_speaker == 0) {
  249. last_speaker = cmpxchg(&xmon_speaker, 0, me);
  250. if (last_speaker == 0)
  251. return;
  252. }
  253. timeout = 10000000;
  254. while (xmon_speaker == last_speaker) {
  255. if (--timeout > 0)
  256. continue;
  257. /* hostile takeover */
  258. prev = cmpxchg(&xmon_speaker, last_speaker, me);
  259. if (prev == last_speaker)
  260. return;
  261. break;
  262. }
  263. }
  264. }
  265. static void release_output_lock(void)
  266. {
  267. xmon_speaker = 0;
  268. }
  269. #endif
  270. int xmon_core(struct pt_regs *regs, int fromipi)
  271. {
  272. int cmd = 0;
  273. unsigned long msr;
  274. struct bpt *bp;
  275. long recurse_jmp[JMP_BUF_LEN];
  276. unsigned long offset;
  277. #ifdef CONFIG_SMP
  278. int cpu;
  279. int secondary;
  280. unsigned long timeout;
  281. #endif
  282. msr = get_msr();
  283. set_msrd(msr & ~MSR_EE); /* disable interrupts */
  284. bp = in_breakpoint_table(regs->nip, &offset);
  285. if (bp != NULL) {
  286. regs->nip = bp->address + offset;
  287. atomic_dec(&bp->ref_count);
  288. }
  289. remove_cpu_bpts();
  290. #ifdef CONFIG_SMP
  291. cpu = smp_processor_id();
  292. if (cpu_isset(cpu, cpus_in_xmon)) {
  293. get_output_lock();
  294. excprint(regs);
  295. printf("cpu 0x%x: Exception %lx %s in xmon, "
  296. "returning to main loop\n",
  297. cpu, regs->trap, getvecname(TRAP(regs)));
  298. release_output_lock();
  299. longjmp(xmon_fault_jmp[cpu], 1);
  300. }
  301. if (setjmp(recurse_jmp) != 0) {
  302. if (!in_xmon || !xmon_gate) {
  303. get_output_lock();
  304. printf("xmon: WARNING: bad recursive fault "
  305. "on cpu 0x%x\n", cpu);
  306. release_output_lock();
  307. goto waiting;
  308. }
  309. secondary = !(xmon_taken && cpu == xmon_owner);
  310. goto cmdloop;
  311. }
  312. xmon_fault_jmp[cpu] = recurse_jmp;
  313. cpu_set(cpu, cpus_in_xmon);
  314. bp = NULL;
  315. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF))
  316. bp = at_breakpoint(regs->nip);
  317. if (bp || (regs->msr & MSR_RI) == 0)
  318. fromipi = 0;
  319. if (!fromipi) {
  320. get_output_lock();
  321. excprint(regs);
  322. if (bp) {
  323. printf("cpu 0x%x stopped at breakpoint 0x%x (",
  324. cpu, BP_NUM(bp));
  325. xmon_print_symbol(regs->nip, " ", ")\n");
  326. }
  327. if ((regs->msr & MSR_RI) == 0)
  328. printf("WARNING: exception is not recoverable, "
  329. "can't continue\n");
  330. release_output_lock();
  331. }
  332. waiting:
  333. secondary = 1;
  334. while (secondary && !xmon_gate) {
  335. if (in_xmon == 0) {
  336. if (fromipi)
  337. goto leave;
  338. secondary = test_and_set_bit(0, &in_xmon);
  339. }
  340. barrier();
  341. }
  342. if (!secondary && !xmon_gate) {
  343. /* we are the first cpu to come in */
  344. /* interrupt other cpu(s) */
  345. int ncpus = num_online_cpus();
  346. xmon_owner = cpu;
  347. mb();
  348. if (ncpus > 1) {
  349. smp_send_debugger_break(MSG_ALL_BUT_SELF);
  350. /* wait for other cpus to come in */
  351. for (timeout = 100000000; timeout != 0; --timeout) {
  352. if (cpus_weight(cpus_in_xmon) >= ncpus)
  353. break;
  354. barrier();
  355. }
  356. }
  357. remove_bpts();
  358. disable_surveillance();
  359. /* for breakpoint or single step, print the current instr. */
  360. if (bp || TRAP(regs) == 0xd00)
  361. ppc_inst_dump(regs->nip, 1, 0);
  362. printf("enter ? for help\n");
  363. mb();
  364. xmon_gate = 1;
  365. barrier();
  366. }
  367. cmdloop:
  368. while (in_xmon) {
  369. if (secondary) {
  370. if (cpu == xmon_owner) {
  371. if (!test_and_set_bit(0, &xmon_taken)) {
  372. secondary = 0;
  373. continue;
  374. }
  375. /* missed it */
  376. while (cpu == xmon_owner)
  377. barrier();
  378. }
  379. barrier();
  380. } else {
  381. cmd = cmds(regs);
  382. if (cmd != 0) {
  383. /* exiting xmon */
  384. insert_bpts();
  385. xmon_gate = 0;
  386. wmb();
  387. in_xmon = 0;
  388. break;
  389. }
  390. /* have switched to some other cpu */
  391. secondary = 1;
  392. }
  393. }
  394. leave:
  395. cpu_clear(cpu, cpus_in_xmon);
  396. xmon_fault_jmp[cpu] = NULL;
  397. #else
  398. /* UP is simple... */
  399. if (in_xmon) {
  400. printf("Exception %lx %s in xmon, returning to main loop\n",
  401. regs->trap, getvecname(TRAP(regs)));
  402. longjmp(xmon_fault_jmp[0], 1);
  403. }
  404. if (setjmp(recurse_jmp) == 0) {
  405. xmon_fault_jmp[0] = recurse_jmp;
  406. in_xmon = 1;
  407. excprint(regs);
  408. bp = at_breakpoint(regs->nip);
  409. if (bp) {
  410. printf("Stopped at breakpoint %x (", BP_NUM(bp));
  411. xmon_print_symbol(regs->nip, " ", ")\n");
  412. }
  413. if ((regs->msr & MSR_RI) == 0)
  414. printf("WARNING: exception is not recoverable, "
  415. "can't continue\n");
  416. remove_bpts();
  417. disable_surveillance();
  418. /* for breakpoint or single step, print the current instr. */
  419. if (bp || TRAP(regs) == 0xd00)
  420. ppc_inst_dump(regs->nip, 1, 0);
  421. printf("enter ? for help\n");
  422. }
  423. cmd = cmds(regs);
  424. insert_bpts();
  425. in_xmon = 0;
  426. #endif
  427. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  428. bp = at_breakpoint(regs->nip);
  429. if (bp != NULL) {
  430. int stepped = emulate_step(regs, bp->instr[0]);
  431. if (stepped == 0) {
  432. regs->nip = (unsigned long) &bp->instr[0];
  433. atomic_inc(&bp->ref_count);
  434. } else if (stepped < 0) {
  435. printf("Couldn't single-step %s instruction\n",
  436. (IS_RFID(bp->instr[0])? "rfid": "mtmsrd"));
  437. }
  438. }
  439. }
  440. insert_cpu_bpts();
  441. set_msrd(msr); /* restore interrupt enable */
  442. return cmd != 'X';
  443. }
  444. int xmon(struct pt_regs *excp)
  445. {
  446. struct pt_regs regs;
  447. if (excp == NULL) {
  448. /* Ok, grab regs as they are now.
  449. This won't do a particularily good job because the
  450. prologue has already been executed.
  451. ToDo: We could reach back into the callers save
  452. area to do a better job of representing the
  453. caller's state.
  454. */
  455. asm volatile ("std 0,0(%0)\n\
  456. std 1,8(%0)\n\
  457. std 2,16(%0)\n\
  458. std 3,24(%0)\n\
  459. std 4,32(%0)\n\
  460. std 5,40(%0)\n\
  461. std 6,48(%0)\n\
  462. std 7,56(%0)\n\
  463. std 8,64(%0)\n\
  464. std 9,72(%0)\n\
  465. std 10,80(%0)\n\
  466. std 11,88(%0)\n\
  467. std 12,96(%0)\n\
  468. std 13,104(%0)\n\
  469. std 14,112(%0)\n\
  470. std 15,120(%0)\n\
  471. std 16,128(%0)\n\
  472. std 17,136(%0)\n\
  473. std 18,144(%0)\n\
  474. std 19,152(%0)\n\
  475. std 20,160(%0)\n\
  476. std 21,168(%0)\n\
  477. std 22,176(%0)\n\
  478. std 23,184(%0)\n\
  479. std 24,192(%0)\n\
  480. std 25,200(%0)\n\
  481. std 26,208(%0)\n\
  482. std 27,216(%0)\n\
  483. std 28,224(%0)\n\
  484. std 29,232(%0)\n\
  485. std 30,240(%0)\n\
  486. std 31,248(%0)" : : "b" (&regs));
  487. regs.nip = regs.link = ((unsigned long *)(regs.gpr[1]))[2];
  488. regs.msr = get_msr();
  489. regs.ctr = get_ctr();
  490. regs.xer = get_xer();
  491. regs.ccr = get_cr();
  492. regs.trap = 0;
  493. excp = &regs;
  494. }
  495. return xmon_core(excp, 0);
  496. }
  497. int xmon_bpt(struct pt_regs *regs)
  498. {
  499. struct bpt *bp;
  500. unsigned long offset;
  501. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  502. return 0;
  503. /* Are we at the trap at bp->instr[1] for some bp? */
  504. bp = in_breakpoint_table(regs->nip, &offset);
  505. if (bp != NULL && offset == 4) {
  506. regs->nip = bp->address + 4;
  507. atomic_dec(&bp->ref_count);
  508. return 1;
  509. }
  510. /* Are we at a breakpoint? */
  511. bp = at_breakpoint(regs->nip);
  512. if (!bp)
  513. return 0;
  514. xmon_core(regs, 0);
  515. return 1;
  516. }
  517. int xmon_sstep(struct pt_regs *regs)
  518. {
  519. if (user_mode(regs))
  520. return 0;
  521. xmon_core(regs, 0);
  522. return 1;
  523. }
  524. int xmon_dabr_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 (dabr.enabled == 0)
  529. return 0;
  530. xmon_core(regs, 0);
  531. return 1;
  532. }
  533. int xmon_iabr_match(struct pt_regs *regs)
  534. {
  535. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  536. return 0;
  537. if (iabr == 0)
  538. return 0;
  539. xmon_core(regs, 0);
  540. return 1;
  541. }
  542. int xmon_ipi(struct pt_regs *regs)
  543. {
  544. #ifdef CONFIG_SMP
  545. if (in_xmon && !cpu_isset(smp_processor_id(), cpus_in_xmon))
  546. xmon_core(regs, 1);
  547. #endif
  548. return 0;
  549. }
  550. int xmon_fault_handler(struct pt_regs *regs)
  551. {
  552. struct bpt *bp;
  553. unsigned long offset;
  554. if (in_xmon && catch_memory_errors)
  555. handle_fault(regs); /* doesn't return */
  556. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  557. bp = in_breakpoint_table(regs->nip, &offset);
  558. if (bp != NULL) {
  559. regs->nip = bp->address + offset;
  560. atomic_dec(&bp->ref_count);
  561. }
  562. }
  563. return 0;
  564. }
  565. static struct bpt *at_breakpoint(unsigned long pc)
  566. {
  567. int i;
  568. struct bpt *bp;
  569. bp = bpts;
  570. for (i = 0; i < NBPTS; ++i, ++bp)
  571. if (bp->enabled && pc == bp->address)
  572. return bp;
  573. return NULL;
  574. }
  575. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  576. {
  577. unsigned long off;
  578. off = nip - (unsigned long) bpts;
  579. if (off >= sizeof(bpts))
  580. return NULL;
  581. off %= sizeof(struct bpt);
  582. if (off != offsetof(struct bpt, instr[0])
  583. && off != offsetof(struct bpt, instr[1]))
  584. return NULL;
  585. *offp = off - offsetof(struct bpt, instr[0]);
  586. return (struct bpt *) (nip - off);
  587. }
  588. static struct bpt *new_breakpoint(unsigned long a)
  589. {
  590. struct bpt *bp;
  591. a &= ~3UL;
  592. bp = at_breakpoint(a);
  593. if (bp)
  594. return bp;
  595. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  596. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  597. bp->address = a;
  598. bp->instr[1] = bpinstr;
  599. store_inst(&bp->instr[1]);
  600. return bp;
  601. }
  602. }
  603. printf("Sorry, no free breakpoints. Please clear one first.\n");
  604. return NULL;
  605. }
  606. static void insert_bpts(void)
  607. {
  608. int i;
  609. struct bpt *bp;
  610. bp = bpts;
  611. for (i = 0; i < NBPTS; ++i, ++bp) {
  612. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  613. continue;
  614. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  615. printf("Couldn't read instruction at %lx, "
  616. "disabling breakpoint there\n", bp->address);
  617. bp->enabled = 0;
  618. continue;
  619. }
  620. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  621. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  622. "instruction, disabling it\n", bp->address);
  623. bp->enabled = 0;
  624. continue;
  625. }
  626. store_inst(&bp->instr[0]);
  627. if (bp->enabled & BP_IABR)
  628. continue;
  629. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  630. printf("Couldn't write instruction at %lx, "
  631. "disabling breakpoint there\n", bp->address);
  632. bp->enabled &= ~BP_TRAP;
  633. continue;
  634. }
  635. store_inst((void *)bp->address);
  636. }
  637. }
  638. static void insert_cpu_bpts(void)
  639. {
  640. if (dabr.enabled)
  641. set_dabr(dabr.address | (dabr.enabled & 7));
  642. if (iabr && cpu_has_feature(CPU_FTR_IABR))
  643. set_iabr(iabr->address
  644. | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
  645. }
  646. static void remove_bpts(void)
  647. {
  648. int i;
  649. struct bpt *bp;
  650. unsigned instr;
  651. bp = bpts;
  652. for (i = 0; i < NBPTS; ++i, ++bp) {
  653. if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
  654. continue;
  655. if (mread(bp->address, &instr, 4) == 4
  656. && instr == bpinstr
  657. && mwrite(bp->address, &bp->instr, 4) != 4)
  658. printf("Couldn't remove breakpoint at %lx\n",
  659. bp->address);
  660. else
  661. store_inst((void *)bp->address);
  662. }
  663. }
  664. static void remove_cpu_bpts(void)
  665. {
  666. set_dabr(0);
  667. if (cpu_has_feature(CPU_FTR_IABR))
  668. set_iabr(0);
  669. }
  670. /* Command interpreting routine */
  671. static char *last_cmd;
  672. static int
  673. cmds(struct pt_regs *excp)
  674. {
  675. int cmd = 0;
  676. last_cmd = NULL;
  677. xmon_regs = excp;
  678. for(;;) {
  679. #ifdef CONFIG_SMP
  680. printf("%x:", smp_processor_id());
  681. #endif /* CONFIG_SMP */
  682. printf("mon> ");
  683. fflush(stdout);
  684. flush_input();
  685. termch = 0;
  686. cmd = skipbl();
  687. if( cmd == '\n' ) {
  688. if (last_cmd == NULL)
  689. continue;
  690. take_input(last_cmd);
  691. last_cmd = NULL;
  692. cmd = inchar();
  693. }
  694. switch (cmd) {
  695. case 'm':
  696. cmd = inchar();
  697. switch (cmd) {
  698. case 'm':
  699. case 's':
  700. case 'd':
  701. memops(cmd);
  702. break;
  703. case 'l':
  704. memlocate();
  705. break;
  706. case 'z':
  707. memzcan();
  708. break;
  709. case 'i':
  710. show_mem();
  711. break;
  712. default:
  713. termch = cmd;
  714. memex();
  715. }
  716. break;
  717. case 'd':
  718. dump();
  719. break;
  720. case 'l':
  721. symbol_lookup();
  722. break;
  723. case 'r':
  724. prregs(excp); /* print regs */
  725. break;
  726. case 'e':
  727. excprint(excp);
  728. break;
  729. case 'S':
  730. super_regs();
  731. break;
  732. case 't':
  733. backtrace(excp);
  734. break;
  735. case 'f':
  736. cacheflush();
  737. break;
  738. case 's':
  739. if (do_step(excp))
  740. return cmd;
  741. break;
  742. case 'x':
  743. case 'X':
  744. case EOF:
  745. return cmd;
  746. case '?':
  747. printf(help_string);
  748. break;
  749. case 'p':
  750. show_state();
  751. break;
  752. case 'b':
  753. bpt_cmds();
  754. break;
  755. case 'C':
  756. csum();
  757. break;
  758. case 'c':
  759. if (cpu_cmd())
  760. return 0;
  761. break;
  762. case 'z':
  763. bootcmds();
  764. break;
  765. case 'T':
  766. debug_trace();
  767. break;
  768. case 'u':
  769. dump_segments();
  770. break;
  771. default:
  772. printf("Unrecognized command: ");
  773. do {
  774. if (' ' < cmd && cmd <= '~')
  775. putchar(cmd);
  776. else
  777. printf("\\x%x", cmd);
  778. cmd = inchar();
  779. } while (cmd != '\n');
  780. printf(" (type ? for help)\n");
  781. break;
  782. }
  783. }
  784. }
  785. /*
  786. * Step a single instruction.
  787. * Some instructions we emulate, others we execute with MSR_SE set.
  788. */
  789. static int do_step(struct pt_regs *regs)
  790. {
  791. unsigned int instr;
  792. int stepped;
  793. /* check we are in 64-bit kernel mode, translation enabled */
  794. if ((regs->msr & (MSR_SF|MSR_PR|MSR_IR)) == (MSR_SF|MSR_IR)) {
  795. if (mread(regs->nip, &instr, 4) == 4) {
  796. stepped = emulate_step(regs, instr);
  797. if (stepped < 0) {
  798. printf("Couldn't single-step %s instruction\n",
  799. (IS_RFID(instr)? "rfid": "mtmsrd"));
  800. return 0;
  801. }
  802. if (stepped > 0) {
  803. regs->trap = 0xd00 | (regs->trap & 1);
  804. printf("stepped to ");
  805. xmon_print_symbol(regs->nip, " ", "\n");
  806. ppc_inst_dump(regs->nip, 1, 0);
  807. return 0;
  808. }
  809. }
  810. }
  811. regs->msr |= MSR_SE;
  812. return 1;
  813. }
  814. static void bootcmds(void)
  815. {
  816. int cmd;
  817. cmd = inchar();
  818. if (cmd == 'r')
  819. ppc_md.restart(NULL);
  820. else if (cmd == 'h')
  821. ppc_md.halt();
  822. else if (cmd == 'p')
  823. ppc_md.power_off();
  824. }
  825. static int cpu_cmd(void)
  826. {
  827. #ifdef CONFIG_SMP
  828. unsigned long cpu;
  829. int timeout;
  830. int count;
  831. if (!scanhex(&cpu)) {
  832. /* print cpus waiting or in xmon */
  833. printf("cpus stopped:");
  834. count = 0;
  835. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  836. if (cpu_isset(cpu, cpus_in_xmon)) {
  837. if (count == 0)
  838. printf(" %x", cpu);
  839. ++count;
  840. } else {
  841. if (count > 1)
  842. printf("-%x", cpu - 1);
  843. count = 0;
  844. }
  845. }
  846. if (count > 1)
  847. printf("-%x", NR_CPUS - 1);
  848. printf("\n");
  849. return 0;
  850. }
  851. /* try to switch to cpu specified */
  852. if (!cpu_isset(cpu, cpus_in_xmon)) {
  853. printf("cpu 0x%x isn't in xmon\n", cpu);
  854. return 0;
  855. }
  856. xmon_taken = 0;
  857. mb();
  858. xmon_owner = cpu;
  859. timeout = 10000000;
  860. while (!xmon_taken) {
  861. if (--timeout == 0) {
  862. if (test_and_set_bit(0, &xmon_taken))
  863. break;
  864. /* take control back */
  865. mb();
  866. xmon_owner = smp_processor_id();
  867. printf("cpu %u didn't take control\n", cpu);
  868. return 0;
  869. }
  870. barrier();
  871. }
  872. return 1;
  873. #else
  874. return 0;
  875. #endif /* CONFIG_SMP */
  876. }
  877. static unsigned short fcstab[256] = {
  878. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  879. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  880. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  881. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  882. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  883. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  884. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  885. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  886. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  887. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  888. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  889. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  890. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  891. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  892. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  893. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  894. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  895. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  896. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  897. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  898. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  899. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  900. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  901. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  902. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  903. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  904. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  905. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  906. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  907. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  908. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  909. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  910. };
  911. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  912. static void
  913. csum(void)
  914. {
  915. unsigned int i;
  916. unsigned short fcs;
  917. unsigned char v;
  918. if (!scanhex(&adrs))
  919. return;
  920. if (!scanhex(&ncsum))
  921. return;
  922. fcs = 0xffff;
  923. for (i = 0; i < ncsum; ++i) {
  924. if (mread(adrs+i, &v, 1) == 0) {
  925. printf("csum stopped at %x\n", adrs+i);
  926. break;
  927. }
  928. fcs = FCS(fcs, v);
  929. }
  930. printf("%x\n", fcs);
  931. }
  932. /*
  933. * Check if this is a suitable place to put a breakpoint.
  934. */
  935. static long check_bp_loc(unsigned long addr)
  936. {
  937. unsigned int instr;
  938. addr &= ~3;
  939. if (addr < KERNELBASE) {
  940. printf("Breakpoints may only be placed at kernel addresses\n");
  941. return 0;
  942. }
  943. if (!mread(addr, &instr, sizeof(instr))) {
  944. printf("Can't read instruction at address %lx\n", addr);
  945. return 0;
  946. }
  947. if (IS_MTMSRD(instr) || IS_RFID(instr)) {
  948. printf("Breakpoints may not be placed on mtmsrd or rfid "
  949. "instructions\n");
  950. return 0;
  951. }
  952. return 1;
  953. }
  954. static char *breakpoint_help_string =
  955. "Breakpoint command usage:\n"
  956. "b show breakpoints\n"
  957. "b <addr> [cnt] set breakpoint at given instr addr\n"
  958. "bc clear all breakpoints\n"
  959. "bc <n/addr> clear breakpoint number n or at addr\n"
  960. "bi <addr> [cnt] set hardware instr breakpoint (POWER3/RS64 only)\n"
  961. "bd <addr> [cnt] set hardware data breakpoint\n"
  962. "";
  963. static void
  964. bpt_cmds(void)
  965. {
  966. int cmd;
  967. unsigned long a;
  968. int mode, i;
  969. struct bpt *bp;
  970. const char badaddr[] = "Only kernel addresses are permitted "
  971. "for breakpoints\n";
  972. cmd = inchar();
  973. switch (cmd) {
  974. case 'd': /* bd - hardware data breakpoint */
  975. mode = 7;
  976. cmd = inchar();
  977. if (cmd == 'r')
  978. mode = 5;
  979. else if (cmd == 'w')
  980. mode = 6;
  981. else
  982. termch = cmd;
  983. dabr.address = 0;
  984. dabr.enabled = 0;
  985. if (scanhex(&dabr.address)) {
  986. if (dabr.address < KERNELBASE) {
  987. printf(badaddr);
  988. break;
  989. }
  990. dabr.address &= ~7;
  991. dabr.enabled = mode | BP_DABR;
  992. }
  993. break;
  994. case 'i': /* bi - hardware instr breakpoint */
  995. if (!cpu_has_feature(CPU_FTR_IABR)) {
  996. printf("Hardware instruction breakpoint "
  997. "not supported on this cpu\n");
  998. break;
  999. }
  1000. if (iabr) {
  1001. iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
  1002. iabr = NULL;
  1003. }
  1004. if (!scanhex(&a))
  1005. break;
  1006. if (!check_bp_loc(a))
  1007. break;
  1008. bp = new_breakpoint(a);
  1009. if (bp != NULL) {
  1010. bp->enabled |= BP_IABR | BP_IABR_TE;
  1011. iabr = bp;
  1012. }
  1013. break;
  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 %.16lx [", 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. static void xmon_show_stack(unsigned long sp, unsigned long lr,
  1124. unsigned long pc)
  1125. {
  1126. unsigned long ip;
  1127. unsigned long newsp;
  1128. unsigned long marker;
  1129. int count = 0;
  1130. struct pt_regs regs;
  1131. do {
  1132. if (sp < PAGE_OFFSET) {
  1133. if (sp != 0)
  1134. printf("SP (%lx) is in userspace\n", sp);
  1135. break;
  1136. }
  1137. if (!mread(sp + 16, &ip, sizeof(unsigned long))
  1138. || !mread(sp, &newsp, sizeof(unsigned long))) {
  1139. printf("Couldn't read stack frame at %lx\n", sp);
  1140. break;
  1141. }
  1142. /*
  1143. * For the first stack frame, try to work out if
  1144. * LR and/or the saved LR value in the bottommost
  1145. * stack frame are valid.
  1146. */
  1147. if ((pc | lr) != 0) {
  1148. unsigned long fnstart, fnend;
  1149. unsigned long nextip;
  1150. int printip = 1;
  1151. get_function_bounds(pc, &fnstart, &fnend);
  1152. nextip = 0;
  1153. if (newsp > sp)
  1154. mread(newsp + 16, &nextip,
  1155. sizeof(unsigned long));
  1156. if (lr == ip) {
  1157. if (lr < PAGE_OFFSET
  1158. || (fnstart <= lr && lr < fnend))
  1159. printip = 0;
  1160. } else if (lr == nextip) {
  1161. printip = 0;
  1162. } else if (lr >= PAGE_OFFSET
  1163. && !(fnstart <= lr && lr < fnend)) {
  1164. printf("[link register ] ");
  1165. xmon_print_symbol(lr, " ", "\n");
  1166. }
  1167. if (printip) {
  1168. printf("[%.16lx] ", sp);
  1169. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1170. }
  1171. pc = lr = 0;
  1172. } else {
  1173. printf("[%.16lx] ", sp);
  1174. xmon_print_symbol(ip, " ", "\n");
  1175. }
  1176. /* Look for "regshere" marker to see if this is
  1177. an exception frame. */
  1178. if (mread(sp + 0x60, &marker, sizeof(unsigned long))
  1179. && marker == 0x7265677368657265ul) {
  1180. if (mread(sp + 0x70, &regs, sizeof(regs))
  1181. != sizeof(regs)) {
  1182. printf("Couldn't read registers at %lx\n",
  1183. sp + 0x70);
  1184. break;
  1185. }
  1186. printf("--- Exception: %lx %s at ", regs.trap,
  1187. getvecname(TRAP(&regs)));
  1188. pc = regs.nip;
  1189. lr = regs.link;
  1190. xmon_print_symbol(pc, " ", "\n");
  1191. }
  1192. if (newsp == 0)
  1193. break;
  1194. sp = newsp;
  1195. } while (count++ < xmon_depth_to_print);
  1196. }
  1197. static void backtrace(struct pt_regs *excp)
  1198. {
  1199. unsigned long sp;
  1200. if (scanhex(&sp))
  1201. xmon_show_stack(sp, 0, 0);
  1202. else
  1203. xmon_show_stack(excp->gpr[1], excp->link, excp->nip);
  1204. scannl();
  1205. }
  1206. static void print_bug_trap(struct pt_regs *regs)
  1207. {
  1208. struct bug_entry *bug;
  1209. unsigned long addr;
  1210. if (regs->msr & MSR_PR)
  1211. return; /* not in kernel */
  1212. addr = regs->nip; /* address of trap instruction */
  1213. if (addr < PAGE_OFFSET)
  1214. return;
  1215. bug = find_bug(regs->nip);
  1216. if (bug == NULL)
  1217. return;
  1218. if (bug->line & BUG_WARNING_TRAP)
  1219. return;
  1220. printf("kernel BUG in %s at %s:%d!\n",
  1221. bug->function, bug->file, (unsigned int)bug->line);
  1222. }
  1223. void excprint(struct pt_regs *fp)
  1224. {
  1225. unsigned long trap;
  1226. #ifdef CONFIG_SMP
  1227. printf("cpu 0x%x: ", smp_processor_id());
  1228. #endif /* CONFIG_SMP */
  1229. trap = TRAP(fp);
  1230. printf("Vector: %lx %s at [%lx]\n", fp->trap, getvecname(trap), fp);
  1231. printf(" pc: ");
  1232. xmon_print_symbol(fp->nip, ": ", "\n");
  1233. printf(" lr: ", fp->link);
  1234. xmon_print_symbol(fp->link, ": ", "\n");
  1235. printf(" sp: %lx\n", fp->gpr[1]);
  1236. printf(" msr: %lx\n", fp->msr);
  1237. if (trap == 0x300 || trap == 0x380 || trap == 0x600) {
  1238. printf(" dar: %lx\n", fp->dar);
  1239. if (trap != 0x380)
  1240. printf(" dsisr: %lx\n", fp->dsisr);
  1241. }
  1242. printf(" current = 0x%lx\n", current);
  1243. printf(" paca = 0x%lx\n", get_paca());
  1244. if (current) {
  1245. printf(" pid = %ld, comm = %s\n",
  1246. current->pid, current->comm);
  1247. }
  1248. if (trap == 0x700)
  1249. print_bug_trap(fp);
  1250. }
  1251. void prregs(struct pt_regs *fp)
  1252. {
  1253. int n;
  1254. unsigned long base;
  1255. struct pt_regs regs;
  1256. if (scanhex(&base)) {
  1257. if (setjmp(bus_error_jmp) == 0) {
  1258. catch_memory_errors = 1;
  1259. sync();
  1260. regs = *(struct pt_regs *)base;
  1261. sync();
  1262. __delay(200);
  1263. } else {
  1264. catch_memory_errors = 0;
  1265. printf("*** Error reading registers from %.16lx\n",
  1266. base);
  1267. return;
  1268. }
  1269. catch_memory_errors = 0;
  1270. fp = &regs;
  1271. }
  1272. if (FULL_REGS(fp)) {
  1273. for (n = 0; n < 16; ++n)
  1274. printf("R%.2ld = %.16lx R%.2ld = %.16lx\n",
  1275. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1276. } else {
  1277. for (n = 0; n < 7; ++n)
  1278. printf("R%.2ld = %.16lx R%.2ld = %.16lx\n",
  1279. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1280. }
  1281. printf("pc = ");
  1282. xmon_print_symbol(fp->nip, " ", "\n");
  1283. printf("lr = ");
  1284. xmon_print_symbol(fp->link, " ", "\n");
  1285. printf("msr = %.16lx cr = %.8lx\n", fp->msr, fp->ccr);
  1286. printf("ctr = %.16lx xer = %.16lx trap = %8lx\n",
  1287. fp->ctr, fp->xer, fp->trap);
  1288. }
  1289. void cacheflush(void)
  1290. {
  1291. int cmd;
  1292. unsigned long nflush;
  1293. cmd = inchar();
  1294. if (cmd != 'i')
  1295. termch = cmd;
  1296. scanhex((void *)&adrs);
  1297. if (termch != '\n')
  1298. termch = 0;
  1299. nflush = 1;
  1300. scanhex(&nflush);
  1301. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1302. if (setjmp(bus_error_jmp) == 0) {
  1303. catch_memory_errors = 1;
  1304. sync();
  1305. if (cmd != 'i') {
  1306. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1307. cflush((void *) adrs);
  1308. } else {
  1309. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1310. cinval((void *) adrs);
  1311. }
  1312. sync();
  1313. /* wait a little while to see if we get a machine check */
  1314. __delay(200);
  1315. }
  1316. catch_memory_errors = 0;
  1317. }
  1318. unsigned long
  1319. read_spr(int n)
  1320. {
  1321. unsigned int instrs[2];
  1322. unsigned long (*code)(void);
  1323. unsigned long opd[3];
  1324. unsigned long ret = -1UL;
  1325. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1326. instrs[1] = 0x4e800020;
  1327. opd[0] = (unsigned long)instrs;
  1328. opd[1] = 0;
  1329. opd[2] = 0;
  1330. store_inst(instrs);
  1331. store_inst(instrs+1);
  1332. code = (unsigned long (*)(void)) opd;
  1333. if (setjmp(bus_error_jmp) == 0) {
  1334. catch_memory_errors = 1;
  1335. sync();
  1336. ret = code();
  1337. sync();
  1338. /* wait a little while to see if we get a machine check */
  1339. __delay(200);
  1340. n = size;
  1341. }
  1342. return ret;
  1343. }
  1344. void
  1345. write_spr(int n, unsigned long val)
  1346. {
  1347. unsigned int instrs[2];
  1348. unsigned long (*code)(unsigned long);
  1349. unsigned long opd[3];
  1350. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1351. instrs[1] = 0x4e800020;
  1352. opd[0] = (unsigned long)instrs;
  1353. opd[1] = 0;
  1354. opd[2] = 0;
  1355. store_inst(instrs);
  1356. store_inst(instrs+1);
  1357. code = (unsigned long (*)(unsigned long)) opd;
  1358. if (setjmp(bus_error_jmp) == 0) {
  1359. catch_memory_errors = 1;
  1360. sync();
  1361. code(val);
  1362. sync();
  1363. /* wait a little while to see if we get a machine check */
  1364. __delay(200);
  1365. n = size;
  1366. }
  1367. }
  1368. static unsigned long regno;
  1369. extern char exc_prolog;
  1370. extern char dec_exc;
  1371. void
  1372. super_regs(void)
  1373. {
  1374. int cmd;
  1375. unsigned long val;
  1376. #ifdef CONFIG_PPC_ISERIES
  1377. struct paca_struct *ptrPaca = NULL;
  1378. struct lppaca *ptrLpPaca = NULL;
  1379. struct ItLpRegSave *ptrLpRegSave = NULL;
  1380. #endif
  1381. cmd = skipbl();
  1382. if (cmd == '\n') {
  1383. unsigned long sp, toc;
  1384. asm("mr %0,1" : "=r" (sp) :);
  1385. asm("mr %0,2" : "=r" (toc) :);
  1386. printf("msr = %.16lx sprg0= %.16lx\n", get_msr(), get_sprg0());
  1387. printf("pvr = %.16lx sprg1= %.16lx\n", get_pvr(), get_sprg1());
  1388. printf("dec = %.16lx sprg2= %.16lx\n", get_dec(), get_sprg2());
  1389. printf("sp = %.16lx sprg3= %.16lx\n", sp, get_sprg3());
  1390. printf("toc = %.16lx dar = %.16lx\n", toc, get_dar());
  1391. printf("srr0 = %.16lx srr1 = %.16lx\n", get_srr0(), get_srr1());
  1392. #ifdef CONFIG_PPC_ISERIES
  1393. // Dump out relevant Paca data areas.
  1394. printf("Paca: \n");
  1395. ptrPaca = get_paca();
  1396. printf(" Local Processor Control Area (LpPaca): \n");
  1397. ptrLpPaca = ptrPaca->lppaca_ptr;
  1398. printf(" Saved Srr0=%.16lx Saved Srr1=%.16lx \n",
  1399. ptrLpPaca->saved_srr0, ptrLpPaca->saved_srr1);
  1400. printf(" Saved Gpr3=%.16lx Saved Gpr4=%.16lx \n",
  1401. ptrLpPaca->saved_gpr3, ptrLpPaca->saved_gpr4);
  1402. printf(" Saved Gpr5=%.16lx \n", ptrLpPaca->saved_gpr5);
  1403. printf(" Local Processor Register Save Area (LpRegSave): \n");
  1404. ptrLpRegSave = ptrPaca->reg_save_ptr;
  1405. printf(" Saved Sprg0=%.16lx Saved Sprg1=%.16lx \n",
  1406. ptrLpRegSave->xSPRG0, ptrLpRegSave->xSPRG0);
  1407. printf(" Saved Sprg2=%.16lx Saved Sprg3=%.16lx \n",
  1408. ptrLpRegSave->xSPRG2, ptrLpRegSave->xSPRG3);
  1409. printf(" Saved Msr =%.16lx Saved Nia =%.16lx \n",
  1410. ptrLpRegSave->xMSR, ptrLpRegSave->xNIA);
  1411. #endif
  1412. return;
  1413. }
  1414. scanhex(&regno);
  1415. switch (cmd) {
  1416. case 'w':
  1417. val = read_spr(regno);
  1418. scanhex(&val);
  1419. write_spr(regno, val);
  1420. /* fall through */
  1421. case 'r':
  1422. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1423. break;
  1424. case 'm':
  1425. val = get_msr();
  1426. scanhex(&val);
  1427. set_msrd(val);
  1428. break;
  1429. }
  1430. scannl();
  1431. }
  1432. /*
  1433. * Stuff for reading and writing memory safely
  1434. */
  1435. int
  1436. mread(unsigned long adrs, void *buf, int size)
  1437. {
  1438. volatile int n;
  1439. char *p, *q;
  1440. n = 0;
  1441. if (setjmp(bus_error_jmp) == 0) {
  1442. catch_memory_errors = 1;
  1443. sync();
  1444. p = (char *)adrs;
  1445. q = (char *)buf;
  1446. switch (size) {
  1447. case 2:
  1448. *(short *)q = *(short *)p;
  1449. break;
  1450. case 4:
  1451. *(int *)q = *(int *)p;
  1452. break;
  1453. case 8:
  1454. *(long *)q = *(long *)p;
  1455. break;
  1456. default:
  1457. for( ; n < size; ++n) {
  1458. *q++ = *p++;
  1459. sync();
  1460. }
  1461. }
  1462. sync();
  1463. /* wait a little while to see if we get a machine check */
  1464. __delay(200);
  1465. n = size;
  1466. }
  1467. catch_memory_errors = 0;
  1468. return n;
  1469. }
  1470. int
  1471. mwrite(unsigned long adrs, void *buf, int size)
  1472. {
  1473. volatile int n;
  1474. char *p, *q;
  1475. n = 0;
  1476. if (setjmp(bus_error_jmp) == 0) {
  1477. catch_memory_errors = 1;
  1478. sync();
  1479. p = (char *) adrs;
  1480. q = (char *) buf;
  1481. switch (size) {
  1482. case 2:
  1483. *(short *)p = *(short *)q;
  1484. break;
  1485. case 4:
  1486. *(int *)p = *(int *)q;
  1487. break;
  1488. case 8:
  1489. *(long *)p = *(long *)q;
  1490. break;
  1491. default:
  1492. for ( ; n < size; ++n) {
  1493. *p++ = *q++;
  1494. sync();
  1495. }
  1496. }
  1497. sync();
  1498. /* wait a little while to see if we get a machine check */
  1499. __delay(200);
  1500. n = size;
  1501. } else {
  1502. printf("*** Error writing address %x\n", adrs + n);
  1503. }
  1504. catch_memory_errors = 0;
  1505. return n;
  1506. }
  1507. static int fault_type;
  1508. static char *fault_chars[] = { "--", "**", "##" };
  1509. static int
  1510. handle_fault(struct pt_regs *regs)
  1511. {
  1512. switch (TRAP(regs)) {
  1513. case 0x200:
  1514. fault_type = 0;
  1515. break;
  1516. case 0x300:
  1517. case 0x380:
  1518. fault_type = 1;
  1519. break;
  1520. default:
  1521. fault_type = 2;
  1522. }
  1523. longjmp(bus_error_jmp, 1);
  1524. return 0;
  1525. }
  1526. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1527. void
  1528. byterev(unsigned char *val, int size)
  1529. {
  1530. int t;
  1531. switch (size) {
  1532. case 2:
  1533. SWAP(val[0], val[1], t);
  1534. break;
  1535. case 4:
  1536. SWAP(val[0], val[3], t);
  1537. SWAP(val[1], val[2], t);
  1538. break;
  1539. case 8: /* is there really any use for this? */
  1540. SWAP(val[0], val[7], t);
  1541. SWAP(val[1], val[6], t);
  1542. SWAP(val[2], val[5], t);
  1543. SWAP(val[3], val[4], t);
  1544. break;
  1545. }
  1546. }
  1547. static int brev;
  1548. static int mnoread;
  1549. static char *memex_help_string =
  1550. "Memory examine command usage:\n"
  1551. "m [addr] [flags] examine/change memory\n"
  1552. " addr is optional. will start where left off.\n"
  1553. " flags may include chars from this set:\n"
  1554. " b modify by bytes (default)\n"
  1555. " w modify by words (2 byte)\n"
  1556. " l modify by longs (4 byte)\n"
  1557. " d modify by doubleword (8 byte)\n"
  1558. " r toggle reverse byte order mode\n"
  1559. " n do not read memory (for i/o spaces)\n"
  1560. " . ok to read (default)\n"
  1561. "NOTE: flags are saved as defaults\n"
  1562. "";
  1563. static char *memex_subcmd_help_string =
  1564. "Memory examine subcommands:\n"
  1565. " hexval write this val to current location\n"
  1566. " 'string' write chars from string to this location\n"
  1567. " ' increment address\n"
  1568. " ^ decrement address\n"
  1569. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1570. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1571. " ` clear no-read flag\n"
  1572. " ; stay at this addr\n"
  1573. " v change to byte mode\n"
  1574. " w change to word (2 byte) mode\n"
  1575. " l change to long (4 byte) mode\n"
  1576. " u change to doubleword (8 byte) mode\n"
  1577. " m addr change current addr\n"
  1578. " n toggle no-read flag\n"
  1579. " r toggle byte reverse flag\n"
  1580. " < count back up count bytes\n"
  1581. " > count skip forward count bytes\n"
  1582. " x exit this mode\n"
  1583. "";
  1584. void
  1585. memex(void)
  1586. {
  1587. int cmd, inc, i, nslash;
  1588. unsigned long n;
  1589. unsigned char val[16];
  1590. scanhex((void *)&adrs);
  1591. cmd = skipbl();
  1592. if (cmd == '?') {
  1593. printf(memex_help_string);
  1594. return;
  1595. } else {
  1596. termch = cmd;
  1597. }
  1598. last_cmd = "m\n";
  1599. while ((cmd = skipbl()) != '\n') {
  1600. switch( cmd ){
  1601. case 'b': size = 1; break;
  1602. case 'w': size = 2; break;
  1603. case 'l': size = 4; break;
  1604. case 'd': size = 8; break;
  1605. case 'r': brev = !brev; break;
  1606. case 'n': mnoread = 1; break;
  1607. case '.': mnoread = 0; break;
  1608. }
  1609. }
  1610. if( size <= 0 )
  1611. size = 1;
  1612. else if( size > 8 )
  1613. size = 8;
  1614. for(;;){
  1615. if (!mnoread)
  1616. n = mread(adrs, val, size);
  1617. printf("%.16x%c", adrs, brev? 'r': ' ');
  1618. if (!mnoread) {
  1619. if (brev)
  1620. byterev(val, size);
  1621. putchar(' ');
  1622. for (i = 0; i < n; ++i)
  1623. printf("%.2x", val[i]);
  1624. for (; i < size; ++i)
  1625. printf("%s", fault_chars[fault_type]);
  1626. }
  1627. putchar(' ');
  1628. inc = size;
  1629. nslash = 0;
  1630. for(;;){
  1631. if( scanhex(&n) ){
  1632. for (i = 0; i < size; ++i)
  1633. val[i] = n >> (i * 8);
  1634. if (!brev)
  1635. byterev(val, size);
  1636. mwrite(adrs, val, size);
  1637. inc = size;
  1638. }
  1639. cmd = skipbl();
  1640. if (cmd == '\n')
  1641. break;
  1642. inc = 0;
  1643. switch (cmd) {
  1644. case '\'':
  1645. for(;;){
  1646. n = inchar();
  1647. if( n == '\\' )
  1648. n = bsesc();
  1649. else if( n == '\'' )
  1650. break;
  1651. for (i = 0; i < size; ++i)
  1652. val[i] = n >> (i * 8);
  1653. if (!brev)
  1654. byterev(val, size);
  1655. mwrite(adrs, val, size);
  1656. adrs += size;
  1657. }
  1658. adrs -= size;
  1659. inc = size;
  1660. break;
  1661. case ',':
  1662. adrs += size;
  1663. break;
  1664. case '.':
  1665. mnoread = 0;
  1666. break;
  1667. case ';':
  1668. break;
  1669. case 'x':
  1670. case EOF:
  1671. scannl();
  1672. return;
  1673. case 'b':
  1674. case 'v':
  1675. size = 1;
  1676. break;
  1677. case 'w':
  1678. size = 2;
  1679. break;
  1680. case 'l':
  1681. size = 4;
  1682. break;
  1683. case 'u':
  1684. size = 8;
  1685. break;
  1686. case '^':
  1687. adrs -= size;
  1688. break;
  1689. break;
  1690. case '/':
  1691. if (nslash > 0)
  1692. adrs -= 1 << nslash;
  1693. else
  1694. nslash = 0;
  1695. nslash += 4;
  1696. adrs += 1 << nslash;
  1697. break;
  1698. case '\\':
  1699. if (nslash < 0)
  1700. adrs += 1 << -nslash;
  1701. else
  1702. nslash = 0;
  1703. nslash -= 4;
  1704. adrs -= 1 << -nslash;
  1705. break;
  1706. case 'm':
  1707. scanhex((void *)&adrs);
  1708. break;
  1709. case 'n':
  1710. mnoread = 1;
  1711. break;
  1712. case 'r':
  1713. brev = !brev;
  1714. break;
  1715. case '<':
  1716. n = size;
  1717. scanhex(&n);
  1718. adrs -= n;
  1719. break;
  1720. case '>':
  1721. n = size;
  1722. scanhex(&n);
  1723. adrs += n;
  1724. break;
  1725. case '?':
  1726. printf(memex_subcmd_help_string);
  1727. break;
  1728. }
  1729. }
  1730. adrs += inc;
  1731. }
  1732. }
  1733. int
  1734. bsesc(void)
  1735. {
  1736. int c;
  1737. c = inchar();
  1738. switch( c ){
  1739. case 'n': c = '\n'; break;
  1740. case 'r': c = '\r'; break;
  1741. case 'b': c = '\b'; break;
  1742. case 't': c = '\t'; break;
  1743. }
  1744. return c;
  1745. }
  1746. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1747. || ('a' <= (c) && (c) <= 'f') \
  1748. || ('A' <= (c) && (c) <= 'F'))
  1749. void
  1750. dump(void)
  1751. {
  1752. int c;
  1753. c = inchar();
  1754. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1755. termch = c;
  1756. scanhex((void *)&adrs);
  1757. if (termch != '\n')
  1758. termch = 0;
  1759. if (c == 'i') {
  1760. scanhex(&nidump);
  1761. if (nidump == 0)
  1762. nidump = 16;
  1763. else if (nidump > MAX_DUMP)
  1764. nidump = MAX_DUMP;
  1765. adrs += ppc_inst_dump(adrs, nidump, 1);
  1766. last_cmd = "di\n";
  1767. } else {
  1768. scanhex(&ndump);
  1769. if (ndump == 0)
  1770. ndump = 64;
  1771. else if (ndump > MAX_DUMP)
  1772. ndump = MAX_DUMP;
  1773. prdump(adrs, ndump);
  1774. adrs += ndump;
  1775. last_cmd = "d\n";
  1776. }
  1777. }
  1778. void
  1779. prdump(unsigned long adrs, long ndump)
  1780. {
  1781. long n, m, c, r, nr;
  1782. unsigned char temp[16];
  1783. for (n = ndump; n > 0;) {
  1784. printf("%.16lx", adrs);
  1785. putchar(' ');
  1786. r = n < 16? n: 16;
  1787. nr = mread(adrs, temp, r);
  1788. adrs += nr;
  1789. for (m = 0; m < r; ++m) {
  1790. if ((m & 7) == 0 && m > 0)
  1791. putchar(' ');
  1792. if (m < nr)
  1793. printf("%.2x", temp[m]);
  1794. else
  1795. printf("%s", fault_chars[fault_type]);
  1796. }
  1797. if (m <= 8)
  1798. printf(" ");
  1799. for (; m < 16; ++m)
  1800. printf(" ");
  1801. printf(" |");
  1802. for (m = 0; m < r; ++m) {
  1803. if (m < nr) {
  1804. c = temp[m];
  1805. putchar(' ' <= c && c <= '~'? c: '.');
  1806. } else
  1807. putchar(' ');
  1808. }
  1809. n -= r;
  1810. for (; m < 16; ++m)
  1811. putchar(' ');
  1812. printf("|\n");
  1813. if (nr < r)
  1814. break;
  1815. }
  1816. }
  1817. int
  1818. ppc_inst_dump(unsigned long adr, long count, int praddr)
  1819. {
  1820. int nr, dotted;
  1821. unsigned long first_adr;
  1822. unsigned long inst, last_inst = 0;
  1823. unsigned char val[4];
  1824. dotted = 0;
  1825. for (first_adr = adr; count > 0; --count, adr += 4) {
  1826. nr = mread(adr, val, 4);
  1827. if (nr == 0) {
  1828. if (praddr) {
  1829. const char *x = fault_chars[fault_type];
  1830. printf("%.16lx %s%s%s%s\n", adr, x, x, x, x);
  1831. }
  1832. break;
  1833. }
  1834. inst = GETWORD(val);
  1835. if (adr > first_adr && inst == last_inst) {
  1836. if (!dotted) {
  1837. printf(" ...\n");
  1838. dotted = 1;
  1839. }
  1840. continue;
  1841. }
  1842. dotted = 0;
  1843. last_inst = inst;
  1844. if (praddr)
  1845. printf("%.16lx %.8x", adr, inst);
  1846. printf("\t");
  1847. print_insn_powerpc(inst, adr, 0); /* always returns 4 */
  1848. printf("\n");
  1849. }
  1850. return adr - first_adr;
  1851. }
  1852. void
  1853. print_address(unsigned long addr)
  1854. {
  1855. xmon_print_symbol(addr, "\t# ", "");
  1856. }
  1857. /*
  1858. * Memory operations - move, set, print differences
  1859. */
  1860. static unsigned long mdest; /* destination address */
  1861. static unsigned long msrc; /* source address */
  1862. static unsigned long mval; /* byte value to set memory to */
  1863. static unsigned long mcount; /* # bytes to affect */
  1864. static unsigned long mdiffs; /* max # differences to print */
  1865. void
  1866. memops(int cmd)
  1867. {
  1868. scanhex((void *)&mdest);
  1869. if( termch != '\n' )
  1870. termch = 0;
  1871. scanhex((void *)(cmd == 's'? &mval: &msrc));
  1872. if( termch != '\n' )
  1873. termch = 0;
  1874. scanhex((void *)&mcount);
  1875. switch( cmd ){
  1876. case 'm':
  1877. memmove((void *)mdest, (void *)msrc, mcount);
  1878. break;
  1879. case 's':
  1880. memset((void *)mdest, mval, mcount);
  1881. break;
  1882. case 'd':
  1883. if( termch != '\n' )
  1884. termch = 0;
  1885. scanhex((void *)&mdiffs);
  1886. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  1887. break;
  1888. }
  1889. }
  1890. void
  1891. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  1892. {
  1893. unsigned n, prt;
  1894. prt = 0;
  1895. for( n = nb; n > 0; --n )
  1896. if( *p1++ != *p2++ )
  1897. if( ++prt <= maxpr )
  1898. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  1899. p1[-1], p2 - 1, p2[-1]);
  1900. if( prt > maxpr )
  1901. printf("Total of %d differences\n", prt);
  1902. }
  1903. static unsigned mend;
  1904. static unsigned mask;
  1905. void
  1906. memlocate(void)
  1907. {
  1908. unsigned a, n;
  1909. unsigned char val[4];
  1910. last_cmd = "ml";
  1911. scanhex((void *)&mdest);
  1912. if (termch != '\n') {
  1913. termch = 0;
  1914. scanhex((void *)&mend);
  1915. if (termch != '\n') {
  1916. termch = 0;
  1917. scanhex((void *)&mval);
  1918. mask = ~0;
  1919. if (termch != '\n') termch = 0;
  1920. scanhex((void *)&mask);
  1921. }
  1922. }
  1923. n = 0;
  1924. for (a = mdest; a < mend; a += 4) {
  1925. if (mread(a, val, 4) == 4
  1926. && ((GETWORD(val) ^ mval) & mask) == 0) {
  1927. printf("%.16x: %.16x\n", a, GETWORD(val));
  1928. if (++n >= 10)
  1929. break;
  1930. }
  1931. }
  1932. }
  1933. static unsigned long mskip = 0x1000;
  1934. static unsigned long mlim = 0xffffffff;
  1935. void
  1936. memzcan(void)
  1937. {
  1938. unsigned char v;
  1939. unsigned a;
  1940. int ok, ook;
  1941. scanhex(&mdest);
  1942. if (termch != '\n') termch = 0;
  1943. scanhex(&mskip);
  1944. if (termch != '\n') termch = 0;
  1945. scanhex(&mlim);
  1946. ook = 0;
  1947. for (a = mdest; a < mlim; a += mskip) {
  1948. ok = mread(a, &v, 1);
  1949. if (ok && !ook) {
  1950. printf("%.8x .. ", a);
  1951. fflush(stdout);
  1952. } else if (!ok && ook)
  1953. printf("%.8x\n", a - mskip);
  1954. ook = ok;
  1955. if (a + mskip < a)
  1956. break;
  1957. }
  1958. if (ook)
  1959. printf("%.8x\n", a - mskip);
  1960. }
  1961. /* Input scanning routines */
  1962. int
  1963. skipbl(void)
  1964. {
  1965. int c;
  1966. if( termch != 0 ){
  1967. c = termch;
  1968. termch = 0;
  1969. } else
  1970. c = inchar();
  1971. while( c == ' ' || c == '\t' )
  1972. c = inchar();
  1973. return c;
  1974. }
  1975. #define N_PTREGS 44
  1976. static char *regnames[N_PTREGS] = {
  1977. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  1978. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  1979. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  1980. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  1981. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr", "softe",
  1982. "trap", "dar", "dsisr", "res"
  1983. };
  1984. int
  1985. scanhex(unsigned long *vp)
  1986. {
  1987. int c, d;
  1988. unsigned long v;
  1989. c = skipbl();
  1990. if (c == '%') {
  1991. /* parse register name */
  1992. char regname[8];
  1993. int i;
  1994. for (i = 0; i < sizeof(regname) - 1; ++i) {
  1995. c = inchar();
  1996. if (!isalnum(c)) {
  1997. termch = c;
  1998. break;
  1999. }
  2000. regname[i] = c;
  2001. }
  2002. regname[i] = 0;
  2003. for (i = 0; i < N_PTREGS; ++i) {
  2004. if (strcmp(regnames[i], regname) == 0) {
  2005. if (xmon_regs == NULL) {
  2006. printf("regs not available\n");
  2007. return 0;
  2008. }
  2009. *vp = ((unsigned long *)xmon_regs)[i];
  2010. return 1;
  2011. }
  2012. }
  2013. printf("invalid register name '%%%s'\n", regname);
  2014. return 0;
  2015. }
  2016. /* skip leading "0x" if any */
  2017. if (c == '0') {
  2018. c = inchar();
  2019. if (c == 'x') {
  2020. c = inchar();
  2021. } else {
  2022. d = hexdigit(c);
  2023. if (d == EOF) {
  2024. termch = c;
  2025. *vp = 0;
  2026. return 1;
  2027. }
  2028. }
  2029. } else if (c == '$') {
  2030. int i;
  2031. for (i=0; i<63; i++) {
  2032. c = inchar();
  2033. if (isspace(c)) {
  2034. termch = c;
  2035. break;
  2036. }
  2037. tmpstr[i] = c;
  2038. }
  2039. tmpstr[i++] = 0;
  2040. *vp = 0;
  2041. if (setjmp(bus_error_jmp) == 0) {
  2042. catch_memory_errors = 1;
  2043. sync();
  2044. *vp = kallsyms_lookup_name(tmpstr);
  2045. sync();
  2046. }
  2047. catch_memory_errors = 0;
  2048. if (!(*vp)) {
  2049. printf("unknown symbol '%s'\n", tmpstr);
  2050. return 0;
  2051. }
  2052. return 1;
  2053. }
  2054. d = hexdigit(c);
  2055. if (d == EOF) {
  2056. termch = c;
  2057. return 0;
  2058. }
  2059. v = 0;
  2060. do {
  2061. v = (v << 4) + d;
  2062. c = inchar();
  2063. d = hexdigit(c);
  2064. } while (d != EOF);
  2065. termch = c;
  2066. *vp = v;
  2067. return 1;
  2068. }
  2069. void
  2070. scannl(void)
  2071. {
  2072. int c;
  2073. c = termch;
  2074. termch = 0;
  2075. while( c != '\n' )
  2076. c = inchar();
  2077. }
  2078. int
  2079. hexdigit(int c)
  2080. {
  2081. if( '0' <= c && c <= '9' )
  2082. return c - '0';
  2083. if( 'A' <= c && c <= 'F' )
  2084. return c - ('A' - 10);
  2085. if( 'a' <= c && c <= 'f' )
  2086. return c - ('a' - 10);
  2087. return EOF;
  2088. }
  2089. void
  2090. getstring(char *s, int size)
  2091. {
  2092. int c;
  2093. c = skipbl();
  2094. do {
  2095. if( size > 1 ){
  2096. *s++ = c;
  2097. --size;
  2098. }
  2099. c = inchar();
  2100. } while( c != ' ' && c != '\t' && c != '\n' );
  2101. termch = c;
  2102. *s = 0;
  2103. }
  2104. static char line[256];
  2105. static char *lineptr;
  2106. void
  2107. flush_input(void)
  2108. {
  2109. lineptr = NULL;
  2110. }
  2111. int
  2112. inchar(void)
  2113. {
  2114. if (lineptr == NULL || *lineptr == 0) {
  2115. if (fgets(line, sizeof(line), stdin) == NULL) {
  2116. lineptr = NULL;
  2117. return EOF;
  2118. }
  2119. lineptr = line;
  2120. }
  2121. return *lineptr++;
  2122. }
  2123. void
  2124. take_input(char *str)
  2125. {
  2126. lineptr = str;
  2127. }
  2128. static void
  2129. symbol_lookup(void)
  2130. {
  2131. int type = inchar();
  2132. unsigned long addr;
  2133. static char tmp[64];
  2134. switch (type) {
  2135. case 'a':
  2136. if (scanhex(&addr))
  2137. xmon_print_symbol(addr, ": ", "\n");
  2138. termch = 0;
  2139. break;
  2140. case 's':
  2141. getstring(tmp, 64);
  2142. if (setjmp(bus_error_jmp) == 0) {
  2143. catch_memory_errors = 1;
  2144. sync();
  2145. addr = kallsyms_lookup_name(tmp);
  2146. if (addr)
  2147. printf("%s: %lx\n", tmp, addr);
  2148. else
  2149. printf("Symbol '%s' not found.\n", tmp);
  2150. sync();
  2151. }
  2152. catch_memory_errors = 0;
  2153. termch = 0;
  2154. break;
  2155. }
  2156. }
  2157. /* Print an address in numeric and symbolic form (if possible) */
  2158. static void xmon_print_symbol(unsigned long address, const char *mid,
  2159. const char *after)
  2160. {
  2161. char *modname;
  2162. const char *name = NULL;
  2163. unsigned long offset, size;
  2164. printf("%.16lx", address);
  2165. if (setjmp(bus_error_jmp) == 0) {
  2166. catch_memory_errors = 1;
  2167. sync();
  2168. name = kallsyms_lookup(address, &size, &offset, &modname,
  2169. tmpstr);
  2170. sync();
  2171. /* wait a little while to see if we get a machine check */
  2172. __delay(200);
  2173. }
  2174. catch_memory_errors = 0;
  2175. if (name) {
  2176. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2177. if (modname)
  2178. printf(" [%s]", modname);
  2179. }
  2180. printf("%s", after);
  2181. }
  2182. static void debug_trace(void)
  2183. {
  2184. unsigned long val, cmd, on;
  2185. cmd = skipbl();
  2186. if (cmd == '\n') {
  2187. /* show current state */
  2188. unsigned long i;
  2189. printf("ppc64_debug_switch = 0x%lx\n", ppc64_debug_switch);
  2190. for (i = 0; i < PPCDBG_NUM_FLAGS ;i++) {
  2191. on = PPCDBG_BITVAL(i) & ppc64_debug_switch;
  2192. printf("%02x %s %12s ", i, on ? "on " : "off", trace_names[i] ? trace_names[i] : "");
  2193. if (((i+1) % 3) == 0)
  2194. printf("\n");
  2195. }
  2196. printf("\n");
  2197. return;
  2198. }
  2199. while (cmd != '\n') {
  2200. on = 1; /* default if no sign given */
  2201. while (cmd == '+' || cmd == '-') {
  2202. on = (cmd == '+');
  2203. cmd = inchar();
  2204. if (cmd == ' ' || cmd == '\n') { /* Turn on or off based on + or - */
  2205. ppc64_debug_switch = on ? PPCDBG_ALL:PPCDBG_NONE;
  2206. printf("Setting all values to %s...\n", on ? "on" : "off");
  2207. if (cmd == '\n') return;
  2208. else cmd = skipbl();
  2209. }
  2210. else
  2211. termch = cmd;
  2212. }
  2213. termch = cmd; /* not +/- ... let scanhex see it */
  2214. scanhex((void *)&val);
  2215. if (val >= 64) {
  2216. printf("Value %x out of range:\n", val);
  2217. return;
  2218. }
  2219. if (on) {
  2220. ppc64_debug_switch |= PPCDBG_BITVAL(val);
  2221. printf("enable debug %x %s\n", val, trace_names[val] ? trace_names[val] : "");
  2222. } else {
  2223. ppc64_debug_switch &= ~PPCDBG_BITVAL(val);
  2224. printf("disable debug %x %s\n", val, trace_names[val] ? trace_names[val] : "");
  2225. }
  2226. cmd = skipbl();
  2227. }
  2228. }
  2229. static void dump_slb(void)
  2230. {
  2231. int i;
  2232. unsigned long tmp;
  2233. printf("SLB contents of cpu %x\n", smp_processor_id());
  2234. for (i = 0; i < SLB_NUM_ENTRIES; i++) {
  2235. asm volatile("slbmfee %0,%1" : "=r" (tmp) : "r" (i));
  2236. printf("%02d %016lx ", i, tmp);
  2237. asm volatile("slbmfev %0,%1" : "=r" (tmp) : "r" (i));
  2238. printf("%016lx\n", tmp);
  2239. }
  2240. }
  2241. static void dump_stab(void)
  2242. {
  2243. int i;
  2244. unsigned long *tmp = (unsigned long *)get_paca()->stab_addr;
  2245. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2246. for (i = 0; i < PAGE_SIZE/16; i++) {
  2247. unsigned long a, b;
  2248. a = *tmp++;
  2249. b = *tmp++;
  2250. if (a || b) {
  2251. printf("%03d %016lx ", i, a);
  2252. printf("%016lx\n", b);
  2253. }
  2254. }
  2255. }
  2256. void xmon_init(int enable)
  2257. {
  2258. if (enable) {
  2259. __debugger = xmon;
  2260. __debugger_ipi = xmon_ipi;
  2261. __debugger_bpt = xmon_bpt;
  2262. __debugger_sstep = xmon_sstep;
  2263. __debugger_iabr_match = xmon_iabr_match;
  2264. __debugger_dabr_match = xmon_dabr_match;
  2265. __debugger_fault_handler = xmon_fault_handler;
  2266. } else {
  2267. __debugger = NULL;
  2268. __debugger_ipi = NULL;
  2269. __debugger_bpt = NULL;
  2270. __debugger_sstep = NULL;
  2271. __debugger_iabr_match = NULL;
  2272. __debugger_dabr_match = NULL;
  2273. __debugger_fault_handler = NULL;
  2274. }
  2275. }
  2276. void dump_segments(void)
  2277. {
  2278. if (cpu_has_feature(CPU_FTR_SLB))
  2279. dump_slb();
  2280. else
  2281. dump_stab();
  2282. }