xmon.c 54 KB

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