xmon.c 53 KB

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