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