xmon.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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. xmon_core(regs, 0);
  529. return 1;
  530. }
  531. int xmon_iabr_match(struct pt_regs *regs)
  532. {
  533. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) != (MSR_IR|MSR_SF))
  534. return 0;
  535. if (iabr == 0)
  536. return 0;
  537. xmon_core(regs, 0);
  538. return 1;
  539. }
  540. int xmon_ipi(struct pt_regs *regs)
  541. {
  542. #ifdef CONFIG_SMP
  543. if (in_xmon && !cpu_isset(smp_processor_id(), cpus_in_xmon))
  544. xmon_core(regs, 1);
  545. #endif
  546. return 0;
  547. }
  548. int xmon_fault_handler(struct pt_regs *regs)
  549. {
  550. struct bpt *bp;
  551. unsigned long offset;
  552. if (in_xmon && catch_memory_errors)
  553. handle_fault(regs); /* doesn't return */
  554. if ((regs->msr & (MSR_IR|MSR_PR|MSR_SF)) == (MSR_IR|MSR_SF)) {
  555. bp = in_breakpoint_table(regs->nip, &offset);
  556. if (bp != NULL) {
  557. regs->nip = bp->address + offset;
  558. atomic_dec(&bp->ref_count);
  559. }
  560. }
  561. return 0;
  562. }
  563. /* On systems with a hypervisor, we can't set the DABR
  564. (data address breakpoint register) directly. */
  565. static void set_controlled_dabr(unsigned long val)
  566. {
  567. #ifdef CONFIG_PPC_PSERIES
  568. if (systemcfg->platform == PLATFORM_PSERIES_LPAR) {
  569. int rc = plpar_hcall_norets(H_SET_DABR, val);
  570. if (rc != H_Success)
  571. xmon_printf("Warning: setting DABR failed (%d)\n", rc);
  572. } else
  573. #endif
  574. set_dabr(val);
  575. }
  576. static struct bpt *at_breakpoint(unsigned long pc)
  577. {
  578. int i;
  579. struct bpt *bp;
  580. bp = bpts;
  581. for (i = 0; i < NBPTS; ++i, ++bp)
  582. if (bp->enabled && pc == bp->address)
  583. return bp;
  584. return NULL;
  585. }
  586. static struct bpt *in_breakpoint_table(unsigned long nip, unsigned long *offp)
  587. {
  588. unsigned long off;
  589. off = nip - (unsigned long) bpts;
  590. if (off >= sizeof(bpts))
  591. return NULL;
  592. off %= sizeof(struct bpt);
  593. if (off != offsetof(struct bpt, instr[0])
  594. && off != offsetof(struct bpt, instr[1]))
  595. return NULL;
  596. *offp = off - offsetof(struct bpt, instr[0]);
  597. return (struct bpt *) (nip - off);
  598. }
  599. static struct bpt *new_breakpoint(unsigned long a)
  600. {
  601. struct bpt *bp;
  602. a &= ~3UL;
  603. bp = at_breakpoint(a);
  604. if (bp)
  605. return bp;
  606. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  607. if (!bp->enabled && atomic_read(&bp->ref_count) == 0) {
  608. bp->address = a;
  609. bp->instr[1] = bpinstr;
  610. store_inst(&bp->instr[1]);
  611. return bp;
  612. }
  613. }
  614. printf("Sorry, no free breakpoints. Please clear one first.\n");
  615. return NULL;
  616. }
  617. static void insert_bpts(void)
  618. {
  619. int i;
  620. struct bpt *bp;
  621. bp = bpts;
  622. for (i = 0; i < NBPTS; ++i, ++bp) {
  623. if ((bp->enabled & (BP_TRAP|BP_IABR)) == 0)
  624. continue;
  625. if (mread(bp->address, &bp->instr[0], 4) != 4) {
  626. printf("Couldn't read instruction at %lx, "
  627. "disabling breakpoint there\n", bp->address);
  628. bp->enabled = 0;
  629. continue;
  630. }
  631. if (IS_MTMSRD(bp->instr[0]) || IS_RFID(bp->instr[0])) {
  632. printf("Breakpoint at %lx is on an mtmsrd or rfid "
  633. "instruction, disabling it\n", bp->address);
  634. bp->enabled = 0;
  635. continue;
  636. }
  637. store_inst(&bp->instr[0]);
  638. if (bp->enabled & BP_IABR)
  639. continue;
  640. if (mwrite(bp->address, &bpinstr, 4) != 4) {
  641. printf("Couldn't write instruction at %lx, "
  642. "disabling breakpoint there\n", bp->address);
  643. bp->enabled &= ~BP_TRAP;
  644. continue;
  645. }
  646. store_inst((void *)bp->address);
  647. }
  648. }
  649. static void insert_cpu_bpts(void)
  650. {
  651. if (dabr.enabled)
  652. set_controlled_dabr(dabr.address | (dabr.enabled & 7));
  653. if (iabr && cpu_has_feature(CPU_FTR_IABR))
  654. set_iabr(iabr->address
  655. | (iabr->enabled & (BP_IABR|BP_IABR_TE)));
  656. }
  657. static void remove_bpts(void)
  658. {
  659. int i;
  660. struct bpt *bp;
  661. unsigned instr;
  662. bp = bpts;
  663. for (i = 0; i < NBPTS; ++i, ++bp) {
  664. if ((bp->enabled & (BP_TRAP|BP_IABR)) != BP_TRAP)
  665. continue;
  666. if (mread(bp->address, &instr, 4) == 4
  667. && instr == bpinstr
  668. && mwrite(bp->address, &bp->instr, 4) != 4)
  669. printf("Couldn't remove breakpoint at %lx\n",
  670. bp->address);
  671. else
  672. store_inst((void *)bp->address);
  673. }
  674. }
  675. static void remove_cpu_bpts(void)
  676. {
  677. set_controlled_dabr(0);
  678. if (cpu_has_feature(CPU_FTR_IABR))
  679. set_iabr(0);
  680. }
  681. /* Command interpreting routine */
  682. static char *last_cmd;
  683. static int
  684. cmds(struct pt_regs *excp)
  685. {
  686. int cmd = 0;
  687. last_cmd = NULL;
  688. xmon_regs = excp;
  689. for(;;) {
  690. #ifdef CONFIG_SMP
  691. printf("%x:", smp_processor_id());
  692. #endif /* CONFIG_SMP */
  693. printf("mon> ");
  694. fflush(stdout);
  695. flush_input();
  696. termch = 0;
  697. cmd = skipbl();
  698. if( cmd == '\n' ) {
  699. if (last_cmd == NULL)
  700. continue;
  701. take_input(last_cmd);
  702. last_cmd = NULL;
  703. cmd = inchar();
  704. }
  705. switch (cmd) {
  706. case 'm':
  707. cmd = inchar();
  708. switch (cmd) {
  709. case 'm':
  710. case 's':
  711. case 'd':
  712. memops(cmd);
  713. break;
  714. case 'l':
  715. memlocate();
  716. break;
  717. case 'z':
  718. memzcan();
  719. break;
  720. case 'i':
  721. show_mem();
  722. break;
  723. default:
  724. termch = cmd;
  725. memex();
  726. }
  727. break;
  728. case 'd':
  729. dump();
  730. break;
  731. case 'l':
  732. symbol_lookup();
  733. break;
  734. case 'r':
  735. prregs(excp); /* print regs */
  736. break;
  737. case 'e':
  738. excprint(excp);
  739. break;
  740. case 'S':
  741. super_regs();
  742. break;
  743. case 't':
  744. backtrace(excp);
  745. break;
  746. case 'f':
  747. cacheflush();
  748. break;
  749. case 's':
  750. if (do_step(excp))
  751. return cmd;
  752. break;
  753. case 'x':
  754. case 'X':
  755. case EOF:
  756. return cmd;
  757. case '?':
  758. printf(help_string);
  759. break;
  760. case 'p':
  761. show_state();
  762. break;
  763. case 'b':
  764. bpt_cmds();
  765. break;
  766. case 'C':
  767. csum();
  768. break;
  769. case 'c':
  770. if (cpu_cmd())
  771. return 0;
  772. break;
  773. case 'z':
  774. bootcmds();
  775. break;
  776. case 'T':
  777. debug_trace();
  778. break;
  779. case 'u':
  780. dump_segments();
  781. break;
  782. default:
  783. printf("Unrecognized command: ");
  784. do {
  785. if (' ' < cmd && cmd <= '~')
  786. putchar(cmd);
  787. else
  788. printf("\\x%x", cmd);
  789. cmd = inchar();
  790. } while (cmd != '\n');
  791. printf(" (type ? for help)\n");
  792. break;
  793. }
  794. }
  795. }
  796. /*
  797. * Step a single instruction.
  798. * Some instructions we emulate, others we execute with MSR_SE set.
  799. */
  800. static int do_step(struct pt_regs *regs)
  801. {
  802. unsigned int instr;
  803. int stepped;
  804. /* check we are in 64-bit kernel mode, translation enabled */
  805. if ((regs->msr & (MSR_SF|MSR_PR|MSR_IR)) == (MSR_SF|MSR_IR)) {
  806. if (mread(regs->nip, &instr, 4) == 4) {
  807. stepped = emulate_step(regs, instr);
  808. if (stepped < 0) {
  809. printf("Couldn't single-step %s instruction\n",
  810. (IS_RFID(instr)? "rfid": "mtmsrd"));
  811. return 0;
  812. }
  813. if (stepped > 0) {
  814. regs->trap = 0xd00 | (regs->trap & 1);
  815. printf("stepped to ");
  816. xmon_print_symbol(regs->nip, " ", "\n");
  817. ppc_inst_dump(regs->nip, 1, 0);
  818. return 0;
  819. }
  820. }
  821. }
  822. regs->msr |= MSR_SE;
  823. return 1;
  824. }
  825. static void bootcmds(void)
  826. {
  827. int cmd;
  828. cmd = inchar();
  829. if (cmd == 'r')
  830. ppc_md.restart(NULL);
  831. else if (cmd == 'h')
  832. ppc_md.halt();
  833. else if (cmd == 'p')
  834. ppc_md.power_off();
  835. }
  836. static int cpu_cmd(void)
  837. {
  838. #ifdef CONFIG_SMP
  839. unsigned long cpu;
  840. int timeout;
  841. int count;
  842. if (!scanhex(&cpu)) {
  843. /* print cpus waiting or in xmon */
  844. printf("cpus stopped:");
  845. count = 0;
  846. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  847. if (cpu_isset(cpu, cpus_in_xmon)) {
  848. if (count == 0)
  849. printf(" %x", cpu);
  850. ++count;
  851. } else {
  852. if (count > 1)
  853. printf("-%x", cpu - 1);
  854. count = 0;
  855. }
  856. }
  857. if (count > 1)
  858. printf("-%x", NR_CPUS - 1);
  859. printf("\n");
  860. return 0;
  861. }
  862. /* try to switch to cpu specified */
  863. if (!cpu_isset(cpu, cpus_in_xmon)) {
  864. printf("cpu 0x%x isn't in xmon\n", cpu);
  865. return 0;
  866. }
  867. xmon_taken = 0;
  868. mb();
  869. xmon_owner = cpu;
  870. timeout = 10000000;
  871. while (!xmon_taken) {
  872. if (--timeout == 0) {
  873. if (test_and_set_bit(0, &xmon_taken))
  874. break;
  875. /* take control back */
  876. mb();
  877. xmon_owner = smp_processor_id();
  878. printf("cpu %u didn't take control\n", cpu);
  879. return 0;
  880. }
  881. barrier();
  882. }
  883. return 1;
  884. #else
  885. return 0;
  886. #endif /* CONFIG_SMP */
  887. }
  888. static unsigned short fcstab[256] = {
  889. 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
  890. 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
  891. 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
  892. 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
  893. 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
  894. 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
  895. 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
  896. 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
  897. 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
  898. 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
  899. 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
  900. 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
  901. 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
  902. 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
  903. 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
  904. 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
  905. 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
  906. 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
  907. 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
  908. 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
  909. 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
  910. 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
  911. 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
  912. 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
  913. 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
  914. 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
  915. 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
  916. 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
  917. 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
  918. 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
  919. 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
  920. 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
  921. };
  922. #define FCS(fcs, c) (((fcs) >> 8) ^ fcstab[((fcs) ^ (c)) & 0xff])
  923. static void
  924. csum(void)
  925. {
  926. unsigned int i;
  927. unsigned short fcs;
  928. unsigned char v;
  929. if (!scanhex(&adrs))
  930. return;
  931. if (!scanhex(&ncsum))
  932. return;
  933. fcs = 0xffff;
  934. for (i = 0; i < ncsum; ++i) {
  935. if (mread(adrs+i, &v, 1) == 0) {
  936. printf("csum stopped at %x\n", adrs+i);
  937. break;
  938. }
  939. fcs = FCS(fcs, v);
  940. }
  941. printf("%x\n", fcs);
  942. }
  943. /*
  944. * Check if this is a suitable place to put a breakpoint.
  945. */
  946. static long check_bp_loc(unsigned long addr)
  947. {
  948. unsigned int instr;
  949. addr &= ~3;
  950. if (addr < KERNELBASE) {
  951. printf("Breakpoints may only be placed at kernel addresses\n");
  952. return 0;
  953. }
  954. if (!mread(addr, &instr, sizeof(instr))) {
  955. printf("Can't read instruction at address %lx\n", addr);
  956. return 0;
  957. }
  958. if (IS_MTMSRD(instr) || IS_RFID(instr)) {
  959. printf("Breakpoints may not be placed on mtmsrd or rfid "
  960. "instructions\n");
  961. return 0;
  962. }
  963. return 1;
  964. }
  965. static char *breakpoint_help_string =
  966. "Breakpoint command usage:\n"
  967. "b show breakpoints\n"
  968. "b <addr> [cnt] set breakpoint at given instr addr\n"
  969. "bc clear all breakpoints\n"
  970. "bc <n/addr> clear breakpoint number n or at addr\n"
  971. "bi <addr> [cnt] set hardware instr breakpoint (POWER3/RS64 only)\n"
  972. "bd <addr> [cnt] set hardware data breakpoint\n"
  973. "";
  974. static void
  975. bpt_cmds(void)
  976. {
  977. int cmd;
  978. unsigned long a;
  979. int mode, i;
  980. struct bpt *bp;
  981. const char badaddr[] = "Only kernel addresses are permitted "
  982. "for breakpoints\n";
  983. cmd = inchar();
  984. switch (cmd) {
  985. case 'd': /* bd - hardware data breakpoint */
  986. mode = 7;
  987. cmd = inchar();
  988. if (cmd == 'r')
  989. mode = 5;
  990. else if (cmd == 'w')
  991. mode = 6;
  992. else
  993. termch = cmd;
  994. dabr.address = 0;
  995. dabr.enabled = 0;
  996. if (scanhex(&dabr.address)) {
  997. if (dabr.address < KERNELBASE) {
  998. printf(badaddr);
  999. break;
  1000. }
  1001. dabr.address &= ~7;
  1002. dabr.enabled = mode | BP_DABR;
  1003. }
  1004. break;
  1005. case 'i': /* bi - hardware instr breakpoint */
  1006. if (!cpu_has_feature(CPU_FTR_IABR)) {
  1007. printf("Hardware instruction breakpoint "
  1008. "not supported on this cpu\n");
  1009. break;
  1010. }
  1011. if (iabr) {
  1012. iabr->enabled &= ~(BP_IABR | BP_IABR_TE);
  1013. iabr = NULL;
  1014. }
  1015. if (!scanhex(&a))
  1016. break;
  1017. if (!check_bp_loc(a))
  1018. break;
  1019. bp = new_breakpoint(a);
  1020. if (bp != NULL) {
  1021. bp->enabled |= BP_IABR | BP_IABR_TE;
  1022. iabr = bp;
  1023. }
  1024. break;
  1025. case 'c':
  1026. if (!scanhex(&a)) {
  1027. /* clear all breakpoints */
  1028. for (i = 0; i < NBPTS; ++i)
  1029. bpts[i].enabled = 0;
  1030. iabr = NULL;
  1031. dabr.enabled = 0;
  1032. printf("All breakpoints cleared\n");
  1033. break;
  1034. }
  1035. if (a <= NBPTS && a >= 1) {
  1036. /* assume a breakpoint number */
  1037. bp = &bpts[a-1]; /* bp nums are 1 based */
  1038. } else {
  1039. /* assume a breakpoint address */
  1040. bp = at_breakpoint(a);
  1041. if (bp == 0) {
  1042. printf("No breakpoint at %x\n", a);
  1043. break;
  1044. }
  1045. }
  1046. printf("Cleared breakpoint %x (", BP_NUM(bp));
  1047. xmon_print_symbol(bp->address, " ", ")\n");
  1048. bp->enabled = 0;
  1049. break;
  1050. default:
  1051. termch = cmd;
  1052. cmd = skipbl();
  1053. if (cmd == '?') {
  1054. printf(breakpoint_help_string);
  1055. break;
  1056. }
  1057. termch = cmd;
  1058. if (!scanhex(&a)) {
  1059. /* print all breakpoints */
  1060. printf(" type address\n");
  1061. if (dabr.enabled) {
  1062. printf(" data %.16lx [", dabr.address);
  1063. if (dabr.enabled & 1)
  1064. printf("r");
  1065. if (dabr.enabled & 2)
  1066. printf("w");
  1067. printf("]\n");
  1068. }
  1069. for (bp = bpts; bp < &bpts[NBPTS]; ++bp) {
  1070. if (!bp->enabled)
  1071. continue;
  1072. printf("%2x %s ", BP_NUM(bp),
  1073. (bp->enabled & BP_IABR)? "inst": "trap");
  1074. xmon_print_symbol(bp->address, " ", "\n");
  1075. }
  1076. break;
  1077. }
  1078. if (!check_bp_loc(a))
  1079. break;
  1080. bp = new_breakpoint(a);
  1081. if (bp != NULL)
  1082. bp->enabled |= BP_TRAP;
  1083. break;
  1084. }
  1085. }
  1086. /* Very cheap human name for vector lookup. */
  1087. static
  1088. const char *getvecname(unsigned long vec)
  1089. {
  1090. char *ret;
  1091. switch (vec) {
  1092. case 0x100: ret = "(System Reset)"; break;
  1093. case 0x200: ret = "(Machine Check)"; break;
  1094. case 0x300: ret = "(Data Access)"; break;
  1095. case 0x380: ret = "(Data SLB Access)"; break;
  1096. case 0x400: ret = "(Instruction Access)"; break;
  1097. case 0x480: ret = "(Instruction SLB Access)"; break;
  1098. case 0x500: ret = "(Hardware Interrupt)"; break;
  1099. case 0x600: ret = "(Alignment)"; break;
  1100. case 0x700: ret = "(Program Check)"; break;
  1101. case 0x800: ret = "(FPU Unavailable)"; break;
  1102. case 0x900: ret = "(Decrementer)"; break;
  1103. case 0xc00: ret = "(System Call)"; break;
  1104. case 0xd00: ret = "(Single Step)"; break;
  1105. case 0xf00: ret = "(Performance Monitor)"; break;
  1106. case 0xf20: ret = "(Altivec Unavailable)"; break;
  1107. case 0x1300: ret = "(Instruction Breakpoint)"; break;
  1108. default: ret = "";
  1109. }
  1110. return ret;
  1111. }
  1112. static void get_function_bounds(unsigned long pc, unsigned long *startp,
  1113. unsigned long *endp)
  1114. {
  1115. unsigned long size, offset;
  1116. const char *name;
  1117. char *modname;
  1118. *startp = *endp = 0;
  1119. if (pc == 0)
  1120. return;
  1121. if (setjmp(bus_error_jmp) == 0) {
  1122. catch_memory_errors = 1;
  1123. sync();
  1124. name = kallsyms_lookup(pc, &size, &offset, &modname, tmpstr);
  1125. if (name != NULL) {
  1126. *startp = pc - offset;
  1127. *endp = pc - offset + size;
  1128. }
  1129. sync();
  1130. }
  1131. catch_memory_errors = 0;
  1132. }
  1133. static int xmon_depth_to_print = 64;
  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 + 16, &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 + 16, &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("[%.16lx] ", sp);
  1180. xmon_print_symbol(ip, " ", " (unreliable)\n");
  1181. }
  1182. pc = lr = 0;
  1183. } else {
  1184. printf("[%.16lx] ", 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 + 0x60, &marker, sizeof(unsigned long))
  1190. && marker == 0x7265677368657265ul) {
  1191. if (mread(sp + 0x70, &regs, sizeof(regs))
  1192. != sizeof(regs)) {
  1193. printf("Couldn't read registers at %lx\n",
  1194. sp + 0x70);
  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. printf(" paca = 0x%lx\n", get_paca());
  1255. if (current) {
  1256. printf(" pid = %ld, comm = %s\n",
  1257. current->pid, current->comm);
  1258. }
  1259. if (trap == 0x700)
  1260. print_bug_trap(fp);
  1261. }
  1262. void prregs(struct pt_regs *fp)
  1263. {
  1264. int n;
  1265. unsigned long base;
  1266. struct pt_regs regs;
  1267. if (scanhex(&base)) {
  1268. if (setjmp(bus_error_jmp) == 0) {
  1269. catch_memory_errors = 1;
  1270. sync();
  1271. regs = *(struct pt_regs *)base;
  1272. sync();
  1273. __delay(200);
  1274. } else {
  1275. catch_memory_errors = 0;
  1276. printf("*** Error reading registers from %.16lx\n",
  1277. base);
  1278. return;
  1279. }
  1280. catch_memory_errors = 0;
  1281. fp = &regs;
  1282. }
  1283. if (FULL_REGS(fp)) {
  1284. for (n = 0; n < 16; ++n)
  1285. printf("R%.2ld = %.16lx R%.2ld = %.16lx\n",
  1286. n, fp->gpr[n], n+16, fp->gpr[n+16]);
  1287. } else {
  1288. for (n = 0; n < 7; ++n)
  1289. printf("R%.2ld = %.16lx R%.2ld = %.16lx\n",
  1290. n, fp->gpr[n], n+7, fp->gpr[n+7]);
  1291. }
  1292. printf("pc = ");
  1293. xmon_print_symbol(fp->nip, " ", "\n");
  1294. printf("lr = ");
  1295. xmon_print_symbol(fp->link, " ", "\n");
  1296. printf("msr = %.16lx cr = %.8lx\n", fp->msr, fp->ccr);
  1297. printf("ctr = %.16lx xer = %.16lx trap = %8lx\n",
  1298. fp->ctr, fp->xer, fp->trap);
  1299. }
  1300. void cacheflush(void)
  1301. {
  1302. int cmd;
  1303. unsigned long nflush;
  1304. cmd = inchar();
  1305. if (cmd != 'i')
  1306. termch = cmd;
  1307. scanhex((void *)&adrs);
  1308. if (termch != '\n')
  1309. termch = 0;
  1310. nflush = 1;
  1311. scanhex(&nflush);
  1312. nflush = (nflush + L1_CACHE_BYTES - 1) / L1_CACHE_BYTES;
  1313. if (setjmp(bus_error_jmp) == 0) {
  1314. catch_memory_errors = 1;
  1315. sync();
  1316. if (cmd != 'i') {
  1317. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1318. cflush((void *) adrs);
  1319. } else {
  1320. for (; nflush > 0; --nflush, adrs += L1_CACHE_BYTES)
  1321. cinval((void *) adrs);
  1322. }
  1323. sync();
  1324. /* wait a little while to see if we get a machine check */
  1325. __delay(200);
  1326. }
  1327. catch_memory_errors = 0;
  1328. }
  1329. unsigned long
  1330. read_spr(int n)
  1331. {
  1332. unsigned int instrs[2];
  1333. unsigned long (*code)(void);
  1334. unsigned long opd[3];
  1335. unsigned long ret = -1UL;
  1336. instrs[0] = 0x7c6002a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1337. instrs[1] = 0x4e800020;
  1338. opd[0] = (unsigned long)instrs;
  1339. opd[1] = 0;
  1340. opd[2] = 0;
  1341. store_inst(instrs);
  1342. store_inst(instrs+1);
  1343. code = (unsigned long (*)(void)) opd;
  1344. if (setjmp(bus_error_jmp) == 0) {
  1345. catch_memory_errors = 1;
  1346. sync();
  1347. ret = code();
  1348. sync();
  1349. /* wait a little while to see if we get a machine check */
  1350. __delay(200);
  1351. n = size;
  1352. }
  1353. return ret;
  1354. }
  1355. void
  1356. write_spr(int n, unsigned long val)
  1357. {
  1358. unsigned int instrs[2];
  1359. unsigned long (*code)(unsigned long);
  1360. unsigned long opd[3];
  1361. instrs[0] = 0x7c6003a6 + ((n & 0x1F) << 16) + ((n & 0x3e0) << 6);
  1362. instrs[1] = 0x4e800020;
  1363. opd[0] = (unsigned long)instrs;
  1364. opd[1] = 0;
  1365. opd[2] = 0;
  1366. store_inst(instrs);
  1367. store_inst(instrs+1);
  1368. code = (unsigned long (*)(unsigned long)) opd;
  1369. if (setjmp(bus_error_jmp) == 0) {
  1370. catch_memory_errors = 1;
  1371. sync();
  1372. code(val);
  1373. sync();
  1374. /* wait a little while to see if we get a machine check */
  1375. __delay(200);
  1376. n = size;
  1377. }
  1378. }
  1379. static unsigned long regno;
  1380. extern char exc_prolog;
  1381. extern char dec_exc;
  1382. void
  1383. super_regs(void)
  1384. {
  1385. int cmd;
  1386. unsigned long val;
  1387. #ifdef CONFIG_PPC_ISERIES
  1388. struct paca_struct *ptrPaca = NULL;
  1389. struct lppaca *ptrLpPaca = NULL;
  1390. struct ItLpRegSave *ptrLpRegSave = NULL;
  1391. #endif
  1392. cmd = skipbl();
  1393. if (cmd == '\n') {
  1394. unsigned long sp, toc;
  1395. asm("mr %0,1" : "=r" (sp) :);
  1396. asm("mr %0,2" : "=r" (toc) :);
  1397. printf("msr = %.16lx sprg0= %.16lx\n", get_msr(), get_sprg0());
  1398. printf("pvr = %.16lx sprg1= %.16lx\n", get_pvr(), get_sprg1());
  1399. printf("dec = %.16lx sprg2= %.16lx\n", get_dec(), get_sprg2());
  1400. printf("sp = %.16lx sprg3= %.16lx\n", sp, get_sprg3());
  1401. printf("toc = %.16lx dar = %.16lx\n", toc, get_dar());
  1402. printf("srr0 = %.16lx srr1 = %.16lx\n", get_srr0(), get_srr1());
  1403. #ifdef CONFIG_PPC_ISERIES
  1404. // Dump out relevant Paca data areas.
  1405. printf("Paca: \n");
  1406. ptrPaca = get_paca();
  1407. printf(" Local Processor Control Area (LpPaca): \n");
  1408. ptrLpPaca = ptrPaca->lppaca_ptr;
  1409. printf(" Saved Srr0=%.16lx Saved Srr1=%.16lx \n",
  1410. ptrLpPaca->saved_srr0, ptrLpPaca->saved_srr1);
  1411. printf(" Saved Gpr3=%.16lx Saved Gpr4=%.16lx \n",
  1412. ptrLpPaca->saved_gpr3, ptrLpPaca->saved_gpr4);
  1413. printf(" Saved Gpr5=%.16lx \n", ptrLpPaca->saved_gpr5);
  1414. printf(" Local Processor Register Save Area (LpRegSave): \n");
  1415. ptrLpRegSave = ptrPaca->reg_save_ptr;
  1416. printf(" Saved Sprg0=%.16lx Saved Sprg1=%.16lx \n",
  1417. ptrLpRegSave->xSPRG0, ptrLpRegSave->xSPRG0);
  1418. printf(" Saved Sprg2=%.16lx Saved Sprg3=%.16lx \n",
  1419. ptrLpRegSave->xSPRG2, ptrLpRegSave->xSPRG3);
  1420. printf(" Saved Msr =%.16lx Saved Nia =%.16lx \n",
  1421. ptrLpRegSave->xMSR, ptrLpRegSave->xNIA);
  1422. #endif
  1423. return;
  1424. }
  1425. scanhex(&regno);
  1426. switch (cmd) {
  1427. case 'w':
  1428. val = read_spr(regno);
  1429. scanhex(&val);
  1430. write_spr(regno, val);
  1431. /* fall through */
  1432. case 'r':
  1433. printf("spr %lx = %lx\n", regno, read_spr(regno));
  1434. break;
  1435. case 'm':
  1436. val = get_msr();
  1437. scanhex(&val);
  1438. set_msrd(val);
  1439. break;
  1440. }
  1441. scannl();
  1442. }
  1443. /*
  1444. * Stuff for reading and writing memory safely
  1445. */
  1446. int
  1447. mread(unsigned long adrs, void *buf, int size)
  1448. {
  1449. volatile int n;
  1450. char *p, *q;
  1451. n = 0;
  1452. if (setjmp(bus_error_jmp) == 0) {
  1453. catch_memory_errors = 1;
  1454. sync();
  1455. p = (char *)adrs;
  1456. q = (char *)buf;
  1457. switch (size) {
  1458. case 2:
  1459. *(short *)q = *(short *)p;
  1460. break;
  1461. case 4:
  1462. *(int *)q = *(int *)p;
  1463. break;
  1464. case 8:
  1465. *(long *)q = *(long *)p;
  1466. break;
  1467. default:
  1468. for( ; n < size; ++n) {
  1469. *q++ = *p++;
  1470. sync();
  1471. }
  1472. }
  1473. sync();
  1474. /* wait a little while to see if we get a machine check */
  1475. __delay(200);
  1476. n = size;
  1477. }
  1478. catch_memory_errors = 0;
  1479. return n;
  1480. }
  1481. int
  1482. mwrite(unsigned long adrs, void *buf, int size)
  1483. {
  1484. volatile int n;
  1485. char *p, *q;
  1486. n = 0;
  1487. if (setjmp(bus_error_jmp) == 0) {
  1488. catch_memory_errors = 1;
  1489. sync();
  1490. p = (char *) adrs;
  1491. q = (char *) buf;
  1492. switch (size) {
  1493. case 2:
  1494. *(short *)p = *(short *)q;
  1495. break;
  1496. case 4:
  1497. *(int *)p = *(int *)q;
  1498. break;
  1499. case 8:
  1500. *(long *)p = *(long *)q;
  1501. break;
  1502. default:
  1503. for ( ; n < size; ++n) {
  1504. *p++ = *q++;
  1505. sync();
  1506. }
  1507. }
  1508. sync();
  1509. /* wait a little while to see if we get a machine check */
  1510. __delay(200);
  1511. n = size;
  1512. } else {
  1513. printf("*** Error writing address %x\n", adrs + n);
  1514. }
  1515. catch_memory_errors = 0;
  1516. return n;
  1517. }
  1518. static int fault_type;
  1519. static char *fault_chars[] = { "--", "**", "##" };
  1520. static int
  1521. handle_fault(struct pt_regs *regs)
  1522. {
  1523. switch (TRAP(regs)) {
  1524. case 0x200:
  1525. fault_type = 0;
  1526. break;
  1527. case 0x300:
  1528. case 0x380:
  1529. fault_type = 1;
  1530. break;
  1531. default:
  1532. fault_type = 2;
  1533. }
  1534. longjmp(bus_error_jmp, 1);
  1535. return 0;
  1536. }
  1537. #define SWAP(a, b, t) ((t) = (a), (a) = (b), (b) = (t))
  1538. void
  1539. byterev(unsigned char *val, int size)
  1540. {
  1541. int t;
  1542. switch (size) {
  1543. case 2:
  1544. SWAP(val[0], val[1], t);
  1545. break;
  1546. case 4:
  1547. SWAP(val[0], val[3], t);
  1548. SWAP(val[1], val[2], t);
  1549. break;
  1550. case 8: /* is there really any use for this? */
  1551. SWAP(val[0], val[7], t);
  1552. SWAP(val[1], val[6], t);
  1553. SWAP(val[2], val[5], t);
  1554. SWAP(val[3], val[4], t);
  1555. break;
  1556. }
  1557. }
  1558. static int brev;
  1559. static int mnoread;
  1560. static char *memex_help_string =
  1561. "Memory examine command usage:\n"
  1562. "m [addr] [flags] examine/change memory\n"
  1563. " addr is optional. will start where left off.\n"
  1564. " flags may include chars from this set:\n"
  1565. " b modify by bytes (default)\n"
  1566. " w modify by words (2 byte)\n"
  1567. " l modify by longs (4 byte)\n"
  1568. " d modify by doubleword (8 byte)\n"
  1569. " r toggle reverse byte order mode\n"
  1570. " n do not read memory (for i/o spaces)\n"
  1571. " . ok to read (default)\n"
  1572. "NOTE: flags are saved as defaults\n"
  1573. "";
  1574. static char *memex_subcmd_help_string =
  1575. "Memory examine subcommands:\n"
  1576. " hexval write this val to current location\n"
  1577. " 'string' write chars from string to this location\n"
  1578. " ' increment address\n"
  1579. " ^ decrement address\n"
  1580. " / increment addr by 0x10. //=0x100, ///=0x1000, etc\n"
  1581. " \\ decrement addr by 0x10. \\\\=0x100, \\\\\\=0x1000, etc\n"
  1582. " ` clear no-read flag\n"
  1583. " ; stay at this addr\n"
  1584. " v change to byte mode\n"
  1585. " w change to word (2 byte) mode\n"
  1586. " l change to long (4 byte) mode\n"
  1587. " u change to doubleword (8 byte) mode\n"
  1588. " m addr change current addr\n"
  1589. " n toggle no-read flag\n"
  1590. " r toggle byte reverse flag\n"
  1591. " < count back up count bytes\n"
  1592. " > count skip forward count bytes\n"
  1593. " x exit this mode\n"
  1594. "";
  1595. void
  1596. memex(void)
  1597. {
  1598. int cmd, inc, i, nslash;
  1599. unsigned long n;
  1600. unsigned char val[16];
  1601. scanhex((void *)&adrs);
  1602. cmd = skipbl();
  1603. if (cmd == '?') {
  1604. printf(memex_help_string);
  1605. return;
  1606. } else {
  1607. termch = cmd;
  1608. }
  1609. last_cmd = "m\n";
  1610. while ((cmd = skipbl()) != '\n') {
  1611. switch( cmd ){
  1612. case 'b': size = 1; break;
  1613. case 'w': size = 2; break;
  1614. case 'l': size = 4; break;
  1615. case 'd': size = 8; break;
  1616. case 'r': brev = !brev; break;
  1617. case 'n': mnoread = 1; break;
  1618. case '.': mnoread = 0; break;
  1619. }
  1620. }
  1621. if( size <= 0 )
  1622. size = 1;
  1623. else if( size > 8 )
  1624. size = 8;
  1625. for(;;){
  1626. if (!mnoread)
  1627. n = mread(adrs, val, size);
  1628. printf("%.16x%c", adrs, brev? 'r': ' ');
  1629. if (!mnoread) {
  1630. if (brev)
  1631. byterev(val, size);
  1632. putchar(' ');
  1633. for (i = 0; i < n; ++i)
  1634. printf("%.2x", val[i]);
  1635. for (; i < size; ++i)
  1636. printf("%s", fault_chars[fault_type]);
  1637. }
  1638. putchar(' ');
  1639. inc = size;
  1640. nslash = 0;
  1641. for(;;){
  1642. if( scanhex(&n) ){
  1643. for (i = 0; i < size; ++i)
  1644. val[i] = n >> (i * 8);
  1645. if (!brev)
  1646. byterev(val, size);
  1647. mwrite(adrs, val, size);
  1648. inc = size;
  1649. }
  1650. cmd = skipbl();
  1651. if (cmd == '\n')
  1652. break;
  1653. inc = 0;
  1654. switch (cmd) {
  1655. case '\'':
  1656. for(;;){
  1657. n = inchar();
  1658. if( n == '\\' )
  1659. n = bsesc();
  1660. else if( n == '\'' )
  1661. break;
  1662. for (i = 0; i < size; ++i)
  1663. val[i] = n >> (i * 8);
  1664. if (!brev)
  1665. byterev(val, size);
  1666. mwrite(adrs, val, size);
  1667. adrs += size;
  1668. }
  1669. adrs -= size;
  1670. inc = size;
  1671. break;
  1672. case ',':
  1673. adrs += size;
  1674. break;
  1675. case '.':
  1676. mnoread = 0;
  1677. break;
  1678. case ';':
  1679. break;
  1680. case 'x':
  1681. case EOF:
  1682. scannl();
  1683. return;
  1684. case 'b':
  1685. case 'v':
  1686. size = 1;
  1687. break;
  1688. case 'w':
  1689. size = 2;
  1690. break;
  1691. case 'l':
  1692. size = 4;
  1693. break;
  1694. case 'u':
  1695. size = 8;
  1696. break;
  1697. case '^':
  1698. adrs -= size;
  1699. break;
  1700. break;
  1701. case '/':
  1702. if (nslash > 0)
  1703. adrs -= 1 << nslash;
  1704. else
  1705. nslash = 0;
  1706. nslash += 4;
  1707. adrs += 1 << nslash;
  1708. break;
  1709. case '\\':
  1710. if (nslash < 0)
  1711. adrs += 1 << -nslash;
  1712. else
  1713. nslash = 0;
  1714. nslash -= 4;
  1715. adrs -= 1 << -nslash;
  1716. break;
  1717. case 'm':
  1718. scanhex((void *)&adrs);
  1719. break;
  1720. case 'n':
  1721. mnoread = 1;
  1722. break;
  1723. case 'r':
  1724. brev = !brev;
  1725. break;
  1726. case '<':
  1727. n = size;
  1728. scanhex(&n);
  1729. adrs -= n;
  1730. break;
  1731. case '>':
  1732. n = size;
  1733. scanhex(&n);
  1734. adrs += n;
  1735. break;
  1736. case '?':
  1737. printf(memex_subcmd_help_string);
  1738. break;
  1739. }
  1740. }
  1741. adrs += inc;
  1742. }
  1743. }
  1744. int
  1745. bsesc(void)
  1746. {
  1747. int c;
  1748. c = inchar();
  1749. switch( c ){
  1750. case 'n': c = '\n'; break;
  1751. case 'r': c = '\r'; break;
  1752. case 'b': c = '\b'; break;
  1753. case 't': c = '\t'; break;
  1754. }
  1755. return c;
  1756. }
  1757. #define isxdigit(c) (('0' <= (c) && (c) <= '9') \
  1758. || ('a' <= (c) && (c) <= 'f') \
  1759. || ('A' <= (c) && (c) <= 'F'))
  1760. void
  1761. dump(void)
  1762. {
  1763. int c;
  1764. c = inchar();
  1765. if ((isxdigit(c) && c != 'f' && c != 'd') || c == '\n')
  1766. termch = c;
  1767. scanhex((void *)&adrs);
  1768. if (termch != '\n')
  1769. termch = 0;
  1770. if (c == 'i') {
  1771. scanhex(&nidump);
  1772. if (nidump == 0)
  1773. nidump = 16;
  1774. else if (nidump > MAX_DUMP)
  1775. nidump = MAX_DUMP;
  1776. adrs += ppc_inst_dump(adrs, nidump, 1);
  1777. last_cmd = "di\n";
  1778. } else {
  1779. scanhex(&ndump);
  1780. if (ndump == 0)
  1781. ndump = 64;
  1782. else if (ndump > MAX_DUMP)
  1783. ndump = MAX_DUMP;
  1784. prdump(adrs, ndump);
  1785. adrs += ndump;
  1786. last_cmd = "d\n";
  1787. }
  1788. }
  1789. void
  1790. prdump(unsigned long adrs, long ndump)
  1791. {
  1792. long n, m, c, r, nr;
  1793. unsigned char temp[16];
  1794. for (n = ndump; n > 0;) {
  1795. printf("%.16lx", adrs);
  1796. putchar(' ');
  1797. r = n < 16? n: 16;
  1798. nr = mread(adrs, temp, r);
  1799. adrs += nr;
  1800. for (m = 0; m < r; ++m) {
  1801. if ((m & 7) == 0 && m > 0)
  1802. putchar(' ');
  1803. if (m < nr)
  1804. printf("%.2x", temp[m]);
  1805. else
  1806. printf("%s", fault_chars[fault_type]);
  1807. }
  1808. if (m <= 8)
  1809. printf(" ");
  1810. for (; m < 16; ++m)
  1811. printf(" ");
  1812. printf(" |");
  1813. for (m = 0; m < r; ++m) {
  1814. if (m < nr) {
  1815. c = temp[m];
  1816. putchar(' ' <= c && c <= '~'? c: '.');
  1817. } else
  1818. putchar(' ');
  1819. }
  1820. n -= r;
  1821. for (; m < 16; ++m)
  1822. putchar(' ');
  1823. printf("|\n");
  1824. if (nr < r)
  1825. break;
  1826. }
  1827. }
  1828. int
  1829. ppc_inst_dump(unsigned long adr, long count, int praddr)
  1830. {
  1831. int nr, dotted;
  1832. unsigned long first_adr;
  1833. unsigned long inst, last_inst = 0;
  1834. unsigned char val[4];
  1835. dotted = 0;
  1836. for (first_adr = adr; count > 0; --count, adr += 4) {
  1837. nr = mread(adr, val, 4);
  1838. if (nr == 0) {
  1839. if (praddr) {
  1840. const char *x = fault_chars[fault_type];
  1841. printf("%.16lx %s%s%s%s\n", adr, x, x, x, x);
  1842. }
  1843. break;
  1844. }
  1845. inst = GETWORD(val);
  1846. if (adr > first_adr && inst == last_inst) {
  1847. if (!dotted) {
  1848. printf(" ...\n");
  1849. dotted = 1;
  1850. }
  1851. continue;
  1852. }
  1853. dotted = 0;
  1854. last_inst = inst;
  1855. if (praddr)
  1856. printf("%.16lx %.8x", adr, inst);
  1857. printf("\t");
  1858. print_insn_powerpc(inst, adr, 0); /* always returns 4 */
  1859. printf("\n");
  1860. }
  1861. return adr - first_adr;
  1862. }
  1863. void
  1864. print_address(unsigned long addr)
  1865. {
  1866. xmon_print_symbol(addr, "\t# ", "");
  1867. }
  1868. /*
  1869. * Memory operations - move, set, print differences
  1870. */
  1871. static unsigned long mdest; /* destination address */
  1872. static unsigned long msrc; /* source address */
  1873. static unsigned long mval; /* byte value to set memory to */
  1874. static unsigned long mcount; /* # bytes to affect */
  1875. static unsigned long mdiffs; /* max # differences to print */
  1876. void
  1877. memops(int cmd)
  1878. {
  1879. scanhex((void *)&mdest);
  1880. if( termch != '\n' )
  1881. termch = 0;
  1882. scanhex((void *)(cmd == 's'? &mval: &msrc));
  1883. if( termch != '\n' )
  1884. termch = 0;
  1885. scanhex((void *)&mcount);
  1886. switch( cmd ){
  1887. case 'm':
  1888. memmove((void *)mdest, (void *)msrc, mcount);
  1889. break;
  1890. case 's':
  1891. memset((void *)mdest, mval, mcount);
  1892. break;
  1893. case 'd':
  1894. if( termch != '\n' )
  1895. termch = 0;
  1896. scanhex((void *)&mdiffs);
  1897. memdiffs((unsigned char *)mdest, (unsigned char *)msrc, mcount, mdiffs);
  1898. break;
  1899. }
  1900. }
  1901. void
  1902. memdiffs(unsigned char *p1, unsigned char *p2, unsigned nb, unsigned maxpr)
  1903. {
  1904. unsigned n, prt;
  1905. prt = 0;
  1906. for( n = nb; n > 0; --n )
  1907. if( *p1++ != *p2++ )
  1908. if( ++prt <= maxpr )
  1909. printf("%.16x %.2x # %.16x %.2x\n", p1 - 1,
  1910. p1[-1], p2 - 1, p2[-1]);
  1911. if( prt > maxpr )
  1912. printf("Total of %d differences\n", prt);
  1913. }
  1914. static unsigned mend;
  1915. static unsigned mask;
  1916. void
  1917. memlocate(void)
  1918. {
  1919. unsigned a, n;
  1920. unsigned char val[4];
  1921. last_cmd = "ml";
  1922. scanhex((void *)&mdest);
  1923. if (termch != '\n') {
  1924. termch = 0;
  1925. scanhex((void *)&mend);
  1926. if (termch != '\n') {
  1927. termch = 0;
  1928. scanhex((void *)&mval);
  1929. mask = ~0;
  1930. if (termch != '\n') termch = 0;
  1931. scanhex((void *)&mask);
  1932. }
  1933. }
  1934. n = 0;
  1935. for (a = mdest; a < mend; a += 4) {
  1936. if (mread(a, val, 4) == 4
  1937. && ((GETWORD(val) ^ mval) & mask) == 0) {
  1938. printf("%.16x: %.16x\n", a, GETWORD(val));
  1939. if (++n >= 10)
  1940. break;
  1941. }
  1942. }
  1943. }
  1944. static unsigned long mskip = 0x1000;
  1945. static unsigned long mlim = 0xffffffff;
  1946. void
  1947. memzcan(void)
  1948. {
  1949. unsigned char v;
  1950. unsigned a;
  1951. int ok, ook;
  1952. scanhex(&mdest);
  1953. if (termch != '\n') termch = 0;
  1954. scanhex(&mskip);
  1955. if (termch != '\n') termch = 0;
  1956. scanhex(&mlim);
  1957. ook = 0;
  1958. for (a = mdest; a < mlim; a += mskip) {
  1959. ok = mread(a, &v, 1);
  1960. if (ok && !ook) {
  1961. printf("%.8x .. ", a);
  1962. fflush(stdout);
  1963. } else if (!ok && ook)
  1964. printf("%.8x\n", a - mskip);
  1965. ook = ok;
  1966. if (a + mskip < a)
  1967. break;
  1968. }
  1969. if (ook)
  1970. printf("%.8x\n", a - mskip);
  1971. }
  1972. /* Input scanning routines */
  1973. int
  1974. skipbl(void)
  1975. {
  1976. int c;
  1977. if( termch != 0 ){
  1978. c = termch;
  1979. termch = 0;
  1980. } else
  1981. c = inchar();
  1982. while( c == ' ' || c == '\t' )
  1983. c = inchar();
  1984. return c;
  1985. }
  1986. #define N_PTREGS 44
  1987. static char *regnames[N_PTREGS] = {
  1988. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  1989. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  1990. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  1991. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
  1992. "pc", "msr", "or3", "ctr", "lr", "xer", "ccr", "softe",
  1993. "trap", "dar", "dsisr", "res"
  1994. };
  1995. int
  1996. scanhex(unsigned long *vp)
  1997. {
  1998. int c, d;
  1999. unsigned long v;
  2000. c = skipbl();
  2001. if (c == '%') {
  2002. /* parse register name */
  2003. char regname[8];
  2004. int i;
  2005. for (i = 0; i < sizeof(regname) - 1; ++i) {
  2006. c = inchar();
  2007. if (!isalnum(c)) {
  2008. termch = c;
  2009. break;
  2010. }
  2011. regname[i] = c;
  2012. }
  2013. regname[i] = 0;
  2014. for (i = 0; i < N_PTREGS; ++i) {
  2015. if (strcmp(regnames[i], regname) == 0) {
  2016. if (xmon_regs == NULL) {
  2017. printf("regs not available\n");
  2018. return 0;
  2019. }
  2020. *vp = ((unsigned long *)xmon_regs)[i];
  2021. return 1;
  2022. }
  2023. }
  2024. printf("invalid register name '%%%s'\n", regname);
  2025. return 0;
  2026. }
  2027. /* skip leading "0x" if any */
  2028. if (c == '0') {
  2029. c = inchar();
  2030. if (c == 'x') {
  2031. c = inchar();
  2032. } else {
  2033. d = hexdigit(c);
  2034. if (d == EOF) {
  2035. termch = c;
  2036. *vp = 0;
  2037. return 1;
  2038. }
  2039. }
  2040. } else if (c == '$') {
  2041. int i;
  2042. for (i=0; i<63; i++) {
  2043. c = inchar();
  2044. if (isspace(c)) {
  2045. termch = c;
  2046. break;
  2047. }
  2048. tmpstr[i] = c;
  2049. }
  2050. tmpstr[i++] = 0;
  2051. *vp = 0;
  2052. if (setjmp(bus_error_jmp) == 0) {
  2053. catch_memory_errors = 1;
  2054. sync();
  2055. *vp = kallsyms_lookup_name(tmpstr);
  2056. sync();
  2057. }
  2058. catch_memory_errors = 0;
  2059. if (!(*vp)) {
  2060. printf("unknown symbol '%s'\n", tmpstr);
  2061. return 0;
  2062. }
  2063. return 1;
  2064. }
  2065. d = hexdigit(c);
  2066. if (d == EOF) {
  2067. termch = c;
  2068. return 0;
  2069. }
  2070. v = 0;
  2071. do {
  2072. v = (v << 4) + d;
  2073. c = inchar();
  2074. d = hexdigit(c);
  2075. } while (d != EOF);
  2076. termch = c;
  2077. *vp = v;
  2078. return 1;
  2079. }
  2080. void
  2081. scannl(void)
  2082. {
  2083. int c;
  2084. c = termch;
  2085. termch = 0;
  2086. while( c != '\n' )
  2087. c = inchar();
  2088. }
  2089. int
  2090. hexdigit(int c)
  2091. {
  2092. if( '0' <= c && c <= '9' )
  2093. return c - '0';
  2094. if( 'A' <= c && c <= 'F' )
  2095. return c - ('A' - 10);
  2096. if( 'a' <= c && c <= 'f' )
  2097. return c - ('a' - 10);
  2098. return EOF;
  2099. }
  2100. void
  2101. getstring(char *s, int size)
  2102. {
  2103. int c;
  2104. c = skipbl();
  2105. do {
  2106. if( size > 1 ){
  2107. *s++ = c;
  2108. --size;
  2109. }
  2110. c = inchar();
  2111. } while( c != ' ' && c != '\t' && c != '\n' );
  2112. termch = c;
  2113. *s = 0;
  2114. }
  2115. static char line[256];
  2116. static char *lineptr;
  2117. void
  2118. flush_input(void)
  2119. {
  2120. lineptr = NULL;
  2121. }
  2122. int
  2123. inchar(void)
  2124. {
  2125. if (lineptr == NULL || *lineptr == 0) {
  2126. if (fgets(line, sizeof(line), stdin) == NULL) {
  2127. lineptr = NULL;
  2128. return EOF;
  2129. }
  2130. lineptr = line;
  2131. }
  2132. return *lineptr++;
  2133. }
  2134. void
  2135. take_input(char *str)
  2136. {
  2137. lineptr = str;
  2138. }
  2139. static void
  2140. symbol_lookup(void)
  2141. {
  2142. int type = inchar();
  2143. unsigned long addr;
  2144. static char tmp[64];
  2145. switch (type) {
  2146. case 'a':
  2147. if (scanhex(&addr))
  2148. xmon_print_symbol(addr, ": ", "\n");
  2149. termch = 0;
  2150. break;
  2151. case 's':
  2152. getstring(tmp, 64);
  2153. if (setjmp(bus_error_jmp) == 0) {
  2154. catch_memory_errors = 1;
  2155. sync();
  2156. addr = kallsyms_lookup_name(tmp);
  2157. if (addr)
  2158. printf("%s: %lx\n", tmp, addr);
  2159. else
  2160. printf("Symbol '%s' not found.\n", tmp);
  2161. sync();
  2162. }
  2163. catch_memory_errors = 0;
  2164. termch = 0;
  2165. break;
  2166. }
  2167. }
  2168. /* Print an address in numeric and symbolic form (if possible) */
  2169. static void xmon_print_symbol(unsigned long address, const char *mid,
  2170. const char *after)
  2171. {
  2172. char *modname;
  2173. const char *name = NULL;
  2174. unsigned long offset, size;
  2175. printf("%.16lx", address);
  2176. if (setjmp(bus_error_jmp) == 0) {
  2177. catch_memory_errors = 1;
  2178. sync();
  2179. name = kallsyms_lookup(address, &size, &offset, &modname,
  2180. tmpstr);
  2181. sync();
  2182. /* wait a little while to see if we get a machine check */
  2183. __delay(200);
  2184. }
  2185. catch_memory_errors = 0;
  2186. if (name) {
  2187. printf("%s%s+%#lx/%#lx", mid, name, offset, size);
  2188. if (modname)
  2189. printf(" [%s]", modname);
  2190. }
  2191. printf("%s", after);
  2192. }
  2193. static void debug_trace(void)
  2194. {
  2195. unsigned long val, cmd, on;
  2196. cmd = skipbl();
  2197. if (cmd == '\n') {
  2198. /* show current state */
  2199. unsigned long i;
  2200. printf("ppc64_debug_switch = 0x%lx\n", ppc64_debug_switch);
  2201. for (i = 0; i < PPCDBG_NUM_FLAGS ;i++) {
  2202. on = PPCDBG_BITVAL(i) & ppc64_debug_switch;
  2203. printf("%02x %s %12s ", i, on ? "on " : "off", trace_names[i] ? trace_names[i] : "");
  2204. if (((i+1) % 3) == 0)
  2205. printf("\n");
  2206. }
  2207. printf("\n");
  2208. return;
  2209. }
  2210. while (cmd != '\n') {
  2211. on = 1; /* default if no sign given */
  2212. while (cmd == '+' || cmd == '-') {
  2213. on = (cmd == '+');
  2214. cmd = inchar();
  2215. if (cmd == ' ' || cmd == '\n') { /* Turn on or off based on + or - */
  2216. ppc64_debug_switch = on ? PPCDBG_ALL:PPCDBG_NONE;
  2217. printf("Setting all values to %s...\n", on ? "on" : "off");
  2218. if (cmd == '\n') return;
  2219. else cmd = skipbl();
  2220. }
  2221. else
  2222. termch = cmd;
  2223. }
  2224. termch = cmd; /* not +/- ... let scanhex see it */
  2225. scanhex((void *)&val);
  2226. if (val >= 64) {
  2227. printf("Value %x out of range:\n", val);
  2228. return;
  2229. }
  2230. if (on) {
  2231. ppc64_debug_switch |= PPCDBG_BITVAL(val);
  2232. printf("enable debug %x %s\n", val, trace_names[val] ? trace_names[val] : "");
  2233. } else {
  2234. ppc64_debug_switch &= ~PPCDBG_BITVAL(val);
  2235. printf("disable debug %x %s\n", val, trace_names[val] ? trace_names[val] : "");
  2236. }
  2237. cmd = skipbl();
  2238. }
  2239. }
  2240. static void dump_slb(void)
  2241. {
  2242. int i;
  2243. unsigned long tmp;
  2244. printf("SLB contents of cpu %x\n", smp_processor_id());
  2245. for (i = 0; i < SLB_NUM_ENTRIES; i++) {
  2246. asm volatile("slbmfee %0,%1" : "=r" (tmp) : "r" (i));
  2247. printf("%02d %016lx ", i, tmp);
  2248. asm volatile("slbmfev %0,%1" : "=r" (tmp) : "r" (i));
  2249. printf("%016lx\n", tmp);
  2250. }
  2251. }
  2252. static void dump_stab(void)
  2253. {
  2254. int i;
  2255. unsigned long *tmp = (unsigned long *)get_paca()->stab_addr;
  2256. printf("Segment table contents of cpu %x\n", smp_processor_id());
  2257. for (i = 0; i < PAGE_SIZE/16; i++) {
  2258. unsigned long a, b;
  2259. a = *tmp++;
  2260. b = *tmp++;
  2261. if (a || b) {
  2262. printf("%03d %016lx ", i, a);
  2263. printf("%016lx\n", b);
  2264. }
  2265. }
  2266. }
  2267. void xmon_init(int enable)
  2268. {
  2269. if (enable) {
  2270. __debugger = xmon;
  2271. __debugger_ipi = xmon_ipi;
  2272. __debugger_bpt = xmon_bpt;
  2273. __debugger_sstep = xmon_sstep;
  2274. __debugger_iabr_match = xmon_iabr_match;
  2275. __debugger_dabr_match = xmon_dabr_match;
  2276. __debugger_fault_handler = xmon_fault_handler;
  2277. } else {
  2278. __debugger = NULL;
  2279. __debugger_ipi = NULL;
  2280. __debugger_bpt = NULL;
  2281. __debugger_sstep = NULL;
  2282. __debugger_iabr_match = NULL;
  2283. __debugger_dabr_match = NULL;
  2284. __debugger_fault_handler = NULL;
  2285. }
  2286. }
  2287. void dump_segments(void)
  2288. {
  2289. if (cpu_has_feature(CPU_FTR_SLB))
  2290. dump_slb();
  2291. else
  2292. dump_stab();
  2293. }