xmon.c 55 KB

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